\cancel mode verify
! bullseye.jnl

! Description: locate and show a bullseye in a region of  data

! Given a 2D XY field to search, and approximate location, and (optionally)
! the height and width of a rectangle to search, this routine will locate
! an extrema, mark the search box and the extreme point in black, and report
! the extreme value, its location, and a 7x7 table of surrounding values
! If no extremum is found the rectangle is marked in red.
! The argrment "output" allows control if no plotting is desired.

! modified 4/29/97

! Usage:		arg 1	2   3     4      5    	 6
!	GO bullseye expression xpt ypt [xwid] [ywid] [output]

!  arg 1 (expression)	- name of variable or expression with bullseye
!  arg 2 (xpt)		- approximate X location of bull's eye
!  arg 3 (ypt)		- approximate Y location of bull's eye
!  arg 4 (xwid)		- width of X box to search (default=10 degrees)
!  arg 5 (ywid)		- width of Y box to search (default=10 degrees)
!  arg 6 (output)	- "LIST", "PLOT", or "BOTH" (default=BOTH)

! example (try it):
!	yes? USE ocean_atlas_annual
!	(yes? CANCEL MODE LONGITUDE	! optional )
!	yes? SHADE  po4[k=1]		! Note Bull's eye at approx 30w 60S
!	yes? GO bullseye po4[k=1] 175e 50n	! simple success

!	yes? GO bullseye po4[k=1] 55e 20n 20 20	! OK with missing data inside

!	yes? GO bullseye po4[k=1] 173e 50n	! failure -- extremum at edge

! test validity of arguments
query/ignore $1%<Usage: GO bullseye expression X=lo:hi  Y=lo:hi%
query/ignore $2%<Usage: GO bullseye expression X=lo:hi  Y=lo:hi%
query/ignore $3%<Usage: GO bullseye expression X=lo:hi  Y=lo:hi%
define sym choice $6"0|LIST>1|PLOT>-1|BOTH>0|<Argument 6 must be LIST, PLOT, or BOTH" ! "list"=1, "plot"=-1, "both"=0

! check for a MAX in region
define symbol extreme MAXIMUM
GO find_bullseye $1 $2 $3 $4"10" $5"10" MAX

! Nope?  Then check for a MIN, instead
IF `($success) NE 1` THEN
  define symbol extreme MINIMUM
  GO find_bullseye $1 $2 $3 $4"10" $5"10" MIN
ENDIF

!========================
! If no success report failure and exit
IF `($success) NE 1` THEN
  SAY *****************************************************************
  SAY ***                                                           ***
  SAY *** Search failed:    ($failure_reason)
  SAY ***                                                           ***
  SAY *****************************************************************
  SAY *** (Ignore the following message about the Unclosed IF statement)
  IF `($choice) GE 0` THEN
    GO bullseye_box 2      ! red box shows failure region
  ENDIF
  EXIT
ENDIF
!========================

! SUCCESS!!
! announce the results
say **********************************************************************
SAY ****                                                         ****
say **** Bullseye=`field[I=($iloc),J=($jloc)]` (($extreme)) at X=($xloc) (I=($iloc)), Y=`yloc` (J=($jloc)) ****
SAY ****                                                         ****
say **********************************************************************

! list a 7 by 7 block of the field centered on the bullseye 
IF `($choice) GE 0` THEN
   SAY LIST/I=`($iloc)-3`:`($iloc)+3`/J=`($jloc)-3`:`($jloc)+3` field
   LIST/I=`($iloc)-3`:`($iloc)+3`/J=`($jloc)-3`:`($jloc)+3` field
ENDIF

! plot the bulls eye and a box around it
IF `($choice) LE 0` THEN
   GO bullseye_box 1   			   	! black box
   PLOT/VS/OVER/NOLABEL/SYMB=2 ($xloc), ($yloc)	! mark the extremum
ENDIF

! clean up
set mode/last verify

