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

输出:

评论 30
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

墨迹嘿嘿

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

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

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

打赏作者

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

抵扣说明:

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

余额充值