function[diurnalCycle_nc,smoothDiurnalCycle_nc,polyEvalTimes_nc,diurnalCycleAbsFactors_nc,snr_grid_nc_for_date,actualLevels_nc,actualProfiles_nc,matrixMapR_nc,matrixMapC_nc,oneMinRes,heightVec_nc0,emergeTime_nc,emergeHeight_nc,minutestamps_nc,resolution_nc,timeResolution_nc,removal_nc,smoothResolutionCurve_nc,negIndVector2_nc,minheight_nc,maxheight_nc,cloud_fracs_nc_idx,not30Indsum_nc,timeDif]=getDiurnal_nc(month,day,year,heightVec_nc,snr_grid_nc,mask_for_snr,localTime_nc,utcTime_nc,cloud_fracs_nc)
    datestr=strcat(int2str(year),'-',int2str(month),'-',int2str(day));
    numMinutes = minutes(diff(datetime({'2009-05-17'; datestr}, 'InputFormat', 'yyyy-MM-dd')));
    localTime_ncAdj=localTime_nc-numMinutes;
    utcTime_ncAdj=utcTime_nc-numMinutes;
    dateIdx=find(localTime_ncAdj>=540 & localTime_ncAdj<=1800);
    if isempty(dateIdx)
        diurnalCycle_nc=NaN;
        smoothDiurnalCycle_nc=NaN;
        polyEvalTimes_nc=NaN;
        diurnalCycleAbsFactors_nc=NaN;
        snr_grid_nc_for_date=NaN;
        actualLevels_nc=NaN;
        actualProfiles_nc=NaN;
        matrixMapR_nc=NaN;
        matrixMapC_nc=NaN;
        oneMinRes=NaN;
        heightVec_nc0=NaN;
        emergeTime_nc=NaN;
        emergeHeight_nc=NaN;
        minutestamps_nc=NaN;
        resolution_nc=NaN;
        timeResolution_nc=NaN;
        removal_nc=NaN;
        smoothResolutionCurve_nc=NaN;
        negIndVector2_nc=NaN;
        minheight_nc=NaN;
        maxheight_nc=NaN;
        cloud_fracs_nc_idx=NaN;
        not30Indsum_nc=NaN;
        timeDif=NaN;
    else
        localTime_ncAdj_idx=localTime_ncAdj(dateIdx);
        utcTime_ncAdj_idx=utcTime_ncAdj(dateIdx);
        timeDif=(localTime_ncAdj_idx(1)-utcTime_ncAdj_idx(1))/60;
        cloud_fracs_nc_idx=cloud_fracs_nc(dateIdx);
        snrGrid_nc_idx=snr_grid_nc(:,dateIdx);
        snrGridRaw_nc_idx=mask_for_snr(:,dateIdx);
        out=sum(snrGridRaw_nc_idx,2);
        rawRows=find(out>0);
        actualProfiles_nc=snrGrid_nc_idx(rawRows,:);
        actualProfiles_nc=actualProfiles_nc';
        actualLevels_nc=NaN.*actualProfiles_nc;
        not30Indsum_nc=0;
        for i=1:size(actualLevels_nc,1)
            actualLevels_nc(i,:)=heightVec_nc(rawRows);
        end

        for i = 1:size(actualProfiles_nc,1)
            for j = 1:size(actualProfiles_nc,2)
                if rem(actualProfiles_nc(i,j),1)~=0
                    actualProfiles_nc(i,j)=NaN;
                    actualLevels_nc(i,j)=NaN;
                end
            end
        end
        
        minheight_nc = min(actualLevels_nc,[],'all');
        maxheight_nc = max(actualLevels_nc,[],'all');
        heightVec_nc(heightVec_nc<minheight_nc)=[];
        heightVec_nc(heightVec_nc>maxheight_nc)=[];



        oneMinRes=localTime_ncAdj_idx(1):1:localTime_ncAdj_idx(size(localTime_ncAdj_idx,1));
        heightVec_nc=heightVec_nc';
        numheights=size(heightVec_nc,2);
        numminutes=size(oneMinRes,2);
        [snr_grid_nc_for_date]=NaN.*ones(numheights,numminutes);
        matrixMapR_nc=NaN.*ones(size(actualLevels_nc,1),size(actualLevels_nc,2));
        matrixMapC_nc=NaN.*ones(size(actualLevels_nc,1),size(actualLevels_nc,2));

         for i =1:size(actualLevels_nc,1)
            colIndex=find(oneMinRes==localTime_ncAdj_idx(i));  
            for j=1:size(actualLevels_nc,2)
                if ~isnan(actualLevels_nc(i,j))
                    rowIndex=find(heightVec_nc==actualLevels_nc(i,j));
                    snr_grid_nc_for_date(rowIndex,colIndex)=actualProfiles_nc(i,j);
                    matrixMapR_nc(i,j)=rowIndex;
                    matrixMapC_nc(i,j)=colIndex;
                end
            end
         end

        snr_grid_nc_for_date0 = fillmissing(snr_grid_nc_for_date,'linear');
        snr_grid_nc_for_date0 = fillmissing(snr_grid_nc_for_date0','linear')';
        [snr_grid_nc_for_date]=snr_grid_nc_for_date0;
        

        %Finding profiles that have high starting heights or low ending heights
        %to remove overly extrapolated values
        highStarts=NaN.*ones(size(actualLevels_nc,1),1);
        lowEnds=NaN.*ones(size(actualLevels_nc,1),2);

        for i=1:size(actualLevels_nc,1)
            %if actualLevels_nc(i,1)~=minheight_nc
            if isnan(actualLevels_nc(i,1))
                highStarts(i,1)=i;
            else
                highStarts(i,1)=0;
            end
            [max0,idx]=max(actualLevels_nc(i,:));
            if max0~=maxheight_nc
                lowEnds(i,1)=i;
                lowEnds(i,2)=idx;
            else
                lowEnds(i,1)=0;
                lowEnds(i,2)=9999;
            end
        end

        %remove overly extrapolated values
        for i=1:size(highStarts,1)
            nancolr=0;
            nancoll=0;
            if highStarts(i)>0
                disp('i');
                disp(i);
                x=1;
                while(isnan(matrixMapR_nc(i,x)))
                    x=x+1;
                end
                disp('x');
                disp(x)
                nanrow=matrixMapR_nc(i,x)-50;

                y=1;
                while(isnan(matrixMapC_nc(i,y)))
                    y=y+1;
                end
                disp('y');
                disp(y);
                nancol=matrixMapC_nc(i,y);
                if i<size(highStarts,1)
                    %nancolr=matrixMapC_nc(i+1,1);
                    nancolr=nanmean(matrixMapC_nc(i+1,:));
                end
                if i>1
                    %nancoll=matrixMapC_nc(i-1,1);
                    nancoll=nanmean(matrixMapC_nc(i-1,:));
                end
                snr_grid_nc_for_date(1:nanrow,nancol)=NaN;
                if nancolr>0
                    snr_grid_nc_for_date(1:nanrow,nancol:nancolr-1)=NaN;
                end
                if nancoll>0
                    snr_grid_nc_for_date(1:nanrow,nancoll+1:nancol)=NaN;
                end
            end
            if lowEnds(i,1)>0
                nanrow=matrixMapR_nc(i,lowEnds(i,2))+50;
                nancol=matrixMapC_nc(i,lowEnds(i,2));
                if i<size(lowEnds,1)
                    [~,idx]=max(actualLevels_nc(i+1,:));
                    nancolr=matrixMapC_nc(i+1,idx);
                end
                if i>1
                    [~,idx]=max(actualLevels_nc(i-1,:));
                    nancoll=matrixMapC_nc(i-1,idx);
                end
                snr_grid_nc_for_date(nanrow:end,nancol)=NaN;
                if nancolr>0
                    snr_grid_nc_for_date(nanrow:end,nancol:nancolr-1)=NaN;
                end
                if nancoll>0
                    snr_grid_nc_for_date(nanrow:end,nancoll+1:nancol)=NaN;
                end
            end
        end


            %emergence time
        %720=12PM
        if oneMinRes(size(oneMinRes,2))>=720
            twelvepmInd=find(oneMinRes==720);
        else
            twelvepmInd=(size(oneMinRes,2));
        end

        emergeIndR=NaN;
        emergeIndC=NaN;

        for i=1:size(snr_grid_nc_for_date,1)
            rastRow=snr_grid_nc_for_date(i,1:twelvepmInd);
            numNans=sum(isnan(rastRow));
            %make sure the row we are looking at for emergence time is filled
            %at least 35% with values
            if numNans<0.65*(oneMinRes(1,twelvepmInd)-oneMinRes(1,1))
                emergeIndR=i;
                break
            end
        end
        if ~isnan(emergeIndR)
            [~,emergeIndC]=max(snr_grid_nc_for_date(emergeIndR,1:twelvepmInd));
        else
            for i=1:size(oneMinRes,2)
                if oneMinRes(i)>720 && oneMinRes(i)<840
                    emergeIndC=i;
                    break
                end
            end
        end
        
        disp('emergeInd');
        disp(emergeIndC);

        rowmeans=nanmean(matrixMapC_nc,2);
        startProf=find(rowmeans==emergeIndC);
        while isempty(startProf) && emergeIndC<size(oneMinRes,2)
            emergeIndC=emergeIndC+1;
            startProf=find(matrixMapC_nc==emergeIndC);
        end
        disp('sp')
        disp(startProf);

        emergeTime_nc=oneMinRes(emergeIndC);
        if ~isnan(emergeIndR)
            emergeHeight_nc=heightVec_nc(emergeIndR);
        else
            emergeHeight_nc=heightVec_nc(1);
        end

        diurnalCycle_nc=NaN.*ones(1,size(actualLevels_nc,1));
        diurnalCycleAbsFactors_nc=NaN.*ones(1,size(actualLevels_nc,1));
        negIndVector_nc=NaN.*ones(1,size(actualLevels_nc,1));

        difs2=[];
        for i=1:size(actualLevels_nc,1)
           difs=[];
           for j=2:size(actualLevels_nc,2)
                dif=actualLevels_nc(i,j)-actualLevels_nc(i,j-1);
                difs=[difs dif];     
           end
           difs2=[difs2 nanmean(difs)];
        end
        [resolution_nc]=nanmean(difs2);

        minutestamps_nc=(localTime_ncAdj_idx);
        within2HrsEtime=max(find(minutestamps_nc<=(emergeTime_nc+120)));
        disp('etime');
        disp(emergeTime_nc);
        disp('within2hrs');
        disp(within2HrsEtime);



        %applying algorithm to profiles
        for i=startProf(1):size(actualLevels_nc,1)
            if i<=within2HrsEtime
                hookstat=0;
            else
                hookstat=1;
            end
            disp('profnum');
            disp(i);
            sigVecColNum=nanmean(matrixMapC_nc(i,:));
            sigvec=snr_grid_nc_for_date(:,sigVecColNum)';
            signonan=sigvec(~isnan(sigvec));
            heightVec_nc2=heightVec_nc(~isnan(sigvec));
            %disp(heightVec_nc2);


            if heightVec_nc2(1)>1000 || heightVec_nc2(size(heightVec_nc2,2))<2000
                
                disp('Insufficient Height Range');
                disp(heightVec_nc2(1)>1000);
                disp(heightVec_nc2(size(heightVec_nc2,2)));
                signonan=NaN;
            end

            noninterpsig=actualProfiles_nc(i,:);
            startnanct=0;
            for n=1:size(noninterpsig,2)
                if ~isnan(noninterpsig(n))
                    break
                else
                    startnanct=startnanct-1;
                end
            end

            noninterpsig=circshift(noninterpsig,startnanct);

            %noninterpsig=noninterpsig(~isnan(noninterpsig));


            %applying algorithm to interpolated profiles from times of
            %measurement
            %40 and 30 are the percentages applied to the absolute max of a
            %profile to determine the abs factor
            if (heightVec_nc2(1,1)-emergeHeight_nc)<1000 
                [PBLHdia,absFactor,negIndicator,not30Ind]=runAlgorithm(signonan,heightVec_nc2,noninterpsig,hookstat,30,40);
            else
                PBLHdia=NaN;
                absFactor=-99;
            end
            
            negIndVector_nc(i)=negIndicator;
            not30Indsum_nc=not30Indsum_nc+not30Ind;


            if i==startProf(1)
                diurnalCycle_nc(i)=emergeHeight_nc; 
                diurnalCycleAbsFactors_nc(i)=-99;
            else
                diurnalCycle_nc(i)=PBLHdia;
                diurnalCycleAbsFactors_nc(i)=absFactor;   
            end                      
        end


        difs=[];
        for i=1:size(minutestamps_nc,1)-1
           dif=minutestamps_nc(i+1,1)-minutestamps_nc(i,1);
           difs=[difs dif];     
        end
        [timeResolution_nc]=nanmean(difs);

        if timeResolution_nc<=48
            consecNanCheck=round(120/timeResolution_nc);
        else
            consecNanCheck=2;
        end

        nanct=0;
        removal_nc=0;
        for i=startProf(1):size(diurnalCycle_nc,2)  
            if nanct>consecNanCheck
                diurnalCycle_nc=NaN;
                removal_nc=1;
                break
            end
            if isnan(diurnalCycle_nc(1,i))
                nanct=nanct+1;
            else
                nanct=0;
            end
        end

        numNansInCycle=sum(isnan(diurnalCycle_nc));
        if numNansInCycle>=.75*size(diurnalCycle_nc,2)
            diurnalCycle_nc=NaN;
            removal_nc=1;
        end






        if size(diurnalCycle_nc,2)>5
            forPolyCurve=diurnalCycle_nc(1,:);
            if rem(emergeTime_nc,60)==0
                nearDiv60start=emergeTime_nc;
            else
                nearDiv60start = emergeTime_nc + (60 - rem(emergeTime_nc,60));
            end
            lastnonan=size(diurnalCycle_nc,2);
            for i = size(diurnalCycle_nc,2):-1:1
                if ~isnan(diurnalCycle_nc(1,i))
                    lastnonan=i;
                    break
                end
            end

            lasttime=minutestamps_nc(lastnonan,1);

            nearDiv60end = lasttime - rem(lasttime,60) ;
            polyEvalTimes_nc=(nearDiv60start:60:nearDiv60end);

            minForPoly=minutestamps_nc(:,1);
            nanIndexes = isnan(forPolyCurve);

            minForPoly(nanIndexes) = [];
            forPolyCurveNoNan=forPolyCurve;
            forPolyCurveNoNan(nanIndexes) = [];
            smfact=3;

            [q,~,mu] = polyfit(minForPoly', forPolyCurveNoNan, smfact);
            [smoothDiurnalCycle_nc] = polyval(q,polyEvalTimes_nc,[],mu);
        else
            smoothDiurnalCycle_nc=NaN;
            polyEvalTimes_nc=NaN;
        end



        if any(smoothDiurnalCycle_nc<0) || any(smoothDiurnalCycle_nc>4000)
            smoothDiurnalCycle_nc=NaN;
        end


        forResPoly=difs2*2;    
        minForPoly=minutestamps_nc(:,1);
        nanIndexes = isnan(forResPoly);
        minForPoly(nanIndexes) = [];
        forResPolyNoNan=forResPoly;
        forResPolyNoNan(nanIndexes) = [];
        smfact=3;

        [q,~,mu] = polyfit(minForPoly, forResPolyNoNan, smfact);
        [smoothResolutionCurve_nc] = polyval(q,polyEvalTimes_nc,[],mu);

        negIndVector2_nc=NaN.*ones(1,size(polyEvalTimes_nc,2));
        for i = 1:size(polyEvalTimes_nc,2)   
            [~,index] = min(abs(minutestamps_nc-polyEvalTimes_nc(i)));
            negIndVector2_nc(i) = negIndVector_nc(index);
        end
    end
    cloud_fracs_idx2=NaN.*ones(size(polyEvalTimes_nc,1),size(polyEvalTimes_nc,2));
    for i=1:size(polyEvalTimes_nc,2)
        if polyEvalTimes_nc(i)<780
            cloudBefore13=cloud_fracs_nc_idx(minutestamps_nc<780);
            cloud_fracs_idx2(i)=cloudBefore13(1);
        elseif polyEvalTimes_nc(i)>=780 && polyEvalTimes_nc(i)<960
            cloud13to16=cloud_fracs_nc_idx(minutestamps_nc>=780 & minutestamps_nc<960);
            cloud_fracs_idx2(i)=cloud13to16(1);
        elseif polyEvalTimes_nc(i)>=960
            cloudAfter16=cloud_fracs_nc_idx(minutestamps_nc>=960);
            cloud_fracs_idx2(i)=cloudAfter16(1);
        end
    end
    cloud_fracs_nc_idx=cloud_fracs_idx2;
    if ~isempty(dateIdx)    
        heightVec_nc0=heightVec_nc;
    end
end