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

Closed
cywhale opened this issue Nov 26, 2024 · 4 comments · Fixed by #413
Closed
Assignees
Labels
bug Something isn't working internals Internal machinery

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

@gmaze
Copy link
Member

gmaze commented Nov 26, 2024

Hi @cywhale
thanks for raising a very well documented issue !

This bug indeed went under our radar and CI tests.

I'm fixing it right now in #413

@gmaze gmaze self-assigned this Nov 26, 2024
@gmaze gmaze added bug Something isn't working internals Internal machinery labels Nov 26, 2024
@gmaze
Copy link
Member

gmaze commented Nov 26, 2024

I think this is the origin:

The error was in

Fetched_constraints = UriCName(Fetched_url)

because later on we had:
this_ds.attrs["Fetched_constraints"] = Fetched_constraints

instead of Fetched_constraints.cname because the cname property is a string

@gmaze
Copy link
Member

gmaze commented Nov 26, 2024

Seems to be working, I merged the fix to the main branch.
So this bug should be fixed in the next release.

In the mean time you can install argopy from the repo to benefit from the fix:

pip install git+http://github.com/euroargodev/argopy.git@master

@cywhale
Copy link
Author

cywhale commented Nov 27, 2024

pip install git+http://github.com/euroargodev/argopy.git@master

Ya, confirmed it. Thanks for the quick revision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working internals Internal machinery
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants