function meandpth() % autogenerated function wrapper
    %  meandpth  finds the average depth for a predefined running
    %  window in terms of number of events, and a selected step
    %  and plots the results.
    %                                                     R.Z. 6/94
    %                          Operates on ZG.newcat
    %
    % turned into function by Celso G Reyes 2017
    
    ZG=ZmapGlobal.Data; % used by get_zmap_globals
    report_this_filefun(mfilename('fullpath'));
    
    
    if ic == 1 || ic == 0
        if isempty(ZG.newcat) ZG.newcat = ZG.primeCatalog; end
        ZG.newcat = ZG.primeCatalog;
        iwln = 100;
        step = 28;
        winlen_days = 2;
        
        figure_w_normalized_uicontrolunits(...
            'Name','MeanDepth Input Parameters',...
            'visible','off',...
            'NumberTitle','off', ...
            'NextPlot','new', ...
            'Units','Pixel',  'Position',[ZG.welcome_pos 550 200'])
        axis off
        set(gca,'visible','off');
        
        % creates a dialog box to input some parameters
        %
        
        freq_field=uicontrol('Style','edit',...
            'Position',[.70 .60 .17 .10],...
            'Units','normalized','String',num2str(iwln),...
            'callback',@callbackfun_001);
        
        inp2_field=uicontrol('Style','edit',...
            'Position',[.70 .40 .17 .10],...
            'Units','normalized','String',num2str(step),...
            'callback',@callbackfun_002);
        
        close_button=uicontrol('Style','Pushbutton',...
            'Position', [.60 .05 .15 .15 ],...
            'Units','normalized','callback',@callbackfun_003,'String','Cancel');
        
        go_button=uicontrol('Style','Pushbutton',...
            'Position',[.25 .05 .15 .15 ],...
            'Units','normalized',...
            'callback',@callbackfun_004,...
            'String','Go');
        
        txt1 = text(...
            'Position',[0. 0.65 0 ],...
            'FontSize',ZmapGlobal.Data.fontsz.m ,...
            'FontWeight','bold' ,...
            'String','Number of events in averaging window:');
        
        txt2 = text(...
            'Position',[0. 0.40 0 ],...
            'FontSize',ZmapGlobal.Data.fontsz.m ,...
            'FontWeight','bold' ,...
            'String','Time-step in days:');
        
        set(gcf,'visible','on')
        
    elseif ic == 2
        
        step = days(step);
        if isempty(ZG.newcat) ZG.newcat = ZG.primeCatalog; end
        len = ZG.newcat.Count;
        xt2  = [ ];
        meand = [ ];
        er = [];
        [t0b, teb] = ZG.newcat.DateRange() ;
        ind = 0;
        
        me = [];
        b = ZG.primeCatalog;
        
        sta = ZG.primeCatalog.Date(iwln+1);
        sto = max(ZG.primeCatalog.Date);
        for it=sta+step:step:sto
            ind = ind + 1;
            t = find( abs(b.Date-it)  == min(abs(b.Date-it)));
            if t <= iwln ; t = iwln+1; end
            meand(ind) =  mean(ZG.newcat(t-iwln:t,7)) ;
            er(ind) =  std(ZG.newcat(t-iwln:t,7)) ;
            xt2(ind) = it;        % time is end of window
        end    % for it
        
        meand = -meand;
        
        % Find out if figure already exists
        %
        depfg=findobj('Type','Figure','-and','Name','Mean Depth');
        
        
        % Set up the Seismicity Map window Enviroment
        %
        if isempty(depfg)
            
            depfg=figure_w_normalized_uicontrolunits(...
                'Name','Mean Depth',...
                'visible','off',...
                'NumberTitle','off', ...
                'NextPlot','new', ...
                'Units','Pixel',  'Position',[ZG.welcome_pos 550 400']);
            hold on
            axis off
            
            
            
            uicontrol('Style','Pushbutton',...
                'Position',[.9 .80 .10 .05],...
                'Units','normalized',...
                'callback',@(~,~)medispas1('ast'),'String','AS');
            uicontrol('Style','Pushbutton',...
                'Position',[.9 .70 .10 .05],...
                'Units','normalized',...
                'callback',@(~,~)medispas1('lta'),'String','LTA');
            uicontrol('Style','Pushbutton',...
                'Position',[.9 .90 .10 .05],...
                'Units','normalized',...
                'callback',@callbackfun_007,'String','Com');
            
            new = uicontrol('style','edit','value',winlen_days,...
                'string',num2str(winlen_days), 'background','y',...
                'callback',@callbackfun_008,...
                'units','norm','pos',[.90 .30 .10 .06],'min',0.1,'max',100);
            
            newlabel = uicontrol('style','text','units','norm','pos',[.85 .30 .05 .06]);
            set(newlabel,'string','winlen_days:','background',color_fbg);
            
            uicontrol('Units','normal',...
                'Position',[.90 .25 .08 .06],'String','Go',...
                'callback',@callbackfun_009)
            
        end  % if figure exist
        
        figure(depfg);
        delete(findobj(depfg,'Type','axes'));
        set(gca,'visible','off');
        
        %orient tall
        set(gcf,'Units','centimeter','PaperPosition',[1 1 7 8])
        rect = [0.15, 0.15, 0.65, 0.30];
        axes('position',rect)
        p5 = gca;
        
        % plot errbar
        errorbar(xt2,meand,er)
        hold on
        plot(xt2,meand,'co')
        pl = plot(xt2,meand,'-r')
        hold on
        set(pl,'LineWidth',1.0)
        if ~isempty(ZG.maepi)
            pl =   plot(ZG.maepi.Date,ZG.maepi.Date*0+mean(meand),'xm');
            set(pl,'LineWidth',2.0)
        end
        
        axis([min(ZG.newcat.Date) max(ZG.newcat.Date+0.1) min(meand*1.1)  max(meand*0.9)])
        v = axis;
        xlabel('Time (years)','FontWeight','bold','FontSize',ZmapGlobal.Data.fontsz.m,'Color','k')
        ylabel('Mean Depth (km)','FontWeight','bold','FontSize',ZmapGlobal.Data.fontsz.m,'Color','k')
        stri = ['Mean Depths and standard deviation ( ' file1 ')'];
        %title(' Mean depths and mean depth error ',...
        %        'FontWeight','bold','FontSize',ZmapGlobal.Data.fontsz.m,'Color','k')
        set(gca,'box','on',...
            'SortMethod','childorder','TickDir','out','FontWeight',...
            'bold','FontSize',ZmapGlobal.Data.fontsz.m,'Linewidth',1.2)
        
        grid
        hold off
        
        rect = [0.15,  0.45, 0.65, 0.30];
        axes('position',rect)
        pl =plot(ZG.newcat.Date,-ZG.newcat.Depth,'.b')
        set(pl,'MarkerSize',3')
        set(pl,'LineWidth',1.0)
        hold on
        if ~isempty(ZG.maepi)
            pl =  plot(ZG.maepi.Date,-ZG.maepi.Depth,'xm');
            set(pl,'LineWidth',2.0)
        end
        axis([ v(1) v(2) -max(ZG.newcat.Depth)  -min(ZG.newcat.Depth)])
        ylabel('Depth (km)','FontWeight','bold','FontSize',ZmapGlobal.Data.fontsz.m,'Color','k')
        set(gca,'XTicklabels',[])
        stro = [' ' file1 '; wl = ' num2str(iwln) ' events, inc = ' num2str(step)];
        title(stro,'FontWeight','bold','FontSize',ZmapGlobal.Data.fontsz.m,'Color','k')
        grid
        
        set(gca,'box','on',...
            'SortMethod','childorder','TickDir','out','FontWeight',...
            'bold','FontSize',ZmapGlobal.Data.fontsz.m,'Linewidth',1.2)
        
        set(gca,'visible','on');
        set(gcf,'visible','on');
        
        
        ic = 1;
        
        
    end    % if ic
    
    
    function callbackfun_001(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        iwln=str2double(freq_field.String);
        freq_field.String=num2str(iwln);
    end
    
    function callbackfun_002(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        step=str2double(inp2_field.String);
        inp2_field.String=num2str(step);
    end
    
    function callbackfun_003(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        close;
        
    end
    
    function callbackfun_004(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        ic = 2;
        close;
        clear meandpth;
        meandpth;
    end
    
    function callbackfun_007(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        dispma4();
    end
    
    function callbackfun_008(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        update_editfield_value(mysrc,myevt);
        winlen_days=mysrc.Value;
        medispas1();
    end
    
    function callbackfun_009(mysrc,myevt)

        callback_tracker(mysrc,myevt,mfilename('fullpath'));
        medispas1();
    end
    
end
