Re: 20040322: Java NetCDF Newbie - Write/Read Array Of Strings

  • To: "Nguyen, Cu (NIH/NCI)" <cun@xxxxxxxxxxxx>
  • Subject: Re: 20040322: Java NetCDF Newbie - Write/Read Array Of Strings
  • From: John Caron <caron@xxxxxxxxxxxxxxxx>
  • Date: Mon, 22 Mar 2004 16:59:02 -0700
Unidata Support wrote:

------- Forwarded Message

To: "'support@xxxxxxxxxxxxxxxx'" <support@xxxxxxxxxxxxxxxx>
From: "Nguyen, Cu (NIH/NCI)" <cun@xxxxxxxxxxxx>
Subject: Java NetCDF Newbie - Write/Read Array Of Strings
Organization: NIH
Keywords: 200403221657.i2MGvJrV029768 netCDF Java

Hi All,

        I am a new Java NetCDF user trying to create an array of strings on
NetCDF using some examples that
I found in the NetCDF FAQ. The NetCDF file created successfully but It only
stores the last string in the array.
Please take a look at the following code and let me know what I am doing
wrong. ( I also tried to set Range between 1 and 2 and ArrayChar.D2 without any success).

                nSets=4;
           testName   = new String[nSets];
           testName[0]="name1";
           testName[1]="name2";
           testName[2]="name3";
           testName[3]="name4";

                NetcdfFileWriteable ncFile = new NetcdfFileWriteable();
        ncFile.setName(filename);
                Dimension t1Dim = ncFile.addDimension("example", nSets);

                Dimension[] dim1 = new Dimension[1];
                dim1[0] = t1Dim;

                ncFile.addVariable("testName", char.class, dim1);
        ncFile.create();

// --------------------------------

        ArrayChar ac = new ArrayChar.D1(5);
for(int y = 0; y < nSets; y++) {
                        ac.setString(testName[y]);
                }

                ncFile.write("testName", new int[1],ac);
                /* ncFile.write("testName", ac);*/

        ncFile.close();

Thanks,
CNguyen

--
NOTE: All email exchanges with Unidata User Support are recorded in the
Unidata inquiry tracking system and then made publically available
through the web.  If you do not want to have your interactions made
available in this way, you must let us know in each email you send to us.

------- End of Forwarded Message

When dealing with Strings, you need a nother dimension. heres how to make it work:

 public void testWriteStringWork() {

   int n=4;
   String[] testName   = new String[n];
   testName[0]="name1";
   testName[1]="name2";
   testName[2]="name3";
   testName[3]="name4";

   NetcdfFileWriteable ncFile = new NetcdfFileWriteable();
   ncFile.setName(TestAll.topDir+"testWriteStringWork.nc");
   Dimension d1 = ncFile.addDimension("example", n);
   Dimension d2 = ncFile.addDimension("strlen", 5);

   ncFile.addVariable("testName", char.class, new Dimension[] {d1, d2});
   try {
     ncFile.create();

     ArrayChar ac = new ArrayChar.D2(n, 5);

     for (int y = 0; y < n; y++) {
       ac.setString(y, testName[y]);
     }

     ncFile.write("testName", new int[2], ac);
     /* ncFile.write("testName", ac);*/

     ncFile.close();
   } catch (IOException ioe) { ioe.printStackTrace(); }
 }

gives this:

netcdf C:/dev/netcdf-java/test/data/testWriteStringWork.nc {
dimensions:
  example = 4;
  strlen = 5;

variables:
  char testName(example, strlen);

data:
 testName ="name1", "name2", "name3", "name4"
}


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