Anne Sophie
Anne Sophie

Reputation: 177

options for saving xarray dataset with to_netcdf

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:

Upvotes: 2

Views: 5511

Answers (1)

Bart
Bart

Reputation: 10253

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" ;

Upvotes: 2

Related Questions