当前位置:   article > 正文

Java进行解析气象数据格式GRB2 ,根据经纬度查询某一格点数值_java读取grib2

java读取grib2

JAVA解析气象GRB2数据

数据展示上图:

数据展示:

  1. public static Map<String, Object> readGrib(String path, String needElement, Double lon, Double lat) {
  2. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  3. File originFile = new File(path);
  4. Map<String, Object> resultMap = new HashMap<>();
  5. if (originFile.exists()) {
  6. RandomAccessFile raf = null;
  7. try {
  8. raf = new RandomAccessFile(path, "r");
  9. Grib2RecordScanner scanner = new Grib2RecordScanner(raf);
  10. while (scanner.hasNext()) {
  11. Grib2Record grib2Record = scanner.next();
  12. Grib2SectionIdentification ids = grib2Record.getId();
  13. /**==========================日期处理=======================*/
  14. // long millis = ids.getReferenceDate().getMillis();
  15. //Date dTime = new Date(millis);
  16. // int hour = ids.getHour();
  17. /**==========================日期处理=======================*/
  18. Grib2SectionGridDefinition gdSsection = grib2Record.getGDSsection();
  19. Grib2Gds gds = gdSsection.getGDS();
  20. if (gds.isLatLon()) {
  21. Grib2Gds.LatLon ll = (Grib2Gds.LatLon) gds;
  22. //经纬度、分辨率、范围值获取
  23. float startLat = ll.la1;
  24. float endLat = ll.la2;
  25. float startLon = ll.lo1;
  26. float endLon = ll.lo2;
  27. float resolutionLat = ll.deltaLat;
  28. float resolutionLon = ll.deltaLon;
  29. int lonCount = ll.getNxRaw();
  30. int latCount = ll.getNyRaw();
  31. int indexGrib2 = Grib2Utils.inLatLon(startLat, endLat, startLon, endLon, resolutionLat, resolutionLon, lonCount, latCount, lon, lat);
  32. if (indexGrib2 == -1) {
  33. resultMap.put("code", "ERROR");
  34. return resultMap;
  35. }
  36. Grib2SectionProductDefinition pdSsection = grib2Record.getPDSsection();
  37. Grib2Pds pds = pdSsection.getPDS();
  38. //时效
  39. // int valid = pds.getForecastTime();
  40. //种类
  41. int c = pds.getParameterCategory();
  42. //参数
  43. int n = pds.getParameterNumber();
  44. // int level = (int) pds.getLevelValue1();
  45. Grib2SectionIndicator iss = grib2Record.getIs();
  46. //产品状态
  47. int d = iss.getDiscipline();
  48. if (path.contains("QC")) {
  49. n = 0;
  50. }
  51. Grib2Parameter param = NcepLocalParams.getParameter(d, c, n);
  52. if (param == null) {
  53. continue;
  54. }
  55. //获取单位和要素国际名称
  56. // String unit = param.unit;
  57. String name = param.getName();
  58. // String elementVal = param.abbrev;
  59. if (!name.contains(needElement)) {
  60. continue;
  61. }
  62. //数据读取
  63. Grib2SectionDataRepresentation drs = grib2Record.getDataRepresentationSection();
  64. float[] datas = grib2Record.readData(raf, drs.getStartingPosition());
  65. for(float f : datas){
  66. System.out.println("数据: " + Double.valueOf((f+"").trim()));
  67. }
  68. int length = datas.length;
  69. if (indexGrib2 > length) {
  70. resultMap.put("code", "ERROR");
  71. }
  72. // String time = sdf.format(dTime);
  73. resultMap.put("code", "SUCCESS");
  74. resultMap.put("data", Arith.round(datas[indexGrib2], 2));
  75. }
  76. }
  77. raf.close();
  78. } catch (Exception e) {
  79. e.printStackTrace();
  80. } finally {
  81. if (raf != null) {
  82. try {
  83. raf.close();
  84. } catch (IOException e) {
  85. e.printStackTrace();
  86. }
  87. }
  88. }
  89. }
  90. return resultMap;
  91. }

测试:

  1. public static void main(String[] args) {
  2. 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);
  3. System.out.println("目前经纬度上的值为: " + map);
  4. }

输出:

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小丑西瓜9/article/detail/293781
推荐阅读
相关标签
  

闽ICP备14008679号