%MAC USERS: Comment out lines 9-10 and comment in lines 11-12
function[diurnalCycle,smoothDiurnalCycle,polyEvalTimes,diurnalCycleAbsFactors,snrRaster,actuallevelsTimeRange,actualprofilesTimeRange,matrixMapR,matrixMapC,resolution,minutestamps,timeResolution,removal,heightvector]=plotProfilesContours_csv(m,d,y,levelheights,snr,dates,avgtime_csv,stationNum,pblhSourceOption)

    [diurnalCycle,smoothDiurnalCycle,polyEvalTimes,diurnalCycleAbsFactors,snrRaster,actuallevelsTimeRange,actualprofilesTimeRange,matrixMapR,matrixMapC,minheight,maxheight,minutesvector,heightvector,allminutes,allheights,emergeTime,emergeHeight,minutestamps,resolution,timeResolution,removal,~,~]=getDiurnal_csv(m,d,y,levelheights,snr,dates,avgtime_csv);
    
    if strcmp(pblhSourceOption,'grwp')==1
        pblh_localtime_adj_idx=NaN;
        pblh_idx=NaN;
        pblh=ncread(strcat('..\..\GRWP_PBLH_NetCDFs\GRWPPBLH_',int2str(stationNum),'.nc'),'pblh');
        pblh_local_time=ncread(strcat('..\..\GRWP_PBLH_NetCDFs\GRWPPBLH_',int2str(stationNum),'.nc'),'time_local');
        %pblh=ncread(strcat('../../GRWP_PBLH_NetCDFs/GRWPPBLH_',int2str(stationNum),'.nc'),'pblh');
        %pblh_local_time=ncread(strcat('../../GRWP_PBLH_NetCDFs/GRWPPBLH_',int2str(stationNum),'.nc'),'time_local');
        pblh_local_time=squeeze(pblh_local_time);
        pblh=squeeze(pblh);
        
        
        datestr=strcat(int2str(y),'-',int2str(m),'-',int2str(d));
        numHours = hours(diff(datetime({'2009-05-17'; datestr}, 'InputFormat', 'yyyy-MM-dd')));
        pblh_localtime_adj=pblh_local_time-numHours;
        hourIdx=find(pblh_localtime_adj>=9 & pblh_localtime_adj<=20);
        %pblh_localtime_adj_idx=pblh_localtime_adj(hourIdx);
        pblh_idx=pblh(hourIdx);
        %cloud_fracs_grwp_idx=cloud_fracs_grwp(hourIdx);

        smoothDiurnalCycle=pblh_idx(2:end);
        diurnalCycle=NaN;
    end
    
    
    %building figures
    colorsMarker={'ko','go','bo','ro','co','yo','mo','ko','go','bo','ro'};
    colors={'k','g','b','r','c','y','m','k','g','b','r'};
    hf = figure; 
    set(gcf, 'Position',  [20, 300, 1500, 350]);
    subplot(1,2,1);
    plottingCtr=0;
   
    %determining how many and which profiles to show and the offset for the
    if size(actuallevelsTimeRange,1)<=10
        i=1;
    elseif size(actuallevelsTimeRange,1)>10 && size(actuallevelsTimeRange,1)<=20
        i=round(size(actuallevelsTimeRange,1)/2)-5;
    elseif size(actuallevelsTimeRange,1)>20 && size(actuallevelsTimeRange,1)<=30
        i=round(size(actuallevelsTimeRange,1)/2)-10;
    elseif size(actuallevelsTimeRange,1)>30 && size(actuallevelsTimeRange,1)<=40
        i=round(size(actuallevelsTimeRange,1)/2)-15;
    elseif size(actuallevelsTimeRange,1)>40 && size(actuallevelsTimeRange,1)<=50
        i=round(size(actuallevelsTimeRange,1)/2)-20;
    elseif size(actuallevelsTimeRange,1)>50 && size(actuallevelsTimeRange,1)<=60
        i=round(size(actuallevelsTimeRange,1)/2)-25;
    elseif size(actuallevelsTimeRange,1)>60 && size(actuallevelsTimeRange,1)<=100
        i=round(size(actuallevelsTimeRange,1)/2)-30;
    end
    for j=1:10
        if size(actuallevelsTimeRange,1)<=20
            i=i+1;
            ofs='5';
            ofs1=5;
        elseif size(actuallevelsTimeRange,1)>20 && size(actuallevelsTimeRange,1)<=30
            i=i+2;
            ofs='10';
            ofs1=10;
        elseif size(actuallevelsTimeRange,1)>30 && size(actuallevelsTimeRange,1)<=40
            i=i+3;
            ofs='15';
            ofs1=15;
        elseif size(actuallevelsTimeRange,1)>40 && size(actuallevelsTimeRange,1)<=50
            i=i+4;
            ofs='20';
            ofs1=20;
        elseif size(actuallevelsTimeRange,1)>50 && size(actuallevelsTimeRange,1)<=60
            i=i+5;
            ofs='25';
            ofs1=25;
        elseif size(actuallevelsTimeRange,1)>60 && size(actuallevelsTimeRange,1)<=100
            i=i+6;
            ofs='30';
            ofs1=30;
        end
        plottingCtr=plottingCtr+1;
           
        
        %Plot interpolated profiles
        try
            p=matrixMapC(i,1);
        catch
            disp("Went through all profs");
        end
        try
            plot(snrRaster(find(heightvector(1,:)==(diurnalCycle(1,i))),p)+(j-1)*ofs1,diurnalCycle(1,i),colorsMarker{plottingCtr},'MarkerSize',5);
       
        catch
            disp("Could not disp " + p+ "th profile.");
        end
        hold on
        
        if plottingCtr==1
            try
                emergeInd=find(heightvector==emergeHeight);
                c1=line(snrRaster(:,p),heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label1={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};   
                plot(actualprofilesTimeRange(i,:),actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c1=line(x,y1,'Color','red','LineStyle','--');
                label1="Out of range";
            end
        end
        if plottingCtr==2
            try
                c2=line(snrRaster(:,p)+ofs1,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label2={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};   
                plot(actualprofilesTimeRange(i,:)+ofs1,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c2=line(x,y1,'Color','red','LineStyle','--');
                label2="Out of range";
            end
        end
        if plottingCtr==3
            try
                c3=line(snrRaster(:,p)+ofs1*2,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label3={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};  
                plot(actualprofilesTimeRange(i,:)+ofs1*2,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c3=line(x,y1,'Color','red','LineStyle','--');
                label3="Out of range";
            end
        end
        if plottingCtr==4
            try
                c4=line(snrRaster(:,p)+ofs1*3,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label4={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};  
                plot(actualprofilesTimeRange(i,:)+ofs1*3,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c4=line(x,y1,'Color','red','LineStyle','--');
                label4="Out of range";
            end
        end
        if plottingCtr==5
            try
                c5=line(snrRaster(:,p)+ofs1*4,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label5={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};  
                plot(actualprofilesTimeRange(i,:)+ofs1*4,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c5=line(x,y1,'Color','red','LineStyle','--');
                label5="Out of range";
            end
        end
        if plottingCtr==6
            try
                c6=line(snrRaster(:,p)+ofs1*5,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label6={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};   
                plot(actualprofilesTimeRange(i,:)+ofs1*5,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c6=line(x,y1,'Color','red','LineStyle','--');
                label6="Out of range";
            end
        end
        if plottingCtr==7
            try
                c7=line(snrRaster(:,p)+ofs1*6,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label7={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};   
                plot(actualprofilesTimeRange(i,:)+ofs1*6,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c7=line(x,y1,'Color','red','LineStyle','--');
                label7="Out of range";
            end
        end
        if plottingCtr==8
            try
                c8=line(snrRaster(:,p)+ofs1*7,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label8={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};  
                plot(actualprofilesTimeRange(i,:)+ofs1*7,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c8=line(x,y1,'Color','red','LineStyle','--');
                label8="Out of range";        
            end
        end
        if plottingCtr==9
            try
                c9=line(snrRaster(:,p)+ofs1*8,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label9={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))}; 
                plot(actualprofilesTimeRange(i,:)+ofs1*8,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c9=line(x,y1,'Color','red','LineStyle','--');
                label9="Out of range";
            end
        end
        if plottingCtr==10
            try
                c10=line(snrRaster(:,p)+ofs1*9,heightvector(1,:),'color',colors{plottingCtr},'linewidth',1);label10={append(int2str(floor(minutesvector(1,p)/60)),':',int2str(mod(minutesvector(1,p),60)))};%,', AbFac: ',string(diurnalCycleAbsFactors(i)))};  
                plot(actualprofilesTimeRange(i,:)+ofs1*9,actuallevelsTimeRange(i,:),'LineStyle','none','color',colors{plottingCtr},'marker','.','MarkerSize',7);
            catch
                x = [0 10];
                y1 = [0 0];
                c10=line(x,y1,'Color','red','LineStyle','--');
                label10="Out of range";
            end
        end
     end
     legend([c1; c2; c3; c4; c5; c6; c7; c8; c9; c10;], [label1; label2; label3; label4; label5; label6; label7; label8; label9; label10;],'Location','NorthEastOutside');
     xlabel("SNR (dB)");
     ylabel("Height (m)");
     set(gca);
     title("SNR/Height Interpolated Profiles for "+int2str(m)+"/"+int2str(d)+"/"+int2str(y)+" for station "+int2str(stationNum));
     subtitle("Each successive profile has a +"+ofs+"dB offset");
     
    [fullgrid]=NaN.*ones(4000,660);
    fullgrid(minheight:maxheight,minutesvector(1)-539:minutesvector(size(minutesvector,2))-539)=snrRaster;
    %fullgridRC=NaN.*ones(4000,660);
    %fullgridRC(minheight:maxheight,minutesvector(1)-539:minutesvector(size(minutesvector,2))-539)=rcSNRraster;
 
   
    subplot(1,2,2);
    imagesc(allminutes,allheights,fullgrid);
    
    xticks(linspace(540,1200,12));

    mv=linspace(540,1200,12);
    if((emergeTime-floor(emergeTime/60)*60)<10)
            minText2="0"+int2str(emergeTime-floor(emergeTime/60)*60);
    elseif(mod(emergeTime-floor(emergeTime/60)*60,10)==0)
            minText2=int2str(emergeTime-floor(emergeTime/60)*60);
    else
            minText2=int2str(emergeTime-floor(emergeTime/60)*60);
    end

  
    mv2={mv};
    for i=1:11
        if((mv(i)-floor(mv(i)/60)*60)<10)
            minText="0"+int2str(mv(i)-floor(mv(i)/60)*60);
        elseif(mod(mv(i)-floor(mv(i)/60)*60,10)==0)
            minText=int2str(mv(i)-floor(mv(i)/60)*60);
        else
            minText=int2str(mv(i)-floor(mv(i)/60)*60);
        end       
        mv2(i)={int2str(floor(mv(i)/60))+":"+minText};
    end
   
    xticklabels(mv2);
    title("Estimated PBL Diurnal Cycle: "+int2str(m)+"/"+int2str(d)+"/"+int2str(y)+" for station "+int2str(stationNum));
    subtitle("Emergence Time= " + int2str(floor(emergeTime/60))+":" +minText2+ "   Emergence Height= "+emergeHeight ); 
    xlabel("Time");
    ylabel("Height (m)");

    hold on
                 

    plot(minutestamps(:,1),(diurnalCycle(1,:)),'linestyle','none','Color','black','marker','o','MarkerSize',6); 
    plot(minutestamps(:,1),(diurnalCycle(1,:)),'linewidth',1.2,'Color','black','marker','o','MarkerSize',6);

    plot(polyEvalTimes,smoothDiurnalCycle,'Color','white','linewidth',3,'Marker', 'none');
    xl=xline(emergeTime,'--k','Emergence Time');
    xl.FontSize = 14;
    xl.LineWidth = 1.5;
    yl=yline(emergeHeight,'--k','Emergence Height');
    yl.LineWidth = 1.5;
    cmap=[1 1 1; jet];
    colormap(cmap);
    h = colorbar;
    ylabel(h, 'SNR (dB)')
    set(gca,'YDir','normal');
    
      


         
end


