Dear John:
I got your email address from the
http://www.unidata.ucar.edu/packages/netcdf-java/ website. I hope my
question is not an intrusion.
I have been trying to use the netCDF Java library to read in some
global climate change data. I had a very small application that was
working for me a couple of years ago. However, I have come back to it
and I keep receiving the following errors:
class HTTPConnection not found in class ucar.netcdf.HTTPRandomAccessFile
class NVPair not found in class ucar.netcdf.HTTPRandomAccessFile
I am using JBuilder3 (!) with SDE 1.2. Unfortunately, someone just
stole our new JBuilder8, so upgrading isn't an option right now.
If you have any hints or insights, I would greatly appreciate them.
Sincerely,
Rob Scheller
readCCPnET.java:
package readCCPnET;
import ucar.ma2.*;
import ucar.nc2.*;
import java.io.*;
import java.util.*;
import java.lang.*;
public class readCCPnET {
public static void main(String [] args) throws IOException {
String GCMname = "HAD2SuM3"; //Either "CCC1SuM4" or "HAD2SuM3"
String outFilename = ("c:\\users\\rob\\data\\vemap\\" + GCMname
+ "outPnET.txt");
BufferedWriter output = new BufferedWriter(new
FileWriter(outFilename));
int climateYear = 2098;
try {
// t=time; lt=latitude; lg=longitude; l=low; h=high
int tindexl, tindexh, ltindexl, ltindexh, lgindexl, lgindexh;
tindexl = 0; tindexh = 1271; //CCC = 1283
ltindexl = 5; ltindexh = 5;
lgindexl = 66; lgindexh = 66;
Range[] r = new Range[3];
r[0] = new Range(tindexl,tindexh); //time
r[1] = new Range(ltindexl,ltindexh); //lat
r[2] = new Range(lgindexl,lgindexh); //long
String s1="", s2="", s3 = "";
double[][] dtemp = getVarData(GCMname, "tmax", r);
double[][] dtemp2 = getVarData(GCMname, "tmin", r);
double[][] dtemp3 = getVarData(GCMname, "pptx", r);
/* From PnET-II README file:
File of climate information - either mean or transient (historic)
Each month is listed on a separate line, current order is
pixel id, year, Julian Day, temp max average, temp min average,
radiation in PAR, precipitation.
expected filename: projectname.clim
The units of input are decimal degrees Centigrade for temperature,
and micromoles of PAR and centimeters of precipitation monthly. */
//write header:
output.write("year\tmonth\ttempMax\ttempMin\tpptAvg\tlat\tlong"
+ calcYear((int) dtemp[0][0]) + "\n");
output.write("MEAN T\n");
for(int i=r[0].first(); i<r[0].last(); i++){
if(dtemp[i][0]==dtemp2[i][0] && dtemp[i][0]==dtemp3[i][0] &&
dtemp[i][1]==dtemp2[i][1] && dtemp[i][1]==dtemp3[i][1] &&
dtemp[i][2]==dtemp2[i][2] && dtemp[i][1]==dtemp3[i][1] &&
calcYear((int) dtemp[i][0])==climateYear) {
//climateYear see above
output.write(calcYear((int) dtemp[i][0]) + "\t");
output.write(calcMonth((int) dtemp[i][0])+ "\t");
output.write(getRound(dtemp[i][3],1) + "\t");
output.write(getRound(dtemp2[i][3],1)+ "\t");
output.write(getRound(dtemp3[i][3],1) + "\t");
output.write(dtemp[0][1] + "\t");
output.write(dtemp[0][2] + "\n");
}
}
} catch (InvalidRangeException e) {
System.err.println("ERROR reading file "+e);
return;
} catch (IOException e) {
System.err.println("ERROR reading file "+e);
return;
}
output.close();
} //end main
private static double[][] getVarData(String GCMname, String
climVar, Range[] r) throws IOException {
//Read climate variable file:
NetcdfFile ncfile = new NetcdfFile("c:/users/rob/data/VEMAPCC/"
+ climVar + GCMname + "i.nc");
System.out.println( "ncfile "+ ncfile);
Variable tempVar = ncfile.findVariable(climVar);
System.out.println(tempVar);
// read all the data and logically subset it
Array tempArray = tempVar.read();
Index tempIndex = tempArray.getIndex();
double[][] d = new double[r[0].last()+1][4]; //4 values: time,
lat, long, climateVar
int time=0, lat=1, lon=2; //set Dimension integers.
for(int i=r[0].first(); i<=r[0].last(); i++){ //time
for(int j=r[1].first(); j<=r[1].last(); j++){ //latitude
for(int k=r[2].first(); k<=r[2].last(); k++){ //longitude
d[i][0] = getDimensionDouble(tempVar, time, i);
//System.out.println(d[i][0]);
d[i][1] = getDimensionDouble(tempVar, lat, j);
d[i][2] = getDimensionDouble(tempVar, lon, k);
d[i][3] = tempArray.getDouble(tempIndex.set(i,j,k));
}
}
}
return d;
}
private static void printArray( Array a) {
IndexIterator iter = a.getIndexIterator();
int cnt = 0;
while (iter.hasNext()){
System.out.println(cnt + " " + iter.getDoubleNext()+ ",");
cnt++;
}
}
//function to retreive values of the indices:
private static double getDimensionDouble(Variable v, int d, int
index) throws IOException {
Array temp = v.getDimension(d).getCoordinateVariable().read();
Index tempi = temp.getIndex();
return temp.getDouble(tempi.set(index));
}
private static double getRound(double d, int i){
int factor = 1;
for(int j=1; j<=i; j++){
factor *= 10;
}
//first round number if necessary.
return (double)( ((int) Math.round(d*factor)) /(double) factor);
}
private static int calcYear(int t){
GregorianCalendar myCal = new GregorianCalendar(1895, 0, 1);
myCal.add(Calendar.DAY_OF_YEAR, t);
return myCal.get(Calendar.YEAR);
}
private static int calcMonth(int t){
GregorianCalendar myCal = new GregorianCalendar(1895, 0, 1);
myCal.add(Calendar.DAY_OF_YEAR, t);
return myCal.get(Calendar.MONTH)+1;
}
private static String convertTime(int t){
GregorianCalendar myCal = new GregorianCalendar(1895, 0, 1);
myCal.add(Calendar.DAY_OF_YEAR, t);
return ( (myCal.get(Calendar.MONTH)+1) + "\t" + //adjust to
1-12 calendar months
//myCal.get(Calendar.DATE) + ",\t" +
myCal.get(Calendar.YEAR));
}
}
Robert M. Scheller
Dept. Forest Ecology & Management
University of Wisconsin-Madison
1630 Linden Dr., Madison, WI 53706
608-265-6321
Forest Landscape Ecology Lab http://landscape.forest.wisc.edu
Webpage and C.V. http://landscape.forest.wisc.edu/staff/rob/