Search in sources :

Example 1 with DriftCandidate

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);
                }
            });
        }
    };
}
Also used : Action0(rx.functions.Action0) DriftCandidate(au.gov.amsa.navigation.DriftCandidate) Action1(rx.functions.Action1) DecimalFormat(java.text.DecimalFormat) AtomicLong(java.util.concurrent.atomic.AtomicLong) Func1(rx.functions.Func1) VesselPosition(au.gov.amsa.navigation.VesselPosition)

Example 2 with DriftCandidate

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();
}
Also used : PrintStream(java.io.PrintStream) Key(au.gov.amsa.ihs.reader.Key) Info(au.gov.amsa.navigation.ShipStaticData.Info) IhsReader(au.gov.amsa.ihs.reader.IhsReader) Logging(com.github.davidmoten.rx.slf4j.Logging) Instant(java.time.Instant) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) DriftCandidates(au.gov.amsa.navigation.DriftCandidates) DriftCandidate(au.gov.amsa.navigation.DriftCandidate) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) Optional(java.util.Optional) ZoneOffset(java.time.ZoneOffset) ShipStaticData(au.gov.amsa.navigation.ShipStaticData) PrintStream(java.io.PrintStream) Optional(java.util.Optional) Info(au.gov.amsa.navigation.ShipStaticData.Info) DateTimeFormatter(java.time.format.DateTimeFormatter) Map(java.util.Map) File(java.io.File)

Aggregations

DriftCandidate (au.gov.amsa.navigation.DriftCandidate)2 IhsReader (au.gov.amsa.ihs.reader.IhsReader)1 Key (au.gov.amsa.ihs.reader.Key)1 DriftCandidates (au.gov.amsa.navigation.DriftCandidates)1 ShipStaticData (au.gov.amsa.navigation.ShipStaticData)1 Info (au.gov.amsa.navigation.ShipStaticData.Info)1 VesselPosition (au.gov.amsa.navigation.VesselPosition)1 Logging (com.github.davidmoten.rx.slf4j.Logging)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 PrintStream (java.io.PrintStream)1 DecimalFormat (java.text.DecimalFormat)1 Instant (java.time.Instant)1 ZoneOffset (java.time.ZoneOffset)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Map (java.util.Map)1 Optional (java.util.Optional)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 Action0 (rx.functions.Action0)1 Action1 (rx.functions.Action1)1