Search in sources :

Example 26 with DataType

use of com.ibm.j9ddr.vm29.j9.DataType in project imageio-ext by geosolutions-it.

the class MercatorOceanConverter 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());
        // input dimensions
        final Dimension latOriginalDim = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
        final Dimension lonOriginalDim = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
        // input VARIABLES
        // Variable time_0 = ncFileIn.findVariable("time");
        // Array time_0_Data = time_0.read();
        // Index time_0_Index = time_0_Data.getIndex();
        final Variable lonOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LONGITUDE);
        final int nLon = lonOriginalDim.getLength();
        final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
        final int nLat = latOriginalDim.getLength();
        final Array latOriginalData = latOriginalVar.read();
        final Index latOriginalIndex = latOriginalData.getIndex();
        final Array lonOriginalData = lonOriginalVar.read();
        final Index lonOriginalIndex = lonOriginalData.getIndex();
        final Variable depthOriginalVar = ncFileIn.findVariable(// Depth
        NetCDFUtilities.DEPTH);
        final int nLevels = depthOriginalVar.getDimension(0).getLength();
        final Array depthOriginalData = depthOriginalVar.read();
        // Dimension timeDim = ncFileOut.addDimension("time",
        // timeDim0.getLength());
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
        Dimension depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nLevels);
        Dimension timeDim = ncFileOut.addDimension(NetCDFUtilities.TIME, 1);
        // writing file
        computeMatrixExtremes(latOriginalData, lonOriginalData, nLon, nLat, latOriginalIndex, lonOriginalIndex);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // //
        // 
        // Time requires a special Management
        // 
        // //
        // time Variable
        Variable timeVar = ncFileOut.addVariable(NetCDFUtilities.TIME, DataType.FLOAT, new Dimension[] { timeDim });
        Attribute referenceTime = ncFileIn.findGlobalAttribute(TIME_ORIGIN);
        Attribute forecastDate = ncFileIn.findGlobalAttribute(FORECAST_DAY);
        int numDay = 0;
        if (referenceTime != null && forecastDate != null) {
            numDay = setTime(ncFileOut, referenceTime, forecastDate);
        }
        // final float referenceTime = setTimeVariableAttributes(timeVar,
        // ncFileOut);
        // lat Variable
        ArrayFloat latDestData = new ArrayFloat(new int[] { latOriginalDim.getLength() });
        Index latDestIndex = latDestData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDim });
        // NOTE: A Strange BUG (I guess)
        // when you copy attributes from old vars to new vars, it overwrite
        // coordvars!!!
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", NetCDFUtilities.LATITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, latOriginalVar.getUnitsString());
        // new String[] { "long_name", UNITS, "step" , "axis"});
        for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
            latDestData.setFloat(latDestIndex.set(yPos), new Float(this.ymax - (new Float(yPos).floatValue() * this.periodY)).floatValue());
        }
        // lon Variable
        ArrayFloat lonDestData = new ArrayFloat(new int[] { lonOriginalDim.getLength() });
        Index lonDestIndex = lonDestData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", NetCDFUtilities.LONGITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, lonOriginalVar.getUnitsString());
        // new String[] { "long_name", UNITS, "step", "axis"});
        for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
            lonDestData.setFloat(lonDestIndex.set(xPos), new Float(this.xmin + (new Float(xPos).floatValue() * this.periodX)).floatValue());
        }
        // depth level Variable
        ArrayFloat depthDestData = new ArrayFloat(new int[] { depthDim.getLength() });
        Index depthDestIndex = depthDestData.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "long_name", NetCDFUtilities.DEPTH);
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, UNITS, depthOriginalVar.getUnitsString());
        final Attribute positiveAttrib = depthOriginalVar.findAttribute("positive");
        String positiveValue = "down";
        if (positiveAttrib != null)
            positiveValue = positiveAttrib.getStringValue();
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "positive", positiveValue);
        for (int wPos = 0; wPos < depthDim.getLength(); wPos++) {
            depthDestData.setFloat(depthDestIndex.set(wPos), depthOriginalData.getFloat(depthDestIndex));
        }
        int numVars = 0;
        final ArrayList<String> variables = new ArrayList<String>(5);
        // {} Variables
        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, "depth");
                if (hasLocalDepth)
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
                else
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
                NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "positions" });
                numVars++;
            }
        }
        // writing bin data ...
        ncFileOut.create();
        ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
        Index timeIndex = timeData.getIndex();
        timeData.setFloat(timeIndex.set(0), numDay);
        ncFileOut.write(NetCDFUtilities.TIME, timeData);
        timeDim.addCoordinateVariable(timeVar);
        Variable latitudeVar = ncFileOut.findVariable(NetCDFUtilities.LAT);
        latDim.addCoordinateVariable(latitudeVar);
        ncFileOut.write(NetCDFUtilities.LAT, latDestData);
        Variable longitudeVar = ncFileOut.findVariable(NetCDFUtilities.LON);
        lonDim.addCoordinateVariable(longitudeVar);
        ncFileOut.write(NetCDFUtilities.LON, lonDestData);
        Variable depthVar = ncFileOut.findVariable(NetCDFUtilities.DEPTH);
        depthDim.addCoordinateVariable(depthVar);
        ncFileOut.write(NetCDFUtilities.DEPTH, depthDestData);
        for (int i = 0; i < numVars; i++) {
            final String varName = (String) variables.get(i);
            final Variable var = ncFileIn.findVariable(varName);
            final boolean hasLocalDepth = NetCDFConverterUtilities.hasThisDimension(var, "depth");
            final Array originalData = var.read();
            Index varIndex = originalData.getIndex();
            final DataType varDataType = var.getDataType();
            final Attribute fv = var.findAttribute("_FillValue");
            float fillValue = Float.NaN;
            if (fv != null) {
                fillValue = (fv.getNumericValue()).floatValue();
            }
            Array destArray = null;
            int[] dimensions = null;
            if (hasLocalDepth) {
                dimensions = new int[] { timeDim.getLength(), depthDim.getLength(), nLat, nLon };
            } else {
                dimensions = new int[] { timeDim.getLength(), nLat, nLon };
            }
            destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
            Index destIndex = destArray.getIndex();
            ArrayFloat tempData = new ArrayFloat(new int[] { latOriginalDim.getLength(), lonOriginalDim.getLength() });
            Index tempIndex = tempData.getIndex();
            if (hasLocalDepth) {
                for (int levelPos = 0; levelPos < depthDim.getLength(); levelPos++) {
                    for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
                        for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
                            tempData.setFloat(tempIndex.set(yPos, xPos), originalData.getFloat(varIndex.set(levelPos, yPos, xPos)));
                        }
                    }
                    final WritableRaster outData = Resampler(latOriginalData, lonOriginalData, lonOriginalDim.getLength(), latOriginalDim.getLength(), 2, tempData, fillValue);
                    for (int j = 0; j < latOriginalDim.getLength(); j++) {
                        for (int k = 0; k < lonOriginalDim.getLength(); k++) {
                            float sample = outData.getSampleFloat(k, j, 0);
                            destArray.setFloat(destIndex.set(0, levelPos, j, k), sample);
                        }
                    }
                }
            } else {
                for (int yPos = 0; yPos < latOriginalDim.getLength(); yPos++) {
                    for (int xPos = 0; xPos < lonOriginalDim.getLength(); xPos++) {
                        tempData.setFloat(tempIndex.set(yPos, xPos), originalData.getFloat(varIndex.set(yPos, xPos)));
                    }
                }
                final WritableRaster outData = Resampler(latOriginalData, lonOriginalData, lonOriginalDim.getLength(), latOriginalDim.getLength(), 2, tempData, fillValue);
                for (int j = 0; j < latOriginalDim.getLength(); j++) {
                    for (int k = 0; k < lonOriginalDim.getLength(); k++) {
                        float sample = outData.getSampleFloat(k, j, 0);
                        destArray.setFloat(destIndex.set(0, j, k), sample);
                    }
                }
            }
            // }
            ncFileOut.write(varName, destArray);
        }
        ncFileOut.close();
        outputFile.renameTo(fileOut);
    } catch (Exception e) {
        // something bad happened
        if (LOGGER.isLoggable(Level.INFO))
            LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        JAI.getDefaultInstance().getTileCache().flush();
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) ArrayList(java.util.ArrayList) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) ParseException(java.text.ParseException) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) ArrayFloat(ucar.ma2.ArrayFloat) WritableRaster(java.awt.image.WritableRaster) DataType(ucar.ma2.DataType) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile)

Example 27 with DataType

use of com.ibm.j9ddr.vm29.j9.DataType 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 28 with DataType

use of com.ibm.j9ddr.vm29.j9.DataType in project ma-core-public by infiniteautomation.

the class PointValueTimeDeserializer method deserialize.

@Override
public PointValueTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException {
    ObjectCodec codec = jsonParser.getCodec();
    JsonNode node = codec.readTree(jsonParser);
    JsonNode dataTypeNode = node.get("dataType");
    if (dataTypeNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing dataType");
    }
    JsonNode valueNode = node.get("value");
    if (valueNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing value");
    }
    JsonNode timestampNode = node.get("timestamp");
    if (timestampNode == null) {
        throw JsonMappingException.from(jsonParser, "Missing timestamp");
    }
    long timestamp = timestampNode.asLong();
    String dataTypeStr = dataTypeNode.asText();
    DataType dataType = DataType.fromName(dataTypeStr);
    if (dataType == null) {
        throw JsonMappingException.from(jsonParser, "Unknown dataType: " + dataTypeStr);
    }
    DataValue dataValue;
    switch(dataType) {
        case ALPHANUMERIC:
            dataValue = new AlphanumericValue(valueNode.asText());
            break;
        case BINARY:
            dataValue = new BinaryValue(valueNode.asBoolean());
            break;
        case MULTISTATE:
            dataValue = new MultistateValue(valueNode.asInt());
            break;
        case NUMERIC:
            dataValue = new NumericValue(valueNode.asDouble());
            break;
        default:
            throw JsonMappingException.from(jsonParser, "Unsupported dataType " + dataType);
    }
    JsonNode annotationNode = node.get("serializedAnnotation");
    if (annotationNode != null && !annotationNode.isNull()) {
        try {
            return new AnnotatedPointValueTime(dataValue, timestamp, TranslatableMessage.deserialize(annotationNode.asText()));
        } catch (TranslatableMessageParseException e) {
            throw JsonMappingException.from(jsonParser, "Can't deserialize annotation", e);
        }
    }
    return new PointValueTime(dataValue, timestamp);
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) BinaryValue(com.serotonin.m2m2.rt.dataImage.types.BinaryValue) JsonNode(com.fasterxml.jackson.databind.JsonNode) TranslatableMessageParseException(com.serotonin.m2m2.i18n.TranslatableMessageParseException) ObjectCodec(com.fasterxml.jackson.core.ObjectCodec) MultistateValue(com.serotonin.m2m2.rt.dataImage.types.MultistateValue) AlphanumericValue(com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) AnnotatedPointValueTime(com.serotonin.m2m2.rt.dataImage.AnnotatedPointValueTime) NumericValue(com.serotonin.m2m2.rt.dataImage.types.NumericValue)

Example 29 with DataType

use of com.ibm.j9ddr.vm29.j9.DataType in project ma-core-public by infiniteautomation.

the class DataPointDao method createValueConverterMap.

@Override
protected Map<String, Function<Object, Object>> createValueConverterMap() {
    Map<String, Function<Object, Object>> converters = super.createValueConverterMap();
    Map<String, Function<Object, Object>> myConverters = new HashMap<>();
    myConverters.put("dataTypeId", value -> {
        if (value instanceof String) {
            DataType type = DataType.fromName((String) value);
            return type == null ? null : type.getId();
        }
        return value;
    });
    return combine(converters, myConverters);
}
Also used : Function(java.util.function.Function) HashMap(java.util.HashMap) DataType(com.serotonin.m2m2.DataType)

Example 30 with DataType

use of com.ibm.j9ddr.vm29.j9.DataType in project ma-core-public by infiniteautomation.

the class SetPointHandlerRT method eventRaised.

@Override
public void eventRaised(EventInstance evt) {
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_NONE)
        return;
    // Validate that the target point is available.
    DataPointRT targetPoint = Common.runtimeManager.getDataPoint(vo.getTargetPointId());
    if (targetPoint == null) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetPointMissing"), evt.getEventType());
        return;
    }
    if (!targetPoint.getPointLocator().isSettable()) {
        raiseFailureEvent(new TranslatableMessage("event.setPoint.targetNotSettable"), evt.getEventType());
        return;
    }
    DataType targetDataType = targetPoint.getVO().getPointLocator().getDataType();
    DataValue value = null;
    if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_POINT_VALUE) {
        // Get the source data point.
        DataPointRT sourcePoint = Common.runtimeManager.getDataPoint(vo.getActivePointId());
        if (sourcePoint == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointMissing"), evt.getEventType());
            return;
        }
        PointValueTime valueTime = sourcePoint.getPointValue();
        if (valueTime == null) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointValue"), evt.getEventType());
            return;
        }
        if (valueTime.getValue().getDataType() != targetDataType) {
            raiseFailureEvent(new TranslatableMessage("event.setPoint.activePointDataType"), evt.getEventType());
            return;
        }
        value = valueTime.getValue();
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_STATIC_VALUE) {
        value = DataValue.stringToValue(vo.getActiveValueToSet(), targetDataType);
    } else if (vo.getActiveAction() == SetPointEventHandlerVO.SET_ACTION_SCRIPT_VALUE) {
        ArrayList<JsonImportExclusion> importExclusions = new ArrayList<JsonImportExclusion>();
        importExclusions.add(new JsonImportExclusion("xid", vo.getXid()) {

            @Override
            public String getImporterType() {
                return ConfigurationExportData.EVENT_HANDLERS;
            }
        });
        Map<String, IDataPointValueSource> context = new HashMap<String, IDataPointValueSource>();
        context.put(SetPointEventHandlerVO.TARGET_CONTEXT_KEY, targetPoint);
        Map<String, Object> additionalContext = new HashMap<String, Object>();
        additionalContext.put(EventInstance.CONTEXT_KEY, evt);
        additionalContext.put(EventInstanceWrapper.CONTEXT_KEY, new EventInstanceWrapper(evt));
        try (ScriptLog scriptLog = new ScriptLog("setPointHandler-" + evt.getId())) {
            for (IntStringPair cxt : vo.getAdditionalContext()) {
                DataPointRT dprt = Common.runtimeManager.getDataPoint(cxt.getKey());
                if (dprt != null)
                    context.put(cxt.getValue(), dprt);
            }
            CompiledMangoJavaScript activeScript = new CompiledMangoJavaScript(new SetCallback(vo.getScriptRoles()), scriptLog, additionalContext, null, importExclusions, false, service, vo.getScriptRoles());
            activeScript.compile(vo.getActiveScript(), true);
            activeScript.initialize(context);
            MangoJavaScriptResult result = activeScript.execute(Common.timer.currentTimeMillis(), evt.getActiveTimestamp(), targetPoint.getDataType());
            PointValueTime pvt = (PointValueTime) result.getResult();
            if (pvt != null)
                value = pvt.getValue();
        } catch (ScriptPermissionsException e) {
            raiseFailureEvent(e.getTranslatableMessage(), evt.getEventType());
            return;
        } catch (ScriptError e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getTranslatableMessage()), evt.getEventType());
            return;
        } catch (ResultTypeException e) {
            raiseFailureEvent(new TranslatableMessage("eventHandlers.invalidActiveScriptError", e.getMessage()), evt.getEventType());
            return;
        }
    } else
        throw new ShouldNeverHappenException("Unknown active action: " + vo.getActiveAction());
    // Queue a work item to perform the set point.
    if (MangoJavaScriptService.UNCHANGED != value)
        Common.backgroundProcessing.addWorkItem(new SetPointWorkItem(vo.getTargetPointId(), new PointValueTime(value, evt.getActiveTimestamp()), this));
}
Also used : DataValue(com.serotonin.m2m2.rt.dataImage.types.DataValue) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SetPointWorkItem(com.serotonin.m2m2.rt.maint.work.SetPointWorkItem) JsonImportExclusion(com.serotonin.m2m2.rt.script.JsonImportExclusion) ScriptLog(com.serotonin.m2m2.rt.script.ScriptLog) ResultTypeException(com.serotonin.m2m2.rt.script.ResultTypeException) ScriptError(com.serotonin.m2m2.rt.script.ScriptError) ScriptPermissionsException(com.serotonin.m2m2.rt.script.ScriptPermissionsException) DataPointRT(com.serotonin.m2m2.rt.dataImage.DataPointRT) PointValueTime(com.serotonin.m2m2.rt.dataImage.PointValueTime) DataType(com.serotonin.m2m2.DataType) TranslatableMessage(com.serotonin.m2m2.i18n.TranslatableMessage) IntStringPair(com.serotonin.db.pair.IntStringPair) MangoJavaScriptResult(com.infiniteautomation.mango.util.script.MangoJavaScriptResult) IDataPointValueSource(com.serotonin.m2m2.rt.dataImage.IDataPointValueSource) ShouldNeverHappenException(com.serotonin.ShouldNeverHappenException) CompiledMangoJavaScript(com.infiniteautomation.mango.util.script.CompiledMangoJavaScript) EventInstanceWrapper(com.serotonin.m2m2.rt.script.EventInstanceWrapper)

Aggregations

DataType (com.serotonin.m2m2.DataType)14 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 DataType (org.osate.aadl2.DataType)10 DataType (ucar.ma2.DataType)10 DataImplementation (org.osate.aadl2.DataImplementation)9 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)8 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)6 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)6 IOException (java.io.IOException)6 Array (ucar.ma2.Array)6 Variable (ucar.nc2.Variable)6 File (java.io.File)5 Dimension (ucar.nc2.Dimension)5 NetcdfFile (ucar.nc2.NetcdfFile)5 DataType (fi.csc.chipster.sessionworker.xml.schema2.DataType)4 PropertyAssociation (org.osate.aadl2.PropertyAssociation)4 ArrayFloat (ucar.ma2.ArrayFloat)4 Index (ucar.ma2.Index)4 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)4