Search in sources :

Example 1 with Logging

use of com.github.davidmoten.rx.slf4j.Logging in project risky by amsa-code.

the class BinaryFixesMain method main.

public static void main(String[] args) {
    // perform a speed test for loading BinaryFixes from disk
    FixImpl.validate = false;
    final ConcurrentHashMap<Long, List<FixImpl>> map = new ConcurrentHashMap<Long, List<FixImpl>>();
    // -downsample-5-mins
    List<File> files = Files.find(new File("/media/an/binary-fixes/2014-year-downsample-5-mins"), Pattern.compile(".*\\.track"));
    long t = System.currentTimeMillis();
    long count = Observable.from(files).buffer(Math.max(1, files.size() / Runtime.getRuntime().availableProcessors())).flatMap(list -> {
        return Observable.from(list).concatMap(file -> BinaryFixes.from(file).countLong()).subscribeOn(Schedulers.computation());
    }).scan(0L, (a, b) -> a + b).lift(Logging.<Long>logger().showCount().prefix("records=").showMemory().every(1000).log()).last().toBlocking().single();
    long elapsed = System.currentTimeMillis() - t;
    System.out.println("Map size = " + map.size());
    System.out.println("Total records = " + count + ", numPerSecond=" + count * 1000.0 / elapsed + ", timeMs=" + elapsed);
}
Also used : List(java.util.List) Logging(com.github.davidmoten.rx.slf4j.Logging) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Schedulers(rx.schedulers.Schedulers) Pattern(java.util.regex.Pattern) Files(au.gov.amsa.util.Files) File(java.io.File) Observable(rx.Observable) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) File(java.io.File)

Example 2 with Logging

use of com.github.davidmoten.rx.slf4j.Logging in project risky by amsa-code.

the class ShipTypeBreakdownMain method main.

