1 | ;+ |
---|
2 | ; |
---|
3 | ; @file_comments given - a list of points, (x,y) position |
---|
4 | ; - the x and y limits of a rectangular grid |
---|
5 | ; find in which cell is located each given point. |
---|
6 | ; |
---|
7 | ; @categories no DO loop, use the wonderfull value_locate function! |
---|
8 | ; |
---|
9 | ; @examples |
---|
10 | ; res = inrecgrid(xin, yin, left, bottom) |
---|
11 | ; |
---|
12 | ; @param x1d {in}{required} a 1d array, the x position on the points |
---|
13 | ; @param y1d {in}{required} a 1d array, the y position on the points |
---|
14 | ; left {in}{required} a 1d, monotonically increasing array, the position of the |
---|
15 | ; "left" border of each cell. |
---|
16 | ; @param bottom {in}{required} a 1d, monotonically increasing array, the position of the |
---|
17 | ; "bottom" border of each cell. |
---|
18 | ; |
---|
19 | ; |
---|
20 | ; @keyword /output2d to get the output as a 2d array (2,n_elements(x1d)), |
---|
21 | ; with res[0,*] the x index accoring to the 1d array defined by |
---|
22 | ; left and res[1,*] the y index accoring to the 1d array defined by |
---|
23 | ; bottom. |
---|
24 | ; |
---|
25 | ; @keyword checkout=[rbgrid,ubgrid] specify the right and upper bondaries of |
---|
26 | ; the grid and check if some points are out. |
---|
27 | ; |
---|
28 | ; @returns the index on the cell accoring to the 2d array defined by |
---|
29 | ; left and bottom. |
---|
30 | ; |
---|
31 | ; @examples |
---|
32 | ; |
---|
33 | ; IDL> a=indgen(5) |
---|
34 | ; IDL> b=indgen(7) |
---|
35 | ; IDL> r=inrecgrid([0.25,3.25,2],[4.25,2.8,1.4],a,b) |
---|
36 | ; IDL> print, r |
---|
37 | ; 20 13 7 |
---|
38 | ; IDL> r=inrecgrid([0.25,3.25,2],[4.25,2.8,1.4],a,a+1,b,b+1,/output2d) |
---|
39 | ; IDL> print, r |
---|
40 | ; 0.00000 4.00000 |
---|
41 | ; 3.00000 2.00000 |
---|
42 | ; 2.00000 1.00000 |
---|
43 | ; |
---|
44 | ; @history |
---|
45 | ; S. Masson (smasson\@lodyc.jussieu.fr) |
---|
46 | ; July 3rd, 2002 |
---|
47 | ; October 3rd, 2003: use value_locate |
---|
48 | ;- |
---|
49 | |
---|
50 | FUNCTION inrecgrid, x1d, y1d, left, bottom, output2d = output2d, checkout = checkout |
---|
51 | ; |
---|
52 | ncellx = n_elements(left) |
---|
53 | ncelly = n_elements(bottom) |
---|
54 | ; |
---|
55 | xpos = value_locate(left[*], x1d[*]) |
---|
56 | ypos = value_locate(bottom[*], y1d[*]) |
---|
57 | ; |
---|
58 | IF n_elements(checkout) EQ 2 THEN BEGIN |
---|
59 | out = where(x1d GT checkout[0]) |
---|
60 | IF out[0] NE -1 THEN xpos[out] = -1 |
---|
61 | out = where(y1d GT checkout[1]) |
---|
62 | IF out[0] NE -1 THEN ypos[out] = -1 |
---|
63 | ENDIF |
---|
64 | ; |
---|
65 | IF keyword_set(output2d) THEN return, [transpose(xpos), transpose(ypos)] |
---|
66 | ; |
---|
67 | IF NOT keyword_set(checkout) THEN RETURN, xpos+ncellx*ypos |
---|
68 | ; |
---|
69 | res = xpos+ncellx*ypos |
---|
70 | out = where(xpos EQ -1 OR ypos EQ -1) |
---|
71 | IF out[0] NE -1 THEN res[out] = -1 |
---|
72 | ; |
---|
73 | RETURN, res |
---|
74 | |
---|
75 | END |
---|