sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1591882 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/measure/Units.java main/java/org/apache/sis/measure/UnitsMap.java test/java/org/apache/sis/measure/UnitsTest.java
Date Fri, 02 May 2014 12:07:23 GMT
Author: desruisseaux
Date: Fri May  2 12:07:22 2014
New Revision: 1591882

URL: http://svn.apache.org/r1591882
Log:
Added tests for units of measurement.

Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java?rev=1591882&r1=1591881&r2=1591882&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/Units.java [UTF-8]
Fri May  2 12:07:22 2014
@@ -77,6 +77,17 @@ public final class Units extends Static 
     public static final Unit<Dimensionless> PPM = Unit.ONE.times(1E-6);//.alternate("ppm");
 
     /**
+     * Salinity measured using PSS-78. While this is a dimensionless measurement, the {@code
"psu"} symbol
+     * is sometime added to PSS-78 measurement. However this is officially discouraged.
+     */
+    static final Unit<Dimensionless> PSU = Unit.ONE.alternate("psu");
+
+    /**
+     * Sigma-level, used in oceanography. This is a way to measure a depth as a fraction
of the sea floor depth.
+     */
+    static final Unit<Dimensionless> SIGMA = Unit.ONE.alternate("sigma");
+
+    /**
      * Returns {@code true} if the given unit is a linear unit.
      * Linear units are convertible to {@link NonSI#DEGREE_ANGLE}.
      *
@@ -395,12 +406,10 @@ public final class Units extends Static 
             if (equalsIgnorePlural(uom, "second")) return SI   .SECOND;
             if (equalsIgnorePlural(uom, "pixel"))  return NonSI.PIXEL;
             if (isCelsius(uom))                    return SI.CELSIUS;
-            if (uom.isEmpty() ||
-                uom.equalsIgnoreCase("psu") ||   // Pratical Salinity Scale (oceanography)
-                uom.equalsIgnoreCase("level"))   // Sigma level (oceanography)
-            {
-                return Unit.ONE;
-            }
+            if (uom.isEmpty())                     return Unit.ONE;
+            if (uom.equalsIgnoreCase("ppm"))       return PPM;
+            if (uom.equalsIgnoreCase("psu"))       return PSU;
+            if (uom.equalsIgnoreCase("sigma"))     return SIGMA;
         }
         final Unit<?> unit;
         try {
@@ -409,6 +418,13 @@ public final class Units extends Static 
             // Provides a better error message than the default JSR-275 0.9.4 implementation.
             throw Exceptions.setMessage(e, Errors.format(Errors.Keys.IllegalArgumentValue_2,
"uom", uom), true);
         }
+        /*
+         * Special case: JSR-275 version 0.6.1 parses "1/s" and "s-1" as "Baud", which is
not what
+         * we use in geoscience. Replace "Baud" by "Hertz" if the symbol was not explicitely
"Bd".
+         */
+        if (unit.isCompatible(SI.HERTZ) && !uom.equals("Bd")) {
+            return SI.HERTZ;
+        }
         return UnitsMap.canonicalize(unit);
     }
 
