NOTICE: This version of the NSF Unidata web site (archive.unidata.ucar.edu) is no longer being updated.
Current content can be found at unidata.ucar.edu.

To learn about what's going on, see About the Archive Site.

Re: Searching for working example...

Hoeven, Maarten van der wrote:

Hi all,

I'm using the Java2-lib to create Netcdf-files. I got it working to the
extend that I can create the file (dimensions, variables) and write a
lot of data to it. All in one go. This is working perfectly.

However, I want to walk a slightly different road. That is, to create
only once an empty netcdf-file (only variables and dimensions stored in
it, hence the structure), and after that to write data to it every day.
To put in other words, to update the netcdf-file with daily data.

This must be possible, because I saw some Java1-lib working examples.
However, it must be working a bit different in version 2 of the
Java-lib, because I run into problems. The problems are that I am
messing around with the origin array; I cant get this working.

Like:

ncfile.write("tn", origin, ArrayAbstract.factory(tnArr));

where origin is an int[1]-array.

I understand I have to use origin, to make clear at what position in the
file the new data must be written. But I must be overlooking
something...


Does someone has a working Java-source for me (capable to use with
Netcdf for Java Version 2 (!)), in which an existing netcdf-file is
updated with new data?

Thanks,
Maarten

+-------------------------------
| Maarten van der Hoeven
| KNMI, De Bilt, The Netherlands
| +31-30-2206 402
| maarten.van.der.hoeven@xxxxxxx
+-------------------------------
--------------------------------------------------------------
Zie ook/see also: http://www.knmi.nl/maildisclaimer.html
attached is an example of creating a file and then later writing to it. let me know if this is adequate.
package ucar.nc2;

import junit.framework.*;

import ucar.ma2.*;
import ucar.nc2.*;
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 TestCreate extends TestCase  {

  static String fileName = TestAll.topDir+"example.nc"; // default name of file 
created

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

  public void testCreate() {
    NetcdfFileWriteable ncfile = new NetcdfFileWriteable();
    ncfile.setName(fileName);

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

    // 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", int.class, dim3);
    ncfile.addVariableAttribute("rh", "long_name", "relative humidity");
    ncfile.addVariableAttribute("rh", "units", "percent");

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

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

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

    // int time(time) ;
    //   time:units = "hours" ;
    ncfile.addVariable("time", int.class, 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);
    }
    //System.out.println( "ncfile = "+ ncfile);

    // 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());
    int i,j,k;
    Index ima = rhA.getIndex();
    // write
    for (i=0; i<2; i++)
      for (j=0; j<latDim.getLength(); j++)
        for (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);
    }

    /* Here's an ArrayAbstract 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", ArrayAbstract.factory(tData));
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    }

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

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

    System.out.println( "*****************Create Netcdf done");

  }

  public void testWriteExisting() {
    NetcdfFileWriteable ncfile = null;
    try {
      ncfile = new NetcdfFileWriteable(fileName);
    } catch (IOException e) {
      System.err.println("ERROR opening file "+fileName);
      assert(false);
    }

    // find dimensions
    Dimension latDim = ncfile.findDimension("lat");
    Dimension lonDim = ncfile.findDimension("lon");
    Dimension timeDim = ncfile.findDimension("time");

    // find Variables
    Variable rh = ncfile.findVariable("rh");

    // create the data values
    ArrayInt rhA = new ArrayInt.D3(2, latDim.getLength(), lonDim.getLength());
    Index ima = rhA.getIndex();
    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), (i+1)*(j+1)*(k+1));

    // write rhData out to disk
    int[] origin = new int[3];
    origin[0] = timeDim.getLength(); // starts at the existing length
    try {
      ncfile.write("rh", origin, rhA);
    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    }

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

    System.out.println( " testWriteExisting Netcdf done");
  }

  public void testReadExisting() {
    NetcdfFile ncfile = null;
    try {
      ncfile = new NetcdfFile(fileName);
    } catch (IOException e) {
      System.err.println("ERROR opening file "+fileName);
      assert(false);
    }

    // find dimensions
    Dimension latDim = ncfile.findDimension("lat");
    Dimension lonDim = ncfile.findDimension("lon");
    Dimension timeDim = ncfile.findDimension("time");
    assert timeDim.getLength() == 4;

    // find Variables
    Variable rh = ncfile.findVariable("rh");
    assert rh.getRank() == 3;
    assert rh.getDataType() == DataType.INT;

    try {
      ArrayInt.D3 rhA = (ArrayInt.D3) rh.read();
      Index ima = rhA.getIndex();
      for (int i = 2; i < 4; i++)
        for (int j = 0; j < latDim.getLength(); j++)
          for (int k = 0; k < lonDim.getLength(); k++)
            assert rhA.getInt(ima.set(i, j, k)) ==  (i - 1) * (j + 1) * (k + 1);

    } catch (IOException e) {
      System.err.println("ERROR writing file");
      assert(false);
    }

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

    System.out.println( " testReadExisting Netcdf done");
  }



}
  • 2003 messages navigation, sorted by:
    1. Thread
    2. Subject
    3. Author
    4. Date
    5. ↑ Table Of Contents
  • Search the netcdf-java archives: