sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1484173 - in /sis/branches/JDK7/storage/sis-netcdf/src: main/java/org/apache/sis/internal/netcdf/ main/java/org/apache/sis/internal/netcdf/impl/ main/java/org/apache/sis/internal/netcdf/ucar/ main/java/org/apache/sis/storage/netcdf/ test/j...
Date Sat, 18 May 2013 20:13:59 GMT
Author: desruisseaux
Date: Sat May 18 20:13:58 2013
New Revision: 1484173

URL: http://svn.apache.org/r1484173
Log:
Starting test cases for NetCDF variables.

Added:
    sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
  (with props)
Modified:
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
    sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -169,11 +169,12 @@ public abstract class Decoder extends Wa
 
     /**
      * Returns all variables found in the NetCDF file.
+     * This method may return a direct reference to an internal array - do not modify.
      *
-     * @return All variables, or an empty list if none.
+     * @return All variables, or an empty array if none.
      * @throws IOException If an I/O operation was necessary but failed.
      */
-    public abstract List<Variable> getVariables() throws IOException;
+    public abstract Variable[] getVariables() throws IOException;
 
     /**
      * Returns all grid geometries (related to coordinate systems) found in the NetCDF file.

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Variable.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -16,7 +16,8 @@
  */
 package org.apache.sis.internal.netcdf;
 
-import java.util.List;
+import org.apache.sis.util.Classes;
+import org.apache.sis.util.Debug;
 
 
 /**
@@ -54,13 +55,27 @@ public abstract class Variable {
     public abstract String getDescription();
 
     /**
+     * Returns the variable data type, as a primitive type if possible.
+     *
+     * @return The variable data type, or {@code null} if unknown.
+     */
+    public abstract Class<?> getDataType();
+
+    /**
      * Returns the name of the variable data type as the name of the primitive type
      * followed by the span of each dimension (in unit of grid cells) between brackets.
      * Example: {@code "short[180][360]"}.
      *
      * @return The name of the variable data type.
      */
-    public abstract String getDataTypeName();
+    public final String getDataTypeName() {
+        final StringBuilder buffer = new StringBuilder(Classes.getShortName(getDataType()));
+        final int[] shape = getDimensionLengths();
+        for (int i=shape.length; --i>=0;) {
+            buffer.append('[').append(shape[i]).append(']');
+        }
+        return buffer.toString();
+    }
 
     /**
      * Returns {@code true} if the given variable can be used for generating an image.
@@ -89,7 +104,15 @@ public abstract class Variable {
      *
      * @return The dimension names.
      */
-    public abstract List<String> getDimensions();
+    public abstract String[] getDimensionNames();
+
+    /**
+     * Returns the length (number of cells) of each dimension.
+     * The length of this array shall be equals to the length of the {@link #getDimensionNames()}
array.
+     *
+     * @return The number of grid cells for each dimension.
+     */
+    public abstract int[] getDimensionLengths();
 
     /**
      * Returns the sequence of values for the given attribute, or an empty array if none.
@@ -101,4 +124,19 @@ public abstract class Variable {
      * @return The sequence of {@link String} or {@link Number} values for the named attribute.
      */
     public abstract Object[] getAttributeValues(String attributeName, boolean numeric);
+
+    /**
+     * Returns a string representation of this variable for debugging purpose.
+     */
+    @Debug
+    @Override
+    public String toString() {
+        final StringBuilder buffer = new StringBuilder(getName())
+                .append(" : ").append(Classes.getShortName(getDataType()));
+        final int[] shape = getDimensionLengths();
+        for (int i=shape.length; --i>=0;) {
+            buffer.append('[').append(shape[i]).append(']');
+        }
+        return buffer.toString();
+    }
 }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -649,7 +649,7 @@ public final class ChannelDecoder extend
     }
 
     @Override
