Normally you should show what the error message is.
I assume you have a class cast exception on
Iterator gi = nc.getGlobalAttributeIterator();
while( gi.hasNext() ) {
Variable v = (Variable) gi.next();
The docs say:
getGlobalAttributeIterator
public java.util.Iterator getGlobalAttributeIterator()
Returns the set of attributes associated with this file, also know as the
"global" attributes. CHANGE TO GENERIC
Returns:
Iterator objects are type Attribute
So the iterator returns objects of type Attribute, not Variable
>
> ------- Forwarded Message
>
> >From: bob@xxxxxxxxxxxxx
> >Organization: NOAA/NODC
> >Keywords: 200206071844.g57IiHJ14153 netCDF Java
>
> June 7, 2002
> To Whom It May Concern:
>
> The following 4 Java modules successfully read
> data from a "netCDF" file.
>
> In attempting to read global data, however, I was
> not successful. The failure is shown in the commented out
> portion of "dumpCDF.java" found near the beginning
> of the while loop.
>
> Could you please send me code which will work. Or,
> please contact me somehow.
>
>
>
> Robert Van Wie
>
> at "Robert.Van.Wie@xxxxxxxx"
> also at "rvanwie@xxxxxxxxxxxxx"
>
>
>
> import ucar.ma2.*;
> import ucar.nc2.*;
> import java.io.IOException;
> import java.util.*;
> import java.util.Iterator;
>
> public class dumpCDF extends aux {
>
> public static void main( String args[] ) {
>
> if (args.length > 0 ) new dumpCDF( args[0], args[1] );
> else new dumpCDF( "example.nc", "outCDF" );
> }
>
> public dumpCDF( String inFile, String outFile ) { // constructor
> int numDep = 0;
> byte ix = 0;
> short ig = 0;
> String global = "";
> String defName = "";
> String DP = "Depth";
>
> try { NetcdfFile nc = new NetcdfFile( inFile ); // readonly
>
> /* Iterator gi = nc.getGlobalAttributeIterator(); does not work !
> while( gi.hasNext() ) {
> Variable v = (Variable) gi.next();
> Array varMa = v.read();
> global = v.getName();
> System.out.print( v.getName() + " =" );
> global = findAttValueIgnoreCase( null, "title", defName );
> System.out.println( ig + " global " + global );
> ig++;
> } // end while */
>
> Iterator it = nc.getVariableIterator();
> while( it.hasNext() ) {
> Variable v = (Variable) it.next();
> Array varMa = v.read();
> var = v.getName();
> varCode = getParmCode(); // numeric identifier of variable
> if ( varCode > -1 ) {
> array = ArrayToString( varMa );
> if ( varCode == 0 ) { // depth/press
> numDep = store( array, varCode ); // store variable in
"depVal"
> alignDepth( numDep );
> if ( var.indexOf( "press" ) > -1 ) DP = "Press";
> ix++;
> } else store( array, varCode ); // store variable in
"depVal"
> if ( varCode == 2 ) { // salinity
> alignSal( numDep ); // align salinity
column
> for ( int l=0; l < numDep; l++ )
> Writer(
GAP9+depVal[0][l]+GAP8+depVal[1][l]+GAP7+depVal[2][l], outFile );
> }
> } else { // non-profile
variable
> if (var.startsWith( "woce_date") ) WOCEdate=ArrayToString(
varMa );
> if ( var.startsWith( "woce_time" ) ) { // process woce_time
> woce_time = ArrayToString( varMa );
> if ( woce_time.length() < 6 ) WOCEtime = "0" + woce_time;
> else WOCEtime = woce_time;
> myTime = parse( WOCEtime ); // change format of
time
> myDate = parse( WOCEdate ); // change format of
date
> Writer( "\n Date & Time: " + myDate + " " + myTime ,
outFile );
> }
> if ( var.startsWith( "longitude" ) ) {
> ix++;
> longitude = ArrayToString( varMa );
> Writer( "\n Position: Latitude " + latitude + ";
Longitude " + longitude, outFile );
> }
> else if ( var.startsWith( "latitude" ) ) latitude
ArrayToString( varMa );
> }
> if ( ix > 1 ) {
> Writer( "\n " + DP + " Temperature Salinity",
outFile );
> ix = 0;
> }
> } // end while
> } catch (java.io.IOException e) { e.printStackTrace(); }
> } // end dumpCDF constructor
>
> public String ArrayToString( Array ma ) {
> StringBuffer buf
> new StringBuffer( toArray( ma, new IndentLevel() ));
> return buf.toString();
> } // end ArrayToString
>
> private String toArray( Array ma, IndentLevel ilev ) {
> final int rank = ma.getRank();
>
> Index ima = ma.getIndex();
> if ( rank == 0 ) return ma.getObject(ima).toString();
>
> StringBuffer buf = new StringBuffer();
> ilev.incr();
> final int [] dims = ma.getShape();
> final int last = dims[0];
> for ( int ii=0; ii < last; ii++ ) {
> Array slice = ma.slice( 0, ii );
> buf.append( toArray( slice, ilev ) );
> if ( varCode == 0 )
> if ( ii != last-1 ) buf.append( " " );
> }
> ilev.decr();
> if ( rank > 1 ) buf.append( "\n" + ilev.getIndent() );
> return buf.toString();
> } // end toArray
>
> } // end class dumpCDF
> import ucar.ma2.*;
> import ucar.nc2.*;
> import java.io.*;
> import java.io.IOException;
> import java.util.*;
>
> public class OrigAux {
>
> final static String GAP7 = " ", GAP8 = " ", GAP9 = "
";
> final static String BLANKS = " ";
> byte varCode = -2; // numeric identifier of data column
> int maxTempL = 0; // string length of highest temperature
> String WOCEdate, WOCEtime, woce_time, depVal[][] = new String[3][9000];
> String var, array, myDate, myTime, latitude, longitude, park;
> IndentLevel il; // id of "IndentLevel" class
>
> protected void alignDepth( int numDp ) {
> // align depth column
> int lenDiff, ln, len;
>
> len = depVal[0][numDp-1].length(); // string length of max depth
> for ( int n=0; n < numDp-1; n++ ) {
> park = depVal[0][n];
> ln = park.length();
> lenDiff = len - ln;
> if ( lenDiff > 0 ) depVal[0][n] = BLANKS.substring( 0, lenDiff ) +
park;
> }
> maxTempL = 0;
> } // end alignDepth
>
> protected void alignSal( int numDp ) {
> // align salinity column
> int lenDiff, ln;
> String sal = "";
>
> for( int n=0; n < numDp; n++ ) {
> park = depVal[1][n];
> ln = park.length();
> lenDiff = maxTempL - ln;
> if ( lenDiff > 0 ) {
> sal = depVal[2][n];
> depVal[2][n] = BLANKS.substring( 0, lenDiff ) + sal;
> }
> } // for loop
> } // end alignSal
>
> protected void Writer( String text, String outfle ) { // write single
string
> try {
> Writer w = new BufferedWriter( new FileWriter( outfle, true ));
> w.write( text + "\n" );
> w.close();
> } // try
> catch (IOException e ) { System.err.println(outfle+" Output file
error..."); }
> catch ( Exception e ) { System.err.println("Input error..Exception in
method Writer"); }
> } // end Writer
>
> } // end class OrigAux.java
> import ucar.ma2.*;
> import ucar.nc2.*;
> import java.io.IOException;
> import java.util.*;
>
> public class aux extends OrigAux {
>
> protected int store( String array, byte i ) {
> int l = 0; // store in "depVal"
array
> int ln = 0;
>
> StringTokenizer st = new StringTokenizer(array);
> while ( st.hasMoreTokens() ) {
> depVal[i][l] = st.nextToken().trim();
> if ( i == 1 ) {
> ln = depVal[1][l].length(); // length of
temperature string
> if ( ln > maxTempL ) maxTempL = ln; // max temp length
> }
> l++; // depth level counter
> }
> return l;
> } // end store
>
> protected byte getParmCode( ) { // depth/press: 0
> byte parmCode = -1; // temperature: 1
> // salinity : 2
> if (var.startsWith("press") ) parmCode = 0;
> else if (var.startsWith("dep") ) parmCode = 0;
> else if (var.startsWith("temp") ) parmCode = 1;
> else if (var.startsWith("sal") ) parmCode = 2;
> return parmCode;
> }
>
> protected String parse( String primative ) {
> // reformat date & time fields
> String part[] = new String[3];
> String sep = "/"; // date field seperator
> byte n = 0, end = 4; // length of year field
>
> if ( primative.length() == 6 ) { // length of time field
> sep = ":"; // time field seperator
> end = 2; // length of hour field
> }
> String whole = ""; // will hold reformatted string
> for ( byte i=0; i < 3; i++ ) {
> part[i] = primative.substring( n, end ); // individual field
> n = end;
> end += 2;
> whole += part[i];
> if ( i < 2 ) whole += sep;
> }
> return whole; // return reformatted string
> } // end method parse
>
> } // end class aux.java
> import ucar.ma2.*;
> import ucar.nc2.*;
> import java.io.IOException;
> import java.util.*;
>
> public class IndentLevel {
>
> private int indentation, level = 0;
> private StringBuffer indent, blanks;
>
> IndentLevel() { this(4); } // end constructor
>
> IndentLevel( int indentation) {
> if (indentation > 0) this.indentation = indentation;
> indent = new StringBuffer();
> blanks = new StringBuffer();
> for( int i=0; i < indentation; i++ )
> blanks.append(" ");
> } // end constructor
>
> public void incr() {
> level += indentation;
> indent.append( blanks );
> }
>
> public void decr() {
> level -= indentation;
> indent.setLength( level );
> }
>
> public String getIndent() {
> return indent.toString();
> }
> } // end class IndentLevel
>
>
> ------- End of Forwarded Message
>