      SUBROUTINE OPEN_SPECTRUM(lu,filename,naxis1,nbins,nfields,izoff)       
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine OPEN_SPECTRUM                 
C.LIBRARY:        GENERAL
C.AUTHOR:         L.Chiappetti - IFCTR Milano
C.VERSIONS:       0.0 - xx Aug 92 - Demo version in demo_xas_spec
C                 0.1 - 11 Jun 93 - moved to GENERAL library
C                 0.2 - 01 Jul 97 - changed name check of first 2 columns
C.PURPOSE:        open am existing spectrum 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_SPECTRUM(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
      CHARACTER     T*3,LEFTNUMBER*3,KEY*8,VAL1*16,VAL2*16
      INCLUDE 'voscommon.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 'SPE' shall also declare error
C     IF(T.NE.'SPE')VOSCOMMON_ERROR=XE_NOSPECTRUM to be defined
      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-->  assuming a fixed order (?) and standard names for fields
      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
            L=0
C-->        these names may change for conformance with OGIP
            IF(VAL1(1:5).EQ.'LOWER')   L=1                  
            IF(VAL1(1:5).EQ.'UPPER')   L=2
            IF(VAL1.EQ.'DATA')    L=3
            IF(VAL1.EQ.'ERROR')   L=4
            IF(L.EQ.0)GOTO 2
            BINCOMMON_OFFSET(HCOMMON_CURFILE,L)=I
            BINCOMMON_BITPIX(HCOMMON_CURFILE,L)=J
            BINCOMMON_DIMENS(HCOMMON_CURFILE,L)=K
 2    CONTINUE
      RETURN
      END
