function meanvel(ADCPData,D,A)
%MEANVEL
%
%meanvel collects all ADCP files and calculates the mean velocities and
%mean directions 
%
% See also: make_a_3d_surfaceplot, Siltprofiler_new_main,
% ADCP_readAquaVisionTekal, slinger

FIG=figure;
OPT.export      = 0;
OPT.singlegraph = 0;
OPT.surfplot    = 0;

time       =[];
A1         =[];
A2         =[];
aaa        =[];
Velo       =[];
Depth      =[];
Direction  =[];


%% Collect Data
for gg=37:81
 
    time   = [ADCPData{gg}.time]';
    A1     = find(time > D(gg).datenum(1));
    A2     = find(time > D(gg).datenum(end)); 
    
    if isempty(A1) == 1
        A1=1;
        j=num2str(gg);
        disp(['WARNING!!!! ADCPfile does not start at the same time as the veritcal profile. Applies to file:', j])
        clear j
    end
    if isempty(A2) == 1
        A2=length(time);
        disp(['WARNING!!!! end time ADCPfile is not equal to GPSfile'])
    end
    
    for kk = A1(1):A2(1)
        vel(gg,kk)= cellfun(@(x) x(kk),{ADCPData{gg}});
    end
    

    for ll=A1(1):A2(1)
        aaa(gg,ll)=[length(vel(gg,ll).curVels)];
    end
        
    a3 = [aaa(gg,[A1(1):A2(1)])];%aaa(aaa~=0);
    minValue(gg)=min(a3);
    disp(minValue(gg))

    
    for jj=A1(1):A2(1)
        for nn=1:minValue(gg)%length(vel(1).depths)
        vel2(gg,jj,nn)= vel(gg,jj).curVels(1,nn);
        depth2(gg,jj,nn)=vel(gg,jj).depths(1,nn);
        direction2(gg,jj,nn)=vel(gg,jj).dirs(1,nn);
        DirRad(gg,jj,nn)    = deg2rad(direction2(gg,jj,nn));
        Dirsin(gg,jj,nn)    = sin(DirRad(gg,jj,nn));
        end
    end 
%% determine mean values
for mm=1:length(vel2(gg,1,:))
    Velo(gg,mm)     = mean(vel2(gg,[A1(1):A2(1)],mm));
    Depth(gg,mm)    = mean(depth2(gg,[A1(1):A2(1)],mm)).*-1;
end
    for ff=1:length(vel2(gg,1,:))
       DD1(gg,ff) = mean(Dirsin(gg,[A1(1):A2(1)],ff));
       DD2(gg,ff)   = asin(DD1(gg,ff));
       BacktoDeg(gg,ff) = rad2deg(DD2(gg,ff));
       Direction(gg,ff) = BacktoDeg(gg,ff);
    end

    nan_locations            = find(Velo == 0);
    Velo(nan_locations)      = NaN;  
    Direction(nan_locations) = NaN;
    Depth(nan_locations)     = NaN;
%% plot single graph        
if OPT.singlegraph

[AX,H1,H2]=plotXX(Velo(gg,:),Depth(gg,:),Direction(gg,:),Depth(gg,:));
axis(AX(1),[0 1.2 -20 0]);
axis(AX(2),[-180 180 -20 0]);
set(get(AX(1),'Xlabel'),'String','Velocitiy [m/s]') 
set(get(AX(2),'Xlabel'),'String','Direction [degree]')
name = filename(A(gg).name);
title([name '_adcp'],'Interpreter','none')
ylabel('Depth [m]')


if OPT.export
   print2screensize( [filename(A(gg).name),'_ADCPData.png'])
end
close(FIG)
end
clear name a3 A1 A2 
end
%% surfplot
if OPT.surfplot
   make_a_3d_surfplot(A,D,Depth,Velo,Direction)
end

