
begin procedure plot-subscans-beg-end
  define integer nsub
  let nsub size(mjdbeg)
  g\pen /col red
  g\set char 1.2/nsub
  for isub 1 to nsub
    g\draw text (mjdbeg[isub]+mjdend[isub])/2 user_ymax "Subscan #"'isub' 2 /user
    g\draw relo mjdbeg[isub] user_ymin /user
    g\draw line mjdbeg[isub] user_ymax /user
    g\draw relo mjdend[isub] user_ymin /user
    g\draw line mjdend[isub] user_ymax /user
  next isub
  g\pen /col foreground
  g\set char 0.48
end procedure plot-subscans-beg-end

if (pro%narg.ne.1) then
  message e plot "Missing IMB-FITS file name as argument"
  return base
endif

mset input bandwidth 0.0

read 'pro%arg[1]' /file /sub 1
variable

define integer ndump
let ndump SUBS%ANTSLOW%HEAD%DESC%NAXIS2

for isub 2 to IMBF%PRIM%N_OBSP
  read 'pro%arg[1]' /file /sub isub
  let ndump ndump+SUBS%ANTSLOW%HEAD%DESC%NAXIS2
next isub

exa ndump

define double mjd[ndump]
define double longoff latoff celevatio coselev /like mjd
if exist(mjdbeg)  del /var mjdbeg mjdend
define double mjdbeg[IMBF%PRIM%N_OBSP] mjdend[IMBF%PRIM%N_OBSP] /global

define integer first last
let first 1
for isub 1 to IMBF%PRIM%N_OBSP
  read 'pro%arg[1]' /file /sub isub
  variable
  let mjdbeg[isub] subs%antslow%head%mjd_beg
  let mjdend[isub] subs%antslow%head%mjd_end
  let last first+SUBS%ANTSLOW%HEAD%DESC%NAXIS2-1
  let mjd[first:last] SUBS%ANTSLOW%TABLE%MJD
  let longoff[first:last] SUBS%ANTSLOW%TABLE%longoff
  let latoff[first:last] SUBS%ANTSLOW%TABLE%latoff
  let celevatio[first:last] SUBS%ANTSLOW%TABLE%celevatio
  let first last+1
next isub

let longoff longoff/sec
let latoff latoff/sec
let coselev cos(celevatio)
!-----------
! let longoff longoff ! /sin(celevatio)
!-----------
let celevatio celevatio*180/pi  ! rad to deg


clear
g\set /def
g\set mark 4 1 0.08 45.0

! g\limits /var mjd longoff
! g\box
! g\point mjd longoff
! g\label "MJD" /x
! g\label "LONGOFF [arcsec]" /y
! 
! pause

! clear
! 
! g\limits /var mjd celevatio
! g\box
! g\point mjd celevatio
! g\label "MJD" /x
! g\label "CELEVATIO [rad]" /y
! 
! pause

! clear
! 
! g\limits /var mjd coselev
! g\box
! g\point mjd coselev
! g\label "MJD" /x
! g\label "COS(CELEVATIO)" /y
! 
! pause

clear
g\set char 0.48

define real tmp /like longoff
define logical symmetric
let symmetric yes

! 1st group (>0)
let tmp -1000.0
let tmp longoff /where longoff.gt.0
define real min1 max1
compute min1 min tmp /blank -1000.0
compute max1 max tmp /blank -1000.0
if min1.eq.-1000.0 then
  let min1 0.0
  let max1 0.0
  let symmetric no
endif

! 2nd group (<0)
let tmp -1000.0
let tmp longoff /where longoff.lt.0
define real min2 max2
compute min2 min tmp /blank -1000.0
compute max2 max tmp /blank -1000.0
if min2.eq.-1000.0 then
  let min2 0.0
  let max2 0.0
  let symmetric no
endif

! Plot limits
define real pmin1 pmax1 pmin2 pmax2
if symmetric then
  ! Set mirror limits
  let pmin1 min(min1,-max2)
  let pmax1 max(max1,-min2)
  let pmin2 -pmax1
  let pmax2 -pmin1
else
  ! Leave as is
  let pmin1 min1
  let pmax1 max1
  let pmin2 min2
  let pmax2 max2
endif


! LONGOFF
g\set box_loc 4.0 13.5 2.0 11.0
g\limits mjd[1] mjd[ndump] pmin2 pmax2 /margin 5 10
g\point mjd longoff
@ plot-subscans-beg-end
g\axis xl
g\axis xu
g\axis yl
g\limits = = pmin2-min2 pmax2-min2 /margin 5 10
g\axis yr /label o

g\set box_loc 4.0 13.5 11.0 19.0
g\limits = = pmin1 pmax1 /margin 5 10
g\point mjd longoff
@ plot-subscans-beg-end
g\axis xl /label n
g\axis xu (user_xmin-mjd[1])*24*60 (user_xmax-mjd[1])*24*60 /label p
g\axis yl
g\limits = = pmin1-max1 pmax1-max1 /margin 5 10
g\axis yr /label o

g\set box_loc 4.0 13.5 2.0 19.0
g\label "MJD [day]" /x
g\label "LONGOFF [arcsec]" /y
g\draw text 0 0.7 "Relative MJD [min]" /box 8
g\draw text 1.8 0 "Relative LONGOFF [arcsec]" 5 90. /box 6

! LATOFF
g\set box_loc 18.5 28.0 11.0 19.0
g\limits /var mjd latoff /margin 5 10
g\box n
g\point mjd latoff
@ plot-subscans-beg-end
! g\label "MJD" /x
g\label "LATOFF [arcsec]" /y 4.5

! CELEVATIO
g\set box_loc 18.5 28.0 2.0 11.0
g\limits /var mjd celevatio /margin 5 10
g\point mjd celevatio
@ plot-subscans-beg-end
g\box
g\label "MJD [day]" /x
g\label "CELEVATIO [deg]" /y 4.5

! Title
g\set box_loc 4.0 28.0 2.0 19.0
define char*64 shortname
sic parse 'pro%arg[1]' shortname
g\draw text 0 1.4 'shortname'".fits ("'IMBF%SCAN%head%SWTCHMOD'")" /box 8

! hard 'shortname'".ps" /dev ps grey /over

g\set char 0.6
