Skip to content

Commit

Permalink
Update tests to handle fix to projection grids
Browse files Browse the repository at this point in the history
Test a non-square projection grid

Update the reference files

Remove redundant test for mpas_to_latlon

Test more examples with remap_file (ncremap)
  • Loading branch information
xylar committed May 24, 2021
1 parent be036ee commit 2d8bd0a
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 36 deletions.
61 changes: 25 additions & 36 deletions pyremap/test/test_interpolate.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ def get_stereographic_array_descriptor(self):
'+lon_0=0.0 +k_0=1.0 +x_0=0.0 +y_0=0.0 '
'+ellps=WGS84')

# a square 61x61 cell map with 100 km resolution and
# a 61x51 cell map with 100 km resolution and
xMax = 3000e3
yMax = 2500e3
res = 100e3
nx = 2 * int(xMax / res) + 1
ny = 2 * int(yMax / res) + 1
x = numpy.linspace(-xMax, xMax, nx)
y = numpy.linspace(-yMax, yMax, ny)
meshName = '{}km_Antarctic_stereo'.format(int(res * 1e-3))
descriptor = ProjectionGridDescriptor.create(projection, x, x,
descriptor = ProjectionGridDescriptor.create(projection, x, y,
meshName)
return descriptor

Expand All @@ -101,26 +104,33 @@ def check_remap(self, inFileName, outFileName, refFileName, remapper,
remap_file=True):

dsRef = xarray.open_dataset(refFileName)
for var in ['lat_bnds', 'lon_bnds', 'gw', 'area', 'nvertices']:
if var in dsRef:
dsRef = dsRef.drop_vars([var])
if remap_file:
# first, test interpolation with ncremap
remapper.remap_file(inFileName=inFileName,
outFileName=outFileName,
replaceMpasFill=True)

shutil.copyfile(outFileName, 'ref_file_{}'.format(os.path.basename(outFileName)))

assert os.path.exists(outFileName)
dsRemapped = xarray.open_dataset(outFileName)
# drop some extra vairables added by ncremap that aren't in the
# reference data set
dsRemapped = dsRemapped.drop_vars(['lat_bnds', 'lon_bnds', 'gw',
'area'])
for var in ['lat_bnds', 'lon_bnds', 'gw', 'area', 'nvertices']:
if var in dsRemapped:
dsRemapped = dsRemapped.drop_vars([var])
self.assertDatasetApproxEqual(dsRemapped, dsRef)

# now, try in-memory remapping
ds = xarray.open_dataset(inFileName)
dsRemapped = remapper.remap(ds, self.renormalizationThreshold)
dsRemapped.to_netcdf('ref_array_{}'.format(os.path.basename(outFileName)))
self.assertDatasetApproxEqual(dsRemapped, dsRef)

def test_mpas_to_latlon_file(self):
def test_mpas_to_latlon(self):
'''
test horizontal interpolation from an MPAS mesh to a destination
lat/lon grid determined from a file containing 'lat' and 'lon' coords
Expand All @@ -130,7 +140,7 @@ def test_mpas_to_latlon_file(self):
'''

weightFileName, outFileName, refFileName = \
self.get_file_names(suffix='mpas_to_latlon_file')
self.get_file_names(suffix='mpas_to_latlon')

sourceDescriptor, mpasMeshFileName, timeSeriesFileName = \
self.get_mpas_descriptor()
Expand All @@ -142,27 +152,6 @@ def test_mpas_to_latlon_file(self):
self.check_remap(timeSeriesFileName, outFileName, refFileName,
remapper, remap_file=True)

def test_mpas_to_latlon_array(self):
'''
test horizontal interpolation from an MPAS mesh to a destination
lat/lon grid determined from config options 'lat' and 'lon'.
Xylar Asay-Davis
04/06/2017
'''

weightFileName, outFileName, refFileName = \
self.get_file_names(suffix='mpas_to_latlon_array')

sourceDescriptor, mpasMeshFileName, timeSeriesFileName = \
self.get_mpas_descriptor()
destinationDescriptor = self.get_latlon_array_descriptor()

remapper = self.build_remapper(sourceDescriptor, destinationDescriptor,
weightFileName)
self.check_remap(timeSeriesFileName, outFileName, refFileName,
remapper, remap_file=True)

def test_latlon_file_to_latlon_array(self):
'''
test horizontal interpolation from a lat/lon grid to a destination
Expand All @@ -184,7 +173,7 @@ def test_latlon_file_to_latlon_array(self):
self.check_remap(latLonGridFileName, outFileName, refFileName,
remapper, remap_file=True)

def test_mpas_to_stereographic_array(self):
def test_mpas_to_stereographic(self):
'''
test horizontal interpolation from an MPAS mesh to a destination
stereographic grid.
Expand All @@ -194,7 +183,7 @@ def test_mpas_to_stereographic_array(self):
'''

weightFileName, outFileName, refFileName = \
self.get_file_names(suffix='mpas_to_stereographic_array')
self.get_file_names(suffix='mpas_to_stereographic')

sourceDescriptor, mpasMeshFileName, timeSeriesFileName = \
self.get_mpas_descriptor()
Expand All @@ -206,9 +195,9 @@ def test_mpas_to_stereographic_array(self):
# ncremap doesn't support stereographic grids so just check the
# Remapper
self.check_remap(timeSeriesFileName, outFileName, refFileName,
remapper, remap_file=False)
remapper, remap_file=True)

def test_latlon_file_to_stereographic_array(self):
def test_latlon_to_stereographic(self):
'''
test horizontal interpolation from a lat/lon grid to a destination
stereographic grid.
Expand All @@ -218,7 +207,7 @@ def test_latlon_file_to_stereographic_array(self):
'''

weightFileName, outFileName, refFileName = \
self.get_file_names(suffix='latlon_file_to_stereographic_array')
self.get_file_names(suffix='latlon_to_stereographic')

sourceDescriptor, latLonGridFileName = \
self.get_latlon_file_descriptor()
Expand All @@ -230,7 +219,7 @@ def test_latlon_file_to_stereographic_array(self):
# ncremap doesn't support stereographic grids so just check the
# Remapper
self.check_remap(latLonGridFileName, outFileName, refFileName,
remapper, remap_file=False)
remapper, remap_file=True)

def test_stereographic_array_to_latlon_array(self):
'''
Expand All @@ -243,7 +232,7 @@ def test_stereographic_array_to_latlon_array(self):
'''

weightFileName, outFileName, refFileName = \
self.get_file_names(suffix='stereographic_array_to_latlon_array')
self.get_file_names(suffix='stereographic_to_latlon')

sourceDescriptor = self.get_stereographic_array_descriptor()
destinationDescriptor = self.get_latlon_array_descriptor()
Expand All @@ -256,10 +245,10 @@ def test_stereographic_array_to_latlon_array(self):
inField = inField.repeat(3, axis=0)
inField = inField.repeat(2, axis=3)

datasetDict = {'dims': ('dim0', 'x', 'y', 'dim3'),
datasetDict = {'dims': ('dim0', 'y', 'x', 'dim3'),
'coords': sourceDescriptor.coords,
'data_vars': {'complicated':
{'dims': ('dim0', 'x', 'y', 'dim3'),
{'dims': ('dim0', 'y', 'x', 'dim3'),
'data': inField}}}

ds = xarray.Dataset.from_dict(datasetDict)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 2d8bd0a

Please sign in to comment.