!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! J.Pety 2014
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin data gag_scratch:noema-sensi.hlp
1 NOEMA sensitivity estimator

  Test version => Stay critical.
  Phase I  will be finished in 2017.
  Phase II will be finished in 2019?

  The STATUS is divided into the project BASELINE (correlator spectral
  resolution between 0.0625 and 2 MHz) and the project OPTIONs (correlator
  spectral resolution between 0.0078 and 2 MHz).

  A value of 0 for the angular resolution, the atmospheric temperature, the
  amount of precipitable water vapor will try to make a clever guess. Any other
  value will override the default.
end data  gag_scratch:noema-sensi.hlp
!
begin data gag_scratch:noema-sensi.init
  TASK\REAL "Frequency [GHz]"                  FREQ$      'noema%freq'
  TASK\REAL "Bandwidth [MHz]"                  BANDWIDTH$ 'noema%fres'
  TASK\INTE "Number of polarizations"          NPOL$      'noema%npol'
  TASK\REAL "Tsys [K]"                         TSYS$      'noema%tsys'
  TASK\REAL "Decorrelation coefficient"        DECORREL$  'noema%decorrel'
  TASK\REAL "On-source integration time [hrs]" ONTIME$    'noema%ontime'
  TASK\INTE "Number of available antennas"     NANT$      'noema%nant'
  TASK\REAL "Antenna efficiency [Jy/K]"        ANTEFF$    'noema%anteff'
  TASK\REAL "Beam [arcsec]"                    BEAM$[2]   'noema%synbeam[1]' 'noema%synbeam[2]'
  TASK\GO
end data gag_scratch:noema-sensi.init
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-definition
  !
  if .not.exist(noema) then
     define structure noema /global
  endif
  if .not.exist(noema%clight) then
     define character noema%phase*2 noema%status*8 noema%season*8 noema%sideband*3 noema%obs_kind*10 /global
     define integer noema%rx_year noema%nant noema%npol /global
     define double noema%temp noema%water noema%decl /global
     define double noema%freq noema%vres noema%fres noema%fresuser noema%fresvals[3] /global
     define double noema%decorrel noema%obstime noema%ontime noema%anteff noema%synbeam[2] noema%tsys /global
     define double noema%beam noema%beam100[3] /global
     define integer noema%nconf /global
     let noema%nconf 3
     define character noema%config*2 noema%configs*2[noema%nconf] /global
     define double noema%clight /global
     define logical noema%widefield /global
     define double noema%area /global
  endif
  !
end procedure noema-sensitivity-definition
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-default
  !
  ! Reasonnable defaults
  !
  let noema%clight 2.99792458e8 ! [m/s]
  let noema%phase    "II"
  let noema%status   "baseline"
  let noema%season   "winter"
  let noema%obs_kind "line"
  let noema%config   "D"
  let noema%nant         12 ! Maximum number of antennas
  let noema%npol          2 ! Always the same tuning for both polars
  let noema%temp          0 ! [   K] 0 => clever default
  let noema%water         0 ! [  mm] 0 => clever default
  let noema%decl         45 ! [ deg]
  let noema%fresuser 0.0625 ! [ MHz]
  let noema%vres          0 ! [km/s]
  let noema%obstime       8 ! [ hrs]
  let noema%freq        230 ! [ GHz]
  let noema%beam          0 ! [  arcsec]  0 => clever default
  let noema%widefield   yes ! [--------] No => Single field
  let noema%area          1 ! [arcmin^2] 
  !
end procedure noema-sensitivity-default
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-linear-inter
  !
  define double x1 x2 y1 y2 alin blin
  let x1 &2
  let x2 &3
  let y1 &4
  let y2 &5
  !
  let alin (y2-y1)/(x2-x1)
  let blin y1-alin*x1
  let &1 alin*noema%freq+blin
  !
