function[snrRaster,rawSnrRaster,matrixMapR,matrixMapC,minutesvector,heightvector,numRemoved]=getSNRgrid(m,d,y,levelheights,snr,dates,avgtime)
    %Run doloop on the raw data to separate profiles for a specified day
    [actuallevels,actualprofiles,newHours,newMinutes,numRemoved]=makeProfiles(m,d,y,levelheights,snr,dates,avgtime);   
    %convert the times of each profile start into number of minutes since
    %midnight and store them in minutestamps if they are between 9AM and
    %8PM. If the timestamp is out of that range, put NaN in its position.
    [minutestamps]=NaN.*ones(size(newHours,1),1);
    for i=1:size(newHours,1)
        stamp=newHours(i)*60+newMinutes(i);
        if stamp>=540 && stamp<=1200
            minutestamps(i)=stamp;
        end
    end
        
    
    %Build a vector that has the same size as minutestamps. Fill all values
    %with NaN. Change the value to 1 if minutestamps has a value at that
    %position. 
    profsInRangeIdx=NaN.*ones(size(minutestamps,1),1);
    for i=1:size(minutestamps)
        if ~isnan(minutestamps(i))
            profsInRangeIdx(i)=1;
        end
    end


    %Use the index of timestamps in range to pull the profiles and levels
    %associated with those timestamps.
    [actuallevelsTimeRange]=actuallevels;
    [actualprofilesTimeRange]=actualprofiles;

    for i=1:size(actuallevels,2)
        actuallevelsTimeRange(:,i)=actuallevelsTimeRange(:,i).*profsInRangeIdx;
        actualprofilesTimeRange(:,i)=actualprofilesTimeRange(:,i).*profsInRangeIdx;
    end




    %For levels and profiles, we have to be careful to only remove rows
    %that are entirely NaN because some profiles/levels may have a NaN
    %within the profile, and we do not want to remove that profile.
    actuallevelsTimeRange(~any(~isnan(actuallevelsTimeRange), 2),:)=[];
    actualprofilesTimeRange(~any(~isnan(actualprofilesTimeRange), 2),:)=[];


   %Now that all of our timestamps, profiles, and levels are in range, we
   %remove the data outside of range.
    minutestamps=minutestamps(~isnan(minutestamps));
   % disp(minutestamps);

    maxheight=max(actuallevelsTimeRange(:));
    minheight=min(actuallevelsTimeRange(:));

    [heightvector]=minheight:1:maxheight;
    try
    [minutesvector]=minutestamps(1,1):1:minutestamps(size(minutestamps,1),1);
    catch
        disp("Not enough data for this date.");
    end

    numheights=size(heightvector,2);
    numminutes=size(minutesvector,2);


    %Build empy matrix for interpolated SNR matrix based on number of
    %meters between lowest and highest recording and number of minutes
    %between first and last recording
    [snrRaster]=NaN.*ones(numheights,numminutes);
    [rawSnrRaster]=zeros(numheights,numminutes);

    %Building 2 matrices of size actuallevelsTimeRange that will contain the row
    %and column indices of where each element is plotted in the
    %interpolated SNR matrix
    %Also inputs values from actuallevelsTimeRange into SNR matrix
    matrixMapR=NaN.*ones(size(actuallevelsTimeRange,1),size(actuallevelsTimeRange,2));
    matrixMapC=NaN.*ones(size(actuallevelsTimeRange,1),size(actuallevelsTimeRange,2));
    for i =1:size(actuallevelsTimeRange,1)
        colIndex=find(minutesvector==minutestamps(i));  
        for j=1:size(actuallevelsTimeRange,2)
            if ~isnan(actuallevelsTimeRange(i,j))
                rowIndex=find(heightvector==actuallevelsTimeRange(i,j));
                snrRaster(rowIndex,colIndex)=actualprofilesTimeRange(i,j);
                rawSnrRaster(rowIndex,colIndex)=1;
                matrixMapR(i,j)=rowIndex;
                matrixMapC(i,j)=colIndex;
            end
        end
    end

    %linear interpolation in height and time
    snrRaster0 = fillmissing(snrRaster,'linear');
    snrRaster0 = fillmissing(snrRaster0','linear')';
    [snrRaster]=snrRaster0;

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

    for i=1:size(actuallevelsTimeRange,1)
        if actuallevelsTimeRange(i,1)~=minheight
            highStarts(i,1)=i;
        else
            highStarts(i,1)=0;
        end
        [max0,idx]=max(actuallevelsTimeRange(i,:));
        if max0~=maxheight
            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
            nanrow=matrixMapR(i,1)-50;
            nancol=matrixMapC(i,1);
            if i<size(highStarts,1)
                nancolr=matrixMapC(i+1,1);
            end
            if i>1
                nancoll=matrixMapC(i-1,1);
            end
            snrRaster(1:nanrow,nancol)=NaN;
            if nancolr>0
                snrRaster(1:nanrow,nancol:nancolr-1)=NaN;
            end
            if nancoll>0
                snrRaster(1:nanrow,nancoll+1:nancol)=NaN;
            end
        end
        if lowEnds(i,1)>0
            nanrow=matrixMapR(i,lowEnds(i,2))+50;
            nancol=matrixMapC(i,lowEnds(i,2));
            if i<size(lowEnds,1)
                [~,idx]=max(actuallevelsTimeRange(i+1,:));
                nancolr=matrixMapC(i+1,idx);
            end
            if i>1
                [~,idx]=max(actuallevelsTimeRange(i-1,:));
                nancoll=matrixMapC(i-1,idx);
            end
            snrRaster(nanrow:end,nancol)=NaN;
            if nancolr>0
                snrRaster(nanrow:end,nancol:nancolr-1)=NaN;
            end
            if nancoll>0
                snrRaster(nanrow:end,nancoll+1:nancol)=NaN;
            end
        end
    end
 end