% Creating a new data set for the autosal values
function [output]=raw_autosal(datfile,rawfile,outfile)
addpath C:\WWARD2\CTD\Calibration\SeaBird_tools;
addpath C:\WWARD2\CTD\Calibration\Seawater;
load_dat=['load ',datfile,'.dat']
eval(load_dat)
aux_dat=eval(datfile)
load_raw=['load ',rawfile,'.raw']
eval(load_raw)
aux_raw=eval(rawfile)
bottle=aux_dat(:,2);
year=input('Enter the year: ');
month=input('Enter the month: ');
day=input('Enter the day: ');
hour=aux_raw(:,11);
minute=aux_raw(:,12);
second=aux_raw(:,13);
k=1;
limite=1;
while limite <= length(bottle)
    bottle_idx=find(bottle==bottle(limite,1));
    limite=max(bottle_idx)+1
    data=aux_raw(min(bottle_idx):max(bottle_idx),1:10);
    d=size(data);
    data=reshape(data,d(1)*d(2),1)
    mean_rt=mean(data);
    std_rt=std(data);
    mean_rt_new=0;
    std_rt_new=0;
    last_std=std_rt;
    while std_rt_new < last_std
     if std_rt < 0.000024
     aux_idx=find(data >= mean_rt-1.8*std_rt & data <= mean_rt+1.8*std_rt);
     else
     aux_idx=find(data >= mean_rt-1.45*std_rt & data <= mean_rt+1.45*std_rt);
     end
     new_data=data(aux_idx);
     mean_rt_new=mean(new_data);
     std_rt_new=std(new_data);
     data=new_data;
     mean_rt=mean_rt_new;
     last_std=std_rt;
     std_rt=std_rt_new;
     end
     data
     length(data)
     pause
     output(k,1)=aux_dat(max(bottle_idx),1);
     output(k,2)=bottle(max(bottle_idx));
     output(k,3)=mean_rt;
     output(k,4)=std_rt;
     output(k,5)=length(data);
     sw_sals(max(data)/2,24)
     sw_sals(min(data)/2,24)
     output(k,6)=sw_sals(max(data)/2,24)-sw_sals(min(data)/2,24);
     output(k,7)=year;
     output(k,8)=month;
     output(k,9)=day;
     output(k,10)=hour(max(bottle_idx));
     output(k,11)=minute(max(bottle_idx));
     output(k,12)=second(max(bottle_idx));
     sw_sals(max(data)/2,24)
     sw_sals(min(data)/2,24)
     output(k,6)=sw_sals(max(data)/2,24)-sw_sals(min(data)/2,24);
     k=k+1;
     fid=fopen(outfile,'w')
     fprintf(fid,'%%Station\t Bottle\t Mean RT\t Std Dev RT\t Data Used\t QC  year month day hour min sec\n')
     fprintf(fid,'%d\t %d\t %1.5f\t %1.6f\t %d\t %1.4f %d\t %d\t %d\t %d\t %d\t %d\n',output')
     fclose(fid)
 end
 return