ucar.nc2.ft.point.remote.PointStream Maven / Gradle / Ivy
The newest version!
/*
* Copyright (c) 1998-2018 John Caron and University Corporation for Atmospheric Research/Unidata
* See LICENSE for license information.
*/
package ucar.nc2.ft.point.remote;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import javax.annotation.Nonnull;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import ucar.ma2.ArrayStructureBB;
import ucar.ma2.DataType;
import ucar.ma2.StructureData;
import ucar.ma2.StructureDataDeep;
import ucar.ma2.StructureMembers;
import ucar.nc2.ft.DsgFeatureCollection;
import ucar.nc2.ft.PointFeature;
import ucar.nc2.ft.PointFeatureCollection;
import ucar.nc2.ft.PointFeatureIterator;
import ucar.nc2.ft.point.PointFeatureImpl;
import ucar.nc2.stream.NcStream;
import ucar.nc2.stream.NcStreamProto;
import ucar.nc2.time.CalendarDateUnit;
import ucar.unidata.geoloc.EarthLocation;
import ucar.unidata.geoloc.Station;
/**
* Defines the point stream format, along with pointStream.proto.
*
* cd c:/dev/github/thredds/cdm/src/main/java
* protoc --proto_path=. --java_out=. ucar/nc2/ft/point/remote/pointStream.proto
*
* @author caron
* @since Feb 16, 2009
*/
public class PointStream {
public enum MessageType {
Start, Header, Data, End, Error, Eos, StationList, PointFeatureCollection, PointFeature
}
private static final byte[] MAGIC_StationList = {(byte) 0xfe, (byte) 0xfe, (byte) 0xef, (byte) 0xef};
private static final byte[] MAGIC_PointFeatureCollection = {(byte) 0xfa, (byte) 0xfa, (byte) 0xaf, (byte) 0xaf};
private static final byte[] MAGIC_PointFeature = {(byte) 0xf0, (byte) 0xf0, (byte) 0x0f, (byte) 0x0f};
private static final boolean debug = false;
public static MessageType readMagic(InputStream is) throws IOException {
byte[] b = new byte[4];
int done = NcStream.readFully(is, b);
if (done != 4)
return MessageType.Eos;
if (test(b, MAGIC_PointFeature))
return MessageType.PointFeature;
if (test(b, MAGIC_PointFeatureCollection))
return MessageType.PointFeatureCollection;
if (test(b, MAGIC_StationList))
return MessageType.StationList;
if (test(b, NcStream.MAGIC_START))
return MessageType.Start;
if (test(b, NcStream.MAGIC_HEADER))
return MessageType.Header;
if (test(b, NcStream.MAGIC_DATA))
return MessageType.Data;
if (test(b, NcStream.MAGIC_END))
return MessageType.End;
if (test(b, NcStream.MAGIC_ERR))
return MessageType.Error;
return null;
}
public static int writeMagic(OutputStream out, MessageType type) throws IOException {
switch (type) {
case PointFeature:
return NcStream.writeBytes(out, PointStream.MAGIC_PointFeature);
case PointFeatureCollection:
return NcStream.writeBytes(out, PointStream.MAGIC_PointFeatureCollection);
case StationList:
return NcStream.writeBytes(out, PointStream.MAGIC_StationList);
case Start:
return NcStream.writeBytes(out, NcStream.MAGIC_START);
case End:
return NcStream.writeBytes(out, NcStream.MAGIC_END);
case Error:
return NcStream.writeBytes(out, NcStream.MAGIC_ERR);
}
return 0;
}
private static boolean test(byte[] b, byte[] m) {
if (b.length != m.length)
return false;
for (int i = 0; i < b.length; i++)
if (b[i] != m[i])
return false;
return true;
}
public static PointStreamProto.PointFeatureCollection encodePointFeatureCollection(String name, String timeUnitString,
String altUnits, PointFeature pf) throws IOException {
PointStreamProto.PointFeatureCollection.Builder builder = PointStreamProto.PointFeatureCollection.newBuilder();
builder.setName(name);
builder.setTimeUnit(timeUnitString);
if (altUnits != null) {
builder.setAltUnit(altUnits);
}
StructureData sdata = pf.getDataAll();
StructureMembers sm = sdata.getStructureMembers();
for (StructureMembers.Member m : sm.getMembers()) {
PointStreamProto.PointFeatureMember.Builder mbuilder = PointStreamProto.PointFeatureMember.newBuilder();
mbuilder.setName(m.getName());
if (null != m.getDescription())
mbuilder.setDesc(m.getDescription());
if (null != m.getUnitsString())
mbuilder.setUnits(m.getUnitsString());
mbuilder.setDataType(NcStream.convertDataType(m.getDataType()));
mbuilder.setSection(NcStream.encodeSection(new ucar.ma2.Section(m.getShape())));
builder.addMembers(mbuilder);
}
return builder.build();
}
public static PointStreamProto.PointFeature encodePointFeature(PointFeature pf) throws IOException {
PointStreamProto.Location.Builder locBuilder = PointStreamProto.Location.newBuilder();
locBuilder.setTime(pf.getObservationTime());
locBuilder.setNomTime(pf.getNominalTime());
EarthLocation loc = pf.getLocation();
locBuilder.setLat(loc.getLatitude());
locBuilder.setLon(loc.getLongitude());
locBuilder.setAlt(loc.getAltitude());
PointStreamProto.PointFeature.Builder builder = PointStreamProto.PointFeature.newBuilder();
builder.setLoc(locBuilder);
StructureData sdata = pf.getDataAll();
ArrayStructureBB abb = StructureDataDeep.copyToArrayBB(sdata);
ByteBuffer bb = abb.getByteBuffer();
if (debug) {
StructureMembers sm = sdata.getStructureMembers();
int size = sm.getStructureSize();
System.out.printf("encodePointFeature size= %d bb=%d%n", size, bb.position());
}
builder.setData(ByteString.copyFrom(bb.array()));
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy