To convert a GRIB file, it is necessary to read it (see Open CEMS-Flood Data for more details)
Supposing you have downloded a GRIB file that you have named "download.grib", then it is possible to save it to disk converted into a NetCDF4 in the following way:
Reading and converting to NetCDF
import xarray as xr ds = xr.open_dataset("download.grib", engine= "cfgrib") ds.to_netcdf("download.nc")
You can use the Python executable below to perform the conversion from the terminal. Copy and paste the content of the code block below and save it in a python file called "grib_to_netcdf.py".
grib_to_netcdf.py
import xarray as xr import argparse import sys def convert(input, output): try: ds = xr.open_dataset( input, engine='cfgrib', backend_kwargs={'indexpath':''} ) except FileNotFoundError: sys.exit("File was not found : {}".format(input)) ds.to_netcdf(output) if __name__ == "__main__": parser = argparse.ArgumentParser(description='GRIB to NetCDF converter\n', formatter_class=argparse.RawTextHelpFormatter) parser.add_argument('-i', dest='input', metavar='INPUT_FILE', required=True, help='INPUT_FILE') parser.add_argument('-o', dest='output', metavar='OUTPUT_FILE', required=True, help='OUTPUT_FILE') args = parser.parse_args() convert(args.input,args.output)
Run the executable with the input and output arguments:
user@host:/path/to/executable$ ./grib_to_netcdf.py -i donwload.grib -o download.nc