       SUBROUTINE PKTCAP_LOAD(SATELLITE,INSTRUMENT,MODE,PACKET)
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine PKTCAP_LOAD
C.LIBRARY:        XASLIB                     
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 10 May 93 - Original version
C                 0.1 - 04 Oct 94 - removed debug messages
C                 0.2 - 15 Apr 96 - logical unit via FREE_LU
C.PURPOSE:        Load packetcap record in common block                       
C.METHOD:         read from disk file
C.SYNTAX:         CALL  PKTCAP_LOAD(SATELLITE,INSTRUMENT,MODE,PACKET)    
C.PARAMETERS:     CHARACTER satellite  : the satellite code
C                 CHARACTER instrument : the instrument code
C                 CHARACTER mode       : mode code (DIR, IND ...)
C                 CHARCATER packet     : packet type
C.RESTRICTIONS:   
C.NOTES:          contains debug write statements
C.FILES:          
C.REFERENCES      
C
C----------------------------------------------------------------------
C   
C      Loads relevant packetcap record in COMMON
C      Return VOS errors if not found or record too long
C
       CHARACTER*(*) SATELLITE,INSTRUMENT,MODE,PACKET
       CHARACTER     FILE*256,NAME*256,THISPACKET*16,BACKSLASH
       PARAMETER     (BACKSLASH=CHAR(92))
C-->   max length of an individual packetcap file line is 132
       CHARACTER      RECORD*132
       INTEGER       I,TRUE_LENGTH,IVOS,ISYS,IPTR ,L,IERR,LU
       LOGICAL       VOSERROR,FOUND,TC
C-->   max length of packetcap entry is 1024
       CHARACTER     PKCOMMON_ENTRY*1024
       COMMON /PKCOMMON/ PKCOMMON_ENTRY
       INCLUDE 'voscommon.inc'
C
C      Build packetcap file name
C
       FILE=SATELLITE
       I=TRUE_LENGTH(FILE) 
       FILE(I+1:)=INSTRUMENT
       I=TRUE_LENGTH(FILE) 
       FILE(I+1:)=MODE
       I=TRUE_LENGTH(FILE) 
       FILE(I+1:)='.packetcap'
C
C      and open the file (it shall reside in $XASTOP/calib/sax/<instrument>
C      allocating a free logical unit
C
       CALL BUILDPATH(FILE,'CALIB',NAME)
       i=true_length(name)
       WRITE(*,*)' packetcap file name is ',NAME (:i)  
       CALL FREE_LU(LU)
C-->   should give error if LU is -1
       CALL Z_OPEN(LU,NAME,'SEQ','OLD',0)
C       write (*,*) 'Open failure'
       IF(VOSERROR(IVOS,ISYS))RETURN
C       write(*,*)' file opened OK'
C
C      scan the file looking for the wished record
C
       FOUND=.FALSE.
       TC   =.FALSE.
       THISPACKET=PACKET
       CALL UPCASE(THISPACKET)
       L=TRUE_LENGTH(THISPACKET)
C
C ---- start reading loop -------------------------------------------------
  1    CONTINUE
       READ(LU,'(A)',END=998,ERR=999,IOSTAT=IERR)RECORD
C      ignore comment lines (with # in column ONE)
       IF(RECORD(1:1).EQ.'#')GOTO 1
C
       IF(.NOT.FOUND
     .    .AND.INDEX(RECORD(:L+1),THISPACKET(:L)//'|').NE.0
     .    .OR. INDEX(RECORD(:L+1),THISPACKET(:L)//':').NE.0)THEN
C        this is the first line of the wished packet 
C        the packet name shall come in column ONE, separated by a colon
C        or optionally folloewd by a comment separated by a pipe
         FOUND=.TRUE.
C
C        store record into COMMON block
         IF(.NOT.TC)THEN
C           it this is the packet requested by the user (and not a tc record
C           to which it secondarily points), just append record
            PKCOMMON_ENTRY=RECORD
C            write(*,*)' adding ',record
C            write(*,*) 'PKCOMMON_ENTRY=',PKCOMMON_ENTRY
         ELSE
C           if processing a tc record it shall not append the packet name
C           and comment, but just the rest of the fields (after first colon)
            I=INDEX(RECORD,':')+1
C-->        must also test overflow when appending : enough space in ENTRY ?
            PKCOMMON_ENTRY(IPTR+1:)=RECORD(I:)
C            write(*,*)' adding ',record(i:)
C            write(*,*) 'PKCOMMON_ENTRY=',PKCOMMON_ENTRY
         ENDIF 
C
       ELSEIF(FOUND)THEN
C        this is a continuation line of the wished packet      
C        ignore any leading blanks and append from first colon   
         I=INDEX(RECORD,':')+1
C-->     must also test overflow when appending : enough space in ENTRY ?
         PKCOMMON_ENTRY(IPTR+1:)=RECORD(I:)
C          write(*,*)' adding ',record(i:)
C
       ELSE
C        it refers to another record and shall be skipped
         GOTO 1
       ENDIF          
C
C      check if continuation records are required (line ends in \), if yes loop 
C
C       write(*,*) 'PKCOMMON_ENTRY=',PKCOMMON_ENTRY
       IPTR=TRUE_LENGTH(PKCOMMON_ENTRY)
       IF(PKCOMMON_ENTRY(IPTR:IPTR).NE.BACKSLASH)GOTO 2
       IPTR=IPTR-1
       GOTO 1
C----- end reading loop ---------------------------------------------------------
C
C      the series of records is terminated ... look if a tc field requests  
C      to load additional secondary entries ...
C
 2     CONTINUE
C       write(*,*)' entry was completed '
       IPTR=INDEX(PKCOMMON_ENTRY,':tc=')
       IF(IPTR.NE.0)THEN
C        a tc entry points to another packet type, which must appear further down
C        in the file, hence go back and look recursively for it
         TC=.TRUE.
         FOUND=.FALSE.         
C        write(*,*)' tc record requested '
         THISPACKET=PKCOMMON_ENTRY(IPTR+4:)
         I=INDEX(THISPACKET,':')
         THISPACKET=THISPACKET(:I-1)
C-->     must make sure entry is :tc=xxxx:  and give error on :tc=: and :tc=xxxx  
         L=TRUE_LENGTH(THISPACKET)
C         write(*,*)' referring to tc entry for packet ',thispacket(:l) 
         GOTO 1
       ENDIF
       CLOSE(LU)
       RETURN
C
C----- error conditions --------------------------------------------------------
C-->   all codes are provisional
C
C      truncation error
C997   CONTINUE
C      VOSCOMMON_ERROR=9999
C      RETURN
C
C      packet not found (read until EOF)
 998   CONTINUE
       IF(.NOT.FOUND)VOSCOMMON_ERROR=9998
       RETURN
C
C      Fortran error
 999   VOSCOMMON_ERROR=9997
       VOSCOMMON_SYSTEMERROR=IERR 
       RETURN
       END
