#!/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:IMC-IM4_TRANS_NSUM_OUT16.mean,m-trend','H1:IMC-PWR_IN_OUT16.mean,m-trend' ]#, 'L1:GRD-ISC_LOCK_STATE_N.min,m-trend']
gpsStart = 1445017495
gpsStop = 1445052163
data = gwpy.timeseries.TimeSeriesDict.fetch(chans,gpsStart, gpsStop, verbose=True, pad=float("-inf"))

ratio = data['H1:IMC-IM4_TRANS_NSUM_OUT16.mean,m-trend'].value/ data['H1:IMC-PWR_IN_OUT16.mean,m-trend'].value


fig, [ax1,ax2]  = plt.subplots(2,1)
ax1.plot(data['H1:IMC-PWR_IN_OUT16.mean,m-trend'].value, data['H1:IMC-PWR_IN_OUT16.mean,m-trend'].value*ratio[0], label = f'slope of {ratio[0]:.2}')

ax1.plot(data['H1:IMC-PWR_IN_OUT16.mean,m-trend'].value, data['H1:IMC-IM4_TRANS_NSUM_OUT16.mean,m-trend'].value, '.', label = 'IM4/IMC input power')

ax2.plot( ratio, '.')


ax1.set_ylabel('IM4 trans power')
ax2.set_ylabel('throughput ratio')
ax1.legend()
ax1.set_xlabel('IMC input power')
ax2.set_xlabel('time [minutes]')
plt.savefig('IOO_throughput_power_up.png')

