!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!
! Recovery mode
on error "return base"
!
! First argument is used to (re)define the method
if (pro%narg.gt.0) then
   let method "&1"
endif
!
! Get obs setup
@ p_setup.map inter
!
! Read required data
clean\read * 'name'
if (myclean%support) then
   clean\support /reset
   clean\read mask 'name'
   clean\support /mask
else
   clean\support /reset
endif
!
define character options*128
let options ""
let method 'method' /lower
if ((method.eq."hogbom").or.(method.eq."clark").or.(method.eq."sdi")) then
   ! Does nothing
else if (.not.setup%int%mosaic) then
   if (method.eq."mrc") then
      if (myclean%ratio.ne.0) then
	 let ratio 'myclean%ratio'
      endif
   else if (method.eq."multi") then
      ! Does nothing
   else
      message e clean "Unknown cleaning method: "'method'
      return base
   endif
else
   message e clean "Method "'method'" not implemented for mosaics"
   return base
endif
symbol symbol_opt 'options'
!
! Real work
define character filename*512
let filename 'name'".maskspectrum"
if file(filename) then
   ! Scale niter according to flux of mean spectrum in dirty map
   greg1\column x 1 y 2 /file 'filename'
   define double max
   sic\compute max max y
   let y nint(abs(y/max)*niter)
   let y 1 /where y.lt.1
   delete /var max
endif
define integer userblc[2] usertrc[2]
let userblc blc
let usertrc trc
let blc setup%used%blc
let trc setup%used%trc
clean\clean 'symbol_opt'
let blc userblc
let trc usertrc
!
! Write result
clean\write * 'name'
!
! User feedback
define integer true_first true_last
clean\load dirty
if (first.eq.0) then
   let true_first 1
else
   let true_first min(first,w%dim[3])
endif
if (last.eq.0) then
   let true_last w%dim[3]
else
   let true_last max(1,last)
endif
message r clean ""
message i clean "Cleaned channels: ["'true_first'","'true_last'"]"
message i clean "Used command: "'method'" 'symbol_opt'"
!
! Clean up
delete /symbol symbol_opt
!
! Set type to cleaned image
let type lmv-clean
message i clean "Set type variable to "'type'
!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
