[netCdfJava #FDZ-419409]: write problems

Hi Santi:

I think you want

origin[] = {100*steps,0,0}

One thing to double check is the order of your dimensions must match the origin 
array.

Attached is an example.



Ticket Details
==================
Ticket ID: FDZ-419409
Department: Support netCDF Java
Priority: Normal
Status: Open
package ucar.nc2;

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

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

/** Test nc2 write JUnit framework. */

public class TestWriteInSections extends TestCase {

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

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

    // define dimensions
    Dimension timeDim = ncfile.addDimension("time", 200, true, false, false); 
// try with and without isUnlimited = true
    Dimension latDim = ncfile.addDimension("lat", 64);
    Dimension lonDim = ncfile.addDimension("lon", 99);

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

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

    // create the file
    ncfile.create();

    // write some data
    ArrayDouble A = new ArrayDouble.D3(10, latDim.getLength(), 
lonDim.getLength());
    Index ima = A.getIndex();
    int[] origin = new int[3];

    int ntimes_per_step = 10;
    int nsteps = timeDim.getLength() / ntimes_per_step;

    for (int step=0; step<nsteps; step++) {

      // fill array with fake data
      for (int t=0; t<ntimes_per_step; t++) {
       for (int i=0; i<latDim.getLength(); i++) {
        for (int j=0; j<lonDim.getLength(); j++) {
          int time = step * ntimes_per_step + t;
          A.setDouble(ima.set(t,i,j), (double) (time*10000+i*100+j));
        }
       }
      }

      // write it to disk
      origin[0] = step * ntimes_per_step;
      ncfile.write("temperature", origin, A);
    }



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

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

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

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

    for (int t=0; t<shape[0]; t++) {
     for (int i=0; i<shape[1]; i++) {
      for (int j=0; j<shape[2]; j++) {
        assert( tA.getDouble(ima.set(t, i,j)) == (double) (t*10000+i*100+j));
      }
     }
    }


    //////////////////////////////////////////////////////////////////////
    // test reading without reopening the file
    ncfile.close();

    NetcdfFile ncfile2 = NetcdfFile.open(filename);

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

    tA = temp.read();
    assert (tA.getRank() == 3);

    ima = tA.getIndex();
    shape = tA.getShape();

    for (int t=0; t<shape[0]; t++) {
     for (int i=0; i<shape[1]; i++) {
      for (int j=0; j<shape[2]; j++) {
        assert( tA.getDouble(ima.set(t, i,j)) == (double) (t*10000+i*100+j));
      }
     }
    }

    ncfile2.close();

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

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