
begin procedure calplot-usage
  !---------------------------------------------------------------------
  ! Usage displayed if bad calling sequence
  !---------------------------------------------------------------------
  message i plot-calibration-scan "Usage:"
  message r plot-calibration-scan " @ plot-calibration-scan FitsName [/AVERAGE YES|NO]"
  message r plot-calibration-scan ""
  message r plot-calibration-scan " Plot the subscans calCold, calAmbient and calSky of a  calibration"
  message r plot-calibration-scan "scan. By default, all the time dumps are averaged,  but  all  these"
  message r plot-calibration-scan "dumps can be plotted with the option /AVERAGE NO.  Take  care  that"
  message r plot-calibration-scan "this option makes the plot much heavier."
end procedure calplot-usage

begin procedure calplot-once
  !---------------------------------------------------------------------
  !
  !---------------------------------------------------------------------
  !
  ! Fill IMBF% structure and find the best Y min-max
  define real allmin[3] allmax[3]
  for isub 1 to 3
    read 'calplot%fitsfile' /file /sub isub
    variable
    compute allmin[isub] min IMBDATA%IMBF%VAL
    compute allmax[isub] max IMBDATA%IMBF%VAL
  next isub
  !
  ! Plot first the right panel
  g\set box_loc 24.0 29.0 2.5 19.5
  g\limits 0. 1. 0. 1.
  g\box n n n
  define real ypos yshift
  g\set charac 0.35
  let ypos -1.0
  let yshift 0.4
  g\draw text 0.05 ypos "Project:"               6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%PRIM%PROJID'        6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Object:"                6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%PRIM%OBJECT'        6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Date:"                  6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%PRIM%DATE_OBS'      6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Scan:"                  6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%SCAN%HEAD%SCANNUM'  6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Backend:"               6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%PRIM%INSTRUME'      6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Obstype:"               6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%PRIM%OBSTYPE'       6 /box 7
  let ypos ypos-2*yshift
  g\draw text 0.05 ypos "Switching mode:"        6 /box 7
  let ypos ypos-yshift
  g\draw text 0.2 ypos 'IMBF%SCAN%HEAD%SWTCHMOD' 6 /box 7
  g\set charac /def

  ! The main plot
  g\set box_loc 3.5 24.0 2.5 19.5
  define real mymin mymax mymargin
  compute mymin min allmin
  compute mymax max allmax
  let mymargin (mymax-mymin)/40.
  g\limits 0.5 IMBDATA%IMBF%NCHAN+0.5 mymin-mymargin mymax+mymargin
  g\set charac .3
  g\set axis expo y
  g\axis yl /tick out
  g\set charac /def
  ! g\box n n n
  g\label "DATA [chunks]" /X '1+16.0/IMBF%BACK%HEAD%DESC%NAXIS2'
  g\label "Loads [counts]" /Y 4.0
  g\draw relo user_xmin user_ymin /user
  g\draw line user_xmax user_ymin /user
  g\draw text 0. 0.5 'IMBF%FILE%NAME' 8 /box 8
  !
  ! Chunks
  g\set charac 1.0/(IMBF%BACK%HEAD%DESC%NAXIS2**0.6)  ! Smaller if more chunks
  define integer beg end
  let yshift (user_ymax-user_ymin)/16/(IMBF%BACK%HEAD%DESC%NAXIS2**0.6)
  g\draw text 0.5 user_ymin-yshift "RECEIVER:" 4 /user
  g\draw text 0.5 user_ymin-2*yshift "BAND:" 4 /user
  g\draw text 0.5 user_ymin-3*yshift "POLAR:" 4 /user
  for ic 1 to IMBF%BACK%HEAD%DESC%NAXIS2
    let beg IMBF%BACK%TABLE%REFCHAN[ic]+IMBF%BACK%TABLE%dropped[ic]
    let end beg+IMBF%BACK%TABLE%USED[ic]-1
    g\pen /co 14
    g\draw relo beg-.5 user_ymin /user
    g\draw line beg-.5 user_ymax /user
    g\draw relo end+.5 user_ymin /user
    g\draw line end+.5 user_ymax /user
    g\pen /co 0
    g\draw text (beg+end)/2 user_ymin-yshift 'IMBF%BACK%TABLE%RECEIVER[ic]' 5 /user
    g\draw text (beg+end)/2 user_ymin-2*yshift 'IMBF%BACK%TABLE%BAND[ic]' 5 /user
    g\draw text (beg+end)/2 user_ymin-3*yshift 'IMBF%BACK%TABLE%POLAR[ic]' 5 /user
  next ic
  g\set charac /def
  del /var beg end

