%BAOPLTsodar_loop_func
% Plot data from BAO sodar

function [retval] = BAOPLTsodar_loop_func( by, bm, bd, ey, em, ed )

pltflg = 1; %flag to plot the data: 1=yes, 0=no
pngflg = 1; %flag to make a png of the plot: 1=yes, 0=no

if ispc
    inpath = 'z:\Tower\Sodar\Raw\';
    outpath  = 'z:\Tower\Sodar\Imdatenumages\';
else
    inpath = '/psd3data/bao/Tower/Sodar/Raw/';
    outpath  = '/psd3data/bao/Tower/Sodar/Images/';
    %outpath  = '/psd3data/bao/Tower/Sodar/Images/';
end

%cd(inpath);

if( nargin == 0 )
    bys = input('Input the beginning year to process (2007) ','s');
    bms = input('Input the beginning the month to process (1-12) ','s');
    bds = input('Input the beginning day to process (1-31) ','s');
    eys = input('Input the endding year to process (2007) ','s');
    ems = input('Input the endding the month to process (1-12) ','s');
    eds = input('Input the endding day to process (1-31) ','s');
    by = str2double( bys );
    bm = str2double( bms );
    bd = str2double( bds );
    ey = str2double( eys );
    em = str2double( ems );
    ed = str2double( eds );
end

bdn = datenum([by bm bd 0 0 0]);
edn = datenum([ey em ed 0 0 0]);

for idn = bdn:edn
    dv=datevec(idn);
    yday = md2yd( dv(1), dv(2), dv(3) );
    fname = sprintf('%s%02d%03d_sodarpc.nc', inpath, dv(1)-2000, yday );
    fprintf(1,'*** Processing Sodar daily %d-%02d-%02d yd %03d***\n',...
        dv(1),dv(2),dv(3),yday);
    
    fid = fopen( fname );
    if( fid > 0 )
        fclose( fid );
    else
        fprintf(1,'No sodar file: %s\n',fname);
        continue;
    end
    
    ncid = netcdf.open(fname,'NC_NOWRITE');
    netcdf.inqDim(ncid,3);
    tid = netcdf.inqVarID(ncid,'Time');
    rid = netcdf.inqVarID(ncid,'ReflectivityData');
    time = netcdf.getVar(ncid,tid,'double');
    ref = netcdf.getVar(ncid,rid,'double');
    netcdf.close(ncid);
    dt=datenum(1970,1,1,0,0,time);
    hts=1.:1.:500;
    
    if pltflg % do plots if pltflg=1
        deltahr=6.;
        bh=0;
        eh=bh+deltahr;
        nplots = 24 / (4 * deltahr);
        for ip=1:nplots
            figure1 = figure('Position',[100 50 650 650]);
            colormap(gray)
            cm=colormap();
            cm1=cm;
            cm(64:-1:1,:)=cm1(1:64,:);
            stitle=sprintf('BAO %4d-%02d-%02d Sodar backscatter', dv(1),dv(2),dv(3));
            for i=1:4
                bt=datenum(dv(1),dv(2),dv(3),bh,0,0);
                et=datenum(dv(1),dv(2),dv(3),eh,0,0);
                dtindx=find(dt>=bt & dt<=et);
                if ~isempty(dtindx)
                    dta6=dt(dtindx);
                    refa = ref(:,dtindx);
                    zindx=refa < 10.0;
                    refa(zindx) = 10.0;
                    lra6=log10(refa(1:500,:));
                    dm2i=lra6 > 5.;
                    lra6(dm2i)= 4.0;
                    dmax=max(max(max(lra6)),4.0);
                    dmed=min(median(refa(400:500,:)));
                    dmin=log10(dmed);
                    %dmin=min(min(lra6));
                    %if dmin < 1.0
                    %    dmin = 1.0;
                    %else
                    %    dmin = 1.7845;
                    %end
                else
                    lra6=ones(500,360);
                    deltat=60.0/86400.;
                    dta6=bt:deltat:et-deltat;
                    dmax=4.4;
                    dmin=1.0;
                end
                subplot(4,1,i),imagesc(dta6,hts,lra6,[dmin dmax]);
                whos dta6 hts lra6
                if i == 1
                    title(stitle)
                end
                if i == 4
                    xlabel('Time (UTC)')
                end
                ylabel('Ht (m)')
                xlim([bt et])
                axis xy
                colormap(cm)
                set(gca,'XTick',bt:(et-bt)/6.:et)
                datetick('x','HH:MM','keeplimits','keepticks');
                bh = bh + deltahr;
                eh = eh + deltahr;
            end
            
            annotation(figure1,'textbox',[0.007154 0.01077 0.4498 0.02462],...
                'String',{'NOAA/ESRL/PSD/Weather & Climate Physics'},...
                'FontSize',8,'FitBoxToText','off','LineStyle','none');
            if pngflg
                print_buffer = sprintf('%sBAO_SFC_%4d%03d_Sodar.png',...
                    outpath, dv(1), yday );
                set(gcf,'PaperPositionMode','auto');
                print('-dpng', print_buffer);
            end
            
        end %for number of plots
        
    end %if pltflg
    
    close all
    
end % loop through all files

 clear all

retval = 0;

