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(); }
@Test public void testFloatingPointCompare() throws Exception { String spec = TestDir.cdmUnitTestDir + "ft/fmrc/fp_precision/sediment_thickness_#yyMMddHHmm#.*\\.nc$"; System.out.printf("%n====================FMRC dataset %s%n", spec); Formatter errlog = new Formatter(); Fmrc fmrc = Fmrc.open(spec, errlog); assert (fmrc != null) : errlog; try (ucar.nc2.dt.GridDataset gridDs = fmrc.getDatasetBest()) { GridDatatype v = gridDs.findGridByShortName("thickness_of_sediment"); assert v != null; GridCoordSystem gcs = v.getCoordinateSystem(); CoordinateAxis1DTime time = gcs.getTimeAxis1D(); Assert.assertEquals("hours since 2015-03-08 12:51:00.000 UTC", time.getUnitsString()); Assert.assertEquals(74, time.getSize()); Array data = time.read(); System.out.printf("%s%n", NCdumpW.toString(data)); for (CalendarDate cd : time.getCalendarDates()) { assert cd.getFieldValue(CalendarPeriod.Field.Minute) == 0 : System.out.printf("%s%n", cd); } } }
private void showDeclaration(BeanTableSorted from, boolean isNcml) { Variable v = getCurrentVariable(from); if (v == null) return; infoTA.clear(); if (isNcml) { Formatter out = new Formatter(); try { NCdumpW.writeNcMLVariable(v, out); } catch (IOException e) { e.printStackTrace(); } infoTA.appendLine(out.toString()); } else { infoTA.appendLine(v.toString()); } if (Debug.isSet("Xdeveloper")) { infoTA.appendLine("\n"); infoTA.appendLine("FULL NAME = " + v.getFullName()); infoTA.appendLine("\n"); infoTA.appendLine(v.toStringDebug()); } infoTA.gotoTop(); infoWindow.setTitle("Variable Info"); infoWindow.show(); }
/* 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 String getValue() { Array value = att.getValues(); try { return NCdumpW.printArray(value, null, null); } catch (IOException e) { return e.getMessage(); } }
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()); } }
String ncdumpmetadata(NetcdfDataset ncfile) throws Exception { StringWriter sw = new StringWriter(); // Print the meta-databuffer using these args to NcdumpW try { if (!ucar.nc2.NCdumpW.print(ncfile, "-unsigned", sw, null)) throw new Exception("NcdumpW failed"); } catch (IOException ioe) { throw new Exception("NcdumpW failed", ioe); } sw.close(); return sw.toString(); }
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()); } }
String ncdumpdata(NetcdfDataset ncfile) throws Exception { StringWriter sw = new StringWriter(); // Dump the databuffer sw = new StringWriter(); try { if (!ucar.nc2.NCdumpW.print(ncfile, "-vall -unsigned", sw, null)) throw new Exception("NCdumpW failed"); } catch (IOException ioe) { ioe.printStackTrace(); throw new Exception("NCdumpW failed", ioe); } sw.close(); return sw.toString(); }
// test offset only gets applied once public void testWrapOnce() throws IOException { String filename = TestAll.cdmUnitTestDir + "ncml/coords/testCoordScaling.ncml"; System.out.printf("%s%n", filename); NetcdfDataset ncd = ucar.nc2.dataset.NetcdfDataset.openDataset(filename); Variable v = ncd.findCoordinateAxis("Longitude"); assert v != null; assert v instanceof CoordinateAxis1D; // if offset is applied twice, the result is not in +-180 range Array data = v.read(); NCdumpW.printArray(data); IndexIterator ii = data.getIndexIterator(); while (ii.hasNext()) { assert Math.abs(ii.getDoubleNext()) < 180 : ii.getDoubleCurrent(); } }
@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); }