# This scripts is doing...
# Patrick Kuehl, date
import time
from pylab import *
from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
import numpy.ma as ma
from matplotlib.colors import LogNorm
from scipy.optimize import leastsq
from matplotlib import dates
import time
import datetime as date
from datetime import datetime
from datetime import timedelta
import os
import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning) 
from matplotlib.backends.backend_pdf import PdfPages


# Note that images2gif.py (in this folder) is required
from images2gif import writeGif
from PIL import Image
import os
""
# create some png files in chronological order
for q in range(30):
  plt.figure()
  plt.title("random random")
  x,y= np.random.random(100),np.random.random(100)
  plt.plot(x,y,'ko')
  plt.xlim(0,1)
  plt.ylim(0,1)
  plt.savefig("%04d.png"%q)
  plt.close()

import subprocess
def grid2gif(image_str, output_gif):
    str1 = 'convert -delay 10 -loop 1 ' + image_str  + ' ' + output_gif
    subprocess.call(str1, shell=True)

grid2gif("*.png", "movie.gif")

"""
# merge all pngs to a gif movie (different time resolutions)
for thisduration in [0.1,0.5]:
  file_names = sorted((fn for fn in os.listdir('.') if fn.endswith('.png')))
  images = [Image.open('%s' %fn) for fn in file_names]
  print file_names
  filename = "test_animation_%s.GIF" %thisduration
  writeGif(filename, images, duration=thisduration)



# open gifs in firefox and remove all pngs gifs, so that the folder remains clean
os.system("rm *png")
os.system("firefox *.GIF")
time.sleep(1)
os.system("rm *.GIF")
"""
