;+
;
; @file_comments
; click on a map and find in which cell the click was
;
; @categories
; Grid
;
; @keyword CELLTYPE
; = 'T', 'W', 'U', 'V' or 'F'
; the type of point
; that is located in the center of the cell which the click is
; located. default is T type of cell (with corner defined by F
; points).
;
; @keyword DRAWCELL
; to draw the cell in which we clicked
;
; @keyword COLOR
; the color used to draw the cells (Clicking one more
; time in the same cell will draw the cell with the white color)
;
; @keyword ORIGINAL
; to get the position of the cell regarding the original
; grid (with no key_shift, ixminmesh, iyminmesh...)
;
; @keyword IJ
; see returns
;
; @keyword _EXTRA
; Used to pass extra keywords to inquad and
; PLOT
; (when /drawcell)
;
; @returns
; the index of the selected cells regarding to the grid which
; is in memory in the variable of the common. If /ij keyword is
; activated give 2D array (2, n) which are the i,j position of the
; n selected cells.
;
; @uses
; common.pro
;
; @examples
; IDL> res = clickincell()
; Click with the left button to select a cell. Clicking one more
; time in the same cell remove the cell from the selection.
; Click on the right button to quit.
;
; IDL> plt, findgen(jpi,jpj),/nodata,map=[90,0,0],/ortho
; IDL> print, clickincell(/draw,color=150,/xy)
;
; @history
; Sebastien Masson (smasson\@lodyc.jussieu.fr)
; August 2003
;
; @version
; $Id$
;
;-
FUNCTION clickincell, CELLTYPE = celltype, DRAWCELL = drawcell, COLOR = color, ORIGINAL = original, IJ = ij, _EXTRA = extra
;
compile_opt idl2, strictarrsubs
;
@common
;
; initialization
cellnum = -1L
selected = 0
;
; Cell list
; get the grid parameter according to celltype
oldgrid = vargrid
IF NOT keyword_set(celltype) THEN celltype = 'T'
CASE strupcase(celltype) OF
'T':vargrid = 'F'
'W':vargrid = 'F'
'U':vargrid = 'V'
'V':vargrid = 'U'
'F':vargrid = 'T'
ENDCASE
grille, -1, glam, gphi, -1, nx, ny, nz, firstx, firsty, firstz, lastx, lasty, lastz
vargrid = oldgrid
; define the corner of the cells in the clockwise direction
IF keyword_set(key_periodic) AND nx EQ jpi THEN BEGIN
x1 = glam[*, 0:ny-2]
y1 = gphi[*, 0:ny-2]
x2 = glam[*, 1:ny-1]
y2 = gphi[*, 1:ny-1]
x3 = shift(glam[*, 1:ny-1], -1, 0)
y3 = shift(gphi[*, 1:ny-1], -1, 0)
x4 = shift(glam[*, 0:ny-2], -1, 0)
y4 = shift(gphi[*, 0:ny-2], -1, 0)
ENDIF ELSE BEGIN
x1 = glam[0:nx-2, 0:ny-2]
y1 = gphi[0:nx-2, 0:ny-2]
x2 = glam[0:nx-2, 1:ny-1]
y2 = gphi[0:nx-2, 1:ny-1]
x3 = glam[1:nx-1, 1:ny-1]
y3 = gphi[1:nx-1, 1:ny-1]
x4 = glam[1:nx-1, 0:ny-2]
y4 = gphi[1:nx-1, 0:ny-2]
ENDELSE
glam = -1 ; free memory
gphi = -1 ; free memory
;
; get mousse position on the reference map
cursor, x, y, /data, /up
;
while (!mouse.button ne 4) do BEGIN
IF finite(x)*finite(x) EQ 0 THEN GOTO, outwhile
;
case !mouse.button of
1:BEGIN
; What is the longitude?
WHILE x GT !x.range[1] DO x = x-360
WHILE x LT !x.range[0] DO x = x+360
IF x GT !x.range[1] THEN GOTO, outwhile
IF y GT !y.range[1] THEN GOTO, outwhile
IF y LT !y.range[0] THEN GOTO, outwhile
;
cell = inquad(x, y, x1, y1, x2, y2, x3, y3, x4, y4 $
, /onsphere, _extra = extra)
;
IF cell[0] EQ -1 OR n_elements(cell) GT 1 THEN GOTO, outwhile
cell = cell[0]
already = (where(cellnum EQ cell))[0]
IF already EQ -1 THEN BEGIN
cellnum = [cellnum, cell]
selected = [selected, 1]
already = n_elements(selected)-1
ENDIF ELSE selected[already] = 1-selected[already]
IF keyword_set(drawcell) THEN BEGIN
oplot, [x1[cell], x2[cell], x3[cell], x4[cell], x1[cell]] $
, [y1[cell], y2[cell], y3[cell], y4[cell], y1[cell]] $
, color = color*selected[already] $
+ (!d.n_colors < 255)*(1-selected[already]) $
, _extra = extra
ENDIF
END
2: ; middle button
ELSE:
ENDCASE
; get mousse position on the reference map
outwhile:
cursor, x, y, /data, /up
ENDWHILE
;
good = where(selected NE 0)
IF good[0] EQ -1 THEN RETURN, -1
;
cellnum = cellnum[good]
;
yy = cellnum/(nx-1+key_periodic*(nx EQ jpi))
xx = cellnum MOD (nx-1+key_periodic*(nx EQ jpi))
;
CASE strupcase(celltype) OF
'T':BEGIN
xx = xx+firstx+1
yy = yy+firsty+1
END
'W':BEGIN
xx = xx+firstx+1
yy = yy+firsty+1
END
'U':BEGIN
xx = xx+firstx
yy = yy+firsty+1
END
'V':BEGIN
xx = xx+firstx+1
yy = yy+firsty
END
'F':BEGIN
xx = xx+firstx
yy = yy+firsty
END
ENDCASE
;
bad = where(xx GE jpi)
IF bad[0] NE -1 THEN BEGIN
xx[bad] = xx[bad]-jpi
yy[bad] = yy[bad]+1
ENDIF
bad = where(yy GE jpj)
IF bad[0] NE -1 THEN stop
;
IF keyword_set(original) THEN BEGIN
xx = xx-key_shift
bad = where(xx LT 0)
IF bad[0] NE -1 THEN xx[bad] = xx[bad]+jpi
xx = xx MOD jpi
xx = xx +ixminmesh
yy = yy+iyminmesh
ENDIF
;
ncell = n_elements(xx)
IF keyword_set(ij) THEN $
RETURN, [reform(xx, 1, ncell, /over) $
, reform(yy, 1, ncell, /over)]
;
IF keyword_set(original) THEN RETURN, xx+jpiglo*yy $
ELSE RETURN, xx+jpi*yy
END