#!/usr/bin/env python
# -*- coding: utf-8 -*-

#before running this, in command line : export NDS2_CLIENT_GAP_HANDLER=STATIC_HANDLER_NEG_INF


import gwpy.timeseries
import numpy as np
import matplotlib.pyplot as plt
import os
#import h5py

os.environ["NDS2_CLIENT_GAP_HANDLER"] = "STATIC_HANDLER_NEG_INF"


chans = ['H1:IOO-OFI_THERMISTOR_2_TEMPERATURE.mean,m-trend','H1:IOO-OFI_PD_A_DC_POWER.mean,m-trend' , 'H1:ASC-AS_C_NSUM_OUT16.mean,m-trend' ]#, 'L1:GRD-ISC_LOCK_STATE_N.min,m-trend']
gpsStart = 1446218947
gpsStop = 1446230265
data = gwpy.timeseries.TimeSeriesDict.fetch(chans,gpsStart, gpsStop, verbose=True, pad=float("-inf"))

ratio = data['H1:IOO-OFI_PD_A_DC_POWER.mean,m-trend'].value*100*1e-3/ data['H1:ASC-AS_C_NSUM_OUT16.mean,m-trend'].value


fig, [ax1,ax2, ax3, ax4]  = plt.subplots(4,1)
ax1.plot(data['H1:IOO-OFI_THERMISTOR_2_TEMPERATURE.mean,m-trend'].value , label = f'OFI thermistor 2 [C]')
ax2.plot(data['H1:IOO-OFI_PD_A_DC_POWER.mean,m-trend'].value , label = f'OFI PD A power [mW]')
ax3.plot(data['H1:ASC-AS_C_NSUM_OUT16.mean,m-trend'].value , label = f'AS port power arriving in HAM6[W]')
ax4.plot(ratio, label ='ratio of power towards HAM7/ power towards HAM6')


for ax in [ax1,ax2,ax3,ax4]:
    ax.legend()
ax4.set_xlabel('time [minutes]')
plt.savefig('OFI_temp_changes.png')

