@@ -160,7 +160,7 @@ def _download_metadata():
160160 )
161161
162162
163- def get_traceid_coords (orig_time = None ):
163+ def get_traceid_coords_old (orig_time = None ):
164164 """
165165 Get coordinates for the trace_ids specified in config file.
166166
@@ -177,13 +177,85 @@ def get_traceid_coords(orig_time=None):
177177 _download_metadata ()
178178 traceid_coords = {}
179179 for trace_id in config .catalog_trace_id :
180+ net , sta , loc , chan = trace_id .split ('.' )
181+ net = net or '@@'
182+ _trace_id = f'{ net } .{ sta } .{ loc } .{ chan } '
180183 try :
181- coords = config .inventory .get_coordinates (trace_id , orig_time )
182- except Exception as err :
184+ coords = config .inventory .get_coordinates (_trace_id , orig_time )
185+ # pylint: disable=broad-except
186+ except Exception :
183187 # note: get_coordinaets raises a generic Exception
188+ # try again with empty channel code
189+ try :
190+ _trace_id = f'{ net } .{ sta } .{ loc } .'
191+ coords = config .inventory .get_coordinates (_trace_id , orig_time )
192+ except Exception as err :
193+ raise MetadataMismatchError (
194+ f'Unable to find coordinates for trace { trace_id } '
195+ f'at time { orig_time } '
196+ ) from err
197+ traceid_coords [trace_id ] = coords
198+ return traceid_coords
199+
200+
201+ def _fetch_coordinates (net , sta , loc , chan , orig_time ):
202+ """
203+ Fetch coordinates for a trace_id at a given time.
204+
205+ Attempt to retrieve coordinates, first with full trace_id,
206+ then without channel.
207+
208+ :param net: network code
209+ :type net: str
210+ :param sta: station code
211+ :type sta: str
212+ :param loc: location code
213+ :type loc: str
214+ :param chan: channel code
215+ :type chan: str
216+ :param orig_time: origin time
217+ :type orig_time: obspy.UTCDateTime
218+
219+ :return: coordinates or None
220+ :rtype: dict
221+ """
222+ for ch in [chan , '' ]:
223+ _trace_id = f'{ net } .{ sta } .{ loc } .{ ch } '
224+ try :
225+ return config .inventory .get_coordinates (_trace_id , orig_time )
226+ # pylint: disable=broad-except
227+ # note: get_coordinaets raises a generic Exception
228+ except Exception :
229+ # Try next option if it fails
230+ continue
231+ # If both attempts fail, return None
232+ return None
233+
234+
235+ def get_traceid_coords (orig_time = None ):
236+ """
237+ Get coordinates for the trace_ids specified in config file.
238+
239+ :param orig_time: origin time
240+ :type orig_time: obspy.UTCDateTime
241+ :return: a dictionary with trace_id as key and coordinates as value
242+ :rtype: dict
243+
244+ :raises MetadataMismatchError: if coordinates are not found
245+ """
246+ if config .inventory is None :
247+ _read_station_metadata ()
248+ if config .inventory is None :
249+ _download_metadata ()
250+ traceid_coords = {}
251+ for trace_id in config .catalog_trace_id :
252+ net , sta , loc , chan = trace_id .split ('.' )
253+ net = net or '@@'
254+ coords = _fetch_coordinates (net , sta , loc , chan , orig_time )
255+ if coords is None :
184256 raise MetadataMismatchError (
185257 f'Unable to find coordinates for trace { trace_id } '
186258 f'at time { orig_time } '
187- ) from err
259+ )
188260 traceid_coords [trace_id ] = coords
189261 return traceid_coords
0 commit comments