-
-
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
CF conventions for time doesn't support years #1467
Comments
I am not sure to understand what you are asking us to do here. The problem with "years" is that their use is not recommended by the CF conventions. Very often (and I think your file means it this way), users would like years to be simple "calendar years" , i.e. : 1901-01-01, 1902-01-01, but this is not what the unit "years" means in the CF conventions: see http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#time-coordinate |
I think I do mean 'years' in the CF convention sense, in this case the time dimension is:
This is correctly interpreted by the NASA Panoply NetCDF file viewer. From glancing at the |
Can you pinpoint to which part of the CF convention? From the link I read: I agree however that interpreting "years" as being "calendar years" is the only way that makes sense. For the record, netCDF4 also doesn't like "years": import netCDF4
ds = netCDF4.Dataset('/home/mowglie/Downloads/histsoc_population_0.5deg_1861-2005.nc4')
time = ds.variables['time']
netCDF4.num2date(time[:], units=time.units)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-15-b38f64c7bce4> in <module>()
2 ds = netCDF4.Dataset('histsoc_population_0.5deg_1861-2005.nc4')
3 time = ds.variables['time']
----> 4 netCDF4.num2date(time[:], units=time.units)
netCDF4/_netCDF4.pyx in netCDF4._netCDF4.num2date (netCDF4/_netCDF4.c:66463)()
ValueError: unsupported time units |
Although I'm not a specialist of CF conventions, this issue may be related to this one: Unidata/cftime#5. The forthcoming |
I would think that this sort of feature belongs in |
I actually have a similar issues with respect to 'months'. I want to write out my xarray dataarray as a netcdf file, with months as time intervals (one value per month, doesn't matter what day of the month is used as a reference). As with the 'years' described above, this does not seem to work in the current framework? |
In order to construct a netcdf file with a 2D field on a monthly resolution (for X number of years), I currently use the lines of code mentioned below. Since I do not care about the type of calendar, I just use 360_day, in which each month of the year has 30 days. Perhaps this can be useful for others. In case a better solution is available, please let me know!
|
Hi Matthias, I think your solution is fine. The best is simply to avoid "months" as units altogether. If one has a "real" calendar one can also let pandas and xarray do the job: t = pd.date_range(start='1980-01', end='2010-12', freq='MS')
target = np.random.rand(len(t), 10, 10)
lat = np.arange(50, 51, 0.1)
lon = np.arange(3, 4, 0.1)
target_xr = xr.Dataset({'test': (['time', 'lat', 'lon'], target)},
coords={'time': ('time', t),
'lat': lat, 'lon': lon}
)
target_xr.to_netcdf('test_2.nc') which creates the following time units automatically:
|
Month unit support in cftime is being discussed in in Unidata/cftime#69 Perhaps xarray folks would like to weigh in. |
I have run into this problem multiple times. The latest example I found were some [CORE ocean model runs] (https://rda.ucar.edu/datasets/ds262.0/index.html#!description).
I understand that 'fully' supporting to decode this unit is hard and should probably addressed upstream. But I think it might be useful to have a utility function that converts a dataset with these units into someting quickly useable with xarray? This way the user is aware that something is not decoded exactly, but can work with the data. |
CF conventions code supports:
{'microseconds': 'us', 'milliseconds': 'ms', 'seconds': 's', 'minutes': 'm', 'hours': 'h', 'days': 'D'}
, but not 'years'. See example file https://www.dropbox.com/s/34dcpliko928yaj/histsoc_population_0.5deg_1861-2005.nc4?dl=0The text was updated successfully, but these errors were encountered: