!-----------------------------------------------------------------------
!  This procedure will display the Table Of Contents of the current ATM
! binary table (Sic logical GAG_ATMOSPHERE) used by the command ATM
! INTERPOLATE:
!  - in the terminal, the sampling of the parameter space is displayed,
!  - in the plotting window, the atmospheric emission is displayed as
!    a function of frequency, for all the available water vapor amount,
!    and for a single value of the pressure, the temperature, and the
!    airmass.


begin procedure table-summary
  say "Table "'atm%table%file'":"
  !
  say "  Pressure   : " 'atm%table%npressure' " values from " 'atm%table%pressure[1]' -
      " to " 'atm%table%pressure[atm%table%npressure]' /format A I4 A F7.2 A F7.2
  !
  say "  Temperature: " 'atm%table%ntemperature' " values from " 'atm%table%temperature[1]' -
      " to " 'atm%table%temperature[atm%table%ntemperature]' /format A I4 A F7.2 A F7.2
  !
  say "  Frequency  : " 'atm%table%nfrequency' " values from " 'atm%table%frequency[1]' -
      " to " 'atm%table%frequency[atm%table%nfrequency]' /format A I4 A F7.2 A F7.2
  !
  say "  Water vapor: " 'atm%table%nwater' " values from " 'atm%table%water[1]' -
      " to " 'atm%table%water[atm%table%nwater]' /format A I4 A F7.2 A F7.2
  !
  say "  Airmass    : " 'atm%table%nairmass' " values from " 'atm%table%airmass[1]' -
      " to " 'atm%table%airmass[atm%table%nairmass]' /format A I4 A F7.2 A F7.2
  !
end procedure table-summary
!
begin procedure table-plot

  ! Define the pressure, temperature, and airmass to be used. Use
  ! arbitrarily the central value of each axis
  define integer ip it ia
  let ip atm%table%npressure/2
  let it atm%table%ntemperature/2
  let ia atm%table%nairmass/2

  g\clear directory  ! Clear only current window

  ! 2 subdirectories
  g\create directory TABPLOT
  g\change directory TABPLOT
  g\set box_loc 4. 22. 2.5 19.0
  g\change directory ..
  g\create directory TABLEGEND
  g\change directory TABLEGEND
  g\set box_loc 22. 28. 2.5 19.0

  ! Plot limits
  g\change directory ..<TABPLOT
  g\limits          /var atm%table%frequency atm%table%emis[ia,1,,it,ip]
  g\limits = = = *  /var atm%table%frequency atm%table%emis[ia,atm%table%nwater,,it,ip]
  !
  g\box
  g\label "Frequency [GHz]" /x
  g\label "Atmospheric emission [K]" /y
  !
  ! Legend
  g\change directory ..<TABLEGEND
  !
  g\box n n n
  g\set char .5
  define char*8 mytext
  define real ylabel
  !
  g\draw text 0. -0.5 'atm%table%file' /box 8
  !
  let mytext 'atm%table%pressure[ip]' /format f0.2
  let ylabel -2.0  ! [cm]
  g\draw text .5 ylabel "Pressure = "'mytext' 6 /box 7
  !
  let mytext 'atm%table%temperature[it]' /format f0.2
  let ylabel ylabel-1.0
  g\draw text .5 ylabel "Temperature = "'mytext' 6 /box 7
  !
  let mytext 'atm%table%airmass[ia]' /format f0.2
  let ylabel ylabel-1.0
  g\draw text .5 ylabel "Airmass = "'mytext' 6 /box 7

  g\lut rainbow1 /pen
  let ylabel ylabel-1.0
  for iw atm%table%nwater to 1 by -1
    g\pen /co '6+2*iw'
    !
    g\change directory ..<TABPLOT
    ! Use histogram so that we see the bin size (i.e. how good the sampling is)
    g\histo atm%table%frequency atm%table%emis[ia,iw,,it,ip]
    !
    ! Label
    g\change directory ..<TABLEGEND
    let mytext 'atm%table%water[iw]' /format F0.3
    let ylabel ylabel-1.0
    g\draw text .5 ylabel "Water = "'mytext' 6 /box 7
  next iw
  g\change directory ..

  g\pen /def
  g\set char /def
  !
end procedure table-plot

! Force loading the table into internal buffers
astro\atm interpolate

! Sanity check. Not perfect...
if .not.exist(ATM%TABLE) then
  message e ATM "Table could not be loaded in memory"
  return error
endif

! Real job
@ table-summary
@ table-plot
