Search in sources :

Example 1 with Pair

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

the class GeoffHAdhocMain method main.

public static void main(String[] args) {
    int option = 1;
    if (option == 1) {
        String filename = "/home/dxm/temp/235099876.track";
        // String filename =
        // "/media/an/binary-fixes-5-minute/2014/235099876.track";
        File file = new File(filename);
        BinaryFixes.from(file).doOnNext(System.out::println).toSortedList((a, b) -> Long.compare(a.time(), b.time())).flatMap(Observable::from).doOnCompleted(() -> System.out.println("complete")).subscribe();
    } else if (option == 2) {
        Observable<File> files = Observable.from(Files.find(new File("/media/an/binary-fixes-5-minute/2014"), Pattern.compile("\\d+\\.track")));
        Func2<Pair<File, Long>, Pair<File, Long>, Pair<File, Long>> earliest = (p1, p2) -> {
            if (p1.b() <= p2.b())
                return p1;
            else
                return p2;
        };
        files.filter(file -> !file.getName().equals("0.track")).flatMap(file -> BinaryFixes.from(file).<Pair<File, Long>>map(fix -> Pair.create(file, fix.time())).reduce(earliest).doOnNext(p -> System.out.println(file + ": " + p)).subscribeOn(Schedulers.computation())).reduce(earliest).doOnNext(System.out::println).toBlocking().single();
    }
}
Also used : Pair(au.gov.amsa.util.Pair) Func2(rx.functions.Func2) BinaryFixes(au.gov.amsa.risky.format.BinaryFixes) Schedulers(rx.schedulers.Schedulers) Pattern(java.util.regex.Pattern) Files(au.gov.amsa.util.Files) File(java.io.File) Observable(rx.Observable) File(java.io.File) Observable(rx.Observable) Func2(rx.functions.Func2)

Example 2 with Pair

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

the class SatelliteReportingIntervalsMain method main.

/**
 * Used this oracle query to get time diffs by mmsi for satellite reports
 * from cts.position.
 *
 * <pre>
 * select mmsi, timeMs from
 * (
 * select mmsi, EXTRACT( DAY    FROM diff ) * 24*60*60000
 *      + EXTRACT( HOUR   FROM diff ) *  60*60000
 *      + EXTRACT( MINUTE FROM diff ) *  60000
 *      + EXTRACT( SECOND FROM diff ) * 1000 timeMs from
 * (
 * select mmsi, position_time - to_timestamp('1970-01-01', 'yyyy-mm-dd') diff from cts.position
 * where position_time >= to_date('2015/06/28','yyyy/mm/dd') and position_time < to_date('2015/06/30','yyyy/mm/dd')
 * and (source_detail like 'AISSat%' or source_detail like 'NORAIS%' or source_detail like 'rEV%')
 * )
 * ) order by mmsi, timeMs
 * </pre>
 *
 * @param args
 */
public static void main(String[] args) {
    Observable<Double> splits = Strings.from(new File("/home/dxm/times.txt")).compose(o -> Strings.split(o, "\n")).filter(line -> line.trim().length() > 0).map(line -> line.split("\t")).map(items -> new Record(Long.parseLong(items[0]), Long.parseLong(items[1]) / ((double) TimeUnit.HOURS.toMillis(1)))).groupBy(record -> record.mmsi).flatMap(g -> g.buffer(2, 1).filter(list -> list.size() == 2).map(list -> list.get(1).timeHrs - list.get(0).timeHrs)).toSortedList().flatMap(list -> Observable.from(list)).cast(Double.class).cache();
    splits.reduce(Pair.create(0, 0.0), (p1, x) -> Pair.<Integer, Double>create(p1.a() + 1, x + p1.b())).map(pair -> pair.b() / pair.a()).doOnNext(value -> System.out.println("average interval hours=" + value)).subscribe();
    Observable<BucketCount> buckets = splits.map(diff -> Math.floor(diff * 10) / 10.0).collect(() -> new HashMap<Double, Integer>(), (map, x) -> {
        if (map.get(x) == null)
            map.put(x, 1);
        else
            map.put(x, map.get(x) + 1);
    }).map(map -> new TreeMap<Double, Integer>(map)).flatMap(map -> Observable.from(map.entrySet())).map(entry -> new BucketCount(entry.getKey(), entry.getValue())).scan(new BucketCount(0.0, 0), (b1, b2) -> new BucketCount(b2.bucket, b1.count + b2.count)).cache();
    double max = buckets.last().map(b -> b.count).toBlocking().single();
    buckets.doOnNext(b -> System.out.println(b.bucket + "\t" + b.count / max * 100)).count().toBlocking().single();
    System.exit(0);
    File file = new File("/home/dxm/2015-06-29.txt.gz");
    Streams.nmeaFromGzip(file).compose(o -> Streams.extractMessages(o)).filter(m -> m.message() instanceof AisPosition).filter(m -> isNorwaySatellite(m.message().getSource())).groupBy(m -> ((AisPosition) m.message()).getMmsi()).flatMap(g -> g.toSortedList()).doOnNext(System.out::println).count().toBlocking().single();
}
Also used : TimeUnit(java.util.concurrent.TimeUnit) Pair(au.gov.amsa.util.Pair) TreeMap(java.util.TreeMap) Streams(au.gov.amsa.ais.rx.Streams) AisPosition(au.gov.amsa.ais.message.AisPosition) HashMap(java.util.HashMap) File(java.io.File) Strings(au.gov.amsa.streams.Strings) Observable(rx.Observable) HashMap(java.util.HashMap) AisPosition(au.gov.amsa.ais.message.AisPosition) File(java.io.File)

Aggregations

Pair (au.gov.amsa.util.Pair)2 File (java.io.File)2 Observable (rx.Observable)2 AisPosition (au.gov.amsa.ais.message.AisPosition)1 Streams (au.gov.amsa.ais.rx.Streams)1 BinaryFixes (au.gov.amsa.risky.format.BinaryFixes)1 Strings (au.gov.amsa.streams.Strings)1 Files (au.gov.amsa.util.Files)1 HashMap (java.util.HashMap)1 TreeMap (java.util.TreeMap)1 TimeUnit (java.util.concurrent.TimeUnit)1 Pattern (java.util.regex.Pattern)1 Func2 (rx.functions.Func2)1 Schedulers (rx.schedulers.Schedulers)1