Search in sources :

Example 1 with MmsiValidator2

use of au.gov.amsa.util.identity.MmsiValidator2 in project risky by amsa-code.

the class DistanceTravelledInEezMain method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    System.out.println("running");
    File tracks = new File("/home/dxm/combinedSortedTracks");
    long t = System.currentTimeMillis();
    List<File> files = Arrays.asList(tracks.listFiles());
    files.sort((a, b) -> a.getName().compareTo(b.getName()));
    try (PrintStream out = new PrintStream(new BufferedOutputStream(new FileOutputStream("target/output.csv")))) {
        out.println(Vessel.headings());
        // 
        Observable.from(// 
        files).filter(// 
        x -> x.getName().endsWith(".track.gz")).filter(// 
        x -> x.getName().startsWith("2017")).flatMap(file -> {
            log.info(file);
            // Note that the Shapefile objects are not thread-safe so we make new one for
            // each file to enable parallel processing
            // used for intersections with eez boundary
            Shapefile eezLine = Eez.loadEezLine();
            // used for contains tests
            Shapefile eezPolygon = Eez.loadEezPolygon();
            long startTime = Util.getStartTime(file);
            long endTime = startTime + TimeUnit.HOURS.toMillis(24);
            return // 
            BinaryFixes.from(file, true, // 
            BinaryFixesFormat.WITH_MMSI).subscribeOn(// 
            Schedulers.computation()).filter(// 
            f -> MmsiValidator2.INSTANCE.isValid(f.mmsi())).filter(// 
            f -> f.time() >= startTime && f.time() <= endTime).groupBy(// 
            fix -> fix.mmsi()).flatMap(o -> calculateDistance(file, eezLine, eezPolygon, o));
        }, // 
        Runtime.getRuntime().availableProcessors()).filter(// 
        x -> x.state.fix != null).toBlocking().forEach(x -> out.println(x.line()));
    }
    System.out.println((System.currentTimeMillis() - t) + "ms");
}
Also used : Arrays(java.util.Arrays) Downsample(au.gov.amsa.risky.format.Downsample) Date(java.util.Date) SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) OperatorEffectiveSpeedChecker(au.gov.amsa.geo.distance.OperatorEffectiveSpeedChecker) SimpleDateFormat(java.text.SimpleDateFormat) ShapefileUtil(au.gov.amsa.geo.ShapefileUtil) BufferedOutputStream(java.io.BufferedOutputStream) TimedPosition(au.gov.amsa.geo.TimedPosition) Observable(rx.Observable) Logger(org.apache.log4j.Logger) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) Eez(au.gov.amsa.geo.Eez) ParseException(java.text.ParseException) PrintStream(java.io.PrintStream) TimeZone(java.util.TimeZone) DecimalFormat(java.text.DecimalFormat) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) BinaryFixesFormat(au.gov.amsa.risky.format.BinaryFixesFormat) TimeUnit(java.util.concurrent.TimeUnit) Position(com.github.davidmoten.grumpy.core.Position) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) Shapefile(au.gov.amsa.gt.Shapefile) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) PrintStream(java.io.PrintStream) FileOutputStream(java.io.FileOutputStream) Shapefile(au.gov.amsa.gt.Shapefile) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 2 with MmsiValidator2

use of au.gov.amsa.util.identity.MmsiValidator2 in project risky by amsa-code.

the class VesselsInGbrMain method main.

public static void main(String[] args) throws IOException {
    long t = System.currentTimeMillis();
    File out = new File("target/mmsi.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();
        // 
        Observable.from(list).groupBy(f -> count.getAndIncrement() % // 
        Runtime.getRuntime().availableProcessors()).flatMap(// 
        files -> vesselsInGbr(files, Schedulers.computation())).distinct(// 
        fix -> fix.mmsi() + fix.aisClass().name()).sorted(// 
        (a, b) -> Integer.compare(a.mmsi(), b.mmsi())).filter(// 
        fix -> MmsiValidator2.INSTANCE.isValid((long) fix.mmsi())).doOnNext(// 
        fix -> write(writer, fix)).toBlocking().subscribe();
    }
    System.out.println((System.currentTimeMillis() - t) + "ms");
}
Also used : BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Scheduler(rx.Scheduler) Files(au.gov.amsa.util.Files) File(java.io.File) ArrayList(java.util.ArrayList) Observable(rx.Observable) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) OutputStreamWriter(java.io.OutputStreamWriter) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 3 with MmsiValidator2

use of au.gov.amsa.util.identity.MmsiValidator2 in project risky by amsa-code.

the class VesselsInCoverageMain method main.

public static void main(String[] args) throws IOException {
    long t = System.currentTimeMillis();
    File out = new File("target/mmsi.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();
        // 
        Observable.from(list).groupBy(f -> count.getAndIncrement() % // 
        Runtime.getRuntime().availableProcessors()).flatMap(// 
        files -> toFixes(files, Schedulers.computation())).distinct(// 
        fix -> fix.mmsi() + fix.aisClass().name()).sorted(// 
        (a, b) -> Integer.compare(a.mmsi(), b.mmsi())).filter(// 
        fix -> MmsiValidator2.INSTANCE.isValid((long) fix.mmsi())).doOnNext(// 
        fix -> write(writer, fix)).toBlocking().subscribe();
    }
    System.out.println((System.currentTimeMillis() - t) + "ms");
}
Also used : BufferedWriter(java.io.BufferedWriter) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) Scheduler(rx.Scheduler) Files(au.gov.amsa.util.Files) File(java.io.File) ArrayList(java.util.ArrayList) Observable(rx.Observable) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) OutputStreamWriter(java.io.OutputStreamWriter) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) Pattern(java.util.regex.Pattern) Pattern(java.util.regex.Pattern) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) BufferedWriter(java.io.BufferedWriter)

Example 4 with MmsiValidator2

use of au.gov.amsa.util.identity.MmsiValidator2 in project risky by amsa-code.

the class DistanceTravelledMain method run.

private static void run(String directory, Options options, boolean gui, String dataSetName) {
    InputStream is;
    try {
        is = new GZIPInputStream(new FileInputStream("/media/an/ship-data/ais/ship-data-2014-v2.txt.gz"));
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    Map<Integer, Info> shipInfo = ShipStaticData.getMapFromReader(new InputStreamReader(is, StandardCharsets.UTF_8));
    List<Setting> settings = new ArrayList<>();
    settings.add(Setting.create(30, 30, "fishing"));
    settings.add(Setting.create(52, 52, "tug"));
    settings.add(Setting.create(60, 69, "passenger"));
    settings.add(Setting.create(70, 79, "cargo"));
    settings.add(Setting.create(80, 89, "tanker"));
    settings.add(Setting.create(90, 99, "other"));
    settings.add(Setting.create(-1, -1, "class_b"));
    settings.add(Setting.create(0, 100, "all"));
    for (Setting setting : settings) {
        // filter out undesired mmsi numbers and ship types
        Func1<Info, Boolean> shipSelector = info -> info != null && ((info.cls == AisClass.B && setting.lowerBound == -1) || (info.cls == AisClass.A && (info.shipType.isPresent() && info.shipType.get() >= setting.lowerBound && info.shipType.get() <= setting.upperBound))) && MmsiValidator2.INSTANCE.isValid(info.mmsi);
        calculateTrafficDensity(directory, options, gui, shipInfo, shipSelector, setting.name + "-" + dataSetName);
    }
}
Also used : Renderer.saveAsPng(au.gov.amsa.geo.distance.Renderer.saveAsPng) BinaryCellValuesObservable(au.gov.amsa.geo.BinaryCellValuesObservable) OperatorCellValuesToBytes(au.gov.amsa.geo.OperatorCellValuesToBytes) GZIPInputStream(java.util.zip.GZIPInputStream) SegmentOptions(au.gov.amsa.geo.model.SegmentOptions) AisClass(au.gov.amsa.risky.format.AisClass) ArrayList(java.util.ArrayList) Observable(rx.Observable) Logger(org.apache.log4j.Logger) Func1(rx.functions.Func1) Util(au.gov.amsa.geo.Util) Bounds(au.gov.amsa.geo.model.Bounds) Map(java.util.Map) ShipStaticData(au.gov.amsa.navigation.ShipStaticData) CalculationResult(au.gov.amsa.geo.distance.DistanceTravelledCalculator.CalculationResult) Info(au.gov.amsa.navigation.ShipStaticData.Info) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream) OperatorWriteBytes(au.gov.amsa.util.rx.OperatorWriteBytes) InputStreamReader(java.io.InputStreamReader) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) CellValue(au.gov.amsa.geo.model.CellValue) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) InputStream(java.io.InputStream) Options(au.gov.amsa.geo.model.Options) InputStreamReader(java.io.InputStreamReader) GZIPInputStream(java.util.zip.GZIPInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Info(au.gov.amsa.navigation.ShipStaticData.Info) FileInputStream(java.io.FileInputStream) GZIPInputStream(java.util.zip.GZIPInputStream)

Example 5 with MmsiValidator2

use of au.gov.amsa.util.identity.MmsiValidator2 in project risky by amsa-code.

the class AdHocMain method main.

public static void main(String[] args) throws IOException {
    long start = ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse("2014-05-13T00:00:00Z")).toEpochSecond() * 1000;
    long finish = ZonedDateTime.from(DateTimeFormatter.ISO_DATE_TIME.parse("2014-05-27T00:00:00Z")).toEpochSecond() * 1000;
    Pattern pattern = Pattern.compile(".*\\.track");
    PrintStream out = new PrintStream("target/output.txt");
    out.println("mmsi\ttime\tlat\tlong\tcourse\tspeedKnots");
    List<File> files = Files.find(new File("/media/an/binary-fixes-5-minute/2014"), pattern);
    Observable.from(files).flatMap(file -> extract(file, start, finish).subscribeOn(Schedulers.computation())).filter(fix -> MmsiValidator2.INSTANCE.isValid(fix.mmsi())).map(f -> String.format("%s\t%s\t%s\t%s\t%s\t%s", f.mmsi(), formatDateTime(f.time()), f.lat(), f.lon(), get(f.courseOverGroundDegrees()), get(f.speedOverGroundKnots()))).doOnNext(out::println).lift(Logging.<String>logger().showCount().every(10000).log()).count().doOnTerminate(out::close).toBlocking().single();
}
Also used : PrintStream(java.io.PrintStream) ZonedDateTime(java.time.ZonedDateTime) Logging(com.github.davidmoten.rx.slf4j.Logging) IOException(java.io.IOException) Instant(java.time.Instant) Files(au.gov.amsa.util.Files) File(java.io.File) Observable(rx.Observable) List(java.util.List) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Func2(rx.functions.Func2) DateTimeFormatter(java.time.format.DateTimeFormatter) Fix(au.gov.amsa.risky.format.Fix) Schedulers(rx.schedulers.Schedulers) Optional(java.util.Optional) MmsiValidator2(au.gov.amsa.util.identity.MmsiValidator2) ZoneOffset(java.time.ZoneOffset) Pattern(java.util.regex.Pattern) Functions(com.github.davidmoten.rx.Functions) Pattern(java.util.regex.Pattern) PrintStream(java.io.PrintStream) File(java.io.File)

Aggregations

MmsiValidator2 (au.gov.amsa.util.identity.MmsiValidator2)5 File (java.io.File)5 IOException (java.io.IOException)5 List (java.util.List)5 Observable (rx.Observable)5 BinaryFixes (au.gov.amsa.risky.format.BinaryFixes)4 Fix (au.gov.amsa.risky.format.Fix)4 Schedulers (rx.schedulers.Schedulers)4 Files (au.gov.amsa.util.Files)3 FileOutputStream (java.io.FileOutputStream)3 ArrayList (java.util.ArrayList)3 Pattern (java.util.regex.Pattern)3 SegmentOptions (au.gov.amsa.geo.model.SegmentOptions)2 BufferedWriter (java.io.BufferedWriter)2 OutputStreamWriter (java.io.OutputStreamWriter)2 PrintStream (java.io.PrintStream)2 GroupedObservable (rx.observables.GroupedObservable)2 BinaryCellValuesObservable (au.gov.amsa.geo.BinaryCellValuesObservable)1 Eez (au.gov.amsa.geo.Eez)1 OperatorCellValuesToBytes (au.gov.amsa.geo.OperatorCellValuesToBytes)1