-
Notifications
You must be signed in to change notification settings - Fork 0
/
output.py
277 lines (153 loc) · 8.09 KB
/
output.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
'''
Author: Jon Paul Miles
Date Created: March 11, 2022
'''
from globals import *
import download
import math
import pandas as pd
import anomaly
import numpy as np
from texttable import Texttable
import time
# Collect statistics on the type of data we are getting
absolute_up_count = 0
absolute_down_count = 0
absolute_trends_array = [[],[],[]]
def get_file_name_from_path(FILE_PATH):
split_file_path = FILE_PATH.split('/')
return split_file_path[len(split_file_path) - 1]
def compose_station_console_output(station_iteration, total_stations, station_id, absolute_visual, absolute_trend, start_year, end_year, station_location, station_quadrant):
which_station = f"Station {'{:,}'.format(station_iteration)} of {'{:,}'.format(total_stations)}"
which_station = which_station.ljust(25, " ")
trend = " ".ljust(17, ' ')
if not math.isnan(absolute_trend):
trend = f"Trend: {absolute_visual} ({'{0:.3f}'.format(absolute_trend)})".ljust(17, ' ')
year_range = f"{start_year}-{end_year}"
padded_station_grid_box = station_quadrant.ljust(19, ' ')
station_location = station_location.replace(" ", " ").replace("\t", " ").strip()
print(f"{which_station} {station_id} {trend} | {year_range} | {padded_station_grid_box} | {station_location}")
def update_statistics(trend):
global absolute_up_count
global absolute_down_count
if math.isnan(trend):
visual = "?"
return visual
if trend > 0:
visual = "↑"
absolute_up_count += 1
absolute_trends_array[0].append(trend)
else:
visual = "↓"
absolute_down_count += 1
absolute_trends_array[1].append(trend)
absolute_trends_array[2].append(trend)
return visual
def compose_file_name(TEMPERATURES_FILE_PATH):
TEMPERATURE_FILE = get_file_name_from_path(TEMPERATURES_FILE_PATH)
reference_timespan = f"{ REFERENCE_START_YEAR }-{ REFERENCE_START_YEAR + REFERENCE_RANGE }"
acceptable_percent = normal_round(ACCEPTABLE_AVAILABLE_DATA_PERCENT * 100)
is_purged = 'some-rejected' if PURGE_FLAGS else 'all'
environment = f"-{SURROUNDING_CLASS}" if SURROUNDING_CLASS else ""
in_country = f"-in-({', '.join(IN_COUNTRY)})" if IN_COUNTRY else ""
minimum_months = f"{MONTHS_REQUIRED_EACH_YEAR}-months"
OUTPUT_FILE_NAME = f"{TEMPERATURE_FILE}-{reference_timespan}-{acceptable_percent}-{minimum_months}-{is_purged}{environment}{in_country}.xlsx"
return OUTPUT_FILE_NAME
def output_file(excel_data, TEMPERATURES_FILE_PATH):
excel_data_pd = pd.DataFrame(excel_data)
OUTPUT_FILE_NAME = compose_file_name(TEMPERATURES_FILE_PATH)
EXCEL_WRITER = pd.ExcelWriter(OUTPUT_FILE_NAME)
excel_data_pd.to_excel(EXCEL_WRITER, encoding='utf-8-sig', sheet_name='Anomalies', index=False)
EXCEL_WRITER.save()
def generate_column(labels, data):
return pd.concat([pd.Series(labels), data]).reset_index(drop = True)
def generate_column_for_output(labels, data):
return generate_column(labels, data) if PRINT_STATION_ANOMALIES else generate_column(labels[1], data)
# Prepare our spreadsheet for output as an Excel File
def create_excel_file(
ungridded_anomalies = [],
ungridded_anomalies_divided = [],
average_of_grids = [],
average_of_grids_divided = [],
average_of_grids_by_land_ratio = [],
average_of_grids_by_land_ratio_divided = [],
anomalies_by_grid = [],
anomalies_by_station = [],
data_source = "unknown",
):
year_sublabel = "Grid Weight" if not PRINT_STATION_ANOMALIES else "Grid Box"
stations_average_sublabel = "Equal Weight" if not PRINT_STATION_ANOMALIES else ""
# Start the base of our xlsx data
excel_data = {
"Year": generate_column_for_output([ "Location", year_sublabel ], pd.Series(range(YEAR_RANGE_START, YEAR_RANGE_END)))
}
# Prepare each column of the dataframe to be saved
excel_data["Average of stations"] = generate_column_for_output(
[ "All Stations", stations_average_sublabel ], ungridded_anomalies
)
excel_data["Average of stations / 100"] = generate_column_for_output(
[ "All Stations", stations_average_sublabel ], ungridded_anomalies_divided
)
excel_data["Average of Grids"] = generate_column_for_output(
[ "All Grids", "" ], average_of_grids
)
excel_data["Average of Grids / 100"] = generate_column_for_output(
[ "All Grids", "" ], average_of_grids_divided
)
excel_data["Average of grids weighed with land ratio"] = generate_column_for_output(
[ "All Grids", "" ], average_of_grids_by_land_ratio
)
excel_data["Average of grids weighed with land ratio / 100"] = generate_column_for_output(
[ "All Grids", "" ], average_of_grids_by_land_ratio_divided
)
if PRINT_STATION_ANOMALIES:
# Print a column for each station anomaly. In GHCNm v4, using all stations, this will cause the program to crash because Excel cannot have a file with 27k columns. But it is useful for testing smaller samples.
# anomalies_by_station.drop(columns=[2], axis=1, inplace=True)
for grid_cell_label, grid in anomalies_by_station.iterrows():
excel_data[grid.iloc[0]] = generate_column_for_output([ grid.iloc[1], grid.iloc[2] ], pd.Series(grid.iloc[3:].to_numpy()))
else:
# Create a column for each box of our latitude/longitude grid with it's anomalies
for grid_cell_label, grid in anomalies_by_grid.iterrows():
excel_data[grid_cell_label] = pd.Series(grid).reset_index(drop = True)
output_file(excel_data, data_source)
def print_settings_to_console(TEMPERATURES_FILE_PATH, STATION_FILE_PATH):
my_table = Texttable()
my_table.add_rows([
["Setting", "Value"],
["Temperatures file", get_file_name_from_path(TEMPERATURES_FILE_PATH)],
["Stations file", get_file_name_from_path(STATION_FILE_PATH)],
["Anomaly reference average range", f"{REFERENCE_START_YEAR}-{REFERENCE_START_YEAR + REFERENCE_RANGE - 1}"],
["Minimum required percent of years in baseline", f"{normal_round(ACCEPTABLE_AVAILABLE_DATA_PERCENT * 100, 0)}%"],
["Absolute trends range", f"{ABSOLUTE_START_YEAR}-{ABSOLUTE_END_YEAR}"],
["Trend range", f"{ABSOLUTE_START_YEAR}-{ABSOLUTE_END_YEAR-1}"],
["Purging flagged data", str(PURGE_FLAGS)],
["Required months", str(MONTHS_REQUIRED_EACH_YEAR)],
["Environment class", str(SURROUNDING_CLASS)],
["Countries used", str(", ".join(IN_COUNTRY) if IN_COUNTRY else "All")]
])
my_table.set_deco(Texttable.HEADER | Texttable.BORDER)
print(my_table.draw())
print(f"Please wait a few seconds...")
print(f"\n")
def console_performance(t0, TOTAL_STATIONS):
end_time = time.perf_counter() - t0
minutes, remainder_seconds = divmod(end_time, 60)
hours, remainder_minutes = divmod(minutes, 60)
seconds = normal_round(end_time)
print(f"Process completed in {int(normal_round(hours))}h:{int(normal_round(remainder_minutes))}m:{int(normal_round(remainder_seconds))}s")
total_minutes = seconds / 60
stations_per_minute = normal_round(TOTAL_STATIONS / total_minutes)
print(f"With {'{:,}'.format(TOTAL_STATIONS)} stations, that's {'{:,}'.format(stations_per_minute)} stations/minute.\n")
# Print Summaries
def print_summary_to_console(total_stations, TEMPERATURES_FILE_PATH):
absolute_upwards_trend = normal_round(np.average(absolute_trends_array[0]), 3) if len(absolute_trends_array[0]) else "Unknown"
absolute_downwards_trend = normal_round(np.average(absolute_trends_array[1]), 3) if len(absolute_trends_array[1]) else "Unknown"
absolute_all_trends = normal_round(np.average(absolute_trends_array[2]), 3) if len(absolute_trends_array[2]) else "Unknown"
# print(f"\n{'Flagged data purged' if PURGE_FLAGS else 'Utilizes all data (including flagged)'}\n")
print("-------------------------------------------------------------------------------------------------------------------------------------")
print(f"Absolute Trends:\t\t{absolute_up_count} ↑ ({absolute_upwards_trend} Avg) \t\t{absolute_down_count} ↓ ({absolute_downwards_trend} Avg)")
if not absolute_all_trends == "Unknown":
print(f"Total Avg: {absolute_all_trends}°C {'rise' if float(absolute_all_trends) > 0 else 'fall'} every century between ({ABSOLUTE_START_YEAR}-{ABSOLUTE_END_YEAR})")
print("")
print(f"File output to {compose_file_name(TEMPERATURES_FILE_PATH)}")
print("\n")