def test_gather():
    imcs = get_pgm_classes('imc')
    imts = get_pgm_classes('imt')

    required_imcs = [
        'calculate_channels', 'calculate_greater_of_two_horizontals',
        'calculate_gmrotd'
    ]
    required_imts = ['calculate_pga', 'calculate_sa', 'calculate_pgv']
    for imc in required_imcs:
        assert imc in imcs
    for imt in required_imts:
        assert imt in imts

    imcs = [
        'rotd0', 'roti10', 'gmrotd22', 'gmroti10', 'rotd0invalid',
        'roti10invalid', 'gmrotd22invalid', 'gmroti10invalid',
        'greater_of_two_horizontals'
    ]
    with warnings.catch_warnings():
        warnings.simplefilter("ignore")
        grouping = group_imcs(imcs)
    target_dict = {
        'rotd': [0.0],
        'roti': [10.0],
        'gmrotd': [22.0],
        'gmroti': [10.0],
        'greater_of_two_horizontals': ''
    }
    assert grouping == target_dict
Esempio n. 2
0
def calculate_sa(stream, imcs, rotation_matrix=None, origin=None):
    """
    Calculate the peak ground acceleration.

    Args:
        stream (obspy.core.stream.Stream): streams of strong ground motion.
            Traces in stream must be in units of %%g.
        imcs (list): list of imcs.
        rotation_matrix (ndarray): A rotation matrix for the rotd component.
            This is required when the rotd component is requested.
        origin (obspy.core.event.origin.Origin):
            Obspy event origin object.

    Returns:
        dictionary: Dictionary of sa for different components.
    """
    sa_dict = {}
    # check units and add channel pga
    for trace in stream:
        if trace.stats['units'] != '%%g':
            raise PGMException('Invalid units for sa: %r. '
                               'Units must be %%g' % trace.stats['units'])
    grouped_imcs = group_imcs(imcs)
    # gather imc classes
    pgm_classes = get_pgm_classes('imc')
    # store sa for imcs
    for imc in grouped_imcs:
        if 'calculate_' + imc in pgm_classes:
            sa_func = pgm_classes['calculate_' + imc]
            sa = sa_func(stream, origin=origin, percentiles=grouped_imcs[imc])
            if imc == 'rotd':
                if rotation_matrix is None:
                    raise PGMException(
                        'The rotation matrix must be included '
                        'in order to calculate the rotd component.')
                sa = sa_func(rotation_matrix,
                             origin=origin,
                             percentiles=grouped_imcs[imc],
                             rotated=True)
                for percentile in sa:
                    sa_dict[imc.upper() + str(percentile)] = sa[percentile]
            elif imc.find('rot') >= 0:
                for percentile in sa:
                    sa_dict[imc.upper() + str(percentile)] = sa[percentile]
            elif imc.find('channels') >= 0:
                for channel in sa:
                    sa_dict[channel] = sa[channel]
            elif imc.find('radial_transverse') >= 0:
                for channel in sa:
                    sa_dict[channel] = sa[channel]
            else:
                sa_dict[imc.upper()] = sa
        else:
            logging.warning('Not a valid IMC: %r. Skipping...' % imc)
    return sa_dict
Esempio n. 3
0
def calculate_pga(stream, imcs, origin=None):
    """
    Calculate the peak ground acceleration.

    Args:
        stream (obspy.core.stream.Stream): streams of strong ground motion.
            Traces in stream must be in units of %%g.
        imcs (list): list of imcs.
        origin (obspy.core.event.origin.Origin):
            Obspy event origin object.

    Returns:
        dictionary: Dictionary of pga for different components.
    """
    pga_dict = {}
    # check units and add channel pga
    for trace in stream:
        if trace.stats['units'] != '%%g':
            raise PGMException('Invalid units for PGA: %r. '
                               'Units must be %%g' % trace.stats['units'])
    # sort imcs
    grouped_imcs = group_imcs(imcs)
    # gather imc classes
    pgm_classes = get_pgm_classes('imc')
    # store pga for imcs
    for imc in grouped_imcs:
        if 'calculate_' + imc in pgm_classes:
            pga_func = pgm_classes['calculate_' + imc]
            pga = pga_func(stream, origin=origin,
                           percentiles=grouped_imcs[imc])
            if imc.find('rot') >= 0:
                for percentile in pga:
                    pga_dict[imc.upper() + str(percentile)] = pga[percentile]
            elif imc.find('channels') >= 0:
                for channel in pga:
                    pga_dict[channel] = pga[channel]
            elif imc.find('radial_transverse') >= 0:
                for channel in pga:
                    pga_dict[channel] = pga[channel]
            else:
                pga_dict[imc.upper()] = pga
        else:
            logging.warning('Not a valid IMC: %r. Skipping...' % imc)
    return pga_dict
Esempio n. 4
0
def calculate_arias(stream, imcs, return_streams=False, origin=None):
    """
    Calculate the peak ground acceleration.
    Args:
        stream (obspy.core.stream.Stream): streams of strong ground motion.
            Traces in stream must be in units of m/s/s.
        imcs (list): list of imcs.
        osccillators (dictionary): Dictionary of oscillators. Used when
                rotation imcs are requested. Default is None.
        return_streams (bool): Whether to return streams.
        origin (obspy.core.event.origin.Origin):
            Obspy event origin object.
    Returns:
        dictionary: Dictionary of arias for different components.
    """
    arias_streams = _calculate_channel_arias(stream)
    arias_stream = arias_streams[0]
    normalized_stream = arias_streams[1]

    if return_streams:
        return (arias_stream, normalized_stream)

    arias_dict = {}
    # sort imcs
    grouped_imcs = group_imcs(imcs)

    # gather imc classes
    pgm_classes = get_pgm_classes('imc')
    # store arias for imcss

    for imc in grouped_imcs:
        arias_func = pgm_classes['calculate_' + imc]
        if 'calculate_' + imc in pgm_classes:
            if imc.find('gmrot') >= 0:
                Ia, NIa = _calculate_rotated_arias(stream, 'gm')
                arias = arias_func(Ia,
                                   percentiles=grouped_imcs[imc],
                                   rotated=True,
                                   origin=origin)
                for percentile in arias:
                    arias_dict[imc.upper() +
                               str(percentile)] = arias[percentile]
            elif imc.find('rot') >= 0:
                Ia, NIa = _calculate_rotated_arias(stream, 'nongm')
                arias = arias_func(Ia,
                                   percentiles=grouped_imcs[imc],
                                   rotated=True,
                                   origin=origin)
                for percentile in arias:
                    arias_dict[imc.upper() +
                               str(percentile)] = arias[percentile]
            elif imc.find('channels') >= 0:
                arias = arias_func(arias_stream, origin=origin)
                for channel in arias:
                    arias_dict[channel] = arias[channel]
            elif imc.find('radial_transverse') >= 0:
                arias = arias_func(arias_stream, origin=origin)
                for channel in arias:
                    arias_dict[channel] = arias[channel]
            else:
                arias = arias_func(arias_stream, origin=origin)
                arias_dict[imc.upper()] = arias
        else:
            logging.warning('Not a valid IMC: %r. Skipping...' % imc)
    return arias_dict