!-----------------------------------------------------------------------------!
! Utility to plot spectral setup from .aot file                               !
!-----------------------------------------------------------------------------!
sic alma\ on
sic alma\
select alma full
!
! Find .aot, user selects which he wants to process
!
define integer ifile
sic find .plot_aot/ObsProposal.xml
if (dir%nfile.gt.0) then
  sic delete .plot_aot/*
else
  sic mkdir .plot_aot
endif
sic find *.aot
if (dir%nfile.le.0) then
  say "Found no .aot file ... aborting"
  return
else if (dir%nfile.gt.1) then
  say "Found many .aot files. Possible choices are:"
  for i 1 to dir%nfile
    say 'i'"):"'dir%file[i]'
  next
  let ifile /prompt "Enter number of the chosen file"
else if (dir%nfile.eq.1) then
  say "Found a single .aot file"
  let ifile 1
endif
say "Processing "'dir%file[ifile]'
if exist(.plot_aot/dummy.aot) then
  sic del .plot_aot/dummy.aot
endif
sic copy 'dir%file[ifile]' .plot_aot/dummy.aot
sic dir .plot_aot
sys "unzip dummy.aot"
!
! Parse ObsProject.xml, user selects with SB he want to process
!
if exist(project) then
  del /var project
endif
def struct project /glo
py parse_xml_project.py ObsProject.xml
define integer n_sb[5] nsb isched
compute n_sb dimof project%obs_unit_set
let nsb n_sb[1]
define character*20 sb_name[nsb] sb_array[nsb]
define integer ifiles[nsb]
sic find SchedBlock*
define integer lchain ichain lchain2 ichain2
for ische 1 to nsb
  let lchain typeof(project%obs_unit_set[ische])
  !
  ! Find matching Schedblock xml file
  !
  say "Looking for entityId "'project%obs_unit_set[ische]'
  let ifiles[ische] -1
  for i 1 to dir%nfile
    if exist(dumm) then
      del /var dumm
    endif
    def struc dumm /glo
    py parse_xml_dumm.py 'dir%file[i]'
    let ichain typeof(dumm%entityid)
    if (lchain.gt.ichain) then
      if exist(dumm_chain) then
        del /var dumm_chain
      endif
      define character*'ichain' dumm_chain
      let dumm_chain 'project%obs_unit_set[ische][1:ichain]'
    else
      if exist(dumm_chain) then
        del /var dumm_chain
      endif
      define character*'lchain' dumm_chain
      let dumm_chain 'project%obs_unit_set[ische]'
    endif
    if (dumm_chain.eq.dumm%entityid) then
      say ""
      say "Found matching xml file: "'dir%file[i]'" : "'dumm%entityid'
      let ifiles[ische] i
      let sb_name[ische] 'dumm%name'
      let sb_array[ische] 'dumm%array'
      break
    else
      say "Not matching: "'dumm%entityid'
    endif
  next
  if (ifiles[ische].eq.-1) then
    say "... Found no matching SchedBlock"
    say "... aborting"
    sic dir ..
    return
  endif
next
!
!
if (nsb.eq.0) then
  say "Found no Scheduling block in this .aot ..."
  say "... aborting"
  sic dir ..
  return
else if (nsb.eq.1) then
  say "Found a single Scheduling Block"
  let isched 1
else if (nsb.gt.1) then
  say "Found many Scheduling Block. Possible choices are:"
  for i 1 to nsb
    say 'i'") "'dir%file[i]'": "'sb_name[i]'" (array "'sb_array[i]'")"
  next
  let  isched /prompt "Enter number of the chosen Scheduling Block"
endif
let ifile ifiles[isched]
say " "
say "... I will process "'dir%file[ifile]'
say " "
!
! Personnal
!
! Define structure collecting parameters
! 
if (exist(sb)) then
  del /var sb
endif
def struct sb /glo
def struct sb%freq /glo
def struct sb%bb /glo
def struct sb%spw /glo
!
! Now calls the python parsing routine
!
say " "
say "... reading the xml file"
say " "

py parse_xml.py 'dir%file[ifile]' 
!
! Check tree
!
def integer dimspw[8] dimbb[8] dimfreq[8]
compute dimspw  dimof sb%spw%address
compute dimbb   dimof sb%bb%address
compute dimfreq dimof sb%freq%lo1_f
let depth_tree dimspw[1] /new integer
def real range_xml[depth_tree]
compute range_xml max sb%spw%address
let nss range_xml[2] /new integer
if (nss.ne.dimfreq[1]) then
  say "Non matching frequency structures ..."
  sic dir ..
  return
endif
!
! Choose which spectral setup to plot
!
def integer iss
if (nss.eq.0) then
  say "Found no Spectral setup in this SB..."
  say " ... aborting"
  sic dir ..
  return
else if (nss.eq.1) then
  say "Found a single Spectral setup"
  let iss 1
else if (nss.gt.1) then
  say "Found many Spectral Setups. Possible choices are:"
  for i 1 to nss
    say 'i'"):"'sb%freq%name_ss[i]'
  next
  let  iss /prompt "Enter number of the chosen Spectral Setup"
endif
!
! Check lines associated to chose spectral setup
!
! First basebands
let nlinebb dimbb[2] /new integer
let countbb 0 /new integer
def integer ilinebb[nlinebb] ss[nlinebb]
for i 1 to nlinebb
  if (sb%bb%address[2,i].eq.iss) then
     countbb = countbb+1
     ilinebb[countbb] = i
     ss[countbb] = sb%bb%address[2,i]
  endif
next
! Then spectral windows
let nlinespw dimspw[2] /new integer
let countspw 0 /new integer
def integer ilinespw[nlinespw] bb[nlinespw]
for i 1 to nlinespw
  if (sb%spw%address[2,i].eq.iss) then
     countspw = countspw+1
     ilinespw[countspw] = i
     bb[countspw] = sb%spw%address[4,i]
  endif
next
!
! And now do the spectral setup "a la astro" the chosen spectral setup
!
if (.not.exist(lo1_f_ghz)) then
   def double lo1_f_ghz[nss] rest_f_ghz[nss] if1_f_mhz[nss] 
   def double center_f_ghz[nlinespw]
   def double spw_width_mhz center_f_mhz lo2_f_mhz[nlinebb]
   def integer n_pol
endif
!
! Convert frequencies into something ASTRO likes
!
if (sb%freq%rest_f_unit[iss].eq."GHz") then
   let rest_f_ghz[iss] sb%freq%rest_f[iss]
else if (sb%freq%rest_f_unit[iss].eq."MHz") then
   let rest_f_ghz[iss] sb%freq%rest_f[iss]/1000
endif
if (sb%freq%lo1_f_unit[iss].eq."GHz") then
   let lo1_f_ghz[iss] sb%freq%lo1_f[iss]
else if (sb%freq%lo1_f_unit[iss].eq."MHz") then
   let lo1_f_ghz[iss] sb%freq%lo1_f[iss]/1000
endif
let if1_f_mhz[iss] abs(rest_f_ghz[iss]-lo1_f_ghz[iss])*1000
!
! Tune the receiver
!
say " "
say "... tuning the receiver"
say " "
if .not.exist(toto)
  def double toto tata
endif
let toto lo1_f_ghz[iss]+6d0
let tata 6000d0
frequency 'sb%freq%name_ss[iss]' toto USB tata
say " "
say "... receiver tuned, LO1 ="'lo1_f_ghz[iss]'"GHz"
say " "
!
! Now basebands
!
say "... defining the basebands"
say " "
if .not.(exist(k))
   defin integ k l
   define integer istart iend
   define real use
endif
let istart ilinebb[1]
let iend ilinebb[countbb]
let k 0
for i istart to iend
   let k k+1
   !
   ! Convert center frequencies in GHz
   !
   if (sb%bb%center_f_unit[i].eq."GHz") then
      let center_f_ghz[i] sb%bb%center_f[i]
   else if (sb%bb%center_f_unit[i].eq."MHz") then
      let center_f_ghz[i] sb%bb%center_f[i]/1000
   endif
   !
   ! ... and LO2 frequencies in MHz
   if (sb%bb%lo2_f_unit[i].eq."GHz") then
      let lo2_f_mhz[i] sb%bb%lo2_f[i]*1000
   else if (sb%bb%lo2_f_unit[i].eq."MHz") then
      let lo2_f_mhz[i] sb%bb%lo2_f[i]
   endif
   !
   ! Take the *actual* center frequency, not the desired one
   ! (accounting for chosen value of LO2) 
   !
   if (center_f_ghz[i].gt.lo1_f_ghz[iss]) then
     let center_f_ghz[i] lo1_f_ghz[iss]+lo2_f_mhz[i]/1000.-3.0
   else
     let center_f_ghz[i] lo1_f_ghz[iss]-lo2_f_mhz[i]/1000.+3.0
   endif
   base k center_f_ghz[i]
   say " "
   say "... baseband "'k'": sky frequency "'center_f_ghz[i]'" GHz; LO2=" -
    'sb%bb%lo2_f[i]'"GHz"
   say " "
next
!
! Now Spectral windows
!
say "... defining the spectral windows"
say " "
let k 0
let istart ilinespw[1]
let iend ilinespw[countspw]
for i istart to iend
   let k k+1
   ! Find width for ASTRO
   if (sb%spw%eff_width_unit[i].eq."GHz") then
      let spw_width_mhz sb%spw%eff_width[i]*1000
   else if (sb%spw%eff_width_unit[i].eq."MHz") then
      let spw_width_mhz sb%spw%eff_width[i]
   endif
   !
   ! Effective in .aot file, nominal in ASTRO (eh oui ...)
   ! 
   if (spw_width_mhz.lt.31.25) then
      let spw_width_mhz 31.25 
   else if (spw_width_mhz.lt.62.5) then
      let spw_width_mhz 62.5
   else if (spw_width_mhz.lt.125) then
      let spw_width_mhz 125
   else if (spw_width_mhz.lt.250) then
      let spw_width_mhz 250
   else if (spw_width_mhz.lt.500) then
      let spw_width_mhz 500
   else if (spw_width_mhz.lt.1000) then
      let spw_width_mhz 1000
   else
      let spw_width_mhz 2000
   endif
   !
   ! Convert center freq in MHz
   ! 
   if (sb%spw%center_f_unit[i].eq."GHz") then
      let center_f_mhz sb%spw%center_f[i]*1000
   else if (sb%spw%center_f_unit[i].eq."MHz") then
      let center_f_mhz sb%spw%center_f[i]
   endif
   !
   ! Derive number of pol. from .aot keywort
   !
   let l bb[k]+istart-1 
   let center_f_mhz lo2_f_mhz[l]-(6000.-center_f_mhz)
   if (sb%spw%pol[i].eq."XX".or.sb%spw%pol[i].eq."YY") then
      let n_pol 1
   else if (sb%spw%pol[i].eq."XX,YY") then
      let n_pol 2
   else
      let n_pol 4
   endif
   !
   ! ... and compute correlator use
   ! 
   if (sb%spw%num_chan_eff[i].lt.300) then
     let use sb%spw%num_chan_eff[i]/248
   else
     let use sb%spw%num_chan_eff[i]*16/15/8192
   endif
   let use nint(use*8)/8
   let use use*n_pol*100
   spw sb%spw%address[5,i] spw_width_mhz center_f_mhz /base bb[k] /pol n_pol /use use
   if (sb%spw%sb[i].eq."USB") then 
      let center_f_mhz lo1_f_ghz[iss]*1000.+center_f_mhz
   else
      let center_f_mhz lo1_f_ghz[iss]*1000.-center_f_mhz
   endif
   say " "
   say "... spectral window "'k'": sky frequency "'center_f_mhz/1000' -
    " GHz; Width = " -
    'spw_width_mhz'"MHz ; Number of polarization "'n_pol'
   say " "
next
! 
! Small extra to get desired lines
!
if ("&1none".eq."none") then
  plot frequency /molecule
else
  plot /molecule
  plot /molecule &1
endif
say " "
say "... Instrument Setup "'iss'"/"'nss'" in this SB"
say " "
sic dir ..
sic dir
