From 331c21913440affe342767b602a0f2671edeb722 Mon Sep 17 00:00:00 2001 From: Nikolay Koldunov Date: Thu, 29 Feb 2024 21:04:58 +0100 Subject: [PATCH] Add missing_value option to save_data function --- src/fint/fint.py | 13 +++++++++++++ test/Taskfile.yml | 9 +++++++++ 2 files changed, 22 insertions(+) diff --git a/src/fint/fint.py b/src/fint/fint.py index 64c8a55..5008f72 100644 --- a/src/fint/fint.py +++ b/src/fint/fint.py @@ -443,6 +443,7 @@ def save_data( lon, lat, out_path, + missing_value=None, ): """ Saves the interpolated data to a NetCDF file. @@ -477,6 +478,10 @@ def save_data( else: out_time = np.atleast_1d(data.time.data[timesteps]) + if missing_value is not None: + interpolated3d = np.where(np.isnan(interpolated3d), missing_value, interpolated3d) + + out1 = xr.Dataset( {variable_name: (["time", "depth", "lat", "lon"], interpolated3d)}, coords={ @@ -678,6 +683,12 @@ def fint(args=None): Valid units are 'D' (days), 'h' (hours), 'm' (minutes), 's' (seconds). \ To substract timedelta, put argument in quotes, and prepend ' -', so SPACE and then -, e.g. ' -10D'.", ) + parser.add_argument( + "--missing_value", + default=None, + type=float, + help="Missing value for the output file. Default is None.", + ) args = parser.parse_args() @@ -1065,6 +1076,7 @@ def fint(args=None): lon, lat, out_path, + missing_value=args.missing_value, ) if args.rotate: save_data( @@ -1079,6 +1091,7 @@ def fint(args=None): lon, lat, out_path2, + missing_value=args.missing_value, ) diff --git a/test/Taskfile.yml b/test/Taskfile.yml index 910f17b..4f50513 100644 --- a/test/Taskfile.yml +++ b/test/Taskfile.yml @@ -23,6 +23,7 @@ tasks: - task: 3d_nodes_interfaces - task: 2d_elements - task: timedelta + - task: missing_value - task: cleanup download_test_data: @@ -153,6 +154,14 @@ tasks: - fint ${FILE} ${MESH} ${INFL} -t 0:10 --timedelta '1h' - fint ${FILE} ${MESH} ${INFL} -t 0:10 --timedelta ' -1h' + missing_value: + deps: + - download_test_data + env: + FILE: "./test/data/ssh.fesom.1948.nc" + cmds: + - fint ${FILE} ${MESH} ${INFL} -t 0:3 --missing_value -9999 + cleanup: cmds: - python -c "import os; [os.remove(f) for f in os.listdir() if f.endswith('.nc')]"