- Timestamp:
- 04/04/2013 05:00:12 PM (8 years ago)
- Location:
- SHX/trunk/SeismicHandler
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
SHX/trunk/SeismicHandler/commands/fetch.py
r927 r928 8 8 from SeismicHandler.basics.command import BaseCommand 9 9 from SeismicHandler.modules.traces import add_traces 10 from SeismicHandler.modules.miniseed import miniseed11 10 from SeismicHandler.basics.messages import log_message 12 11 from SeismicHandler.basics.error import ShxError 13 12 from SeismicHandler.config import Settings 14 from obspy.core import Stream, read 13 from SeismicHandler.modules.stations import resolveStations 14 from obspy.core import Stream 15 15 from obspy.sh.core import toUTCDateTime 16 16 from obspy.arclink.client import Client … … 22 22 URI:http://www.seismic-handler.org/portal/wiki/ShFetch 23 23 """ 24 numberOfParameters = [ 3]24 numberOfParameters = [5] 25 25 parameterQueries = [ 26 26 { … … 51 51 def __init__(self, *args, **kwargs): 52 52 # unroll args & kwargs 53 self._client = None 53 54 BaseCommand.__init__(self, *args, **kwargs) 54 55 55 56 def run(self): 56 param = self.parameters[0].split('.') 57 if len(param) != 4: 58 msg = "Malformed station/component code (use e.g. GR.GRA1..BHZ)!" 59 raise ShxError(msg) 57 stations = resolveStations(self.parameters[0]) 58 channel = self.parameters[1] 59 components = self.parameters[2] 60 start = self.parameters[3] 61 duration = float(self.parameters[4]) 62 63 final = Stream() 64 for station in stations: 65 for component in components: 66 st = [] 67 if self.qualifiers["USECACHE"]: 68 st = self.from_cache(station, channel, component, start, duration) 69 70 # fetch from remote if not found 71 if not st: 72 try: 73 st = self.arclink(station, channel, component, start, duration) 74 except: 75 continue 76 77 if self.qualifiers["STOREMETA"]: 78 pass 79 80 if st: 81 final += st 82 83 add_traces(final, True) 84 85 def from_cache(self, station, channel, component, start, duration): 86 """ 87 Load files from cache. 88 """ 89 from SeismicHandler.commands.reads import reads 90 91 path = "sds:" + Settings.config.paths.cache[0] 92 st = reads.load(station, channel, component, start, duration, path, return_stream=True) 93 94 # not found at all 95 if not st or st == 1: 96 return [] 97 98 # check length (allow one second offset) 99 start = toUTCDateTime(start) 100 t = st[0] 101 if t.stats.starttime > start + 1 or \ 102 t.stats.endtime < start + duration - 1: 103 104 st = [] 105 msg = "not in cache completely: %s.%s%s %s to %s" % ( 106 station, 107 channel, 108 component, 109 start, 110 start + 600 111 ) 112 log_message("info.fetch", msg) 113 if Settings.swChatty: 114 print msg 115 116 return st 117 118 def arclink(self, stationid, channel, component, start, duration): 119 """ 120 Request data from (remote) arclink server. 121 """ 60 122 try: 61 param.append(toUTCDateTime(self.parameters[1])) 62 except: 63 raise ShxError("Malformed time code!") 64 65 try: 66 param.append(param[-1] + float(self.parameters[2])) 67 except: 68 raise ShxError("Malformed waveform length parameter!") 69 70 # read from own cache / no support for wildcards 71 if self.qualifiers["USECACHE"]: 72 k = ["network", "station", "location", "channel", "start"] 73 param2 = dict(zip(k, param[:5])) 74 param2["sds"] = Settings.config.paths.cache[0] 75 flist = miniseed(**param2) 76 st = Stream() 77 for f in flist: 78 try: 79 t = read(f, format="MSEED") 80 log_message("info.fetch", "Reading from cache: %s" % f) 81 except: 82 log_message("warning.fetch", "Cannot read file: %s" % f) 83 continue 84 st += t 85 start = toUTCDateTime(self.parameters[1]) 86 st.merge(method=1, fill_value='latest') 87 st.trim(start, start + float(self.parameters[2])) 123 network, station, location = stationid.split('.') 124 except ValueError: 125 raise ShxError("Invalid station code: %s" % stationid) 126 127 # reuse client if possible 128 if self._client is not None: 129 client = self._client 88 130 else: 89 st = self.arclink(param) 90 91 if self.qualifiers["STOREMETA"]: 92 pass 93 94 add_traces(st, True) 95 96 def arclink(self, parameters): 97 """Documentation""" 98 host, port = Settings.config.arclink['host'][0].split(':') 99 if self.qualifiers["HOST"]: 100 host, port = self.qualifiers["HOST"].split(':') 101 try: 102 login = Settings.config.arclink['login'][0] 103 except: 104 login = '' 105 106 if self.qualifiers["USER"]: 107 user = self.qualifiers["USER"] 108 else: 109 user = Settings.config.arclink['user'][0] 110 if not user: 111 import getpass 112 import platform 113 user = "%s@%s.SHX.auto.local" % ( 114 getpass.getuser(), platform.node()) 115 msg = "No user id for remote access provided, please edit " 116 msg += "configuration! Using '%s' meantime..." 117 log_message("warning.fetch", msg % user) 118 119 if self.qualifiers["PASSWORD"]: 120 user = self.qualifiers["PASSWORD"] 121 122 client = Client( 123 user=user, 124 host=host, 125 port=port, 126 password=login, 127 debug=True 128 ) 129 131 host, port = Settings.config.arclink['host'][0].split(':') 132 if self.qualifiers["HOST"]: 133 host, port = self.qualifiers["HOST"].split(':') 134 try: 135 login = Settings.config.arclink['login'][0] 136 except: 137 login = '' 138 139 if self.qualifiers["USER"]: 140 user = self.qualifiers["USER"] 141 else: 142 user = Settings.config.arclink['user'][0] 143 if not user: 144 import getpass 145 import platform 146 user = "%s@%s.SHX.auto.local" % ( 147 getpass.getuser(), platform.node()) 148 msg = "No user id for remote access provided, please edit " 149 msg += "configuration! Using '%s' meanwhile..." 150 log_message("warning.fetch", msg % user) 151 152 if self.qualifiers["PASSWORD"]: 153 user = self.qualifiers["PASSWORD"] 154 155 client = Client( 156 user=user, 157 host=host, 158 port=port, 159 password=login, 160 debug=False 161 ) 162 self._client = client 163 164 # basic request 165 start = toUTCDateTime(start) 166 parameters = [ 167 network, 168 station, 169 location, 170 "%s%s" % (channel, component), 171 start, 172 start + duration 173 ] 130 174 # format, compression, metadata, routing 131 175 parameters.extend(['MSEED', True, True, False]) 132 176 try: 133 177 st = client.getWaveform(*parameters) 134 msg = " %u streams fetched via webdc." % len(st)178 msg = "fetched via webdc: %s" % ",".join([_i.id for _i in st]) 135 179 log_message("info.fetch", msg) 136 180 if Settings.swChatty: 137 181 print msg 138 182 except Exception as E: 139 print E 140 raise ShxError("Data could not be fetched remotely.") 183 raise ShxError("%s: %s" % (str(E), stationid)) 141 184 142 185 # save into cache … … 170 213 fname = os.path.join(folder, fname2) 171 214 t.write(fname, format="MSEED", encoding="FLOAT32") 172 msg = "Trace of %s data saved in %s" 173 log_message("info.fetch", msg % (t.id, fname)) 215 msg = "Trace of %s data saved in %s" % (t.id, fname) 216 log_message("info.fetch", msg) 217 if Settings.swChatty: 218 print msg 174 219 175 220 return st -
SHX/trunk/SeismicHandler/commands/reads.py
r927 r928 6 6 7 7 import os 8 from obspy.core import read9 from obspy.sh.core import toUTCDateTime10 8 from SeismicHandler.basics.command import BaseCommand 11 9 from SeismicHandler.modules.miniseed import miniseed 12 10 from SeismicHandler.basics.messages import log_message 13 11 from SeismicHandler.modules.traces import Stream, add_traces 12 from SeismicHandler.config import Settings 13 from obspy.core import read 14 from obspy.sh.core import toUTCDateTime 14 15 15 16 … … 107 108 pass 108 109 elif path[:4].lower() == "sds:": 109 options["sds"] = path[4:] 110 sdspath = path[4:] 111 # use cache by default 112 if not sdspath: 113 sdspath = Settings.config.paths.cache[0] 114 options["sds"] = sdspath.replace('\\', '/') 110 115 elif path.endswith(":"): 111 116 options["sfdfile"] = os.path.join( … … 119 124 120 125 if not len(mseed): 121 msg = " Waveform data base contains no information at all for %s"126 msg = "No data found at all for %s in requested time span" 122 127 log_message("warning.command.reads", msg % stations) 123 128 return 1 -
SHX/trunk/SeismicHandler/modules/miniseed.py
r927 r928 247 247 request = AttribDict(kwargs) 248 248 249 # this doesn't support wildcards! 250 folder = os.path.join( 251 request.sds, 252 "%s" % request.start.year, 253 request.network, 254 request.station, 255 "%s.D" % request.channel, 256 ) 257 258 fname = "%s.%s.%s.%s.D.%u.%03u" % ( 259 request.network, 260 request.station, 261 request.location, 262 request.channel, 263 request.start.year, 264 request.start.julday 265 ) 266 267 flist = glob.glob(os.path.join(folder, "%s*" % fname)) 268 self._filelist = flist 269 self._stations = resolveStations(request.station) 249 self._stations = resolveStations(request.stations) 250 self._filelist = [] 251 252 for station in self._stations: 253 network, name, location = station.split('.') 254 for component in request.components: 255 256 # this doesn't support wildcards! 257 folder = os.path.join( 258 request.sds, 259 "%s" % request.start.year, 260 network, 261 name, 262 "%s%s.D" % (request.channel, component), 263 ) 264 265 fname = "%s.%s.%s.%s%s.D.%u.%03u" % ( 266 network, 267 name, 268 location, 269 request.channel, 270 component, 271 request.start.year, 272 request.start.julday 273 ) 274 275 flist = glob.glob(os.path.join(folder, "%s*" % fname)) 276 277 if flist: 278 self._filelist.extend(flist) 270 279 271 280
Note: See TracChangeset
for help on using the changeset viewer.