C******************************************************************************
C*     PROGRAM SYNTEX.FOR :
C*    
C*     1. SHOWS LAST POINT OF COLLECTED DATA BEFORE ABORT OF RUN. 
C*     2. SHOWS STARTING ANGLE FOR NEW RUN.
C*     3. COPIES MEASUREMENT POINTS FROM NEW RUN INTO THE PLACE
C*        OF ABORTED RUN.
C*
C*     AUTOR: SLAWEK ZDZIESZYNSKI             DATE: APR-20-92. 
C*
C******************************************************************************
      INTEGER*2 DUMY,ITYP,IOVER,MESFLG,NRNG,MPRO,NREP,FTYP,MTYP,
     &          FIRST,N2T,NTT,NIT,NTI,MCATYP,MCAVER,MCACHA,MCAMOD
      INTEGER*4 LENG,ISBF,NHDB,IRPR(12,2)
      CHARACTER IRLR*4,FILE*32,FILINP*32,NAME(3)*4,SLID(20)*4,SCAN*1
     &         ,ROTAT*1,CHGTYP,RING,DIFGAS
      REAL*4 BANG(15),YNEW(1500),YOBS(15000),THETA(15000),DATM,TIMM,
     &       INTMAX,RES,C2DER(5),C1DER(5),DTIME,TTOFF,MAXXTR,MAXYTR,
     &       SPCAN(2)
      DIMENSION NRNS(20),IRLR(12),KREC(20),MREC(20)
      DATA IINP/1/,LUNI/3/
      TYPE 10
