Re: 20050815: netCDF Java - I need example code

attacheed are a couple of examples that work
package ucar.nc2;

import junit.framework.*;

import ucar.ma2.*;
import java.io.IOException;

/**
 * Simple example to create a new netCDF file corresponding to the following
 * CDL:
 * <pre>
 *  netcdf example {
 *  dimensions:
 *      lat = 3 ;
 *      lon = 4 ;
 *      time = UNLIMITED ;
 *  variables:
 *      int rh(time, lat, lon) ;
 *              rh:long_name="relative humidity" ;
 *              rh:units = "percent" ;
 *      double T(time, lat, lon) ;
 *              T:long_name="surface temperature" ;
 *              T:units = "degC" ;
 *      float lat(lat) ;
 *              lat:units = "degrees_north" ;
 *      float lon(lon) ;
 *              lon:units = "degrees_east" ;
 *      int time(time) ;
 *              time:units = "hours" ;
 *  // global attributes:
 *              :title = "Example Data" ;
 *  data:
 *   rh
 *     1, 2, 3, 4,
 *     5, 6, 7, 8,
 *     9, 10, 11, 12,
 *     21, 22, 23, 24,
 *     25, 26, 27, 28,
 *     29, 30, 31, 32 ;
 *   T
 *     1, 2, 3, 4,
 *     2, 4, 6, 8,
 *     3, 6, 9, 12,
 *     2.5, 5, 7.5, 10,
 *     5, 10, 15, 20,
 *     7.5, 15, 22.5, 30 ;
 *   lat = 41, 40, 39 ;
 *   lon = -109, -107, -105, -103 ;
 *   time = 6, 18 ;
 *  }
 * </pre>
 *
 * @author : Russ Rew
 * @author : John Caron
 */
public class TestWriteRecord extends TestCase  {

  static String fileName = TestNC2.topDir+"testWriteRecord.nc"; // default name 
of file created
  static boolean dumpAfterCreate = false;

  public TestWriteRecord( String name) {
    super(name);
  }

