%% CTD Calibration m-file that goes through the routines necessary for
%% calibrating the Seabird processed data
% Note:  It is critical that these scripts are run in the following order.
% They continually build upon each other, creating new variables that are
% required in subsequent functions

% Needs to be run to make sure the proper paths are set and the cruiseid is
% loaded.

%prompts user for the cruise id (ex. ab1104 where 1104=yymm)
cruiseid = 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(cruiseid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Load Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% Change directory locations accordingly

copyfile('/Users/jhooper/Documents/Work_data/WBTS/ab1209/sbe_ctd/raw_data/*.bl',...
    '/Users/jhooper/Documents/Work_data/WBTS/ab1209/raw_data','f');
copyfile('/Users/jhooper/Documents/Work_data/WBTS/ab1209/sbe_ctd/bottle/*.btl',...
    '/Users/jhooper/Documents/Work_data/WBTS/ab1209/bottle','f');
copyfile('/Users/jhooper/Documents/Work_data/WBTS/ab1209/sbe_ctd/1db/proc_data/d*.cnv',...
    '/Users/jhooper/Documents/Work_data/WBTS/ab1209/proc_data','f');


cruiseid='ab1209';



bl = load_bl_molly(cruiseid);
btl = load_new_btl(cruiseid);
[cnv cnv_load] = load_new_cnv_molly(cruiseid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Clean Up Data %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% fill_surface2 - Fills the data gap between the surface and where the CTD
% package was actually stopped right below the surface
% despike1&2 - Remove spikes in the primary and secondary CTD sensors
% deloop - Remove further potential density inversions created due to
% ship roll  %Molly automated 
cnv_fill = fill_surface2(cruiseid)

cnv_ds1 = despike1_jay(cruiseid)

cnv_ds1 = despike2_new(cruiseid)

cnv_gterp = gap_interp(cruiseid)

cnv_dlp = deloop_molly(cruiseid)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%% Sensor and Bottle Comparison %%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% could use this to fix our primary T sensor
correct_temp_molly(cruiseid)
% compare_ctd_sensors.  Plots both sensor profiles in one figure and
% then plots the differences next to it for each sensor parameter
compare_ctd_sensors_jay(cruiseid) 

plot_historical_data(cruiseid)

% compare_tsplot - Creates a TS plot allowing you to see the comparison
% between the sensor data and the bottle data.  Determine which sensor
% best fits the bottle data
% NOTE: If you enter historical data you must put in single quotes 
% i.e. 'abaco_historical.dat' 
compare_tsplot_old(cruiseid)
compare_toplot(cruiseid)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Salts %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

process_autosal_ab1202
% must be run from within the salts directory
clear salts
salts = load_salts_molly(cruiseid);
% make_cond_sumfile - creates a file combining the autosal salts with
% sensor profiles at each corresponding fired depth
cond_match = make_cond_sumfile_molly(cruiseid); 

check_bad_salt_CF1

%compare_cond - 
compare_cond(cruiseid)

% cond_calfit_w - This is has desired cal depth weighted, hardcoded in.
% Change it 
[cond_coeff]=cond_calfit_w(cruiseid);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% Oxygens %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% load_oxy - loads the titrated oxygen samples for all stations
% make_oxy_sumfile - creates a file combining the titrated oxygen samples
% with the sensor profiles at each corresponding fired depth
% fixed to read with new sbe headers
clear oxy
oxy = load_oxy_new(cruiseid);
%    cruiseid
%    station
%    niskin
%    sample_bottle
%    thio_vol
%    oxygen
%    units

oxy_match = make_oxy_sumfile_jay2_ab1209(cruiseid); 

check_bad_oxy_CF 

%compare_oxy - 
compare_oxy(cruiseid)                
% Calculate new oxygen coefficient to calibrate the sensors
[oxy_coeff2]=oxy_cal_new_equation_jay1(cruiseid);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%% Calibrations %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Apply the new conductivity and oxygen coefficients 
%ab1209
apply_calibration1_ab1209(cruiseid,cond_coeff,oxy_coeff1,oxy_coeff2);

% End of Calibration process

% This appears to be the same as apply_calibration_ab1104 perhaps without
% all the plots.
%Use when you need to modify for multiple sensors
make_cal_sumfile_new_equation_ab1209(cruiseid,cond_coeff,oxy_coeff1,oxy_coeff2)

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

eval_calibration_oxy(cruiseid);

%eval_calibration_jay(cruiseid,oxy_coeff);

% for loading and calibrating the 1hz data
cal_1hz_cnv

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Find out what the hell this does
write_aoml3 

% For WOCE format
save_final_results
make_woce_bottle_file(cruiseid,0,0,0,oxy_coeff)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% For Reports %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
srf_dck_prs_report
ctd_sum_latex
plot_btl_dph_wbts

