;pro inspec

inuvfile='unspecified'
chk=strlen(findfile('inspec.inp'))
if chk[0] ne 0 then restore,'inspec.inp'
 tbegstr ='184400'
 tendstr ='184410'
 poln    ='R'
 Bif     =1 ; >=1
 Eif     =5 ; <=nf
 imsiz   =256           ; 64,128,256,512 only
 clbox   =[-10,-10,10,10]
 niter   =2000
 clgain  =0.1
 clstop  =5.
 dowhat  ='C'
 methodstr  ='CLEAN only'
 outfile ='imagr.fts'
partab  =['UV','TB','TE','PO','BI','EI','IM','OU']
print,'======================================================'
print,'INSPEC: allows user to look at data and determine
print,'    parameters for IMAGR & save them into a text file
print,'======================================================'
start:
print,' '
print,' ____I_N_S_P_E_C_____________________________________'
print,'| Parameters       || List | View | Go | Help | Quit | '
print,' ----------------------------------------------------  '
print,' UVfile name     : ',inuvfile
print,' '
print,'Specify the UVfile name & type "go" to proceed'
;----------------------------------------------------
; Prompt for input and read response from operator.
;----------------------------------------------------
;on_ioerror,errorinput
read:
inline=' '
read,inline,prompt='inspec> '
inline = strtrim(inline,2)
cmnd   = strupcase(strmid(inline,0,2))

if cmnd eq 'LI' then begin
 spawn,'ls ./*.uv',results
 print,' '
 print,'List of uv files: '
 print,results
 print,' '
 goto,read
 endif

if cmnd eq 'GO' then goto, donow

if (cmnd eq 'VI') or (cmnd eq 'IN') then goto,start

if cmnd eq 'HE' then begin
 spawn,'more inspec.hlp',helptxt
 for i=0,n_elements(helptxt)-1 do print,helptxt[i]
 goto,read
 endif

if cmnd eq 'QU' then goto,getout

;if cmnd eq 'DO' then begin
;  strbeg=strpos(inline,' ')+1
;  strlng=strlen(inline)-strbeg
;  reads,strmid(inline,strbeg,1),dowhat
;  dowhat=strupcase(dowhat)
;  case dowhat of
;   'D': methodstr='DIRTY MAP'
;   'C': methodstr='CLEAN MAP'
;   'S': methodstr='CLEAN + SelfCal'
;       'M': methodstr='CLEAN + MEM'
;       'F': methodstr='CLEAN + FwdFit'
;  endcase
;  goto,read
;endif

; at this point user want to change a parameter
par_exist=where(cmnd eq partab)
parindx  =par_exist[0]
if (parindx ne -1) then begin
 strbeg=strpos(inline,' ')+1
 strlng=strlen(inline)-strbeg
 case parindx of
  0: reads,strmid(inline,strbeg,strlng),inuvfile
  1: reads,strmid(inline,strbeg,strlng),tbegstr
  2: reads,strmid(inline,strbeg,strlng),tendstr
  3: reads,strmid(inline,strbeg,1),poln
  4: reads,strmid(inline,strbeg,strlng),bif
  5: reads,strmid(inline,strbeg,strlng),eif
  6: reads,strmid(inline,strbeg,strlng),imsiz
  7: reads,strmid(inline,strbeg,strlng),outfile
 endcase
    poln=strupcase(poln)
 goto,read
endif

errorinput:
print,'Error in input -- please reenter.'
goto,read


;------------------------------------------------------------
; Now let use choose time/frequency range
;----------------------------------------------------------
donow:
save,filename='inspec.inp',inuvfile
;----1. using the dynamic spectrum

print,'          retoring uvfile, ',inuvfile
restore,inuvfile

tp_R=float(cal.tp[*,0,*])   ; if you use only ant 1 for this purpose
tp_L=imaginary(cal.tp[*,0,*])   ; if you use only ant 1 for this purpose
TP=(tp_R+tp_L)/2.
tp=reform(tp)
nok = where(finite(tp) eq 0)
if nok[0] gt -1 then tp(nok)=0.01
tp  = tp>0.01
t=cal.t
f=cal.f
titl=cal.info.date

tf_range = inspec_tf(tp, t, f, titl)

;tf_range=[60,170,3,30]
;----2. using time profiles

; ultimately desire to have a form like:
; t_range = inspec_t(cal, trange=tf_range[0:1])
; but now we just use JQ's version
findx=[tf_range[2],total(tf_range[2:3])/2,tf_range[3]]
f_show=f(findx)

inspec_t, cal, ans,trange=tf_range[0:1],f_show=f_show, $
    tim_interval_sec=tim_interval_sec

ntin = n_elements(ans[0,*])

tbegstr_=strarr(ntin)
tendstr_=strarr(ntin)

for i=0,ntin-1 do begin
 tbegstr_[i]=ans[0,i]
 tendstr_[i]=ans[1,i]
endfor

;----3. using spectra

inspec_f,cal,frqid,tim_interval=tim_interval_sec,frange=tf_range[2:3]
 Bifc = strcompress(string(min(frqid)),/rem)
 Eifc = strcompress(string(max(frqid)),/rem)
outfiles=strarr(ntin*2)
for i=0,ntin-1 do outfiles[i]='t'+tbegstr_[i] $
    +'f'+bifc+'.R'+'.cln.fts'
for i=0,ntin-1 do outfiles[i+ntin]='t'+tbegstr_[i] $
    +'f'+bifc+'.L'+'.cln.fts'


;---other stuff

 imsiz   =256           ; 64,128,256,512 only
 cimsiz  =strcompress(string(imsiz),/rem)
 clbox   =[-10,-10,10,10]
 cclbox  =string(clbox)
 niter   =2000
 clgain  =0.1
 clstop  =5.
 dowhat  ='C'
 methodstr  ='CLEAN only'

openw,1,'inspec.txt'

poln     ='R'
printf,1,'uvfile ',inuvfile
printf,1,'Tbegin ',tbegstr_[0]
printf,1,'Tend ',tendstr_[0]
printf,1,'BIF ',Bifc
printf,1,'EIF ',Eifc
printf,1,'Polarization ',poln
printf,1,'CLBOX ',cclbox
printf,1,'Imsize ',cimsiz
printf,1,'Do ',dowhat
printf,1,'Output ',outfiles[0]
printf,1,'go'
if ntin gt 1 then begin
 for i=1,ntin-1 do begin
 printf,1,'Tbegin ',tbegstr_[i]
 printf,1,'Tend ',tendstr_[i]
 printf,1,'Output ',outfiles[i]
 printf,1,'go'
 endfor
endif
poln     ='L'
printf,1,'Polarization ',poln
for i=0,ntin-1 do begin
printf,1,'Tbegin ',tbegstr_[i]
printf,1,'Tend ',tendstr_[i]
printf,1,'Output ',outfiles[i+ntin]
printf,1,'go'
endfor

close,1
print,'outputs are written to inspec.txt.'
goto, endnow

getout:
  print,'INSPEC: quit without imaging'

endnow:
end