public static void main(String[] args) throws FileNotFoundException, IOException {
    // load a shapefile
    final GeometryFactory gf = new GeometryFactory();
    File file = new File("/home/dxm/temp/srr.shp");
    Map<String, Serializable> map = new HashMap<>();
    map.put("url", file.toURI().toURL());
    DataStore datastore = DataStoreFinder.getDataStore(map);
    String typeName = datastore.getTypeNames()[0];
    System.out.println(typeName);
    SimpleFeatureSource source = datastore.getFeatureSource(typeName);
    final SimpleFeatureCollection features = source.getFeatures();
    final List<PreparedGeometry> geometries = new ArrayList<>();
    SimpleFeatureIterator it = features.features();
    while (it.hasNext()) {
        SimpleFeature feature = it.next();
        Geometry g = (Geometry) feature.getDefaultGeometry();
        geometries.add(PreparedGeometryFactory.prepare(g));
    }
    // System.exit(0);
    String filename = "/media/analysis/nmea/2014/NMEA_ITU_20140815.gz";
    final Set<Long> mmsi = new HashSet<Long>();
    final Set<Long> mmsiA = new HashSet<Long>();
    final Set<Long> mmsiB = new HashSet<Long>();
    Streams.extract(Streams.nmeaFromGzip(filename)).flatMap(aisPositionsOnly).lift(Logging.<TimestampedAndLine<AisPosition>>logger().showCount().every(100000).log()).doOnNext(m -> {
        AisPosition p = m.getMessage().get().message();
        if (p.getLatitude() != null && p.getLongitude() != null && contains(gf, geometries, p.getLatitude(), p.getLongitude())) {
            long mmsiNo = m.getMessage().get().message().getMmsi();
            mmsi.add(mmsiNo);
            if (m.getMessage().get().message() instanceof AisPositionA)
                mmsiA.add(mmsiNo);
            else
                mmsiB.add(mmsiNo);
        }
    }).subscribe();
    final Map<ShipTypeClass, Set<Integer>> countsByShipType = new ConcurrentHashMap<>();
    Streams.extract(Streams.nmeaFromGzip(filename)).flatMap(aisShipStaticOnly).doOnNext(m -> {
        AisShipStatic s = m.getMessage().get().message();
        if (mmsi.contains(Long.valueOf(s.getMmsi()))) {
            boolean isClassA = s instanceof AisShipStaticA;
            ShipTypeClass shipTypeClass = new ShipTypeClass(isClassA, s.getShipType());
            if (countsByShipType.get(shipTypeClass) == null)
                countsByShipType.put(shipTypeClass, new HashSet<Integer>());
            else
                countsByShipType.get(shipTypeClass).add(s.getMmsi());
        }
    }).subscribe();
    System.out.println(countsByShipType);
    Set<String> set = new TreeSet<String>();
    int sizeA = 0;
    int sizeB = 0;
    for (Entry<ShipTypeClass, Set<Integer>> s : countsByShipType.entrySet()) {
        set.add(ShipTypeDecoder.getShipType(s.getKey().shipType) + "\t" + (s.getKey().isClassA ? "A" : "B") + "\t" + s.getValue().size());
        if (s.getKey().isClassA)
            sizeA += s.getValue().size();
        else
            sizeB += s.getValue().size();
    }
    set.stream().forEach(System.out::println);
    System.out.println("Unknown\tA\t" + (mmsiA.size() - sizeA));
    System.out.println("Unknown\tB\t" + (mmsiB.size() - sizeB));
    log.info("finished");
}
Also used : ShipTypeDecoder(au.gov.amsa.ais.ShipTypeDecoder) Logging(com.github.davidmoten.rx.slf4j.Logging) AisPosition(au.gov.amsa.ais.message.AisPosition) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) LoggerFactory(org.slf4j.LoggerFactory) Coordinate(org.locationtech.jts.geom.Coordinate) HashMap(java.util.HashMap) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) Observable(rx.Observable) HashSet(java.util.HashSet) Func1(rx.functions.Func1) AisPositionA(au.gov.amsa.ais.message.AisPositionA) SimpleFeature(org.opengis.feature.simple.SimpleFeature) Streams(au.gov.amsa.ais.rx.Streams) Map(java.util.Map) DataStoreFinder(org.geotools.data.DataStoreFinder) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) Logger(org.slf4j.Logger) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) AisMessage(au.gov.amsa.ais.AisMessage) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) DataStore(org.geotools.data.DataStore) Serializable(java.io.Serializable) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) Geometry(org.locationtech.jts.geom.Geometry) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) Timestamped(au.gov.amsa.ais.Timestamped) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) Serializable(java.io.Serializable) GeometryFactory(org.locationtech.jts.geom.GeometryFactory) PreparedGeometryFactory(org.locationtech.jts.geom.prep.PreparedGeometryFactory) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) SimpleFeatureSource(org.geotools.data.simple.SimpleFeatureSource) ArrayList(java.util.ArrayList) SimpleFeatureIterator(org.geotools.data.simple.SimpleFeatureIterator) AisPositionA(au.gov.amsa.ais.message.AisPositionA) TreeSet(java.util.TreeSet) DataStore(org.geotools.data.DataStore) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) HashSet(java.util.HashSet) SimpleFeature(org.opengis.feature.simple.SimpleFeature) SimpleFeatureCollection(org.geotools.data.simple.SimpleFeatureCollection) AisPosition(au.gov.amsa.ais.message.AisPosition) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) Geometry(org.locationtech.jts.geom.Geometry) PreparedGeometry(org.locationtech.jts.geom.prep.PreparedGeometry) File(java.io.File)

Example 3 with Logging

use of com.github.davidmoten.rx.slf4j.Logging in project risky by amsa-code.

the class Collator method main.

public static void main(String[] args) throws IOException {
    final PrintWriter out = new PrintWriter(new OutputStreamWriter(new FileOutputStream("target/destinations.txt"), Charset.forName("UTF-8")));
    File[] files = new File("/media/analysis/nmea/2013").listFiles(f -> f.getName().endsWith(".gz"));
    Arrays.sort(files, (f1, f2) -> f1.getName().compareTo(f2.getName()));
    Observable<File> fileList = Observable.from(files).lift(Logging.<File>logger().showValue().log());
    final AtomicInteger count = new AtomicInteger();
    Observable<Observable<String>> nmeas = Streams.nmeasFromGzip(fileList);
    nmeas.flatMap(nmea -> getDestinations(nmea.doOnNext(line -> {
        int n = count.incrementAndGet();
        if (n % 1000000 == 0)
            System.out.println("lines read=" + (n / 1000000) + "m");
    }))).filter(line -> {
        if (line == null)
            System.out.println("line is null!");
        return line != null;
    }).distinct().doOnNext(destination -> {
        out.println(destination);
        out.flush();
    }).lift(Logging.<String>logger().showCount().showValue().showMemory().every(100).log()).count().toBlocking().single();
    out.close();
}
Also used : PrintWriter(java.io.PrintWriter) Arrays(java.util.Arrays) Charset(java.nio.charset.Charset) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Streams(au.gov.amsa.ais.rx.Streams) Logging(com.github.davidmoten.rx.slf4j.Logging) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) File(java.io.File) Observable(rx.Observable) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) File(java.io.File) Observable(rx.Observable) PrintWriter(java.io.PrintWriter)

