!
let nb 0 /new integer
if (kepler_show%spec) let nb nb+1
if (kepler_show%prof) let nb nb+1
if (kepler_show%pv) let nb nb+1
if nb.eq.0 return
!
let g_%hfs exist(hfs)
if (g_%hfs) then
  let g_%tau hfs%tau ! For HFS structure
else
  let g_%tau 0
endif
!
!
@ window_tools
@ plot_position
!
! Define the Velo Limits and Flux limits
define double zz /like kepler_spectrum[1]
let zz kepler_spectrum[2]+3*kepler_spectrum[3] /where kepler_spectrum[2].ne.kepler_pv%blank[1]
greg1\limits /var kepler_spectrum[1] zz !! kepler_spectrum[2]
let zz kepler_spectrum[2]-kepler_spectrum[3] /where kepler_spectrum[2].ne.kepler_pv%blank[1]
limi = = < > /var kepler_spectrum[1] zz !! kepler_spectrum[2]
define double vlim[2] flim[2]
let vlim user_xmin user_xmax
let flim user_ymin user_ymax
delete /var zz
!
! Define the Radius and Tb Limits
define double zz /like kepler_profile[1]
let zz kepler_profile[2]+2*kepler_profile[3] /where kepler_profile[2].ne.kepler_pv%blank[1]
greg1\limits 0 * 0 * /var kepler_profile[1] zz
let zz kepler_profile[2]-kepler_profile[3] /where kepler_profile[2].ne.kepler_pv%blank[1]
limi = = < > /var kepler_profile[1] zz
define double rlim[2] tlim[2]
let rlim user_xmin user_xmax
let tlim user_ymin user_ymax
if any(kepler_show%r.ne.0) let rlim kepler_show%r
if any(kepler_show%t.ne.0) let tlim kepler_show%t
delete /var zz
!
if (kepler_show%pv).and.(nb.ne.1) then
  @ find_nx_ny 2 2
  greg1\set expand 0.8
  let nb 3
else if nb.eq.2 then
  @ find_nx_ny 2
  greg1\set expand 0.8
  let inter 0.15
  let aspect 1.25
  @ window_xy 2 1 1
else
  let inter 0.15
  let aspect 1.25
  ! For LandScape
  let position 0.17  1.081000      8.5000001E-02  0.813
  @ find_nx_ny 1
  greg1\set expand 1
  @ window_xy 1 1 1
endif
!
if (nb.eq.1) then
  let flim[2] 1.3*flim[2]
endif
if any(kepler_show%v.ne.0) let vlim kepler_show%v
if any(kepler_show%f.ne.0) let flim kepler_show%f
!
if (kepler_show%pv) then
  create dir KPV
  change dir KPV
  @ p_scale kepler_pv ! Color LUT
  greg2\rgdata kepler_pv /vari
  if (nb.eq.3) then
    @ window_xy 2 2 2 1  ! Bottom Right
    if (kepler_show%layout) then
      greg1\set box 8.5 21.0 1.785 1.785+12.5
    endif
    greg1\limits vlim[1] vlim[2] rlim[1] rlim[2]
  else
    @ window_xy 1 1 1
    greg1\limits /rgdata
    if (user_xmin.gt.user_xmax) then
      greg1\limits /rgdata /reve x
    endif
    let vlim user_xmin user_xmax
    let rlim user_ymin user_ymax
  endif
  greg2\plot
  if (kepler_show%prof) then
    greg1\axis xl
    greg1\axis xu
    greg1\axis yl /label n
    greg1\label "Velocity (km/s)" /X
    greg2\wedge right
  else
    greg1\box
    greg1\label "Velocity (km/s)" /X
    greg1\label "Radius (au)" /Y
    if (nb.ne.3) then
      greg2\wedge top
    else
      greg2\wedge right
    endif
  endif
  if (color[3].ne.0) @ p_color
  change dir ..
endif
!
if (kepler_show%spec) then
  ! Integrated Spectrum
  create dir KSPEC
  change dir KSPEC
  greg1\set blank kepler_pv%blank[1]  1
  if nb.eq.3 then
    @ window_xy 2 2 2 2  ! Top  Right
    if (kepler_show%layout) then
      greg1\set box 8.5 21.0 15.0 20.5
    endif
  endif
  !
  greg1\limits vlim[1] vlim[2] flim[1] flim[2]
  !
  ! Do the FIT
  @ kepler_dofit kepler_spectrum
  !
  if (nb.eq.3) then
    greg1\box n o
  else
    greg1\box
  endif
  greg1\histogram kepler_spectrum[1] kepler_spectrum[2]
  !
  change dir ..
  gtvl\create dir FIT
  gtvl\change dir FIT
  if nb.eq.3 then
    @ window_xy 2 2 2 2  ! Top  Right
    if (kepler_show%layout) then
      greg1\set box 8.5 21.0 15.0 20.5
    endif
  endif
  !
  greg1\limits vlim[1] vlim[2] flim[1] flim[2]
  greg1\pencil 1
  greg1\connect kepler_spectrum[1] g_%fit
  greg1\pencil 0
  !
  ! Error bars with re-scaling if needed
  define double zz /like g_%fit
  let mark_size  marker_size /new real
  set marker * * 0
  if (g_%hfs) then
    !
    ! The noise is not necessarily well computed
    define double zrms rmsz
    let zz kepler_pv%blank[1]
    let zz kepler_spectrum[2]-g_%fit  /where kepler_spectrum[2].ne.kepler_pv%blank[1]
    compute zrms rms zz /blanking kepler_pv%blank[1]
    compute rmsz mean kepler_spectrum[3]
    let rmsz max(1E-5,rmsz)
    say "Correcting noise by " zrms/rmsz /forma A 1pg10.2
    let zz kepler_spectrum[3]*zrms/rmsz
    greg1\errorbar y kepler_spectrum[1] kepler_spectrum[2] zz
  else
    let zz max(kepler_spectrum[3],1E-5)
    greg1\errorbar y kepler_spectrum[1] kepler_spectrum[2] zz ! kepler_spectrum[3]
  endif
  set marker * * mark_size
  delet /var zz mark_size
  !
  defin char str*6 ste*5 sth*5[3]
  define integer kp[3] i
  let kp 1 3 2 
  let sth "S" "V" "\gDV" 
  !
  define integer nlist
  if (kepler_usevdisk) then
    if (kepler_show%dv.eq.0) then
      let kp 1 0 2
    else
      let kp 1 0 0
    endif
  else
    if (kepler_show%dv.eq.0) then
      let kp 1 2 3
    else
      let kp 1 2 0
    endif
  endif
  if (kepler_show%dv.ne.0) let nlist nlist-1
  !
  ! Now display the results in the appropriate place
  define integer key_adj key_box
  if (nb.eq.1) then
    let key_adj 6
    let key_box 7
  else if (nb.eq.3) then
    let db box_xmin-4 /new real
    set box max(0,box_xmin-10) db  box_ymin box_ymax
    let key_adj 6
    let key_box 7
  else if (nb.eq.2) then
    let db box_xmax+8 /new real
    set box box_xmin box_xmax box_ymax+4 db
    let key_adj 6
    let key_box 7
  endif
  for j 1 to 3 
    let i kp[j]
    if i.ne.0 then
      if (kepler_fit%r[j].lt.0.01) then
        let str kepler_fit%r[j] /forma f6.3
        let ste kepler_fit%e[j] /forma f5.3
      else
        let str kepler_fit%r[j] /forma f6.2
        let ste kepler_fit%e[j] /forma f5.2
      endif
      draw text 1.2 -1.2*j 'sth[j]'" = "'str'"\s+"'ste' key_adj 0 /char key_box
    else
      if (j.eq.2) then
        let str g_%pos /forma f6.3
      else
        let str g_%width /forma f6.3
      endif
      draw text 1.2 -1.2*j 'sth[j]'" = "'str' key_adj 0 /char key_box
    endif
  next
  if (.not.kepler_usevdisk) then
    let kepler_vdisk kepler_fit%r[2]
  endif
  !
  let str kepler_fit%r[4] /format f6.2
  let ste kepler_fit%e[4] /format f6.2
  draw text 1.2 -5.2 "\g| S\d\gn\gDV = "'str'" \s+ "'ste' key_adj 0 /char key_box
  !
  if (g_%hfs) then
    ! Display opacity if fitted
    if (hfs%tau.lt.0) then
      let str kepler_fit%r[5] /forma f6.2
      let ste kepler_fit%e[5] /forma f5.2
      draw text 1.2 -6.7 "\gt  = "'str'" \s+ "'ste' key_adj 0 /char key_box
    endif
  endif
  !
  ! Display Integrated Line Flux
  greg1\set blank 0  -1
  gtvl\change dir ..
  change dir KSPEC
  !
  if (nb.eq.1) then
    !
    ! Display spectral lines
    ! Get Limits of display In Frequency
    define double dble fmin fmax
    dble = (user_xmin-kepler_pv%convert[2,1])/kepler_pv%convert[3,1] ! +kepler_convert[1,1]
    fmin = dble*kepler_pv%freqres+kepler_pv%restfre
    dble = (user_xmax-kepler_pv%convert[2,1])/kepler_pv%convert[3,1] 
    fmax = dble*kepler_pv%freqres+kepler_pv%restfre
    @ preview_lines 'fmin' 'fmax' !! Not user_xmin user_xmax
    @ kepler-drawlines 'fmin' 'fmax'
  endif
  !
  !
  if (nb.ne.3) label "Velocity (km/s)" /X
  greg1\label "Line flux (Jy)" /Y 4
  !
  change dir ..
