C  PREDICT_LC.F == PROGRAM TO GENERATE PLOT OF RELATIVE PHASES
C     OF OBSERVED DATA POINTS AND PREDICTED PHASES AT SPECIFIC
C     TIMES ON A SPECIFIC NIGHT.  
C     USE TO DETERMINE WHEN TO SCHEDULE OBSERVATIONS
C
C     APR04 == ACL -- UPDATED 2006JUNE FOR MDM IR PHOTOM
C
      program predict_lc

      implicit real*8(a-h,o-z)

      character fyle*80, star*10
C      character fyle*80, star*10, rest*38

C HANDLE FILES
      write(6,110) 
 110  format(/,' Enter name of input data file (eg, jd.rrlyr):  ',$)
      read(5,100) fyle
      open(8,file=fyle,status='old')
 100  format(a)
      write(6,120) 
 120  format(' Enter name for output phased data file (eg, phi.rrlyr):  ',$)
      read(5,100) fyle
      open(10,file=fyle,status='new')
C PROJECTION OF OBS TIMES
C      write(6,130) 
C 130  format(' Enter beginning HJDs (obs1, obs2):  ',$)
C      read(5,*) hjd1,hjd2
C      write(6,132) 
C 132  format(' Enter number of nights:  ',$)
C      read(5,*) nights
      write(6,132) 
 132  format(' Enter integer part of JD tonight (eg 2453890):  ',$)
      read(5,*) hjdnow
C      write(6,134) 
C 134  format(' Enter mag for predicted times:  ',$)
C      read(5,*) brti
      brti = 0.5

C READ HEADER STUFF
      read(8,140) star,per
 140  format(a10,2x,f10.8)
      write(6,140) star,per
C      write(10,140) star,per
C      read(8,100)
C      write(10,100)
C      read(8,100)
C      write(10,100)

C PUT DOTS IN CORNERS OF PLOT
       write(10,270) 
 270   format(' 0.0  -0.1')
       write(10,272) 
 272   format(' 1.0   0.55')
    

      ndata = 0
      iut = -1
      do 10 i=1,9999
C         read(8,*,end=11) hjd, vmagi,cmagi, vmagk,cmagk
C         read(8,*,end=11) hjd, vmagi,cmagi
         read(8,*,end=11) hjd
C         write(6,*) hjd, vmagi
         if(i.eq.1) hjd0 = hjd
         ratio = (hjd-hjd0)/per
         phi = ratio - int(ratio)
         if(phi.lt.0.0) phi=phi+1.0
         if(phi.ge.1.0) phi=phi-1.0         
C  DeltaMag == var-comp
C         dmagi = vmagi-cmagi
C         dmagi = (vmagi-cmagi)*-1.0
C         dmagk = vmagk-cmagk
         ndata = ndata+1
C         write(10,200) hjd,vmagi,cmagi
C 200     format(f14.5, 2f8.3)
         write(10,200) phi,hjd
 200     format(f8.4,'  0.0', f14.5)
 10      continue

C  PREDICTED VALUES AT SAME ST EACH NIGHT (24h-04m=0.9972222d)
C 11   do 20 i=1,nights
C         pjd1 = hjd1 + 0.9972222*(i-1)
C         pjd2 = hjd2 + 0.9972222*(i-1)

C    THIS DOES IT EVERY 0.1 DAYS, STARTING AT JD.5 = UT0h
C 11   do 20 i=1,6
C          pjd1 = hjdnow + 0.1*(i+4)
C    THIS DOES IT EVERY HOUR, STARTING AT JD.5 = UT0h
 11   do 20 i=1,13
         iut = iut+1
         pjd1 = hjdnow + 0.5 + 0.04166667*(i-1)
C         brt1 = (brti - 0.02*(i-1))*-1.0
         brt1 = (brti - 0.02*(i-1))
         ratio = (pjd1-hjd0)/per
         phi = ratio - int(ratio)
         if(phi.lt.0.0) phi=phi+1.0
         if(phi.ge.1.0) phi=phi-1.0         
C         write(10,220) pjd1,brt1
C 220     format(f14.5, f8.3,'   0.00 ')
         write(10,220) phi,brt1,pjd1,iut
 220     format(f8.4, f8.3, f14.5, i4)
 20      continue

      write(6,300) ndata
 300  format(i5,' actual observations plotted')
      write(6,310) fyle
 310  format(/,'graph ',a12,' pointmode+ szmark=0.01')
      write(6,320)
 320  format(/,'Diagonal dots: UT = 0h, 1h, 2h, ..., 12h')
      stop
      end
