import nds
import numpy as np 
import matplotlib.pyplot as plt
import gpstime

# cheap rounding function to round times to nearest minute
def myround(x, base=60):
    return int(base * round(float(x)/base)) 

# start times
stop_time = int(gpstime.tconvert())-3600
start_time = stop_time - 7*24*3600

start=myround(start_time) 
stop=myround(stop_time)  

#set up nds connection
conn = nds.connection('h1nds1',8088)

# setting gap handling in nds
conn.set_parameter('GAP_HANDLER', 'STATIC_HANDLER_NAN')

#pitch
channels_pit =  ['H1:SUS-ETMX_L3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-ETMY_L3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-ITMX_L3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-ITMY_L3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-BS_M3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-PR3_M3_OPLEV_PIT_OUT_DQ.mean,m-trend','H1:SUS-SR3_M3_OPLEV_PIT_OUT_DQ.mean,m-trend']

buffers= conn.fetch(start,stop,channels_pit)

pitch=buffers

#yaw
channels_yaw =  ['H1:SUS-ETMX_L3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-ETMY_L3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-ITMX_L3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-ITMY_L3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-BS_M3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-PR3_M3_OPLEV_YAW_OUT_DQ.mean,m-trend','H1:SUS-SR3_M3_OPLEV_YAW_OUT_DQ.mean,m-trend']

buffers= conn.fetch(start,stop,channels_yaw) 

yaw=buffers

#sum
channels_sum =  ['H1:SUS-ETMX_L3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-ETMY_L3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-ITMX_L3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-ITMY_L3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-BS_M3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-PR3_M3_OPLEV_SUM_OUT_DQ.mean,m-trend','H1:SUS-SR3_M3_OPLEV_SUM_OUT_DQ.mean,m-trend']

buffers= conn.fetch(start,stop,channels_sum) 
  
pdsum=buffers

#optic names for labeling plots
names = ['ETMX','ETMY','ITMX','ITMY','BS','PR3','SR3']

time = np.arange(start,stop,60)  

#set up plots 
 
plots = range(1,4)

optics = range(1,8)

colors = ['b','g','r','c','m','k','y']

titles= ['OPLEV PIT', 'OPLEV YAW', 'OPLEV SUM']



# make xtick date array

days = np.arange(start,stop+24*3600,24*3600)

week = range(1,8)

xlabel =[]

for ii in week:
    xlabel.append(gpstime.tconvert(days[ii-1])) 

#make plots
#    plt.setp(axes,xticks=[0, .14, .29, .43, .57, .72, .86],xticklabels=xlabel) 

for ii in optics:
    fig = plt.figure(1)
    fig.suptitle(titles[0] + ' \n Trends for '  + xlabel[0] + ' to ' + xlabel[6] ,fontsize=32)
    ax=plt.subplot(330+ii)
    ax.set_title(names[ii-1],fontsize=24)
    ax.plot(time,pitch[ii-1].data,colors[ii-1])
    ax.set_xticks(days)
    ax.set_xlim(start,stop)
    ax.set_xticklabels(xlabel,rotation=25,fontsize=10)
    plt.grid()


for ii in optics:
    fig = plt.figure(2)
    fig.suptitle(titles[0] + ' \n Trends for '  + xlabel[0] + ' to ' + xlabel[6] ,fontsize=32)
    ax=plt.subplot(330+ii)
    ax.set_title(names[ii-1],fontsize=24)
    ax.plot(time,yaw[ii-1].data,colors[ii-1])
    ax.set_xticks(days)
    ax.set_xlim(start,stop)
    ax.set_xticklabels(xlabel,rotation=25,fontsize=10)
    plt.grid()


for ii in optics:
    fig = plt.figure(3)
    fig.suptitle(titles[0] + ' \n Trends for '  + xlabel[0] + ' to ' + xlabel[6] ,fontsize=32)
    ax=plt.subplot(330+ii)
    ax.set_title(names[ii-1],fontsize=24)
    ax.plot(time,pdsum[ii-1].data,colors[ii-1])
    ax.set_xticks(days)
    ax.set_xlim(start,stop)
    ax.set_xticklabels(xlabel,rotation=25,fontsize=10)
    plt.grid()


plt.show()


