Search in sources :

Example 1 with BinaryFixes

use of au.gov.amsa.risky.format.BinaryFixes 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));
            }
        }
    }
}
Also used : SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) ZonedDateTime(java.time.ZonedDateTime) OperatorEffectiveSpeedChecker(au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker) Preconditions(com.github.davidmoten.guavamini.Preconditions) BufferedOutputStream(java.io.BufferedOutputStream) ArrayList(java.util.ArrayList) Observable(rx.Observable) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Fix(au.gov.amsa.risky.format.Fix) OutputStreamWriter(java.io.OutputStreamWriter) ZoneOffset(java.time.ZoneOffset) OutputStream(java.io.OutputStream) PrintStream(java.io.PrintStream) BufferedWriter(java.io.BufferedWriter) Collection(java.util.Collection) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) EffectiveSpeedCheck(au.gov.amsa.geo.distance.EffectiveSpeedCheck) Reader(java.io.Reader) Checked(com.github.davidmoten.rx.Checked) Instant(java.time.Instant) InputStreamReader(java.io.InputStreamReader) Files(au.gov.amsa.util.Files) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) BinaryFixesFormat(au.gov.amsa.risky.format.BinaryFixesFormat) Strings(au.gov.amsa.streams.Strings) Position(com.github.davidmoten.grumpy.core.Position) VisibleForTesting(com.github.davidmoten.guavamini.annotations.VisibleForTesting) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) TreeMap(java.util.TreeMap) Closeable(java.io.Closeable) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Shapefile(au.gov.amsa.gt.Shapefile) Pattern(java.util.regex.Pattern) PrintStream(java.io.PrintStream) OperatorEffectiveSpeedChecker(au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker) TreeMap(java.util.TreeMap) BufferedWriter(java.io.BufferedWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) Shapefile(au.gov.amsa.gt.Shapefile) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) OutputStreamWriter(java.io.OutputStreamWriter)

Example 2 with BinaryFixes

use of au.gov.amsa.risky.format.BinaryFixes in project risky by amsa-code.

the class SingleThreadedPerfMain method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    long t = System.currentTimeMillis();
    File out = new File("target/positions.txt");
    out.delete();
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)))) {
        Pattern pattern = Pattern.compile(".*\\.track");
        List<File> list = new ArrayList<File>();
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2014"), pattern));
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2015"), pattern));
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2016"), pattern));
        // 
        Observable.from(list).concatMap(// 
        BinaryFixes::from).count().doOnNext(// 
        System.out::println).toBlocking().subscribe();
    }
    System.out.println((System.currentTimeMillis() - t) + "ms");
}
Also used : Pattern(java.util.regex.Pattern) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 3 with BinaryFixes

use of au.gov.amsa.risky.format.BinaryFixes in project risky by amsa-code.

the class PositionsInGbrMain method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    long t = System.currentTimeMillis();
    File out = new File("target/positions.txt");
    out.delete();
    try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(out)))) {
        Pattern pattern = Pattern.compile(".*\\.track");
        List<File> list = new ArrayList<File>();
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2014"), pattern));
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2015"), pattern));
        list.addAll(Files.find(new File("/media/an/binary-fixes-5-minute/2016"), pattern));
        AtomicInteger count = new AtomicInteger();
        int numFiles = list.size();
        AtomicInteger fileNumber = new AtomicInteger(0);
        // 
        Observable.from(list).groupBy(f -> count.getAndIncrement() % // 
        Runtime.getRuntime().availableProcessors()).flatMap(files -> // 
        files.doOnNext(file -> {
            int n = fileNumber.incrementAndGet();
            if (n % 1000 == 0)
                System.out.println("complete: " + new DecimalFormat("0.0").format(n / (double) numFiles * 100) + "%");
        }).concatMap(// 
        BinaryFixes::from).filter(// 
        x -> inGbr(x)).onBackpressureBuffer().subscribeOn(// 
        Schedulers.computation())).count().doOnNext(// 
        System.out::println).toBlocking().subscribe();
    }
    System.out.println((System.currentTimeMillis() - t) + "ms");
}
Also used : BufferedWriter(java.io.BufferedWriter) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Files(au.gov.amsa.util.Files) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) Observable(rx.Observable) List(java.util.List) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Fix(au.gov.amsa.risky.format.Fix) OutputStreamWriter(java.io.OutputStreamWriter) Pattern(java.util.regex.Pattern) Schedulers(com.github.davidmoten.rx.Schedulers) Pattern(java.util.regex.Pattern) DecimalFormat(java.text.DecimalFormat) ArrayList(java.util.ArrayList) BufferedWriter(java.io.BufferedWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File)

Aggregations

BinaryFixes (au.gov.amsa.risky.format.BinaryFixes)3 BufferedWriter (java.io.BufferedWriter)3 File (java.io.File)3 FileOutputStream (java.io.FileOutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 Fix (au.gov.amsa.risky.format.Fix)2 Files (au.gov.amsa.util.Files)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 DecimalFormat (java.text.DecimalFormat)2 List (java.util.List)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 EffectiveSpeedCheck (au.gov.amsa.geo.distance.EffectiveSpeedCheck)1 OperatorEffectiveSpeedChecker (au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker)1 SegmentOptions (au.gov.amsa.geo.model.SegmentOptions)1 Shapefile (au.gov.amsa.gt.Shapefile)1 BinaryFixesFormat (au.gov.amsa.risky.format.BinaryFixesFormat)1 Strings (au.gov.amsa.streams.Strings)1