endif
!!
if (nb.eq.2) then
  @ window_xy 2 1 2
endif
!
if (kepler_show%prof) then
  ! Radial Profile
  create dir KPROF
  change dir KPROF
  greg1\set blank kepler_pv%blank[1]  1
  let mark_size marker_size /new real 
  set marker * * 0
  if nb.eq.3 then
    @ window_xy 2 2 1 1  ! Bottom Left
    if (kepler_show%layout) then
      greg1\set box 2.5 8.0 1.785 1.785+12.5
    endif
    greg1\limits tlim[1] tlim[2] rlim[1] rlim[2]
    greg1\box
    greg1\connect kepler_profile[2] kepler_profile[1]
    greg1\label "T\dB (K)" /x
    greg1\label "Radius (au)" /Y
    !
    greg1\errorbar x kepler_profile[2] kepler_profile[1] kepler_profile[3]
  else
    greg1\limits rlim[1] rlim[2] tlim[1] tlim[2]
    if (nb.eq.2)
      greg1\box n n
      greg1\axis xl
      greg1\axis yr /label o
      greg1\histogram kepler_profile[1] kepler_profile[2]
      greg1\draw text 3.5 0 "Mean Brightness (K)" 5 90 /char 6
    else
      greg1\box
      greg1\histogram kepler_profile[1] kepler_profile[2]
      greg1\draw text -3.5 0 "Mean Brightness (K)" 5 90 /char 4
    endif
    greg1\label "Radius (au)" /X
    !
    greg1\errorbar y kepler_profile[1] kepler_profile[2] kepler_profile[3]
  endif
  set marker * * mark_size
  delet /var mark_size
  !
  pen 3
  greg1\draw relocate 0 kepler_rint /user
  greg1\draw line 0 kepler_rout /user
  pen 0
  greg1\set blank -1000 -1
  !
  change dir ..
endif
!
! Standard Header if specified
if do_header return
!
! Some Header in most cases
if (nb.eq.3.and.kepler_show%layout) return
!
gtvl\create directory HEADER
gtvl\change directory HEADER
def char sfre*12
let sfre &1%restfre /format f12.3
if (nb.eq.3) then
  if (kepler_show%layout) then
    greg1\set box 2.5 8.0 15 20.5
  else
    @ window_xy 2 2 1 1
  endif
  greg1\set expand 0.8
  greg1\draw text 0 -2.5 '&1%line' 6 /char  7
  greg1\draw text 1 -0.5 'sfre'" MHz" 6 /char 7
  greg1\set expand 1.0
  greg1\draw text 0 +1 '&1%source' 6 /char 1
else
  greg1\set expand 1.0
  greg1\draw text 0 1 '&1%line'" @ "'sfre'" MHz" 4 /char 9
  if (nb.eq.2) then
    @ window_xy 2 1 1
  endif
  greg1\draw text 0 1 '&1%source' 6 /char 7
endif
gtvl\change dir ..
