        SUBROUTINE WRWTAG
C
C  PROGRAMMER:  R.S. ZHOU
C               ALFRED UNIVERSITY,  FEB-1987
C updated by Stefan Justi 1992
C       THIS ROUTINE WRITES OUT A SCINTAG RAW DATA FILE
C       FROM THE COMMON BLOCKS
C
        INCLUDE         'FILECONV.CBS'
        PARAMETER       NMSPD = 3200 !number of microsteps per degree
        INTEGER*4       Y,M,D,H,S
        CHARACTER       ITIT(20)*4
        EQUIVALENCE     (ITIT(1),SLID)
 
        CLOSE( UNIT=IOUT )
        OPEN( UNIT=IOUT, FILE=FILOUT, STATUS='NEW',
     1 FORM='UNFORMATTED', ACCESS='SEQUENTIAL', RECL=1,
     1 RECORDTYPE='FIXED', ERR=888 )
C
C  WRITE THE FILE HEADER INFORMATION...
C
        DO I=1,20,1
          WRITE(IOUT,ERR=888) ITIT(I)
        ENDDO
 
        WRITE(IOUT,ERR=888) INT( BANG(1) * NMSPD)       ! STARTING ANGLE
        WRITE(IOUT,ERR=888) INT( BANG(2) * NMSPD)       ! STOPPING ANGLE
        WRITE(IOUT,ERR=888) INT( BANG(3) * NMSPD)       ! WIDTH
 
        ZERO = 0.0
        WRITE(IOUT,ERR=888) DANG(10)    ! preset time (ex.: 3.0 )
        WRITE(IOUT,ERR=888) ZERO        ! preset count
        WRITE(IOUT,ERR=888) WAVE(1,4)   ! wave length for Cu
 
        IZERO = 0
        WRITE(IOUT,ERR=888) NMSPD       ! number of microsteps per degree
        WRITE(IOUT,ERR=888) izero       ! spin
        WRITE(IOUT,ERR=888) izero       ! oscill.
 
        CALL CONV_DATE_SCT( DATM, Y,M,D )
        WRITE(IOUT,ERR=888) Y
        WRITE(IOUT,ERR=888) M
        WRITE(IOUT,ERR=888) D
 
        CALL CONV_TIME_SCT( TIMM, H,M,S )
        WRITE(IOUT,ERR=888) H
        WRITE(IOUT,ERR=888) M
        WRITE(IOUT,ERR=888) S
 
C--------- total nr of records: nr of points x 2 rec/point + 50 rec (header):
        WRITE(IOUT,ERR=888) (NPTS * 2 + 50-1)
 
C--------- angle of max intensity: BANG(4) x 60 / DANG(10)
        WRITE(IOUT,ERR=888) INT(BANG(4) * 60 / DANG(10))
 
C--------- max intensity position: point x 2 rec/point + 50 + 1
        WRITE(IOUT,ERR=888) INT( (DANG(5)-DANG(1))/DANG(3)*2+50+1 )
 
        DO I=1,12,1
          WRITE(IOUT,ERR=888) IZERO
        ENDDO
 
C
C  WRITE THE INTENSITY DATA...
C
        ANG = DANG(1)
        DO I=1, NPTS
           IY = INT( YOBS(I) * 60. / DANG(10) )
           WRITE(IOUT,ERR=888)  IY
           WRITE(IOUT,ERR=888)  IZERO
           ANG = ANG + DANG(3)
        ENDDO
        GOTO 999
 
888     IERR = 1
999     RETURN
        END
 
 
C-----------------------------------------------------------------------
        SUBROUTINE CONV_DATE_SCT( CDAY, Y,M,D )
C
C  PROGRAMMER:  MIHAI OLARIU
C
C       THIS ROUTINE DECODES D-500 SYSTEM DATE TO SCINTAG FORMAT
C       Ex:  41090 --> 1990 10 4  (4-oct-1990)
C
        CHARACTER       DAT*6
        INTEGER*4       D,M,Y
 
        IF( CDAY.GT.0.0 ) THEN
            IDAY = CDAY                 ! float --> integer
            ENCODE( 6, 1, DAT ) IDAY    ! DAT has date as a string; ex 041090
            DECODE( 2, 2, DAT(1:2) ) D
            DECODE( 2, 2, DAT(3:4) ) M
            M = M/3 + 1
            DECODE( 2, 2, DAT(5:6) ) Y
            Y = Y + 1900
        ENDIF
1       FORMAT(I6)
2       FORMAT(I4)
 
        RETURN
        END
 
C-----------------------------------------------------------------------
        SUBROUTINE CONV_TIME_SCT( RTIM, H,M,S )
C
C  PROGRAMMER:  MIHAI OLARIU
C
C       THIS ROUTINE DECODES D-500 SYSTEM TIME INTO SCINTAG FORMAT
C       Ex:  200512. --> 20 05 12  (ie. 20:05:12)
C
        CHARACTER       ATIM*6
        INTEGER*4       H,M,S
 
1       FORMAT(I6)
2       FORMAT(I4)
 
        ITIM = RTIM                     ! float to integer conversion
        ENCODE( 6, 1, ATIM ) ITIM        ! integer to string conversion
        DECODE( 2, 2, ATIM(1:2)) H      ! get hours, mins, secs
        DECODE( 2, 2, ATIM(3:4)) M
        DECODE( 2, 2, ATIM(5:6)) S
 
        RETURN
        END
 
