Search in sources :

Example 1 with CoordinateAxis

use of ucar.nc2.dataset.CoordinateAxis in project sis by apache.

the class GridGeometryWrapper method getAxes.

/**
 * Returns all axes of the netCDF coordinate system, together with the grid dimension to which the axis
 * is associated.
 *
 * <p>In this method, the words "domain" and "range" are used in the netCDF sense: they are the input
 * (domain) and output (range) of the function that convert grid indices to geodetic coordinates.</p>
 *
 * <p>The domain of all axes (or the {@linkplain CoordinateSystem#getDomain() coordinate system domain})
 * is often the same than the domain of the variable, but not necessarily.
 * In particular, the relationship is not straightforward when the coordinate system contains instances
 * of {@link CoordinateAxis2D}.</p>
 *
 * @return the CRS axes, in netCDF order (reverse of "natural" order).
 * @throws IOException if an I/O operation was necessary but failed.
 * @throws DataStoreException if a logical error occurred.
 */
@Override
public Axis[] getAxes() throws IOException, DataStoreException {
    final List<Dimension> domain = netcdfCS.getDomain();
    final List<CoordinateAxis> range = netcdfCS.getCoordinateAxes();
    /*
         * In this method, 'sourceDim' and 'targetDim' are relative to "grid to CRS" conversion.
         * So 'sourceDim' is the grid (domain) dimension and 'targetDim' is the CRS (range) dimension.
         */
    int targetDim = range.size();
    final Axis[] axes = new Axis[targetDim];
    while (--targetDim >= 0) {
        final CoordinateAxis axis = range.get(targetDim);
        /*
             * The AttributeNames are for ISO 19115 metadata. They are not used for locating grid cells
             * on Earth, but we nevertheless get them now for making MetadataReader work easier.
             */
        AttributeNames.Dimension attributeNames = null;
        final AxisType type = axis.getAxisType();
        if (type != null)
            switch(type) {
                case Lon:
                    attributeNames = AttributeNames.LONGITUDE;
                    break;
                case Lat:
                    attributeNames = AttributeNames.LATITUDE;
                    break;
                // Fallthrough: consider as Height
                case Pressure:
                case Height:
                    attributeNames = AttributeNames.VERTICAL;
                    break;
                // Fallthrough: consider as Time
                case RunTime:
                case Time:
                    attributeNames = AttributeNames.TIME;
                    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 List<Dimension> axisDomain = axis.getDimensions();
        final int[] indices = new int[axisDomain.size()];
        final int[] sizes = new int[indices.length];
        for (final Dimension dimension : axisDomain) {
            final int sourceDim = domain.lastIndexOf(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 ('i' not incremented).
                 */
        }
        axes[targetDim] = new Axis(this, axis, attributeNames, ArraysExt.resize(indices, i), ArraysExt.resize(sizes, i));
    }
    return axes;
}
Also used : CoordinateAxis(ucar.nc2.dataset.CoordinateAxis) AttributeNames(org.apache.sis.storage.netcdf.AttributeNames) AxisType(ucar.nc2.constants.AxisType) Dimension(ucar.nc2.Dimension) CoordinateAxis(ucar.nc2.dataset.CoordinateAxis) Axis(org.apache.sis.internal.netcdf.Axis)

Aggregations

Axis (org.apache.sis.internal.netcdf.Axis)1 AttributeNames (org.apache.sis.storage.netcdf.AttributeNames)1 Dimension (ucar.nc2.Dimension)1 AxisType (ucar.nc2.constants.AxisType)1 CoordinateAxis (ucar.nc2.dataset.CoordinateAxis)1