function analyse_series(varargin)
%% plot series line 7
% close all; %clear all;

survey_ind = list_surveys

for line = 7%1:22 %1:22

    % for i = 1:length(survey_ind.Survey_pairs)
    for i = 1:5:length(survey_ind.Survey_pairs)
        i


        %     retrieve month and year
        temp_time = nc_varget('vlugtenburg.nc','time',[survey_ind.Survey_pairs(i,1)-1 0],[1 -1]);
        month = str2num(temp_time(1:2));
        year = str2num(temp_time(7:10));
%         [cross_shore,altitude(:,i)] = combine_surveys(year,month,line);
        [cross_shore,altitude(:,i)] = combine_surveys_nr(survey_ind,i,line);

        figure(str2num([num2str(line) '1']))
%         subplot(2,1,1)
        plot(cross_shore,altitude(:,i),...
            'DisplayName', datestr(datenum(num2str([month year]),'mm yyyy'),'mmmm yy'));
        hold all;

%         if i>1
%             figure(str2num([num2str(line) '1']))
%             subplot(2,1,2)
%             plot(cross_shore,altitude(:,i)-altitude(:,1),...
%                 'DisplayName', ['\Delta until ' datestr(datenum(num2str([month year]),'mm yyyy'),'mmmm yy')]);
%             hold all
%         end

        if 0
            figure(99)
            plot(cross_shore,altitude(:,i),...
                'DisplayName', datestr(datenum(num2str([month year]),'mm yyyy'),'mmmm yy'));
            hold all;
        end

    end

end


set(gcf,'PaperOrientation','landscape','PaperPosition',[0.634518 0.634517 28.4084 19.715])

% subplot(2,1,1)
legend show
title(['Elevation along line ' num2str(line)])
xlabel('Cross Shore [m]')
ylabel('Elevation wrt NAP [m]')
xlim([-300 1500])
hline(0)
hline(3,':g')

% subplot(2,1,2)
% legend show
% title(['Cumulative changes along surey line ' num2str(line)])
% xlabel('Cross Shore [m]')
% ylabel('Bed level change [m]')
% xlim([-300 1500])
% plot([-500 2000],[0 0],'--','Color',[0.702 0.7804 1])

if 0
    print('-dpng',['./plot_series/diff' num2str(line) 'a.png'])
end

if 0
    print('-depsc',['./plot_series/diff' num2str(line) 'a.eps'])
end

%% plot aeolian area
figure(str2num([num2str(line) '2']))
set(gcf,'PaperOrientation','landscape','PaperPosition',[0.634518 0.634517 28.4084 19.715])
subplot(2,1,1)
hold all
title(['Difference along profile ' num2str(line)])

plot(cross_shore,altitude(:,9)-altitude(:,1))

plot([-300 1400],[0 0],'k')

xlim([-200 200])
ylim([-1 1])
ylabel('[m]')



subplot(2,1,2)
hold all
title(['Profile ' num2str(line) ' (March)'])
xlabel('[m]')
ylabel('[m]')
plot(cross_shore,altitude(:,9));
plot([-300 1400],[0 0],':r')
xlim([-200 200])
ylim([-1 7])
hold off

% print('-dpng',['./plot_series/diff' num2str(line) 'b.png'])

% close all


%% Mean and Variance
if 0
    % use the altiude which is already loaded
    pro_var =  nanvar(altitude')';
    pro_mean = nanmean(altitude')';

    if 0
        figure;
        plot(cross_shore,pro_var);
        title(['Variance around water line (line nr. ' num2str(line) ')'])
        xlabel('Cross Shore [m]')
        ylabel('Variance [m^2]')
        %     xlim([0 200])
    end

    % determine runup limit
    cross_runup_lim = cross_shore(find(cross_shore > 0 & cross_shore < 200 & pro_var > 0.005 ,1,'first'));
    Z_runup_lim = pro_mean(find(cross_shore == cross_runup_lim))
end


%% analyse volume differences

if 0
    %%% Volume 1 --- from -8 until 0 NAP %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    upper_boundary = 0; %(NAP)
    upper_boundary = Z_runup_lim; %(NAP)
    lower_boundary = -8; %(NAP)
    land_boundary = 0;

    figure(100 + line*10)
    subplot(4,1,2)
    volume1 = plot_zone_bars(cross_shore, altitude,upper_boundary,...
        lower_boundary,land_boundary,[]);
    hold all
    title('-8m NAP until 0m NAP')
    ylim([-75 75])

    %%% Volume 2 --- from 0 NAP until Dune top %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    % For determining dune top
    % determine maximum of the mean within 100m range of the fence
    [value index] = max(pro_mean(cross_shore > -100 & cross_shore < -0));

    land_boundary = cross_shore(cross_shore > -100 & cross_shore < -0 & pro_mean == value);
    lower_boundary = 0;
    lower_boundary = Z_runup_lim;

    figure(100 + line*10)
    subplot(4,1,3)
    volume2 = plot_zone_bars(cross_shore, altitude,[],...
        lower_boundary,land_boundary,[]);
    title('0m NAP until Base Dune Top')
    ylim([-75 75])

    %%% volume 3 from base dune top until dune foot %%%%%%%%%%%%%%%%%%%%%%%%%%%

    % For determining dune top
    % determine maximum within 100m range of the fence
    [value index] = max(pro_mean(cross_shore > -100 & cross_shore < -0));
    % set boundaries
    sea_boundary = cross_shore(cross_shore > -100 & cross_shore < -0 & pro_mean == value);
    land_boundary = cross_shore(find(pro_mean<3,1,'first'));
    lower_boundary = -5;

    figure(100 + line*10)
    subplot(4,1,4)
    volume3 = plot_zone_bars(cross_shore, altitude,[],...
        lower_boundary,land_boundary,sea_boundary);
    title('Base dune top until old DuneFoot')
    ylim([-75 75])

    %%% Volume 0 --- from -8 until Dune Foot %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    lower_boundary = -8; %(NAP)
    land_boundary = cross_shore(find(pro_mean<3,1,'first'));

    figure(100 + line*10)
    subplot(4,1,1)
    volume0 = plot_zone_bars(cross_shore, altitude,[],...
        lower_boundary,land_boundary,[]);
    title('-8 NAP until old DuneFoot (total volume)')
    ylim([-75 75])
end


if 0
    %%% Check volumes
    volume_check = volume1+volume2+volume3;
    figure;
    plot(detrend(volume_check,'constant'))
    hold all
    plot(detrend(volume0,'constant'))
end

% %% Variance plot
%
% % use the altiude which is already loaded
% pro_var =  nanvar(altitude')';
%
% figure;
% plot(cross_shore,pro_var);
%
% % determine runup limit
% cross_runup_lim = cross_shore(find(cross_shore > 0 & cross_shore < 200 & pro_var > 0.005 ,1,'first'));
% Z_runup_lim = pro_mean(find(cross_shore == cross_runup_lim))

%% functions

function Volume = plot_zone_bars(cross_shore, altitude,upper_boundary,lower_boundary, land_boundary,sea_boundary)

if 1
    for i = 1:length(altitude(1,:))
        [Volume(i)] = jarkus_getVolume(cross_shore, altitude(:,i),...
            upper_boundary,lower_boundary,land_boundary,sea_boundary);
    end
    bar(detrend(Volume,'constant'))
end









