Skip to content

Commit 74b7bbf

Browse files
committed
Add/update unit testing for Mpas(Analysis)Xarray
1 parent 91f46f0 commit 74b7bbf

File tree

4 files changed

+150
-101
lines changed

4 files changed

+150
-101
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
"""
2+
Unit test infrastructure for mpas_xarray.
3+
4+
Xylar Asay-Davis, Phillip J. Wolfram
5+
12/07/2016
6+
"""
7+
8+
import pytest
9+
from mpas_analysis.test import TestCase, loaddatadir
10+
from mpas_analysis.shared.MpasAnalysisXarray import MpasAnalysisXarray
11+
12+
13+
@pytest.mark.usefixtures("loaddatadir")
14+
class TestMpasAnalysisXarray(TestCase):
15+
16+
def test_variable_map(self):
17+
fileName = str(self.datadir.join('example_jan.nc'))
18+
varMap = {
19+
'avgSurfaceTemperature':
20+
['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature',
21+
'other_string',
22+
'yet_another_string'],
23+
'daysSinceStartOfSim':
24+
['time_avg_daysSinceStartOfSim',
25+
'xtime',
26+
'something_else'],
27+
'avgLayerTemperature':
28+
['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature',
29+
'test1',
30+
'test2'],
31+
'Time': [['xtime_start', 'xtime_end'],
32+
'time_avg_daysSinceStartOfSim']}
33+
34+
varList = ['avgSurfaceTemperature', 'avgLayerTemperature',
35+
'refBottomDepth', 'daysSinceStartOfSim']
36+
37+
for calendar in ['gregorian', 'gregorian_noleap']:
38+
# preprocess_mpas will use varMap to map the variable names from
39+
# their values in the file to the desired values in varList
40+
mpasXarray = MpasAnalysisXarray.MpasAnalysisXarray()
41+
ds = mpasXarray.openMultifileDataSet(fileName,
42+
calendar,
43+
timeVariableName='Time',
44+
variableList=varList,
45+
variableMap=varMap,
46+
yearOffset=1850)
47+
48+
# make sure the remapping happened as expected
49+
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
50+
51+
def test_open_dataset_fn(self):
52+
fileName = str(self.datadir.join('example_jan.nc'))
53+
timestr = ['xtime_start', 'xtime_end']
54+
varList = ['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature']
55+
56+
for calendar in ['gregorian', 'gregorian_noleap']:
57+
ds = MpasAnalysisXarray.openMultifileDataSet(
58+
fileName, calendar, timeVariableName=timestr,
59+
variableList=varList, yearOffset=1850)
60+
self.assertEqual(ds.data_vars.keys(), varList)
61+
62+
def test_start_end(self):
63+
fileName = str(self.datadir.join('example_jan_feb.nc'))
64+
timestr = ['xtime_start', 'xtime_end']
65+
varList = ['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature']
66+
67+
for calendar in ['gregorian', 'gregorian_noleap']:
68+
# all dates
69+
ds = MpasAnalysisXarray.openMultifileDataSet(
70+
fileName, calendar, timeVariableName=timestr,
71+
variableList=varList, startDate='0001-01-01',
72+
endDate='9999-12-31', yearOffset=1850)
73+
self.assertEqual(len(ds.Time), 2)
74+
75+
# just the first date
76+
ds = MpasAnalysisXarray.openMultifileDataSet(
77+
fileName, calendar, timeVariableName=timestr,
78+
variableList=varList, startDate='0005-01-01',
79+
endDate='0005-02-01', yearOffset=1850)
80+
self.assertEqual(len(ds.Time), 1)
81+
82+
# just the second date
83+
ds = MpasAnalysisXarray.openMultifileDataSet(
84+
fileName, calendar, timeVariableName=timestr,
85+
variableList=varList, startDate='0005-02-01',
86+
endDate='0005-03-01', yearOffset=1850)
87+
self.assertEqual(len(ds.Time), 1)
88+
89+
90+
91+
# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test_mpas_xarray/example_jan.nc
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../test_mpas_xarray/example_jan_feb.nc

mpas_analysis/test/test_mpas_xarray.py

Lines changed: 57 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
import pytest
99
from mpas_analysis.test import TestCase, loaddatadir
10-
from mpas_analysis.shared.mpas_xarray import mpas_xarray
10+
from mpas_analysis.shared.MpasXarray import MpasXarray
1111
import xarray as xr
1212
import pandas as pd
1313

