Search in sources :

Example 11 with Range

use of ucar.ma2.Range 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 12 with Range

use of ucar.ma2.Range in project imageio-ext by geosolutions-it.

the class NCOMConverter method run.

private void run(String fileNameIn, String fileNameOut) {
    try {
        final File fileIn = new File(fileNameIn);
        final NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
        final File fileOut = new File(fileNameOut);
        // keep original name
        final File outputFile = File.createTempFile(fileIn.getName(), ".tmp");
        final NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(outputFile.getAbsolutePath());
        boolean hasDepth = false;
        // input dimensions
        final Dimension timeDim0 = ncFileIn.findDimension("time");
        final int nTimes = timeDim0.getLength();
        final Dimension latDim0 = ncFileIn.findDimension("lat");
        final int nLat = latDim0.getLength();
        final Dimension lonDim0 = ncFileIn.findDimension("lon");
        final int nLon = lonDim0.getLength();
        // input VARIABLES
        final Variable timeOriginalVar = ncFileIn.findVariable("time");
        final Array timeOriginalData = timeOriginalVar.read();
        final Index timeOriginalIndex = timeOriginalData.getIndex();
        final DataType timeDataType = timeOriginalVar.getDataType();
        final Variable lonOriginalVar = ncFileIn.findVariable("lon");
        final DataType lonDataType = lonOriginalVar.getDataType();
        final Variable latOriginalVar = ncFileIn.findVariable("lat");
        final DataType latDataType = latOriginalVar.getDataType();
        final Array latOriginalData = latOriginalVar.read();
        final Array lonOriginalData = lonOriginalVar.read();
        // //
        // 
        // Depth related vars
        // 
        // //
        Array depthOriginalData = null;
        int nDepths = 0;
        Array depth1Data = null;
        Dimension depthDim = null;
        DataType depthDataType = null;
        // Depth
        final Variable depthOriginalVar = ncFileIn.findVariable("depth");
        if (depthOriginalVar != null) {
            nDepths = depthOriginalVar.getDimension(0).getLength();
            depthOriginalData = depthOriginalVar.read();
            depthDataType = depthOriginalVar.getDataType();
            hasDepth = true;
        }
        Dimension timeDim = ncFileOut.addDimension("time", nTimes);
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
        if (hasDepth)
            depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nDepths);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // Dimensions
        Variable timeVar = ncFileOut.addVariable("time", timeDataType, new Dimension[] { timeDim });
        NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, new String[] { "long_name" });
        ncFileOut.addVariableAttribute("time", "long_name", "time");
        ncFileOut.addVariable(NetCDFUtilities.LAT, latDataType, new Dimension[] { latDim });
        NetCDFConverterUtilities.setVariableAttributes(latOriginalVar, ncFileOut);
        ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
        NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut);
        if (hasDepth) {
            ncFileOut.addVariable(NetCDFUtilities.DEPTH, depthDataType, new Dimension[] { depthDim });
            NetCDFConverterUtilities.setVariableAttributes(depthOriginalVar, ncFileOut);
        }
        // lat Variable
        Array lat1Data = NetCDFConverterUtilities.getArray(nLat, latDataType);
        NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, latDataType, nLat, true);
        // lon Variable
        Array lon1Data = NetCDFConverterUtilities.getArray(nLon, lonDataType);
        NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, lonDataType, nLon, false);
        if (hasDepth) {
            // depth level Variable
            depth1Data = NetCDFConverterUtilities.getArray(nDepths, depthDataType);
            NetCDFConverterUtilities.setData1D(depthOriginalData, depth1Data, depthDataType, nDepths, false);
        }
        // {} Variables
        final ArrayList<String> variables = new ArrayList<String>(5);
        final HashMap<String, String> updatingValidRange = new HashMap<String, String>(5);
        int numVars = 0;
        for (int i = 0; i < NUMVARS; i++) {
            String varName = (String) VARIABLES.get(i);
            Variable var = ncFileIn.findVariable(varName);
            if (var != null) {
                variables.add(varName);
                List<Dimension> dims = var.getDimensions();
                boolean hasLocalDepth = false;
                for (Dimension dim : dims) {
                    if (dim.getName().equalsIgnoreCase("depth")) {
                        hasLocalDepth = true;
                        break;
                    }
                }
                if (hasDepth && hasLocalDepth)
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
                else
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
                Attribute validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MAX);
                if (validRange != null)
                    validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MIN);
                if (validRange == null)
                    validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE);
                if (validRange == null) {
                    updatingValidRange.put(varName, "");
                    ArrayShort range = new ArrayShort(new int[] { 2 });
                    Index index = range.getIndex();
                    range.setShort(index.set(0), Short.MIN_VALUE);
                    range.setShort(index.set(1), Short.MAX_VALUE);
                    ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.VALID_RANGE, range);
                }
                NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut);
                numVars++;
            }
        }
        // writing bin data ...
        ncFileOut.create();
        ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
        Index timeIndex = timeData.getIndex();
        for (int t = 0; t < timeDim.getLength(); t++) {
            timeData.setFloat(timeIndex.set(t), timeOriginalData.getFloat(timeOriginalIndex.set(t)));
        }
        ncFileOut.write("time", timeData);
        timeVar = ncFileOut.findVariable("time");
        timeDim.addCoordinateVariable(timeVar);
        ncFileOut.write(NetCDFUtilities.LAT, lat1Data);
        ncFileOut.write(NetCDFUtilities.LON, lon1Data);
        if (hasDepth) {
            Variable depthVar = ncFileOut.findVariable("depth");
            depthDim.addCoordinateVariable(depthVar);
            ncFileOut.write(NetCDFUtilities.DEPTH, depth1Data);
        }
        for (int i = 0; i < numVars; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, "depth");
            Array originalVarArray = var.read();
            DataType varDataType = var.getDataType();
            Array destArray = null;
            int[] dimensions = null;
            if (hasDepth && hasLocalDepth) {
                dimensions = new int[] { timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength() };
            } else {
                dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
            }
            destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
            boolean findNewRange = updatingValidRange.containsKey(varName);
            final boolean setDepth = hasDepth && hasLocalDepth;
            final int[] loopLengths;
            if (setDepth)
                loopLengths = new int[] { nTimes, nDepths, nLat, nLon };
            else
                loopLengths = new int[] { nTimes, nLat, nLon };
            NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, findNewRange, false, loopLengths, true);
        }
        ncFileOut.close();
        outputFile.renameTo(fileOut);
    } catch (Exception e) {
        // something bad happened
        if (NetCDFConverterUtilities.LOGGER.isLoggable(Level.INFO))
            NetCDFConverterUtilities.LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        JAI.getDefaultInstance().getTileCache().flush();
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) HashMap(java.util.HashMap) Attribute(ucar.nc2.Attribute) ArrayList(java.util.ArrayList) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) DataType(ucar.ma2.DataType) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile) ArrayShort(ucar.ma2.ArrayShort)

Example 13 with Range

use of ucar.ma2.Range in project imageio-ext by geosolutions-it.

the class NetCDFConverterUtilities method isFillValueOutsideValidRange.

public static boolean isFillValueOutsideValidRange(Attribute validRange, Attribute fillValue, DataType dataType) {
    Array range = validRange.getValues();
    Index index = range.getIndex();
    if (dataType == DataType.FLOAT) {
        final float min = range.getFloat(index.set(0));
        final float max = range.getFloat(index.set(1));
        final float fill = fillValue.getNumericValue().floatValue();
        if (fill == min || fill == max)
            return false;
        else
            return true;
    } else if (dataType == DataType.DOUBLE) {
        final double min = range.getDouble(index.set(0));
        final double max = range.getDouble(index.set(1));
        final double fill = fillValue.getNumericValue().doubleValue();
        if (fill == min || fill == max)
            return false;
        else
            return true;
    } else if (dataType == DataType.BYTE) {
        final byte min = range.getByte(index.set(0));
        final byte max = range.getByte(index.set(1));
        final byte fill = fillValue.getNumericValue().byteValue();
        if (fill == min || fill == max)
            return false;
        else
            return true;
    } else if (dataType == DataType.SHORT) {
        final short min = range.getShort(index.set(0));
        final short max = range.getShort(index.set(1));
        final short fill = fillValue.getNumericValue().shortValue();
        if (fill == min || fill == max)
            return false;
        else
            return true;
    } else if (dataType == DataType.INT) {
        final int min = range.getInt(index.set(0));
        final int max = range.getInt(index.set(1));
        final int fill = fillValue.getNumericValue().intValue();
        if (fill == min || fill == max)
            return false;
        else
            return true;
    }
    throw new IllegalArgumentException("Actually unsupported Datatype");
}
Also used : Array(ucar.ma2.Array) Index(ucar.ma2.Index)

