-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRainfallGridIntegrator.java
More file actions
113 lines (96 loc) · 4.34 KB
/
Copy pathRainfallGridIntegrator.java
File metadata and controls
113 lines (96 loc) · 4.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package heyingzhe;
import java.util.*;
public class RainfallGridIntegrator {
// 定义一个类来封装站点的经纬度和站点名
public static class Station {
public String name;
public double latitude;
public double longitude;
public String id;
public Station(String name, double latitude, double longitude, String id) {
this.name = name;
this.latitude = latitude;
this.longitude = longitude;
this.id = id;
}
}
// 定义一个类来封装降雨量数据
public static class PrecipitationData {
public String date;
public double prcp_mm;
public PrecipitationData(String date, double prcp_mm) {
this.date = date;
this.prcp_mm = prcp_mm;
}
}
// 整合数据并返回二维数组的方法
public static double[][] integrateRainfallToGrid(
List<Station> stations, // 站点数据
Map<String, List<PrecipitationData>> stationDataMap, // 站点降雨数据
int ncols, int nrows, // DEM 网格的列数和行数
double xllcorner, double yllcorner, // DEM 数据的最左下角经纬度
double cellsize, // DEM 网格的大小(单位:度)
String targetDate // 目标日期
) {
// 创建一个二维数组来存储降雨量数据
double[][] rainfallGrid = new double[nrows][ncols];
// 初始化数组,所有元素默认为0
for (int i = 0; i < nrows; i++) {
Arrays.fill(rainfallGrid[i], 0.0);
}
// 遍历所有站点并计算它们在网格中的行列位置
for (Station station : stations) {
// 计算该站点在网格中的位置 (行列)
int row = (int) Math.floor((station.latitude - yllcorner) / cellsize);
int col = (int) Math.floor((station.longitude - xllcorner) / cellsize);
// 确保计算出的行列号在网格范围内
if (row >= 0 && row < nrows && col >= 0 && col < ncols) {
// 获取该站点在目标日期的降雨量数据
List<PrecipitationData> precipitationDataList = stationDataMap.get(station.name);
if (precipitationDataList != null) {
for (PrecipitationData data : precipitationDataList) {
if (data.date.equals(targetDate)) {
// 将降雨量数据赋值到二维数组的对应位置
rainfallGrid[row][col] += data.prcp_mm;
break; // 找到目标日期后停止查找
}
}
}
}
}
return rainfallGrid;
}
public static void main(String[] args) {
// 示例数据
List<Station> stations = new ArrayList<>();
stations.add(new Station("NANCHENG", 27.5833333, 116.65, "1"));
stations.add(new Station("SHAOWU", 27.3333333, 117.4666667, "2"));
Map<String, List<PrecipitationData>> stationDataMap = new HashMap<>();
stationDataMap.put("NANCHENG", Arrays.asList(
new PrecipitationData("2023-07-05", 6.604),
new PrecipitationData("2023-07-06", 0.0)
));
stationDataMap.put("SHAOWU", Arrays.asList(
new PrecipitationData("2023-07-05", 3.2),
new PrecipitationData("2023-07-06", 0.0)
));
// DEM 数据
int ncols = 2208;
int nrows = 1612;
double xllcorner = 116.0531570965;
double yllcorner = 24.890489306101;
double cellsize = 0.0020154416431345;
String targetDate = "2023-07-05"; // 目标日期
// 调用方法进行数据整合
double[][] rainfallGrid = integrateRainfallToGrid(
stations, stationDataMap, ncols, nrows, xllcorner, yllcorner, cellsize, targetDate
);
// 输出结果(只输出部分数据,确保不打印过多)
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
System.out.printf("%.2f ", rainfallGrid[i][j]);
}
System.out.println();
}
}
}