Infers header from a CSV or other tab-delimited file.
    
    This is essentially small extension of the csv.Sniffer.has_header algorithm.
    provided in the Python csv module.   First, it checks to see whether a 
    metametadata dictionary is present, specifiying the lines numbers of 
    metadata lines in the header, and if so, sets the header lines to include
    at least those lines.  Then iookms to see if a comments character is 
    present, and if so, includes those lines as well.  If either of the above 
    returns a nono-zero number of headerlines, the function returns that 
    number; otherwise, it uses the csv.Sniffer module, checking each line in 
    succession, and stopping at the first line where the sniffer module finds no 
    evidence of a header, and returning that line numner.
    
    **Parameters** 
    
        **lines** : line of strings 
        
            The list of lines representing lines in the file
            
        **comments** : single-character string, optional
        
            Comments character  specification. 
            
        **metadata** : metadata dictionary, optional
        
            Used to determine a comments character and metametadata dicationary, 
            if present.

    **Returns**
    
        Integer, representing the number of (inferred) header lines at the top 
        of the file.

def inferheader(lines, comments=None, metadata=None,
                verbosity=DEFAULT_VERBOSITY):
    """
    Infers header from a CSV or other tab-delimited file.
    
    This is essentially small extension of the csv.Sniffer.has_header algorithm.
    provided in the Python csv module.   First, it checks to see whether a 
    metametadata dictionary is present, specifiying the lines numbers of 
    metadata lines in the header, and if so, sets the header lines to include
    at least those lines.  Then iookms to see if a comments character is 
    present, and if so, includes those lines as well.  If either of the above 
    returns a nono-zero number of headerlines, the function returns that 
    number; otherwise, it uses the csv.Sniffer module, checking each line in 
    succession, and stopping at the first line where the sniffer module finds no 
    evidence of a header, and returning that line numner.
    
    **Parameters** 
    
        **lines** : line of strings 
        
            The list of lines representing lines in the file
            
        **comments** : single-character string, optional
        
            Comments character  specification. 
            
        **metadata** : metadata dictionary, optional
        
            Used to determine a comments character and metametadata dicationary, 
            if present.

    **Returns**
    
        Integer, representing the number of (inferred) header lines at the top 
        of the file.
    
    """
     
    if ((comments is None) and metadata and ('comments' in metadata.keys())):
        comments = metadata['comments']
    if (comments is None):
        comments = '#'  

    if ('metametadata' in metadata.keys()):
        mmd = metadata['metametadata']
        cc = 1 + max([v if isinstance(v, int) else max(v) 
                      for v in mmd.values()])
    else:
        cc = 0
        
    if (comments != ''):
        if (cc < len(lines)):
            for l in xrange(cc,len(lines)):
                if not lines[l].startswith(comments):
                    break
        else:
            l = cc

    if (l > 0):
        return l
    else:
        for j in xrange(min(1000, len(lines))):
            hasheader = 'unset'
            for k in [100, 200, 400, 800, 1600]:
                F = '\n'.join(lines[j:(j+k)])
                try:
                    hasheader = csv.Sniffer().has_header(F)
                except:
                    pass
                else:
                    break
            if not hasheader:
                return j