pro rd_specdat,filin,ra,dec,teff,logz,logg,chi2
; this routine reads Dave Latham's various files of spectroscopic parameter
; estimates, dealing with inconsistent formats and such.
; On return, ra(decimal hours), dec(decimal degrees), teff(K),
; logz, logg in usual units.
; Array chi2 is returned full of zeros if the input file has no analogous
; thing;  if a measure of the Doppler chi2 is present, it is returned in
; this array.  Large (>10 or so) values presumably signify duplicity.

; a bunch of special cases, identified by hard-wired filenames.
iz=strpos(filin,'m67.slmem2.ok',0)
if(iz ge 0) then begin
  openr,iun,filin,/get_lun
; count lines of input
  ss=''
  nl=0
  while(not eof(iun)) do begin
   readf,iun,ss
   nl=nl+1
  endwhile
  
; make output arrays, go back to beginning, fill them
  f1='(a13,1x,a10,1x,a9,1x,i5,1x,f5.1,1x,f7.1,1x,f3.1)'
  ra=dblarr(nl)
  dec=dblarr(nl)
  teff=fltarr(nl)
  logz=fltarr(nl)
  logg=fltarr(nl)
  chi2=fltarr(nl)             ; returned empty in this case
  s1=''
  sra=''
  sdec=''
  point_lun,iun,0
  for i=0,nl-1 do begin
    readf,iun,s1,sra,sdec,te,lz,vsi,lg,format=f1
    hh=fix(strmid(sra,0,2))
    mm=fix(strmid(sra,3,2))
    ss=float(strmid(sra,6,4))
    ra(i)=ten(hh,mm,ss)
    dd=fix(strmid(sdec,0,3))
    mm=fix(strmid(sdec,4,2))
    ss=fix(strmid(sdec,7,2))
    dec(i)=ten(dd,mm,ss)
    teff(i)=te
    logz(i)=lz
    logg(i)=lg
  endfor
  close,iun
  free_lun,iun
  goto,fini
endif

iz=strpos(filin,'m67.sumt',0)
if(iz ge 0) then begin
  openr,iun,filin,/get_lun
; count lines of input
  ss=''
  nl=0
  while(not eof(iun)) do begin
   readf,iun,ss
   nl=nl+1
  endwhile
  nl=nl-1            ; to allow for header line
  
; make output arrays, go back to beginning, fill them
  f2='(a11,1x,a10,1x,a9,1x,i5,1x,f4.1,1x,f4.1,1x,i4,1x,i5,1x,f5.1,1x,'+ $
       'f7.2,1x,f5.2,1x,f5.2,1x,f5.2,1x,f5.2,1x,f8.2)'
  ra=dblarr(nl)
  dec=dblarr(nl)
  teff=fltarr(nl)
  logz=fltarr(nl)
  logg=fltarr(nl)
  chi2=fltarr(nl)
  s1=''
  sra=''
  sdec=''
  point_lun,iun,0
  readf,iun,s1
  for i=0,nl-1 do begin
    readf,iun,s1,sra,sdec,te,lz,lg,nn,sp,vr,va,pm,ex,int,ei,c2,format=f2
    hh=fix(strmid(sra,0,2))
    mm=fix(strmid(sra,3,2))
    ss=float(strmid(sra,6,4))
    ra(i)=ten(hh,mm,ss)
    dd=fix(strmid(sdec,0,3))
    mm=fix(strmid(sdec,4,2))
    ss=fix(strmid(sdec,7,2))
    dec(i)=ten(dd,mm,ss)
    teff(i)=te
    logz(i)=lz
    logg(i)=lg
    chi2(i)=c2
  endfor
  close,iun
  free_lun,iun
  goto,fini
endif

fini:
end