function varargout  = correct_temp(cruiseid)
% correct_temp : Apply the correction for temperature sensors
%
% CTD Calibration toolbox.
%
% INPUT: cruiseid: Cruise identification name. 
%
% USAGE: 
% correct_temp(cruiseid): reads the btl files and the cnv files in the cruise 
%                         database and apply the correction for the
%                         temperature sensors
%                     
%
% AUTHORS:Carlos Fonseca 
%         UM/CIMAS
%
% CHANGELOG: 
%   5-Jan-2006, Version 1.0
%		* Initial version.
% Check for correct number of inputs.
error(nargchk(1,1,nargin))

% Retrieve the necessary preferences for this cruise
group = group_name(cruiseid);

% btl_dir is the calibration data directory 
if ispref(group,'cal_btl_dir')
	btl_dir=getpref(group,'cal_btl_dir');
else
   	error(['The cal_btl_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end

% cruise_dir is the calibration data directory 
if ispref(group,'cal_data_dir')
	cruise_dir=getpref(group,'cal_data_dir');
else
  	error(['The cal_data_dir preference was not set for cruise: ' cruiseid '.  Run register_cruise.m']);
end

% Load data from the cruise database
db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
if exist(db_file)==2
	load(db_file);
else
	error(['The cruise data base file was not found on the path.  ' db_file]);
end
% Check for the necessary variables in the cruise database.
n=who('btl');
if isempty(n)==1
	error(['btl variable not found in the cruise database. Run load_btl.m ']);
	return;
end
n=who('cnv_load');
if isempty(n)==1
	error(['cnv_load variable not found in the cruise database. Run load_cnv.m ']);
	return;
end

stations= unique(vertcat(cnv_load.station));
disp('****************************************** ')
disp('TEMPERATURE SENSOR CORRECTION')
disp('****************************************** ')
disp('The data found in cnv_load goes from') 
disp(['the station ',num2str(min(stations)),' to the station ',num2str(max(stations)),'.'] ) 
disp('****************************************** ')
%
staa=input('Input first station: ');
stab=input('Input last station: ');

t1_slope=input('Enter the slope for the primary sensor: ');
t1_offset=input('Enter the offset for the primary sensor: ');

t2_slope=input('Enter the slope for the secondary sensor: ');
t2_offset=input('Enter the offset for the secondary sensor: ');

for j=1:1:length(btl) 
    if staa==btl(j).station %& cond_cal.castnbr(i)==bl(j).cast      
        staa_idx=j;
    end  
    if stab==btl(j).station %& cond_cal.castnbr(i)==bl(j).cast      
        stab_idx=j;
    end
end

[ctd_pr_idx, ctd_c1_idx, ctd_c2_idx, ctd_t1_idx, ctd_t2_idx, flag_conv1, flag_message1] = ...
    determine_column_names_of_btl_file (btl);

if exist('staa_idx')~=0
    if exist('stab_idx')==0
        stab_idx=length(btl);
    end

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%Correcting BTL files
    for i=staa_idx:1:stab_idx
        for j=1:length(btl(i).names);
            if strncmp(btl(i).names(j),'pressure',8);
                ctd_pr_idx=j;
            end
            if strncmp(btl(i).names(j),'t68_pri',7);
                ctd_t1_idx=j;
            end
            if strncmp(btl(i).names(j),'t68_sec',7);
                ctd_t2_idx=j;
            end
            if strncmp(btl(i).names(j),'t90_pri',7);
                ctd_t1_idx=j;
            end
            if strncmp(btl(i).names(j),'t90_sec',7);
                ctd_t2_idx=j;
            end
        end
        display(['Btl file']);
        display(['Applying the temperature correction for station ',num2str(btl(i).station)]);
        if btl(i).tcor==1
            display(['Temperature Sensor Correction for this station was already applied']);
            display(['Going to the next station']);
        else   
            btl(i).data(:,ctd_t1_idx) = btl(i).data(:,ctd_t1_idx) + (t1_offset+(t1_slope*btl(i).data(:,ctd_pr_idx)));
            btl(i).data(:,ctd_t2_idx) = btl(i).data(:,ctd_t2_idx) + (t2_offset+(t2_slope*btl(i).data(:,ctd_pr_idx)));
            btl(i).tcor=1;
        end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end
for j=1:1:length(cnv_load) 
 if staa==cnv_load(j).station %& cond_cal.castnbr(i)==bl(j).cast      
   staa_idx=j;
 end  
 if stab==cnv_load(j).station %& cond_cal.castnbr(i)==bl(j).cast      
   stab_idx=j;
 end
end
%Correcting CNV files
for i=staa_idx:1:stab_idx
     display(['CNV file']);
    display(['Aplying the temperature correction for station ',num2str(cnv_load(i).station)]);
    if cnv_load(i).tcor==1
    display(['Temperature Sensor Correction for this station was already applied']);
    display(['Going to the next station']);
    else
  	cnv_load(i).temppri=cnv_load(i).temppri+(t1_offset+(t1_slope*cnv_load(i).pressure));
    cnv_load(i).tempsec=cnv_load(i).tempsec+(t2_offset+(t2_slope*cnv_load(i).pressure));
    cnv_load(i).tcor=1;
    end

end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Open the output file

db_file = fullfile(cruise_dir,[cruiseid '_db.mat']);
save(db_file,'btl','-append');
save(db_file,'cnv_load','-append');
if nargout==0
	assignin('caller','ans',btl);
    return
else
    varargout{1}=btl;
    return
end
%

