Search in sources :

Example 1 with OperatorWriteBytes

use of au.gov.amsa.util.rx.OperatorWriteBytes in project risky by amsa-code.

the class DistanceTravelledMain method calculateTrafficDensity.

private static void calculateTrafficDensity(String directory, Options options, boolean gui, Map<Integer, Info> shipInfo, Func1<Info, Boolean> shipSelector, String name) {
    System.out.println("-----------------------------------------------------");
    System.out.println("------ " + name);
    System.out.println("-----------------------------------------------------");
    final Observable<File> files = Util.getFiles(directory, ".*\\.track").filter(file -> {
        String s = file.getName();
        String mmsiString = s.substring(0, s.indexOf(".track"));
        long mmsi = Long.parseLong(mmsiString);
        Info info = shipInfo.get(mmsi);
        return shipSelector.call(info);
    });
    CalculationResult result = DistanceTravelledCalculator.calculateTrafficDensity(options, files, 1, 1);
    if (gui) {
        DisplayPanel.displayGui(files, options, result);
    }
    String filename = result.getCells().lift(new OperatorCellValuesToBytes(options)).lift(new OperatorWriteBytes()).toBlocking().single();
    log.info("result saved to file " + filename);
    CalculationResult resultFromFile = new CalculationResult(BinaryCellValuesObservable.readValues(new File(filename)).skip(1).cast(CellValue.class), result.getMetrics());
    boolean produceImage = false;
    boolean produceDensitiesText = false;
    boolean produceDensitiesNetcdf = true;
    File outputDirectory = new File("/media/an/traffic-density/netcdf");
    if (produceImage) {
        // 8:5 is ok ratio
        saveAsPng(Renderer.createImage(options, 2, 12800, resultFromFile), new File(outputDirectory, name + "-map.png"));
    }
    if (produceDensitiesText) {
        DistanceTravelledCalculator.saveCalculationResultAsText(options, result, new File(outputDirectory, name + "-densities.txt").getAbsolutePath());
    }
    if (produceDensitiesNetcdf) {
        DistanceTravelledCalculator.saveCalculationResultAsNetcdf(options, result, new File(outputDirectory, name + "-densities.nc").getAbsolutePath());
    }
}
Also used : CalculationResult(au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult) OperatorCellValuesToBytes(au.gov.amsa.geo.OperatorCellValuesToBytes) CellValue(au.gov.amsa.geo.model.CellValue) Info(au.gov.amsa.navigation.ShipStaticData.Info) File(java.io.File) OperatorWriteBytes(au.gov.amsa.util.rx.OperatorWriteBytes)

Example 2 with OperatorWriteBytes

use of au.gov.amsa.util.rx.OperatorWriteBytes in project risky by amsa-code.

the class BinaryCellValuesObservableTest method testSavingCellValuesToBinaryFileAndReadingBackAgain.

@Test
public void testSavingCellValuesToBinaryFileAndReadingBackAgain() {
    Options options = Options.builder().cellSizeDegrees(1.2).build();
    CellValue cv = new CellValue(-20, 123, 0.01);
    String filename = Observable.just(cv).lift(new OperatorCellValuesToBytes(options)).lift(new OperatorWriteBytes()).toBlocking().first();
    assertNotNull(filename);
    File file = new File(filename);
    assertTrue(file.exists());
    System.out.println(filename);
    Observable<?> o = BinaryCellValuesObservable.readValues(file);
    List<?> list = o.toList().toBlocking().single();
    assertEquals(1.2, (Double) list.get(0), 0.00001);
    assertTrue(list.get(1) instanceof CellValue);
    CellValue v = (CellValue) list.get(1);
    assertEquals(-20, v.getCentreLat(), PRECISION);
    assertEquals(123, v.getCentreLon(), PRECISION);
    assertEquals(0.01, v.getValue(), PRECISION);
    assertEquals(2, list.size());
}
Also used : Options(au.gov.amsa.geo.model.Options) CellValue(au.gov.amsa.geo.model.CellValue) File(java.io.File) OperatorWriteBytes(au.gov.amsa.util.rx.OperatorWriteBytes) Test(org.junit.Test)

Aggregations

CellValue (au.gov.amsa.geo.model.CellValue)2 OperatorWriteBytes (au.gov.amsa.util.rx.OperatorWriteBytes)2 File (java.io.File)2 OperatorCellValuesToBytes (au.gov.amsa.geo.OperatorCellValuesToBytes)1 CalculationResult (au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult)1 Options (au.gov.amsa.geo.model.Options)1 Info (au.gov.amsa.navigation.ShipStaticData.Info)1 Test (org.junit.Test)1