% Wind rose of BAO 10 and 300m data

function [retval] = bao10300_month_rosehist_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:\BAO\Tower\Processed\monthly\';
    outpath  = 'z:\BAO\Tower\Processed_Images\monthly\';
else
    inpath = '/archive/BAO/Tower/Processed/monthly/';
    outpath  = '/archive/BAO/Tower/Processed_Images/monthly/';
end

%cd(inpath);

if( nargin == 0 )
    bys = input('Input year to process (2007) ','s');
    bms = input('Input the month to process (1-12) ','s');
    bds = input('Input the beginning day to process (1-31) ','s');
    eys = input('Input year to process (2007) ','s');
    ems = input('Input the month to process (1-12) ','s');
    eds = input('Input end 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]);
bdv=datevec(bdn);
edv=datevec(edn);
yr = bdv(1);
mo = bdv(2);

nloops = edv(2) + (edv(1) - bdv(1)) * 12 - bdv(2) + 1;

for i = 1:nloops
    name10 = sprintf('%sBAO_010_%4d%02d.dat', inpath, yr, mo );
    name100 = sprintf('%sBAO_100_%4d%02d.dat', inpath, yr, mo );
    name300 = sprintf('%sBAO_300_%4d%02d.dat', inpath, yr, mo );
    fprintf(1,'*** Processing 10m, 100m and 300m monthly ***\n');
    
    %%%%%%% PSD 10m data
    %1    2   3   4     5     6      7      8       9      10        11     12     13    14
    %ID  YR  YD  hhmm  T      RH     WSv   WSs     Dir    WDt       Bat    press  precip WDT
    %10,2007,165,1405,15.095,50.858,1.808,-999.000,238.61,-999.0000,13.515,843.
    
    %%%%300m data
    %"TIMESTAMP","RECORD","SE1_SONICWS_Avg","SE2_SONICWD_Avg","SE2_SONICWD_Std","SE1_SONICWS_Std","SE3_SONICTEMP_Avg","D3_PRTRES_Avg","D3_PRTTEMPAPPROX_Avg","SE4_HMPTEMP_Avg","SE9_HMPRH_Avg","P1_AIRFLOW_Avg","SE10_ACPOWER","BT_BATVOLT_Min","PRT_AVG","SONICWDT_AVG"
    %"TS","RN","ms","Deg","Deg","ms","mV","","Deg C","Deg C","%","Counts","ms","Volts","DegC","Deg True"
    
    fid10 = fopen( name10 );
    if( fid10 > 0 )
        fclose( fid10 );
    else
        fprintf(1,'No  10m: %s\n',name10);
        continue;
    end
    
    fid100 = fopen( name100 );
    if( fid100 > 0 )
        fclose( fid100 );
    else
        fprintf(1,'No 100m: %s\n',name100);
        continue;
    end
    
    fid300 = fopen( name300 );
    if( fid300 > 0 )
        fclose( fid300 );
    else
        fprintf(1,'No 300m: %s\n',name300);
        continue;
    end
    
    data10 = load(name10);
    
    data100 = load(name100);
    
    nmax = 90000;
    [data300 dt n] = load300( name300, nmax);
    
    hhmm = data10(:,4);
    hh = fix(hhmm/100);
    mn = hhmm - (hh*100);
    data10(:,3) = data10(:,3) + (((mn/60.) + hh)/24); %Julian day
    clear hhmm hh mm
    
    [nh,xout]=hist(data300(1:n,14),72);
    nhmax=max(nh);
    pnhmax=nhmax/n*100;
    %ci=[pnhmax*0.5 pnhmax];
    di=0:3:30;
    
    if pltflg %check for plotting

        figure1 = figure('Position',[100 50 650 650]);
        stitle10=sprintf('BAO %4d-%02d 10m', yr, mo);
        s10=find(data10(:,7) > 0.0);
        wind_rose(data10(s10,12),data10(s10,7),'di',di,'n',72,...
            'dtype','meteo','labtitle',stitle10,'lablegend','Speed m/s')
        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_010_%4d%02d_WR.png',...
                                   outpath, yr, mo );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
        
        figure2 = figure('Position',[100 50 650 650]);
        stitle300=sprintf('BAO %4d-%02d 300m', yr, mo);
        wind_rose(data300(1:n,14),data300(1:n,1),'di',di,'n',72,...
            'dtype','meteo','labtitle',stitle300,'lablegend','Speed m/s')
        annotation(figure2,'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_300_%4d%02d_WR.png',...
                                   outpath, yr, mo );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
       
        % T/RH histograms
        figure3 = figure('Position',[100 50 650 650]);
        set(gcf,'defaultaxesfonts',13)
        set(gca,'fontname','time','fontsize',13);

        subplot(3,1,1),hist(data300(1:n,13),55);
        stitle=sprintf('BAO %4d-%02d', yr, mo);
        title(stitle);
        xlabel('T (C) 300m');
        axis tight
        grid on
        xlim([-15,40])

        subplot(3,1,2),hist(data100(:,5),55);
        xlabel('T (C) 100m');
        axis tight
        grid on
        xlim([-15,40])

        subplot(3,1,3),hist(data10(:,5),55);
        xlabel('T (C) 10m');
        axis tight
        grid on
        xlim([-15,40])
        annotation(figure3,'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_10100300_%4d%02d_HT.png',...
                                   outpath, yr, mo );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
        
        figure4 = figure('Position',[100 50 650 650]);
        set(gcf,'defaultaxesfonts',13)
        set(gca,'fontname','time','fontsize',13);
        subplot(3,1,1),hist(data300(1:n,9),50);
        title(stitle);
        xlabel('RH (%) 300m');
        axis tight
        grid on
        xlim([0,100])
        subplot(3,1,2),hist(data100(:,6),50);
        xlabel('RH (%) 100m');
        axis tight
        grid on
        xlim([0,100])
        subplot(3,1,3),hist(data10(:,6),50);
        xlabel('RH (%) 10m');
        axis tight
        grid on
        xlim([0,100])
        annotation(figure4,'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_10100300_%4d%02d_HRH.png',...
                                   outpath, yr, mo );
            set(gcf,'PaperPositionMode','auto');
            print('-dpng', print_buffer);
        end
        
    end % skip plotting
 
    clear data10 data300 dt nh xout
    close all

    mo = mo + 1;
    if mo > 12
        mo = mo - 12;
        yr = yr + 1;
    end

end % loop through all files

clear all

retval = 0;
