-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_data.py
290 lines (237 loc) · 11.4 KB
/
plot_data.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
289
290
from obspy import read
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import os
import json
import calendar
def make_plot_dictionary():
"""
Makes dictionary with available plots in directories.
plot_dictionary[year][month][day][doy] is the corresponding day of the year for day and month of year,
plot_dictionary[year][month][day][stations] are the stations that have plots for day and month of year.
:return: plot dictionary
"""
plot_dict = {}
for year in [1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977]:
plot_dict[year] = {}
for month in range(1, 13):
plot_dict[year][month] = {}
# check if each station has a plot for a day
for station in ["s11", "s12", "s14", "s15", "s16"]:
directory = os.path.join(os.getcwd(), "plots", station, str(year))
available_days = []
for (dirpath, dirnames, filenames) in os.walk(directory):
available_days.extend(dirnames)
break
available_days = set([int(day) for day in available_days])
if calendar.isleap(year):
day_range = range(1, 367)
else:
day_range = range(1, 366)
# days of year
for day in day_range:
# days of year converted to dates
date = pd.to_datetime(year*1000 + day, format="%Y%j")
day_month = int(date.day)
month = int(date.month)
# first time visiting day of month, init
if day_month not in plot_dict[year][month]:
plot_dict[year][month][day_month] = {}
plot_dict[year][month][day_month]["doy"] = "%03d" % day
plot_dict[year][month][day_month]["stations"] = []
# station has a plot for day
if day in available_days:
plot_dict[year][month][day_month]["stations"].append(station)
# output
file_path_out = os.path.join(os.getcwd(), "plots", "plot_dict.json")
with open(file_path_out, "w") as file_out:
file_out.write(json.dumps(plot_dict, indent=4))
def get_saved_stations(return_num=False):
"""
Gets available stations from saved directories.
:param return_num: if True returns station name as number instead of string (e.g. 11 instead of "s11")
:return: station name list (empty if data doesn't exist)
"""
directory = os.path.join(os.getcwd(), "data")
if not os.path.isdir(directory):
return []
stations = []
for (dirpath, dirnames, filenames) in os.walk(directory):
stations.extend(dirnames)
break
try:
stations.remove("nakamura")
except ValueError:
pass
if return_num:
stations = [int(station[1:]) for station in stations]
return stations
def get_saved_years(station, return_num=False):
"""
Gets available data years for a station from saved directories.
:param station: station name
:param return_num: if True returns year as number instead of string
:return: years list (empty if station name is invalid or data doesn't exist)
"""
# add "s" in front of station number
station = str(station)
if not station.startswith("s"):
station = "s" + station
directory = os.path.join(os.getcwd(), "data", station)
if not os.path.isdir(directory):
return []
years = []
for (dirpath, dirnames, filenames) in os.walk(directory):
years.extend(dirnames)
break
if return_num:
years = [int(year) for year in years]
return years
def get_saved_days(station, year, return_num=False):
"""
Gets available data days for a year of a station from saved directories.
:param station: station name
:param year: year
:param return_num: if True returns day as number instead of 3-character string (e.g. 1 instead of "001")
:return: days of the year list (empty if station name or year are invalid or data doesn't exist)
"""
# add "s" in front of station number
station = str(station)
if not station.startswith("s"):
station = "s" + station
directory = os.path.join(os.getcwd(), "data", station, str(year))
if not os.path.isdir(directory):
return []
days = []
for (dirpath, dirnames, filenames) in os.walk(directory):
days.extend(dirnames)
break
if return_num:
days = [int(day) for day in days]
return days
def sensor_plot(ax=None, fontsize=15, title='', tr=None):
"""
Adds plot with PSE data to axis.
:param ax: axis
:param fontsize: plot title fontsize
:param title: plot title
:param tr: trace
"""
ax.locator_params(nbins=3)
ax.set_xlabel('Time', fontsize=fontsize)
ax.set_ylabel('DU', fontsize=fontsize)
ax.set_yticks(np.linspace(min(tr.data), max(tr.data), 5))
ax.set_title(title, fontsize=20, fontweight="bold", pad=10)
ax.plot(tr.times("matplotlib"), tr.data, "b-")
ax.xaxis_date()
def plot_data(stations=None, years=None, days=None, display=False):
"""
Plots PSE data and calculates max deviation from average values for every plot.
:param stations: station name or station name list (only valid station names used), if None plot for all saved
stations
:param years: year or year list, if None plot for all available years
:param days: day or day list, if None plot for all available days
:param display: display plots if True
"""
saved_stations = get_saved_stations(return_num=True)
if stations is None:
valid_stations = ["s" + str(station) for station in saved_stations] # all available stations
else:
if not isinstance(stations, list):
stations = [stations]
# only valid stations
valid_stations = [str(station) for station in stations if int(str(station).lstrip("s")) in saved_stations]
for index_station, station in enumerate(valid_stations):
saved_years = get_saved_years(station, return_num=False)
if years is None:
valid_years = saved_years # all available years
else:
if not isinstance(years, list):
years = [years]
# only valid years
valid_years = [str(year) for year in years if str(year) in saved_years]
for index_year, year in enumerate(valid_years):
saved_days = get_saved_days(station, year, return_num=True)
if days is None:
valid_days = ["%03d" % day for day in saved_days] # all available days
else:
if not isinstance(days, list):
days = [days]
# only valid days
valid_days = ["%03d" % int(day) for day in days if int(day) in saved_days]
for index_day, day in enumerate(valid_days):
# add "s" in front of station number
if not station.startswith("s"):
station_name = "s" + station
else:
station_name = station
# iterate over files in directory to read data
daily_trace = {}
daily_max = {}
directory_in = os.path.join(os.getcwd(), "data", station_name, year, day)
for filename in os.listdir(directory_in):
file = os.path.join(directory_in, filename)
st = read(file)
tr = st[0]
tr.data = tr.data.astype(np.float64)
avg = np.average(tr.data[tr.data != -1])
max_dev = np.max(np.abs(tr.data - avg))
tr.data[tr.data == -1] = np.nan
sensor = filename[10:13]
daily_trace[sensor] = tr # save trace of this day
daily_max[sensor] = max_dev
# plot data from files
with plt.style.context("seaborn-darkgrid"):
fig, ((ax1), (ax2), (ax3)) = plt.subplots(nrows=3, ncols=1)
fig.set_figwidth(15)
fig.set_figheight(15)
if "mh1" in daily_max and "mh1" in daily_trace:
sensor_plot(ax=ax1, title='MH1 (Mid-Period Sensor) ' +
'[Max Deviation Value: ' + str(daily_max['mh1']) + ']',
tr=daily_trace['mh1'])
if "mh2" in daily_max and "mh2" in daily_trace:
sensor_plot(ax=ax2, title='MH2 (Mid-Period Sensor) ' +
'[Max Deviation Value: ' + str(daily_max['mh2']) + ']',
tr=daily_trace['mh2'])
if "mhz" in daily_max and "mhz" in daily_trace:
sensor_plot(ax=ax3, title='MHZ (Mid-Period Sensor) ' +
'[Max Deviation Value: ' + str(daily_max['mhz']) + ']',
tr=daily_trace['mhz'])
try:
fig.tight_layout(pad=1.5)
fig.subplots_adjust(hspace=0.3)
if display:
fig.show()
# output plot
file_out = "{station}_{year}_{day}_mh_plot.png".format(station=station_name, year=year, day=day)
directory_out = os.path.join(os.getcwd(), "plots", station_name, year, day)
if not os.path.isdir(directory_out):
os.makedirs(directory_out)
file_path_out = os.path.join(directory_out, file_out)
fig.savefig(file_path_out)
plt.close(fig)
print("Saved",
station,
"({station}/{stations})".format(station=index_station + 1, stations=len(valid_stations)),
year, "({year}/{years})".format(year=index_year + 1, years=len(valid_years)),
day, "({day}/{days})".format(day=index_day + 1, days=len(valid_days)))
# output max deviation
file_out = "{station}_{year}_{day}_mh_dev.json".format(station=station_name, year=year, day=day)
file_path_out = os.path.join(directory_out, file_out)
with open(file_path_out, "w") as file_out:
file_out.write(json.dumps(daily_max, indent=4))
except ValueError:
print("ERROR",
station,
"({station}/{stations})".format(station=index_station + 1, stations=len(valid_stations)),
year, "({year}/{years})".format(year=index_year + 1, years=len(valid_years)),
day, "({day}/{days})".format(day=index_day + 1, days=len(valid_days)))
continue
if __name__ == "__main__":
plot_data("s11")
plot_data("s12")
plot_data("s14")
plot_data("s15")
plot_data("s16")
make_plot_dictionary()