-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathDownloadECMWFClimateData.py
288 lines (254 loc) · 7.6 KB
/
DownloadECMWFClimateData.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
from ECMWFDataServer import ECMWFDataServer
from datetime import date, timedelta, datetime
import os
import processing
from processing.tools import dataobjects
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly
import shutil
def ECMWFImport(
email,
token,
startdate,
enddate,
tmax_dst_folder,
tmin_dst_folder,
LeftLon,
RightLon,
TopLat,
BottomLat,
progress,
):
"""Importing ECMWF temperature data using the
ECMWFDataServer class provided by ECMWF"""
DownloadDirectory = os.path.join(tmax_dst_folder, "Temporary")
# Create Temp download folder
if not os.path.isdir(DownloadDirectory):
os.mkdir(DownloadDirectory)
# Get max enddate (updated once a month, with two months delay: for June 2016, end of March)
# and min startdate (1979-01-01)
enddate_adjust = (datetime.now() - timedelta(days=60)).date()
max_enddate = date(enddate_adjust.year, enddate_adjust.month, 1) - timedelta(days=1)
min_startdate = datetime.strptime(("1979-01-01"), "%Y-%m-%d").date()
if startdate < min_startdate:
startdate = min_startdate
progress.setConsoleInfo(
"Start date corrected to: " + startdate.strftime("%Y%m%d") + "..."
)
if enddate > max_enddate:
enddate = max_enddate
progress.setConsoleInfo(
"End date corrected to: " + enddate.strftime("%Y%m%d") + "..."
)
if startdate > max_enddate:
return
# Get dates
FirstYear = startdate.year
FirstMonth = startdate.month
FirstDay = startdate.day
LastYear = enddate.year
LastMonth = enddate.month
LastDay = enddate.day
# Start data server
server = ECMWFDataServer("https://api.ecmwf.int/v1", token, email)
# Run all at a time
dst_file = os.path.join(
DownloadDirectory,
startdate.strftime("%Y%m%d") + "_to_" + enddate.strftime("%Y%m%d") + ".grb",
)
GetECMWF(
server,
FirstYear,
FirstMonth,
FirstDay,
LastYear,
LastMonth,
LastDay,
LeftLon,
RightLon,
TopLat,
BottomLat,
dst_file,
progress,
)
tiff_filelist = gdal2GeoTiff_ECMWF_WGS84(dst_file, progress)
Max_Daily_FileList, Min_Daily_FileList = ECMWF2DailyMaps(tiff_filelist, progress)
# Move files and clean up
for f in Max_Daily_FileList:
try:
shutil.copy(f, os.path.join(tmax_dst_folder, os.path.split(f)[1]))
except:
pass
for f in Min_Daily_FileList:
try:
shutil.copy(f, os.path.join(tmin_dst_folder, os.path.split(f)[1]))
except:
pass
for f in os.listdir(DownloadDirectory):
try:
os.remove(f)
except:
pass
try:
shutil.rmtree(DownloadDirectory) # Remove Temp dir
except:
pass
server = None
def GetECMWF(
server,
FirstYear,
FirstMonth,
FirstDay,
LastYear,
LastMonth,
LastDay,
LeftLon,
RightLon,
TopLat,
BottomLat,
dst_file,
progress,
):
progress.setConsoleInfo(
"Sending data request to ECMWF. It might take a long time to get it "
+ "processed, please be patient..."
)
server.retrieve(
{
"dataset": "interim",
"date": str(FirstYear)
+ "-"
+ str(FirstMonth).zfill(2)
+ "-"
+ str(FirstDay).zfill(2)
+ "/to/"
+ str(LastYear)
+ "-"
+ str(LastMonth).zfill(2)
+ "-"
+ str(LastDay).zfill(2),
"time": "00:00:00/06:00:00/12:00:00/18:00:00",
"grid": "0.75/0.75",
"step": "0",
"levtype": "sfc",
"type": "an",
"param": "167.128",
"area": str(TopLat)
+ "/"
+ str(LeftLon)
+ "/"
+ str(BottomLat)
+ "/"
+ str(RightLon),
"target": dst_file,
}
)
return
def gdal2GeoTiff_ECMWF_WGS84(Filename, progress):
progress.setConsoleInfo("Translating to GeoTIFF...")
tiff_filename_base = os.path.split(Filename)[0] + os.sep
tiff_filelist = []
d = datetime(1970, 1, 1)
# Read raster bands from file
data = gdal.Open(Filename, GA_ReadOnly)
number_of_bands = data.RasterCount
# Get extent
extent = dataobjects.extent([Filename])
for i in range(1, number_of_bands + 1):
# data = gdal.Open(Filename, GA_ReadOnly)
band = data.GetRasterBand(i)
htime = band.GetMetadata()["GRIB_REF_TIME"]
userange = len(htime) - 7
UTCtime_delta = int(band.GetMetadata()["GRIB_REF_TIME"][0:userange])
# data = None
tiff_filename = (
tiff_filename_base
+ str((d + timedelta(seconds=UTCtime_delta)).year)
+ str((d + timedelta(seconds=UTCtime_delta)).month).zfill(2)
+ str((d + timedelta(seconds=UTCtime_delta)).day).zfill(2)
+ "_"
+ str((d + timedelta(seconds=UTCtime_delta)).hour).zfill(2)
+ "ECMWF.tif"
)
# Convert to GeoTIFF using processing GDAL
param = {
"INPUT": Filename,
"OUTSIZE": 100,
"OUTSIZE_PERC": True,
"NO_DATA": "",
"EXPAND": 0,
"SRS": "EPSG:4326",
"PROJWIN": extent,
"SDS": False,
"EXTRA": "-b " + str(i),
"OUTPUT": tiff_filename,
}
processing.runalg("gdalogr:translate", param)
tiff_filelist.append(tiff_filename)
data = None
return tiff_filelist
def ECMWF2DailyMaps(filelist, progress):
progress.setConsoleInfo("Computing daily maps...")
# Get all days
dates = []
Tmax_Daily_FileList = []
Tmin_Daily_FileList = []
for f in filelist:
dates.append(os.path.split(f)[1][0:8])
# Get unique dates
unique_dates = list(set(dates))
layer = dataobjects.getObjectFromUri(filelist[0])
extent = (
str(layer.extent().xMinimum())
+ ","
+ str(layer.extent().xMaximum())
+ ","
+ str(layer.extent().yMinimum())
+ ","
+ str(layer.extent().yMaximum())
)
# Calculate daily maps
for datestr in unique_dates:
# If all four daily maps exist
if dates.count(datestr) == 4:
maps = []
# Get the four maps
for f in filelist:
if datestr in f:
maps.append(f)
# Do map calculations using processing GRASS
# TMAX
out_file = (
os.path.split(maps[0])[0]
+ os.sep
+ datestr
+ "_TMAX_"
+ "ECMWF"
+ ".tif"
)
Tmax_Daily_FileList.append(out_file)
formula = "maximum(maximum(A,B),maximum(C,D))"
doMapCalc(maps, out_file, formula)
# TMIN
out_file = (
os.path.split(maps[0])[0]
+ os.sep
+ datestr
+ "_TMIN_"
+ "ECMWF"
+ ".tif"
)
Tmin_Daily_FileList.append(out_file)
formula = "minimum(minimum(A,B),minimum(C,D))"
doMapCalc(maps, out_file, formula)
return Tmax_Daily_FileList, Tmin_Daily_FileList
def doMapCalc(maps, out_file, formula):
param = {
"INPUT_A": maps[0],
"INPUT_B": maps[1],
"INPUT_C": maps[2],
"INPUT_D": maps[3],
"FORMULA": formula,
"OUTPUT": out_file,
}
processing.runalg("gdalogr:rastercalculator", param)