Skip to content
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

Fetched_constraints is a class of argopy.utils.format.UriCName cannot directly save to netcdf #412

Open
cywhale opened this issue Nov 26, 2024 · 0 comments

Comments

@cywhale
Copy link

cywhale commented Nov 26, 2024

Reproducible code:

ds = DataFetcher().float(5903377).to_xarray()
ds
ds.to_netcdf("./argo_data.nc")

Error message
File ~/.pyenv/versions/py312numpy1/lib/python3.12/site-packages/xarray/backends/api.py:1292, in to_netcdf(dataset, path_or_file, mode, format, group, engine, encoding, unlimited_dims, compute, multifile, invalid_netcdf)
1290 # validate Dataset keys, DataArray names, and attr keys/values
1291 _validate_dataset_names(dataset)
...
197 "netCDF files, its value must be of one of the following types: "
198 f"{', '.join([vtype.name for vtype in valid_types])}"
199 )

TypeError: Invalid value for attr 'Fetched_constraints': WMO5903377. For serialization to netCDF files, its value must be of one of the following types: str, Number, ndarray, number, list, tuple

Debugging:

for attr, value in ds.attrs.items():
    print(f"{attr}: {type(value)}")
    if not isinstance(value, (str, int, float, list, tuple, np.ndarray)):
        print(f"Converting {attr} to string")
        ds.attrs[attr] = str(value)  # Convert unsupported types to string

ds.to_netcdf("../tmp/argo_data.nc")

Fetched_constraints: <class 'argopy.utils.format.UriCName'>
Converting Fetched_constraints to string

and then save done

Problem Description

I think it come from
#400

ds.attrs["Fetched_constraints"] = UriCName(url).cname

Versions

Output of `argopy.show_versions()`

SYSTEM

commit: None
python: 3.12.5 (main, Sep 2 2024, 14:21:15) [GCC 13.2.0]
python-bits: 64
OS: Linux
OS-release: 6.8.0-45-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: zh_TW.UTF-8
LOCALE: zh_TW.UTF-8
libhdf5: 1.14.2
libnetcdf: 4.9.3-development

INSTALLED VERSIONS: CORE

aiohttp : 3.10.5
argopy : 1.0.0
decorator : 5.1.1
erddapy : 2.2.0
fsspec : 2023.10.0
netCDF4 : 1.7.1.post2
...

INSTALLED VERSIONS: PIP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant