Skip to content

Commit

Permalink
3 color movie generation plots working
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Mar 29, 2018
1 parent 6a21485 commit 7f4571f
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions dascutils/plots.py
@@ -1,12 +1,10 @@
from pathlib import Path
from os import devnull
from numpy import arange
from pytz import UTC
from datetime import datetime
import xarray
import numpy as np
from datetime import datetime, timedelta
from scipy.interpolate import interp1d
from matplotlib.pyplot import draw,pause,figure
from matplotlib.colors import LogNorm
import matplotlib.animation as anim
#
try:
import themisasi.plots as themisplot
Expand Down Expand Up @@ -40,44 +38,57 @@ def histogram_dasc(imgs, odir=None):
fg.savefig(ofn, bbox_inches='tight')


def moviedasc(img,odir,cadence,rows=None,cols=None):
def moviedasc(imgs:xarray.Dataset, odir:Path, cadence:float, rows=None, cols=None):

if odir:
print('writing to',odir)
odir = Path(odir).expanduser()

fg = figure(figsize=(15,5))
axs = fg.subplots(1,3)

wavelength = [d for d in imgs.data_vars if not isinstance(d,str)]
axs = np.atleast_1d(fg.subplots(1,len(wavelength)))

hi = []; ht=[]
for a,w,x,mm,c in zip(axs,wavelength,(0.225,0.5,0.775),
((350,800),(350,9000),(350,900)),('b','g','r')):
#a.axis('off') #this also removes xlabel,ylabel
a.set_xticks([]); a.set_yticks([])
a.set_xlabel('{} nm'.format(w),color=c)
hi.append(a.imshow(img[0][0],vmin=mm[0],vmax=mm[1],
time = imgs.time.values
# %% setup figures
for ax,w,mm,c in zip(axs,
wavelength,
((350,800),(350,9000),(350,900)),
('b','g','r')):
#ax.axis('off') #this also removes xlabel,ylabel
ax.set_xticks([])
ax.set_yticks([])
ax.set_xlabel(f'{w} nm', color=c)

hi.append(ax.imshow(imgs[w][0],
vmin=mm[0],vmax=mm[1],
origin='lower',
norm=LogNorm(),cmap='gray'))
ht.append(a.set_title('',color=c))

ht.append(ax.set_title('', color=c))
#fg.colorbar(hi[-1],ax=a).set_label('14-bit data numbers')
if themisplot is not None:
themisplot.overlayrowcol(a, rows, cols)

T = max([t[0,0] for t in times])
Tmax = min([t[-1,0] for t in times])
themisplot.overlayrowcol(ax, rows, cols)

fg.tight_layout(h_pad=1.08) #get rid of big white space in between figures
#%% loop
print('generating video until',datetime.fromtimestamp(Tmax))
with writer.saving(fg, str(ofn), DPI):
while T<=Tmax:
for I,Hi,Ht,t in zip(img,hi,ht,times):
ft = interp1d(t[:,0],arange(len(t)),kind='nearest')
ind = ft(T).astype(int)
#print(ind,end=' ')
Hi.set_data(I[ind])
try:
Ht.set_text(datetime.fromtimestamp(t[ind,0],tz=UTC))
except OSError: #file had corrupted time
Ht.set_text('')
print('generating video until', time[-1])
t = time[0]
dt = timedelta(seconds=cadence)
while t <= time[-1]:
for w,Hi,Ht in zip(wavelength,hi,ht):
I = imgs[w].sel(time=t, method='nearest')
Hi.set_data(I)
try:
Ht.set_text(str(I.time.values))
except OSError: #file had corrupted time
Ht.set_text('')

draw(), pause(0.05) # the pause avoids random crashes
t += dt

draw(), pause(0.05) # the pause avoids random crashes
T += cadence
if write:
print('generating AVI @ ',datetime.fromtimestamp(T),'from',datetime.fromtimestamp(t[ind][0]), end='\r')
writer.grab_frame(facecolor='k')
if odir:
ofn = odir / (str(t)+'.png')
print('saving', ofn,end='\r')
fg.savefig(ofn, bbox_inches='tight',facecolor='k')

0 comments on commit 7f4571f

Please sign in to comment.