-
Notifications
You must be signed in to change notification settings - Fork 16
Description
Relevant autotester PR: podaac/l2ss-py-autotest#3920
Version: 3.2.0rc4
Explanation:
The encoding of the time variable causes an error when trying to write out the subsetted netcdf.
This error occurs in 3.2.0rc4 at subset.py#L470.
Specifically the value of encoding['/geolocation']['time'] will be {'_FillValue': None, 'units': 's since 1993-01-01T00:00:00Z', 'dtype': dtype('float64')}. Xarray appears to expect the time units to be in an netcdf compliant format and attempts to find its numpy unit string via a look up table (see times.py#L133). So it is expecting 'seconds' and not 's'.
Since CF compliant unit strings follow a consistent format, a quick fix i put together off the dome and tested is when constructing the time_encoding dictionary (initialized @ subset.py#L326) checking if the unit is a a numpy datetime unit, and replacing it via a lookup table if it is.
from typing import get_args
from xarray.core.types import NPDatetimeUnitOptions
# taken from xarray.coding.times, could just import but it is a private
# method to the times module
def _numpy_to_netcdf_timeunit(units: NPDatetimeUnitOptions) -> str:
return {
"ns": "nanoseconds",
"us": "microseconds",
"ms": "milliseconds",
"s": "seconds",
"m": "minutes",
"h": "hours",
"D": "days",
}[units]
def check_time_units(unit_str: str) -> str:
unit_str_list = unit_str.split(" ")
unit = unit_str_list[0]
if unit in get_args(NPDatetimeUnitOptions):
unit_str_list[0] = _numpy_to_netcdf_timeunit(unit)
return " ".join(unit_str_list)And then replacing
l2ss-py/podaac/subsetter/subset.py
Line 356 in 6d63fd1
| time_encoding[group_path][var_name]['units'] = units |
time_encoding[group_path][var_name]["units"] = check_time_units(units)