      PROGRAM TEST
C
C ********************************************************************
C ********************************************************************
C
      INTEGER ISIZE,IUNIT
      REAL, DIMENSION (:), ALLOCATABLE :: VALUES
      PARAMETER (ISIZE = 512000)
C
      ALLOCATE( VALUES(ISIZE) )
C
      IUNIT = 30
      ISTEP = 2
      NSTEP = 100
C
C      write fresh records to file
      CALL WRITE_TO_FILE(IUNIT,VALUES,ISIZE,NSTEP)
C
C      rewrite records from ISTEP
      CALL RE_WRITE_RECORD(IUNIT,VALUES,ISIZE,ISTEP,NSTEP)
C
      DEALLOCATE( VALUES )
C
      STOP
      END
C
C
C--------------------------------------------------------
C     CREATES A FILE WITH FRESH RECORD
C--------------------------------------------------------
      SUBROUTINE WRITE_TO_FILE(IUNIT,VALUES,ISIZE,NSTEP)
C
      INTEGER IUNIT,ISIZE,NSTEP
      DIMENSION VALUES(ISIZE)
      CHARACTER*4 NAME
C
      NAME = 'NAME'
C
      OPEN(UNIT=IUNIT,FILE='test.dat',STATUS='UNKNOWN'
     &    ,FORM='UNFORMATTED',IOSTAT=NERR,ERR=10)
      REWIND(IUNIT)
C
      IF ( NERR .NE. 0 ) THEN
  10    PRINT *, "Error while opening the file"
        CALL EXIT(1)
      ENDIF
C
C
      DO I = 1,ISIZE
        VALUES(I) = I
      ENDDO
C
      DO I = 1, NSTEP
        WRITE(IUNIT) NAME
        WRITE(IUNIT) I
        WRITE(IUNIT) (VALUES(IT),IT=1,ISIZE)
      ENDDO
C
      CLOSE(IUNIT)
C
      RETURN
      END
C
C
C
C
C--------------------------------------------------------
C    REWRITES THE RECORD FROM PARTICULAR STEP
C--------------------------------------------------------
      SUBROUTINE RE_WRITE_RECORD(IUNIT,VALUES,ISIZE,ISTEP,NSTEP)
C
      CHARACTER*4 NAME
      INTEGER IUNIT,ISIZE,ISTEP,NSTEP
      DIMENSION VALUES(ISIZE)
C
      OPEN(UNIT=IUNIT,FILE='test.dat',STATUS='UNKNOWN'
     &    ,FORM='UNFORMATTED',IOSTAT=NERR,ERR=20)
      REWIND(IUNIT)
C
      IF ( NERR .NE. 0 ) THEN
  20    PRINT *, "Error while opening the file"
        CALL EXIT(1)
      ENDIF
C
      I = 0
      DO 50 WHILE( I .LT. ISTEP )
C
        READ(IUNIT) NAME
        READ(IUNIT) I
        READ(IUNIT) (VALUES(IT),IT=1,ISIZE)
C
        IF ( I.EQ.ISTEP ) THEN
          BACKSPACE(IUNIT)
          BACKSPACE(IUNIT)
          BACKSPACE(IUNIT,IOSTAT=IOS)
        ENDIF
C
  50   CONTINUE
C
C       rewrite the records from this position
C
      DO I = ISTEP,NSTEP
        WRITE(IUNIT) "NAME"
        WRITE(IUNIT) I
        WRITE(IUNIT) (VALUES(IT),IT=1,ISIZE)
      ENDDO
C
      CLOSE(IUNIT)
C
      RETURN
      END