From cdfb0b4ff2d0bf059cc8f0dc2b19b1ca982dc9aa Mon Sep 17 00:00:00 2001 From: roeldegoede Date: Wed, 4 Sep 2024 23:23:48 +0200 Subject: [PATCH] bugfix in reading of subgrid netcdf file --- hydromt_sfincs/subgrid.py | 54 ++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 15 deletions(-) diff --git a/hydromt_sfincs/subgrid.py b/hydromt_sfincs/subgrid.py index c76a8a1f..b40446b5 100644 --- a/hydromt_sfincs/subgrid.py +++ b/hydromt_sfincs/subgrid.py @@ -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 @@ -96,11 +92,11 @@ 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() @@ -108,20 +104,48 @@ def read(self, file_name, mask): 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()