Exemplo n.º 1
0
def evalresp_for_frequencies(t_samp,
                             frequencies,
                             filename,
                             date,
                             station='*',
                             channel='*',
                             network='*',
                             locid='*',
                             units="VEL",
                             debug=False):
    """
    Use the evalresp library to extract instrument response information from a
    SEED RESP-file for the specified frequencies.

    :type t_samp: float
    :param t_samp: Sampling interval in seconds
    :type frequencies: list of float
    :param frequencies: Discrete frequencies to calculate response for.
    :type filename: str or file
    :param filename: SEED RESP-filename or open file like object with RESP
        information. Any object that provides a read() method will be
        considered to be a file like object.
    :type date: :class:`~obspy.core.utcdatetime.UTCDateTime`
    :param date: Date of interest
    :type station: str
    :param station: Station id
    :type channel: str
    :param channel: Channel id
    :type network: str
    :param network: Network id
    :type locid: str
    :param locid: Location id
    :type units: str
    :param units: Units to return response in. Can be either DIS, VEL or ACC
    :type debug: bool
    :param debug: Verbose output to stdout. Disabled by default.
    :rtype: :class:`numpy.ndarray` complex128
    :return: Frequency response from SEED RESP-file for given frequencies
    """
    if isinstance(filename, (str, native_str)):
        with open(filename, 'rb') as fh:
            data = fh.read()
    elif hasattr(filename, 'read'):
        data = filename.read()
    # evalresp needs files with correct line separators depending on OS
    with NamedTemporaryFile() as fh:
        tempfile = fh.name
        fh.write(os.linesep.encode('ascii', 'strict').join(data.splitlines()))
        fh.close()

        # start at zero to get zero for offset/ DC of fft
        start_stage = C.c_int(-1)
        stop_stage = C.c_int(0)
        stdio_flag = C.c_int(0)
        sta = C.create_string_buffer(station.encode('ascii', 'strict'))
        cha = C.create_string_buffer(channel.encode('ascii', 'strict'))
        net = C.create_string_buffer(network.encode('ascii', 'strict'))
        locid = C.create_string_buffer(locid.encode('ascii', 'strict'))
        unts = C.create_string_buffer(units.encode('ascii', 'strict'))
        if debug:
            vbs = C.create_string_buffer(b"-v")
        else:
            vbs = C.create_string_buffer(b"")
        rtyp = C.create_string_buffer(b"CS")
        datime = C.create_string_buffer(date.format_seed().encode(
            'ascii', 'strict'))
        fn = C.create_string_buffer(tempfile.encode('ascii', 'strict'))
        frequencies = np.asarray(frequencies)
        nfreqs = C.c_int(frequencies.shape[0])
        res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn,
                                frequencies, nfreqs, rtyp, vbs, start_stage,
                                stop_stage, stdio_flag, C.c_int(0))
        # optimizing performance, see
        # https://wiki.python.org/moin/PythonSpeed/PerformanceTips
        try:
            nfreqs, rfreqs, rvec = \
                res[0].nfreqs, res[0].freqs, res[0].rvec
        except ValueError:
            msg = "evalresp failed to calculate a response."
            raise ValueError(msg)
        h = np.empty(nfreqs, dtype=np.complex128)
        for i in range(nfreqs):
            h[i] = rvec[i].real + rvec[i].imag * 1j
        clibevresp.free_response(res)
        del nfreqs, rfreqs, rvec, res
    return h
