Time decoding for String data #9825
Replies: 3 comments 11 replies
-
Is there a CF attribute that tells you these values are timestamps in strings? Without that, I don't see how you can do this automatically. |
Beta Was this translation helpful? Give feedback.
-
In this simplified example there isn't. The workflow driving this case, however, would have some attributes that could be used, for example:
The other piece of information that could help identify this case automatically is that it is a string valued coordinate variable (one-dimensional variable with the same name as its dimension - CF, which refers to the netCDF User's Guide). This is perhaps the bigger thing to key off of to check if the values are ISO timestamps. @dopplershift had some ideas where this might fit within the codebase, so maybe he has some thoughts on how it could be best identified. |
Beta Was this translation helpful? Give feedback.
-
I want to clarify a few things: The behavior of encoding date/times in a
This is very much "in the wild", since this discussion is being prompted by a support request for the THREDDS team; the user is aggregating netCDF files using NCML and complaining that xarray gives an error when parsing the data returned by the THREDDS server in this case. I'll also note that the current documentation for
One could easily interpret "standard netCDF datetime format" as any of the options recommended by the netCDF User's Guide, though I understand if the resolution is "fix the docs" for that. The work-around isn't horrific: ds = xarray.open_dataset(opendap_url)
times = ds.time.values
dt64 = np.array([datetime.fromisoformat(t.decode('utf-8')).replace(tzinfo=None) for t in times], dtype='datetime64[ns]')
ds2 = ds.assign_coords(time=dt64) (suggestions for improvement welcome). The question is if there was an appetite for supporting another "standard netCDF datetime format" directly within xarray, and where we might possibly look to put that if we were putting together a PR. |
Beta Was this translation helpful? Give feedback.
-
As a simplified example, let say I have a netCDF4 file that contains a time variable where time is encoded as ISO 8601 strings:
While not CF compliant, with a little work these time values can be decoded for better use in xarray using something like:
Side note: when reading this through OPeNDAP, the
times
array hasdtype='|S64'
, so the values need to be decoded toutf-8
inside thedatetime.fromisoformat
call.My question is if there is already way to decode these time values using
xarray.open_dataset
directly without the extra steps, and if not, would there be interest in a contribution to enable that extra bit of flexibility?Beta Was this translation helpful? Give feedback.
All reactions