Last active
September 7, 2019 06:03
-
-
Save kaichao/4d7b9ad8c3ccc3f291d2bd5ca0b7b112 to your computer and use it in GitHub Desktop.
read grib2 大气数据文件
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String dataFile = "data/NWP_NMC_T639R/Z_NAFP_C_BABJ_20161108000000_P_CNPC-T639-GMFS-HNEHE-00000.grib2"; | |
try { | |
// open netcdf/grib/grib2 file from argument | |
NetcdfDataset gid = NetcdfDataset.openDataset(dataFile); | |
// get all grid tables in the file | |
List<Variable> variables = gid.getReferencedFile().getVariables(); | |
for (int i = 0; i < variables.size(); i++) { | |
Variable var = variables.get(i); | |
if ((!var.isMetadata()) && (!var.getRanges().isEmpty())) { | |
System.out.format("%s, %s: %s %s\n", var.getName(), var.getDataType().toString(),var.getRanges().toString(),var.getUnitsString()); | |
// 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 = var.getDimensions(); | |
for (int j = 0; j < dimensions.size(); j++) { | |
Dimension dim = dimensions.get(j); | |
try { | |
// the name of the dimension | |
System.out.print(dim.getName()); | |
System.out.print(", "); | |
// the type of the dimension | |
System.out.print(gid.getReferencedFile().findVariable(dim.getName()).findAttribute("_CoordinateAxisType").getStringValue()); | |
System.out.print(": "); | |
// the unit of the dimension | |
System.out.println(gid.getReferencedFile().findVariable(dim.getName()).getUnitsString()); | |
} catch (Exception e) { | |
System.out.println(); | |
continue; | |
} | |
} | |
// the data in the grid table | |
Array dataArray = var.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 < var.getSize(); j++) { | |
System.out.print(var.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(App.class.getName()).log(Level.SEVERE, null, ex); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment