-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsun_blinker.py
145 lines (106 loc) · 4.23 KB
/
sun_blinker.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
import sunpy
import sunpy.map
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from astropy.visualization import (ImageNormalize, AsinhStretch,
)
from IPython.display import HTML, display
# from mpl_animators import ArrayAnimatorWCS
class SunBlinker():
def __init__(self, map1, map2, reproject=False, fps=5, figsize=(5,5),
norm1=None, norm2=None, save_fname=None) -> None:
self.map1 = map1
if reproject:
self.map2 = map2.reproject_to(map1.wcs)
else:
self.map2 = map2
self.fps = fps
self.figsize = figsize
if norm1 is None:
self.norm1 = self.map1.plot_settings['norm']
else:
self.norm1 = norm1
if norm2 is None:
self.norm2 = self.map2.plot_settings['norm']
else:
self.norm2 = norm2
self._init_plot()
self.anim = FuncAnimation(self.fig, self._update_plot, interval=1000/self.fps, blit=True,frames=2,
repeat=True)
self.anim_html = HTML(self.anim.to_jshtml())
if save_fname is not None:
self.anim.save(save_fname, writer='imagemagick', fps=self.fps)
if matplotlib.get_backend() == 'qtagg':
plt.show()
else:
self.fig.clf()
plt.close()
display(self.anim_html)
def _init_plot(self):
self.fig = plt.figure(figsize=self.figsize)
self.ax = self.fig.add_subplot(111, projection=self.map1)
if 'aspect' in self.map1.plot_settings.keys():
self.ax.set_aspect(self.map1.plot_settings['aspect'])
self.im = self.map1.plot(axes=self.ax)
self.ax.set_title(None)
def _update_plot(self,i):
# self.ax.clear()
if i == 0:
self.im.set_array(self.map1.data)
self.im.set_norm(self.norm1)
self.im.set_cmap(self.map1.plot_settings['cmap'])
else:
self.im.set_array(self.map2.data)
self.im.set_norm(self.norm2)
self.im.set_cmap(self.map2.plot_settings['cmap'])
return [self.im]
class ImageBlinker():
def __init__(self, image1, image2, fps=5, figsize=(5,5),
norm1=None, norm2=None, aspect=1,
save_fname=None, **kwargs) -> None:
self.image1 = image1
self.image2 = image2
self.fps = fps
self.figsize = figsize
self.aspect = aspect
self.kwargs = kwargs
if norm1 is None:
self.norm1 = ImageNormalize(vmin=np.nanpercentile(image1, 0.1),
vmax=np.nanpercentile(image1, 99.9),
stretch=AsinhStretch())
else:
self.norm1 = norm1
if norm2 is None:
self.norm2 = ImageNormalize(vmin=np.nanpercentile(image2, 0.1),
vmax=np.nanpercentile(image2, 99.9),
stretch=AsinhStretch())
else:
self.norm2 = norm2
self._init_plot()
self.anim = FuncAnimation(self.fig, self._update_plot, interval=1000/self.fps, blit=True,frames=2,
repeat=True)
self.anim_html = HTML(self.anim.to_jshtml())
if save_fname is not None:
self.anim.save(save_fname, writer='imagemagick', fps=self.fps)
if matplotlib.get_backend() == 'qtagg':
plt.show()
else:
self.fig.clf()
plt.close()
display(self.anim_html)
def _init_plot(self):
self.fig = plt.figure(figsize=self.figsize)
self.ax = self.fig.add_subplot(111)
self.im = self.ax.imshow(self.image1, norm=self.norm1, origin='lower',
aspect=self.aspect, **self.kwargs)
def _update_plot(self,i):
# self.ax.clear()
if i == 0:
self.im.set_array(self.image1)
self.im.set_norm(self.norm1)
else:
self.im.set_array(self.image2)
self.im.set_norm(self.norm2)
return [self.im]