       SUBROUTINE SAX_WHICH_DATA(PACKET,DATATYPE,N) 
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine SAX_WHICH_DATA
C.LIBRARY:        FOTLIB                     
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - 10 May 93 - Original DUMMY version
C                 0.1 - 04 Oct 94 - dialogue via X_READ + cal data support
C                 0.2 - 22 Mar 95 - support to observation chains
C                                   and tentative code for FOT
C                                   added DATATYPE argument
C                 0.3 - 05 Jul 95 - support to latest FOT layout document
C                 0.4 - 10 Apr 96 - compliance with FOT verified
C                 0.5 - 29 Apr 96 - returns also number of eligible packets
C                 0.4b- 12 Feb 96 - (not in library), added extra common
C                                   also loads packet name here in common
C                 0.6 - 14 May 96 - merged 0.4b changes in library
C                 0.7 - 24 Dec 96 -default FLIGHT is on
C.PURPOSE:        Select which telemetry data to use                     
C.METHOD:         PROVISIONALLY ask the user via usual XAS user interface
C                 a' regime should look into the observation directories
C                 
C.SYNTAX:         CALL  SAX_WHICH_DATA(PACKET)
C.PARAMETERS:     CHARACTER packet     : packet type (out)
C                 CHARACTER datatype   : type of accumulation (in)
C                                        code as Image,Spectra,Photon,Time ...
C.RESTRICTIONS:   
C.NOTES:          
C.FILES:          
C.REFERENCES      
C
C----------------------------------------------------------------------
C   
      CHARACTER  PACKET*(*),INSTR*4,BUFFER*10,BUFFER2*12,PREPARSE*12
      CHARACTER  FLIGHTDATA*3,DATATYPE,DUMMY,PACKET2*20
      INTEGER    LU,N,M,I,IERR
      INCLUDE  'hcommon.inc'
      INCLUDE  'bincommon.inc'
      INCLUDE  'accumcommon.inc'
C
      PACKET=' '
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
       write(*,*)' processing instrument ',instr 
       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)THEN
         N=N+1
         PACKET=PACKET2
         WRITE(*,*)' packets ',PACKET,' are eligible for accumulation'
       ENDIF
       GOTO 1
2      CONTINUE
       CLOSE(LU)       
C
C-->   provisional error handling
       IF(N.EQ.0)
     . WRITE(*,*)' WARNING : no eligible packets for accumulation'
       IF(N.GT.1)THEN 
C
C        ask manually if more than one eligible packet type
         WRITE(*,*)' select one of the above',N,' packet types'
         CALL X_PROMPT(' enter wished packet type ',0)
         CALL X_READ  (0,1,BUFFER)
         BUFFER2=PREPARSE(BUFFER)
         READ(BUFFER2,*,IOSTAT=IERR)PACKET
       ENDIF
C
C      save requested packet also here (redundantly repeated in sax_acc_open_tlm)
       CALL LOWCASE(PACKET)   
       ACCUMCOMMON_PACKET=PACKET
       RETURN
C
C----- error conditions -------------------------------------------------------------
C
 900   WRITE(*,*)' No instrument defined '
       CALL Z_EXIT(99)
 901   CONTINUE
       CALL Z_EXIT(999)
       END
