-
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
better handling of non-cf-compliant time data #263
Merged
Merged
Changes from all commits
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
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
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 |
---|---|---|
|
@@ -41,9 +41,10 @@ def open_dataset( | |
the Dataset, by default True. Bounds are required for many xCDAT | ||
features. | ||
decode_times: bool, optional | ||
If True, decode times encoded in the standard NetCDF datetime format | ||
into datetime objects. Otherwise, leave them encoded as numbers. | ||
This keyword may not be supported by all the backends, by default True. | ||
If True, attempt to decode times encoded in the standard NetCDF | ||
datetime format into datetime objects. Otherwise, leave them encoded | ||
as numbers. This keyword may not be supported by all the backends, | ||
by default True. | ||
center_times: bool, optional | ||
If True, center time coordinates using the midpoint between its upper | ||
and lower bounds. Otherwise, use the provided time coordinates, by | ||
|
@@ -84,6 +85,7 @@ def open_dataset( | |
if cf_compliant_time is False: | ||
# XCDAT handles decoding time values with non-CF units. | ||
ds = xr.open_dataset(path, decode_times=False, **kwargs) | ||
# attempt to decode non-cf-compliant time axis | ||
ds = decode_non_cf_time(ds) | ||
else: | ||
ds = xr.open_dataset(path, decode_times=True, **kwargs) | ||
|
@@ -225,13 +227,14 @@ def decode_non_cf_time(dataset: xr.Dataset) -> xr.Dataset: | |
numerically encoded time values (representing the offset from the reference | ||
date) to pandas DateOffset objects. These offset values are added to the | ||
reference date, forming DataArrays of datetime objects that replace the time | ||
coordinate and time bounds (if they exist) values in the Dataset. | ||
coordinate and time bounds (if they exist) in the Dataset. | ||
|
||
Parameters | ||
---------- | ||
dataset : xr.Dataset | ||
Dataset with numerically encoded time coordinates and time bounds (if | ||
they exist). | ||
they exist). If the time coordinates cannot be decoded then the original | ||
dataset is returned. | ||
|
||
Returns | ||
------- | ||
|
@@ -304,7 +307,14 @@ def decode_non_cf_time(dataset: xr.Dataset) -> xr.Dataset: | |
time = ds.cf["T"] | ||
time_bounds = ds.get(time.attrs.get("bounds"), None) | ||
units_attr = time.attrs.get("units") | ||
units, ref_date = _split_time_units_attr(units_attr) | ||
|
||
# If the time units cannot be split into a unit and reference date, it | ||
# cannot be decoded so the original dateset is returned. | ||
try: | ||
units, ref_date = _split_time_units_attr(units_attr) | ||
except ValueError: | ||
return ds | ||
|
||
ref_date = pd.to_datetime(ref_date) | ||
|
||
data = [ref_date + pd.DateOffset(**{units: offset}) for offset in time.data] | ||
|
@@ -403,7 +413,13 @@ def _has_cf_compliant_time( | |
return None | ||
|
||
time = ds.cf["T"] | ||
units = _split_time_units_attr(time.attrs.get("units"))[0] | ||
|
||
# If the time units attr cannot be split, it is not cf_compliant. | ||
try: | ||
units = _split_time_units_attr(time.attrs.get("units"))[0] | ||
except ValueError: | ||
return False | ||
|
||
cf_compliant = units not in NON_CF_TIME_UNITS | ||
|
||
return cf_compliant | ||
|
@@ -589,6 +605,7 @@ def _preprocess_non_cf_dataset( | |
if callable: | ||
ds_new = callable(ds) | ||
|
||
# Attempt to decode non-cf-compliant time axis. | ||
ds_new = decode_non_cf_time(ds_new) | ||
|
||
return ds_new | ||
|
@@ -606,11 +623,23 @@ def _split_time_units_attr(units_attr: str) -> Tuple[str, str]: | |
------- | ||
Tuple[str, str] | ||
The units (e.g, "months") and the reference date (e.g., "1800-01-01"). | ||
If the units attribute doesn't exist for the time coordinates. | ||
|
||
Raises | ||
------ | ||
KeyError | ||
If the time units attribute was not found. | ||
|
||
ValueError | ||
If the time units attribute is not of the form `X since Y`. | ||
""" | ||
if units_attr is None: | ||
raise KeyError("No 'units' attribute found for the dataset's time coordinates.") | ||
raise KeyError("The dataset's time coordinates does not have a 'units' attr.") | ||
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. Updated a preexisting KeyError message |
||
|
||
units, reference_date = units_attr.split(" since ") | ||
if "since" in units_attr: | ||
units, reference_date = units_attr.split(" since ") | ||
else: | ||
raise ValueError( | ||
"This dataset does not have time coordinates of the form 'X since Y'." | ||
) | ||
|
||
return units, reference_date |
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.
I created test bounds for a dataset that is non-cf-compliant (but not handled by xcdat), but I didn't end up using these in the unit test I created (see below).