%Name: getRaintype.m
%Purpose: gives the rain type value for the new grid by searching for the
%nearest neighbout in the old grid
%Latest update: 2007/11/29
%By: Darren Wilton, (corrected by Ulrike Romatschke)
%Used in: getTRMM_india.m

function [nearestneighbour] = getRaintype(gridlong, gridlat, longitude, latitude, rain)

degrees = 0.05;

vector=zeros(size(longitude));

%vector will assing 1 for locations within the box that are close to the pixel
vector(longitude < gridlong+degrees & longitude > gridlong-degrees & ...
    latitude < gridlat+degrees  & latitude > gridlat-degrees) = 1;

if mean(vector)==0
    nearestneighbour = NaN;
  
elseif isnan(rain(vector>0))
    nearestneighbour = NaN;
  
else
    rt2 = rain(vector>0);
    longitude2 = longitude(vector>0,1);
    latitude2 = latitude(vector>0,1);

    distance2point(:,1) = distance(gridlat,gridlong,latitude2(:,1),longitude2(:,1));
    valuesatpoint(:,:) = [rt2(:,1) distance2point(:,1)];

    XXX = min(valuesatpoint(:,2));
    nearestneighbour = valuesatpoint(valuesatpoint(:,2)==XXX,1);
end