Eric, I tried what you say about masked values, and that has helped. However there are still a few tracks that creep through. Below is a short script that shows what I mean. It loads in a matfile with the drifter track, which I will send to you separately.
%run path_example.py
import pylab as P
import numpy as N
from scipy import io
from matplotlib.transforms import Bbox as BB
import matplotlib.path as PP
P.close(‘all’)
Load drifter track (lon, lat)
lonlat = io.loadmat(‘lonlat’)
lon = N.squeeze(lonlat.values()[1])
lat = N.squeeze(lonlat.values()[0])
Set up box
box = BB.from_extents(-16.6, 28.05, -15.65, 28.15)
bx = [box.xmin, box.xmax, box.xmax, box.xmin, box.xmin]
by = [box.ymin, box.ymin, box.ymax, box.ymax, box.ymin]
track_all = N.transpose([lon, lat])
track_1 = N.transpose([lon[81:295], lat[81:295]])
track_2 = N.transpose([lon[81:290], lat[81:290]])
track_all = PP.Path(track_all)
track_1 = PP.Path(track_1)
track_2 = PP.Path(track_2)
Test if drifter passes thru box (it doesn’t so should be 0)
print track_all.intersects_bbox(box), ‘Should be 0’
print track_1.intersects_bbox(box), ‘Should be 0’
print track_2.intersects_bbox(box), ‘Should be 0’
P.figure()
P.plot(lon, lat, ‘r’, lw=0.3)
P.plot(lon[81:290], lat[81:290], ‘g’, lw=0.5)
P.plot(bx, by, ‘b’)
P.show()
···
On Tue, Sep 9, 2008 at 2:02 AM, Eric Firing <efiring@…202…> wrote:
Evan Mason wrote:
Hi, wonder if anyone can help me with path and bbox. I have a set of ocean drifter tracks and I want to know if they pass through a particular boxed area. This is straightforward to do but I wanted to try to do it with matplotlib.transforms and matplotlib.path, which look well-suited to this kind of task using ‘intersects_bbox’.
The essence of my code is below. I have a single drifter track (lon, lat) and have defined a boxed area. At no time does the drifter approach or enter the box - however, the final line,
print track.intersects_bbox(bbox)
returns True, which it shouldn’t. In total I have 340 tracks. My code says 74 pass though the box, but by counting there should be only about 9. Any help appreciated, thanks, Evan
import numpy as N
import numpy.ma <http://numpy.ma> as MA
import matplotlib.transforms as BB
import matplotlib.path as PP
In [200]: lon
Out[200]:
masked_array(data = [-15.52 -15.521 -15.541 …, – -- --],
mask = [False False False ..., True True True],
fill_value=1e+20)
In [201]: lat
Out[201]:
masked_array(data = [29.2 29.2 29.196 …, – -- --],
mask = [False False False ..., True True True],
fill_value=1e+20)
In [202]: len(lon), len(lat)
Out[202]: (3750, 3750)
track = MA.transpose([lon, lat])
track = PP.Path(track)
bbox = BB.Bbox.from_extents(-15.95, 29.6, -15.9, 29.65)
In [206]: print track.intersects_bbox(bbox)
1
Evan,
Can you strip the problem down more? That is, find one track that is misdiagnosed, and then try to find the shortest segment of it that still gets misdiagnosed. You could try subsampling and/or chopping chunks off the ends. If you can get it down to a track with only a few points, so that you can email a very short but complete script with illustrative data, that would make it much easier for someone to figure out what is going on.
I suspect it is related to the masked values. If you strip the masked points out of your lon and lat arrays, do you get the same result?
Try making an example with only 3 points, say with the middle one masked, and then unmasked.
Eric