Search in sources :

Example 36 with Array

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

the class NetCDFExtractor method parseDataBlockTS.

private static ExtractionResult parseDataBlockTS(final NCFieldAnalyze analyze, final String procedureID, final List<String> acceptedSensorID, Set<org.opengis.observation.Phenomenon> phenomenons) throws NetCDFParsingException {
    final ExtractionResult results = new ExtractionResult();
    if (analyze.mainField == null) {
        LOGGER.warning("No main field found");
        return results;
    }
    LOGGER.info("parsing netCDF TS");
    try {
        final List<String> separators = parseSeparatorValues(analyze);
        final boolean single = separators.isEmpty();
        Array latArray = null;
        Array lonArray = null;
        if (analyze.hasSpatial()) {
            latArray = analyze.getArrayFromField(analyze.latField);
            lonArray = analyze.getArrayFromField(analyze.lonField);
        }
        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 boolean constantT = analyze.mainField.dimension == 1;
        final boolean timeFirst = analyze.mainField.mainVariableFirst;
        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);
        if (single) {
            if (acceptedSensorID == null || acceptedSensorID.contains(procedureID)) {
                final Process proc = (Process) OMUtils.buildProcess(procedureID);
                final ProcedureTree compo = new ProcedureTree(proc.getHref(), proc.getName(), proc.getDescription(), "Component", "timeseries");
                results.procedures.add(compo);
                final MeasureStringBuilder sb = new MeasureStringBuilder();
                final int count = timeVar.getDimension(0).getLength();
                final GeoSpatialBound gb = new GeoSpatialBound();
                final String identifier = UUID.randomUUID().toString();
                // read geometry (assume point)
                SamplingFeature sp = null;
                if (analyze.hasSpatial()) {
                    final double latitude = getDoubleValue(latArray, analyze.latField.fillValue);
                    final double longitude = Longitude.normalize(getDoubleValue(lonArray, analyze.lonField.fillValue));
                    if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
                        sp = OMUtils.buildSamplingPoint(identifier, latitude, longitude);
                        results.addFeatureOfInterest(sp);
                        gb.addXYCoordinate(longitude, latitude);
                        gb.addGeometry((AbstractGeometry) sp.getGeometry());
                    }
                }
                // iterating over time
                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, 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);
                compo.spatialBound.merge(gb);
            }
        } else {
            final Process proc = (Process) OMUtils.buildProcess(procedureID);
            final ProcedureTree system = new ProcedureTree(proc.getHref(), proc.getName(), proc.getDescription(), "System", "timeseries");
            results.procedures.add(system);
            for (int j = 0; j < separators.size(); j++) {
                final String identifier = separators.get(j);
                final MeasureStringBuilder sb = new MeasureStringBuilder();
                final int count = getGoodTimeDimension(timeVar, analyze.dimensionSeparator).getLength();
                final GeoSpatialBound gb = new GeoSpatialBound();
                final String currentProcID = procedureID + '-' + identifier;
                final Process currentProc = (Process) OMUtils.buildProcess(currentProcID);
                final ProcedureTree compo = new ProcedureTree(currentProc.getHref(), currentProc.getName(), currentProc.getDescription(), "Component", "timeseries");
                if (acceptedSensorID == null || acceptedSensorID.contains(currentProcID)) {
                    // read geometry (assume point)
                    SamplingFeature sp = null;
                    if (analyze.hasSpatial()) {
                        final double latitude = getDoubleValue(latArray, j, analyze.latField.fillValue);
                        final double longitude = Longitude.normalize(getDoubleValue(lonArray, j, analyze.lonField.fillValue));
                        if (!Double.isNaN(latitude) && !Double.isNaN(longitude)) {
                            sp = OMUtils.buildSamplingPoint(identifier, latitude, longitude);
                            results.addFeatureOfInterest(sp);
                            gb.addXYCoordinate(longitude, latitude);
                            gb.addGeometry((AbstractGeometry) sp.getGeometry());
                        }
                    }
                    for (int i = 0; i < count; i++) {
                        final long millis = getTimeValue(timeUnits, timeFirst, constantT, timeArray, i, j);
                        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 boolean mainFirst = field.mainVariableFirst;
                            final Double value = getDoubleValue(mainFirst, phenArray, i, j, field.fillValue);
                            sb.appendValue(value);
                        }
                        // remove the last token separator
                        sb.closeBlock();
                    }
                    compo.spatialBound.merge(gb);
                    system.children.add(compo);
                    final String obsid = UUID.randomUUID().toString();
                    results.observations.add(// id
                    OMUtils.buildObservation(// id
                    obsid, // foi
                    sp, // phenomenon
                    phenomenon, // procedure
                    currentProc, // result
                    count, // result
                    datarecord, // result
                    sb, // time
                    gb.getTimeObject("2.0.0")));
                    results.spatialBound.merge(gb);
                }
            }
        }
    } catch (IOException | IllegalArgumentException ex) {
        throw new NetCDFParsingException("error while parsing netcdf timeserie", 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)

Example 37 with Array

use of org.openmuc.openiec61850.Array in project seadas-toolbox by seadas.

the class BathymetryOp method computeTile.

@Override
public void computeTile(Band targetBand, Tile targetTile, ProgressMonitor pm) throws OperatorException {
    final String targetBandName = targetBand.getName();
    final Rectangle rectangle = targetTile.getRectangle();
    // not sure if this is really needed but just in case
    if (!targetBandName.equals(BATHYMETRY_BAND_NAME) && !targetBandName.equals(TOPOGRAPHY_BAND_NAME) && !targetBandName.equals(ELEVATION_BAND_NAME)) {
        for (int y = rectangle.y; y < rectangle.y + rectangle.height; y++) {
            for (int x = rectangle.x; x < rectangle.x + rectangle.width; x++) {
                int dataValue = 0;
                targetTile.setSample(x, y, dataValue);
            }
        }
        return;
    }
    try {
        final GeoCoding geoCoding = sourceProduct.getSceneGeoCoding();
        final PixelPos pixelPos = new PixelPos();
        final GeoPos geoPos = new GeoPos();
        // loop through tile, adding each pixel geolocation to it's appropriate earthBox via motherEarthBox
        // at this point the earthBoxes will adjust their mins and maxes based on the given lats and lons.
        MotherEarthBox motherEarthBox = new MotherEarthBox();
        for (int y = rectangle.y; y < rectangle.y + rectangle.height; y++) {
            for (int x = rectangle.x; x < rectangle.x + rectangle.width; x++) {
                pixelPos.setLocation(x, y);
                geoCoding.getGeoPos(pixelPos, geoPos);
                motherEarthBox.add(geoPos);
            }
        }
        for (EarthBox earthBox : motherEarthBox.getFilledEarthBoxes()) {
            // add dimensions to the earthBox
            int minLatIndex = bathymetryReader.getLatIndex(earthBox.getMinLat());
            int maxLatIndex = bathymetryReader.getLatIndex(earthBox.getMaxLat());
            int minLonIndex = bathymetryReader.getLonIndex(earthBox.getMinLon());
            int maxLonIndex = bathymetryReader.getLonIndex(earthBox.getMaxLon());
            if (minLatIndex > 0) {
                minLatIndex--;
            }
            if (maxLatIndex < bathymetryReader.dimensionLat - 1) {
                maxLatIndex++;
            }
            if (minLonIndex > 0) {
                minLonIndex--;
            }
            if (maxLonIndex < bathymetryReader.dimensionLon - 1) {
                maxLonIndex++;
            }
            // determine length of each dimension for the chunk array to be pulled out of the netcdf source
            int latDimensionLength = maxLatIndex - minLatIndex + 1;
            int lonDimensionLength = maxLonIndex - minLonIndex + 1;
            // get the bathymetry height array from the netcdf source
            int[] origin = new int[] { minLatIndex, minLonIndex };
            int[] shape = new int[] { latDimensionLength, lonDimensionLength };
            // retrieve the bathymetry height array from netcdf
            Array heightArray = bathymetryReader.getHeightArray(origin, shape);
            Array waterSurfaceheightArray = bathymetryReader.getWaterSurfaceHeightArray(origin, shape);
            // convert heightArray from ucar.ma2.Array format to regular java array
            short[][] heights = (short[][]) heightArray.copyToNDJavaArray();
            short[][] waterSurfaceHeights = (short[][]) waterSurfaceheightArray.copyToNDJavaArray();
            // add the value array to the earthBox
            float minLat = bathymetryReader.getLat(minLatIndex);
            float maxLat = bathymetryReader.getLat(maxLatIndex);
            float minLon = bathymetryReader.getLon(minLonIndex);
            float maxLon = bathymetryReader.getLon(maxLonIndex);
            short missingValue = bathymetryReader.getMissingValue();
            earthBox.setValues(minLat, maxLat, minLon, maxLon, heights, waterSurfaceHeights, missingValue);
            earthBox.setGetValueAverage(true);
        }
        // loop through all the tile pixels, geolocate them, and get their bathymetry height from motherEarthBox.
        for (int y = rectangle.y; y < rectangle.y + rectangle.height; y++) {
            for (int x = rectangle.x; x < rectangle.x + rectangle.width; x++) {
                pixelPos.setLocation(x, y);
                geoCoding.getGeoPos(pixelPos, geoPos);
                int value;
                int waterSurfaceHeight;
                int height;
                if (geoPos.isValid()) {
                    height = motherEarthBox.getValue(geoPos);
                    waterSurfaceHeight = motherEarthBox.getWaterSurfaceValue(geoPos);
                    if (targetBandName.equals(ELEVATION_BAND_NAME)) {
                        value = height;
                    } else if (targetBandName.equals(TOPOGRAPHY_BAND_NAME)) {
                        int valueSurface = height - waterSurfaceHeight;
                        if (valueSurface > 0) {
                            value = motherEarthBox.getValue(geoPos);
                        } else {
                            value = bathymetryReader.getMissingValue();
                        }
                    } else if (targetBandName.equals(BATHYMETRY_BAND_NAME)) {
                        int valueSurface = height - waterSurfaceHeight;
                        if (valueSurface <= 0) {
                            value = valueSurface;
                        } else {
                            value = bathymetryReader.getMissingValue();
                        }
                        // convert  to positive if not NaN
                        if (value > -32000) {
                            value = -value;
                        }
                    } else {
                        value = bathymetryReader.getMissingValue();
                    }
                } else {
                    value = bathymetryReader.getMissingValue();
                }
                targetTile.setSample(x, y, value);
            }
        }
    } catch (Exception e) {
        throw new OperatorException("Error computing tile '" + targetTile.getRectangle().toString() + "'.", e);
    }
}
Also used : IOException(java.io.IOException) OperatorException(org.esa.snap.core.gpf.OperatorException) Array(ucar.ma2.Array) OperatorException(org.esa.snap.core.gpf.OperatorException)

Example 38 with Array

use of org.openmuc.openiec61850.Array in project SOS by 52North.

the class AbstractNetcdfEncoder method initHeightDephtArray.

private Array initHeightDephtArray(List<Dimension> zDims) {
    Array array = getArray(zDims);
    initArrayWithFillValue(array, getNetcdfHelper().getFillValue());
    return array;
}
Also used : Array(ucar.ma2.Array)

Example 39 with Array

use of org.openmuc.openiec61850.Array in project SOS by 52North.

the class AbstractNetcdfEncoder method getLatitudeArray.

protected Array getLatitudeArray(AbstractSensorDataset sensorDataset) throws EncodingException {
    if (sensorDataset instanceof StaticLocationDataset) {
        StaticLocationDataset locationDataset = (StaticLocationDataset) sensorDataset;
        if (locationDataset.getLat() != null) {
            Array array = getArray();
            initArrayWithFillValue(array, getNetcdfHelper().getFillValue());
            Index index = array.getIndex();
            index.set(0);
            array.setDouble(index, locationDataset.getLat());
        }
    } else {
        // TODO support varying lat
        throw new EncodingException("Varying lat are not yet supported.");
    }
    return null;
}
Also used : Array(ucar.ma2.Array) StaticLocationDataset(org.n52.sos.netcdf.data.dataset.StaticLocationDataset) EncodingException(org.n52.svalbard.encode.exception.EncodingException) Index(ucar.ma2.Index)

Example 40 with Array

use of org.openmuc.openiec61850.Array in project rosetta by Unidata.

the class TestTagUniversalFileFormatOneLocManyOb method testMatchup.

@Test
public void testMatchup() throws IOException {
    // for this date and ob:
    // "2005-04-16 00:00:00",6,21.50,"temperature","Celsius"
    // there is a lat/lon matchup
    CalendarDate findTimeCd = CalendarDate.parseISOformat("gregorian", "2005-04-16 00:00:00");
    long findTime = findTimeCd.toDate().getTime() / 1000;
    Variable time = ncd.findVariable("time");
    Array timeVals = time.read();
    int foundIndex = 0;
    boolean found = false;
    for (int i = 0; i < timeVals.getSize(); i++) {
        long checkTime = timeVals.getLong(i);
        if (checkTime == findTime) {
            found = true;
            foundIndex = i;
        }
    }
    assertTrue(found);
    // check that temperature value is as expected
    Variable temp = ncd.findVariable("temperature");
    Array tempVals = temp.read();
    assertEquals(21.50, tempVals.getFloat(foundIndex), 0.001);
}
Also used : Array(ucar.ma2.Array) CalendarDate(ucar.nc2.time.CalendarDate) Variable(ucar.nc2.Variable) Test(org.junit.Test)

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