Example 14 with Range

use of ucar.ma2.Range in project imageio-ext by geosolutions-it.

the class HDF4APSImageReader method initializeProfile.

/**
 * Initialize main properties for this <code>HDF4APSImageReader</code>
 */
protected void initializeProfile() throws IOException {
    final NetcdfDataset dataset = reader.getDataset();
    if (dataset == null) {
        throw new IOException("Unable to initialize profile due to a null dataset");
    }
    final List<Variable> variables = dataset.getVariables();
    final List<Attribute> attributes = dataset.getGlobalAttributes();
    final int numVars = variables.size();
    reader.setNumGlobalAttributes(attributes.size());
    // //
    // 
    // Getting projection dataset name
    // 
    // //
    final String navAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PFA_NA_MAPPROJECTION);
    if (navAttrib != null && navAttrib.length() > 0) {
        projectionDatasetName = navAttrib;
    }
    final String prodAttrib = NetCDFUtilities.getGlobalAttributeAsString(dataset, HDF4APSProperties.PRODLIST);
    int numImages = 0;
    if (prodAttrib != null && prodAttrib.length() > 0) {
        String[] products = prodAttrib.split(",");
        productList = HDF4APSProperties.refineProductList(products);
        numImages = productList.length;
    } else {
        numImages = numVars;
    }
    setNumImages(numImages);
    reader.setNumImages(numImages);
    final Map<Range, APSDatasetWrapper> indexMap = new HashMap<Range, APSDatasetWrapper>(numImages);
    Variable varProjection;
    // //
    // 
    // Setting spatial domain
    // 
    // //
    // getting map dataset
    varProjection = dataset.findVariable(projectionDatasetName);
    if (varProjection != null && varProjection.getName().equalsIgnoreCase(projectionDatasetName)) {
        // TODO: All projection share the same dataset
        // structure?
        Array data = varProjection.read();
        final int datatype = NetCDFUtilities.getRawDataType(varProjection);
        if (projectionMap == null) {
            projectionMap = buildProjectionAttributesMap(data, datatype);
        // Force UoM of MapBoundary product as the last element in
        // the map
        }
    }
    try {
        // Scanning all the datasets
        for (Variable var : variables) {
            final String name = var.getName();
            for (int j = 0; j < numImages; j++) {
                // Checking if the actual dataset is a product.
                if (name.equals(productList[j])) {
                    // Updating the subDatasetsMap map
                    indexMap.put(new Range(j, j + 1), new APSDatasetWrapper(var));
                    break;
                }
            }
        }
    } catch (InvalidRangeException e) {
        throw new IllegalArgumentException("Error occurred during NetCDF file parsing", e);
    }
    reader.setIndexMap(indexMap);
}
Also used : Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InvalidRangeException(ucar.ma2.InvalidRangeException) NetcdfDataset(ucar.nc2.dataset.NetcdfDataset) IOException(java.io.IOException) Range(ucar.ma2.Range) Array(ucar.ma2.Array)

Example 15 with Range

use of ucar.ma2.Range in project imageio-ext by geosolutions-it.

the class BaseNetCDFImageReader method getVariableWrapper.

public BaseVariableWrapper getVariableWrapper(final int imageIndex) {
    checkImageIndex(imageIndex);
    BaseVariableWrapper wrapper = null;
    for (Range range : indexMap.keySet()) {
        if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
            wrapper = indexMap.get(range);
        }
    }
    return wrapper;
}
Also used : Range(ucar.ma2.Range)

Aggregations

Range (org.apache.commons.lang3.Range)11 Date (java.util.Date)9 Test (org.junit.jupiter.api.Test)9 Variable (ucar.nc2.Variable)9 Array (ucar.ma2.Array)8 Range (ucar.ma2.Range)8 InvalidRangeException (ucar.ma2.InvalidRangeException)7 Attribute (ucar.nc2.Attribute)6 Point (java.awt.Point)5 HashMap (java.util.HashMap)5 IOException (java.io.IOException)4 ArrayFloat (ucar.ma2.ArrayFloat)4 ArrayShort (ucar.ma2.ArrayShort)4 DataType (ucar.ma2.DataType)4 NetcdfDataset (ucar.nc2.dataset.NetcdfDataset)4 Range (controlP5.Range)3 Slider (controlP5.Slider)3 Toggle (controlP5.Toggle)3 Rectangle (java.awt.Rectangle)3 BandedSampleModel (java.awt.image.BandedSampleModel)3