-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_dataset.py
243 lines (184 loc) · 14.3 KB
/
create_dataset.py
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# Built-in modules
import os
from os import listdir as list_dir
import pandas as pd
import warnings
warnings.filterwarnings('ignore')
warnings.simplefilter('ignore')
## This function to merge data based on same date & location
def create_dataset(target_points, model:str, AOI:str, dataset:list, dates):
assert model in ['NO2', 'PM2_5']
assert AOI in ['Italy', 'California','South_Africa']
if dates is not None: ## This is for validation dataset
if model == 'NO2':
pred_df = dataset[8]
pred_df = pred_df[pred_df['date'].isin(list(dates['date']))]
pred_df = pd.merge(pred_df, dates, how="left", on=["date"])
## Inner join by time & coordinates (CAMS - PM 2.5)
pred_df = pd.merge(pred_df, dataset[0], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[1], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[2], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[3], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (CAMS - NO2 surface)
pred_df = pd.merge(pred_df, dataset[4], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df,dataset[5], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[6], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[7], on=['date','hour','lon', 'lat'], how='inner')
if AOI == 'Italy' or AOI == 'California':
## Inner join by time & coordinates (S5P - NO2 & UV Aerosol Index)
pred_df = pd.merge(pred_df, dataset[9], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (ERA5)
pred_df = pd.merge(pred_df, dataset[10], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[11], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[12], on=['date', 'hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[13], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (MODIS)
pred_df = pd.merge(pred_df, dataset[14], on=['date','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[15], on=['date','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[16], on=['date','lon', 'lat'], how='inner')
## Inner join by coordinates (Land cover)
pred_df = pd.merge(pred_df, dataset[17], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[18], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[19], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[20], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[21], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[22], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[23], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[24], on=['lon', 'lat'], how='inner')
if AOI == 'Italy':
pred_df = pd.merge(pred_df, dataset[25], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[26], on=['lon', 'lat'], how='inner')
return pred_df
elif model == 'PM2_5':
pred_df = dataset[0]
pred_df = pred_df[pred_df['date'].isin(list(dates['date']))]
pred_df = pd.merge(pred_df, dates, how="left", on=["date"])
## Inner join by time & coordinates (CAMS - PM 2.5)
pred_df = pd.merge(pred_df, dataset[1], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[2], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[3], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (CAMS - NO2 surface)
pred_df = pd.merge(pred_df, dataset[4], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[5], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[6], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[7], on=['date','hour','lon', 'lat'], how='inner')
if AOI == 'Italy' or AOI == 'California':
## Inner join by time & coordinates (S5P - NO2 & UV Aerosol Index)
pred_df = pd.merge(pred_df, dataset[8], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[9], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (ERA5)
pred_df = pd.merge(pred_df, dataset[10], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[11], on=['date','hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[12], on=['date', 'hour','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[13], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (MODIS)
pred_df = pd.merge(pred_df, dataset[14], on=['date','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[15], on=['date','lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[16], on=['date','lon', 'lat'], how='inner')
## Inner join by coordinates (Land cover)
pred_df = pd.merge(pred_df, dataset[17], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[18], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[19], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[20], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[21], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[22], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[23], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[24], on=['lon', 'lat'], how='inner')
if AOI == 'Italy':
pred_df = pd.merge(pred_df, dataset[25], on=['lon', 'lat'], how='inner')
pred_df = pd.merge(pred_df, dataset[26], on=['lon', 'lat'], how='inner')
return pred_df
else: ## This is for training dataset
if AOI == 'California' and model == 'PM2_5' : # There are stations at the same location for California - here we calculated the mean value of those stations
pm25_gt = target_points
pm25g_gt_mean = pm25_gt.groupby(['SITE_LONGI','SITE_LATIT','Date']).mean()
pm25g_gt_mean= pm25g_gt_mean.rename(columns={"AirQuality": "avg_AirQuality"})
pm25_gt = pd.merge(pm25_gt, pm25g_gt_mean, on=['SITE_LONGI','SITE_LATIT','Date'], how='inner')
pm25_gt= pm25_gt.drop(columns=['AirQuality'])
pm25_gt= pm25_gt.rename(columns={"avg_AirQuality": "AirQuality"})
pm25_gt = pm25_gt.drop_duplicates()
target_points = pm25_gt
# Rround corrdinates of stations to 6 digits - to be the same as the coordinates of extracted data
target_points= target_points.rename(columns={"SITE_LATIT": "lat", "SITE_LONGI": "lon"})
for i, point in enumerate(target_points['geometry']):
target_points.loc[i,'lon'] = round(point.xy[0][0],6)
target_points.loc[i,'lat'] = round(point.xy[1][0],6)
target_points['date'] = target_points['Date']
target_points = target_points.dropna()
if model == 'PM2_5':
## Inner join by time & coordinates (CAMS - PM 2.5)
train_df = pd.merge(target_points, dataset[0], on=['date','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[1], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[2], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[3], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (CAMS - NO2 surface)
train_df = pd.merge(train_df, dataset[4], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[5], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[6], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[7], on=['date','hour','lon', 'lat'], how='inner')
if AOI == 'Italy' or AOI == 'California':
## Inner join by time & coordinates (S5P - NO2 & UV Aerosol Index)
train_df = pd.merge(train_df, dataset[8], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[9], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (ERA5)
train_df = pd.merge(train_df, dataset[10], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[11], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[12], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[13], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (MODIS)
train_df = pd.merge(train_df, dataset[14], on=['date','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[15], on=['date','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[16], on=['date','lon', 'lat'], how='inner')
## Inner join by coordinates (Land cover)
train_df = pd.merge(train_df, dataset[17], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[18], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[19], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[20], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[21], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[22], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[23], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[24], on=['lon', 'lat'], how='inner')
if AOI == 'Italy':
train_df = pd.merge(train_df, dataset[25], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[26], on=['lon', 'lat'], how='inner')
return train_df
elif model == 'NO2':
## Inner join by time & coordinates (S5P - NO2)
train_df = pd.merge(target_points, dataset[8], on=['date','lon', 'lat'], how='inner')
## Inner join by time & coordinates (CAMS - PM 2.5)
train_df = pd.merge(train_df, dataset[0], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[1], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[2], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[3], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (CAMS - NO2 surface)
train_df = pd.merge(train_df, dataset[4], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[5], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[6], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[7], on=['date','hour','lon', 'lat'], how='inner')
if AOI == 'Italy' or AOI == 'California':
## Inner join by time & coordinates (S5P - UV Aerosol Index)
train_df = pd.merge(train_df, dataset[9], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (ERA5)
train_df = pd.merge(train_df, dataset[10], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[11], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[12], on=['date','hour','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[13], on=['date','hour','lon', 'lat'], how='inner')
## Inner join by time & coordinates (MODIS)
train_df = pd.merge(train_df, dataset[14], on=['date','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[15], on=['date','lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[16], on=['date','lon', 'lat'], how='inner')
## Inner join by coordinates (Land cover)
train_df = pd.merge(train_df, dataset[17], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[18], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[19], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[20], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[21], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[22], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[23], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[24], on=['lon', 'lat'], how='inner')
if AOI == 'Italy':
train_df = pd.merge(train_df, dataset[25], on=['lon', 'lat'], how='inner')
train_df = pd.merge(train_df, dataset[26], on=['lon', 'lat'], how='inner')
## convert the unit of stations to the same unit of s5p NO2
train_df['AirQuality'] = (train_df['AirQuality'] / (1.0e-15 * 1.9125))/(6.02214e+19)
return train_df