Search in sources :

Example 36 with DataType

use of com.serotonin.m2m2.DataType in project ma-modules-public by infiniteautomation.

the class MultiPointSeriesIdStatisticsStreamTest method createDataPoint.

@Override
protected DataPointVO createDataPoint(int dataSourceId, DataType dataType, int defaultCacheSize) {
    DataPointVO vo = super.createDataPoint(dataSourceId, dataType, defaultCacheSize);
    // Change series id
    int seriesId = DataPointDao.getInstance().insertNewTimeSeries();
    while (vo.getId() == seriesId) {
        seriesId = DataPointDao.getInstance().insertNewTimeSeries();
    }
    vo.setSeriesId(seriesId);
    DataPointDao.getInstance().update(vo.getId(), vo);
    return vo;
}
Also used : DataPointVO(com.serotonin.m2m2.vo.DataPointVO)

Example 37 with DataType

use of com.serotonin.m2m2.DataType in project imageio-ext by geosolutions-it.

the class NetCDFUtilities method getRawDataType.

/**
 * Returns the data type which most closely represents the "raw" internal
 * data of the variable. This is the value returned by the default
 * implementation of {@link NetcdfImageReader#getRawDataType}.
 *
 * @param variable
 *                The variable.
 * @return The data type, or {@link DataBuffer#TYPE_UNDEFINED} if unknown.
 *
 * @see NetcdfImageReader#getRawDataType
 */
public static int getRawDataType(final VariableIF variable) {
    VariableDS ds = (VariableDS) variable;
    final DataType type = ds.getOriginalDataType();
    return transcodeNetCDFDataType(type, variable.isUnsigned());
}
Also used : VariableDS(ucar.nc2.dataset.VariableDS) DataType(ucar.ma2.DataType)

Example 38 with DataType

use of com.serotonin.m2m2.DataType in project imageio-ext by geosolutions-it.

the class NetCDFUtilities method getAttributesAsString.

/**
 * Return the value of a NetCDF {@code Attribute} instance as a
 * {@code String}. The {@code isUnsigned} parameter allow to handle byte
 * attributes as unsigned, in order to represent values in the range
 * [0,255].
 */
public static String getAttributesAsString(Attribute attr, final boolean isUnsigned) {
    String[] values = null;
    if (attr != null) {
        final int nValues = attr.getLength();
        values = new String[nValues];
        final DataType datatype = attr.getDataType();
        // TODO: Improve the unsigned management
        if (datatype == DataType.BYTE) {
            if (isUnsigned)
                for (int i = 0; i < nValues; i++) {
                    byte val = attr.getNumericValue(i).byteValue();
                    int myByte = (0x000000FF & ((int) val));
                    short anUnsignedByte = (short) myByte;
                    values[i] = Short.toString(anUnsignedByte);
                }
            else {
                for (int i = 0; i < nValues; i++) {
                    byte val = attr.getNumericValue(i).byteValue();
                    values[i] = Byte.toString(val);
                }
            }
        } else if (datatype == DataType.SHORT) {
            for (int i = 0; i < nValues; i++) {
                short val = attr.getNumericValue(i).shortValue();
                values[i] = Short.toString(val);
            }
        } else if (datatype == DataType.INT) {
            for (int i = 0; i < nValues; i++) {
                int val = attr.getNumericValue(i).intValue();
                values[i] = Integer.toString(val);
            }
        } else if (datatype == DataType.LONG) {
            for (int i = 0; i < nValues; i++) {
                long val = attr.getNumericValue(i).longValue();
                values[i] = Long.toString(val);
            }
        } else if (datatype == DataType.DOUBLE) {
            for (int i = 0; i < nValues; i++) {
                double val = attr.getNumericValue(i).doubleValue();
                values[i] = Double.toString(val);
            }
        } else if (datatype == DataType.FLOAT) {
            for (int i = 0; i < nValues; i++) {
                float val = attr.getNumericValue(i).floatValue();
                values[i] = Float.toString(val);
            }
        } else if (datatype == DataType.STRING) {
            for (int i = 0; i < nValues; i++) {
                values[i] = attr.getStringValue(i);
            }
        } else {
            if (LOGGER.isLoggable(Level.WARNING))
                LOGGER.warning("Unhandled Attribute datatype " + attr.getDataType().getClassType().toString());
        }
    }
    String value = "";
    if (values != null) {
        StringBuffer sb = new StringBuffer();
        int j = 0;
        for (; j < values.length - 1; j++) {
            sb.append(values[j]).append(",");
        }
        sb.append(values[j]);
        value = sb.toString();
    }
    return value;
}
Also used : DataType(ucar.ma2.DataType)

Example 39 with DataType

use of com.serotonin.m2m2.DataType in project imageio-ext by geosolutions-it.

the class NCOMConverter method run.

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

Example 40 with DataType

use of com.serotonin.m2m2.DataType in project imageio-ext by geosolutions-it.

the class SWANConverter 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 hasZeta = false;
        // input dimensions
        final Dimension timeDim0 = ncFileIn.findDimension("time");
        final int nTimes = timeDim0.getLength();
        final Dimension latDim0 = ncFileIn.findDimension(NetCDFUtilities.LATITUDE);
        final int nLat = latDim0.getLength();
        final Dimension lonDim0 = ncFileIn.findDimension(NetCDFUtilities.LONGITUDE);
        final int nLon = lonDim0.getLength();
        // input VARIABLES
        final Variable timeOriginalVar = ncFileIn.findVariable("time");
        final Array timeOriginalData = timeOriginalVar.read();
        final Index timeOriginalIndex = timeOriginalData.getIndex();
        final DataType timeDataType = timeOriginalVar.getDataType();
        final Variable lonOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LONGITUDE);
        final DataType lonDataType = lonOriginalVar.getDataType();
        final Variable latOriginalVar = ncFileIn.findVariable(NetCDFUtilities.LATITUDE);
        final DataType latDataType = latOriginalVar.getDataType();
        final Array latOriginalData = latOriginalVar.read();
        final Array lonOriginalData = lonOriginalVar.read();
        // //
        // 
        // Depth related vars
        // 
        // //
        Array levelOriginalData = null;
        int nZeta = 0;
        Array zeta1Data = null;
        Dimension zDim = null;
        DataType zetaDataType = null;
        // Depth
        final Variable levelOriginalVar = ncFileIn.findVariable("z");
        if (levelOriginalVar != null) {
            nZeta = levelOriginalVar.getDimension(0).getLength();
            levelOriginalData = levelOriginalVar.read();
            zetaDataType = levelOriginalVar.getDataType();
            hasZeta = true;
        }
        Dimension timeDim = ncFileOut.addDimension("time", nTimes);
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, nLat);
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, nLon);
        if (hasZeta)
            zDim = ncFileOut.addDimension(NetCDFUtilities.HEIGHT, nZeta);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // Dimensions
        Variable timeVar = ncFileOut.addVariable("time", timeDataType, new Dimension[] { timeDim });
        NetCDFConverterUtilities.setVariableAttributes(timeOriginalVar, ncFileOut, new String[] { "long_name" });
        ncFileOut.addVariableAttribute("time", "long_name", "time");
        ncFileOut.addVariable(NetCDFUtilities.LAT, latDataType, new Dimension[] { latDim });
        NetCDFConverterUtilities.setVariableAttributes(latOriginalVar, ncFileOut, NetCDFUtilities.LAT);
        ncFileOut.addVariable(NetCDFUtilities.LON, lonDataType, new Dimension[] { lonDim });
        NetCDFConverterUtilities.setVariableAttributes(lonOriginalVar, ncFileOut, NetCDFUtilities.LON);
        if (hasZeta) {
            ncFileOut.addVariable(NetCDFUtilities.HEIGHT, zetaDataType, new Dimension[] { zDim });
            NetCDFConverterUtilities.setVariableAttributes(levelOriginalVar, ncFileOut, NetCDFUtilities.HEIGHT, new String[] { "long_name" });
            ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "positive", "up");
            ncFileOut.addVariableAttribute(NetCDFUtilities.HEIGHT, "long_name", NetCDFUtilities.HEIGHT);
        }
        // lat Variable
        Array lat1Data = NetCDFConverterUtilities.getArray(nLat, latDataType);
        NetCDFConverterUtilities.setData1D(latOriginalData, lat1Data, latDataType, nLat, true);
        // lon Variable
        Array lon1Data = NetCDFConverterUtilities.getArray(nLon, lonDataType);
        NetCDFConverterUtilities.setData1D(lonOriginalData, lon1Data, lonDataType, nLon, false);
        if (hasZeta) {
            // depth level Variable
            zeta1Data = NetCDFConverterUtilities.getArray(nZeta, zetaDataType);
            NetCDFConverterUtilities.setData1D(levelOriginalData, zeta1Data, zetaDataType, nZeta, false);
        }
        // {} Variables
        final ArrayList<String> variables = new ArrayList<String>(5);
        int numVars = 0;
        List<Variable> findVariables = ncFileIn.getVariables();
        for (Variable var : findVariables) {
            if (var != null) {
                String varName = var.getName();
                if (varName.equalsIgnoreCase(NetCDFUtilities.LATITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.LONGITUDE) || varName.equalsIgnoreCase(NetCDFUtilities.TIME) || varName.equalsIgnoreCase(NetCDFUtilities.ZETA))
                    continue;
                variables.add(varName);
                List<Dimension> dims = var.getDimensions();
                boolean hasLocalZeta = false;
                for (Dimension dim : dims) {
                    if (dim.getName().equalsIgnoreCase(NetCDFUtilities.ZETA)) {
                        hasLocalZeta = true;
                        break;
                    }
                }
                if (hasZeta && hasLocalZeta)
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, zDim, latDim, lonDim });
                else
                    ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, latDim, lonDim });
                NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "missing_value" });
                numVars++;
            }
        }
        // writing bin data ...
        ncFileOut.create();
        ArrayFloat timeData = new ArrayFloat(new int[] { timeDim.getLength() });
        Index timeIndex = timeData.getIndex();
        for (int t = 0; t < timeDim.getLength(); t++) {
            timeData.setFloat(timeIndex.set(t), timeOriginalData.getFloat(timeOriginalIndex.set(t)));
        }
        ncFileOut.write("time", timeData);
        timeVar = ncFileOut.findVariable("time");
        timeDim.addCoordinateVariable(timeVar);
        ncFileOut.write(NetCDFUtilities.LAT, lat1Data);
        ncFileOut.write(NetCDFUtilities.LON, lon1Data);
        if (hasZeta) {
            Variable heightVar = ncFileOut.findVariable(NetCDFUtilities.HEIGHT);
            zDim.addCoordinateVariable(heightVar);
            ncFileOut.write(NetCDFUtilities.HEIGHT, zeta1Data);
        }
        for (int i = 0; i < numVars; i++) {
            String varName = (String) variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            boolean hasLocalZeta = NetCDFConverterUtilities.hasThisDimension(var, NetCDFUtilities.ZETA);
            Array originalVarArray = var.read();
            DataType varDataType = var.getDataType();
            Array destArray = null;
            int[] dimensions = null;
            if (hasZeta && hasLocalZeta) {
                dimensions = new int[] { timeDim.getLength(), zDim.getLength(), latDim.getLength(), lonDim.getLength() };
            } else {
                dimensions = new int[] { timeDim.getLength(), latDim.getLength(), lonDim.getLength() };
            }
            destArray = NetCDFConverterUtilities.getArray(dimensions, varDataType);
            final boolean setZeta = hasZeta && hasLocalZeta;
            final int[] loopLengths;
            if (setZeta)
                loopLengths = new int[] { nTimes, nZeta, nLat, nLon };
            else
                loopLengths = new int[] { nTimes, nLat, nLon };
            NetCDFConverterUtilities.writeData(ncFileOut, varName, var, originalVarArray, destArray, false, false, loopLengths, true);
        }
        ncFileOut.close();
        outputFile.renameTo(fileOut);
    } catch (Exception e) {
        // something bad happened
        if (NetCDFConverterUtilities.LOGGER.isLoggable(Level.INFO))
            NetCDFConverterUtilities.LOGGER.log(Level.INFO, e.getLocalizedMessage(), e);
        JAI.getDefaultInstance().getTileCache().flush();
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) ArrayList(java.util.ArrayList) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) DataType(ucar.ma2.DataType) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile)

Aggregations

ArrayList (java.util.ArrayList)26 DataValue (com.serotonin.m2m2.rt.dataImage.types.DataValue)23 PointValueTime (com.serotonin.m2m2.rt.dataImage.PointValueTime)17 DataType (com.serotonin.m2m2.DataType)14 HashMap (java.util.HashMap)14 ShouldNeverHappenException (com.serotonin.ShouldNeverHappenException)13 TranslatableMessage (com.serotonin.m2m2.i18n.TranslatableMessage)11 DataPointVO (com.serotonin.m2m2.vo.DataPointVO)11 AlphanumericValue (com.serotonin.m2m2.rt.dataImage.types.AlphanumericValue)10 MultistateValue (com.serotonin.m2m2.rt.dataImage.types.MultistateValue)10 NumericValue (com.serotonin.m2m2.rt.dataImage.types.NumericValue)10 DataType (org.osate.aadl2.DataType)10 DataType (ucar.ma2.DataType)10 BinaryValue (com.serotonin.m2m2.rt.dataImage.types.BinaryValue)9 IOException (java.io.IOException)9 DataImplementation (org.osate.aadl2.DataImplementation)9 ResultTypeException (com.serotonin.m2m2.rt.script.ResultTypeException)8 File (java.io.File)7 ScriptError (com.serotonin.m2m2.rt.script.ScriptError)6 ScriptPermissionsException (com.serotonin.m2m2.rt.script.ScriptPermissionsException)6