%BAOPLT010_loop_func
% Plot data from BAO data loggers
% Systems turned back on April 2007
% Not calibrated old sensors
% 10m
%ID,YR,YearDay,hhmm,T,RH,WS,WSvec,DIR,Dsig,VDC,P,Precip,Corrected Dir
%10,2007,099,1413,7.35,87.654,.305,.305,292.84,2.03,13.981,826.61,0,111.234

function [retval] = SmtPLTsodar_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 = 'Y:\arctic\greenland\archive\sodar\raw\';
    outpath  = 'c:\tmp\';
else
    inpath = '/archive/BAO/Tower/Sodar/Raw/';
    outpath  = '/archive/BAO/Tower/Sodar/Processed_Images/';
end

%cd(inpath);

if( nargin == 0 )
    bys = input('Input the beginning year to process (2010) ','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 (2010) ','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.gz', inpath, dv(1)-2000, yday );
    %fprintf(1,'*** Processing %d-%02d-%02d %s***\n',...
    %    dv(1),dv(2),dv(3),fname);
    
    %fid = fopen( fname );
    %if( fid > 0 )
    %    fclose( fid );
    %else
    %    fprintf(1,'No sodar file: %s\n',fname);
    %    continue;
    %end
    
    %dogunzip = 0;
    %if dogunzip == 1
    %    tic;
    %    gunzip(fname,'C:\tmp\')
    %    toc;
    %end
    fname = sprintf('%s%02d%03d_SodarPC.nc', 'C:\tmp\', dv(1)-2000, yday );
    ncid = netcdf.open(fname,'NC_NOWRITE');
    [tname,tlen]=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);
    %system('del c:\tmp\*.nc');
    dt=datenum(1970,1,1,0,0,time);
    nhts=512;
    %hts=1.:1.:nhts;
    hts=0.3125:0.3125:160.;
    
    deltahr=.5;
    bh=0;
    eh=bh+deltahr;
    nplots = 24 / (4 * deltahr);
    if pltflg % do plots if pltflg=1
        for ip=1:nplots
            figure1 = figure('Position',[100 50 650 650]);
            bhp = bh;
            ehp = bhp + deltahr * 4;
            stitle=sprintf('Summit, Greenland %4d-%02d-%02d %02d-%02d Sodar backscatter',...
                dv(1),dv(2),dv(3),bhp,ehp);
            if ip == 1
                cms=256;
                colormap(gray(cms))
                cm=colormap();
                cm1=cm;
                cm(cms:-1:1,:)=cm1(1:cms,:);
            end
            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)
                    dta=dt(dtindx);
                    refa = ref(:,dtindx);
                    zindx=refa < 10.0;
                    refa(zindx) = 10.0;
                    refa=log10(refa(1:nhts,:));
                    dm2i=refa > 5.;
                    refa(dm2i)= 4.5;
                    dmax=max(max(max(refa)),4.5);
                    dmin=dmax-3.0;
                    %dmed=min(median(refa(300:400,:)));
                    %dmin=log10(dmed);
                    %dmin=min(min(refa));
                    %if dmin < 1.0
                    %    dmin = 1.0;
                    %else
                    %    dmin = 1.7845;
                    %end
                else
                    refa=ones(nhts,360);
                    deltat=60.0/86400.;
                    dta=bt:deltat:et-deltat;
                    dmax=4.4;
                    dmin=1.4;
                end
                subplot(4,1,i),imagesc(dta,hts,refa,[dmin dmax]);
                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('%sSMT_%4d%03d_%02d-%02d_Sodar.png',...
                    outpath, dv(1), yday, bhp, ehp );
                set(gcf,'PaperPositionMode','auto');
                print('-dpng', print_buffer);
            end
            
            close all
            
        end %for number of plots
        
    end %if pltflg
    
end % loop through all files

clear all

retval = 0;

