figpath = fullfile(fileparts(which(mfilename)), '..', 'figures');
close all
D = dir2(figpath,...
    'no_dirs', true,...
    'file_incl', 'desb_xbeach_artificial_jarkus_plot_profiles_Hrms.*.fig$');

suffix = regexp({D.name}, '(?<=_Hrms).*(?=.fig)', 'match');
profilepart = cellfun(@(str) str{1}, regexp({D.name}, '(?<=ms_)\D+ward', 'match'),...
    'uniformoutput', false);
ylim_landward = [0 2];
ylim_seaward = [0 2];
for i = 1:length(D)
    open(fullfile(figpath, D(i).name))
    if strcmp(profilepart{i}, 'Landward')
        ylim = ylim_landward;
    else
        ylim = ylim_seaward;
    end
    fh = gcf;
    ph = findall(gcf,...
        'type', 'line',...
        '-not', 'displayname', '');
    ah = get(ph(1), 'parent');
    set(ah,...
        'xticklabel', -get(ah, 'xtick'))
    
    [~, fname] = fileparts(D(i).name);

    print(gcf, '-depsc2', fullfile(figpath, [fname '.eps']))
    if ispc
        saveas(gcf, fullfile(figpath, [fname '.emf']))
    end
    saveas(fh, fullfile(figpath, [fname '.fig']))
    
    profileid = regexp(D(i).name, '\d+', 'match');
    refid = ~cellfun(@isempty, regexp(get(ph, {'displayname'}), profileid{1}));
    lfid = ~cellfun(@isempty, regexp(get(ph, {'displayname'}), 'Hrms_lf'));
    hfid = ~cellfun(@isempty, regexp(get(ph, {'displayname'}), 'Hrms_hf'));
    
    x = cell2mat(get(ph, 'xdata'));
    y = cell2mat(get(ph, 'ydata'));
    
    Hrms_lf = y(lfid,:);
    Hrms_lf_baseline = y(lfid&refid,:);
    Hrms_lf_rel = Hrms_lf ./ repmat(Hrms_lf_baseline, sum(lfid), 1);

    figure;
    phrlf = plot(x(lfid,:)', Hrms_lf_rel');
    set(phrlf,...
        {'color'}, get(ph(lfid), 'color'))
    xlabel('Cross-shore coordinate [m]')
    ylabel('Relative Hrms lf')
    figureStyle(@styleThesis,...
        {'Figure' 'Position' get(fh, 'position') - [0 0 0 2]},...
        {'Axes' 'Position' get(ah, 'position') .* [1 1.5 1 .95] 'xlim' get(ah, 'xlim') 'xdir' get(ah, 'xdir') 'ylim' ylim 'xticklabel' -get(ah, 'xtick')});
    
    fname = [mfilename '_lf' suffix{i}{1}];
    set(gcf, 'tag', '$HeadURL$ $Revision$')
    print(gcf, '-depsc2', fullfile(figpath, [fname '.eps']))
    if ispc
        saveas(gcf, fullfile(figpath, [fname '.emf']))
    end
    saveas(gcf, fullfile(figpath, [fname '.fig']))
    
    Hrms_hf = y(hfid,:);
    Hrms_hf_baseline = y(hfid&refid,:);
    Hrms_hf_rel = Hrms_hf ./ repmat(Hrms_hf_baseline, sum(hfid), 1);
    
    figure;
    phrhf = plot(x(hfid,:)', Hrms_hf_rel');
    set(phrhf,...
        {'color'}, get(ph(hfid), 'color'))
    xlabel('Cross-shore coordinate [m]')
    ylabel('Relative Hrms hf')
    figureStyle(@styleThesis,...
        {'Figure' 'Position' get(fh, 'position') - [0 0 0 2]},...
        {'Axes' 'Position' get(ah, 'position') .* [1 1.5 1 .95] 'xlim' get(ah, 'xlim') 'xdir' get(ah, 'xdir') 'ylim' ylim 'xticklabel' -get(ah, 'xtick')});

    fname = [mfilename '_hf' suffix{i}{1}];
    set(gcf, 'tag', '$HeadURL$ $Revision$')
    print(gcf, '-depsc2', fullfile(figpath, [fname '.eps']))
    if ispc
        saveas(gcf, fullfile(figpath, [fname '.emf']))
    end
    saveas(gcf, fullfile(figpath, [fname '.fig']))
end