forked from NOAA-EMC/WW3-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding window_mask.py for variable unstructured mesh, and revising th…
…e spacing.py and ocn_ww3.py to read the mask file, addressing issue NOAA-EMC#55
- Loading branch information
Showing
4 changed files
with
141 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
""" Jigsaw meshes for WW3 with global bathymetry | ||
""" | ||
|
||
# Authors: Ali Salimi, Darren | ||
|
||
# This script will create mesh spacing based on user defined windows in json format. | ||
|
||
import numpy as np | ||
import netCDF4 as nc | ||
import argparse | ||
import json | ||
|
||
def create_mask_file(windows): | ||
# Open the data file | ||
data = nc.Dataset("RTopo_2_0_4_GEBCO_v2023_60sec_pixel.nc", "r") | ||
|
||
# Extract longitude and latitude arrays | ||
xlon = np.asarray(data["lon"][:]) | ||
ylat = np.asarray(data["lat"][:]) | ||
elev = np.asarray(data["bed_elevation"][:], dtype=np.float32) + np.asarray(data["ice_thickness"][:], dtype=np.float32) | ||
|
||
# Compute midpoints for longitude and latitude | ||
xmid = 0.5 * (xlon[:-1] + xlon[+1:]) | ||
ymid = 0.5 * (ylat[:-1] + ylat[+1:]) | ||
|
||
# Create a meshgrid of midpoints | ||
xmat, ymat = np.meshgrid(xmid, ymid) | ||
|
||
# Initialize scal with a default condition | ||
#scal = np.where(ymat > 50, 9, np.where(ymat < -20, 30, 20)) | ||
|
||
# Define the boundaries and scaling values | ||
upper_bound = 50 | ||
middle_bound = -20 | ||
lower_bound = -90 | ||
scale_north = 9 | ||
scale_middle = 20 | ||
scale_south_upper = 30 | ||
scale_south_lower = 9 | ||
|
||
# Calculate the scaling using conditions | ||
scal = np.where(ymat > upper_bound, | ||
scale_north, # Use 9 km for ymat > 50 | ||
np.where(ymat > middle_bound, | ||
scale_middle, # Use 20 km for -20 < ymat <= 50 | ||
# Gradual decrease from 30 km to 9 km as latitude decreases from -20 to -90 | ||
scale_south_upper + (scale_south_lower - scale_south_upper) * | ||
(ymat - middle_bound) / (lower_bound - middle_bound) | ||
)) | ||
|
||
# Process each window | ||
for window in windows: | ||
# Apply the 'hshr' value for the current window where conditions are met | ||
window_mask_lon = np.logical_and(xmat >= window["min_lon"], xmat <= window["max_lon"]) | ||
window_mask_lat = np.logical_and(ymat >= window["min_lat"], ymat <= window["max_lat"]) | ||
window_mask = np.logical_and(window_mask_lon, window_mask_lat) | ||
|
||
mask_elevation = np.logical_and(elev >= -4., elev <= +8.) | ||
mask_final = np.logical_and(mask_elevation, window_mask) | ||
scal[mask_final] = window["hshr"] | ||
|
||
# Create a new NetCDF file to store the mask | ||
data_out = nc.Dataset("wmask.nc", "w") | ||
data_out.createDimension("nlon", xmid.size) | ||
data_out.createDimension("nlat", ymid.size) | ||
if "val" not in data_out.variables.keys(): | ||
data_out.createVariable("val", "f4", ("nlat", "nlon")) | ||
data_out["val"][:, :] = scal | ||
data_out.close() | ||
|
||
def parse_windows_from_args(): | ||
parser = argparse.ArgumentParser(description='Create a mask file with multiple windows.') | ||
parser.add_argument('--windows', type=str, required=True, | ||
help='JSON string with window definitions. Example: \'[{"min_lon": -130, "max_lon": -64, "min_lat": 24.5, "max_lat": 47.5, "hshr": 5}]\'') | ||
|
||
args = parser.parse_args() | ||
windows = json.loads(args.windows) | ||
return windows | ||
|
||
if __name__ == "__main__": | ||
windows = parse_windows_from_args() | ||
create_mask_file(windows) | ||
|