% Script that combines and simplifies the ctd calibration process
% Author: Jay Hooper 11/11
clear all;
%Step 1 - 
%% Step 1: Define cruise name and directories. Run make_cruise_dir('cruise_name').

%prompts user for the cruise id (ex. ab1104 where 1104=yymm)
crname = input(['Enter cruise id (ex. abyymm) : '], 's');
    
% Creates cruise directories as well as thre m_files (ctdcalpath.m,
% local.m, and register_cruise.m)    
make_cruise_dir(crname);

% Adds necessary paths, creates cruiseid variable, and runs
% register_cruise_cruiseid.m for managing data
local;

%% Step 2: Copy the data of CTD: *.bl, *.btl, and *.cnv(down cast only) into the
%% directories raw_data, proc_data, and bottle
return
% Change 1 to 0 if you run proc_ctdcal.m more than once.  Not good to keep
% writing over data.
if 0,
% Copy files from data directry... We can create a function to do this...    
[raw_status,raw_m,raw_mid]=copyfile('/Users/jhooper/Documents/Work_data/Abicos/ab1104/sbe_proc/raw_data/*.bl','/Users/jhooper/Documents/Work_data/Abicos/ab1104/ab1104/raw_data');
[proc_status,proc_m,proc_mid]=copyfile('/Users/jhooper/Documents/Work_data/Abicos/ab1104/sbe_proc/1db/proc_data/d*.cnv', '/Users/jhooper/Documents/Work_data/Abicos/ab1104/ab1104/proc_data');
[bot_status,bot_m,bot_mid]=copyfile('/Users/jhooper/Documents/Work_data/Abicos/ab1104/sbe_proc/bottle/*.btl', '/Users/jhooper/Documents/Work_data/Abicos/ab1104/ab1104/bottle');
end;

%% load the .bl, .btl, and .cnv data and save in to a database
%Something not right going on with these processes.  Might just be the
%CLIVAR data.  Check with Abico data.  Put commented lines back in
load_bl(cruiseid)     
load_btl(cruiseid)% CLIVAR Had to comment out line 872 of read_sbebtl.m ERROR: ??? Undefined function or variable "sensors".
load_cnv(cruiseid)% CLIVAR Had to comment out line 295 of read_sbebtl.m ERROR: ??? Undefined function or variable "sensors".


%% Further processing of the data

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Use this to automate the process instead of entering in the station     % 
% inteval every time you run one of these functions.  Will probably take a%
% lot of manipulating and tweaking, but shouldn't be too bad.             %
%                                                                         %
%staa = input('Enter first station: ');                                   % 
%stab = input('Enter last station: ');                                    %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

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

% 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


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

% For salts
load_salts(cruiseid)   % ok
make_cond_sumfile(cruiseid)    % Doesn't work.  gamma_n function not compiled
                               % to run on Macs, PC only
% For oxygen
load_oxy(cruiseid)     % ok
make_oxy_sumfile(cruiseid)     % Doesn't work.  gamma_n function not compiled
                               % to run on Macs, PC only
                               
%% 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)        % File doesn't exist


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

% 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 File doesn't exist

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%% 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);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Rigo doesn't run these last few stops.  The calibration is done after the above step.
%% The following look to be optional for looking at the calibrated data in further detail

%% 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);
