public boolean compareVariables(NetcdfFile org, NetcdfFile copy) { f.format("Original = %s%n", org.getLocation()); f.format("CompareTo= %s%n", copy.getLocation()); boolean ok = true; for (Variable orgV : org.getVariables()) { if (orgV.isCoordinateVariable()) continue; Variable copyVar = copy.findVariable(orgV.getShortName()); if (copyVar == null) { f.format(" MISSING '%s' in 2nd file%n", orgV.getFullName()); ok = false; } else { List<Dimension> dims1 = orgV.getDimensions(); List<Dimension> dims2 = copyVar.getDimensions(); if (!compare(dims1, dims2)) { f.format(" %s != %s%n", orgV.getNameAndDimensions(), copyVar.getNameAndDimensions()); } else { // f.format(" ok %s%n", orgV.getName()); } } } f.format("%n"); for (Variable orgV : copy.getVariables()) { if (orgV.isCoordinateVariable()) continue; Variable copyVar = org.findVariable(orgV.getShortName()); if (copyVar == null) { f.format(" MISSING '%s' in 1st file%n", orgV.getFullName()); ok = false; } } return ok; }
private float testReadScalar(Variable v) throws IOException { if (show) System.out.printf(" read %s%n", v.getNameAndDimensions()); assert (null != v); Array a = v.read(); assert (null != a); IndexIterator ii = a.getIndexIterator(); return ii.getFloatNext(); }
@Ignore("files not available") @Test public void testCacheTiming() throws IOException, InvalidRangeException { String filename = "file:testCacheTiming.xml"; System.out.printf("%s%n", filename); String cacheDirName = TestDir.temporaryLocalDataDir + "testAggExistingCache/"; System.out.printf("cacheDir=%s%n", cacheDirName); File cacheDir = new File(cacheDirName); FileUtils.deleteDirectory(cacheDir); // from commons-io assert !cacheDir.exists(); DiskCache2 cache = new DiskCache2(cacheDirName, false, 0, 0); cache.setAlwaysUseCache(true); Assert.assertEquals(cache.getRootDirectory(), cacheDirName); assert new File(cache.getRootDirectory()).exists(); Aggregation.setPersistenceCache(cache); AggregationExisting.countCacheUse = 0; long start = System.currentTimeMillis(); try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml2), filename, null)) { System.out.printf("%nTestNcmlAggExisting.open %s%n", filename); Variable time = ncfile.findVariable("time"); System.out.printf(" Variable %s%n", time.getNameAndDimensions()); time.read(); } System.out.printf(" countCacheUse = %d%n", AggregationExisting.countCacheUse); long took = System.currentTimeMillis() - start; System.out.printf(" first took %d msecs%n", took); AggregationExisting.countCacheUse = 0; start = System.currentTimeMillis(); try (NetcdfFile ncfile = NcMLReader.readNcML(new StringReader(ncml2), filename, null)) { System.out.printf("%nTestNcmlAggExisting.open %s%n", filename); Variable time = ncfile.findVariable("time"); System.out.printf(" Variable %s%n", time.getNameAndDimensions()); time.read(); } System.out.printf(" countCacheUse = %d%n", AggregationExisting.countCacheUse); took = System.currentTimeMillis() - start; System.out.printf(" second took %d msecs%n", took); }
// convert to shared dimensions private void setSharedDimensions( Variable v, List<Element> values, List<Dimension> unknownDims, String location) { if (values.size() == 0) return; // remove the "scalar" dumbension Iterator<Element> iter = values.iterator(); while (iter.hasNext()) { Element value = iter.next(); String dimName = value.getText().trim(); if (dimName.equalsIgnoreCase("scalar")) iter.remove(); } // gotta have same number of dimensions List<Dimension> oldDims = v.getDimensions(); if (oldDims.size() != values.size()) { log.error("Different number of dimensions for {} {}", v, location); return; } List<Dimension> newDims = new ArrayList<>(); Group group = v.getParentGroup(); for (int i = 0; i < values.size(); i++) { Element value = values.get(i); String dimName = value.getText().trim(); dimName = NetcdfFile.makeValidCdmObjectName(dimName); Dimension dim = group.findDimension(dimName); Dimension oldDim = oldDims.get(i); if (dim == null) dim = checkUnknownDims(dimName, unknownDims, oldDim, location); if (dim == null) { log.error( "Unknown Dimension= {} for variable = {} {} ", dimName, v.getFullName(), location); return; } if (dim.getLength() != oldDim.getLength()) { log.error( "Shared dimension (" + dim.getShortName() + ") has different length than data dimension (" + oldDim.getShortName() + ") shared=" + dim.getLength() + " org=" + oldDim.getLength() + " for " + v + " " + location); return; } newDims.add(dim); } v.setDimensions(newDims); if (showWork) System.out.printf(" set shared dimensions for %s %n", v.getNameAndDimensions()); }
private void testReadData(Variable v) throws IOException { if (show) System.out.printf(" read %s%n", v.getNameAndDimensions()); assert (null != v); assert (null != v.getDimension(0)); Array a = v.read(); assert (null != a); assert (v.getSize() == a.getSize()); }
private void check(NetcdfFile ncfile, int n) throws IOException, InvalidRangeException { Variable v = ncfile.findVariable("time"); assert v != null; System.out.printf(" time= %s%n", v.getNameAndDimensions()); assert v.getSize() == n : v.getSize(); v = ncfile.findVariable("eta"); assert v != null; assert v.getRank() == 3 : v.getRank(); }
private void compareVariableData( Variable var1, Variable var2, boolean showCompare, boolean justOne) throws IOException { Array data1 = var1.read(); Array data2 = var2.read(); if (showCompare) f.format( " compareArrays %s unlimited=%s size=%d%n", var1.getNameAndDimensions(), var1.isUnlimited(), data1.getSize()); compareData(var1.getFullName(), data1, data2, justOne); if (showCompare) f.format(" ok%n"); }