public void testEnhanceDefer() throws IOException { NetcdfDataset ncd = NetcdfDataset.openDataset( filename, EnumSet.of(NetcdfDataset.Enhance.ScaleMissing), -1, null, null); VariableDS enhancedVar = (VariableDS) ncd.findVariable("t1"); NetcdfDataset ncdefer = NetcdfDataset.openDataset( filename, EnumSet.of(NetcdfDataset.Enhance.ScaleMissingDefer), -1, null, null); VariableDS deferVar = (VariableDS) ncdefer.findVariable("t1"); Array data = enhancedVar.read(); Array dataDefer = deferVar.read(); System.out.printf("Enhanced="); NCdumpW.printArray(data); System.out.printf("%nDeferred="); NCdumpW.printArray(dataDefer); System.out.printf("%nProcessed="); CompareNetcdf2 nc = new CompareNetcdf2(new Formatter(System.out), false, false, true); assert !nc.compareData(enhancedVar.getShortName(), data, dataDefer, false); IndexIterator ii = dataDefer.getIndexIterator(); while (ii.hasNext()) { double val = deferVar.convertScaleOffsetMissing(ii.getDoubleNext()); ii.setDoubleCurrent(val); } NCdumpW.printArray(dataDefer); assert nc.compareData(enhancedVar.getShortName(), data, dataDefer, false); ncd.close(); ncdefer.close(); }
/** * CDL representation * * @param strict if true, create strict CDL, escaping names * @return CDL representation */ public String toString(boolean strict) { StringBuilder buff = new StringBuilder(); buff.append(strict ? NetcdfFile.escapeNameCDL(getName()) : getName()); if (isString()) { buff.append(" = "); for (int i = 0; i < getLength(); i++) { if (i != 0) buff.append(", "); String val = getStringValue(i); if (val != null) buff.append("\"").append(NCdumpW.encodeString(val)).append("\""); } } else { buff.append(" = "); for (int i = 0; i < getLength(); i++) { if (i != 0) buff.append(", "); buff.append(getNumericValue(i)); if (dataType == DataType.FLOAT) buff.append("f"); else if (dataType == DataType.SHORT) { if (isUnsigned()) buff.append("US"); else buff.append("S"); } else if (dataType == DataType.BYTE) { if (isUnsigned()) buff.append("UB"); else buff.append("B"); } else if (dataType == DataType.LONG) { if (isUnsigned()) buff.append("UL"); else buff.append("L"); } else if (dataType == DataType.INT) { if (isUnsigned()) buff.append("U"); } } } return buff.toString(); }
/* Structure { int a_name; byte b_name(3); byte c_name(3); short d_name(3); int e_name(3); long f_name(3); int g_name(3); short h_name(3); int i_name(3); long j_name(3); float k_name(3); double l_name(3); } CompoundNative(15); type = Layout(8); type= 1 (contiguous) storageSize = (15,144) dataSize=0 dataAddress=2048 */ @Test public void testReadH5StructureArrayMembers() throws java.io.IOException { try (NetcdfFile ncfile = TestH5.openH5("complex/compound_native.h5")) { Variable dset = ncfile.findVariable("CompoundNative"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 15); Dimension d = dset.getDimension(0); assert (d.getLength() == 15); Structure s = (Structure) dset; // read all with the iterator StructureDataIterator iter = s.getStructureIterator(); while (iter.hasNext()) { StructureData sd = iter.next(); for (StructureMembers.Member m : sd.getMembers()) { Array data = sd.getArray(m); NCdumpW.printArray(data, m.getName(), out, null); } } } System.out.println("*** testReadH5StructureArrayMembers ok"); }
public void testStride(String stride) throws IOException, InvalidRangeException { Variable time = ncfile.findVariable("time"); ArrayInt all = (ArrayInt) time.read(); ArrayInt correct = (ArrayInt) all.section(new Section(stride).getRanges()); System.out.printf("correct(%s) %s", stride, NCdumpW.toString(correct)); ArrayInt data = (ArrayInt) time.read(stride); System.out.printf("data(%s) %s%n", stride, NCdumpW.toString(data)); Index ci = correct.getIndex(); Index di = data.getIndex(); for (int i = 0; i < data.getSize(); i++) assert (data.getInt(di.set(i)) == correct.getInt(ci.set(i))) : stride + " index " + i + " = " + data.getInt(di.set(i)) + " != " + correct.getInt(ci.set(i)); }
private void showValueDiffs(CoordinateAxis axis) { if (!axis.isNumeric()) return; try { if (axis instanceof CoordinateAxis1D) { CoordinateAxis1D axis1D = (CoordinateAxis1D) axis; double[] mids = axis1D.getCoordValues(); double[] diffs = new double[mids.length]; for (int i = 0; i < mids.length - 1; i++) diffs[i] = mids[i + 1] - mids[i]; printArrays("midpoint differences", mids, diffs); } else if (axis instanceof CoordinateAxis2D) { CoordinateAxis2D axis2D = (CoordinateAxis2D) axis; ArrayDouble.D2 mids = axis2D.getCoordValuesArray(); int[] shape = mids.getShape(); ArrayDouble.D2 diffx = (ArrayDouble.D2) Array.factory(DataType.DOUBLE, new int[] {shape[0], shape[1] - 1}); for (int j = 0; j < shape[0]; j++) { for (int i = 0; i < shape[1] - 1; i++) { double diff = mids.get(j, i + 1) - mids.get(j, i); diffx.set(j, i, diff); } } infoTA.appendLine(NCdumpW.toString(diffx, "diff in x", null)); ArrayDouble.D2 diffy = (ArrayDouble.D2) Array.factory(DataType.DOUBLE, new int[] {shape[0] - 1, shape[1]}); for (int j = 0; j < shape[0] - 1; j++) { for (int i = 0; i < shape[1]; i++) { double diff = mids.get(j + 1, i) - mids.get(j, i); diffy.set(j, i, diff); } } infoTA.appendLine("\n\n\n"); infoTA.appendLine(NCdumpW.toString(diffy, "diff in y", null)); } } catch (Exception e1) { e1.printStackTrace(); infoTA.appendLine(e1.getMessage()); } }
private void showValues(CoordinateAxis axis) { try { if (axis instanceof CoordinateAxis1D && axis.isNumeric()) { CoordinateAxis1D axis1D = (CoordinateAxis1D) axis; printArray("midpoints=", axis1D.getCoordValues()); if (!axis1D.isInterval()) { printArray("edges=", axis1D.getCoordEdges()); } else { printArray("bound1=", axis1D.getBound1()); printArray("bound2=", axis1D.getBound2()); Formatter f = new Formatter(); double[] mid = axis1D.getCoordValues(); double[] b1 = axis1D.getBound1(); double[] b2 = axis1D.getBound2(); for (int i = 0; i < b1.length; i++) { f.format("%f (%f,%f) = %f%n", mid[i], b1[i], b2[i], b2[i] - b1[i]); } infoTA.appendLine(f.toString()); } } else if (axis instanceof CoordinateAxis2D && axis.isNumeric()) { infoTA.appendLine(NCdumpW.printVariableData(axis, null)); showValues2D((CoordinateAxis2D) axis); } else { infoTA.appendLine(NCdumpW.printVariableData(axis, null)); } } catch (IOException e1) { e1.printStackTrace(); infoTA.appendLine(e1.getMessage()); } }
@Test public void testH5StructureDS() throws java.io.IOException { int a_name = 0; String[] b_name = new String[] { "A fight is a contract that takes two people to honor.", "A combative stance means that you've accepted the contract.", "In which case, you deserve what you get.", " -- Professor Cheng Man-ch'ing" }; String c_name = "Hello!"; // H5header.setDebugFlags(new ucar.nc2.util.DebugFlagsImpl("H5header/header")); try (NetcdfDataset ncfile = NetcdfDataset.openDataset(TestH5.testDir + "complex/compound_complex.h5")) { Variable dset = ncfile.findVariable("CompoundComplex"); assert (null != dset); assert (dset.getDataType() == DataType.STRUCTURE); assert (dset.getRank() == 1); assert (dset.getSize() == 6); Dimension d = dset.getDimension(0); assert (d.getLength() == 6); Structure s = (Structure) dset; // read all with the iterator StructureDataIterator iter = s.getStructureIterator(); while (iter.hasNext()) { StructureData sd = iter.next(); assert sd.getScalarInt("a_name") == a_name; a_name++; assert sd.getScalarString("c_name").equals(c_name); String[] results = sd.getJavaArrayString(sd.findMember("b_name")); assert results.length == b_name.length; int count = 0; for (String r : results) assert r.equals(b_name[count++]); for (StructureMembers.Member m : sd.getMembers()) { Array data = sd.getArray(m); NCdumpW.printArray(data, m.getName(), out, null); } } } System.out.println("*** testH5StructureDS ok"); }
private void showValuesAsDates(CoordinateAxis axis) { String units = axis.getUnitsString(); String cal = getCalendarAttribute(axis); CalendarDateUnit cdu = CalendarDateUnit.of(cal, units); try { infoTA.appendLine(units); infoTA.appendLine(NCdumpW.printVariableData(axis, null)); if (axis.getDataType().isNumeric()) { if (axis instanceof CoordinateAxis2D) { showDates2D((CoordinateAxis2D) axis, cdu); } else if (axis instanceof CoordinateAxis1D) { // 1D showDates1D((CoordinateAxis1D) axis, cdu); } else { // > 2D Array data = axis.read(); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) { double val = ii.getDoubleNext(); infoTA.appendLine(makeCalendarDateStringOrMissing(cdu, val)); } } } else { // must be iso dates Array data = axis.read(); Formatter f = new Formatter(); if (data instanceof ArrayChar) { ArrayChar dataS = (ArrayChar) data; ArrayChar.StringIterator iter = dataS.getStringIterator(); while (iter.hasNext()) f.format(" %s%n", iter.next()); infoTA.appendLine(f.toString()); } else if (data instanceof ArrayObject) { IndexIterator iter = data.getIndexIterator(); while (iter.hasNext()) f.format(" %s%n", iter.next()); infoTA.appendLine(f.toString()); } } } catch (Exception ex) { ex.printStackTrace(); infoTA.appendLine(ex.getMessage()); } }
public String getValue() { Array value = att.getValues(); return NCdumpW.toString(value, null, null); }