#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
Created on Mon Jun 19 09:04:16 2017

@author: xliq
"""
'''
The MetroPy Phases File:
    Generates the Phase plots for the Metrology data.
'''
#### Initialization
from sys import argv
FITS_file = argv[1]
rmsLength = int(argv[2])
resolution = int(argv[3])
img = argv[4]

#### Importing
# import necessary things from the init file
from MetroPy_Init import plotstyle, d_color, fc_color
from MetroPy_Init import time, phase
from MetroPy_Helper import plotnumber, plotlabel

# import necessary packages
import os
import matplotlib.pyplot as plt
# plotstyle for Metrology data
plt.style.use(plotstyle)
# numpy ofcourse
import numpy as np
#import progressbar as pb

# phase data plots (8 plots with 5 signals each)
# n = 0, 1, 2, 3 == FT plots
# n = 4, 5, 6, 7 == SC plots
size = [24, 20]
counter = 0
# activate progressbar
#bar = pb.ProgressBar()
for n in np.arange(0, 12, 1):
    
    fig = plt.gcf()
    fig.set_size_inches(size)
    plt.subplot(4, 3, plotnumber('phase', n, counter))

    if n < 8:

        ### FC plot
        plt.plot(time, phase[0][:,32 + n],
                 label='FC',
                 color=fc_color)
         
        ### Diode plots
        # D1
        plt.plot(time, phase[0][:,4*n], 
                 label='D1',
                 color=d_color[0])
        
        # D2
        plt.plot(time, phase[0][:,4*n + 1], 
                 label='D2',
                 color=d_color[1])
        
        # D3
        plt.plot(time, phase[0][:,4*n + 2], 
                 label='D3',
                 color=d_color[2])
        
        # D4
        plt.plot(time, phase[0][:,4*n + 3], 
                 label='D4',
                 color=d_color[3])
        
        plt.title('AT%s' %plotlabel('phase', n))
        plt.xlabel('Time [s]')
        plt.ylabel('Phase [rad]')
        
        # plot one legend for color coding
        if n == 0:
            plt.legend(loc='best', prop={'size':10  })
        
    ### Substraction plots
    # plot FT - SC
    if n >= 8:
        
        # Diodes
        fig = plt.subplot(4, 3, plotnumber('phase', n, counter))
        
        plt.plot(time, phase[0][:,4*(n%8)] - phase[0][:,16 + 4*(n%8)],
                 label='AT%s D1' %(4-(n%8)),
                 color=d_color[0])
        plt.plot(time, phase[0][:,4*(n%8) + 1] - phase[0][:,16 + 4*(n%8) + 1], 
                 label='AT%s D1' %(4-(n%8)),
                 color=d_color[1])
        plt.plot(time, phase[0][:,4*(n%8) + 2] - phase[0][:,16 + 4*(n%8) + 2], 
                 label='AT%s D1' %(4-(n%8)),
                 color=d_color[2])
        plt.plot(time, phase[0][:,4*(n%8) + 3] - phase[0][:,16 + 4*(n%8) + 3], 
                 label='AT%s D1' %(4-(n%8)),
                 color=d_color[3])
        
        # FC
        plt.plot(time, phase[0][:,32 + (n%8)] - phase[0][:,32 + (n%8) + 4],
                 label='AT%s D4' %(4-(n%8)),
                 color=fc_color)
        
        plt.title('AT%s %s' %((4-(n%8)), plotlabel('phase', 8+(n%8))))
        plt.xlabel('Time [s]')
        plt.ylabel('Phase [rad]')

plt.savefig('%s.Phase.%s' %(FITS_file, img), bbox_inches='tight')

print('Phase Plots: Done!')
