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);
}
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);
}
}
}
}
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 + "");
}
}
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));
}
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");
}
Aggregations