From a270ddabec6a0cb15ae9b51c2c98bd916397137d Mon Sep 17 00:00:00 2001 From: Xylar Asay-Davis Date: Tue, 10 Mar 2020 12:00:19 +0100 Subject: [PATCH] Allow user specified temp directory during remap This should solve some problems on compute nodes. Also, switch to the more up-to-date tempfile approach by making a temp directory. --- pyremap/__init__.py | 2 +- pyremap/remapper.py | 26 +++++++++++++++++++------- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/pyremap/__init__.py b/pyremap/__init__.py index c583dce..ad41cc9 100644 --- a/pyremap/__init__.py +++ b/pyremap/__init__.py @@ -6,5 +6,5 @@ from pyremap.polar import get_polar_descriptor_from_file, get_polar_descriptor -__version_info__ = (0, 0, 4) +__version_info__ = (0, 0, 5) __version__ = '.'.join(str(vi) for vi in __version_info__) diff --git a/pyremap/remapper.py b/pyremap/remapper.py index a29e9c0..1a7e528 100644 --- a/pyremap/remapper.py +++ b/pyremap/remapper.py @@ -23,7 +23,7 @@ # Xylar Asay-Davis import subprocess -import tempfile +from backports.tempfile import TemporaryDirectory import os from distutils.spawn import find_executable import numpy @@ -98,7 +98,7 @@ def __init__(self, sourceDescriptor, destinationDescriptor, # }}} def build_mapping_file(self, method='bilinear', additionalArgs=None, - logger=None, mpiTasks=1): # {{{ + logger=None, mpiTasks=1, tempdir=None): # {{{ ''' Given a source file defining either an MPAS mesh or a lat-lon grid and a destination file or set of arrays defining a lat-lon grid, constructs @@ -121,6 +121,12 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None, The number of MPI tasks (a number > 1 implies that ESMF_RegridWeightGen will be called with ``mpirun``) + tempdir: str, optional + A temporary directory. By default, a temporary directory is + created, typically in ``/tmp`` but on some systems such as compute + nodes this may not be visible to all processors in the subsequent + ``ESMF_RegridWeightGen`` call + Raises ------ OSError @@ -156,8 +162,15 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None, 'channel.') # Write source and destination SCRIP files in temporary locations - self.sourceDescriptor.to_scrip(_get_temp_path()) - self.destinationDescriptor.to_scrip(_get_temp_path()) + if tempdir is None: + tempobj = TemporaryDirectory() + tempdir = tempobj.name + else: + tempobj = None + + self.sourceDescriptor.to_scrip('{}/src_mesh.nc'.format(tempdir)) + self.destinationDescriptor.to_scrip( + '{}/dst_mesh.nc'.format(tempdir)) args = [rwgPath, '--source', self.sourceDescriptor.scripFileName, @@ -214,9 +227,8 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None, raise subprocess.CalledProcessError(process.returncode, ' '.join(args)) - # remove the temporary SCRIP files - os.remove(self.sourceDescriptor.scripFileName) - os.remove(self.destinationDescriptor.scripFileName) + if tempobj is not None: + tempobj.cleanup() # }}}