package test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import ucar.nc2.grib.grib2.Grib2Gds;
import ucar.nc2.grib.grib2.Grib2Gds.LatLon;
import ucar.nc2.grib.grib2.Grib2Parameter;
import ucar.nc2.grib.grib2.Grib2Pds;
import ucar.nc2.grib.grib2.Grib2Record;
import ucar.nc2.grib.grib2.Grib2RecordScanner;
import ucar.nc2.grib.grib2.Grib2SectionBitMap;
import ucar.nc2.grib.grib2.Grib2SectionData;
import ucar.nc2.grib.grib2.Grib2SectionDataRepresentation;
import ucar.nc2.grib.grib2.Grib2SectionGridDefinition;
import ucar.nc2.grib.grib2.Grib2SectionIdentification;
import ucar.nc2.grib.grib2.Grib2SectionIndicator;
import ucar.nc2.grib.grib2.Grib2SectionLocalUse;
import ucar.nc2.grib.grib2.Grib2SectionProductDefinition;
import ucar.nc2.grib.grib2.table.Grib2Customizer;
import ucar.nc2.grib.grib2.table.NcepLocalParams;
import ucar.unidata.io.RandomAccessFile;
public class ReadGrib2 {
/**
* Method description goes here.
* @param args
*/
public static void main(String[] args) {
String path = File.class.getResource("/").getPath();
File f = new File(path);
String name = f.getParent() + File.separator
+ "gmf.639.2015060500003.grb2";
String indexFileName = f.getParent() + File.separator + "index.txt";
File index = new File(indexFileName);
BufferedWriter writer = null;
RandomAccessFile raf = null;
try {
if (index.exists()) {
index.delete();
} else {
index.createNewFile();
}
writer = new BufferedWriter(new FileWriter(index));
raf = new RandomAccessFile(name, "r");
Grib2RecordScanner scan = new Grib2RecordScanner(raf);
int i = 0;
while (scan.hasNext()) {
Grib2Record gr2 = scan.next();
// section 0
Grib2SectionIndicator iss = gr2.getIs();
StringBuilder section0 = new StringBuilder();
section0.append("section0 : ");
section0.append("StartingPosition ").append(iss.getStartPos())
.append(",");
section0.append("MessageLength ")
.append(iss.getMessageLength()).append(",");
section0.append("EndPos ").append(iss.getEndPos()).append(",");
// section 1
Grib2SectionIdentification ids = gr2.getId();
StringBuilder section1 = new StringBuilder();
section1.append("section1 : ").append(ids.toString());
// section 2
Grib2SectionLocalUse lus = gr2.getLocalUseSection();
StringBuilder section2 = new StringBuilder();
byte[] lu = lus.getRawBytes();
section2.append("section2 : ");
if (lu != null && lu.length != 0) {
section2.append("Length ").append(lu.length).append(",");
section2.append("str ").append(new String(lu)).append(",");
} else {
section2.append("Length 0,");
}
// section 3
Grib2SectionGridDefinition gds = gr2.getGDSsection();
Grib2Gds tempGds = gds.getGDS();
tempGds.getNxRaw();// 每行格数
tempGds.getNyRaw();// 行数
StringBuilder section3 = new StringBuilder();
section3.append("section3 : ");
section3.append("Length ").append(gds.getLength()).append(",");
section3.append("NumberPoints ")
.append(tempGds.getNyRaw() + "*" + tempGds.getNxRaw()
+ "=" + gds.getNumberPoints()).append(",");
String gdsStr = null;
if (tempGds.isLatLon()) {
// 经纬度范围
LatLon ll = (LatLon) tempGds;
String la = "la:" + ll.la1 + "~" + ll.la2 + ",deltaLat:"
+ ll.deltaLat + ",";
String lo = "lo:" + ll.lo1 + "~" + ll.lo2 + ",deltaLon:"
+ ll.deltaLon + ",";
gdsStr = la + lo;
}
section3.append(gdsStr);
// section 4
Grib2SectionProductDefinition pds = gr2.getPDSsection();
Grib2Pds tempPds = pds.getPDS();
// 参数层高由以下四个数值决定
int type1 = tempPds.getLevelType1();
double value1 = tempPds.getLevelValue1();
int type2 = tempPds.getLevelType2();
double value2 = tempPds.getLevelValue2();
// 参数类型由以下三个参数决定
int d = iss.getDiscipline();
int c = tempPds.getParameterCategory();
int n = tempPds.getParameterNumber();
int paramType = -1;// 0温度,1风向,2风速U分量,3风速V分量,4气压
if (d == 0 && c == 0 && n == 0) {
// 温度
paramType = 0;
}
if (d == 0 & c == 2 && n == 0) {
// 风向
paramType = 1;
}
if (d == 0 & c == 2 && n == 2) {
// 风速U风量
paramType = 2;
}
if (d == 0 & c == 2 && n == 3) {
// 风速V风量
paramType = 3;
}
if (d == 0 && c == 3 && n == 0) {
// 气压
paramType = 4;
}
// 从xml中读取参数类型
Grib2Parameter param = NcepLocalParams.getParameter(d, c, n);
if (param == null) {
if (paramType != -1) {
System.out.println("error");
}
continue;
}
// 从xml中读取层高类型
Grib2Customizer gc2 = Grib2Customizer.factory(gr2);
String type1str = gc2.getLevelName(type1);
String type2str = gc2.getLevelName(type2);
String s = type1str + ":" + value1;
if (!"Missing".equalsIgnoreCase(type2str)) {
s += "-" + value2;
}
StringBuilder section4 = new StringBuilder();
section4.append("section4 : ");
section4.append("Length:").append(pds.getLength()).append(",");
section4.append("d c n:").append(d + " " + c + " " + n)
.append(",");
section4.append("name:").append(param.name).append(",");
section4.append("unit:").append(param.unit).append(",");
section4.append("abbrev:").append(param.abbrev).append(",");
section4.append(s);
section4.append(",LevelType1:").append(type1).append(",");
// section 5
Grib2SectionDataRepresentation drs = gr2
.getDataRepresentationSection();
StringBuilder section5 = new StringBuilder();
section5.append("section5 : ");
section5.append("StartingPosition ")
.append(drs.getStartingPosition()).append(",");
section5.append("Length ").append(drs.getLength(raf))
.append(",");
section5.append("DataPoints ").append(drs.getDataPoints())
.append(",");
// section 6
Grib2SectionBi
- 1
- 2
前往页