pro mkcds, infile, sca_type=sca_type, plot=plot, norefsub=norefsub, verbose=verbose

       ; NAME:
       ;       mkcds.pro
       ;
       ; PURPOSE: This program calibrates CDS data cubes.  Calibration
       ;       includes collapsing the CDS cube and performing
       ;       reference pixel correction. If 'Generic' SCA type is
       ;       selected, the reference pixel correction is not done.
       ;
       ; CALLING SEQUENCE:
       ;       mkcds, input_filename
       ;
       ; INPUTS:
       ;       input_filename - A CDS FITS file
       ; 
       ;
       ; KEYWORD PARAMETERS:
       ;       sca_type - {H1R|H1RG|H2RG|SB290|SB304|Generic}
       ;       plot     - Show plots while doing refsub
       ;       norefsub - Do not perform reference pixel correction
       ;       verbose  - Show verbose output
       ;
       ;
       ;
       ; EXAMPLE
       ;       mkcds, 'input.fits'
       ; 
       ;       This creates a new file called 'input.cal.fits'
       ;
       ;
       ; REFERENCE:
       ;
       ;
       ; MODIFICATION HISTORY:
       ;       Written by:  B.J. Rauscher, IDTL, May, 2002
       ;-

       ;; Set command line defaults
       if (not keyword_set(plot)) then plot = 0; Default is not plots during refsub
       if (not keyword_set(norefsub)) then norefsub = 0   ; Default is to do refsub
       if (not keyword_set(verbose)) then verbose = 0     ; Default is quiet

       ;; Read in FITS data. This comes before getting the sca type
       ;; because we need the header to get the SCA type
       fits_read, infile, data, header

       ;; Get sca type.
       ;;
       ;; The first place to check is the command line. In this case,
       ;; the sca type is already known.
       if (not keyword_set(sca_type)) then begin

           ;; If sca_type is not specified on the command line, try to find it in the header
           sca_type = sxpar(header, 'DETNAME', count=count)
           if (count eq 1) then begin

               sca_type = strmid(sca_type, 0, strpos(sca_type, '-'))

           endif else begin

               ;; sca type is not specified in the header or command line, type = Generic
               sca_type = 'Generic'

           endelse
       endif

       ;; Check sca type against list of known types. Use generic in
       ;; case of no match
       case sca_type of

           'H1R': 
           'H1RG': 
           'H1RG_STAMP':
           'H2RG': 
           'SB290': 
           'SB301': 
           'Generic':

           else: stop, 'ERROR: sca type not recognized'

       endcase

       ;; Advise user of SCA type
       if (verbose) then print, 'Using sca_type = '+sca_type

       ;; Make the output file name.
       _i = RSTRPOS(infile, '.fits')
       outfile = strmid(infile, 0, _i)+'.cal.fits'

       ;; Calculate CDS difference and write result to the output file
       if (verbose) then print, 'Subtracting CDS samples'
       data = data[*,*,1] - data[*,*,0]
       fits_write, outfile, data, header

       ;; Perform reference pixel correction
       if (strpos(sca_type, 'Generic') eq -1) then begin

           if (not norefsub) then begin

               if (verbose) then print, 'Doing reference pixel correction'

               refsub, outfile, sca_type, plot=plot

               ;; Refsub writes its output to a file with the suffix '.rc.fits'. Rename
               ;; this to be the new output file.
               _i = RSTRPOS(infile, '.fits')
               refsub_outfile = strmid(infile, 0, _i)+'.cal.rc.fits'
               spawn, 'mv -f '+refsub_outfile+' '+outfile

           endif

       endif

end
