%======================================================================
%                    S E T _ C A S T _ P A R A M S . M 
%                    doc: Mon Apr 20 09:03:09 2015
%                    dlm: Thu Mar 16 11:18:22 2017
%                    (c) 2015 A.M. Thurnherr
%                    uE-Info: 52 3 NIL 0 0 72 0 2 4 NIL ofnI
%======================================================================

% P18 Section: [008:026 028:123 126:140 142:172 174:180 182:208 210:217 219 221:227]
%
% Leg 1:
%	001-002	shakedown tests?
%	003		shakedown cast with IMP; no 24-Hz CTD
%	004		shakedown tests?
%	005		shakedown cast with IMP; no 24-Hz CTD
%	006-007	shakedown tests?
%	027 	no CTD data
%	124-125	embargoed (Chilean EEZ)

% Leg 2 (126-end):
%	141		CTD went into water with bad sensor; no cast done
%	173		no cast (sat on deck for 30mins, then restarted)
%	181		LADCP data not downloaded and erased from instrument before next cast
%	209		nothing (no data?)
%	218		number skipped(?)
%	220		likely test number (no data)

% DL INSTRUMENT:
%	001-217		#19394	Frankenhead with new power supply; bad orientation switch
%	218-227		#24544	spanking new WH150 with updated power supply

% SADCP:
%	001-125		Leg1.mat
%	126-138		Leg2a.mat   12/31 23:58 - 01/04 06:26
%	139-148		Leg2b.mat	01/04 06:56 - 01/06 08:15 requires modified mkSADCP.m for pre-processing since data span year-boundary
%	149-150		Leg2c.mat	01/06 08:38 - 01/06 19:36 requires modified mkSADCP.m for pre-processing
%	151-155		Leg2d.mat	01/06 20:03 - 01/08 01:16 requires modified mkSADCP.m for pre-processing
%	156-227		Leg2e.mat	01/08 01:47 - 02/02 15:59 requires std mkSADCP.m for pre-processing

close all;
more off;
figure(100);

%processing_version = 1;					% std QC
%processing_version = 2;				% QC w IMP (only 3, 5; with 1Hz files during cruise)
%processing_version = 3;				% QC DL bins 2-6; only some profiles (most of leg 1)
%processing_version = 4; 				% QC DL only
processing_version = 5;				% all constraints (final version)

if (stn == 124 || stn == 125)
	error('data embargoed')
end

%--------------------------------------------------

ps.sadcpfac = 1;						% defaults

p.btrk_ts       = 50;					% required for BT 010
p.btrk_mode 	= 2;

p.savecdf = 1;

if stn <= 217
	f.ladcpdo = sprintf('../data/raw/%03d/%03dDL000.corrected',stn,stn);	% WH150 with bad orientation switch
else 																		% files created with editPD0 -x ensure_DL
	f.ladcpdo = sprintf('../data/raw/%03d/%03dDL000.000',stn,stn);
end
if stn == 8 || stn == 14							% 8: multiple files; longest alone does not process
	f.ladcpup = '';									% 14: UL file contains dc data only
else
	f.ladcpup = sprintf('../data/raw/%03d/%03dUL000.000',stn,stn);
end

% at least a subset of the following stations can be processed with the 
% default p.getdepth as long as the ODF-provided 1-Hz files are used.

%if stn==13 || stn==21 || stn==62 || stn==131 || stn==150 || stn==161 || stn==162 || stn==164 ...
%		   || stn==168 
% Quite a few stations had to be processed with the following when the LADCP_w_CTD-derived
% time series were used. In the end, the parameter was set consistently for all profiles 
% for simplicity.

p.getdepth = 1;

% the following profiles caused out-of-memory errors on whoosher when processed with 
% default parameters.
if stn==202 || stn==203 || stn==205 || stn==211
	p.avdz = 10;
end

%--------------------------------------------------

if processing_version == 1
	proc_id = 'GPS+BT';
    ps.sadcpfac = 0;
elseif processing_version == 2
	proc_id = 'IMP/GPS+BT';	
    ps.sadcpfac = 0;
	f.ladcpdo = sprintf('../data/IMP/%03dDL.000',stn);
	f.ladcpup = sprintf('../data/IMP/%03dUL.000',stn);
elseif processing_version == 3
	proc_id = 'GPS+BT; DL near bins';
    ps.sadcpfac = 0;
	p.edit_mask_dn_bins = [7:99];
elseif processing_version == 4
	proc_id = 'DL GPS+BT';
    ps.sadcpfac = 0;
	f.ladcpup = '';
elseif processing_version == 5
	proc_id = 'final version';	
	p.savecdf = 1;
else    
    error('Unknown processing version\n');
end

if stn <= 125
	f.sadcp = '../data/SADCP/Leg1.mat';
elseif stn <= 138
	f.sadcp = '../data/SADCP/Leg2a.mat';
elseif stn <= 148
	f.sadcp = '../data/SADCP/Leg2b.mat';
elseif stn <= 150
	f.sadcp = '../data/SADCP/Leg2c.mat';
elseif stn <= 155
	f.sadcp = '../data/SADCP/Leg2d.mat';
else
	f.sadcp = '../data/SADCP/Leg2e.mat';
end

f.nav = sprintf('../data/CTD/%03d.1Hz',stn);
f.nav_header_lines          = 35;
f.nav_fields_per_line       = 12;
f.nav_time_field            = 1;
f.nav_lat_field             = 11;
f.nav_lon_field             = 12;
f.nav_time_base             = 0;

f.ctd = f.nav;
f.ctd_header_lines      = f.nav_header_lines;
f.ctd_fields_per_line	= f.nav_fields_per_line;
f.ctd_pressure_field	= 2;
f.ctd_temperature_field = 3;
f.ctd_salinity_field	= 6;
f.ctd_time_field		= f.nav_time_field;
f.ctd_time_base         = f.nav_time_base;


%--------------------------------------------------

out_dir = sprintf('V%d',processing_version);


LADCP_id 	= ncread('STATIONNUMBERS.nc','LADCP_id');
cruise_leg 	= ncread('STATIONNUMBERS.nc','cruise_leg');
CTD_station = ncread('STATIONNUMBERS.nc','CTD_station');
CTD_cast 	= ncread('STATIONNUMBERS.nc','CTD_cast');
i = find(LADCP_id == stn);
CTD_id = sprintf('%d%03d%d',cruise_leg(i),CTD_station(i),CTD_cast(i));
if ~isfinite(CTD_id)
	error('cannot determine CTD id');
end


%f.res = sprintf('%s/rh%s',out_dir,CTD_id);
f.res = sprintf('%s/%03d',out_dir,stn);
f.checkpoints = sprintf('checkpoints/%03d',stn);

ps.fig3_colormap = 1;			% better for deep casts
ps.fig3_err_y_axis = 1;

p.ladcp_station     = stn;
p.checkpoints       = [1];
p.cruise_id         = ' 2016/17 P18';
p.whoami            = 'A.M. Thurnherr ';
p.name              = sprintf('rh%s (LADCP #%03d; V%d)',CTD_id,p.ladcp_station,processing_version);
p.saveplot          = [1:6 9:11 13:14];
p.saveplot_png      = [];
