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.

Writing Strings and arrays of strings

----- Original Message -----
From: "Robert Van Wie" <Robert.Van.Wie@xxxxxxxx>
To: <netcdf-java@xxxxxxxxxxxxxxxx>
Sent: Monday, July 08, 2002 10:03 AM
Subject: all


>



> import java.awt.*;
> import java.io.*;
> import java.awt.event.*;
> import javax.swing.*;
> import ucar.ma2.*;
> import ucar.nc2.*;
>
> public class make
> {
>     private static String parms[] = new String[8];
>     private static int numArgs;
>     public  net        nt;
>     NetcdfFileWriteable ncfle;
>
>     make( String parms[] ) {                     // constructor
>
>      if ( parms[0] == null ) return;
>      NetcdfFileWriteable ncfle = new NetcdfFileWriteable();
>      net nt = new net( ncfle );
>      nt.netHead( );
>    }                                // end constructor
>
>     public static void main( String args[] ) {
>        numArgs = args.length;
>        parms[0] = args[0];
>        if ( numArgs > 1 ) parms[1] = args[1];
>        new make( parms );
>     }
> }                                 // end make
>
> import ucar.ma2.*;
> import ucar.nc2.*;
> import java.io.IOException;
> import java.util.*;
>
> public class net extends NetcdfFile {
>
>    String ncfle = "medsAscii.nc";
>    String outfle = "netOut";
>
>    net( NetcdfFileWriteable ncfle ) { super(); }
>
>    public void netHead( ) {
>      char Aray[] = { ' ',' ',' ',' '};
>      char Aray2[][];
>       int noProf = 2;
>
>      NetcdfFileWriteable ncfle = new NetcdfFileWriteable();
>      String profileType[]={"TEMP","PSAL"};
>      new net( ncfle );
>      ncfle.setName(outfle);                  // output file name
>
>        Dimension profDim = ncfle.addDimension("profile", 4);
>        Dimension[] dimM = new Dimension[1];
>        dimM[0] = profDim;
>        Aray2 = new char[ 2 ][ 4 ];
>        for( byte n=0; n < 2; n++ ) {
>          Aray = to_char( profileType[n] );
>          for( byte m=0; m < 4; m++ ) Aray2[n][m] = Aray[m];
>        }
>        ncfle.addVariable("Profile Code", char.class, new Dimension[]
{profDim});
>        ncfle.addVariableAttribute("Profile Code", "units", "GTSPP Code
Tables");
>        try {
>          ncfle.create();                           // create CDF binary
file
>        } catch (IOException e) { System.out.println("ERROR creating
file"); }
>        System.out.println( "ncfle = "+ ncfle);
>
>        try {                                        // write CDF binary
file
> //       ncfle.write("Profile Code",ArrayAbstract.factory( Aray2 )); //
does not work
>        for( byte n=0; n < 2; n++ )
>          for( byte k=0; k < 4; k++ ) Aray[k] = Aray2[n][k];
>        ncfle.write("Profile Code",ArrayAbstract.factory( Aray ));  //
works !
>        } catch (IOException e) { System.out.println("ERROR creating
file"); }
>        try { ncfle.close();
>        } catch (IOException e) { System.out.println("ERROR closing
file"); }
>    }                             // end netHead
>
>     char[] to_char( String strng ) {            // convert to character
array
>
>        int  len = strng.length();
>        char Array[];                              // input character array
>
>        Array = new char[ len ];
>        strng.getChars(0, len, Array, 0 );               // place into char
array
>        return Array;
>      }                                        // end dateLeadBlanks method
> }
>                                             July 2, 2002
>    To Whom It may Concern:
>
>        Using java, I am trying to write a simple character
multi-dimensional
>   array, named "Aray2" above, into a NetCDF file.
>
>        Please help me.
>
>                                      Bob Van Wie
>                                       Robert.Van.Wie@xxxxxxxx
>


1. normally you should include whatever error messages you see, or explain
what you know "does not work"

2. the javadoc says:

public static ArrayAbstract factory(java.lang.Object javaArray)
  Generate a new Array from a java multidimensional array of primitives.
This makes a COPY of the data values of javaArray.

since it makes a copy of the array, you need to fill the array before you
write it. Im not sure if this is your problem.

3. I assume you are interested in writing strings. (for byte data, use
ArrayByte, not ArrayChar)

heres how to write a 2D char array:

    // write char variable
    int[] origin = new int[2];
    ArrayChar ac = new ArrayChar.D2(n, len)
    ima = ac.getIndex();
    String val = "Testing 1-2-3";
    for (j=0; j<val.length(); j++)
      ac.setChar(ima.set(j), val.charAt(j));

    try {
      ncfile.write("svar", origin1, ac);
    } catch (IOException e) {
      System.err.println("ERROR writing Achar");
      assert(false);
    }

I realize this is obscure, so Ive added some new helper classes into
ArrayChar (release 2.10 now avail).

Example, assuming you want to write a single string

      ArrayChar ac2 = new ArrayChar.D1(str_len);
      ac2.setString( "Yer String here");

    // write String
    try {
      ncfile.write("StringVariable", origin, ac2);
    } catch (IOException e) {
    }

Example, assuming you have an array of n Strings with maximum length
max_len:

      String s[n];
      ArrayChar ac2 = new ArrayChar.D2(n, max_len);
      ac2.setString( 0, s[0]);
      ac2.setString( 1, s[1]);
      ac2.setString( 2, s[2]);

    // write String array
    try {
      ncfile.write("StringArray", origin, ac2);
    } catch (IOException e) {
    }



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