Java Code Examples for ucar.ma2.DataType#ENUM2
The following examples show how to use
ucar.ma2.DataType#ENUM2 .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CDMTypeFcns.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
public static DataType enumTypeFor(DapType type) { switch (type.getTypeSort()) { case Char: case Int8: case UInt8: return DataType.ENUM1; case Int16: case UInt16: return DataType.ENUM2; case Int32: case UInt32: return DataType.ENUM4; case Int64: case UInt64: // Unfortunately, CDM does not support (U)Int64, so truncate to 32. return DataType.ENUM4; case Enum: // Coverity[FB.BC_UNCONFIRMED_CAST] return CDMTypeFcns.enumTypeFor(((DapEnumeration) type).getBaseType()); default: break; } return null; }
Example 2
Source File: H5headerNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 6 votes |
protected Array convertEnums(Map<Integer, String> map, DataType dataType, Array values) { Array result = Array.factory(DataType.STRING, values.getShape()); IndexIterator ii = result.getIndexIterator(); values.resetLocalIterator(); while (values.hasNext()) { int ival; if (dataType == DataType.ENUM1) ival = (int) DataType.unsignedByteToShort(values.nextByte()); else if (dataType == DataType.ENUM2) ival = DataType.unsignedShortToInt(values.nextShort()); else ival = values.nextInt(); String sval = map.get(ival); if (sval == null) sval = "Unknown enum value=" + ival; ii.setObjectNext(sval); } return result; }
Example 3
Source File: CDMTypeFcns.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static DataType daptype2cdmtype(DapType type) { assert (type != null); switch (type.getTypeSort()) { case Char: return DataType.CHAR; case UInt8: return DataType.UBYTE; case Int8: return DataType.BYTE; case Int16: return DataType.SHORT; case UInt16: return DataType.USHORT; case Int32: return DataType.INT; case UInt32: return DataType.UINT; case Int64: return DataType.LONG; case UInt64: return DataType.ULONG; case Float32: return DataType.FLOAT; case Float64: return DataType.DOUBLE; case String: case URL: return DataType.STRING; case Opaque: return DataType.OPAQUE; case Enum: // Coverity[FB.BC_UNCONFIRMED_CAST] DapEnumeration dapenum = (DapEnumeration) type; switch (dapenum.getBaseType().getTypeSort()) { case Char: case UInt8: case Int8: return DataType.ENUM1; case Int16: case UInt16: return DataType.ENUM2; case Int32: case UInt32: return DataType.ENUM4; case Int64: case UInt64: // since there is no ENUM8, use ENUM4 return DataType.ENUM4; default: break; } break; case Structure: return DataType.STRUCTURE; case Sequence: return DataType.SEQUENCE; default: break; } return null; }
Example 4
Source File: NcStream.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
public static DataType convertDataType(ucar.nc2.stream.NcStreamProto.DataType dtype) { switch (dtype) { case CHAR: return DataType.CHAR; case BYTE: return DataType.BYTE; case SHORT: return DataType.SHORT; case INT: return DataType.INT; case LONG: return DataType.LONG; case FLOAT: return DataType.FLOAT; case DOUBLE: return DataType.DOUBLE; case STRING: return DataType.STRING; case STRUCTURE: return DataType.STRUCTURE; case SEQUENCE: return DataType.SEQUENCE; case ENUM1: return DataType.ENUM1; case ENUM2: return DataType.ENUM2; case ENUM4: return DataType.ENUM4; case OPAQUE: return DataType.OPAQUE; case UBYTE: return DataType.UBYTE; case USHORT: return DataType.USHORT; case UINT: return DataType.UINT; case ULONG: return DataType.ULONG; } throw new IllegalStateException("illegal data type " + dtype); }
Example 5
Source File: H5headerNew.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
private TypeInfo calcNCtype(MessageDatatype mdt) { int hdfType = mdt.type; int byteSize = mdt.byteSize; byte[] flags = mdt.flags; // boolean unsigned = mdt.unsigned; TypeInfo tinfo = new TypeInfo(hdfType, byteSize); if (hdfType == 0) { // int, long, short, byte tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned); tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN; tinfo.unsigned = ((flags[0] & 8) == 0); } else if (hdfType == 1) { // floats, doubles tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned); tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN; } else if (hdfType == 2) { // time tinfo.dataType = DataType.STRING; tinfo.endian = ((flags[0] & 1) == 0) ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN; } else if (hdfType == 3) { // fixed length strings map to CHAR. String is used for Vlen type = 1. tinfo.dataType = DataType.CHAR; tinfo.vpad = (flags[0] & 0xf); // when elem length = 1, there is a problem with dimensionality. // eg char cr(2); has a storage_size of [1,1]. } else if (hdfType == 4) { // bit field tinfo.dataType = getNCtype(hdfType, byteSize, mdt.unsigned); } else if (hdfType == 5) { // opaque tinfo.dataType = DataType.OPAQUE; } else if (hdfType == 6) { // structure tinfo.dataType = DataType.STRUCTURE; } else if (hdfType == 7) { // reference tinfo.endian = RandomAccessFile.LITTLE_ENDIAN; tinfo.dataType = DataType.LONG; // file offset of the referenced object // LOOK - should get the object, and change type to whatever it is (?) } else if (hdfType == 8) { // enums if (tinfo.byteSize == 1) tinfo.dataType = DataType.ENUM1; else if (tinfo.byteSize == 2) tinfo.dataType = DataType.ENUM2; else if (tinfo.byteSize == 4) tinfo.dataType = DataType.ENUM4; else { log.warn("Illegal byte suze for enum type = {}", tinfo.byteSize); throw new IllegalStateException("Illegal byte suze for enum type = " + tinfo.byteSize); } // enumMap = mdt.map; } else if (hdfType == 9) { // variable length array tinfo.isVString = mdt.isVString; tinfo.isVlen = mdt.isVlen; if (mdt.isVString) { tinfo.vpad = ((flags[0] >> 4) & 0xf); tinfo.dataType = DataType.STRING; } else { tinfo.dataType = getNCtype(mdt.getBaseType(), mdt.getBaseSize(), mdt.base.unsigned); tinfo.endian = mdt.base.endian; tinfo.unsigned = mdt.base.unsigned; } } else if (hdfType == 10) { // array : used for structure members tinfo.endian = (mdt.getFlags()[0] & 1) == 0 ? RandomAccessFile.LITTLE_ENDIAN : RandomAccessFile.BIG_ENDIAN; if (mdt.isVString()) { tinfo.dataType = DataType.STRING; } else { int basetype = mdt.getBaseType(); tinfo.dataType = getNCtype(basetype, mdt.getBaseSize(), mdt.unsigned); } } else if (warnings) { log.debug("WARNING not handling hdf dataType = " + hdfType + " size= " + byteSize); } if (mdt.base != null) { tinfo.base = calcNCtype(mdt.base); } return tinfo; }
Example 6
Source File: TestNc4IospWriting.java From netcdf-java with BSD 3-Clause "New" or "Revised" License | 4 votes |
@Test public void writeEnumType() throws IOException { // NetcdfFile's 0-arg constructor is protected, so must use NetcdfFileSubclass NetcdfFile ncFile = new NetcdfFileSubclass(); // Create shared, unlimited Dimension Dimension timeDim = new Dimension("time", 3, true, true, false); ncFile.addDimension(null, timeDim); // Create a map from integers to strings. Map<Integer, String> enumMap = new HashMap<>(); enumMap.put(18, "pie"); enumMap.put(268, "donut"); enumMap.put(3284, "cake"); // Create EnumTypedef and add it to root group. EnumTypedef dessertType = new EnumTypedef("dessertType", enumMap, DataType.ENUM2); ncFile.getRootGroup().addEnumeration(dessertType); // Create Variable of type dessertType. Variable dessert = new Variable(ncFile, null, null, "dessert", DataType.ENUM2, "time"); dessert.setEnumTypedef(dessertType); // Add data to dessert variable. short[] dessertStorage = new short[] {18, 268, 3284}; dessert.setCachedData(Array.factory(DataType.SHORT, new int[] {3}, dessertStorage), true); // Add the variable to the root group and finish ncFile ncFile.addVariable(null, dessert); ncFile.finish(); File outFile = File.createTempFile("writeEnumType", ".nc"); try { FileWriter2 writer = new FileWriter2(ncFile, outFile.getAbsolutePath(), NetcdfFileWriter.Version.netcdf4, new Nc4ChunkingStrategyNone()); try (NetcdfFile ncFileOut = writer.write()) { // override name so that CDL will compare equal String org = Ncdump.builder(ncFile).setShowAllValues().setLocationName("writeEnumType").build().print(); String copy = Ncdump.builder(ncFileOut).setShowAllValues().setLocationName("writeEnumType").build().print(); String diffs = UnitTestCommon.compare("TestNc4IospWriting.writeEnumType", org, copy); Assert.assertTrue(diffs, diffs == null); } } finally { ncFile.close(); outFile.delete(); } }