Example 4 with Logging

use of com.github.davidmoten.rx.slf4j.Logging in project risky by amsa-code.

the class ShipStaticDataCreator method writeStaticDataToFileWithTimestamps.

public static Observable<Timestamped<AisShipStatic>> writeStaticDataToFileWithTimestamps(List<File> files, File outputFile, Scheduler scheduler) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'");
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
    Func0<PrintStream> resourceFactory = Checked.f0(() -> new PrintStream(outputFile));
    Func1<PrintStream, Observable<Timestamped<AisShipStatic>>> observableFactory = out -> Observable.from(files).buffer(Math.max(1, files.size() / Runtime.getRuntime().availableProcessors() - 1)).flatMap(list -> // 
    Observable.from(list).lift(// 
    Logging.<File>logger().showValue().showMemory().log()).concatMap(// 
    file -> timestampedShipStatics(file))).groupBy(// 
    m -> m.message().getMmsi()).flatMap(// 
    g -> collect(g).subscribeOn(scheduler)).compose(Transformers.doOnFirst(x -> {
        out.println("# MMSI, Time, IMO, AisClass, AisShipType, MaxPresentStaticDraughtMetres, DimAMetres, DimBMetres, DimCMetres, DimDMetres, LengthMetres, WidthMetres, Name");
        out.println("# columns are tab delimited");
        out.println("# -1 = not present");
    })).filter(// 
    set -> set.size() <= 10).flatMapIterable(// 
    set -> set).doOnNext(k -> {
        AisShipStatic m = k.message();
        out.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", m.getMmsi(), sdf.format(new Date(k.time())), getImo(m).orElse(-1), m instanceof AisShipStaticA ? "A" : "B", m.getShipType(), getMaximumPresentStaticDraughtMetres(m).orElse(-1F), m.getDimensionA().orElse(-1), m.getDimensionB().orElse(-1), m.getDimensionC().orElse(-1), m.getDimensionD().orElse(-1), AisShipStaticUtil.lengthMetres(m).orElse(-1), AisShipStaticUtil.widthMetres(m).orElse(-1), prepareName(m.getName()));
        out.flush();
    });
    Action1<PrintStream> disposeAction = out -> out.close();
    return Observable.using(resourceFactory, observableFactory, disposeAction);
}
Also used : Date(java.util.Date) Logging(com.github.davidmoten.rx.slf4j.Logging) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) SimpleDateFormat(java.text.SimpleDateFormat) Preconditions(com.github.davidmoten.guavamini.Preconditions) Action1(rx.functions.Action1) TreeSet(java.util.TreeSet) Observable(rx.Observable) Func0(rx.functions.Func0) Func1(rx.functions.Func1) AisShipStaticUtil(au.gov.amsa.ais.message.AisShipStaticUtil) Streams(au.gov.amsa.ais.rx.Streams) Schedulers(rx.schedulers.Schedulers) TimestampedAndLine(au.gov.amsa.ais.rx.Streams.TimestampedAndLine) PrintStream(java.io.PrintStream) Transformers(com.github.davidmoten.rx.Transformers) Iterator(java.util.Iterator) TimeZone(java.util.TimeZone) Checked(com.github.davidmoten.rx.Checked) AisMessage(au.gov.amsa.ais.AisMessage) Scheduler(rx.Scheduler) File(java.io.File) List(java.util.List) GroupedObservable(rx.observables.GroupedObservable) Optional(java.util.Optional) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) Timestamped(au.gov.amsa.ais.Timestamped) PrintStream(java.io.PrintStream) AisShipStatic(au.gov.amsa.ais.message.AisShipStatic) AisShipStaticA(au.gov.amsa.ais.message.AisShipStaticA) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) Observable(rx.Observable) GroupedObservable(rx.observables.GroupedObservable) Date(java.util.Date)

