# -*- coding: utf-8 -*- 
#Nicolas Enfon - 31/07/14 - LSIS DYNI
#This scripts plots the scalograms
from numpy import *
from matplotlib.pyplot import *
from scipy.io.wavfile import read, write
from pylab import specgram, norm
import cPickle, gzip, os, glob, pylab, time
import pywt, time

curdir = '/NAS3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/CODE'
wavdir = '/NAS3/SABIOD/SITE/AMAZONE_BIRD_LIFECLEF/2014/LIFECLEF2014_BIRDAMAZON_XC_WAV_RN/'
validdir = '/NAS3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/DATA/VALID/'
traindir = '/NAS3/SABIOD/METHODES/NICOLAS/CNN2D_LIFEBIRD/DATA/TRAIN/'
outdir = '/NAS3/SABIOD/public_data/BIRD_AMAZONE_DAUBECHIES/SCALOGRAMS/'

def plot_folder(folder, folderout=outdir):
    print 'Applying to folder ', folder, ' ...'
    tic = time.clock()
    os.chdir(folder)
    scalos = array(glob.glob('*.scalo'))
    for i in xrange(len(scalos)):
	print 'plotting ',scalos[i], ' ...'
	scalo = loadtxt(scalos[i])
	figure(figsize=(30,20))
	imshow(scalo[::-1], aspect='auto', interpolation='nearest')
	title("Scalogram - Pywt, level=6, wavelet='db2'")
	xlabel('Time')
	ylabel('Scalogram coefficients (~frequency)')
	savefig(outdir+scalos[i]+'.png')
    print 'Done in ', time.clock() - tic, ' seconds'

if __name__ == '__main__':
    print 'Running plot_the_scalos.py ...'
    plot_folder(validdir)
    #plot_folder(traindir)
