clear;close all;
IFO = 'H1';
chans = {[IFO ':SUS-ETMY_M0_DAMP_V_INMON'],...
    [IFO ':SUS-ETMX_M0_DAMP_V_INMON'],...
    [IFO ':SUS-ITMY_M0_DAMP_V_INMON'],...
    [IFO ':SUS-ITMX_M0_DAMP_V_INMON']};

if strcmp(IFO,'H1')
    weather_chan = ['H1:PEM-MY_TEMP_ROOF_WEATHER_DEGC'];
    vac_chans = {'H0:VAC-LY_Y1_PT120A_PRESS_TORR',...
        'H0:VAC-EY_Y3_PT410A_PRESS_TORR',...
        'H0:VAC-EX_X3_PT510A_PRESS_TORR'};
    save_folder = '~/Desktop/';
else
    weather_chan = ['L1:PEM-LVEA_TEMP_AIR_WEATHER_DEGC'];
    vac_chans = {'LVE-LY:Y1_620BTORR',...
        'LVE-EY:Y3_710BTORR',...
        'LVE-EX:X3_810BTORR'};
    save_folder = '/data/arnaud.pele/';
end
duration = 365*24*3600;
aaa = cdsgetdata([chans vac_chans weather_chan],'minute',1257815759-duration,duration);

save([save_folder 'weather_data_' IFO '.mat']);

%%
h1 = load('~/Desktop/weather_data_H1.mat');
l1 = load('~/Downloads/weather_data_L1.mat');

%%
Fs = h1.aaa(1).rate;
Ts = 1/Fs;
smooth_n = 500;
time = [Ts:Ts:h1.duration]/3600/24;

% Do some data processing??

figure(1)
subplot(221)
plot(time,smooth(h1.aaa(end).data,50),...
    'linewidth',2)
ylim([-10 40])
grid on
title('H1 outside temp (deg C)')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(223)
plot(time(h1.aaa(5).data<0.1),smooth(h1.aaa(3).data(h1.aaa(5).data<0.1)-mean(h1.aaa(3).data),smooth_n),...
    time(h1.aaa(5).data<0.1),smooth(h1.aaa(4).data(h1.aaa(5).data<0.1)-mean(h1.aaa(4).data),smooth_n),...
    'linewidth',2);
grid on
legend('ITMY','ITMX')
ylim([-50 50])
ylabel('um')

title('H1 corner-quads')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(222)
plot(time,smooth(l1.aaa(end).data,50),...
    [time(find(time==351)) time(find(time==351))],[-50 50],'k--',...
    'linewidth',2)
ylim([-10 40])
grid on
title('L1 outside temp (deg C)')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(224)
plot(time,smooth(l1.aaa(3).data-mean(l1.aaa(3.).data),smooth_n),...
    time,smooth(l1.aaa(4).data-mean(l1.aaa(4).data),smooth_n),...
    [time(find(time==351)) time(find(time==351))],[-50 50],'k--',...
    'linewidth',2);

grid on
legend('ITMY','ITMX','problems with violin mode')
ylim([-50 50])
ylabel('um')

title('L1 corner-quads')
xlim([0 365])
xlabel('days since Nov 15 2018')
FillPage('w');
saveas(gcf,'/ligo/www/www/exports/arnaud.pele/temps_corner.pdf')
saveas(gcf,'/ligo/www/www/exports/arnaud.pele/temps_corner.png')
%%
figure(2)
subplot(221)
plot(time,smooth(h1.aaa(end).data,50),...
    'linewidth',2)
ylim([-10 40])
grid on
title('H1 outside temp (deg C)')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(223)
plot(time(h1.aaa(6).data<0.1),smooth(h1.aaa(1).data(h1.aaa(6).data<0.1)-mean(h1.aaa(1).data),smooth_n),...
    time(h1.aaa(7).data<0.1),smooth(h1.aaa(2).data(h1.aaa(7).data<0.1)-mean(h1.aaa(2).data),smooth_n),...
    'linewidth',2);
grid on
legend('ETMY','ETMX')
ylim([-50 50])
ylabel('um')
title('H1 end-quads')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(222)
plot(time,smooth(l1.aaa(end).data,50),...
    [time(find(time==110)) time(find(time==110))],[-50 50],'k--',...
    [time(find(time==319)) time(find(time==319))],[-50 50],'g--',...
    'linewidth',2)
ylim([-10 40])
grid on
title('L1 outside temp (deg C)')
xlim([0 365])
xlabel('days since Nov 15 2018')

subplot(224)
vac1 = 1.2e-8;
vac2 = 1.3e-8;

plot(time(l1.aaa(6).data<vac1 & l1.aaa(6).data~=0),smooth(l1.aaa(1).data(l1.aaa(6).data<vac1 & l1.aaa(6).data~=0)-mean(l1.aaa(1).data),smooth_n),...
    time(l1.aaa(7).data<vac2 & l1.aaa(7).data~=0),smooth(l1.aaa(2).data(l1.aaa(7).data<vac2 & l1.aaa(7).data~=0)-mean(l1.aaa(2).data(250000:300000)),smooth_n)+10,...
    [time(find(time==110)) time(find(time==110))],[-50 50],'k--',...
    [time(find(time==319)) time(find(time==319))],[-50 50],'g--',...
    'linewidth',2);

grid on
legend('ETMY','ETMX','temp sensors off the wall','Oct vent')
ylim([-50 50])
ylabel('um')
title('L1 end-quads')
xlim([0 365])
xlabel('days since Nov 15 2018')

FillPage('w');
saveas(gcf,'/ligo/www/www/exports/arnaud.pele/temps_ends.pdf')
saveas(gcf,'/ligo/www/www/exports/arnaud.pele/temps_ends.png')