Search in sources :

Example 1 with VariableDS

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

the class VariableWrapper method getGrid.

/**
 * Returns a builder for the grid geometry of this variable, or {@code null} if this variable is not a data cube.
 * This method searches for a grid previously computed by {@link DecoderWrapper#getGrids()}, keeping in mind that
 * the UCAR library sometime builds {@link CoordinateSystem} instances with axes in different order than what we
 * would expect. This method delegates to the super-class method only if the grid requires a different analysis
 * than the one performed by UCAR library.
 *
 * <p>This method should be invoked by {@link #getGridGeometry()} only once.
 * For that reason, it does not need to cache the value.</p>
 *
 * @see DecoderWrapper#getGrids()
 */
@Override
protected Grid getGrid(final GridAdjustment adjustment) throws IOException, DataStoreException {
    /*
         * In some netCDF files, more than one grid could be associated to a variable. If the names of the
         * variables to use as coordinate system axes have been specified, use those names for filtering.
         * Otherwise no filtering is applied (which is the common case). If more than one grid fit, take
         * the first grid having the largest number of dimensions.
         *
         * This block duplicates work done in super.getGrid(…), except that it focuses on the set of coordinate
         * systems identified by UCAR for this variable while super.getGrid(…) inspects all dimensions found in
         * the file. Note that those coordinate systems may have been set by the user.
         */
    if (variable instanceof VariableDS) {
        final List<CoordinateSystem> systems = ((VariableDS) variable).getCoordinateSystems();
        if (!systems.isEmpty()) {
            GridWrapper grid = null;
            final String[] axisNames = decoder.convention().namesOfAxisVariables(this);
            for (final Grid candidate : decoder.getGrids()) {
                final GridWrapper ordered = ((GridWrapper) candidate).forVariable(variable, systems, axisNames);
                if (ordered != null && (grid == null || ordered.getSourceDimensions() > grid.getSourceDimensions())) {
                    grid = ordered;
                }
            }
            if (grid != null) {
                return grid;
            }
        }
    }
    /*
         * If we reach this point, we did not found a grid using the dimensions of this variable.
         * But maybe there is a grid using other dimensions (typically with a decimation) that we
         * can map to the variable dimension using attribute values. This mechanism is described
         * in Convention.nameOfDimension(…).
         */
    return (GridWrapper) super.getGrid(adjustment);
}
Also used : CoordinateSystem(ucar.nc2.dataset.CoordinateSystem) VariableDS(ucar.nc2.dataset.VariableDS) Grid(org.apache.sis.internal.netcdf.Grid)

Example 2 with VariableDS

use of ucar.nc2.dataset.VariableDS in project imageio-ext by geosolutions-it.

the class GRIB1ImageReader method initialize.

/**
 * Initialize main properties for this reader.
 *
 * @throws exception
 *                 {@link InvalidRangeException}
 */
private synchronized void initialize() {
    int numImages = 0;
    indexMap = new HashMap<Range, GribVariableWrapper>();
    final NetcdfDataset dataset = reader.getDataset();
    try {
        if (dataset != null) {
            final List<Variable> variables = dataset.getVariables();
            if (variables != null) {
                for (final Variable variable : variables) {
                    if (variable != null && variable instanceof VariableDS) {
                        if (!NetCDFUtilities.isVariableAccepted(variable, CheckType.NONE)) {
                            if (variable.getName().equalsIgnoreCase(NetCDFUtilities.COORDSYS)) {
                                horizontalGrid = variable;
                            }
                            continue;
                        }
                        int[] shape = variable.getShape();
                        Range wrapperRange = null;
                        switch(shape.length) {
                            case 2:
                                wrapperRange = new Range(numImages, numImages + 1);
                                indexMap.put(wrapperRange, new GribVariableWrapper(variable, wrapperRange));
                                numImages++;
                                break;
                            case 3:
                                wrapperRange = new Range(numImages, numImages + shape[0]);
                                indexMap.put(wrapperRange, new GribVariableWrapper(variable, wrapperRange));
                                numImages += shape[0];
                                break;
                            case 4:
                                wrapperRange = new Range(numImages, numImages + shape[0] * shape[1]);
                                indexMap.put(wrapperRange, new GribVariableWrapper(variable, wrapperRange));
                                numImages += shape[0] * shape[1];
                                break;
                        }
                    }
                }
            }
        }
    } catch (InvalidRangeException e) {
        throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
    }
    setNumImages(numImages);
    reader.setNumImages(numImages);
    reader.setIndexMap(indexMap);
// numGlobalAttributes = 0;
// final List<Attribute> globalAttributes = dataset.getGlobalAttributes();
// if (globalAttributes != null && !globalAttributes.isEmpty())
// numGlobalAttributes = globalAttributes.size();
}
Also used : Variable(ucar.nc2.Variable) InvalidRangeException(ucar.ma2.InvalidRangeException) VariableDS(ucar.nc2.dataset.VariableDS) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) Range(ucar.ma2.Range) Point(java.awt.Point)

