def test_cf_infer_cf_specs(): cf_content0 = """ [register] [[attrs]] source="*hycom3d*" [data_vars] [[temp]] name=mytemp """ cf_specs0 = cf.CFSpecs(cf_content0) cf_content1 = """ [data_vars] [[temp]] name=mytemp [[sal]] name=mysal [coords] [[lon]] name=mylon """ cf_specs1 = cf.CFSpecs(cf_content1) cf_content2 = """ [register] name=hycom3d [data_vars] [[temp]] name=mytemp [[sal]] name=mysal """ cf_specs2 = cf.CFSpecs(cf_content2) cf_cache = cf._get_cache_() cf_cache["registered"].clear() cf.register_cf_specs(cf_specs0, cf_specs1, cf_specs2) temp = xr.DataArray([1], dims="mylon") sal = xr.DataArray([1], dims="mylon") lon = xr.DataArray([1], dims="mylon") ds = xr.Dataset({"mytemp": temp, "mysal": sal}, coords={"mylon": lon}) assert cf.infer_cf_specs(ds) is cf_specs1 ds.attrs.update(source="my hycom3d!") assert cf.infer_cf_specs(ds) is cf_specs0 ds.attrs.update(cfspecs="hycom3d") assert cf.infer_cf_specs(ds) is cf_specs2 # test_cf_cfspecs_decode_encode() # test_cf_cfspecs_format_data_var_loc()
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"
def test_cf_get_cf_specs_from_encoding(): cf_cache = cf._get_cache_() cf_cache["registered"].clear() content = """ [register] name=mynam234 [data_vars] [[temp]] name=mytemp """ cf_specs_in = cf.CFSpecs(content) cf.register_cf_specs(cf_specs_in) ds = xr.Dataset( { "mytemp": (["mylat", "mylon"], np.ones((2, 2))), "mysal": (["mylat", "mylon"], np.ones((2, 2))) }, coords={ "mylon": np.arange(2), "mylat": np.arange(2) }) ds.encoding.update(cfspecs="mynam234") assert cf.get_cf_specs_from_encoding(ds) is cf_specs_in ds.mytemp.encoding.update(cfspecs="mynam234") assert cf.get_cf_specs_from_encoding(ds.mytemp) is cf_specs_in ds.mylon.encoding.update(cfspecs="mynam234") assert cf.get_cf_specs_from_encoding(ds.mylon) is cf_specs_in assert cf.get_cf_specs_from_encoding(ds.mylat) is None
def test_cf_cfspecs_format_data_var_specialize(): da = xr.DataArray(1, name="salinity") cfspecs = cf.CFSpecs({'data_vars': {'sal': {'name': 'supersal'}}}) da = cfspecs.format_data_var(da, specialize=True) assert da.name == "supersal" assert da.standard_name == "sea_water_salinity"
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
def test_cf_get_cf_specs_matching_score(): cf_content0 = """ [data_vars] [[temp]] name=mytemp """ cf_specs0 = cf.CFSpecs(cf_content0) cf_content1 = """ [data_vars] [[temp]] name=mytemp [[sal]] name=mysal [coords] [[lon]] name=mylon """ cf_specs1 = cf.CFSpecs(cf_content1) cf_content2 = """ [data_vars] [[temp]] name=mytemp [[sal]] name=mysal """ cf_specs2 = cf.CFSpecs(cf_content2) ds = xr.Dataset( { "mytemp": (["mylat", "mylon"], np.ones((2, 2))), "mysal": (["mylat", "mylon"], np.ones((2, 2))) }, coords={ "mylon": np.arange(2), "mylat": np.arange(2) }) for cf_specs, score in [(cf_specs0, 25), (cf_specs1, 75), (cf_specs2, 50)]: assert cf.get_cf_specs_matching_score(ds, cf_specs) == score
def test_cf_set_cf_specs_registered(): cf_cache = cf._get_cache_() cf_cache["registered"].clear() content = """ [register] name=myname2 [data_vars] [[temp]] name=mytemp """ cf_specs_in = cf.CFSpecs(content) cf.register_cf_specs(cf_specs_in) with cf.set_cf_specs("myname2") as cfspecs: assert cfspecs is cf_specs_in
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
def test_cf_cfspecs_decode_encode(): ds = xoa.open_data_sample("croco.south-africa.meridional.nc") cfspecs = cf.CFSpecs(xoa.get_data_sample("croco.cfg")) dsc = cfspecs.decode(ds) assert list(dsc) == [ 'akt', 'cs_r', 'cs_w', 'Vtransform', 'angle', 'el', 'corio', 'bathy', 'hbl', 'hc', 'mask_rho', 'ex', 'ey', 'sal', 'sc_r', 'sc_w', 'ptemp', 'time_step', 'u', 'v', 'w', 'xl', 'ssh' ] assert list(dsc.coords) == [ 'y_rho', 'y_v', 'lat_rho', 'lat_u', 'lat_v', 'lon_rho', 'lon_u', 'lon_v', 'sig_rho', 'sig_w', 'time', 'x_rho', 'x_u' ] assert list(dsc.dims) == [ 'auxil', 'sig_rho', 'sig_w', 'time', 'x_rho', 'x_u', 'y_rho', 'y_v' ] dse = cfspecs.encode(dsc) assert list(dse) == list(ds) assert list(dse.coords) == list(ds.coords) assert list(dse.dims) == list(ds.dims) ds.close()
def test_cf_register_cf_specs(): cf_cache = cf._get_cache_() cf_cache["registered"].clear() content = """ [register] name=myname [data_vars] [[temp]] name=mytemp """ cf_specs = cf.CFSpecs(content) assert cf_specs.name == "myname" cf.register_cf_specs(cf_specs) assert cf_specs in cf_cache["registered"] assert cf_specs.name == "myname" cf.register_cf_specs(myothername=cf_specs) assert cf_specs in cf_cache["registered"] assert cf_specs.name == "myothername"
def test_cf_cfspecs_get_name(specialize, expected): cfspecs = cf.CFSpecs({"data_vars": {"temp": {"name": "temperature"}}}) assert (cfspecs.data_vars.get_name("temp", specialize=specialize) == expected)