Skip to content

Commit bf56da8

Browse files
Fix up target_cells for rof weights generation (#137)
* Fix up target_cells for rof weights generation * Use where with boolean arays for readability Co-authored-by: Dougie Squire <dougie.squire@anu.edu.au> * Update mesh_generation/generate_rof_weights.py Co-authored-by: Dougie Squire <dougie.squire@anu.edu.au> --------- Co-authored-by: Dougie Squire <dougie.squire@anu.edu.au>
1 parent 0e71249 commit bf56da8

1 file changed

Lines changed: 9 additions & 6 deletions

File tree

mesh_generation/generate_rof_weights.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,19 +99,22 @@ def drof_remapping_weights(mesh_filename, weights_filename, nx, ny, global_attrs
9999
land_neighbours = binary_dilation(mask_2d == 0)
100100

101101
# target for runoff is ocean cells which are adjacent land
102-
target_cells = (land_neighbours & mask_2d).flatten()
102+
target_cells = ((land_neighbours & mask_2d) == 1).flatten()
103103

104-
# Find index for all target cells
105-
mask_target = (mod_mesh_ds.elementCount * target_cells).astype("int")
104+
# convert back to a DataArray
105+
target_cells_da = xr.DataArray(target_cells, dims="elementCount")
106106

107107
# Haversine distances expect lat first, lon second, so index coordDim backwards
108108
center_coords_rad = deg2rad(mod_mesh_ds.centerCoords.isel(coordDim=[1, 0]))
109109

110110
# Make a BallTree from the ocean cells
111111
mask_tree = BallTree(
112-
center_coords_rad.isel(elementCount=mask_target), metric="haversine"
112+
center_coords_rad.where(target_cells_da, drop=True), metric="haversine"
113113
)
114114

115+
# Index for the target_cells (other=0 preserves integer dtype)
116+
target_cells_i = mod_mesh_ds.elementCount.where(target_cells_da, other=0, drop=True)
117+
115118
# Using the Tree, look up the nearest ocean cell to every destination grid cell in our weights file. Note our
116119
# weights are indexed from 1 (i.e. Fortran style) but xarray starts from 0 (i.e. python style), so subract one from
117120
# our destination grid cell indices.
@@ -120,7 +123,7 @@ def drof_remapping_weights(mesh_filename, weights_filename, nx, ny, global_attrs
120123
center_coords_rad.isel(elementCount=(weights_ds.row - 1)), return_distance=False
121124
)
122125

123-
new_row = mask_target[ii[:, 0]] + 1
126+
new_row = target_cells_i[ii[:, 0]] + 1
124127

125128
# Get the mesh element areas and adjust:
126129
# n.b. per CMEPS we are using the internally calculated areas, not the user provided ones.
@@ -191,7 +194,7 @@ def main():
191194
this_file = os.path.normpath(__file__)
192195

193196
# Add some info about how the file was generated
194-
runcmd = f"python3 {os.path.basename(this_file)} --mesh-filename={mesh_filename} --nx={args.nx} --ny={args.ny} --weights_filename={weights_filename} "
197+
runcmd = f"python3 {os.path.basename(this_file)} --mesh_filename={mesh_filename} --nx={args.nx} --ny={args.ny} --weights_filename={weights_filename} "
195198

196199
global_attrs = {"history": get_provenance_metadata(this_file, runcmd)}
197200

0 commit comments

Comments
 (0)