Search in sources :

Example 41 with Array

use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.

the class NodeContainer method writeFloatArray.

public void writeFloatArray(final SubDataAttribute child, final Float[] values) throws NodeWriteException {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    if (array.size() != values.length) {
        throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
    }
    for (int i = 0; i < values.length; i++) {
        final BdaFloat32 bdaFloat = (BdaFloat32) array.getChild(i);
        bdaFloat.setFloat(values[i]);
        this.writeNode(bdaFloat);
    }
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
Also used : Array(org.openmuc.openiec61850.Array) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaFloat32(org.openmuc.openiec61850.BdaFloat32)

Example 42 with Array

use of com.beanit.openiec61850.Array in project Protocol-Adapter-IEC61850 by OSGP.

the class NodeContainer method writeDateArray.

public void writeDateArray(final SubDataAttribute child, final Date[] values) throws NodeWriteException {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    if (array.size() != values.length) {
        throw new NodeWriteException(String.format("Invalid array size %d. Size on device is %d", values.length, array.size()));
    }
    for (int i = 0; i < values.length; i++) {
        final BdaTimestamp bdaTimestamp = (BdaTimestamp) array.getChild(i);
        bdaTimestamp.setDate(values[i]);
        this.writeNode(bdaTimestamp);
    }
// Unfortunately writing an array using "this.writeNode(array);"
// doesn't seem to work...
// Therefore the items are written in individual calls...
}
Also used : Array(org.openmuc.openiec61850.Array) NodeWriteException(com.alliander.osgp.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaTimestamp(org.openmuc.openiec61850.BdaTimestamp)

Example 43 with Array

use of com.beanit.openiec61850.Array in project sis by apache.

the class VariableWrapper method read.

/**
 * Reads a sub-sampled sub-area of the variable.
 *
 * @param  areaLower    index of the first value to read along each dimension.
 * @param  areaUpper    index after the last value to read along each dimension.
 * @param  subsampling  sub-sampling along each dimension. 1 means no sub-sampling.
 * @return the data as an array of a Java primitive type.
 */
@Override
public Vector read(final int[] areaLower, final int[] areaUpper, final int[] subsampling) throws IOException, DataStoreException {
    final int[] size = new int[areaUpper.length];
    for (int i = 0; i < size.length; i++) {
        size[i] = areaUpper[i] - areaLower[i];
    }
    final Array array;
    try {
        array = variable.read(new Section(areaLower, size, subsampling));
    } catch (InvalidRangeException e) {
        throw new DataStoreContentException(e);
    }
    return Vector.create(array.get1DJavaArray(array.getElementType()), variable.isUnsigned());
}
Also used : Array(ucar.ma2.Array) DataStoreContentException(org.apache.sis.storage.DataStoreContentException) InvalidRangeException(ucar.ma2.InvalidRangeException) Section(ucar.ma2.Section)

Example 44 with Array

use of com.beanit.openiec61850.Array in project sis by apache.

the class VariableWrapper method readArray.

/**
 * Reads the data from this variable and returns them as an array of a Java primitive type.
 * Multi-dimensional variables are flattened as a one-dimensional array (wrapped in a vector).
 * This method may replace fill/missing values by NaN values and caches the returned vector.
 *
 * @param  area         indices of cell values to read along each dimension, in "natural" order.
 * @param  subsampling  subsampling along each dimension, or {@code null} if none.
 * @return the data as an array of a Java primitive type.
 *
 * @see #read()
 * @see #read(GridExtent, int[])
 */
private Object readArray(final GridExtent area, final int[] subsampling) throws IOException, DataStoreException {
    int n = area.getDimension();
    final int[] lower = new int[n];
    final int[] size = new int[n];
    final int[] sub = (subsampling != null) ? new int[n] : null;
    n--;
    for (int i = 0; i <= n; i++) {
        final int j = (n - i);
        lower[j] = Math.toIntExact(area.getLow(i));
        size[j] = Math.toIntExact(area.getSize(i));
        if (sub != null) {
            sub[j] = subsampling[i];
        }
    }
    final Array array;
    try {
        array = variable.read(sub != null ? new Section(lower, size, sub) : new Section(lower, size));
    } catch (InvalidRangeException e) {
        throw new DataStoreException(e);
    }
    return get1DJavaArray(array);
}
Also used : Array(ucar.ma2.Array) DataStoreException(org.apache.sis.storage.DataStoreException) InvalidRangeException(ucar.ma2.InvalidRangeException) Section(ucar.ma2.Section)

Example 45 with Array

use of com.beanit.openiec61850.Array in project imageio-ext by geosolutions-it.

the class INGVConverter 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
        String timeName = "time_counter";
        Dimension timeOriginalDim = ncFileIn.findDimension(timeName);
        if (timeOriginalDim == null) {
            timeOriginalDim = ncFileIn.findDimension("time");
            timeName = "time";
        }
        final Dimension yDim = ncFileIn.findDimension("y");
        final Dimension xDim = ncFileIn.findDimension("x");
        // input VARIABLES
        final Variable timeOriginalVar = ncFileIn.findVariable(timeName);
        final Array timeOriginalData = timeOriginalVar.read();
        final DataType timeDataType = timeOriginalVar.getDataType();
        final Variable navLat = ncFileIn.findVariable(NAV_LAT);
        final DataType navLatDataType = navLat.getDataType();
        final Variable navLon = ncFileIn.findVariable(NAV_LON);
        final DataType navLonDataType = navLon.getDataType();
        final int nLat = yDim.getLength();
        final int nLon = xDim.getLength();
        final int nTimes = timeOriginalDim.getLength();
        final Array latOriginalData = navLat.read("0:" + (nLat - 1) + ":1, 0:0:1").reduce();
        final Array lonOriginalData = navLon.read("0:0:1, 0:" + (nLon - 1) + ":1").reduce();
        // //
        // 
        // Depth related vars
        // 
        // //
        Array depthOriginalData = null;
        DataType depthDataType = null;
        int nDepths = 0;
        Array depthDestData = null;
        Dimension depthDim = null;
        String depthName = "depth";
        Variable depthOriginalVar = null;
        int dName = 0;
        while (depthOriginalVar == null) {
            if (dName == depthNames.length)
                break;
            String name = depthNames[dName++];
            // Depth
            depthOriginalVar = ncFileIn.findVariable(name);
        }
        if (depthOriginalVar != null) {
            depthName = depthNames[dName - 1];
            nDepths = depthOriginalVar.getDimension(0).getLength();
            depthOriginalData = depthOriginalVar.read();
            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);
        // writing file
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // //
        // 
        // Time requires a special Management
        // 
        // //
        // time Variable
        Variable timeVar = ncFileOut.addVariable(NetCDFUtilities.TIME, timeDataType, new Dimension[] { timeDim });
        NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, NetCDFUtilities.TIME);
        // Dimensions
        ncFileOut.addVariable(NetCDFUtilities.LAT, navLatDataType, new Dimension[] { latDim });
        NetCDFConverterUtilities.setVariableAttributes(navLat, ncFileOut, NetCDFUtilities.LAT);
        ncFileOut.addVariable(NetCDFUtilities.LON, navLonDataType, new Dimension[] { lonDim });
        NetCDFConverterUtilities.setVariableAttributes(navLon, ncFileOut, NetCDFUtilities.LON);
        Array lat1Data = NetCDFConverterUtilities.getArray(nLat, navLatDataType);
        NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, navLatDataType, nLat, true);
        // lon Variable
        Array lon1Data = NetCDFConverterUtilities.getArray(nLon, navLonDataType);
        NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, navLonDataType, nLon, false);
        if (hasDepth) {
            depthDataType = depthOriginalVar.getDataType();
            ncFileOut.addVariable(NetCDFUtilities.DEPTH, depthDataType, new Dimension[] { depthDim });
            NetCDFConverterUtilities.setVariableAttributes(depthOriginalVar, ncFileOut, NetCDFUtilities.DEPTH);
        }
        if (hasDepth) {
            // depth level Variable
            depthDestData = NetCDFConverterUtilities.getArray(nDepths, depthDataType);
            NetCDFConverterUtilities.setData1D(depthOriginalData, depthDestData, depthDataType, nDepths, false);
        }
        // {} Variables
        final ArrayList<String> variables = new ArrayList<String>(5);
        final HashMap<String, String> updatingValidRange = new HashMap<String, String>(5);
        final HashMap<String, String> updatingFilLValue = 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);
                boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, depthName);
                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 });
                // //
                // 
                // Check for updating valid range
                // 
                // //
                boolean hasMinMax = false;
                Attribute validMax = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MAX);
                Attribute validMin = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_MIN);
                Attribute fillValue = var.findAttribute(NetCDFUtilities.DatasetAttribs.FILL_VALUE);
                boolean hasMissingValue = false;
                boolean hasFillValue = true;
                if (fillValue == null) {
                    hasFillValue = false;
                    fillValue = var.findAttribute(NetCDFUtilities.DatasetAttribs.MISSING_VALUE);
                    if (fillValue != null)
                        hasMissingValue = true;
                }
                Attribute validRange = var.findAttribute(NetCDFUtilities.DatasetAttribs.VALID_RANGE);
                boolean hasValidRange = false;
                boolean rewriteAttribute = false;
                if (validMin != null && validMax != null && fillValue != null) {
                    rewriteAttribute = !NetCDFConverterUtilities.isFillValueOutsideValidRange(validMax, validMin, fillValue, var.getDataType());
                    hasMinMax = true;
                } else if (validRange != null && fillValue != null) {
                    rewriteAttribute = !NetCDFConverterUtilities.isFillValueOutsideValidRange(validRange, fillValue, var.getDataType());
                    hasValidRange = true;
                } else {
                    rewriteAttribute = true;
                }
                if (rewriteAttribute) {
                    updatingValidRange.put(varName, "");
                    DataType varDatatype = var.getDataType();
                    Array range = NetCDFConverterUtilities.getRangeArray(varDatatype);
                    ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.VALID_RANGE, range);
                    if (hasMissingValue && !hasFillValue) {
                        updatingFilLValue.put(varName, "");
                        Number fillVal = NetCDFConverterUtilities.getNumber(varDatatype);
                        ncFileOut.addVariableAttribute(varName, NetCDFUtilities.DatasetAttribs.FILL_VALUE, fillVal);
                    }
                }
                String[] exceptions = null;
                if (hasMinMax) {
                    if (hasMissingValue)
                        exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_MAX, NetCDFUtilities.DatasetAttribs.VALID_MIN, NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
                    else
                        exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_MAX, NetCDFUtilities.DatasetAttribs.VALID_MIN };
                } else if (hasValidRange) {
                    if (hasMissingValue)
                        exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_RANGE, NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
                    else
                        exceptions = new String[] { NetCDFUtilities.DatasetAttribs.VALID_RANGE };
                } else if (hasMissingValue)
                    exceptions = new String[] { NetCDFUtilities.DatasetAttribs.MISSING_VALUE };
                NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, exceptions);
                numVars++;
            }
        }
        // writing bin data ...
        ncFileOut.create();
        Array timeData = NetCDFConverterUtilities.getArray(nTimes, timeDataType);
        NetCDFConverterUtilities.setData1D(timeOriginalData, timeData, timeDataType, nTimes, false);
        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, depthDestData);
        }
        for (int i = 0; i < numVars; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            final boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, depthName);
            Array originalVarArray = var.read();
            DataType varDataType = var.getDataType();
            Array destArray = null;
            int[] dimensions = null;
            final boolean setDepth = hasDepth && hasLocalDepth;
            if (setDepth) {
                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);
            boolean updateFillValue = updatingFilLValue.containsKey(varName);
            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, updateFillValue, 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);
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) HashMap(java.util.HashMap) Attribute(ucar.nc2.Attribute) ArrayList(java.util.ArrayList) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) DataType(ucar.ma2.DataType) NetcdfFile(ucar.nc2.NetcdfFile) File(java.io.File)

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