-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ValueError when trying to encode time variable in a NetCDF file with CF convensions #3739
Comments
Hi @avatar101 - thanks for your issue! I couldn't fully reproduce your example, since it references files that I don't have access to. For the future, please consider trying to create a "Minimal, Complete and Verifiable Example" (MCVE): (http://matthewrocklin.com/blog/work/2018/02/28/minimal-bug-reports) In the meantime, I do have a suggestion you could try. Instead of ds_test.time.attrs['units'] = "Seconds since 1970-01-01 00:00:00" try ds_test.time.encoding['units'] = "Seconds since 1970-01-01 00:00:00" The reason is that, since you have created your time coordinate using |
Hi Ryan, thanks for your reply. Apologies for not creating a reproducible problem earlier as the files weren't created by xarray routine. Please find my attempt at reproducing the problem below: Minimum steps to reproduce the errorimport numpy as np
import xarray as xr
import pandas as pd
data1 = np.ones(shape=(1, 181, 360))
lats=np.arange(-90,91, 1)
lons=np.arange(-180,180,1)
time1 = np.array([0])
# creating the first dataset
da_1 = xr.DataArray(data1, coords=[time1, lats, lons], dims=['time', 'lats', 'lons'])
da_1.time.attrs['units'] = "hours since 1988-01-01 00:00:00"
da_1.time.attrs['calendar'] = "proleptic_gregorian"
da_1.time.attrs['standard_name'] = "time"
ds_1 = xr.Dataset({'V':da_1})
ds_1.attrs['Conventions'] = 'CF'
ds_1.to_netcdf('ds_1.nc', encoding=None)
# creating second test dataset
time2=np.array([6]) # wrong time value
da_2 = xr.DataArray(data1, coords=[time2, lats, lons], dims=['time', 'lats', 'lons'])
da_2.time.attrs['units'] = "hours since 1988-01-01 06:00:00"
da_2.time.attrs['calendar'] = "proleptic_gregorian"
da_2.time.attrs['standard_name'] = "time"
ds_2 = xr.Dataset({'V':da_2})
ds_2.attrs['Conventions'] = 'CF'
# saving it with wrong time value
ds_2.to_netcdf('ds_2.nc', encoding=None)
# Reading the 2 files and concatenating them
files = ['/path/to/ds_1.nc', '/path/to/ds_2.nc']
ds_test = xr.open_mfdataset(files, combine='nested', concat_dim='time', decode_cf=False)
yr = 1988 # year
dates = pd.date_range(start=(yr), end=str(yr+1), freq='6H', closed='left')
ds_test.time.values=dates[:2] # fixing the time values
ds_test.time.attrs['units'] = "Seconds since 1970-01-01 00:00:00" #required encoding
ds_test.to_netcdf('ds_1_2.nc') # gives the same error
I've also mentioned your suggestion earlier in the original post. It also gives the same error message Trying time encoding solution
|
hey,avatar, thanks for your examples. I'm wondering that problem has figured out or not . I have met the same problem. |
The following works (using import numpy as np
import xarray as xr
import pandas as pd
files = ['ds_1.nc', 'ds_2.nc']
ds_test = xr.open_mfdataset(files, combine='nested', concat_dim='time', decode_cf=False)
yr = 1988 # year
dates = pd.date_range(start=str(yr), end=str(yr+1), freq='6H', closed='left')
ds_test.assign_coords(time=dates[:2])
ds_test.time.encoding['units'] = "seconds since 1970-01-01 00:00:00"
ds_test.time.encoding['calendar'] = "proleptic_gregorian"
ds_test.to_netcdf('ds_1_2.nc') The following works as well: ds_test = xr.open_mfdataset(files, combine='nested', concat_dim='time')
ds_test.time.encoding['units'] = "seconds since 1970-01-01 00:00:00"
ds_test.to_netcdf('ds_1_2.nc') However, what does indeed not work is the following ds_test = xr.open_mfdataset(files, combine='nested', concat_dim='time')
ds_test.time.attrs['units'] = "seconds since 1970-01-01 00:00:00"
ds_test.to_netcdf('ds_1_2.nc') which I don't entirely understand, because |
thanks for your suggestions. Your first solution works fine for correcting the time data stored in the array. I also don't understand why @Chan-Jer Another work around which I used was to set the correct time value using cdo's |
I got a similar error message when opening a Zarr store with datetime64 time values, where I tried to set the "calendar" attribute on the time axis (the attribute was unset in the original store). I've found some xarray code that appears to treat the "calendar" and "units" time attributes as special, and it essentially prevents users from setting or changing these values, even when those values are not present: From xarray/coding/times.py:
|
The initial issue seems to be sorted out, by overwriting/setting the needed keys in |
Expected Output
Correctly encode
time
such that it saves the file by correctly converting value oftime
according to the reference units. I have the flexibility of dropping CF-conventions as long as time values are correct but it would also be nice to have a solution which keeps the CF-conventions intact.Problem Description
I'm trying to concatenate
netcdf
files which haveCF
conventions mentioned in their global attributes. These files have an incorrect time dimension which I try to fix with the code above. It seems that some existing encoding is preventing from writing the files back. But when I print the encoding, it doesn't show any such clashingunits
. I'm not sure if this is a bug or a wrong usage issue. Thus, any usage help on how to correctly encodetime
such that it saves the time values by correctly converting according to the reference units is much appreciated.Output of
xr.show_versions()
xarray: 0.13.0
pandas: 0.25.3
numpy: 1.18.1
scipy: 1.3.2
netCDF4: 1.4.2
pydap: None
h5netcdf: None
h5py: None
Nio: None
zarr: None
cftime: 1.0.4.2
nc_time_axis: None
PseudoNetCDF: None
rasterio: None
cfgrib: None
iris: None
bottleneck: None
dask: 2.9.2
distributed: 2.9.3
matplotlib: 3.1.0
cartopy: 0.17.0
seaborn: 0.9.0
numbagg: None
setuptools: 44.0.0.post20200106
pip: 19.3.1
conda: None
pytest: None
IPython: 7.11.1
sphinx: None
The text was updated successfully, but these errors were encountered: