function datastr = getaverageCTD(filename)

eval (['load ' filename '.mat'])
eval (['load ' filename 'index.asc'])
 
 i = eval ([ filename 'index'])



sizei=size(i)

%if sizei(1)==2
 %   i(3)=i(1);
  %  i(4)=i(2);
   % sizei=4
%end

if sizei(1)>=4
top=[ctdpress(i(3):i(4)) ctdtemp(i(3):i(4)) ctdcond(i(3):i(4)) ctdsal(i(3):i(4))  ctdturbidity(i(3):i(4))]
topstart=ctdtime(i(3),:)
topend=ctdtime(i(4),:)
[p,q]=size(top)
meantop=mean(top)
data(1,:)=meantop
times(1,1:3)=topstart;
times(1,4:6)=topend;
times(1,7)=p
end
pause;

if sizei(1)>=6
mid=[ctdpress(i(5):i(6)) ctdtemp(i(5):i(6)) ctdcond(i(5):i(6)) ctdsal(i(5):i(6)) ctdturbidity(i(5):i(6))]
midstart=ctdtime(i(5),:)
midend=ctdtime(i(6),:)
[p,q]=size(mid)
meanmid=mean(mid)
data(2,:)=meanmid
times(2,1:3)=midstart;
times(2,4:6)=midend;
times(2,7)=p
pause;
end

if sizei(1)>=8
bot=[ctdpress(i(7):i(8)) ctdtemp(i(7):i(8)) ctdcond(i(7):i(8)) ctdsal(i(7):i(8)) ctdturbidity(i(7):i(8))]
botstart=ctdtime(i(7),:)
botend=ctdtime(i(8),:)
[p,q]=size(bot)
meanbot=mean(bot)
data(3,:)=meanbot
times(3,1:3)=botstart;
times(3,4:6)=botend;
times(3,7)=p
pause;
end

if sizei(1)>=10
bot2=[ctdpress(i(9):i(10)) ctdtemp(i(9):i(10)) ctdcond(i(9):i(10)) ctdsal(i(9):i(10)) ctdturbidity(i(9):i(10))]
botstart=ctdtime(i(9),:)
botend=ctdtime(i(10),:)
[p,q]=size(bot2)
meanbot2=mean(bot2)
data(4,:)=meanbot2
times(4,1:3)=botstart;
times(4,4:6)=botend;
times(4,7)=p
pause;
end

if sizei(1)>=12
bot3=[ctdpress(i(11):i(12)) ctdtemp(i(11):i(12)) ctdcond(i(11):i(12)) ctdsal(i(11):i(12)) ctdturbidity(i(11):i(12))]
botstart=ctdtime(i(11),:)
botend=ctdtime(i(12),:)
[p,q]=size(bot3)
meanbot3=mean(bot3)
data(5,:)=meanbot3
times(5,1:3)=botstart;
times(5,4:6)=botend;
times(5,7)=p
pause;
end

if sizei(1)>=14
bot4=[ctdpress(i(13):i(14)) ctdtemp(i(13):i(14)) ctdcond(i(13):i(14)) ctdsal(i(13):i(14)) ctdturbidity(i(13):i(14))]
botstart=ctdtime(i(13),:)
botend=ctdtime(i(14),:)
[p,q]=size(bot4)
meanbot4=mean(bot4)
data(6,:)=meanbot4
times(6,1:3)=botstart;
times(6,4:6)=botend;
times(6,7)=p
pause;

end
eval(['save ' filename 'AVG.txt data -ascii'])
eval(['save ' filename 'times_n.txt times -ascii'])
eval(['save ' filename 'times_n.mat times -mat'])
eval(['save ' filename 'AVG.mat data -mat'])


%end
