You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@sbiner are you looking at the encoding attribute of the full Dataset or the time variable? The time variable should retain the calendar encoding (the Dataset will not). E.g.:
In [1]: import cftime
In [2]: import numpy as np
In [3]: import xarray as xr
In [4]: units = 'days since 2000-02-25'
In [5]: times = cftime.num2date(np.arange(7), units=units, calendar='365_day')
In [6]: da = xr.DataArray(np.arange(7), coords=[times], dims=['time'], name='a')
In [7]: da.to_netcdf('data-noleap.nc')
In [8]: ds = xr.open_dataset('data-noleap.nc')
In [9]: ds.encoding['calendar']
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-38-677c245c7bb8> in <module>()
----> 1 default.encoding['calendar']
KeyError: 'calendar'
In [10]: ds.time.encoding['calendar']
Out[10]: u'noleap'
@spencerkclark Yes I was looking at time.encoding. Following you example I did some tests and the problem is related to the fact that I am opening multiple netCDF files with open_mfdataset. Doing so time.encoding is empty while it is as expected when opening any of the files with open_dataset instead.
If open_mfdataset() is actually dropping the encoding, then this is an issue related to #1614. That's because in open_mfdataset() while the attrs are explicitly set to those of the first supplied dataset, I don't see any similar explicit treatment of the encoding. I think that means the encoding is being set by what happens inside the core of auto_combine(), and is presumably being lost upon some of the concat or merge operations which happen inside auto_combine().
So I think to fix this then either open_mfdataset() should contain explicit treatment of the encoding, or the rules for propagating the encoding through the auto_combine() should be solidified.
@TomNicholas I forgot about this sorry. I just made a quick check with the latest xarray master and I still have the problem ... see code.
Related question but maybe out of line, is there any way to know that the snw.time type is cftime.DatetimeNoLeap (as it is visible in the overview of snw.time)?
snw = xr.open_mfdataset(l_f, combine='nested', concat_dim='time')['snw']
ipdb> xr.__version__
'0.15.2.dev29+g6048356'
ipdb> snw.time
<xarray.DataArray 'time' (time: 277393)>
array([cftime.DatetimeNoLeap(2006-01-01 00:00:00),
cftime.DatetimeNoLeap(2006-01-01 03:00:00),
cftime.DatetimeNoLeap(2006-01-01 06:00:00), ...,
cftime.DatetimeNoLeap(2100-12-30 18:00:00),
cftime.DatetimeNoLeap(2100-12-30 21:00:00),
cftime.DatetimeNoLeap(2100-12-31 00:00:00)], dtype=object)
Coordinates:
* time (time) object 2006-01-01 00:00:00 ... 2100-12-31 00:00:00
Attributes:
long_name: time
standard_name: time
axis: T
coordinate_defines: point
ipdb> snw.time.encoding
{}
I just made a quick check with the latest xarray master and I still have the problem ... see code.
#3498 added a new keyword argument to open_mfdataset, to choose which file to load to attributes from, can you try using that?
time.encoding is empty while it is as expected when opening any of the files with open_dataset instead
If this is the case, then to solve your original problem, you could also try using the preprocess argument to open_mfdataset to store the encoding somewhere where it won't be lost? i.e.
Related question but maybe out of line, is there any way to know that the snw.time type is cftime.DatetimeNoLeap (as it is visible in the overview of snw.time)?
I'm not familiar with these classes, but presumably you mean more than just checking with isinstance()? e.g.
#3498 added a new keyword argument to open_mfdataset, to choose which file to load to attributes from, can you try using that?
#3498 says something about a master_file keyword but xr.open_mfdataset does not accept it and I do not see anything else similar in the documentation except attrs_file but it is the first file by default and it did not return the calendar even when I specified attrs_file=l_f[0].
If this is the case, then to solve your original problem, you could also try using the preprocess argument to open_mfdataset to store the encoding somewhere where it won't be lost? i.e.
Related question but maybe out of line, is there any way to know that the snw.time type is cftime.DatetimeNoLeap (as it is visible in the overview of snw.time)?
I'm not familiar with these classes, but presumably you mean more than just checking with isinstance()? e.g.
Yes, I was more thinking of something like type(ds.time) which would return cftime.DatetimeNoLeap
unfortunately, numpy does not allow us to put cftime object into dtypes (yet!), so ds.time.values is a numpy.ndarray with dtype object, containing cftime objects. To make the code work, use it with ds.time.values[0]. Of course, that won't help if the array contains objects of more than one type.
In #3498, the original proposal was to name the new kwarg master_file, but later it was renamed to attrs_file. If l_f is a list of file paths, you used it correctly.
Before trying to help with debugging your issue: could you post the output of xr.show_versions()? That would help narrowing down on whether it's a dependency issue or a bug in xarray.
Also, could you try to demonstrate your issue using a synthetic example? I've been trying to reproduce it with:
unfortunately, numpy does not allow us to put cftime object into dtypes (yet!), so ds.time.values is a numpy.ndarray with dtype object, containing cftime objects. To make the code work, use it with ds.time.values[0]. Of course, that won't help if the array contains objects of more than one type.
In #3498, the original proposal was to name the new kwarg master_file, but later it was renamed to attrs_file. If l_f is a list of file paths, you used it correctly.
Yes, l_f is a list of file paths.
Before trying to help with debugging your issue: could you post the output of xr.show_versions()? That would help narrowing down on whether it's a dependency issue or a bug in xarray.
I used your code and it works for me also. I noticed the synthetic file is NETCDF4 and has int time variable while my files are NETCDF4_CLASSIC and the time variable is double. I modified the synthetic code to produce NETCDF4_CLASSIC files with double time variable but it does not change the results: encoding does not have any values related to the calendar.
Here is an output ouf ncdump -hs for one file, maybe it could help.
Any news about this issue? I am facing the same problem and I had to get the calendars by hand... I tried to update xarray but there is still the same issue of missing the ds.time.encoding with open_mfdataset when the dimension to concatenate is the time (because the example from @keewis above works but the concatenation is on x).
Any update about this issue? I'm working on a code where I want to make sure I have consistent calendars for all my inputs. Couldn't we add an option to use the encoding from the first file in the list or something?
Activity
rabernat commentedon Sep 24, 2018
Do you know you can access them in the
.encodingnamespace? Is that not sufficient for your needs?sbiner commentedon Sep 24, 2018
It would be ok but it is (or looks) empty when I use open_dataset()
spencerkclark commentedon Sep 25, 2018
@sbiner are you looking at the encoding attribute of the full Dataset or the time variable? The time variable should retain the calendar encoding (the Dataset will not). E.g.:
sbiner commentedon Sep 25, 2018
@spencerkclark Yes I was looking at time.encoding. Following you example I did some tests and the problem is related to the fact that I am opening multiple netCDF files with open_mfdataset. Doing so time.encoding is empty while it is as expected when opening any of the files with open_dataset instead.
TomNicholas commentedon Dec 24, 2018
If
open_mfdataset()is actually dropping the encoding, then this is an issue related to #1614. That's because inopen_mfdataset()while the attrs are explicitly set to those of the first supplied dataset, I don't see any similar explicit treatment of the encoding. I think that means the encoding is being set by what happens inside the core ofauto_combine(), and is presumably being lost upon some of theconcatormergeoperations which happen insideauto_combine().So I think to fix this then either
open_mfdataset()should contain explicit treatment of the encoding, or the rules for propagating the encoding through theauto_combine()should be solidified.TomNicholas commentedon Apr 5, 2020
@sbiner I know it's been a while, but I expect that #3498 and #3877 probably resolve your issue?
sbiner commentedon Apr 6, 2020
@TomNicholas I forgot about this sorry. I just made a quick check with the latest xarray master and I still have the problem ... see code.
Related question but maybe out of line, is there any way to know that the snw.time type is cftime.DatetimeNoLeap (as it is visible in the overview of
snw.time)?TomNicholas commentedon Apr 6, 2020
No worries!
#3498 added a new keyword argument to
open_mfdataset, to choose which file to load to attributes from, can you try using that?If this is the case, then to solve your original problem, you could also try using the
preprocessargument toopen_mfdatasetto store the encoding somewhere where it won't be lost? i.e.I'm not familiar with these classes, but presumably you mean more than just checking with
isinstance()? e.g.sbiner commentedon Apr 6, 2020
#3498 says something about a
master_filekeyword butxr.open_mfdatasetdoes not accept it and I do not see anything else similar in the documentation exceptattrs_filebut it is the first file by default and it did not return the calendar even when I specifiedattrs_file=l_f[0].I tried and it did not work ...
Yes, I was more thinking of something like
type(ds.time)which would returncftime.DatetimeNoLeapkeewis commentedon Apr 6, 2020
unfortunately,
numpydoes not allow us to putcftimeobject into dtypes (yet!), sods.time.valuesis anumpy.ndarraywith dtypeobject, containingcftimeobjects. To make the code work, use it withds.time.values[0]. Of course, that won't help if the array contains objects of more than one type.In #3498, the original proposal was to name the new kwarg
master_file, but later it was renamed toattrs_file. Ifl_fis a list of file paths, you used it correctly.Before trying to help with debugging your issue: could you post the output of
xr.show_versions()? That would help narrowing down on whether it's a dependency issue or a bug inxarray.Also, could you try to demonstrate your issue using a synthetic example? I've been trying to reproduce it with:
dcherian commentedon Apr 6, 2020
This example works without
attrs_filespecified.keewis commentedon Apr 6, 2020
I removed it since it doesn't change anything.
sbiner commentedon Apr 7, 2020
I use the following, which seems to work for me but I thought something shorter and more elegant could be done ...
Yes,
l_fis a list of file paths.Here is the output:
I used your code and it works for me also. I noticed the synthetic file is
NETCDF4and hasinttime variable while my files areNETCDF4_CLASSICand the time variable isdouble. I modified the synthetic code to produceNETCDF4_CLASSICfiles withdoubletime variable but it does not change the results: encoding does not have any values related to the calendar.Here is an output ouf
ncdump -hsfor one file, maybe it could help.I guess the next option could be to go into xarray code to try to find what the problem is but I would need some direction for doing this.
mickaellalande commentedon Jul 21, 2020
Description
Any news about this issue? I am facing the same problem and I had to get the calendars by hand... I tried to update xarray but there is still the same issue of missing the
ds.time.encodingwithopen_mfdatasetwhen the dimension to concatenate is the time (because the example from @keewis above works but the concatenation is onx).Step to reproduce
Here is a simple example to illustrate:
that gives:
{'source': '/home/lalandmi/.xarray_tutorial_data/air_temperature.nc', 'original_shape': (2920,), 'dtype': dtype('float32'), 'units': 'hours since 1800-01-01', 'calendar': 'standard'}Let's slipt this dataset and try to read it back with
open_mfdataset:that results in an empty dictionary:
{}Adding some arguments
attrs_file=paths[0]with the name of the files didn't change anything for me. Is there any other way or update about this?Xarray version
xr.show_versions()
[-]allow access to time attributes from netcdf files[/-][+]save "encoding" when using open_mfdataset[/+]corentincarton commentedon Jul 28, 2021
Any update about this issue? I'm working on a code where I want to make sure I have consistent calendars for all my inputs. Couldn't we add an option to use the encoding from the first file in the list or something?
open_mfdataset not playing well with encoding time when re-saving
open_mfdataset not playing well with encoding time when re-saving
open_mfdataset not playing well with encoding time when re-saving
open_mfdataset not playing well with encoding time when re-saving
open_mfdataset not playing well with encoding time when re-saving