use of au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker in project risky by amsa-code.
the class DistanceTravelledInEezMain method calculateDistance.
private static Observable<? extends Vessel> calculateDistance(File file, Shapefile eezLine, Shapefile eezPolygon, GroupedObservable<Integer, Fix> o) {
return Observable.defer(() -> {
State state = new State();
state.date = file.getName().substring(0, file.getName().indexOf(".track.gz"));
state.mmsi = o.getKey();
state.location = Location.UNKNOWN;
return //
o.compose(//
Downsample.minTimeStep(5, TimeUnit.MINUTES)).lift(new OperatorEffectiveSpeedChecker(SegmentOptions.builder().acceptAnyFixHours(480L).maxSpeedKnots(50).build())).filter(//
check -> check.isOk()).map(//
check -> check.fix()).doOnNext(fix -> {
// TODO unit test
boolean inside = eezPolygon.contains(fix.lat(), fix.lon());
Location location = inside ? Location.IN : Location.OUT;
if (state.location != Location.UNKNOWN) {
boolean crossed = state.location != location;
if (crossed) {
TimedPosition point = ShapefileUtil.findRegionCrossingPoint(eezLine, state.fix, fix);
final double distance;
if (location == Location.IN) {
distance = distanceKm(fix.lat(), fix.lon(), point.lat, point.lon);
} else {
distance = distanceKm(state.fix.lat(), state.fix.lon(), point.lat, point.lon);
}
state.distanceKm += distance;
double d = distanceKm(state.fix.lat(), state.fix.lon(), fix.lat(), fix.lon());
if (d >= MIN_DISTANCE_KM_TO_ESTIMATE_TIME) {
// we ensure that d is not close to zero so that the time estimate does not get
// blown out by instability in the division.
state.totalTimeMs += distance / d * (fix.time() - state.fix.time());
}
} else if (location == Location.IN) {
state.distanceKm += distanceKm(state.fix.lat(), state.fix.lon(), fix.lat(), fix.lon());
state.totalTimeMs += fix.time() - state.fix.time();
}
}
state.fix = fix;
state.location = location;
}).count().map(count -> new Vessel(count, state));
});
}
use of au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker in project risky by amsa-code.
the class EffectiveSpeedCheckFailures2Main method main.
public static void main(String[] args) throws ParseException {
SegmentOptions options = SegmentOptions.builder().acceptAnyFixHours(12L).maxSpeedKnots(50).build();
tasmania().groupBy(fix -> fix.mmsi()).flatMap(g -> g.lift(new OperatorEffectiveSpeedChecker(options)).buffer(2, 1).filter(list -> list.size() == 2 && list.get(0).isOk() && !list.get(1).isOk()).doOnNext(list -> {
System.out.println(" ok," + list.get(0));
System.out.println("bad," + list.get(1));
})).count().toBlocking().single();
}
use of au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker in project risky by amsa-code.
the class EffectiveSpeedFailuresMain method main.
public static void main(String[] args) {
Pattern pattern = Pattern.compile(".*\\.track");
List<File> files = Files.find(new File("/media/an/binary-fixes-5-minute/2015"), pattern);
int count = Observable.from(files).filter(file -> !file.getName().equals("0.track")).flatMap(file -> BinaryFixes.from(file).lift(new OperatorEffectiveSpeedChecker(SegmentOptions.builder().acceptAnyFixHours(12L).maxSpeedKnots(50).build())).filter(check -> !check.isOk()).reduce(new MmsiCount(0, 0), (mc, fix) -> new MmsiCount(fix.fix().mmsi(), mc.count + 1)).filter(mc -> mc.count >= 1000)).toSortedList((a, b) -> Long.compare(b.count, a.count)).flatMapIterable(x -> x).doOnNext(mc -> System.out.println(mc.mmsi)).count().toBlocking().single();
System.out.println(count);
}
use of au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker in project risky by amsa-code.
the class VoyageDatasetProducer method produce.
public static void produce(File output, File fixesOutput, List<File> list) throws Exception {
// reset output directories
output.delete();
FileUtils.deleteDirectory(fixesOutput);
int numFiles = list.size();
System.out.println(numFiles + "binary fix files");
AtomicInteger fileNumber = new AtomicInteger(0);
Collection<Port> ports = loadPorts();
Collection<EezWaypoint> eezWaypoints = readEezWaypoints();
Shapefile eezLine = Eez.loadEezLine();
Shapefile eezPolygon = Eez.loadEezPolygon();
System.out.println("loaded eez shapefiles");
long t = System.currentTimeMillis();
AtomicLong failedCheck = new AtomicLong();
AtomicLong fixCount = new AtomicLong();
Map<Integer, Integer> mmsisWithFailedChecks = new TreeMap<>();
Persister persister = new Persister(fixesOutput);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(output)))) {
// Note that in the observable below we don't employ parallel techniques
// this is because the runtime is acceptable
//
Observable.from(list).groupBy(//
f -> mmsiFromFilename(f)).flatMap(files -> {
String mmsi = files.getKey();
if (!isShipMmsi(mmsi)) {
return Observable.empty();
} else {
return //
files.compose(//
o -> logPercentCompleted(numFiles, t, o, fileNumber)).concatMap(//
BinaryFixes::from).lift(new OperatorEffectiveSpeedChecker(SegmentOptions.builder().acceptAnyFixHours(24L).maxSpeedKnots(50).build())).doOnNext(//
check -> updatedCounts(failedCheck, fixCount, mmsisWithFailedChecks, check)).filter(//
check -> check.isOk()).map(//
check -> check.fix()).doOnNext(fix -> persister.persist(fix)).compose(//
o -> toLegs(eezLine, eezPolygon, ports, eezWaypoints, o)).filter(x -> includeLeg(x));
}
}).sorted(//
(a, b) -> compareByMmsiThenLegStartTime(a, b)).doOnNext(//
x -> write(writer, x)).doOnTerminate(//
Checked.a0(() -> persister.close())).toBlocking().subscribe();
System.out.println((System.currentTimeMillis() - t) + "ms");
System.out.println("total fixes=" + fixCount.get());
System.out.println("num fixes rejected due failed effective speed check=" + failedCheck.get());
System.out.println("num mmsis with failed effective speed checks=" + mmsisWithFailedChecks.size());
try (PrintStream p = new PrintStream("target/info.txt")) {
p.println("total fixes=" + fixCount.get());
p.println("num fixes rejected due failed effective speed check=" + failedCheck.get());
p.println("num mmsis with failed effective speed checks=" + mmsisWithFailedChecks.size());
}
try (PrintStream p = new PrintStream("target/failures.txt")) {
p.println("failures mmsi <TAB> number of rejected fixes");
for (Integer mmsi : mmsisWithFailedChecks.keySet()) {
p.println(mmsi + "\t" + mmsisWithFailedChecks.get(mmsi));
}
}
}
}
Aggregations