Skip to content

Converted XTIME from datetime64 to timedelta, in line with its description #71

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

Merged
merged 5 commits into from
Apr 20, 2022
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
20 changes: 19 additions & 1 deletion tests/test_postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ def sample_dataset(request):
return xwrf.tutorial.open_dataset(request.param)


@pytest.fixture(scope='session')
def sample_dataset_with_kwargs(request):
return xwrf.tutorial.open_dataset(request.param[0], **request.param[1])


@pytest.mark.parametrize('sample_dataset', ['dummy'], indirect=True)
@pytest.mark.parametrize('variable', ('Q2', 'PSFC'))
def test_cf_attrs_added(sample_dataset, variable):
Expand Down Expand Up @@ -87,11 +92,24 @@ def test_rename_dims(sample_dataset):
def test_decode_times(times):
ds = xr.Dataset({'Times': times})
dsa = xwrf.postprocess._decode_times(ds)
assert dsa['Time'].dtype == 'datetime64[ns]'
assert np.issubdtype(dsa['Time'].dtype, np.datetime64)
assert dsa['Time'].attrs['long_name'] == 'Time'
assert dsa['Time'].attrs['standard_name'] == 'time'


@pytest.mark.parametrize(
'sample_dataset_with_kwargs,xtime_dtype',
[
(('mercator', {'decode_times': True}), np.timedelta64),
(('mercator', {'decode_times': False}), np.float32),
],
indirect=['sample_dataset_with_kwargs'],
)
def test_xtime_handling(sample_dataset_with_kwargs, xtime_dtype):
dataset = xwrf.postprocess._decode_times(sample_dataset_with_kwargs)
assert np.issubdtype(dataset.XTIME.dtype, xtime_dtype)


@pytest.mark.parametrize('sample_dataset', ['lambert_conformal'], indirect=True)
def test_assign_coord_to_dim_of_different_name(sample_dataset):
dataset = sample_dataset.pipe(xwrf.postprocess._collapse_time_dim).pipe(
Expand Down
9 changes: 9 additions & 0 deletions xwrf/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import re
import warnings

import numpy as np
import pandas as pd
import xarray as xr

Expand All @@ -24,6 +25,14 @@ def _decode_times(ds: xr.Dataset) -> xr.Dataset:
)
ds = ds.assign_coords({'Time': _time})
ds.Time.attrs = {'long_name': 'Time', 'standard_name': 'time'}
# make XTIME be consistent with its description
if 'XTIME' in ds.variables and np.issubdtype(ds.XTIME.dtype, np.datetime64):
ds['XTIME'].data = (
ds.XTIME.data
- pd.to_datetime(
ds['XTIME'].description, format='minutes since %Y-%m-%d %H:%M:%S'
).to_datetime64()
)
return ds


Expand Down