You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2018/12/20 15:26:58 UTC
[sis] 03/03: Share the netCDF coordinate axes accross netCDF files.
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 0574eb2e186b15e2bf6dbc6f6c017202c5367c7a
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Dec 20 16:26:32 2018 +0100
Share the netCDF coordinate axes accross netCDF files.
---
.../main/java/org/apache/sis/internal/netcdf/Grid.java | 2 +-
.../java/org/apache/sis/internal/netcdf/Variable.java | 10 ++++++++++
.../apache/sis/internal/netcdf/impl/VariableInfo.java | 1 +
.../sis/internal/netcdf/ucar/VariableWrapper.java | 18 +++++++++++++++---
4 files changed, 27 insertions(+), 4 deletions(-)
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 606ec0b..1b74793 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
@@ -89,7 +89,7 @@ public abstract class Grid extends NamedElement {
private boolean isCRSDetermined;
/**
- * The geometry of this grid (is extent, its CRS and its conversion to the CRS).
+ * The geometry of this grid (its extent, its CRS and its conversion to the CRS).
* May be {@code null} even after we attempted to create it.
*/
private GridGeometry geometry;
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 a440652..2d4b8ff 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
@@ -32,6 +32,7 @@ import org.apache.sis.math.Vector;
import org.apache.sis.math.DecimalFunctions;
import org.apache.sis.measure.NumberRange;
import org.apache.sis.util.Numbers;
+import org.apache.sis.util.collection.WeakHashSet;
import org.apache.sis.util.logging.WarningListeners;
import org.apache.sis.util.resources.Errors;
import ucar.nc2.constants.CDM; // We use only String constants.
@@ -49,6 +50,15 @@ import ucar.nc2.constants.CF;
*/
public abstract class Variable extends NamedElement {
/**
+ * Pool of vectors created by the {@link #read()} method. This pool is used for sharing netCDF coordinate axes,
+ * since the same vectors tend to be repeated in many netCDF files produced by the same data producer. Because
+ * those vectors can be large, sharing common instances may save a lot of memory.
+ *
+ * <p>All shared vectors shall be considered read-only.</p>
+ */
+ protected static final WeakHashSet<Vector> SHARED_VECTORS = new WeakHashSet<>(Vector.class);
+
+ /**
* Names of attributes where to fetch minimum and maximum sample values, in preference order.
*
* @see #getValidValues()
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 99c9e82..03203a6 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
@@ -692,6 +692,7 @@ final class VariableInfo extends Variable implements Comparable<VariableInfo> {
}
}
values = data.compress(tolerance);
+ values = SHARED_VECTORS.unique(values);
}
/**
diff --git a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
index b4d24cf..bed9c0c 100644
--- a/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
+++ b/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/VariableWrapper.java
@@ -73,6 +73,13 @@ final class VariableWrapper extends Variable {
private final VariableIF raw;
/**
+ * The values of the whole variable, or {@code null} if not yet read. This vector should be assigned only
+ * for relatively small variables, or for variables that are critical to the use of other variables
+ * (for example the values in coordinate system axes).
+ */
+ private transient Vector values;
+
+ /**
* Creates a new variable wrapping the given netCDF interface.
*/
VariableWrapper(final WarningListeners<?> listeners, VariableIF v) {
@@ -359,12 +366,17 @@ final class VariableWrapper extends Variable {
/**
* Reads all the data for this variable and returns them as an array of a Java primitive type.
* Multi-dimensional variables are flattened as a one-dimensional array (wrapped in a vector).
- * This method may cache the returned vector, at UCAR library choice.
+ * This method caches the returned vector.
*/
@Override
+ @SuppressWarnings("ReturnOfCollectionOrArrayField")
public Vector read() throws IOException {
- final Array array = variable.read(); // May be cached by the UCAR library.
- return createDecimalVector(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
+ if (values == null) {
+ final Array array = variable.read(); // May be already cached by the UCAR library.
+ values = createDecimalVector(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
+ values = SHARED_VECTORS.unique(values);
+ }
+ return values;
}
/**