From 14128e9edb8540fe162d42bdbd55104a97941be0 Mon Sep 17 00:00:00 2001 From: Francois Colleoni Date: Thu, 28 Mar 2024 10:08:41 +0100 Subject: [PATCH] FIX: Min Max computation for physiographic descriptor This commits resolves an issue when computing min and max value of a physiographic descriptor when there are missing values that are converted to -99 --- doc/source/release/1.0.0-notes.rst | 6 ++++++ smash/core/model/_read_input_data.py | 28 ++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/doc/source/release/1.0.0-notes.rst b/doc/source/release/1.0.0-notes.rst index aafea523..5fc4ab40 100644 --- a/doc/source/release/1.0.0-notes.rst +++ b/doc/source/release/1.0.0-notes.rst @@ -705,3 +705,9 @@ Error in projected gradient value with ANN mapping There was an error in the values of the projected gradient when using ANN mapping. The current iteration was printing the projected gradient of the next iteration. + +Error when computing min and max of physiograhic descriptor +*********************************************************** + +There was an error when computing the min and max of a physiograhic descriptor if there is no data (i.e., +sea in the domain). No data from tif file are converted to -99 which leads to a wrong minimal value. diff --git a/smash/core/model/_read_input_data.py b/smash/core/model/_read_input_data.py index 094cfc6b..16a8fbb3 100644 --- a/smash/core/model/_read_input_data.py +++ b/smash/core/model/_read_input_data.py @@ -152,7 +152,6 @@ def _read_qobs(setup: SetupDT, mesh: MeshDT, input_data: Input_DataDT): stacklevel=2, ) else: - ind_start_dat = max(0, start_diff) ind_end_dat = min(dat.index.max(), end_diff) ind_start_arr = max(0, -start_diff) @@ -461,15 +460,28 @@ def _read_descriptor(setup: SetupDT, mesh: MeshDT, input_data: Input_DataDT): miss.append(name) else: - input_data.physio_data.descriptor[..., i] = _read_windowed_raster(path[0], mesh) - input_data.physio_data.l_descriptor[i] = np.min(input_data.physio_data.descriptor[..., i]) - input_data.physio_data.u_descriptor[i] = np.max(input_data.physio_data.descriptor[..., i]) - # % Check if descriptors are uniform - if input_data.physio_data.l_descriptor[i] == input_data.physio_data.u_descriptor[i]: + desc = _read_windowed_raster(path[0], mesh) + mask = desc != -99.0 + + # % Check if descriptor contains only no data + if not np.any(mask): + raise ValueError( + f"Invalid descriptor '{name}'. It contains only missing values on the selected domain" + ) + + low = np.min(desc, where=mask, initial=np.inf) + upp = np.max(desc, where=mask, initial=-np.inf) + + # % Check if descriptor is uniform + if low == upp: raise ValueError( - f"Reading spatially uniform descriptor '{name}'. It must be removed to perform " - f"optimization" + f"Invalid descriptor '{name}'. Spatially uniform values on the selected domain. It must " + "be removed to perform optimization" ) + # % Assign values + input_data.physio_data.descriptor[..., i] = desc + input_data.physio_data.l_descriptor[i] = low + input_data.physio_data.u_descriptor[i] = upp if miss: warnings.warn(f"Missing {len(miss)} descriptor file(s): {miss}", stacklevel=2)