sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] branch geoapi-4.0 updated: Allow special case for parsing units of measurement in GCOM-W files.
Date Tue, 04 Feb 2020 11:36: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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 3c438f1  Allow special case for parsing units of measurement in GCOM-W files.
3c438f1 is described below

commit 3c438f13aa13a231ae9468092cb58dc1e1cae842
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Feb 4 12:35:01 2020 +0100

    Allow special case for parsing units of measurement in GCOM-W files.
---
 .../apache/sis/internal/earth/netcdf/GCOM_W.java   | 24 +++++++++++++++++++++-
 .../org/apache/sis/internal/netcdf/Convention.java | 16 ++++++++++++++-
 .../org/apache/sis/internal/netcdf/Variable.java   | 13 +++++++++++-
 3 files changed, 50 insertions(+), 3 deletions(-)

diff --git a/profiles/sis-japan-profile/src/main/java/org/apache/sis/internal/earth/netcdf/GCOM_W.java
b/profiles/sis-japan-profile/src/main/java/org/apache/sis/internal/earth/netcdf/GCOM_W.java
index 233dc84..3672708 100644
--- a/profiles/sis-japan-profile/src/main/java/org/apache/sis/internal/earth/netcdf/GCOM_W.java
+++ b/profiles/sis-japan-profile/src/main/java/org/apache/sis/internal/earth/netcdf/GCOM_W.java
@@ -21,6 +21,9 @@ import java.util.Map;
 import java.util.HashMap;
 import java.util.Collections;
 import java.util.regex.Pattern;
+import javax.measure.Unit;
+import javax.measure.format.ParserException;
+import org.apache.sis.measure.Units;
 import org.apache.sis.storage.netcdf.AttributeNames;
 import org.apache.sis.internal.netcdf.Convention;
 import org.apache.sis.internal.netcdf.Decoder;
@@ -65,7 +68,7 @@ import org.apache.sis.referencing.operation.transform.TransferFunction;
  * </ul>
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  *
  * @see <a href="http://global.jaxa.jp/projects/sat/gcom_w/">SHIZUKU (GCOM-W) on JAXA</a>
  * @see <a href="https://en.wikipedia.org/wiki/Global_Change_Observation_Mission">GCOM
on Wikipedia</a>
@@ -231,4 +234,23 @@ public final class GCOM_W extends Convention {
         }
         return tr;
     }
+
+    /**
+     * Returns the unit of measurement to use as a fallback if it can not be determined in
a standard way.
+     *
+     * @param  data  the variable for which to get the unit of measurement.
+     * @return the unit of measurement, or {@code null} if none or unknown.
+     * @throws ParserException if the unit symbol can not be parsed.
+     */
+    @Override
+    public Unit<?> getUnitFallback(final Variable data) throws ParserException {
+        final String symbol = data.getAttributeAsString("UNIT");
+        if (symbol == null) {
+            return super.getUnitFallback(data);
+        }
+        if (symbol.equals("C")) {       // Missing "°" before "C".
+            return Units.CELSIUS;
+        }
+        return Units.valueOf(symbol);
+    }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
index 0cce4f1..3858191 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Convention.java
@@ -24,6 +24,8 @@ import java.util.Collections;
 import java.util.LinkedHashMap;
 import java.util.Locale;
 import java.awt.image.DataBuffer;
+import javax.measure.Unit;
+import javax.measure.format.ParserException;
 import org.opengis.referencing.crs.ProjectedCRS;
 import org.opengis.referencing.crs.GeographicCRS;
 import org.opengis.referencing.operation.MathTransform;
@@ -537,7 +539,6 @@ public class Convention {
     // │                                       COVERAGE RANGE                         
             │
     // └────────────────────────────────────────────────────────────────────────────────────────────┘
 
-
     /**
      * Returns the range of valid values, or {@code null} if unknown.
      * The default implementation takes the range of values from the following properties,
in precedence order:
@@ -715,4 +716,17 @@ public class Convention {
         if (!Double.isNaN(offset)) tr.setOffset(offset);
         return tr;
     }
+
+    /**
+     * Returns the unit of measurement to use as a fallback if it can not be determined in
a standard way.
+     * Default implementation returns {@code null}. Subclasses can override if the unit can
be determined
+     * in a way specific to this convention.
+     *
+     * @param  data  the variable for which to get the unit of measurement.
+     * @return the unit of measurement, or {@code null} if none or unknown.
+     * @throws ParserException if the unit symbol can not be parsed.
+     */
+    public Unit<?> getUnitFallback(final Variable data) throws ParserException {
+        return null;
+    }
 }
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
index 308f47d..6ebaad6 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
@@ -27,6 +27,7 @@ import java.util.regex.Pattern;
 import java.io.IOException;
 import java.time.Instant;
 import javax.measure.Unit;
+import javax.measure.format.ParserException;
 import org.opengis.referencing.operation.Matrix;
 import org.apache.sis.referencing.operation.transform.TransferFunction;
 import org.apache.sis.storage.DataStoreException;
@@ -253,10 +254,20 @@ public abstract class Variable extends Node {
         if (!unitParsed) {
             unitParsed = true;                          // Set first for avoiding to report
errors many times.
             final String symbols = getUnitsString();
+            Exception error = null;
             if (symbols != null) try {
                 unit = parseUnit(symbols);
             } catch (Exception ex) {
-                error(Variable.class, "getUnit", ex, Errors.Keys.CanNotAssignUnitToVariable_2,
getName(), symbols);
+                error = ex;
+            }
+            if (unit == null) try {
+                unit = decoder.convention().getUnitFallback(this);
+            } catch (ParserException ex) {
+                if (error == null) error = ex;
+                else error.addSuppressed(ex);
+            }
+            if (error != null) {
+                error(Variable.class, "getUnit", error, Errors.Keys.CanNotAssignUnitToVariable_2,
getName(), symbols);
             }
         }
         return unit;


Mime
View raw message