Skip to content

Commit cb0758e

Browse files
committed
Move geojson bnds setup to greenland mesh.py
This commit moves the calculation of geom_points and geom_edges using a geojson of the gis contintental shelf extent to compass/landice/tests/greenland/mesh.py, instead of compass/landice/mesh.py. If this options is not being used, thn geom_points and geom_edges are calculated within build_cell_width, as per usual
1 parent 69fbf46 commit cb0758e

File tree

2 files changed

+76
-55
lines changed

2 files changed

+76
-55
lines changed

compass/landice/mesh.py

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import json
21
import os
32
import re
43
import sys
@@ -18,8 +17,6 @@
1817
from netCDF4 import Dataset
1918
from scipy.interpolate import NearestNDInterpolator, interpn
2019

21-
from compass.step import add_input_file
22-
2320

2421
def mpas_flood_fill(seed_mask, grow_mask, cellsOnCell, nEdgesOnCell,
2522
grow_iters=sys.maxsize):
@@ -520,7 +517,7 @@ def get_dist_to_edge_and_gl(self, thk, topg, x, y,
520517

521518

522519
def build_cell_width(self, section_name, gridded_dataset,
523-
flood_fill_start=[None, None]):
520+
flood_fill_start=[None, None], calc_geom_bnds=True):
524521
"""
525522
Determine MPAS mesh cell size based on user-defined density function.
526523
@@ -545,6 +542,11 @@ def build_cell_width(self, section_name, gridded_dataset,
545542
fill. Most cases will use ``[None, None]``, which will just start the
546543
flood fill in the center of the gridded dataset.
547544
545+
calc_geom_bnds : logical
546+
Option to calculate geom_points and geom_edges needed for jigsaw within
547+
build_cell_width. Default is to perform calculation, but the user may
548+
opt out if these are determined elsewhere (e.g., using a geojson file)
549+
548550
Returns
549551
-------
550552
cell_width : numpy.ndarray
@@ -583,49 +585,8 @@ def build_cell_width(self, section_name, gridded_dataset,
583585

584586
f.close()
585587

586-
# Get bounds defined by user, or use bound of gridded dataset
587-
if section.get('define_bnds_by_geojson') == 'True':
588-
# change file location either to compass or geometric_features
589-
add_input_file(filename='gis_contShelfExtent.geojson',
590-
package='compass.landice.tests.greenland',
591-
target='gis_contShelfExtent.geojson',
592-
database=None)
593-
594-
with open('gis_contShelfExtent.geojson', 'r') as meshMarginFile:
595-
geojson_data = json.load(meshMarginFile)
596-
597-
lon = []
598-
lat = []
599-
start_edge = []
600-
end_edge = []
601-
ct = 0
602-
603-
for feature in geojson_data['features']:
604-
geometry = feature['geometry']
605-
for coord in geometry['coordinates']:
606-
for sub_coord in coord:
607-
lon.append(sub_coord[0])
608-
lat.append(sub_coord[1])
609-
610-
start_edge.append(ct)
611-
end_edge.append(ct + 1)
612-
ct = ct + 1
613-
lon = np.array(lon)
614-
lat = np.array(lat)
615-
start_edge = np.array(start_edge)
616-
end_edge = np.array(end_edge)
617-
618-
start_edge[-1] = ct - 1
619-
end_edge[-1] = 0
620-
621-
geom_points = np.array([((lon[i], lat[i]), 0)
622-
for i in range(len(lat))],
623-
dtype=jigsawpy.jigsaw_msh_t.VERT2_t)
624-
625-
geom_edges = np.array([((start_edge[i], end_edge[i]), 0)
626-
for i in range(len(start_edge))],
627-
dtype=jigsawpy.jigsaw_msh_t.EDGE2_t)
628-
else:
588+
# If necessary, get bounds defined by user or use bound of gridded dataset
589+
if calc_geom_bnds:
629590
bnds = [np.min(x1), np.max(x1), np.min(y1), np.max(y1)]
630591
bnds_options = ['x_min', 'x_max', 'y_min', 'y_max']
631592
for index, option in enumerate(bnds_options):
@@ -655,8 +616,12 @@ def build_cell_width(self, section_name, gridded_dataset,
655616
flood_fill_iStart=flood_fill_start[0],
656617
flood_fill_jStart=flood_fill_start[1])
657618

658-
return (cell_width.astype('float64'), x1.astype('float64'),
659-
y1.astype('float64'), geom_points, geom_edges, flood_mask)
619+
if not calc_geom_bnds:
620+
return (cell_width.astype('float64'), x1.astype('float64'),
621+
y1.astype('float64'), flood_mask)
622+
else:
623+
return (cell_width.astype('float64'), x1.astype('float64'),
624+
y1.astype('float64'), geom_points, geom_edges, flood_mask)
660625

661626

662627
def build_mali_mesh(self, cell_width, x1, y1, geom_points,

compass/landice/tests/greenland/mesh.py

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
import os
23

4+
import jigsawpy
35
import numpy as np
46
import xarray as xr
57
from mpas_tools.scrip.from_mpas import scrip_from_mpas
@@ -31,7 +33,7 @@ def __init__(self, test_case):
3133
Parameters
3234
----------
3335
test_case : compass.TestCase
34-
The test case this step belongs to
36+
The test case this step belongs to/
3537
3638
"""
3739
super().__init__(test_case=test_case, name='mesh', cpus_per_task=128,
@@ -85,11 +87,21 @@ def run(self):
8587
source_gridded_dataset_2km = 'greenland_2km_2024_01_29.epsg3413.nc'
8688

8789
logger.info('calling build_cell_width')
88-
cell_width, x1, y1, geom_points, geom_edges, floodMask = \
89-
build_cell_width(
90-
self, section_name=section_name,
91-
gridded_dataset=source_gridded_dataset_2km,
92-
flood_fill_start=[100, 700])
90+
if section_gis.get("define_bnds_by_geojson") == 'True':
91+
geom_points, geom_edges = set_geojson_geom_points_and_edges(self)
92+
93+
cell_width, x1, y1, floodMask = \
94+
build_cell_width(self,
95+
section_name=section_name,
96+
gridded_dataset=source_gridded_dataset_2km,
97+
flood_fill_start=[100, 700],
98+
calc_geom_bnds=False)
99+
else:
100+
cell_width, x1, y1, geom_points, geom_edges, floodMask = \
101+
build_cell_width(
102+
self, section_name=section_name,
103+
gridded_dataset=source_gridded_dataset_2km,
104+
flood_fill_start=[100, 700])
93105

94106
# Now build the base mesh and perform the standard interpolation
95107
build_mali_mesh(
@@ -166,3 +178,47 @@ def run(self):
166178
ds["observedThicknessTendencyUncertainty"] = dHdtErr
167179
# Write the data to disk
168180
ds.to_netcdf(self.mesh_filename, 'a')
181+
182+
183+
def set_geojson_geom_points_and_edges(self):
184+
self.add_input_file(filename='gis_contShelfExtent.geojson',
185+
package='compass.landice.tests.greenland',
186+
target='gis_contShelfExtent.geojson',
187+
database=None)
188+
189+
with open('gis_contShelfExtent.geojson', 'r') as meshMarginFile:
190+
geojson_data = json.load(meshMarginFile)
191+
192+
x = []
193+
y = []
194+
start_edge = []
195+
end_edge = []
196+
ct = 0
197+
198+
for feature in geojson_data['features']:
199+
geometry = feature['geometry']
200+
for coord in geometry['coordinates']:
201+
for sub_coord in coord:
202+
x.append(sub_coord[0])
203+
y.append(sub_coord[1])
204+
205+
start_edge.append(ct)
206+
end_edge.append(ct + 1)
207+
ct = ct + 1
208+
x = np.array(x)
209+
y = np.array(y)
210+
start_edge = np.array(start_edge)
211+
end_edge = np.array(end_edge)
212+
213+
start_edge[-1] = ct - 1
214+
end_edge[-1] = 0
215+
216+
geom_points = np.array([((x[i], y[i]), 0)
217+
for i in range(len(y))],
218+
dtype=jigsawpy.jigsaw_msh_t.VERT2_t)
219+
220+
geom_edges = np.array([((start_edge[i], end_edge[i]), 0)
221+
for i in range(len(start_edge))],
222+
dtype=jigsawpy.jigsaw_msh_t.EDGE2_t)
223+
224+
return (geom_points, geom_edges)

0 commit comments

Comments
 (0)