Search in sources :

Example 56 with Array

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

the class NetCDFCF_CLewis_Converter method run.

private void run(String fileNameIn, String fileNameOut) {
    try {
        NetcdfFile ncFileIn = NetcdfFile.open(fileNameIn);
        ncFileIn.writeCDL(System.out, true);
        NetcdfFileWriteable ncFileOut = NetcdfFileWriteable.createNew(fileNameOut);
        // input dimensions
        Dimension timeDim0 = ncFileIn.findDimension("time");
        Dimension latDim0 = ncFileIn.findDimension("lat");
        Dimension lonDim0 = ncFileIn.findDimension("lon");
        // input variables
        Variable time_0 = ncFileIn.findVariable("time");
        Array time_0_Data = time_0.read();
        Index time_0_Index = time_0_Data.getIndex();
        Variable lon_0 = ncFileIn.findVariable("lon");
        final int nLon = lon_0.getDimension(0).getLength();
        Variable lat_0 = ncFileIn.findVariable("lat");
        final int nLat = lat_0.getDimension(0).getLength();
        Array lat_0_Data = lat_0.read();
        Index lat_0_Index = lat_0_Data.getIndex();
        Array lon_0_Data = lon_0.read();
        Index lon_0_Index = lon_0_Data.getIndex();
        // Depth
        Variable z_0 = ncFileIn.findVariable("z");
        final int nLevels = z_0.getDimension(0).getLength();
        Array z_0_Data = z_0.read();
        Index z_0_Index = z_0_Data.getIndex();
        Dimension timeDim = ncFileOut.addDimension("time", timeDim0.getLength());
        Dimension latDim = ncFileOut.addDimension(NetCDFUtilities.LAT, latDim0.getLength());
        Dimension lonDim = ncFileOut.addDimension(NetCDFUtilities.LON, lonDim0.getLength());
        Dimension depthDim = ncFileOut.addDimension(NetCDFUtilities.DEPTH, nLevels);
        // writing file
        this.computeMatrixExtremes(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), lat_0_Index, lon_0_Index);
        NetCDFConverterUtilities.copyGlobalAttributes(ncFileOut, ncFileIn.getGlobalAttributes());
        // //
        // 
        // Time requires a special Management
        // 
        // //
        // time Variable
        ncFileOut.addVariable("time", DataType.FLOAT, new Dimension[] { timeDim });
        // ncFileOut.addVariableAttribute("Time", "long_name", "Time");
        final float referenceTime = setTimeVariableAttributes(time_0, ncFileOut);
        // lat Variable
        ArrayFloat lat_1_Data = new ArrayFloat(new int[] { latDim0.getLength() });
        Index lat_1_Index = lat_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LAT, DataType.FLOAT, new Dimension[] { latDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, "long_name", NetCDFUtilities.LATITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LAT, UNITS, lat_0.getUnitsString());
        for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
            lat_1_Data.setFloat(lat_1_Index.set(yPos), new Float(this.ymax - (new Float(yPos).floatValue() * this.periodY)).floatValue());
        // new Float(
        // this.ymin
        // + (new Float(yPos)
        // .floatValue() * this.periodY))
        // .floatValue());
        }
        // lon Variable
        ArrayFloat lon_1_Data = new ArrayFloat(new int[] { lonDim0.getLength() });
        Index lon_1_Index = lon_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.LON, DataType.FLOAT, new Dimension[] { lonDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, "long_name", NetCDFUtilities.LONGITUDE);
        ncFileOut.addVariableAttribute(NetCDFUtilities.LON, UNITS, lon_0.getUnitsString());
        for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
            lon_1_Data.setFloat(lon_1_Index.set(xPos), new Float(this.xmin + (new Float(xPos).floatValue() * this.periodX)).floatValue());
        }
        // depth level Variable
        ArrayFloat depthlevelDim_1_Data = new ArrayFloat(new int[] { depthDim.getLength() });
        Index depthlevelDim_1_Index = depthlevelDim_1_Data.getIndex();
        ncFileOut.addVariable(NetCDFUtilities.DEPTH, DataType.FLOAT, new Dimension[] { depthDim });
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "long_name", NetCDFUtilities.DEPTH);
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, UNITS, z_0.getUnitsString());
        ncFileOut.addVariableAttribute(NetCDFUtilities.DEPTH, "positive", "up");
        for (int wPos = 0; wPos < depthDim.getLength(); wPos++) {
            depthlevelDim_1_Data.setFloat(depthlevelDim_1_Index.set(wPos), z_0_Data.getFloat(depthlevelDim_1_Index));
        }
        // {} Variables
        for (int i = 0; i < NUMVARS; i++) {
            String varName = variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            ncFileOut.addVariable(varName, var.getDataType(), new Dimension[] { timeDim, depthDim, latDim, lonDim });
            NetCDFConverterUtilities.setVariableAttributes(var, ncFileOut, new String[] { "positions" });
        }
        // 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++) {
            float julianTime = time_0_Data.getFloat(time_0_Index.set(t)) - referenceTime;
            timeData.setFloat(timeIndex.set(t), julianTime);
        }
        ncFileOut.write("time", timeData);
        Variable timeVar = ncFileOut.findVariable("time");
        timeDim.addCoordinateVariable(timeVar);
        ncFileOut.write(NetCDFUtilities.LAT, lat_1_Data);
        ncFileOut.write(NetCDFUtilities.LON, lon_1_Data);
        Variable depthVar = ncFileOut.findVariable("depth");
        depthDim.addCoordinateVariable(depthVar);
        ncFileOut.write(NetCDFUtilities.DEPTH, depthlevelDim_1_Data);
        // TODO: AutoApply MASK?
        ArrayFloat maskMatrix = new ArrayFloat.D2(latDim.getLength(), lonDim.getLength());
        Index maskIma = maskMatrix.getIndex();
        if (APPLY_MASK) {
            Variable mask = ncFileIn.findVariable("mask");
            Array maskData = mask.read();
            Index maskIndex = maskData.getIndex();
            ArrayFloat tempData = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
            Index tempIndex = tempData.getIndex();
            for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
                for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
                    tempData.setFloat(tempIndex.set(yPos, xPos), maskData.getFloat(maskIndex.set(yPos, xPos)));
                }
            }
            WritableRaster outData = this.Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, tempData, -1);
            for (int j = 0; j < latDim0.getLength(); j++) {
                for (int k = 0; k < lonDim0.getLength(); k++) {
                    float sample = outData.getSampleFloat(k, j, 0);
                    maskMatrix.setFloat(maskIma.set(j, k), sample);
                }
            }
        }
        for (int i = 0; i < NUMVARS; i++) {
            String varName = variables.get(i);
            Variable var = ncFileIn.findVariable(varName);
            Array originalVarData = var.read();
            Index varIndex = originalVarData.getIndex();
            Attribute fv = var.findAttribute("_FillValue");
            float fillValue = Float.NaN;
            if (fv != null) {
                fillValue = (fv.getNumericValue()).floatValue();
            }
            ArrayFloat T_tmp_Data = new ArrayFloat(new int[] { latDim0.getLength(), lonDim0.getLength() });
            Index T_tmp_Index = T_tmp_Data.getIndex();
            ArrayFloat Tmatrix = new ArrayFloat.D4(timeDim.getLength(), depthDim.getLength(), latDim.getLength(), lonDim.getLength());
            Index Tima = Tmatrix.getIndex();
            for (int tPos = 0; tPos < timeDim0.getLength(); tPos++) {
                for (int levelPos = 0; levelPos < depthDim.getLength(); levelPos++) {
                    for (int yPos = 0; yPos < latDim0.getLength(); yPos++) {
                        for (int xPos = 0; xPos < lonDim0.getLength(); xPos++) {
                            T_tmp_Data.setFloat(T_tmp_Index.set(yPos, xPos), originalVarData.getFloat(varIndex.set(tPos, yPos, xPos, levelPos)));
                        }
                    }
                    WritableRaster outData = this.Resampler(lat_0_Data, lon_0_Data, lonDim0.getLength(), latDim0.getLength(), 2, T_tmp_Data, fillValue);
                    for (int j = 0; j < latDim0.getLength(); j++) {
                        for (int k = 0; k < lonDim0.getLength(); k++) {
                            float sample = outData.getSampleFloat(k, j, 0);
                            if (APPLY_MASK) {
                                float maskValue = maskMatrix.getFloat(maskIma.set(j, k));
                                if (maskValue == 0)
                                    sample = fillValue;
                            }
                            Tmatrix.setFloat(Tima.set(tPos, levelPos, j, k), sample);
                        }
                    }
                }
            }
            ncFileOut.write(varName, Tmatrix);
        }
        ncFileOut.close();
    } catch (Exception e) {
        e.printStackTrace(System.out);
    }
}
Also used : NetcdfFileWriteable(ucar.nc2.NetcdfFileWriteable) Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) Index(ucar.ma2.Index) ArrayFloat(ucar.ma2.ArrayFloat) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) ArrayFloat(ucar.ma2.ArrayFloat) WritableRaster(java.awt.image.WritableRaster)

Example 57 with Array

use of org.openmuc.openiec61850.Array in project risky by amsa-code.

the class NetcdfFixesWriter method writeFixes.

public static void writeFixes(List<HasFix> fixes, File file) {
    if (fixes.size() == 0) {
        return;
    }
    try {
        // TODO evaluate use of NetCdf structures
        NetcdfFileWriter f = NetcdfFileWriter.createNew(Version.netcdf3, file.getPath());
        // add version attribute
        f.addGroupAttribute(null, new Attribute("version", "0.1"));
        // Create netCDF dimensions
        Dimension dimTime = f.addUnlimitedDimension("time");
        Dimension dimLat = f.addDimension(null, "latitude", fixes.size());
        Dimension dimLon = f.addDimension(null, "longitude", fixes.size());
        Dimension dimSource = f.addDimension(null, "source", fixes.size());
        Dimension dimLatency = f.addDimension(null, "latency", fixes.size());
        Dimension dimNavStatus = f.addDimension(null, "navigational_status", fixes.size());
        Dimension dimRateOfTurn = f.addDimension(null, "rate_of_turn", fixes.size());
        Dimension dimSpeedOverGround = f.addDimension(null, "speed_over_ground", fixes.size());
        Dimension dimCourseOverGround = f.addDimension(null, "course_over_ground", fixes.size());
        Dimension dimHeading = f.addDimension(null, "heading", fixes.size());
        Dimension dimAisClass = f.addDimension(null, "ais_class", fixes.size());
        Variable varLat = f.addVariable(null, "latitude", DataType.FLOAT, Arrays.asList(dimLat));
        varLat.addAttribute(new Attribute("units", "degrees_east"));
        varLat.addAttribute(new Attribute("standard_name", "latitude"));
        varLat.addAttribute(new Attribute("long_name", "latitude of craft position"));
        Variable varLon = f.addVariable(null, "longitude", DataType.FLOAT, Arrays.asList(dimLon));
        varLon.addAttribute(new Attribute("units", "degrees_north"));
        varLon.addAttribute(new Attribute("standard_name", "longitude"));
        varLon.addAttribute(new Attribute("long_name", "longitude of craft position"));
        Variable varTime = f.addVariable(null, "time", DataType.DOUBLE, Arrays.asList(dimTime));
        varTime.addAttribute(new Attribute("units", "days since 1970-01-01 00:00:00 UTC"));
        Variable varSource = f.addVariable(null, "source", DataType.SHORT, Arrays.asList(dimSource));
        varSource.addAttribute(new Attribute("encoding", "0=not present, 1=present, others TBA"));
        Variable varLatency = f.addVariable(null, "latency", DataType.INT, Arrays.asList(dimLatency));
        varLatency.addAttribute(new Attribute("units", "s"));
        varLatency.addAttribute(new Attribute("encoding", "-1=not present"));
        Variable varNavStatus = f.addVariable(null, "navigational_status", DataType.BYTE, Arrays.asList(dimNavStatus));
        varNavStatus.addAttribute(new Attribute("encoding", "127=not present"));
        Variable varRateOfTurn = f.addVariable(null, "rate_of_turn", DataType.BYTE, Arrays.asList(dimRateOfTurn));
        varRateOfTurn.addAttribute(new Attribute("encoding", "-128=not present, others TBA"));
        Variable varSpeedOverGround = f.addVariable(null, "speed_over_ground", DataType.SHORT, Arrays.asList(dimSpeedOverGround));
        varSpeedOverGround.addAttribute(new Attribute("units", "1/10 knot"));
        varSpeedOverGround.addAttribute(new Attribute("encoding", "1023=not present"));
        Variable varCourseOverGround = f.addVariable(null, "course_over_ground", DataType.SHORT, Arrays.asList(dimCourseOverGround));
        varCourseOverGround.addAttribute(new Attribute("units", "1/10 degree"));
        varCourseOverGround.addAttribute(new Attribute("encoding", "3600=not present"));
        Variable varHeading = f.addVariable(null, "heading", DataType.SHORT, Arrays.asList(dimHeading));
        varHeading.addAttribute(new Attribute("units", "degrees"));
        varHeading.addAttribute(new Attribute("encoding", "360=not present"));
        Variable varAisClass = f.addVariable(null, "ais_class", DataType.BYTE, Arrays.asList(dimAisClass));
        varAisClass.addAttribute(new Attribute("encoding", "0=A,1=B"));
        // create the file
        f.create();
        int[] shape = new int[] { fixes.size() };
        Array dataLat = Array.factory(DataType.FLOAT, shape);
        Array dataLon = Array.factory(DataType.FLOAT, shape);
        Array dataTime = Array.factory(DataType.DOUBLE, shape);
        Array dataSource = Array.factory(DataType.SHORT, shape);
        Array dataLatency = Array.factory(DataType.INT, shape);
        Array dataNavStatus = Array.factory(DataType.BYTE, shape);
        Array dataRateOfTurn = Array.factory(DataType.BYTE, shape);
        Array dataSpeedOverGround = Array.factory(DataType.SHORT, shape);
        Array dataCourseOverGround = Array.factory(DataType.SHORT, shape);
        Array dataHeading = Array.factory(DataType.SHORT, shape);
        Array dataAisClass = Array.factory(DataType.BYTE, shape);
        for (int i = 0; i < fixes.size(); i++) {
            Fix fix = fixes.get(i).fix();
            // latitude
            dataLat.setFloat(i, fix.lat());
            // longitude
            dataLon.setFloat(i, fix.lon());
            // time
            double days = (double) fix.time() / TimeUnit.DAYS.toMillis(1);
            dataTime.setDouble(i, days);
            // source
            dataSource.setShort(i, fix.source().orElse(BinaryFixes.SOURCE_ABSENT));
            // latency
            dataLatency.setInt(i, fix.latencySeconds().orElse(BinaryFixes.LATENCY_ABSENT));
            // navigational status
            int navStatus;
            if (fix.navigationalStatus().isPresent())
                navStatus = fix.navigationalStatus().get().ordinal();
            else
                navStatus = BinaryFixes.NAV_STATUS_ABSENT;
            dataNavStatus.setByte(i, (byte) navStatus);
            // rate of turn
            dataRateOfTurn.setByte(i, BinaryFixes.RATE_OF_TURN_ABSENT);
            // SOG
            final short sog;
            if (fix.speedOverGroundKnots().isPresent())
                sog = (short) Math.round(fix.speedOverGroundKnots().get() * 10);
            else
                sog = 1023;
            dataSpeedOverGround.setShort(i, sog);
            // COG
            final short cog;
            if (fix.courseOverGroundDegrees().isPresent())
                cog = (short) Math.round(fix.courseOverGroundDegrees().get() * 10);
            else
                cog = 3600;
            dataCourseOverGround.setShort(i, cog);
            // heading
            final short heading;
            if (fix.headingDegrees().isPresent())
                heading = (short) Math.floor(fix.headingDegrees().get() + 0.01f);
            else
                heading = (short) 360;
            dataHeading.setShort(i, heading);
            // ais class
            byte aisClass;
            if (fix.aisClass() == AisClass.A)
                aisClass = (byte) 0;
            else
                aisClass = (byte) 1;
            dataAisClass.setByte(i, aisClass);
        }
        f.write(varLat, dataLat);
        f.write(varLon, dataLon);
        f.write(varTime, dataTime);
        f.write(varSource, dataSource);
        f.write(varLatency, dataLatency);
        f.write(varNavStatus, dataNavStatus);
        f.write(varRateOfTurn, dataRateOfTurn);
        f.write(varSpeedOverGround, dataSpeedOverGround);
        f.write(varCourseOverGround, dataCourseOverGround);
        f.write(varHeading, dataHeading);
        f.write(varAisClass, dataAisClass);
        f.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InvalidRangeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Variable(ucar.nc2.Variable) Attribute(ucar.nc2.Attribute) InvalidRangeException(ucar.ma2.InvalidRangeException) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFileWriter(ucar.nc2.NetcdfFileWriter) Array(ucar.ma2.Array)

Example 58 with Array

use of org.openmuc.openiec61850.Array in project open-smart-grid-platform 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(array);
}
Also used : Array(com.beanit.openiec61850.Array) NodeWriteException(org.opensmartgridplatform.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaFloat32(com.beanit.openiec61850.BdaFloat32)

Example 59 with Array

use of org.openmuc.openiec61850.Array in project open-smart-grid-platform 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(array);
}
Also used : Array(com.beanit.openiec61850.Array) NodeWriteException(org.opensmartgridplatform.adapter.protocol.iec61850.exceptions.NodeWriteException) BdaTimestamp(com.beanit.openiec61850.BdaTimestamp)

Example 60 with Array

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