/** * Create an ArrayChar from an ArrayObject of Strings. * * @param values ArrayObject of String * @return equivilent ArrayChar, using maximum length of String. Unused are zero filled. */ public static ArrayChar makeFromStringArray(ArrayObject values) { // find longest string IndexIterator ii = values.getIndexIterator(); int strlen = 0; while (ii.hasNext()) { String s = (String) ii.next(); strlen = Math.max(s.length(), strlen); } return makeFromStringArray(values, strlen); }
/** * Create an ArrayChar from an ArrayObject of Strings. * * @param values ArrayObject of String * @return equivilent ArrayChar, using maximum length of String. Unused are zero filled. */ public static ArrayChar makeFromStringArray(ArrayObject values) { // find longest string IndexIterator ii = values.getIndexIterator(); int strlen = 0; while (ii.hasNext()) { String s = (String) ii.next(); strlen = Math.max(s.length(), strlen); } return makeFromStringArray(values, strlen); }
/** * Create an ArrayChar from an ArrayObject of Strings. * * @param values ArrayObject of String * @return equivilent ArrayChar, using maximum length of String. Unused are zero filled. */ public static ArrayChar makeFromStringArray(ArrayObject values) { // find longest string IndexIterator ii = values.getIndexIterator(); int strlen = 0; while (ii.hasNext()) { String s = (String) ii.next(); strlen = Math.max(s.length(), strlen); } return makeFromStringArray(values, strlen); }
static private void printVariableArray(PrintWriter out, ArrayObject array, Indent indent, CancelTask ct) { // throws IOException { out.print("\n" + indent + "{"); indent.incr(); IndexIterator iter = array.getIndexIterator(); boolean first = true; while (iter.hasNext()) { Array data = (Array) iter.next(); if(!first) { out.print(", "); } printArray(data, out, indent, ct); first = false; } indent.decr(); out.print("\n" + indent + "}"); }
static private void printStructureDataArray(PrintStream out, Array array, Indent indent, ucar.nc2.util.CancelTask ct) { //int saveIndent = ilev.getIndentLevel(); for (IndexIterator ii = array.getIndexIterator(); ii.hasNext(); ) { StructureData sdata = (StructureData) ii.next(); out.println("\n" + indent + "{"); printStructureData( out, sdata, indent, ct); //ilev.setIndentLevel(saveIndent); out.print(indent+ "} "+sdata.getName()+"("+ii+")"); if (ct != null && ct.isCancel()) return; } }
static private void printVariableArray(PrintWriter out, ArrayObject array, Indent indent, CancelTask ct) { // throws IOException { out.println("\n" + indent + "{"); indent.incr(); IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { Array data = (Array) iter.next(); printArray(data, out, indent, ct); } indent.decr(); out.print(indent + "}"); }
static private void printStructureDataArray(PrintStream out, Array array, Indent indent, ucar.nc2.util.CancelTask ct) { //int saveIndent = ilev.getIndentLevel(); for (IndexIterator ii = array.getIndexIterator(); ii.hasNext(); ) { StructureData sdata = (StructureData) ii.next(); out.println("\n" + indent + "{"); printStructureData( out, sdata, indent, ct); //ilev.setIndentLevel(saveIndent); out.print(indent+ "} "+sdata.getName()+"("+ii+")"); if (ct != null && ct.isCancel()) return; } }
static private void printVariableArray(PrintWriter out, ArrayObject array, Indent indent, CancelTask ct) { // throws IOException { out.println("\n" + indent + "{"); indent.incr(); IndexIterator iter = array.getIndexIterator(); while (iter.hasNext()) { Array data = (Array) iter.next(); printArray(data, out, indent, ct); } indent.decr(); out.print(indent + "}"); }
/** * An implementation of {@link Object#hashCode} that is consistent with {@link #equals(Array, Array)}. * * @param array an array to hash. * @return a hash code value for the array. */ // TODO: Should we add this to Array as the Object.hashCode() implementation? How much work is that? public static int hashCode(Array array) { if (array == null) { return 0; } int hash = 3; hash = 29 * hash + array.getDataType().hashCode(); hash = 29 * hash + Arrays.hashCode(array.getShape()); // We can't simply hash array.getStorage(), because array may be a "view" that doesn't include all of the // elements in the backing store. for (IndexIterator iter = array.getIndexIterator(); iter.hasNext(); ) { hash = 29 * hash + iter.next().hashCode(); hash = 29 * hash + Arrays.hashCode(iter.getCurrentCounter()); } return hash; } }
static private void writeNcMLValues(Variable v, Formatter out, Indent indent) throws IOException { Array data = v.read(); int width = formatValues(indent + "<values>", out, 0, indent); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) width = formatValues(ii.next() + " ", out, width, indent); formatValues("</values>\n", out, width, indent); }
void encodeVlenData(NcStreamProto.DataRow.Builder builder, Section section, Array data) { if (!(data instanceof ArrayObject)) throw new IllegalStateException("Unknown class for OPAQUE =" + data.getClass().getName()); IndexIterator iter = data.getIndexIterator(); int count = 0; while (iter.hasNext()) { Array varray = (Array) iter.next(); int vlensize = (int) varray.getSize(); builder.addVlens(vlensize); count += vlensize; } builder.setNelems(count); Section ssection = section.removeLast(); builder.setSection(NcStream.encodeSection(ssection)); assert section.computeSize() == count; int nbytes = count * data.getDataType().getSize(); ByteBuffer bb = ByteBuffer.allocate(nbytes); // copyArrayToBB(data, bb); iter = data.getIndexIterator(); while (iter.hasNext()) { Array varray = (Array) iter.next(); copyArrayToBB(varray, bb); } }
static private void writeNcMLValues(Variable v, PrintStream out, Indent indent) throws IOException { Array data = v.read(); int width = formatValues(indent+"<values>", out, 0, indent); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) width = formatValues(ii.next()+" ", out, width, indent); formatValues("</values>\n", out, width, indent); }
static private void writeNcMLValues(Variable v, Formatter out, Indent indent) throws IOException { Array data = v.read(); int width = formatValues(indent + "<values>", out, 0, indent); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) width = formatValues(ii.next() + " ", out, width, indent); formatValues("</values>\n", out, width, indent); }
static private void writeNcMLValues(Variable v, PrintStream out, Indent indent) throws IOException { Array data = v.read(); int width = formatValues(indent+"<values>", out, 0, indent); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) width = formatValues(ii.next()+" ", out, width, indent); formatValues("</values>\n", out, width, indent); }
private void testArrayObjectIter(Array a, int expected) { int count = 0; IndexIterator iter = a.getIndexIterator(); // fast iterator while (iter.hasNext()) { Object result = iter.next(); count++; } assert (count == expected); Index index = a.getIndex(); IndexIterator iter2 = index.getSlowIndexIterator(a); // slow iterator count = 0; while (iter2.hasNext()) { Object result = iter2.next(); count++; } assert (count == expected); } }
@Test public void testReadArrayOfStructs() throws IOException, InvalidRangeException { Variable v = dodsfile.findVariable("types"); assert v != null; assert v instanceof DODSStructure; assert v.getRank() == 1; assert v.getDataType() == DataType.STRUCTURE; DODSStructure struct = (DODSStructure) v; Array data = struct.read(); assert data.getRank() == 1; assert data.getElementType().equals(StructureData.class); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) { Object d = ii.next(); assert d instanceof StructureData : d.getClass().getName(); } }
@Test public void testReadArrayOfStructs() throws IOException, InvalidRangeException { Variable v = dodsfile.findVariable("types"); assert v != null; assert v instanceof DODSStructure; assert v.getRank() == 1; assert v.getDataType() == DataType.STRUCTURE; DODSStructure struct = (DODSStructure) v; Array data = struct.read(); assert data.getRank() == 1; assert data.getElementType().equals(StructureData.class); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) { Object d = ii.next(); assert d instanceof StructureData : d.getClass().getName(); } }
private void testVlenEndian(String filename, int n) throws IOException { try (NetcdfFile ncfile = NetcdfDataset.openFile(filename, null)) { Variable v = ncfile.findVariable("levels"); assert (null != v); assert (v.getDataType() == DataType.UINT); assert (v.getRank() == 2); assert (v.getShape()[0] == n) : v.getShape()[0]; try { Array data = v.read(); // assert(data.getElementType() instanceof ucar.ma2.ArrayInt.class) : data.getElementType(); assert (data instanceof ArrayObject); IndexIterator iter = data.getIndexIterator(); while (iter.hasNext()) { Array inner = (Array) iter.next(); assert (inner instanceof ArrayInt.D1); int firstVal = inner.getInt(0); System.out.printf("%d (%d) = %s%n", firstVal, inner.getSize(), inner); assert (firstVal < Short.MAX_VALUE) : firstVal; } } catch (IOException e) { assert false; } } }
@org.junit.Test public void testReadSequence() throws IOException { DODSNetcdfFile dodsfile = TestDODSRead.open("test.07"); Variable v = dodsfile.findVariable("person"); assert null != v; assert v instanceof Structure; assert v instanceof DODSStructure; assert v.getRank() == 1; Array a = v.read(); assert a.getRank() == 1; assert a.getSize() == 5 : a.getSize(); //NCdump.printArray(a, "person",System.out,null); int count = 0; int fib = 1, prev = 1; IndexIterator iter = a.getIndexIterator(); while (iter.hasNext()) { StructureData data = (StructureData) iter.next(); assert data.findMember("name") != null; String name = data.getScalarString("name"); assert name != null; assert name.equals("This is a data test string (pass "+count+").") : name; assert data.findMember("age") != null; int agev = data.getScalarInt("age"); assert agev == fib : fib +"!="+agev; count++; int hold = fib; fib += prev; prev = hold; } }
@org.junit.Test public void testNestedSequenceParent() throws IOException { DODSNetcdfFile dodsfile = TestDODSRead.open("NestedSeq"); Variable v = dodsfile.findVariable("person1"); assert null != v; assert v instanceof Structure; assert v instanceof DODSStructure; assert v.getRank() == 1; assert v.isVariableLength(); Array a = v.read(); // DODSNetcdfFile.readFromServer = <person1> assert a.getRank() == 1; assert a.getSize() == 5 : a.getSize(); //NCdump.printArray(a, "person",System.out,null); int count = 0; int fib = 1, prev = 1; IndexIterator iter = a.getIndexIterator(); while (iter.hasNext()) { StructureData data = (StructureData) iter.next(); StructureMembers.Member stuff = data.findMember("stuff"); assert stuff != null; assert data.findMember("age") != null; int agev = data.getScalarInt("age"); assert agev == fib : fib +"!="+agev; count++; int hold = fib; fib += prev; prev = hold; } }