; hsi_printf_keyword_tbl

;+
function string_cleanup,var,str_search,str2

; PURPOSE:
;  Extracts the first keyword in each line of a string array containing
;    HELP lines, discarding everything else
;  Called by hsi_printf_keyword_tbl.pro
;
; INPUTS:
;  var=string array to be cleaned
;  str_search=substring to be found in a line (e.g. 'VIS' or 'SATO)
;  str2='SET' or 'GET'
;
; OUTPUTS:
;  out_str(0,*)=keyword containing the substring str_search (string)
;  out_str(1,*)=str_search (string)
;  out_str(1,*)=str2  (for future expansion)
;  Returns an empty string if no valid keywords exist
;
; HISTORY:
;  EJS Sept. 10, 2001, GSFC 301-286-5114
;-

if n_elements(str2) EQ 0 then str2=''

keyw=grep(str_search,var) ;help sometimes breaks lines, so this is necessary
if (keyw[0] EQ '') then begin
  out_string=''
  message,'No '+str_search+' keywords found',/info
endif else begin
  nv=n_elements(keyw)
  out_string=strarr(3,nv)
  out_string(1,*)=str_search & out_string(2,*)=str2
  for j=0,nv-1 do begin  
    key=strcompress(keyw(j))
    npos=strpos(key,' ',1) 
    if npos EQ -1 then key1=strmid(key,1) else key1=strmid(key,1,npos)
    out_string[0,j]=key1[0]
  endfor
endelse

return,out_string

end

FUNCTION strip_lines,p
k=0
np=n_elements(p)
par=strarr(np)
for j=0,np-1 do begin  ; the following assumes keywords have length < 32
  p1=strmid(p[j],3,32)  ; strip off leading blanks & truncate
  if (strmid(p1,0,1) NE ' ') then begin
    par[k]=p1                ; ignore lines with blanks in position 2
    k=k+1
  endif
endfor
params=par[1:k-1]    ; first line is bogus, and those GE k are empty strings


RETURN,params
END 

;+
PRO hsi_printf_keyword_tbl, tbl_filename, VERB=verb

; PURPOSE:
;   Prints to disk the current HESSI keywords given by 
;   obj_im=hsi_image &   help,obj_im->set(/vis,/sato,/clean,/pixon,/ff)
;   AND
;   im=obj_im-> getdata(...) & pars = obj_im -> get()  &help,pars,/st
;
; INPUTS:
;  tbl_filename=full path name of the file to be written to disk
;  optional print to screen if VERB is set
;
; OUTPUTS:
;  A disk file whose name is given by the variable tbl_filename
;  Defaulting to 'keyword.tbl'  Contains over 200 keywords.
;  Format:  
;   keyword (up to 32 chars), method (10 chars), help (10 chars)
;
; HISTORY:
;  EJS Sept. 10, 2001, GSFC 301-286-5114
;  EJS Sept. 17, 2001, Various bug fixes and improvements.
;-


obj_im = hsi_image() 

obj_im -> set,image_algorithm='memvis' 
help,obj_im->get(/vis),/st,output=vis

obj_im -> set,image_algorithm='memsato' 
help,obj_im->get(/sato),/st,output=sato

obj_im -> set,image_algorithm='clean' 
help,obj_im->get(/clean),/st,output=clean

obj_im -> set,image_algorithm='pixon' 
help,obj_im->get(/pixon),/st,output=pixon

obj_im -> set,image_algorithm='forward_fitting' 
help,obj_im->get(/ff),/st,output=forward


if n_elements(tbl_filename) EQ 0 then tbl_filename='keyword.tbl'
openw,lun,tbl_filename,/get_lun

obj_im = hsi_image() 

obj_im -> set,image_algorithm='memvis' 
help,obj_im->get(/vis),/st,output=vis

n_keywords=0
keyw_vis=string_cleanup(vis,'VIS','SET')
if keyword_set(VERB) then print,keyw_vis
if (keyw_vis[0] NE '') then printf,lun,keyw_vis,format='(a32,t32,a10,t42,a10)'
n_keywords=n_keywords+n_elements(keyw_vis(0,*))
;for j=0,n_elements(keyw_vis(0,*))-1 do print,j+1,': ',keyw_vis[*,j]

keyw_sato=string_cleanup(sato,'SATO','SET')
if keyword_set(VERB) then print,keyw_sato
if (keyw_sato[0] NE '') then printf,lun,keyw_sato,format='(a32,t32,a10,t42,a10)'
n_keywords=n_keywords+n_elements(keyw_sato(0,*))
;for j=0,n_elements(keyw_sato(0,*))-1 do print,j+1,': ',keyw_sato[*,j]

keyw_pixon=string_cleanup(pixon,'PIXON','SET')
if keyword_set(VERB) then print,keyw_pixon
if (keyw_pixon[0] NE '') then printf,lun,keyw_pixon,format='(a32,t32,a10,t42,a10)'
n_keywords=n_keywords+n_elements(keyw_pixon(0,*))
;for j=0,n_elements(keyw_pixon(0,*))-1 do print,j+1,': ',keyw_pixon[*,j]

keyw_clean=string_cleanup(clean,'CLEAN','SET')
if keyword_set(VERB) then print,keyw_clean
if (keyw_clean[0] NE '') then printf,lun,keyw_clean,format='(a32,t32,a10,t42,a10)'
n_keywords=n_keywords+n_elements(keyw_clean(0,*))
;for j=0,n_elements(keyw_clean(0,*))-1 do print,j+1,': ',keyw_clean[*,j]

keyw_fwd=string_cleanup(forward,'FF','SET')
if keyword_set(VERB) then print,keyw_fwd
if (keyw_fwd[0] NE '') then printf,lun,keyw_fwd,format='(a32,t32,a10,t42,a10)'
n_keywords=n_keywords+n_elements(keyw_fwd(0,*))
;for j=0,n_elements(keyw_fwd(0,*))-1 do print,j+1,': ',keyw_fwd[*,j]


; GET GENERAL IMAGE KEYWORDS USING IMAGE OBJECT:
flarelist = hsi_read_flarelist() 
peak_time=(flarelist.PEAK_TIME)[0]  ; testdata flare #1
fpk1=peak_time ; START of PEAK ROTATION (in DOUBLE sec)
fpk2=fpk1+4.d0 ; START + 4 SECONDS
obs_time_interval=[fpk1,fpk2]
obj_im = hsi_image() 
x_position=(flarelist.x_position)[0]
y_position=(flarelist.y_position)[0]
xyoffset=[x_position,y_position]
energy_band=[12.,25.] ;
det_index=byte([0,0,1,0,0,0,0,0,0]) ;byte([0,0,1,1,1,1,1,1,0])
image_algorithm='bproj' ; 
obj_im -> set,energy_band=energy_band ;
obj_im -> set,image_algorithm=image_algorithm
obj_im -> set,pixel_size=pixel_size
obj_im -> set,det_index_mask=det_index

; do a simulation to get the sim keywords
im=obj_im -> getdata( )
simpars = obj_im -> get() 
help,simpars,/st,OUTPUT=simparams

; Get a test data image for other keywords
im=obj_im -> getdata( XYOFFSET=xyoffset, ENERGY_BAND=energy_band,$
             OBS_TIME_INTERVAL=obs_time_interval, DET_INDEX=det_index  ) 
pars = obj_im -> get() 
help,pars,/st,OUTPUT=params

paras=[params,simparams]

np=n_elements(paras)
paras=strip_lines(paras)


np=n_elements(paras)
keyw_pars=strarr(3,np)
keyword_pars=strarr(np)
k=0
for j=0,np-1 do begin
  paras[j]=paras[j]+' '  ; append a space for those lines ending without one
  bpos=strpos(paras[j],' ') ; get the pos of the first ' ' in the line
  keywd=strmid(paras[j],0,bpos)   ; extract the keyword
 if (strmid(keywd,0,1) NE ' ') then begin
     keyword_pars[k]=keywd
     k=k+1
  endif
endfor

keyword_pars=keyword_pars[0:k-1]               ; clip off empty lines
keyword_pars=keyword_pars(sort(keyword_pars))  ; sort
keyword_pars=keyword_pars(uniq(keyword_pars))  ; keep only unique ones

nk=n_elements(keyword_pars)
print,'Eliminated '+strcompress(k-nk)+' duplicates.'

for j=0,nk-1 do begin
  keyw_pars[0,j]=keyword_pars[j]
  keyw_pars[1,j]='PARS' &     keyw_pars[2,j]='GET' 
  if keyword_set(VERB) then  print,keyw_pars[*,j]
  printf,lun,keyw_pars[*,j],format='(a32,a10,a10)'
  n_keywords=n_keywords+1
        ;print,j+1,': ',keyw_pars[0,j]
endfor


close,lun
print,'Wrote',strcompress(n_keywords),' keywords to ',tbl_filename

RETURN

END