  public void testNC3WriteWithRecordVariables() throws IOException {
    NetcdfFileWriteable ncfile = new NetcdfFileWriteable(fileName, false);

    // define dimensions, including unlimited
    Dimension latDim = ncfile.addDimension("lat", 3);
    Dimension lonDim = ncfile.addDimension("lon", 4);
    Dimension timeDim = ncfile.addDimension("time", -1, true, true, false);

    // define Variables
    Dimension[] dim3 = new Dimension[3];
    dim3[0] = timeDim;
    dim3[1] = latDim;
    dim3[2] = lonDim;

    // int rh(time, lat, lon) ;
    //    rh:long_name="relative humidity" ;
    //    rh:units = "percent" ;
    ncfile.addVariable("rh", DataType.INT, dim3);
    ncfile.addVariableAttribute("rh", "long_name", "relative humidity");
    ncfile.addVariableAttribute("rh", "units", "percent");

    // test attribute array
    ArrayInt.D1 valid_range = new ArrayInt.D1(2);
    valid_range.set(0, 0);
    valid_range.set(1,100);
    ncfile.addVariableAttribute("rh", "range", valid_range);

    ncfile.addVariableAttribute( "rh", "valid_range", Array.factory(new  
double[] {0d, 100d}) ); 

    // double T(time, lat, lon) ;
    //   T:long_name="surface temperature" ;
    //   T:units = "degC" ;
    ncfile.addVariable("T", DataType.DOUBLE, dim3);
    ncfile.addVariableAttribute("T", "long_name", "surface temperature");
    ncfile.addVariableAttribute("T", "units", "degC");


    // float lat(lat) ;
    //   lat:units = "degrees_north" ;
    ncfile.addVariable("lat", DataType.FLOAT, new Dimension[] {latDim});
    ncfile.addVariableAttribute("lat", "units", "degrees_north");

    // float lon(lon) ;
    // lon:units = "degrees_east" ;
    ncfile.addVariable("lon", DataType.FLOAT, new Dimension[] {lonDim});
    ncfile.addVariableAttribute("lon", "units", "degrees_east");

    // int time(time) ;
    //   time:units = "hours" ;
    ncfile.addVariable("time", DataType.INT, new Dimension[] {timeDim});
    ncfile.addVariableAttribute("time", "units", "hours");

    //  :title = "Example Data" ;
    ncfile.addGlobalAttribute("title", "Example Data");

    // create the file
    try {
      ncfile.create();
    }  catch (IOException e) {
      System.err.println("ERROR creating file");
      assert(false);
    }
    if (dumpAfterCreate) System.out.println( "ncfile = "+ ncfile);

    Variable v = ncfile.findTopVariable("rh");
    assert v != null;
    assert v.isUnlimited();

    // write the RH data one value at a time to an Array
    int[][][] rhData = {{{ 1,  2,  3,  4}, { 5,  6,  7,  8}, { 9, 10, 11, 12}},
                        {{21, 22, 23, 24}, {25, 26, 27, 28}, {29, 30, 31, 32}}};

    ArrayInt rhA = new ArrayInt.D3(2, latDim.getLength(), lonDim.getLength());
    Index ima = rhA.getIndex();
    // write
    for (int i=0; i<2; i++)
      for (int j=0; j<latDim.getLength(); j++)
        for (int k=0; k<lonDim.getLength(); k++)
          rhA.setInt(ima.set(i,j,k), rhData[i][j][k]);

    // write rhData out to disk
    try {
      ncfile.write("rh", rhA);
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }


    // Here's an Array approach to set the values of T all at once.
    double[][][] tData = {
        {{  1., 2,   3,  4}, {2.,  4,  6,  8}, {  3.,  6,  9,   12}},
        {{2.5, 5, 7.5, 10}, {5., 10, 15, 20}, {7.5, 15, 22.5, 30}}
    };
    try {
      ncfile.write("T", Array.factory(tData));
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }

    // Store the rest of variable values
   try {
     ncfile.write("lat", Array.factory(new float[] {41, 40, 39}));
     ncfile.write("lon", Array.factory(new float[] {-109, -107, -105, -103}));
     ncfile.write("time", Array.factory(new int[] {6, 18}));
   } catch (IOException e) {
     System.err.println("ERROR writing file");
     assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }

    // test reading without closing and reopening
    try {
      /* Get the value of the global attribute named "title" */
      String title = ncfile.findAttValueIgnoreCase( null, "title", "N/A");

      /* Read the latitudes into an array of double.
         This works regardless of the external
         type of the "lat" variable. */
      Variable lat = ncfile.findVariable("lat");
      assert(lat.getRank() == 1);       // make sure it's 1-dimensional
      int nlats = lat.getShape()[0]; // number of latitudes
      double [] lats = new double[nlats];       // where to put them

      Array values = lat.read(); // read all into memory
      ima = values.getIndex(); // index array to specify which value
      for (int ilat = 0; ilat < nlats; ilat++) {
        lats[ilat] = values.getDouble(ima.set0(ilat));
      }
      /* Read units attribute of lat variable */
      String latUnits = ncfile.findAttValueIgnoreCase( lat, "units", "N/A");
      assert( latUnits.equals("degrees_north"));

      /* Read the longitudes. */
      Variable lon = ncfile.findVariable("lon");
      values = lon.read();
      assert( values instanceof ArrayFloat.D1);
      ArrayFloat.D1 fa = (ArrayFloat.D1) values;
      assert ( TestAll.closeEnough(fa.get(0), -109.0f)) : fa.get(0);
      assert ( TestAll.closeEnough(fa.get(1), -107.0f)) : fa.get(1);
      assert ( TestAll.closeEnough(fa.get(2), -105.0f)) : fa.get(2);
      assert ( TestAll.closeEnough(fa.get(3), -103.0f)) : fa.get(3);

      /* Now we can just use the MultiArray to access values, or
         we can copy the MultiArray elements to another array with
         toArray(), or we can get access to the MultiArray storage
         without copying.  Each of these approaches to accessing
         the data are illustrated below. */

      /* Whats the time dimensin length ? */
      Dimension td = ncfile.findDimension("time");
      assert td.getLength() == 2;

      /* Read the times: unlimited dimension */
      Variable time = ncfile.findVariable("time");
      Array timeValues = time.read();
      assert( timeValues instanceof ArrayInt.D1);
      ArrayInt.D1 ta = (ArrayInt.D1) timeValues;
      assert ( ta.get(0) ==  6) : ta.get(0);
      assert ( ta.get(1) ==  18) : ta.get(1);

      /* Read the relative humidity data */
      Variable rh = ncfile.findVariable("rh");
      Array rhValues = rh.read();
      assert( rhValues instanceof ArrayInt.D3);
      ArrayInt.D3 rha = (ArrayInt.D3) rhValues;
      int[] shape = rha.getShape();
      for (int i=0; i<shape[0]; i++) {
       for (int j=0; j<shape[1]; j++) {
        for (int k=0; k<shape[2]; k++) {
          int want = 20*i + 4*j + k+1;
          int val = rha.get(i, j, k);
          //System.out.println(" "+i+" "+j+" "+k+" "+want+" "+val);
          assert ( want == val) : val;
        }
       }
      }

      /* Read the temperature data */
      Variable t = ncfile.findVariable("T");
      Array tValues = t.read();
      assert( tValues instanceof ArrayDouble.D3);
      ArrayDouble.D3 Ta = (ArrayDouble.D3) tValues;
      assert TestAll.closeEnough( Ta.get(0,0,0), 1.0f) : Ta.get(0, 0, 0);
      assert TestAll.closeEnough( Ta.get(1,1,1), 10.0f) : Ta.get(1, 1, 1);

      /* Read subset of the temperature data */
      tValues = t.read(new int[3], new int[] {2,2,2});
      assert( tValues instanceof ArrayDouble.D3);
      Ta = (ArrayDouble.D3) tValues;
      assert TestAll.closeEnough( Ta.get(0,0,0), 1.0f) : Ta.get(0, 0, 0);
      assert TestAll.closeEnough( Ta.get(1,1,1), 10.0f) : Ta.get(1, 1, 1);

    } catch (InvalidRangeException e) {
        e.printStackTrace();
     } catch (java.io.IOException e) {
        e.printStackTrace();
    }



    // all done
    try {
      ncfile.flush();
      ncfile.close();
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    }


    System.out.println( "**** TestWriteRecord done");

  }

