You are viewing an old version of this page. View the current version.
Compare with Current
View Page History
« Previous
Version 10
Next »
To convert a GRIB file, it is necessary to read it: Open CEMS-Flood Data.
Then it is possible to save it to disk:
import xarray as xr
ds = xr.open_dataset("download.grib", engine= "cfgrib")
ds.to_netcdf("download.nc")
It is possible to create a Python executable to use from the terminal:
user@host:/path/to/executable$ ./grib_to_netcdf.py -i donwload.grib -o download.nc
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)