% modeling the backscattering vs frequency at different T and P using DWBA
% 
% Created by Dezhang Chu, NWFSC, NOAA Fisheries
% Date 02-24-2017
% Modified by Serdar Sakinan on 3/13/2017 - read data directly from an Excel file
% modified by Dezhang Chu on 3/13/2017 to run the loop more efficiently

clear
response3 =[]
lcf=1
for frk = 333

input_filename = 'G_H_oceanographic_parametrisation.csv';
output_filename = [num2str(frk),'n_atlantic_TS_333_JAN2019.csv'];


%% load measured data
[nordic, var_name] = xlsread(input_filename,1);
table_header = var_name(1,:);
[ntot, nvar] = size(nordic);

output_array_indx = [2:7 11:13 16 nvar+1];
output_var_header = [table_header(1, output_array_indx(1:end-1)) , 'TS'];

% set pextract parameters from the table
T = nordic(:,5);                   % temperature in degC
P = nordic(:,4);                   % Pressure in dbar
cw = nordic(:,8);                  % sound speed of seawater
rho_w = nordic(:,7);               % density of sea waterLmf = 2.5;                    



g_f = nordic(:,13)./nordic(:,7);  
g_lip = nordic(:,12);
h_f=nordic(:,16);
h_lip  = nordic(:,11);

% g_f =g_f*0+ 0.9913;  
% g_lip = g_lip*0+0.9913;
% h_f=h_f*0+1.0467;
% h_lip  = h_lip*0+1.0467;
    
% set more parameters
Lmf = 2.45*lcf;                    % mean length of the copepods in mm  
Lml = 2.34*lcf;                  % mean length of the lipid inside the body in mm (assumed the same as its body flesh length)
Wmf = 0.806*lcf;                  % mean width of the copepods in mm
Wml = 0.44*lcf;              % mean width of the lipid inside the body in mm
thm = 90;                   % mean angle of orientation (0 deg is end-on)
th_std = 30;                % standard deviation of orientation (90 deg is heads up (assume a Gaussian distribution)
nA = 200;                    % number of angle values
Ln_std = 0.1;               % standard deviation of normalized length, dLf/Lmf = dLl/Lml = dL/Lmf
nL = 100;                   % number of length values
nf = 2;                    % number of ferquwncies
fmin = frk;                  % minimum frequency in kHz
fmax = frk;                 % maximum frequency in kHz
freq_indx = 1;

% angle distribution struct
th_p.thm = thm;
th_p.th_std = 30;
% normalized length distribution struct
L_p.L_std = Ln_std;
%  frequency
%  array
freq = linspace(fmin, fmax, nf);

bscat_ave=nan*ones(ntot,nf);

tic
for i = 1:ntot
    if rem(i, 100) == 0
        fprintf('%d of %d \n',i, ntot)
    end

    % differential backscattering cross section as a function of angle of orientation and length
    bscat_ave(i, :) = copepod_DWBA_bscat_analy(freq, Lmf, Lml, Wmf, Wml, th_p, L_p, cw(i), g_f(i), g_lip(i), h_f(i), h_lip(i));
    
    nordic(i, nvar +1) = 20*log10(abs(bscat_ave(i, freq_indx)));
end

output_array_indx = [2:7 11:13 16 nvar+1];

output_data = nordic(:, output_array_indx);
output_var_header = [table_header(1, output_array_indx(1:end-1)) , 'TS'];

%plot(output_data(1:end,11),output_data(1:end,3))
response3=[response3,output_data(1:end,11)];
end
frk = 333 ; %0:100:1200;
xlswrite(output_filename, output_var_header);
xlswrite(output_filename, output_data, 1, 'A2');
toc
