      SUBROUTINE IO(AT,AF,AA,PH,SIGA,SIGPH,IPART,AJDH,IND,E,OMINC,MODE) 
C     
C     ROUTINE TO READ/WTRITE TO DISK FILE, THE SET OF TIMES 
C     (HJD FORMAT), PERIODS AND AMPLITUDES OF THE MAXIMA OF THE   
C     FOURIER TRANSFORMS CALCULATED BY ANALYS   
C     
      DIMENSION AT(1),AF(2,500),IND(1),STORE(8,1000),ISTORE(2,1000)     
      DIMENSION AA(1),PH(1),SIGA(1),SIGPH(1),E(1),OMINC(1)  
      CHARACTER FNAME*25
      DOUBLE PRECISION AJDH,X,Y     
      READ 1,FNAME
      CLOSE(11)   
      OPEN(11,FILE=FNAME,STATUS='UNKNOWN')
      LU=11 
      READ *,HJD1,HJD2  
    1 FORMAT(A)   
      NOREC=0     
      IF(HJD1.EQ.0.0)HJD1=-1.0
      IF(HJD2.EQ.0.0)HJD2=1.E10     
      IF(MODE.EQ.1)GO TO 50   
      READ(LU,*,ERR=5)NOREC   
      GO TO 10    
    5 PRINT 6     
    6 FORMAT(12X,'DATA FILE EMPTY') 
   10 PRINT 11,NOREC,FNAME    
   11 FORMAT(12X,'READING ',I3,' RECORDS FROM FILE ',A,' : '/)    
      IC=0  
      IF(NOREC.GT.0)GO TO 14  
      PRINT 12    
   12 FORMAT(12X,33HNO RECORDS READ: MEMORY UNCHANGED/)     
      RETURN
   14 DO 20 J=1,NOREC   
        READ(LU,15)ITIME,FTIME,PER,AMP,PHIN,SIGAIN,SIGPIN,IN,CYC,OMC    
   15   FORMAT(I10,0PF8.7,1P5E15.6,I10,0PF10.0,0PF10.6) 
        X=FTIME   
        X=X+ITIME
        IF(X.LT.HJD1.OR.X.GT.HJD2)GO TO 20
        IC=IC+1   
        AF(2,IC)=PER    
        AA(IC)=AMP
        IND(IC)=IN
        IF(IC.EQ.1)AJDH=X     
        AT(IC)=(X-AJDH)*86400.0D0   
        AF(1,IC)=1.0/PER
        PH(IC)=PHIN     
        SIGA(IC)=SIGAIN 
        SIGPH(IC)=SIGPIN
        E(IC)=CYC 
        OMINC(IC)=OMC   
        PRINT 18,X,AA(IC),AT(IC),AJDH     
   18   FORMAT(12X,'HJD = ',4PE15.5,'   AMP = ',1PE15.5,'     TIME = ', 
     *         0PF11.1,'  RELATIVE TO HJD ',4PD15.5)  
   20 CONTINUE    
      REWIND LU   
      CLOSE(LU)
      IPART=IC    
      RETURN
   50 READ(LU,*,ERR=80)NOREC  
      IF(NOREC.EQ.0)GO TO 70  
      DO 60 J=1,NOREC   
        READ(LU,15)ISTORE(1,J),(STORE(K,J),K=1,6),ISTORE(2,J),    
     *             (STORE(K,J),K=7,8)     
   60 CONTINUE    
   70 REWIND LU   
   80 II=NOREC+IPART    
      PRINT 81,II,FNAME 
   81 FORMAT(12X,'WRITING A TOTAL OF ',I3,' RECORDS OUT TO FILE : ',A)  
      WRITE(LU,*)II     
      INOREC=0    
      IF(NOREC.GT.0)INOREC=1  
      III=0 
      IF(IPART.GT.0)III=1     
      IBOLD=0     
      ININ=0
      INLAST=0    
      DO 100 J=1,10000  
        X=-1.0    
        IF(III.GT.0)X=AT(III)/86400.D0+AJDH     
        Y=-1.0    
        IF(INOREC.GT.0)THEN
          Y=ISTORE(1,INOREC)
          Y=Y+STORE(1,INOREC)   
        END IF
        IF(X.LT.0.0.AND.Y.LT.0.0)GO TO 200
        IF((X.LT.0.0.AND.Y.GE.0.0).OR.    
     *     (X.GE.0.0.AND.Y.GE.0.0.AND.X.GT.Y))GO TO 85
        IF((X.GE.0.0.AND.Y.LT.0.0).OR.    
     *     (X.GE.0.0.AND.Y.GE.0.0.AND.X.LE.Y))GO TO 72
        PRINT 71,X,Y,III,INOREC,II,IPART,NOREC  
   71   FORMAT(12X,47HSOMETHING WRONG WITH READ/WRITE TO DISK:CHECK!!/  
     *         12X,3HX= ,D15.6/12X,3HY= ,D15.6/12X,5HIII= ,I5/12X,
     *         8HINOREC= ,I5/12X,4HII= ,I5/12X,7HIPART= ,I5/12X,  
     *         7HNOREC= ,I5)  
        RETURN    
   72   ITIME=X   
        FTIME=X-ITIME   
        PER=AF(2,III)   
        AMP=AA(III)     
        PHIN=PH(III)    
        SIGAIN=SIGA(III)
        SIGPIN=SIGPH(III)     
        IN=IND(III)     
        CYC=E(III)
        OMC=OMINC(III)  
        IF((IN.NE.ININ.AND.IN.NE.INLAST).OR.    
     *     (IN.EQ.ININ.AND.IBOLD.EQ.2))ININ=ININ+1    
   75   WRITE(LU,15)ITIME,FTIME,PER,AMP,PHIN,SIGAIN,SIGPIN,ININ,CYC,OMC 
        III=III+1 
        IF(III.GT.IPART)III=0 
        IBOLD=1   
        INLAST=IN 
        GO TO 100 
   85   ITIME=Y   
        FTIME=Y-ITIME   
        PER=STORE(2,INOREC)   
        AMP=STORE(3,INOREC)   
        PHIN=STORE(4,INOREC)  
        SIGAIN=STORE(5,INOREC)
        SIGPIN=STORE(6,INOREC)
        IN=ISTORE(2,INOREC)   
        CYC=STORE(7,INOREC)   
        OMC=STORE(8,INOREC)   
        IF((IN.NE.ININ.AND.IN.NE.INLAST).OR.    
     *     (IN.EQ.ININ.AND.IBOLD.EQ.1))ININ=ININ+1    
   95   WRITE(LU,15)ITIME,FTIME,PER,AMP,PHIN,SIGAIN,SIGPIN,ININ,CYC,OMC 
        INOREC=INOREC+1 
        IF(INOREC.GT.NOREC)INOREC=0 
        IBOLD=2   
        INLAST=IN 
  100 CONTINUE    
  200 ENDFILE LU  
      REWIND LU   
      CLOSE(LU)
      RETURN
      END   