10    FORMAT(////' *** This program helps to prepare a full RAW data',X,
     &'file   ***'/' *** from partial files in the case when previous',X,
     &'run'5X'***',/,' *** was aborted. Program shows last good writen',
     &' angle'4X'***'/' *** and angle which new run should start from.',
     &11X'***'/' *** After finish of data collecting the program allows'
     &,3X,'***',/,' *** to copy new data points into the first aborted'
     &,X,'file. ***',/,' *** It can help to save a time for long runs',X 
     &,'where'7X'***'/' *** probability of aborted run is',X,
     &'higher.',16X,'***',////) 
      TYPE 101,FILINP
101   FORMAT(//' *** ENTER FILE NAME OF ABORTED RUN (.RAW):',$,A32)
      ACCEPT 2,FILINP
2     FORMAT(A32)
      CLOSE( UNIT=IINP )

      OPEN( UNIT=IINP, FILE=FILINP, STATUS='OLD', ERR=888,
     &ACCESS='DIRECT', RECL=12 )
C
C  READ THE FILE HEADER BLOCK...
C
      MESFLG=1  
      READ(IINP,REC= 1) DUMY,ITYP,NHDB,ISBF,LENG,MESFLG,IOVER
      WRITE(IINP,REC= 1) DUMY,ITYP,NHDB,ISBF,LENG,MESFLG,IOVER

C
C  READ THE SPECIFICATION BLOCK REFERENCES...
C
      IREC = ISBF
     
      READ(IINP,REC=IREC)(IRPR(I,1),IRPR(I,2),IRLR(I),I=1,4)
      READ(IINP,REC=IREC+1)(IRPR(I,1),IRPR(I,2),IRLR(I),I=5,8)
      READ(IINP,REC=IREC+2)(IRPR(I,1),IRPR(I,2),IRLR(I),I=9,12)
C
C  READ FILE DESCRIPTION BLOCK...
C
      READ(IINP,REC=IRPR(3,1))NAME,DATM,TIMM,FTYP,MTYP
     &,INTMAX,RES,NREP,MPRO,NRNG,SCAN,ROTAT,(SPCAN(I),I=1,2)
C
C  READ THE LINE MEASUREMENT BLOCK...
C
     	IREC = IRPR(4,1)

      READ(IINP,REC= IREC) (BANG(I),I=1,12)
      READ(IINP,REC= IREC+1) (BANG(I),I=13,14)

      NPTS = INT( (BANG(2)-BANG(1))/BANG(3)+0.5)+1
      IF( NPTS.EQ.0) GO TO 999
C
C  READ THE DATA BLOCK  
C      
      FIRST=1
      LAST=12
      THETA(0)=BANG(1)-BANG(3)
      DO J=0,IRPR(6,2)-1
      IREC=IRPR(6,1)
      READ(IINP,REC=IREC+J) (YOBS(K),K=FIRST,LAST)
      DO K=FIRST,LAST
      IF(YOBS(K).GT.INTMAX)INTMAX=YOBS(K)
      THETA(K)=THETA(K-1)+BANG(3)
      IF(YOBS(K).EQ.0 )THEN !last readout before ABORT
      THETA(K-1) = (BANG(1)+(K-1)*BANG(3))-BANG(3)
      THETA(K) = (BANG(1)+K*BANG(3))-BANG(3)
      TYPE 115, THETA(K-1),THETA(K)
115   FORMAT(//' *** LAST CORRECT WRITEN ANGLE IS:',F8.3,/
     &' *** THE NEW MEASUREMENT SHOULD START FROM:'F8.3)         
      TYPE 16
16    FORMAT(//' ARE YOU READY TO WRITE NEW DATA INTO YOUR',X,
     &'ABORTED FILE (Y/N):',$)
      ACCEPT 17, CHOICE
17    FORMAT(A1)
      IF(CHOICE.EQ.'Y')  GO TO 1002
      IF(CHOICE.NE.'Y') GO TO 999
C
C  TAKE DATA FROM OTHER FILE.
C

1002    TYPE 113,FILE
113     FORMAT(//' *** ENTER YOUR SECOND FILE NAME:',$,A32)
        ACCEPT 112,FILE
112     FORMAT(A32)
     	CLOSE( UNIT=LUNI )
        OPEN( UNIT=LUNI, FILE=FILE, STATUS='OLD',          
     &  ACCESS='DIRECT', RECL=12,  READONLY )
        BANG(4)=INTMAX
        CALL TAKE(YNEW,LUNI,LPTS,N2T,NTT,C2DER,C1DER,NAME,DATM,
     &  TIMM,FTYP,MTYP,INTMAX,RES,NREP,MPRO,NRNG,SCAN,ROTAT,SPCAN)
        IF(BANG(4).GT.INTMAX)INTMAX=BANG(4)
        INTMAX=INTMAX/BANG(10)
        WRITE(IINP,REC=IRPR(3,1)) NAME,DATM,TIMM,FTYP,MTYP,
     &  INTMAX,RES,NREP,MPRO,NRNG,SCAN,ROTAT,(SPCAN(I),I=1,2)

        DO L=1,LPTS
        YOBS(L+K-1)=YNEW(L)
        ENDDO
        DO I=IREC+J,IRPR(6,2)+IRPR(6,1)
        WRITE(IINP,REC=I)(YOBS(L),L=FIRST,LAST)
        FIRST=FIRST+12
        LAST=LAST+12
        IF(K+LPTS-LAST.LT.0) LAST=K+LPTS
        ENDDO
        ENDIF
        ENDDO
        FIRST=FIRST+12
        LAST=LAST+12
        IF(NPTS-LAST.LT.0) GO TO 1000!  LAST=NPTS
        ENDDO
1000    TYPE 1001,LPTS,FILINP
1001    FORMAT(//' *** ',I5,' MEASUREMENT POINTS HAS BEEN WRITEN INTO' 
     &  ,X,'A FILE:',X,A15)
      WRITE(IINP,REC=IRPR(4,1))(BANG(I),I=1,12)

      READ(IINP,REC=IRPR(1,1)+6)DTIME,DIFGAS,RING,MAXXTR,MAXYTR,CHGTYP
     &                     ,MCATYP,MCAVER,MCACHA,MCAMOD,TTOFF,N2T
     &                     ,NTT,NIT,NTI  
      IF(N2T.NE.0. OR .NTT.NE.0) GO TO 1004
      CALL TAKE(YNEW,LUNI,LPTS,N2T,NTT,C2DER,C1DER)              
      WRITE(IINP,REC=IRPR(1,1)+7)(C2DER(I),I=1,5),(C1DER(I),I=1,5)
      IF(N2T.NE.0) TYPE 1006,N2T,FILINP
      IF(NTT.NE.0) TYPE 1006,NTT,FILINP
1006  FORMAT(//' ***',X,I1,'CALIBRATION COEFFICIENTS HAS BEEN WRITEN',X,
     & ' INTO FILE:',X,A15)
1004    CLOSE(UNIT=IINP)
        CLOSE(UNIT=LUNI)
     	GOTO 999

777	IERR = 1
     	GOTO 999
888	IERR = 2
999	STOP
    	END
      SUBROUTINE TAKE(YNEW,LUNI,LPTS,N2T,NTT,C2DER,C1DER  
     &,NAME,DATM,TIMM,FTYP,MTYP
     &,INTMAX,RES,NREP,MPRO,NRNG,SCAN,ROTAT,SPCAN)

     	INTEGER*2 DUMY,ITYP,IOVER,MESFLG,NRNG,MPRO,NREP
     &           ,FTYP,MTYP,FIRST
        INTEGER*4 LENG,ISBF,NHDB,IRPR(12,2)
     	CHARACTER IRLR*4,NAME(3)*4,SLID(20)*4,SCAN*1,ROTAT*1
        REAL*4 BANG(15),YNEW(15000),THETA(15000),DATM,TIMM,
     &         INTMAX,RES,SPCAN(2)
     	DIMENSION NRNS(20),IRLR(12),KREC(20),MREC(20),C2DER(5),C1DER(5)

C
C  READ THE FILE HEADER BLOCK...
C
     	READ(LUNI,REC= 1) DUMY,ITYP,NHDB,ISBF,LENG,MESFLG,IOVER
C
C  READ THE SPECIFICATION BLOCK REFERENCES...
C
     	IREC = ISBF                         !200
     
      READ(LUNI,REC=IREC)(IRPR(I,1),IRPR(I,2),IRLR(I),I=1,4)
      READ(LUNI,REC=IREC+1)(IRPR(I,1),IRPR(I,2),IRLR(I),I=5,8)
      READ(LUNI,REC=IREC+2)(IRPR(I,1),IRPR(I,2),IRLR(I),I=9,12)
C
C  READ FILE DESCRIPTION BLOCK
C
      READ(LUNI,REC=IRPR(3,1))NAME,DATM,TIMM,FTYP,MTYP
     &,INTMAX,RES,NREP,MPRO,NRNG,SCAN,ROTAT,(SPCAN(I),I=1,2)
C
C  READ THE LINE MEASUREMENT BLOCK...
C
     	IREC = IRPR(4,1)

              READ(LUNI,REC= IREC) (BANG(I),I=1,12)
     	      READ(LUNI,REC= IREC+1) (BANG(I),I=13,14)

        LPTS = INT( (BANG(2)-BANG(1))/BANG(3)+0.5)+1
        IF( LPTS.EQ.0) GO TO 888
        INTMAX=BANG(4)
C
C  READ THE DATA POINTS
C
        FIRST=1
        LAST=12
        DO J=0,IRPR(6,2)-1
        IREC=IRPR(6,1)
        READ(LUNI,REC=IREC+J ) (YNEW(L),L=FIRST,LAST)
        FIRST=FIRST+12
        LAST=LAST+12
        IF(LPTS-LAST.LT.0) LAST=LPTS
        ENDDO
C
C READ THE CALIBRATION COEFFICIENTS
C
C      READ(LUNI,REC=IRPR(1,1)+6)DTIME,DIFGAS,RING,MAXXTR,MAXYTR,CHGTYP
C     &                     ,MCATYP,MCAVER,MCACHA,MCAMOD,TTOFF,N2T
C     &                     ,NTT,NIT,NTI  

       READ(LUNI,REC=IRPR(1,1)+7)(C2DER(I),I=1,5),(C1DER(I),I=1,5)

888     RETURN
        END
C*****************************************************************************

