\ cancel mode verify    
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++!
! mp_graticule_limit.jnl --  Overlay a graticule on a map projection.  For working
!                  with map projection scripts in Ferret v4.50
!                               
! Ansley Manke
! 12/16/02
!
! Description:  Overlay a graticule on a map, limited by a range of x and
!               y page coordinates.
!
! Based on mp_graticule.jnl
!
! This journal file comes from equations in:
!  J. P. Snyder & P. M. Voxland, 1989, An Album of Map Projections,
!    U.S. Geological Survey, Professional Paper 1453, U.S. Government
!    Printing Office, Washington, DC, 249 pp. 
!
! This script presumes that following are predefined a projection script
! run previous to this script:
!
!       mp_lambda  longitude in radians
!       mp_phi     latitude in radians
!       x_page  field describing transformation from lat/lon to X page coordinates
!       x_page  field describing transformation from lat/lon to Y page coordinates
!
! Usage:              arg1      arg2      arg3        arg4      arg5      arg6      arg7   arg8    arg9    arg10   arg11
!  go mp_graticule [lon_min] [lon_max] [lon_delta] [lat_min] [lat_max] [lat_delta] [pen] [mp_minxp] [mp_maxxp] [mp_minyp] [mp_maxyp]
!
! arg 1 - minimum longitude
! arg 2 - maximum longitude
! arg 3 - spacing for the longitude graticule
! arg 4 - minimum latitude
! arg 5 - maximum latitude
! arg 6 - spacing for the latitude graticule
! arg 7 - pen number
! arg 8 - minimum x to plot, in page-coordinates  (if these are specified,
! arg 9 - maximum x to plot, in page-coordinates   all four must be provided.)
! arg10 - minimum y to plot, in page-coordinates
! arg11 - maximum y to plot, in page-coordinates
! arg12 - dash characteristics: dash(dn,up,dn,up)
!
! example:  Limit the plot to a square, in terms of plot page coordinates.
!   use etopo60
!   set region/y=0:90
!   go mp_stereographic_north
!   set grid rose
!   go mp_aspect
!   shade/l=1/hlim=-1:1/vlim=-1:1/noax/nolab rose,x_page,y_page
!   go mp_graticule_limit " ", " ", " ", " ", " ", " ", " ", -1,1,-1,1, dash=(.1,.18,.1,.18)
!   plot/over/vs/line/nolabel/color=black {-1,1,1,-1,-1}, {-1,-1,1,1,-1}
!
!
!   ! Note:   If overlaying on a basemap or in any case where a SET GRID has
!         not been issued, the current default region will determine
!	  the region to be plotted.

define region/default save
set grid/save

let/quiet mp_x = x
let/quiet mp_y = y

!   Turn windowing of plot lines off, so the  
!   lines are complete around the edge of the plot.

ppl window off
!
! create reasonable defaults for graticule spacing
!

let/quiet mp_xmin = $1"`mp_x[i=@min]`"
let/quiet mp_xmax = $2"`mp_x[i=@max]`"

if `mp_xmax - mp_xmin ge 180` then
  let/quiet mp_xdel = $3"30"
elif `mp_xmax - mp_xmin ge 60` then
  let/quiet mp_xdel = $3"10"
elif `mp_xmax - mp_xmin ge 10` then
  let/quiet mp_xdel = $3"5"
else
  let/quiet mp_xdel = $3"1"
endif

let/quiet mp_ymin = $4"`mp_y[j=@min]`"
let/quiet mp_ymax = $5"`mp_y[j=@max]`"

if `mp_ymax - mp_ymin ge 180` then
  let/quiet mp_ydel = $6"30"
elif `mp_ymax - mp_ymin ge 60` then
  let/quiet mp_ydel = $6"10"
elif `mp_ymax - mp_ymin ge 10` then
  let/quiet mp_ydel = $6"5"
else
  let/quiet mp_ydel = $6"1"
endif

! parameters to limit the graticule to a rectangle
let/quiet mp_minxp = $8%99%
let/quiet mp_maxxp = $9%99%
let/quiet mp_minyp = $10%99%
let/quiet mp_maxyp = $11%99%

def sym dashline = /$12%dash=(1,0,1,0)%

! This test will keep the central meridians defined by "from space" views
!
let/quiet mp_gtest = `mp_central_meridian` - `(mp_x[i=@max] + mp_x[i=@min])/2`

if `mp_gtest eq 0` then
   let/quiet mp_central_meridian = `(mp_x[i=@max] + mp_x[i=@min])/2`
endif

let/quiet mp_gtest = `mp_standard_parallel` - `(mp_y[j=@max] + mp_y[j=@min])/2`

if `mp_gtest eq 0` then
   let/quiet mp_standard_parallel = `(mp_y[j=@max] + mp_y[j=@min])/2`
endif

cancel variable mp_gtest

let/quiet mp_std_parallel_north = `mp_y[j=@max]`
let/quiet mp_std_parallel_south = `mp_y[j=@min]`

!
! Now for the appropriate latitude lines:
!   Define a grid with the 'fast' index being longitude
!   Have the axis be one point 'too long' and make the last point a bad value to avoid 'sweep-back'
!   Set the region appropriately
!   Redefine mp_lambda and mp_phi, mask and plot
!
!  ACM  6/01
! Ned's  def uses mp_xdel.  This can be too coarse and results in a spider-web 
! effect on projection plots over large areas
!define axis/x=`mp_xmin`:`mp_xmax + mp_xdel`:`mp_xdel` lon_field_i_axis