1414

1515
@pytest.mark.usefixtures("loaddatadir")
16-
class TestNamelist(TestCase):
16+
class TestMpasXarray(TestCase):
1717

1818
def test_subset_variables(self):
1919
fileName = str(self.datadir.join('example_jan.nc'))
@@ -22,35 +22,31 @@ def test_subset_variables(self):
2222

2323
# first, test loading the whole data set and then calling
2424
# subset_variables explicitly
25-
ds = xr.open_mfdataset(
26-
fileName,
27-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
28-
timestr=timestr,
29-
yearoffset=1850))
30-
ds = mpas_xarray.subset_variables(ds, varList)
25+
mpasXarray = MpasXarray.MpasXarray()
26+
ds = mpasXarray.openMultifileDataSet(fileName,
27+
timeVariableName=timestr,
28+
yearOffset=1850)
29+
ds = MpasXarray.subsetVariables(ds, varList)
3130
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
3231
self.assertEqual(pd.Timestamp(ds.Time.values[0]),
3332
pd.Timestamp('1855-01-16 12:22:30'))
3433

3534
# next, test the same with the onlyvars argument
36-
ds = xr.open_mfdataset(
37-
fileName,
38-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
39-
timestr=timestr,
40-
onlyvars=varList,
41-
yearoffset=1850))
35+
mpasXarray = MpasXarray.MpasXarray()
36+
ds = mpasXarray.openMultifileDataSet(fileName,
37+
timeVariableName=timestr,
38+
variableList=varList,
39+
yearOffset=1850)
4240
self.assertEqual(ds.data_vars.keys(), varList)
4341

44-
with self.assertRaisesRegexp(AssertionError,
42+
with self.assertRaisesRegexp(ValueError,
4543
'Empty dataset is returned.'):
4644
missingvars = ['foo', 'bar']
47-
ds = xr.open_mfdataset(
48-
fileName,
49-
preprocess=lambda x:
50-
mpas_xarray.preprocess_mpas(x,
51-
timestr=timestr,
52-
onlyvars=missingvars,
53-
yearoffset=1850))
45+
mpasXarray = MpasXarray.MpasXarray()
46+
ds = mpasXarray.openMultifileDataSet(fileName,
47+
timeVariableName=timestr,
48+
variableList=missingvars,
49+
yearOffset=1850)
5450

5551
def test_iselvals(self):
5652
fileName = str(self.datadir.join('example_jan.nc'))
@@ -60,13 +56,13 @@ def test_iselvals(self):
6056
'refBottomDepth']
6157

6258
iselvals = {'nVertLevels': slice(0, 3)}
63-
ds = xr.open_mfdataset(
64-
fileName,
65-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
66-
timestr=timestr,
67-
onlyvars=varList,
68-
iselvals=iselvals,
69-
yearoffset=1850))
59+
mpasXarray = MpasXarray.MpasXarray()
60+
ds = mpasXarray.openMultifileDataSet(fileName,
61+
timeVariableName=timestr,
62+
variableList=varList,
63+
selectCoordIndices=iselvals,
64+
yearOffset=1850)
65+
7066
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
7167
self.assertEqual(ds[varList[0]].shape, (1, 7, 3))
7268
self.assertEqual(ds['refBottomDepth'].shape, (3,))
@@ -84,12 +80,11 @@ def test_no_units(self):
8480
['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature',
8581
'refBottomDepth']
8682

87-
ds = xr.open_mfdataset(
88-
fileName,
89-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
90-
timestr=timestr,
91-
onlyvars=varList,
92-
yearoffset=1850))
83+
mpasXarray = MpasXarray.MpasXarray()
84+
ds = mpasXarray.openMultifileDataSet(fileName,
85+
timeVariableName=timestr,
86+
variableList=varList,
87+
yearOffset=1850)
9388
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
9489
date = pd.Timestamp(ds.Time.values[0])
9590
# round to nearest second
@@ -107,14 +102,12 @@ def test_bad_selvals(self):
107102
with self.assertRaisesRegexp(AssertionError,
108103
'not a dimension in the dataset that '
109104
'can be used for selection'):
110-
ds = xr.open_mfdataset(
111-
fileName,
112-
preprocess=lambda x:
113-
mpas_xarray.preprocess_mpas(x,
114-
timestr=timestr,
115-
onlyvars=varList,
116-
selvals=selvals,
117-
yearoffset=1850))
105+
mpasXarray = MpasXarray.MpasXarray()
106+
mpasXarray.openMultifileDataSet(fileName,
107+
timeVariableName=timestr,
108+
variableList=varList,
109+
selectCoordValues=selvals,
110+
yearOffset=1850)
118111

