Search in sources :

Example 51 with Array

use of com.beanit.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 52 with Array

use of com.beanit.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 53 with Array

use of com.beanit.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 54 with Array

use of com.beanit.openiec61850.Array in project OpenTripPlanner by opentripplanner.

the class GenericEdgeUpdater method getClosestPropertyValue.

/**
 * Returns closest a value of selected property for given point.
 * <p>
 * Returned array represent value for selected property time
 *
 * @param samplePoint  point
 * @param propertyName propertyName
 * @return result the closest value for selected property for given point
 */
private <E> List<E> getClosestPropertyValue(Point2D samplePoint, String propertyName) {
    double lon = samplePoint.getX();
    double lat = samplePoint.getY();
    int lonIndex = getClosestIndex(dataFile.getLongitudeArray(), lon);
    int latIndex = getClosestIndex(dataFile.getLatitudeArray(), lat);
    int timeSize = (int) dataFile.getTimeArray().getSize();
    List result = new ArrayList<>();
    int height = 0;
    for (int timeIndex = 0; timeIndex < timeSize; timeIndex++) {
        Array dataArray = genericVariablesData.get(propertyName);
        Index selectIndex = dataArray.getIndex();
        if (selectIndex.getRank() == 3) {
            selectIndex.set(timeIndex, latIndex, lonIndex);
        } else if (selectIndex.getRank() == 4) {
            selectIndex.set(timeIndex, height, latIndex, lonIndex);
        } else {
            throw new IllegalArgumentException(String.format("Invalid data array shape for %s", propertyName));
        }
        Class dataArrayType = dataArray.getDataType().getPrimitiveClassType();
        if (dataArrayType.equals(Integer.TYPE)) {
            result.add(timeIndex, ((ArrayInt) dataArray).get(selectIndex));
        } else if (dataArrayType.equals(Double.TYPE)) {
            result.add(timeIndex, ((ArrayDouble) dataArray).get(selectIndex));
        } else if (dataArrayType.equals(Float.TYPE)) {
            result.add(timeIndex, ((ArrayFloat) dataArray).get(selectIndex));
        } else if (dataArrayType.equals(Long.TYPE)) {
            result.add(timeIndex, ((ArrayLong) dataArray).get(selectIndex));
        } else {
            throw new IllegalArgumentException(String.format("Unsupported format %s of %s variable", dataArrayType, propertyName));
        }
    }
    return result;
}
Also used : Array(ucar.ma2.Array) ArrayDouble(ucar.ma2.ArrayDouble) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Index(ucar.ma2.Index) ArrayLong(ucar.ma2.ArrayLong)

Example 55 with Array

use of com.beanit.openiec61850.Array in project open-smart-grid-platform by OSGP.

the class NodeContainer method getFloatArray.

public Float[] getFloatArray(final SubDataAttribute child) {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    final int size = array.size();
    final Float[] result = new Float[size];
    for (int i = 0; i < size; i++) {
        result[i] = ((BdaFloat32) array.getChild(i)).getFloat();
    }
    return result;
}
Also used : Array(com.beanit.openiec61850.Array)

Aggregations

Array (ucar.ma2.Array)62 IOException (java.io.IOException)34 Variable (ucar.nc2.Variable)31 Index (ucar.ma2.Index)20 InvalidRangeException (ucar.ma2.InvalidRangeException)19 Attribute (ucar.nc2.Attribute)19 ArrayList (java.util.ArrayList)14 Dimension (ucar.nc2.Dimension)13 File (java.io.File)12 ArrayFloat (ucar.ma2.ArrayFloat)12 NetcdfFile (ucar.nc2.NetcdfFile)12 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 ArrayDouble (ucar.ma2.ArrayDouble)7 DataType (ucar.ma2.DataType)7 HashMap (java.util.HashMap)5 ArrayInt (ucar.ma2.ArrayInt)5