Skip to content

Commit

Permalink
bugfix in reading of subgrid netcdf file
Browse files Browse the repository at this point in the history
  • Loading branch information
roeldegoede committed Sep 4, 2024
1 parent 12f1081 commit cdfb0b4
Showing 1 changed file with 39 additions and 15 deletions.
54 changes: 39 additions & 15 deletions hydromt_sfincs/subgrid.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,9 @@ def read(self, file_name, mask):
# find indices of active cells
index_nm, index_mu1, index_nu1 = utils.find_uv_indices(mask)
active_indices = np.where(index_nm > -1)[0]
active_indices_u = np.where(index_mu1 > -1)[0]
active_indices_v = np.where(index_nu1 > -1)[0]

# convert 1D indices to 2D indices
active_z = np.unravel_index(active_indices, grid_dim, order="F")
active_u = np.unravel_index(active_indices_u, grid_dim, order="F")
active_v = np.unravel_index(active_indices_v, grid_dim, order="F")
active_cells = np.unravel_index(active_indices, grid_dim, order="F")

# Initialize the data-arrays
# Z points
Expand Down Expand Up @@ -96,32 +92,60 @@ def read(self, file_name, mask):

# Now read the data and add it to the data-arrays
# use index_nm of the active cells in the new dataset
self.z_zmin[active_z] = ds["z_zmin"].values.flatten()
self.z_zmax[active_z] = ds["z_zmax"].values.flatten()
self.z_volmax[active_z] = ds["z_volmax"].values.flatten()
self.z_zmin[active_cells] = ds["z_zmin"].values.flatten()
self.z_zmax[active_cells] = ds["z_zmax"].values.flatten()
self.z_volmax[active_cells] = ds["z_volmax"].values.flatten()
for ilevel in range(self.nlevels):
self.z_level[ilevel, active_z[0], active_z[1]] = ds["z_level"][
self.z_level[ilevel, active_cells[0], active_cells[1]] = ds["z_level"][
ilevel
].values.flatten()

# now use index_mu1 and index_nu1 to put the values of the active cells in the new dataset
var_list = ["zmin", "zmax", "ffit", "navg"]
for var in var_list:
uv_var = ds["uv_" + var].values.flatten()
self.u_zmin[active_u] = uv_var[index_mu1[active_indices_u]]
self.v_zmin[active_v] = uv_var[index_nu1[active_indices_v]]

# Dynamically set the attribute for self.u_var and self.v_var
u_attr_name = f"u_{var}"
v_attr_name = f"v_{var}"

# Retrieve the current attribute values
u_array = getattr(self, u_attr_name)
v_array = getattr(self, v_attr_name)

# Update only the active indices
u_array[active_cells] = uv_var[index_mu1[active_indices]]
v_array[active_cells] = uv_var[index_nu1[active_indices]]

# Set the modified arrays back to the attributes
setattr(self, u_attr_name, u_array)
setattr(self, v_attr_name, v_array)

var_list_levels = ["havg", "nrep", "pwet"]
for var in var_list_levels:
for ilevel in range(self.nlevels):
uv_var = ds["uv_" + var][ilevel].values.flatten()
self.u_havg[ilevel, active_u[0], active_u[1]] = uv_var[
index_mu1[active_indices_u]

# Dynamically set the attribute for self.u_var and self.v_var
u_attr_name = f"u_{var}"
v_attr_name = f"v_{var}"

# Retrieve the current attribute values
u_array = getattr(self, u_attr_name)
v_array = getattr(self, v_attr_name)

# Update only the active indices
u_array[ilevel, active_cells[0], active_cells[1]] = uv_var[
index_mu1[active_indices]
]
self.v_havg[ilevel, active_v[0], active_v[1]] = uv_var[
index_nu1[active_indices_v]
v_array[ilevel, active_cells[0], active_cells[1]] = uv_var[
index_nu1[active_indices]
]

# Set the modified arrays back to the attributes
setattr(self, u_attr_name, u_array)
setattr(self, v_attr_name, v_array)

# close the dataset
ds.close()

Expand Down

0 comments on commit cdfb0b4

Please sign in to comment.