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

1 篇文章 0 订阅
该文介绍如何引入ucar库的netcdfAlljar包来处理Grib2文件,包括创建GribValue实体类存储天气要素值信息,以及提供两个方法readMoreData和readData,用于按层次读取多时间点的经纬度对应的气象要素值。文章通过GridDataset接口获取变量和坐标系统,然后解析时间并提取数据。
摘要由CSDN通过智能技术生成
  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
    评论
Java可以通过使用特定的库来解析grib2文件。这些库提供了一些可以读取和处理grib2文件的类和方法。解析grib2文件的过程主要包括以下几个步骤: 1. 导入相应的库和类:首先,我们需要在Java项目中导入用于解析grib2文件的库,例如NetCDF库或者GRIB库。 2. 打开grib2文件:使用库提供的方法,我们可以打开grib2文件,并将其作为输入流读取到内存中。 3. 解析grib2文件:通过读取文件的结构和元数据,我们可以确定文件中包含的各种数据集的类型、时间范围、空间范围等信息。这些信息可以通过使用库提供的一些类和方法来解析和获取。 4. 提取数据:通过解析grib2文件的结构和元数据,我们可以确定文件中包含的数据集的位置和格式。通过提供相应的索引或坐标,我们可以从文件中提取出特定的数据集。 5. 数据处理:一旦我们提取了所需的数据集,我们可以对其进行进一步的处理和分析。例如,我们可以将数据转换为特定的单位、重新采样、插值等,以便于进一步使用或可视化。 6. 关闭文件:在完成对grib2文件的解析和数据处理后,我们应该关闭文件,释放内存资源。可以使用库提供的相应方法来关闭文件。 总之,使用Java解析grib2文件主要依赖于特定的库和类。通过这些库和类,我们可以打开、解析和提取grib2文件中的数据集,并对其进行处理和分析。这样,我们可以利用Java的强大功能来处理气象和气候数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值