%% COLLECT DATA THAT SERVE AS INPUT TO THE BAYESIAN NETWORK
% Run-script to obtain data out of the Netcdf and store it in case style
% (every row in the output file is an unique case/scenario).

% In this script the following functions are called:
% csb_getNourishVol      => extract nourishvolumes for three types out of NetCdf
% csb_createDataMatrix   => main function makes matrix to print
% csb_getIndicator       => function in makeM, extracts indicator data out of
%                           Netcdf
% netica_write_case_file => function to write matrix in an ASCII file (cas)

clear;close all;clc;

%% input settings
OPT = struct(...
    'year',             1965:2010,...
    'id',               'Noord-Holland',... % enter transect id [7000000:7999999] or name kustvak ('Noord-Holland')
    'timewindow',       [1 4 9],...
    'rounding',         [1 1 0.01],...
    'basedir',          'NETDATA');
% add indicator externally to prevent lot of structures
OPT.indicator = {'mkl','MDV','p_f'};

%% make directory to store output (make new dir to prevent overwriting)
basedir     = OPT.basedir;
runfolders  = dir(fullfile(basedir, 'datadir*'));
runfolder   = fullfile(basedir, sprintf('datadir%03i', length(runfolders)));
mkdir(runfolder)
casname     = strcat(sprintf('TESTCASE%03i', length(runfolders)),'.cas');

%% get nourisment volumes out of netcdf
[nourishment, RWS_info] = csb_getNourishVol(...
    'year', OPT.year,...
    'id', OPT.id);

%% store nourishments and coastal indicator in matrix
[M,colNames] = csb_createDataMatrix(...
    'nourishment', nourishment,...
    'indicator', OPT.indicator,...
    'timewindow', OPT.timewindow,...
    'rounding', OPT.rounding);

% make a cell array out of the columns of interest of the matrix with ALL
% information. Make sure the names corresponds to the columns.
Mnew = num2cell(M, 1);
varnames = colNames;
inputargs = [varnames; Mnew]; % input arguments you want to write to .cas


%% write inputargs to .cas file
cd(runfolder)

netica_write_case_file(casname,inputargs{:}); % output is .cas file

%% save m-file and nodenames 
save('Data','M');
save('Varnames','varnames');
