cdm/src/main/java/ucar/nc2/NCdumpW.java
public static boolean print(NetcdfFile nc, Writer out, WantValues showValues, boolean ncml, boolean strict,
          String varNames, ucar.nc2.util.CancelTask ct) throws IOException {
    boolean headerOnly = (showValues == WantValues.none) && (varNames == null);

    try {
      if (ncml)
        writeNcML(nc, out, showValues, null); // output schema in NcML
      else if (headerOnly)
        nc.writeCDL(new PrintWriter(out), strict); // output schema in CDL form (like ncdump)
      else {
        PrintWriter ps = new PrintWriter(out);
        nc.toStringStart(ps, strict);

        Indent indent = new Indent(2);
        indent.incr();
        ps.printf("%n%sdata:%n", indent);
        indent.incr();

        if (showValues == WantValues.all) { // dump all data
          for (Variable v : nc.getVariables()) {
            printArray(v.read(), v.getFullName(), ps, indent, ct);
            if (ct != null && ct.isCancel()) return false;
          }
        } else if (showValues == WantValues.coordsOnly) { // dump coordVars
          for (Variable v : nc.getVariables()) {
            if (v.isCoordinateVariable())
              printArray(v.read(), v.getFullName(), ps, indent, ct);
            if (ct != null && ct.isCancel()) return false;
          }
        }

        if ((showValues != WantValues.all) && (varNames != null)) { // dump the list of variables
          StringTokenizer stoke = new StringTokenizer(varNames, ";");
          while (stoke.hasMoreTokens()) {
            String varSubset = stoke.nextToken(); // variable name and optionally a subset

            if (varSubset.indexOf('(') >= 0) { // has a selector
              Array data = nc.readSection(varSubset);
              printArray(data, varSubset, ps, indent, ct);

            } else {   // do entire variable
              Variable v = nc.findVariable(varSubset);
              if (v == null) {
                ps.print(" cant find variable: " + varSubset + "\n   " + usage);
                continue;
              }
              // dont print coord vars if they are already printed
              if ((showValues != WantValues.coordsOnly) || v.isCoordinateVariable())
                printArray(v.read(), v.getFullName(), ps, indent, ct);
            }
            if (ct != null && ct.isCancel()) return false;
          }
        }

        indent.decr();
        indent.decr();
        nc.toStringEnd(ps);
      }

    } catch (Exception e) {
      e.printStackTrace();
      out.write(e.getMessage());
      out.flush();
      return false;
    }

    out.flush();
    return true;
  }
public static boolean print(NetcdfFile nc, Writer out, WantValues showValues, boolean ncml, boolean strict,
          String varNames, ucar.nc2.util.CancelTask ct) throws IOException {
    boolean headerOnly = (showValues == WantValues.none) && (varNames == null);

    try {
      if (ncml)
        writeNcML(nc, out, showValues, null); // output schema in NcML
      else if (headerOnly)
        nc.writeCDL(new PrintWriter(out), strict); // output schema in CDL form (like ncdump)
      else {
        PrintWriter ps = new PrintWriter(out);
        nc.toStringStart(ps, strict);

        Indent indent = new Indent(2);
        indent.incr();
        ps.printf("%n%sdata:%n", indent);
        indent.incr();

        if (showValues == WantValues.all) { // dump all data
          for (Variable v : nc.getVariables()) {
            printArray(v.read(), v.getFullName(), ps, indent, ct);
            if (ct != null && ct.isCancel()) return false;
          }
        } else if (showValues == WantValues.coordsOnly) { // dump coordVars
          for (Variable v : nc.getVariables()) {
            if (v.isCoordinateVariable())
              printArray(v.read(), v.getFullName(), ps, indent, ct);
            if (ct != null && ct.isCancel()) return false;
          }
        }

        if ((showValues != WantValues.all) && (varNames != null)) { // dump the list of variables
          StringTokenizer stoke = new StringTokenizer(varNames, ";");
          while (stoke.hasMoreTokens()) {
            String varSubset = stoke.nextToken(); // variable name and optionally a subset

            if (varSubset.indexOf('(') >= 0) { // has a selector
              Array data = nc.readSection(varSubset);
              printArray(data, varSubset, ps, indent, ct);

            } else {   // do entire variable
              Variable v = nc.findVariable(varSubset);
              if (v == null) {
                ps.print(" cant find variable: " + varSubset + "\n   " + usage);
                continue;
              }
              // dont print coord vars if they are already printed
              if ((showValues != WantValues.coordsOnly) || v.isCoordinateVariable())
                printArray(v.read(), v.getFullName(), ps, indent, ct);
            }
            if (ct != null && ct.isCancel()) return false;
          }
        }

        indent.decr();
        indent.decr();
        nc.toStringEnd(ps);
      }

    } catch (Exception e) {
      e.printStackTrace();
      out.write(e.getMessage());
      out.flush();
      return false;
    }

    out.flush();
    return true;
  }
ncdump-like print of netcdf file.

@param nc         already opened NetcdfFile
@param out        print to this stream
@param showValues do you want the variable values printed?
@param ncml       print NcML representation (other arguments are ignored)
@param strict     print strict CDL representation
@param varNames   semicolon delimited list of variables whose data should be printed. May have
Fortran90 like selector: eg varName(1:2,*,2)
@param ct         allow task to be cancelled; may be null.
@return true if successful
@throws IOException on write error