Search in sources :

Example 1 with Options

use of au.gov.amsa.geo.model.Options in project risky by amsa-code.

the class DistanceTravelledCalculator method partition.

/**
 * Returns a sequence of {@link Options} that are same as the source apart
 * from the {@link Bounds} which are partitioned according to horizontal and
 * vertical parameters. For map-reduce purposes we need to be able to
 * partition the bounds of Options. Passing horizontal=1 and vertical=1 will
 * return one item only being a copy of the source {@link Options}.
 *
 * @param options
 * @param horizontal
 *            number of regions (with longitude)
 * @param vertical
 *            number of regions (with latitude)
 * @return
 */
public static Observable<Options> partition(final Options options, final int horizontal, final int vertical) {
    List<Options> list = new ArrayList<>();
    Bounds bounds = options.getBounds();
    double h = bounds.getWidthDegrees() / horizontal;
    double v = bounds.getHeightDegrees() / vertical;
    for (int i = 0; i < horizontal; i++) {
        for (int j = 0; j < vertical; j++) {
            double lat = bounds.getTopLeftLat() - j * v;
            double lon = bounds.getTopLeftLon() + i * h;
            Bounds b = new Bounds(lat, lon, lat - v, lon + h);
            list.add(options.buildFrom().bounds(b).filterBounds(b.expand(7, 7)).build());
        }
    }
    return Observable.from(list);
}
Also used : SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) Options(au.gov.amsa.geo.model.Options) Bounds(au.gov.amsa.geo.model.Bounds) ArrayList(java.util.ArrayList)

Example 2 with Options

use of au.gov.amsa.geo.model.Options in project risky by amsa-code.

the class DistanceTravelledCalculator method saveCalculationResultAsNetcdf.

public static void saveCalculationResultAsNetcdf(Options options, CalculationResult calculationResult, String filename) {
    List<CellValue> list = calculationResult.getCells().toList().toBlocking().single();
    int maxLonIndex = list.stream().map(cell -> options.getGrid().cellAt(cell.getCentreLat(), cell.getCentreLon())).filter(// 
    x -> x.isPresent()).map(x -> x.get().getLonIndex()).max(// 
    Comparator.<Long>naturalOrder()).get().intValue();
    int maxLatIndex = list.stream().map(cell -> options.getGrid().cellAt(cell.getCentreLat(), cell.getCentreLon())).filter(x -> x.isPresent()).map(x -> x.get().getLatIndex()).max(Comparator.<Long>naturalOrder()).get().intValue();
    File file = new File(filename);
    // Create the file.
    NetcdfFileWriter f = null;
    try {
        // Create new netcdf-3 file with the given filename
        f = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, file.getPath());
        // In addition to the latitude and longitude dimensions, we will
        // also create latitude and longitude netCDF variables which will
        // hold the actual latitudes and longitudes. Since they hold data
        // about the coordinate system, the netCDF term for these is:
        // "coordinate variables."
        Dimension dimLat = f.addDimension(null, "latitude", maxLatIndex + 1);
        Dimension dimLon = f.addDimension(null, "longitude", maxLonIndex + 1);
        List<Dimension> dims = new ArrayList<Dimension>();
        dims.add(dimLat);
        dims.add(dimLon);
        // coordinate variables
        Variable vLat = f.addVariable(null, "latitude", DataType.DOUBLE, "latitude");
        Variable vLon = f.addVariable(null, "longitude", DataType.DOUBLE, "longitude");
        // value variables
        Variable vDensity = f.addVariable(null, "traffic_density", DataType.DOUBLE, dims);
        // Define units attributes for coordinate vars. This attaches a
        // text attribute to each of the coordinate variables, containing
        // the units.
        vLon.addAttribute(new Attribute("units", "degrees_east"));
        vLat.addAttribute(new Attribute("units", "degrees_north"));
        // Define units attributes for variables.
        vDensity.addAttribute(new Attribute("units", "nm-1"));
        vDensity.addAttribute(new Attribute("long_name", ""));
        // Write the coordinate variable data. This will put the latitudes
        // and longitudes of our data grid into the netCDF file.
        f.create();
        {
            Array dataLat = Array.factory(DataType.DOUBLE, new int[] { dimLat.getLength() });
            Array dataLon = Array.factory(DataType.DOUBLE, new int[] { dimLon.getLength() });
            // set latitudes
            for (int i = 0; i <= maxLatIndex; i++) {
                dataLat.setDouble(i, options.getGrid().centreLat(i));
            }
            // set longitudes
            for (int i = 0; i <= maxLonIndex; i++) {
                dataLon.setDouble(i, options.getGrid().centreLon(i));
            }
            f.write(vLat, dataLat);
            f.write(vLon, dataLon);
        }
        // write the value variable data
        {
            int[] iDim = new int[] { dimLat.getLength(), dimLon.getLength() };
            Array dataDensity = ArrayDouble.D2.factory(DataType.DOUBLE, iDim);
            Index2D idx = new Index2D(iDim);
            for (CellValue point : list) {
                Optional<Cell> cell = options.getGrid().cellAt(point.getCentreLat(), point.getCentreLon());
                if (cell.isPresent()) {
                    idx.set((int) cell.get().getLatIndex(), (int) cell.get().getLonIndex());
                    dataDensity.setDouble(idx, point.getValue());
                }
            }
            f.write(vDensity, dataDensity);
        }
    } catch (IOException | InvalidRangeException e) {
        throw new RuntimeException(e);
    } finally {
        if (f != null) {
            try {
                f.close();
            } catch (IOException ioe) {
                throw new RuntimeException(ioe);
            }
        }
    }
}
Also used : SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) Logging(com.github.davidmoten.rx.slf4j.Logging) LoggerFactory(org.slf4j.LoggerFactory) Preconditions(com.github.davidmoten.guavamini.Preconditions) HashMap(java.util.HashMap) Action1(rx.functions.Action1) Action2(rx.functions.Action2) ArrayList(java.util.ArrayList) Observable(rx.Observable) GridTraversor(au.gov.amsa.geo.model.GridTraversor) Func0(rx.functions.Func0) Func1(rx.functions.Func1) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Bounds(au.gov.amsa.geo.model.Bounds) Map(java.util.Map) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) Variable(ucar.nc2.Variable) Cell(au.gov.amsa.geo.model.Cell) ArrayDouble(ucar.ma2.ArrayDouble) PrintWriter(java.io.PrintWriter) Index2D(ucar.ma2.Index2D) Logger(org.slf4j.Logger) Subscriber(rx.Subscriber) HasPosition(au.gov.amsa.risky.format.HasPosition) DataType(ucar.ma2.DataType) IOException(java.io.IOException) Position(au.gov.amsa.util.navigation.Position) Observer(rx.Observer) Array(ucar.ma2.Array) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) CellValue(au.gov.amsa.geo.model.CellValue) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) InvalidRangeException(ucar.ma2.InvalidRangeException) Attribute(ucar.nc2.Attribute) Util(au.gov.amsa.geo.model.Util) OnSubscribe(rx.Observable.OnSubscribe) Entry(java.util.Map.Entry) Optional(java.util.Optional) Dimension(ucar.nc2.Dimension) NetcdfFileWriter(ucar.nc2.NetcdfFileWriter) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Comparator(java.util.Comparator) Options(au.gov.amsa.geo.model.Options) Variable(ucar.nc2.Variable) Optional(java.util.Optional) Attribute(ucar.nc2.Attribute) InvalidRangeException(ucar.ma2.InvalidRangeException) ArrayList(java.util.ArrayList) Index2D(ucar.ma2.Index2D) Dimension(ucar.nc2.Dimension) IOException(java.io.IOException) NetcdfFileWriter(ucar.nc2.NetcdfFileWriter) Array(ucar.ma2.Array) CellValue(au.gov.amsa.geo.model.CellValue) File(java.io.File)

