!-----------------------------------------------------------------------
! This script can be used to generate the figure
! mrtcal-horizontal-true.pdf
!-----------------------------------------------------------------------
begin procedure draw-variables
  if exist(draw)  del /var draw
  !
  define structure draw             /global
  ! Plot projection center and size
  define char*15   draw%ra          /global
  define char*15   draw%dec         /global
  define real      draw%psize       /global
  ! Arc on this plot
  define double    draw%xarc[6]     /global
  define double    draw%yarc[6]     /global
  ! The 3 WSW positions (beams) on this plot
  define double    draw%xwsw[16,3]  /global
  define double    draw%ywsw[16,3]  /global
  ! Some astronomical region and jittering size on this plot
  define double    draw%xarea[9]    /global
  define double    draw%yarea[9]    /global
  define double    draw%xareac[16]  /global
  define double    draw%yareac[16]  /global
end procedure draw-variables

begin procedure draw-set-arc
  define double xarcsize yarcsize
  let xarcsize 5*pi/180  ! [rad] 5 deg
  let yarcsize 1*pi/180  ! [rad] small width for visibility
  let draw%xarc -xarcsize         0 +xarcsize +xarcsize         0 -xarcsize
  let draw%yarc +yarcsize +yarcsize +yarcsize -yarcsize -yarcsize -yarcsize
end procedure draw-set-arc
begin procedure draw-repro-arc
  define real xc yc
  let xc 'pro%arg[1]'
  let yc 'pro%arg[2]'
  set system galactic
  projection draw%ra draw%dec /type ortho
  g\convert xc yc /type ortho /var draw%xarc draw%yarc /unit rad /system galactic
end procedure draw-repro-arc
begin procedure draw-arc
  define integer n
  let n size(draw%xarc)
  define real poly[n,2]
  let poly[1] draw%xarc
  let poly[2] draw%yarc
  g\poly poly /var /fill blue
end procedure draw-arc

begin procedure draw-set-wsw
  define double wswextent wswcenter[3] wswsize
  let wswextent 5*pi/180  ! [rad] 5 deg apart from center
  let wswcenter -wswextent 0 +wswextent
  let wswsize 2.5*pi/180
  for ipos 1 to 3
    for ipnt 1 to 16
      let draw%xwsw[ipnt,ipos] wswcenter[ipos]+wswsize*cos(2*pi*ipnt/16)
      let draw%ywsw[ipnt,ipos]               0+wswsize*sin(2*pi*ipnt/16)
    next ipnt
  next ipos
end procedure draw-set-wsw
begin procedure draw-repro-wsw
  define real xc yc
  let xc 'pro%arg[1]'
  let yc 'pro%arg[2]'
  set system galactic
  projection draw%ra draw%dec /type ortho
  g\convert xc yc /type ortho /var draw%xwsw draw%ywsw /unit rad /system galactic
end procedure draw-repro-wsw
begin procedure draw-wsw
  define integer n
  let n size(draw%xwsw[1])
  define real poly[n,2]
  for ipos 1 to 3
    let poly[1] draw%xwsw[ipos]
    let poly[2] draw%ywsw[ipos]
    g\poly poly /var /fill blue
  next ipos
end procedure draw-wsw

begin procedure reset-region
  define double mapxsize mapysize
  let mapxsize 10*pi/180  ! [rad] 10 deg
  let mapysize  5*pi/180  ! [rad] 5 deg
  let draw%xarea -mapxsize 0 +mapxsize +mapxsize +mapxsize 0 -mapxsize -mapxsize -mapxsize  ! [rad]
  let draw%yarea +mapysize +mapysize +mapysize 0 -mapysize -mapysize -mapysize 0 +mapysize  ! [rad]
  ! Jittering area
  define double jittsize
  let jittsize 3*pi/180
  for ipnt 1 to 16
    let draw%xareac[ipnt] 0+jittsize*cos(2*pi*ipnt/16)
    let draw%yareac[ipnt] 0+jittsize*sin(2*pi*ipnt/16)
  next ipnt
end procedure reset-region
begin procedure draw-repro-region
  define real xc yc
  let xc 'pro%arg[1]'
  let yc 'pro%arg[2]'
  set system galactic
  projection draw%ra draw%dec /type ortho
  g\convert xc yc /type ortho /var draw%xarea  draw%yarea /unit rad /system galactic
  g\convert xc yc /type ortho /var draw%xareac draw%yareac /unit rad /system galactic
end procedure draw-repro-region
begin procedure draw-region
  define integer n
  let n size(draw%xarea)
  define real poly[n,2]
  let poly[1] draw%xarea
  let poly[2] draw%yarea
  g\poly poly /var /fill dark_grey
  del /var poly
  ! Jittering area
  let n size(draw%xareac)
  define real poly[n,2]
  let poly[1] draw%xareac
  let poly[2] draw%yareac
  g\poly poly /var /fill blue
end procedure draw-region

begin procedure draw-grid
  projection draw%ra draw%dec /type ortho
  g\set char 0.2
  grid 20 /labe  ! Draw the grid on sphere every 20 deg
  pen /col red
  grid 210  ! Draw only the Equator and 0-meridian
  g\set char 0.4
  g\draw text 0.95 -0.4 "Horizon" 5 -48 /user
  g\draw text 0.2   0.8 "Zenith" /user
  pen /col foreground
end procedure draw-grid

@ window_tools
@ window_init
@ draw-variables

let aspect_ratio yes
let aspect 1
let inter 0.1 0.1

! Displayed sphere (work in degrees - not hour angle - and use galactic
! system mimicking Az/El system).
let draw%ra 20 /sexa d d
let draw%dec 50 /sexa d d
let draw%psize 70  ! [deg]

pen /we 2  ! For the PDF
clear

! --- Plot: show arc at different elevations ---

@ window_xy 3 1 1

limits -draw%psize draw%psize -draw%psize draw%psize deg /rev x
box n n n /unit d
! Title
! g\set char 0.6
! g\draw text 0 0.3 "Same arc at different elevations" /box 8
@ draw-grid

@ draw-set-arc
@ draw-repro-arc 0 0  ! Arc center
@ draw-arc
!
@ draw-set-arc
@ draw-repro-arc 0 40  ! Arc center
@ draw-arc
!
@ draw-set-arc
@ draw-repro-arc 0 80  ! Arc center
@ draw-arc

! --- Plot: show WSW at different elevations ---

@ window_xy 3 1 2

limits -draw%psize draw%psize -draw%psize draw%psize deg /rev x
box n n n /unit d
! Title
! g\set char 0.6
! g\draw text 0 0.3 "Same arc at different elevations" /box 8

@ draw-set-wsw
@ draw-repro-wsw 0 0  ! WSW center
@ draw-wsw
!
@ draw-set-wsw
@ draw-repro-wsw 0 40  ! WSW center
@ draw-wsw
!
@ draw-set-wsw
@ draw-repro-wsw 0 80  ! WSW center
@ draw-wsw

@ draw-grid

! --- Plot: region on sky ---

@ window_xy 3 1 3

limits -draw%psize draw%psize -draw%psize draw%psize deg /rev x
box n n n /unit d
! Title
! g\set char 0.6
! g\draw text 0 0.3 "3D celestial sphere" /box 8

@ reset-region
@ draw-repro-region 0 0  ! Region center
@ draw-region
!
@ reset-region
@ draw-repro-region 0 40  ! Region center
@ draw-region
!
@ reset-region
@ draw-repro-region 0 80  ! Region center
@ draw-region

@ draw-grid

! ---

hard mrtcal-horizontal-true.pdf /dev epdf /over
