Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Output int32 time dimension in netCDF using xarray

Let's say I have time data that looks like this in an xarray Dataset:

ds = xr.Dataset({'time': pd.date_range('2000-01-01', periods=10)})
ds.to_netcdf('asdf.nc')

xarray's to_netcdf() method outputs the time dimension as int64:

$ ncdump -v time asdf.nc
netcdf asdf {
dimensions:
    time = 10 ;
variables:
    int64 time(time) ;
        time:units = "days since 2000-01-01 00:00:00" ;
        time:calendar = "proleptic_gregorian" ;
data:

 time = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ;
}

Because I'm working with a THREDDS server which does not support int64, I would like for these time data to be int32. Is this possible to do using xarray?

like image 699
Dan Avatar asked Jul 31 '26 15:07

Dan


1 Answers

You can specify the data type of each output variable via the encoding property or the encoding keyword argument to to_netcdf. In your example, this would simply look like:

ds.to_netcdf('asdf.nc', encoding={'time': {'dtype': 'i4'}})

More information on writing encoded data can be found in the xarray documentation: http://xarray.pydata.org/en/latest/io.html#writing-encoded-data

like image 86
jhamman Avatar answered Aug 06 '26 05:08

jhamman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!