source: trunk/SRC/Interpolation/inquad.pro @ 125

Last change on this file since 125 was 125, checked in by pinsard, 18 years ago

improvements of Interpolation/*.pro header

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 9.8 KB
Line 
1;+
2; @file_comments
3; to find if an (x,y) point is in a quadrilateral (x1,x2,x3,x4)
4;
5; @categories grid manipulation
6;
7; @param x {in}{required}
8; @param y {in}{required}
9;  the coordinates of the point we want to know where it is.
10;  Must be a scalar if /ONSPHERE activated else can be scalar or array.
11;
12; @param x1 {in}{required}
13; @param y1 {in}{required}
14; @param x2 {in}{required}
15; @param y2 {in}{required}
16; @param x3 {in}{required}
17; @param y3 {in}{required}
18; @param x4 {in}{required}
19; @param y4 {in}{required}
20;  the coordinates of the quadrilateral given in the CLOCKWISE order.
21;  Scalar or array.
22;
23; @keyword DOUBLE use double precision to perform the computation
24;
25; @keyword ONSPHERE to specify that the quadilateral are on a sphere and
26;    that teir coordinates are longitude-latitude coordinates. In this
27;    case, est-west periodicity, poles singularity and other pbs
28;    related to longitude-latitude coordinates are managed
29;    automatically.
30;
31; @keyword ZOOMRADIUS {default=4}
32; the zoom (circle centred on the (x,y) with a radius of
33;    zoomradius degree where we look for the the quadrilateral which
34;    contains the (x,y) point) used for the satellite projection
35;    when /ONSPHERE is activated.
36;    4 seems to be the minimum which can be used.
37;    Can be increase if the cell size is larger than 5 degrees.
38;
39; @keyword NOPRINT to suppress the print messages.
40;
41; @keyword NEWCOORD
42;
43; @returns
44;    a n element vector. Where n is the number of elements of
45;    x. res[i]=j means that the point number i is located in the
46;    quadrilateral number j with (0 <= j <= n_elements(x0)-1)
47;
48; @restrictions
49; I think degenerated quadrilateral (e.g. flat of twisted) is not work.
50; This has to be tested.
51;
52; @examples
53;
54; IDL> x = 1.*[1, 2, 6, 7, 3]
55; IDL> y = 1.*[1, 3, 3, 4, 7]
56; IDL> x1 = 1.*[0,4,2]
57; IDL> y1 = 1.*[1,4,8]
58; IDL> x2 = 1.*[1,6,4]
59; IDL> y2 = 1.*[5,6,8]
60; IDL> x3 = 1.*[3,8,4]
61; IDL> y3 = 1.*[4,4,6]
62; IDL> x4 = 1.*[2,6,2]
63; IDL> y4 = 1.*[0,2,6]
64; IDL> splot, [0,10], [0,10], xstyle = 1, ystyle = 1,/nodata
65; IDL> for i=0,2 do oplot, [x4[i],x1[i],x2[i],x3[i],x4[i]],[y4[i],y1[i],y2[i],y3[i],y4[i]]
66; IDL> oplot, x, y, color = 20, psym = 1, thick = 2
67; IDL> print, inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4)
68;
69;      On a sphere see clickincell.pro...
70;
71; @history
72;      Sebastien Masson (smasson\@lodyc.jussieu.fr)
73;      August 2003
74;      Based on Convert_clic_ij.pro written by Gurvan Madec
75;
76; @version $Id$
77;
78;-
79FUNCTION inquad, x, y, x1, y1, x2, y2, x3, y3, x4, y4, ONSPHERE = onsphere,  DOUBLE = double, ZOOMRADIUS = zoomradius, NOPRINT = noprint, NEWCOORD = newcoord
80;
81  compile_opt idl2, strictarrsubs
82;
83  ntofind = n_elements(x)
84  nquad = n_elements(x2)
85;
86  IF keyword_set(onsphere) THEN BEGIN
87; save the inputs parameters
88    xin = x
89    yin = y
90    x1in = x1
91    y1in = y1
92    x2in = x2
93    y2in = y2
94    x3in = x3
95    y3in = y3
96    x4in = x4
97    y4in = y4
98; for map_set
99    x = x MOD 360
100    x1 = x1 MOD 360
101    x2 = x2 MOD 360
102    x3 = x3 MOD 360
103    x4 = x4 MOD 360
104; save !map
105    save = {map:!map, x:!x, y:!y, z:!z, p:!p}
106; do a satellite projection
107    IF NOT keyword_set(zoomradius) THEN zoomradius = 4
108    map_set, y[0], x[0], 0, /satellite, sat_p = [1+zoomradius*20/6371.229, 0, 0], /noerase, /iso, /noborder
109; use normal coordinates to reject cells which are out of the projection.
110    tmp  = convert_coord(x, y, /DATA, /TO_NORMAL, DOUBLE = double)
111    tmp1 = convert_coord(x1, y1, /DATA, /TO_NORMAL, DOUBLE = double)
112    tmp2 = convert_coord(x2, y2, /DATA, /TO_NORMAL, DOUBLE = double)
113    tmp3 = convert_coord(x3, y3, /DATA, /TO_NORMAL, DOUBLE = double)
114    tmp4 = convert_coord(x4, y4, /DATA, /TO_NORMAL, DOUBLE = double)
115; remove cell which have one corner with coordinates equal to NaN
116    test = finite(tmp1[0, *]+tmp1[1, *]+tmp2[0, *]+tmp2[1, *] $
117                  +tmp3[0, *]+tmp3[1, *]+tmp4[0, *]+tmp4[1, *])
118    good = where(temporary(test) EQ 1)
119;
120    IF good[0] EQ -1 THEN BEGIN
121      IF NOT keyword_set(noprint) THEN print, 'The point is out of the cells'
122; restore the input parameters
123      x = temporary(xin)
124      y = temporary(yin)
125      x1 = temporary(x1in)
126      y1 = temporary(y1in)
127      x2 = temporary(x2in)
128      y2 = temporary(y2in)
129      x3 = temporary(x3in)
130      y3 = temporary(y3in)
131      x4 = temporary(x4in)
132      y4 = temporary(y4in)
133; restore old !map...
134      !map = save.map
135      !x = save.x
136      !y = save.y
137      !z = save.z
138      !p = save.p
139      RETURN,  -1
140    ENDIF
141;
142    x  = tmp[0]
143    y  = tmp[1]
144    x1 = tmp1[0, good]
145    y1 = tmp1[1, good]
146    x2 = tmp2[0, good]
147    y2 = tmp2[1, good]
148    x3 = tmp3[0, good]
149    y3 = tmp3[1, good]
150    x4 = tmp4[0, good]
151    y4 = tmp4[1, good]
152;
153    tmp1 = -1 & tmp2 = -1 & tmp3 = -1 & tmp4 = -1
154; remove cells which are obviously bad
155    test = (x1 GT x AND x2 GT x AND x3 GT x AND x4 GT x) $
156      OR (x1 LT x AND x2 LT x AND x3 LT x AND x4 LT x) $
157      OR (y1 GT y AND y2 GT y AND y3 GT y AND y4 GT y) $
158      OR (y1 LT y AND y2 LT y AND y3 LT y AND y4 LT y)
159    good2 = where(temporary(test) EQ 0)
160;
161    IF good2[0] EQ -1 THEN BEGIN
162      IF NOT keyword_set(noprint) THEN print, 'The point is out of the cells'
163; restore the input parameters
164      x = temporary(xin)
165      y = temporary(yin)
166      x1 = temporary(x1in)
167      y1 = temporary(y1in)
168      x2 = temporary(x2in)
169      y2 = temporary(y2in)
170      x3 = temporary(x3in)
171      y3 = temporary(y3in)
172      x4 = temporary(x4in)
173      y4 = temporary(y4in)
174; restore old !map...
175      !map = save.map
176      !x = save.x
177      !y = save.y
178      !z = save.z
179      !p = save.p
180      RETURN,  -1
181    ENDIF
182;
183    nquad = n_elements(good2)
184    x1 = x1[good2]
185    y1 = y1[good2]
186    x2 = x2[good2]
187    y2 = y2[good2]
188    x3 = x3[good2]
189    y3 = y3[good2]
190    x4 = x4[good2]
191    y4 = y4[good2]
192  ENDIF
193;
194;
195; the point is inside the quadilateral if test eq 1
196; with test equal to:
197;     test = ((x-x1)*(y2-y1) GE (x2-x1)*(y-y1)) $
198;       *((x-x2)*(y3-y2) GT (x3-x2)*(y-y2)) $
199;       *((x-x3)*(y4-y3) GT (x4-x3)*(y-y3)) $
200;       *((x-x4)*(y1-y4) GE (x1-x4)*(y-y4))
201;
202; computation of test without any do loop for ntofind points (x,y) and
203; nquad quadilateral((x1,x2,x3,x4),(y1,y2,y3,y4))
204; test dimensions are (ntofind, nquad)
205; column i of test corresponds to the intersection of point i with all
206; quadirlateral.
207; row j of test corresponds to all the points localized in cell j
208  test = $
209; (x-x1)
210  ((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x1[*]) $
211; *(y2-y1)
212  *(replicate(1, ntofind)#(y2-y1)[*]) $
213; GE (x2-x1)
214  GE ((replicate(1, ntofind)#(x2-x1)[*]) $
215; *(y-y1)
216  *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y1[*])))
217;-------
218  test = temporary(test) $
219; *(x-x2)
220  *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x2[*]) $
221; *(y3-y2)
222  *(replicate(1, ntofind)#(y3-y2)[*]) $
223; GE (x3-x2)
224  GE ((replicate(1, ntofind)#(x3-x2)[*]) $
225; *(y-y2)
226  *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y2[*])))
227;-------
228  test = temporary(test) $
229; *(x-x3)
230  *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x3[*]) $
231; *(y4-y3)
232  *(replicate(1, ntofind)#(y4-y3)[*]) $
233; GE (x4-x3)
234  GE ((replicate(1, ntofind)#(x4-x3)[*]) $
235; *(y-y3)
236  *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y3[*])))
237;-------
238  test = temporary(test) $
239; *(x-x4)
240  *((x[*]#replicate(1, nquad)-replicate(1, ntofind)#x4[*]) $
241; *(y1-y4)
242  *(replicate(1, ntofind)#(y1-y4)[*]) $
243; GE (x1-x4)
244  GE ((replicate(1, ntofind)#(x1-x4)[*]) $
245; *(y-y4)
246  *(y[*]#replicate(1, nquad)-replicate(1, ntofind)#y4[*])))
247;
248; check test if ntofind gt 1
249; if ntofind gt 1, each point must be localised in one uniq cell.
250  IF ntofind GT 1 THEN BEGIN
251; each column of test must have only 1 position equal to one
252    chtest = total(test, 2)
253; points out of the cells
254    IF (where(chtest EQ 0))[0] NE -1 THEN BEGIN
255      IF NOT keyword_set(noprint) THEN print, 'Points number '+strjoin(strtrim(where(chtest EQ 0), 1), ', ')+' are out of the grid'
256      stop
257    ENDIF
258; points in more than one cell
259    IF (where(chtest GT 1))[0] NE -1 THEN BEGIN
260      IF NOT keyword_set(noprint) THEN print, 'Points number '+strjoin(strtrim(where(chtest GT 1), 1), ', ')+' are in more than one cell'
261      stop
262    ENDIF
263  ENDIF
264; find the points for which test eq 1
265  found = where(temporary(test) EQ 1)
266; if ntofind eq 1, the point may be localised in more than one grid
267; cell ou may also be out of the cells
268  IF ntofind EQ 1 THEN BEGIN
269    CASE 1 OF
270      found[0] EQ -1:BEGIN
271        IF NOT keyword_set(noprint) THEN print, 'The point is out of the cells'
272        IF keyword_set(onsphere) THEN BEGIN
273; restore old !map...
274          !map = save.map
275          !x = save.x
276          !y = save.y
277          !z = save.z
278          !p = save.p
279        ENDIF
280        return,  -1
281      END
282      n_elements(found) GT ntofind:BEGIN
283        IF NOT keyword_set(noprint) THEN print, 'The point is in more than one cell'
284      END
285      ELSE:
286    ENDCASE
287  ENDIF ELSE BEGIN
288; if ntofind GT 1, found must be sorted
289; i position of found. this corresponds to one x,y point
290    forsort = found MOD ntofind
291; j position of found. this corresponds to cell in which is one x,y
292; point
293    found = temporary(found)/ntofind
294; found must be sorted accordind to forsort
295    found = found[sort(forsort)]
296  ENDELSE
297;
298  IF keyword_set(onsphere) THEN BEGIN
299    IF arg_present(newcoord) THEN BEGIN
300      found = found[0]
301      newcoord = [[x1[found], y1[found]] $
302                  , [x2[found], y2[found]] $
303                  , [x3[found], y3[found]] $
304                  , [x4[found], y4[found]] $
305                  , [x, y]]
306    ENDIF
307;
308    found = good[good2[found]]
309; restore the input parameters
310    x = temporary(xin)
311    y = temporary(yin)
312    x1 = temporary(x1in)
313    y1 = temporary(y1in)
314    x2 = temporary(x2in)
315    y2 = temporary(y2in)
316    x3 = temporary(x3in)
317    y3 = temporary(y3in)
318    x4 = temporary(x4in)
319    y4 = temporary(y4in)
320; restore old !map...
321    !map = save.map
322    !x = save.x
323    !y = save.y
324    !z = save.z
325    !p = save.p
326  ENDIF
327;;
328  RETURN, found
329END
Note: See TracBrowser for help on using the repository browser.