 World Ocean Circulation Experiment
Global Data Resource

Home   |   Documents   |   Software/Resources   |   Help       WOCE Integrated Search Tool Longitude Search Algorithm  Steve Loch of BODC,UK has come up with an algorithm that determines whether, knowing the western(d1) and eastern(d2) limits of a data set, it lies wholly or partly within the limits(s1,s2) of a search area. It allows for a discontinuity at the dateline or Greenwich meridian.

int boxsel(float s1, float s2, float d1, float d2){
if(s1>s2){if(s1>d2) s1 -=360; else s2 += 360;}
if(d1>d2){if(s2<d1) d1 -=360; else d2 += 360;}
return !(s2<d1 || s1>d2); }
The justification runs as follows:-

The fourth line says that if the right margin of the selection box is to the left of the left margin of the data or the left margin of the selection box is to the right of the right margin of the data there is no overlap and then (using !) negates it.

A wrapped box is one where the left margin is numerically greater than the right margin. If there are no wrapped boxes no changes are made to s1,s2,d1 or d2 and the test is applied. What happens then when you have wrapped boxes?

You can unwrap a wrapped selection box (more properly interval) in two ways: either reduce the left margin by 360 or increment the right margin by the same amount (these are the -=, += statements); In unwrapping the selection box you need to consider if the selection box left margin (s1) is numerically greater than the right margin (d2) of the data. This is one of the conditions for non-overlap (see 4'th line) Adding 360 to s2 at this point changes nothing. We need to look at the other possibility of reducing s1.

If the data box is also wrapped we need to see which way to unwrap it. If the right margin of the selection box is left of the left margin of the data adding to the data's right margin does nothing. Instead we need to check the other possibility of numerically reducing the left margin of the data (d1).

From this we deduce that the algorithm works irrespective of definition interval. E.g. [0,360) , [-180,180), (-180,180] or even [540,900), but note, you should as indicated exclude one of the endpoints, and that the data and selection definition intervals are the same.