
\ cancel mode verify
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! mp_annotate.jnl --  Map projection version of annotate command. For
!                     working with map projection scripts in Ferret v7
!		   		
! Olivier Marti
! 07/2019

! Description:  write text on a predefined map projection. 
!
! This script presumes that following are predefined a projection script
! run previous to this script:
!
!	x_page	field describing transformation from lat/lon to X page coordinates
!	x_page	field describing transformation from lat/lon to Y page coordinates
!	mp_mask  mask for the map projection
!
! Usage:               $1   $2   $3   $4 
! Yes?  GO mp_annotate xpos ypos text [annotate_qualifiers]
! $1 - X coordinates of text (longitude)
! $2 - X coordinates of text (latitude)
! $3 - Texte
! $4 - The annotate qualifiers (eg. halign/valign/angle/size)
!
! Example:
!     set win/size=.4
!     use coads_climatology
!     set region/x=130:250/y=20:64/l=1
!     go mp_hammer
!     set grid sst
!     shade/noaxis sst, x_page, y_page
!     go mp_land
!     
!     use polydata
!     go mp_polymark poly/over/noaxis/key lon lat sst star 0.5
!     go mp_annotate 180 40 "SST" /halign=0/valign=0/size=0.3
!
! Notes:
!     Mostly based on mp_polymark by Jonathan Callahan (Thanks !)  
!
! Argument check
query/ignore $1"<usage: yes? GO mp_annotate xpos ypos text [annotate_qualifiers]"
query/ignore $2"<usage: yes? GO mp_annotate xpos ypos text [annotate_qualifiers]"
query/ignore $3"<usage: yes? GO mp_annotate xpos ypos text [annotate_qualifiers]"


define symbol MeQual   = $4"halign=0/valign=0/angle=0"
! Add a leading / to MeQual if needed
IF ($MeQual"false|*>true") THEN 
   IF `strrindex ( "($MeQual)", "/" ) NE 1` THEN DEFINE SYMBOL MeQual = /($MeQual)
ENDIF

!message/cont "If this script does not complete you will need to rerun the map projection script."
!
! Save the region and grid
!
define region/default save
set grid/save

!  acm (as in mp_land etc)
!  tie down mp_central_meridian and parallel or else they get redefined
!  when we cancel the region and use the one implied by lon and lat that 
!  come into this script.

set data/save
set grid/save

let/quiet mp_x = x
let/quiet mp_xmin = `mp_x[i=@min]`
let/quiet mp_xmax = `mp_x[i=@max]`
let/quiet mp_y = y
let/quiet mp_ymin = `mp_y[j=@min]`
let/quiet mp_ymax = `mp_y[j=@max]`
let/quiet mp_std_parallel_north = mp_ymax
let/quiet mp_std_parallel_south = mp_ymin

!
! This test will keep the central meridians defined by "from space" views
!
let/quiet mp_test = `mp_central_meridian` - `(mp_x[i=@max] + mp_x[i=@min])/2`
if `mp_test eq 0` then
   let/quiet mp_central_meridian = `(mp_x[i=@max] + mp_x[i=@min])/2`
endif
let/quiet mp_test = `mp_standard_parallel` - `(mp_y[j=@max] + mp_y[j=@min])/2`
if `mp_test eq 0` then
   let/quiet mp_standard_parallel = `(mp_y[j=@max] + mp_y[j=@min])/2`
endif
cancel variable mp_test


! Cancel the region because the I index info is used to restrict
! the points to be plotted as a line.
!
cancel region/x/y

!
! Alter the map projection equations to use xpos and ypos
!
let/quiet mp_x = $1
let/quiet mp_y = $2

annonate/user/xpos=`x_page*mp_mask`/ypos=`y_page*mp_mask`($MeQual) $3

!
! Restore the region, grid and map projection equations
!
set region save
set grid/restore


let/quiet mp_x = x
let/quiet mp_y = y
let/quiet mp_test = `mp_central_meridian` - `(mp_x[i=@max] + mp_x[i=@min])/2`
if `mp_test eq 0` then
   let/quiet mp_central_meridian = (mp_x[i=@max] + mp_x[i=@min])/2
endif
let/quiet mp_test = `mp_standard_parallel` - `(mp_y[j=@max] + mp_y[j=@min])/2`
if `mp_test eq 0` then
   let/quiet mp_standard_parallel = (mp_y[j=@max] + mp_y[j=@min])/2
endif
cancel variable mp_test
let/quiet mp_std_parallel_north = mp_y[j=@max]
let/quiet mp_std_parallel_south = mp_y[j=@min]
let/quiet mp_lambda = mp_x * deg2rad
let/quiet mp_phi = mp_y * deg2rad


!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
set mode/last verify


