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: NetCDF native metadata should include attributes of variables (in addition of global attributes).
Date Tue, 04 Feb 2020 10:59:24 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 1fd64e4  NetCDF native metadata should include attributes of variables (in addition
of global attributes).
1fd64e4 is described below

commit 1fd64e4fd29cbe2ece0dba2a3e34891f25c49c2d
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Tue Feb 4 11:57:31 2020 +0100

    NetCDF native metadata should include attributes of variables (in addition of global attributes).
---
 .../org/apache/sis/internal/netcdf/Decoder.java    |  6 +-
 .../sis/internal/netcdf/impl/ChannelDecoder.java   | 16 ++---
 .../sis/internal/netcdf/impl/VariableInfo.java     | 36 ++++++++++-
 .../sis/internal/netcdf/ucar/DecoderWrapper.java   | 75 ++++++++++++++--------
 .../org/apache/sis/storage/netcdf/NetcdfStore.java |  2 +-
 5 files changed, 95 insertions(+), 40 deletions(-)

diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
index d3dc0e0..e1c8c9b 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
@@ -180,11 +180,13 @@ public abstract class Decoder extends ReferencingFactoryContainer implements
Clo
     }
 
     /**
-     * Adds netCDF attributes to the given node.
+     * Adds netCDF attributes to the given node, including variables and sub-groups attributes.
+     * Groups are shown first, then variables attributes, and finally global attributes.
+     * Showing global attributes last is consistent with ncML ("netCDF dump") output.
      *
      * @param  root  the node where to add netCDF attributes.
      */
-    public abstract void addNativeMetadata(TreeTable.Node root);
+    public abstract void addAttributesTo(TreeTable.Node root);
 
     /**
      * Returns a filename for formatting error message and for information purpose.
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
index bac80a7..c278a8d 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/ChannelDecoder.java
@@ -1038,23 +1038,19 @@ nextVar:    for (final VariableInfo variable : variables) {
     }
 
     /**
-     * Adds netCDF attributes to the given node.
+     * Adds netCDF attributes to the given node, including variables attributes.
+     * Variables attributes are shown fist, and global attributes are last.
      *
      * @param  root  the node where to add netCDF attributes.
      */
     @Override
-    public void addNativeMetadata(final TreeTable.Node root) {
-        for (final Map.Entry<String,Object> entry : attributeMap.entrySet()) {
+    public void addAttributesTo(final TreeTable.Node root) {
+        for (final Map.Entry<String,VariableInfo> entry : variableMap.entrySet()) {
             final TreeTable.Node node = root.newChild();
             node.setValue(TableColumn.NAME, entry.getKey());
-            Object value = entry.getValue();
-            if (value != null) {
-                if (value instanceof Vector) {
-                    value = ((Vector) value).toArray();
-                }
-                node.setValue(TableColumn.VALUE, value);
-            }
+            entry.getValue().addAttributesTo(node);
         }
+        VariableInfo.addAttributesTo(root, attributeMap);
     }
 
     /**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
index 0c0b3bd..9f6a095 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/impl/VariableInfo.java
@@ -44,6 +44,8 @@ import org.apache.sis.internal.util.UnmodifiableArrayList;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.DataStoreContentException;
 import org.apache.sis.storage.netcdf.AttributeNames;
+import org.apache.sis.util.collection.TableColumn;
+import org.apache.sis.util.collection.TreeTable;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.Classes;
@@ -59,7 +61,7 @@ import org.apache.sis.math.Vector;
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
- * @version 1.0
+ * @version 1.1
  * @since   0.3
  * @module
  */
@@ -547,6 +549,38 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo>
{
     }
 
     /**
+     * Adds the attributes of this variable to the given node. This is used for building
the tree returned by
+     * {@link org.apache.sis.storage.netcdf.NetcdfStore#getNativeMetadata()}. For information
purpose only.
+     *
+     * @param  branch  where to add new nodes for the attributes of this variable.
+     */
+    final void addAttributesTo(final TreeTable.Node branch) {
+        addAttributesTo(branch, attributes);
+    }
+
+    /**
+     * Adds the given attributes to the given node. This is used for building the tree
+     * returned by {@link org.apache.sis.storage.netcdf.NetcdfStore#getNativeMetadata()}.
+     * This tree is for information purpose only.
+     *
+     * @param  branch      where to add new nodes for the given attributes.
+     * @param  attributes  the attributes to add to the specified branch.
+     */
+    static void addAttributesTo(final TreeTable.Node branch, final Map<String,Object>
attributes) {
+        for (final Map.Entry<String,Object> entry : attributes.entrySet()) {
+            final TreeTable.Node node = branch.newChild();
+            node.setValue(TableColumn.NAME, entry.getKey());
+            Object value = entry.getValue();
+            if (value != null) {
+                if (value instanceof Vector) {
+                    value = ((Vector) value).toArray();
+                }
+                node.setValue(TableColumn.VALUE, value);
+            }
+        }
+    }
+
+    /**
      * Sets the values in this variable. The values are normally read from the netCDF file
by the {@link #read()} method,
      * but this {@code setValues(Object)} method may also be invoked if we want to overwrite
those values.
      *
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
index d2766ec..d3cd549 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
@@ -502,46 +502,69 @@ public final class DecoderWrapper extends Decoder implements CancelTask
{
     }
 
     /**
-     * Adds netCDF attributes to the given node.
+     * Adds netCDF attributes to the given node, including variables and sub-groups attributes.
+     * Groups are shown first, then variables attributes, and finally global attributes.
      *
      * @param  root  the node where to add netCDF attributes.
      */
     @Override
-    public void addNativeMetadata(final TreeTable.Node root) {
-        addNativeMetadata(root, file.getRootGroup());
+    public void addAttributesTo(final TreeTable.Node root) {
+        addAttributesTo(root, file.getRootGroup());
     }
 
     /**
      * Adds all attributes of the given group, then create nodes for sub-groups (if any).
      * This method invokes itself recursively.
+     *
+     * @param  branch  where to add new nodes for the children of given group.
+     * @param  group   group for which to add sub-group, variables and attributes.
      */
-    private void addNativeMetadata(final TreeTable.Node branch, final Group group) {
-        for (final Attribute attribute : group.getAttributes()) {
+    private void addAttributesTo(final TreeTable.Node branch, final Group group) {
+        for (final Group sub : group.getGroups()) {
             final TreeTable.Node node = branch.newChild();
-            node.setValue(TableColumn.NAME, attribute.getShortName());
-            final int length = attribute.getLength();
-            final Object value;
-            switch (length) {
-                case 0: continue;
-                case 1: {
-                    value = attribute.getValue(0);
-                    break;
-                }
-                default: {
-                    final Object[] values = new Object[length];
-                    for (int i=0; i<length; i++) {
-                        values[i] = attribute.getValue(i);
+            node.setValue(TableColumn.NAME, sub.getShortName());
+            addAttributesTo(node, sub);
+        }
+        for (final ucar.nc2.Variable variable : group.getVariables()) {
+            final TreeTable.Node node = branch.newChild();
+            node.setValue(TableColumn.NAME, variable.getShortName());
+            addAttributesTo(node, variable.getAttributes());
+        }
+        addAttributesTo(branch, group.getAttributes());
+    }
+
+    /**
+     * Adds the given attributes to the given node. This is used for building the tree
+     * returned by {@link org.apache.sis.storage.netcdf.NetcdfStore#getNativeMetadata()}.
+     * This tree is for information purpose only.
+     *
+     * @param  branch      where to add new nodes for the given attributes.
+     * @param  attributes  the attributes to add to the specified branch.
+     */
+    private static void addAttributesTo(final TreeTable.Node branch, final List<Attribute>
attributes) {
+        if (attributes != null) {
+            for (final Attribute attribute : attributes) {
+                final TreeTable.Node node = branch.newChild();
+                node.setValue(TableColumn.NAME, attribute.getShortName());
+                final int length = attribute.getLength();
+                final Object value;
+                switch (length) {
+                    case 0: continue;
+                    case 1: {
+                        value = attribute.getValue(0);
+                        break;
+                    }
+                    default: {
+                        final Object[] values = new Object[length];
+                        for (int i=0; i<length; i++) {
+                            values[i] = attribute.getValue(i);
+                        }
+                        value = values;
+                        break;
                     }
-                    value = values;
-                    break;
                 }
+                node.setValue(TableColumn.VALUE, value);
             }
-            node.setValue(TableColumn.VALUE, value);
-        }
-        for (final Group sub : group.getGroups()) {
-            final TreeTable.Node node = branch.newChild();
-            node.setValue(TableColumn.NAME, sub.getShortName());
-            addNativeMetadata(node, sub);
         }
     }
 
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
index 4844f61..f8d802a 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStore.java
@@ -209,7 +209,7 @@ public class NetcdfStore extends DataStore implements Aggregate {
         final DefaultTreeTable table = new DefaultTreeTable(TableColumn.NAME, TableColumn.VALUE);
         final TreeTable.Node root = table.getRoot();
         root.setValue(TableColumn.NAME, NetcdfStoreProvider.NAME);
-        decoder.addNativeMetadata(root);
+        decoder.addAttributesTo(root);
         return Optional.of(table);
     }
 


Mime
View raw message