c    --------------------------------------------------------------
      subroutine wt_spec(infile,outfile,hduclas2,phaversn,fchan,
     &                   channel,counts,rcts,dtype,qerror,serr,
     &                   qsys,syserr,qqual,qualty,qgroup,grping,
     &                   qascale,ascale,qbscale,bscale,
     &                   n_comm,comms,ckeys,cform,ckrec,cdesc,nckeys,
     &                   cmpmode,ctype,errflg,chatter)
c    --------------------------------------------------------------
c --- DESCRIPTION -------------------------------------------------
c This subroutine writes the SPECTRUM extension with the current
c changes. It is appended onto the Primary Header. In OGIP standard
c format. Written for the HEASARC FTOOLS tasks GRPPHA.
c --- VARIABLES ---------------------------------------------------
      character*(*) infile,outfile,ckeys(*),ckrec(*)
      character*(*) cform(*),cdesc(*),cmpmode,ctype
      character*(*) task, comms(*), hduclas2, phaversn
      integer phsize,nchan,chatter,specext,nckeys,n_comm,fchan
      integer channel(*),counts(*),qualty(*)
      integer grping(*),dtype,errflg,detchans
      integer*2 conv(*)
      real serr(*),syserr(*),rcts(*),ascale(*),bscale(*)
      logical qerror,qsys,qqual,qgroup,long_str
      logical qascale,qbscale
      character*5 mnver
c --- VARIABLE DIRECTORY ------------------------------------------
c Arguments ...
c infile      char  : PHA filename, user defined
c outfile     char  : Output filename, user defined
c chatter     int   : Chattines flag
c phsize      int   : Array dimensions
c channel     int   : Array of detector channels
c dtype       int   : Datatype, 1 if counts, 2 is rcts
c counts      int   : Array of observed counts
c rcts        real  : Array of count rate
c qualty      int   : Array of qualty flag
c grping      int   : Array of grouping flag
c serr        real  : Array of Observed statistical errors
c syserr      real  : Array of Fractional systematic error
c ascale      real  : Array of areascal values
c bscale      real  : Array of backscal values
c qgroup      logical: True if data is grouped
c qqual       logical: True if data has qualty flags
c qerror      logical: True if statistical errors included
c qsys        logical: True if systematic errors included
c qascale     logical: True if vector areascal
c qbscale     logical: True if vector backscal
c ckeys       char   : Keywords that can have their values changed
c cform       char   : contains keywords and their form ; "S" or "E"
c                      in the form RESPFILE S
c ckrec       char   : Records of ckeys
c cdesc       char   : CHKEY record descriptions
c nckeys      int    : counter for above                          
c --- CALLED ROUTINES ---------------------------------------------
c FTOPEN        : (FITSIO) Opens FITS file
c FTCRHD        : (FITSIO) Creates header
c FTMAHD        : (FITSIO) Move to specified header number
c FTMRHD        : (FITSIO) Move a specified number of headers
c                 forward or backward
c FTGHSP        : (FITSIO) Obtain the number of keywords
c FTPHIS        : (FITSIO) Write history keywords
c FTBDEF        : (FITSIO) Define Binary header
c FTPCL(IE)     : (FITSIO) Write (integer real) column
c FTCLOS        : (FITSIO) Close FITS FILE
c --- AUTHORS/MODIFICATION HISTORY --------------------------------
c Rehana Yusaf (1993 April 7)
c Rehana Yusaf (1993 June 23) : Add history about this routine
c Rehana Yusaf (1993 July 6)  : CKEYS, and CKREC are used
c Rehana Yusaf (1993 July 19) : Comment array is passed to routine
c Rehana Yusaf (1993 August 6) (1.0.4): Use FTPREC instead of CFTPREC,
c                                illegal characters in comments or
c                                history, no longer supported                              
c Rehana Yusaf (1993 Nov 18) 1.0.5; hduclas2,phaversn, detchans and
c                                fchan are passed to this subroutine
c                                In addition new keyword CREATOR is
c                                used to write taskname and version
c Rehana Yusaf (1993 Dec 1) 1.0.6; Fix getkeys related bug, some
c                                were written twice.
c Rehana Yusaf (1993 Dec 6) 1.0.7; Fix chkey bug - chkey keywords
c                                were not being written after last
c                                change
c Rehana Yusaf (1994 Feb 23) 1.0.8; Write counts column in J form
c                                prev written as I.
c Rehana Yusaf (1994 July 7) 1.0.9; additional argument, cform,cdesc
c Rehana Yusaf (1994 Sept 14) 1.1.0; use FTPKYL to write long strings
c Rehana Yusaf (1994 Oct 26) 1.1.1; If any of the chkeys are not in
c                                   original file then still write
c Rehana Yusaf (1994 Dec 19) 1.1.2; POISERR = T when qerror = T
c                                   POISERR = F when false
c                                   bug fix, write 'CHANNEL' not '
c Rehana Yusaf (1995 Dec 14) 1.1.3; addd wtinfo and friends
c Rehana Yusaf (1996 Feb 22) 1.1.4; added cmpmode
c Banashree Mitra Seifert (1996, March) 1.2.0:
c        . corrected call for wtwarm
c        . variable sizes to carry over from calling routine instead
c          defining fixed as phsize
c  Banashree Mitra Seifert (1996, JulY) 1.3.0:
c        . CHANTYPE included to write in the output file
c Banashree Mitra Seifert (1996, Sept12)1.4.0:
c        . if POISSON error not applicable, then POISSERR=FALSE
c          (it was by mistake TRUE)
c Banashree M Seifert (1997, May 15) 1.5.0:
c        . parameter hist increased from charecter*80 to 200 so
c          it can write long infile names
c kaa (2001 June 6) 1.6.0:
c        . Added support for vector AREASCAL and BACKSCAL
c ------------------------------------------------------------------