Search in sources :

Example 26 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 27 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 28 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 29 with Array

use of org.openmuc.openiec61850.Array in project geotoolkit by Geomatys.

the class NCFieldAnalyze method getPhenomenonArrayMap.

public Map<String, Array> getPhenomenonArrayMap() throws IOException {
    final Map<String, Array> phenArrays = new HashMap<>();
    for (NCField field : phenfields) {
        final Array phenArray = getArrayFromField(field);
        phenArrays.put(field.name, phenArray);
    }
    return phenArrays;
}
Also used : Array(ucar.ma2.Array) HashMap(java.util.HashMap)

Example 30 with Array

use of org.openmuc.openiec61850.Array in project geotoolkit by Geomatys.

the class NetCDFExtractor method parseDataBlockGrid.

private static ExtractionResult parseDataBlockGrid(final NCFieldAnalyze analyze, final String procedureID, final List<String> acceptedSensorID, Set<org.opengis.observation.Phenomenon> phenomenons) throws NetCDFParsingException {
    final ExtractionResult results = new ExtractionResult();
    if (acceptedSensorID == null || acceptedSensorID.contains(procedureID)) {
        final Process proc = (Process) OMUtils.buildProcess(procedureID);
        final ProcedureTree compo = new ProcedureTree(proc.getHref(), proc.getName(), proc.getDescription(), "grid", "Component");
        results.procedures.add(compo);
        if (analyze.mainField == null) {
            LOGGER.warning("No main field found");
            return results;
        }
        LOGGER.info("parsing netCDF GRID");
        try {
            final Variable latVar = analyze.vars.get(analyze.latField.name);
            final Variable lonVar = analyze.vars.get(analyze.lonField.name);
            final Array latArray = analyze.file.readArrays(Arrays.asList(latVar)).get(0);
            final Array lonArray = analyze.file.readArrays(Arrays.asList(lonVar)).get(0);
            final Variable timeVar = analyze.vars.get(analyze.mainField.name);
            final String timeUnits = analyze.mainField.uom;
            final Array timeArray = analyze.file.readArrays(Arrays.asList(timeVar)).get(0);
            final Map<String, Array> phenArrays = analyze.getPhenomenonArrayMap();
            results.fields.addAll(phenArrays.keySet());
            final AbstractDataRecord datarecord = OMUtils.getDataRecordTimeSeries("2.0.0", analyze.phenfields);
            final Phenomenon phenomenon = OMUtils.getPhenomenon("2.0.0", analyze.phenfields, phenomenons);
            results.phenomenons.add(phenomenon);
            final int latSize = latVar.getDimension(0).getLength();
            for (int latIndex = 0; latIndex < latSize; latIndex++) {
                final int lonSize = lonVar.getDimension(0).getLength();
                for (int lonIndex = 0; lonIndex < lonSize; lonIndex++) {
                    final String identifier = UUID.randomUUID().toString();
                    final MeasureStringBuilder sb = new MeasureStringBuilder();
                    final int count = timeVar.getDimension(0).getLength();
                    final GeoSpatialBound gb = new GeoSpatialBound();
                    SamplingFeature sp = null;
                    final double latitude = getDoubleValue(latArray, latIndex, analyze.latField.fillValue);
                    final double longitude = Longitude.normalize(getDoubleValue(lonArray, lonIndex, analyze.lonField.fillValue));
                    if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
                        sp = OMUtils.buildSamplingPoint(identifier, latitude, longitude);
                        results.addFeatureOfInterest(sp);
                        gb.addXYCoordinate(longitude, latitude);
                    }
                    for (int i = 0; i < count; i++) {
                        final long millis = getTimeValue(timeUnits, timeArray, i);
                        if (millis == 0 || millis == LIMIT) {
                            continue;
                        }
                        gb.addDate(millis);
                        sb.appendDate(millis);
                        for (NCField field : analyze.phenfields) {
                            final Array phenArray = phenArrays.get(field.name);
                            final Double value = getDoubleValue(phenArray, i, latIndex, lonIndex, field.fillValue);
                            sb.appendValue(value);
                        }
                        sb.closeBlock();
                    }
                    results.observations.add(// id
                    OMUtils.buildObservation(// id
                    identifier, // foi
                    sp, // phenomenon
                    phenomenon, // procedure
                    proc, // result
                    count, // result
                    datarecord, // result
                    sb, // time
                    gb.getTimeObject("2.0.0")));
                    results.spatialBound.merge(gb);
                }
            }
            results.spatialBound.addGeometry(results.spatialBound.getPolyGonBounds("2.0.0"));
            compo.spatialBound.addGeometry(results.spatialBound.getPolyGonBounds("2.0.0"));
        } catch (IOException | IllegalArgumentException ex) {
            throw new NetCDFParsingException("error while parsing netcdf grid", ex);
        }
    }
    LOGGER.info("datablock parsed");
    return results;
}
Also used : Variable(ucar.nc2.Variable) MeasureStringBuilder(org.geotoolkit.sos.MeasureStringBuilder) Process(org.geotoolkit.observation.xml.Process) GeoSpatialBound(org.geotoolkit.observation.model.GeoSpatialBound) SamplingFeature(org.geotoolkit.sampling.xml.SamplingFeature) IOException(java.io.IOException) Array(ucar.ma2.Array) Phenomenon(org.geotoolkit.swe.xml.Phenomenon) ProcedureTree(org.geotoolkit.observation.model.ExtractionResult.ProcedureTree) ExtractionResult(org.geotoolkit.observation.model.ExtractionResult) AbstractDataRecord(org.geotoolkit.swe.xml.AbstractDataRecord)

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