cdm/src/main/java/ucar/nc2/NetcdfFileWriter.java
public Variable addStringVariable(Group g, Variable stringVar, List<Dimension> dims) {
    if (!defineMode)
      throw new UnsupportedOperationException("not in define mode");

    if (!N3iosp.isValidNetcdfObjectName(stringVar.getShortName()))
      throw new IllegalArgumentException("illegal netCDF-3 variable name: " + stringVar.getShortName());

    // convert STRING to CHAR
    int max_strlen = 0;
    Array data;

    try {
      data = stringVar.read();
      IndexIterator ii = data.getIndexIterator();
      while (ii.hasNext()) {
        String s = (String) ii.getObjectNext();
        max_strlen = Math.max(max_strlen, s.length());
      }
    } catch (IOException e) {
      e.printStackTrace();
      String err = "No data found for Variable " + stringVar.getShortName() +
              ". Cannot determine the lentgh of the new CHAR variable.";
      log.error(err);
      System.out.println(err);
    }

    return addStringVariable(g, stringVar.getShortName(), dims, max_strlen);
  }
public Variable addStringVariable(Group g, Variable stringVar, List<Dimension> dims) {
    if (!defineMode)
      throw new UnsupportedOperationException("not in define mode");

    if (!N3iosp.isValidNetcdfObjectName(stringVar.getShortName()))
      throw new IllegalArgumentException("illegal netCDF-3 variable name: " + stringVar.getShortName());

    // convert STRING to CHAR
    int max_strlen = 0;
    Array data;

    try {
      data = stringVar.read();
      IndexIterator ii = data.getIndexIterator();
      while (ii.hasNext()) {
        String s = (String) ii.getObjectNext();
        max_strlen = Math.max(max_strlen, s.length());
      }
    } catch (IOException e) {
      e.printStackTrace();
      String err = "No data found for Variable " + stringVar.getShortName() +
              ". Cannot determine the lentgh of the new CHAR variable.";
      log.error(err);
      System.out.println(err);
    }

    return addStringVariable(g, stringVar.getShortName(), dims, max_strlen);
  }
Add a variable with DataType = String to a netCDF-3 file. Must be in define mode.
The variable will be stored in the file as a CHAR variable.
A new dimension with name "stringVar.getShortName()_strlen" is automatically
added, with length max_strlen, as determined from the data contained in the
stringVar.

@param g         add to this group in the new file
@param stringVar string variable.
@param dims      list of Dimensions for the string variable.
@return the CHAR variable generated from stringVar