Skip to content

Commit

Permalink
Don't delete temp dir after convert() or cull()
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed Aug 17, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 548d139 commit 8bdcbbb
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions conda_package/mpas_tools/mesh/conversion.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os
import xarray
from tempfile import TemporaryDirectory
from tempfile import TemporaryDirectory, mkdtemp
import shutil

import mpas_tools.io
@@ -38,26 +38,26 @@ def convert(dsIn, graphInfoFileName=None, logger=None, dir=None):
"""
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)
tempdir = mkdtemp(dir=dir)
inFileName = '{}/mesh_in.nc'.format(tempdir)
write_netcdf(dsIn, inFileName)

if graphInfoFileName is not None:
graphInfoFileName = os.path.abspath(graphInfoFileName)
outFileName = '{}/mesh_out.nc'.format(tempdir)

outDir = os.path.dirname(outFileName)
if graphInfoFileName is not None:
graphInfoFileName = os.path.abspath(graphInfoFileName)

check_call(['MpasMeshConverter.x', inFileName, outFileName],
logger)
outDir = os.path.dirname(outFileName)

dsOut = xarray.open_dataset(outFileName)
dsOut.load()
check_call(['MpasMeshConverter.x', inFileName, outFileName], logger)

if graphInfoFileName is not None:
shutil.copyfile('{}/graph.info'.format(outDir),
graphInfoFileName)
dsOut = xarray.open_dataset(outFileName)
dsOut.load()

if graphInfoFileName is not None:
shutil.copyfile('{}/graph.info'.format(outDir),
graphInfoFileName)

return dsOut

@@ -109,50 +109,50 @@ def cull(dsIn, dsMask=None, dsInverse=None, dsPreserve=None,
"""
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)
tempdir = mkdtemp(dir=dir)
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

0 comments on commit 8bdcbbb

Please sign in to comment.