;+
; NAME:
;       TXTPROF2STRUCT 
;
; PURPOSE:
;       This function reads in the input txt file (created by image_profiles)
;       and returns an IDL structrue containing a single profile from the
;       input file. 
;
; CALLING SEQUENCE:
;       Result = TXTPROF2STRUCT(Filename,/plot,/silent)
;
; INPUTS:
;       Filename:   A txt file created by image_profiles. 
;
; OPTIONAL KEYWORDS:
;       /PLOT:       plot the profile before returning it. 
;       /SILENT:     Don't print/display structure to the screen.
;
;
; OUTPUTS:
;       Result: A structrue containing a single profile.
;
; MODIFICATION HISTORY:
;               Written By: Esfandiari, NRL
;   Version 1   aee  Apr 05, 1999    Initial release.
;

function txtprof2struct,fn,plot=plot,silent=silent

  fn= strtrim(fn,2)

  openr,txt_lun,fn,/get_lun,error=err
  if (err ne 0) then return,!err_string
  if(eof(txt_lun)) then return,fn+' is empty.'

  line= ''

  filename= fn

  readf,txt_lun,line
  if(strpos(line,'.FTS') le 0) then return,'Invalid input file.' 
    
  mtitle= strtrim(line,2)
  ;print,mtitle
  ftsname= strmid(line,strpos(line,'.FTS')-8,12)
  camera= strmid(ftsname,0,1)
  if(camera eq '4') then $ 
    camera= 'EIT' $
  else $
    camera= 'C'+camera
  date= strmid(line,strpos(line,'.FTS')+6,19) 
  ;print,date

  pcnt= 0 

  readf,txt_lun,line
  next_plot:
  pcnt= pcnt+1 
  xyout= strtrim(line,2)

  readf,txt_lun,line
  while (strpos(line,'x_axis') lt 0) do begin
    xyout= xyout+' & '+strtrim(line,2) 
    readf,txt_lun,line
  end
  ;print,xyout

  line= strmid(line,10,100)
  xtitle= strmid(line,0,strpos(line,')='))
  ;print,xtitle

  xarr= 0.0
  yarr= 0.0
  
  readf,txt_lun,line
  while(strpos(line,'y_axis') lt 0) do begin
    toks=str_sep(line,' ')
    ok=where(toks ne '',cnt)
    ;if (cnt gt 0) then print,float(toks(ok))
    if (cnt gt 0) then xarr= [xarr,float(toks(ok))]
    readf,txt_lun,line
  end 
  xarr= xarr[1:n_elements(xarr)-1]

  line= strmid(line,10,100)
  ytitle=strmid(line,0,strpos(line,')='))
  ;print,ytitle

  readf,txt_lun,line
  while (not eof(txt_lun)) do begin
    toks=str_sep(line,' ')
    ok=where(toks ne '',cnt)
    ;if (cnt gt 0) then print,float(toks(ok))
    if (cnt gt 0) then yarr= [yarr,float(toks(ok))]
    readf,txt_lun,line

    if(strpos(line,':') ge 0 or strpos(line,'=') gt 0) then begin
       yarr=  yarr[1:n_elements(yarr)-1]  
       st={parent: filename,$
           camera:  camera, $
           ftsname: ftsname,$
           date:    date,   $
           mtitle:  mtitle, $
           xtitle:  xtitle, $
           ytitle:  ytitle, $
           xyout:   xyout,  $
           xarr:   xarr,  $
           yarr:   yarr}
      ;help,/st,st
      mtitle= ftsname+' ('+date+')'
      stat= execute("st"+string(pcnt,'(i2.2)')+"= st")
      goto, next_plot
    end
  end
  yarr=  yarr[1:n_elements(yarr)-1]  

  free_lun,txt_lun
  close,txt_lun


  st=     {parent: filename,$
           camera: camera,  $
           ftsname: ftsname,$
           date:    date,   $
           mtitle:  mtitle, $
           xtitle:  xtitle, $
           ytitle:  ytitle, $
           xyout:   xyout,  $
           xarr:   xarr,  $
           yarr:   yarr}

  stat= execute("st"+string(pcnt,'(i2.2)')+"= st")

  ;help,/st,st

  reply= 1 
  if(pcnt gt 1) then begin
;    print,''
    again:
;    print,string(pcnt,'(i2)')+" different plots were detected in "+fn+" file."
;    read," Which one should I use (1, 2, ...)? ",reply
    str= string(pcnt,'(i2)')+" different plots were detected in "+fn+" file. Which one should I use? Enter a value between 1 and"+string(pcnt,'(i2)')+":"
    reply= fix(get_reply(str))
    if(reply lt 1 or reply gt pcnt) then begin
      print,'   Invalid value.'
      goto,again
    end
  end

  ;Copy the correct plot data in "st" structrue:

  stat= execute("st= st"+string(reply,'(i2.2)'))

  if(pcnt gt 1) then $
    st.parent=filename+' (profile '+string(reply,'(i2)')+' of '+string(pcnt,'(i2)')+')'

  if(not keyword_set(silent)) then begin
    print,''
    print,'Returned structure contains the following fields:'
    print,''  
    help,/st,st
  endif

  if(keyword_set(plot)) then begin
    !mtitle= st.mtitle
    plot,xarr,yarr, ytitle= st.ytitle, xtitle= st.xtitle
    sx= st.xarr(sort(st.xarr))
    xind= fix(n_elements(sx) * 0.1) 
    sy= st.yarr(sort(st.yarr))
    yind= n_elements(sy)/2 
    xyouts,sx(xind),sy(yind),st.xyout
    !mtitle=''
  end

  return,st

end

