Search in sources :

Example 1 with Output

use of org.geotoolkit.processing.science.drift.Output in project geotoolkit by Geomatys.

the class Predictor method compute.

private List<Output> compute(final Instant startTime, final Instant endTime, final PredictionContext ctx, final MeteoDataset.TimeSet meteo) throws ProcessException {
    final double totalSeconds = startTime.until(endTime, ChronoUnit.SECONDS);
    boolean newDay = false;
    Instant nextDay = startTime.plus(1, ChronoUnit.DAYS);
    Instant stepTime = startTime;
    // TODO : We should think about a better way of managing output grid, because here we waste a lot of space.
    final double[] globalProba = new double[ctx.grid.width * ctx.grid.height];
    final double[] dayProba = new double[globalProba.length];
    final List<Output> outputs = new ArrayList<>();
    // When computing point drift, we'll add a point for each weight available. As we don't want this amount to grow
    // past a configured maximum, we have to purge available points before each computing pass.
    final int maxAllowedPoints = ctx.points.maxPts / (ctx.weights.length + 1);
    do {
        final long timePassed = startTime.until(stepTime, ChronoUnit.SECONDS);
        fireProgressing("Drifting: " + stepTime, (float) (timePassed / totalSeconds) * 100f, false);
        MeteoDataset.Snapshot snapshot = meteo.setTime(stepTime).map(calibration -> calibration.setHorizontalComponent(ctx.grid.model.getEnvelope())).orElse(null);
        if (snapshot == null)
            break;
        ctx.points.removeLeastProbable(maxAllowedPoints);
        final double[] stepProba = advance(ctx, snapshot);
        if (stepProba == null)
            break;
        // TODO : add abstraction here : we could reduce loop by iterating only over a rectangle where probabilities
        // have really been updated.
        IntStream.range(0, dayProba.length).parallel().forEach(i -> {
            dayProba[i] += stepProba[i];
            globalProba[i] += stepProba[i];
        });
        newDay = stepTime.isAfter(nextDay);
        if (newDay) {
            nextDay = nextDay.plus(1, ChronoUnit.DAYS);
            outputs.add(new Output(dayProba, ctx.grid.width, ctx.grid.height));
            Arrays.fill(dayProba, 0);
        }
    } while ((stepTime = ctx.step(stepTime)).isBefore(endTime));
    if (stepTime.equals(startTime)) {
        throw new ProcessException("No data available for time: " + stepTime, this);
    }
    if (!newDay) {
        outputs.add(new Output(dayProba, ctx.grid.width, ctx.grid.height));
    }
    outputs.add(new Output(globalProba, ctx.grid.width, ctx.grid.height));
    outputParameters.getOrCreate(ACTUAL_END_TIMESTAMP).setValue(stepTime.toEpochMilli());
    return outputs;
}
Also used : IntStream(java.util.stream.IntStream) Output(org.geotoolkit.processing.science.drift.Output) Arrays(java.util.Arrays) GridGeometry(org.apache.sis.coverage.grid.GridGeometry) CRS(org.apache.sis.referencing.CRS) PixelInCell(org.opengis.referencing.datum.PixelInCell) AffineTransform2D(org.apache.sis.internal.referencing.j2d.AffineTransform2D) HashMap(java.util.HashMap) NoninvertibleTransformException(org.opengis.referencing.operation.NoninvertibleTransformException) Supplier(java.util.function.Supplier) AbstractProcess(org.geotoolkit.processing.AbstractProcess) ArrayList(java.util.ArrayList) SingleCRS(org.opengis.referencing.crs.SingleCRS) Duration(java.time.Duration) Map(java.util.Map) AxisDirections(org.apache.sis.internal.referencing.AxisDirections) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) Path(java.nio.file.Path) GridCoverageResource(org.apache.sis.storage.GridCoverageResource) DirectPosition(org.opengis.geometry.DirectPosition) TransformException(org.opengis.referencing.operation.TransformException) Weight(org.geotoolkit.processing.science.drift.Weight) PrintStream(java.io.PrintStream) Parameters(org.apache.sis.parameter.Parameters) Files(java.nio.file.Files) PointReference(org.geotoolkit.processing.science.drift.v2.PointBucket.PointReference) ProcessDescriptor(org.geotoolkit.process.ProcessDescriptor) IOException(java.io.IOException) Vector2d(org.geotoolkit.geometry.math.Vector2d) AffineTransform(java.awt.geom.AffineTransform) Instant(java.time.Instant) List(java.util.List) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) InvalidRangeException(ucar.ma2.InvalidRangeException) ChronoUnit(java.time.temporal.ChronoUnit) ParameterValueGroup(org.opengis.parameter.ParameterValueGroup) MathTransform(org.opengis.referencing.operation.MathTransform) Optional(java.util.Optional) GridExtent(org.apache.sis.coverage.grid.GridExtent) Utilities.setTime(org.geotoolkit.processing.science.drift.v2.Utilities.setTime) PredictorDescriptor(org.geotoolkit.processing.science.drift.v2.PredictorDescriptor) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem) FactoryException(org.opengis.util.FactoryException) Instant(java.time.Instant) ArrayList(java.util.ArrayList) ProcessException(org.geotoolkit.process.ProcessException) Output(org.geotoolkit.processing.science.drift.Output)

Example 2 with Output

use of org.geotoolkit.processing.science.drift.Output in project geotoolkit by Geomatys.

the class Predictor method execute.

@Override
protected void execute() throws ProcessException {
    // // TODO: move in prediction context
    final Instant startTime = Instant.ofEpochMilli(inputParameters.getMandatoryValue(START_TIMESTAMP));
    final Instant endTime = Instant.ofEpochMilli(inputParameters.getMandatoryValue(END_TIMESTAMP));
    final MeteoDataset meteo = initData();
    final Origin origin;
    try {
        DirectPosition tmpOrigin = inputParameters.getMandatoryValue(START_POINT);
        tmpOrigin = setTime(tmpOrigin, startTime);
        origin = new Origin(tmpOrigin);
    } catch (FactoryException | TransformException ex) {
        throw new ProcessException("Cannot align start position with meteo coordinate system", this, ex);
    }
    final MeteoDataset.TimeSet initedMeteo = init(meteo, origin.getSource());
    final PredictionContext ctx = initContext(origin.getOrigin2d());
    // // END TODO
    /* Note: we should create it at the end, BUT: If this fails, we cannot store processing results, so it's useless
         * to start any heavy computing before being sure we will be able to store them. Doing tmp file creation
         * beforehand allows to ensure we can write on disk.
         */
    final Path outputFile;
    try {
        outputFile = Files.createTempFile("drift", ".nc");
    } catch (IOException ex) {
        throw new ProcessException("Cannot create a temporary file for result storage", this, ex);
    }
    try {
        // TODO: stream all this sh*t
        List<Output> outputs = compute(startTime, endTime, ctx, initedMeteo);
        write(outputs, ctx.grid.model, startTime, outputFile);
    } catch (Exception ex) {
        try {
            Files.delete(outputFile);
        } catch (Exception bis) {
            ex.addSuppressed(bis);
        }
        if (ex instanceof ProcessException) {
            throw (ProcessException) ex;
        }
        throw new ProcessException("Error while computing", this, ex);
    }
    outputParameters.getOrCreate(OUTPUT_DATA).setValue(outputFile);
}
Also used : Path(java.nio.file.Path) DirectPosition(org.opengis.geometry.DirectPosition) FactoryException(org.opengis.util.FactoryException) Instant(java.time.Instant) NoninvertibleTransformException(org.opengis.referencing.operation.NoninvertibleTransformException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) NoninvertibleTransformException(org.opengis.referencing.operation.NoninvertibleTransformException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) TransformException(org.opengis.referencing.operation.TransformException) IOException(java.io.IOException) InvalidRangeException(ucar.ma2.InvalidRangeException) FactoryException(org.opengis.util.FactoryException) ProcessException(org.geotoolkit.process.ProcessException) Output(org.geotoolkit.processing.science.drift.Output)

Aggregations

IOException (java.io.IOException)2 Path (java.nio.file.Path)2 Instant (java.time.Instant)2 DataStoreException (org.apache.sis.storage.DataStoreException)2 ProcessException (org.geotoolkit.process.ProcessException)2 Output (org.geotoolkit.processing.science.drift.Output)2 DirectPosition (org.opengis.geometry.DirectPosition)2 AffineTransform (java.awt.geom.AffineTransform)1 PrintStream (java.io.PrintStream)1 Files (java.nio.file.Files)1 Duration (java.time.Duration)1 ChronoUnit (java.time.temporal.ChronoUnit)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Supplier (java.util.function.Supplier)1 IntStream (java.util.stream.IntStream)1