!-----------------------------------------------------------------------
! p_combine.ima
!
! Support for IMAGER command COMBINE
!   COMBINE Out Code Line Cont /FACTOR  Fl Fc /THRESHOLD  Tl Tc /BLANK
!-----------------------------------------------------------------------
let  mapping_error = yes
on error return
!
parse 4  5 /factor 1 2 /threshold 1 2 /blanking 1
define real factor[2] threshold[2] blank
let factor 1
SYMBOL S_THRE " "
SYMBOL S_BLAN " "
if (pro%parse%factor%narg.eq.2) let factor[2] = 'pro%parse%factor%arg[2]'
if (pro%parse%factor%narg.ge.1) let factor[1] = 'pro%parse%factor%arg[1]'
if (pro%parse%threshold%narg.ge.1) then
  let threshold[1] = 'pro%parse%threshold%arg[1]'
  if (pro%parse%threshold%narg.eq.2) then
    let threshold[2] = 'pro%parse%threshold%arg[2]'
    symbol S_THRE " /THRESHOLD "'threshold[1]'" "'threshold[2]'
  else
    symbol S_THRE " /THRESHOLD "'threshold[1]'
  endif
endif
if (pro%parse%blanking%narg.eq.1) then
  symbol S_BLAN " /BLANK "'pro%parse%blanking%arg[1]'
endif
!
define character*256 file short
define character*8 code
define character*16 cin1 cin2 cout
define logical out_var in1_var in2_var
!
let file 'pro%parse%command%arg[1]'
sic parse 'file' short
let out_var = file.eq.short
!
let code 'pro%parse%command%arg[2]' /upper
!
let file 'pro%parse%command%arg[3]'
sic parse 'file' short
let in1_var = file.eq.short
!
let file 'pro%parse%command%arg[4]'
sic parse 'file' short
let in2_var = file.eq.short
!
! Verify Conformity of In1 and In2
define integer n1 n2 nout m1 m2 m3 i3
if (in1_var) then
  let cin1 'pro%parse%command%arg[3]'
  if .not.exist('cin1'%ndim) then
    message e COMBINE 'cin1'" is not a SIC image variable"
    return
  endif
else
  let cin1 "in1"
  define header in1 'pro%parse%command%arg[3]' read
endif
let n1 'cin1'%ndim
if (n1.lt.2.or.n1.gt.3) then
  message e COMBINE "Only works on 2-D or 3-D images"
  return
endif
let m1 'Cin1'%dim[1]
let m2 'Cin1'%dim[2]
let m3 max(1,'Cin1'%dim[3])
!
if (in2_var) then
  let cin2 'pro%parse%command%arg[4]'
  if .not.exist('cin2'%ndim) then
    message e COMBINE 'cin2'" is not a SIC image variable"
    return
  endif
else
  let cin2 "in2"
  define header in2 'pro%parse%command%arg[4]' read
endif
let n2 'cin2'%ndim
let i3 max('cin2'%dim[3],1)
!
if (n2.gt.n1) then
  message e COMBINE "Rank of second data cannot be larger than that of 1st data"
  return
else if (n2.eq.n1-1) then
  message w COMBINE "Combining a Cube with a Plane"
else if (n2.eq.2) then
  message I COMBINE "Combining two 2-D images"
else if (n2.eq.3) then
  message I COMBINE "Combining two 3-D data cubes"
endif
!
define ima tmp[m1,m2,i3] * real
!
if (out_var) then
  if (exist(&1)) then
    if (.not.exist(&1%ndim)) then
      message I COMBINE "Output Variable is not an Image"
      return
    endif
    if (&1%ndim.ne.n1) then
      message I COMBINE "Output Variable does not match 1st data"
      return base
    endif
    let err no /new log
    if (m1.ne.&1%dim[1]) let err = yes
    if (m2.ne.&1%dim[2]) let err = yes
    if (n1.eq.3) then
      ! One could add Velocity Resampling here if needed.
      if (m3.ne.&1%dim[3]) let err = yes
    endif
    if (err) then
      message I COMBINE "Output Variable does not match 1st data"
      return
    endif
  else
    define image &1[m1,m2,m3] * real /global
  endif
endif
!
! Reproject the Second data if not matching the First one.
clean\map_reproject tmp &4 /like 'cin1'
!
if (pro%parse%command%narg.ne.5) then
  clean\map_combine &1 'CODE' &3 tmp /FACTOR 'factor[1]' 'factor[2]' 'S_THRE' 'S_BLAN'
  let mapping_error NO
  return
endif
!
! This cannot be in an IF - ELSE block, because it is parsed even if not executed
clean\map_combine &1 'CODE' &3 tmp /FACTOR 'factor[1]' 'factor[2]' 'S_THRE' 'S_BLAN'
!
! Clear the Error status in case of success
let mapping_error NO
!
