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.
Hi Mark:you cant have an attribute with an array of Strings, only arrays of numbers are allowed. put it all in one string, and decode it when you read, eg
String v = "one, two"; ncFile.addVariableAttribute("edrmap", "units", v.getBytes()); when you read, you can use java.lang.StringTokenizer to break into tokens: Attribute att = edrmapVar.findAttribute("units"); String val = att.getStringValue(); StringTokenizer st = new StringTokenizer(val, ", "); while (st.hasMoreTokens()) { println(st.nextToken()); } /** * Add an attribute of type Array to the named Variable. Must be in define mode. ** @param String varName: name of attribute. IllegalArgumentException if not valid name.
* @param String attName: name of attribute.* @param Object value: must be 1D array of double, float, int, short, char, or byte
*/ public void addVariableAttribute(String varName, String attName, Object value) ; Mark A Ohrenschall wrote:
Hello, I'm trying to define an array-valued variable attribute, e.g.,ncFile.addVariableAttribute("edrmap", "units", new String [] {"one", "two"});But I get a run-time error: Exception in thread "main" java.lang.IllegalArgumentException: Invalid Type: class java.lang.String at ucar.netcdf.Attribute.<init>(Attribute.java:129) at ucar.nc2.NetcdfFileWriteable.addVariableAttribute(NetcdfFileWriteable.java:214) at edrmap.Edrmap.<init>(Edrmap.java:99) at edrmap.Edrmap.main(Edrmap.java:121)I'm able to pass in an array of ints. The API documentation for addGlobalAttribute includes "Remark: this looks buggy; allowed types unclear." Perhaps this is true for addVariableAttribute as well?I'm using 2.10 beta of the (Java) netCDF library. Thanks, Mark Ohrenschall