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

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);
    }

输出:

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 30
    评论
GRIB(GRIdded Binary)是一种常用的气象数据格式,通常用于存储和传输大气和海洋模型的输出数据。Java中可以使用NetCDF-Java库来解析GRIB2文件。 以下是使用NetCDF-Java解析GRIB2文件的示例代码: ``` // 导入相关的包 import ucar.nc2.grib.Grib2Data; import ucar.nc2.grib.Grib2DataSection; import ucar.nc2.grib.Grib2Record; import ucar.nc2.grib.Grib2SectionDataRepresentation; import ucar.nc2.grib.Grib2SectionGridDefinition; import ucar.nc2.grib.GribNumbers; import ucar.nc2.grib.grib2.Grib2Parameter; import ucar.nc2.grib.grib2.Grib2Pds; import ucar.unidata.io.RandomAccessFile; // 定义要解析GRIB2文件的路径 String filePath = "path/to/grib2/file"; // 打开文件 RandomAccessFile raf = new RandomAccessFile(filePath, "r"); // 读取文件中的每个GRIB2记录 while (raf.getFilePointer() < raf.length()) { Grib2Record record = new Grib2Record(raf); // 从记录中获取相应的数据 Grib2Pds pds = record.getPDS(); Grib2Parameter parameter = pds.getParameter(); Grib2SectionGridDefinition gdss = record.getGDSsection(); Grib2SectionDataRepresentation drs = record.getDRSsection(); Grib2DataSection dataSection = record.getDataSection(); int[] data = Grib2Data.getData(dataSection.getStartingPosition(), drs.getDataTemplate(), gdss.getNumberPoints(), raf); // 处理数据... } // 关闭文件 raf.close(); ``` 在上面的代码中,我们使用NetCDF-Java库中的`Grib2Record`类来读取GRIB2记录。通过这个类,我们可以获取PDS、GDS、DRS和数据部分的信息,并使用`Grib2Data`类来解码数据。 这只是一个简单的示例,具体的解析过程可能因为GRIB2文件的特性而有所不同。如果您需要更详细的信息,建议查看NetCDF-Java库的文档或参考其他开源的GRIB2解析库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

墨迹嘿嘿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值