Light_B
Light_B

Reputation: 1800

Updating time as Proleptic Gregorian in a NetCDF using Xarray

I have individual 6 hourly NetCDF files of a year but the time value is corrupt i.e; timedelta64[ns] 00:00:00 for each file. My aim is to correct that and merge it into one combined file using xarrays. The file has a CF-convention and time is in Proleptic Gregorian.

I tried to store time axis values as timestamp but I got an error while writing it back to .nc format saying ValueError: failed to prevent overwriting existing key calendar in attrs on variable 'time'.

I'm trying to update the time values as seconds from 01-01-1970. It works but the problem is that when I read back the saved file, I would like the time as displayed in the actual yyyy-mm-dd so that I could use .sel method to quickly explore data.

Is there a better way to write my output file while maintaining the standard conventions of Proleptic Gregorian so that when I read it back, my time axis is automatically in yyyy-mm-dd instead of converting it back from seconds to yyyy-mm-dd format each time after reading.

import xarray as xr
big_ds = xr.open_mfdataset('path/*',   autoclose=True, concat_dim='time')
dt = pd.date_range(start=datetime(2000,1,1), end=datetime(2000,12,31,18), freq='6H')
big_ds.time.values = (dt - std_time).total_seconds()
big_ds.to_netcdf('outfile.nc')

test_ds = xr.open_dataset('outfile.nc')
test_ds.time
>><xarray.DataArray 'time' (time: 1464)>
array([9.466848e+08, 9.467064e+08, 9.467280e+08, ..., 9.782424e+08,
   9.782640e+08, 9.782856e+08])
Coordinates:
* time     (time) float64 9.467e+08 9.467e+08 ... 9.783e+08 9.783e+08
Attributes:
calendar:  proleptic_gregorian

Upvotes: 0

Views: 1425

Answers (1)

Light_B
Light_B

Reputation: 1800

The short fix was to add units to the time attribute.

big_ds.time.attrs['units'] = 'Seconds since 01/01/1970 00:00:00 UTC'

So, while reading data back, xarray is intelligent enough to see this and convert the time values stored in seconds to datetime64[ns] so that one can access them as yyyy-mm-dd

Upvotes: 3

Related Questions