赞
踩
JAVA解析气象GRB2数据
数据展示上图:
数据展示:
- public static Map<String, Object> readGrib(String path, String needElement, Double lon, Double lat) {
- SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
- File originFile = new File(path);
- Map<String, Object> resultMap = new HashMap<>();
- if (originFile.exists()) {
- RandomAccessFile raf = null;
- try {
- raf = new RandomAccessFile(path, "r");
- Grib2RecordScanner scanner = new Grib2RecordScanner(raf);
- while (scanner.hasNext()) {
- Grib2Record grib2Record = scanner.next();
- Grib2SectionIdentification ids = grib2Record.getId();
- /**==========================日期处理=======================*/
- // long millis = ids.getReferenceDate().getMillis();
- //Date dTime = new Date(millis);
- // int hour = ids.getHour();
- /**==========================日期处理=======================*/
- Grib2SectionGridDefinition gdSsection = grib2Record.getGDSsection();
- Grib2Gds gds = gdSsection.getGDS();
- if (gds.isLatLon()) {
- Grib2Gds.LatLon ll = (Grib2Gds.LatLon) gds;
- //经纬度、分辨率、范围值获取
- float startLat = ll.la1;
- float endLat = ll.la2;
- float startLon = ll.lo1;
- float endLon = ll.lo2;
- float resolutionLat = ll.deltaLat;
- float resolutionLon = ll.deltaLon;
- int lonCount = ll.getNxRaw();
- int latCount = ll.getNyRaw();
- int indexGrib2 = Grib2Utils.inLatLon(startLat, endLat, startLon, endLon, resolutionLat, resolutionLon, lonCount, latCount, lon, lat);
- if (indexGrib2 == -1) {
- resultMap.put("code", "ERROR");
- return resultMap;
- }
- Grib2SectionProductDefinition pdSsection = grib2Record.getPDSsection();
- Grib2Pds pds = pdSsection.getPDS();
- //时效
- // int valid = pds.getForecastTime();
- //种类
- int c = pds.getParameterCategory();
- //参数
- int n = pds.getParameterNumber();
- // int level = (int) pds.getLevelValue1();
- Grib2SectionIndicator iss = grib2Record.getIs();
- //产品状态
- int d = iss.getDiscipline();
- if (path.contains("QC")) {
- n = 0;
- }
- Grib2Parameter param = NcepLocalParams.getParameter(d, c, n);
- if (param == null) {
- continue;
- }
- //获取单位和要素国际名称
- // String unit = param.unit;
- String name = param.getName();
- // String elementVal = param.abbrev;
- if (!name.contains(needElement)) {
- continue;
- }
- //数据读取
- Grib2SectionDataRepresentation drs = grib2Record.getDataRepresentationSection();
- float[] datas = grib2Record.readData(raf, drs.getStartingPosition());
- for(float f : datas){
- System.out.println("数据: " + Double.valueOf((f+"").trim()));
- }
- int length = datas.length;
- if (indexGrib2 > length) {
- resultMap.put("code", "ERROR");
- }
- // String time = sdf.format(dTime);
- resultMap.put("code", "SUCCESS");
- resultMap.put("data", Arith.round(datas[indexGrib2], 2));
- }
- }
- raf.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (raf != null) {
- try {
- raf.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- return resultMap;
- }
测试:
- public static void main(String[] args) {
- Map<String, Object> map = Grib2Utils.readGrib("D:\\tmp\\CLDAS\\20210330000000\\Z_NAFP_C_BABJ_20210330000805_P_CLDAS_RT_CHN_0P05_HOR-TEM-2021033000.GRB2","",103.2,38.5);
- System.out.println("目前经纬度上的值为: " + map);
- }
输出:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。