Beispiel #1
0
def test_sort_by_time():
    result = utils.sort_by_time([
        local_path(TESTDATA['cmip5_tasmax_2007_nc']),
        local_path(TESTDATA['cmip5_tasmax_2006_nc'])
    ])
    assert '200601' in result[0]
    assert '200701' in result[1]
Beispiel #2
0
def test_aggregations():
    nc_files = []
    nc_files.append(local_path(TESTDATA["cmip5_tasmax_2007_nc"]))
    nc_files.append(local_path(TESTDATA["cmip5_tasmax_2006_nc"]))

    aggs = utils.aggregations(nc_files)

    assert len(aggs) == 1
    assert "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1" in aggs
    agg = aggs["tasmax_MPI-ESM-MR_RCP4.5_r1i1p1"]

    # check aggregation files
    agg_files = agg["files"]
    assert len(agg_files) == 2
    assert "tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200601-200612.nc" in agg_files[0]
    assert "tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200701-200712.nc" in agg_files[1]

    # check timestamps
    assert agg["from_timestamp"] == "20060116"
    assert agg["to_timestamp"] == "20071216"

    # check variable
    assert agg["variable"] == "tasmax"

    # check filename
    assert agg["filename"] == "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20071216.nc"
Beispiel #3
0
    def test_aggregations(self):
        nc_files = []
        nc_files.append(utils.local_path(TESTDATA['cmip5_tasmax_2007_nc']))
        nc_files.append(utils.local_path(TESTDATA['cmip5_tasmax_2006_nc']))

        aggs = utils.aggregations(nc_files)
        
        nose.tools.ok_(len(aggs) == 1, len(aggs))
        nose.tools.ok_("tasmax_MPI-ESM-MR_RCP4.5_r1i1p1" in aggs, aggs)
        agg = aggs["tasmax_MPI-ESM-MR_RCP4.5_r1i1p1"]

        # check aggregation files
        agg_files = agg['files']
        nose.tools.ok_(len(agg_files) == 2, agg)
        nose.tools.ok_("tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200601-200612.nc" in agg_files[0], agg)
        nose.tools.ok_("tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200701-200712.nc" in agg_files[1], agg)

        # check timestamps
        nose.tools.ok_(agg['from_timestamp'] == '20060116', agg['from_timestamp'])
        nose.tools.ok_(agg['to_timestamp'] == '20071216', agg['to_timestamp'])

        # check variable
        nose.tools.ok_(agg['variable'] == "tasmax", agg['variable'])

        # check filename
        nose.tools.ok_(agg['filename'] == 'tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20071216.nc', agg['filename'])
Beispiel #4
0
def test_unrotate_pole():
    ncs = [
        local_path(TESTDATA['cordex_tasmax_2006_nc']),
        local_path(TESTDATA['cordex_tasmax_2007_nc'])
    ]
    lats, lons = utils.unrotate_pole(ncs)
    assert lats.shape == (103, 106)
Beispiel #5
0
def test_aggregations():
    nc_files = []
    nc_files.append(local_path(TESTDATA['cmip5_tasmax_2007_nc']))
    nc_files.append(local_path(TESTDATA['cmip5_tasmax_2006_nc']))

    aggs = utils.aggregations(nc_files)

    assert len(aggs) == 1
    assert "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1" in aggs
    agg = aggs["tasmax_MPI-ESM-MR_RCP4.5_r1i1p1"]

    # check aggregation files
    agg_files = agg['files']
    assert len(agg_files) == 2
    assert "tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200601-200612.nc" in agg_files[0]
    assert "tasmax_Amon_MPI-ESM-MR_rcp45_r1i1p1_200701-200712.nc" in agg_files[1]

    # check timestamps
    assert agg['from_timestamp'] == '20060116'
    assert agg['to_timestamp'] == '20071216'

    # check variable
    assert agg['variable'] == "tasmax"

    # check filename
    assert agg['filename'] == 'tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20071216.nc'
