Netcdf-Java Cookbook

Write a dataset to a netCDF-3 format file:

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
For example use the <remove> element to remove variables that you don't want to save:
<?xml version="1.0" encoding="UTF-8"?>
<netcdf xmlns="" location="/data/file.grib2">
<remove name="bad1" type="variable" />
<remove name="bad2" type="variable" />

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);
    System.out.println("NetcdfFile successfully written = " + ncfileOut);


Reading from Sequences

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 =;
   Variable v = ncfile.findVariable(outerSeq);
   assert v != null;
   assert v instanceof Sequence;

   Sequence record = (Sequence) v;

   Array data =;
   assert data instanceof ArraySequence;
   ArraySequence as = (ArraySequence) data;

   // outer sequence iteration
   StructureDataIterator iter = as.getStructureDataIterator();
2) try {
     while (iter.hasNext()) {
       StructureData sdata =;
       ArraySequence nested = sdata.getArraySequence(innerSeq);
       // inner sequence iteration
       StructureDataIterator nestedIter = nested.getStructureDataIterator();
3)     try {
         while (nestedIter.hasNext()) {
           StructureData nestedData =;
           NCdumpW.printStructureData(pw, nestedData); // process inner sequencce
3)     } finally {
2) } finally {
1)} finally {
   if (ncfile != null) ncfile.close();


  1. Make sure that the file gets closed
  2. Make sure finish() is called on the outer iterator
  3. Make sure finish() is called on the inner iterator

This document was last updated May 2013