def Riffle(filenames,vb=False):

   if vb: print "Looking at",len(filenames),"files: ",filenames

   # Break down file names. Naming convention: fruit_flavor.fits
   fruits = []
   flavors = []
   for filename in set(filenames):
      pieces = string.split(filename,'_')
      fruits.append(string.join(pieces[0:-1],'_'))
      flavors.append(string.split(pieces[-1],'.')[0])

   if len(set(flavors)) > 2:
       raise ValueError("ERROR: expecting 1 or 2 flavors of datafile, got more")
   elif len(set(flavors)) == 0:
       raise ValueError("ERROR: expecting 1 or 2 flavors of datafile, got none")

   if 'sci' not in set(flavors):
       raise ValueError("ERROR: expecting at least some files to be xxx_sci.fits")

   if len(set(flavors)) == 1:
       whttype = 'No-one_will_ever_choose_this_flavor'   
   else:
       for x in (set(flavors) - set(['sci'])):
           whttype = x

   number = len(set(fruits))
   scifiles = []
   whtfiles = []
   for fruit in set(fruits):
      x = fruit+'_sci.fits'
      if os.path.exists(x):
         scifiles.append(x)
      else:
         scifiles.append(None)
      x = fruit+'_'+whttype+'.fits'
      if os.path.exists(x):
         whtfiles.append(x)
      else:
         whtfiles.append(None)

   if vb:
      print "Riffled files into",number,"pair(s)"
      if len(set(flavors)) == 1:     
          print "Only 1 flavor of file found, sci"
      else:
          print "2 flavors of file found: sci and",whttype
      for i in range(number):
         print "   ",i+1,"th pair:",[scifiles[i],whtfiles[i]]

   return scifiles,whtfiles