Coverage for /usr/local/lib/python3.11/dist-packages/pyrocko/model/content.py: 58%
48 statements
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
« prev ^ index » next coverage.py v6.5.0, created at 2023-10-06 06:59 +0000
1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6'''
7Glue code to use :py:mod:`pyrocko.model` objects in :py:mod:`pyrocko.squirrel`.
8'''
10import math
11import re
13from pyrocko import util
16g_tmin, g_tmax = util.get_working_system_time_range()[:2]
19def time_or_none_to_str(x, format):
20 if x is None:
21 return '...'
22 else:
23 return util.time_to_str(x, format=format)
26def shorten_time_str(s):
27 return re.sub(r'(((((-01)?-01)? 00)?:00)?:00)?(\.0+)?$', '', s)
30def squirrel_content(cls):
32 def str_codes(self):
33 return str(self.codes)
35 cls.str_codes = property(str_codes)
37 def str_time_span(self):
38 tmin, tmax = self.time_span
39 deltat = getattr(self, 'deltat', 0)
40 if deltat is not None and deltat > 0:
41 fmt = min(9, max(0, -int(math.floor(math.log10(self.deltat)))))
42 else:
43 fmt = 6
45 if tmin == tmax:
46 return '%s' % time_or_none_to_str(tmin, fmt)
47 else:
48 return '%s - %s' % (
49 time_or_none_to_str(tmin, fmt), time_or_none_to_str(tmax, fmt))
51 cls.str_time_span = property(str_time_span)
53 def str_time_span_short(self):
54 return ' - '.join(
55 shorten_time_str(x) for x in self.str_time_span.split(' - '))
57 cls.str_time_span_short = property(str_time_span_short)
59 def summary_entries(self):
60 return (
61 self.__class__.__name__,
62 str(self.codes),
63 self.str_time_span)
65 if not hasattr(cls, 'summary_entries'):
66 cls.summary_entries = property(summary_entries)
68 def summary(self):
69 return util.fmt_summary(self.summary_entries, (10, 20, 0))
71 if not hasattr(cls, 'summary'):
72 cls.summary = property(summary)
74 def __lt__(self, other):
75 return self.__key__() < other.__key__()
77 cls.__lt__ = __lt__
79 def __key__(self):
80 return self.codes, self.time_span_g_clipped
82 cls.__key__ = __key__
84 @property
85 def time_span_g_clipped(self):
86 tmin, tmax = self.time_span
87 return (
88 tmin if tmin is not None else g_tmin,
89 tmax if tmax is not None else g_tmax)
91 cls.time_span_g_clipped = property(time_span_g_clipped)
93 return cls