source: trunk/SRC/Interpolation/clickincell.pro @ 118

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

add $ in Calendar, Grid, Interpolation, Obsolete and Postscript *.pro files, add svn:keywords Id to all these files, some improvements in header

  • Property svn:executable set to *
  • Property svn:keywords set to Id
File size: 5.3 KB
Line 
1;+
2; @file_comments click on a map and find in which cell the click was
3;
4; @categories finding where is a point on a grid
5;
6; @examples
7;
8;     res = clickincell()
9;     Click with the left button to select a cell. Clicking one more
10;     time in the same cell remove the cell from the selection.
11;     Click on the right button to quit. 
12;
13; @keyword     CELLTYPE = 'T', 'W', 'U', 'V' or 'F' This this the type of point
14;     that is located in the center of the cell which the click is
15;     located. default is T type of cell (with corner defined by F
16;     points).
17;
18; @keyword     /DRAWCELL to draw the cell in which we clicked
19;
20; @keyword     COLOR  the color used to draw the cells (Clicking one more
21;     time in the same cell will draw the cell with the white color)
22;
23; @keyword     /ORIGINAL to get the position of the cell regarding the original
24;     grid (with no key_shift, ixminmesh, iyminmesh...)
25;
26; @keyword     /IJ see outpus
27;
28; @keyword     _EXTRA to pass extra keywords to inquad and plot (when /drawcell)
29;
30; @returns
31;     the index of the selected cells regarding to the grid which
32;     is in memory in the variable of the common. If /ij keyword is
33;     activated give 2D array (2, n) which are the i,j position of the
34;     n selected cells.
35;
36; @uses common.pro
37;
38; @examples
39;
40;   IDL> plt, findgen(jpi,jpj),/nodata,map=[90,0,0],/ortho
41;   IDL> print, clickincell(/draw,color=150,/xy)
42;
43; @history
44;      Sebastien Masson (smasson\@lodyc.jussieu.fr)
45;      August 2003
46;
47; @version $Id$
48;
49;
50;-
51FUNCTION clickincell, CELLTYPE = celltype, DRAWCELL = drawcell, COLOR = color, ORIGINAL = original, IJ = ij, _EXTRA = extra
52;
53  compile_opt idl2, strictarrsubs
54;
55@common
56;
57; initialization
58  cellnum = -1L
59  selected = 0
60;
61; Cell list
62; get the grid parameter according to celltype
63  oldgrid = vargrid
64  IF NOT keyword_set(celltype) THEN celltype = 'T'
65  CASE strupcase(celltype) OF
66    'T':vargrid = 'F'
67    'W':vargrid = 'F'
68    'U':vargrid = 'V'
69    'V':vargrid = 'U'
70    'F':vargrid = 'T'
71  ENDCASE
72  grille, -1, glam, gphi, -1, nx, ny, nz, firstx, firsty, firstz, lastx, lasty, lastz
73  vargrid = oldgrid
74; define the corner of the cells in the clockwise direction
75  IF keyword_set(key_periodic) AND nx EQ jpi THEN BEGIN
76    x1 = glam[*, 0:ny-2]
77    y1 = gphi[*, 0:ny-2]
78    x2 = glam[*, 1:ny-1]
79    y2 = gphi[*, 1:ny-1]
80    x3 = shift(glam[*, 1:ny-1], -1, 0)
81    y3 = shift(gphi[*, 1:ny-1], -1, 0)
82    x4 = shift(glam[*, 0:ny-2], -1, 0)
83    y4 = shift(gphi[*, 0:ny-2], -1, 0)
84  ENDIF ELSE BEGIN
85    x1 = glam[0:nx-2, 0:ny-2]
86    y1 = gphi[0:nx-2, 0:ny-2]
87    x2 = glam[0:nx-2, 1:ny-1]
88    y2 = gphi[0:nx-2, 1:ny-1]
89    x3 = glam[1:nx-1, 1:ny-1]
90    y3 = gphi[1:nx-1, 1:ny-1]
91    x4 = glam[1:nx-1, 0:ny-2]
92    y4 = gphi[1:nx-1, 0:ny-2]
93  ENDELSE
94  glam = -1 ; free memory
95  gphi = -1 ; free memory
96;
97; get mousse position on the reference map
98  cursor, x, y, /data, /up
99;
100  while (!mouse.button ne 4) do BEGIN
101    IF finite(x)*finite(x) EQ 0 THEN GOTO, outwhile
102;
103    case !mouse.button of
104      1:BEGIN
105; What is the longitude?
106        WHILE x GT !x.range[1] DO x = x-360
107        WHILE x LT !x.range[0] DO x = x+360
108        IF x GT !x.range[1] THEN GOTO, outwhile
109        IF y GT !y.range[1] THEN GOTO, outwhile
110        IF y LT !y.range[0] THEN GOTO, outwhile
111;
112        cell = inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4 $
113                      , /onsphere, _extra = extra)
114;
115        IF cell[0] EQ -1 OR n_elements(cell) GT 1 THEN GOTO, outwhile
116        cell = cell[0]
117        already = (where(cellnum EQ cell))[0]
118        IF already EQ -1 THEN BEGIN
119          cellnum = [cellnum, cell]
120          selected = [selected, 1]
121          already = n_elements(selected)-1
122        ENDIF ELSE selected[already] = 1-selected[already]
123        IF keyword_set(drawcell) THEN BEGIN
124          oplot, [x1[cell], x2[cell], x3[cell], x4[cell], x1[cell]] $
125            , [y1[cell], y2[cell], y3[cell], y4[cell], y1[cell]] $
126            , color = color*selected[already] $
127            + (!d.n_colors < 255)*(1-selected[already]) $
128            , _extra = extra
129        ENDIF
130      END
131      2:                        ; middle button
132      ELSE:
133    ENDCASE   
134; get mousse position on the reference map
135outwhile:
136    cursor, x, y, /data, /up
137  ENDWHILE
138;
139  good = where(selected NE 0)
140  IF good[0] EQ -1 THEN RETURN, -1
141;
142  cellnum = cellnum[good]
143;
144  yy = cellnum/(nx-1+key_periodic*(nx EQ jpi))
145  xx = cellnum MOD (nx-1+key_periodic*(nx EQ jpi))
146;
147  CASE strupcase(celltype) OF
148    'T':BEGIN
149      xx = xx+firstx+1
150      yy = yy+firsty+1
151    END
152    'W':BEGIN
153      xx = xx+firstx+1
154      yy = yy+firsty+1
155    END
156    'U':BEGIN
157      xx = xx+firstx
158      yy = yy+firsty+1
159    END
160    'V':BEGIN
161      xx = xx+firstx+1
162      yy = yy+firsty
163    END
164    'F':BEGIN
165      xx = xx+firstx
166      yy = yy+firsty
167    END
168  ENDCASE
169;
170  bad = where(xx GE jpi)
171  IF bad[0] NE -1 THEN BEGIN
172    xx[bad] = xx[bad]-jpi
173    yy[bad] = yy[bad]+1
174  ENDIF
175  bad = where(yy GE jpj)
176  IF bad[0] NE -1 THEN stop
177;
178  IF keyword_set(original) THEN BEGIN
179    xx = xx-key_shift
180    bad = where(xx LT 0)
181    IF bad[0] NE -1 THEN xx[bad] = xx[bad]+jpi
182    xx = xx MOD jpi
183    xx = xx +ixminmesh
184    yy = yy+iyminmesh
185  ENDIF
186;
187  ncell = n_elements(xx)
188  IF keyword_set(ij) THEN $
189    RETURN, [reform(xx, 1, ncell, /over) $
190             , reform(yy, 1, ncell, /over)]
191;
192  IF keyword_set(original) THEN RETURN, xx+jpiglo*yy $
193  ELSE RETURN, xx+jpi*yy
194END
Note: See TracBrowser for help on using the repository browser.