Example 3 with VariableDS

use of ucar.nc2.dataset.VariableDS in project imageio-ext by geosolutions-it.

the class NetCDFUtilities method getRawDataType.

/**
 * Returns the data type which most closely represents the "raw" internal
 * data of the variable. This is the value returned by the default
 * implementation of {@link NetcdfImageReader#getRawDataType}.
 *
 * @param variable
 *                The variable.
 * @return The data type, or {@link DataBuffer#TYPE_UNDEFINED} if unknown.
 *
 * @see NetcdfImageReader#getRawDataType
 */
public static int getRawDataType(final VariableIF variable) {
    VariableDS ds = (VariableDS) variable;
    final DataType type = ds.getOriginalDataType();
    return transcodeNetCDFDataType(type, variable.isUnsigned());
}
Also used : VariableDS(ucar.nc2.dataset.VariableDS) DataType(ucar.ma2.DataType)

Example 4 with VariableDS

use of ucar.nc2.dataset.VariableDS in project imageio-ext by geosolutions-it.

the class NetCDFImageReader method initialize.

/**
 * Initialize main properties for this reader.
 *
 * @throws exception
 *                 {@link InvalidRangeException}
 */
protected synchronized void initialize() {
    int numImages = 0;
    final Map<Range, NetCDFVariableWrapper> indexMap = new HashMap<Range, NetCDFVariableWrapper>();
    final NetcdfDataset dataset = reader.getDataset();
    try {
        if (dataset != null) {
            checkType = NetCDFUtilities.getCheckType(dataset);
            final List<Variable> variables = dataset.getVariables();
            if (variables != null) {
                for (final Variable variable : variables) {
                    if (variable != null && variable instanceof VariableDS) {
                        if (!NetCDFUtilities.isVariableAccepted(variable, checkType))
                            continue;
                        int[] shape = variable.getShape();
                        switch(shape.length) {
                            case 2:
                                indexMap.put(new Range(numImages, numImages + 1), new NetCDFVariableWrapper(variable));
                                numImages++;
                                break;
                            case 3:
                                indexMap.put(new Range(numImages, numImages + shape[0]), new NetCDFVariableWrapper(variable));
                                numImages += shape[0];
                                break;
                            case 4:
                                indexMap.put(new Range(numImages, numImages + shape[0] * shape[1]), new NetCDFVariableWrapper(variable));
                                numImages += shape[0] * shape[1];
                                break;
                        }
                    }
                }
            }
        } else
            throw new IllegalArgumentException("Not a valid dataset has been found");
    } catch (InvalidRangeException e) {
        throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
    }
    reader.setIndexMap(indexMap);
    setNumImages(numImages);
    reader.setNumImages(numImages);
    int numAttribs = 0;
    final List<Attribute> globalAttributes = dataset.getGlobalAttributes();
    if (globalAttributes != null && !globalAttributes.isEmpty())
        numAttribs = globalAttributes.size();
    reader.setNumGlobalAttributes(numAttribs);
}
Also used : Variable(ucar.nc2.Variable) HashMap(java.util.HashMap) Attribute(ucar.nc2.Attribute) InvalidRangeException(ucar.ma2.InvalidRangeException) VariableDS(ucar.nc2.dataset.VariableDS) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) Range(ucar.ma2.Range) Point(java.awt.Point)

Aggregations

VariableDS (ucar.nc2.dataset.VariableDS)4 Point (java.awt.Point)2 InvalidRangeException (ucar.ma2.InvalidRangeException)2 Range (ucar.ma2.Range)2 Variable (ucar.nc2.Variable)2 NetcdfDataset (ucar.nc2.dataset.NetcdfDataset)2 HashMap (java.util.HashMap)1 Grid (org.apache.sis.internal.netcdf.Grid)1 DataType (ucar.ma2.DataType)1 Attribute (ucar.nc2.Attribute)1 CoordinateSystem (ucar.nc2.dataset.CoordinateSystem)1