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-04 09:52 +0000

1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

4# ---|P------/S----------~Lg---------- 

5 

6''' 

7Glue code to use :py:mod:`pyrocko.model` objects in :py:mod:`pyrocko.squirrel`. 

8''' 

9 

10import math 

11import re 

12 

13from pyrocko import util 

14 

15 

16g_tmin, g_tmax = util.get_working_system_time_range()[:2] 

17 

18 

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) 

24 

25 

26def shorten_time_str(s): 

27 return re.sub(r'(((((-01)?-01)? 00)?:00)?:00)?(\.0+)?$', '', s) 

28 

29 

30def squirrel_content(cls): 

31 

32 def str_codes(self): 

33 return str(self.codes) 

34 

35 cls.str_codes = property(str_codes) 

36 

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 

44 

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)) 

50 

51 cls.str_time_span = property(str_time_span) 

52 

53 def str_time_span_short(self): 

54 return ' - '.join( 

55 shorten_time_str(x) for x in self.str_time_span.split(' - ')) 

56 

57 cls.str_time_span_short = property(str_time_span_short) 

58 

59 def summary_entries(self): 

60 return ( 

61 self.__class__.__name__, 

62 str(self.codes), 

63 self.str_time_span) 

64 

65 if not hasattr(cls, 'summary_entries'): 

66 cls.summary_entries = property(summary_entries) 

67 

68 def summary(self): 

69 return util.fmt_summary(self.summary_entries, (10, 20, 0)) 

70 

71 if not hasattr(cls, 'summary'): 

72 cls.summary = property(summary) 

73 

74 def __lt__(self, other): 

75 return self.__key__() < other.__key__() 

76 

77 cls.__lt__ = __lt__ 

78 

79 def __key__(self): 

80 return self.codes, self.time_span_g_clipped 

81 

82 cls.__key__ = __key__ 

83 

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) 

90 

91 cls.time_span_g_clipped = property(time_span_g_clipped) 

92 

93 return cls