-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_mesh.py
executable file
·422 lines (347 loc) · 13 KB
/
generate_mesh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# Copyright 2023 ACCESS-NRI and contributors. See the top-level COPYRIGHT file for details.
# SPDX-License-Identifier: Apache-2.0
# =========================================================================================
# Generate an ESMF mesh file from an input grid.
#
# To run:
# python generate_mesh.py --grid_type=<gridtype> --grid_filename=<grid_file> \
# --mask_filename=<mask_file> --mesh_filename=<output_file>
#
# This script currently supports two grid_types:
# - "mom" to generate a mesh representation of h-cells from a MOM supergrid
# - "latlon" to generate a mesh representation from lat/lon locations
# For more information, run `python generate_mesh.py -h`
#
# Contact:
# Dougie Squire <[email protected]>
#
# Dependencies:
# argparse, xarray, numpy and pandas
# =========================================================================================
import os
import subprocess
from datetime import datetime
import numpy as np
import xarray as xr
import pandas as pd
def is_git_repo():
"""
Return True/False depending on whether or not the current directory is a git repo.
"""
return subprocess.call(
['git', '-C', '.', 'status'],
stderr=subprocess.STDOUT,
stdout = open(os.devnull, 'w')
) == 0
def git_info():
"""
Return the git repo origin url, relative path to this file, and latest commit hash.
"""
url = subprocess.check_output(
["git", "remote", "get-url", "origin"]
).decode('ascii').strip()
top_level_dir = subprocess.check_output(
['git', 'rev-parse', '--show-toplevel']
).decode('ascii').strip()
rel_path = os.path.relpath(__file__, top_level_dir)
hash = subprocess.check_output(
['git', 'rev-parse', 'HEAD']
).decode('ascii').strip()
return url, rel_path, hash
class BaseGrid:
def __init__(self, x_centres, y_centres, x_corners, y_corners, area=None, mask=None, inputs=None):
"""
Initialise a mesh object
Parameters
----------
x_centres: len(elementCount) array-like
Longitudinal positions of the element centre coords
y_centres: len(elementCount) array-like
Latitudinal positions of the element centre coords
x_corners: (elementCount x 4) array-like
Longitudinal positions of the corner nodes of each element, ordered ll, lr, ur, ul
y_corners: (elementCount x 4) array-like
LongitLatitudinaludinal positions of the corner nodes of each element, ordered ll, lr, ur, ul
area: len(elementCount) array-like, optional
Areas of each element
mask: len(elementCount) array-like
Mask values for each element, optional
inputs: str or list of str, optional
Paths to the files used to create the grid
"""
self.x_centres = x_centres
self.y_centres = y_centres
self.x_corners = x_corners.flatten()
self.y_corners = y_corners.flatten()
self.area = area
self.mask = mask
if isinstance(inputs, str):
inputs = [inputs]
self.inputs = inputs
self.mesh = None
def create_mesh(self, wrap_lons=False, global_attrs=None):
"""
Create the mesh as an xarray Dataset
Parameters
----------
wrap_lons: boolean, optional
If True, wrap longitude values into the range between 0 and 360
global_attrs: dict
Global attributes to the mesh object
"""
if wrap_lons:
self.x_centres = (self.x_centres + 360) % 360
self.x_corners = (self.x_corners + 360) % 360
centres = np.stack((self.x_centres, self.y_centres), axis=1)
corners_df = pd.DataFrame({"x": self.x_corners, "y": self.y_corners})
# calculate indexes of corner nodes per element
elem_conn = (
corners_df.groupby(['x','y'], sort=False).ngroup()+1
).to_numpy().reshape((-1,4))
# calculate corner nodes
nodes = corners_df.drop_duplicates().to_numpy()
# create mask if we don't have one
if self.mask is None:
self.mask = np.ones_like(self.x_centres, dtype=np.int8)
# create a new dataset for the mesh
ds = xr.Dataset()
ds['nodeCoords'] = xr.DataArray(
nodes.astype(np.float64),
dims=('nodeCount', 'coordDim'),
attrs={'units': 'degrees'}
)
ds['elementConn'] = xr.DataArray(
elem_conn.astype(np.int32),
dims=('elementCount', 'maxNodePElement'),
attrs={'long_name': 'Node indices that define the element connectivity'}
)
ds['numElementConn'] = xr.DataArray(
4 * np.ones_like(self.x_centres, dtype=np.int32),
dims=('elementCount'),
attrs={'long_name': 'Number of nodes per element'}
)
ds['centerCoords'] = xr.DataArray(
centres.astype(np.float64),
dims=('elementCount', 'coordDim'),
attrs={'units': 'degrees'}
)
ds["elementMask"] = xr.DataArray(
self.mask.astype(np.int8),
dims=('elementCount'),
)
if self.area is not None:
ds["elementArea"] = xr.DataArray(
self.area.astype(np.float64),
dims=('elementCount'),
)
# force no _FillValue (for now)
for v in ds.variables:
if '_FillValue' not in ds[v].encoding:
ds[v].encoding['_FillValue'] = None
# add global attributes
ds.attrs = {
"gridType" : "unstructured mesh",
"timeGenerated": f"{datetime.now()}",
"created_by": f"{os.environ.get('USER')}"
}
if self.inputs:
ds.attrs["inputFile"] = ", ".join(self.inputs)
# add git info to history
if global_attrs:
ds.attrs |= global_attrs
self.mesh = ds
return self
def write(self, filename):
"""
Save the mesh to a file
"""
if self.mesh is None:
raise ValueError("Before writing, you must first create the mesh object using self.create_mesh()")
self.mesh.to_netcdf(filename)
class MomSuperGrid(BaseGrid):
def __init__(self, hgrid_filename, mask_filename=None):
"""
Initialise a mesh representation of h-cells from a MOM supergrid
Parameters
----------
hgrid_filename: str
Path to the MOM hgrid netcdf file
mask_filename: str, optional
Path to a netcdf file containing a mask corresponding to the MOM hgrid
"""
grid = xr.open_dataset(hgrid_filename)
inputs = [hgrid_filename]
if mask_filename:
mask = xr.open_dataset(mask_filename).mask.values.flatten()
inputs += [mask_filename]
else:
mask = None
# sum areas in elements
area = grid.area.values
area = (
area[::2, ::2] + area[1::2, ::2] + area[1::2, 1::2] + area[::2, 1::2]
).flatten()
x = grid.x.values
y = grid.y.values
# prep x corners
ll = x[:-2:2, :-2:2]
lr = x[:-2:2, 2::2]
ul = x[2::2, :-2:2]
ur = x[2::2, 2::2]
x_corners = np.stack((ll.flatten(), lr.flatten(), ur.flatten(), ul.flatten()), axis=1)
x_centres = x[1:-1:2, 1:-1:2].flatten()
# prep y corners
ll = y[:-2:2, :-2:2]
lr = y[:-2:2, 2::2]
ul = y[2::2, :-2:2]
ur = y[2::2, 2::2]
y_corners = np.stack((ll.flatten(), lr.flatten(), ur.flatten(), ul.flatten()), axis=1)
y_centres = y[1:-1:2, 1:-1:2].flatten()
super().__init__(
x_centres=x_centres,
y_centres=y_centres,
x_corners=x_corners,
y_corners=y_corners,
area=area,
mask=mask,
inputs=inputs,
)
class LatLonGrid(BaseGrid):
def __init__(self, grid_filename, mask_filename=None, lon_dim="lon", lat_dim="lat", area_var="area"):
"""
Initialise a mesh representation from lat/lon locations
Parameters
----------
grid_filename: str
Path to a netcdf file containing a lat/lon grid
mask_filename: str, optional
Path to a netcdf file containing a mask corresponding to the lat/lon grid
lon_dim: str, optional
The name of the longitude dimension
lat_dim: str, optional
The name of the latitude dimension
area_var: str, optional
The name of the area variable if one exists
"""
grid = xr.open_dataset(grid_filename, chunks=-1)
inputs = [grid_filename]
if mask_filename:
mask = xr.open_dataset(mask_filename).values.flatten()
inputs += [mask_filename]
else:
mask = None
if area_var in grid:
area = grid[area_var].values.flatten()
else:
area = None
x_centres = grid[lon_dim].values
y_centres = grid[lat_dim].values
has_lon_bounds = hasattr(grid[lon_dim], "bounds") and grid[lon_dim].bounds in grid
has_lat_bounds = hasattr(grid[lat_dim], "bounds") and grid[lat_dim].bounds in grid
if has_lon_bounds:
lon_bnds = grid[getattr(grid[lon_dim], "bounds")]
# flip and concat for ll, lr, ur, ul
x_corners = np.concatenate([lon_bnds.values, lon_bnds[...,::-1].values], axis=-1)
else:
# Average neighbouring cells to get bounds
ext = np.pad(x_centres, (1,), mode='reflect', reflect_type='odd')
bnds = (ext[:-1] + ext[1:]) / 2
# stack as ll, lr, ur, ul
x_corners = np.stack([bnds[:-1], bnds[1:], bnds[1:], bnds[:-1]], axis=1)
if has_lat_bounds:
lat_bnds = grid[getattr(grid[lat_dim], "bounds")]
# repeat for ll, lr, ur, ul
y_corners = np.repeat(lat_bnds.values, 2, axis=1)
else:
# Average neighbouring cells to get bounds
ext = np.pad(y_centres, (1,), mode='reflect', reflect_type='odd')
bnds = (ext[:-1] + ext[1:]) / 2
# stack as ll, lr, ur, ul
y_corners = np.stack([bnds[:-1], bnds[:-1], bnds[1:], bnds[1:]], axis=1)
# broadcast corners
x_corners, y_corners = np.broadcast_arrays(
np.expand_dims(x_corners, axis=0),
np.expand_dims(y_corners, axis=1)
)
x_corners = x_corners.reshape(-1, 4)
y_corners = y_corners.reshape(-1, 4)
# broadcast centres
x_centres, y_centres = np.broadcast_arrays(
np.expand_dims(x_centres, axis=0),
np.expand_dims(y_centres, axis=1)
)
x_centres = x_centres.flatten()
y_centres = y_centres.flatten()
super().__init__(
x_centres=x_centres,
y_centres=y_centres,
x_corners=x_corners,
y_corners=y_corners,
area=area,
mask=mask,
inputs=inputs,
)
gridtype_dispatch = {
"latlon": LatLonGrid,
"mom": MomSuperGrid,
}
def main():
parser = argparse.ArgumentParser(
description="Create an ESMF mesh file from a grid in a netcdf file."
)
parser.add_argument(
"--grid-type",
choices=gridtype_dispatch.keys(),
required=True,
help='The type of grid in the netcdf file.',
)
parser.add_argument(
"--wrap-lons",
default=False,
action="store_true",
help="Wrap longitude values into the range between 0 and 360."
)
parser.add_argument(
"--grid-filename",
type=str,
required=True,
help="The path to the netcdf file specifying the grid.",
)
parser.add_argument(
"--mask-filename",
type=str,
default=None,
help="The path to a netcdf file specifying the mask. If not passed, no mask is set in the mesh.",
)
parser.add_argument(
"--mesh-filename",
type=str,
required=True,
help="The path to the mesh file to create.",
)
args = parser.parse_args()
grid_type = args.grid_type
wrap_lons = args.wrap_lons
grid_filename = args.grid_filename
mask_filename = args.mask_filename
mesh_filename = args.mesh_filename
# Add some info about how the file was generated
runcmd = (
f"python3 {__file__} --grid-type={grid_type} --grid-filename={grid_filename} "
f"--mesh-filename={mesh_filename}"
)
if mask_filename:
runcmd += f" --mask-filename={mask_filename}"
if wrap_lons:
runcmd += f" --wrap-lons"
if is_git_repo():
url, rel_path, hash = git_info()
prepend = f"Created using commit {hash} of {rel_path} in {url}: "
else:
prepend = "Created using: "
global_attrs = {"history": prepend + runcmd}
mesh = gridtype_dispatch[grid_type](grid_filename, mask_filename)
mesh.create_mesh(wrap_lons=wrap_lons, global_attrs=global_attrs).write(mesh_filename)
if __name__ == "__main__":
import argparse
main()