end procedure calplot-once
!
begin procedure calplot-one
  !---------------------------------------------------------------------
  ! Plot one calibration subscan
  !---------------------------------------------------------------------

  define integer isub
  let isub 'pro%arg[1]'

  read 'calplot%fitsfile' /file /sub isub
  variable

  ! Sanity check
  define character*16 obstype
  let obstype 'IMBF%PRIM%OBSTYPE' /upper
  if obstype.ne."CALIBRATE" then
    message e plot-calibration-scan "Scan must be a calibration: "'IMBF%FILE%NAME'
    return error
  endif

  define integer ipen
  define char*16 substype
  let substype 'SUBS%ANTSLOW%HEAD%SUBSTYPE' /upper
  if substype.eq."CALSKY" then
    let ipen 0
  else if substype.eq."CALAMBIENT" then
    let ipen 1
  else if substype.eq."CALCOLD" then
    let ipen 3
  endif

  g\pen /co ipen
  g\set chara 0.4
  g\draw text 0.05 2*isub 'SUBS%ANTSLOW%HEAD%SUBSTYPE'":"      6 /box 3
  g\draw text 0.2 2*isub-0.4 "subscan #"'isub'                 6 /box 3
  if calplot%average then
    g\draw text 0.2 2*isub-0.8 'IMBDATA%IMBF%NTIME'" time dumps averaged" 6 /box 3
  else
    g\draw text 0.2 2*isub-0.8 'IMBDATA%IMBF%NTIME'" time dumps" 6 /box 3
  endif
  g\set chara /def

  ! DATA
  define real xx[IMBDATA%IMBF%NCHAN]
  let xx[i] i
  if calplot%average then
    define real yy /like IMBDATA%IMBF%VAL[,1,1]
    compute yy mean IMBDATA%IMBF%VAL[,1,]
    g\histo xx yy
    del /var yy
  else
    for it 1 to IMBDATA%IMBF%NTIME
      g\histo xx IMBDATA%IMBF%VAL[,1,it]
    next it
  endif
  del /var xx
  g\pen /co 0

  ! Chunks
  g\set charac 1.0/(IMBF%BACK%HEAD%DESC%NAXIS2**0.6)  ! Smaller if more chunks
  define integer beg end
  define real mymean
  define charac*9 toto
  define real yshift
  let yshift (user_ymax-user_ymin)/100
  for ic 1 to IMBF%BACK%HEAD%DESC%NAXIS2
    let beg IMBF%BACK%TABLE%REFCHAN[ic]+IMBF%BACK%TABLE%dropped[ic]
    let end beg+IMBF%BACK%TABLE%USED[ic]-1
    !
    compute mymean mean IMBDATA%IMBF%VAL[beg:end,1,]
    let toto 'mymean' /format 1pg9.3
    g\draw text (beg+end)/2 'mymean+yshift' 'toto' 5 /user
  next ic
  g\set charac /def
  !
end procedure calplot-one

! Command line parsing
if pro%narg.le.0 then
  @ calplot-usage
  return
else
  define structure     calplot           /global
  define character*256 calplot%fitsfile  /global
  define logical       calplot%average   /global
  !
  parse /average
  let calplot%fitsfile 'pro%parse%command%arg[1]'
  define char*4 argum
  if pro%parse%average%narg.le.0 then
    let argum "YES"
  else
    let argum 'pro%parse%average%arg[1]' /upper
  endif
  let calplot%average  argum.eq."YES"
endif

g\clear
g\set box_loc landscape
mset input band 0.0

@ calplot-once

@ calplot-one 2  ! calAmbient
@ calplot-one 1  ! calSky
@ calplot-one 3  ! calCold

del /var calplot
