2

I'm trying to use xarray to append and write a netCDF file. Some info on the original file is below.

Dimensions:    (Time: 744, south_north: 289, west_east: 339)
Coordinates:
XLAT       (Time, south_north, west_east) float32 ...
XLONG      (Time, south_north, west_east) float32 ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
Times      (Time) |S19 ...
RAINNC     (Time, south_north, west_east) float32 ...
I_RAINNC   (Time, south_north, west_east) int32 ...
SNOWNC     (Time, south_north, west_east) float32 ...
GRAUPELNC  (Time, south_north, west_east) float32 ...
HAILNC     (Time, south_north, west_east) float32 ...  

Here's some information on the appended file (two variables added).

Dimensions:    (Time: 744, south_north: 289, west_east: 339)
Coordinates:
XLAT       (Time, south_north, west_east) float32 ...
XLONG      (Time, south_north, west_east) float32 ...
Dimensions without coordinates: Time, south_north, west_east
Data variables:
Times      (Time) |S19 ...
RAINNC     (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0
I_RAINNC   (Time, south_north, west_east) int32 0 0 0 0 0 0 0 0 0 0 0 0 
SNOWNC     (Time, south_north, west_east) float32 ...
GRAUPELNC  (Time, south_north, west_east) float32 ...
HAILNC     (Time, south_north, west_east) float32 ...
PRCP       (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0 
CUMPRCP    (Time, south_north, west_east) float32 0.0 0.0 0.0 0.0 0.0

I'm attempting to write a new file with xarray.to_netcdf(), but am receiving this error:

ValueError: cannot serialize coordinates because variable RAINNC 
already has an attribute 'coordinates'

Any ideas on how to resolve this error?

EDIT:

Data was generated through Weather and Research Forecasting Model (WRF). Data loaded via open_dataset() with decode_cf at default. Attributes for RAINNC are the same for both files and listed below.

OrderedDict([('FieldType', 104),
         ('MemoryOrder', 'XY '),
         ('description', 'ACCUMULATED TOTAL GRID SCALE PRECIPITATION'),
         ('units', 'mm'),
         ('stagger', ''),
         ('coordinates', 'XLONG XLAT XTIME')])

2 Answers 2

3

How did you load the first xarray.Dataset? What attributes are on the RAINNC variable, (1) in the netCDF file, (2) in the original dataset loaded with xarray and (3) in the combined dataset?

This error message is telling you that RAINNC has a 'coordinates' attribute in your combined datasets. Xarray is raising an error because it uses the 'coordinates' attribute (according to CF conventions) to save coordinates (in your case, XLAT and XLONG) to netCDF files. However, it won't do that if there is a preexisting 'coordinates' attribute, to avoid over-writing existing data.

Usually this doesn't come up, because preexisting 'coordinates' attributes in netCDF files on disk are removed when variables are added to coords in xarray's data model. However, it could arise if you are using xarray.open_dataset() with decode_cf=False, or if used some sort of manual logic to create the Dataset.

A simple work-around around would be to remove any 'coordinates' attributes from data variables in your combined dataset (e.g., del ds['RAINNC'].attrs['coordinates']), but you should definitely look at those values first to make sure you aren't removing important metadata.

Sign up to request clarification or add additional context in comments.

1 Comment

Data was generated through Weather and Research Forecasting Model (WRF). Data loaded via open_dataset() with decode_cf at default. Attributes for RAINNC are the same for both files. Effective workaround, thank you.
1

I ran into the same issue with WRF files and found an easier workaround, which is to set decode_coords=False when invoking xarray.open_dataset(), for example:

xr.open_dataset('wrfout_d01_2019-04-16_15_00_00', decode_coords=False).to_netcdf('test.nc')

Further discussed here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.