Example 3 with Options

use of au.gov.amsa.geo.model.Options in project risky by amsa-code.

the class DistanceTravelledMain method main.

public static void main(String[] args) throws InterruptedException {
    log.info("starting");
    final double cellSizeDegrees;
    if (args.length > 1)
        cellSizeDegrees = Double.parseDouble(args[1]);
    else
        cellSizeDegrees = 0.02;
    final Options options = createOptions(cellSizeDegrees);
    for (int i = 0; i <= 10; i++) System.out.println(options.getGrid().centreLon(i));
    for (int i = 2014; i <= 2016; i++) {
        String directory = "/media/an/binary-fixes-5-minute/" + i;
        run(directory, options, false, i + "");
    }
}
Also used : SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) Options(au.gov.amsa.geo.model.Options)

Example 4 with Options

use of au.gov.amsa.geo.model.Options in project risky by amsa-code.

the class DistanceTravelledMovieMaker method saveImageWithTimeRange.

private static void saveImageWithTimeRange(Options options, final Observable<File> files, long startTime, long finishTime, String filename) {
    Options op = options.buildFrom().startTime(of(startTime)).finishTime(of(finishTime)).build();
    CalculationResult result = calculateTrafficDensity(op, files);
    saveAsPng(Renderer.createImage(op, 2, 1600, result), new File(filename));
}
Also used : CalculationResult(au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult) Options(au.gov.amsa.geo.model.Options) File(java.io.File)

Example 5 with Options

use of au.gov.amsa.geo.model.Options in project risky by amsa-code.

the class DistanceTravelledMovieMaker method main.

public static void main(String[] args) {
    double cellSizeDegrees = 0.2;
    Options options = createOptions(cellSizeDegrees);
    String directory = System.getProperty("user.home") + "/Downloads/positions-365-days";
    final Observable<File> files = Util.getFiles(directory, "craft-");
    Observable<Long> times = Observable.range(1, 13).map(new Func1<Integer, Long>() {

        @Override
        public Long call(Integer n) {
            return DateTime.parse("2013-06-01").plusMonths(n - 1).getMillis();
        }
    });
    saveImagesWithTimeRange(options, files, times, "target");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Options(au.gov.amsa.geo.model.Options) File(java.io.File)

Aggregations

Options (au.gov.amsa.geo.model.Options)10 Bounds (au.gov.amsa.geo.model.Bounds)6 File (java.io.File)5 SegmentOptions (au.gov.amsa.geo.model.SegmentOptions)4 Test (org.junit.Test)4 CellValue (au.gov.amsa.geo.model.CellValue)3 ArrayList (java.util.ArrayList)3 CalculationResult (au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult)2 OperatorWriteBytes (au.gov.amsa.util.rx.OperatorWriteBytes)2 IOException (java.io.IOException)2 List (java.util.List)2 Map (java.util.Map)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Observable (rx.Observable)2 BinaryCellValuesObservable (au.gov.amsa.geo.BinaryCellValuesObservable)1 OperatorCellValuesToBytes (au.gov.amsa.geo.OperatorCellValuesToBytes)1 Util (au.gov.amsa.geo.Util)1 Renderer.saveAsPng (au.gov.amsa.geo.distance.Renderer.saveAsPng)1 Cell (au.gov.amsa.geo.model.Cell)1