geospace-code / georinex

Python RINEX 2 / 3 NAV / OBS / sp3 reader & batch convert to HDF5 with C-like speed
MIT License
216 stars 89 forks source link

SP3 to netCDF conversion: 't0' attribute serialization problem #95

Closed linustan closed 9 months ago

linustan commented 1 year ago

fixed by #96

xarray gets grumpy when trying to serialize the t0 attribute in the SP3 dataset generated by load_sp3, as it is a datetime object, but xarray cannot handle it because it wants a string, number, or list-like type.

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/georinex/read/__main__.py", line 51, in <module>
    data = gr.load(
           ^^^^^^^^
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/georinex/base.py", line 83, in load
    return load_sp3(rinexfn, outfn)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/georinex/sp3.py", line 113, in load_sp3
    ds.to_netcdf(outfn, mode="w", encoding=enc)
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/xarray/core/dataset.py", line 1912, in to_netcdf
    return to_netcdf(  # type: ignore  # mypy cannot resolve the overloads:(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/xarray/backends/api.py", line 1185, in to_netcdf
    _validate_attrs(dataset, invalid_netcdf=invalid_netcdf and engine == "h5netcdf")
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/xarray/backends/api.py", line 203, in _validate_attrs
    check_attr(k, v, valid_types)
  File "/Users/linus/.pyenv/versions/local/lib/python3.11/site-packages/xarray/backends/api.py", line 195, in check_attr
    raise TypeError(
TypeError: Invalid value for attr 't0': datetime.datetime(2023, 1, 23, 22, 29, 50). For serialization to netCDF files, its value must be of one of the following types: str, Number, ndarray, number, list, tuple

It seems that the library is not handling this case, and it should have a mechanism to handle this case by converting the datetime object to a string, or a number before writing it to the netCDF file.

Repro:

> python -m georinex.read -o out.nc in.sp3
linustan commented 1 year ago

PR to fix this issue: https://github.com/geospace-code/georinex/pull/96