#!/usr/bin/env python 
import nds
import numpy as np 
import matplotlib.pyplot as plt
import gpstime


def myround(x, base=60):
    return int(base * round(float(x)/base)) 


conn = nds.connection('nds.ligo.caltech.edu', 31200)
#conn = nds.connection('h1nds1',8088)
conn.set_parameter('GAP_HANDLER', 'STATIC_HANDLER_NAN')

startO1 = myround(1126623517)
stopO1 = myround(1136649617)
startO2 = myround(1164556817)
stopO2 = myround(gpstime.tconvert('dec 23 2016 0:00 utc'))


#get H1 data
channelsH1 = ['H1:PEM-EY_WIND_ROOF_WEATHER_MPH.max,m-trend','H1:ISI-GND_STS_ITMY_Z_BLRMS_100M_300M.mean,m-trend','H1:CDS-SENSMON_CAL_SNSW_EFFECTIVE_RANGE_MPC.mean,m-trend']

dataH1O1 = conn.fetch(startO1,stopO1,channelsH1)
dataH1O2 = conn.fetch(startO2,stopO2,channelsH1)

# get L1 data
channelsL1 = ['L1:PEM-LVEA_WIND_10M_WEATHER_MPH.max,m-trend','L1:ISI-GND_STS_ITMY_Z_BLRMS_100M_300M.mean,m-trend','L1:CDS-SENSEMON_CAL_L1_SNSW_EFFECTIVE_RANGE_MPC.mean,m-trend']

dataL1O1 = conn.fetch(startO1,stopO1,channelsL1)
dataL1O2 = conn.fetch(startO2,stopO2,channelsL1)


# make plots
optics = range(3)
time = range(len(dataH1O2[0].data))


fig = plt.figure(1)
ax=plt.subplot(311)
ax.set_title(channelsH1[0],fontsize=24)
ax.plot(time,dataH1O2[0].data,linewidth=2.0)
ax.set_ylim(0,50)
plt.grid()
ax=plt.subplot(312)
ax.set_title(channelsH1[1],fontsize=24)
ax.plot(time,dataH1O2[1].data,linewidth=2.0)
ax.set_ylim(0,4e3)
plt.grid()
ax=plt.subplot(313)
ax.set_title(channelsH1[2],fontsize=24)
ax.plot(time,dataH1O2[2].data,linewidth=2.0)
plt.grid()
ax.set_ylim(0,100)

fig = plt.figure(2)
ax=plt.subplot(311)
ax.set_title(channelsL1[0],fontsize=24)
ax.plot(time,dataL1O2[0].data,linewidth=2.0)
ax.set_ylim(0,50)
plt.grid()
ax=plt.subplot(312)
ax.set_title(channelsL1[1],fontsize=24)
ax.plot(time,dataL1O2[1].data,linewidth=2.0)
ax.set_ylim(0,4e3)
plt.grid()
ax=plt.subplot(313)
ax.set_title(channelsL1[2],fontsize=24)
ax.plot(time,dataL1O2[2].data,linewidth=2.0)
plt.grid()
ax.set_ylim(0,100)

fig = plt.figure(3)
ax=plt.subplot(311)
ax.set_title(channelsL1[0],fontsize=24)
ax.plot(time,dataL1O2[0].data,'b',linewidth=2.0)
ax.plot(time,dataH1O2[0].data,'r',linewidth=2.0)
ax.set_ylim(0,50)
plt.grid()
ax=plt.subplot(312)
ax.set_title(channelsL1[1],fontsize=24)
ax.plot(time,dataL1O2[1].data,'b',linewidth=2.0)
ax.plot(time,dataH1O2[1].data,'r',linewidth=2.0)
ax.set_ylim(0,4e3)
plt.grid()
ax=plt.subplot(313)
ax.set_title(channelsL1[2],fontsize=24)
ax.plot(time,dataL1O2[2].data,'b',linewidth=2.0)
ax.plot(time,dataH1O2[2].data,'r',linewidth=2.0)
plt.grid()
ax.set_ylim(0,100)



fig = plt.figure(4)
ax=plt.subplot(311)
ax.set_title(channelsL1[0],fontsize=24)
ax.plot(time,dataL1O1[0].data,'b',linewidth=2.0)
ax.plot(time,dataH1O1[0].data,'r',linewidth=2.0)
ax.set_ylim(0,50)
plt.grid()
ax=plt.subplot(312)
ax.set_title(channelsL1[1],fontsize=24)
ax.plot(time,dataL1O1[1].data,'b',linewidth=2.0)
ax.plot(time,dataH1O1[1].data,'r',linewidth=2.0)
ax.set_ylim(0,4e3)
plt.grid()
ax=plt.subplot(313)
ax.set_title(channelsL1[2],fontsize=24)
ax.plot(time,dataL1O1[2].data,'b',linewidth=2.0)
ax.plot(time,dataH1O1[2].data,'r',linewidth=2.0)
plt.grid()
ax.set_ylim(0,100)


plt.show()
