def getpath(self, date, mseed_dir): """ Gets path to mseed file (normally residing in subdir 'basedir/yyyy-mm/') can also reside in subdir basedir/yyyy/ @type date: L{UTCDateTime} or L{datetime} or L{date} @rtype: unicode """ files, subdir_len = psutils.filelist(mseed_dir, ext='mseed', subdirs=True) if subdir_len == 7: subdir = '{y:04d}-{m:02d}'.format(y=date.year, m=date.month) elif subdir_len == 4: subdir = '{y:04d}'.format(y=date.year) if not subdir in self.subdirs: s = 'No data for station {s} at date {d}!!' raise Exception(s.format(s=self.name, d=date.date)) path = os.path.join(self.basedir, subdir, self.file) return path
def get_stations(mseed_dir=MSEED_DIR, xml_inventories=(), dataless_inventories=(), networks=None, startday=None, endday=None, coord_tolerance=1E-4, verbose=True): """ Gets the list of stations from miniseed files, and extracts information from StationXML and dataless inventories. @type mseed_dir: str or unicode @type xml_inventories: list of L{obspy.station.inventory.Inventory} @type dataless_inventories: list of L{obspy.xseed.parser.Parser}) @type networks: list of str @type startday: L{datetime.date} @type endday: L{datetime.date} @rtype: list of L{Station} """ if verbose: print "Scanning stations in dir: " + mseed_dir # initializing list of stations by scanning name of miniseed files stations = [] files, subdir_len = psutils.filelist(mseed_dir, ext='mseed', subdirs=True, verbose=True) #if verbose: # print(subdir_len); print(files) for f in files: # splitting subdir/basename subdir, filename = os.path.split(f) # subdir = e.g., 1990-03 if len = 7 else subdir can equal just a year e.g. 1999 if len(subdir) == 7: year, month = int(subdir.split('-')[0]), int(subdir.split('-')[1]) # checking that month is within selected intervals if startday and (year, month) < (startday.year, startday.month): continue if endday and (year, month) > (endday.year, endday.month): continue elif len(subdir) == 4: year = int(subdir) # checking that year is within selected intervals # network, station name and station channel in basename, # e.g., BL.CACB.BHZ.mseed network, name, channel = filename.split('.')[0:3] if networks and network not in networks: continue # looking for station in list try: match = lambda s: [s.network, s.name, s.channel ] == [network, name, channel] station = next(s for s in stations if match(s)) except StopIteration: # appending new station, with current subdir station = Station(name=name, network=network, channel=channel, filename=filename, basedir=mseed_dir, subdirs=[subdir]) stations.append(station) else: # appending subdir to list of subdirs of station station.subdirs.append(subdir) if verbose: print 'Found {0} stations'.format(len(stations)) # adding lon/lat of stations from inventories if verbose: print "Inserting coordinates to stations from inventories" for sta in copy(stations): # coordinates of station in dataless inventories coords_set = set( (c['longitude'], c['latitude']) for inv in dataless_inventories for c in inv.getInventory()['channels'] if c['channel_id'].split('.')[:2] == [sta.network, sta.name]) # coordinates of station in xml inventories coords_set = coords_set.union( (s.longitude, s.latitude) for inv in xml_inventories for net in inv for s in net.stations if net.code == sta.network and s.code == sta.name) if not coords_set: # no coords found: removing station if verbose: print "WARNING: skipping {} as no coords were found".format( repr(sta)) stations.remove(sta) elif len(coords_set) == 1: # one set of coords found sta.coord = list(coords_set)[0] else: # several sets of coordinates: calculating max diff lons = [lon for lon, _ in coords_set] lons_combinations = list(it.combinations(lons, 2)) lats = [lat for _, lat in coords_set] lats_combinations = list(it.combinations(lats, 2)) maxdiff_lon = np.abs(np.diff(lons_combinations)).max() maxdiff_lat = np.abs(np.diff(lats_combinations)).max() if maxdiff_lon <= coord_tolerance and maxdiff_lat <= coord_tolerance: # coordinates differences are within tolerance: # assigning means of coordinates if verbose: s = ("{} has several sets of coords within " "tolerance: assigning mean coordinates") print s.format(repr(sta)) sta.coord = (np.mean(lons), np.mean(lats)) else: # coordinates differences are not within tolerance: # removing station if verbose: s = ( "WARNING: skipping {} with several sets of coords not " "within tolerance (max lon diff = {}, max lat diff = {})" ) print s.format(repr(sta), maxdiff_lon, maxdiff_lat) stations.remove(sta) return stations, subdir_len
def get_stations(mseed_dir=MSEED_DIR, xml_inventories=(), dataless_inventories=(), networks=None, startday=None, endday=None, coord_tolerance=1E-4, verbose=True): """ Gets the list of stations from miniseed files, and extracts information from StationXML and dataless inventories. @type mseed_dir: str or unicode @type xml_inventories: list of L{obspy.station.inventory.Inventory} @type dataless_inventories: list of L{obspy.xseed.parser.Parser}) @type networks: list of str @type startday: L{datetime.date} @type endday: L{datetime.date} @rtype: list of L{Station} """ if verbose: print "Scanning stations in dir: " + mseed_dir # initializing list of stations by scanning name of miniseed files stations = [] files, subdir_len = psutils.filelist(mseed_dir, ext='mseed', subdirs=True, verbose=True) #if verbose: # print(subdir_len); print(files) for f in files: # splitting subdir/basename subdir, filename = os.path.split(f) # subdir = e.g., 1990-03 if len = 7 else subdir can equal just a year e.g. 1999 if len(subdir) == 7: year, month = int(subdir.split('-')[0]), int(subdir.split('-')[1]) # checking that month is within selected intervals if startday and (year, month) < (startday.year, startday.month): continue if endday and (year, month) > (endday.year, endday.month): continue elif len(subdir) == 4: year = int(subdir) # checking that year is within selected intervals # network, station name and station channel in basename, # e.g., BL.CACB.BHZ.mseed network, name, channel = filename.split('.')[0:3] if networks and network not in networks: continue # looking for station in list try: match = lambda s: [s.network, s.name, s.channel] == [network, name, channel] station = next(s for s in stations if match(s)) except StopIteration: # appending new station, with current subdir station = Station(name=name, network=network, channel=channel, filename=filename, basedir=mseed_dir, subdirs=[subdir]) stations.append(station) else: # appending subdir to list of subdirs of station station.subdirs.append(subdir) if verbose: print 'Found {0} stations'.format(len(stations)) # adding lon/lat of stations from inventories if verbose: print "Inserting coordinates to stations from inventories" for sta in copy(stations): # coordinates of station in dataless inventories coords_set = set((c['longitude'], c['latitude']) for inv in dataless_inventories for c in inv.getInventory()['channels'] if c['channel_id'].split('.')[:2] == [sta.network, sta.name]) # coordinates of station in xml inventories coords_set = coords_set.union((s.longitude, s.latitude) for inv in xml_inventories for net in inv for s in net.stations if net.code == sta.network and s.code == sta.name) if not coords_set: # no coords found: removing station if verbose: print "WARNING: skipping {} as no coords were found".format(repr(sta)) stations.remove(sta) elif len(coords_set) == 1: # one set of coords found sta.coord = list(coords_set)[0] else: # several sets of coordinates: calculating max diff lons = [lon for lon, _ in coords_set] lons_combinations = list(it.combinations(lons, 2)) lats = [lat for _, lat in coords_set] lats_combinations = list(it.combinations(lats, 2)) maxdiff_lon = np.abs(np.diff(lons_combinations)).max() maxdiff_lat = np.abs(np.diff(lats_combinations)).max() if maxdiff_lon <= coord_tolerance and maxdiff_lat <= coord_tolerance: # coordinates differences are within tolerance: # assigning means of coordinates if verbose: s = ("{} has several sets of coords within " "tolerance: assigning mean coordinates") print s.format(repr(sta)) sta.coord = (np.mean(lons), np.mean(lats)) else: # coordinates differences are not within tolerance: # removing station if verbose: s = ("WARNING: skipping {} with several sets of coords not " "within tolerance (max lon diff = {}, max lat diff = {})") print s.format(repr(sta), maxdiff_lon, maxdiff_lat) stations.remove(sta) return stations, subdir_len