DAPDownloader { void writeNetcdfFile(File targetDir, String fileName, String constraintExpression, final DODSNetcdfFile sourceNetcdfFile, final boolean isLargeFile) throws IOException { final File file = new File(targetDir, fileName); if (file.exists() && !downloadContext.mayOverwrite(fileName)) { downloadContext.notifyFileDownloaded(file); updateProgressBar(fileName, (int) (file.length() / 1024)); return; } if (StringUtils.isNullOrEmpty(constraintExpression)) { try { Thread thread = new Thread(new Runnable() { @Override public void run() { try { FileWriter2 fileWriter = new FileWriter2(sourceNetcdfFile, file.getAbsolutePath(), NetcdfFileWriter.Version.netcdf3, null); fileWriter.write(); } catch (IOException e) { throw new RuntimeException(e); } } }); thread.start(); int downloadedBefore = 0; while (thread.isAlive()) { try { Thread.sleep(1000L); } catch (InterruptedException ignore) { } int downloaded = (int) (file.length() / 1024); int delta = downloaded - downloadedBefore; if (delta > 0) { updateProgressBar(fileName, delta); downloadedBefore = downloaded; } } } catch (RuntimeException e) { if (e.getCause() instanceof IOException) { throw (IOException) e.getCause(); } else { throw e; } } if (!pm.isCanceled()) { downloadContext.notifyFileDownloaded(file); } return; } final List<Variable> variables = sourceNetcdfFile.getVariables(); final List<String> variableNames = new ArrayList<String>(); for (Variable variable : variables) { variableNames.add(variable.getFullName()); } final List<String> filteredVariables = filterVariables(variableNames, constraintExpression); final List<Dimension> filteredDimensions = filterDimensions(filteredVariables, sourceNetcdfFile); NetcdfFileWriter targetNetCDF = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf3, file.getAbsolutePath()); for (Dimension filteredDimension : filteredDimensions) { targetNetCDF.addDimension(null, filteredDimension.getFullName(), filteredDimension.getLength(), filteredDimension.isShared(), filteredDimension.isUnlimited(), filteredDimension.isVariableLength()); } for (String filteredVariable : filteredVariables) { String varName = EscapeStrings.backslashEscape(filteredVariable, NetcdfFile.reservedSectionSpec); final Variable variable = sourceNetcdfFile.findVariable(varName); final Variable targetVariable = targetNetCDF.addVariable(null, variable.getFullName(), variable.getDataType(), variable.getDimensions()); for (Attribute attribute : variable.getAttributes()) { targetVariable.addAttribute(attribute); } } for (Attribute attribute : sourceNetcdfFile.getGlobalAttributes()) { targetNetCDF.addGroupAttribute(null, attribute); } targetNetCDF.create(); for (String filteredVariable : filteredVariables) { String varName = EscapeStrings.backslashEscape(filteredVariable, NetcdfFile.reservedSectionSpec); final Variable sourceVariable = sourceNetcdfFile.findVariable(varName); String ceForVariable = getConstraintExpression(filteredVariable, constraintExpression); final Array values = sourceNetcdfFile.readWithCE(sourceVariable, ceForVariable); final int[] origin = getOrigin(filteredVariable, constraintExpression, sourceVariable.getDimensions().size()); try { targetNetCDF.write(sourceVariable, origin, values); } catch (InvalidRangeException e) { throw new IOException(MessageFormat.format("Unable to download variable ''{0}'' into file ''{1}''.", filteredVariable, fileName), e); } } targetNetCDF.close(); downloadContext.notifyFileDownloaded(file); } DAPDownloader(Map<String, Boolean> dapUris, List<String> fileURIs, DownloadContext downloadContext,
                         DownloadProgressBarPM pm); void saveProducts(File targetDir);  }
@Ignore @Test public void testActualWriting() throws Exception { final DAPDownloader dapDownloader = new DAPDownloader(null, null, new NullDownloadContext(), new NullLabelledProgressBarPM()); final DODSNetcdfFile sourceNetcdfFile = new DODSNetcdfFile( "http: dapDownloader.writeNetcdfFile(TESTDATA_DIR, "deleteme.nc", "", sourceNetcdfFile, false); final File testFile = getTestFile("deleteme.nc"); assertTrue(testFile.exists()); assertTrue(NetcdfFile.canOpen(testFile.getAbsolutePath())); final NetcdfFile netcdfFile = NetcdfFileOpener.open(testFile.getAbsolutePath()); assertNotNull(netcdfFile.findVariable("SST")); }
@Ignore @Test public void testActualWriting_WithConstraint() throws Exception { final DAPDownloader dapDownloader = new DAPDownloader(null, null, new NullDownloadContext(), new NullLabelledProgressBarPM()); final DODSNetcdfFile sourceNetcdfFile = new DODSNetcdfFile( "http: dapDownloader.writeNetcdfFile(TESTDATA_DIR, "deleteme.nc", "COADSX[0:1:4]", sourceNetcdfFile, false); final File testFile = getTestFile("deleteme.nc"); assertTrue(testFile.exists()); assertTrue(NetcdfFile.canOpen(testFile.getAbsolutePath())); final NetcdfFile netcdfFile = NetcdfFileOpener.open(testFile.getAbsolutePath()); assertNull(netcdfFile.findVariable("SST")); assertNotNull(netcdfFile.findVariable("COADSX")); }