-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_plot_precipitation_all.py
executable file
·112 lines (90 loc) · 3.51 KB
/
run_plot_precipitation_all.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
#!/usr/bin/env python3
from itertools import product
import os
import argparse
import matplotlib.pyplot as plt
from precip.cli import plot_precipitation
import pandas as pd
# This is needed to run on a server without a display
import matplotlib
matplotlib.use('Agg')
PRECIP_HOME = os.environ.get('PRECIP_HOME')
SCRATCH_DIR = os.environ.get('SCRATCHDIR')
VOLCANO_FILE = PRECIP_HOME + '/src/precip/Holocene_Volcanoes_precip_cfg..xlsx'
DEFAULT_STYLES = ['map', 'bar', 'annual', 'strength']
# DEFAULT_STYLES = ['bar', 'annual', 'strength'] # FA 7/2025 map gives problems woth GMT
BINS = [1, 2, 3, 4]
EXAMPLES = """
Examples:
Plot all styles for all volcanoes:
run_plot_precipitation_all.py --period 20060101:20070101
Plot all volcanoes with a different plot directory:
run_plot_precipitation_all.py --plot-dir .
run_plot_precipitation_all.py --plot-dir /path/to/plot_dir
Plot with a different volcano file:
run_plot_precipitation_all.py --volcano-file /path/to/volcano_file.xlsx
Plot with different styles:
run_plot_precipitation_all.py --styles map bar
plot_precipitation --help for more options
"""
def create_parser():
synopsis = 'Wrapper tool to run plot_precipitation with multiple styles and all volcanoes'
parser = argparse.ArgumentParser(
description=synopsis,
formatter_class=argparse.RawTextHelpFormatter,
epilog=EXAMPLES
)
parser.add_argument('--volcano-file',
default=VOLCANO_FILE,
metavar='',
help='File with volcano names (default: %(default)s)')
parser.add_argument('--plot-dir',
default=PRECIP_HOME,
help='Directory to save plots (default: %(default)s)')
parser.add_argument('--styles',
nargs='+',
default=DEFAULT_STYLES,
help='List of plot styles to use (default: %(default)s)')
return parser
def get_volcanoes():
df = pd.read_excel(VOLCANO_FILE, skiprows=1)
df = df[df['Precip'] != False]
volcano_dict = {
r['Volcano Name'] : {
'id': r['Volcano Number']
} for _, r in df.iterrows()}
return volcano_dict
def main():
parser = create_parser()
args, unknown_args = parser.parse_known_args()
plot_dir = os.path.join(args.plot_dir, 'precip_plots')
os.makedirs(plot_dir, exist_ok=True)
volcanoes = get_volcanoes()
failures = {}
i = 0
plot_params = [(style, i) for style, i in product(args.styles, BINS) if not (style == 'map' and i > 1)]
for volcano, info in volcanoes.items():
id = info['id']
volcano_dir = os.path.join(plot_dir, str(id))
os.makedirs(volcano_dir, exist_ok=True)
for style, bins in plot_params:
i+=1
inps = argparse.Namespace(style=style,
volcano_name=[volcano],
no_show=True,
bins=bins)
png_path = os.path.join(volcano_dir, f'{id}_{style}_bin_{bins}.png')
try:
plot_precipitation.main(unknown_args, inps)
plt.savefig(png_path)
plt.close()
except KeyboardInterrupt:
break
except Exception as e:
failures[png_path] = e
print('#'*50)
print(f'Failed to plot for the following volcanoes: {len(failures)}/{i}')
print(failures.keys())
print(failures)
if __name__ == '__main__':
main()