Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright 2009-2012 University Corporation for Atmospheric Research/Unidata
*
* Portions of this software were developed by the Unidata Program at the
* University Corporation for Atmospheric Research.
*
* Access and use of this software shall impose the following obligations
* and understandings on the user. The user is granted the right, without
* any fee or cost, to use, copy, modify, alter, enhance and distribute
* this software, and any derivative works thereof, and its supporting
* documentation for any purpose whatsoever, provided that this entire
* notice appears in all copies of the software, derivative works and
* supporting documentation. Further, UCAR requests that the user credit
* UCAR/Unidata in any publications that result from the use of this
* software or in any product that includes this software. The names UCAR
* and/or Unidata, however, may not be used in any advertising or publicity
* to endorse or promote any products or commercial entity unless specific
* written permission is obtained from UCAR/Unidata. The user also
* understands that UCAR/Unidata is not obligated to provide the user with
* any support, consulting, training or assistance of any kind with regard
* to the use, operation and performance of this software nor to provide
* the user with any updates, revisions, new versions or "bug fixes."
*
* THIS SOFTWARE IS PROVIDED BY UCAR/UNIDATA "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL UCAR/UNIDATA BE LIABLE FOR ANY SPECIAL,
* INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
* WITH THE ACCESS, USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package ucar.nc2.stream;
import ucar.nc2.*;
import ucar.ma2.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.channels.WritableByteChannel;
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import ucar.nc2.constants.CDM;
import ucar.nc2.iosp.IospHelper;
import ucar.unidata.io.RandomAccessFile;
/**
* Defines the ncstream format, along with ncStream.proto.
*
* To regenerate ncStreamProto.java from ncStream.proto:
* cd c:/dev/tds4.2/thredds/cdm/src/main/java
* protoc --proto_path=. --java_out=. ucar/nc2/stream/ncStream.proto
*
*
* @see "http://www.unidata.ucar.edu/software/netcdf-java/stream/NcStream.html"
* @see "http://www.unidata.ucar.edu/software/netcdf-java/stream/NcstreamGrammer.html"
*/
public class NcStream {
// must start with this "CDFS"
static public final byte[] MAGIC_START = new byte[]{0x43, 0x44, 0x46, 0x53};
static public final byte[] MAGIC_HEADER = new byte[]{(byte) 0xad, (byte) 0xec, (byte) 0xce, (byte) 0xda};
static public final byte[] MAGIC_DATA = new byte[]{(byte) 0xab, (byte) 0xec, (byte) 0xce, (byte) 0xba};
static public final byte[] MAGIC_VDATA = new byte[]{(byte) 0xab, (byte) 0xef, (byte) 0xfe, (byte) 0xba};
static public final byte[] MAGIC_VEND = new byte[]{(byte) 0xed, (byte) 0xef, (byte) 0xfe, (byte) 0xda};
static public final byte[] MAGIC_ERR = new byte[]{(byte) 0xab, (byte) 0xad, (byte) 0xba, (byte) 0xda};
static public final byte[] MAGIC_END = new byte[]{(byte) 0xed, (byte) 0xed, (byte) 0xde, (byte) 0xde};
static NcStreamProto.Group.Builder encodeGroup(Group g, int sizeToCache) throws IOException {
NcStreamProto.Group.Builder groupBuilder = NcStreamProto.Group.newBuilder();
groupBuilder.setName(g.getShortName());
for (Dimension dim : g.getDimensions())
groupBuilder.addDims(NcStream.encodeDim(dim));
for (Attribute att : g.getAttributes())
groupBuilder.addAtts(NcStream.encodeAtt(att));
for (EnumTypedef enumType : g.getEnumTypedefs())
groupBuilder.addEnumTypes(NcStream.encodeEnumTypedef(enumType));
for (Variable var : g.getVariables()) {
if (var instanceof Structure)
groupBuilder.addStructs(NcStream.encodeStructure((Structure) var));
else
groupBuilder.addVars(NcStream.encodeVar(var, sizeToCache));
}
for (Group ng : g.getGroups())
groupBuilder.addGroups(encodeGroup(ng, sizeToCache));
return groupBuilder;
}
static NcStreamProto.Attribute.Builder encodeAtt(Attribute att) {
NcStreamProto.Attribute.Builder attBuilder = NcStreamProto.Attribute.newBuilder();
attBuilder.setName(att.getShortName());
attBuilder.setType(encodeAttributeType(att.getDataType()));
attBuilder.setLen(att.getLength());
// values
if (att.getLength() > 0) {
if (att.isString()) {
for (int i = 0; i < att.getLength(); i++)
attBuilder.addSdata(att.getStringValue(i));
} else {
Array data = att.getValues();
ByteBuffer bb = data.getDataAsByteBuffer();
attBuilder.setData(ByteString.copyFrom(bb.array()));
}
}
return attBuilder;
}
static NcStreamProto.Dimension.Builder encodeDim(Dimension dim) {
NcStreamProto.Dimension.Builder dimBuilder = NcStreamProto.Dimension.newBuilder();
if (dim.getShortName() != null)
dimBuilder.setName(dim.getShortName());
dimBuilder.setLength(dim.getLength());
if (!dim.isShared())
dimBuilder.setIsPrivate(true);
if (dim.isVariableLength())
dimBuilder.setIsVlen(true);
if (dim.isUnlimited()) dimBuilder.setIsUnlimited(true);
return dimBuilder;
}
static NcStreamProto.EnumTypedef.Builder encodeEnumTypedef(EnumTypedef enumType) throws IOException {
NcStreamProto.EnumTypedef.Builder builder = NcStreamProto.EnumTypedef.newBuilder();
builder.setName(enumType.getShortName());
Map map = enumType.getMap();
NcStreamProto.EnumTypedef.EnumType.Builder b2 = NcStreamProto.EnumTypedef.EnumType.newBuilder();
for (int code : map.keySet()) {
b2.clear();
b2.setCode(code);
b2.setValue(map.get(code));
builder.addMap(b2);
}
return builder;
}
static NcStreamProto.Variable.Builder encodeVar(Variable var, int sizeToCache) throws IOException {
NcStreamProto.Variable.Builder builder = NcStreamProto.Variable.newBuilder();
builder.setName(var.getShortName());
builder.setDataType(encodeDataType(var.getDataType()));
if (var.isUnsigned())
builder.setUnsigned(true);
if (var.getDataType().isEnum()) {
EnumTypedef enumType = var.getEnumTypedef();
if (enumType != null)
builder.setEnumType(enumType.getShortName());
}
for (Dimension dim : var.getDimensions()) {
builder.addShape(encodeDim(dim));
}
for (Attribute att : var.getAttributes()) {
builder.addAtts(encodeAtt(att));
}
// put small amounts of data in header "immediate mode"
if (var.isCaching() && var.getDataType().isNumeric()) {
if (var.isCoordinateVariable() || var.getSize() * var.getElementSize() < sizeToCache) {
Array data = var.read();
ByteBuffer bb = data.getDataAsByteBuffer();
builder.setData(ByteString.copyFrom(bb.array()));
}
}
return builder;
}
static NcStreamProto.Structure.Builder encodeStructure(Structure s) throws IOException {
NcStreamProto.Structure.Builder builder = NcStreamProto.Structure.newBuilder();
builder.setName(s.getShortName());
builder.setDataType(encodeDataType(s.getDataType()));
for (Dimension dim : s.getDimensions())
builder.addShape(encodeDim(dim));
for (Attribute att : s.getAttributes())
builder.addAtts(encodeAtt(att));
for (Variable v : s.getVariables()) {
if (v instanceof Structure)
builder.addStructs(NcStream.encodeStructure((Structure) v));
else
builder.addVars(NcStream.encodeVar(v, -1));
}
return builder;
}
static public NcStreamProto.Error encodeErrorMessage(String message) {
NcStreamProto.Error.Builder builder = NcStreamProto.Error.newBuilder();
builder.setMessage(message);
return builder.build();
}
static NcStreamProto.Data encodeDataProto(Variable var, Section section, boolean deflate, int uncompressedLength) {
NcStreamProto.Data.Builder builder = NcStreamProto.Data.newBuilder();
builder.setVarName(var.getFullNameEscaped());
builder.setDataType(encodeDataType(var.getDataType()));
builder.setSection(encodeSection(section));
if (deflate) {
builder.setCompress(NcStreamProto.Compress.DEFLATE);
builder.setUncompressedSize(uncompressedLength);
}
if (var.isVariableLength()) builder.setVdata(true);
builder.setVersion(2);
return builder.build();
}
static public NcStreamProto.Section encodeSection(Section section) {
NcStreamProto.Section.Builder sbuilder = NcStreamProto.Section.newBuilder();
for (Range r : section.getRanges()) {
NcStreamProto.Range.Builder rbuilder = NcStreamProto.Range.newBuilder();
if (r.first() != 0) rbuilder.setStart(r.first());
rbuilder.setSize(r.length());
if (r.stride() != 1) rbuilder.setStride(r.stride());
sbuilder.addRange(rbuilder);
}
return sbuilder.build();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
public static long encodeArrayStructure(ArrayStructure as, OutputStream os) throws java.io.IOException {
long size = 0;
ArrayStructureBB dataBB = StructureDataDeep.copyToArrayBB(as, null); // LOOK what should the bo be ??
List ss = new ArrayList();
List