!-----------------------------------------------------------------------
! @ p_spacing Header
!
!   Convert the required unit of spacing in current Image units
!
!     Allowed units are  (2nd element of spacing)
!           UNITS or any value (let SPACING OneNumber)
!           Jy
!           mJy   milliJy
!           microJy
!           K
!           mK    milliK
!           NOISE SIGMA
!           
!     Allowed mode is USER, LIN or LOG  (3rd element of spacing)
!-----------------------------------------------------------------------
!
!
! New code
let spacing_rule%r 'spacing[1]'
if (spacing_rule%r.eq.0) then
  ! 0 is the default behaviour, must be LINear
  if (spacing_rule%c.eq."USER") let spacing_rule%c Units
  let space_type "LIN"
else if (spacing[1].ne.spacing[2]) then
  let spacing_rule%c 'spacing[2]' /upper
endif
let spacing_rule%l 'space_type' /upper
! EXA SPACING_RULE%
!
if (spacing_rule%l.eq."LOG").or.(spacing_rule%l.eq."EXPO") then
  let spacing_rule%l "LOG"
else if (spacing_rule%c.eq."USER") then
  let spacing_rule%l "USER"
  return ! User  specified levels...
else if (spacing_rule%l.ne."USER") then
  let spacing_rule%l "LIN"
else
  return ! User  specified levels...
endif
!
let spacing_rule%s 1.0
!
if (spacing_rule%r.eq.0).or.(spacing_rule%c.eq."NOISE").or.(spacing_rule%c.eq."SIGMA") then
  ! Get the noise level
  let spacing_rule%n 0
  if (&1%rms.eq.0) then
    let spacing_rule%n &1%noise
  else if (&1%noise.ne.0) then
    ! Even if the theoretical noise non-zero, it may be non-sense
    ! or the true noise may also be Dynamic range limited...
    let spacing_rule%n &1%rms
  else
    let spacing_rule%n &1%rms
  endif
  if (spacing_rule%n.eq.0) then
    compute spacing_rule%n rms &1
  endif
  if (spacing_rule%r.ne.0) then
    let spacing_rule%s spacing_rule%n
  endif
  return
endif
let spacing_rule%n 1.0 
!
! Convert the required unit to the current Variable unit
define real lwave jyperk
if (&1%major.ne.0) then
  let lwave 2.99792458e8|&1%restfre*1e-6
  let jyperk 2.0*1.38e3*pi*&1%major*&1%minor|4.|log(2.0)|lwave^2
else
  let jyperk 1.0
endif
!
if (spacing_rule%c.eq."NOISE").or.(spacing_rule%c.eq."SIGMA") then
  let spacing_rule%s spacing_rule%n
  !
else if (spacing_rule%c.eq."JY").or.(spacing_rule%c.eq."MJY").or.(spacing_rule%c.eq."MILLIJY").or.(spacing_rule%c.eq."MICROJY") then
  if (spacing_rule%c.eq."MJY").or.(spacing_rule%c.eq."MILLIJY") then
    let spacing_rule%s 1E-3
  else if (spacing_rule%c.eq."MICROJY") then
    let spacing_rule%s 1E-6
  endif
  if (&1%unit.eq."Jy".or.&1%unit.eq."Jy/beam") then
    continue
  else if (&1%unit.eq."K") then
    let spacing_rule%r spacing_rule%r/jyperk
  else
!    let spacing_rule%c AUTO
  endif
else if (spacing_rule%c.eq."K").or.(spacing_rule%c.eq."MK").or.(spacing_rule%c.eq."MILLIK").or.(spacing_rule%c.eq."MICROK") then
  if (spacing_rule%c.eq."MK").or.(spacing_rule%c.eq."MILLIK") then
    let spacing_rule%s 1E-3
  else if (spacing_rule%c.eq."MICROK") then
    let spacing_rule%s 1E-6
  endif
  if (&1%unit.eq."K") then
    continue
  else if (&1%unit.eq."Jy".or.&1%unit.eq."Jy/beam") then
    let spacing_rule%r spacing_rule%r*jyperk
  else
!    let spacing_rule%c AUTO
  endif
endif  
!
exa spacing_rule%
  
