예제 #1
0
def clean_trace(data,
                samplerate,
                trace_start,
                trace_end,
                need_start=False,
                need_end=False):
    '''
    Apply time stamps to our values and cut extra data.
    Return clean list of touples.
    '''

    new_data = []
    period = 1 / samplerate

    if not need_start: need_start = trace_start
    if not need_end: need_end = trace_end

    logging.debug("clean_trace samplerate:[%s]" % (samplerate))
    logging.debug("clean_trace points:[%s]" % (len(data)))
    logging.debug("actual[%s,%s]" % (trace_start, trace_end))
    logging.debug("needed[%s,%s]" % (need_start, need_end))

    for d in data:
        if trace_start >= need_start and trace_start <= need_end:
            new_data.append(d)
        trace_start += period

    logging.debug('New trace is: %s' % len(new_data))

    return new_data
예제 #2
0
def makeHelm(traces,
             append='',
             outputfile='',
             perline=7,
             total=14,
             decimal=5,
             folder='.dbmoment'):
    """
    Routine to write Helmberger Format Seismograms
    """
    logging.debug('makeHelm')

    filename = {}
    #global tmp_folder

    if not outputfile:
        (f, outputfile) = mkstemp(suffix='.Helm_data',
                                  prefix='dbmoment_%s_' % append,
                                  dir=folder,
                                  text=True)
        f = os.fdopen(f, 'w')
    else:
        try:
            f = open(outputfile, 'w')
        except Exception, e:
            self.logging.error('Cannot open new file %s %s' % (outputfile, e))
예제 #3
0
def verify_trace_time(start, end, time, endtime):
    '''
    Verify that we have the requested data
    '''
    logging.debug("Requested time: [%s,%s]" %
                  (stock.strtime(start), stock.strtime(end)))
    logging.debug("In trace object: [%s,%s]" %
                  (stock.strtime(time), stock.strtime(endtime)))

    tw = end - start

    if endtime - time < tw:
        logging.warning('Got %s secs but expected [%s].' %
                        ((endtime - time), tw))
        return False

    if start < time:
        logging.warning('Trace starts [%s] seconds late.' % (time - start))
        return False

    if endtime < end:
        logging.warning('Trace ends [%s] seconds early.' % (end - endtime))
        return False

    return True
예제 #4
0
def clean_trace(data, samplerate, trace_start, trace_end, need_start=False, need_end=False):
    '''
    Apply time stamps to our values and cut extra data.
    Return clean list of touples.
    '''


    new_data = []
    period = 1/samplerate

    if not need_start: need_start = trace_start
    if not need_end: need_end = trace_end

    logging.debug( "clean_trace samplerate:[%s]" % (samplerate) )
    logging.debug( "clean_trace points:[%s]" % (len(data)) )
    logging.debug( "actual[%s,%s]" % (trace_start, trace_end) )
    logging.debug( "needed[%s,%s]" % (need_start, need_end) )

    for d in data:
        if trace_start >= need_start and trace_start <= need_end:
            new_data.append(d)
        trace_start += period

    logging.debug('New trace is: %s' % len(new_data) )

    return new_data
예제 #5
0
def dynamic_loader(module):
    '''
    Load some libs defined on the pf file.
    '''
    logging.debug( "load dbmoment.%s" % module )
    try:
        return __import__("dbmoment.%s" % module, globals(), locals(), [module], -1)
    except Exception,e:
        logging.error("Import Error: [%s] => [%s]" % (module,e) )
예제 #6
0
def dynamic_loader(module):
    '''
    Load some libs defined on the pf file.
    '''
    logging.debug("load dbmoment.%s" % module)
    try:
        return __import__("dbmoment.%s" % module, globals(), locals(),
                          [module], -1)
    except Exception, e:
        logging.error("Import Error: [%s] => [%s]" % (module, e))
예제 #7
0
def makeHelm(traces, append='', outputfile='', perline=7, total=14, decimal=5, folder='.dbmoment'):
    """
    Routine to write Helmberger Format Seismograms
    """
    logging.debug('makeHelm')

    filename = {}
    #global tmp_folder

    if not outputfile:
        (f, outputfile) = mkstemp(suffix='.Helm_data', prefix='dbmoment_%s_' % append,
                                dir=folder, text=True)
        f = os.fdopen(f, 'w')
    else:
        try:
            f = open(outputfile, 'w')
        except Exception,e:
            self.logging.error('Cannot open new file %s %s'% (outputfile,e))
예제 #8
0
def open_verify_pf(pf, mttime=False):
    '''
    Verify that we can get the file and check
    the value of PF_MTTIME if needed.
    Returns pf_object
    '''

    logging.debug('Look for parameter file: %s' % pf)

    if mttime:
        logging.debug('Verify that %s is newer than %s' % (pf, mttime))

        PF_STATUS = stock.pfrequire(pf, mttime)
        if PF_STATUS == stock.PF_MTIME_NOT_FOUND:
            logging.warning('Problems looking for %s. PF_MTTIME_NOT_FOUND.' %
                            pf)
            logging.error(
                'No MTTIME in PF file. Need a new version of the %s file!!!' %
                pf)
        elif PF_STATUS == stock.PF_MTIME_OLD:
            logging.warning('Problems looking for %s. PF_MTTIME_OLD.' % pf)
            logging.error('Need a new version of the %s file!!!' % pf)
        elif PF_STATUS == stock.PF_SYNTAX_ERROR:
            logging.warning('Problems looking for %s. PF_SYNTAX_ERROR.' % pf)
            logging.error('Need a working version of the %s file!!!' % pf)
        elif PF_STATUS == stock.PF_NOT_FOUND:
            logging.warning('Problems looking for %s. PF_NOT_FOUND.' % pf)
            logging.error('No file  %s found!!!' % pf)

        logging.debug('%s => PF_MTIME_OK' % pf)

    try:
        return stock.pfread(pf)
    except Exception, e:
        logging.error('Problem looking for %s => %s' % (pf, e))
예제 #9
0
def open_verify_pf(pf,mttime=False):
    '''
    Verify that we can get the file and check
    the value of PF_MTTIME if needed.
    Returns pf_object
    '''

    logging.debug( 'Look for parameter file: %s' % pf )

    if mttime:
        logging.debug( 'Verify that %s is newer than %s' % (pf,mttime) )

        PF_STATUS = stock.pfrequire(pf, mttime)
        if PF_STATUS == stock.PF_MTIME_NOT_FOUND:
            logging.warning( 'Problems looking for %s. PF_MTTIME_NOT_FOUND.' % pf )
            logging.error( 'No MTTIME in PF file. Need a new version of the %s file!!!' % pf )
        elif PF_STATUS == stock.PF_MTIME_OLD:
            logging.warning( 'Problems looking for %s. PF_MTTIME_OLD.' % pf )
            logging.error( 'Need a new version of the %s file!!!' % pf )
        elif PF_STATUS == stock.PF_SYNTAX_ERROR:
            logging.warning( 'Problems looking for %s. PF_SYNTAX_ERROR.' % pf )
            logging.error( 'Need a working version of the %s file!!!' % pf )
        elif PF_STATUS == stock.PF_NOT_FOUND:
            logging.warning( 'Problems looking for %s. PF_NOT_FOUND.' % pf )
            logging.error( 'No file  %s found!!!' % pf )

        logging.debug( '%s => PF_MTIME_OK' % pf )

    try:
        return stock.pfread( pf )
    except Exception,e:
        logging.error( 'Problem looking for %s => %s' % ( pf, e ) )
예제 #10
0
def verify_trace_time(start, end, time, endtime):
    '''
    Verify that we have the requested data
    '''
    logging.debug( "Requested time: [%s,%s]" % (stock.strtime(start),stock.strtime(end)) )
    logging.debug( "In trace object: [%s,%s]" % (stock.strtime(time),stock.strtime(endtime)) )

    tw = end - start

    if endtime - time < tw:
        logging.warning('Got %s secs but expected [%s].' % ( (endtime-time), tw) )
        return False

    if start < time:
        logging.warning('Trace starts [%s] seconds late.' % ( time - start ) )
        return False

    if endtime < end:
        logging.warning('Trace ends [%s] seconds early.' % ( end - endtime ) )
        return False

    return True
예제 #11
0
def plot_results(id,
                 stations,
                 results,
                 event,
                 folder='./',
                 acknowledgement='dbmoment'):

    from __main__ import logging

    total = 1

    #N = len(stations.keys())
    total_stations = len(stations.keys()) + 2
    gcf = pyplot.gcf()
    #fig = pyplot.figure(figsize=( 20, (3*N) ))
    fig = pyplot.figure(figsize=(20, 3 * total_stations))

    max_all = []
    min_all = []
    points_all = []

    #for sta in sorted(stations.keys()):
    for sta in sorted(stations.keys(), key=lambda x: stations[x].realdistance):
        axs = stations[sta].max_min_all()
        points_all.append(axs[1])
        min_all.append(axs[2])
        max_all.append(axs[3])

    max_plot = max(max_all)
    min_plot = min(min_all)
    points_plot = max(points_all)

    # First panel
    text = "%s \n" % event.strtime
    text += "\n"
    text += "ID: %s   %s\n" % (id, results['estatus'])
    text += "\n"
    text += "Location: \n"
    text += "   Lat:    %s \n" % event.lat
    text += "   Lon:    %s \n" % event.lon
    text += "   Depth:  %s km\n" % event.depth
    text += "Filter:    %s \n" % event.filter
    text += "Model:     %s" % event.model
    ax = fig.add_subplot(total_stations, 3, total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate(text, (0, 1),
                xycoords="axes fraction",
                va="top",
                ha="left",
                fontsize=12,
                bbox=dict(edgecolor='none', boxstyle="round, pad=2", fc="w"))
    total += 1

    # Second panel
    text = "Mw:       %s \n" % results['Mw']
    text += "Strike:%s Rake:%s Dip:%s\n" % \
            ( results['Strike'], results['Rake'], results['Dip'] )
    text += "Pdc:      %0d %%\n" % (results['Pdc'] * 100)
    text += "Pclvd:    %0d %%\n" % (results['Pclvd'] * 100)
    text += "VAR:   %s \n" % results['Variance']
    text += "VarRed:     %s \n" % results['VarRed']
    text += "Var/Pdc:    %s \n" % results['Var/Pdc']
    text += "Mo:         %s \n" % results['Mo']
    text += "Mxx:%0.3f  Mxy:%0.3f  Mxz:%0.3f\n" % (float(
        results['Mxx']), float(results['Mxy']), float(results['Mxz']))
    text += "Myy:%0.3f  Myz:%0.3f  Mzz:%0.3f\n" % (float(
        results['Myy']), float(results['Myz']), float(results['Mzz']))
    ax = fig.add_subplot(total_stations, 3, total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate(text, (0, 1),
                xycoords="axes fraction",
                va="top",
                ha="left",
                fontsize=12,
                bbox=dict(edgecolor='none', boxstyle="round, pad=2", fc="w"))
    total += 1

    # Only run if library ObsPy is present on system.
    if beachball:
        # Third panel for beachball
        ax = fig.add_subplot(total_stations, 3, total, frameon=False)
        ax.xaxis.set_visible(False)
        ax.yaxis.set_visible(False)
        ax.patch.set_alpha(0.0)

        ax.annotate('ObsPy used for beachball image', (0.5, 0),
                    xycoords="axes fraction",
                    va="top",
                    ha="left",
                    fontsize=7,
                    bbox=dict(edgecolor='none',
                              boxstyle="round, pad=2",
                              fc="w"))

        # TEST FOR COMPARING 3 and 6 components methods
        #mt = [ float(results['Mxx']), float(results['Myy']), float(results['Mzz']),
        #    float(results['Mxy']), float(results['Mxz']), float(results['Myz']) ]
        #mt2 = [ results['Strike'][0], results['Dip'][0], results['Rake'][0], ]
        #bb = beachball(mt, mopad_basis='NED', xy=(-50, -50),width=80)
        #bb2 = beachball(mt2, xy=(50,50), width=80)
        #ax.add_collection( bb )
        #ax.add_collection( bb2 )

        mt = [
            float(results['Mxx']),
            float(results['Myy']),
            float(results['Mzz']),
            float(results['Mxy']),
            float(results['Mxz']),
            float(results['Myz'])
        ]
        bb = beachball(mt, mopad_basis='NED')
        ax.add_collection(bb)
        ax.set_xlim((-110, 110))
        ax.set_ylim((-110, 110))
        ax.set_aspect("equal")

    total += 1

    #for sta in sorted(stations.keys()):
    for sta in sorted(stations.keys(),
                      key=lambda x: float(stations[x].realdistance)):

        logging.debug('Plot traces for results on %s' % sta)
        if not stations[sta].real:
            logging.error('Empty Records for data on %s' % sta)
        if not stations[sta].synth_zrt:
            logging.error('Empty Records for converted ZRT on %s' % sta)

        distance = int(float(stations[sta].realdistance))
        azimuth = int(float(stations[sta].azimuth))
        #real = stations[sta].real
        convertedsynth = stations[sta].synth_zrt
        try:
            zcor = results['zcor'][sta]
        except:
            zcor = '-'
        variance = round(results['variance'][sta], 1)

        # Scale all traces the same way
        #axs = stations[sta].max_min_all()
        axs = (0, points_plot, min_plot, max_plot)

        #for chan, data in real:
        for chan in ['T', 'R', 'Z']:
            ax = fig.add_subplot(total_stations, 3, total)
            #real_line, = pyplot.plot(data, label='data' )
            real_line, = pyplot.plot(stations[sta].real.get(chan),
                                     label='data')
            synth_line, = pyplot.plot(stations[sta].synth_zrt.get(chan),
                                      linestyle='--',
                                      label='synth')

            pyplot.legend(loc=5, fontsize=8)

            pyplot.axis(axs)
            pyplot.ylabel('centimeters', fontsize=8)
            pyplot.yticks(size=8)

            ax.get_xaxis().set_ticks([])
            #ax.get_yaxis().set_ticks([])
            ax.yaxis.set_major_formatter(pyplot.FormatStrFormatter('%.1e'))

            # Top of plot
            pyplot.text(0.5,
                        0.9,
                        r'$\mathbf{%s}\ -\ \mathbf{%s}\ \ %s_{km}\ \ %s ^o$' %
                        (sta, chan, distance, azimuth),
                        horizontalalignment='center',
                        fontsize=13,
                        verticalalignment='center',
                        transform=ax.transAxes)

            # Bottom of plot
            text = "zcor:%s   variance_reduction:%s%%" % (zcor, variance)
            pyplot.text(0.5,
                        0.1,
                        text,
                        horizontalalignment='center',
                        fontsize=11,
                        verticalalignment='center',
                        transform=ax.transAxes)

            pyplot.draw()
            total += 1

    title = "%s Mw    " % results['Mw']
    title += "%s   " % event.strtime
    pyplot.suptitle(title, fontsize=18, ha='center')

    text = "%s \n" % event.strtime

    # Acknowledgement panel
    ax = fig.add_subplot(total_stations, 3, total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate(unicode(acknowledgement, "utf-8"), (0, 0),
                xycoords="axes fraction",
                va="bottom",
                ha="left",
                fontsize=8,
                bbox=dict(edgecolor='gray', boxstyle="round, pad=2", fc="w"))

    # Extra info panel
    total += 1
    text = "%s\n" % ' '.join(sys.argv)
    #text += "%s\n" % os.environ['ANTELOPE']
    text += "Generated at %s" % stock.strtime(stock.now())

    ax = fig.add_subplot(total_stations, 3, total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate(unicode(text, "utf-8"), (0, 0),
                xycoords="axes fraction",
                va="bottom",
                ha="left",
                fontsize=8,
                bbox=dict(edgecolor='gray', boxstyle="round, pad=2", fc="w"))

    try:
        if not os.path.isdir(folder): os.makedirs(folder)
    except Exception, e:
        logging.error("Problems while creating folder [%s] %s" % (folder, e))
예제 #12
0
def readHelm(inputfile):
    """
    Routine to read Helmberger Format Seismograms
    Returning full object:
        traces[chan]['data'] = data
        return traces

    """
    logging.debug('readHelm: %s' % inputfile)
    results = {}

    fo = open(inputfile, "r")

    # Number of channels
    try:
        total_chans = int(fo.readline().strip())
        logging.debug("Total channels [%s]" % total_chans)
    except:
        logging.error("NOT VALID FILE [%s]" % inputfile)

    if total_chans == 1:
        channels = 'X'
    if total_chans == 3:
        # From dbmoment.xpy code. Also in data.py
        global seismic_channels
        channels = seismic_channels
    else:
        # From dbmoment.xpy code.
        global synth_channels
        channels = synth_channels

    # Data Format
    try:
        data_format = fo.readline().strip()
        logging.debug('file format: %s' % data_format)
        temp = re.match("\((\d+)e(\d+)\.(\d+)\)", data_format)
        perline = int(temp.group(1))
        spaces = int(temp.group(2))
        logging.debug('perline: %s  spaces: %s' % (perline, spaces))

    except:
        logging.error("NOT VALID FILE [%s]" % inputfile)

    for chan in range(total_chans):
        channame = channels[chan]
        logging.debug("chan %s" % channame)
        try:
            header1 = fo.readline().strip().split()
            header2 = fo.readline().strip().split()
            total_points = int(header2[0])
            samplerate = 1 / float(header2[1])
        except:
            logging.error("NOT VALID FILE [%s]" % inputfile)

        if not total_points or not samplerate:
            logging.error("NOT VALID FILE [%s]" % inputfile)

        cache = []
        logging.debug("Total points [%s]" % total_points)
        logging.debug("Sampelrate [%s]" % samplerate)

        while (total_points > 0):
            row = fo.readline().strip('\n')
            #logging.info('row: %s' % row)
            #logging.info('missing: %s' % total_points)
            if not row: logging.error('Problems readHelm(%s)' % inputfile)

            while (len(row) > 0):
                point = float(row[0:spaces])
                row = row[spaces:]
                #logging.info('%s' % point)
                cache.append(point)
                total_points -= 1

            #row = [float(row[i:i+spaces]) for i in range(0, perline)]
            #cache.extend( row )
            #total_points -= len( row )

        cache = array(cache)
        if total_chans == 1: return cache

        if not results:
            # need new object
            results = Records(samplerate)
            results.file = inputfile

        results.trace(channame, cache)

    return results
예제 #13
0

def safe_pf_get(pf, field, defaultval=False):
    '''
    Safe method to extract values from parameter file
    with a default value option.
    '''
    value = defaultval
    if pf.has_key(field):
        try:
            value = pf.get(field, defaultval)
        except Exception, e:
            logging.warning('Problems safe_pf_get(%s,%s)' % (field, e))
            pass

    logging.debug("pf.get(%s,%s) => %s" % (field, defaultval, value))

    return value


def clean_trace(data,
                samplerate,
                trace_start,
                trace_end,
                need_start=False,
                need_end=False):
    '''
    Apply time stamps to our values and cut extra data.
    Return clean list of touples.
    '''
예제 #14
0
def plot_results( id, stations, results, event, folder='./',
                       acknowledgement='dbmoment'):

    from __main__ import logging

    total = 1


    #N = len(stations.keys())
    total_stations = len(stations.keys()) + 2
    gcf = pyplot.gcf()
    #fig = pyplot.figure(figsize=( 20, (3*N) ))
    fig = pyplot.figure(figsize=( 20, 3 * total_stations ))

    max_all = []
    min_all = []
    points_all = []

    #for sta in sorted(stations.keys()):
    for sta in sorted(stations.keys(), key=lambda x: stations[x].realdistance):
        axs = stations[sta].max_min_all()
        points_all.append( axs[1] )
        min_all.append( axs[2] )
        max_all.append( axs[3] )

    max_plot = max(max_all)
    min_plot =  min(min_all)
    points_plot = max(points_all)



    # First panel
    text = "%s \n" % event.strtime
    text += "\n"
    text += "ID: %s   %s\n" % (id, results['estatus'] )
    text += "\n"
    text += "Location: \n"
    text += "   Lat:    %s \n" % event.lat
    text += "   Lon:    %s \n" % event.lon
    text += "   Depth:  %s km\n" % event.depth
    text += "Filter:    %s \n" % event.filter
    text += "Model:     %s" % event.model
    ax = fig.add_subplot(total_stations,3,total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate( text, (0, 1), xycoords="axes fraction", va="top", ha="left",
                 fontsize=12, bbox=dict(edgecolor='none',boxstyle="round, pad=2", fc="w"))
    total += 1

    # Second panel
    text = "Mw:       %s \n" % results['Mw']
    text += "Strike:%s Rake:%s Dip:%s\n" % \
            ( results['Strike'], results['Rake'], results['Dip'] )
    text += "Pdc:      %0d %%\n" % (results['Pdc'] * 100)
    text += "Pclvd:    %0d %%\n" % (results['Pclvd'] * 100)
    text += "VAR:   %s \n" % results['Variance']
    text += "VarRed:     %s \n" % results['VarRed']
    text += "Var/Pdc:    %s \n" % results['Var/Pdc']
    text += "Mo:         %s \n" % results['Mo']
    text += "Mxx:%0.3f  Mxy:%0.3f  Mxz:%0.3f\n" % (float(results['Mxx']), float(results['Mxy']), float(results['Mxz']))
    text += "Myy:%0.3f  Myz:%0.3f  Mzz:%0.3f\n" % (float(results['Myy']), float(results['Myz']), float(results['Mzz']))
    ax = fig.add_subplot(total_stations,3,total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate( text, (0, 1), xycoords="axes fraction", va="top", ha="left",
                 fontsize=12, bbox=dict(edgecolor='none',boxstyle="round, pad=2", fc="w"))
    total += 1

    # Only run if library ObsPy is present on system.
    if beachball:
        # Third panel for beachball
        ax = fig.add_subplot(total_stations,3,total, frameon=False)
        ax.xaxis.set_visible(False)
        ax.yaxis.set_visible(False)
        ax.patch.set_alpha(0.0)

        ax.annotate( 'ObsPy used for beachball image', (0.5, 0), xycoords="axes fraction", va="top", ha="left",
                     fontsize=7, bbox=dict(edgecolor='none',boxstyle="round, pad=2", fc="w"))

        # TEST FOR COMPARING 3 and 6 components methods
        #mt = [ float(results['Mxx']), float(results['Myy']), float(results['Mzz']),
        #    float(results['Mxy']), float(results['Mxz']), float(results['Myz']) ]
        #mt2 = [ results['Strike'][0], results['Dip'][0], results['Rake'][0], ]
        #bb = beachball(mt, mopad_basis='NED', xy=(-50, -50),width=80)
        #bb2 = beachball(mt2, xy=(50,50), width=80)
        #ax.add_collection( bb )
        #ax.add_collection( bb2 )

        mt = [ float(results['Mxx']), float(results['Myy']), float(results['Mzz']),
            float(results['Mxy']), float(results['Mxz']), float(results['Myz']) ]
        bb = beachball(mt, mopad_basis='NED')
        ax.add_collection( bb )
        ax.set_xlim((-110, 110))
        ax.set_ylim((-110, 110))
        ax.set_aspect("equal")

    total += 1

    #for sta in sorted(stations.keys()):
    for sta in sorted(stations.keys(), key=lambda x: float(stations[x].realdistance) ):

        logging.debug('Plot traces for results on %s' % sta )
        if not stations[sta].real:
            logging.error('Empty Records for data on %s' % sta )
        if not stations[sta].synth_zrt:
            logging.error('Empty Records for converted ZRT on %s' % sta)

        distance = int( float(stations[sta].realdistance) )
        azimuth = int( float(stations[sta].azimuth) )
        #real = stations[sta].real
        convertedsynth = stations[sta].synth_zrt
        try:
            zcor = results['zcor'][sta]
        except:
            zcor = '-'
        variance = round( results['variance'][sta], 1)

        # Scale all traces the same way
        #axs = stations[sta].max_min_all()
        axs = (0, points_plot, min_plot, max_plot)


        #for chan, data in real:
        for chan in ['T', 'R', 'Z']:
            ax = fig.add_subplot(total_stations,3,total)
            #real_line, = pyplot.plot(data, label='data' )
            real_line, = pyplot.plot(stations[sta].real.get(chan), label='data' )
            synth_line, = pyplot.plot(stations[sta].synth_zrt.get(chan), linestyle='--', label='synth')

            pyplot.legend(loc=5, fontsize=8)

            pyplot.axis( axs )
            pyplot.ylabel('centimeters', fontsize=8)
            pyplot.yticks(size=8)

            ax.get_xaxis().set_ticks([])
            #ax.get_yaxis().set_ticks([])
            ax.yaxis.set_major_formatter(pyplot.FormatStrFormatter('%.1e'))

            # Top of plot
            pyplot.text (0.5, 0.9, r'$\mathbf{%s}\ -\ \mathbf{%s}\ \ %s_{km}\ \ %s ^o$' % (sta,chan,distance,azimuth),
                  horizontalalignment='center', fontsize=13,
                  verticalalignment='center', transform = ax.transAxes)

            # Bottom of plot
            text = "zcor:%s   variance_reduction:%s%%" % (zcor, variance)
            pyplot.text (0.5, 0.1,text, horizontalalignment='center', fontsize=11,
                  verticalalignment='center', transform = ax.transAxes)


            pyplot.draw()
            total += 1

    title = "%s Mw    " % results['Mw']
    title += "%s   " % event.strtime
    pyplot.suptitle(title, fontsize=18, ha='center')

    text = "%s \n" % event.strtime

    # Acknowledgement panel
    ax = fig.add_subplot(total_stations,3,total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate( unicode(acknowledgement, "utf-8"), (0, 0), xycoords="axes fraction", va="bottom", ha="left",
                 fontsize=8, bbox=dict(edgecolor='gray',boxstyle="round, pad=2", fc="w"))

    # Extra info panel
    total += 1
    text = "%s\n" % ' '.join( sys.argv )
    #text += "%s\n" % os.environ['ANTELOPE']
    text += "Generated at %s" % stock.strtime( stock.now() )

    ax = fig.add_subplot(total_stations,3,total, frameon=False)
    ax.xaxis.set_visible(False)
    ax.yaxis.set_visible(False)
    ax.patch.set_alpha(0.0)
    ax.annotate( unicode(text, "utf-8"), (0, 0), xycoords="axes fraction", va="bottom", ha="left",
                 fontsize=8, bbox=dict(edgecolor='gray',boxstyle="round, pad=2", fc="w"))

    try:
        if not os.path.isdir(folder): os.makedirs(folder)
    except Exception,e:
        logging.error("Problems while creating folder [%s] %s" % (folder,e))
예제 #15
0
def readHelm(inputfile):
    """
    Routine to read Helmberger Format Seismograms
    Returning full object:
        traces[chan]['data'] = data
        return traces

    """
    logging.debug('readHelm: %s' % inputfile)
    results = {}


    fo = open(inputfile, "r")

    # Number of channels
    try:
        total_chans = int( fo.readline().strip() )
        logging.debug( "Total channels [%s]" % total_chans )
    except:
        logging.error("NOT VALID FILE [%s]" % inputfile)

    if total_chans == 1:
        channels = 'X'
    if total_chans == 3:
        # From dbmoment.xpy code. Also in data.py
        global seismic_channels
        channels = seismic_channels
    else:
        # From dbmoment.xpy code.
        global synth_channels
        channels = synth_channels

    # Data Format
    try:
        data_format = fo.readline().strip()
        logging.debug('file format: %s' % data_format )
        temp = re.match("\((\d+)e(\d+)\.(\d+)\)",data_format)
        perline = int(temp.group(1))
        spaces = int(temp.group(2))
        logging.debug('perline: %s  spaces: %s' % (perline, spaces) )


    except:
        logging.error( "NOT VALID FILE [%s]" % inputfile )

    for chan in range(total_chans):
        channame = channels[chan]
        logging.debug( "chan %s" % channame )
        try:
            header1 = fo.readline().strip().split()
            header2 = fo.readline().strip().split()
            total_points = int( header2[0] )
            samplerate =  1/float( header2[1] )
        except:
            logging.error( "NOT VALID FILE [%s]" % inputfile )

        if not total_points or not samplerate:
            logging.error( "NOT VALID FILE [%s]" % inputfile )

        cache = []
        logging.debug( "Total points [%s]" % total_points )
        logging.debug( "Sampelrate [%s]" % samplerate )

        while (total_points > 0):
            row = fo.readline().strip('\n')
            #logging.info('row: %s' % row)
            #logging.info('missing: %s' % total_points)
            if not row: logging.error( 'Problems readHelm(%s)' % inputfile )

            while ( len(row) > 0 ):
                point = float(row[0:spaces])
                row = row[spaces:]
                #logging.info('%s' % point)
                cache.append( point )
                total_points -= 1

            #row = [float(row[i:i+spaces]) for i in range(0, perline)]
            #cache.extend( row )
            #total_points -= len( row )

        cache = array( cache )
        if total_chans == 1: return cache

        if not results:
            # need new object
            results =  Records(samplerate)
            results.file = inputfile

        results.trace( channame, cache )

    return results
예제 #16
0

def safe_pf_get(pf,field,defaultval=False):
    '''
    Safe method to extract values from parameter file
    with a default value option.
    '''
    value = defaultval
    if pf.has_key(field):
        try:
            value = pf.get(field,defaultval)
        except Exception,e:
            logging.warning('Problems safe_pf_get(%s,%s)' % (field,e))
            pass

    logging.debug( "pf.get(%s,%s) => %s" % (field,defaultval,value) )

    return value


def clean_trace(data, samplerate, trace_start, trace_end, need_start=False, need_end=False):
    '''
    Apply time stamps to our values and cut extra data.
    Return clean list of touples.
    '''


    new_data = []
    period = 1/samplerate

    if not need_start: need_start = trace_start