[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Newbite question about using NetCDF Java Library (Version 2) to add a java.lang.String scalar variable



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();
}