Stream: general
Topic: converting and renaming
Jean-Francois Lamarque (Apr 25 2021 at 21:22):
Hi I am trying to read standard CESM output and extract single variables for CMIP6-style output. I might also have to do unit conversion.
Script is ~lamar/Python/H2/rw_h0.ipynb
I am reading the files using
ds=xr.open_mfdataset(files[0:3], concat_dim="time", combine='nested')
then extracting a specific variable
var = ds["H2"]
I tried
var.rename({"H2": "h2"})
but this does not work. The error message is
"cannot rename 'H2' because it is not a variable or dimension in this dataset"
Thoughts?
Stephen Yeager (Apr 25 2021 at 21:32):
Use:
var.rename('h2')
For some reason, the dict construct is only for coordinates?
Jean-Francois Lamarque (Apr 25 2021 at 21:34):
Thanks. Steve. It "works" in that it does not complain, but the output netcdf file still has "H2", not "h2"
/glade/scratch/lamar/tmp/H2/h2_Amon_WACCM6-fr_hydrogen_c1_r1_gn_201001-201412.nc
Stephen Yeager (Apr 25 2021 at 21:35):
You need to overwrite var by doing:
var = var.rename('h2')
Jean-Francois Lamarque (Apr 25 2021 at 22:24):
Making progress :-)
Now it crashes writing the netcdf file
TypeError: unhashable type: 'set'
Jean-Francois Lamarque (Apr 25 2021 at 23:10):
Found a way around it. Not pretty but it works. Since I have a lot of variables, is
there a way to create a function that would do that?
var = ds["H2"]
dsout = xr.Dataset(
data_vars=dict( h2=(["time", "lev", "lat", "lon"], var), ), coords=dict( lon=(["lon"], lon), lat=(["lat"], lat), lev=(["lev"], lev), time=date, ), attrs=dict(description="Hydrogen MIP"),
)
dsout['h2'].attrs["units"] = "mol/mol"
Deepak Cherian (Apr 25 2021 at 23:33):
I would do something like
ds=xr.open_mfdataset(files[0:3], concat_dim="time", combine='nested') renamer = {"H2": "h2",} # could add other variables here attrs = {"h2": {"units": "mol/mol", "description="Hydrogen MIP"}} ds_renamed = ds.rename_vars(rename) for var in ds_renamed: # alternatively, ds_renamed[var].attrs = attrs[var] ds_renamed[var].assign_attrs(attrs[var]).to_netcdf(f"{var}.nc")
Jean-Francois Lamarque (Apr 25 2021 at 23:56):
Thanks, Deepak. I'll try that!
Last updated: Jan 30 2022 at 12:01 UTC