use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class StreamsTest method testExtractShipStaticA.
@Test
public void testExtractShipStaticA() {
String line = "\\c:1432212545,g:1-1-6*1F\\!BSVDM,1,1,6,B,58LOWB02BafgUKWO7V0LhuHU>0l4E=A8v2222216D8N<D1Kb0CQiAC3kQp8888888888880,0*6C";
TimestampedAndLine<AisMessage> x = Streams.extract(Observable.just(line)).toBlocking().single();
AisShipStaticA m = (AisShipStaticA) x.getMessage().get().message();
assertEquals(566749000, m.getMmsi());
assertEquals(9610987, (int) m.getImo().get());
assertEquals("9V9719", m.getCallsign());
assertEquals("GLOVIS MAESTRO", m.getName());
assertEquals(161, (int) m.getDimensionA().get());
assertEquals(70, m.getShipType());
System.out.println(x);
}
use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class ShipStaticDataCreator method writeStaticDataToFile.
public static Observable<AisShipStatic> writeStaticDataToFile(List<File> files, File outputFile, Scheduler scheduler) {
Func0<PrintStream> resourceFactory = Checked.f0(() -> new PrintStream(outputFile));
Func1<PrintStream, Observable<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 -> //
Streams.extract(Streams.nmeaFromGzip(file)).flatMap(//
aisShipStaticOnly).map(//
m -> m.getMessage().get().message()).distinct(//
m -> m.getMmsi()).doOnError(e -> System.err.println("could not read " + file + ": " + //
e.getMessage())).onErrorResumeNext(//
Observable.<AisShipStatic>empty())).distinct(//
m -> m.getMmsi()).subscribeOn(//
scheduler)).distinct(//
m -> m.getMmsi()).compose(//
Transformers.mapWithIndex()).doOnNext(indexed -> {
if (indexed.index() == 0) {
out.println("# MMSI, IMO, AisClass, AisShipType, MaxPresentStaticDraughtMetres, DimAMetres, DimBMetres, DimCMetres, DimDMetres, LengthMetres, WidthMetres, Name");
out.println("# columns are tab delimited");
out.println("# -1 = not present");
}
}).map(indexed -> indexed.value()).doOnNext(m -> {
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\n", m.getMmsi(), 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);
}
use of au.gov.amsa.ais.message.AisShipStaticA in project risky by amsa-code.
the class ExtractStaticDataAsCsvFromNmeaGz method extractStaticData.
public static void extractStaticData(File input, File output) throws IOException {
output.getParentFile().mkdirs();
try (PrintStream out = new PrintStream(output)) {
//
Streams.nmeaFromGzip(input).compose(//
o -> Streams.extractMessages(o)).filter(x -> //
(x.message() instanceof AisShipStatic) || //
(x.message() instanceof AisBStaticDataReportPartA) || //
(x.message() instanceof AisBStaticDataReportPartB)).groupBy(//
x -> ((HasMmsi) x.message()).getMmsi()).flatMap(o -> //
o.scan(//
new Timed<State>(new State(o.getKey(), null, null, null, null, null), 0L), (timed, x) -> {
State state = timed.object;
String name = state.name;
String callsign = state.callsign;
Integer imo = state.imo;
Integer shipType = state.shipType;
AisClass aisClass = state.aisClass;
if (x.message() instanceof AisShipStaticA) {
AisShipStaticA a = (AisShipStaticA) x.message();
callsign = a.getCallsign();
name = a.getName();
imo = a.getImo().orElse(null);
shipType = a.getShipType();
aisClass = AisClass.A;
} else if (x.message() instanceof AisPositionBExtended) {
AisPositionBExtended a = (AisPositionBExtended) x.message();
name = a.getName();
shipType = a.getShipType();
aisClass = AisClass.B;
} else if (x.message() instanceof AisBStaticDataReportPartA) {
AisBStaticDataReportPartA a = (AisBStaticDataReportPartA) x.message();
if (a.getName().isPresent()) {
name = a.getName().get();
}
aisClass = AisClass.B;
} else if (x.message() instanceof AisBStaticDataReportPartB) {
aisClass = AisClass.B;
}
return new Timed<State>(new State(o.getKey(), aisClass, name, callsign, imo, shipType), x.time());
}).skip(//
1).distinctUntilChanged(ts -> ts.object)).doOnNext(x -> {
State y = x.object;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
String line = //
y.mmsi + "," + sdf.format(new Date(x.time)) + //
"," + y.aisClass + //
"," + integer(y.imo) + //
"," + integer(y.shipType) + //
"," + string(y.name) + ",";
out.println(line);
}).subscribe();
}
}
Aggregations