Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

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:

...

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".

Code Block
languagepy
titlegrib_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)

...