\ cancel mode verify
!
! DESCRIPTION: GO file to make multiple viewports with 
!                    1. desired size/position.
!                    2. four corner boxes (per viewport) for labeling
!                    3. polygon commands to choose background color
!                          and boundary line properties for corner boxes.
!
! REQUIRES : Ferret version 5.7 or higher (which supports /AXES qualifier
!                to  "define view" and /RANGE qualifier to "REPEAT" commands.
!
! Some typical bugs/problems and theirs fixes
! --------------------------------------
!   1  segmentation fault : If you face this, and if you have a 
!          "set mode metafile" command, then add a "cancel mode meta" towards 
!          the end of the script. See
!          http://ferret.pmel.noaa.gov/Ferret/Mail_Archives/fu_2006/msg00080.html
!
!   2  /SIGDIG qualifier to the CONTOUR command may not be effective
!          sometimes. Then use the 4th argument to /LEVELS qualifier
!              /LEVELS=(lo, hi, delta, ndigits)  & the usage is 
!              ndigits = -1 for integer format
!                      = -3 to omit numerical labels 
!                      = 1/2/ for valid precision/significant digits
!
!   3  blank/white regions at edges of viewport : do any of the following
!             - tune /hlimits or /vlimits
!             - tune "set reg" command (set is slightly bigger than hlimits
!                                         or vlimits)
!             - first shade and then overlay with fill
!             - just repeat the script once again, in the same ferret session.
!          Also see mails, starting with this one : 
!          http://www.pmel.noaa.gov/maillists/tmap/ferret_users/fu_2005/msg00148.html
!
!   4  some times polygone commands ( ($polf) of ($poll) ) does not work. Then
!             - issue the polygon command twice. See example 2 below.
!
!   5. In some cases the polygon command will change colors of already drawn plots,
!          on the screen (especially if you use rgb_colors go file). However, the
!          metafile and resulting ps file will be just fine, but the gif file 
!          (FRAME/FILE=mygiffile.gif) will be in bad shape.
!
! NOTES
!      1.  To define two sets of viewports with similar features (position & size), 
!               call multi_view two times with different values ONLY for the 9th 
!               argument. See example 2 below.
!
!      2.  The ordering of viewports is shown towards the end of these comments. 
!
!      3.  The corer box will have suffixes like  ("*" is the viewport name).
!                      *_bl  : bottom left
!                      *_br  : bottom right
!                      *_tr  : top    right
!                      *_tl  : top    left
!
!               To put labels inside the corner boxes, use the predefined polygon
!               commands ( ($polfl)  and ($poll) ). Remember that: 1. Xaxis and 
!               Yaxis varies from 1 to 10, 2. xpol and ypol are variables to polygon
!               command
!
!               set view v11_bl
!                   ($polfl)/palette=white/color=1  xpol, ypol   ! white background 
!                   label 4.5, 4.5, 0, 0, 0.35 @C010(a)
! 
!      4.  While using ($polfl), be careful not to give any commands which will
!               alter the default ferret behavior (like using palette within /SET 
!               environment and commands like "ppl tics" etc.).
!
! Default   1 : Just "go multi_view" (ie without any arguments) will define 15 
! Behavior       viewports in 5 rows and 3 columns with names v11, v12,...v53
!
!           2 : There is no mandatory arguments for this go file ; all
!                 arguments are optional.
!
!                          $1       $2      $3      $4       $5     $6       $7
! USAGE :  go multi_view [rows],[columns],[xsize],[xstart],[xgap],[ysize],[ystart]
!                        [ygap],[view_name_prefix],[CB_xsize],[CB_ysize] 
!                           $8         $9              $10        $11
!
!             CB -> Corner Box
!
!          rows  : number of rows            columns : number of columns
!          xsize : view size along X         ysize   : view size along Y
!          xstart: xlo for lefternmost view  ystart  : ylo for bottommost view
!          xgap  : spacing of view along X   ygap    : spacing of view along Y 
!          prefix: prefix to view number     CB_xsize : xsize for corner box
!                     default is "V"         CB_ysize : ysize for corner box
!                            
! EXAMPLE  1: ! 6 viewports of similar size  
!               use coads_climatology
!               set reg/l=1/x=30:120/y=-30:30
!               set window/aspect=1.3
!               go multi_view 5, 3, 0.25, 0.1, 0.03, 0.15, 0.10, 0.02, , 0.05,0.035 
!               set view v11 ; ppl axlabp  1,-1 ; fill/nolab/line sst
!               set view v12 ; ppl axlabp  1, 0 ; fill/nolab/line sst
!               set view v13 ; ppl axlabp  1, 1 ; fill/nolab/line sst
!               set view v21 ; ppl axlabp -1,-1 ; fill/nolab/line sst
!               set view v22 ; ppl axlabp -1, 0 ; fill/nolab/line sst
!               set view v23 ; ppl axlabp -1, 1 ; fill/nolab/line sst
!            !How to use the corner boxes for labeling ?
!            !     polfl --> think about polygon/fill/line
!            !     poll  --> think about polygon/line
!               set view v22_bl 
!                 ($polfl)/pal=white/COLOR=1 xpol, ypol
!                 label 4.3,4.3,0,0,0.35 @C010 (a)
!               set view v23_br 
!                 ($polfl)/palette=black/COLOR=1 xpol, ypol
!                 label 4.3,4.3,0,0,0.35 @C011 (b) 
!
! Example 2: ! 2 sets of viewports, to plot MLD over depth-lat/lon section of temp.
!            use levitus_climatology
!            let zero_at_mld = temp[d=1] - (temp[d=1,k=1]-1)
!            let mld         = zero_at_mld[Z=@LOC:0]
!            go multi_view 1,2,0.30,0.12,0.03,0.35,0.4,0.01,VL,0.045,0.045
!            go multi_view 1,2,0.30,0.12,0.03,0.35,0.4,0.01,VR,0.045,0.045
!            palette=orange_purple ; PPL AXLSZE 0.12,0.12 ;  PPL AXNMTC 1,1
!
!            set view VL11 ; ppl axlabp -1,-1
!              fill/vlim=0:150:20/hlim=54.5:75.5:5/nolab/lev=(15,29,1)/nokey temp[y=6N]
!            set view VR11  
!              plot/vlim=150:0:20/hlim=54.5:75.5:5/nolab/noaxes/color=10     mld[y=6N]
!              set view VL11_bl
!                 ($poll)/color=1 xpol, ypol  ;  ($poll)/color=1 xpol, ypol  
!                 label 4.5,4.5,0,0,0.38 @C008 (a)
!        
!            set view VL12 ; ppl axlabp -1,0
!              fill/vlim=0:150:20/hlim=79.5:100.5:5/nolab/lev=(15,29,1)      temp[y=6S]
!            set view VR12  
!              plot/vlim=150:0:20/hlim=79.5:100.5:5/nolab/noaxes/color=10    mld[y=6S] 
!              set view VL12_bl 
!                ($polfl)/palette=white xpol, ypol ; ($polfl)/palette=white xpol, ypol  
!                label 4.5,4.5,0,0,0.38 @C008 (b) 
!------------------------------------------------------------------------------
!
!                                               +-------+-------+-------+
! Written By    : Jaison Kurian                 !  v11  |  v12  |  v13  |
! Contact       : jaison@caos.iisc.ernet.in     +-------+-------+-------+
!                 jaisonkurian@gmail.com        !  v21  |  v22  |  v23  |
! Version       : multi_view                    +-------+-------+-------+
! Date          : 27-FEB-2006                   !  v31  |  v32  |  v33  |
! Comments      : None                          +-------+-------+-------+
! Modifications : 2                             !  v41  |  v42  |  v43  |
!                                               +-------+-------+-------+
!                                               !  v51  |  v52  |  v53  |
!                                               +-------+-------+-------+
! Modifications :
! ---------------
!    1. On 15/Nov/2006 : Corner boxes for figure labelling have been added
!                           with two additional arguments ($10 & $11).
!
!         +---+---------+ +-------------+ +-------------+ +---------+---+
!         | A |         | |             | |             | |         | D |
!         |---+         | |             | |             | |         +---|
!         |             | |             | |             | |             |
!         |             | |---+         | |         +---| |             |
!         |             | | B |         | |         | C | |             |
!         +-------------+ +---+---------+ +---------+---+ +-------------+
!                                             
!    2. On 14/Jun/2007 : Polygon commands were added to make the Figure labelling
!                           easy (symbols "polfl" and "poll"). 
!
!-------------------------------------------------------------------------------
!
! define internal variables
!
    define symbol mv_r    = $1"5"      ! number of rows
    define symbol mv_c    = $2"3"      ! number of columns
    define symbol mv_xs   = $3"0.25"   ! X size
    define symbol mv_xb   = $4"0.10"   ! X beginning
    define symbol mv_xg   = $5"0.03"   ! X gap 
    define symbol mv_ys   = $6"0.15"   ! Y size 
    define symbol mv_yb   = $7"0.10"   ! Y beginning
    define symbol mv_yg   = $8"0.02"   ! Y gap
    define symbol mv_prefx= $9"v"      ! Prefix for the viewport name
    define symbol mv_cbw  = $10"0.07"  ! Corner box width
    define symbol mv_cbh  = $11"0.07"  ! Corner box height
!
! Issue a warning message if request values does not fit in
!
    let mv_xh = `($mv_xb)+($mv_c)*($mv_xs)+(($mv_c)-1)*($mv_xg)`  ! X high
    let mv_yh = `($mv_yb)+($mv_r)*($mv_ys)+(($mv_r)-1)*($mv_yg)`  ! Y high

    IF `($mv_r) LT 1` THEN  ! minimum number of rows is 1
          SAY ; SAY " ERROR (multi_view) : Number of rows should be > 0" ; SAY
          EXIT/PROMPT
    ENDIF 
    IF `($mv_c) LT 1` THEN  ! minimum number of columns is 1
          SAY ; SAY " ERROR (multi_view) : Number of columns should be > 0" ; SAY
          EXIT/PROMPT
    ENDIF 

    IF `($mv_xb) GT 0.9 OR ($mv_xb) LT 0` THEN   ! upper/lower bound for xstart 
          SAY ; SAY " ERROR (multi_view) : X_Start should be between 0.0 and 0.9" ; SAY
          EXIT/PROMPT
    ENDIF
    IF `($mv_yb) GT 0.9 OR ($mv_yb) LT 0` THEN   ! upper/lower bound for ystart 
          SAY ; SAY " ERROR (multi_view) : Y_Start should be between 0.0 and 0.9" ; SAY
          EXIT/PROMPT
    ENDIF
    IF `mv_xh GT 1.0` THEN 
          SAY 
          SAY " ERROR (multi_view) : Exceeded Max Xsize --> "
          SAY "       Reduce Number of Columns or Xsize/Xstart/Xgap...."
          SAY ; EXIT/PROMPT
    ENDIF
    IF `mv_yh GT 1.0` THEN 
          SAY 
          SAY " ERROR (multi_view) : Exceeded Max Ysize --> "
          SAY "       Reduce Number of  Rows or Ysize/Ystart/Ygap...."
          SAY ; EXIT/PROMPT
    ENDIF
    IF `($mv_cbh) GE ($mv_ys)` THEN
          SAY 
          SAY " ERROR (multi_view) : Corner Box Height is > Ysize --> "
          SAY "       Reduce Corner Box Height (arg 11) or increase Ysize (arg 6)."
          SAY ; EXIT/PROMPT
    ENDIF
    IF `($mv_cbw) GE ($mv_xs)` THEN
          SAY 
          SAY " ERROR (multi_view) : Corner Box Width is > Xsize --> "
          SAY "       Reduce Corner Box Width (arg 10) or increase Xsize (arg 3)."
          SAY ; EXIT/PROMPT
    ENDIF
!
! IF arguments are valid, define the viewports
!
    REPEAT/RANGE=1:`($mv_r)`:1/NAME=mv_ro ( ;\
       let mv_ylo = ($mv_yb) + (($mv_r)-`mv_ro`)*($mv_yg) + (($mv_r)-`mv_ro`)*($mv_ys)     ;\
       let mv_yhi = ($mv_yb) + (($mv_r)-`mv_ro`)*($mv_yg) + (($mv_r)-(`mv_ro`-1))*($mv_ys) ;\
       REPEAT/RANGE=1:`($mv_c)`:1/NAME=mv_co (  ;\
          let mv_xlo = ($mv_xb) + (`mv_co`-1)*($mv_xg) + (`mv_co`-1)*($mv_xs) ;\
          let mv_xhi = ($mv_xb) + (`mv_co`-1)*($mv_xg) + `mv_co`*($mv_xs)     ;\
          define view/axes/xlim=`mv_xlo`:`mv_xhi`/ylim=`mv_ylo`:`mv_yhi` ($mv_prefx)`mv_ro``mv_co` ;\
          define view/axes/xlim=`mv_xlo`:`mv_xlo+($mv_cbw)`/ylim=`mv_ylo`:`mv_ylo+($mv_cbh)` ($mv_prefx)`mv_ro``mv_co`_bl ;\
          define view/axes/xlim=`mv_xhi-($mv_cbw)`:`mv_xhi`/ylim=`mv_ylo`:`mv_ylo+($mv_cbh)` ($mv_prefx)`mv_ro``mv_co`_br ;\
          define view/axes/xlim=`mv_xlo`:`mv_xlo+($mv_cbw)`/ylim=`mv_yhi-($mv_cbh)`:`mv_yhi` ($mv_prefx)`mv_ro``mv_co`_tl ;\ 
          define view/axes/xlim=`mv_xhi-($mv_cbw)`:`mv_xhi`/ylim=`mv_yhi-($mv_cbh)`:`mv_yhi` ($mv_prefx)`mv_ro``mv_co`_tr ;\
       ) ;\
    )
!
! Define few polygon commands which will make the labelling easy
!
     define symbol polfl = POLYGON/fill/line/noaxes/nolab/nokey/vlim=1:10/hlim=1:10    
     define symbol poll  = POLYGON/line/noaxes/nolab/nokey/vlim=1:10/hlim=1:10    
     let xpol = XSEQUENCE({1,10,10,1,1})
     let ypol = XSEQUENCE({1,1,10,10,1})
!
! Clean Up
!    
    cancel variable mv_* ; cancel symbol mv_*
    set mode/last verify
