|
| 1 | +""" |
| 2 | +Unit test infrastructure for variable mapping. |
| 3 | +
|
| 4 | +Xylar Asay-Davis |
| 5 | +02/08/2017 |
| 6 | +""" |
| 7 | + |
| 8 | +import pytest |
| 9 | +from mpas_analysis.test import TestCase, loaddatadir |
| 10 | +from mpas_analysis.shared.dataset.utility import openMultifileDataset |
| 11 | + |
| 12 | + |
| 13 | +@pytest.mark.usefixtures("loaddatadir") |
| 14 | +class TestVariableMapping(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 | + # preprocess_mpas will use varMap to map the variable names from their |
| 38 | + # values in the file to the desired values in varList |
| 39 | + ds = openMultifileDataset(fileName, calendar='gregorian', |
| 40 | + timeVariableName='Time', |
| 41 | + variableList=varList, variableMap=varMap, |
| 42 | + yearOffset=1850) |
| 43 | + |
| 44 | + # make sure the remapping happened as expected |
| 45 | + self.assertEqual(sorted(ds.data_vars.keys()), sorted(varList)) |
| 46 | + |
| 47 | +# vim: foldmethod=marker ai ts=4 sts=4 et sw=4 ft=python |
0 commit comments