docs: def stack(outfile,infiles,verbose=0):
      """
      Stick infiles into outfiles as FITS extensions.
  
      outfile willl contain an MEF format file of the single extension FITS
      files named in the infiles array
      """
      
      import os, sys, string, tempfile, shutil
      import pyfits, re, time
  
  ### if there is a pre-existing MEF file for output then append to it
  ### otherwise we need to create a PrimaryHDU
      if os.access(outfile,os.R_OK)!=1:
          if verbose:
              print "Creating new MEF file: ",outfile
          outfile=create_mef(outfile)
          
  ### get a handle for the output image, _open is the local variant of
  ### pyfits.open and just does some error recovery if pyfits.open raises an
  ### exception.
      out = pyfits.open(outfile,'append')
      hdr = out[0].header
      count=0
  
  ### append the fits files given on the command line to the
  ### output file.
      det_xmin=None
      det_xmax=None
      det_ymin=None
      det_ymax=None
      for infile in infiles:
          if verbose:
              print "Adding ",infile," to ",outfile
  
          ### _open  tries to handle bad fits format exceptions.
          file=_open(infile)
          if not file:
              raise IOError("Cann't get the HDU for "+infile)
  
          for hdu in file:
              extname=None
              if hdu.header.has_key('EXTNAME') :
                  extname=hdu.header['EXTNAME']
              elif hdu.header.has_key('EXTVER') :
                  extname="ccd"+string.zfill(hdu.header.has_key('EXTVER'),2)
                      
              if hdu.header.has_key('EPOCH'):
                  if hdu.header.has_key('EQUINOX'):
                      del hdu.header['EPOCH']
                  else:
                      hdu.header.update('EQUINOX',hdu.header['EQUINOX'].value,
                                        comment=hdu.header['EQUINOX'].comment)
  
              ahdu=pyfits.ImageHDU(data=hdu.data, header=hdu.header, name=extname)
              out.append(ahdu)
  
          ### build the size of the overall detector
              if hdu.header.has_key('DETSEC'):
                  values=re.findall(r'(\d+)',
                                    hdu.header['DETSEC'])
                  if len(values)==4:
                      xmin=int(values[0])
                      xmax=int(values[1])
                      ymin=int(values[2])
                      ymax=int(values[3])
  
                  if xmin>xmax:
                      t=xmin
                      xmin=xmax
                      xmax=t
                  if ymin>ymax:
                      t=ymin
                      ymin=ymax
                      ymax=t
  
                  if xmin<det_xmin or not det_xmin:
                      det_xmin=xmin
                  if xmax>det_xmax or not det_xmax:
                      det_xmax=xmax
                  if ymin<det_ymin or not det_ymin:
                      det_ymin=ymin
                  if ymax>det_ymax or not det_ymax:
                      det_ymax=ymax
      
  
          file.close()
  
  
      detsize='['+str(det_xmin)+':'+str(det_xmax)+','+str(det_ymin)+':'+str(det_ymax)+']'
      out[0].header.update('DETSIZE',detsize,comment='Size of Mosaic')
      out.close()    
      if verbose:
          print "Done building MEF: ",outfile
      return 0