1

I try to read and write a netCDF file using xarray.

Originate file:

netcdf test {
dimensions:
        TIME = 1 ;
variables:
        double TIME(TIME) ;
        char VHM0_DM(TIME) ;

data:

 TIME = 25625;

 VHM0_DM =
  "D";
}

Python code:

import xarray as xr

ds = xr.open_dataset("test.nc")
ds.to_netcdf("test-write.nc")

Result:

netcdf test-write {
dimensions:
        TIME = 1 ;
        string1 = 1 ;
variables:
        double TIME(TIME) ;
                TIME:_FillValue = NaN ;
        char VHM0_DM(TIME, string1) ;
}

How can I prevent Xarray from:

  • creating new dimension string1
  • adding new variable attribute _FillValue
Ludo
  • 133
  • 1
  • 13

1 Answers1

0

One workaround is:

ds.to_netcdf("modified.nc", encoding={'VHM0_DM': {'dtype': 'str'}}) 

source: https://github.com/pydata/xarray/issues/2899

To remove _FillValue:

ds.to_netcdf("modified.nc", encoding={'TIME': {'_FillValue': None}, 'VHM0_DM': {'dtype': 'str'}})

source: xarray automatically applying _FillValue to coordinates on netCDF output

Note that it is now None instead of False.

Ludo
  • 133
  • 1
  • 13