[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: 20010711: Netcdf Java problem



thanks for handling that.
BTW you should cc: to support-netcdf-java instead of / in addition to support-netcdf

Russ Rew wrote:

To: address@hidden
From: "Michael P Duff" <address@hidden>
Subject: Netcdf Java problem
Organization: UCAR/Unidata
Keywords: 200107111340.f6BDeO122826


Hi Mike,


Hi.  I am having a problem with the NetCDF Java version 2.  I am reading
data from an existing NetCDF file using the read() function to place the
data into an Array. The data is stored as a double in the Netcdf file.   I
then use the IndexIterator to step through the Array.  I get a compile
error.  I am using the code I found in the API for the Interface
IndexIterator, the "Replace array with its square" example.  The program
works if I just print the values instead of put them into a variable.  I
have below my code, my compile error and the "Replace array with its
square" example.  I would appreciate any help you can give me.

Thank you,

Mike
------------------------------------------------------------------------------------------------------------------------------
// My code that gets a compile error
import ucar.ma2.*;
import ucar.nc2.*;
import java.io.*;
import java.util.*;

public class ncproblem {

     public static void main(String[] args) {
          try {
               NetcdfFile nc = new NetcdfFile(args[0]);
               System.out.println(nc);
               Variable heightVar = nc.findVariable("height");
               System.out.println(heightVar);
               int[] origin = new int[4];
               origin[0] = 45000;
               int[] shape = new int[4];
               shape[0] = 50;
               for (int i = 1; i<4; i+=1){
                    origin[i] = 1;
                    shape[i] = 1;
               }
               Array hgtVals = heightVar.read(origin,shape);
               IndexIterator iter = hgtVals.getIndexIterator();
               while(iter.hasNext()) {
                    for(int i=0; i<50; i+=1) {
                         Double val = iter.getDoubleNext();
                         System.out.println(val + ",");
                    }
               }
          }catch (java.io.IOException e) {
               e.printStackTrace();
          }
     }
}
------------------------------------------------------------------------------------------------------------------------------
The compile error is:
incompatible types
found      :  double
required:  java.lang.Double
                              Double val = iter.getDoubleNext();
                                                             ^
1 error
------------------------------------------------------------------------------------------------------------------------------


Since IndexIterator.getDoubleNext() returns double rather than Double,
you need to change this statement to:

   double val = iter.getDoubleNext();

After that, you'll get another compile error because the read() method
can generate an InvalidRangeException, which is not a java.io.IOException:

 ncproblem.java:23: unreported exception ucar.ma2.InvalidRangeException; must 
be caught or declared to be thrown
                Array hgtVals = heightVar.read(origin,shape);
                                         ^
One (crude) way to handle this would be to change your catch statement to:

          }catch (Exception e) {

--Russ