Skip to content

Commit f13ce7c

Browse files
committed
Add geojson to mask icebergFjordMask
Adds an option to use a geojson file to define a region where icebergFjordMask is permanently 1
1 parent e4605a3 commit f13ce7c

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

landice/mesh_tools_li/interpolate_ecco_to_mali.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
import time
1515
import cftime
1616
import glob
17+
import json
18+
from shapely.geometry import shape
19+
from shapely.prepared import prep
20+
from shapely.geometry import Point
1721

1822
"""
1923
<< TO DO >>:
@@ -30,6 +34,7 @@ def __init__(self):
3034
parser.add_argument("--eccoDir", dest="eccoDir", required=True, help="Directory where ECCO files are stored. Should be only netcdf files in that directory", metavar="FILENAME")
3135
parser.add_argument("--maliMesh", dest="maliMesh", required=True, help="MALI mesh to be interpolated onto", metavar="FILENAME")
3236
parser.add_argument("--eccoVars", type=str, nargs='+', dest="eccoVars", required=True, help="ECCO variables to interpolate, current options are any combination of 'THETA', 'SALT', 'SIarea'")
37+
parser.add_argument("--geojson", dest="geojson", help="Optional geojson used to permanently define icebergFjordMask within a region")
3338
parser.add_argument("--maxDepth", type=int, dest="maxDepth", default=1500, help="Maximum depth in meters to include in the interpolation")
3439
parser.add_argument("--ntasks", dest="ntasks", type=str, help="Number of processors to use with ESMF_RegridWeightGen", default='128')
3540
parser.add_argument("--method", dest="method", type=str, help="Remapping method, either 'bilinear' or 'neareststod'", default='conserve')
@@ -352,19 +357,33 @@ def remap_ecco_to_mali(self):
352357

353358
print("Applying final edits to remapped file ...")
354359
# define icebergFjordMask from SIA
360+
ds_out = xr.open_dataset(remappedOutFile, decode_times=False, decode_cf=False, mode='r+')
355361
ds_sia = xr.open_dataset(siaRemappedOutFile)
356362
iac = ds_sia['iceAreaCell'].values
357363
icebergFjordMask = np.zeros(iac.shape)
358364
# Find cells where sea ice fraction exceeds threshold
359-
mask = iac > self.options.iceAreaThresh
360-
icebergFjordMask[mask] = 1
365+
seaice_mask = iac > self.options.iceAreaThresh
366+
icebergFjordMask[seaice_mask] = 1
367+
368+
# Use geojson to add any additional cells to icebergFjordMask
369+
if hasattr(self.options, "geojson"):
370+
with open(self.options.geojson) as f:
371+
geo = json.load(f)
372+
polygon = prep(shape(geo["features"][0]["geometry"]))
373+
374+
lonCell = ds_out['lon'][:].values - 360 # Convert to geojson longitude convention
375+
latCell = ds_out['lat'][:].values
376+
377+
points = [Point(xy) for xy in zip(lonCell, latCell)]
378+
geo_mask = np.array([polygon.contains(p) for p in points])
379+
icebergFjordMask[:,geo_mask] = 1
380+
361381
ifm = xr.DataArray(icebergFjordMask.astype('int32'), dims=("Time", "nCells"))
362382
sia = xr.DataArray(iac.astype('float32'), dims=("Time", "nCells"))
363383

364384
# During conservative remapping, some valid ocean cells are averaged with invalid ocean cells. Use
365385
# float version of orig3dOceanMask to identify these cells and remove. Resave orig3dOceanMask as int32
366386
# so MALI recognizes it
367-
ds_out = xr.open_dataset(remappedOutFile, decode_times=False, decode_cf=False, mode='r+')
368387
o3dm = ds_out['orig3dOceanMask']
369388
temp = ds_out['oceanTemperature']
370389
sal = ds_out['oceanSalinity']

0 commit comments

Comments
 (0)