-
Notifications
You must be signed in to change notification settings - Fork 20
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
dask: Data_asreftime
; Data._asdatetime
; Data.year
& friends
#322
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -10,6 +10,7 @@ | |||
import dask.array as da | ||||
import numpy as np | ||||
|
||||
from ..cfdatetime import dt2rt, rt2dt | ||||
from ..functions import atol as cf_atol | ||||
from ..functions import rtol as cf_rtol | ||||
|
||||
|
@@ -439,3 +440,116 @@ def cf_where(array, condition, x, y, hardmask): | |||
array.harden_mask() | ||||
|
||||
return array | ||||
|
||||
|
||||
def _getattr(x, attr): | ||||
return getattr(x, attr, False) | ||||
|
||||
|
||||
_array_getattr = np.vectorize(_getattr, excluded="attr") | ||||
|
||||
|
||||
def cf_YMDhms(a, attr): | ||||
"""Return a date-time component from an array of date-time objects. | ||||
|
||||
Only applicable for data with reference time units. The returned | ||||
array will have the same mask hardness as the original array. | ||||
|
||||
.. versionadded:: TODODASK | ||||
|
||||
.. seealso:: `~cf.Data.year`, ~cf.Data.month`, `~cf.Data.day`, | ||||
`~cf.Data.hour`, `~cf.Data.minute`, `~cf.Data.second` | ||||
|
||||
:Parameters: | ||||
|
||||
a: `numpy.ndarray` | ||||
The array from which to extract date-time component. | ||||
|
||||
attr: `str` | ||||
The name of the date-time component, one of ``'year'``, | ||||
``'month'``, ``'day'``, ``'hour'``, ``'minute'``, | ||||
``'second'``. | ||||
|
||||
:Returns: | ||||
|
||||
`numpy.ndarray` | ||||
The date-time component. | ||||
|
||||
**Examples** | ||||
|
||||
>>> import numpy as np | ||||
>>> a = np.array([ | ||||
... cftime.DatetimeGregorian(2000, 1, 1, 0, 0, 0, 0, has_year_zero=False) | ||||
... cftime.DatetimeGregorian(2000, 1, 2, 0, 0, 0, 0, has_year_zero=False) | ||||
... ]) | ||||
>>> cf_YMDmhs(a, 'day') | ||||
array([1, 2]) | ||||
|
||||
""" | ||||
return _array_getattr(a, attr=attr) | ||||
|
||||
|
||||
def cf_rt2dt(a, units): | ||||
"""Convert an array of reference times to date-time objects. | ||||
|
||||
.. versionadded:: TODODASK | ||||
|
||||
.. seealso:: `cf._dt2rt`, `cf.Data._asdatetime` | ||||
|
||||
:Parameters: | ||||
|
||||
a: `numpy.ndarray` | ||||
An array of numeric reference times. | ||||
|
||||
units: `Units` | ||||
The units for the reference times | ||||
|
||||
|
||||
:Returns: | ||||
|
||||
`numpy.ndarray` | ||||
A array containing date-time objects. | ||||
|
||||
**Examples** | ||||
|
||||
>>> import numpy as np | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the equivalent reason to the above:
Suggested change
|
||||
>>> print(cf_rt2dt(np.array([0, 1]), cf.Units('days since 2000-01-01'))) | ||||
[cftime.DatetimeGregorian(2000, 1, 1, 0, 0, 0, 0, has_year_zero=False) | ||||
cftime.DatetimeGregorian(2000, 1, 2, 0, 0, 0, 0, has_year_zero=False)] | ||||
|
||||
""" | ||||
return rt2dt(a, units_in=units) | ||||
|
||||
|
||||
def cf_dt2rt(a, units): | ||||
"""Convert an array of date-time objects to reference times. | ||||
|
||||
.. versionadded:: TODODASK | ||||
|
||||
.. seealso:: `cf._rt2dt`, `cf.Data._asreftime` | ||||
|
||||
:Parameters: | ||||
|
||||
a: `numpy.ndarray` | ||||
An array of date-time objects. | ||||
|
||||
units: `Units` | ||||
The units for the reference times | ||||
|
||||
:Returns: | ||||
|
||||
`numpy.ndarray` | ||||
An array containing numeric reference times | ||||
|
||||
**Examples** | ||||
|
||||
>>> import numpy as np | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
>>> a = np.array([ | ||||
... cftime.DatetimeGregorian(2000, 1, 1, 0, 0, 0, 0, has_year_zero=False) | ||||
... cftime.DatetimeGregorian(2000, 1, 2, 0, 0, 0, 0, has_year_zero=False) | ||||
... ]) | ||||
>>> print(cf_dt2rt(a, cf.Units('days since 1999-01-01'))) | ||||
[365 366] | ||||
|
||||
""" | ||||
return dt2rt(a, units_out=units, units_in=None) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably unnecessary and somewhat distracting - the example snippet is not doctest-able anyway as-is unless you instead use
cf.data.dask_utils.cf_YMDmhs
...