Skip to content

Commit 509c96e

Browse files
committed
Amended time parsing and unit handling
1 parent a4c21b9 commit 509c96e

File tree

4 files changed

+36
-143
lines changed

4 files changed

+36
-143
lines changed

xwrf/accessors.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
_collapse_time_dim,
77
_decode_times,
88
_modify_attrs_to_cf,
9-
_remove_units_from_bool_arrays,
9+
_remove_invalid_units,
1010
)
1111

1212

@@ -41,7 +41,7 @@ def postprocess(self, decode_times=True) -> xr.Dataset:
4141
"""
4242
ds = (
4343
self.xarray_obj.pipe(_modify_attrs_to_cf)
44-
.pipe(_remove_units_from_bool_arrays)
44+
.pipe(_remove_invalid_units)
4545
.pipe(_collapse_time_dim)
4646
)
4747
if decode_times:

xwrf/config.yaml

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,13 @@ time_coords:
2222
- Time
2323
- time
2424

25-
boolean_units_attrs:
25+
invalid_units_attrs:
2626
- '-'
2727
- flag
28+
- '0/1 Flag'
29+
- 'whoknows'
30+
- 'category'
31+
- 'none'
2832

2933
cf_attribute_map:
3034
ZNW:
@@ -92,3 +96,23 @@ cf_attribute_map:
9296
units: Pa
9397
ST:
9498
units: kelvin
99+
SM100255:
100+
units: dimensionless
101+
SM028100:
102+
units: dimensionless
103+
SM007028:
104+
units: dimensionless
105+
SM000007:
106+
units: dimensionless
107+
SCB_DOM:
108+
units: dimensionless
109+
SCB_DOM:
110+
units: dimensionless
111+
COSALPHA_V:
112+
units: dimensionless
113+
GREENFRAC:
114+
units: dimensionless
115+
SOILTEMP:
116+
units: kelvin
117+
RH:
118+
units: %

xwrf/io_plugin.py

Lines changed: 0 additions & 134 deletions
This file was deleted.

xwrf/postprocess.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,24 @@ def _decode_times(ds: xr.Dataset) -> xr.Dataset:
1010
"""
1111
Decode the time variable to datetime64.
1212
"""
13+
try:
14+
_time = pd.to_datetime(ds.Times.data.astype('str'), errors='raise', format='%Y-%m-%d_%H:%M:%S')
15+
except ValueError:
16+
_time = pd.to_datetime(ds.Times.data.astype('str'), errors='raise', format='%Y-%m-%dT%H:%M:%S.%f')
1317
ds = ds.assign_coords(
1418
{
15-
'Time': pd.to_datetime(
16-
ds.Times.data.astype('str'), errors='raise', format='%Y-%m-%d_%H:%M:%S'
17-
)
19+
'Time': _time
1820
}
1921
)
2022
ds.Time.attrs = {'long_name': 'Time', 'standard_name': 'time'}
2123
return ds
2224

2325

24-
def _remove_units_from_bool_arrays(ds: xr.Dataset) -> xr.Dataset:
25-
boolean_units_attrs = config.get('boolean_units_attrs')
26+
def _remove_invalid_units(ds: xr.Dataset) -> xr.Dataset:
27+
invalid_units_attrs = config.get('invalid_units_attrs')
28+
print(invalid_units_attrs)
2629
for variable in ds.data_vars:
27-
if ds[variable].attrs.get('units') in boolean_units_attrs:
30+
if ds[variable].attrs.get('units') in invalid_units_attrs:
2831
ds[variable].attrs.pop('units', None)
2932
return ds
3033

0 commit comments

Comments
 (0)