forked from MPAS-Dev/MPAS-Tools
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconversion.py
More file actions
214 lines (165 loc) · 6.86 KB
/
conversion.py
File metadata and controls
214 lines (165 loc) · 6.86 KB
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
import os
import xarray
from tempfile import TemporaryDirectory
import shutil
import mpas_tools.io
from mpas_tools.io import write_netcdf
from mpas_tools.logging import check_call
def convert(dsIn, graphInfoFileName=None, logger=None, dir=None):
"""
Use ``MpasMeshConverter.x`` to convert an input mesh to a valid MPAS
mesh that is fully compliant with the MPAS mesh specification.
https://mpas-dev.github.io/files/documents/MPAS-MeshSpec.pdf
Parameters
----------
dsIn : xarray.Dataset
A data set to convert
graphInfoFileName : str, optional
A file path (relative or absolute) where the graph file (typically
``graph.info`` should be written out. By default, ``graph.info`` is
not saved.
logger : logging.Logger, optional
A logger for the output if not stdout
dir : str, optional
A directory in which a temporary directory will be added with files
produced during conversion and then deleted upon completion.
Returns
-------
dsOut : xarray.Dataset
The MPAS mesh
"""
if dir is not None:
dir = os.path.abspath(dir)
with TemporaryDirectory(dir=dir) as tempdir:
inFileName = '{}/mesh_in.nc'.format(tempdir)
write_netcdf(dsIn, inFileName)
outFileName = '{}/mesh_out.nc'.format(tempdir)
if graphInfoFileName is not None:
graphInfoFileName = os.path.abspath(graphInfoFileName)
outDir = os.path.dirname(outFileName)
check_call(['MpasMeshConverter.x', inFileName, outFileName],
logger)
dsOut = xarray.open_dataset(outFileName)
dsOut.load()
if graphInfoFileName is not None:
shutil.copyfile('{}/graph.info'.format(outDir),
graphInfoFileName)
return dsOut
def cull(dsIn, dsMask=None, dsInverse=None, dsPreserve=None,
graphInfoFileName=None, logger=None, dir=None):
"""
Use ``MpasCellCuller.x`` to cull cells from a mesh based on the
``cullCell`` field in the input file or DataSet and/or the provided masks.
``cullCell``, dsMask and dsInverse are merged together so that the final
mask is the union of these 3. The preserve mask is then used to determine
where cells should *not* be culled.
Parameters
----------
dsIn : xarray.Dataset
A data set to cull, possibly with a ``cullCell`` field set to one where
cells should be removed
dsMask : xarray.Dataset or list, optional
A data set (or data sets) with region masks that are 1 where cells
should be culled
dsInverse : xarray.Dataset or list, optional
A data set (or data sets) with region masks that are 0 where cells
should be culled
dsPreserve : xarray.Dataset or list, optional
A data set (or data sets) with region masks that are 1 where cells
should *not* be culled
graphInfoFileName : str, optional
A file path (relative or absolute) where the graph file (typically
``culled_graph.info`` should be written out. By default,
``culled_graph.info`` is not saved.
logger : logging.Logger, optional
A logger for the output if not stdout
dir : str, optional
A directory in which a temporary directory will be added with files
produced during cell culling and then deleted upon completion.
Returns
-------
dsOut : xarray.Dataset
The culled mesh
"""
if dir is not None:
dir = os.path.abspath(dir)
with TemporaryDirectory(dir=dir) as tempdir:
inFileName = '{}/ds_in.nc'.format(tempdir)
write_netcdf(dsIn, inFileName)
outFileName = '{}/ds_out.nc'.format(tempdir)
args = ['MpasCellCuller.x', inFileName, outFileName]
if dsMask is not None:
if not isinstance(dsMask, list):
dsMask = [dsMask]
for index, ds in enumerate(dsMask):
fileName = '{}/mask{}.nc'.format(tempdir, index)
write_netcdf(ds, fileName)
args.extend(['-m', fileName])
if dsInverse is not None:
if not isinstance(dsInverse, list):
dsInverse = [dsInverse]
for index, ds in enumerate(dsInverse):
fileName = '{}/inverse{}.nc'.format(tempdir, index)
write_netcdf(ds, fileName)
args.extend(['-i', fileName])
if dsPreserve is not None:
if not isinstance(dsPreserve, list):
dsPreserve = [dsPreserve]
for index, ds in enumerate(dsPreserve):
fileName = '{}/preserve{}.nc'.format(tempdir, index)
write_netcdf(ds, fileName)
args.extend(['-p', fileName])
if graphInfoFileName is not None:
graphInfoFileName = os.path.abspath(graphInfoFileName)
outDir = os.path.dirname(outFileName)
check_call(args=args, logger=logger)
dsOut = xarray.open_dataset(outFileName)
dsOut.load()
if graphInfoFileName is not None:
shutil.copyfile('{}/culled_graph.info'.format(outDir),
graphInfoFileName)
return dsOut
def mask(dsMesh, fcMask=None, logger=None, dir=None, cores=1):
"""
Use ``compute_mpas_region_masks`` to create a set of region masks either
from mask feature collections
Parameters
----------
dsMesh : xarray.Dataset, optional
An MPAS mesh on which the masks should be created
fcMask : geometric_features.FeatureCollection, optional
A feature collection containing features to use to create the mask
logger : logging.Logger, optional
A logger for the output if not stdout
dir : str, optional
A directory in which a temporary directory will be added with files
produced during mask creation and then deleted upon completion.
cores : int, optional
The number of cores to use for python multiprocessing
Returns
-------
dsMask : xarray.Dataset
The masks
"""
if dir is not None:
dir = os.path.abspath(dir)
with TemporaryDirectory(dir=dir) as tempdir:
inFileName = f'{tempdir}/mesh_in.nc'
write_netcdf(dsMesh, inFileName)
outFileName = f'{tempdir}/mask_out.nc'
geojsonFileName = f'{tempdir}/mask.geojson'
fcMask.to_geojson(geojsonFileName)
args = ['compute_mpas_region_masks',
'-m', inFileName,
'-o', outFileName,
'-g', geojsonFileName,
'-t', 'cell',
'--process_count', f'{cores}',
'--format', mpas_tools.io.default_format,
]
if mpas_tools.io.default_engine is not None:
args.extend(['--engine', mpas_tools.io.default_engine])
check_call(args=args, logger=logger)
dsOut = xarray.open_dataset(outFileName)
dsOut.load()
return dsOut