Exemplo n.º 2
0
def evalresp(t_samp, nfft, filename, date, station='*', channel='*',
             network='*', locid='*', units="VEL", freq=False,
             debug=False):
    """
    Use the evalresp library to extract instrument response
    information from a SEED RESP-file.

    :type t_samp: float
    :param t_samp: Sampling interval in seconds
    :type nfft: int
    :param nfft: Number of FFT points of signal which needs correction
    :type filename: str or file
    :param filename: SEED RESP-filename or open file like object with RESP
        information. Any object that provides a read() method will be
        considered to be a file like object.
    :type date: :class:`~obspy.core.utcdatetime.UTCDateTime`
    :param date: Date of interest
    :type station: str
    :param station: Station id
    :type channel: str
    :param channel: Channel id
    :type network: str
    :param network: Network id
    :type locid: str
    :param locid: Location id
    :type units: str
    :param units: Units to return response in. Can be either DIS, VEL or ACC
    :type debug: bool
    :param debug: Verbose output to stdout. Disabled by default.
    :rtype: :class:`numpy.ndarray` complex128
    :return: Frequency response from SEED RESP-file of length nfft
    """
    if isinstance(filename, (str, native_str)):
        with open(filename, 'rb') as fh:
            data = fh.read()
    elif hasattr(filename, 'read'):
        data = filename.read()
    # evalresp needs files with correct line separators depending on OS
    with NamedTemporaryFile() as fh:
        tempfile = fh.name
        fh.write(os.linesep.encode('ascii', 'strict').join(data.splitlines()))
        fh.close()

        fy = 1 / (t_samp * 2.0)
        # start at zero to get zero for offset/ DC of fft
        freqs = np.linspace(0, fy, nfft // 2 + 1)
        start_stage = C.c_int(-1)
        stop_stage = C.c_int(0)
        stdio_flag = C.c_int(0)
        sta = C.create_string_buffer(station.encode('ascii', 'strict'))
        cha = C.create_string_buffer(channel.encode('ascii', 'strict'))
        net = C.create_string_buffer(network.encode('ascii', 'strict'))
        locid = C.create_string_buffer(locid.encode('ascii', 'strict'))
        unts = C.create_string_buffer(units.encode('ascii', 'strict'))
        if debug:
            vbs = C.create_string_buffer(b"-v")
        else:
            vbs = C.create_string_buffer(b"")
        rtyp = C.create_string_buffer(b"CS")
        datime = C.create_string_buffer(
            date.format_seed().encode('ascii', 'strict'))
        fn = C.create_string_buffer(tempfile.encode('ascii', 'strict'))
        nfreqs = C.c_int(freqs.shape[0])
        res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn,
                                freqs, nfreqs, rtyp, vbs, start_stage,
                                stop_stage, stdio_flag, C.c_int(0))
        # optimizing performance, see
        # https://wiki.python.org/moin/PythonSpeed/PerformanceTips
        try:
            nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec
        except ValueError:
            msg = "evalresp failed to calculate a response."
            raise ValueError(msg)
        h = np.empty(nfreqs, dtype=np.complex128)
        f = np.empty(nfreqs, dtype=np.float64)
        for i in range(nfreqs):
            h[i] = rvec[i].real + rvec[i].imag * 1j
            f[i] = rfreqs[i]
        clibevresp.free_response(res)
        del nfreqs, rfreqs, rvec, res
    if freq:
        return h, f
    return h
