This archive contains answers to questions sent to Unidata support through mid-2025. Note that the archive is no longer being updated. We provide the archive for reference; many of the answers presented here remain technically correct, even if somewhat outdated. For the most up-to-date information on the use of NSF Unidata software and data services, please consult the Software Documentation first.
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(); }