% This file plots results from the model simulations

function bla = plot_this(Z,tdlist,H)

fields = fieldnames(H);
for i = 1:length(fields)
    cur_field = fields{i};
    eval([cur_field ' = H.(cur_field);']);
end

fields = fieldnames(Z);
for i = 1:length(fields)
    cur_field = fields{i};
    eval([cur_field ' = Z.(cur_field);']);
end

% make plots
n_fields = length(fields_to_plot);
n_lines = length(tdlist);

if exist('yaxis_percent','var')==false
    yaxis_percent = zeros(1,n_fields);
end

figure();i=1;m=ceil(sqrt(n_fields));n=ceil(n_fields/m);title(plot_name);
for i = 1:n_fields
    cur_field = fields_to_plot{i};
    
    % use first as normalization?
    shift = 0;offset = 0;
    if first_is_norm(i) == 1 && n_lines > 1
        shift = 1;
        offset = 1;
    elseif first_is_norm(i) == 2  && n_lines > 1
        shift = 1;
    end
    
    data = cell(1,(n_lines-shift));
    minn = Inf;
    maxx = -Inf;
    for k = 1:(n_lines-shift)
        var = tdlist{k+shift}.(cur_field)';
        tt  = tdlist{k+shift}.t';
        if offset == 1
            var_offset = interpolate_matt(tdlist{1}.t',tdlist{1}.(cur_field)',tt,true);
            var = var - var_offset;
        end
        
        % percent?
        if yaxis_percent(i)==1
            var = var*100;
        end
        
        subplot(m,n,i);
        hold on
        title(field_labels{i});
        plot(tt,var);
        data{k} = [tt,var];
        minn = min(minn,min(var(tt<max_T)));
        maxx = max(maxx,max(var(tt<max_T)));
    end
    
    % cut down x axis
    axis([0 max_T ylim])
    
    % get stepsize from plot?
    cur_ytick = get(gca,'YTick');
    if stepsize(i) == 0
        stepsize(i) = cur_ytick(2) - cur_ytick(1);
    end
    
    % write tikz
    headspace = true;
    if exist('yticks_list','var') == false
        yticks  = create_ticks(minn,maxx,stepsize(i),headspace);
    else
        yticks = yticks_list{i};
    end
    if exist('xticks','var') == false
        xticks = num2cell([0:2:max_T]);
    end
    if exist('yaxis','var') == false
        cur_yaxis = {{yticks{1}-stepsize(i)/4,yticks{end}+stepsize(i)/4}};
    else
        cur_yaxis = yaxis{i};
    end
    if exist('scale','var') == false
        scale = 1;
    end
    O = struct(...
        'data', {data}, ...
        'scale',scale,...
        'plot_name', [plot_name '_' cur_field], ...
        'xaxis',{{0,max_T}},...
        'title',field_labels{i},...
        'colors',{{colorlist{1+shift:end}}},...
        'styles',{{stylelist{1+shift:end}}},...
        'globalstyle',globalstyle,...
        'legend',{{legend_{1+shift:end}}},...
        'legend_cols',5,...
        'xlabel',x_label,...
        'width',width,...
        'height',height,...
        'ytick',{yticks},...
        'xtick',{xticks},...
        'yaxis',cur_yaxis,...
        'yaxis_percent',yaxis_percent(i),...
        'compile_pdf',compile_pdf,...
        'compile_pdf_folder',compile_pdf_folder,...
        'compile_pdf_paperpath',root_path,...
        'compile_pdf_tikzfolder',compile_pdf_tikzfolder, ...
        'pdflatexpath', pdflatexpath ...
        );
    if write_tikz == 1
        write_tikz_file(O,path_tikz);
    end
end

bla = 0;

end