def sri_to_hdr(sri, data_type, data_format):
    """
    Generates an X-Midas header file from the SRI information.
    
    Inputs:
        <sri>          The BULKIO.StreamSRI object
        <data_type>    The X-Midas file type (1000, 2000, etc)
        <data_format>  The X-Midas data format (SD, SF, CF, etc)
    
    Output:
        Returns an X-Midas header file 
    """
    kwds = {}
    
    kwds['xstart'] = sri.xstart
    kwds['xdelta'] = sri.xdelta
    kwds['xunits'] = sri.xunits
    
    kwds['ystart'] = sri.ystart
    kwds['ydelta'] = sri.ydelta
    kwds['yunits'] = sri.yunits
    
    kwds['format'] = data_format
    kwds['type'] = data_type
    
    ext_hdr = sri.keywords
    if len(ext_hdr) > 0:
        items = []
        for item in ext_hdr:
            items.append((item.id, item.value.value()))
        
        kwds['ext_header'] = items
        
    return bluefile.header(**kwds)
def sri_to_hdr(sri, data_type, data_format):
    """
    Generates an X-Midas header file from the SRI information.
    
    Inputs:
        <sri>          The BULKIO.StreamSRI object
        <data_type>    The X-Midas file type (1000, 2000, etc)
        <data_format>  The X-Midas data format (SD, SF, CF, etc)
    
    Output:
        Returns an X-Midas header file 
    """
    kwds = {}

    kwds['xstart'] = sri.xstart
    kwds['xdelta'] = sri.xdelta
    kwds['xunits'] = sri.xunits

    kwds['ystart'] = sri.ystart
    kwds['ydelta'] = sri.ydelta
    kwds['yunits'] = sri.yunits

    kwds['format'] = data_format
    kwds['type'] = data_type

    ext_hdr = sri.keywords
    if len(ext_hdr) > 0:
        items = []
        for item in ext_hdr:
            items.append((item.id, item.value.value()))

        kwds['ext_header'] = items

    return bluefile.header(**kwds)
def make_midas_header(xfile, yfile, file_format):
    hdr = bluefile.header(type=2000, format=file_format, subsize=xfile)
    #hdr, data = bluefile.read('mydata_SL_500_1000.tmp')
    hdr['xstart'] = 0
    hdr['ystart'] = 0
    hdr['xdelta'] = 1
    hdr['ydelta'] = 1
    #hdr['subsize']=xfile
    #hdr['size']=yfile
    # hdr['Version']="BLUE"
    # hdr['head_rep']="EEEI"
    # hdr['data_rep']="EEEI"
    return hdr
def spectrogram(samprate, data, fftsize=2048, log_scale=True, overlap=0.0, removeDC=False):
    epsilon = 1e-20
    nsamp = len(data)
    first_samp = 0
    last_samp = fftsize
    step = int(fftsize*(1.0-overlap))
    
    samp_period = 1.0 / samprate
    freqs = fftfreq(fftsize, samp_period)
    hdr = bluefile.header(type=2000, format='SF')
    hdr['xstart'] = freqs[0]
    hdr['xdelta'] = freqs[1]-freqs[0]
    hdr['ystart'] = 0.0
    hdr['ydelta'] = step*samp_period
    hdr['subsize'] = fftsize/2
    
    frames = []
    while last_samp < nsamp:
        chunk = data[first_samp:last_samp]
        
        if removeDC:
            dc = float(sum(chunk))/len(chunk)
            chunk = [x - dc for x in chunk]
        
        if len(chunk) < fftsize:
            chunk.extend([0.0]*(fftsize-len(chunk)))
        chunk = fft(chunk)[:fftsize/2]
        
        if log_scale:
            try:
                frame = array.array('f', [10*math.log((x*x.conj()).real+epsilon) for x in chunk])
            except:
                print x, x*x.conj()
                raise
        else:
            frame = array.array('f', [(x*x.conj()).real for x in chunk])
        
        frames.append(frame)

        first_samp += step
        last_samp = first_samp + fftsize
    
    print len(frames), len(frames[0]), len(frames[-1])
    return hdr, frames