! Use delta of 1 as in Jon's original script, or mp_xdel if its smaller
let/quiet mp_xaxdel = MIN(mp_xdel, 1)
define axis/x=`mp_xmin`:`mp_xmax + mp_xaxdel`:`mp_xaxdel` lon_field_i_axis
define axis/y=`mp_ymin`:`mp_ymax`:`mp_ydel` lon_field_j_axis

let i_index = i[gx=lon_field_i_axis]
let j_index = j[gy=lon_field_j_axis]

let i_index_max = i_index[i=@max]
let j_index_max = j_index[j=@max]

set region/x=`mp_xmin`:`mp_xmax + mp_xaxdel`/y=`mp_ymin`:`mp_ymax`

define grid/x=lon_field_i_axis/y=lon_field_j_axis lon_field_grid

let/quiet lon_field_i = x[g=lon_field_grid] + 0*y[g=lon_field_grid]
let/quiet lon_field_j = 0*x[g=lon_field_grid] + y[g=lon_field_grid]
let/quiet lon_field_i_2 = if i[g=lon_field_grid] ne i_index_max then lon_field_i

let/quiet mp_lambda = lon_field_i_2 * deg2rad
let/quiet mp_phi    = lon_field_j   * deg2rad

let/quiet masked_x_page = mp_mask * x_page
let/quiet masked_y_page = mp_mask * y_page

! If requested, limit the lines to a box defined by the last 4 arguments.


let ok1 = if `mp_minxp NE 99` then 1
let ok2 = if `mp_maxxp NE 99` then 1
let ok3 = if `mp_minyp NE 99` then 1
let ok4 = if `mp_maxyp NE 99` then 1
let ok = ok1 + ok2 + ok3 + ok4

IF `ok eq 4` THEN
  let/quiet limit_x_page_a = if masked_x_page ge mp_minxp then masked_x_page
  let/quiet limit_y_page_a = if masked_y_page ge mp_minyp then masked_y_page

  let/quiet limit_x_page = if masked_x_page LE mp_maxxp then limit_x_page_a
  let/quiet limit_y_page = if masked_y_page LE mp_maxyp then limit_y_page_a
ELSE
  let limit_x_page - masked_x_page
  let limit_y_page = masked_y_page
ENDIF

plot/vs/over/nolab/line=$7"1"($dashline) limit_x_page, limit_y_page

!
! Now for the appropriate longitude lines:
!
! ACM 6/2001 choose smaller delta here too.
!define axis/x=`mp_ymin`:`mp_ymax + mp_ydel`:`mp_ydel` lat_field_i_axis

let/quiet mp_yaxdel = MIN(mp_ydel,1)
define axis/x=`mp_ymin`:`mp_ymax + mp_yaxdel`:`mp_yaxdel` lat_field_i_axis
define axis/y=`mp_xmin`:`mp_xmax`:`mp_xdel` lat_field_j_axis

let i_index = i[gx=lat_field_i_axis]
let j_index = j[gy=lat_field_j_axis]

set region/x=`mp_ymin`:`mp_ymax + mp_yaxdel`/y=`mp_xmin`:`mp_xmax`

define grid/x=lat_field_i_axis/y=lat_field_j_axis lat_field_grid

let/quiet lat_field_i = x[g=lat_field_grid] + 0*y[g=lat_field_grid]
let/quiet lat_field_j = 0*x[g=lat_field_grid] + y[g=lat_field_grid]
let/quiet lat_field_i_2 = if i[g=lat_field_grid] ne i_index_max then lat_field_i

let/quiet mp_lambda = lat_field_j   * deg2rad
!let/quiet mp_phi    = lat_field_i_2 * deg2rad

let/quiet mp_phi    = lat_field_i_2[g=lat_field_grid] * deg2rad	! acm - like Jons original

let/quiet masked_x_page = mp_mask * x_page
let/quiet masked_y_page = mp_mask * y_page


IF `ok` THEN
  let/quiet limit_x_page_a = if masked_x_page ge mp_minxp then masked_x_page
  let/quiet limit_y_page_a = if masked_y_page ge mp_minyp then masked_y_page

  let/quiet limit_x_page = if masked_x_page LE mp_maxxp then limit_x_page_a
  let/quiet limit_y_page = if masked_y_page LE mp_maxxp then limit_y_page_a
ELSE
  let limit_x_page - masked_x_page
  let limit_y_page = masked_y_page
ENDIF

plot/vs/over/nolab/line=$7"1"($dashline) limit_x_page, limit_y_page


!
! Restore all the previous settings and variables
!
set region save
set grid/restore

ppl window on

let/quiet mp_x = x
let/quiet mp_y = y

let/quiet mp_gtest = `mp_central_meridian` - `(mp_x[i=@max] + mp_x[i=@min])/2`

if `mp_gtest eq 0` then
   let/quiet mp_central_meridian = (mp_x[i=@max] + mp_x[i=@min])/2
endif

let/quiet mp_gtest = `mp_standard_parallel` - `(mp_y[j=@max] + mp_y[j=@min])/2`
if `mp_gtest eq 0` then
   let/quiet mp_standard_parallel = (mp_y[j=@max] + mp_y[j=@min])/2
endif

cancel variable mp_gtest

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
