-
Notifications
You must be signed in to change notification settings - Fork 12
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
[Refactor]: Possibly refactor our logic for converting numeric time values to datetime #310
Comments
Can you handle all the calendars that can be found in climate models (calendars in CF convention)? @oliviermarti mentioned problems he had with understanding and working with What I remember, is how easy it was to work with the component times and relative times in Also, being able to easily check the actual dates of a time axis, using the
|
@jypeter, I couldn't agree more. The (cdm315) bash-4.2$ python
Python 3.10.5 | packaged by conda-forge | (main, Jun 14 2022, 07:04:59) [GCC 10.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cdtime as cdt
>>> dir(cdt)
['Calendar360', 'ClimCalendar', 'ClimLeapCalendar', 'Day', 'Days', 'DefaultCalendar', 'GregorianCalendar',
'Hour', 'Hours', 'JulianCalendar', 'Minute', 'Minutes', 'MixedCalendar', 'Month', 'Months', 'NoLeapCalendar',
'Season', 'Seasons', 'Second', 'Seconds', 'StandardCalendar', 'Week', 'Weeks', 'Year', 'Years', '__all__',
'__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__',
'__spec__', '_cdtime', 'abstime', 'c2r', 'compare', 'componenttime', 'compreltime', 'comptime', 'dump',
'error', 'numpy', 'r2c', 'r2r', 'relativetime', 'reltime', 's2c', 's2r']
# example use component time
>>> ct = cdt.comptime(2022,8,12,11,16,00)
>>> ct
2022-8-12 11:16:0.0
>>> print(ct.add(36, cdt.Hours))
2022-8-13 23:15:60.0
# example use relative time
>>> rt = cdt.reltime(11,"days since 2021-8-1 0:0:0")
>>> rt
11.000000 days since 2021-8-1 0:0:0
>>> print(rt.add(1., cdt.Day))
12.000000 days since 2021-8-1 0:0:0
# comptime and reltime classes
>>> dir(ct)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'add', 'cmp',
'sub', 'tocomp', 'tocomponent', 'torel', 'torelative']
>>> dir(rt)
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__',
'__gt__', '__hash__', '__init__', '__init_subclass__', '__le__', '__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'add', 'cmp', 'sub',
'tocomp', 'tocomponent', 'torel', 'torelative'] |
This ticket illustrates some issues with
And you can also format the time axis and perform cftime operations. |
I found that Our implementation of |
I found some existing functions that convert numeric time values to datetime objects (which we need specifically for decoding non-CF compliant time units). We might be able to refactor our implementation of this function, or even remove it entirely and use one of these instead.
Our implementation:
_get_cftime_coords()
xcdat/xcdat/dataset.py
Lines 694 to 733 in 4c51879
cftime.num2date
API Docs: https://unidata.github.io/cftime/api.html#cftime.num2date
API Code: https://github.com/Unidata/cftime/blob/76336464e27809c55471f360c46bfbea78787057/src/cftime/_cftime.pyx#L507-L628
EDIT 9/27/22 This function does not meet our requirements because "months since is allowed only for the 360_day calendar and common_years since is allowed only for the 365_day calendar."
xr.coding.times.decode_cf_datetime()
This function uses
cftime.num2date()
.API Code: https://github.com/pydata/xarray/blob/f8fee902360f2330ab8c002d54480d357365c172/xarray/coding/times.py#L253-L302
Docstring
This one might now work because the docstring says, "Note that time unit in
units
must not be smaller than microseconds and not larger than days."The text was updated successfully, but these errors were encountered: