      SUBROUTINE OPEN_PHOTON(lu,filename,naxis1,nbins,nfields,izoff)
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine OPEN_PHOTON                   
C.LIBRARY:        GENERAL
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - xx Aug 92 - Demo version in demo_xas_hot
C                 0.1 -  4 Sep 92 - moved to GENERAL library
C                 0.2 -  8 Sep 92 - added support to multiple tables
C.VERSIONS:       1.0 - 29 Oct 92 - New error handling codes
C.PURPOSE:        open am existing photon file         
C.METHOD:         open via OPEN_OLD_XAS_FILE and fill table description 
C                 array in  common BINCOMMON. 
C                 error code ir returned thru VOSCOMMON               
C.SYNTAX:         CALL OPEN_PHOTON(lu,file,naxis,nbins,nfields,izoff)           
C
C.PARAMETERS:     INTEGER       lu            logical unit (input)
C                 CHARACTER*(*) file          VOS filename (input)
C                 all other argumetns are output parameters
C                 INTEGER       naxis1        record length         
C                 INTEGER       nbins         number of photons in file    
C                 INTEGER       nfields       number of fields in table  
C                 INTEGER       izoff         zero-record offset 
C.RESTRICTIONS:   none 
C.NOTES:          this makes an assumption about fields present           
C.FILES:          none
C.REFERENCES:     OPEN_OLD_XAS_FILE, H_READ_xKEYWORD, DECODETFORM
C                 and LEFTNUMBER in same library
C
C----------------------------------------------------------------------
C
C
C     error return thru voscommon !!!
C
C     INCLUDE 'implicit_none.inc'
      CHARACTER*(*) FILENAME
      INTEGER       LU,I,IXAS,IZOFF,NBINS,NFIELDS,NAXIS1,K,NELEM
      INTEGER       IERR,J,L,M
      CHARACTER     T*3,LEFTNUMBER*3,KEY*8,VAL1*16,VAL2*16
      INCLUDE      'voscommon.inc'
      INCLUDE      'errors.inc'
      INCLUDE      'hcommon.inc'
      INCLUDE      'bincommon.inc'
      EXTERNAL      BLKBINCOMMON
C
C     this is the routine to be called to open a generic XAS file
C
      CALL OPEN_OLD_XAS_FILE(LU,FILENAME,T,NAXIS1,
     .NBINS,IXAS,IZOFF)
C     if T is not 'PHO' shall also declare error
      IF(T.NE.'PHO')VOSCOMMON_ERROR=XE_NOPHOTON
      IF (VOSCOMMON_ERROR.NE.0)RETURN
C
C     read some mandatory keywords in header
C     a neat job shall test IERR after each call ...
C
      NELEM=1
      CALL H_READ_JKEYWORD('TFIELDS' ,NFIELDS,0,IERR,NELEM)
      IF (VOSCOMMON_ERROR.NE.0)RETURN
C     shall fill the table descriptor array ....
C     ... for the current table, which is also the current XAS file
C     assuming a fixed order (?) and standard names for fields
      M=0
      DO 2 I=1,NFIELDS
            KEY='TTYPE'//LEFTNUMBER(I,K)
            CALL H_READ_KEYWORD(KEY,VAL1,' ',IERR)
            KEY='TFORM'//LEFTNUMBER(I,K)
            CALL H_READ_KEYWORD(KEY,VAL2,' ',IERR)
            CALL DECODETFORM(VAL2,J,K)
            IF (VOSCOMMON_ERROR.NE.0)RETURN
C           here we have two options :
C 1)        one (between comments ***1*** below) is to recognise
C           only particular field names and give them a 
C           standard logical order : time x y pha bl
C 2)        the other one is to recognise all fields with a
C           non-blank label in whatever order they appear
C           and is given between ***2*** comments)
C
C ONLY ONE CHOICE SHALL BE ENABLED IN THE PROGRAM !!!!!
C
C***1***    arrange search to ignore unexpected fields
C           L=0
C           IF(VAL1.EQ.'TIME    ')  L=1
C           IF(VAL1.EQ.'XPOSITN ')  L=2
C           IF(VAL1.EQ.'YPOSITN ')  L=3
C           IF(VAL1.EQ.'PHA     ')  L=4
C           IF(VAL1.EQ.'BL      ')  L=5
C           IF(L.EQ.0)GOTO 2
C***1***    end
C
C***2***    take all labelled fields as they appear
            IF(VAL1.EQ.' ')GOTO 2
            M=M+1
            L=M
C***2***    end
C
            BINCOMMON_OFFSET(HCOMMON_CURFILE,L)=I
            BINCOMMON_BITPIX(HCOMMON_CURFILE,L)=J
            BINCOMMON_DIMENS(HCOMMON_CURFILE,L)=K
 2    CONTINUE
      RETURN
      END