end procedure noema-sensitivity-linear-inter
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-estimation
  !
  ! Noema defaults...
  observatory bure
  set azimuth south
  !
  let noema%status 'noema%status' /lower
  let noema%phase 'noema%phase' /upper
  let noema%configs "A" "BC" "D"
  if (noema%phase.eq."I") then
     let noema%rx_year 2017
     let noema%nant 10
     let noema%beam100 1.0 2.0 3.5 ! ["] From J.Boissier's report
  else if (noema%phase.eq."II") then
     let noema%rx_year 2019
     let noema%nant 12
     let noema%beam100 0.5 1.0 2.5 ! ["] From J.Boissier's report
  else
     sic message e noema-sensitivity "Unknown NOEMA phase :"'noema%phase'
     return base
  endif
  !
  ! Frontend/Backend specifications
  define integer nrec nfreq
  let nrec  4 ! Number of receivers
  define double freqrange[2,nrec] fresrange[2] gim[nrec] contbandwidth[nrec] iffreq
  let freqrange[1]    72 116 ! [GHz] Receiver 1
  let freqrange[2]   127 179 ! [GHz] Receiver 2
  let freqrange[3]   200 276 ! [GHz] Receiver 3
  let freqrange[4]   275 373 ! [GHz] Receiver 4
  !
  if (noema%status.eq."baseline") then
     let fresrange 0.0625 15488 ! [MHz] Spectral resolution range of correlator
  else if (noema%status.eq."options") then
     let fresrange 0.0078 15488 ! [MHz] Spectral resolution range of correlator
  else
     sic message e noema-sensitivity "Unknown NOEMA status :"'noema%status'
     return base
  endif
  let noema%fresvals 0.0625 0.250 2.00
  !
  let gim            0.05 ! [   ] Image gains (13 dB)
  let iffreq        7.744 ! [GHz] Intermediate Frequency
  let contbandwidth 15488 ! [MHz] Continuum bandwidth
  !
  ! Trec values (From A.Navarrini on Feb. 18th, 2014)
  !
  let nfreq 2 ! Number of frequencies used to define Trec
  define double temps[nfreq,2,nrec]
  ! Receiver 1
  let temps[1,1]   72 116 ! [GHz] Frequencies
  let temps[2,1]   35  35 ! [K]   Trec
  ! Receiver 2
  let temps[1,2]  127 179 ! [GHz] Frequencies
  let temps[2,2]   50  50 ! [K]   Trec
  ! Receiver 3
  let temps[1,3]  200 276 ! [GHz] Frequencies
  let temps[2,3]   60  60 ! [K]   Trec
  ! Receiver 4
  let temps[1,4]  275 373 ! [GHz] Frequencies
  let temps[2,4]   70  70 ! [K]   Trec
  ! Limit the number of receivers for the 2014 generation...
  if (noema%rx_year.eq.2017) then
     let nrec 3
  endif
  !
  ! Downcase...
  let noema%season   'noema%season'   /lower
  let noema%obs_kind 'noema%obs_kind' /lower
  !
  ! Available frequency ranges
  define integer irec
  let irec 0
  for jrec 1 to nrec
     if ((freqrange[1,jrec].le.noema%freq).and.(noema%freq.le.freqrange[2,jrec])) then
        let irec jrec
     endif
  next jrec
  if (irec.eq.0) then
     message e noema-sensitivity "Frequency outside receiver range"
     message i noema-sensitivity "Available frequency range:"
     for jrec 1 to nrec
        message i noema-sensitivity "   * Receiver "'jrec'": "'freqrange[1,jrec]'"-"'freqrange[2,jrec]'" GHz"
     next jrec
     return base
  endif
  let noema%sideband "usb"
  if ((irec.eq.1).and.(noema%freq.le.100)) then
     let noema%sideband "lsb"
  else if ((irec.eq.2).and.(noema%freq.le.155)) then
     let noema%sideband "lsb"
  else if ((irec.eq.3).and.(noema%freq.le.240)) then
     let noema%sideband "lsb"
  else if ((irec.eq.4).and.(noema%freq.le.335)) then
     let noema%sideband "lsb"
  endif
  !
  ! Available frequency resolutions
  if (noema%obs_kind.eq."continuum") then
     message i noema-sensitivity "Continuum observation"
     let noema%fres 'contbandwidth[irec]'
     let noema%vres 0.
     let noema%npol 2
  else if (noema%obs_kind.eq."line") then
     message i noema-sensitivity "Line observation"
     if (noema%vres.ne.0) then
        let noema%fres '1e6*noema%vres*noema%freq/noema%clight'
     else 
        let noema%fres 'noema%fresuser'
     endif
     if ((noema%fres.lt.fresrange[1]).or.(noema%fres.gt.fresrange[2])) then
        say "E-NOEMA-SENSITIVITY, Frequency resolution outside correlator range"
        say "I-NOEMA-SENSITIVITY, Available range: "'fresrange[1]'"-"'fresrange[2]'" MHz"
        return base
     endif
  else
     message e noema-sensitivity "Observation kind unavailable: "'noema%obs_kind'
     return base
  endif
  !
  ! Number of antenna (summer is maintenance season)
  if (noema%season.eq."summer") then
     let noema%nant noema%nant-1
  endif
  !
  ! Antenna efficiency values
  if (noema%rx_year.ge.2017) then
     if (irec.eq.1) then
        let noema%anteff 22
     else if (irec.eq.2) then
        let noema%anteff 24
     else if (irec.eq.3) then
        let noema%anteff 28
     else if (irec.eq.4) then
        let noema%anteff 35
     endif
  else
     message e noema-sensitivity "Unknown receiver generation :"'noema%rx_year'
     return base
  endif
  !
  ! Decorrelation coefficient
  if (noema%rx_year.ge.2017) then
     if (noema%season.eq."winter") then
        if (irec.eq.1) then
           let noema%decorrel 0.9
        else if (irec.eq.2) then
           let noema%decorrel 0.85
        else if (irec.eq.3) then
           let noema%decorrel 0.8
        else if (irec.eq.4) then
           let noema%decorrel 0.7
        endif
     else
        if (irec.eq.1) then
           let noema%decorrel 0.9
        else if (irec.eq.2) then
           let noema%decorrel 0.85
        else if (irec.eq.3) then
           let noema%decorrel 0.8
        else if (irec.eq.4) then
           let noema%decorrel 0.7
        endif
     endif
  else
     message e noema-sensitivity "Unknown receiver generation :"'noema%rx_year'
     return base
  endif
  !
  ! Tsys
  define integer jfreq[2] ifreq1 ifreq2
  compute jfreq loc temps[1,irec] noema%freq
  let ifreq1 jfreq[1]
  let ifreq2 jfreq[2]
  if (ifreq1.eq.ifreq2) then
     let ifreq1 ifreq1-1
  endif
  @ noema-sensitivity-linear-inter trec 'temps[ifreq1,1,irec]' 'temps[ifreq2,1,irec]' -
                                        'temps[ifreq1,2,irec]' 'temps[ifreq2,2,irec]'
  let forward_eff 0.95
  let gain_image gim[irec]
  if (noema%temp.eq.0) then
     if (noema%season.eq."winter") then
        let temperature 273
     else
        let temperature 283
     endif
  else
     let temperature noema%temp
  endif
  if (noema%water.eq.0) then
     if (noema%season.eq."winter") then
        let water 2.5
     else
        let water 5
     endif
  else
     let water noema%water
  endif
  let freq_sig 'noema%freq'
  if (noema%sideband.eq."usb") then
     let freq_ima 'noema%freq-2*iffreq'
  else if (noema%sideband.eq."lsb") then
     let freq_ima 'noema%freq+2*iffreq'
  else
     message e noema-sensitivity 'noema%sideband'" unavailable"
     return base
  endif
  !
  ! Here I compute the elevation "au passage au meridien". This obviously
  ! is optimistic. To have a better estimate, we should take an average Tsys
  ! value over the range of accessible elevations:
  ! Tsys_ave = sqrt(< Tsys(elev)^2 >)
  !
  !  time 00:00:00.000
  !  source COUCOU EQ 2000 00:00:00.0000 'noema%decl*pi/rad'
  define double myelevation
  let myelevation 90-45+noema%decl
  let myelevation myelevation*pi/180
  let airmass abs(1/sin(myelevation))
  delete /var myelevation
  atmosphere
  let noema%tsys tsys
  !
  message i noema-sensitivity "       Signal freq: "'freq_sig'" GHz"
  message i noema-sensitivity "        Image freq: "'freq_ima'" GHz"
  message i noema-sensitivity "        Image gain: "'gain_image'
  message i noema-sensitivity "  Atm. Temperature: "'temperature'" K"
  message i noema-sensitivity "              Trec: "'trec'" K"
  message i noema-sensitivity "Precipitable water: "'water'" mm"
  message i noema-sensitivity "           Airmass: "'airmass'
  !
  ! Integration time
  let noema%ontime noema%obstime/1.6 ! Here I take into account the overall observatory efficiency
  if (noema%widefield) then
     ! Wide field imaging => Compute the number of independent fields
     define double lambda diameter beam_fwhm beam_area nfields
     let lambda noema%clight/(freq_sig*1e9)
     let diameter 15
     let beam_fwhm 1.2*lambda/diameter/(60*sec)
     let beam_area 0.8*beam_fwhm^2*pi/(4*log(2)) ! The 0.8 tries to take into account the primary beam truncation.
     let nfields noema%area/beam_area
     let noema%ontime noema%ontime/nfields
     !
     message i noema-sensitivity " "
     message i noema-sensitivity "          Wavelength: "'lambda'" m"
     message i noema-sensitivity "            Diameter: "'diameter'" m"
     message i noema-sensitivity "           Beam FWHM: "'beam_fwhm*60'" arcsec"
     message i noema-sensitivity "           Beam area: "'beam_area'" arcmin^2"
     message i noema-sensitivity "          Field area: "'noema%area'" arcmin^2"
     message i noema-sensitivity "        Nindependent: "'nfields'
     message i noema-sensitivity "On-time per position: "'noema%ontime'
  endif
  !
  ! Spatial resolution
  if (noema%beam.ne.0) then
     let noema%synbeam 'noema%beam'
  else
     define integer jconf
     for iconf 1 to noema%nconf
        if (noema%config.eq.noema%configs[iconf]) then
           let jconf iconf
           break
        endif
     next ! iconf
     if (jconf.le.noema%nconf) then
        let noema%synbeam  noema%beam100[jconf]*100/freq_sig
     else
        message e noema-sensitivity "Unknown configuration: "'noema%config'
        return base
     endif
     delete /var jconf
  endif
  !
  ! Computes
  run inter-sensitivity gag_scratch:noema-sensi /nowindow
  !
end procedure noema-sensitivity-estimation
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-gui
  !
  gui\panel "Noema Sensitivity Estimator (J.Pety)" gag_scratch:noema-sensi.hlp
     sic\let noema%phase        'noema%phase'     /prompt "Phase"  /choice "II" "I"
     sic\let noema%status       'noema%status'    /prompt "Status" /choice "baseline" "options"
     sic\let noema%season       'noema%season'    /prompt "Observing session" /choice "summer" "winter"
     sic\let noema%config       'noema%config'    /prompt "Configuration" /choice "A" "BC" "D" !'noema%configs[1]' 'noema%configs[2]' 'noema%configs[3]'
     sic\let noema%obs_kind     'noema%obs_kind'  /prompt "Observation kind" /choice "continuum" "line"
     sic\let noema%decl         'noema%decl'      /prompt "Source declination [deg]"
     sic\let noema%freq         'noema%freq'      /prompt "Observing Frequency [GHz]"
     sic\let noema%fresuser     'noema%fresuser'  /prompt "Frequency resolution [MHz]" /choice 0.0625 0.250 2.00 !'noema%fresvals[1]' 'noema%fres%vals[2]' 'noema%fres%vals[3]'
     sic\let noema%obstime      'noema%obstime'   /prompt "Observatory time [hrs]"
     say ""
     sic\let noema%widefield    'noema%widefield' /prompt "Single (No) / Wide (Yes) Field"
     sic\let noema%area         'noema%area'      /prompt "Field area [arcmin^2]"
     say ""
     sic\let noema%water        'noema%water'     /prompt "Precipitable water amount [mm]" /choice 5 3 2 1 0
     sic\let noema%vres         'noema%vres'      /prompt "Velocity  resolution [km/s]"
     sic\let noema%beam         'noema%beam'      /prompt "Spatial resolution [arcsec]"
     sic\let noema%temp         'noema%temp'      /prompt "Atmospheric temperature [K]"
  gui\go "@ noema-sensitivity-estimation"
  !
end procedure noema-sensitivity-gui
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
@ noema-sensitivity-definition
@ noema-sensitivity-default
@ noema-sensitivity-gui
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
