Changes to grib to netCDF converter on CDS-Beta/ADS-Beta

I’ve figured out a work around to use my existing codebase with the new netcdf4 ERA5 files by converting the new netcdf4 files back to netcdf3.

First run the python script (This is to rename variables back to their old variable names, change the order of pressure, and remove ‘expver’ and ‘numbers’ which causes issue with autoconversion from netcdf4 to 3):

import xarray as xr

# Open the slow file
ds = xr.open_dataset('ERA5.nc')

if 'expver' in ds.data_vars:
    ds = ds.drop_vars('expver')
    ds = ds.drop_vars('number')

ds = ds.rename({'valid_time': 'time', 'pressure_level': 'level'})
ds = ds.reindex(level=ds.level[::-1])

ds.to_netcdf('optimized_ERA5.nc')

Now, this file will still be in netcdf4 instead of netcdf3. You can check with

ncdump -k ERA5.nc

The slow file will report back “netcdf-4”, and old forecasts will report back “64-bit offset”

So next use nco to convert netcdf4 back to netcdf3 (I could not figure out how to do this conversion without running the above python script first to remove the expver variable)

ncks -6 optimized_ERA5.nc optimized_ERA5.nc

1 Like