sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 04/04: Support the case where one of the CRS axes in a netCDF file is a scalar (number of dimensions = 0). In such case, the scale factor does not exist in the "gridToCRS" matrix; we have only a translation term. In other words, there is no corresponding source coordinate, only a target coordinate which is constant. Some netCDF files use scalar for specifying for example that all values are located at a height of 2 metres.
Date Thu, 07 Jan 2021 18:08:33 GMT
This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit f2701ff293df5c802e2281dcf38bab50b6f2b144
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Thu Jan 7 19:04:27 2021 +0100

    Support the case where one of the CRS axes in a netCDF file is a scalar (number of dimensions
= 0).
    In such case, the scale factor does not exist in the "gridToCRS" matrix; we have only
a translation term.
    In other words, there is no corresponding source coordinate, only a target coordinate
which is constant.
    Some netCDF files use scalar for specifying for example that all values are located at
a height of 2 metres.
---
 .../java/org/apache/sis/internal/netcdf/Axis.java  | 12 +++++++++--
 .../java/org/apache/sis/internal/netcdf/Grid.java  | 24 ++++++++++++++++++++++
 .../sis/internal/netcdf/ucar/GridWrapper.java      | 10 ++++++---
 3 files changed, 41 insertions(+), 5 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
index 281754d..5c1db79 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
@@ -669,9 +669,17 @@ public final class Axis extends NamedElement {
     {
         switch (getNumDimensions()) {
             /*
-             * Defined as a matter of principle, but should never happen.
+             * Variable is a scalar, which is represented by an array of length 1.
+             * There is no source dimension, only a target dimension fixed to a constant
value.
+             * Consequently the scale factor does not exist in the matrix, only the translation.
              */
-            case 0: return true;
+            case 0: {
+                final Vector data = read();
+                if (!data.isEmpty()) {
+                    gridToCRS.setElement(tgtDim, gridToCRS.getNumCol() - 1, data.doubleValue(0));
+                }
+                return true;
+            }
             /*
              * Normal case where the axis has only one dimension.
              */
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
index 432cef3..f3f3d45 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Grid.java
@@ -228,6 +228,30 @@ public abstract class Grid extends NamedElement {
                     i++;
                 }
             }
+            /*
+             * If some variables are scalar, those variables can safely be moved anywhere
because they do not
+             * use any input coordinate (their output coordinates are constant). Try to move
those scalars to
+             * more natural positions. We rely on `AxisDirection` ordering: North, East,
South, West, Up/Down,
+             * Future/Past directions in that order.
+             */
+            for (i=0; i < axes.length; i++) {
+                final Axis axis = axes[i];
+                if (axis.getNumDimensions() == 0) {
+                    int p = i;
+                    for (int j=0; j<axes.length; j++) {
+                        if (axis.direction.compareTo(axes[j].direction) > 0) {
+                            p = j + 1;      // After the last element that should be ordered
before `axis`.
+                        }
+                    }
+                    if (p != i) {
+                        if (p > i) p--;
+                        System.arraycopy(axes, i+1, axes, i, axes.length - (i+1));      //
Remove element at i.
+                        System.arraycopy(axes, p, axes, p+1, axes.length - (p+1));      //
Insert a space at p.
+                        axes[p] = axis;
+                        break;
+                    }
+                }
+            }
         }
         return axes;
     }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridWrapper.java
index 0896332..e102591 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridWrapper.java
@@ -292,11 +292,15 @@ next:       for (final String name : axisNames) {
          * in the same order we presume that this is the "netCDF" order (as opposed to a
"coordinates"
          * attribute value order).
          */
-        for (int i = Math.min(domain.size(), range.size()); --i >= 0;) {
+        int i = range.size();
+        int j = domain.size();
+        while (--i >= 0) {
             final List<Dimension> dimensions = range.get(i).getDimensions();
-            if (dimensions.size() != 1 || !dimensions.get(0).equals(domain.get(i))) {
-                return axes;
+            switch (dimensions.size()) {
+                case 0: continue;           // Ignore scalars as they can appear anywhere.
+                case 1: if (--j >= 0 && dimensions.get(0).equals(domain.get(j)))
continue;
             }
+            return axes;
         }
         ArraysExt.reverse(axes);
         return axes;


Mime
View raw message