diff --git a/climax/core/cli.py b/climax/core/cli.py index 5d553100e7d09adce542dd0df2c365c300e8c60f..949af56b018c9bbb689ea47b418924eb1794f5d9 100644 --- a/climax/core/cli.py +++ b/climax/core/cli.py @@ -132,6 +132,10 @@ def preprocess_era5_parser(): # positional arguments + # positional argument: path to the target grid file + parser.add_argument('grid', type=pathlib.Path, + help='Path to the target grid file.') + # positional argument: path to search for ERA5 NetCDF files parser.add_argument('source', type=pathlib.Path, help='Path to search for ERA5 NetCDF files.') @@ -162,4 +166,10 @@ def preprocess_era5_parser(): .format(default)), default=False, nargs='?', const=True, metavar='') + # optional argument: resampling mode + parser.add_argument('-m', '--mode', type=str, + help='Resampling mode {}.'.format(default), + default='bilinear', choices=CDO_RESAMPLING_MODES, + metavar='') + return parser diff --git a/climax/main/preprocess_ERA5.py b/climax/main/preprocess_ERA5.py index 6ed9a868399909594621e9d82a61a0b07bbf092b..19a76de09ecdf203d8f42b94a2ff05279a9acf79 100644 --- a/climax/main/preprocess_ERA5.py +++ b/climax/main/preprocess_ERA5.py @@ -15,6 +15,7 @@ import xarray as xr # locals from climax.core.cli import preprocess_era5_parser from climax.core.constants import ERA5_VARIABLES +from climax.core.utils import reproject_cdo from pysegcnn.core.logging import log_conf from pysegcnn.core.utils import search_files from pysegcnn.core.trainer import LogConfig @@ -41,6 +42,11 @@ if __name__ == '__main__': # check whether the source directory exists if args.source.exists(): + # check whether the target grid file exists + if not args.grid.exists(): + LOGGER.info('{} does not exist.'.format(args.grid)) + sys.exit() + # check whether a single variable is specified variables = ERA5_VARIABLES if args.variable is not None: @@ -92,6 +98,10 @@ if __name__ == '__main__': LOGGER.info('Compressing NetCDF: {}'.format(filename)) ds.to_netcdf(filename, engine='h5netcdf') + # reproject and resample to target grid + reproject_cdo(args.grid, filename, filename, mode=args.mode, + overwrite=args.overwrite) + else: LOGGER.info('{} does not exist.'.format(str(args.source))) sys.exit()