Skip to content
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

Additional fixes for PR #158 #173

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ Top-level API

dataset.open_dataset
dataset.open_mfdataset
dataset.has_cf_compliant_time
dataset.decode_non_cf_time
dataset.infer_or_keep_var
dataset.decode_time_units
dataset.get_inferred_var

.. currentmodule:: xarray
Expand Down
50 changes: 25 additions & 25 deletions tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,20 @@
],
dims=["time"],
attrs={
"axis": "T",
"long_name": "time",
"standard_name": "time",
"axis": "T",
},
)
time_non_cf = xr.DataArray(
data=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
dims=["time"],
attrs={
"units": "months since 2000-01-01",
"calendar": "standard",
"axis": "T",
"long_name": "time",
"standard_name": "time",
"axis": "T",
},
)

Expand Down Expand Up @@ -72,18 +74,18 @@
time_bnds_non_cf = xr.DataArray(
name="time_bnds",
data=[
[datetime(1999, 12, 16, 12), datetime(2000, 1, 16, 12)],
[datetime(2000, 1, 16, 12), datetime(2000, 2, 15, 12)],
[datetime(2000, 2, 15, 12), datetime(2000, 3, 16, 12)],
[datetime(2000, 3, 16, 12), datetime(2000, 4, 16)],
[datetime(2000, 4, 16), datetime(2000, 5, 16, 12)],
[datetime(2000, 5, 16, 12), datetime(2000, 6, 16)],
[datetime(2000, 6, 16), datetime(2000, 7, 16, 12)],
[datetime(2000, 7, 16, 12), datetime(2000, 8, 16, 12)],
[datetime(2000, 8, 16, 12), datetime(2000, 9, 16)],
[datetime(2000, 9, 16), datetime(2000, 10, 16, 12)],
[datetime(2000, 10, 16, 12), datetime(2000, 11, 16)],
[datetime(2000, 11, 16), datetime(2000, 12, 16)],
[-1, 0],
[0, 1],
[1, 2],
[2, 3],
[3, 4],
[4, 5],
[5, 6],
[6, 7],
[7, 8],
[8, 9],
[9, 10],
[10, 11],
],
coords={"time": time_non_cf},
dims=["time", "bnds"],
tomvothecoder marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -172,19 +174,18 @@ def generate_dataset(cf_compliant: bool, has_bounds: bool) -> xr.Dataset:
)

if cf_compliant:
ds = ds.assign({"time_bnds": time_bnds.copy()})
ds = ds.assign_coords({"time": time_cf.copy()})
ds.coords["time"] = time_cf.copy()
ds["time_bnds"] = time_bnds.copy()
elif not cf_compliant:
ds = ds.assign({"time_bnds": time_bnds_non_cf.copy()})
ds = ds.assign_coords({"time": time_non_cf.copy()})
ds["time"] = ds.time.assign_attrs(units="months since 2000-01-01")
ds.coords["time"] = time_non_cf.copy()
ds["time_bnds"] = time_bnds_non_cf.copy()

# If the "bounds" attribute is included in an existing DataArray and
# added to a new Dataset, it will get dropped. Therefore, it needs to be
# assigned to the DataArrays after they are added to Dataset.
ds["lat"] = ds.lat.assign_attrs(bounds="lat_bnds")
ds["lon"] = ds.lon.assign_attrs(bounds="lon_bnds")
ds["time"] = ds.time.assign_attrs(bounds="time_bnds")
ds["lat"].attrs["bounds"] = "lat_bnds"
ds["lon"].attrs["bounds"] = "lon_bnds"
ds["time"].attrs["bounds"] = "time_bnds"

elif not has_bounds:
ds = xr.Dataset(
Expand All @@ -193,9 +194,8 @@ def generate_dataset(cf_compliant: bool, has_bounds: bool) -> xr.Dataset:
)

if cf_compliant:
ds = ds.assign_coords({"time": time_cf.copy()})
ds.coords["time"] = time_cf.copy()
elif not cf_compliant:
ds = ds.assign_coords({"time": time_non_cf.copy()})
ds["time"] = ds.time.assign_attrs(units="months since 2000-01-01")
ds.coords["time"] = time_non_cf.copy()

return ds
tomvothecoder marked this conversation as resolved.
Show resolved Hide resolved
Loading