closest neighbour?

Hi,

I am trying to extract a a line (or transect) of data across a 2 dimensional array. I want to know what the best way of finding data points within my 2D dataset closest to each point on my line. Is there a matplotlib pre-defined function to find the closest point within a radius or must I create my own?

Thanks a lot, Marjolaine.

Marjolaine Rouault
CSIR - NRE
Research Group : Earth Observation
15 Lower Hope street, Rosebank
7700
South Africa

Tel.: +27 (0) 21 658-2755
Fax: +27 (0) 21 658-2744
mrouault@...1229...

···

--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.

Marjolaine Rouault wrote:

Hi,

I am trying to extract a a line (or transect) of data across a 2 dimensional array. I want to know what the best way of finding data points within my 2D dataset closest to each point on my line. Is there a matplotlib pre-defined function to find the closest point within a radius or must I create my own?

Thanks a lot, Marjolaine.
  
Marjolaine: Sounds like what you want is nearest neighbor interpolation. The Basemap toolkit has an interp function that can do bilinear or nearest neighbor interpolation. It's typically used for regridding, but I think it will work for your use case too.

Here's some pseudo-code:

from mpl_toolkits.basemap import interp
# here datarr is 2d data array on a regular grid with x,y coordinates
# defined by 1-d arrays x,y. The arrays xout, yout describe the points
# on the transect. order=0 means nearest neighbor interp, order=1 means bilinear.
# dataout is the data interpolated to the transect.
dataout = interp(datarr,x,y,xout,yout,order=0)

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

Hi Jeff,

Yes, I think this is what I needed. Many thanks.

Marjolaine.

Jeff Whitaker <jswhit@...146...> 03/09/09 5:21 PM >>>

Marjolaine Rouault wrote:

Hi,

I am trying to extract a a line (or transect) of data across a 2 dimensional array. I want to know what the best way of finding data points within my 2D dataset closest to each point on my line. Is there a matplotlib pre-defined function to find the closest point within a radius or must I create my own?

Thanks a lot, Marjolaine.
  
Marjolaine: Sounds like what you want is nearest neighbor interpolation. The Basemap toolkit has an interp function that can do bilinear or nearest neighbor interpolation. It's typically used for regridding, but I think it will work for your use case too.

Here's some pseudo-code:

from mpl_toolkits.basemap import interp
# here datarr is 2d data array on a regular grid with x,y coordinates
# defined by 1-d arrays x,y. The arrays xout, yout describe the points
# on the transect. order=0 means nearest neighbor interp, order=1 means bilinear.
# dataout is the data interpolated to the transect.
dataout = interp(datarr,x,y,xout,yout,order=0)

-Jeff

···

--
Jeffrey S. Whitaker Phone : (303)497-6313
Meteorologist FAX : (303)497-6449
NOAA/OAR/PSD R/PSD1 Email : Jeffrey.S.Whitaker@...259...
325 Broadway Office : Skaggs Research Cntr 1D-113
Boulder, CO, USA 80303-3328 Web : http://tinyurl.com/5telg

--
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard.
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.

This message has been scanned for viruses and dangerous content by MailScanner,
and is believed to be clean. MailScanner thanks Transtec Computers for their support.