1# http://pyrocko.org - GPLv3
2#
3# The Pyrocko Developers, 21st Century
4# ---|P------/S----------~Lg----------
6import math
7import re
9from pyrocko import util
12g_tmin, g_tmax = util.get_working_system_time_range()[:2]
15def time_or_none_to_str(x, format):
16 if x is None:
17 return '...'
18 else:
19 return util.time_to_str(x, format=format)
22def shorten_time_str(s):
23 return re.sub(r'(((((-01)?-01)? 00)?:00)?:00)?(\.0+)?$', '', s)
26def squirrel_content(cls):
28 def str_codes(self):
29 return str(self.codes)
31 cls.str_codes = property(str_codes)
33 def str_time_span(self):
34 tmin, tmax = self.time_span
35 deltat = getattr(self, 'deltat', 0)
36 if deltat is not None and deltat > 0:
37 fmt = min(9, max(0, -int(math.floor(math.log10(self.deltat)))))
38 else:
39 fmt = 6
41 if tmin == tmax:
42 return '%s' % time_or_none_to_str(tmin, fmt)
43 else:
44 return '%s - %s' % (
45 time_or_none_to_str(tmin, fmt), time_or_none_to_str(tmax, fmt))
47 cls.str_time_span = property(str_time_span)
49 def str_time_span_short(self):
50 return ' - '.join(
51 shorten_time_str(x) for x in self.str_time_span.split(' - '))
53 cls.str_time_span_short = property(str_time_span_short)
55 def summary_entries(self):
56 return (
57 self.__class__.__name__,
58 str(self.codes),
59 self.str_time_span)
61 if not hasattr(cls, 'summary_entries'):
62 cls.summary_entries = property(summary_entries)
64 def summary(self):
65 return util.fmt_summary(self.summary_entries, (10, 20, 0))
67 if not hasattr(cls, 'summary'):
68 cls.summary = property(summary)
70 def __lt__(self, other):
71 return self.__key__() < other.__key__()
73 cls.__lt__ = __lt__
75 def __key__(self):
76 return self.codes, self.time_span_g_clipped
78 cls.__key__ = __key__
80 @property
81 def time_span_g_clipped(self):
82 tmin, tmax = self.time_span
83 return (
84 tmin if tmin is not None else g_tmin,
85 tmax if tmax is not None else g_tmax)
87 cls.time_span_g_clipped = property(time_span_g_clipped)
89 return cls