       SUBROUTINE CHECK_PACKET(PACKET,DATATYPE,N) 
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine CHECK_PACKET
C.LIBRARY:        FOTLIB                     
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 15 May 96 - based on SAX_WHICH_DATA 0.6
C                 0.1 - 24 Dec 96 - default FLIGHT is ON
C.PURPOSE:        checks how many eligible packets exist                  
C.METHOD:         look into the observation directories
C                 
C.SYNTAX:         CALL  CHECK_PACKET(PACKET,DATATYPE,N) 
C.PARAMETERS:     CHARACTER packet     : packet type (in, might be blank)
C                 CHARACTER datatype   : type of accumulation (in)
C                                        code as Image,Spectra,Photon,Time 
C                 INTEGER   n          : number of eligible packets
C.RESTRICTIONS:   
C.NOTES:          intended for use by SAXIACCUM and SAXSACCUM
C.FILES:          
C.REFERENCES      
C
C----------------------------------------------------------------------
C   
      CHARACTER  PACKET*(*),INSTR*4,BUFFER*10
      CHARACTER  FLIGHTDATA*3,DATATYPE,DUMMY,PACKET2*20
      INTEGER    LU,N,M,I,IERR
      INCLUDE  'hcommon.inc'
      INCLUDE  'bincommon.inc'
      INCLUDE  'accumcommon.inc'
C
       CALL GET_GLOBAL_DEFAULT('SPACECRAFT','NONE',INSTR)
       IF(INSTR.EQ.'NONE')THEN
         WRITE(*,*)' setting default spacecraft to SAX'
         CALL Z_SET_GLOBAL('SPACECRAFT','sax')   
       ENDIF      
C
C      retrieve instrument from context
       CALL GET_GLOBAL_DEFAULT('INSTRUMENT','NONE',INSTR)
       IF(INSTR.EQ.'NONE')GOTO 900
       ACCUMCOMMON_INSTR=INSTR
C
C-->   temporary switch for flight/ground data
       CALL GET_GLOBAL_DEFAULT('FLIGHT','ON',FLIGHTDATA)
       CALL UPCASE(FLIGHTDATA)
C
C      scan observation directory for "eligible" packets
C
       CALL SAX_OPEN_DIR(LU,1,DUMMY)
       IF(LU.EQ.-1)GOTO 901
       CALL UPCASE(DATATYPE)
       N=0
C
 1     CONTINUE
C      format of FOT record per latest documentation
C      ignore uninteresting fileds
       IF(FLIGHTDATA.EQ.'ON')THEN
C-->      the IOSTAT clause allows to read records with NULLs which otherwise give errors
C-->      another way would be to read the entire record (as in sax_acc_open_tlm)
          READ(LU,*,END=2,IOSTAT=IERR)DUMMY,I,PACKET2,I,I,I
       ELSE
          READ(LU,*,END=2)PACKET2
          I=INDEX(PACKET2,'.')
          PACKET2=PACKET2(I+1:)
       ENDIF
C      verify eligibility for the type of accumulation
C-->   use datatype passed by caller or could it use only context ?
       CALL LOWCASE(PACKET2)
C      list revised for FOT data, includes Panter cal data
C      excludes PDS GRBM 'grb' packets and exotic WFC modes
       IF(DATATYPE.EQ.'I')M=INDEX(PACKET2,'dir')+INDEX(PACKET2,'dip')+
     .                      INDEX(PACKET2,'ima')
       IF(DATATYPE.EQ.'S')M=INDEX(PACKET2,'dir')+INDEX(PACKET2,'dip')+
     .                      INDEX(PACKET2,'ind')+INDEX(PACKET2,'sp')+
     .                      INDEX(PACKET2,'cal')+INDEX(PACKET2,'lat')+
     .                      INDEX(PACKET2,'psa')
       IF(DATATYPE.EQ.'T')M=INDEX(PACKET2,'dir')+INDEX(PACKET2,'dip')+
     .                      INDEX(PACKET2,'ind')+INDEX(PACKET2,'tim')
       IF(DATATYPE.EQ.'P')M=INDEX(PACKET2,'dir')+INDEX(PACKET2,'dip')
       IF(DATATYPE.EQ.'H')M=INDEX(PACKET2,'eng')+INDEX(PACKET2,'hkd')+
     .                      INDEX(PACKET2,'rat')+INDEX(PACKET2,'hk')
C
       IF(M.NE.0)N=N+1
       GOTO 1
2      CONTINUE
       CLOSE(LU)  
C
       RETURN
C
C----- error conditions -------------------------------------------------------------
C
 900   WRITE(*,*)' No instrument defined '
       CALL Z_EXIT(99)
 901   CONTINUE
       CALL Z_EXIT(999)
       END
