I would like to add units, long_name, and maybe a description to a variable while using the to_netcdf command. Let me know if you know how.

Here is my code that work:

filename = path+'file.nc' ds = xr.Dataset({'sla': (('time_counter','x', 'y'), SLA)}, coords={'time_counter':time_counter,'nav_lon':(('x','y'),lon),'nav_lat':(('x','y'),lat)}) ds.to_netcdf(filename, 'w') 

Supplementary informations if you want to use this:

  • 'sla' is the name I give while saving the variable SLA
  • SLA has 3 dimensions; I give them the names 'time_counter', 'x', and 'y'
  • I defined coordinates, one of which ('time_counter') is directly a dimension of SLA, but also it is possible to have a coordinate with multiple dimensions (e.g., 'nav_lon' and 'nav_lat' have 2 dimensions.
  • Here is the link that explain the function:

1 Answer

You can set the attributes of each variable before saving the Dataset to NetCDF, for example (after creating your ds):

ds['sla'].attrs = {'units': 'something'} 

After the to_netcdf() step I get (part of the ncdump -h):

double sla(time_counter, x, y) ; ... sla:units = "something" ; 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.