;+
; NAME:
;	WRT2DSPEC
;
; PURPOSE:
;	Write a multi-extension FITS file.
;
; CALLING SEQUENCE:
;        wrt2dspec, specname, image, sigmamap, mask, header, $
;           skyimage=, wset=, telluric_spec=, telluric_head=, $
;           datapath=, /gzip
;
; INPUTS:
;	specname - output file name
;
; OPTIONAL INPUTS:
;       skyimage      - two-dimensional sky image (see ISKYSUBTRACT2D) 
;       wset          - trace set generated by IARCFIT and selected by
;                       ISKYSUBTRACT2D corresponding to the
;                       two-dimensional wavelength map
;       telluric_spec - telluric absorption spectrum (see ISENSFUNC)
;       telluric_head - header for TELLURIC_SPEC
;	datapath      - I/O data path
;	
; KEYWORD PARAMETERS:
;       gzip - compress the output FITS file using SPAWN and GZIP 
;
; OUTPUTS:
;       A file called SPECNAME (FITS file) is written to DATAPATH.  
;
; COMMENTS:
;
; PROCEDURES USED:
;	MWRFITS, CWD()
;
; MODIFICATION HISTORY:
;	J. Moustakas, 2002 January 24, U of A
;       jm03apr10uofa - checked out ISPEC v1.0.0
;       jm03dec04uofa - added GZIP keyword
;       jm03dec08uofa - added SKYIMAGE optional input
;       jm03dec24uofa - added the TELLURIC_SPEC and TELLURIC_HEAD
;                       optional inputs
;       jm04sep01uofa - remove .GZ extension if it exists 
;       jm05jun20uofa - added WSET optional input; improved treatment
;                       of appending the telluric spectrum
;
; Copyright (C) 2002-2005, John Moustakas
; 
; This program is free software; you can redistribute it and/or modify 
; it under the terms of the GNU General Public License as published by 
; the Free Software Foundation; either version 2 of the License, or
; (at your option) any later version. 
; 
; This program is distributed in the hope that it will be useful, but 
; WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
; General Public License for more details. 
;-

pro wrt2dspec, specname, image, sigmamap, mask, header, $
  skyimage=skyimage, wset=wset, telluric_spec=telluric_spec, $
  telluric_head=telluric_head, datapath=datapath, gzip=gzip

    if n_elements(specname) eq 0L then begin
       print, 'Syntax - wrt2dspec, specname, image, sigmamap, mask, $'
       print, '   header, skyimage=, wset=, telluric_spec=, telluric_head=, $'
       print, '   datapath=, /gzip'
       return
    endif
    
    if (n_elements(datapath) eq 0L) then datapath = cwd()

    specname = repstr(specname,'.gz','')
    
    mwrfits, image, datapath+specname, header, /create
    mwrfits, sigmamap, datapath+specname
    mwrfits, mask, datapath+specname

    if (n_elements(skyimage) ne 0L) then mwrfits, skyimage, datapath+specname
    if (n_elements(wset) ne 0L) then mwrfits, wset, datapath+specname
    if (n_elements(telluric_spec) ne 0L) and (n_elements(telluric_head) ne 0L) then begin
       mwrfits, telluric_spec, datapath+specname, telluric_head
    endif

    if keyword_set(gzip) then spawn, ['gzip -f '+datapath+specname], /sh
    
return
end
