Skip to content

Commit

Permalink
Merge pull request #346 from xylar/fix_tmp_dir
Browse files Browse the repository at this point in the history
Allow conversion tools to be called with a directory
  • Loading branch information
xylar authored Sep 5, 2020
2 parents bdd9a7e + d81d308 commit bf2d40b
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 30 deletions.
4 changes: 2 additions & 2 deletions compass/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "compass" %}
{% set version = "0.1.10" %}
{% set version = "0.1.11" %}
{% set build = 0 %}

{% if mpi == "nompi" %}
Expand Down Expand Up @@ -31,7 +31,7 @@ requirements:
run:
- python
- geometric_features 0.1.11
- mpas_tools 0.0.12
- mpas_tools 0.0.13
- jigsaw 0.9.12
- jigsawpy 0.2.1
- metis
Expand Down
3 changes: 3 additions & 0 deletions conda_package/docs/versions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Documentation On GitHub
`stable`_ `master`_
`v0.0.11`_ `0.0.11`_
`v0.0.12`_ `0.0.12`_
`v0.0.13`_ `0.0.13`_
================ ===============

.. _`stable`: ../stable/index.html
Expand All @@ -17,3 +18,5 @@ Documentation On GitHub
.. _`0.0.11`: https://github.com/MPAS-Dev/MPAS-Analysis/tree/0.0.11
.. _`v0.0.12`: ../0.0.12/index.html
.. _`0.0.12`: https://github.com/MPAS-Dev/MPAS-Analysis/tree/0.0.12
.. _`v0.0.13`: ../0.0.13/index.html
.. _`0.0.13`: https://github.com/MPAS-Dev/MPAS-Analysis/tree/0.0.13
2 changes: 1 addition & 1 deletion conda_package/mpas_tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
__version_info__ = (0, 0, 12)
__version_info__ = (0, 0, 13)
__version__ = '.'.join(str(vi) for vi in __version_info__)
51 changes: 32 additions & 19 deletions conda_package/mpas_tools/mesh/conversion.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
from __future__ import absolute_import, division, print_function, \
unicode_literals

import os
import xarray
import subprocess
from backports.tempfile import TemporaryDirectory
from tempfile import TemporaryDirectory
import shutil

from mpas_tools.io import write_netcdf


def convert(dsIn, graphInfoFileName=None, logger=None):
'''
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
Expand All @@ -29,13 +26,18 @@ def convert(dsIn, graphInfoFileName=None, logger=None):
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
'''

