Skip to content

Commit

Permalink
Merge pull request #353 from davidhassell/dask-array
Browse files Browse the repository at this point in the history
dask: `Data.array` and friends
  • Loading branch information
davidhassell authored Mar 18, 2022
2 parents 6866ffd + 4e51892 commit c684ca6
Show file tree
Hide file tree
Showing 10 changed files with 681 additions and 264 deletions.
2 changes: 1 addition & 1 deletion cf/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@
)

# Check the version of cftime
_minimum_vn = "1.5.0"
_minimum_vn = "1.6.0"
if LooseVersion(cftime.__version__) < LooseVersion(_minimum_vn):
raise RuntimeError(
f"Bad cftime version: cf requires cftime>={_minimum_vn}. "
Expand Down
32 changes: 26 additions & 6 deletions cf/cfdatetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from .functions import _DEPRECATION_ERROR_CLASS

_default_calendar = "gregorian"
default_calendar = "gregorian"

# --------------------------------------------------------------------
# Mapping of CF calendars to cftime date-time objects
Expand All @@ -21,6 +21,21 @@
("julian",): cftime.DatetimeJulian,
}

canonical_calendar = {
None: "standard",
"gregorian": "standard",
"standard": "standard",
"proleptic_gregorian": "proleptic_gregorian",
"julian": "julian",
"noleap": "noleap",
"365_day": "noleap",
"all_366_day": "all_leap",
"all_leap": "all_leap",
"": "",
"none": "",
}


_calendar_map = {None: "gregorian"}


Expand Down Expand Up @@ -138,7 +153,7 @@ def dt(
(year, month, day, hour, minute, second) = arg.timetuple()[:6]
microsecond = arg.microsecond
if calendar == "":
calendar = _default_calendar
calendar = default_calendar

else:
year = arg
Expand Down Expand Up @@ -426,6 +441,9 @@ def rt2dt(array, units_in, units_out=None, dummy1=None):
array, units, calendar, only_use_cftime_datetimes=True
)

if not isinstance(array, np.ndarray):
array = np.array(array, dtype=object)

return array


Expand Down Expand Up @@ -462,15 +480,17 @@ def dt2rt(array, units_in, units_out, dummy1=None):
An array of numbers with the same shape as *array*.
"""
ndim = np.ndim(array)
isscalar = np.ndim(array)

# array = units_out._utime.date2num(array)
array = cftime.date2num(
array, units=units_out.units, calendar=units_out._utime.calendar
)

if not ndim:
array = np.asanyarray(array)
if isscalar:
if array is np.ma.masked:
array = np.ma.masked_all(())
else:
array = np.asanyarray(array)

return array

Expand Down
10 changes: 2 additions & 8 deletions cf/data/creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,19 +70,13 @@ def convert_to_builtin_type(x):
raise TypeError(f"{type(x)!r} object is not JSON serializable: {x!r}")


def to_dask(array, chunks, dask_from_array_options):
def to_dask(array, chunks, **from_array_options):
"""TODODASK.
.. versionadded:: 4.0.0
"""
if "chunks" in dask_from_array_options:
raise TypeError(
"Can't define chunks in the 'dask_from_array_options' "
"dictionary. Use the 'chunks' parameter instead"
)

kwargs = dask_from_array_options.copy()
kwargs = from_array_options
kwargs.setdefault("asarray", getattr(array, "dask_asarray", None))
kwargs.setdefault("lock", getattr(array, "dask_lock", False))

Expand Down
Loading

0 comments on commit c684ca6

Please sign in to comment.