@@ -559,7 +575,7 @@ public final class Units extends Static 
      */
     public static Integer getEpsgCode(final Unit<?> unit, final boolean inAxis) {
         Integer code = UnitsMap.EPSG_CODES.get(unit);
-        if (inAxis && code != null && code.intValue() == 9102) {
+        if (inAxis && code != null && code == 9102) {
             code = UnitsMap.I9122;
         }
         return code;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java?rev=1591882&r1=1591881&r2=1591882&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/measure/UnitsMap.java
[UTF-8] Fri May  2 12:07:22 2014
@@ -71,7 +71,7 @@ final class UnitsMap extends Static {
      */
     private static final Map<Unit<?>,Unit<?>> COMMONS = new HashMap<>(48);
     static {
-        COMMONS.put(PPM, PPM);
+        COMMONS.put(MILLISECOND, MILLISECOND);
         boolean nonSI = false;
         do for (final Field field : (nonSI ? NonSI.class : SI.class).getFields()) {
             final int modifiers = field.getModifiers();

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java?rev=1591882&r1=1591881&r2=1591882&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/measure/UnitsTest.java
[UTF-8] Fri May  2 12:07:22 2014
@@ -23,8 +23,16 @@ import org.junit.Test;
 
 import static javax.measure.unit.Unit.ONE;
 import static javax.measure.unit.SI.CELSIUS;
+import static javax.measure.unit.SI.KELVIN;
 import static javax.measure.unit.SI.METRE;
+import static javax.measure.unit.SI.METRES_PER_SECOND;
+import static javax.measure.unit.SI.SQUARE_METRE;
 import static javax.measure.unit.SI.KILOMETRE;
+import static javax.measure.unit.SI.KILOGRAM;
+import static javax.measure.unit.SI.JOULE;
+import static javax.measure.unit.SI.PASCAL;
+import static javax.measure.unit.SI.SECOND;
+import static javax.measure.unit.SI.HERTZ;
 import static javax.measure.unit.SI.RADIAN;
 import static javax.measure.unit.NonSI.CENTIRADIAN;
 import static javax.measure.unit.NonSI.DEGREE_ANGLE;
@@ -35,6 +43,7 @@ import static javax.measure.unit.NonSI.D
 import static javax.measure.unit.NonSI.SPHERE;
 import static javax.measure.unit.NonSI.ATMOSPHERE;
 import static javax.measure.unit.NonSI.NAUTICAL_MILE;
+import static javax.measure.unit.NonSI.PERCENT;
 import static org.apache.sis.measure.SexagesimalConverter.*;
 import static org.apache.sis.measure.Units.*;
 import static org.apache.sis.test.Assert.*;
@@ -214,7 +223,7 @@ public final strictfp class UnitsTest ex
     }
 
     /**
-     * Tests {@link Units#valueOf(String)}.
+     * Tests {@link Units#valueOf(String)} with units most commonly found in geospatial data.
      */
     @Test
     public void testValueOf() {
@@ -226,12 +235,14 @@ public final strictfp class UnitsTest ex
         assertSame(DEGREE_ANGLE, valueOf("DEGREES"));
         assertSame(DEGREE_ANGLE, valueOf("DEGRÉES"));
         assertSame(DEGREE_ANGLE, valueOf("degrees_east"));
+        assertSame(DEGREE_ANGLE, valueOf("degrees_north"));
         assertSame(DEGREE_ANGLE, valueOf("degrées_north"));
         assertSame(DEGREE_ANGLE, valueOf("decimal_degree"));
         assertSame(SECOND_ANGLE, valueOf("arcsec"));
         assertSame(RADIAN,       valueOf("rad"));
         assertSame(RADIAN,       valueOf("radian"));
         assertSame(RADIAN,       valueOf("radians"));
+        assertSame(SECOND,       valueOf("s"));
         assertSame(METRE,        valueOf("m"));
         assertSame(METRE,        valueOf("metre"));
         assertSame(METRE,        valueOf("meter"));
@@ -244,9 +255,37 @@ public final strictfp class UnitsTest ex
         assertSame(KILOMETRE,    valueOf("kilometres"));
         assertSame(KILOMETRE,    valueOf("kilomètres"));
         assertSame(KILOMETRE,    valueOf("kilometers"));
+        assertSame(KELVIN,       valueOf("K"));
         assertSame(CELSIUS,      valueOf("Celsius"));
         assertSame(CELSIUS,      valueOf("degree Celsius"));
         assertSame(CELSIUS,      valueOf("degree_Celcius"));
+        assertSame(PASCAL,       valueOf("Pa"));
+    }
+
+    /**
+     * Tests {@link Units#valueOf(String)} with more advanced units.
+     * Those units are found in NetCDF files among others.
+     */
+    @Test
+    public void testAdvancedValueOf() {
+        assertSame  (Units.MILLISECOND,             valueOf("ms"));
+        assertEquals(METRES_PER_SECOND,             valueOf("m/s"));
+        assertEquals(METRES_PER_SECOND,             valueOf("m.s-1"));
+        assertEquals(SQUARE_METRE.divide(SECOND),   valueOf("m2.s-1"));
+        assertEquals(KILOGRAM.divide(SQUARE_METRE), valueOf("kg.m-2"));
+        assertEquals(JOULE.divide(KILOGRAM),        valueOf("J/kg"));
+        assertEquals(PASCAL.divide(SECOND),         valueOf("Pa/s"));
+        assertSame  (HERTZ,                         valueOf("1/s"));
+        assertSame  (HERTZ,                         valueOf("s-1"));
+        assertSame  (PERCENT,                       valueOf("%"));
+        assertSame  (Unit.ONE,                      valueOf("kg/kg"));
+        assertSame  (Unit.ONE,                      valueOf("kg.kg-1"));
+        assertSame  (Units.PPM,                     valueOf("ppm")); // Parts per million
+        assertSame  (Units.PSU,                     valueOf("psu")); // Pratical Salinity
Unit
+        assertSame  (Units.SIGMA,                   valueOf("sigma"));
+
+        // Potential vorticity surface
+        assertEquals(KELVIN.times(SQUARE_METRE).divide(KILOGRAM.times(SECOND)), valueOf("K.m2.kg-1.s-1"));
     }
 
     /**



Mime
View raw message