\cancel mode verify	
! overlaying coastal outline using detailed geo_borders_intermed data.

! Description: Plot outlines of continents, countries, and/or states
! History: Simplified 6/11/2012 to remove SET GRID commands, and to
!	   handle longitude replications differently

! usage:   go mp_land_detail arg1 [arg2] [arg3] [arg4] [arg5] [arg6] [arg7]
!
!    arg1  continent_pen  
!    arg2 "basemap" "overlay", or omitted for overlay
!    arg3 country_pen	Pen for optional national boundaries 
!    arg4 state_pen	Pen for optional state boundaries (Western hemisphere)
!    arg5 rivers_pen	Pen for largest rivers
!    arg6 more_rivers	Pen for additional rivers 
!    arg7 marine boundaries  Pen for marine boundaries in South Pacific, Bering Strait
!
! pens 	may be "0"(background), "1"(foreground), "thick"(same as pen 7),
!	"black", "red", "green", "blue", or 2-18.
! arg 2 may be "basemap", "overlay", "fast" (fast, 20e:20e overlay) or omitted

!	Note 1: A pen color of "black" will actually pen number 1
!		-- normally white if the window background is black
!	Note 2: An "N" for the continent pen means no continents
!		-- use this for the special case of geo-political
!		   overlay without continents.
! Example calls:
!   GO mp_land_detail red	     - overlay continents using red pen (2)
!   GO mp_land_detail  10           - overlay continents using pen number 10
!   GO mp_land_detail 1 basemap     - basemap of continents outline in foreground color
!   GO mp_land_detail 1 " " red     - overlay continents with countries in red
!   GO mp_land_detail thick basemap black black - continents, countries, and states
!   GO mp_land_detail thick " " red green blue lightblue purple - continents, countries, states, 
!                                                 all rivers, and marine boundaries
!   GO mp_land_detail N " " 1 1     - overlay: no continents; yes political boundaries
! 
! Example:
!   USE coads_climatology
!   SET REGION/X=0:360/Y=-90:90/L=1
!   GO mp_orthographic 210 45
!   SET GRID sst
!   GO mp_aspect
!   LET masked_sst = sst * mp_mask
!   FILL/TITLE="View From Space"/NOAXES masked_sst, x_page, y_page
!   GO mp_fland
!   GO mp_land_detail thick overlay red green blue lightblue purple
!
! Notes:
!  1) This script is SLOW! It computes the map projection for all the 
!       boundary data, even when a small subset is being plotted.
!  2) If a pen value is given for the country_pen or state_pen then the
!	corresponding geopolitical boundaries will be drawn - else
!	they will be omitted
!  3) use  GO line_samples    to view the first 6 pen numbers
!       Higher pen numbers cycle with thicker lines (max 18)
!  3) For more control over basemaps use "GO basemap"

! test argument before performing any actions

query/ignore $1%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%
query/ignore $2%default|basemap|overlay|<argument 2 can be "basemap", "overlay"%
query/ignore $3%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%
query/ignore $4%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%
query/ignore $5%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%
query/ignore $6%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%
query/ignore $7%1|black|red|green|blue|lightblue|purple|white|thick|0|1|2|3|4|5|6|7|8|9|10|11|12|13|14|15|16|17|18|19|N|<go land_detail [pen or "N"] ["basemap"] [pen] [pen] with pen = 0(back), 1(fore), thick, 2-18 or a color%

! introduce the outline data set
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

define region/default save
cancel region

use geo_borders_intermed


! Initialize
let landi_addx = 0
let landi_datxmin = continent_lon[x=@min]
let landi_datxmax = continent_lon[x=@max]

let landi_basemap = $2"0|basemap>1|*>0"
IF `landi_basemap EQ 0` THEN

   IF `mp_xmin LT landi_datxmin` then let landi_addx = -360
   IF `mp_xmax GT landi_datxmax` then let landi_addx = 360

ENDIF

! always do the continents

! Add replication of longitudes if necessary to match whats on the map.
IF `landi_addx NE 0` THEN 
   SET VAR/NAME=continent_lon_in continent_lon
   SET VAR/NAME=continent_lat_in continent_lat
   LET continent_lon = XCAT(continent_lon_in, continent_lon_in + (landi_addx))
   LET continent_lat = XCAT(continent_lat_in, continent_lat_in)
ENDIF

LET/QUIET mp_x = x[gx=continent_lon]
LET/QUIET mp_y = y[gy=continent_lon]

LET/QUIET mp_lambda = continent_lon * deg2rad
LET/QUIET mp_phi = continent_lat * deg2rad
LET/QUIET masked_x_page = mp_mask * x_page
LET/QUIET masked_y_page = mp_mask * y_page

$1"plot|N>QUERY/IGNORE stuff |*>plot"/set/vs/nolab/noax/line=1/$2"i=1:200826/overlay|basemap>i=1:200826/overly|*>i=1:200826/overlay" masked_x_page, masked_y_page

! select line style and plot
ppl pen 1 $1"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|N>1|"

! draw it - as an overlay or as a basemap

$1"ppl plot|N>QUERY/IGNORE stuff |*>ppl plot"$2"/overlay|basemap> |*>/overlay"

CANCEL VAR continent*


! **** now the optional country borders
IF ($3%0|*>1%) THEN

! Add replication of longitudes if necessary to match whats on the map.
   IF `landi_addx NE 0` THEN 
      SET VAR/NAME=country_lon_in country_lon
      SET VAR/NAME=country_lat_in country_lat
      LET country_lon = XCAT(country_lon_in, country_lon_in + (landi_addx))
      LET country_lat = XCAT(country_lat_in, country_lat_in)
   ENDIF

   LET/QUIET mp_x = x[gx=country_lon]
   LET/QUIET mp_y = y[gy=country_lon]

   LET/QUIET mp_lambda = country_lon * deg2rad
   LET/QUIET mp_phi = country_lat * deg2rad

   LET/QUIET masked_x_page = mp_mask * x_page
   LET/QUIET masked_y_page = mp_mask * y_page
   $3"query/ignore|*>plot/set/vs/nolab/line=1/overlay" masked_x_page, masked_y_page

! select line style and plot
   ppl pen 1 $3"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|"

! draw it - as an overlay or as a basemap
   $3"query/ignore|*>ppl plot/overlay"

   CANCEL VAR country*
ENDIF

! **** now the optional state borders
IF ($4%0|*>1%) THEN

   IF `landi_addx NE 0` THEN 
      SET VAR/NAME=state_lon_in state_lon
      SET VAR/NAME=state_lat_in state_lat
      LET state_lon = XCAT(state_lon_in, state_lon_in + (landi_addx))
      LET state_lat = XCAT(state_lat_in, state_lat_in)
   ENDIF

   LET/QUIET mp_x = x[gx=state_lon]
   LET/QUIET mp_y = y[gy=state_lon]

   LET/QUIET mp_lambda = state_lon * deg2rad
   LET/QUIET mp_phi = state_lat * deg2rad
   
   LET/QUIET masked_x_page = mp_mask * x_page
   LET/QUIET masked_y_page = mp_mask * y_page
   $4"query/ignore|*>plot/set/vs/nolab/line=1/overlay" masked_x_page, masked_y_page

! select line style and plot
   ppl pen 1 $4"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|"

! draw it - as an overlay
   $4"query/ignore|*>ppl plot/overlay"

   CANCEL VAR state*
ENDIF


! **** now the optional rivers (secondary ones first)
IF ($6%0|*>1%) THEN

   IF `landi_addx NE 0` THEN 
      SET VAR/NAME=river1_2_lon_in river1_2_lon
      SET VAR/NAME=river1_2_lat_in river1_2_lat
      LET river1_2_lon = XCAT(river1_2_lon_in, river1_2_lon_in + (landi_addx))
      LET river1_2_lat = XCAT(river1_2_lat_in, river1_2_lat_in)
   ENDIF

   LET/QUIET mp_x = x[gx=river1_2_lon]
   LET/QUIET mp_y = y[gy=river1_2_lon]

   LET/QUIET mp_lambda = river1_2_lon * deg2rad
   LET/QUIET mp_phi = river1_2_lat * deg2rad

   LET/QUIET masked_x_page = mp_mask * x_page
   LET/QUIET masked_y_page = mp_mask * y_page

   $6"query/ignore|*>plot/set/vs/nolab/line=1/overlay" masked_x_page, masked_y_page

! select line style and plot
   ppl pen 1 $6"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|"

! draw it - as an overlay or as a basemap
   $6"query/ignore|*>ppl plot/overlay"

   CANCEL VAR river1_2*
ENDIF


! **** now the optional rivers (primary ones second)
IF ($5%0|*>1%) THEN

   IF `landi_addx NE 0` THEN 
      SET VAR/NAME=river1_lon_in river1_lon
      SET VAR/NAME=river1_lat_in river1_lat
      LET river1_lon = XCAT(river1_lon_in, river1_lon_in + (landi_addx))
      LET river1_lat = XCAT(river1_lat_in, river1_lat_in)
   ENDIF

   LET/QUIET mp_x = x[gx=river1_lon]
   LET/QUIET mp_y = y[gy=river1_lon]

   LET/QUIET mp_lambda = river1_lon * deg2rad
   LET/QUIET mp_phi = river1_lat * deg2rad
   
   LET/QUIET masked_x_page = mp_mask * x_page
   LET/QUIET masked_y_page = mp_mask * y_page
$5"query/ignore|*>plot/set/vs/nolab/line=1/overlay" masked_x_page, masked_y_page

! select line style and plot
ppl pen 1 $5"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|"

! draw it - as an overlay or as a basemap
$5"query/ignore|*>ppl plot/overlay"

   CANCEL VAR river1*
ENDIF


! **** now the optional marine boundaries

IF ($7%0|*>1%) THEN

   IF `landi_addx NE 0` THEN 
      SET VAR/NAME=marine_lon_in marine_lon
      SET VAR/NAME=marine_lat_in marine_lat
      LET marine_lon = XCAT(marine_lon_in, marine_lon_in + (landi_addx))
      LET marine_lat = XCAT(marine_lat_in, marine_lat_in)
   ENDIF

   LET/QUIET mp_x = x[gx=marine_lon]
   LET/QUIET mp_y = y[gy=marine_lon]

   LET/QUIET mp_lambda = marine_lon * deg2rad
   LET/QUIET mp_phi = marine_lat * deg2rad
   
   LET/QUIET masked_x_page = mp_mask * x_page
   LET/QUIET masked_y_page = mp_mask * y_page

   $7"query/ignore|*>plot/set/vs/nolab/line=1/overlay" masked_x_page, masked_y_page

! select line style and plot
   ppl pen 1 $7"1|black>1|red>2|green>3|blue>4|lightblue>5|purple>6|white>19|thick>7|0>0|1>1|2>2|3>3|4>4|5>5|6>6|7>7|8>8|9>9|10>10|11>11|12>12|13>13|14>14|15>15|16>16|17>17|18>18|"

! draw it - as an overlay or as a basemap
   $7"query/ignore|*>ppl plot/overlay"

   CANCEL VAR marine*
ENDIF


cancel data geo_borders_intermed

set grid/restore
set data/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 region save
set mode/last verify

