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: writing netcdf file along unlimited dimension

The NetcdfFileWriteable.write() signature says:

 /** Write data to the named variable. Must not be in define mode.
  * @param varName name of variable. IllegalArgumentException if variable name 
does not exist.
  * @param origin offset within the variable to start writing.
  * @param values write this array; must be same type and rank as Variable
  * @throws IOException
  */
 public void write(String varName, int [] origin, Array values) throws 
java.io.IOException, InvalidRangeException.

So the problem is it expects that the values array has same rank as variable. 
If it doesnt, i dont think its possible (in general, not in the scalar case) to 
figure out how to write the data. so i will leave it.




Ethan Davis wrote:
I just answered a netcdf-java question about not having to read all data before writing netcdf. I put together some sample code to send them. My first stab was using Arrays with rank 0 but got some out of bounds problems. It does fine if I use an Array of rank 1. Looks like with a rank 0 array the NetcdfFileWriteable.write() calls the N3iosp.writeData() with a sectionList that is empty which then does a get(0) on an empty list.

Probably a pretty quick fix but I haven't thought about it enough to take a stab at it.

Here's my code:

 public void testOne()
 {

   String fileName = "testWriteAlongUnlimited.nc";
NetcdfFileWriteable ncfile = NetcdfFileWriteable.createNew( fileName, true ); Dimension timeDim = new Dimension( "time", Dimension.UNLIMITED.getLength(), true, true, false );
   ncfile.addDimension( null, timeDim );
   Dimension [] dims = { timeDim};
   ncfile.addVariable( "time", DataType.INT, dims );
   ncfile.addVariable( "temp", DataType.FLOAT, dims );
   ncfile.addVariable( "pres", DataType.FLOAT, dims );

   try
   {
     ncfile.create();
   }
   catch ( IOException e )
   {
assertTrue( "Failed to create file <" + fileName + ">: " + e.getMessage(),
                 false);
   }

   ArrayInt.D0 timeArray = new ArrayInt.D0();
   ArrayFloat.D0 tempArray = new ArrayFloat.D0();
   ArrayFloat.D0 presArray = new ArrayFloat.D0();
   for ( int i = 0; i < 10; i++ )
   {
     int[] origin = { i+1};
     timeArray.set( i);
     tempArray.set( i);
     presArray.set( i);
     try
     {
       ncfile.write( "time", origin, timeArray);
       ncfile.write( "temp", origin, tempArray);
       ncfile.write( "pres", origin, presArray);
     }
     catch ( IOException e )
     {
assertTrue( "Failed to write file <" + fileName + "> at index= " + i + ": " + e.getMessage(),
                   false );
     }
     catch ( InvalidRangeException e )
     {
assertTrue( "Bad range writing file <" + fileName + "> at index= " + i + ": " + e.getMessage(),
                   false );
     }
   }
   try
   {
     ncfile.flush();
     ncfile.close();
   }
   catch ( IOException e )
   {
assertTrue( "Failed to flush/close file <" + fileName + ">: " + e.getMessage(),
                 false );
   }
 }





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