Exemple #1
0
 def setup_config(self):
     configPath = self.datadir.join('config.QU240')
     config = MpasAnalysisConfigParser()
     config.read(str(configPath))
     config.set('input', 'baseDirectory', str(self.datadir))
     config.set('output', 'baseDirectory', str(self.test_dir))
     return config
Exemple #2
0
 def setup_config(self):
     configPath = self.datadir.join('config.remap_obs')
     config = MpasAnalysisConfigParser()
     config.read(str(configPath))
     config.set('input', 'baseDirectory', str(self.datadir))
     config.set('diagnostics', 'baseDirectory', str(self.datadir))
     config.set('oceanObservations', 'obsSubdirectory', '.')
     config.set('output', 'baseDirectory', str(self.test_dir))
     return config
    def get_latlon_array_descriptor(self):
        configPath = str(self.datadir.join('config.analysis'))
        config = MpasAnalysisConfigParser()
        config.read(configPath)

        lat = numpy.array(
            config.getExpression('interpolate', 'lat', usenumpyfunc=True))
        lon = numpy.array(
            config.getExpression('interpolate', 'lon', usenumpyfunc=True))

        descriptor = LatLonGridDescriptor.create(lat, lon, units='degrees')
        return descriptor
def remap(inDir, outDir):

    inGridName = 'SouthernOcean_0.25x0.125degree'
    inFileName = '{}/Schmidtko_et_al_2014_bottom_PT_S_PD_{}.nc'.format(
        inDir, inGridName)

    config = MpasAnalysisConfigParser()
    config.read('mpas_analysis/config.default')

    inDescriptor = LatLonGridDescriptor()

    inDescriptor = LatLonGridDescriptor.read(inFileName,
                                             latVarName='lat',
                                             lonVarName='lon')

    outDescriptor = get_comparison_descriptor(config, 'antarctic')
    outGridName = outDescriptor.meshName

    outFileName = '{}/Schmidtko_et_al_2014_bottom_PT_S_PD_{}.nc'.format(
        outDir, outGridName)

    mappingFileName = '{}/map_{}_to_{}.nc'.format(inDir, inGridName,
                                                  outGridName)

    remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

    remapper.build_mapping_file(method='bilinear')

    if not os.path.exists(outFileName):
        print('Remapping...')
        with xarray.open_dataset(inFileName) as dsIn:
            with remapper.remap(dsIn, renormalizationThreshold=0.01) \
                    as remappedMLD:
                print('Done.')
                remappedMLD.attrs['history'] = ' '.join(sys.argv)
                write_netcdf(remappedMLD, outFileName)
Exemple #5
0
class TestMPASAnalysisConfigParser(TestCase):
    def setup_config(self):
        configPath = self.datadir.join('config.analysis')
        self.config = MpasAnalysisConfigParser()
        self.config.read(str(configPath))

    def check_container(self, container, container_type, item_type):
        assert isinstance(container, container_type)
        for item in container:
            assert isinstance(item, item_type)

    def test_read_config(self):
        self.setup_config()

        colorMapName = self.config.get('sst_modelvsobs', 'cmapDiff')
        self.assertEqual(colorMapName, 'coolwarm')

        self.assertEqual(self.config.getint('Test', 'testInt'), 15)
        self.assertEqual(self.config.getExpression('Test', 'testInt'), 15)

        self.assertEqual(self.config.getfloat('Test', 'testFloat'), 18.0)
        self.assertEqual(self.config.getExpression('Test', 'testFloat'), 18.0)

        self.assertEqual(self.config.getfloat('Test', 'testFloat2'), 3.)
        self.assertEqual(self.config.getExpression('Test', 'testFloat2'), 3.)

        self.assertEqual(self.config.getboolean('Test', 'testBool'), True)
        self.assertEqual(self.config.getExpression('Test', 'testBool'), True)

        testList = self.config.getExpression('sst_modelvsobs',
                                             'cmapIndicesModelObs')
        self.check_container(testList, list, int)
        self.assertEqual(testList, [0, 40, 80, 110, 140, 170, 200, 230, 255])

        testList = self.config.getExpression('sst_modelvsobs',
                                             'cmapIndicesModelObs',
                                             elementType=float)
        self.check_container(testList, list, float)
        self.assertEqual(testList,
                         [0., 40., 80., 110., 140., 170., 200., 230., 255.])

        testList = self.config.getExpression('sst_modelvsobs',
                                             'comparisonTimes')
        self.check_container(testList, list, str)
        self.assertEqual(testList, ['JFM', 'JAS', 'ANN'])

        testList = self.config.getExpression('Test', 'testList')
        self.check_container(testList, list, float)
        self.assertEqual(testList, [0.5, 0.1, 0.5])

        testTuple = self.config.getExpression('Test', 'testTuple')
        assert isinstance(testTuple, tuple)
        self.assertEqual(testTuple, (5, 0.1, 'string'))

        testDict = self.config.getExpression('Test', 'testDict')
        assert isinstance(testDict, dict)
        self.assertEqual(testDict, {
            'key1': 'string',
            'key2': -12,
            'key3': False
        })

        with six.assertRaisesRegex(
                self, configparser.NoOptionError,
                "No option 'doesntexist' in section: 'Test'"):
            self.config.getExpression(str('Test'), str('doesntexist'))

    @requires_numpy
    def test_read_config_numpy(self):
        self.setup_config()

        # tests numpy evaluation capability
        import numpy as np
        for testname in ['testNumpyarange' + str(ii) for ii in np.arange(3)]:
            self.assertArrayEqual(
                self.config.getExpression('TestNumpy',
                                          testname,
                                          usenumpyfunc=True),
                np.arange(0, 1, 10))
        for testname in ['testNumpylinspace' + str(ii) for ii in np.arange(3)]:
            self.assertArrayEqual(
                self.config.getExpression('TestNumpy',
                                          testname,
                                          usenumpyfunc=True),
                np.linspace(0, 1, 10))
        for testNumpy in ['testNumpypi' + str(ii) for ii in np.arange(3)] + \
                ['testNumpyPi']:
            self.assertEqual(
                self.config.getExpression('TestNumpy',
                                          testNumpy,
                                          usenumpyfunc=True), np.pi)
        with six.assertRaisesRegex(
                self, AssertionError,
                "'__' is not allowed in .* for `usenumpyfunc=True`"):
            self.config.getExpression('TestNumpy',
                                      'testBadStr',
                                      usenumpyfunc=True),

    def test_get_with_default(self):
        self.setup_config()

        def check_get_with_default(name, value, dtype):
            # test an options that doesn't exist using getWithDefault
            var = self.config.getWithDefault('sst_modelvsobs', name, value)
            assert isinstance(var, dtype)
            self.assertEqual(var, value)

        # test several types with getWithDefault
        check_get_with_default(name='aBool', value=True, dtype=bool)
        check_get_with_default(name='anInt', value=1, dtype=six.integer_types)
        check_get_with_default(name='aFloat', value=1.0, dtype=float)
        check_get_with_default(name='aList', value=[1, 2, 3], dtype=list)
        check_get_with_default(name='aTuple', value=(1, 2, 3), dtype=tuple)
        check_get_with_default(name='aDict', value={'blah': 1}, dtype=dict)
        check_get_with_default(name='aStr',
                               value='blah',
                               dtype=six.string_types)
def main():
    parser = argparse.ArgumentParser(
        description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument("-i",
                        "--inDir",
                        dest="inDir",
                        required=True,
                        help="Directory where intermediate files used in "
                        "processing should be downloaded")
    parser.add_argument("-o",
                        "--outDir",
                        dest="outDir",
                        required=True,
                        help="Directory where final preprocessed observation "
                        "are stored")
    args = parser.parse_args()

    date = '20190603'

    inGridName = 'SouthernOcean_0.167x0.167degree_{}'.format(date)

    inTPrefix = 'THETA_mnthlyBar.0000000100'
    inSPrefix = 'SALT_mnthlyBar.0000000100'
    inMLDPrefix = 'MLD_mnthlyBar.0000000100'
    inUPrefix = 'UVEL_mnthlyBar.0000000100'
    inVPrefix = 'VVEL_mnthlyBar.0000000100'
    inGammaNPrefix = 'GAMMA_mnthlyBar.0000000100'

    # size in km of the polar stereographic grid
    antarcticStereoWidth = 10000

    config = MpasAnalysisConfigParser()
    config.read('mpas_analysis/config.default')
    config.set('climatology', 'comparisonAntarcticStereoWidth',
               '{}'.format(antarcticStereoWidth))

    outDescriptor = get_comparison_descriptor(config, 'antarctic')
    outGridName = '{}_{}'.format(outDescriptor.meshName, date)

    inPrefixes = [
        inTPrefix, inSPrefix, inMLDPrefix, inUPrefix, inVPrefix, inGammaNPrefix
    ]

    inGridFileName = '{}/grid.mat'.format(args.inDir)

    try:
        os.makedirs(args.inDir)
    except OSError:
        pass

    try:
        os.makedirs(args.outDir)
    except OSError:
        pass

    # dowload the desired file
    download_files(['GRID_README.txt'],
                   urlBase='http://sose.ucsd.edu/DATA',
                   outDir=args.inDir)

    urlBase = 'http://sose.ucsd.edu/DATA/SO6_V2'

    fileList = ['grid.mat']
    for prefix in inPrefixes:
        fileList.append('{}.data.gz'.format(prefix))
        fileList.append('{}.meta'.format(prefix))

    download_files(fileList, urlBase, args.inDir)
    unzip_sose_data(inPrefixes, args.inDir)

    prefix = '{}/SOSE'.format(args.outDir)

    sose_volume_to_nc(prefix, inGridName, inGridFileName, args.inDir)

    prefix = '{}/SOSE_2005-2010_monthly'.format(args.outDir)

    remap_pt_s(prefix, inGridName, inGridFileName, args.inDir, inTPrefix,
               inSPrefix, inGammaNPrefix, outDescriptor, outGridName)

    remap_mld(prefix, inGridName, inGridFileName, args.inDir, inMLDPrefix,
              outDescriptor, outGridName)

    remap_u(prefix, inGridName, inGridFileName, args.inDir, inUPrefix,
            outDescriptor, outGridName)
    remap_v(prefix, inGridName, inGridFileName, args.inDir, inVPrefix,
            outDescriptor, outGridName)

    compute_vel_mag(prefix, inGridName, args.inDir, outGridName)

    compute_pot_density(prefix, inGridName, args.inDir, outGridName)
Exemple #7
0
resolution.
'''

from mpas_analysis.shared.interpolation import Remapper
from mpas_analysis.shared.grid import MpasMeshDescriptor
from mpas_analysis.shared.climatology import get_comparison_descriptor
from mpas_analysis.configuration import MpasAnalysisConfigParser

# replace with the MPAS mesh name
inGridName = 'oQU240'

# replace with the path to the desired mesh or restart file
inGridFileName = '/media/xylar/extra_data/analysis/edison/G-QU240-master-intel/run/mpaso.rst.0001-01-06_00000.nc'

config = MpasAnalysisConfigParser()
config.read('mpas_analysis/config.default')
# replace 1.0 with the desired resolution of the output mesh
config.set('climatology', 'comparisonLatResolution', '1.0')
config.set('climatology', 'comparisonLonResolution', '1.0')

inDescriptor = MpasMeshDescriptor(inGridFileName, inGridName)

outDescriptor = get_comparison_descriptor(config, 'latlon')
outGridName = outDescriptor.meshName

mappingFileName = 'map_{}_to_{}.nc'.format(inGridName, outGridName)

remapper = Remapper(inDescriptor, outDescriptor, mappingFileName)

remapper.build_mapping_file(method='bilinear')