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?
Use:
var.rename('h2')
For some reason, the dict construct is only for coordinates?
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
You need to overwrite var by doing:
var = var.rename('h2')
Making progress :-)
Now it crashes writing the netcdf file
TypeError: unhashable type: 'set'
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"]
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"
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")
Thanks, Deepak. I'll try that!
Last updated: Jan 27 2025 at 22:16 UTC