  // make an example  writing records
  public void testNC3WriteWithRecord() throws IOException {
    NetcdfFileWriteable ncfile = new 
NetcdfFileWriteable("C:/temp/writeRecordExample.nc", false);

    // define dimensions, including unlimited
    Dimension latDim = ncfile.addDimension("lat", 64);
    Dimension lonDim = ncfile.addDimension("lon", 128);
    Dimension timeDim = ncfile.addDimension("time", -1, true, true, false);

    // define Variables
    Dimension[] dim3 = new Dimension[3];
    dim3[0] = timeDim;
    dim3[1] = latDim;
    dim3[2] = lonDim;

    // double T(time, lat, lon) ;
    //   T:long_name="surface temperature" ;
    //   T:units = "degC" ;
    ncfile.addVariable("T", DataType.DOUBLE, dim3);
    ncfile.addVariableAttribute("T", "long_name", "surface temperature");
    ncfile.addVariableAttribute("T", "units", "degC");


    // float lat(lat) ;
    //   lat:units = "degrees_north" ;
    ncfile.addVariable("lat", DataType.FLOAT, new Dimension[] {latDim});
    ncfile.addVariableAttribute("lat", "units", "degrees_north");

    // float lon(lon) ;
    // lon:units = "degrees_east" ;
    ncfile.addVariable("lon", DataType.FLOAT, new Dimension[] {lonDim});
    ncfile.addVariableAttribute("lon", "units", "degrees_east");

    // int time(time) ;
    //   time:units = "hours" ;
    ncfile.addVariable("time", DataType.INT, new Dimension[] {timeDim});
    ncfile.addVariableAttribute("time", "units", "hours");

    //  :title = "Example Data" ;
    ncfile.addGlobalAttribute("title", "Example Data");

    // create the file
    try {
      ncfile.create();
    }  catch (IOException e) {
      System.err.println("ERROR creating file");
      e.printStackTrace();
      return;
    }
    System.out.println( "ncfile = "+ ncfile);

    // now write one record at a time
    Variable v = ncfile.findTopVariable("T");
    ArrayDouble data = new ArrayDouble.D3(1, latDim.getLength(), 
lonDim.getLength());
    ArrayInt timeData = new ArrayInt.D1(1);
    int[] origin = new int[v.getRank()];
    int[] timeOrigin = new int[1];

    for (int time=0; time<100; time++) {
      // fill the data array
      Index ima = data.getIndex();
      for (int j=0; j<latDim.getLength(); j++)
        for (int k=0; k<lonDim.getLength(); k++)
          data.setDouble(ima.set(0,j,k), (double) time*j*k);
      timeData.setInt( timeData.getIndex(), time);

      // write to file
      origin[0] = time;
      timeOrigin[0] = time;
      try {
        ncfile.write("T", origin, data);
        ncfile.write("time", timeOrigin, timeData);
      } catch (IOException e) {
        System.err.println("ERROR writing file");
      } catch (InvalidRangeException e) {
        e.printStackTrace();
      }
    }

    // all done
    try {
      ncfile.close();
    } catch (IOException e) {
      System.err.println("ERROR writing file");
    }

    System.out.println( "**** TestWriteRecord done");
  }


}
package ucar.nc2;

import junit.framework.*;
import ucar.ma2.*;

import java.io.*;
import java.util.*;

/** Test nc2 write JUnit framework. */

