CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
      SUBROUTINE FIND_HEADER_LENGTH(FILENAME, APID, LENGTH)
C+
C
C FUNCTIONAL DESCRIPTION:
C
C    This routine finds the length of a file header for a SOHO data file.
C
C FORMAL ARGUMENTS:
C
C    FILENAME:
C	CHAR	  I	Name of SOHO file to be examined.
C
C    APID:
C	BYTE	  I	The application ID.
C
C    LENGTH:
C	I*4	  O	Upon return will contain the number of bytes in the
C			header of FILENAME.
C
C INTERFACE:
C
!	INTERFACE
!	  SUBROUTINE FIND_HEADER_LENGTH(FILENAME, APID, LENGTH)
!	    CHARACTER(LEN=80), INTENT(IN) :: FILENAME
!	    INTEGER(KIND=1), INTENT(IN) :: APID
!	    INTEGER, INTENT(OUT) :: LENGTH
!	  END SUBROUTINE FIND_HEADER_LENGTH
!	END INTERFACE
C
C DESIGN:
C
C    This routine finds the length of the header by using the following
C    assumptions: 
C
C        1) The file header is the first item in the file.
C        2) The file can be read by a Fortran unformatted READ of 512 bytes.
C           This is true if the file is organized as fixed-length 512-byte
C           records.  This is automatically the case if the file has been
C           transferred via binary FTP.
C        3) The file header is less 511 bytes in length; ie, is fully contained
C           within the first 512 bytes of the file.
C        4) The first thing after the file header is the first data packet,
C           whose first byte is 88(hex) and whose second byte is equal to the
C           application ID.
C
C    The routine does a Fortran OPEN on the file specified by FILENAME, reads
C    the first 512 bytes of the file, and methodically searches for the first
C    occurence of '88'X followed by APID.  One less than the index of '88'X is
C    the file header length.  If the file header is more than 512 bytes long,
C    this is not guaranteed to be in the first record of the file, and a
C    problem may result.
C
C IMPLICIT INPUTS:
C
C    None
C
C IMPLICIT OUTPUTS:
C
C    None
C
C SIDE EFFECTS:
C
C    Fortran unit 11 is opened, used to read the file, and closed.
C
C    If the tag '88'X and the APID are not found in the first 512 bytes this
C    routine will halt the program with a STOP 111.
C
C COMMON BLOCKS:
C
C    None
C
C INCLUDE FILES:
C
C    None
C
C CREATED:
C
C    9-OCT-1995    John Paquette
C
C This version:   1.2    2-DEC-1999
C
C DEVELOPMENT HISTORY:
C 
C  Author       Date		Description of Change 
C  --------	-----------	--------------------- 
C  J. PAQUETTE   9-OCT-1995     Created
C  J. PAQUETTE  12-OCT-1995     Added comments
C  L. Bleau	 9-APR-1999	added comments
C  L. Bleau	 2-DEC-1999	added comments, IMPLICIT NONE
C
C-
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
CDEC$ IDENT	'1.2'
      IMPLICIT NONE
      BYTE BUFFER(512), APID
      CHARACTER*80 FILENAME
      INTEGER I, LENGTH
C
C Open the input file and read the first record from it.
C
      OPEN(UNIT=11, NAME=FILENAME, TYPE='OLD', FORM='UNFORMATTED', READONLY)
      READ(11) BUFFER
C
C Find the start of the first data packet, which will be the hex digits 88
C followed by the APID. 
C
      I = 1
      DO 100 WHILE((ZEXT(BUFFER(I)) .NE. '88'X) .OR. (BUFFER(I+1) .NE. APID))
        I = I + 1
        IF(I.GT.512)STOP 111
 100  CONTINUE
C
C The start of the 1st packet is immediately after the end of the header.  The
C length of the header is thus 1 less than the index of the start of the first
C packet. 
C
      LENGTH = I - 1
C
C Now close the file.  The rest of the program is going to read the file using
C stream i/o (some C routines), not by using Fortran OPEN, READ etc.
C
      CLOSE(UNIT=11)
      RETURN
      END
