gwd = GWData();
gwd.make_kerberos_ready;
gwd.site_info(2).server = 'h1nds1';
gwd.site_info(2).port = 8088;
chans = {'H1:ASC-AS_A_DC_NSUM_OUT_DQ.max,m-trend'}; %, 
start_time = 1184305878-60*60*24*5;
%1164556817 
stop_time = 1187733618;
dt = stop_time-start_time;%60*60*24*7;
tic
[data, t, info] = gwd.fetch(start_time, dt, chans);
toc

%find times when AS_A exceeded some value
flash_indx=find(data(:,1)>12e4);
flash_times=start_time+t(flash_indx);

fast_chans = {'H1:ASC-AS_A_DC_NSUM_OUT_DQ','H1:ASC-AS_C_NSUM_OUT_DQ','H1:ASC-AS_B_DC_NSUM_OUT_DQ', 'H1:OMC-DCPD_SUM_OUT_DQ','H1:OMC-PZT1_MON_DC_OUT_DQ'}; %, 

gwd.site_info(2).server = 'nds2.ligo.caltech.edu';
gwd.site_info(2).port = 31200;
tic
for ii=1:length(flash_times)
    [flash_data{ii}, flash_t{ii}, info] = gwd.fetch(flash_times(ii)-60, 120, fast_chans);
    this_flash_time=flash_t{ii};
    this_flash_data = flash_data{ii};
    flash_high_indx = find(this_flash_data(:,1)>5e4);
%     flash_length(ii)=length(diff(flash_high_indx)==1)  %make sure that there is only one flash in each segment, otherwise power calc will be off.
%     if high_length ~= flash_length
%         disp 'mulitple flashes in this time'
%     end
    time_step=this_flash_time(2);
    flash_length(ii) = time_step*length(flash_high_indx);
    flash_energy(ii)=time_step*trapz(this_flash_data(flash_high_indx,2));
end
toc


%% plot some things about energy and duration of flashes
figure(1)
subplot(2,1,1)
plot(t, data(:,1))
title('AS A SUM')
% subplot(2,1,2)
% plot(t, data(:,2))
% title('AS C SUM')

figure(2)
subplot(2,1,1)
hist(flash_length,100)
xlabel('Duration (seconds)')
subplot(2,1,2)
hist(flash_energy,100)
xlabel('Energy (Joules past fast shutter)')

long_flashes=find(flash_length>2)
high_energy_flashes=find(flash_energy>1)
%grab some info from the shutter for the highest energy flashes
shutter_chans={'H1:SYS-MOTION_C_SHUTTER_G_STATE', 'H1:SYS-MOTION_C_SHUTTER_G_TRIGGER_VOLTS'};

for ii=1:length(high_energy_flashes)
    [shutter_data, shutter_t, info] = gwd.fetch(flash_times(high_energy_flashes(ii))-60, 120, shutter_chans);
    %make plotrs of the higest energy flashes
    this_flash_data=flash_data{high_energy_flashes(ii)};
    this_flash_time=flash_t{high_energy_flashes(ii)};
    %find the time when the shutter triggered so you can make zoomed in
    %plots
    shutter_t= shutter_t+0.125;
    shutter_trig_time = shutter_t(find(shutter_data(:,1)>0.1,1));
    
    ASC_RCG_mA=this_flash_data(:,2)/(1.62e-5*2.07e5);  %first undo the calibration in the filter module (divide by 1.62 Watts/cnt) then calibrate cnts into mA on QPD 2*10^(36/20)*1638.4 cnts/V = 2.07e5 cnts/mA) see a;pg 29078 and comments
    Trigger_mA = shutter_data(:,2)/0.2845;  % gain from D1001974
    figure(100+ii)
    subplot(2,1,1)
    plot(this_flash_time, this_flash_data(:,1)/1e5,this_flash_time, this_flash_data(:,3)/1e5,this_flash_time, this_flash_data(:,2),shutter_t, shutter_data(:,2)) 
    legend('AS A SUM counts/1e5', 'AS B sum counts/1e5','AS C sum (W into HAM6)', 'Shutter G TRIGGER Volts (Beckhoff)','Location', 'NorthEast', 'FontSize', 5)
%     subplot(2,2,2)
%     plot(this_flash_time, this_flash_data(:,2), this_flash_time, this_flash_data(:,1)./1e5, shutter_t, shutter_data(:,1))
%     title('AS C')
%     ylim([0,3])
%     legend('Watts into HAM6', 'SYS MOTION C SHUTTER G TRIGGER VOTLS')
    subplot(2,1,2)
    plot(shutter_t, shutter_data(:,1),this_flash_time, (this_flash_data(:,5))/10,this_flash_time, this_flash_data(:,4)./25) 
    legend('shutter state (Beckhoff)', 'PZT1 MON', 'OMC DCPD SUM/25 mA','Location', 'NorthWest', 'FontSize', 5)
    ylim([-0.1,1.1])
    for kk=1:2
        subplot(2,1,kk)
        xlim([shutter_trig_time-7,shutter_trig_time+2])
        xlabel('Seconds')
    end
    print(strcat('AS_flash_',num2str(flash_times(high_energy_flashes(ii)))), '-dpng')
end

