Exemple #1
0
def test_cf_set_cf_specs():
    cf.reset_cache(disk=False)
    cfspecs = cf.get_cf_specs()
    cf.set_cf_specs(cfspecs)
    cf_cache = cf._get_cache_()
    assert cf_cache["current"] is cfspecs
    assert cf.get_cf_specs() is cfspecs
Exemple #2
0
def test_cf_set_cf_specs():
    cf._CACHE.clear()
    cfspecs = cf.get_cf_specs()
    cf.set_cf_specs(cfspecs)
    assert "specs" in cf._CACHE
    assert cf._CACHE["specs"] is cfspecs
    assert cf.get_cf_specs() is cfspecs
Exemple #3
0
def test_cf_get_cfg_specs_var():
    specs = cf.get_cf_specs("temp", "data_vars")
    assert specs["name"][0] == "temp"
    assert specs["standard_name"][0] == "sea_water_temperature"
    assert specs["cmap"] == "cmo.thermal"
    new_specs = cf.get_cf_specs("temp")
    assert new_specs is specs
Exemple #4
0
def test_cf_get_cfg_specs_var():
    specs = cf.get_cf_specs().data_vars["temp"]
    assert specs["alt_names"][0] == "temperature"
    assert specs["attrs"]["standard_name"][0] == "sea_water_temperature"
    assert specs["cmap"] == "cmo.thermal"
    new_specs = cf.get_cf_specs()["temp"]
    assert new_specs is specs
Exemple #5
0
def test_cf_set_cf_specs_context():
    cfspecs0 = cf.get_cf_specs()
    cfspecs1 = cf.CFSpecs({"data_vars": {"temp": {"name": "tempouille"}}})
    assert cf.get_cf_specs() is cfspecs0
    with cf.set_cf_specs(cfspecs1) as cfspecs:
        assert cfspecs is cfspecs1
        assert cf.get_cf_specs() is cfspecs1
    assert cf.get_cf_specs() is cfspecs0
Exemple #6
0
def test_cf_cfspecs_format_data_var_loc():
    temp = xr.DataArray(0,
                        name='xtemp',
                        attrs={'standard_name': 'banana_at_x_location'})
    cfspecs = cf.get_cf_specs()

    temp_fmt = cfspecs.format_data_var(temp,
                                       "temp",
                                       format_coords=False,
                                       replace_attrs=True)
    assert temp_fmt.name == "temp"
    assert temp_fmt.standard_name == "sea_water_temperature_at_x_location"

    temp_fmt = cfspecs.format_data_var(temp,
                                       "temp",
                                       format_coords=False,
                                       replace_attrs=True,
                                       add_loc_to_name=True)
    assert temp_fmt.name == "temp_x"

    cfspecs = cf.CFSpecs({"data_vars": {"temp": {"add_loc": True}}})
    temp_fmt = cfspecs.format_data_var(temp,
                                       "temp",
                                       format_coords=False,
                                       replace_attrs=True)
    assert temp_fmt.name == "temp_x"
Exemple #7
0
def test_cf_cfspecs_coords_search_dim():
    cfspecs = cf.get_cf_specs().coords

    # from name
    temp = xr.DataArray(np.arange(2 * 3).reshape(1, 2, 3),
                        dims=('aa', 'ny', 'x'))
    assert cfspecs.search_dim(temp, 'y') == 'ny'
    assert cfspecs.search_dim(temp) is None

    # from explicit axis attribute
    depth = xr.DataArray([1], dims='aa', attrs={'axis': 'z'})
    temp.coords['aa'] = depth
    assert cfspecs.search_dim(temp, 'z') == 'aa'
    assert cfspecs.search_dim(temp) is None

    # from known coordinate
    del temp.coords['aa'].attrs['axis']
    temp.coords['aa'].attrs['standard_name'] = 'ocean_layer_depth'
    assert cfspecs.search_dim(temp, 'z') == 'aa'
    assert cfspecs.search_dim(temp) is None

    # subcoords
    level = xr.DataArray(np.arange(2), dims='level')
    depth = xr.DataArray(np.arange(2 * 3).reshape(2, 3),
                         dims=('level', 'x'),
                         name='depth',
                         coords={'level': level})
    assert cfspecs.search_dim(depth) == ('level', 'z')
    assert cfspecs.search_dim(depth.level) == ('level', 'z')
    depth = depth.rename(level='aa')
    depth.aa.attrs['axis'] = 'Z'
    assert cfspecs.search_dim(depth) == ('aa', 'z')

    assert cfspecs.search_dim(xr.DataArray([5], dims='bb')) == ('bb', None)