Example 5 with Logging

use of com.github.davidmoten.rx.slf4j.Logging in project risky by amsa-code.

the class CollisionDetectorMain method main.

public static void main(String[] args) throws IOException, InterruptedException {
    VesselPosition.validate = true;
    CollisionDetector c = new CollisionDetector();
    // String filename = "/media/an/nmea/2013/NMEA_ITU_20130108.gz";
    Map<Integer, Info> ships = ShipStaticData.getMapFromResource("/ship-data-2014.txt");
    // nmea from file
    // Streams.nmeaFromGzip(filename)
    File file = new File("/media/an/daily-fixes/2014/2014-02-01.fix");
    File candidates = new File("/media/an/temp/" + file.getName() + ".collision-candidates.txt");
    try (PrintStream out = new PrintStream(candidates)) {
        out.println("time,mmsi1,lat1, lon1, cog1, m/s, mmsi2, lat2, lon2, cog2, m/s");
        BinaryFixes.from(file, true, BinaryFixesFormat.WITH_MMSI).map(VesselPositions.TO_VESSEL_POSITION).lift(Logging.<VesselPosition>logger().showCount().every(1000).showMemory().log()).filter(onlyClassA).filter(p -> p.speedMetresPerSecond().isPresent()).filter(p -> p.cogDegrees().isPresent()).filter(p -> {
            Mmsi mmsi = (Mmsi) p.id();
            Optional<Info> info = Optional.ofNullable(ships.get(mmsi.value()));
            return (!info.isPresent() || !info.get().shipType.isPresent() || !isTugPilotTowing(info.get().shipType.get()));
        }).filter(p -> p.speedKnots().get() >= 5).compose(CollisionDetector.detectCollisionCandidates()).lift(Logging.<CollisionCandidate>logger().showCount().every(1).showValue().showMemory().log()).doOnNext(cc -> out.format("%s,%s,%s,%s,%s,%s,%s,%s,%s,%s", cc.time(), cc.position1().id().uniqueId(), cc.position1().lat(), cc.position1().lon(), cc.position1().cogDegrees(), cc.position1().speedMetresPerSecond().map(x -> String.valueOf(x)).orElse(""), cc.position2().id().uniqueId(), cc.position2().lat(), cc.position2().lon(), cc.position2().cogDegrees(), cc.position2().speedMetresPerSecond().map(x -> String.valueOf(x)).orElse(""))).count().toBlocking().single();
    }
}
Also used : PrintStream(java.io.PrintStream) Info(au.gov.amsa.navigation.ShipStaticData.Info) Func1(rx.functions.Func1) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Map(java.util.Map) Logging(com.github.davidmoten.rx.slf4j.Logging) Optional(java.util.Optional) Set(java.util.Set) IOException(java.io.IOException) File(java.io.File) Sets(com.google.common.collect.Sets) BinaryFixesFormat(au.gov.amsa.risky.format.BinaryFixesFormat) PrintStream(java.io.PrintStream) Optional(java.util.Optional) Info(au.gov.amsa.navigation.ShipStaticData.Info) File(java.io.File)

Aggregations

Logging (com.github.davidmoten.rx.slf4j.Logging)9 File (java.io.File)9 Observable (rx.Observable)7 List (java.util.List)6 Optional (java.util.Optional)6 IOException (java.io.IOException)5 PrintStream (java.io.PrintStream)5 Schedulers (rx.schedulers.Schedulers)5 AisShipStaticA (au.gov.amsa.ais.message.AisShipStaticA)4 Streams (au.gov.amsa.ais.rx.Streams)4 Func1 (rx.functions.Func1)4 AisMessage (au.gov.amsa.ais.AisMessage)3 Timestamped (au.gov.amsa.ais.Timestamped)3 AisShipStatic (au.gov.amsa.ais.message.AisShipStatic)3 TimestampedAndLine (au.gov.amsa.ais.rx.Streams.TimestampedAndLine)3 Files (au.gov.amsa.util.Files)3 Instant (java.time.Instant)3 ZoneOffset (java.time.ZoneOffset)3 DateTimeFormatter (java.time.format.DateTimeFormatter)3 Map (java.util.Map)3