-
Notifications
You must be signed in to change notification settings - Fork 5
/
ClimateStations.py
244 lines (227 loc) · 10.4 KB
/
ClimateStations.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
from datetime import date, timedelta
import datetime
import os
import numpy
from processing.core.GeoAlgorithmExecutionException import (
GeoAlgorithmExecutionException,
)
class ClimateStations:
def __init__(self, stations_filename):
if os.path.isfile(stations_filename):
self.station_filename = stations_filename
self.station_id = []
self.station_lati = []
self.station_long = []
self.station_elev = []
# Reading station file
with open(stations_filename, "r") as station_file:
for line in station_file.readlines()[1:]:
info = line.split(",")
try:
self.station_id.append(info[1][0:6])
self.station_lati.append(float(info[2][0:6]))
self.station_long.append(float(info[3][0:6]))
self.station_elev.append(int(info[4][0:4]))
except ValueError:
pass
# Convert id's to dictionary
self.station_id_dict = {}
for i in self.station_id:
self.station_id_dict[int(i)] = i
else:
raise GeoAlgorithmExecutionException(
'No such file: "' + stations_filename + '" '
)
def readPcpFiles(self, log_file):
"""Reading the Budyko .pcp files for all stations in station file"""
station_folder = os.path.split(self.station_filename)[0]
# Sort id's
station_id_sorted = sorted(self.station_id_dict)
# Reading .pcp files to numpy array
pcp_array = None
pcp_dates = []
for i in station_id_sorted:
station_pcp_filename = os.path.join(
station_folder, self.station_id_dict[i] + ".txt"
)
if os.path.isfile(station_pcp_filename):
with open(station_pcp_filename, "r") as pcp_file:
lines = pcp_file.readlines()
lines_number = len(lines)
if pcp_array is None:
pcp_array = numpy.zeros(
[lines_number - 1, len(station_id_sorted)],
dtype=numpy.float,
)
for n in range(lines_number):
if n == 0 and i == 1:
t = datetime.datetime.strptime(lines[0][0:8], "%Y%m%d")
if lines_number == 1:
tt_first = (t - timedelta(days=1)).timetuple()
julian_tfirst = "%d%03d" % (
tt_first.tm_year,
tt_first.tm_yday,
)
tt = t.timetuple()
julian_t = "%d%03d" % (tt.tm_year, tt.tm_yday)
else:
tt = t.timetuple()
julian_t = "%d%03d" % (tt.tm_year, tt.tm_yday)
pcp_dates.append(julian_t)
else:
if i == 1:
tt = (t + timedelta(days=n)).timetuple()
julian_t = "%d%03d" % (tt.tm_year, tt.tm_yday)
pcp_dates.append(julian_t)
if n > 0:
pcp_array[n - 1, i - 1] = float(lines[n])
else:
raise GeoAlgorithmExecutionException(
'No pcp file found: "' + station_pcp_filename + '" '
)
if pcp_dates:
# From Julian day to date
last_pcp_date = date(int(pcp_dates[-1][0:4]), 1, 1) + timedelta(
days=int(pcp_dates[-1][4:7]) - 1
)
first_pcp_date = date(int(pcp_dates[0][0:4]), 1, 1) + timedelta(
days=int(pcp_dates[0][4:7]) - 1
)
log_file.write(
"First day of pcp in .pcp file: %s \n"
% first_pcp_date.strftime("%Y%m%d")
)
log_file.write(
"Last day of pcp in .pcp file: %s \n" % last_pcp_date.strftime("%Y%m%d")
)
else:
last_pcp_date = date(int(julian_tfirst[0:4]), 1, 1) + timedelta(
days=int(julian_tfirst[4:7]) - 1
)
first_pcp_date = date(int(julian_t[0:4]), 1, 1) + timedelta(
days=int(julian_t[4:7]) - 1
)
pcp_dates.append(julian_t)
return pcp_dates, first_pcp_date, last_pcp_date, pcp_array
def readTmpFiles(self, log_file):
"""Reading the Budyko .tmp files for all stations in station file"""
station_folder = os.path.split(self.station_filename)[0]
# Sort id's
station_id_sorted = sorted(self.station_id_dict)
# Reading old .tmp files to numpy array
tmp_max_array = None
tmp_min_array = None
tmp_dates = []
for i in station_id_sorted:
station_tmp_filename = os.path.join(
station_folder, self.station_id_dict[i] + "temp.txt"
)
if os.path.isfile(station_tmp_filename):
with open(station_tmp_filename, "r") as tmp_file:
lines = tmp_file.readlines()
lines_number = len(lines)
if tmp_max_array is None:
tmp_max_array = numpy.zeros(
[lines_number - 1, len(station_id_sorted)], dtype=numpy.float
)
tmp_min_array = numpy.zeros(
[lines_number - 1, len(station_id_sorted)], dtype=numpy.float
)
for n in range(lines_number):
if n == 0 and i == 1:
t = datetime.datetime.strptime(lines[0][0:8], "%Y%m%d")
if lines_number == 1:
tt_first = (t - timedelta(days=1)).timetuple()
julian_tfirst = "%d%03d" % (
tt_first.tm_year,
tt_first.tm_yday,
)
tt = t.timetuple()
julian_t = "%d%03d" % (tt.tm_year, tt.tm_yday)
else:
tt = t.timetuple()
julian_t = "%d%03d" % (tt.tm_year, tt.tm_yday)
tmp_dates.append(julian_t)
else:
# t=time.strptime('20110531','%Y%m%d')
if i == 1:
tt_next = (t + timedelta(days=n)).timetuple()
julian_t = "%d%03d" % (tt_next.tm_year, tt_next.tm_yday)
tmp_dates.append(julian_t)
if n > 0:
tmp_max_array[n - 1, i - 1] = float(lines[n][0:5])
tmp_min_array[n - 1, i - 1] = float(lines[n][6:13])
else:
raise GeoAlgorithmExecutionException(
'No tmp file found: "' + station_tmp_filename + '" '
)
if tmp_dates:
# From Julian day to date
last_tmp_date = date(int(tmp_dates[-1][0:4]), 1, 1) + timedelta(
days=int(tmp_dates[-1][4:7]) - 1
)
first_tmp_date = date(int(tmp_dates[0][0:4]), 1, 1) + timedelta(
days=int(tmp_dates[0][4:7]) - 1
)
log_file.write(
"First day of tmp in .tmp file: %s \n"
% first_tmp_date.strftime("%Y%m%d")
)
log_file.write(
"Last day of tmp in .tmp file: %s \n" % last_tmp_date.strftime("%Y%m%d")
)
else:
last_tmp_date = date(int(julian_tfirst[0:4]), 1, 1) + timedelta(
days=int(julian_tfirst[4:7]) - 1
)
first_tmp_date = date(int(julian_t[0:4]), 1, 1) + timedelta(
days=int(julian_t[4:7]) - 1
)
tmp_dates.append(julian_t)
return tmp_dates, first_tmp_date, last_tmp_date, tmp_max_array, tmp_min_array
def writePcpFiles(self, first_pcp_date, pcp_array, log_file):
"""Write the Budyko .pcp files for all stations in station file"""
station_folder = os.path.split(self.station_filename)[0]
# Sort id's
station_id_sorted = sorted(self.station_id_dict)
# Writing .pcp files
for i in station_id_sorted:
station_pcp_filename = os.path.join(
station_folder, self.station_id_dict[i] + ".txt"
)
if os.path.isfile(station_pcp_filename):
with open(station_pcp_filename, "w") as f:
# Write startdate
f.write("%s\n" % (first_pcp_date).strftime("%Y%m%d"))
# Write array to file
for l in range(len(pcp_array)):
f.write(("%.1f" % pcp_array[l, i - 1]))
f.write("\n")
else:
raise GeoAlgorithmExecutionException(
'No .pcp file found:: "' + station_pcp_filename + '" '
)
def writeTmpFiles(self, first_tmp_date, tmp_max_array, tmp_min_array, log_file):
"""Write the Budyko temp.txt files for all stations in station file"""
station_folder = os.path.split(self.station_filename)[0]
# Sort id's
station_id_sorted = sorted(self.station_id_dict)
# Writing .pcp files
for i in station_id_sorted:
station_tmp_filename = os.path.join(
station_folder, self.station_id_dict[i] + "temp.txt"
)
if os.path.isfile(station_tmp_filename):
with open(station_tmp_filename, "w") as f:
# Write header
f.write("%s\n" % (first_tmp_date).strftime("%Y%m%d"))
# Write array to file
for l in range(len(tmp_max_array)):
f.write("%05.1f" % tmp_max_array[l, i - 1])
f.write(",")
f.write(("%05.1f" % tmp_min_array[l, i - 1]))
f.write("\n")
else:
raise GeoAlgorithmExecutionException(
'No .tmp file found: "' + station_tmp_filename + '" '
)