Exemple #8
0
def test_cf_cfspecs_match_coord(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5), dims=in_name, name=in_name, attrs=in_attrs)
    res = cf.get_cf_specs().match_coord(lon, cf_name)
    if cf_name is None:
        assert res == 'lon'
    else:
        assert res is True
Exemple #9
0
def test_cf_cfspecs_format_coord(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5), dims=in_name, name=in_name, attrs=in_attrs)
    lon = cf.get_cf_specs().format_coord(lon, cf_name)
    assert lon.name == "lon"
    assert lon.standard_name == "longitude"
    assert lon.long_name == "Longitude"
    assert lon.units == "degrees_east"
Exemple #10
0
def test_cf_cfspecs_search_coord(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5), dims=in_name, name=in_name, attrs=in_attrs)
    temp = xr.DataArray(range(20, 25),
                        dims=in_name,
                        coords={in_name: lon},
                        name='temp')
    res = cf.get_cf_specs().search_coord(temp, cf_name, get="name")
    assert res == 'lon'
Exemple #11
0
def test_cf_cfspecs_format_coord_unknown():
    coord = xr.DataArray(range(5), name='foo')
    cfspecs = cf.get_cf_specs()

    coord_fmt = cfspecs.format_coord(coord, rename=False)
    assert coord_fmt is None

    coord_fmt = cfspecs.format_coord(coord, rename=True)
    assert coord_fmt.name == "foo"
Exemple #12
0
def test_cf_cfspecs_format_data_var_loc():
    temp = xr.DataArray(0,
                        name='xtemp_t',
                        attrs={'standard_name': 'banana_at_x_location'})
    cfspecs = cf.get_cf_specs()

    temp_fmt = cfspecs.format_data_var(temp, "temp", format_coords=False)
    assert temp_fmt.name == "temp_t"
    assert temp_fmt.standard_name == "sea_water_temperature_at_x_location"
Exemple #13
0
def test_cf_cfspecs_format_data_var_unkown():
    da = xr.DataArray(range(5), name='foo')
    cfspecs = cf.get_cf_specs()

    da_fmt = cfspecs.format_data_var(da, rename=False)
    assert da_fmt is None

    da_fmt = cfspecs.format_data_var(da, rename=True)
    assert da_fmt.name == "foo"
Exemple #14
0
def test_cf_cfspecs_search_data_var(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5), dims='lon', name='lon')
    temp = xr.DataArray(range(20, 25),
                        dims='lon',
                        coords={'lon': lon},
                        name=in_name,
                        attrs=in_attrs)
    ds = temp.to_dataset()
    assert cf.get_cf_specs().search_data_var(ds, cf_name, get="name") == 'temp'
Exemple #15
0
def test_cf_cfspecs_coords_get_dim_type():
    cfspecs = cf.get_cf_specs().coords

    # from name
    assert cfspecs.get_dim_type('aa') is None
    assert cfspecs.get_dim_type('xi') == "x"

    # from a known coordinate
    coord = xr.DataArray([1], dims='aa', attrs={'standard_name': 'longitude'})
    da = xr.DataArray([1], dims='aa', coords={'aa': coord})
    assert cfspecs.get_dim_type('aa', da=da) == "x"
