Search in sources :

Example 51 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class NetCDFConverterUtilities method writeData.

public static void writeData(NetcdfFileWriteable ncFileOut, final String varName, Variable var, final Array originalVarData, final Array destArray, final boolean findNewRange, final boolean updateFillValue, final int[] loopLengths, final boolean flipY) throws IOException, InvalidRangeException {
    final int nestedLoops = loopLengths.length;
    final boolean setDepth = nestedLoops > 3;
    // timeDim
    final int timePositions = loopLengths[0];
    final int depthPositions;
    final int latPositions;
    final int lonPositions;
    if (setDepth) {
        depthPositions = loopLengths[1];
        latPositions = loopLengths[2];
        lonPositions = loopLengths[3];
    } else {
        depthPositions = -1;
        latPositions = loopLengths[1];
        lonPositions = loopLengths[2];
    }
    final DataType varDataType = var.getDataType();
    Attribute fv = null;
    if (updateFillValue)
        fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.MISSING_VALUE);
    else
        fv = var.findAttribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE);
    Index varIndex = originalVarData.getIndex();
    Index destIndex = destArray.getIndex();
    // //
    if (varDataType == DataType.FLOAT) {
        float min = Float.MAX_VALUE;
        float max = Float.MIN_VALUE;
        float fillValue = Float.MAX_VALUE;
        if (fv != null) {
            fillValue = (fv.getNumericValue()).floatValue();
        }
        if (setDepth) {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
                    for (int yPos = 0; yPos < latPositions; yPos++) {
                        for (int xPos = 0; xPos < lonPositions; xPos++) {
                            float sVal = originalVarData.getFloat(varIndex.set(tPos, levelPos, yPos, xPos));
                            if (findNewRange) {
                                if (sVal >= max && sVal != fillValue)
                                    max = sVal;
                                if (sVal <= min && sVal != fillValue)
                                    min = sVal;
                            }
                            int newYpos = yPos;
                            // Flipping y
                            if (flipY) {
                                newYpos = latPositions - yPos - 1;
                            }
                            destArray.setFloat(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
                        }
                    }
                }
            }
        } else {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int yPos = 0; yPos < latPositions; yPos++) {
                    for (int xPos = 0; xPos < lonPositions; xPos++) {
                        float sVal = originalVarData.getFloat(varIndex.set(tPos, yPos, xPos));
                        if (findNewRange) {
                            if (sVal >= max && sVal != fillValue)
                                max = sVal;
                            if (sVal <= min && sVal != fillValue)
                                min = sVal;
                        }
                        // Flipping y
                        int newYpos = yPos;
                        // Flipping y
                        if (flipY) {
                            newYpos = latPositions - yPos - 1;
                        }
                        destArray.setFloat(destIndex.set(tPos, newYpos, xPos), sVal);
                    }
                }
            }
        }
        ncFileOut.write(varName, destArray);
        if (findNewRange) {
            Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
            Index index = range.getIndex();
            range.setFloat(index.set(0), min);
            range.setFloat(index.set(1), max);
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
        }
        if (updateFillValue) {
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Float(fillValue)));
        }
    // //
    // 
    // DOUBLE
    // 
    // //
    } else if (varDataType == DataType.DOUBLE) {
        double min = Double.MAX_VALUE;
        double max = Double.MIN_VALUE;
        double fillValue = Double.MAX_VALUE;
        if (fv != null) {
            fillValue = (fv.getNumericValue()).doubleValue();
        }
        if (setDepth) {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
                    for (int yPos = 0; yPos < latPositions; yPos++) {
                        for (int xPos = 0; xPos < lonPositions; xPos++) {
                            double sVal = originalVarData.getDouble(varIndex.set(tPos, levelPos, yPos, xPos));
                            if (findNewRange) {
                                if (sVal >= max && sVal != fillValue)
                                    max = sVal;
                                if (sVal <= min && sVal != fillValue)
                                    min = sVal;
                            }
                            int newYpos = yPos;
                            // Flipping y
                            if (flipY) {
                                newYpos = latPositions - yPos - 1;
                            }
                            destArray.setDouble(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
                        }
                    }
                }
            }
        } else {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int yPos = 0; yPos < latPositions; yPos++) {
                    for (int xPos = 0; xPos < lonPositions; xPos++) {
                        double sVal = originalVarData.getDouble(varIndex.set(tPos, yPos, xPos));
                        if (findNewRange) {
                            if (sVal >= max && sVal != fillValue)
                                max = sVal;
                            if (sVal <= min && sVal != fillValue)
                                min = sVal;
                        }
                        // Flipping y
                        int newYpos = yPos;
                        // Flipping y
                        if (flipY) {
                            newYpos = latPositions - yPos - 1;
                        }
                        destArray.setDouble(destIndex.set(tPos, newYpos, xPos), sVal);
                    }
                }
            }
        }
        ncFileOut.write(varName, destArray);
        if (findNewRange) {
            Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
            Index index = range.getIndex();
            range.setDouble(index.set(0), min);
            range.setDouble(index.set(1), max);
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
        }
        if (updateFillValue) {
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Double(fillValue)));
        }
    // //
    // 
    // BYTE
    // 
    // //
    } else if (varDataType == DataType.BYTE) {
        byte min = Byte.MAX_VALUE;
        byte max = Byte.MIN_VALUE;
        byte fillValue = Byte.MAX_VALUE;
        if (fv != null) {
            fillValue = (fv.getNumericValue()).byteValue();
        }
        if (setDepth) {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
                    for (int yPos = 0; yPos < latPositions; yPos++) {
                        for (int xPos = 0; xPos < lonPositions; xPos++) {
                            byte sVal = originalVarData.getByte(varIndex.set(tPos, levelPos, yPos, xPos));
                            if (findNewRange) {
                                if (sVal >= max && sVal != fillValue)
                                    max = sVal;
                                if (sVal <= min && sVal != fillValue)
                                    min = sVal;
                            }
                            int newYpos = yPos;
                            // Flipping y
                            if (flipY) {
                                newYpos = latPositions - yPos - 1;
                            }
                            destArray.setByte(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
                        }
                    }
                }
            }
        } else {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int yPos = 0; yPos < latPositions; yPos++) {
                    for (int xPos = 0; xPos < lonPositions; xPos++) {
                        byte sVal = originalVarData.getByte(varIndex.set(tPos, yPos, xPos));
                        if (findNewRange) {
                            if (sVal >= max && sVal != fillValue)
                                max = sVal;
                            if (sVal <= min && sVal != fillValue)
                                min = sVal;
                        }
                        // Flipping y
                        int newYpos = yPos;
                        // Flipping y
                        if (flipY) {
                            newYpos = latPositions - yPos - 1;
                        }
                        destArray.setByte(destIndex.set(tPos, newYpos, xPos), sVal);
                    }
                }
            }
        }
        ncFileOut.write(varName, destArray);
        if (findNewRange) {
            Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
            Index index = range.getIndex();
            range.setByte(index.set(0), min);
            range.setByte(index.set(1), max);
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
        }
        if (updateFillValue) {
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Byte(fillValue)));
        }
    // //
    // 
    // SHORT
    // 
    // //
    } else if (varDataType == DataType.SHORT) {
        short min = Short.MAX_VALUE;
        short max = Short.MIN_VALUE;
        short fillValue = Short.MAX_VALUE;
        if (fv != null) {
            fillValue = (fv.getNumericValue()).shortValue();
        }
        if (setDepth) {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
                    for (int yPos = 0; yPos < latPositions; yPos++) {
                        for (int xPos = 0; xPos < lonPositions; xPos++) {
                            short sVal = originalVarData.getShort(varIndex.set(tPos, levelPos, yPos, xPos));
                            if (findNewRange) {
                                if (sVal >= max && sVal != fillValue)
                                    max = sVal;
                                if (sVal <= min && sVal != fillValue)
                                    min = sVal;
                            }
                            int newYpos = yPos;
                            // Flipping y
                            if (flipY) {
                                newYpos = latPositions - yPos - 1;
                            }
                            destArray.setShort(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
                        }
                    }
                }
            }
        } else {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int yPos = 0; yPos < latPositions; yPos++) {
                    for (int xPos = 0; xPos < lonPositions; xPos++) {
                        short sVal = originalVarData.getShort(varIndex.set(tPos, yPos, xPos));
                        if (findNewRange) {
                            if (sVal >= max && sVal != fillValue)
                                max = sVal;
                            if (sVal <= min && sVal != fillValue)
                                min = sVal;
                        }
                        // Flipping y
                        int newYpos = yPos;
                        // Flipping y
                        if (flipY) {
                            newYpos = latPositions - yPos - 1;
                        }
                        destArray.setShort(destIndex.set(tPos, newYpos, xPos), sVal);
                    }
                }
            }
        }
        ncFileOut.write(varName, destArray);
        if (findNewRange) {
            Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
            Index index = range.getIndex();
            range.setShort(index.set(0), min);
            range.setShort(index.set(1), max);
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
        }
        if (updateFillValue) {
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Short(fillValue)));
        }
    } else // //
    if (varDataType == DataType.INT) {
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int fillValue = Integer.MAX_VALUE;
        if (fv != null) {
            fillValue = (fv.getNumericValue()).intValue();
        }
        if (setDepth) {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int levelPos = 0; levelPos < depthPositions; levelPos++) {
                    for (int yPos = 0; yPos < latPositions; yPos++) {
                        for (int xPos = 0; xPos < lonPositions; xPos++) {
                            int sVal = originalVarData.getInt(varIndex.set(tPos, levelPos, yPos, xPos));
                            if (findNewRange) {
                                if (sVal >= max && sVal != fillValue)
                                    max = sVal;
                                if (sVal <= min && sVal != fillValue)
                                    min = sVal;
                            }
                            int newYpos = yPos;
                            // Flipping y
                            if (flipY) {
                                newYpos = latPositions - yPos - 1;
                            }
                            destArray.setInt(destIndex.set(tPos, levelPos, newYpos, xPos), sVal);
                        }
                    }
                }
            }
        } else {
            for (int tPos = 0; tPos < timePositions; tPos++) {
                for (int yPos = 0; yPos < latPositions; yPos++) {
                    for (int xPos = 0; xPos < lonPositions; xPos++) {
                        int sVal = originalVarData.getInt(varIndex.set(tPos, yPos, xPos));
                        if (findNewRange) {
                            if (sVal >= max && sVal != fillValue)
                                max = sVal;
                            if (sVal <= min && sVal != fillValue)
                                min = sVal;
                        }
                        // Flipping y
                        int newYpos = yPos;
                        // Flipping y
                        if (flipY) {
                            newYpos = latPositions - yPos - 1;
                        }
                        destArray.setInt(destIndex.set(tPos, newYpos, xPos), sVal);
                    }
                }
            }
        }
        ncFileOut.write(varName, destArray);
        if (findNewRange) {
            Array range = NetCDFConverterUtilities.getRangeArray(varDataType);
            Index index = range.getIndex();
            range.setInt(index.set(0), min);
            range.setInt(index.set(1), max);
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE, range));
        }
        if (updateFillValue) {
            ncFileOut.updateAttribute(ncFileOut.findVariable(varName), new Attribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE, new Integer(fillValue)));
        }
    } else
        throw new IllegalArgumentException("Unsupported DataType");
}
Also used : Attribute(ucar.nc2.Attribute) Index(ucar.ma2.Index) ArrayDouble(ucar.ma2.ArrayDouble) Array(ucar.ma2.Array) ArrayFloat(ucar.ma2.ArrayFloat) ArrayByte(ucar.ma2.ArrayByte) DataType(ucar.ma2.DataType) ArrayShort(ucar.ma2.ArrayShort)

Example 52 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class NetCDFImageReader method read.

/**
 * @see javax.imageio.ImageReader#read(int, javax.imageio.ImageReadParam)
 */
@Override
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
    clearAbortRequest();
    Variable variable = null;
    Range indexRange = null;
    NetCDFVariableWrapper wrapper = null;
    Map<Range, ?> indexMap = reader.getIndexMap();
    for (Range range : indexMap.keySet()) {
        if (range.contains(imageIndex) && range.first() <= imageIndex && imageIndex < range.last()) {
            wrapper = (NetCDFVariableWrapper) indexMap.get(range);
            indexRange = range;
            break;
        }
    }
    variable = wrapper.getVariable();
    /*
         * Fetches the parameters that are not already processed by utility
         * methods like 'getDestination' or 'computeRegions' (invoked below).
         */
    final int strideX, strideY;
    final int[] srcBands, dstBands;
    if (param != null) {
        strideX = param.getSourceXSubsampling();
        strideY = param.getSourceYSubsampling();
        srcBands = param.getSourceBands();
        dstBands = param.getDestinationBands();
    } else {
        strideX = 1;
        strideY = 1;
        srcBands = null;
        dstBands = null;
    }
    final int rank = wrapper.getRank();
    final int bandDimension = rank - NetCDFUtilities.Z_DIMENSION;
    /*
         * Gets the destination image of appropriate size. We create it now
         * since it is a convenient way to get the number of destination bands.
         */
    final int width = wrapper.getWidth();
    final int height = wrapper.getHeight();
    /*
         * Computes the source region (in the NetCDF file) and the destination
         * region (in the buffered image). Copies those informations into UCAR
         * Range structure.
         */
    final Rectangle srcRegion = new Rectangle();
    final Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, null, srcRegion, destRegion);
    // flipVertically(param, height, srcRegion);
    int destWidth = destRegion.x + destRegion.width;
    int destHeight = destRegion.y + destRegion.height;
    final List<Range> ranges = new LinkedList<Range>();
    for (int i = 0; i < rank; i++) {
        final int first, length, stride;
        switch(rank - i) {
            case NetCDFUtilities.X_DIMENSION:
                {
                    first = srcRegion.x;
                    length = srcRegion.width;
                    stride = strideX;
                    break;
                }
            case NetCDFUtilities.Y_DIMENSION:
                {
                    first = srcRegion.y;
                    length = srcRegion.height;
                    stride = strideY;
                    break;
                }
            default:
                {
                    if (i == bandDimension) {
                        first = NetCDFUtilities.getZIndex(variable, indexRange, imageIndex);
                    } else {
                        first = NetCDFUtilities.getTIndex(variable, indexRange, imageIndex);
                    }
                    length = 1;
                    stride = 1;
                    break;
                }
        }
        try {
            ranges.add(new Range(first, first + length - 1, stride));
        } catch (InvalidRangeException e) {
            throw netcdfFailure(e);
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    final WritableRaster raster = Raster.createWritableRaster(sampleModel, new Point(0, 0));
    final BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    /*
         * Reads the requested sub-region only.
         */
    processImageStarted(imageIndex);
    final int numDstBands = 1;
    final float toPercent = 100f / numDstBands;
    final int type = raster.getSampleModel().getDataType();
    final int xmin = destRegion.x;
    final int ymin = destRegion.y;
    final int xmax = destRegion.width + xmin;
    final int ymax = destRegion.height + ymin;
    for (int zi = 0; zi < numDstBands; zi++) {
        // final int srcBand = (srcBands == null) ? zi : srcBands[zi];
        final int dstBand = (dstBands == null) ? zi : dstBands[zi];
        final Array array;
        try {
            array = variable.read(sections);
        } catch (InvalidRangeException e) {
            throw netcdfFailure(e);
        }
        final IndexIterator it = array.getIndexIterator();
        // for (int y = ymax; --y >= ymin;) {
        for (int y = ymin; y < ymax; y++) {
            for (int x = xmin; x < xmax; x++) {
                switch(type) {
                    case DataBuffer.TYPE_DOUBLE:
                        {
                            raster.setSample(x, y, dstBand, it.getDoubleNext());
                            break;
                        }
                    case DataBuffer.TYPE_FLOAT:
                        {
                            raster.setSample(x, y, dstBand, it.getFloatNext());
                            break;
                        }
                    case DataBuffer.TYPE_BYTE:
                        {
                            byte b = it.getByteNext();
                            // int myByte = (0x000000FF & ((int) b));
                            // short anUnsignedByte = (short) myByte;
                            // raster.setSample(x, y, dstBand, anUnsignedByte);
                            raster.setSample(x, y, dstBand, b);
                            break;
                        }
                    default:
                        {
                            raster.setSample(x, y, dstBand, it.getIntNext());
                            break;
                        }
                }
            }
        }
        /*
             * Checks for abort requests after reading. It would be a waste of a
             * potentially good image (maybe the abort request occurred after we
             * just finished the reading) if we didn't implemented the
             * 'isCancel()' method. But because of the later, which is checked
             * by the NetCDF library, we can't assume that the image is
             * complete.
             */
        if (abortRequested()) {
            processReadAborted();
            return image;
        }
        /*
             * Reports progress here, not in the deeper loop, because the costly
             * part is the call to 'variable.read(...)' which can't report
             * progress. The loop that copy pixel values is fast, so reporting
             * progress there would be pointless.
             */
        processImageProgress(zi * toPercent);
    }
    if (lastError != null) {
        throw new IIOException(lastError);
    }
    processImageComplete();
    return image;
}
Also used : Variable(ucar.nc2.Variable) InvalidRangeException(ucar.ma2.InvalidRangeException) Rectangle(java.awt.Rectangle) IIOException(javax.imageio.IIOException) Point(java.awt.Point) IndexIterator(ucar.ma2.IndexIterator) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) BufferedImage(java.awt.image.BufferedImage) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster)

Example 53 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class GRIB1Utilities method getValuesAsString.

public static String getValuesAsString(Variable variable, int[] indexes) {
    final int dataType = NetCDFUtilities.getRawDataType(variable);
    final StringBuilder sb = new StringBuilder("");
    try {
        final int size = indexes.length;
        final Array values = variable.read();
        for (int i = 0; i < size; i++) {
            switch(dataType) {
                // TODO: ADD MORE.
                case DataBuffer.TYPE_SHORT:
                    final short val1s = values.getShort(indexes[i]);
                    sb.append(Short.toString(val1s));
                    break;
                case DataBuffer.TYPE_INT:
                    final int val1 = values.getInt(indexes[i]);
                    sb.append(Integer.toString(val1));
                    break;
                case DataBuffer.TYPE_FLOAT:
                    final float val1f = values.getFloat(indexes[i]);
                    sb.append(Float.toString(val1f));
                    break;
                case DataBuffer.TYPE_DOUBLE:
                    final double val1d = values.getDouble(indexes[i]);
                    sb.append(Double.toString(val1d));
                    break;
            }
            if (size > 1 && i != size - 1)
                sb.append(VALUES_SEPARATOR);
        }
    } catch (IOException e) {
    // TODO Auto-generated catch block
    // TODO LOG ME
    }
    return sb.toString();
}
Also used : Array(ucar.ma2.Array) IOException(java.io.IOException)

Example 54 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class BaseHDF4ImageReader method read2DVariable.

protected BufferedImage read2DVariable(final int imageIndex, final ImageReadParam param) throws IOException {
    BufferedImage image = null;
    final HDF4DatasetWrapper wrapper = getDatasetWrapper(imageIndex);
    final Variable variable = wrapper.getVariable();
    /*
         * Fetches the parameters that are not already processed by utility
         * methods like 'getDestination' or 'computeRegions' (invoked below).
         */
    final int strideX, strideY;
    final int[] srcBands, dstBands;
    if (param != null) {
        strideX = param.getSourceXSubsampling();
        strideY = param.getSourceYSubsampling();
        srcBands = param.getSourceBands();
        dstBands = param.getDestinationBands();
    } else {
        strideX = 1;
        strideY = 1;
        srcBands = null;
        dstBands = null;
    }
    final int rank = variable.getRank();
    /*
         * Gets the destination image of appropriate size. We create it now
         * since it is a convenient way to get the number of destination bands.
         */
    final int width = wrapper.getWidth();
    final int height = wrapper.getHeight();
    final int numBands = wrapper.getNumBands();
    /*
         * Computes the source region (in the NetCDF file) and the destination
         * region (in the buffered image). Copies those informations into UCAR
         * Range structure.
         */
    final Rectangle srcRegion = new Rectangle();
    final Rectangle destRegion = new Rectangle();
    computeRegions(param, width, height, null, srcRegion, destRegion);
    // flipVertically(param, height, srcRegion);
    int destWidth = destRegion.x + destRegion.width;
    int destHeight = destRegion.y + destRegion.height;
    final List<Range> ranges = new LinkedList<Range>();
    for (int i = 0; i < rank; i++) {
        final int first, length, stride;
        switch(i) {
            case 1:
                {
                    first = srcRegion.x;
                    length = srcRegion.width;
                    stride = strideX;
                    break;
                }
            case 0:
                {
                    first = srcRegion.y;
                    length = srcRegion.height;
                    stride = strideY;
                    break;
                }
            default:
                {
                    first = 0;
                    length = numBands;
                    stride = 1;
                    break;
                }
        }
        try {
            ranges.add(new Range(first, first + length - 1, stride));
        } catch (InvalidRangeException e) {
        // TODO LOGME
        }
    }
    final Section sections = new Section(ranges);
    /*
         * Setting SampleModel and ColorModel.
         */
    final SampleModel sampleModel = wrapper.getSampleModel().createCompatibleSampleModel(destWidth, destHeight);
    final ColorModel colorModel = ImageIOUtilities.createColorModel(sampleModel);
    /*
         * Reads the requested sub-region only.
         */
    final int size = destHeight * destWidth * numBands;
    Array array = null;
    try {
        array = variable.read(sections);
        DataBuffer dataBuffer = null;
        if (array instanceof ArrayByte) {
            dataBuffer = new DataBufferByte((byte[]) array.get1DJavaArray(byte.class), size);
        } else if (array instanceof ArrayShort) {
            dataBuffer = new DataBufferShort((short[]) array.get1DJavaArray(short.class), size);
        } else if (array instanceof ArrayInt) {
            dataBuffer = new DataBufferInt((int[]) array.get1DJavaArray(int.class), size);
        } else if (array instanceof ArrayFloat) {
            dataBuffer = new DataBufferFloat((float[]) array.get1DJavaArray(float.class), size);
        } else if (array instanceof ArrayDouble) {
            dataBuffer = new DataBufferDouble((double[]) array.get1DJavaArray(double.class), size);
        }
        WritableRaster raster = Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0, 0));
        image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null);
    } catch (InvalidRangeException e) {
    // TODO LOGME
    }
    return image;
}
Also used : DataBufferDouble(java.awt.image.DataBufferDouble) Variable(ucar.nc2.Variable) Rectangle(java.awt.Rectangle) ArrayFloat(ucar.ma2.ArrayFloat) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) ArrayDouble(ucar.ma2.ArrayDouble) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) ArrayByte(ucar.ma2.ArrayByte) ArrayShort(ucar.ma2.ArrayShort) DataBuffer(java.awt.image.DataBuffer) InvalidRangeException(ucar.ma2.InvalidRangeException) Point(java.awt.Point) Range(ucar.ma2.Range) Section(ucar.ma2.Section) Point(java.awt.Point) LinkedList(java.util.LinkedList) Array(ucar.ma2.Array) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) ArrayInt(ucar.ma2.ArrayInt) DataBufferFloat(java.awt.image.DataBufferFloat)

Example 55 with Array

use of org.openmuc.openiec61850.Array in project imageio-ext by geosolutions-it.

the class NetCDFCF_CLewis_Converter method setTimeVariableAttributes.

// ////////////////////////////////////////////////////////////////////////
// 
// HELPERS !!!
// 
// ////////////////////////////////////////////////////////////////////////
private float setTimeVariableAttributes(Variable time_0, NetcdfFileWriteable ncFileOut) throws IOException {
    Array time_0_Data = time_0.read();
    Index time_0_Index = time_0_Data.getIndex();
    final String name = time_0.getName();
    final float referenceTime = time_0_Data.getFloat(time_0_Index.set(0));
    float fTime = referenceTime;
    Attribute offset = time_0.findAttribute("add_offset");
    if (offset != null)
        fTime += offset.getNumericValue().floatValue();
    GregorianCalendar calendar = null;
    if (time_0.getDescription().toLowerCase().contains("modified julian")) {
        calendar = NetCDFConverterUtilities.fromModifiedJulian(fTime, time_0.getDescription(), time_0.getUnitsString());
    } else {
        calendar = NetCDFConverterUtilities.fromJulian(fTime);
    }
    final String year = Integer.toString(calendar.get(Calendar.YEAR));
    final String month = Integer.toString((calendar.get(Calendar.MONTH) + 1));
    final String day = Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
    String hour = Integer.toString(calendar.get(Calendar.HOUR));
    if (hour.equalsIgnoreCase("0"))
        hour += "0";
    String minute = Integer.toString(calendar.get(Calendar.MINUTE));
    if (minute.equalsIgnoreCase("0"))
        minute += "0";
    String second = Integer.toString(calendar.get(Calendar.SECOND));
    if (second.equalsIgnoreCase("0"))
        second += "0";
    final String millisecond = Integer.toString(calendar.get(Calendar.MILLISECOND));
    final StringBuffer sbTime = new StringBuffer(year).append("-").append(month).append("-").append(day).append(" ").append(hour).append(":").append(minute).append(":").append(second).append(".").append(millisecond);
    ncFileOut.addVariableAttribute(name, "units", "days since " + sbTime.toString());
    ncFileOut.addVariableAttribute(name, "long_name", "time");
    return referenceTime;
}
Also used : Array(ucar.ma2.Array) Attribute(ucar.nc2.Attribute) GregorianCalendar(java.util.GregorianCalendar) Index(ucar.ma2.Index)

Aggregations

Array (ucar.ma2.Array)72 Variable (ucar.nc2.Variable)41 IOException (java.io.IOException)36 Attribute (ucar.nc2.Attribute)29 InvalidRangeException (ucar.ma2.InvalidRangeException)20 Index (ucar.ma2.Index)18 ArrayList (java.util.ArrayList)17 Dimension (ucar.nc2.Dimension)15 NetcdfFile (ucar.nc2.NetcdfFile)15 ArrayFloat (ucar.ma2.ArrayFloat)13 File (java.io.File)12 RosettaGlobalAttribute (edu.ucar.unidata.rosetta.domain.RosettaGlobalAttribute)9 Test (org.junit.Test)9 ArrayDouble (ucar.ma2.ArrayDouble)9 DataType (ucar.ma2.DataType)9 WritableRaster (java.awt.image.WritableRaster)8 ProcedureTree (org.geotoolkit.observation.model.ExtractionResult.ProcedureTree)8 GeoSpatialBound (org.geotoolkit.observation.model.GeoSpatialBound)8 Process (org.geotoolkit.observation.xml.Process)8 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8