-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
WRF output : cannot serialize variable #1809
Comments
Would you mind printing |
It looks like your variable has an attribute |
Did you ever find a solution to this? |
See https://stackoverflow.com/questions/50475453/xarray-cannot-serialize-coordinates/50475925. It seems like WRF may often produce netCDF files that lead to this issue. If the problem can be reproduced by simply opening and resaving a netCDF file then we may want to revisit our logic in xarray, because we always want |
Thanks for the solution (that was my SO post as well). Deleting the attrs.['coordinates'] was a clean workaround. Here's the requested WRF meta. It was concatenated with NCO, but meta should be consistent. |
Hello, I realized I never followed up with this but am again running into issues. The real problem for me is that deleting coordinates on 30+ variables isn't really feasible. Is it possible to delete the coordinates attribute for all variables? |
@gbromley The best option is probably a loop, e.g., def remove_problematic_attrs(ds):
for variable in ds.variables.values():
if 'coordinates' in variable.attrs:
del variable.attrs['coordinates'] |
That's perfect. Thank you! |
I ran into this issue trying to roundtrip a WRF output file. It looks like xarray raises an error for any NetCDF file that has variables with a # These coordinates are saved according to CF conventions
for var_name, coord_names in variable_coordinates.items():
attrs = variables[var_name].attrs
if 'coordinates' in attrs:
raise ValueError('cannot serialize coordinates because variable '
"%s already has an attribute 'coordinates'"
% var_name)
attrs['coordinates'] = ' '.join(map(str, coord_names))
Or perhaps I'm missing something obvious here... Let me know either way. I'd be happy to make a PR to patch this. |
I can't seem recreate this with a minimal example, from netCDF4 import Dataset
import xarray as xr
with Dataset('test.nc', format='NETCDF4', mode='w') as nc:
nc.createDimension('dim1', size=0)
var = nc.createVariable('var1', 'f8', dimensions=('dim1'))
var[:] = [1., 2., 3.]
var.setncattr('coordinates', 'dim1')
xr.open_dataset('test.nc').to_netcdf('test2.nc') There is something peculiar about how WRF handles the Interestingly, I can workaround the WRF xr.open_dataset('wrfout_d01_2019-04-16_15_00_00', decode_coords=False).to_netcdf('test.nc') while this doesn't: xr.open_dataset('wrfout_d01_2019-04-16_15_00_00').to_netcdf('test.nc') |
Hi, not sure if WRF has fixed this inconsistency since back then. But I'm trying to add some more insight to this issue, to probably fix it in some way: Here the coordinates decoding is done: Lines 443 to 450 in af1c709
The major issue here is, that the coordinates are only correctly propagated to We could at least warn here and remove XTIME from the decoded coordinates to read the dataset with XLONG/XLAT as coordinates. We could also raise a meaningful error. For this to work |
Code Sample, a copy-pastable example if possible
Problem description
This dataset is the wrfinput_d01 file for use with the WRF model. Reading and modifying the variables worked, but I cannot figure out how to write the changes out to the file. I get the above error. I saw another post about the same issue, but wasn't sure how it was resolved.
The text was updated successfully, but these errors were encountered: