Search in sources :

Example 1 with ProcessListenerAdapter

use of org.geotoolkit.processing.ProcessListenerAdapter in project geotoolkit by Geomatys.

the class PredictorTest method sampleDrift.

@Test
public void sampleDrift() throws Exception {
    final Parameters input = Parameters.castOrWrap(DESCRIPTOR.getInputDescriptor().createValue());
    input.getOrCreate(PredictorDescriptor.MAX_POINTS).setValue(200);
    addWeight(input, 0.5f, 0.5f, 0.5f);
    addWeight(input, 0.2f, 0.8f, 0.8f);
    input.getOrCreate(PredictorDescriptor.TARGET_RESOLUTION).setValue(1);
    input.getOrCreate(PredictorDescriptor.TARGET_WIDTH).setValue(256);
    input.getOrCreate(PredictorDescriptor.TARGET_HEIGHT).setValue(256);
    input.getOrCreate(PredictorDescriptor.TIMESTEP).setValue(1);
    input.getOrCreate(PredictorDescriptor.START_TIMESTAMP).setValue(0);
    final long expectedEndTime = 7000;
    input.getOrCreate(PredictorDescriptor.END_TIMESTAMP).setValue(expectedEndTime);
    input.getOrCreate(PredictorDescriptor.START_POINT).setValue(new DirectPosition2D(CommonCRS.defaultGeographic(), 0.1, 0.2));
    input.getOrCreate(PredictorDescriptor.WIND_RESOURCE).setValue(new MemoryGridResource(null, MOCK_UV_DATA));
    input.getOrCreate(PredictorDescriptor.CURRENT_RESOURCE).setValue(new MemoryGridResource(null, MOCK_UV_DATA));
    final Predictor predictor = new Predictor(DESCRIPTOR, input);
    predictor.addListener(new ProcessListenerAdapter() {

        @Override
        public void progressing(ProcessEvent event) {
            final Exception error = event.getException();
            if (error != null) {
                Utilities.LOGGER.log(Level.WARNING, event.getTask().toString(), error);
            } else {
                Utilities.LOGGER.log(Level.INFO, event.getTask().toString());
            }
        }
    });
    Utilities.LOGGER.info("Starting drift processing");
    final Parameters output = CompletableFuture.supplyAsync(() -> {
        try {
            return predictor.call();
        } catch (ProcessException ex) {
            throw new RuntimeException(ex);
        }
    }).thenApply(Parameters::castOrWrap).get(30, TimeUnit.SECONDS);
    final long outTime = output.getMandatoryValue(PredictorDescriptor.ACTUAL_END_TIMESTAMP);
    final Path netcdf = output.getMandatoryValue(PredictorDescriptor.OUTPUT_DATA);
    try {
        assertEquals("Expected time of ending", expectedEndTime, outTime);
        assertNotNull("Output file path", netcdf);
        assertTrue("Output file is not readable", Files.isRegularFile(netcdf));
        checkContent(netcdf);
    } finally {
        Files.delete(netcdf);
    }
}
Also used : Path(java.nio.file.Path) ProcessException(org.geotoolkit.process.ProcessException) Parameters(org.apache.sis.parameter.Parameters) ProcessEvent(org.geotoolkit.process.ProcessEvent) MemoryGridResource(org.apache.sis.internal.storage.MemoryGridResource) ProcessListenerAdapter(org.geotoolkit.processing.ProcessListenerAdapter) DirectPosition2D(org.apache.sis.geometry.DirectPosition2D) NoSuchIdentifierException(org.opengis.util.NoSuchIdentifierException) ProcessException(org.geotoolkit.process.ProcessException) DataStoreException(org.apache.sis.storage.DataStoreException) Test(org.junit.Test)

Aggregations

Path (java.nio.file.Path)1 DirectPosition2D (org.apache.sis.geometry.DirectPosition2D)1 MemoryGridResource (org.apache.sis.internal.storage.MemoryGridResource)1 Parameters (org.apache.sis.parameter.Parameters)1 DataStoreException (org.apache.sis.storage.DataStoreException)1 ProcessEvent (org.geotoolkit.process.ProcessEvent)1 ProcessException (org.geotoolkit.process.ProcessException)1 ProcessListenerAdapter (org.geotoolkit.processing.ProcessListenerAdapter)1 Test (org.junit.Test)1 NoSuchIdentifierException (org.opengis.util.NoSuchIdentifierException)1