From the command line:
java -Xmx512m -classpath netdfAll-<version>.jar ucar.nc2.dataset.NetcdfDataset -in <fileIn> -out <fileOut> [-isLargeFile]Use NcML to modify the original dataset:
You can wrap the original dataset in NcML. The NcML file must have the suffix ".ncml", and it becomes the "in" file:
java -Xmx512m -classpath netcdfAll-<version>.jar ucar.nc2.dataset.NetcdfDataset -in myfile.ncml -out out.ncFor example use the <remove> element to remove variables that you don't want to save:<?xml version="1.0" encoding="UTF-8"?>
<netcdf xmlns="http://www.unidata.ucar.edu/namespaces/netcdf/ncml-2.2" location="/data/file.grib2">
<remove name="bad1" type="variable" />
<remove name="bad2" type="variable" />
</netcdf>From a Java program:
public boolean writeToNetdf3(String datasetIn, String datasetOut, boolean isLargeFile) throws IOException { NetcdfFile ncfileIn = ucar.nc2.dataset.NetcdfDataset.openFile(datasetIn, null); System.out.println("Read from " + datasetIn + " write to " + datasetOut); NetcdfFile ncfileOut = ucar.nc2.FileWriter.writeToFile(ncfileIn, datasetOut, false, -1, isLargeFile); ncfileIn.close(); ncfileOut.close(); System.out.println("NetcdfFile successfully written = " + ncfileOut); }
Notes:
A Sequence is a variable length array of Structures. The only thing you can do with them is to iterate over them. Heres an example with a nested sequence:
public void showNestedSequence(String filename, String outerSeq, String innerSeq) throws IOException { PrintWriter pw = new PrintWriter(System.out); NetcdfFile ncfile = null; 1)try { ncfile = NetcdfFile.open(filename); Variable v = ncfile.findVariable(outerSeq); assert v != null; assert v instanceof Sequence; Sequence record = (Sequence) v; Array data = v.read(); assert data instanceof ArraySequence; ArraySequence as = (ArraySequence) data; // outer sequence iteration StructureDataIterator iter = as.getStructureDataIterator(); 2) try { while (iter.hasNext()) { StructureData sdata = iter.next(); ArraySequence nested = sdata.getArraySequence(innerSeq); // inner sequence iteration StructureDataIterator nestedIter = nested.getStructureDataIterator(); 3) try { while (nestedIter.hasNext()) { StructureData nestedData = nestedIter.next(); NCdumpW.printStructureData(pw, nestedData); // process inner sequencce System.out.printf("%n"); } 3) } finally { nestedIter.finish(); } } 2) } finally { iter.finish(); } 1)} finally { if (ncfile != null) ncfile.close(); } }
Notes: