Due to the current gap in continued funding from the U.S. National Science Foundation (NSF), the NSF Unidata Program Center has temporarily paused most operations. See NSF Unidata Pause in Most Operations for details.
Dear All, I would like to write a netCDF4 file in Java based on https://www.unidata.ucar.edu/software/thredds/current/netcdf-java/tutorial/NetcdfWriting.html. I have a lot of variables and I would like to select just some and write only them to netcdf file. I tried to solve this by the the following way: 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(); } } But after I run the code I get java.lang.NullPointerException at ucar.nc2.NetcdfFileWriter.write(NetcdfFileWriter.java:939). I tried to solve it with define global variables size e.g public static Variable time = new Variable(); but it is not possible. Could you please informe me how I should fix this issue? Thank you for your help in adavnce! Best regards, Beata
netcdf-java
archives: