- 引入jar包
<dependency>
<groupId>edu.ucar</groupId>
<artifactId>netcdfAll</artifactId>
<version>5.5.2</version>
</dependency>
- 创建实体类
@ApiModel("grib天气要素值信息")
@Data
public class GribValue {
@ApiModelProperty("天气要素值所属时间")
private LocalDateTime time;
@ApiModelProperty("天气要素值(如取风速则为风-U)")
private float value;
@ApiModelProperty("天气要素值(如取风速则为风-V)")
private float value2;
}
- 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