我打算基于https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfWriting.html用Java编写一个netCDF4文件。我有很多变量,我只想选择一些变量,只将它们写到netcdf文件中。我尝试通过以下方式解决此问题:
public class JavaDB {
public static Variable time;
public static Variable cloud_1st;
public static Variable cloud_2nd;
public static Variable cloud_3th;
public static Variable cloud_4th;
public static Variable cloud_5th;
public static Variable Layer_1st;
public static Variable Layer_2nd;
public static Variable Layer_3th;
public static Variable Layer_4th;
public static Variable Layer_5th;
public static Variable CBN;
public static void getDataNC(String location, ArrayList<Integer> Values) throws Exception{
String version = System.getProperty("java.version");
NetcdfFileWriter ncfile = NetcdfFileWriter.createNew(NetcdfFileWriter.Version.netcdf4, location, null);
// add dimensions
Dimension heightDim = ncfile.addDimension(null,"height",1539);
Dimension timeDim = ncfile.addUnlimitedDimension("time");
//define variables
for(int k=0; k<Values.size(); k++){
switch(Values.get(k)){
case 1: Variable time = ncfile.addVariable(null, "time", DataType.DOUBLE,"time");
time.addAttribute( new Attribute("units", "days since 1970-01-01"));
break;
case 2: Variable cloud_1st = ncfile.addVariable(null,"cloud_1st", DataType.INT,"time");
cloud_1st.addAttribute( new Attribute("long_name", "cloud amount in 1st layer1"));
cloud_1st.addAttribute( new Attribute("units", "octa"));
cloud_1st.addAttribute(new Attribute("_FillValue",-9999));
cloud_1st.addAttribute(new Attribute("Vertical Visibility",9));
cloud_1st.addAttribute(new Attribute("No enough data",99));
break;
case 3: Variable cloud_2nd = ncfile.addVariable(null,"cloud_2nd", DataType.INT,"time");
cloud_2nd.addAttribute( new Attribute("long_name", "cloud amount in 2nd layer"));
cloud_2nd.addAttribute( new Attribute("units", "octa"));
cloud_2nd.addAttribute(new Attribute("_FillValue",-9999));
cloud_2nd.addAttribute(new Attribute("Vertical Visibility",9));
cloud_2nd.addAttribute(new Attribute("No enough data",99));
break;
case 4: Variable cloud_3th = ncfile.addVariable(null,"cloud_3th", DataType.INT,"time");
cloud_3th.addAttribute( new Attribute("long_name", "cloud amount in 3th layer"));
cloud_3th.addAttribute( new Attribute("units", "octa"));
cloud_3th.addAttribute(new Attribute("_FillValue",-9999));
cloud_3th.addAttribute(new Attribute("Vertical Visibility",9));
cloud_3th.addAttribute(new Attribute("No enough data",99));
break;
case 5: Variable cloud_4th = ncfile.addVariable(null, "cloud_4th", DataType.INT, "time");
cloud_4th.addAttribute( new Attribute("long_name", "cloud amount in 4th layer"));
cloud_4th.addAttribute( new Attribute("units", "octa"));
cloud_4th.addAttribute(new Attribute("_FillValue",-9999));
cloud_4th.addAttribute(new Attribute("Vertical Visibility", 9));
cloud_4th.addAttribute(new Attribute("No enough data",99));
break;
case 6: Variable cloud_5th = ncfile.addVariable(null, "cloud_5th", DataType.INT, "time");
cloud_5th.addAttribute( new Attribute("long_name", "cloud amount in 5th layer"));
cloud_5th.addAttribute( new Attribute("units", "octa"));
cloud_5th.addAttribute(new Attribute("_FillValue",-9999));
cloud_5th.addAttribute(new Attribute("Vertical Visibility",9));
cloud_5th.addAttribute(new Attribute("no enough data",99));
break;
case 7: Variable Layer_1st = ncfile.addVariable(null,"Layer_1st",DataType.INT,"time");
Layer_1st.addAttribute(new Attribute("long_name", "1st cloud layer height"));
Layer_1st.addAttribute(new Attribute("units","meter"));
Layer_1st.addAttribute(new Attribute("_FillValue",-9999));
break;
case 8: Variable Layer_2nd = ncfile.addVariable(null,"Layer_2nd",DataType.INT,"time");
Layer_2nd.addAttribute(new Attribute("long_name", "2nd cloud layer height"));
Layer_2nd.addAttribute(new Attribute("units","meter"));
Layer_2nd.addAttribute(new Attribute("_FillValue",-9999));
break;
case 9: Variable Layer_3th = ncfile.addVariable(null,"Layer_3th",DataType.INT,"time");
Layer_3th.addAttribute(new Attribute("long_name", "3th cloud layer height"));
Layer_3th.addAttribute(new Attribute("units","meter"));
Layer_3th.addAttribute(new Attribute("_FillValue",-9999));
break;
case 10: Variable Layer_4th = ncfile.addVariable(null,"Layer_4th",DataType.INT,"time");
Layer_4th.addAttribute(new Attribute("long_name", "4th cloud layer height"));
Layer_4th.addAttribute(new Attribute("units","meter"));
Layer_4th.addAttribute(new Attribute("_FillValue",-9999));
break;
case 11: Variable Layer_5th = ncfile.addVariable(null,"Layer_5th",DataType.INT,"time");
Layer_5th.addAttribute(new Attribute("long_name", "5th cloud layer height"));
Layer_5th.addAttribute(new Attribute("units","meter"));
Layer_5th.addAttribute(new Attribute("_FillValue",-9999));
break;
case 12: Variable CBN = ncfile.addVariable(null,"CBN",DataType.INT,"time");
CBN.addAttribute(new Attribute("long_name","cloud base number"));
CBN.addAttribute(new Attribute("units", "number"));
CBN.addAttribute(new Attribute("no significant backscatter",0));
CBN.addAttribute(new Attribute("obscuration but no cloud",4));
CBN.addAttribute(new Attribute("transparent obscuration",5));
CBN.addAttribute(new Attribute("_FillValue",-9));
break;
}
}
ncfile.create();
try{
ArrayDouble.D1 timeData = new ArrayDouble.D1(countLinesResult);
ArrayInt.D1 cloudL1Data = new ArrayInt.D1(countLinesResult);
Index ima = cloudL1Data.getIndex();
ArrayInt.D1 cloudL2Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cloudL3Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cloudL4Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cloudL5Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cLayer1Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cLayer2Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cLayer3Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cLayer4Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 cLayer5Data = new ArrayInt.D1(countLinesResult);
ArrayInt.D1 CBNData = new ArrayInt.D1(countLinesResult);
for (int timeIdx = 0; timeIdx < countLinesResult;timeIdx++){
for(int k=0; k<Values.size(); k++){
// System.out.println(Values.get(k));
if(Values.get(k)==1){
timeData.setDouble(timeIdx,tdate.get(timeIdx));}
if(Values.get(k)==2){
cloudL1Data.setInt(ima.set(timeIdx), cloud_layer_1st.get(timeIdx));}
if(Values.get(k)==3){
cloudL2Data.setInt(ima.set(timeIdx), cloud_layer_2nd.get(timeIdx));}
if(Values.get(k)==4){
cloudL3Data.setInt(ima.set(timeIdx), cloud_layer_3th.get(timeIdx));}
if(Values.get(k)==5){
cloudL4Data.setInt(ima.set(timeIdx), cloud_layer_4th.get(timeIdx));}
if(Values.get(k)==6){
cloudL5Data.setInt(ima.set(timeIdx), cloud_layer_5th.get(timeIdx));}
if(Values.get(k)==7){
cLayer1Data.setInt(ima.set(timeIdx), LayerC_1st.get(timeIdx));}
if(Values.get(k)==8){
cLayer2Data.setInt(ima.set(timeIdx), LayerC_2nd.get(timeIdx));}
if(Values.get(k)==9){
cLayer3Data.setInt(ima.set(timeIdx), LayerC_3th.get(timeIdx));}
if(Values.get(k)==10){
cLayer4Data.setInt(ima.set(timeIdx), LayerC_4th.get(timeIdx));}
if(Values.get(k)==11){
cLayer5Data.setInt(ima.set(timeIdx), LayerC_5th.get(timeIdx));}
if(Values.get(k)==12){
CBNData.setInt(ima.set(timeIdx),nbcb.get(timeIdx));}
}
}
// write the data into netCDF file
for(int k=0; k<Values.size(); k++){
if(Values.get(k)==1){
ncfile.write(time, timeData);}
if(Values.get(k)==2){
ncfile.write(cloud_1st, cloudL1Data);}
if(Values.get(k)==3){
ncfile.write(cloud_2nd, cloudL2Data);}
if(Values.get(k)==4){
ncfile.write(cloud_3th, cloudL3Data);}
if(Values.get(k)==5){
ncfile.write(cloud_4th, cloudL4Data);}
if(Values.get(k)==6){
ncfile.write(cloud_5th, cloudL5Data);}
if(Values.get(k)==7){
ncfile.write(Layer_1st, cLayer1Data);}
if(Values.get(k)==8){
ncfile.write(Layer_2nd, cLayer2Data);}
if(Values.get(k)==9){
ncfile.write(Layer_3th, cLayer3Data);}
if(Values.get(k)==10){
ncfile.write(Layer_4th, cLayer4Data);}
if(Values.get(k)==11){
ncfile.write(Layer_5th, cLayer5Data);}
if(Values.get(k)==12){
ncfile.write(CBN,CBNData);}
}
}catch(Exception e){
e.printStackTrace();}
finally{
try{
ncfile.close();
}catch(IOException ex){
ex.printStackTrace();
}
}
运行它后,我得到java.lang.NullPointerException
。可能是由于未初始化全局变量。有人可以写信给我我该如何解决该错误?
答案 0 :(得分:0)
您两次声明变量,一次声明为静态变量:
public static Variable time;
以及其他在您的交换机本地范围内的
Variable time = ncfile.addVariable(...);
,因此您正在本地范围内“丢失”已初始化的对象。
仅通过将初始化的对象分配给静态变量声明来替换本地声明:
time = ncfile.addVariable(...);
致谢
安东尼奥