
mfilename = 'merged_026_g.asc'
cast = 'filt'
mdata = textread(mfilename);

col_pres = 1
col_a = 3:11
col_c = 12:20
col_bbblu = 23
col_bbred = 24
col_chl = 25
col_tempc = 26
col_salin = 27

lambda = [412 440 488 510 532 555 650 676 715];

% Apply ac-9 diw blank (using means of diw cals taken by students in lab)
a_221_diw = [-0.0621	-0.0335	-0.0110	-0.0067	-0.0027	-0.0026	0.0043	0.0100	0.0115];
c_221_diw = [0.0680	0.0598	0.0358	0.0299	0.0255	0.0221	0.0145	0.0027	0.0104];

mdata(:,col_a) = mdata(:,col_a) - repmat(a_221_diw, size(mdata(:,col_a),1),1);
mdata(:,col_c) = mdata(:,col_c) - repmat(c_221_diw, size(mdata(:,col_c),1),1);
	
% Apply TS correction to a and c data
[a_tscorr, c_tscorr] = tscorr(mdata(:,col_tempc)', mdata(:,col_salin)', ...
				mdata(:,col_a)', mdata(:,col_c)', 23.4);
if strcmpi(cast, 'tot')
	% Apply scattering correction to absorption data (spectrally varying)
	[a_scatcorr,reff] = ascatcr3(a_tscorr',c_tscorr',3);
	a_scatcorr = [a_scatcorr zeros(size(a_scatcorr,1),1)];				
	% Move corrected ac-9 data back into merged data matrix				
	mdata(:,col_a) = a_scatcorr;
	mdata(:,col_c) = c_tscorr';
elseif strcmpi(cast, 'filt')
	mdata(:,col_a) = a_tscorr';
	mdata(:,col_c) = c_tscorr';
else
	error('Invalid cast type')
end

% Apply calibration to bb2f blue and red backscatter
% Dark readings (470 nm, 700 nm)		52			99
% Scale factor for beta (470, 700)		2.20E-05	2.95E-06
% Chi value of 1.1
mdata(:,col_bbblu) = 2*pi*1.1*(mdata(:,col_bbblu)-52).*2.20E-05;
mdata(:,col_bbred) = 2*pi*1.1*(mdata(:,col_bbred)-99).*2.95E-06;


cfilename = strrep(mfilename, 'merged', 'corrected')
dlmwrite(cfilename, mdata, 'delimiter','\t','precision','%g')



