You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sdap.apache.org by sk...@apache.org on 2023/02/16 05:14:45 UTC

[incubator-sdap-nexus] 01/01: Simplify _to_standard_index

This is an automated email from the ASF dual-hosted git repository.

skperez pushed a commit to branch SDAP-435-simple
in repository https://gitbox.apache.org/repos/asf/incubator-sdap-nexus.git

commit 28042eb0ce1aed7ada854fde26f937261a846ed4
Author: skorper <st...@gmail.com>
AuthorDate: Wed Feb 15 21:14:28 2023 -0800

    Simplify _to_standard_index
---
 data-access/nexustiles/dao/CassandraProxy.py | 66 ++++++++++------------------
 1 file changed, 23 insertions(+), 43 deletions(-)

diff --git a/data-access/nexustiles/dao/CassandraProxy.py b/data-access/nexustiles/dao/CassandraProxy.py
index 2d42edd..96f7c4c 100644
--- a/data-access/nexustiles/dao/CassandraProxy.py
+++ b/data-access/nexustiles/dao/CassandraProxy.py
@@ -226,57 +226,37 @@ class NexusTileData(Model):
         :rtype: np.array
         """
 
-        if desired_shape[0] == 1:
-            if not is_multi_var:
-                reshaped_array = np.ma.masked_all((desired_shape[1], desired_shape[2]))
-                row, col = np.indices(data_array.shape)
-
-                reshaped_array[np.diag_indices(desired_shape[1], len(reshaped_array.shape))] = data_array[
-                    row.flat, col.flat]
-                reshaped_array.mask[np.diag_indices(desired_shape[1], len(reshaped_array.shape))] = data_array.mask[
-                    row.flat, col.flat]
-                reshaped_array = reshaped_array[np.newaxis, :]
-            else:
-                reshaped_data_array = np.moveaxis(data_array, -1, 0)
-                reshaped_array = []
-
-                for variable_data_array in reshaped_data_array:
-                    variable_reshaped_array = np.ma.masked_all((desired_shape[1], desired_shape[2]))
-                    row, col = np.indices(variable_data_array.shape)
-
-                    variable_reshaped_array[np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = \
-                    variable_data_array[
-                        row.flat, col.flat]
-                    variable_reshaped_array.mask[
-                        np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = \
-                    variable_data_array.mask[
-                        row.flat, col.flat]
-                    reshaped_array.append(variable_reshaped_array[np.newaxis, :])
-        elif is_multi_var == True:
-            # Break the array up by variable. Translate shape from
-            # len(times) x len(latitudes) x len(longitudes) x num_vars,
-            # to
-            # num_vars x len(times) x len(latitudes) x len(longitudes)
+        reshaped_array = []
+        if is_multi_var:
             reshaped_data_array = np.moveaxis(data_array, -1, 0)
-            reshaped_array = []
+        else:
+            reshaped_data_array = [data_array]
 
-            for variable_data_array in reshaped_data_array:
+        for variable_data_array in reshaped_data_array:
+            if desired_shape[0] == 1:
+                variable_reshaped_array = np.ma.masked_all((desired_shape[1], desired_shape[2]))
+            else:
                 variable_reshaped_array = np.ma.masked_all(desired_shape)
-                row, col = np.indices(variable_data_array.shape)
 
-                variable_reshaped_array[np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = variable_data_array[
+            row, col = np.indices(variable_data_array.shape)
+
+            variable_reshaped_array[
+                np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = \
+                variable_data_array[
                     row.flat, col.flat]
-                variable_reshaped_array.mask[np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = variable_data_array.mask[
+            variable_reshaped_array.mask[
+                np.diag_indices(desired_shape[1], len(variable_reshaped_array.shape))] = \
+                variable_data_array.mask[
                     row.flat, col.flat]
+
+            if desired_shape[0] == 1:
+                reshaped_array.append(variable_reshaped_array[np.newaxis, :])
+            else:
                 reshaped_array.append(variable_reshaped_array)
-        else:
-            reshaped_array = np.ma.masked_all(desired_shape)
-            row, col = np.indices(data_array.shape)
 
-            reshaped_array[np.diag_indices(desired_shape[1], len(reshaped_array.shape))] = data_array[
-                row.flat, col.flat]
-            reshaped_array.mask[np.diag_indices(desired_shape[1], len(reshaped_array.shape))] = data_array.mask[
-                row.flat, col.flat]
+        if not is_multi_var:
+            # If single var, squeeze extra dim out of array
+            reshaped_array = reshaped_array[0]
 
         return reshaped_array