Exemple #16
0
def test_cf_cfspecs_copy():
    cfspecs0 = cf.get_cf_specs()
    cfspecs1 = cfspecs0.copy()
    assert id(cfspecs0._dict) != id(cfspecs1._dict)
    assert sorted(list(cfspecs0._dict["data_vars"])) == sorted(
        list(cfspecs1._dict["data_vars"]))
    assert cfspecs0._dict["coords"] == cfspecs1._dict["coords"]
    assert (cfspecs0._dict["data_vars"]["temp"] == cfspecs1._dict["data_vars"]
            ["temp"])
    assert "temp" in cfspecs1["data_vars"]
    assert "temperature" in cfspecs1["data_vars"]["temp"]["name"]
Exemple #17
0
def test_cf_cfspecs_coords_get_dim_type():
    cfspecs = cf.get_cf_specs().coords

    # from attrs
    depth = xr.DataArray([1], dims='aa', attrs={'axis': 'z'})
    assert cfspecs.get_dim_type(depth) == 'z'

    # from CF specs
    depth = xr.DataArray([1],
                         dims='aa',
                         attrs={'standard_name': 'ocean_layer_depth'})
    assert cfspecs.get_dim_type(depth) == 'z'
Exemple #18
0
def test_cf_cfspecs_match_data_var(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5), dims='lon', name='lon')
    temp = xr.DataArray(range(20, 25),
                        dims='lon',
                        coords={'lon': lon},
                        name=in_name,
                        attrs=in_attrs)
    res = cf.get_cf_specs().match_data_var(temp, cf_name)
    if cf_name is None:
        assert res == 'temp'
    else:
        assert res is True
Exemple #19
0
def test_cf_cfspecs_coords_get_dim_types():
    cfspecs = cf.get_cf_specs().coords

    aa = xr.DataArray([0, 1], dims="aa", attrs={"standard_name": "latitude"})
    da = xr.DataArray(np.ones((2, 2, 2)),
                      dims=('foo', 'aa', 'xi'),
                      coords={'aa': aa})

    assert cfspecs.get_dim_types(da) == (None, 'y', 'x')
    assert cfspecs.get_dim_types(da, unknown='-') == ("-", 'y', 'x')
    assert cfspecs.get_dim_types(da, asdict=True) == {
        "foo": None,
        "aa": "y",
        "xi": "x"
    }
Exemple #20
0
def test_cf_cfspecs_coords_get_dims():
    lat = xr.DataArray([4, 5], dims='yy', attrs={'units': 'degrees_north'})
    depth = xr.DataArray([4, 5], dims='level', attrs={'axis': 'Z'})
    da = xr.DataArray(np.ones((2, 2, 2, 2)),
                      dims=('r', 'level', 'yy', 'xi'),
                      coords={
                          'level': depth,
                          'yy': lat
                      })

    cfspecs = cf.get_cf_specs().coords
    dims = cfspecs.get_dims(da, 'xyzt', allow_positional=True)
    assert dims == ('xi', 'yy', 'level', 'r')
    dims = cfspecs.get_dims(da, 'f', errors="ignore")
    assert dims == (None, )
Exemple #21
0
def test_cf_get_cf_specs_registered():

    cf_cache = cf._get_cache_()
    cf_cache["registered"].clear()
    content = """
        [register]
        name=myname

        [data_vars]
            [[temp]]
            name=mytemp
        """
    cf_specs_in = cf.CFSpecs(content)
    cf.register_cf_specs(cf_specs_in)

    cf_specs_out = cf.get_cf_specs(name='myname')
    assert cf_specs_out is cf_specs_in
Exemple #22
0
def test_cf_cfspecs_format_data_var(cf_name, in_name, in_attrs):

    lon = xr.DataArray(range(5),
                       dims='xxx',
                       name='xxx',
                       attrs={'standard_name': 'longitude'})
    temp = xr.DataArray(range(20, 25),
                        dims='xxx',
                        coords={'xxx': lon},
                        name=in_name,
                        attrs=in_attrs)
    temp = cf.get_cf_specs().format_data_var(temp, cf_name)
    assert temp.name == "temp"
    assert temp.standard_name == "sea_water_temperature"
    assert temp.long_name == "Temperature"
    assert temp.units == "degrees_celsius"
    assert temp.lon.standard_name == "longitude"
Exemple #23
0
def test_cf_cfspecs_coords_search_from_dim():

    lon = xr.DataArray([1, 2], dims='lon')
    level = xr.DataArray([1, 2, 3],
                         dims='aa',
                         attrs={'standard_name': 'ocean_sigma_coordinate'})
    mem = xr.DataArray(range(3), dims='mem')
    temp = xr.DataArray(np.zeros((mem.size, level.size, lon.size)),
                        dims=('mem', 'aa', 'lon'),
                        coords={
                            'mem': mem,
                            'aa': level,
                            'lon': lon
                        })

    cfspecs = cf.get_cf_specs().coords

    # Direct coordinate
    assert cfspecs.search_from_dim(temp, 'aa').name == 'aa'

    # Depth coordinate because the only one with this dim
    depth = xr.DataArray(np.ones((level.size, lon.size)),
                         dims=('aa', 'lon'),
                         coords={
                             'aa': level,
                             'lon': lon
                         })
    temp.coords['depth'] = depth
    assert cfspecs.search_from_dim(temp, 'aa').name == 'depth'

    # Cannot get dim_type and we have multiple coords with this dim
    del temp.coords['aa']
    fake = xr.DataArray(np.ones((level.size, lon.size)),
                        dims=('aa', 'lon'),
                        coords={'lon': lon})
    temp.coords['fake'] = fake
    assert cfspecs.search_from_dim(temp, 'aa') is None

    # Can get dim_type back from name
    temp = temp.rename(aa='level')
    assert cfspecs.search_from_dim(temp, 'level').name == 'depth'

    # Nothing identifiable
    temp = xr.DataArray([3], dims='banana')
    assert cfspecs.search_from_dim(temp, "banana") is None
Exemple #24
0
def test_cf_cfspecs_infer_coords():
    ds = xr.Dataset({"temp": ("nx", [1, 2]), "lon": ("nx", [4, 5])})
    ds = cf.get_cf_specs().infer_coords(ds)
    assert "lon" in ds.coords
Exemple #25
0
def test_cf_get_cfg_specs(cache):
    assert isinstance(cf.get_cf_specs(cache=cache), cf.CFSpecs)
Exemple #26
0
def test_cf_get_cfg_specs_var_inherit():
    specs = cf.get_cf_specs("sst", "data_vars")
    assert specs["standard_name"][0] == "sea_surface_temperature"
    assert specs["units"][0] == "degrees_celsius"
Exemple #27
0
def test_cf_get_cfg_specs_coord():
    specs = cf.get_cf_specs("lon", "coords")
    assert specs["name"][0] == "lon"
    assert "longitude" in specs["name"]
    new_specs = cf.get_cf_specs("lon")
    assert new_specs is specs
Exemple #28
0
def test_cf_cfspecs_load_cfg(cfg, key, name):
    cfspecs = cf.get_cf_specs()
    cfspecs.load_cfg(cfg)
    assert name in cfspecs["data_vars"][key]["name"]
Exemple #29
0
def test_cf_get_cfg_specs_coord_inherit():
    specs = cf.get_cf_specs("depth", "coords")
    assert specs["name"][0] == "depth"
    assert specs["long_name"][0] == "Depth"
Exemple #30
0
def test_cf_cfspecs_format_data_var_coord():
    da = xr.DataArray(0, attrs={'standard_name': 'longitude_at_u_location'})
    da = cf.get_cf_specs().format_data_var(da)