;+
; NAME:
;     	imagr_batch
; PURPOSE:
;     	do batch job for imagr
;	imagr does things interactively
; RESTRICTIONS:
;   	1. have to set keyword 'clbox=clbox' in calling clean
; MODIFICATION HISTORY:
;     Written 05-JAN-2001 by JL
;-
; sample inp_min file
; inp_min ={	inuvfile:'flare22mar.uv', $
;            	tbegstr:'184400',
;		tendstr:'184420', $
;	    	bif:15, $
;		eif:18, $
;		poln:'L', $
;            	imsiz:128, $
;            	clbox:[-10,10,-8,9], $
;		dowhat:'C', $
;            	outfile:'t00_f1518_L.CLN.fts'}
;            	
;   14-Mar-2014  DG
;      Changed some routine names by appending ovsa_ to avoid potential conflict with the copies of the original
;      routines located in the ssw\hessi tree
;-------------------------------------------------

pro imagr_batch,inp_min,misc=misc

 inuvfile=inp_min.inuvfile
 tbegstr =inp_min.tbegstr
 tendstr =inp_min.tendstr
 Bif	 =inp_min.bif
 Eif	 =inp_min.eif
 poln	 =inp_min.poln
 imsiz	 =inp_min.imsiz
 clbox	 =inp_min.clbox
 niter	 =2000
 clgain	 =0.1
 clstop	 =5.
 dowhat  =inp_min.dowhat
 outfile =inp_min.outfile

  case dowhat of
 	'D': methodstr='DIRTY MAP'
 	'C': methodstr='CLEAN MAP'
 	'S': methodstr='CLEAN + SelfCal'
   	'M': methodstr='CLEAN + MEM'
   	'F': methodstr='CLEAN + FwdFit'
  endcase

inp ={inuvfile:inuvfile, tbegstr:tbegstr,tendstr:tendstr, $
	bif:bif, eif:eif, poln:poln, imsiz:imsiz, $
	niter:niter, clgain:clgain, clstop:clstop, $
	method:methodstr, outfile:outfile}

;-----------------------------------------
;  Visibility Data restoration/selection
;-----------------------------------------

imagr_vsel, inp, uv_ij, vis_ij, wgt_ij, vsl

if vsl.chk eq 0 then goto,nodata

;-------------------------------
; 	Imaging
;-------------------------------

misc =['Remark: '+methodstr] ; will be altered if not 'D' or 'C'

case dowhat of
'D' : ovsa_clean, uv_ij, vis_ij, inp, vsl, map, cln, /dirty
'C' : ovsa_clean, uv_ij, vis_ij, inp, vsl, map, cln, clbox=clbox
'S' : begin
      ovsa_clean, uv_ij, vis_ij, inp, vsl, cmap, cln, clbox=clbox
      slfcal, cmap, cln, uv_ij, vis_ij, wgt_ij, map, misc
      end
'M' : begin
      ovsa_clean, uv_ij, vis_ij, inp, vsl, cmap, cln, clbox=clbox
      ovsa_mem, cmap, cln, uv_ij, vis_ij, wgt_ij, map, misc
      end
'F' : begin
      ovsa_clean, uv_ij, vis_ij, inp, vsl, cmap, cln, clbox=clbox
      fast_fwdfit, cmap, cln, uv_ij, vis_ij, wgt_ij, map, misc, clbox=clbox
      end
endcase

;------------------
;  S a v e
;------------------

sav:
  data=map
header=imagr_whdr(inp,vsl,cln,misc)

imagr_wfits,data,header,inp.outfile

print,'IMAGR: successful execution. '

goto,endnow

;------------------
; E X I T
;------------------

nodata:
  print,'Warning: either no data or wrong time selected.'
misc=['No good data','Remark: src # 1 1. 0. 0. 0.']
getout:
  print,'IMAGR: quit without imaging'

endnow:

end
