% Programa de prueba para correr los programas que realizan la calibracion
% de los CTD

% %%
% ccp
% 
% % We define the ID and directory of the cruise:
% % cruiseid='ab0911'; % I run this as an example in the Lab. CF help to figure
% %                      out a problem to read the .bl data
% cruiseid='ab1104';
% cruise_dir=['C:\cal_data\' cruiseid '\'];
% 
% 
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %% To make the process we follow the steps:
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% %%%% IMPORTANT NOTE: UNCOMMENT STATEMENTS ONLY AT THE BEGGINING OF THE %%%
% %%%% CRUISE
% 
% % %% Step #1. Create the directory of the cruise that go to be calibrated
% % make_cruise_dir(cruiseid)  % OK
% % 
% % eval(['cd ' cruise_dir]);
% 
% %%
% ctdcalpath;        % Run the ctdcalpath that has been created

%
% All theinstruction commented above are now in the script called have_cruise_ready.m
%

have_cruise_ready

%% Step #2. Registrar el crucero (Un paso insoportable)
% register_cruise(cruiseid) % No funciona por ahora, 11/24/2008

% - Edit the script register_cruise_'cruiseid'.m that reside in the mfiles
% subdirectory
% - Run that script

%% New for cruise ab1104...created by RG 4/15/2011
% Copy files from data directry... We can create a function to do this...
! copy Z:\Documents\Work_data\Abicos\ab1104\ctd\raw_data\*.bl Z:\Documents\Work_data\Abicos\abp1104\raw_data
! copy Z:\Documents\Work_data\Abicos\ab1104\ctd\1db\proc_data\d*.cnv Z:\Documents\Work_data\Abicos\abp1104\proc_data
! copy Z:\Documents\Work_data\Abicos\ab1104\ctd\bottle\*.btl Z:\Documents\Work_data\Abicos\abp1104\bottle


%% Step #3. Copy the data of CTD: *.bl, *.btl, and *.cnv(down cast only) in
%% the directories: raw_data; bottle; and proc_data

% NOTE: These script run for all the data in each directory.
% You don't have a chance to tell which station(s) do you want to load in
% the Matlab structure

load_bl(cruiseid)      % ok after a lot of work...
load_btl(cruiseid)
load_cnv(cruiseid)


%% Step #4. Edit CTD data for both sensors (if applicable)

% NOTE: In these script you have a chance to tell which station(s) 
% do you want to load in the Matlab structure

fill_surface(cruiseid)   % ok
despike1(cruiseid)       % ok
despike2(cruiseid)       % ok
deloop(cruiseid)         % ok


%% Step #5. Make some plot using ctd data

% NOTE: In this script you have a chance to tell which station(s) 
% do you want to load and compare the sensors...
% In the first time you can use the raw or deloop data...

compare_ctd_sensors(cruiseid)  % ok


%% Step #6. Load in the salt and O2 samples

% NOTE: Read all files that you have in the subdirectry salts

% For salts
load_salts(cruiseid)   % ok
make_cond_sumfile(cruiseid)    % do not work fine. Break in the sta 17 for ab0911

% For oxygen
load_oxy(cruiseid)     % ok
make_oxy_sumfile(cruiseid)     % do not work fine


%% Step #7. Make some plot using ctd data and samples data (if applicable)

% NOTE: You can select the number of station(s) 

compare_tsplot(cruiseid)   % ok

check_deep_water_ts(cruiseid)        % New function from CF


%% Step #8. Here start really the calibration, using the best sensor that
%% agree with the samples data

% NOTA (sp.): Compare de verdad cuales son los mejores sensores al 
% relacionarlos con los datos de las muestras obtenidas en el crucero

% NOTE: You can select the number of station(s) 

% Need check really bad salts samples 4/30/2010
check_bad_salt_CF1

% Salinity
compare_cond(cruiseid)

rg_compare_cond(cruiseid,11,11);   % Add on 4/7/2010

% Hacer el print de las salidas de este programa
% print_compare_cond(cruiseid)
% close all

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% NOTA IMPORTANTE: Cambiar los nombres a la figura para que mis
%%%% programas que hacen el informe las puedan leer
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


% Oxygen
compare_oxy(cruiseid)

% print_compare_oxy(cruiseid)
% close all

%% Step #9. Correr ahora los programas de las calibraciones para cada uno de los
%% sensores...
 
% Conductivity
% [offset_out,offset_fit,slope_out,slope_fit,pcor]=cond_cal(cruiseid); %
% Old function. Commented out by RG on 4/28/2011. Using the "new" CF
% function. See note below...
[cond_coeff]=cond_calfit_w(cruiseid);

%%%% [cond_coeff]=cond_calfit_w(cruiseid)   % New function to do conductivity
%%%% calibration 4/30/2010

% Salvar los resultados para aplicarlos a los datos de CTD despues y hacer
% las grafiacas para el reporte
% print_cond_cal(cruiseid) %%% NOTA: da problemas. Revisar 11/24/2008

% Oxygen
%[oxy_coeff,fh]=oxy_cal(cruiseid);
% Old function. Commented out by RG on 4/28/2011. Using the "new" CF
% function. See note below...
[oxy_coeff]=oxy_cal_new_equation1(cruiseid);

% New function to calibrate the oxygen.... 4/30/2010
% [oxy_coeff]=oxy_cal_new_equation1(cruiseid)

% Salvar los resultados para aplicarlos a los datos de CTD despues y hacer
% las graficas para el reporte
% print_oxy_cal(cruiseid) %%% NOTA: Si funciona

%% Now apply calibration...using the values for both sensors: conductivity
%% and oxygen...
% apply_calibration1 % Generic function. Need to be edited... 4/30/2010
% Example
apply_calibration1_ab1104(cruiseid,cond_coeff,oxy_coeff);

%% 4/30/2010
make_cal_sumfile_new_equation(cruiseid,cond_coeff,oxy_coeff)

%% To evaluate the calibration use  (4/30/2010)
eval_calibration_cond(cruiseid);

eval_calibration_oxy(cruiseid);
