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 2013/05/15 13:26:07 UTC
svn commit: r1482769 - in
/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf:
Axis.java GridGeometry.java ucar/GridGeometryWrapper.java
Author: desruisseaux
Date: Wed May 15 11:26:06 2013
New Revision: 1482769
URL: http://svn.apache.org/r1482769
Log:
More generic creation of the list of Axes.
Modified:
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridGeometry.java
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridGeometryWrapper.java
Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java?rev=1482769&r1=1482768&r2=1482769&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java [UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Axis.java [UTF-8] Wed May 15 11:26:06 2013
@@ -16,7 +16,6 @@
*/
package org.apache.sis.internal.netcdf;
-import org.apache.sis.util.ArraysExt;
import org.apache.sis.storage.netcdf.AttributeNames;
@@ -59,72 +58,35 @@ public final class Axis {
public final int[] sourceSizes;
/**
- * Constructs a new axis associated to no grid dimension.
- * Actually this should never happen, but we try to be safe.
- *
- * @param attributeNames The attributes to use for fetching dimension information, or {@code null} if unknown.
- */
- public Axis(final AttributeNames.Dimension attributeNames) {
- this.attributeNames = attributeNames;
- this.sourceDimensions = ArraysExt.EMPTY_INT;
- this.sourceSizes = ArraysExt.EMPTY_INT;
- }
-
- /**
- * Constructs a new axis associated to a single grid dimension.
- *
- * @param attributeNames The attributes to use for fetching dimension information, or {@code null} if unknown.
- * @param sourceDim The index of the grid dimension associated to this axis.
- * @param sourceSize The number of cell elements along that axis.
- */
- public Axis(final AttributeNames.Dimension attributeNames, final int sourceDim, final int sourceSize) {
- this.attributeNames = attributeNames;
- this.sourceDimensions = new int[] {sourceDim};
- this.sourceSizes = new int[] {sourceSize};
- }
-
- /**
- * Constructs a new axis associated to exactly two grid dimensions.
- * This constructor will detects by itself which grid dimension varies fastest.
- *
- * @param attributeNames The attributes to use for fetching dimension information, or {@code null} if unknown.
- * @param sourceDim1 The index of a first grid dimension associated to this axis.
- * @param sourceSize1 The number of cell elements along the first grid dimension.
- * @param sourceDim2 The index of a second grid dimension associated to this axis.
- * @param sourceSize2 The number of cell elements along the second grid dimension.
- * @param toTarget The conversion from grid coordinates to geodetic coordinates.
- */
- public Axis(final AttributeNames.Dimension attributeNames,
- final int sourceDim1, final int sourceSize1,
- final int sourceDim2, final int sourceSize2,
- final GridGeometry toTarget)
- {
- this.attributeNames = attributeNames;
- final int mid1 = sourceSize1 / 2;
- final int mid2 = sourceSize2 / 2;
- final double d1 = (toTarget.coordinateForCurrentAxis(0, mid2) - toTarget.coordinateForCurrentAxis(sourceSize1-1, mid2)) / sourceSize1;
- final double d2 = (toTarget.coordinateForCurrentAxis(mid1, 0) - toTarget.coordinateForCurrentAxis(mid1, sourceSize2-1)) / sourceSize2;
- if (Math.abs(d2) > Math.abs(d1)) {
- sourceDimensions = new int[] {sourceDim2, sourceDim1};
- sourceSizes = new int[] {sourceSize2, sourceSize1};
- } else {
- sourceDimensions = new int[] {sourceDim1, sourceDim2};
- sourceSizes = new int[] {sourceSize1, sourceSize2};
- }
- }
-
- /**
* Constructs a new axis associated to an arbitrary number of grid dimension.
- * Current implementation does not try to identify the "main" dimension
- * (we may try to improve that in a future SIS version).
+ * In the particular case where the number of dimensions is equals to 2, this
+ * his constructor will detects by itself which grid dimension varies fastest.
*
+ * @param owner Provides callback for the conversion from grid coordinates to geodetic coordinates.
* @param attributeNames The attributes to use for fetching dimension information, or {@code null} if unknown.
* @param sourceDimensions The index of the grid dimension associated to this axis.
* @param sourceSizes The number of cell elements along that axis.
*/
- public Axis(final AttributeNames.Dimension attributeNames, final int[] sourceDimensions, final int[] sourceSizes) {
+ public Axis(final GridGeometry owner, final AttributeNames.Dimension attributeNames,
+ final int[] sourceDimensions, final int[] sourceSizes)
+ {
this.attributeNames = attributeNames;
this.sourceDimensions = sourceDimensions;
this.sourceSizes = sourceSizes;
+ if (sourceDimensions.length == 2) {
+ final int up0 = sourceSizes[0];
+ final int up1 = sourceSizes[1];
+ final int mid0 = up0 / 2;
+ final int mid1 = up1 / 2;
+ final double d1 = (owner.coordinateForCurrentAxis(0, mid1) - owner.coordinateForCurrentAxis(up0-1, mid1)) / up0;
+ final double d2 = (owner.coordinateForCurrentAxis(mid0, 0) - owner.coordinateForCurrentAxis(mid0, up1-1)) / up1;
+ if (Math.abs(d2) > Math.abs(d1)) {
+ sourceSizes[0] = up1;
+ sourceSizes[1] = up0;
+ final int swap = sourceDimensions[0];
+ sourceDimensions[0] = sourceDimensions[1];
+ sourceDimensions[1] = swap;
+ }
+ }
}
}
Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridGeometry.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridGeometry.java?rev=1482769&r1=1482768&r2=1482769&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridGeometry.java [UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/GridGeometry.java [UTF-8] Wed May 15 11:26:06 2013
@@ -69,7 +69,7 @@ public abstract class GridGeometry exten
*
* @param j The fastest varying (right-most) index.
* @param i The slowest varying (left-most) index.
- * @return The coordinate at the given index.
+ * @return The coordinate at the given index, or {@link Double#NaN} if it can not be computed.
*/
protected abstract double coordinateForCurrentAxis(final int j, final int i);
}
Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridGeometryWrapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridGeometryWrapper.java?rev=1482769&r1=1482768&r2=1482769&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridGeometryWrapper.java [UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/GridGeometryWrapper.java [UTF-8] Wed May 15 11:26:06 2013
@@ -18,7 +18,6 @@ package org.apache.sis.internal.netcdf.u
import java.util.List;
import java.util.ArrayList;
-import java.util.Collections;
import ucar.nc2.Dimension;
import ucar.nc2.constants.AxisType;
import ucar.nc2.dataset.CoordinateAxis;
@@ -28,6 +27,7 @@ import org.apache.sis.internal.netcdf.Ax
import org.apache.sis.internal.netcdf.GridGeometry;
import org.apache.sis.internal.netcdf.WarningProducer;
import org.apache.sis.storage.netcdf.AttributeNames;
+import org.apache.sis.util.ArraysExt;
/**
@@ -89,12 +89,17 @@ final class GridGeometryWrapper extends
* of {@link CoordinateAxis2D}.</p>
*/
@Override
- @SuppressWarnings("fallthrough")
public List<Axis> getAxes() {
final List<Dimension> sourceDimensions = netcdfCS.getDomain();
final List<CoordinateAxis> netcdfAxes = netcdfCS.getCoordinateAxes();
- final List<Axis> axes = new ArrayList<>(netcdfAxes.size());
- for (final CoordinateAxis netcdfAxis : netcdfAxes) {
+ int targetDim = netcdfAxes.size();
+ final List<Axis> axes = new ArrayList<>(targetDim);
+ /*
+ * NetCDF files declare axes in reverse order, so we iterate in the 'netcdfAxes'
+ * list in reverse order for adding to the 'axes' list in "natural" order.
+ */
+ while (--targetDim >= 0) {
+ final CoordinateAxis netcdfAxis = netcdfAxes.get(targetDim);
final List<Dimension> dimensions = netcdfAxis.getDimensions();
AttributeNames.Dimension attributeNames = null;
final AxisType type = netcdfAxis.getAxisType();
@@ -106,54 +111,30 @@ final class GridGeometryWrapper extends
case RunTime: // Fallthrough: consider as Time
case Time: attributeNames = AttributeNames.TIME; break;
}
- final Axis axis;
- switch (dimensions.size()) {
- case 0: {
- // Should never happen, but defined by paranoia.
- axis = new Axis(attributeNames);
- break;
- }
- case 1: {
- // The most common case where one source axis == one target axis.
- final Dimension dim = dimensions.get(0);
- axis = new Axis(attributeNames,
- sourceDimensions.indexOf(dim), dim.getLength());
- break;
- }
- case 2: {
- // An other case managed by the UCAR API.
- if (netcdfAxis instanceof CoordinateAxis2D) {
- axis2D = (CoordinateAxis2D) netcdfAxis;
- final CoordinateAxis2D a2 = (CoordinateAxis2D) netcdfAxis;
- final Dimension dim0 = dimensions.get(0);
- final Dimension dim1 = dimensions.get(1);
- axis = new Axis(attributeNames,
- sourceDimensions.indexOf(dim0), dim0.getLength(),
- sourceDimensions.indexOf(dim1), dim1.getLength(),
- this);
- break;
- }
- // Fallthrough: use the generic case as a fallback.
- }
- default: {
- // Uncommon case.
- final int[] indices = new int[dimensions.size()];
- final int[] sizes = new int[indices.length];
- for (int i=indices.length; --i>=0;) {
- final Dimension dimension = dimensions.get(i);
- indices[i] = sourceDimensions.indexOf(dimension);
- sizes[i] = dimension.getLength();
- }
- axis = new Axis(attributeNames, indices, sizes);
- break;
+ /*
+ * Get the grid dimensions (part of the "domain" in UCAR terminology) used for computing
+ * the ordinate values along the current axis. There is exactly 1 such grid dimension in
+ * straightforward NetCDF files. However some more complex files may have 2 dimensions.
+ */
+ int i = 0;
+ final int[] indices = new int[dimensions.size()];
+ final int[] sizes = new int[indices.length];
+ for (final Dimension dimension : dimensions) {
+ final int sourceDim = sourceDimensions.indexOf(dimension);
+ if (sourceDim >= 0) {
+ indices[i] = sourceDim;
+ sizes[i++] = dimension.getLength();
}
+ /*
+ * If the axis dimension has not been found in the coordinate system (sourceDim < 0),
+ * then there is maybe a problem with the NetCDF file. However for the purpose of this
+ * package, we can proceed as if the dimension does not exist.
+ */
}
+ axis2D = (netcdfAxis instanceof CoordinateAxis2D) ? (CoordinateAxis2D) netcdfAxis : null;
+ axes.add(new Axis(this, attributeNames, ArraysExt.resize(indices, i), ArraysExt.resize(sizes, i)));
}
- /*
- * NetCDF files define the axes in reverse order.
- * Retores them in "natural" order.
- */
- Collections.reverse(axes);
+ axis2D = null;
return axes;
}
@@ -163,6 +144,6 @@ final class GridGeometryWrapper extends
*/
@Override
protected double coordinateForCurrentAxis(final int j, final int i) {
- return axis2D.getCoordValue(j, i);
+ return (axis2D != null) ? axis2D.getCoordValue(j, i) : Double.NaN;
}
}