forked from DHI-GRAS/qgis-processing-swat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OSFWF_GetECMWFData.py
115 lines (100 loc) · 6.03 KB
/
OSFWF_GetECMWFData.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
"""
***************************************************************************
OSFWF_GetECMWFData.py
-------------------------------------
Copyright (C) 2014 TIGER-NET (www.tiger-net.org)
***************************************************************************
* This plugin is part of the Water Observation Information System (WOIS) *
* developed under the TIGER-NET project funded by the European Space *
* Agency as part of the long-term TIGER initiative aiming at promoting *
* the use of Earth Observation (EO) for improved Integrated Water *
* Resources Management (IWRM) in Africa. *
* *
* WOIS is a free software i.e. you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published *
* by the Free Software Foundation, either version 3 of the License, *
* or (at your option) any later version. *
* *
* WOIS is distributed in the hope that it will be useful, but WITHOUT ANY *
* WARRANTY; without even the implied warranty of MERCHANTABILITY or *
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
* for more details. *
* *
* You should have received a copy of the GNU General Public License along *
* with this program. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************
"""
import os
import shutil
from datetime import date, timedelta, datetime
import math
from PyQt4 import QtGui
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import *
from SWATAlgorithm import SWATAlgorithm
import GetECMWFClimateData
class OSFWF_GetECMWFData(SWATAlgorithm):
TMAX_DST_FOLDER = "TMAX_DST_FOLDER"
TMIN_DST_FOLDER = "TMIN_DST_FOLDER"
START_DATE = "START_DATE"
END_DATE = "END_DATE"
EMAIL = "EMAIL"
TOKEN = "TOKEN"
LEFT_LONG = "LEFT_LONG"
RIGHT_LONG = "RIGHT_LONG"
TOP_LAT = "TOP_LAT"
BOTTOM_LAT = "BOTTOM_LAT"
def __init__(self):
super(OSFWF_GetECMWFData, self).__init__(__file__)
def defineCharacteristics(self):
self.name = "1.3 - Get ECMWF data (OSFWF)"
self.group = "Operational simulation and forecasting workflow (OSFWF)"
self.addParameter(ParameterFile(OSFWF_GetECMWFData.TMAX_DST_FOLDER, "Select maximum temperature folder", True, False))
self.addParameter(ParameterFile(OSFWF_GetECMWFData.TMIN_DST_FOLDER, "Select minimum temperature folder", True, False))
self.addParameter(ParameterString(OSFWF_GetECMWFData.START_DATE, "Start date [yyyymmdd]. After 19790101.", "20120101", False))
self.addParameter(ParameterString(OSFWF_GetECMWFData.END_DATE, "End date [yyyymmdd]. It is recomended that no more than 6 months of data is downloaded at a time.", "20120601", False))
self.addParameter(ParameterString(OSFWF_GetECMWFData.EMAIL, "Email", "", False))
self.addParameter(ParameterString(OSFWF_GetECMWFData.TOKEN, "Token", "", False))
param = ParameterNumber(OSFWF_GetECMWFData.LEFT_LONG, "Left longitude", -180, 180, -20)
param.isAdvanced = True
self.addParameter(param)
param = ParameterNumber(OSFWF_GetECMWFData.RIGHT_LONG, "Right longitude", -180, 180, 55)
param.isAdvanced = True
self.addParameter(param)
param = ParameterNumber(OSFWF_GetECMWFData.TOP_LAT, "Top latitude", -89, 89, 40)
param.isAdvanced = True
self.addParameter(param)
param = ParameterNumber(OSFWF_GetECMWFData.BOTTOM_LAT, "Bottom latitude", -89, 89, -40)
param.isAdvanced = True
self.addParameter(param)
def processAlgorithm(self, progress):
# Get inputs
tmax_dst_folder = self.getParameterValue(OSFWF_GetECMWFData.TMAX_DST_FOLDER)
tmin_dst_folder = self.getParameterValue(OSFWF_GetECMWFData.TMIN_DST_FOLDER)
start_date = self.getParameterValue(OSFWF_GetECMWFData.START_DATE)
end_date = self.getParameterValue(OSFWF_GetECMWFData.END_DATE)
email = self.getParameterValue(OSFWF_GetECMWFData.EMAIL)
token = self.getParameterValue(OSFWF_GetECMWFData.TOKEN)
# Setting coordinates
left_long = math.floor(self.getParameterValue(OSFWF_GetECMWFData.LEFT_LONG))
right_long = math.ceil(self.getParameterValue(OSFWF_GetECMWFData.RIGHT_LONG))
top_lat = math.ceil(self.getParameterValue(OSFWF_GetECMWFData.TOP_LAT))
bottom_lat = math.ceil(self.getParameterValue(OSFWF_GetECMWFData.BOTTOM_LAT))
# Check coordinates
if (left_long >= right_long) or (bottom_lat >= top_lat):
raise GeoAlgorithmExecutionException('Error in coordinates: \"Left :' + str(left_long) + \
'< Right: ' + str(right_long) + ', Top :' + str(top_lat) + '> Bottom :' + str(bottom_lat) + '\"')
# Get dates
try:
start_date = datetime.strptime(start_date, "%Y%m%d").date()
except:
raise GeoAlgorithmExecutionException('Error in data format: \"' + start_date + '\". Must be in YYYYMMDD.')
try:
end_date = datetime.strptime(end_date, "%Y%m%d").date()
except:
raise GeoAlgorithmExecutionException('Error in data format: ' + end_date + '\". Must be in YYYYMMDD.')
if os.path.isdir(tmax_dst_folder) and os.path.isdir(tmin_dst_folder):
# Download, extract and translate to GeoTIFF
GetECMWFClimateData.ECMWFImport(email, token, start_date, end_date, tmax_dst_folder, tmin_dst_folder, left_long, right_long, top_lat, bottom_lat, progress)
else:
raise GeoAlgorithmExecutionException('No such directory: \"' + dst_folder + '\" ')