Search in sources :

Example 21 with Array

use of org.openmuc.openiec61850.Array in project s1tbx by senbox-org.

the class NetCDFWriter method writeProductNodesImpl.

/**
 * Writes the in-memory representation of a data product. This method was called by <code>writeProductNodes(product,
 * output)</code> of the AbstractProductWriter.
 *
 * @throws IllegalArgumentException if <code>output</code> type is not one of the supported output sources.
 * @throws java.io.IOException      if an I/O error occurs
 */
@Override
protected void writeProductNodesImpl() throws IOException {
    outputFile = null;
    final File file;
    if (getOutput() instanceof String) {
        file = new File((String) getOutput());
    } else {
        file = (File) getOutput();
    }
    outputFile = FileUtils.ensureExtension(file, NetcdfConstants.NETCDF_FORMAT_FILE_EXTENSIONS[0]);
    deleteOutput();
    final Product product = getSourceProduct();
    netCDFWriteable = NetcdfFileWriter.createNew(outputFile.getAbsolutePath(), true);
    netCDFWriteable.addDimension(NetcdfConstants.LON_VAR_NAMES[0], product.getSceneRasterWidth());
    netCDFWriteable.addDimension(NetcdfConstants.LAT_VAR_NAMES[0], product.getSceneRasterHeight());
    final Group rootGroup = netCDFWriteable.getNetcdfFile().getRootGroup();
    final List<Dimension> latLonList = new ArrayList<>();
    latLonList.add(rootGroup.findDimension(NetcdfConstants.LAT_VAR_NAMES[0]));
    latLonList.add(rootGroup.findDimension(NetcdfConstants.LON_VAR_NAMES[0]));
    netCDFWriteable.addVariable(NetcdfConstants.LAT_VAR_NAMES[0], DataType.FLOAT, latLonList.subList(0, 0));
    netCDFWriteable.addVariableAttribute(NetcdfConstants.LAT_VAR_NAMES[0], "units", "degrees_north (+N/-S)");
    netCDFWriteable.addVariable(NetcdfConstants.LON_VAR_NAMES[0], DataType.FLOAT, latLonList.subList(1, 1));
    netCDFWriteable.addVariableAttribute(NetcdfConstants.LON_VAR_NAMES[0], "units", "degrees_east (+E/-W)");
    for (Band band : product.getBands()) {
        final String name = StringUtils.createValidName(band.getName(), new char[] { '_' }, '_');
        netCDFWriteable.addVariable(name, DataType.DOUBLE, latLonList);
        if (band.getDescription() != null)
            netCDFWriteable.addVariableAttribute(name, "description", band.getDescription());
        if (band.getUnit() != null)
            netCDFWriteable.addVariableAttribute(name, "unit", band.getUnit());
    }
    for (TiePointGrid tpg : product.getTiePointGrids()) {
        final String name = tpg.getName();
        netCDFWriteable.addDimension(name + 'x', tpg.getGridWidth());
        netCDFWriteable.addDimension(name + 'y', tpg.getGridHeight());
        final List<Dimension> nameXYList = new ArrayList<>();
        nameXYList.add(rootGroup.findDimension(name + 'y'));
        nameXYList.add(rootGroup.findDimension(name + 'x'));
        netCDFWriteable.addVariable(name, DataType.FLOAT, nameXYList);
        if (tpg.getDescription() != null)
            netCDFWriteable.addVariableAttribute(name, "description", tpg.getDescription());
        if (tpg.getUnit() != null)
            netCDFWriteable.addVariableAttribute(name, "unit", tpg.getUnit());
    }
    addMetadata(product);
    netCDFWriteable.create();
    final GeoCoding sourceGeoCoding = product.getSceneGeoCoding();
    String latGridName = "latitude";
    String lonGridName = "longitude";
    if (sourceGeoCoding instanceof TiePointGeoCoding) {
        final TiePointGeoCoding geoCoding = (TiePointGeoCoding) sourceGeoCoding;
        latGridName = geoCoding.getLatGrid().getName();
        lonGridName = geoCoding.getLonGrid().getName();
    }
    final float[] latData = getLatData(product, latGridName);
    final float[] lonData = getLonData(product, lonGridName);
    if (latData != null && lonData != null) {
        final Array latNcArray = Array.factory(DataType.FLOAT, new int[] { latData.length }, latData);
        final Array lonNcArray = Array.factory(DataType.FLOAT, new int[] { lonData.length }, lonData);
        try {
            netCDFWriteable.write(NetcdfConstants.LAT_VAR_NAMES[0], latNcArray);
            netCDFWriteable.write(NetcdfConstants.LON_VAR_NAMES[0], lonNcArray);
            for (TiePointGrid tpg : product.getTiePointGrids()) {
                final float[][] tiePointGridData = getTiePointGridData(tpg);
                final int[] tiePointShape = new int[tiePointGridData.length];
                for (int i = 0; i < tiePointGridData.length; ++i) {
                    tiePointShape[i] = tiePointGridData[i].length;
                }
                final Array tpgArray = Array.factory(DataType.FLOAT, tiePointShape, tiePointGridData);
                netCDFWriteable.write(tpg.getName(), tpgArray);
            }
        } catch (InvalidRangeException rangeE) {
            rangeE.printStackTrace();
            throw new RuntimeException(rangeE);
        }
    }
}
Also used : Group(ucar.nc2.Group) InvalidRangeException(ucar.ma2.InvalidRangeException) Product(org.esa.snap.core.datamodel.Product) VirtualBand(org.esa.snap.core.datamodel.VirtualBand) Band(org.esa.snap.core.datamodel.Band) Dimension(ucar.nc2.Dimension) Array(ucar.ma2.Array) TiePointGeoCoding(org.esa.snap.core.datamodel.TiePointGeoCoding) TiePointGrid(org.esa.snap.core.datamodel.TiePointGrid) File(java.io.File) GeoCoding(org.esa.snap.core.datamodel.GeoCoding) TiePointGeoCoding(org.esa.snap.core.datamodel.TiePointGeoCoding)

Example 22 with Array

use of org.openmuc.openiec61850.Array in project s1tbx by senbox-org.

the class NetCDFReader method readBandRasterDataImpl.

/**
 * {@inheritDoc}
 */
@Override
protected synchronized void readBandRasterDataImpl(int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int sourceStepX, int sourceStepY, Band destBand, int destOffsetX, int destOffsetY, int destWidth, int destHeight, ProductData destBuffer, ProgressMonitor pm) throws IOException {
    Guardian.assertTrue("sourceStepX == 1 && sourceStepY == 1", sourceStepX == 1 && sourceStepY == 1);
    Guardian.assertTrue("sourceWidth == destWidth", sourceWidth == destWidth);
    Guardian.assertTrue("sourceHeight == destHeight", sourceHeight == destHeight);
    final int sceneHeight = product.getSceneRasterHeight();
    final int y0 = yFlipped ? (sceneHeight - 1) - sourceOffsetY : sourceOffsetY;
    final Variable[] variables = variableMap.getAll();
    Variable variable = null;
    for (Variable var : variables) {
        if (destBand.getName().equalsIgnoreCase(var.getName()) || destBand.getName().equalsIgnoreCase(var.getShortName())) {
            variable = var;
            break;
        }
    }
    if (variable == null) {
        throw new IOException("Band " + destBand.getName() + " not found");
    }
    final int rank = variable.getRank();
    final int[] origin = new int[rank];
    final int[] shape = new int[rank];
    for (int i = 0; i < rank; i++) {
        shape[i] = 1;
        origin[i] = 0;
    }
    shape[rank - 2] = 1;
    shape[rank - 1] = destWidth;
    origin[rank - 1] = sourceOffsetX;
    pm.beginTask("Reading data from band " + destBand.getName(), destHeight);
    try {
        for (int y = 0; y < destHeight; y++) {
            origin[rank - 2] = yFlipped ? y0 - y : y0 + y;
            final Array array;
            synchronized (netcdfFile) {
                array = variable.read(origin, shape);
            }
            System.arraycopy(array.getStorage(), 0, destBuffer.getElems(), y * destWidth, destWidth);
            pm.worked(1);
            if (pm.isCanceled()) {
                throw new IOException("Process terminated by user.");
            /*I18N*/
            }
        }
    } catch (InvalidRangeException e) {
        final IOException ioException = new IOException(e.getMessage());
        ioException.initCause(e);
        throw ioException;
    } finally {
        pm.done();
    }
}
Also used : Array(ucar.ma2.Array) Variable(ucar.nc2.Variable) InvalidRangeException(ucar.ma2.InvalidRangeException) IOException(java.io.IOException)

Example 23 with Array

use of org.openmuc.openiec61850.Array in project s1tbx by senbox-org.

the class NetCDFUtils method addAttribute.

public static void addAttribute(Variable variable, MetadataElement valuesElem, int maxNumValuesRead) {
    final int productDataType = getProductDataType(variable);
    if (productDataType == -1) {
        return;
    }
    final Array values;
    try {
        long variableSize = variable.getSize();
        if (variableSize >= maxNumValuesRead && maxNumValuesRead >= 0) {
            values = variable.read(new int[] { 0 }, new int[] { maxNumValuesRead });
            valuesElem.setDescription("Showing " + maxNumValuesRead + " of " + variableSize + " values.");
        } else {
            values = variable.read();
        }
        String name = "data";
        if (variableSize == 1 || productDataType == ProductData.TYPE_ASCII) {
            name = variable.getShortName();
        }
        final ProductData pd = ReaderUtils.createProductData(productDataType, values);
        final MetadataAttribute attribute = new MetadataAttribute(name, pd, true);
        valuesElem.addAttribute(attribute);
    } catch (IOException | InvalidRangeException e) {
        Debug.trace(e);
    }
}
Also used : Array(ucar.ma2.Array) ProductData(org.esa.snap.core.datamodel.ProductData) InvalidRangeException(ucar.ma2.InvalidRangeException) MetadataAttribute(org.esa.snap.core.datamodel.MetadataAttribute) IOException(java.io.IOException)

Example 24 with Array

use of org.openmuc.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 25 with Array

use of org.openmuc.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)

Aggregations

Array (ucar.ma2.Array)62 IOException (java.io.IOException)34 Variable (ucar.nc2.Variable)31 Index (ucar.ma2.Index)20 InvalidRangeException (ucar.ma2.InvalidRangeException)19 Attribute (ucar.nc2.Attribute)19 ArrayList (java.util.ArrayList)13 Dimension (ucar.nc2.Dimension)13 File (java.io.File)12 ArrayFloat (ucar.ma2.ArrayFloat)12 NetcdfFile (ucar.nc2.NetcdfFile)12 WritableRaster (java.awt.image.WritableRaster)8 ProcedureTree (org.geotoolkit.observation.model.ExtractionResult.ProcedureTree)8 GeoSpatialBound (org.geotoolkit.observation.model.GeoSpatialBound)8 Process (org.geotoolkit.observation.xml.Process)8 NetcdfFileWriteable (ucar.nc2.NetcdfFileWriteable)8 ArrayDouble (ucar.ma2.ArrayDouble)7 DataType (ucar.ma2.DataType)7 HashMap (java.util.HashMap)5 ArrayInt (ucar.ma2.ArrayInt)5