; MODIFICATION HISTORY:
;     18-Jul-2001 GN
;       Replaced get_index by ovsa_get_index to avoid conflict with Yohkoh
;-
pro bl_pseudo,infile,outfile,dxdydz
   openr,lun,/get_lun,infile
   openw,outlun,/get_lun,outfile
   line = ''
   head = strarr(2)
   readf,lun,head
   printf,outlun,head
   oldfile = ''
   datlun = 0
   while(not EOF(lun)) do begin
      readf,lun,line
      reads,strmid(line,12),hrec,tim
      filename = !defaults.datadir+strmid(line,0,12)
      if (filename ne oldfile) then begin
          if (datlun ne 0) then free_lun,datlun
          datlun = openarc(filename,a,nrec)
          if (datlun eq 0) then begin
             print,'BL_PSEUDO: Problem reading data (.ARC) file.'
             goto,skip
          endif
          idx = ovsa_get_index(a,nrec)
          if (size(idx,/type) eq 2) then begin
             print,'BL_PSEUDO: Data (.ARC) file does not have an INDEX segment.'
             free_lun,datlun
             goto,skip
          endif
          oldfile = filename
      endif
      ; Read info from the index
      this = where((*idx.pscan).srec eq hrec,nthis)
      if (nthis eq 0) then begin
         print,'BL_PSEUDO: Index entry for header record ',hrec,' not found.'
         goto,skip
      endif
      icode = where(idx.codes eq !segm.ephem,ncode)
      if (ncode ne 1) then begin
         print,'BL_PSEUDO: No EPHEM segment in INDEX list?'
         goto,skip
      endif
      this_scan = (*idx.pscan)[this]
      this_name = strmid(this_scan.srcname,0,8)   ; Read name, truncated to 8 char
      this_ephem_rec = this_scan.recs[icode]
      data = getdata(this_ephem_rec-1,a)
      ephem = decode(data,!SEGM.EPHEM)
      i = 0
      isrc = where(ephem.cal.caleph.name eq this_name,nsrc)
      if (nsrc eq 0) then begin
         print,'BL_PSEUDO: Source name not found in EPHEM segment?'
         goto,skip
      endif
      this_ra = ephem.cal.caleph[isrc].ra/10000.D     ;[degrees]
      this_dec = ephem.cal.caleph[isrc].dec/10000.D   ;[degrees]
      midmsec = tim*1000D
      mjd = cvdoy(this_scan.syear,this_scan.sday[0],/JUL) + midmsec/8.64D7
      this_ha = ovsa_lst(mjd)/240000.D - this_ra   ; Hour angle, in degrees
      dec = this_dec*!dtor
      ha  = this_ha *!dtor

      ; Calculate phase error, in degrees, at 5 GHz, corresponding to baseline errors
      ; dx,dy,dz, in cm.
      nant = n_elements(dxdydz[0,*])
      nbl = nant*2 - 3
      sgn = 1
      k = 0
      dx_cm = (dy_cm = (dz_cm = fltarr(nbl)))
      for i = 0, 1 do begin
         if (i eq 1) then sgn = -1
         for j = i+1,nant-1 do begin
            dx_cm[k] = sgn*(dxdydz[0,j] - dxdydz[0,i])
            dy_cm[k] = sgn*(dxdydz[1,j] - dxdydz[1,i])
            dz_cm[k] = sgn*(dxdydz[2,j] - dxdydz[2,i])
            k = k + 1
         endfor
      endfor
      phioff = [-50,50,-100]
      phase = 360*(cos(dec)*cos(ha)*dx_cm - cos(dec)*sin(ha)*dy_cm + sin(dec)*dz_cm)/6.0 + phioff

      ; Write out a line corresponding to input line
      for k = 0, nbl-1 do strput,line,string(phase[k],format='(F6.1)'),27+11*k
      printf,outlun,line
skip:
   endwhile
   free_lun,lun
   if (datlun ne 0) then free_lun,datlun
   free_lun,outlun

return
end