	PROGRAM SEISMO_1
C
C THIS PROGRAM READS DATA FROM THE SEISMOMETRY A/D-CONVERTER
C AND WRITES IT INTO A RAW DATA FILE.
C
C THE FORMAT OF A SEISMIC DATAGRAM IS AS FOLLOWS:
C
C  SYNC SYNC LOW_0 HIGH_0 LOW_1 HIGH_1 .. LOW_7 HIGH_7
C
C THE SYNC CHARACTER IS 0XFF, EACH OF THE EIGHT DATA VALUES
C IS 2 BYTES, TRANSMITTED AS LOW/HIGH BYTE PAIR.
C
C THE FIRST COLUMN OF THE OUTPUT FILE CONTAINS EITHER THE
C VALUE 0 OR 1:
C
C  0: THE SYNC CHARACTER PAIR WAS NOT FOUND AT THE EXPECTED
C     PLACE, THE NUMBER FOLLOWING THIS MARKER REPRESENTS THE
C     NUMBER OF BYTES WHICH HAD TO BE SKIPPED UNTIL THE NEXT
C     SYNC CHARACTER COULD BE FOUND.
C  1: A COMPLETE DATAGRAM WAS READ, ALL EIGHT MEASUREMENT
C     VALUES ARE FOLLOWING AS REAL NUMBERS RANGING FROM 
C     +1 TO (NEARLY :-) ) -1 (MINUS ONE BIT).
C
C 03-JAN-2002, B. ULMANN FECIT
C
	IMPLICIT NONE
C
	INCLUDE '($SYSSRVNAM)'
C
	INTEGER*2 INPUT_CHANNEL
	INTEGER*4 STATUS, SKIPPED_BYTES, SYNCHRONIZE
	REAL*4 BLOCK (8)
	CHARACTER*(*) SEISMIC_IN, FILENAME
	CHARACTER SYNC_CHAR
C
	PARAMETER (SEISMIC_IN = 'CONVIN', SYNC_CHAR = 'A',
	1 FILENAME = 'SEISMIC.DATA')
C
	STATUS = SYS$ASSIGN (SEISMIC_IN, INPUT_CHANNEL, , )
	IF (.NOT. STATUS) CALL LIB$STOP (%VAL (STATUS))
C
	OPEN (UNIT = 1, STATUS = 'NEW', CARRIAGECONTROL = 'LIST',
	1     NAME = FILENAME)
C
	DO WHILE (.TRUE.)
	  SKIPPED_BYTES = SYNCHRONIZE (SYNC_CHAR, INPUT_CHANNEL)
	  IF (SKIPPED_BYTES .GT. 0) 
	1   WRITE (1, '(X, A, I)') '0	', SKIPPED_BYTES
	  CALL READ_BLOCK (INPUT_CHANNEL, BLOCK)
	  WRITE (1, '(X, A, 8F)') '1	', 
	1   BLOCK (1), BLOCK (2), BLOCK (3), BLOCK (4), 
	1   BLOCK (5), BLOCK (6), BLOCK (7), BLOCK (8)
	END DO
C
	CLOSE (UNIT = 1)
C
	END
C------------------------------------------------------------------------------
	SUBROUTINE READ_BLOCK (CHANNEL, BLOCK)
C
	IMPLICIT NONE
C
	CHARACTER BYTES (2), GET_BYTE
	INTEGER*2 CHANNEL, VALUE
	INTEGER*4 COUNTER
	REAL*4 BLOCK (8)
	EQUIVALENCE (BYTES, VALUE)
C
	DO COUNTER = 1, 8
	  BYTES (1) = GET_BYTE (CHANNEL)
	  BYTES (2) = GET_BYTE (CHANNEL)
	  IF (VALUE .GT. 32767) VALUE = 65536 - VALUE
	  BLOCK (COUNTER) = REAL (VALUE) / 32767.
	END DO
C
	END
C------------------------------------------------------------------------------
	INTEGER*4 FUNCTION SYNCHRONIZE (SYNC_CHAR, CHANNEL)
C
	IMPLICIT NONE
C
	LOGICAL*4 NOT_SYNCED
	INTEGER*2 CHANNEL
	INTEGER*4 SKIP_COUNTER
	CHARACTER SYNC_CHAR, GET_BYTE
C
	SKIP_COUNTER = -2
	NOT_SYNCED = .TRUE.
C
	DO WHILE (NOT_SYNCED)
	  SKIP_COUNTER = SKIP_COUNTER + 1
	  IF (GET_BYTE (CHANNEL) .EQ. SYNC_CHAR) THEN
	    SKIP_COUNTER = SKIP_COUNTER + 1
	    IF (GET_BYTE (CHANNEL) .EQ. SYNC_CHAR) NOT_SYNCED = .FALSE.
	  ENDIF
	END DO
	SYNCHRONIZE = SKIP_COUNTER
C
	END
C------------------------------------------------------------------------------
	CHARACTER FUNCTION GET_BYTE (INPUT_CHANNEL)
C
	IMPLICIT NONE
C
	INCLUDE '($SYSSRVNAM)'
	INCLUDE '($IODEF)'
C
	INTEGER*4 STATUS
	INTEGER*2 INPUT_CHANNEL
	CHARACTER INPUT
C
	STRUCTURE /TT_READ_IOSB/
	  INTEGER*2 STATUS
	  INTEGER*2 TERM_OFFSET
	  INTEGER*2 TERMINATOR
	  INTEGER*2 TERM_SIZE
	END STRUCTURE
	RECORD /TT_READ_IOSB/ READ_IOSB
C
	STATUS = SYS$QIOW (, 
	1                  %VAL (INPUT_CHANNEL),
	1                  %VAL (IO$_READVBLK .OR. IO$M_NOECHO),
	1                  READ_IOSB, 
	1                  , , 
	1                  %REF (INPUT), 
	1                  %VAL (1),
	1                  , , ,
	1                 )
	IF (.NOT. STATUS) CALL LIB$SIGNAL (%VAL (STATUS))
	  IF (.NOT. READ_IOSB.STATUS) 
	1   CALL LIB$SIGNAL (%VAL (READ_IOSB.STATUS))
C
	GET_BYTE = INPUT
C
	END
