% This is an example matlab program that will read in flux test data and use the 
% Bourassa 2006 MFT12 flux code.  The files you need in your working directory are
%   mexmft12.c
%   mft12.c
%   mft12.h
%   testdata12.dat or testdata12.csv
% These files are available for download at http://coaps.fsu.edu/~bourassa/MFT_html/MatLab_users.php

% To link the c files as matlab executables, use the mex command
	mex mexmft12.c  
% Note, this command only needs to be used the first time the code is run, OR
% anytime mexmft12.c file is edited

% If using testdata12.csv, change the following read_code variable to 1
% If using testdata12.dat, change read_code variable to 2
	read_code = 1;

% For the CSV file type
if (read_code == 1)
    
	% Import the data into matlab
	td = csvread('testdata12.csv',1);
    
    % Length of input data array
    len = size(td,1);
    
    % Extra input parameters
    conv_crit(1:len,1)      = 0.00005; 
    sst_prm(1:len,1)        = 1;  
    z_wanted(1:len,1)       = 10; 
    eqv_neut_prm(1:len,1)   = 0;
    Qnet(1:len,1)           = 300;
    warn(1:len,1)           = 0;
    flux_model(1:len,1)     = 9;
    z0_mom_prm(1:len,1)     = 0;
    z0_TQ_prm(1:len,1)      = 0;
    stable_prm(1:len,1)     = 0;
    
    % Concatenate all input parameters into one array
    testdata12 = [ td(:,1:3), td(:,16), conv_crit, td(:,14), td(:,6:9), td(:,15),...
        td(:,4:5), td(:,11), sst_prm, td(:,10), td(:,12:13), z_wanted, td(:,17),...
        eqv_neut_prm, Qnet, warn, flux_model, z0_mom_prm, z0_TQ_prm, stable_prm]; 
    
    clear conv_crit sst_prm z_wanted eqv_neut_prm Qnet warn flux_model z0_mom_prm z0_TQ_prm stable_prm len td

	% For each line of input data, run the flux code
	% The output data is located in the outputarray array.  Validation data of 
	% the output array is located at the bottom of testdata12.dat
	for i = 1:size(testdata12,1)
		data = (testdata12(i,:));
		outputarray(i,:) = mexmft12(data);
	end
	
end

% For the DAT file type
if (read_code == 2)
	clear all;

	% Import the data into matlab, while eliminating the header
	td = textread('testdata12.dat','',62,'headerlines',1);
	% Remove the first column of data (line number)
	td(:,1) = [];
    
    % Length of input data array
    len = size(td,1);
    
    % Extra input parameters
    conv_crit(1:len,1)      = 0.00005; 
    sst_prm(1:len,1)        = 1;  
    z_wanted(1:len,1)       = 10; 
    eqv_neut_prm(1:len,1)   = 0;
    Qnet(1:len,1)           = 300;
    warn(1:len,1)           = 0;
    flux_model(1:len,1)     = 9;
    z0_mom_prm(1:len,1)     = 0;
    z0_TQ_prm(1:len,1)      = 0;
    stable_prm(1:len,1)     = 0;
    
    % Concatenate all input parameters into one array
    testdata12 = [ td(:,1:3), td(:,16), conv_crit, td(:,14), td(:,6:9), td(:,15),...
        td(:,4:5), td(:,11), sst_prm, td(:,10), td(:,12:13), z_wanted, td(:,17),...
        eqv_neut_prm, Qnet, warn, flux_model, z0_mom_prm, z0_TQ_prm, stable_prm]; 
    
    clear conv_crit sst_prm z_wanted eqv_neut_prm Qnet warn flux_model z0_mom_prm z0_TQ_prm stable_prm len td

	% For each line of input data, run the flux code
	% The output data is located in the outputarray array.  Validation data of 
	% the output array is located at the bottom of testdata12.dat	
	for i = 1:size(testdata12,1)
	data = (testdata12(i,:)); 
	outputarray(i,:) = mexmft12(data); 
	end

end

clear data i read_code