119112
def test_selvals(self):
120113
fileName = str(self.datadir.join('example_jan.nc'))
@@ -123,23 +116,22 @@ def test_selvals(self):
123116
['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature',
124117
'refBottomDepth']
125118

126-
dsRef = xr.open_mfdataset(
127-
fileName,
128-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
129-
timestr=timestr,
130-
onlyvars=varList,
131-
selvals=None,
132-
yearoffset=1850))
119+
mpasXarray = MpasXarray.MpasXarray()
120+
dsRef = mpasXarray.openMultifileDataSet(fileName,
121+
timeVariableName=timestr,
122+
variableList=varList,
123+
selectCoordValues=None,
124+
yearOffset=1850)
133125

134126
for vertIndex in range(0, 11):
135127
selvals = {'nVertLevels': vertIndex}
136-
ds = xr.open_mfdataset(
137-
fileName,
138-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
139-
timestr=timestr,
140-
onlyvars=varList,
141-
selvals=selvals,
142-
yearoffset=1850))
128+
mpasXarray = MpasXarray.MpasXarray()
129+
ds = mpasXarray.openMultifileDataSet(fileName,
130+
timeVariableName=timestr,
131+
variableList=varList,
132+
selectCoordValues=selvals,
133+
yearOffset=1850)
134+
143135
self.assertEqual(ds.data_vars.keys(), varList)
144136
self.assertEqual(ds[varList[0]].shape, (1, 7))
145137
self.assertEqual(ds['refBottomDepth'],
@@ -150,52 +142,16 @@ def test_remove_repeated_time_index(self):
150142
timestr = ['xtime_start', 'xtime_end']
151143
varList = ['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature']
152144

153-
ds = xr.open_mfdataset(
154-
fileName,
155-
preprocess=lambda x: mpas_xarray.preprocess_mpas(x,
156-
timestr=timestr,
157-
onlyvars=varList,
158-
yearoffset=1850))
145+
mpasXarray = MpasXarray.MpasXarray()
146+
# repeat time indices are removed in openMultifileDataSet
147+
ds = mpasXarray.openMultifileDataSet(fileName,
148+
timeVariableName=timestr,
149+
variableList=varList,
150+
yearOffset=1850)
159151

160152
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
161-
self.assertEqual(len(ds.Time.values), 3)
162-
163-
ds = mpas_xarray.remove_repeated_time_index(ds)
153+
# There would be 3 time indices if repeat indices had not been removed.
154+
# Make sure there are 2.
164155
self.assertEqual(len(ds.Time.values), 2)
165156

166-
def test_variable_map(self):
167-
fileName = str(self.datadir.join('example_jan.nc'))
168-
varMap = {
169-
'avgSurfaceTemperature':
170-
['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature',
171-
'other_string',
172-
'yet_another_string'],
173-
'daysSinceStartOfSim':
174-
['time_avg_daysSinceStartOfSim',
175-
'xtime',
176-
'something_else'],
177-
'avgLayerTemperature':
178-
['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature',
179-
'test1',
180-
'test2'],
181-
'Time': [['xtime_start', 'xtime_end'],
182-
'time_avg_daysSinceStartOfSim']}
183-
184-
varList = ['avgSurfaceTemperature', 'avgLayerTemperature',
185-
'refBottomDepth', 'daysSinceStartOfSim']
186-
187-
# preprocess_mpas will use varMap to map the variable names from their
188-
# values in the file to the desired values in varList
189-
ds = xr.open_mfdataset(
190-
fileName,
191-
preprocess=lambda x: mpas_xarray.preprocess_mpas(
192-
x,
193-
timestr='Time',
194-
onlyvars=varList,
195-
yearoffset=1850,
196-
varmap=varMap))
197-
198-
# make sure the remapping happened as expected
199-
self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList))
200-
201157
# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python

0 commit comments

Comments
 (0)