with TemporaryDirectory() as tempdir:
"""
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)

Expand Down Expand Up @@ -64,8 +66,8 @@ def convert(dsIn, graphInfoFileName=None, logger=None):


def cull(dsIn, dsMask=None, dsInverse=None, dsPreserve=None,
graphInfoFileName=None, logger=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
Expand Down Expand Up @@ -98,14 +100,19 @@ def cull(dsIn, dsMask=None, dsInverse=None, dsPreserve=None,
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
'''

with TemporaryDirectory() as tempdir:
"""
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)
Expand Down Expand Up @@ -158,8 +165,9 @@ def cull(dsIn, dsMask=None, dsInverse=None, dsPreserve=None,
return dsOut


def mask(dsMesh, fcMask=None, fcSeed=None, positiveLon=False, logger=None):
'''
def mask(dsMesh, fcMask=None, fcSeed=None, positiveLon=False, logger=None,
dir=None):
"""
Use ``MpasMaskCreator.x`` to create a set of region masks either from
mask feature collecitons or from seed points to be used to flood fill
Expand All @@ -178,14 +186,19 @@ def mask(dsMesh, fcMask=None, fcSeed=None, positiveLon=False, logger=None):
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.
Returns
-------
dsMask : ``xarray.Dataset``
The masks
'''

with TemporaryDirectory() as tempdir:
"""
if dir is not None:
dir = os.path.abspath(dir)
with TemporaryDirectory(dir=dir) as tempdir:
inFileName = '{}/mesh_in.nc'.format(tempdir)
write_netcdf(dsMesh, inFileName)
outFileName = '{}/mesh_out.nc'.format(tempdir)
Expand Down
9 changes: 7 additions & 2 deletions conda_package/mpas_tools/mesh/creation/build_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@


def build_spherical_mesh(cellWidth, lon, lat, earth_radius,
out_filename='base_mesh.nc', plot_cellWidth=True):
out_filename='base_mesh.nc', plot_cellWidth=True,
dir='./'):
"""
Build an MPAS mesh using JIGSAW with the given cell sizes as a function of
latitude and longitude.
Expand Down Expand Up @@ -46,6 +47,10 @@ def build_spherical_mesh(cellWidth, lon, lat, earth_radius,
plot_cellWidth : bool, optional
Whether to produce a plot of ``cellWidth``. If so, it will be written to
``cellWidthGlobal.png``.
dir : str, optional
A directory in which a temporary directory will be added with files
produced during mesh conversion and then deleted upon completion.
"""

da = xarray.DataArray(cellWidth,
Expand Down Expand Up @@ -92,7 +97,7 @@ def build_spherical_mesh(cellWidth, lon, lat, earth_radius,
sphere_radius=earth_radius)

print('Step 3. Convert from triangles to MPAS mesh')
write_netcdf(convert(xarray.open_dataset('mesh_triangles.nc')),
write_netcdf(convert(xarray.open_dataset('mesh_triangles.nc'), dir=dir),
out_filename)


Expand Down
9 changes: 7 additions & 2 deletions conda_package/mpas_tools/ocean/moc.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ def make_moc_basins_and_transects(gf, mesh_filename,
mask_and_transect_filename,
geojson_filename=None,
mask_filename=None,
logger=None):
logger=None,
dir=None):
"""
Builds features defining the ocean basins and southern transects used in
computing the meridional overturning circulation (MOC)
Expand All @@ -42,6 +43,10 @@ def make_moc_basins_and_transects(gf, mesh_filename,
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
-------
fc : ``FeatureCollection``
Expand All @@ -58,7 +63,7 @@ def make_moc_basins_and_transects(gf, mesh_filename,

dsMesh = xarray.open_dataset(mesh_filename)
dsMasks = mpas_tools.mesh.conversion.mask(dsMesh=dsMesh, fcMask=fcMOC,
logger=logger)
logger=logger, dir=dir)

if mask_filename is not None:
write_netcdf(dsMasks, mask_filename)
Expand Down
6 changes: 4 additions & 2 deletions conda_package/mpas_tools/viz/paraview_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2142,11 +2142,13 @@ def _cull_files(fc_region_mask, temp_dir, mesh_filename, time_file_names,
with xarray.open_dataset(mesh_filename) as ds_mesh:
ds_mesh = ds_mesh[mesh_vars]
print('Making a region mask file')
ds_mask = mask(dsMesh=ds_mesh, fcMask=fc_region_mask, logger=logger)
ds_mask = mask(dsMesh=ds_mesh, fcMask=fc_region_mask, logger=logger,
dir=temp_dir)
write_netcdf(ds_mask, '{}/mask.nc'.format(temp_dir))
print('Cropping mesh to region')
out_mesh_filename = '{}/mesh.nc'.format(temp_dir)
ds_culled = cull(dsIn=ds_mesh, dsInverse=ds_mask, logger=logger)
ds_culled = cull(dsIn=ds_mesh, dsInverse=ds_mask, logger=logger,
dir=temp_dir)
write_netcdf(ds_culled, out_mesh_filename)

region_masks = dict()
Expand Down
3 changes: 1 addition & 2 deletions conda_package/recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% set name = "mpas_tools" %}
{% set version = "0.0.12" %}
{% set version = "0.0.13" %}

package:
name: {{ name|lower }}
Expand Down Expand Up @@ -45,7 +45,6 @@ requirements:
- geometric_features
- pyevtk
- future
- backports.tempfile
- jigsaw >=0.9.12
- jigsawpy >=0.2.1
- pyflann
Expand Down

0 comments on commit bf2d40b

Please sign in to comment.