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

Last change on this file since 114 was 114, checked in by smasson, 18 years ago

new compilation options (compile_opt idl2, strictarrsubs) in each routine

  • Property svn:executable set to *
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;-
48FUNCTION clickincell, CELLTYPE = celltype, DRAWCELL = drawcell, COLOR = color, ORIGINAL = original, IJ = ij, _EXTRA = extra
49;
50  compile_opt idl2, strictarrsubs
51;
52@common
53;
54; initialization
55  cellnum = -1L
56  selected = 0
57;
58; Cell list
59; get the grid parameter according to celltype
60  oldgrid = vargrid
61  IF NOT keyword_set(celltype) THEN celltype = 'T'
62  CASE strupcase(celltype) OF
63    'T':vargrid = 'F'
64    'W':vargrid = 'F'
65    'U':vargrid = 'V'
66    'V':vargrid = 'U'
67    'F':vargrid = 'T'
68  ENDCASE
69  grille, -1, glam, gphi, -1, nx, ny, nz, firstx, firsty, firstz, lastx, lasty, lastz
70  vargrid = oldgrid
71; define the corner of the cells in the clockwise direction
72  IF keyword_set(key_periodic) AND nx EQ jpi THEN BEGIN
73    x1 = glam[*, 0:ny-2]
74    y1 = gphi[*, 0:ny-2]
75    x2 = glam[*, 1:ny-1]
76    y2 = gphi[*, 1:ny-1]
77    x3 = shift(glam[*, 1:ny-1], -1, 0)
78    y3 = shift(gphi[*, 1:ny-1], -1, 0)
79    x4 = shift(glam[*, 0:ny-2], -1, 0)
80    y4 = shift(gphi[*, 0:ny-2], -1, 0)
81  ENDIF ELSE BEGIN
82    x1 = glam[0:nx-2, 0:ny-2]
83    y1 = gphi[0:nx-2, 0:ny-2]
84    x2 = glam[0:nx-2, 1:ny-1]
85    y2 = gphi[0:nx-2, 1:ny-1]
86    x3 = glam[1:nx-1, 1:ny-1]
87    y3 = gphi[1:nx-1, 1:ny-1]
88    x4 = glam[1:nx-1, 0:ny-2]
89    y4 = gphi[1:nx-1, 0:ny-2]
90  ENDELSE
91  glam = -1 ; free memory
92  gphi = -1 ; free memory
93;
94; get mousse position on the reference map
95  cursor, x, y, /data, /up
96;
97  while (!mouse.button ne 4) do BEGIN
98    IF finite(x)*finite(x) EQ 0 THEN GOTO, outwhile
99;
100    case !mouse.button of
101      1:BEGIN
102; What is the longitude?
103        WHILE x GT !x.range[1] DO x = x-360
104        WHILE x LT !x.range[0] DO x = x+360
105        IF x GT !x.range[1] THEN GOTO, outwhile
106        IF y GT !y.range[1] THEN GOTO, outwhile
107        IF y LT !y.range[0] THEN GOTO, outwhile
108;
109        cell = inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4 $
110                      , /onsphere, _extra = extra)
111;
112        IF cell[0] EQ -1 OR n_elements(cell) GT 1 THEN GOTO, outwhile
113        cell = cell[0]
114        already = (where(cellnum EQ cell))[0]
115        IF already EQ -1 THEN BEGIN
116          cellnum = [cellnum, cell]
117          selected = [selected, 1]
118          already = n_elements(selected)-1
119        ENDIF ELSE selected[already] = 1-selected[already]
120        IF keyword_set(drawcell) THEN BEGIN
121          oplot, [x1[cell], x2[cell], x3[cell], x4[cell], x1[cell]] $
122            , [y1[cell], y2[cell], y3[cell], y4[cell], y1[cell]] $
123            , color = color*selected[already] $
124            + (!d.n_colors < 255)*(1-selected[already]) $
125            , _extra = extra
126        ENDIF
127      END
128      2:                        ; middle button
129      ELSE:
130    ENDCASE   
131; get mousse position on the reference map
132outwhile:
133    cursor, x, y, /data, /up
134  ENDWHILE
135;
136  good = where(selected NE 0)
137  IF good[0] EQ -1 THEN RETURN, -1
138;
139  cellnum = cellnum[good]
140;
141  yy = cellnum/(nx-1+key_periodic*(nx EQ jpi))
142  xx = cellnum MOD (nx-1+key_periodic*(nx EQ jpi))
143;
144  CASE strupcase(celltype) OF
145    'T':BEGIN
146      xx = xx+firstx+1
147      yy = yy+firsty+1
148    END
149    'W':BEGIN
150      xx = xx+firstx+1
151      yy = yy+firsty+1
152    END
153    'U':BEGIN
154      xx = xx+firstx
155      yy = yy+firsty+1
156    END
157    'V':BEGIN
158      xx = xx+firstx+1
159      yy = yy+firsty
160    END
161    'F':BEGIN
162      xx = xx+firstx
163      yy = yy+firsty
164    END
165  ENDCASE
166;
167  bad = where(xx GE jpi)
168  IF bad[0] NE -1 THEN BEGIN
169    xx[bad] = xx[bad]-jpi
170    yy[bad] = yy[bad]+1
171  ENDIF
172  bad = where(yy GE jpj)
173  IF bad[0] NE -1 THEN stop
174;
175  IF keyword_set(original) THEN BEGIN
176    xx = xx-key_shift
177    bad = where(xx LT 0)
178    IF bad[0] NE -1 THEN xx[bad] = xx[bad]+jpi
179    xx = xx MOD jpi
180    xx = xx +ixminmesh
181    yy = yy+iyminmesh
182  ENDIF
183;
184  ncell = n_elements(xx)
185  IF keyword_set(ij) THEN $
186    RETURN, [reform(xx, 1, ncell, /over) $
187             , reform(yy, 1, ncell, /over)]
188;
189  IF keyword_set(original) THEN RETURN, xx+jpiglo*yy $
190  ELSE RETURN, xx+jpi*yy
191END
Note: See TracBrowser for help on using the repository browser.