Example #1
0
    def getWaveformNSCL(self, seedname, starttime, duration):
        """
        Gets a regular expression of channels from a start time for a duration
        in seconds. The regular expression must represent all characters of
        the 12-character NNSSSSSCCCLL pattern e.g. "US.....[BSHE]HZ.." is
        valid, but "US.....[BSHE]H" is not. Complex regular expressions are
        permitted "US.....BHZ..|CU.....[BH]HZ.."

        .. rubric:: Notes

        For detailed information regarding the usage of regular expressions
        in the query, see also the documentation for CWBQuery ("CWBQuery.doc")
        available at ftp://hazards.cr.usgs.gov/CWBQuery/.
        Using ".*" regular expression might or might not work. If the 12
        character seed name regular expression is less than 12 characters it
        might get padded with spaces on the server side.

        :type seedname: str
        :param seedname: The 12 character seedname or 12 character regexp
            matching channels
        :type start: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param start: The starting date/time to get
        :type duration: float
        :param duration: The duration in seconds to get
        :rtype: :class:`~obspy.core.stream.Stream`
        :returns: Stream object with requested data

        .. rubric:: Example

        >>> from obspy.neic import Client
        >>> from obspy import UTCDateTime
        >>> client = Client()
        >>> t = UTCDateTime() - 5 * 3600  # 5 hours before now
        >>> st = client.getWaveformNSCL("USISCO BH.00", t, 10)
        >>> print st  # doctest: +ELLIPSIS
        3 Trace(s) in Stream:
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        >>> st = client.getWaveformNSCL("USISCO B.*", t, 10)
        >>> print st  # doctest: +ELLIPSIS
        3 Trace(s) in Stream:
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        """
        start = str(UTCDateTime(starttime)).replace("T", " ").replace("Z", "")
        line = "'-dbg' '-s' '%s' '-b' '%s' '-d' '%s'\t" % \
            (seedname, start, duration)
        if self.debug:
            print ascdate() + " " + asctime() + " line=" + line
        success = False
        while not success:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                with NamedTemporaryFile() as tf:
                    if self.debug:
                        print ascdate(), asctime(), "connecting temp file", \
                              tf.name
                    s.connect((self.host, self.port))
                    s.setblocking(0)
                    s.send(line)
                    if self.debug:
                        print ascdate(), asctime(), "Connected - start reads"
                    slept = 0
                    maxslept = self.timeout / 0.05
                    totlen = 0
                    while True:
                        try:
                            data = s.recv(102400)
                            if self.debug:
                                print ascdate(), asctime(), "read len", \
                                    str(len(data)), " total", str(totlen)
                            if data.find("EOR") >= 0:
                                if self.debug:
                                    print ascdate(), asctime(), "<EOR> seen"
                                tf.write(data[0:data.find("<EOR>")])
                                totlen += len(data[0:data.find("<EOR>")])
                                tf.seek(0)
                                try:
                                    st = read(tf.name, 'MSEED')
                                except Exception, e:
                                    st = Stream()
                                st.trim(starttime, starttime + duration)
                                s.close()
                                success = True
                                break
                            else:
                                totlen += len(data)
                                tf.write(data)
                                slept = 0
                        except socket.error as e:
                            if slept > maxslept:
                                print ascdate(), asctime(), \
                                    "Timeout on connection", \
                                    "- try to reconnect"
                                slept = 0
                                s.close()
                            sleep(0.05)
                            slept += 1
