NetCDF Java Library example - dump table
Requirement:
Download and import the NetCDF Library from Unidata. Version 4.1 is used in this example.
Potential problem:
If the table is too large, but the heap size (or ram size) is too small, "Array dataArray = variables.get(i).read();" will return error.
package javaapplication3; import java.io.IOException; import java.util.ArrayList; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import ucar.ma2.Array; import ucar.nc2.Dimension; import ucar.nc2.Variable; import ucar.nc2.dataset.NetcdfDataset; public class Main { public static void main(String[] args) { try { // open netcdf/grib/grib2 file from argument NetcdfDataset gid = NetcdfDataset.openDataset(args[0]); // get all grid tables in the file List<Variable> variables = gid.getReferencedFile().getVariables(); for (int i = 0; i < variables.size(); i++) { if ((!variables.get(i).isMetadata()) && (!variables.get(i).getRanges().isEmpty())) { System.out.print(variables.get(i).getName()); System.out.print(", "); System.out.print(variables.get(i).getDataType()); System.out.print(": "); // the range of number of the grid System.out.print(variables.get(i).getRanges()); System.out.print(" "); // the unit System.out.println(variables.get(i).getUnitsString()); //the dimensions of the grid table List<Dimension> dimensions = variables.get(i).getDimensions(); for (int j = 0; j < dimensions.size(); j++) { try { // the name of the dimension System.out.print(dimensions.get(j).getName()); System.out.print(", "); // the type of the dimension System.out.print(gid.getReferencedFile().findVariable(dimensions.get(j).getName()).findAttribute("_CoordinateAxisType").getStringValue()); System.out.print(": "); // the unit of the dimension System.out.println(gid.getReferencedFile().findVariable(dimensions.get(j).getName()).getUnitsString()); } catch (Exception e) { System.out.println(); continue; } } // the data in the grid table Array dataArray = variables.get(i).read(); // calculate the total number of dimension combination int dimensionTotal = 1; ArrayList<Array> dimensionsData = new ArrayList<Array>(); for (int k = 0; k < dimensions.size(); k++) { dimensionTotal *= dimensions.get(k).getLength(); //preload the dimension value dimensionsData.add(gid.getReferencedFile().findVariable(dimensions.get(k).getName()).read()); } for (int j = 0; j < variables.get(i).getSize(); j++) { System.out.print(variables.get(i).getName()); System.out.print(", "); // for merging the dimension value int previousDimensionTotal = dimensionTotal; for (int k = 0; k < dimensions.size(); k++) { previousDimensionTotal /= dimensions.get(k).getLength(); int arrayNumber = (j / previousDimensionTotal) % (dimensions.get(k).getLength()); Variable dimension = gid.getReferencedFile().findVariable(dimensions.get(k).getName()); System.out.print(dimension.findAttribute("_CoordinateAxisType").getStringValue()); System.out.print(": "); // show the value System.out.print(dimensionsData.get(k).getFloat(arrayNumber)); if ((k + 1) != dimensions.size()) { System.out.print(", "); } } System.out.print(" --- "); System.out.print(" "); // print the value System.out.print(dataArray.getFloat(j)); System.out.println(); } System.out.println("--------------------------------------------------"); } } gid.close(); } catch (IOException ex) { Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); } } }
Sample Output of reading GFS GRIB2 data from NOAA:
Absolute_vorticity, float: [0:0, 0:46, 0:10, 0:20] s-1 time, Time: hours since 2010-04-13T06:00:00Z pressure, Pressure: Pa lat, Lat: degrees_north lon, Lon: degrees_east Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 110.0 --- 5.9E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 110.5 --- 8.7E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 111.0 --- 7.8E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 111.5 --- 3.6E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 112.0 --- 3.0E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 112.5 --- 4.2E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 113.0 --- 4.6E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 113.5 --- 4.5E-5 Absolute_vorticity, Time: 12.0, Pressure: 100000.0, Lat: 20.0, Lon: 114.0 --- 4.6E-5
本文連結