function analyze_this(test, run, data_dir, output_dir)

%% coefficients and settings
%addpath ..\..\..\Testbed\tools\matlab\

test_id = [test '_' run];

fid = fopen([data_dir 'datanam.txt']);
Tnam = fgetl(fid);
nvar = str2num(fgetl(fid));
for i = 1:nvar
    line = fgetl(fid);
    [nam,remain] = strtok(line,' ');
    [dummy,remain] = strtok(remain,'=');
    par.(nam) = str2num(remain(2:end));
end
fclose(fid);

%% Simulation results
tic;

XB = getdimensions;

nam = [{'zb'};{'H'}; {'zs'};{'hh'};{'u'}; {'ue'};{'urms'};{'ccg'};{'dzav'};{'ua'};{'As'};{'Sk'};{'Tbore'};{'R'}; {'DR'};{'kb'};{'c'}; {'ceqsg'};{'ceqbg'};{'Susg'}; {'Subg'};   ]; % {'Fx'};
dim = [{'2D'};{'2D'};{'2D'};{'2D'};{'2D'};{'2D'};{'2D'};  {'2D'}; {'2D'};  {'2D'};{'2D'};{'2D'};{'2D'};   {'2D'};{'2D'};{'2D'};{'2D'};{'3Dsed'};{'3Dsed'};{'3Dsed'};{'3Dsed'};  ]; % {'2D'};
    
for j = 1:length(nam)
    temp=readvar([nam{j},'.dat'],XB,dim{j});
    s.(nam{j}) = squeeze(temp(:,2,:));
end

toc;

%% Measurements
m = load([data_dir,'T01data.mat']);

%% Figures
% waves
[s,m] = pwh(s,m,par,XB.nt,XB.nx,XB.ny,XB.x(:,2),XB.y(:,2),output_dir,test_id);

% flows
[s,m] = pu(s,m,par,XB.nt,XB.nx,XB.ny,XB.x(:,2),XB.y(:,2),output_dir,test_id);

% sediment transports
[s,m] = pst(s,m,par,XB.nt,XB.nx,XB.ny,XB.x(:,2),XB.y(:,2),output_dir,test_id);

% profile evolutions
[s,m] = ppe(s,m,par,XB.nt,XB.nx,XB.ny,XB.x(:,2),XB.y(:,2),output_dir,test_id);

%% Quantitative performance statistics
[r2(1),sci(1),relbias(1),bss(1)]        = compskill_tb([XB.x(:,2) s.Hrms_hfm] ,[m.xhrms; m.Hrms_hf]');
[r2(2),sci(2),relbias(2),bss(2)]        = compskill_tb([XB.x(:,2) s.Hrms_lfm] ,[m.xhrms; m.Hrms_lf]');
[r2(3),sci(3),relbias(3),bss(3)]        = compskill_tb([XB.x(:,2) s.rho'    ] ,[m.xhrms; m.rho    ]');
[r2(4),sci(4),relbias(4),bss(4)]        = compskill_tb([XB.x(:,2) s.Skm     ] ,[m.xhrms; m.Sku    ]');
[r2(5),sci(5),relbias(5),bss(5)]        = compskill_tb([XB.x(:,2) s.Asm     ] ,[m.xhrms; m.Asu    ]');
[r2(6),sci(6),relbias(6),bss(6)]        = compskill_tb([XB.x(:,2) s.Beta    ] ,[m.xhrms; m.BetaFW ]');
[r2(7),sci(7),relbias(7),bss(7)]        = compskill_tb([XB.x(:,2) s.B       ] ,[m.xhrms; m.BFW    ]');
[r2(8),sci(8),relbias(8),bss(8)]        = compskill_tb([XB.x(:,2) s.Urms_hfm] ,[m.xurms; m.Urms_hf]');
[r2(9),sci(9),relbias(9),bss(9)]        = compskill_tb([XB.x(:,2) s.Urms_lfm] ,[m.xurms; m.Urms_lf]');
[r2(10),sci(10),relbias(10),bss(10)]    = compskill_tb([XB.x(:,2) s.Um      ] ,[m.xum;   m.Um     ]');
[r2(11),sci(11),relbias(11),bss(11)]    = compskill_tb([XB.x(:,2) s.Cm      ] ,[m.xc;    m.Cm     ]');
[r2(12),sci(12),relbias(12),bss(12)]    = compskill_tb([XB.x(:,2) s.sedero  ] ,[m.x;     m.sedero ]');
[r2(13),sci(13),relbias(13),bss(13)]    = compskill_tb([m.hours2; s.A       ]',[m.hours2;m.A      ]');

% LaTeX text with subscript or superscript should be written between
% $-signs (this is the so-called math-mode, leading to italic text).
% varnames and errnames are 
varnames = {'$H_{rms,hf}$','$H_{rms,lf}$','$\rho$','$S_{k}$','$A_{s}$','$\beta$','B','$U_{rms,hf}$','$U_{rms,lf}$','$U_{m}$','$C_{m}$','sedero','A'};
errnames = {'$R^2$','SCI','Rel. Bias','BSS'};

OPT = {...
    'title', ['error statistics test ',test],...
    'filename', [output_dir,test_id '_tab1.tex'],...
    'where', '!tbp',...
    'rowlabel',varnames,...
    'rowjustification', 'l',...
    'collabel',errnames,...
    'caption', '',...
    'justification', 'center'};

table = [r2; sci; relbias; bss;];

matrix2latex(table',OPT{:});


