根据经纬度获取Grib2文件指定气象要素信息

1 篇文章 0 订阅
  1. 引入jar包
 <dependency>
            <groupId>edu.ucar</groupId>
            <artifactId>netcdfAll</artifactId>
            <version>5.5.2</version>
        </dependency>
  1. 创建实体类
@ApiModel("grib天气要素值信息")
@Data
public class GribValue {

    @ApiModelProperty("天气要素值所属时间")
    private LocalDateTime time;

    @ApiModelProperty("天气要素值(如取风速则为风-U)")
    private float value;

    @ApiModelProperty("天气要素值(如取风速则为风-V)")
    private float value2;
}

  1. grib2工具类
 /**
     * open the dataset, find the variable and its coordinate system
     *
     * @param path grib文件路径
     * @return 变量及坐标系信息
     * @throws IOException
     */
    public static GridDataset getGridDataset(String path) throws IOException {
        return GridDataset.open(path);
    }

    /**
     * 产品类型层次多个时调用改方法(2个层次值,目前暂时用于风速)
     *
     * @param gds 文件信息
     * @param lon 经度
     * @param lat 纬度
     * @return 气象要素值信息(含日期及对应值)
     * @throws IOException
     */
    public static List<GribValue> readMoreData(GridDataset gds, double lat, double lon) throws IOException {
        List<GridDatatype> gridVarLists = gds.getGrids();
        List<GridDatatype> gridDatatypes = new ArrayList<>();
        for (GridDatatype datatype : gridVarLists) {
            gridDatatypes.add(gds.findGridDatatype(datatype.getName()));
        }

        // 获取预测日期
        GridCoordSystem gcs = gridDatatypes.get(0).getCoordinateSystem();
        int[] xy = gcs.findXYindexFromLatLon(lat, lon, null);
        // use actual grid midpoint
        List<CalendarDate> calendarDates = gcs.getCalendarDates();

        // 获取时间以及对应的值
        List<GribValue> list = new ArrayList<>();
        GribValue gribValue = null;
        for (int i = 0; i < calendarDates.size(); i++) {
            Array data = gridDatatypes.get(0).readDataSlice(i, -1, xy[1], xy[0]);
            Array data2 = gridDatatypes.get(1).readDataSlice(i, -1, xy[1], xy[0]);
            gribValue = new GribValue();
            long millis = calendarDates.get(i).getMillis();
            Instant instant = Instant.ofEpochMilli(millis);
            ZoneId zoneId = ZoneId.of("UTC");
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            gribValue.setTime(localDateTime);
            gribValue.setValue(data.getFloat(data.getIndex()));
            gribValue.setValue2(data2.getFloat(data2.getIndex()));
            list.add(gribValue);
        }
        return list;


    }


    /**
     * 获取预报数值集合
     *
     * @param gds 文件信息
     * @param lon 经度
     * @param lat 纬度
     * @return 气象要素值信息(含日期及对应值)
     */
    public static List<GribValue> readData(GridDataset gds, double lat, double lon) throws IOException {
        List<GridDatatype> gridVarLists = gds.getGrids();
        /*
         * 变量及坐标系信息
         * The order of 4D test data is first, T axis, second, Z axis, followed
         * by y and x axis.
         */
        GridDatatype grid = gds.findGridDatatype(gridVarLists.get(0).getName());
        GridCoordSystem gcs = grid.getCoordinateSystem();
        int[] xy = gcs.findXYindexFromLatLon(lat, lon, null);
        // use actual grid midpoint
        LatLonPoint latlon = gcs.getLatLon(xy[0], xy[1]);
        List<CalendarDate> calendarDates = gcs.getCalendarDates();

        // 获取时间以及对应的值
        List<GribValue> list = new ArrayList<>();
        GribValue gribValue = null;
        for (int i = 0; i < calendarDates.size(); i++) {
            Array data = grid.readDataSlice(i, -1, xy[1], xy[0]);
            gribValue = new GribValue();
            long millis = calendarDates.get(i).getMillis();
            Instant instant = Instant.ofEpochMilli(millis);
            ZoneId zoneId = ZoneId.of("UTC");
            LocalDateTime localDateTime = instant.atZone(zoneId).toLocalDateTime();
            gribValue.setTime(localDateTime);
            gribValue.setValue(data.getFloat(data.getIndex()));
            list.add(gribValue);
        }
        return list;
    }

参考地址:https://docs.unidata.ucar.edu/netcdf-java/current/userguide/grid_datasets.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值