Beispiel #6
0
def test_get_variable():
    variable = utils.get_variable(local_path(TESTDATA['cmip5_tasmax_2007_nc']))
    assert 'tasmax' == variable

    variable = utils.get_variable(local_path(
        TESTDATA['cordex_tasmax_2007_nc']))
    assert 'tasmax' == variable
Beispiel #7
0
def test_get_index_lat():
    ncs = [local_path(TESTDATA['cordex_tasmax_2006_nc']),
           local_path(TESTDATA['cordex_tasmax_2007_nc'])]
    index = utils.get_index_lat(ncs)
    assert 1 == index
    index = utils.get_index_lat(ncs[0])
    assert 1 == index
    index = utils.get_index_lat(local_path(TESTDATA['cmip5_tasmax_2007_nc']))
    assert 1 == index
Beispiel #8
0
def test_dissimilarity_op():
    """Test with a real file."""
    import datetime as dt
    lon, lat = -72, 46
    g = Point(lon, lat)

    cfn = local_path(TESTDATA['indicators_small.nc'])
    tfn = local_path(TESTDATA['indicators_medium.nc'])

    indices = ['meantemp', 'totalpr']

    # Candidate fields
    candidate = ocgis.RequestDataset(cfn,
                                     variable=indices,
                                     time_range=[dt.datetime(1970, 1, 1), dt.datetime(2000, 1, 1)],
                                     )

    # The indicators_small dataset is just a subset of the indicators_medium
    # dataset. Below is the code to create the small dataset.
    # Running the test with the full file takes about 2 minutes, so we'll
    # crop the data to 4 grid cells.
    """
    op = ocgis.OcgOperations(dataset=crd, geom=g,
                             select_nearest=False, search_radius_mult=1.75,
                             output_format='nc',
                             output_format_options={'data_model': 'NETCDF4'},
                             dir_output='/tmp', prefix='indicators_small'
                             )
    res = op.execute()
    """
    # Target fields
    # Extract values from one grid cell
    trd = ocgis.RequestDataset(tfn,
                               variable=indices,
                               time_range=[dt.datetime(1970, 1, 1),
                                           dt.datetime(2000, 1, 1)],
                               )

    op = ocgis.OcgOperations(dataset=trd, geom=g,
                             search_radius_mult=1.75, select_nearest=True)
    target = op.execute().get_element()

    ops = ocgis.OcgOperations(
        calc=[{'func': 'dissimilarity', 'name': 'spatial_analog',
               'kwds': {'dist': 'seuclidean', 'target': target,
                        'candidate': indices}}],
        dataset=candidate
    )

    res = ops.execute()
    out = res.get_element()
    val = out['dissimilarity'].get_value()
    i = np.argmin(np.abs(out['lon'].get_value() - lon))
    j = np.argmin(np.abs(out['lat'].get_value() - lat))
    np.testing.assert_almost_equal(val[j, i], 0, 6)
    np.testing.assert_array_equal(val > 0, True)
Beispiel #9
0
def test_get_time():
    timestamps = utils.get_time(local_path(TESTDATA['cmip5_tasmax_2007_nc']))
    assert 12 == len(timestamps)

    timestamps = utils.get_time(local_path(TESTDATA['cordex_tasmax_2007_nc']))
    assert 12 == len(timestamps)

    values = utils.get_values([local_path(TESTDATA['cordex_tasmax_2006_nc']),
                               local_path(TESTDATA['cordex_tasmax_2007_nc'])])
    assert 23 == len(values)
Beispiel #10
0
def test_get_coordinates():
    ncs = [local_path(TESTDATA['cordex_tasmax_2006_nc']),
           local_path(TESTDATA['cordex_tasmax_2007_nc'])]

    lats, lons = utils.get_coordinates(ncs, unrotate=False)

    assert 1 == len(lats.shape)

    lats, lons = utils.get_coordinates(ncs)
    assert 103 == len(lats)
    assert 106 == len(lons)
Beispiel #11
0
def test_get_time():
    timestamps = utils.get_time(local_path(TESTDATA["cmip5_tasmax_2007_nc"]))
    assert 12 == len(timestamps)

    timestamps = utils.get_time(local_path(TESTDATA["cordex_tasmax_2007_nc"]))
    assert 12 == len(timestamps)

    values = utils.get_values(
        [local_path(TESTDATA["cordex_tasmax_2006_nc"]), local_path(TESTDATA["cordex_tasmax_2007_nc"])]
    )
    assert 23 == len(values)
Beispiel #12
0
def test_drs_filename():
    # cordex
    filename = utils.drs_filename(local_path(TESTDATA['cordex_tasmax_2006_nc']), skip_timestamp=False)
    assert filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_20060215-20061216.nc"

    # cordex ... skip timestamp
    filename = utils.drs_filename(local_path(TESTDATA['cordex_tasmax_2006_nc']), skip_timestamp=True)
    assert filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon.nc"

    # cmip5
    filename = utils.drs_filename(local_path(TESTDATA['cmip5_tasmax_2006_nc']), skip_timestamp=False)
    assert filename == "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20061216.nc"
Beispiel #13
0
    def test_drs_filename(self):
        # cordex
        filename = utils.drs_filename(utils.local_path(TESTDATA['cordex_tasmax_nc']))
        nose.tools.ok_(filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_20060215-20061216.nc", filename)

        # cordex ... skip timestamp
        filename = utils.drs_filename(utils.local_path(TESTDATA['cordex_tasmax_nc']), skip_timestamp=True)
        nose.tools.ok_(filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon.nc", filename)
        
        # cmip5
        filename = utils.drs_filename(utils.local_path(TESTDATA['cmip5_tasmax_nc']))
        nose.tools.ok_(filename == "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20061216.nc", filename)
Beispiel #14
0
def test_drs_filename():
    # cordex
    filename = utils.drs_filename(local_path(TESTDATA["cordex_tasmax_2006_nc"]), skip_timestamp=False)
    assert filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_20060215-20061216.nc"

    # cordex ... skip timestamp
    filename = utils.drs_filename(local_path(TESTDATA["cordex_tasmax_2006_nc"]), skip_timestamp=True)
    assert filename == "tasmax_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon.nc"

    # cmip5
    filename = utils.drs_filename(local_path(TESTDATA["cmip5_tasmax_2006_nc"]), skip_timestamp=False)
    assert filename == "tasmax_MPI-ESM-MR_RCP4.5_r1i1p1_20060116-20061216.nc"
Beispiel #15
0
def test_get_timerange():
    start, end = utils.get_timerange(local_path(TESTDATA['cmip5_tasmax_2006_nc']))
    assert "20060116" == start
    assert "20061216" == end

    start, end = utils.get_timerange(local_path(TESTDATA['cordex_tasmax_2007_nc']))
    assert "20070116" == start
    assert "20071216" == end

    start, end = utils.get_timerange([local_path(TESTDATA['cordex_tasmax_2006_nc']),
                                     local_path(TESTDATA['cordex_tasmax_2007_nc'])])
    assert "20060215" == start
    assert "20071216" == end
Beispiel #16
0
def test_get_timerange():
    start, end = utils.get_timerange(local_path(TESTDATA["cmip5_tasmax_2006_nc"]))
    assert "20060116" == start
    assert "20061216" == end

    start, end = utils.get_timerange(local_path(TESTDATA["cordex_tasmax_2007_nc"]))
    assert "20070116" == start
    assert "20071216" == end

    start, end = utils.get_timerange(
        [local_path(TESTDATA["cordex_tasmax_2006_nc"]), local_path(TESTDATA["cordex_tasmax_2007_nc"])]
    )
    assert "20060215" == start
    assert "20071216" == end
Beispiel #17
0
def test_archive_zip():
    result = utils.archive(
        [local_path(TESTDATA['cmip5_tasmax_2007_nc'])],
        format='zip',
        dir_output=tempfile.mkdtemp())
    zipf = zipfile.ZipFile(result)
    assert len(zipf.namelist()) == 1
Beispiel #18
0
def test_archive_tar():
    result = utils.archive(
        [local_path(TESTDATA['cmip5_tasmax_2007_nc'])],
        format='tar',
        dir_output=tempfile.mkdtemp())
    tar = tarfile.open(result)
    assert len(tar.getnames()) == 1
Beispiel #19
0
def test_indice_simple():
    # SU expects tasmax
    resources = [local_path(TESTDATA['cordex_tasmax_2006_nc'])]
    output = indices.calc_indice_simple(
        resources,
        indices=['SU'], groupings='year', dir_output=tempfile.mkdtemp())

    assert os.path.basename(output[0]) == 'SU_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_20060215-20061216.nc'

    ds = Dataset(output[0])
    # SU variable must be in result
    assert 'SU' in ds.variables
    # 1 year
    assert len(ds.variables['time']) == 1
def test_indice_simple():
    # SU expects tasmax
    resources = [local_path(TESTDATA['cordex_tasmax_2006_nc'])]
    output = indices.calc_indice_simple(
        resources,
        indice='SU', grouping='yr', dir_output=tempfile.mkdtemp())

    assert os.path.basename(output[0]) == 'SU_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_200602-200612.nc'  # noqa

    ds = Dataset(output[0])
    # SU variable must be in result
    assert 'SU' in ds.variables
    # 1 year
    assert len(ds.variables['time']) == 1
Beispiel #21
0
def test_indice_percentile():
    # TX90p expects tasmax
    resources = [local_path(TESTDATA['cordex_tasmax_2006_nc'])]
    output = indices.calc_indice_percentile(
        resources, variable='tasmax',
        indices=['TX'], percentile=90, groupings='year',
        dir_output=tempfile.mkdtemp())

    assert os.path.basename(output[0]) == 'TX_EUR-44_MPI-M-MPI-ESM-LR_rcp45_r1i1p1_MPI-CSC-REMO2009_v1_mon_200602-200612.nc'

    ds = Dataset(output[0])
    # SU variable must be in result
    assert 'TX' in ds.variables
    # 1 year
    assert len(ds.variables['time']) == 1
Beispiel #22
0
    def test_indice_su_tasmax(self):
        raise SkipTest
        out_dir = tempfile.mkdtemp()
        # SU expects tasmax
        output = indices.calc_indice_single(
            [local_path(TESTDATA['cordex_tasmax_nc'])],
            indices=['SU'], groupings='year', dir_output=out_dir)

        nose.tools.ok_(
            os.path.basename(output) == 'TG_EUR-44_MPI-M-MPI-ESM-LR_historical_r1i1p1_CLMcom-CCLM4-8-17_v1_day.nc',
            output)
        
        ds = Dataset(output)
        # SU variable must be in result
        nose.tools.ok_('SU' in ds.variables, ds.variables.keys())
        # 5 years
        nose.tools.ok_(len(ds.variables['time']) == 5, len(ds.variables['time']))
Beispiel #23
0
def test_archive_zip():
    result = utils.archive([local_path(TESTDATA["cmip5_tasmax_2007_nc"])], format="zip", dir_output=tempfile.mkdtemp())
    zipf = zipfile.ZipFile(result)
    assert len(zipf.namelist()) == 1
Beispiel #24
0
def test_get_frequency():
    freq = utils.get_frequency(local_path(TESTDATA["cmip5_tasmax_2007_nc"]))
    assert "mon" == freq
Beispiel #25
0
def test_get_variable():
    variable = utils.get_variable(local_path(TESTDATA["cmip5_tasmax_2007_nc"]))
    assert "tasmax" == variable

    variable = utils.get_variable(local_path(TESTDATA["cordex_tasmax_2007_nc"]))
    assert "tasmax" == variable
Beispiel #26
0
def test_has_Lambert_Conformal():
    has_lambert = ocgis_module.has_Lambert_Conformal([
        local_path(TESTDATA['cordex_tasmax_2006_nc']),
        local_path(TESTDATA['cordex_tasmax_2007_nc'])
    ])
    assert False == has_lambert
Beispiel #27
0
 def test_get_timestamps(self):
     start,end = utils.get_timestamps(utils.local_path(TESTDATA['cmip5_tasmax_nc']))
     nose.tools.ok_("20060116" == start, start)
     nose.tools.ok_("20061216" == end, end)
Beispiel #28
0
def test_get_frequency():
    freq = utils.get_frequency(local_path(TESTDATA['cmip5_tasmax_2007_nc']))
    assert 'mon' == freq
Beispiel #29
0
def test_has_Lambert_Conformal():
    has_lambert = ocgis_module.has_Lambert_Conformal(
        [local_path(TESTDATA['cordex_tasmax_2006_nc']),
        local_path(TESTDATA['cordex_tasmax_2007_nc'])])
    assert False == has_lambert
Beispiel #30
0
 def test_sort_by_time(self):
     result = utils.sort_by_time( [utils.local_path(TESTDATA['cmip5_tasmax_2007_nc']),
                                   utils.local_path(TESTDATA['cmip5_tasmax_2006_nc'])] )
     nose.tools.ok_('200601' in result[0], result)
     nose.tools.ok_('200701' in result[1], result)
Beispiel #31
0
def test_sinfo():
    cdo.sinfo(input=local_path(TESTDATA['cmip5_tasmax_2006_nc']))
Beispiel #32
0
def test_local_path():
    assert local_path('file:///tmp/test.nc') == '/tmp/test.nc'
    assert local_path('/tmp/test.nc') == '/tmp/test.nc'
Beispiel #33
0
def test_has_variable():
    assert utils.has_variable(
        local_path(TESTDATA['cmip5_tasmax_2006_nc']), 'tasmax') is True
Beispiel #34
0
def test_archive_tar():
    result = utils.archive([local_path(TESTDATA["cmip5_tasmax_2007_nc"])], format="tar", dir_output=tempfile.mkdtemp())
    tar = tarfile.open(result)
    assert len(tar.getnames()) == 1
Beispiel #35
0
 def test_has_variable(self):
     nose.tools.ok_(utils.has_variable(utils.local_path(TESTDATA['cmip5_tasmax_nc']), 'tasmax') == True)
Beispiel #36
0
def test_sort_by_time():
    result = utils.sort_by_time(
        [local_path(TESTDATA["cmip5_tasmax_2007_nc"]), local_path(TESTDATA["cmip5_tasmax_2006_nc"])]
    )
    assert "200601" in result[0]
    assert "200701" in result[1]
Beispiel #37
0
 def test_get_variable(self):
     variable = utils.get_variable(utils.local_path(TESTDATA['cmip5_tasmax_nc']))
     nose.tools.ok_("tasmax" == variable, variable)
Beispiel #38
0
def test_local_path():
    assert local_path("file:///tmp/test.nc") == "/tmp/test.nc"
    assert local_path("/tmp/test.nc") == "/tmp/test.nc"
Beispiel #39
0
 def test_local_path(self):
     nose.tools.ok_(utils.local_path('file:///tmp/test.nc') == '/tmp/test.nc')
     nose.tools.ok_(utils.local_path('/tmp/test.nc') == '/tmp/test.nc')
Beispiel #40
0
def test_has_variable():
    assert utils.has_variable(local_path(TESTDATA["cmip5_tasmax_2006_nc"]), "tasmax") is True