public class TestWrite extends TestCase {
  private boolean show = false;

  public TestWrite( String name) {
    super(name);
  }

  public void testNC3Write() throws IOException {
    String filename = TestNC2.topDir+"testWrite.nc";
    NetcdfFileWriteable ncfile = new NetcdfFileWriteable(filename, false);

    // define dimensions
    Dimension latDim = ncfile.addDimension("lat", 64);
    Dimension lonDim = ncfile.addDimension("lon", 128);

    // define Variables
    ArrayList dims = new ArrayList();
    dims.add( latDim);
    dims.add( lonDim);

    ncfile.addVariable("temperature", DataType.DOUBLE, dims);
    ncfile.addVariableAttribute("temperature", "units", "K");

    int[] attValue = new int[3];
    attValue[0] = 1;
    attValue[1] = 2;
    attValue[2] = 3;
    Array data = Array.factory( int.class, new int [] {3}, attValue);
    ncfile.addVariableAttribute("temperature", "scale", data);
    ncfile.addVariableAttribute("temperature", "versionD", new Double(1.2));
    ncfile.addVariableAttribute("temperature", "versionF", new Float(1.2));
    ncfile.addVariableAttribute("temperature", "versionI", new Integer(1));
    ncfile.addVariableAttribute("temperature", "versionS", new Short((short)2));
    ncfile.addVariableAttribute("temperature", "versionB", new Byte((byte)3));

    ncfile.addVariableAttribute("temperature", "versionString", "1.2");

    // add string-valued variables
    Dimension svar_len = ncfile.addDimension("svar_len", 80);
    dims = new ArrayList();
    dims.add( svar_len);
    ncfile.addVariable("svar", DataType.CHAR, dims);
    ncfile.addVariable("svar2", DataType.CHAR, dims);

    // string array
    Dimension names = ncfile.addDimension("names", 3);
    ArrayList dima = new ArrayList();
    dima.add(names);
    dima.add(svar_len);

    ncfile.addVariable("names", DataType.CHAR, dima);
    ncfile.addVariable("names2", DataType.CHAR, dima);

    // add global attributes
    ncfile.addGlobalAttribute("yo", "face");
    ncfile.addGlobalAttribute("versionD", new Double(1.2));
    ncfile.addGlobalAttribute("versionF", new Float(1.2));
    ncfile.addGlobalAttribute("versionI", new Integer(1));
    ncfile.addGlobalAttribute("versionS", new Short((short)2));
    ncfile.addGlobalAttribute("versionB", new Byte((byte)3));

      // test some errors
    try {
      Array bad = Array.factory( ArrayList.class, new int[] {1});
      ncfile.addGlobalAttribute("versionC", bad);
      assert(false);
    }  catch (IllegalArgumentException e) {
      assert(true);
    }

    // create the file
    try {
      ncfile.create();
    }  catch (IOException e) {
      System.err.println("ERROR creating file "+ncfile.getLocation()+"\n"+e);
      assert(false);
    }

    // write some data
    ArrayDouble A = new ArrayDouble.D2(latDim.getLength(), lonDim.getLength());
    int i,j;
    Index ima = A.getIndex();
    // write
    for (i=0; i<latDim.getLength(); i++) {
      for (j=0; j<lonDim.getLength(); j++) {
        A.setDouble(ima.set(i,j), (double) (i*1000000+j*1000));
      }
    }

    int[] origin = new int[2];
    try {
      ncfile.write("temperature", origin, A);
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }


    // write char variable
    int[] origin1 = new int[1];
    ArrayChar ac = new ArrayChar.D1(svar_len.getLength());
    ima = ac.getIndex();
    String val = "Testing 1-2-3";
    for (j=0; j<val.length(); j++)
      ac.setChar(ima.set(j), val.charAt(j));

    try {
      ncfile.write("svar", origin1, ac);
    } catch (IOException e) {
      System.err.println("ERROR writing Achar");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }


    // write char variable as String
    try {
      ArrayChar ac2 = new ArrayChar.D1(svar_len.getLength());
      ac2.setString( "Two pairs of ladies stockings!");
      ncfile.write("svar2", origin1, ac2);
    } catch (IOException e) {
      System.err.println("ERROR writing Achar2");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }


    // write String array
    try {
      ArrayChar ac2 = new ArrayChar.D2(names.getLength(), svar_len.getLength());
      ima = ac2.getIndex();
      ac2.setString( ima.set(0), "No pairs of ladies stockings!");
      ac2.setString( ima.set(1), "One pair of ladies stockings!");
      ac2.setString( ima.set(2), "Two pairs of ladies stockings!");
      ncfile.write("names", origin, ac2);
    } catch (IOException e) {
      System.err.println("ERROR writing Achar3");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }


    // write String array
    try {
      ArrayChar ac2 = new ArrayChar.D2(names.getLength(), svar_len.getLength());
      ima = ac2.getIndex();
      ac2.setString( 0, "0 pairs of ladies stockings!");
      ac2.setString( 1, "1 pair of ladies stockings!");
      ac2.setString( 2, "2 pairs of ladies stockings!");
      ncfile.write("names2", origin, ac2);
    } catch (IOException e) {
      System.err.println("ERROR writing Achar4");
      assert(false);
    } catch (InvalidRangeException e) {
      e.printStackTrace();
      assert(false);
    }

    try {
      ncfile.flush();
    } catch (IOException e) {
      e.printStackTrace();
      assert(false);
    }

    if (show) System.out.println( "ncfile = "+ ncfile);



    //////////////////////////////////////////////////////////////////////
    // test reading without closing the file

        // read entire array
    Variable temp = ncfile.findVariable("temperature");
    assert (null != temp);

    Array tA = temp.read();
    assert (tA.getRank() == 2);

    ima = tA.getIndex();
    int[] shape = tA.getShape();

    for (i=0; i<shape[0]; i++) {
      for (j=0; j<shape[1]; j++) {
        assert( tA.getDouble(ima.set(i,j)) == (double) (i*1000000+j*1000));
      }
    }

    // read part of array
    int[] origin2 = new int[2];
    int[] shape2 = new int[2];
    shape2[0] = 1;
    shape2[1] = temp.getShape()[1];
    try {
      tA = temp.read(origin2, shape2);
    } catch (InvalidRangeException e) {
      System.err.println("ERROR reading file " +e);
      assert(false);
      return;
    } catch (IOException e) {
      System.err.println("ERROR reading file");
      assert(false);
      return;
    }
    assert (tA.getRank() == 2);

    for (j=0; j<shape2[1]; j++) {
      assert( tA.getDouble(ima.set(0,j)) == (double) (j*1000));
    }

    // rank reduction
    Array Areduce = tA.reduce();
    Index ima2 = Areduce.getIndex();
    assert (Areduce.getRank() == 1);

    for (j=0; j<shape2[1]; j++) {
      assert( Areduce.getDouble(ima2.set(j)) == (double) (j*1000));
    }

    // read char variable
    Variable c = null;
    assert(null != (c = ncfile.findVariable("svar")));
    try {
      tA = c.read();
    } catch (IOException e) {
      assert(false);
    }
    assert(tA instanceof ArrayChar);
    ArrayChar achar = (ArrayChar) tA;
    String sval = achar.getString(ac.getIndex());
    assert sval.equals("Testing 1-2-3") : sval;
    //System.out.println( "val = "+ val);

    // read char variable 2
    Variable c2 = null;
    assert(null != (c2 = ncfile.findVariable("svar2")));
    try {
      tA = c2.read();
    } catch (IOException e) {
      assert(false);
    }
    assert(tA instanceof ArrayChar);
    ArrayChar ac2 = (ArrayChar) tA;
    assert(ac2.getString().equals("Two pairs of ladies stockings!"));

    // read String Array
    Variable c3 = null;
    assert(null != (c3 = ncfile.findVariable("names")));
    try {
      tA = c3.read();
    } catch (IOException e) {
      assert(false);
    }
    assert(tA instanceof ArrayChar);
    ArrayChar ac3 = (ArrayChar) tA;
    ima = ac3.getIndex();

    assert(ac3.getString(ima.set(0)).equals("No pairs of ladies stockings!"));
    assert(ac3.getString(ima.set(1)).equals("One pair of ladies stockings!"));
    assert(ac3.getString(ima.set(2)).equals("Two pairs of ladies stockings!"));

    // read String Array - 2
    Variable c4 = null;
    assert(null != (c4 = ncfile.findVariable("names2")));
    try {
      tA = c4.read();
    } catch (IOException e) {
      assert(false);
    }
    assert(tA instanceof ArrayChar);
    ArrayChar ac4 = (ArrayChar) tA;
    ima = ac4.getIndex();

    assert(ac4.getString(0).equals("0 pairs of ladies stockings!"));
    assert(ac4.getString(1).equals("1 pair of ladies stockings!"));
    assert(ac4.getString(2).equals("2 pairs of ladies stockings!"));

    /////////////////////////////////////////////////////////////////////
    // all done
    try {
      ncfile.close();
    } catch (IOException e) {
      e.printStackTrace();
      assert(false);
    }

    System.out.println( "*****************Test Write done on "+filename);
  }

}