forked from python-windrose/windrose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_animate.py
198 lines (149 loc) · 6.33 KB
/
example_animate.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This sample need to be improve to provide
a clean API to output animation
Monthly
python samples/example_animate.py --by M --exit_at 5 --rmax 1000
Daily
python samples/example_animate.py --by D --exit_at 5 --rmax 60
"""
import click
import time
import logging
import traceback
import matplotlib
# matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation
import matplotlib.cm as cm
import pandas as pd
import numpy as np
import datetime
from windrose import (WindroseAxes, FIGSIZE_DEFAULT, DPI_DEFAULT)
logging.Formatter.converter = time.gmtime
logging.basicConfig(format='%(asctime)s %(levelname)s %(message)s', level=logging.DEBUG)
logger = logging.getLogger(__name__)
pd.set_option('max_rows', 10)
S_FIGSIZE_DEFAULT = ",".join(map(str, FIGSIZE_DEFAULT))
def get_by_func(by=None, by_func=None):
if by is None and by_func is None:
by = 'MS'
if by in ['year', 'yearly', 'Y']:
return lambda dt: dt.year
elif by in ['month', 'monthly', 'MS']: # MS: month start
return lambda dt: (dt.year, dt.month)
elif by in ['day', 'daily', 'D']:
return lambda dt: (dt.year, dt.month, dt.day)
elif by is None and by_func is not None:
return by_func
else:
raise NotImplementedError("'%s' is not an allowed 'by' parameter" % by)
def generate(df_all, func, copy=True):
if copy:
df_all = df_all.copy()
df_all['by'] = df_all.index.map(func)
df = df_all.reset_index().set_index(['by', df_all.index.name])
for by_val in df.index.levels[0]:
yield df.loc[by_val]
def count(df_all, func):
return len(np.unique(df_all.index.map(func)))
@click.command()
@click.option("--filename", default="samples/sample_wind_poitiers.csv", help="Input filename")
@click.option("--exit_at", default=0, help="premature exit (int) - must be > 1")
@click.option("--by", default='month', help="Animate by (year, month, day...)")
@click.option("--rmax", default=1000, help="rmax")
@click.option("--filename_out", default="windrose_animation.mp4", help="Output filename")
@click.option("--dpi", default=DPI_DEFAULT, help="Dot per inch for plot generation")
@click.option("--figsize", default=S_FIGSIZE_DEFAULT, help="Figure size x,y - default=%s" % S_FIGSIZE_DEFAULT)
@click.option("--fps", default=7, help="Number of frame per seconds for video generation")
@click.option("--bins_min", default=0.01, help="Bins minimum value")
@click.option("--bins_max", default=20, help="Bins maximum value")
@click.option("--bins_step", default=2, help="Bins step value")
@click.option("--fontname", default="Courier New", help="Font name")
def main(filename, exit_at, by, rmax, dpi, figsize, fps, bins_min, bins_max, bins_step, fontname, filename_out):
# convert figsize (string like "8,9" to a list of float [8.0, 9.0]
figsize = figsize.split(",")
figsize = map(float, figsize)
by_func = get_by_func(by)
# Read CSV file to a Pandas DataFrame
df_all = pd.read_csv(filename)
df_all['Timestamp'] = pd.to_datetime(df_all['Timestamp'])
df_all = df_all.set_index('Timestamp')
df_all.index = df_all.index.tz_localize('UTC').tz_convert('UTC')
dt_start = df_all.index[0]
dt_end = df_all.index[-1]
td = dt_end - dt_start
Nslides = count(df_all, by_func)
msg = """Starting
First dt: %s
Last dt: %s
td: %s
Slides: %d""" % (dt_start, dt_end, td, Nslides)
logger.info(msg)
# Define bins
bins = np.arange(bins_min, bins_max, bins_step)
# Create figure
fig = plt.figure(figsize=figsize, dpi=dpi, facecolor='w', edgecolor='w')
# Create a video writer (ffmpeg can create MPEG files)
FFMpegWriter = matplotlib.animation.writers['ffmpeg']
metadata = dict(title='windrose', artist='windrose',
comment="""Made with windrose
http://www.github.com/scls19fr/windrose""")
writer = FFMpegWriter(fps=fps, metadata=metadata)
dt_start_process = datetime.datetime.now()
with writer.saving(fig, filename_out, 100):
try:
for i, df in enumerate(generate(df_all, by_func)):
dt1 = df.index[0]
dt2 = df.index[-1]
td = dt2 - dt1
msg = """ Slide %s/%s
From %s
to %s
td %s""" % (i + 1, Nslides, dt1, dt2, td)
logger.info(msg)
remaining = Nslides - (i + 1)
now = datetime.datetime.now()
td_remaining = (now - dt_start_process) / (i + 1) * remaining
logger.info(""" Expected
time: %s
end at: %s
""" % (td_remaining, now + td_remaining))
title = " From %s\n to %s" % (dt1, dt2)
try:
ax = WindroseAxes.from_ax(fig=fig, rmax=rmax) # scatter, bar, box, contour, contourf
direction = df['direction'].values
var = df['speed'].values
# ax.scatter(direction, var, alpha=0.2)
# ax.set_xlim([-bins[-1], bins[-1]])
# ax.set_ylim([-bins[-1], bins[-1]])
# ax.bar(direction, var, bins=bins, normed=True, opening=0.8, edgecolor='white')
# ax.box(direction, var, bins=bins)
# ax.contour(direction, var, cmap=cm.hot, lw=3, bins=bins)
ax.contourf(direction, var, bins=bins, cmap=cm.hot)
ax.contour(direction, var, bins=bins, colors='black', lw=3)
ax.set_legend()
# ax = WindAxes.from_ax(fig=fig) # pdf: probability density function
# ax.pdf(var, bins=bins)
# ax.set_xlim([0, bins[-1]])
# ax.set_ylim([0, 0.4])
ax.set_title(title, fontname=fontname)
writer.grab_frame()
except KeyboardInterrupt:
break
except Exception:
logger.error(traceback.format_exc())
fig.clf()
if i > exit_at - 1 and exit_at != 0: # exit_at must be > 1
break
except KeyboardInterrupt:
return
except Exception:
logger.error(traceback.format_exc())
N = i + 1
logger.info("Number of slides: %d" % N)
# plt.show()
logger.info("Save file to '%s'" % filename_out)
if __name__ == "__main__":
main()