use of au.gov.amsa.navigation.DriftCandidate in project risky by amsa-code.
the class DriftingLayer method filenameToDriftCandidates.
private static Func1<String, Observable<DriftCandidate>> filenameToDriftCandidates() {
return new Func1<String, Observable<DriftCandidate>>() {
@Override
public Observable<DriftCandidate> call(final String filename) {
return Streams.nmeaFromGzip(filename).compose(AisVesselPositions.positions()).doOnRequest(new Action1<Long>() {
@Override
public void call(Long n) {
// log.info("requested=" + n);
}
}).doOnNext(new Action1<VesselPosition>() {
final long startTime = System.currentTimeMillis();
long lastTime = System.currentTimeMillis();
DecimalFormat df = new DecimalFormat("0");
@Override
public void call(VesselPosition vp) {
long n = 100000;
if (totalCount.incrementAndGet() % n == 0) {
long now = System.currentTimeMillis();
final double rate;
if (now == lastTime)
rate = -1;
else {
rate = n / (double) (now - lastTime) * 1000d;
}
lastTime = now;
final double rateSinceStart;
if (now == startTime)
rateSinceStart = -1;
else
rateSinceStart = totalCount.get() / (double) (now - startTime) * 1000d;
log.info("totalCount=" + totalCount.get() + ", msgsPerSecond=" + df.format(rate) + ", msgPerSecondOverall=" + df.format(rateSinceStart));
}
}
}).compose(DriftDetector.detectDrift()).subscribeOn(Schedulers.computation()).doOnCompleted(new Action0() {
@Override
public void call() {
log.info("finished " + filename);
}
});
}
};
}
use of au.gov.amsa.navigation.DriftCandidate in project risky by amsa-code.
the class DriftCandidatesMain method main.
public static void main(String[] args) throws FileNotFoundException {
PrintStream out = new PrintStream("target/output.txt");
out.format("%s\t%s\t%s\t%s\t%s\n", "mmsi", "imo", "date", "lat", "lon");
DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
Map<Integer, Info> ships = ShipStaticData.getMapFromResource("/ship-data-2014.txt");
Map<String, Map<String, String>> ihs = IhsReader.fromZipAsMapByMmsi(new File("/media/an/ship-data/ihs/608750-2015-04-01.zip")).toBlocking().single();
for (int year = 2012; year <= 2014; year++) {
DriftCandidates.fromCsv(new File("/media/an/drift-candidates/drift-candidates-" + year + ".txt.gz"), true).lift(Logging.<DriftCandidate>logger().showCount().showMemory().every(100000).log()).filter(c -> c.driftingSince() == c.fix().time()).distinct(c -> c.fix().mmsi() + ":" + DateTimeFormatter.ISO_DATE.format(Instant.ofEpochMilli(c.fix().time()).atZone(ZoneOffset.UTC))).doOnNext(c -> {
// lookup the imo from ais ship static reports
Optional<Info> aisInfo = Optional.ofNullable(ships.get(c.fix().mmsi()));
Optional<String> aisImo;
Optional<String> ihsImo;
if (aisInfo.isPresent() && aisInfo.get().imo.isPresent()) {
aisImo = Optional.of(aisInfo.get().imo.get());
ihsImo = Optional.empty();
} else {
aisImo = Optional.empty();
// lookup the imo from ihs data
Optional<Map<String, String>> ihsInfo = Optional.ofNullable(ihs.get(c.fix().mmsi()));
if (ihsInfo.isPresent()) {
ihsImo = Optional.ofNullable(ihsInfo.get().get(Key.LRIMOShipNo.toString()));
} else {
ihsImo = Optional.empty();
}
}
String imo = aisImo.orElse(ihsImo.orElse(""));
out.format("%s\t%s\t%s\t%s\t%s\n", c.fix().mmsi(), imo, dtf.format(Instant.ofEpochMilli(c.fix().time()).atZone(ZoneOffset.UTC)), c.fix().lat(), c.fix().lon());
}).count().toBlocking().single();
}
out.close();
}
Aggregations