1# http://pyrocko.org - GPLv3 

2# 

3# The Pyrocko Developers, 21st Century 

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

5 

6from __future__ import absolute_import, print_function 

7 

8import re 

9 

10from pyrocko.guts import load_string, dump 

11 

12from .. import common 

13 

14path_prefix = ''' 

15# All file paths given below are treated relative to the location of this 

16# configuration file. Here we may give a common prefix. For example, if the 

17# configuration file is in the sub-directory 'PROJECT/config/', set it to '..' 

18# so that all paths are relative to 'PROJECT/'. 

19path_prefix: '.' 

20'''.strip() 

21 

22 

23def _template_online_dataset(**kwargs): 

24 lqargs = [] 

25 for k in ['network', 'station', 'channel']: 

26 if k in kwargs: 

27 v = kwargs.pop(k) 

28 lqargs.append(" %s: '%s'" % (k, v)) 

29 

30 kwargs['qargs'] = '\n' + '\n'.join(lqargs) if lqargs else '{}' 

31 return ''' 

32--- !squirrel.Dataset 

33 

34{path_prefix} 

35 

36# Data sources to be added (LocalData, FDSNSource, CatalogSource, ...) 

37sources: 

38- !squirrel.FDSNSource 

39 

40 # URL or alias of FDSN site. 

41 site: {site} 

42 

43 # FDSN query arguments to make metadata queries. 

44 # See http://www.fdsn.org/webservices/fdsnws-station-1.1.pdf 

45 # Time span arguments should not be added here, because they are handled 

46 # automatically by Squirrel. 

47 query_args: {qargs} 

48'''.format(path_prefix=path_prefix, **kwargs).strip() 

49 

50 

51templates = { 

52 'dataset-local': { 

53 'description': 

54 'A typical collection of local files.', 

55 'yaml': ''' 

56--- !squirrel.Dataset 

57 

58{path_prefix} 

59 

60# Data sources to be added (LocalData, FDSNSource, CatalogSource, ...) 

61sources: 

62- !squirrel.LocalData # This data source is for local files. 

63 

64 # These paths are scanned for waveforms, stations, events. 

65 paths: 

66 - 'catalogs/events.txt' 

67 - 'meta/stations.xml' 

68 - 'data/waveforms' 

69 

70 # Select file format or 'detect' for autodetection. 

71 format: 'detect' 

72'''.format(path_prefix=path_prefix).strip()}, 

73 

74 'dataset-geofon': { 

75 'description': 

76 'Everything available through GEOFON.', 

77 'yaml': _template_online_dataset(site='geofon') 

78 }, 

79 

80 'dataset-iris-seis': { 

81 'description': 

82 'All high- and low-gain seismometer channels at IRIS.', 

83 'yaml': _template_online_dataset( 

84 site='iris', channel='?H?,?L?') 

85 }, 

86 

87 'dataset-iris-seis-bb': { 

88 'description': 

89 'All broad-band high-gain seismometer channels at IRIS.', 

90 'yaml': _template_online_dataset( 

91 site='iris', channel='VH?,LH?,BH?,HH?') 

92 }, 

93 

94 'dataset-bgr-gr-bfo': { 

95 'description': 'Station GR.BFO from BGR.', 

96 'yaml': _template_online_dataset( 

97 site='iris', network='GR', station='BFO') 

98 }, 

99} 

100 

101names = sorted(templates.keys()) 

102 

103template_listing = '\n'.join( 

104 '%-30s %s' % ( 

105 '%s:' % name, 

106 templates[name]['description']) for name in templates) 

107 

108 

109def setup(subparsers): 

110 p = common.add_parser( 

111 subparsers, 'template', 

112 help='Print configuration snippets.', 

113 description=''' 

114Available configuration SNIPPETs: 

115 

116{} 

117'''.format(template_listing).strip()) 

118 

119 p.add_argument( 

120 'name', 

121 choices=names, 

122 nargs='?', 

123 metavar='SNIPPET', 

124 help='Name of template snippet to print.') 

125 

126 p.add_argument( 

127 '--format', '-f', 

128 choices=['commented', 'normal', 'brief'], 

129 default='commented', 

130 metavar='FMT', 

131 help='Set verbosity level of output YAML. Choices: %(choices)s. ' 

132 'Default: %(default)s.') 

133 

134 return p 

135 

136 

137def decomment(s): 

138 out = [] 

139 for line in s.splitlines(): 

140 line = re.sub(r'#.+', '', line) 

141 if line.strip(): 

142 out.append(line) 

143 

144 return '\n'.join(out) 

145 

146 

147def brief(s): 

148 return dump(load_string(s)) 

149 

150 

151def call(parser, args): 

152 

153 if not args.name: 

154 print(template_listing) 

155 

156 else: 

157 func = { 

158 'brief': brief, 

159 'commented': lambda s: s, 

160 'normal': decomment}[args.format] 

161 

162 print(func(templates[args.name]['yaml']))