clc; clear; close all;

current_ambient='/Users/kakani/Google Drive Stanford/1503xx_LaserTest/150311_LaserAmbientTest/Current/Test 2015-03-11-2-Current_noheader.dat';
data=dlmread(current_ambient);
time_curramb=data(:,3);
currmax_curramb=data(:,5);
currave_curramb=data(:,6);
currmin_curramb=data(:,7);
clear data
photodiode_ambient='/Users/kakani/Google Drive Stanford/1503xx_LaserTest/150311_LaserAmbientTest/TempLightCurrent/Test 2015-03-11-2-Temp-Light_noheader.dat';
data=dlmread(photodiode_ambient);
time_amb=data(:,5);
templaser_amb=data(:,9);
tempair_amb=data(:,10);
curr_amb=data(:,6);
photo_amb=data(:,7);
clear data


figure(2)
[ax,h1,h2] = plotyy(time_amb,templaser_amb,time_amb,curr_amb);
set(h1,'Color','b')
set(h2,'Color','r')
hold(ax(1))
plot(ax(1),time_amb,tempair_amb,'b+')
set(get(ax(1),'YLabel'),'String','Temperature (C)','FontSize',16,'FontName','Times New Roman') 
set(ax(1),'YColor','b','YTick',[5,10,15,20,25,30],'FontSize',14,'FontName','Times New Roman')
hold(ax(2))
plot(ax(2),time_amb,photo_amb,'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 Ambient Temperatures')

clear ax h1 h2

% index=find(currave_curramb<0);
% currave_curramb(index)=0.0001;
% percent=(currmax_curramb-currmin_curramb)./currave_curramb*100;

figure(3)
[ax,h1,h2] = plotyy(time_curramb,currave_curramb,time_amb,templaser_amb);
hold(ax(1))
plot(ax(1),time_curramb,currmax_curramb,'b',time_curramb,currmin_curramb,'b')
hold(ax(2))
% plot(ax(2),time_curramb,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 Ambient Temperatures')

window =100;
b=(1/window)*ones(1,window);
a=1;
curr_ambsm=filter(b,a,curr_amb);
figure(4)
plot(time_amb,curr_amb,time_amb,curr_ambsm)
title('Laser Performance in Ambient Temperatures')

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

figure(5)
plot(time_amb,derivative,'.')
grid on
title('Laser Performance in Ambient 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_amb(index),curr_ambsm(index),'k*')
hold off
figure(5)
hold on
plot(time_amb(index),index*0,'k*')
hold off

counter=1;
range_amb=templaser_amb(index);
remove=find(range_amb<5);
range_amb(remove)=[];
minrange_amb=min(range_amb)
maxrange_amb=max(range_amb)