!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! @ l-old-box-setup
!
! &1: aaa
! &2: boxtype (image or spectrum)
! &3: default y title if "&2".eq."spectrum"
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
define character xy*1
define double pos
!
! Coordinate type (for spatial or velocity axes): Absolute or relative
if (do%coord.eq."*") then
   let true%coord "abs"
else
   let true%coord 'do%coord' /lower
endif
! Spatial coordinate unit
if (do%scoord.eq."*") then
   let true%scoord[1] "sec"
else
   let true%scoord[1] 'do%scoord' /lower
endif
! Velocity coordinate unit
if (do%vcoord.eq."*") then
   let true%vcoord "vel"
else
   let true%vcoord 'do%vcoord' /lower
endif
!
! Legend position
if (do%legend) then
   let true%leg%pos 'do%leg%pos' /lower
   if (true%leg%pos.eq."*") then
      let true%leg%pos "plot"
   else if (true%leg%pos.eq."header") then
      if (.not.do%header) then
         let true%leg%pos "plot"
      endif
   else if (true%leg%pos.ne."plot".and.true%leg%pos.ne."none") then
      message e box-setup "Unknown legend position: "'do%leg%pos'
      return base
   endif
else
   let true%leg%pos "none"
endif
!
for iaxis 1 to 2
   ! X axis label definition
   if (do%lab%xp[iaxis].eq."*") then
      ! Default value
      if (iaxis.eq.1) then
         let true%lab%xp[iaxis] "p"
      else
         let true%lab%xp[iaxis] "n"
      endif
   else
      ! User defined
      let true%lab%xp[iaxis] 'do%lab%xp[iaxis]' /lower
   endif
   if ((true%lab%xp[iaxis].ne."o").and.(true%lab%xp[iaxis].ne."p").and.(true%lab%xp[iaxis].ne."n")) then
      ! Error
      message e box-setup "Unknow X label position: "'do%lab%xp[iaxis]'
   endif
   !
   ! Y axis label definition
   if (do%lab%yp[iaxis].eq."*") then
      ! Default value
      if (iaxis.eq.1) then
         let true%lab%yp[iaxis] "o"
      else
         let true%lab%yp[iaxis] "n"
      endif
   else
      ! User defined
      let true%lab%yp[iaxis] 'do%lab%yp[iaxis]' /lower
   endif
   if ((true%lab%yp[iaxis].ne."o").and.(true%lab%yp[iaxis].ne."p").and.(true%lab%yp[iaxis].ne."n")) then
      ! Error
      message e box-setup "Unknow Y label position: "'do%lab%yp[iaxis]'
   endif
next iaxis
!
! Legend and coordinate definitions
define logical rcoord
if (true%coord.eq."rel") then
   let rcoord yes
else if (true%coord.eq."abs") then
   let rcoord no
else
   message e box-setup "Unknown coordinate type: "'do%coord'
   return base
endif
define character boxtype*12
let boxtype "&2" /lower
if (boxtype.eq."spectrum") then
   ! Spectrum => x-axis is velocity and y-axis is intensity
   ! X legend title
   if (do%leg%xt.eq."*") then
      ! Default value
      if (true%vcoord.eq."vel") then
         if (rcoord) then
            let true%leg%xt "V\\dobject\\u (km.s\\u-1\\d)"
         else
            let true%leg%xt "V\\dLSR\\u (km.s\\u-1\\d)"
         endif
      else if (true%vcoord.eq."fre") then
         if (rcoord) then
            let true%leg%xt "F\\dobject\\u-F\\drest\\u (MHz)"
         else
            let true%leg%xt "F\\dobject\\u (MHz)"
         endif
      else if (true%vcoord.eq."pix") then
         if (rcoord) then
            let true%leg%xt "I-I\\dref\\u (Channels)"
         else
            let true%leg%xt "I (Channels)"
         endif
      else
         message e box-setup "Unknown coordinate type: "'do%vcoord'
         return base
      endif
   else
      ! User defined
      let true%leg%xt 'do%leg%xt'
   endif
   ! Y legend title
   if (do%leg%yt.eq."*") then
      ! Default value
      if (pro%narg.ge.3) then
         let true%leg%yt "&3"
      else
         let true%leg%yt "Intensity ("'aaa1%unit'")"
      endif
   else
      ! User defined
      let true%leg%yt 'do%leg%yt'
   endif
   ! X legend position
   if (do%leg%xp.eq.0) then
      let true%leg%xp -2
   else
      let true%leg%xp do%leg%xp
   endif
   ! Y legend position
   if (do%leg%yp.eq.0) then
      let true%leg%yp -3.5
   else
      let true%leg%yp do%leg%yp
   endif
   ! X and Y label unit
   let true%lab%xu "oth"
   let true%lab%yu "oth"
else if (boxtype.eq."image") then
   ! Image => No well defined x-axis and y-axis...
   for iaxis 1 to 2
      if (iaxis.eq.1) then
         let xy "x"
         let pos -2
      else
         let xy "y"
         let pos -3.5
      endif
      ! Default legend title
      if (iaxis.eq.&1%f_axis) then
         ! Frequency/Velocity axis
         if (true%vcoord.eq."vel") then
            if (rcoord) then
               let true%leg%'xy't "V\\dobject\\u (km.s\\u-1\\d)"
            else
               let true%leg%'xy't "V\\dLSR\\u (km.s\\u-1\\d)"
            endif
         else if (true%vcoord.eq."fre") then
            if (rcoord) then
               let true%leg%xt "F\\dobject\\u-F\\drest\\u (MHz)"
            else
               let true%leg%xt "F\\dobject\\u (MHz)"
            endif
         else if (true%vcoord.eq."pix") then
            if (rcoord) then
               let true%leg%'xy't "V axis (I-I\\uref\\d)"
            else
               let true%leg%'xy't "V axis (Channels)"
            endif
         else
            message e box-setup "Unknown coordinate type: "'do%vcoord'
            return base
         endif
         let true%lab%'xy'u "oth" ! Other
      else if ((iaxis.eq.&1%x_axis).or.(iaxis.eq.&1%y_axis)) then
         ! Spatial axis
         if (true%scoord[1].eq."pix") then
            ! Pixel unit
            if (iaxis.eq.&1%x_axis) then
               let true%leg%'xy't "X axis (Pixels)"
               let true%lab%'xy'u "pix" ! Pixel
            else if (iaxis.eq.&1%y_axis) then
               let true%leg%'xy't "Y axis (Pixels)"
               let true%lab%'xy'u "pix" ! Pixel
            endif
         else
            ! physical unit
            ! Check coordinate type
            if (true%coord.eq."abs") then
               if ((abs(true%prange%r[2,1]-true%prange%r[1,1]).gt.0.2).or.(abs(true%prange%r[2,2]-true%prange%r[1,2]).gt.0.2)) then
                  let true%coord "rel"
                  let rcoord yes
               else if (&1%angle.ne.0) then
                  let true%coord "rel"
                   let rcoord yes
               endif
            endif
            ! Check coordinate unit
            if (rcoord) then
               if ((true%scoord[1].eq."*").or.(true%scoord[1].eq."abs")) then
                  if ((abs(true%prange%r[2,1]-true%prange%r[1,1]).gt.0.15).or.(abs(true%prange%r[2,2]-true%prange%r[1,2]).gt.0.87)) then
                     let true%scoord "deg" "^"
                  else if ((abs(true%prange%r[2,1]-true%prange%r[1,1]).gt.0.15).or.(abs(true%prange%r[2,2]-true%prange%r[1,2]).gt.1.45e-3)) then
                     let true%scoord "min" "'"
                  else if ((abs(true%prange%r[2,1]-true%prange%r[1,1]).gt.0.15).or.(abs(true%prange%r[2,2]-true%prange%r[1,2]).gt.2.42e-5)) then
                     let true%scoord "sec" "`"
                  endif
               else if (true%scoord[1].eq."deg") then
                  let true%scoord[2] "^"
               else if (true%scoord[1].eq."min") then
                  let true%scoord[2] "'"
               else if (true%scoord[1].eq."sec") then
                  let true%scoord[2] "`"
               else
                  message e box-setup "Unknown relative coordinate unit: "'true%scoord[1]'
                  return base
               endif
            else
               let true%scoord "abs" "abs"
               if (iaxis.eq.2) then
                  let pos -5.5
               endif
            endif
            let true%lab%'xy'u 'true%scoord[1]'
            if (&1%unit'iaxis'.eq."RA") then
               define character equinox*6
	       if (&1%system.eq."ICRS") then
		  let equinox "(ICRS)"
	       else
		  let equinox &1%equinox /format f6.1
	       endif
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdx ("'true%scoord[2]'")"
               else if (rcoord) then
                  let true%leg%'xy't "\GdR.A. "'equinox'" ("'true%scoord[2]'")"
               else
                  let true%leg%'xy't "R.A. "'equinox'
               endif
               delete /var equinox
            else if (&1%unit'iaxis'.eq."DEC") then
               define character equinox*6
               if (&1%system.eq."ICRS") then
		  let equinox "(ICRS)"
	       else
		  let equinox &1%equinox /format f6.1
	       endif
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdy  ("'true%scoord[2]'")"
               else if (rcoord) then
                  let true%leg%'xy't "\GdDec "'equinox'" ("'true%scoord[2]'")"
               else
                  let true%leg%'xy't "Dec "'equinox'
               endif
               delete /var equinox
            else if (&1%unit'iaxis'.eq."LII") then
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdx ("'true%scoord[2]'")"
               else if (rcoord) then
                  let true%leg%'xy't "\GdL\\DII\\U ("'true%scoord[2]'")"
               else
                  let true%leg%'xy't "L\\DII\\U (^)"
               endif
            else if (&1%unit'iaxis'.eq."BII") then
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdy  ("'true%scoord[2]'")"
               else if (rcoord) then
                  let true%leg%'xy't "\GdB\\DII\\U ("'true%scoord[2]'")"
               else
                  let true%leg%'xy't "B\\DII\\U (^)"
               endif
            else if (&1%unit'iaxis'.eq."Radius") then
               ! This one is for grid in polar coordinates
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdr  ("'true%scoord[2]'")"
               else
                  let true%leg%'xy't "\Gdr "'&1%equinox'" ("'true%scoord[2]'")"
               endif
            else if (&1%unit'iaxis'.eq."ANGLE") then
               ! *** JP: Why this one?
               if (&1%angle.ne.0) then
                  let true%leg%'xy't "\Gdpos  ("'true%scoord[2]'")"
               endif
            else
               @ l-old-let.cube true%leg%'xy't &1%unit'iaxis'
               let true%lab%'xy'u "oth" ! Other
            endif
         endif
      else
         @ l-old-let.cube true%leg%'xy't &1%unit'iaxis'
         let true%lab%'xy'u "oth" ! Other
      endif
      ! User legend title overides default
      if (do%leg%'xy't.ne."*") then
         @ l-old-let.cube true%leg%'xy't do%leg%'xy't
      endif
      ! Legend position
      if (do%leg%'xy'p.eq.0) then
         let true%leg%'xy'p 'pos'
      else
         @ l-old-let.cube true%leg%'xy'p do%leg%'xy'p
      endif
   next iaxis
else
   message e box-setup "Unknown box type: "'boxtype'
   return base
endif
!
if (.not.do%legend) then
   let true%leg%xt ""
   let true%leg%yt ""
endif
if (.not.do%label) then
   let true%lab%xp "n" "n"
   let true%lab%yp "n" "n"
endif
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
