%rdSAMOS_nc
% Plot SAMOS data captured off SAMOS web site

% D Wolfe 8/11/2015
clear
clf
close all

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


    inpath = 'c:/Data/Notre-Dame/data/netcdf/';
    outpath  = 'c:/Data/Notre-Dame/images';

% Change for date/time you want to process
   by = 2015;
    bm = 8;
    bd = 6;
    ey = 2015;
    em = 8;
    ed = 6;


if 0  %( 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]);
j = 1;
for j = 1:1  %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 );
    fname = 'c:/Data/Notre-Dame/data/netcdf/WDC9417_20150807v10001.nc'
%     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 SAMOS file: %s\n',fname);
        continue;
    end
    
    ncid = netcdf.open(fname,'NC_NOWRITE');
    % From Jeremy
%     nc = netcdf(fname, 'nowrite');                        % Open NetCDF file.
%      description = ncid.description(:);                      % Global attribute.
%      variables = var(ncid);    % Get variable data.

    [tname,tlen]=netcdf.inqDim(ncid,3);
    tid = netcdf.inqVarID(ncid,'time_of_day');
    Ta = netcdf.inqVarID(ncid,'T');
    Ts = netcdf.inqVarID(ncid,'TS');
    dat = netcdf.inqVarID(ncid,'date');
    time = netcdf.getVar(ncid,tid,'double'); %hhmmss
    airt = netcdf.getVar(ncid,Ta,'double');
    sst = netcdf.getVar(ncid,Ts,'double');
    DT = netcdf.getVar(ncid,dat,'double'); %YYYYMMDD
    
    ln = netcdf.inqVarID(ncid,'lon');
    lt = netcdf.inqVarID(ncid,'lat');
    lon = netcdf.getVar(ncid,ln,'double');
    lat = netcdf.getVar(ncid,lt,'double');
    netcdf.close(ncid);
    dt=datenum(1970,1,1,0,0,time);
    
    
    figure
    plot(airt,'r')
    hold
    plot(sst,'b')
    title('Atlantic Explorer');
    ylabel('Temp (C)');
    legend('Air T','SST','Location','Best');
    
    figure
    plot(lon-360.,lat)
    xlabel('Longitude')
    ylabel('Latitude');
    
    
if 0    % skip plotting
    hts=1.:1.:500;
    
    deltahr=6;  % changed to do 1 24 hr plot (4 panels just like data browser D Wolfe
    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('BAO %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)
                    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]);
                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_%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;
end  % skip plotting

