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: Newbite question about using NetCDF Java Library (Version 2) to add a java.lang.String scalar variable

  • To: Ed Yu <ekyu@xxxxxxxxxx>
  • Subject: Re: Newbite question about using NetCDF Java Library (Version 2) to add a java.lang.String scalar variable
  • From: John Caron <caron@xxxxxxxxxxxxxxxx>
  • Date: Mon, 10 Nov 2003 10:44:01 -0700
Ed Yu wrote:

Hi all,
Does anyone has an example on how to write a scalar String variable say comment ("this is a comment") to a netcdf file? Should I use an ArrayChar.D1 instead of the 0-rank? Code and comments is appreciated. Thanks!

sorry for the lack of examples, heres some scalar String utilities:

public void createString(NetcdfFileWriteable ncfile, String name, String val) {
 Dimension slen = ncfile.addDimension(name+"_len", val.length());
 ncfile.addVariable(name, char.class, new Dimension[] {slen});
}

public void writeString(NetcdfFileWriteable ncfile, String name, String val) throws IOException {
 ArrayChar ac = new ArrayChar.D1(val.length());
 ac.setString(val);
 ncfile.write(name, new int[1], ac);
}

public String readString(NetcdfFile ncfile, String name) throws IOException {
 Variable v = ncfile.findVariable(name);
 ArrayChar ac = (ArrayChar) v.read();
 return ac.getString();
}



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