...
Code Block |
---|
language | py |
---|
title | Reading and converting to NetCDF |
---|
collapse | true |
---|
|
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:
Code Block |
---|
language | bash |
---|
theme | RDark |
---|
collapse | true |
---|
|
user@host:/path/to/executable$ ./grib_to_netcdf.py -i donwload.grib -o download.nc |
Code Block |
---|
language | py |
---|
title | Example executatablegrib_to_netcdf.py |
---|
collapse | true |
---|
|
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)
|
...