Search in sources :

Example 16 with Array

use of com.beanit.openiec61850.Array 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)

Example 17 with Array

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

the class NodeContainer method getDateArray.

public Date[] getDateArray(final SubDataAttribute child) {
    final Array array = (Array) this.parent.getChild(child.getDescription());
    final int size = array.size();
    final Date[] result = new Date[size];
    for (int i = 0; i < size; i++) {
        result[i] = ((BdaTimestamp) array.getChild(i)).getDate();
    }
    return result;
}
Also used : Array(com.beanit.openiec61850.Array) Date(java.util.Date)

Example 18 with Array

use of com.beanit.openiec61850.Array in project risky by amsa-code.

the class DistanceTravelledCalculator method saveCalculationResultAsNetcdf.

public static void saveCalculationResultAsNetcdf(Options options, CalculationResult calculationResult, String filename) {
    List<CellValue> list = calculationResult.getCells().toList().toBlocking().single();
    int maxLonIndex = list.stream().map(cell -> options.getGrid().cellAt(cell.getCentreLat(), cell.getCentreLon())).filter(// 
    x -> x.isPresent()).map(x -> x.get().getLonIndex()).max(// 
    Comparator.<Long>naturalOrder()).get().intValue();
    int maxLatIndex = list.stream().map(cell -> options.getGrid().cellAt(cell.getCentreLat(), cell.getCentreLon())).filter(x -> x.isPresent()).map(x -> x.get().getLatIndex()).max(Comparator.<Long>naturalOrder()).get().intValue();
    File file = new File(filename);
    // Create the file.
    NetcdfFileWriter f = null;
    try {
        // Create new netcdf-3 file with the given filename
        f = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, file.getPath());
        // In addition to the latitude and longitude dimensions, we will
        // also create latitude and longitude netCDF variables which will
        // hold the actual latitudes and longitudes. Since they hold data
        // about the coordinate system, the netCDF term for these is:
        // "coordinate variables."
        Dimension dimLat = f.addDimension(null, "latitude", maxLatIndex + 1);
        Dimension dimLon = f.addDimension(null, "longitude", maxLonIndex + 1);
        List<Dimension> dims = new ArrayList<Dimension>();
        dims.add(dimLat);
        dims.add(dimLon);
        // coordinate variables
        Variable vLat = f.addVariable(null, "latitude", DataType.DOUBLE, "latitude");
        Variable vLon = f.addVariable(null, "longitude", DataType.DOUBLE, "longitude");
        // value variables
        Variable vDensity = f.addVariable(null, "traffic_density", DataType.DOUBLE, dims);
        // Define units attributes for coordinate vars. This attaches a
        // text attribute to each of the coordinate variables, containing
        // the units.
        vLon.addAttribute(new Attribute("units", "degrees_east"));
        vLat.addAttribute(new Attribute("units", "degrees_north"));
        // Define units attributes for variables.
        vDensity.addAttribute(new Attribute("units", "nm-1"));
        vDensity.addAttribute(new Attribute("long_name", ""));
        // Write the coordinate variable data. This will put the latitudes
        // and longitudes of our data grid into the netCDF file.
        f.create();
        {
            Array dataLat = Array.factory(DataType.DOUBLE, new int[] { dimLat.getLength() });
            Array dataLon = Array.factory(DataType.DOUBLE, new int[] { dimLon.getLength() });
            // set latitudes
            for (int i = 0; i <= maxLatIndex; i++) {
                dataLat.setDouble(i, options.getGrid().centreLat(i));
            }
            // set longitudes
            for (int i = 0; i <= maxLonIndex; i++) {
                dataLon.setDouble(i, options.getGrid().centreLon(i));
            }
            f.write(vLat, dataLat);
            f.write(vLon, dataLon);
        }
        // write the value variable data
        {
            int[] iDim = new int[] { dimLat.getLength(), dimLon.getLength() };
            Array dataDensity = ArrayDouble.D2.factory(DataType.DOUBLE, iDim);
            Index2D idx = new Index2D(iDim);
            for (CellValue point : list) {
                Optional<Cell> cell = options.getGrid().cellAt(point.getCentreLat(), point.getCentreLon());
                if (cell.isPresent()) {
                    idx.set((int) cell.get().getLatIndex(), (int) cell.get().getLonIndex());
                    dataDensity.setDouble(idx, point.getValue());
                }
            }
            f.write(vDensity, dataDensity);
        }
    } catch (IOException | InvalidRangeException e) {
        throw new RuntimeException(e);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }
}
Also used : SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) Logging(com.github.davidmoten.rx.slf4j.Logging) Preconditions(com.github.davidmoten.guavamini.Preconditions) HashMap(java.util.HashMap) Action1(rx.functions.Action1) Action2(rx.functions.Action2) ArrayList(java.util.ArrayList) Observable(rx.Observable) Logger(org.apache.log4j.Logger) GridTraversor(au.gov.amsa.geo.model.GridTraversor) Func0(rx.functions.Func0) Func1(rx.functions.Func1) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Bounds(au.gov.amsa.geo.model.Bounds) Map(java.util.Map) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) Variable(ucar.nc2.Variable) Cell(au.gov.amsa.geo.model.Cell) ArrayDouble(ucar.ma2.ArrayDouble) PrintWriter(java.io.PrintWriter) Index2D(ucar.ma2.Index2D) Subscriber(rx.Subscriber) HasPosition(au.gov.amsa.risky.format.HasPosition) DataType(ucar.ma2.DataType) IOException(java.io.IOException) Position(au.gov.amsa.util.navigation.Position) Observer(rx.Observer) Array(ucar.ma2.Array) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) CellValue(au.gov.amsa.geo.model.CellValue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) InvalidRangeException(ucar.ma2.InvalidRangeException) Attribute(ucar.nc2.Attribute) Util(au.gov.amsa.geo.model.Util) OnSubscribe(rx.Observable.OnSubscribe) Entry(java.util.Map.Entry) Optional(java.util.Optional) Dimension(ucar.nc2.Dimension) NetcdfFileWriter(ucar.nc2.NetcdfFileWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Options(au.gov.amsa.geo.model.Options) Variable(ucar.nc2.Variable) Optional(java.util.Optional) Attribute(ucar.nc2.Attribute) InvalidRangeException(ucar.ma2.InvalidRangeException) ArrayList(java.util.ArrayList) Index2D(ucar.ma2.Index2D) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFileWriter(ucar.nc2.NetcdfFileWriter) Array(ucar.ma2.Array) CellValue(au.gov.amsa.geo.model.CellValue) File(java.io.File)

Example 19 with Array

use of com.beanit.openiec61850.Array in project risky by amsa-code.

the class NetcdfFixesWriterTest method testNetcdfConverter.

@Test
public void testNetcdfConverter() throws IOException, InvalidRangeException {
    TestingUtil.writeTwoBinaryFixes("target/987654321.track", BinaryFixesFormat.WITHOUT_MMSI);
    int count = NetcdfFixesWriter.convertToNetcdf(new File("target"), new File("target/nc"), Pattern.compile("987654321.track")).count().toBlocking().single();
    assertEquals(1, count);
    // test output
    File ncFile = new File("target/nc/987654321.nc");
    assertTrue(ncFile.exists());
    assertTrue(ncFile.length() > 0);
    NetcdfFile nc = NetcdfFile.open(ncFile.getCanonicalPath());
    List<Attribute> attributes = nc.findGroup(null).getAttributes();
    System.out.println(attributes);
    assertFalse(attributes.isEmpty());
    System.out.println(nc.getDimensions());
    {
        Array array = nc.readSection("latitude");
        assertEquals(-10f, array.getFloat(0), PRECISION);
        assertEquals(-10.1f, array.getFloat(1), PRECISION);
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("longitude");
        assertEquals(135f, array.getFloat(0), PRECISION);
        assertEquals(135.2f, array.getFloat(1), PRECISION);
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("source");
        assertEquals((short) 1, array.getShort(0));
        assertEquals((short) 2, array.getShort(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("latency");
        assertEquals(12, array.getInt(0));
        assertEquals(13, array.getInt(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("navigational_status");
        assertEquals(NavigationalStatus.ENGAGED_IN_FISHING.ordinal(), array.getByte(0));
        assertEquals(NavigationalStatus.AT_ANCHOR.ordinal(), array.getByte(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("rate_of_turn");
        assertEquals(-128, array.getByte(0));
        assertEquals(-128, array.getByte(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("speed_over_ground");
        assertEquals(75, array.getInt(0));
        assertEquals(45, array.getInt(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("course_over_ground");
        assertEquals(450, array.getShort(0));
        assertEquals(200, array.getShort(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("heading");
        assertEquals(46, array.getShort(0));
        assertEquals(30, array.getShort(1));
        assertEquals(2, array.getSize());
    }
    {
        Array array = nc.readSection("ais_class");
        assertEquals(1, array.getInt(0));
        assertEquals(1, array.getInt(1));
        assertEquals(2, array.getSize());
    }
}
Also used : NetcdfFile(ucar.nc2.NetcdfFile) Array(ucar.ma2.Array) Attribute(ucar.nc2.Attribute) File(java.io.File) NetcdfFile(ucar.nc2.NetcdfFile) Test(org.junit.Test)

Example 20 with Array

use of com.beanit.openiec61850.Array in project risky by amsa-code.

the class NetCdfWriter method close.

@Override
public void close() {
    try {
        f.create();
        for (Var<?> var : map.keySet()) {
            List<?> list = map.get(var);
            int[] shape = new int[] { list.size() };
            Array data = Array.factory(DataType.getType(var.cls()), shape);
            for (int i = 0; i < list.size(); i++) {
                data.setObject(i, list.get(i));
            }
            f.write(var.variable(), data);
        }
        f.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    } catch (InvalidRangeException e) {
        throw new RuntimeException(e);
    }
}
Also used : Array(ucar.ma2.Array) InvalidRangeException(ucar.ma2.InvalidRangeException) IOException(java.io.IOException)

Aggregations

Array (ucar.ma2.Array)30 IOException (java.io.IOException)17 Attribute (ucar.nc2.Attribute)17 Variable (ucar.nc2.Variable)15 Index (ucar.ma2.Index)14 ArrayFloat (ucar.ma2.ArrayFloat)11 Dimension (ucar.nc2.Dimension)10 NetcdfFile (ucar.nc2.NetcdfFile)10 File (java.io.File)9 InvalidRangeException (ucar.ma2.InvalidRangeException)9 WritableRaster (java.awt.image.WritableRaster)8 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8 ArrayList (java.util.ArrayList)7 DataType (ucar.ma2.DataType)6 ArrayDouble (ucar.ma2.ArrayDouble)5 ArrayShort (ucar.ma2.ArrayShort)5 Array (com.beanit.openiec61850.Array)4 Array (org.openmuc.openiec61850.Array)4 ArrayInt (ucar.ma2.ArrayInt)4 Range (ucar.ma2.Range)4