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: IllegalArgumentException with addVariableAttribute

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




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