CF Compliance of variable length strings written by xarray #392
Unanswered
jalder-usgs
asked this question in
Q&A about using CF
Replies: 1 comment 5 replies
-
Hi @jalder-usgs to get xarray to do the right CF thing here, you need to set the dtype of the encoding property of the DataArray to "S1", details in the io string encoding section of xarray's docs. If my memory isn't bad, you cannot pass in the encoding as part of the constructors for a DataArray, you need to set it after the object is created. Your example would become: import numpy as np
import xarray as xr
out_file = 'nc_string_test.nc'
strings = np.arange(0, 30).astype("str")
ds = xr.Dataset(
data_vars=dict(
str_variable=("strings", strings, dict(long_name = "strings of type string"))
),
attrs=dict(Conventions="CF-1.8")
)
ds.str_variable.encoding["dtype"] = "S1"
ds.to_netcdf(out_file) Output of
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Question
Hi all, I have a problem that has vexed me for some time that I hope we can pin down. I'm really having trouble writing string variables into NetCDF files and have them pass the cfchecks application. The documentation for strings is here. I've created a sample in Python using xarray that produces a similar scheme:
Here is the ncdump
From a high level, it looks a lot like the documentation. However, when I run cfchecks I get:
This has been plaguing me for some time now. I don't know if it is coming from xarray or the underlying netCDF4 python library, but this is a pretty simple example that you'd think should work. I've also tried xarray engine="h5netcdf" and get the same result. I'm ultimately trying to make CF-compliant Discrete Sampling Geometries time series NetCDF files, but I use string variables for the geometry ids. I also noticed the cfchecks application lists supported types as char, byte, short, int, float, real, double, but excluding strings, which seems to run counter to the CF-conventions documentation. Is this an issue with xarray or the cfchecker application itself?
Any help would be appreciated.
Beta Was this translation helpful? Give feedback.
All reactions