From 29bcffdecc1d3090b3c10036ce621a129e8d8484 Mon Sep 17 00:00:00 2001
From: "Daniel.Frisinghelli" <daniel.frisinghelli@eurac.edu>
Date: Thu, 24 Jun 2021 10:25:24 +0200
Subject: [PATCH] Reproject and resample to target grid.

---
 climax/core/cli.py             | 10 ++++++++++
 climax/main/preprocess_ERA5.py | 10 ++++++++++
 2 files changed, 20 insertions(+)

diff --git a/climax/core/cli.py b/climax/core/cli.py
index 5d55310..949af56 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 6ed9a86..19a76de 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()
-- 
GitLab