pro ctraj2orbit, infil, outbuf, outfil=outfil, outdir=outdir, asca=asca
;+
; NAME:
;  ctraj2orbit
; PURPOSE:
;  Read a traj file and convert it to a orbit file
; CALLING SEQUENCE:
;  ctraj2orbit, infil, buff
;  ctraj2orbit, infil, outfil=outfil
;  ctraj2orbit, infil, /outfil	; outfil = orbit_xxx 
;
; INPUTS:
;  infil	= Traj file name
;  outfil	= Output file name.  If outfil = 1, then
;  		  outfil will be orbit_xxx where _xxx is then
;		  element number. 
; 		  if infil = 'traj_0174' then outfil will be 'orbit_0174'
;  buff		= String array of the contents of the output file
;
; MODIFICATION HISTORY:
;  27-jul-94, J. R. Lemen LPARL, Written.
;-

if n_elements(infil) eq 0 then begin
   doc_library,'ctraj2orbit'
   return
endif else if not file_exist(infil) then begin
   message,'File does not exist:  '+infil,/cont
   return
endif

; -----------
;  Read the file

buff = rd_tfile(infil)
ss = where(strlen(strtrim(buff,2)) ne 0)
buff = buff(ss)

ss = where(strpos(buff,'.') ne -1)
buff = buff(ss)

ss = wc_where(buff,'*ELEMENT*')
element = buff(ss(0))
element = long(strmid(element,strpos(element,':')+1,strlen(element)))

buff = buff(ss(0)+1:ss(1)-1)

; Pick off the EPOCH Time:
ss = (wc_where(buff,'*EPOC*'))(0)
xx = str2arr(strcompress(strtrim(buff(ss),2)),' ')
timarr = intarr(7)
timarr(4) = long(reverse(str2arr(xx(3),'-')))
timarr(0) = long(strmid(xx(4),0,strpos(xx(4),'H')))
timarr(1) = long(strmid(xx(5),0,strpos(xx(5),'M')))
secs      = float(strmid(xx(5),0,strpos(xx(5),'S')))
timarr(2) = long(secs)
timarr(3) = (secs - timarr(2)) * 1000.
;epoch = string(reverse(timarr(4:6)),reverse(timarr(0:2)),'.',timarr(3),format='(6i2.2,a,i3.3)')
epoch = string(reverse(timarr(4:6)),timarr(0:2),'.',timarr(3),format='(6i2.2,a,i3.3)')

MJD = (str2arr(strcompress(buff(ss)),'MJD'))(1)
MJD = str_replace(str_replace(MJD,')',' '),'=',' ')

; Get x, y, and z

str = ['X','Y','Z','VX','VY','VZ']
str = [str,'PIN','LAM','H','A','E','I','AN','AP','MA']
str = [str,'A','E','I','AN','AP','MA']
str = [str,'A.DOT','E.DOT','I.DOT','AN.DOT','AP.DOT','N','N.DOT']
str = [str,'P','P.DOT','HA','HP','VA','VP']
str = ' ' + str + ' '

code = intarr(n_elements(str))
val  = dblarr(n_elements(str))
code(15:20) = 1

for i=0,n_elements(str)-1 do begin
  ss = (wc_where(buff,'*'+str(i)+'*'))(code(i))
  val(i) = double((str2arr((strtrim(str2arr(' '+strcompress(buff(ss)),str(i)),2))(1),' '))(0))
endfor

outbuf = strarr(8)
outbuf(0) = epoch + string(mjd,val(0:2),format='(4d16.9)')

for i=1,6 do begin
  case i of
    4: fmt = '(3d16.9,2E16.9)'
    5: fmt = '(3E16.9,1d16.9,1E16.9)'
    6: fmt = '(1d16.9,1E16.9,3d16.9)'
    else: fmt = '(5d16.9)'
  endcase
  outbuf(i) = string(val(i*5-2:i*5+2),format=fmt)
endfor
  outbuf(7) = string(val(n_elements(val)-1),format='(d16.9)')
outbuf = str_replace(outbuf,'E','D')

; Write an output file:

if keyword_set(outfil) then begin
  sz = size(outfil)
  if (sz(sz(0)+1) lt 7) then begin
    file = string('orbit_',element,format='(a,i4.4)')
    if (keyword_set(asca)) then begin
      if (n_elements(outdir) eq 0) then outdir = getenv('DIR_ASCA_ORBIT_SOL')
    end else begin
      if (n_elements(outdir) eq 0) then outdir = getenv('DIR_GEN_ORBIT_SOL')
    end
    file = concat_dir(outdir, file)
  end else begin
    file = outfil
  end
  prstr,outbuf,file=file & message,'Writing file = '+file,/info
endif


end



