Skip to content

Commit

Permalink
Add support for remapping to or from vertices in remapper
Browse files Browse the repository at this point in the history
  • Loading branch information
xylar committed May 24, 2021
1 parent 83c5162 commit 704dbc9
Showing 1 changed file with 65 additions and 22 deletions.
87 changes: 65 additions & 22 deletions pyremap/remapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,48 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None,
else:
tempobj = None

self.sourceDescriptor.to_scrip('{}/src_mesh.nc'.format(tempdir))
self.destinationDescriptor.to_scrip(
'{}/dst_mesh.nc'.format(tempdir))
sourceFileName = '{}/src_mesh.nc'.format(tempdir)
destinationFileName = '{}/dst_mesh.nc'.format(tempdir)

src_loc = 'center'
src_file_format = 'scrip'
if isinstance(self.sourceDescriptor, MpasMeshDescriptor):
src_file_format = 'esmf'
if self.sourceDescriptor.vertices:
if 'conserve' in method:
raise ValueError('Can\'t remap from MPAS vertices with '
'conservative methods')
src_loc = 'corner'

dst_loc = 'center'
dst_file_format = 'scrip'
if isinstance(self.destinationDescriptor, MpasMeshDescriptor):
dst_file_format = 'esmf'
if self.destinationDescriptor.vertices:
if 'conserve' in method:
raise ValueError('Can\'t remap to MPAS vertices with '
'conservative methods')
dst_loc = 'corner'

if src_file_format == 'scrip':
self.sourceDescriptor.to_scrip(sourceFileName)
elif src_file_format == 'esmf':
self.sourceDescriptor.to_esmf(sourceFileName)
else:
raise ValueError('Unexpected file format {}'.format(
src_file_format))

if dst_file_format == 'scrip':
self.destinationDescriptor.to_scrip(destinationFileName)
elif dst_file_format == 'esmf':
self.destinationDescriptor.to_esmf(destinationFileName)
else:
raise ValueError('Unexpected file format {}'.format(
dst_file_format))

args = [rwgPath,
'--source', self.sourceDescriptor.scripFileName,
'--destination', self.destinationDescriptor.scripFileName,
'--source', sourceFileName,
'--destination', destinationFileName,
'--weight', self.mappingFileName,
'--method', method,
'--netcdf4',
Expand All @@ -204,6 +239,11 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None,
if extrap_method is not None:
args.extend(['--extrap_method', extrap_method])

if src_file_format == 'esmf':
args.extend(['--src_loc', src_loc])
if dst_file_format == 'esmf':
args.extend(['--dst_loc', dst_loc])

parallel_args = []

if mpiTasks > 1:
Expand Down Expand Up @@ -253,7 +293,7 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None,
args.extend(additionalArgs)

if logger is None:
_print_running(args)
_print_running(args, fn=print)
# make sure any output is flushed before we add output from the
# subprocess
sys.stdout.flush()
Expand All @@ -265,8 +305,7 @@ def build_mapping_file(self, method='bilinear', additionalArgs=None,
subprocess.check_call(args, stdout=DEVNULL)

else:
_print_running(args)
logger.info('running: {}'.format(' '.join(args)))
_print_running(args, fn=logger.info)
for handler in logger.handlers:
handler.flush()

Expand Down Expand Up @@ -367,6 +406,20 @@ def remap_file(self, inFileName, outFileName, variableList=None,

regridArgs = []

if isinstance(self.sourceDescriptor, MpasMeshDescriptor):
if self.sourceDescriptor.vertices:
regridArgs.extend(['--rgr col_nm=nVertices'])
else:
args.extend(['-P', 'mpas'])
if not replaceMpasFill:
# the -C (climatology) flag prevents ncremap from trying to
# add a _FillValue attribute that might already be present
# and quits with an error
args.append('-C')

if variableList is not None:
args.extend(['-v', ','.join(variableList)])

if renormalize is not None:
regridArgs.append('--renormalize={}'.format(renormalize))

Expand Down Expand Up @@ -398,17 +451,6 @@ def remap_file(self, inFileName, outFileName, variableList=None,
if len(regridArgs) > 0:
args.extend(['-R', ' '.join(regridArgs)])

if isinstance(self.sourceDescriptor, MpasMeshDescriptor):
args.extend(['-P', 'mpas'])
if not replaceMpasFill:
# the -C (climatology) flag prevents ncremap from trying to
# add a _FillValue attribute that might already be present and
# quits with an error
args.append('-C')

if variableList is not None:
args.extend(['-v', ','.join(variableList)])

# set an environment variable to make sure we're not using czender's
# local version of NCO instead of one we have intentionally loaded
env = os.environ.copy()
Expand All @@ -422,9 +464,10 @@ def remap_file(self, inFileName, outFileName, variableList=None,
sys.stdout.flush()
sys.stderr.flush()

_print_running(args, fn=print)
subprocess.check_call(args, env=env)
else:
logger.info('running: {}'.format(' '.join(args)))
_print_running(args, fn=logger.info)
for handler in logger.handlers:
handler.flush()

Expand Down Expand Up @@ -728,12 +771,12 @@ def _remap_numpy_array(self, inField, remapAxes,
return outField # }}}


def _print_running(args):
def _print_running(args, fn):
print_args = []
for arg in args:
if ' ' in arg:
arg = '"{}"'.format(arg)
print_args.append(arg)
print('running: {}'.format(' '.join(print_args)))
fn('running: {}'.format(' '.join(print_args)))

# vim: ai ts=4 sts=4 et sw=4 ft=python

0 comments on commit 704dbc9

Please sign in to comment.