Example #2
0
    def getWaveformNSCL(self, seedname, starttime, duration):
        """
        Gets a regular expression of channels from a start time for a duration
        in seconds. The regular expression must represent all characters of
        the 12-character NNSSSSSCCCLL pattern e.g. "US.....[BSHE]HZ.." is
        valid, but "US.....[BSHE]H" is not. Complex regular expressions are
        permitted "US.....BHZ..|CU.....[BH]HZ.."

        .. rubric:: Notes

        For detailed information regarding the usage of regular expressions
        in the query, see also the documentation for CWBQuery ("CWBQuery.doc")
        available at ftp://hazards.cr.usgs.gov/CWBQuery/.
        Using ".*" regular expression might or might not work. If the 12
        character seed name regular expression is less than 12 characters it
        might get padded with spaces on the server side.

        :type seedname: str
        :param seedname: The 12 character seedname or 12 character regexp
            matching channels
        :type start: :class:`~obspy.core.utcdatetime.UTCDateTime`
        :param start: The starting date/time to get
        :type duration: float
        :param duration: The duration in seconds to get
        :rtype: :class:`~obspy.core.stream.Stream`
        :returns: Stream object with requested data

        .. rubric:: Example

        >>> from obspy.neic import Client
        >>> from obspy import UTCDateTime
        >>> client = Client()
        >>> t = UTCDateTime() - 5 * 3600  # 5 hours before now
        >>> st = client.getWaveformNSCL("USISCO BH.00", t, 10)
        >>> print st  # doctest: +ELLIPSIS
        3 Trace(s) in Stream:
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        >>> st = client.getWaveformNSCL("USISCO B.*", t, 10)
        >>> print st  # doctest: +ELLIPSIS
        3 Trace(s) in Stream:
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        US.ISCO.00.BH... | 40.0 Hz, 401 samples
        """
        start = str(UTCDateTime(starttime)).replace("T", " ").replace("Z", "")
        line = "'-dbg' '-s' '%s' '-b' '%s' '-d' '%s'\t" % \
            (seedname, start, duration)
        if self.debug:
            print ascdate() + " " + asctime() + " line=" + line
        success = False
        while not success:
            try:
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
                with NamedTemporaryFile() as tf:
                    if self.debug:
                        print ascdate(), asctime(), "connecting temp file", \
                              tf.name
                    s.connect((self.host, self.port))
                    s.setblocking(0)
                    s.send(line)
                    if self.debug:
                        print ascdate(), asctime(), "Connected - start reads"
                    slept = 0
                    maxslept = self.timeout / 0.05
                    totlen = 0
                    while True:
                        try:
                            data = s.recv(102400)
                            if self.debug:
                                print ascdate(), asctime(), "read len", \
                                    str(len(data)), " total", str(totlen)
                            if data.find("EOR") >= 0:
                                if self.debug:
                                    print ascdate(), asctime(), "<EOR> seen"
                                tf.write(data[0:data.find("<EOR>")])
                                totlen += len(data[0:data.find("<EOR>")])
                                tf.seek(0)
                                try:
                                    st = read(tf.name, 'MSEED')
                                except Exception, e:
                                    st = Stream()
                                st.trim(starttime, starttime + duration)
                                s.close()
                                success = True
                                break
                            else:
                                totlen += len(data)
                                tf.write(data)
                                slept = 0
                        except socket.error as e:
                            if slept > maxslept:
                                print ascdate(), asctime(), \
                                    "Timeout on connection", \
                                    "- try to reconnect"
                                slept = 0
                                s.close()
                            sleep(0.05)
                            slept += 1
Example #3
0
                                success = True
                                break
                            else:
                                totlen += len(data)
                                tf.write(data)
                                slept = 0
                        except socket.error as e:
                            if slept > maxslept:
                                print ascdate(), asctime(), \
                                    "Timeout on connection", \
                                    "- try to reconnect"
                                slept = 0
                                s.close()
                            sleep(0.05)
                            slept += 1
            except socket.error as e:
                print traceback.format_exc()
                print "CWB QueryServer at " + self.host + "/" + str(self.port)
            except Exception as e:
                print traceback.format_exc()
                print "**** exception found=" + str(e)
        if self.debug:
            print ascdate() + " " + asctime() + " success?  len=" + str(totlen)
        st.merge(-1)
        return st


if __name__ == '__main__':
    import doctest
    doctest.testmod(exclude_empty=True)
Example #4
0
                                success = True
                                break
                            else:
                                totlen += len(data)
                                tf.write(data)
                                slept = 0
                        except socket.error as e:
                            if slept > maxslept:
                                print ascdate(), asctime(), \
                                    "Timeout on connection", \
                                    "- try to reconnect"
                                slept = 0
                                s.close()
                            sleep(0.05)
                            slept += 1
            except socket.error as e:
                print traceback.format_exc()
                print "CWB QueryServer at " + self.host + "/" + str(self.port)
            except Exception as e:
                print traceback.format_exc()
                print "**** exception found=" + str(e)
        if self.debug:
            print ascdate() + " " + asctime() + " success?  len=" + str(totlen)
        st.merge(-1)
        return st


if __name__ == '__main__':
    import doctest
    doctest.testmod(exclude_empty=True)