Viewing contents of file '../idllib/contrib/buie/astrepro.pro'
;+
; NAME:
;  astrepro
; PURPOSE:
;  Re-reduce existing astrometry originally measured with ASTROM
; DESCRIPTION:
;  This file attempts to rereduce astrometric measurements in the current
;    directory.  These measures are x,y positions that are found in the
;    file, position.dat.  The files fitcoeff.dat and centers.dat are
;    scanned for the corresponding transformation relations between x,y
;    and RA,DEC.  Any object.ast files in this directory will be overwritten.
;
; CATEGORY:
;  Astrometry
; CALLING SEQUENCE:
;  astrepro
; INPUTS:
;  All input information comes from files.
;
; OPTIONAL INPUT PARAMETERS:
;
; KEYWORD INPUT PARAMETERS:
;
; OUTPUTS:
;  All output information is sent to files.
;
; KEYWORD OUTPUT PARAMETERS:
;
; COMMON BLOCKS:
;
; SIDE EFFECTS:
;
; RESTRICTIONS:
;
; PROCEDURE:
;
; MODIFICATION HISTORY:
;  97/06/20, Written by Marc W. Buie, Lowell Observatory
;
;-
PRO astrepro

   ; first check to see if the three required files exist.  If not, quit.
   err=''
   IF not exists('centers.dat') THEN err=[err,'centers.dat']
   IF not exists('position.dat') THEN err=[err,'position.dat']
   IF not exists('fitcoeff.dat') THEN err=[err,'fitcoeff.dat']

   IF n_elements(err) ne 1 THEN BEGIN
      IF n_elements(err) eq 2 THEN BEGIN
         print,'Input file ',err[1],' is not present.'
      ENDIF ELSE IF n_elements(err) eq 3 THEN BEGIN
         print,'Input files ',err[1],' and ',err[2],' are not present.'
      ENDIF ELSE IF n_elements(err) eq 4 THEN BEGIN
         print,'Input files ',err[1],', ',err[2],', and ',err[3],' are not present.'
      ENDIF
      print,'Unable to continue.'
      return
   ENDIF

   ; Pre-declare strings for later use.
   version=''
   line=''

   ; First load the position.dat file.
   readcol,'position.dat',pfn,pname,pobjrad,px,py,format='a,a,f,d,d',/silent
   npos=n_elements(pfn)
   print,npos,' total object positions found.'

   ; Next load the fit coefficients file, watch for different versions.
   openr,lun,'fitcoeff.dat',/get_lun

   ; Count the number of lines in file.
   nlines=0L
   WHILE not eof(lun) DO BEGIN
      readf,lun,line,format='(a1)'
      nlines=nlines+1
   ENDWHILE
   point_lun,lun,0

   ; First line gives hint to version
   readf,lun,version,format='(a)'
   IF version eq 'ASTFIT v1.0' THEN BEGIN
      nlines=nlines-1
   ENDIF ELSE BEGIN
      ; This is no recognized version tag, that means it's either an incorrect
      ;   file type, or, it's the original version that didn't have a tag.
      ;   If the latter, there must be a certain number of blank delimited
      ;   fields in the line.
      version=strtrim(strcompress(version),2)
      words=str_sep(version,' ')
      nwords=n_elements(words)
      IF nwords le 15 THEN BEGIN
         print,'fitcoeff.dat file is of an unrecognized format, aborting.'
         return
      ENDIF
      nterms = fix(total(fix(words[4:13])))
      IF nwords ne nterms+14 THEN BEGIN
         print,'fitcoeff.dat file is of an unrecognized format, aborting.'
         return
      ENDIF
      version='ASTFIT v0.0'
      point_lun,lun,0

   ENDELSE

   ; Common init
   ffn      = strarr(nlines)
   ftype    = strarr(nlines)
   xc       = dblarr(nlines)
   yc       = dblarr(nlines)
   cra      = dblarr(nlines)
   cdec     = dblarr(nlines)
   flagarr  = intarr(nlines,10)
   coeffarr = dblarr(nlines,10)
   ncoeffs  = intarr(nlines)
   print,nlines,' total coefficient sets found.'

   IF version eq 'ASTFIT v0.0' THEN BEGIN
      FOR i=0,nlines-1 DO BEGIN
         readf,lun,line,format='(a)'
         line=strtrim(strcompress(line),2)
         words=str_sep(line,' ')
         ffn[i] = words[0]
         ftype[i] = words[1]
         xc[i]    = double(words[2])
         yc[i]    = double(words[3])
         flagarr[i,*]=fix(words[4:13])
         ncoeffs[i]=fix(total(flagarr[i,*])+0.5)
         coeffarr[i,0:ncoeffs[i]-1] = double(words[14:14+ncoeffs[i]-1])
      ENDFOR

      ; Load the centers.dat file
      readcol,'centers.dat',cfn,cras,cdecs,format='a,a,a',/silent
      ncen=n_elements(cfn)
      cra0=raparse(cras)
      cdec0=decparse(cdecs)
      print,ncen,' total centers found.'

      ; Copy the centers to cra,cdec
      FOR i=0,nlines-1 DO BEGIN
         ; Locate index for center information
         z=where(ffn[i] eq cfn,count)
         IF count eq 0 THEN BEGIN
            print,'Error: No center found for file: ',ffn[i]
            ffn[i] = ''
         ENDIF ELSE IF count eq 1 THEN BEGIN
            cra[i]  = cra0[z[0]]
            cdec[i] = cdec0[z[0]]
         ENDIF ELSE BEGIN
            print,'Error: Multiple centers found for file: ',ffn[i]
            ffn[i] = ''
         ENDELSE
      ENDFOR

   ENDIF ELSE IF version eq 'ASTFIT v1.0' THEN BEGIN
      FOR i=0,nlines-1 DO BEGIN
         readf,lun,line,format='(a)'
         line=strtrim(strcompress(line),2)
         words=str_sep(line,' ')
         ffn[i] = words[0]
         ftype[i] = words[1]
         xc[i]    = double(words[2])
         yc[i]    = double(words[3])
         cra[i]   = raparse(words[4])
         cdec[i]  = decparse(words[5])
         flagarr[i,*]=fix(words[6:15])
         ncoeffs[i]=fix(total(flagarr[i,*])+0.5)
         coeffarr[i,0:ncoeffs[i]-1] = double(words[16:16+ncoeffs[i]-1])
      ENDFOR
   ENDIF

   free_lun,lun

   ; Create a list of unique objects in position data.
   oblist=pname[uniq(pname,sort(pname))]
   nobj=n_elements(oblist)

   ; Now, loop over the objects
   FOR j=0,nobj-1 DO BEGIN
      obfile = oblist[j]+'.ast'
      obnew  = oblist[j]+'.astnew'
      obold  = oblist[j]+'.old'

      IF exists(obfile) THEN BEGIN
         readcol,obfile,ofn,jd,mag,format='a,a,x,x,a',/silent
         print,obfile,': ',n_elements(ofn),' points'

         openw,lun,obnew,/get_lun
      
         FOR i=0,n_elements(ofn)-1 DO BEGIN

            ; Locate index for coefficients information
            z=where(ofn[i] eq ffn,count)
            IF count ne 2 THEN BEGIN
               print,'Error: No coefficients found for file: ',ofn[i]
               ploc = -1
            ENDIF ELSE BEGIN
               IF ftype[z[0]] eq 'eta' THEN BEGIN
                  floce = z[0]
                  flocx = z[1]
               ENDIF ELSE BEGIN
                  floce = z[1]
                  flocx = z[0]
               ENDELSE
               ploc = 0
            ENDELSE

            ; Locate index for centroid information
            IF ploc ne -1 THEN BEGIN
               z=where(ofn[i] eq pfn and oblist[j] eq pname,count)
               IF count ne 1 THEN BEGIN
                  print,'Error: No ',oblist[j],' centroid found for file: ',ofn[i]
                  ploc = -1
               ENDIF ELSE BEGIN
                  ploc = z[0]
               ENDELSE
            ENDIF

            IF ploc ne -1 THEN BEGIN
               cxi  = coeffarr[flocx,0:ncoeffs[flocx]-1]
               ceta = coeffarr[floce,0:ncoeffs[floce]-1]
               cxi  = cxi[*]
               ceta = ceta[*]
               xiterms  = flagarr[flocx,*]
               etaterms = flagarr[floce,*]
               xiterms  = xiterms[*]
               etaterms = etaterms[*]

               xi = asteval(px[ploc]-xc[flocx],py[ploc]-yc[flocx],cxi,xiterms)/3600.0d0*!dpi/180.0d0
               eta= asteval(px[ploc]-xc[floce],py[ploc]-yc[floce],ceta,etaterms)/3600.0d0*!dpi/180.0d0
               astsn2rd,xi,eta,cra[flocx],cdec[floce],ra,dec
               rastr,ra,4,ras
               decstr,dec,3,decs
               printf,lun,ofn[i],jd[i],ras,decs,mag[i], $
                  format='(a,1x,a,1x,a,1x,a,1x,a4)'
;               print,ofn[i],' ',jd[i],' ',ras,' ',decs,' ',mag[i]
            ENDIF

         ENDFOR ; loop over object points
         free_lun,lun
         spawn,'mv '+obfile+' '+obold+' ; mv '+obnew+' '+obfile

      ENDIF ELSE BEGIN
         print,'File ',obfile,' not found.'
      ENDELSE

   ENDFOR ; object for loop

END