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

     Tool to compute sensitivities in preparation for observations with NOEMA.
     The PROPOSAL mode used to follow precisely the capabilities offered in the call 
     for proposals. It is now replaced by the online estimator (http://oms.iram.fr/tse/#noema)
     The DETAILED mode provides more flexibility. The main difference 
     is that the System Temperature is interpolated from tabulated values for the PROPOSAL 
     mode, while it is computed using an atmosphere model (with tunable parameters) 
     in the DETAILED mode.
     
     The computations performed in PROPOSAL mode are described in a dedicated document 
     accessible from the widget menu Help > NOEMA > astro-noema-time-estimator.pdf

2 NOEMA%SENSITIVITY%NANTUSER
     In DETAILED mode, the user defines the number of antennas to be used. 
     Not available in PROPOSAL mode, where the actual number of antennas is used.

2 NOEMA%SENSITIVITY%RX_KIND 
       Choose the backend/receiver generation to be considered. 
       + PolyFiX = NOEMA receiver and PolyFiX correlator
       + Widex/NB = NOEMA receiver and Widex/NB correlator
       + 2006 = 2006 "new generation" receiver and Widex/NB correlator (before NOEMA)
       + 1995 = 1995 receiver and corresponding correlator

2 NOEMA%SENSITIVITY%SEASON
     Summer or Winter characteristics (number of available antennas, Tsys)

2 NOEMA%SENSITIVITY%OBS_KIND
     Line to compute sensitivity in a given spectral resolution
     Continuum to compute the sensitivity over the full available bandwidth.
     For continuum the input frequency should be the LO frequency of the tuning.
     Continuum is not available in Detailed mode for PolyFiX hardware.

2 NOEMA%SENSITIVITY%BW_KIND
     In line mode, spectral resolution can be given in frequency (MHz) or velocity (km/s)

2 NOEMA%SENSITIVITY%SIDEBAND
     Observations can be USB or LSB. Only in Detailed mode.

2 NOEMA%SENSITIVITY%NPOL
     Number of polarizations averaged in the final product

2 NOEMA%SENSITIVITY%DECL
     Declination of the source (Deg). Used to determine System Temperature (Tsys) 
     at source transit. In PROPOSAL mode, Tsys is interpolated from a pre-computed table.
     In DETAILED mode, an atmospere model is used to compute the Tsys according to the 
     Ambiant Temperature and Precipitable Water Amount (PWV) set by the user.

2 NOEMA%SENSITIVITY%FREQ
     Sky frequency of the observation (GHz).
     For PolyFiX CONTINUUM projects in PROPOSAL mode, LO frequency of the tuning (GHz)

2 NOEMA%SENSITIVITY%VRES
     Spectral resolution in km/s.
     Provided by the user if BW_KIND is velocity, computed if BW_KIND is frequency

2 NOEMA%SENSITIVITY%FRES
     Spectral resolution in MHz.
     Provided by the user if BW_KIND is frequency, computed if BW_KIND is velocity

2 NOEMA%SENSITIVITY%TELTIME
     Total telescope time (all sources/fields summed) in hours. It cannot be shorter 
     than 40minutes (0.67h) because any project requires a fixed time for tuning and 
     basic calibration (RF, Flux,...). The on source time is obtained dividing the total 
     time by the observing efficiency (1.6) when the total time is longer than 3.2h 
     (2h on source * 1.6). 
     For projects between 40min and 3.2h, an time offset is added to the on source time.
     The offset value decreases from 40minutes (when total time is 40minutes) to 0minutes 
     (when the total time is 3.2h)

2 NOEMA%SENSITIVITY%SYNBEAM
     Expected synthesized beam major and minor axes, in arcsec

2 NOEMA%SENSITIVITY%MOSAIC
     To select the mosaic mode

2 NOEMA%SENSITIVITY%AREA
     In mosaic mode, size of the images area (in arcmin^2)

2 NOEMA%SENSITIVITY%TRSH
     To select the track sharing mode

2 NOEMA%SENSITIVITY%NSOU
     Number of sources in track sharing mode

2 NOEMA%SENSITIVITY%TEMP
     in DETAILED mode, the user might set the ambient temperature (K) that will be 
     used to compute the system temperature. Default is 273K for winter and 283K for summer.

2 NOEMA%SENSITIVITY%WATER
     in DETAILED mode, the user might set the precipitable water amount (PWV, in mm) 
     that will be used to compute the system temperature. 
     Default is 3mm for winter and 7mm for summer.

end data gag_scratch:noema-sensi.hlp
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin data gag_scratch:noema-sensi.init
  TASK\REAL "Frequency [GHz]"                  FREQ$      'noema%sensitivity%freq'
  TASK\REAL "Bandwidth [MHz]"                  BANDWIDTH$ 'noema%sensitivity%fres'
  TASK\INTE "Number of polarizations"          NPOL$      'noema%sensitivity%npol'
  TASK\REAL "Tsys [K]"                         TSYS$      'noema%sensitivity%tsys'
  TASK\REAL "Decorrelation coefficient"        DECORREL$  'noema%sensitivity%decorrel'
  TASK\REAL "On-source integration time [hrs]" ONTIME$    'noema%sensitivity%ontime'
  TASK\INTE "Number of available antennas"     NANT$      'noema%sensitivity%nant'
  TASK\REAL "Antenna efficiency [Jy/K]"        ANTEFF$    'noema%sensitivity%anteff'
  TASK\REAL "Beam [arcsec]"                    BEAM$[2]   'noema%sensitivity%synbeam[1]' 'noema%sensitivity%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 exist(noema%sensitivity) then
    delete /var noema%sensitivity
  endif
  define structure noema%sensitivity /global
  define character noema%sensitivity%season*8  noema%sensitivity%obs_kind*10 noema%sensitivity%rx_kind*8 /global 
  define character noema%sensitivity%sideband*3 noema%sensitivity%bw_kind*9 /global
  define integer noema%sensitivity%npol noema%sensitivity%nsou /global 
  define integer noema%sensitivity%nant noema%sensitivity%nantcall noema%sensitivity%nantuser noema%sensitivity%nantdef /global
  define logical noema%sensitivity%mosaic noema%sensitivity%trsh /global
  define double noema%sensitivity%temp noema%sensitivity%water noema%sensitivity%freq /global
  define double noema%sensitivity%declmin noema%sensitivity%decl noema%sensitivity%elev /global
  define double noema%sensitivity%fres noema%sensitivity%vres noema%sensitivity%area /global
  define double noema%sensitivity%decorrel  noema%sensitivity%anteff  noema%sensitivity%synbeam[2] noema%sensitivity%tsys noema%sensitivity%pwv /global
  define double noema%sensitivity%soutime noema%sensitivity%ontime  noema%sensitivity%teltime /global
  define double noema%sensitivity%tracktime noema%sensitivity%ntrack /global
  define double noema%sensitivity%obseff noema%sensitivity%obseffref /global 
  define double noema%sensitivity%ttune  /global
  define structure noema%sensitivity%mos /global
  define double noema%sensitivity%mos%field2point /global
  define double noema%sensitivity%mos%eta /global
  define double noema%sensitivity%mos%tintmin noema%sensitivity%mos%tintstd /global
  define double noema%sensitivity%mos%tslew noema%sensitivity%mos%tcycle /global
  ! Factorize parameters 
  define double noema%sensitivity%clight noema%sensitivity%burelat /global
  define double noema%sensitivity%ant_diameter /global
  define double noema%sensitivity%tsummer noema%sensitivity%twinter  /global
  define double noema%sensitivity%pwvsummer noema%sensitivity%pwvwinter  /global
  ! Factorize receiver/correlator properties - PolyFiX case only
  define double noema%sensitivity%pfx_gim noema%sensitivity%pfx_contbw noema%sensitivity%pfx_iffreq /global
  define double noema%sensitivity%pfx_resmin noema%sensitivity%pfx_resmax /global
  define integer noema%sensitivity%pfx_nrec /global
  def integer mrec
  let mrec 4
  define double noema%sensitivity%pfx_winterpwv[mrec] noema%sensitivity%pfx_summerpwv[mrec] /global
  define double noema%sensitivity%pfx_fmin[mrec] noema%sensitivity%pfx_fmax[mrec] /global ! limits for line
  define double noema%sensitivity%pfx_flomin[mrec] noema%sensitivity%pfx_flomax[mrec] /global ! limits for cont
  define double noema%sensitivity%pfx_trec[mrec] noema%sensitivity%pfx_anteff[mrec]  /global
  define double noema%sensitivity%pfx_feff[mrec] /global
  define double noema%sensitivity%pfx_decorrel_winter[mrec] noema%sensitivity%pfx_decorrel_summer[mrec] /global
  !
end procedure noema-sensitivity-definition
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-default
  !
  ! Constant
  let noema%sensitivity%clight 2.99792458e8 ! [m/s]
  let noema%sensitivity%burelat 44.63 ! bure latitude [deg]
  let noema%sensitivity%ant_diameter 15
  ! Parameters
  let noema%sensitivity%nantdef 10 ! actual number of antennas at NOEMA site
  let noema%sensitivity%declmin    -30 ! minimal accepted declination [deg]
  let noema%sensitivity%mos%field2point (7d0/4d0)^2 ! conversion from number of fields to number of pointing for mosaic (assuming standard sampling)
  let noema%sensitivity%mos%tintstd 45   ! [sec] Standard integration time at bure
  let noema%sensitivity%mos%tintmin 10   ! [sec] Minimum integration time authorized to stay efficient enough
  let noema%sensitivity%mos%tslew   11    ! [sec] Time to slew from one pointing to the other one [RN/NC 2019]
  let noema%sensitivity%mos%tcycle 45*60 ! [sec] Maximum time to cycle over all pointings of the mosaic. Twice the typical calibration time
  let noema%sensitivity%mos%eta     2    ! [] 
  let noema%sensitivity%obseffref   1.6  ! Observations efficiency during observing cycles
  let noema%sensitivity%ttune       40/60 ! Tuning time [h] for a project is 40min
  let noema%sensitivity%pfx_iffreq 7.744 ! if frequency GHz
  let noema%sensitivity%pfx_gim 0.05 ! Image gain
  let noema%sensitivity%pfx_contbw 15488 ! continuum bandwitdh in 1 polar - MHz
  let noema%sensitivity%pfx_resmin 0.0625 ! [MHz] Spectral resolution range of correlator
  let noema%sensitivity%pfx_resmax 15488 ! [MHz] Spectral resolution range of correlator
  let noema%sensitivity%pfx_nrec 4 ! number of receiver bands
  let noema%sensitivity%pfx_fmin 70.384  124.384 196.128 275.2 ! Lower limit GHz - for lines
  let noema%sensitivity%pfx_fmax 121.616 183.616 279.616 372.8 ! Upper limit GHz - for lines
  let noema%sensitivity%pfx_flomin  82 136 207.744 275  ! Lower limit GHz - for continuum
  let noema%sensitivity%pfx_flomax 110 172 268 373  ! Upper limit GHz - for continuum
  let noema%sensitivity%pfx_summerpwv 7 5 4 4 ! pwv per band in summer conditions MK S18
  let noema%sensitivity%pfx_winterpwv 5 4 3 2.5 ! pwv per band in winter conditions [MK W18]
  let noema%sensitivity%pfx_trec   35 45 55 0 ! trec as provided by Nichol - Band 4 still hardcoded
  let noema%sensitivity%pfx_anteff 22 26 33 45 ! Jy/K
  let noema%sensitivity%pfx_decorrel_winter 0.9 0.85 0.8 0.7  ! decorrelation
  let noema%sensitivity%pfx_decorrel_summer 0.9 0.8 0.6 0.5  ! decorrelation
  let noema%sensitivity%pfx_feff 0.96 0.94 0.9 0.9 ! forward efficiency
  ! Reasonnable defaults
  let noema%sensitivity%rx_kind  "PolyFiX"
  let noema%sensitivity%season   "winter"
  let noema%sensitivity%sideband "lsb"
  let noema%sensitivity%obs_kind "line"
  let noema%sensitivity%bw_kind  "velocity"
  let noema%sensitivity%npol        2 ! 2 polars because most of the time, both polars share the same setup
  let noema%sensitivity%temp        0 ! [K]  0 => clever default
  let noema%sensitivity%water       0 ! [mm] 0 => clever default
  let noema%sensitivity%decl       25 ! [deg]
  let noema%sensitivity%vres     0.25 ! [km/s]
  let noema%sensitivity%teltime     8 ! [hrs]
  let noema%sensitivity%freq      230 ! [GHz]
  let noema%sensitivity%synbeam     1 ! [arcsec]
  let noema%sensitivity%mosaic      no ! no = single field
  let noema%sensitivity%area        0 !arcmin^2
  let noema%sensitivity%trsh        no ! no = single field
  let noema%sensitivity%nsou        1 !
  let noema%sensitivity%twinter 273 ! K
  let noema%sensitivity%tsummer 283 ! K
  let noema%sensitivity%pwvwinter 3 ! mm
  let noema%sensitivity%pwvsummer 7 ! mm
  !
end procedure noema-sensitivity-default
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-linear-inter
  !
  define double x1 x2 y1 y2 alin blin
  let x1 &3
  let x2 &4
  let y1 &5
  let y2 &6
  !
  let alin (y2-y1)/(x2-x1)
  let blin y1-alin*x1
  let &1 alin*&2+blin
  !
end procedure noema-sensitivity-linear-inter
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-estimation
  !
  ! Defaults...
  if (noema%sensitivity%rx_kind.eq."PolyFiX") then
    if (observatory.ne."NOEMA") then
      observatory NOEMA
    endif
  else
    if (observatory.ne."BURE") then
      observatory BURE
    endif
  endif
  !
  set azimuth south
  !
  ! Check usage
  if (pro%narg.ne.1) then
     message e NOEMA-SENSITIVITY-ESTIMATOR "Usage: @ noema-sensitivity-estimation proposal|detailed"
     return base
  endif
  define character mode*12
  let mode "&1" /lower
  !
  ! Frontend/Backend specifications
  define integer nrec nfreq
  if (noema%sensitivity%rx_kind.eq."PolyFiX") then
     ! NOEMA receivers
     let nrec  noema%sensitivity%pfx_nrec ! Number of receivers
     define double freqrange[2,nrec] fresrange[2] gim[nrec] contbandwidth[nrec] iffreq obspwv[nrec]
     !Frequency ranges
     if (noema%sensitivity%obs_kind.eq."line") then
       for j 1 to nrec
         let freqrange[j]    noema%sensitivity%pfx_fmin[j] noema%sensitivity%pfx_fmax[j]
       next
     else if (noema%sensitivity%obs_kind.eq."continuum") then
       if (mode.ne."proposal") then
          message e NOEMA-SENSITIVITY-ESTIMATOR "Continuum sensitivity estimation not implemented for PolyFiX in DETAILED mode"
          message e NOEMA-SENSITIVITY-ESTIMATOR "Please use the online estimator: http://oms.iram.fr/tse/#noema"
          return base
       endif
       for j 1 to nrec
         let freqrange[j]    noema%sensitivity%pfx_flomin[j] noema%sensitivity%pfx_flomax[j]
       next 
     endif
     let fresrange    noema%sensitivity%pfx_resmin noema%sensitivity%pfx_resmax
     !
     let gim            noema%sensitivity%pfx_gim     ! [   ] Image gains
     let iffreq         noema%sensitivity%pfx_iffreq  ! [GHz] Intermediate Frequency
     let contbandwidth  noema%sensitivity%pfx_contbw  ! [MHz] Continuum bandwidth
     !
     if (mode.eq."proposal") then
        ! Now we interpolate Tsys at zenith and then correct for elevation effect
        ! We just need the PWV assumed for the frequency
        if (noema%sensitivity%season.eq."winter") then
           let obspwv noema%sensitivity%pfx_winterpwv ! to be updated
        else
           ! Summer
           let obspwv noema%sensitivity%pfx_summerpwv ! to be updated
        endif
     else
        ! Detailed mode
        ! Trec values site data from commissionning (31-jan-2017)
        let nfreq 2 ! Number of frequencies used to define Trec
        define double temps[nfreq,2,nrec]
        for j 1 to nrec
          let temps[1,j] 'noema%sensitivity%pfx_fmin[j]-0.1' 'noema%sensitivity%pfx_fmax[j]+0.1'   ! [GHz] Frequencies  ! Jun16 should be wider than freqrange
          let temps[2,j]   noema%sensitivity%pfx_trec[j] ! [K]   Trec!From Nichol Cunningham Jun 2018
        next
        ! Receiver 4 - not standard, done by hand
        let temps[2,4]   50  80 ! [K]   Trec !JB from actual trec measurements
     endif
  else if (noema%sensitivity%rx_kind.eq."Widex/NB") then
     ! NOEMA receiver and widex/nb backend
     let nrec  4 ! Number of receivers
     define double freqrange[2,nrec] fresrange[2] gim[nrec] contbandwidth[nrec] iffreq
     let freqrange[1]    74.7  117.8 ! [GHz] Receiver 1  JMW Jun 2016
     let freqrange[2]   128.2  179.8 ! [GHz] Receiver 2  JMW Jun 2016
     let freqrange[3]   200.2  275.8 ! [GHz] Receiver 3  JMW Jun 2016 
     let freqrange[4]   275.2  372.8 ! [GHz] Receiver 4  JMW Jan 2016
     let fresrange    0.039 3600 ! [MHz] Spectral resolution range of correlator
     !
     let gim           0.05 ! [   ] Image gains
     let iffreq           6 ! [GHz] Intermediate Frequency
     let contbandwidth 3600 ! [MHz] Continuum bandwidth
     !
     if (mode.eq."proposal") then
        let nfreq 3 ! Number of frequencies used to define Tsys
        say "W-NOEMA-SENSITIVITY-ESTIMATOR, This is not the present generation of receiver"
        define double temps[nfreq,2,nrec]
            let temps[1,1]  74 110 118 ! [GHz] Frequencies
            let temps[1,2]  128 150 180 ! [GHz] Frequencies
            let temps[1,3]  199 230 276 ! [GHz] Frequencies
            let temps[1,4]  275 350 373 ! [GHz] Frequencies
        if (noema%sensitivity%season.eq."winter") then
            ! Receiver 1
            let temps[2,1]  75  85 313 ! [K]
            ! Receiver 2
            let temps[2,2]  110 110 195 ! [K]   Tsys
            ! Receiver 3
            let temps[2,3]  190 190 190 ! [K]   Tsys
            ! Receiver 4
            let temps[2,4]  230 250 480 ! [K]   Tsys
        else ! SUMMER
           ! Receiver 1
           let temps[2,1]  85  95 323 ! [K]   Tsys
           ! Receiver 2
           let temps[2,2]  140 140 242 ! [K]   Tsys
           ! Receiver 3
           let temps[2,3]  250 250 250 ! [K]   Tsys
           ! Receiver 4
           let temps[2,4]  500 500 500 ! [K]   Tsys
        endif
     else
        ! Trec values (lab. measurements) from B.Lazareff (Aug. 2006)
        !
        let nfreq 2 ! Number of frequencies used to define Trec
        define double temps[nfreq,2,nrec]
        ! Receiver 1
        let temps[1,1]   74 118 ! [GHz] Frequencies 
        let temps[2,1]   30  30 ! [K]   Trec
        ! Receiver 2
        let temps[1,2]  129 180 ! [GHz] Frequencies
        let temps[2,2]   40  40 ! [K]   Trec
        ! Receiver 3
        let temps[1,3]  199 277 ! [GHz] Frequencies
        let temps[2,3]   45  45 ! [K]   Trec
        ! Receiver 4
        let temps[1,4]  275 373 ! [GHz] Frequencies !JB from actual trec measurements
        let temps[2,4]   50  80 ! [K]   Trec !JB from actual trec measurements
     endif
  else if (noema%sensitivity%rx_kind.eq."2006") then
     ! 2006 generation receiver and widex/nb backend
     ! Frequency limits might not be 100% true...
     let nrec  4 ! Number of receivers
     define double freqrange[2,nrec] fresrange[2] gim[nrec] contbandwidth[nrec] iffreq
     let freqrange[1]    78.2  117.8 ! [GHz] Receiver 1  
     let freqrange[2]   127.2  178.8 ! [GHz] Receiver 2  
     let freqrange[3]   199.2  268.8 ! [GHz] Receiver 3  
     let freqrange[4]   275.2  372.8 ! [GHz] Receiver 4  
     let fresrange    0.039 3600 ! [MHz] Spectral resolution range of correlator
     !
     let gim            0.1 ! [   ] Image gains
     let iffreq           6 ! [GHz] Intermediate Frequency
     let contbandwidth 3600 ! [MHz] Continuum bandwidth
     !
     if (mode.eq."proposal") then
        let nfreq 3 ! Number of frequencies used to define Tsys
        say "W-NOEMA-SENSITIVITY-ESTIMATOR, This is not the present generation of receiver"
        define double temps[nfreq,2,nrec]
        let temps[1,1]  78 110 118 ! [GHz] Frequencies
        let temps[1,2]  127 150 179 ! [GHz] Frequencies
        let temps[1,3]  199 230 269 ! [GHz] Frequencies
        let temps[1,4]  275 350 373 ! [GHz] Frequencies
        if (noema%sensitivity%season.eq."winter") then
            ! Receiver 1
            let temps[2,1]  100 100  170 ! [K]
            ! Receiver 2
            let temps[2,2]  130 130 170 ! [K]   Tsys
            ! Receiver 3
            let temps[2,3]  200 200 200 ! [K]   Tsys
            ! Receiver 4
            let temps[2,4]  370 370 370 ! [K]   Tsys
        else
           ! Receiver 1
           let temps[2,1]  100 100 180 ! [K]   Tsys
           ! Receiver 2
           let temps[2,2]  150 150 200 ! [K]   Tsys
           ! Receiver 3
           let temps[2,3]  250 250 250 ! [K]   Tsys
           ! Receiver 4
           let temps[2,4]  500 500 500 ! [K]   Tsys
        endif
     else
        ! Trec values (lab. measurements) from B.Lazareff (Aug. 2006)
        !
        let nfreq 2 ! Number of frequencies used to define Trec
        define double temps[nfreq,2,nrec]
        ! Receiver 1
        let temps[1,1]   78 118 ! [GHz] Frequencies 
        let temps[2,1]   55  55 ! [K]   Trec
        ! Receiver 2
        let temps[1,2]  127 179 ! [GHz] Frequencies
        let temps[2,2]   60  60 ! [K]   Trec
        ! Receiver 3
        let temps[1,3]  199 269 ! [GHz] Frequencies
        let temps[2,3]   60  60 ! [K]   Trec
        ! Receiver 4
        let temps[1,4]  275 373 ! [GHz] Frequencies !JB from actual trec measurements
        let temps[2,4]   50  80 ! [K]   Trec !JB from actual trec measurements
     endif
  else
     ! 1995 generation receiver
     let nrec  2 ! Number of receivers
     let nfreq 4 ! Number of frequencies used to define Trec
     define double freqrange[2,nrec] fresrange[2] gim[nrec] contbandwidth[nrec] iffreq
     let gim           0.1  1.0 ! [   ] Image gains
     let iffreq             1.5 ! [GHz] Intermediate Frequency
     let freqrange[1]   85  116 ! [GHz] Receiver 1
     let freqrange[2]  210  250 ! [GHz] Receiver 2
     let fresrange   0.039  2.5 ! [MHz] Spectral resolution range of correlator
     let contbandwidth 580 1160 ! [MHz] Continuum bandwidth
     !
     if (mode.eq."proposal") then
        say "E-NOEMA-SENSITIVITY-ESTIMATOR, Mode not implemented"
        return base
     else
        ! Trec values from J.Y.Chenu (Sep. 2004)
        !
        define double temps[nfreq,2,nrec]
        ! Receiver 1
        let temps[1,1]  86.5  96.5 106.5 113.5 ! [GHz] Frequencies
        let temps[2,1]  55    65    70    80   ! [K]   Trec
        ! Receiver 2
        let temps[1,2] 211.5 221.4 231.5 238.5 ! [GHz] Frequencies
        let temps[2,2]  65    65    70    80   ! [K]   Trec
     endif
  endif
  !
  ! Downcase...
  let noema%sensitivity%season   'noema%sensitivity%season'   /lower
  let noema%sensitivity%sideband 'noema%sensitivity%sideband' /lower
  let noema%sensitivity%obs_kind 'noema%sensitivity%obs_kind' /lower
  let noema%sensitivity%bw_kind  'noema%sensitivity%bw_kind' /lower
  !
  ! Available frequency ranges
  define integer irec
  let irec 0
  for jrec 1 to nrec
     if ((freqrange[1,jrec].le.noema%sensitivity%freq).and.(noema%sensitivity%freq.le.freqrange[2,jrec])) then
        let irec jrec
        break
     endif
  next jrec
  if (irec.eq.0) then
     say "E-NOEMA-SENSITIVITY-ESTIMATOR, Frequency outside receiver range"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR, Available frequency range:"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR, Hardware: "'noema%sensitivity%rx_kind'" Mode: "'noema%sensitivity%obs_kind'
     for jrec 1 to nrec
        say "I-NOEMA-SENSITIVITY-ESTIMATOR,   * Receiver "'jrec'": "'freqrange[1,jrec]'"-"'freqrange[2,jrec]'" GHz"
     next jrec
     return base
  endif
  if (mode.eq."proposal") then
    if (irec.eq.4) then
      say "E-NOEMA-SENSITIVITY-ESTIMATOR, Band 4 is not offered in the present call for proposals for NOEMA"
      say "I-NOEMA-SENSITIVITY-ESTIMATOR, Band 4 sensitivity can be estimated with the DETAILED SENSITIVITY ESTIMATOR"
      return base
    endif
  endif
  !
  ! Available frequency resolutions
  if (noema%sensitivity%obs_kind.eq."continuum") then
     say "I-NOEMA-SENSITIVITY-ESTIMATOR, Continuum observation"
     let noema%sensitivity%fres 'contbandwidth[irec]'
     let noema%sensitivity%vres 0.
     let noema%sensitivity%npol 2
  else if (noema%sensitivity%obs_kind.eq."line") then
     say "I-NOEMA-SENSITIVITY-ESTIMATOR, Line observation"
     if (noema%sensitivity%bw_kind.eq."velocity") then
        let noema%sensitivity%fres '1e6*noema%sensitivity%vres*noema%sensitivity%freq/noema%sensitivity%clight'
     else if (noema%sensitivity%bw_kind.eq."frequency") then
        let noema%sensitivity%vres '1e-6*(noema%sensitivity%clight*noema%sensitivity%fres/noema%sensitivity%freq)'
     else
        say "E-NOEMA-SENSITIVITY-ESTIMATOR, Bandwidth input kind unavailable: "'noema%sensitivity%bw_kind'
     endif
  else
     say "E-NOEMA-SENSITIVITY-ESTIMATOR, Observation kind unavailable: "'noema%sensitivity%obs_kind'
     return base
  endif
  if (mode.eq."proposal") then
    if ((noema%sensitivity%fres.lt.fresrange[1]).or.(noema%sensitivity%fres.gt.fresrange[2])) then
      say "E-NOEMA-SENSITIVITY-ESTIMATOR, Frequency resolution outside correlator range"
      say "I-NOEMA-SENSITIVITY-ESTIMATOR, Available range: "'fresrange[1]'"-"'fresrange[2]'" MHz"
      if (noema%sensitivity%bw_kind.eq."velocity") then
        say "I-NOEMA-SENSITIVITY-ESTIMATOR, i.e.: " 'fresrange[1]*noema%sensitivity%clight/noema%sensitivity%freq/1d3' "-" 'fresrange[2]*noema%sensitivity%clight/noema%sensitivity%freq/1d3' " km/s" /format a f0.2 a f0.0 a
      endif
      return base
    endif
  endif
  !
  ! Number of antenna (summer is maintenance season)
  if (mode.eq."proposal") then
     if (noema%sensitivity%rx_kind.eq."PolyFiX") then
        ! jan 2017 in view of first PolyFiX call for proposal
        if (noema%sensitivity%season.eq."summer") then
           let noema%sensitivity%nantcall noema%sensitivity%nantdef-1
        else
           let noema%sensitivity%nantcall noema%sensitivity%nantdef
        endif
        let noema%sensitivity%nant noema%sensitivity%nantcall
     else if (noema%sensitivity%rx_kind.eq."Widex/NB") then
        ! Backends were NB and widex for 2006 generation
        if (noema%sensitivity%season.eq."summer") then
           let noema%sensitivity%nantcall noema%sensitivity%nantdef-1
        else
           let noema%sensitivity%nantcall noema%sensitivity%nantdef
        endif
        say "   "
        if (noema%sensitivity%fres.ge.2) then
           let noema%sensitivity%nant min(noema%sensitivity%nantcall,8)
           if (noema%sensitivity%nant.lt.noema%sensitivity%nantcall) then
              say "   "
              say "W-NOEMA-SENSITIVITY-ESTIMATOR, Widex correlator can accomodate at most 8 antennas"
           endif
        else if (noema%sensitivity%fres.lt.2) then
           let noema%sensitivity%nant min(noema%sensitivity%nantcall,6)
           if (noema%sensitivity%nant.lt.noema%sensitivity%nantcall) then
              say "   "
              say "W-NOEMA-SENSITIVITY-ESTIMATOR, Frequency resolution <2MHz achieved only with the narrow band correlator which can accomodate at most 6 antennas"
           endif
        endif
     else if (noema%sensitivity%rx_kind.eq."2006") then
        ! Backends were NB and widex for 2006 generation
        if (noema%sensitivity%season.eq."summer") then
           let noema%sensitivity%nantcall 5
        else
           let noema%sensitivity%nantcall 6
        endif
        say "   "
        say "W-NOEMA-SENSITIVITY-ESTIMATOR, At most 6 antennas between 2006 and 2015"
     endif
  else
     ! Detailed mode
     if (noema%sensitivity%nantuser.ge.2) then
        let noema%sensitivity%nant noema%sensitivity%nantuser
     else
        say "  "
        say "E-NOEMA-SENSITIVITY-ESTIMATOR, Interferometric sensitivity requires at least 2 antennas"
        return base
     endif
  endif
  !
  ! Antenna efficiency values
  !
  if (noema%sensitivity%rx_kind.eq."PolyFiX") then
    let noema%sensitivity%anteff noema%sensitivity%pfx_anteff[irec]
  else if (noema%sensitivity%rx_kind.eq."Widex/NB") then
    ! Increased artificially to take into accound correlator sampling difference between widex and PolyFiX
    if (irec.eq.1) then
        let noema%sensitivity%anteff 23
     else if (irec.eq.2) then
        let noema%sensitivity%anteff 29
     else if (irec.eq.3) then
        let noema%sensitivity%anteff 35
     else if (irec.eq.4) then
        let noema%sensitivity%anteff 45
     endif
  else if (noema%sensitivity%rx_kind.eq."2006") then
     if (irec.eq.1) then
        let noema%sensitivity%anteff 22
     else if (irec.eq.2) then
        let noema%sensitivity%anteff 29
     else if (irec.eq.3) then
        let noema%sensitivity%anteff 35
     else if (irec.eq.4) then
        let noema%sensitivity%anteff 45
     endif
  else
     if (irec.eq.1) then
        let noema%sensitivity%anteff 22
     else
        let noema%sensitivity%anteff 35
     endif
  endif
  !
  ! Decorrelation coefficient
  if (noema%sensitivity%rx_kind.eq."PolyFiX") then
     if (noema%sensitivity%season.eq."winter") then
        let noema%sensitivity%decorrel noema%sensitivity%pfx_decorrel_winter[irec]
     else
        let noema%sensitivity%decorrel noema%sensitivity%pfx_decorrel_summer[irec]
     endif
  else if (noema%sensitivity%rx_kind.ne."1995") then
     if (noema%sensitivity%season.eq."winter") then
        if (irec.eq.1) then
           let noema%sensitivity%decorrel 0.9
        else if (irec.eq.2) then
           let noema%sensitivity%decorrel 0.85
        else if (irec.eq.3) then
           let noema%sensitivity%decorrel 0.8
        else
           let noema%sensitivity%decorrel 0.7
        endif
     else
        if (irec.eq.1) then
           let noema%sensitivity%decorrel 0.9
        else if (irec.eq.2) then
           let noema%sensitivity%decorrel 0.8
        else if (irec.eq.3) then
           let noema%sensitivity%decorrel 0.6
        else
           let noema%sensitivity%decorrel 0.5
        endif
     endif
  else ! 1995 case
     if (noema%sensitivity%season.eq."winter") then
        if (irec.eq.1) then
           let noema%sensitivity%decorrel 0.9
        else
           let noema%sensitivity%decorrel 0.8
        endif
     else
        if (irec.eq.1) then
           let noema%sensitivity%decorrel 0.9
        else
           let noema%sensitivity%decorrel 0.6
        endif
     endif
  endif
  !
  ! Max elevation of source:
  if noema%sensitivity%decl.lt.noema%sensitivity%declmin then
    message e noema-sensitivity "Sensitivity estimation not valid for source lower than "'nint(noema%sensitivity%declmin)'" degrees"
    return base
  endif
  let noema%sensitivity%elev 90-noema%sensitivity%burelat+noema%sensitivity%decl ! deg
  if  (noema%sensitivity%elev.gt.90) then
    ! circumpolar
    let noema%sensitivity%elev 180-noema%sensitivity%elev
  endif
  !
  if (mode.ne."proposal".or.noema%sensitivity%rx_kind.ne."PolyFiX") then
    ! Prepare for interpolation Tsys/Trec
    define integer jfreq[2] ifreq1 ifreq2
    compute jfreq loc temps[1,irec] noema%sensitivity%freq
    let ifreq1 jfreq[1]
    let ifreq2 jfreq[2]
    if (ifreq1.eq.ifreq2) then
      let ifreq1 ifreq1-1
    endif
  endif
  if (mode.eq."proposal") then
     if (noema%sensitivity%rx_kind.eq."PolyFiX") then
        ! interpolation in tabulated tsys computed in advance
        let noema%sensitivity%pwv  obspwv[irec]
        if (noema%sensitivity%obs_kind.eq."line") then
          ! Line case
          @ noema-tsys-table-interpolate 'noema%sensitivity%season' -
            'noema%sensitivity%freq' 'noema%sensitivity%pwv' 'noema%sensitivity%elev'
          let noema%sensitivity%tsys noema%tsys%done%tsys
        else if (noema%sensitivity%obs_kind.eq."continuum") then
          ! Continuum case
          @ noema-continuum-table-interpolate 'noema%sensitivity%season' -
            'noema%sensitivity%freq' 'noema%sensitivity%pwv' 'noema%sensitivity%elev'
          let noema%sensitivity%tsys noema%cont%done%tsys_aver
        endif
     else
      ! interpolation into previously defined simple tables
      if (noema%sensitivity%decl.ge.20) then
          @ noema-sensitivity-linear-inter noema%sensitivity%tsys  noema%sensitivity%freq 'temps[ifreq1,1,irec]' 'temps[ifreq2,1,irec]' -
                                                    'temps[ifreq1,2,irec]' 'temps[ifreq2,2,irec]'
      else
          say "E-NOEMA-SENSITIVITY-ESTIMATOR, Declination ("'noema%sensitivity%decl'"deg) too low for PROPOSAL estimator"
          say "E-NOEMA-SENSITIVITY-ESTIMATOR, Use the DETAILED estimator instead"
          return base
      endif
    endif
  else
     ! Detailed mode: interpolate Trec and run ATM
     @ noema-sensitivity-linear-inter trec noema%sensitivity%freq 'temps[ifreq1,1,irec]' 'temps[ifreq2,1,irec]' -
                                          'temps[ifreq1,2,irec]' 'temps[ifreq2,2,irec]'

     if (noema%sensitivity%rx_kind.eq."PolyFiX") then
        let forward_eff noema%sensitivity%pfx_feff[irec]
     else
       if irec.eq.3 then
         let forward_eff 0.9
       else
         let forward_eff 0.95
       endif
     endif
     let gain_image gim[irec]
     if (noema%sensitivity%temp.eq.0) then
        if (noema%sensitivity%season.eq."winter") then
           let temperature noema%sensitivity%twinter
        else
           let temperature noema%sensitivity%tsummer
        endif
     else
        let temperature noema%sensitivity%temp
     endif
     if (noema%sensitivity%water.eq.0) then
        if (noema%sensitivity%season.eq."winter") then
           let water noema%sensitivity%pwvwinter
        else
           let water noema%sensitivity%pwvsummer
        endif
     else
        let water noema%sensitivity%water
     endif
     let freq_sig 'noema%sensitivity%freq'
     if (noema%sensitivity%sideband.eq."usb") then
        let freq_ima 'noema%sensitivity%freq-2*iffreq'
     else if (noema%sensitivity%sideband.eq."lsb") then
        let freq_ima 'noema%sensitivity%freq+2*iffreq'
     else
        say "E-NOEMA-SENSITIVITY-ESTIMATOR, "'noema%sensitivity%sideband'" unavailable"
     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%sensitivity%decl*pi/rad'
     
     let noema%sensitivity%elev noema%sensitivity%elev*pi/180 ! radian
     let airmass abs(1/sin(noema%sensitivity%elev))
     atmosphere
     let noema%sensitivity%tsys tsys
     !
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,         Signal freq: "'freq_sig'" GHz"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,          Image freq: "'freq_ima'" GHz"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,          Image gain: "'gain_image'
     say "I-NOEMA-SENSITIVITY-ESTIMATOR, Ambient Temperature: "'temperature'" K"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,                Trec: "'trec'" K"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,  Precipitable water: "'water'" mm"
     say "I-NOEMA-SENSITIVITY-ESTIMATOR,             Airmass: "'airmass'
  endif
  !
  !Integration time
  !Compute track length according to declination
  if (noema%sensitivity%decl.ge.0) then
    let noema%sensitivity%tracktime 8
    let noema%sensitivity%ntrack noema%sensitivity%teltime/(noema%sensitivity%tracktime+noema%sensitivity%ttune)
  else if (noema%sensitivity%decl.le.-30) then
    let noema%sensitivity%tracktime 0
    let noema%sensitivity%ntrack 0
  else
    ! interpolate
    define integer nd
    let nd 4
    define real ddec[nd] dtime[nd]
    let ddec -30 -20  -10  0
    let dtime 0  3.9  6.5  8.2
    ! Prepare for interpolation 
    define integer jdec[2] id1 id2
    compute jdec loc ddec noema%sensitivity%decl
    let id1 jDec[1]
    let id2 jdec[2]
    if (id1.eq.id2) then
      let id1 id1-1
    endif
    @ noema-sensitivity-linear-inter noema%sensitivity%tracktime noema%sensitivity%decl 'ddec[id1]'  'ddec[id2]' 'dtime[id1]' 'dtime[id2]' 
    if (noema%sensitivity%tracktime.gt.8) then
      let noema%sensitivity%tracktime 8
    endif
    let noema%sensitivity%ntrack noema%sensitivity%teltime/(noema%sensitivity%tracktime+noema%sensitivity%ttune)
  endif
  !Check if mosaic and compute new obseff
  if (noema%sensitivity%mosaic) then
    if (noema%sensitivity%trsh) then
      message e noema-sensitivity "Cannot combine track sharing and mosaic mode"
      return base
    endif
    ! Wide field imaging --> compute the number of independent fields
    define double lambda diameter beam_fwhm beam_area amaxuv bsyn tint
    define double npointpertrack npointmax npoint nbeam 
    let lambda noema%sensitivity%clight/(noema%sensitivity%freq*1e9)
    let diameter noema%sensitivity%ant_diameter
    let beam_fwhm 1.2*lambda/diameter/(60*sec)  ! Primary beam size in arcmin
    let beam_area 0.8*beam_fwhm^2*pi/(4*log(2)) ! Beam area. The 0.8 tries to take into account the primary beam truncation.
    let nbeam noema%sensitivity%area/beam_area
    if (nbeam.lt.2) then
      ! Noema%sensitivity%area [mosaic area] should be larger than 2xbeam area
      say ""
      message e noema-sensitivity "Mosaic area should be larger than twice the primary beam, i.e. "'nint(2*beam_area*1e5)/1e5'" arcmin^2 at "'noema%sensitivity%freq'" GHz"
      return base
    endif
    let npoint nbeam*noema%sensitivity%mos%field2point
    if noema%sensitivity%ntrack.eq.0 then
      let npointpertrack 0
    else
      let npointpertrack npoint/noema%sensitivity%ntrack
    endif
    !
    ! Max number of pointing per track: 1 cycle of pointing completed in 2 cycles between calibrators. 
    ! Minimum integration time on a pointing: typically 10s. Time to move antennas between 2 positions: typically 8s.
    let npointmax noema%sensitivity%mos%tcycle/(noema%sensitivity%mos%tintmin+noema%sensitivity%mos%tslew) ! i.e. 150 fields
    if (npoint.gt.npointmax) then
      message i noema-sensitivity ""
      message w noema-sensitivity "The field area requires "'npoint'" pointings"
      if (npointpertrack.gt.npointmax) then
         message e noema-sensitivity "This translates into "'nint(npointpertrack)'" pointings per track that is higher than the maximum value: "'npointmax'
         message e noema-sensitivity "Either increase the observing time or decrease the field of view"
         return base
      else
         message i noema-sensitivity "The number of pointings per track is lower than "'npointmax'
      endif
    endif
    ! check whether the map is not too large   
    ! amaxuv: max area to avoid a visibility to get out of a uvcell during the minimal integration time (10s)
    ! expression taken from pety and rodriguez-fernandez 2010: Amax= (6900*SyntheBeam/(dtmin*eta))^2
    ! eta = adhoc factor to ensure condition. we take 2
    let bsyn sqrt(noema%sensitivity%synbeam[1]*noema%sensitivity%synbeam[2])/60 ! Synthesized beam in arcmin
    let amaxuv (6900*bsyn/(noema%sensitivity%mos%tintmin*noema%sensitivity%mos%eta))^2 ! [arcmin^2] minimum integration time = 10s, eta=2
    if (noema%sensitivity%area.gt.amaxuv) then
      message e noema-sensitivity ""
      message e noema-sensitivity "The field of view area is too large given the required spatial resolution."
      message e noema-sensitivity "Observations would require an integration time shorter than "'noema%sensitivity%mos%tintmin'" sec per subscan (current mininum at NOEMA)"
      message e noema-sensitivity "Largest possible area for the required resolution: " 'amaxuv' " arcmin^2" /format a f6.3 a
      return base
    endif
    !
    let tint (6900/noema%sensitivity%mos%eta)*(bsyn/sqrt(noema%sensitivity%area)) ! [sec]
    if (tint.ge.noema%sensitivity%mos%tintstd) then
       let noema%sensitivity%obseff noema%sensitivity%obseffref
    else
       let noema%sensitivity%obseff noema%sensitivity%obseffref*(tint+noema%sensitivity%mos%tslew)/tint
    endif
  else
    let noema%sensitivity%obseff noema%sensitivity%obseffref
  endif
  ! Tobs is (Ttel-nTune*40)/obseff
  define real ntune
  if noema%sensitivity%ntrack.lt.1 then 
    let ntune 1 ! no fraction of track for short projects
  else
    let ntune noema%sensitivity%ntrack
  endif
  if (noema%sensitivity%teltime-ntune*noema%sensitivity%ttune).lt.0 then
    message e noema-sensitivity "Telescope time must be larger than time needed to set up the instrument (40min=0.67h)"
    return base
  endif
  let noema%sensitivity%soutime (noema%sensitivity%teltime-ntune*noema%sensitivity%ttune)/noema%sensitivity%obseff
  ! Now distribute onsource time according to observation mode
  if (noema%sensitivity%mosaic) then
    let noema%sensitivity%ontime noema%sensitivity%soutime/nbeam
    message i noema-sensitivity ""
    message i noema-sensitivity "         Mosaic mode: "
    message i noema-sensitivity "          Wavelength: " 'lambda*1e3' "mm" /format a f0.3 a
    message i noema-sensitivity "            Diameter: " 'diameter' "m" /format a i0 a
    message i noema-sensitivity "           Beam FWHM: " 'beam_fwhm*60' "arcsec" /format a f0.1 a
    message i noema-sensitivity "           Beam area: " 'beam_area' "arcmin^2" /format a f4.2 a
    message i noema-sensitivity "          Field area: " 'noema%sensitivity%area' "arcmin^2" /format a f0.2 a
    message i noema-sensitivity " N independent beams: " 'nbeam' /format a f0.1
    message i noema-sensitivity "   Total N pointings: " 'npoint' /format a f0.1
    if (npoint.gt.npointmax) then
       message i noema-sensitivity "Minimum N pointings per track: " 'npointpertrack' /format a i0
    endif
    message i noema-sensitivity "Recommanded int time: " 'tint' "sec" /format a f7.1 a
    if (noema%sensitivity%ontime.ge.1) then
       message i noema-sensitivity "On-time per independent beam: " 'noema%sensitivity%ontime' "hr" /format a f7.1 a
    else
       message i noema-sensitivity "On-time per independent beam: " 'noema%sensitivity%ontime*60' "min" /format a f7.1 a
    endif
  else if (noema%sensitivity%trsh) then
    ! Track sharing
    let noema%sensitivity%ontime noema%sensitivity%soutime/noema%sensitivity%nsou
    message i noema-sensitivity "   "
    message i noema-sensitivity "Track sharing mode: "
    message i noema-sensitivity "On-time per source: " 'noema%sensitivity%ontime' "hr" /format a f0.3 a
  else
    ! Single field
    let noema%sensitivity%ontime noema%sensitivity%soutime
  endif
  !
  ! Available spatial resolutions: Not yet implemented.
  !
  ! Computes
  say ""
  run inter-sensitivity gag_scratch:noema-sensi /nowindow
  say ""
  if (noema%sensitivity%soutime.ge.1) then
    message i noema-sensitivity "       Total On-time: " 'noema%sensitivity%soutime' "hr" /format a f0.3 a
    message i noema-sensitivity "Total telescope time: " 'noema%sensitivity%teltime' "hr" /format a f0.3 a
      else
    message i noema-sensitivity "       Total On-time: " 'noema%sensitivity%soutime*60' "min" /format a f0.3 a
    message i noema-sensitivity "Total telescope time: " 'noema%sensitivity%teltime*60' "min" /format a f0.3 a
  endif
  !
  message i noema-sensitivity 'ntune' " tracks: " 'ntune*noema%sensitivity%ttune' " h for instrument set up"  /format f0.1 a f0.1 a 
  message i noema-sensitivity "Track duration: " 'noema%sensitivity%tracktime' " h"  /format a f0.1 a 
  ! Warning for detailed mode
  if (mode.ne."proposal") then
     say ""
     say "W-NOEMA-SENSITIVITY-ESTIMATOR, Please use the online estimator  (http://oms.iram.fr/tse/#noema) for proposal sensitivity calculations!"
     say ""
  endif
  !
end procedure noema-sensitivity-estimation
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
begin procedure noema-sensitivity-gui
  !
  gui\panel "NOEMA Sensitivity Estimator (&1)" gag_scratch:noema-sensi.hlp
     if (pro%arg[1].ne."proposal") then
        sic\let noema%sensitivity%nantuser         'noema%sensitivity%nantdef'                    /prompt "Number of antennas"
     endif
     sic\let noema%sensitivity%rx_kind      'noema%sensitivity%rx_kind'                 /prompt "NOEMA hardware generation" /choice "PolyFiX" "Widex/NB" "2006" "1995"
     sic\let noema%sensitivity%season       'noema%sensitivity%season'                  /prompt "Observing session" /choice "summer" "winter"
     sic\let noema%sensitivity%obs_kind     'noema%sensitivity%obs_kind'                /prompt "Observation kind" /choice "continuum" "line"
     sic\let noema%sensitivity%bw_kind      'noema%sensitivity%bw_kind'                 /prompt "Bandwidth resolution input kind" /choice "velocity" "frequency"
     if (pro%arg[1].ne."proposal") then
       sic\let noema%sensitivity%sideband     'noema%sensitivity%sideband'                /prompt "Signal sideband" /choice "lsb" "usb"
     endif
     sic\let noema%sensitivity%npol         'noema%sensitivity%npol'                    /prompt "Number of polarizations with the same setup" /index "1" "2"
     sic\let noema%sensitivity%decl         'noema%sensitivity%decl'                    /prompt "Source declination [deg]"
     sic\let noema%sensitivity%freq         'noema%sensitivity%freq'                    /prompt "Observing Frequency [GHz]"
     sic\let noema%sensitivity%vres         'noema%sensitivity%vres'                    /prompt "Velocity  resolution [km/s]"
     sic\let noema%sensitivity%fres         'noema%sensitivity%fres'                    /prompt "Frequency resolution [MHz]"
     sic\let noema%sensitivity%teltime       'noema%sensitivity%teltime'                 /prompt "Total telescope time [hrs]"
     sic\let noema%sensitivity%synbeam 'noema%sensitivity%synbeam[1]' 'noema%sensitivity%synbeam[2]' /prompt "Spatial resolution [arcsec]"
     say ""
     sic\let noema%sensitivity%mosaic    'noema%sensitivity%mosaic'                      /prompt "Single field (No) / Mosaic (Yes)"
     sic\let noema%sensitivity%area         'noema%sensitivity%area'                     /prompt "Field area [arcmin^2]"
     say ""
     sic\let noema%sensitivity%trsh   'noema%sensitivity%trsh'                        /prompt "Single source (No) / Track-sharing (Yes)"
     sic\let noema%sensitivity%nsou         'noema%sensitivity%nsou'                     /prompt "Number of sources"
     say ""
     if (pro%arg[1].ne."proposal") then
        sic\let noema%sensitivity%temp         'noema%sensitivity%temp'                    /prompt "Ambient temperature [K]"
        sic\let noema%sensitivity%water        'noema%sensitivity%water'                   /prompt "Precipitable water amount [mm]"
     endif
  gui\go "@ noema-sensitivity-estimation &1"
  !
end procedure noema-sensitivity-gui
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
if (pro%narg.ne.1) then
   message e NOEMA-SENSITIVITY-ESTIMATOR "Usage: @ noema-sensitivity-estimation proposal|detailed"
   return base
endif
! Load Tsys table
@ noema-tsys-table  ! Load the procedures
@ noema-tsys-table-load
@ noema-continuum-table  ! Load the procedures
@ noema-continuum-table-load
! Definition and defaults
@ noema-sensitivity-definition
@ noema-sensitivity-default
! Gui
@ noema-sensitivity-gui &1
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