Exemplo n.º 3
0
def evalresp(
    t_samp, nfft, filename, date, station="*", channel="*", network="*", locid="*", units="VEL", freq=False, debug=False
):
    """
    Use the evalresp library to extract instrument response
    information from a SEED RESP-file.

    :type t_samp: float
    :param t_samp: Sampling interval in seconds
    :type nfft: int
    :param nfft: Number of FFT points of signal which needs correction
    :type filename: str (or open file like object)
    :param filename: SEED RESP-filename or open file like object with RESP
        information. Any object that provides a read() method will be
        considered to be a file like object.
    :type date: UTCDateTime
    :param date: Date of interest
    :type station: str
    :param station: Station id
    :type channel: str
    :param channel: Channel id
    :type network: str
    :param network: Network id
    :type locid: str
    :param locid: Location id
    :type units: str
    :param units: Units to return response in. Can be either DIS, VEL or ACC
    :type debug: bool
    :param debug: Verbose output to stdout. Disabled by default.
    :rtype: numpy.ndarray complex128
    :return: Frequency response from SEED RESP-file of length nfft
    """
    if isinstance(filename, basestring):
        with open(filename, "rb") as fh:
            data = fh.read()
    elif hasattr(filename, "read"):
        data = filename.read()
    # evalresp needs files with correct line separators depending on OS
    with NamedTemporaryFile() as fh:
        tempfile = fh.name
        fh.write(os.linesep.join(data.splitlines()))
        fh.close()

        fy = 1 / (t_samp * 2.0)
        # start at zero to get zero for offset/ DC of fft
        freqs = np.linspace(0, fy, nfft // 2 + 1)
        start_stage = C.c_int(-1)
        stop_stage = C.c_int(0)
        stdio_flag = C.c_int(0)
        sta = C.create_string_buffer(station)
        cha = C.create_string_buffer(channel)
        net = C.create_string_buffer(network)
        locid = C.create_string_buffer(locid)
        unts = C.create_string_buffer(units)
        if debug:
            vbs = C.create_string_buffer("-v")
        else:
            vbs = C.create_string_buffer("")
        rtyp = C.create_string_buffer("CS")
        datime = C.create_string_buffer(date.formatSEED())
        fn = C.create_string_buffer(tempfile)
        nfreqs = C.c_int(freqs.shape[0])
        res = clibevresp.evresp(
            sta,
            cha,
            net,
            locid,
            datime,
            unts,
            fn,
            freqs,
            nfreqs,
            rtyp,
            vbs,
            start_stage,
            stop_stage,
            stdio_flag,
            C.c_int(0),
        )
        # optimizing performance, see
        # http://wiki.python.org/moin/PythonSpeed/PerformanceTips
        nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec
        h = np.empty(nfreqs, dtype="complex128")
        f = np.empty(nfreqs, dtype="float64")
        for i in xrange(nfreqs):
            h[i] = rvec[i].real + rvec[i].imag * 1j
            f[i] = rfreqs[i]
        clibevresp.free_response(res)
        del nfreqs, rfreqs, rvec, res
    if freq:
        return h, f
    return h
Exemplo n.º 4
0
def evalresp(t_samp,
             nfft,
             filename,
             date,
             station='*',
             channel='*',
             network='*',
             locid='*',
             units="VEL",
             freq=False,
             debug=False):
    """
    Use the evalresp library to extract instrument response
    information from a SEED RESP-file.

    :type t_samp: float
    :param t_samp: Sampling interval in seconds
    :type nfft: int
    :param nfft: Number of FFT points of signal which needs correction
    :type filename: str (or open file like object)
    :param filename: SEED RESP-filename or open file like object with RESP
        information. Any object that provides a read() method will be
        considered to be a file like object.
    :type date: UTCDateTime
    :param date: Date of interest
    :type station: str
    :param station: Station id
    :type channel: str
    :param channel: Channel id
    :type network: str
    :param network: Network id
    :type locid: str
    :param locid: Location id
    :type units: str
    :param units: Units to return response in. Can be either DIS, VEL or ACC
    :type debug: bool
    :param debug: Verbose output to stdout. Disabled by default.
    :rtype: numpy.ndarray complex128
    :return: Frequency response from SEED RESP-file of length nfft
    """
    if isinstance(filename, basestring):
        with open(filename, 'rb') as fh:
            data = fh.read()
    elif hasattr(filename, 'read'):
        data = filename.read()
    # evalresp needs files with correct line separators depending on OS
    with NamedTemporaryFile() as fh:
        tempfile = fh.name
        fh.write(os.linesep.join(data.splitlines()))
        fh.close()

        fy = 1 / (t_samp * 2.0)
        # start at zero to get zero for offset/ DC of fft
        freqs = np.linspace(0, fy, nfft // 2 + 1)
        start_stage = C.c_int(-1)
        stop_stage = C.c_int(0)
        stdio_flag = C.c_int(0)
        sta = C.create_string_buffer(station)
        cha = C.create_string_buffer(channel)
        net = C.create_string_buffer(network)
        locid = C.create_string_buffer(locid)
        unts = C.create_string_buffer(units)
        if debug:
            vbs = C.create_string_buffer("-v")
        else:
            vbs = C.create_string_buffer("")
        rtyp = C.create_string_buffer("CS")
        datime = C.create_string_buffer(date.formatSEED())
        fn = C.create_string_buffer(tempfile)
        nfreqs = C.c_int(freqs.shape[0])
        res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn, freqs,
                                nfreqs, rtyp, vbs, start_stage, stop_stage,
                                stdio_flag, C.c_int(0))
        # optimizing performance, see
        # http://wiki.python.org/moin/PythonSpeed/PerformanceTips
        nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec
        h = np.empty(nfreqs, dtype='complex128')
        f = np.empty(nfreqs, dtype='float64')
        for i in xrange(nfreqs):
            h[i] = rvec[i].real + rvec[i].imag * 1j
            f[i] = rfreqs[i]
        clibevresp.free_response(res)
        del nfreqs, rfreqs, rvec, res
    if freq:
        return h, f
    return h
Exemplo n.º 5
0
def evalresp(t_samp, nfft, filename, date, station='*', channel='*',
             network='*', locid='*', units="VEL", freq=False,
             debug=False):
    """
    Use the evalresp library to extract instrument response
    information from a SEED RESP-file.

    :type t_samp: float
    :param t_samp: Sampling interval in seconds
    :type nfft: int
    :param nfft: Number of FFT points of signal which needs correction
    :type filename: str
    :param filename: SEED RESP-filename or content of RESP file
    :type date: UTCDateTime
    :param date: Date of interest
    :type station: str
    :param station: Station id
    :type channel: str
    :param channel: Channel id
    :type network: str
    :param network: Network id
    :type locid: str
    :param locid: Location id
    :type units: str
    :param units: Units to return response in. Can be either DIS, VEL or ACC
    :type debug: bool
    :param debug: Verbose output to stdout. Disabled by default.
    :rtype: numpy.ndarray complex128
    :return: Frequency response from SEED RESP-file of length nfft
    """
    # evalresp needs files with correct line separators depending on OS
    data = open(filename, 'rb').read()
    fh = NamedTemporaryFile()
    tempfile = fh.name
    fh.write(os.linesep.join(data.splitlines()))
    fh.close()

    fy = 1 / (t_samp * 2.0)
    # start at zero to get zero for offset/ DC of fft
    freqs = np.linspace(0, fy, nfft // 2 + 1)
    start_stage = C.c_int(-1)
    stop_stage = C.c_int(0)
    stdio_flag = C.c_int(0)
    sta = C.create_string_buffer(station)
    cha = C.create_string_buffer(channel)
    net = C.create_string_buffer(network)
    locid = C.create_string_buffer(locid)
    unts = C.create_string_buffer(units)
    if debug:
        vbs = C.create_string_buffer("-v")
    else:
        vbs = C.create_string_buffer("")
    rtyp = C.create_string_buffer("CS")
    datime = C.create_string_buffer("%d,%3d" % (date.year, date.julday))
    fn = C.create_string_buffer(tempfile)
    nfreqs = C.c_int(freqs.shape[0])
    res = clibevresp.evresp(sta, cha, net, locid, datime, unts, fn,
                            freqs, nfreqs, rtyp, vbs, start_stage,
                            stop_stage, stdio_flag, C.c_int(0))
    # optimizing performance, see
    # http://wiki.python.org/moin/PythonSpeed/PerformanceTips
    nfreqs, rfreqs, rvec = res[0].nfreqs, res[0].freqs, res[0].rvec
    h = np.empty(nfreqs, dtype='complex128')
    f = np.empty(nfreqs, dtype='float64')
    for i in xrange(nfreqs):
        h[i] = rvec[i].real + rvec[i].imag * 1j
        f[i] = rfreqs[i]
    clibevresp.free_response(res)
    del nfreqs, rfreqs, rvec, res
    # delete temporary file
    try:
        os.remove(tempfile)
    except:
        pass
    if freq:
        return h, f
    return h