def test_subset_variables(self): fileName = str(self.datadir.join('example_jan.nc')) calendar = 'gregorian_noleap' timestr = ['xtime_start', 'xtime_end'] variableList = \ ['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature'] # first, test loading the whole data set and then calling # subset_variables explicitly ds = mpas_xarray.open_multifile_dataset(fileNames=fileName, calendar=calendar, timeVariableName=timestr) ds = mpas_xarray.subset_variables(ds, variableList) dsVarList = list(ds.data_vars.keys()) + list(ds.coords.keys()) assert (numpy.all([var in dsVarList for var in variableList])) self.assertEqual( days_to_datetime(days=ds.Time.values, referenceDate='0001-01-01', calendar=calendar), string_to_datetime('0005-01-16 12:22:30')) # next, test the same with the onlyvars argument ds = mpas_xarray.open_multifile_dataset(fileNames=fileName, calendar=calendar, timeVariableName=timestr, variableList=variableList) self.assertEqual(list(ds.data_vars.keys()), variableList) with six.assertRaisesRegex(self, ValueError, 'Empty dataset is returned.'): missingvars = ['foo', 'bar'] ds = mpas_xarray.open_multifile_dataset(fileNames=fileName, calendar=calendar, timeVariableName=timestr, variableList=missingvars)
def test_selvals(self): fileName = str(self.datadir.join('example_jan.nc')) calendar = 'gregorian_noleap' simulationStartTime = '0001-01-01' timestr = 'time_avg_daysSinceStartOfSim' variableList = \ ['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature', 'refBottomDepth'] dsRef = mpas_xarray.open_multifile_dataset( fileNames=fileName, calendar=calendar, simulationStartTime=simulationStartTime, timeVariableName=timestr, variableList=variableList, selValues=None) for vertIndex in range(0, 11): selvals = {'nVertLevels': vertIndex} ds = mpas_xarray.open_multifile_dataset( fileNames=fileName, calendar=calendar, simulationStartTime=simulationStartTime, timeVariableName=timestr, variableList=variableList, selValues=selvals) dsVarList = list(ds.data_vars.keys()) + list(ds.coords.keys()) assert (numpy.all([var in dsVarList for var in variableList])) self.assertEqual(ds[variableList[0]].shape, (1, 7)) self.assertEqual(ds['refBottomDepth'], dsRef['refBottomDepth'][vertIndex])
def test_iselvals(self): fileName = str(self.datadir.join('example_jan.nc')) calendar = 'gregorian_noleap' simulationStartTime = '0001-01-01' timestr = 'time_avg_daysSinceStartOfSim' variableList = \ ['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature', 'refBottomDepth'] iselvals = {'nVertLevels': slice(0, 3)} ds = mpas_xarray.open_multifile_dataset( fileNames=fileName, calendar=calendar, simulationStartTime=simulationStartTime, timeVariableName=timestr, variableList=variableList, iselValues=iselvals) dsVarList = list(ds.data_vars.keys()) + list(ds.coords.keys()) assert (numpy.all([var in dsVarList for var in variableList])) self.assertEqual(ds[variableList[0]].shape, (1, 7, 3)) self.assertEqual(ds['refBottomDepth'].shape, (3, )) self.assertApproxEqual(ds['refBottomDepth'][-1], 4.882000207901) self.assertEqual( days_to_datetime(days=ds.Time.values[0], referenceDate='0001-01-01', calendar=calendar), string_to_datetime('0005-01-14 12:24:14'))
def test_bad_selvals(self): fileName = str(self.datadir.join('example_jan.nc')) calendar = 'gregorian_noleap' simulationStartTime = '0001-01-01' timestr = 'time_avg_daysSinceStartOfSim' variableList = \ ['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature', 'refBottomDepth'] selvals = {'refBottomDepth': 8.77999997138977} with six.assertRaisesRegex( self, AssertionError, 'not a dimension in the dataset that ' 'can be used for selection'): mpas_xarray.open_multifile_dataset( fileNames=fileName, calendar=calendar, simulationStartTime=simulationStartTime, timeVariableName=timestr, variableList=variableList, selValues=selvals)
def test_remove_repeated_time_index(self): fileName = str(self.datadir.join('example_jan*.nc')) calendar = 'gregorian_noleap' timestr = ['xtime_start', 'xtime_end'] variableList = \ ['time_avg_avgValueWithinOceanRegion_avgSurfaceTemperature'] # repeat time indices are removed in openMultifileDataSet ds = mpas_xarray.open_multifile_dataset(fileNames=fileName, calendar=calendar, timeVariableName=timestr, variableList=variableList) self.assertEqual(sorted(ds.data_vars.keys()), sorted(variableList)) # There would be 3 time indices if repeat indices had not been removed. # Make sure there are 2. self.assertEqual(len(ds.Time.values), 2)
def test_no_units(self): fileName = str(self.datadir.join('example_no_units_jan.nc')) calendar = 'gregorian_noleap' simulationStartTime = '0001-01-01' timestr = 'time_avg_daysSinceStartOfSim' variableList = \ ['time_avg_avgValueWithinOceanLayerRegion_avgLayerTemperature', 'refBottomDepth'] ds = mpas_xarray.open_multifile_dataset( fileNames=fileName, calendar=calendar, simulationStartTime=simulationStartTime, timeVariableName=timestr, variableList=variableList) self.assertEqual(sorted(ds.data_vars.keys()), sorted(variableList)) self.assertEqual( days_to_datetime(days=ds.Time.values[0], referenceDate='0001-01-01', calendar=calendar), string_to_datetime('0005-01-14 12:24:14'))