-    public List<Variable> getVariables() throws IOException {
+    public Variable[] getVariables() throws IOException {
         throw new UnsupportedOperationException();
     }
 

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -68,6 +68,13 @@ public final class DecoderWrapper extend
     private Group[] groups;
 
     /**
+     * The variables, computed when first needed.
+     *
+     * @see #getVariables()
+     */
+    private transient Variable[] variables;
+
+    /**
      * Creates a new decoder for the given NetCDF file. While this constructor accepts arbitrary
      * {@link NetcdfFile} instance, the {@link NetcdfDataset} subclass is necessary in order
to
      * get coordinate system information.
@@ -258,16 +265,19 @@ public final class DecoderWrapper extend
 
     /**
      * Returns all variables found in the NetCDF file.
+     * This method returns a direct reference to its internal array - do not modify.
      */
     @Override
-    public List<Variable> getVariables() {
-        final List<? extends VariableIF> all = file.getVariables();
-        if (all == null) {
-            return Collections.emptyList();
-        }
-        final List<Variable> variables = new ArrayList<>(all.size());
-        for (final VariableIF variable : all) {
-            variables.add(new VariableWrapper(variable, all));
+    public Variable[] getVariables() {
+        if (variables == null) {
+            List<? extends VariableIF> all = file.getVariables();
+            if (all == null) {
+                all = Collections.emptyList();
+            }
+            variables = new Variable[all.size()];
+            for (int i=0; i<variables.length; i++) {
+                variables[i] = new VariableWrapper(all.get(i), all);
+            }
         }
         return variables;
     }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -18,7 +18,6 @@ package org.apache.sis.internal.netcdf.u
 
 import org.apache.sis.internal.netcdf.Variable;
 import java.util.List;
-import java.util.ArrayList;
 import java.awt.image.DataBuffer;
 import ucar.ma2.DataType;
 import ucar.nc2.Attribute;
@@ -72,17 +71,12 @@ final class VariableWrapper extends Vari
     }
 
     /**
-     * Returns the name of the variable data type as the name of the primitive type
-     * followed by the span of each dimension (in unit of grid cells) between brackets.
+     * Returns the variable data type, as a primitive type if possible.
+     * This method may return {@code null}.
      */
     @Override
-    public String getDataTypeName() {
-        final StringBuilder buffer = new StringBuilder(variable.getDataType().getPrimitiveClassType().getSimpleName());
-        final int[] shape = variable.getShape();
-        for (int i=shape.length; --i>=0;) {
-            buffer.append('[').append(shape[i]).append(']');
-        }
-        return buffer.toString();
+    public Class<?> getDataType() {
+        return variable.getDataType().getPrimitiveClassType();
     }
 
     /**
@@ -145,16 +139,24 @@ final class VariableWrapper extends Vari
      * @return The dimension names.
      */
     @Override
-    public List<String> getDimensions() {
+    public String[] getDimensionNames() {
         final List<Dimension> dimensions = variable.getDimensions();
-        final List<String> names = new ArrayList<>(dimensions.size());
-        for (final Dimension dimension : dimensions) {
-            names.add(dimension.getShortName());
+        final String[] names = new String[dimensions.size()];
+        for (int i=0; i<names.length; i++) {
+            names[i] = dimensions.get(i).getShortName();
         }
         return names;
     }
 
     /**
+     * Returns the length (number of cells) of each dimension.
+     */
+    @Override
+    public int[] getDimensionLengths() {
+        return variable.getShape();
+    }
+
+    /**
      * Returns the sequence of values for the given attribute, or an empty array if none.
      * The elements will be of class {@link String} if {@code numeric} is {@code false},
      * or {@link Number} if {@code numeric} is {@code true}.

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/MetadataReader.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -24,6 +24,7 @@ import java.util.Set;
 import java.util.LinkedHashSet;
 import java.util.Map;
 import java.util.HashMap;
+import java.util.Arrays;
 import java.util.Collection;
 import java.io.IOException;
 import javax.measure.unit.Unit;
@@ -727,8 +728,7 @@ final class MetadataReader extends Warni
     private Collection<DefaultCoverageDescription> createContentInfo() throws IOException
{
         final Map<List<String>, DefaultCoverageDescription> contents = new HashMap<>(4);
         final String processingLevel = decoder.stringValue(PROCESSING_LEVEL);
-        final List<? extends Variable> variables = decoder.getVariables();
-        for (final Variable variable : variables) {
+        for (final Variable variable : decoder.getVariables()) {
             if (!variable.isCoverage(2)) {
                 continue;
             }
@@ -737,7 +737,7 @@ final class MetadataReader extends Warni
              * (e.g. longitude,latitude,time). This separation is based on the fact that
a
              * coverage has only one domain for every range of values.
              */
-            final List<String> dimensions = variable.getDimensions();
+            final List<String> dimensions = Arrays.asList(variable.getDimensionNames());
             DefaultCoverageDescription content = contents.get(dimensions);
             if (content == null) {
                 /*
@@ -786,8 +786,6 @@ final class MetadataReader extends Warni
             if (nameFactory == null) {
                 nameFactory = DefaultFactories.forClass(NameFactory.class);
             }
-            // TODO: should be band.setName(...) with ISO 19115:2011.
-            // Sequence identifiers are supposed to be numbers only.
             band.setSequenceIdentifier(nameFactory.createMemberName(null, name,
                     nameFactory.createTypeName(null, variable.getDataTypeName())));
         }

Added: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java?rev=1484173&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
(added)
+++ sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
Sat May 18 20:13:58 2013
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.internal.netcdf;
+
+import java.io.IOException;
+import static org.opengis.wrapper.netcdf.IOTestCase.NCEP;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link Variable} implementation. The default implementation tests
+ * {@link org.apache.sis.internal.netcdf.ucar.VariableWrapper} since the UCAR
+ * library is our reference implementation. However subclasses can override the
+ * {@link #createDecoder(String)} method in order to test a different implementation.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public strictfp class VariableTest extends TestCase {
+    /**
+     * Expected number of columns per variables for the {@code expected} argument
+     * given to the {@link #assertBasicPropertiesEqual(Object[], Variable[])} method.
+     */
+    private static final int NUM_BASIC_PROPERTY_COLUMNS = 5;
+
+    /**
+     * Tests the basic properties of all variables found in the {@link #NCEP} file.
+     * The tested methods are:
+     *
+     * <ul>
+     *   <li>{@link Variable#getName()}</li>
+     *   <li>{@link Variable#getDescription()}</li>
+     *   <li>{@link Variable#getDataType()}</li>
+     *   <li>{@link Variable#getDimensionLengths()}</li>
+     *   <li>{@link Variable#isCoverage(int)}</li>
+     * </ul>
+     *
+     * @throws IOException If an error occurred while reading the NetCDF file.
+     */
+    @Test
+    public void testGetName() throws IOException {
+        assertBasicPropertiesEqual(new Object[] {
+        // __name______________description_________________________________data type____dim_raster?
+            "reftime",        "reference time",                            double.class,
1, false,
+            "datetime",       "reference date and time",                   char  .class,
2, false,
+            "forecasttime",   "forecast date and time",                    char  .class,
2, false,
+            "model_id",       "generating process ID number",              int   .class,
1, false,
+            "nav_model",      "navigation model name",                     char  .class,
2, false,
+            "grid_type_code", "GRIB-1 GDS data representation type",       int   .class,
1, false,
+            "grid_type",      "GRIB-1 grid type",                          char  .class,
2, false,
+            "grid_name",      "grid name",                                 char  .class,
2, false,
+            "grid_center",    "GRIB-1 originating center ID",              int   .class,
1, false,
+            "grid_number",    "GRIB-1 catalogued grid numbers",            int   .class,
2, false,
+            "i_dim",          "longitude dimension name",                  char  .class,
2, false,
+            "j_dim",          "latitude dimension name",                   char  .class,
2, false,
+            "Ni",             "number of points along a latitude circle",  int   .class,
1, false,
+            "Nj",             "number of points along a longitude circle", int   .class,
1, false,
+            "La1",            "latitude of first grid point",              float .class,
1, false,
+            "Lo1",            "longitude of first grid point",             float .class,
1, false,
+            "La2",            "latitude of last grid point",               float .class,
1, false,
+            "Lo2",            "longitude of last grid point",              float .class,
1, false,
+            "Di",             "longitudinal direction increment",          float .class,
1, false,
+            "Dj",             "latitudinal direction increment",           float .class,
1, false,
+            "ResCompFlag",    "resolution and component flags",            byte  .class,
1, false,
+            "SST",            "Sea temperature",                           float .class,
3, true,
+            "valtime",        "valid time",                                double.class,
1, false,
+            "valtime_offset", "hours from reference time",                 double.class,
1, false,
+            "lat",            "latitude",                                  float .class,
1, false,
+            "lon",            "longitude",                                 float .class,
1, false
+        }, selectDataset(NCEP).getVariables());
+    }
+
+    /**
+     * Compares the basic properties of the given variables.
+     *
+     * @param expected  The expected property values.
+     * @param variables The variable for which to test properties.
+     */
+    private static void assertBasicPropertiesEqual(final Object[] expected, final Variable[]
variables) {
+        int propertyIndex = 0;
+        for (final Variable variable : variables) {
+            assertFalse("Too many variables.", propertyIndex == expected.length);
+            assertEquals("getName()",             expected[propertyIndex++], variable.getName());
+            assertEquals("getDescription()",      expected[propertyIndex++], variable.getDescription());
+            assertEquals("getDataType()",         expected[propertyIndex++], variable.getDataType());
+            assertEquals("getDimensionLengths()", expected[propertyIndex++], variable.getDimensionLengths().length);
+            assertEquals("isCoverage(2)",         expected[propertyIndex++], variable.isCoverage(2));
+            assertEquals(0, propertyIndex % NUM_BASIC_PROPERTY_COLUMNS); // Sanity check
for VariableTest itself.
+        }
+        assertEquals("Expected more variables.",
+                expected.length / NUM_BASIC_PROPERTY_COLUMNS,
+                propertyIndex   / NUM_BASIC_PROPERTY_COLUMNS);
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/VariableTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java?rev=1484173&r1=1484172&r2=1484173&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/test/suite/NetcdfTestSuite.java
[UTF-8] Sat May 18 20:13:58 2013
@@ -32,6 +32,7 @@ import org.junit.BeforeClass;
  */
 @Suite.SuiteClasses({
     org.apache.sis.internal.netcdf.DecoderTest.class,
+    org.apache.sis.internal.netcdf.VariableTest.class,
     org.apache.sis.internal.netcdf.impl.ChannelDecoderTest.class
 })
 public final strictfp class NetcdfTestSuite extends TestSuite {



Mime
View raw message