clc; clear; close all

current_cold='/Users/kakani/Google Drive Stanford/1503xx_LaserTest/150312_LaserColdTest/Current-Maxim-Gorkiy/Test 2015-03-12-Current_noheader.dat';
data=dlmread(current_cold);
time_currcold=data(:,3);
currmax_currcold=data(:,5);
currave_currcold=data(:,6);
currmin_currcold=data(:,7);
clear data
photodiode_cold='/Users/kakani/Google Drive Stanford/1503xx_LaserTest/150312_LaserColdTest/Photodiode-Temperature-Current-SMT232/Test 2015-03-12-Temp-Light_noheader.dat';
data=dlmread(photodiode_cold);
time_cold=data(:,5);
templaser_cold=data(:,9);
tempair_cold=data(:,10);
curr_cold=data(:,6);
photo_cold=data(:,7);
clear data

figure(1)
[ax,h1,h2] = plotyy(time_cold,templaser_cold,time_cold,curr_cold);
set(h1,'Color','b')
set(h2,'Color','r')
hold(ax(1))
plot(ax(1),time_cold,tempair_cold,'b+')
set(get(ax(1),'YLabel'),'String','Temperature (C)','FontSize',16,'FontName','Times New Roman') 
set(ax(1),'YColor','b','YTick',[-5,0,5,10,15,20,25],'FontSize',14,'FontName','Times New Roman')
hold(ax(2))
plot(ax(2),time_cold,photo_cold,'r+')
set(get(ax(2),'YLabel'),'String','Voltage (V)','FontSize',16,'FontName','Times New Roman')
set(ax(2),'YColor','r','YTick',[0,2,4,6,8,10,12],'FontSize',14,'FontName','Times New Roman')
grid on
legend('Laser Temperature (C)','Air Temperature (C)','Laser Current (A/V)','Photodiode Voltage (V)','Location','North')
xlabel('Time (hrs)','FontSize',16,'FontName','Times New Roman')
title('Laser Performance in Cold Temperatures')

clear ax h1 h2

% index=find(currave_currcold<0);
% currave_currcold(index)=0.0001;
% percent=(currmax_currcold-currmin_currcold)./currave_currcold*100;

figure(3)
[ax,h1,h2] = plotyy(time_currcold,currave_currcold,time_cold,templaser_cold);
hold(ax(1))
plot(ax(1),time_currcold,currmax_currcold,'b',time_currcold,currmin_currcold,'b')
hold(ax(2))
% plot(ax(2),time_currcold,percent,'k')
set(h1,'Color','b','Marker','x')
set(h2,'Color','r')
set(ax(1),'Ycolor','b','YTick',[0,2,4,6,8,10,12],'FontSize',14,'FontName','Times New Roman')
set(ax(2),'YColor','r','YTick',[-5,0,5,10,15,20,25],'FontSize',14,'FontName','Times New Roman')
xlabel('Time (hrs)','FontSize',16,'FontName','Times New Roman')
set(get(ax(1),'YLabel'),'String','Currrent (A)','FontSize',16,'FontName','Times New Roman')
set(get(ax(2),'YLabel'),'String','Temperature (C)','FontSize',16,'FontName','Times New Roman') 
grid on
title('Laser Performance in Cold Temperatures')

window =100;
b=(1/window)*ones(1,window);
a=1;
curr_coldsm=filter(b,a,curr_cold);
figure(4)
plot(time_cold,curr_cold,time_cold,curr_coldsm)
title('Laser Performance in Cold Temperatures')

derivative=curr_cold*0;
for i=1:1:size(curr_coldsm,1)-window
    derivative(i+1,1)=curr_coldsm(i+window,1)-curr_coldsm(i,1);
end

figure(5)
plot(time_cold,derivative,'.')
grid on
title('Laser Performance in Cold Temperatures')

index=[];
for i=1:1:size(derivative,1)-1
    if derivative(i,1)<0 && derivative(i+1,1)>0
        index=[index,i];
    end
end

figure(4)
hold on
plot(time_cold(index),curr_coldsm(index),'k*')
hold off
figure(5)
hold on
plot(time_cold(index),index*0,'k*')
hold off

counter=1;
range_cold=templaser_cold(index);
remove=find(range_cold<5);
range_cold(remove)=[];
minrange_cold=min(range_cold)
maxrange_cold=max(range_cold)