def iris2quakeml(url, output_folder=None): if "/spudservice/" not in url: url = url.replace("/spud/", "/spudservice/") if url.endswith("/"): url += "quakeml" else: url += "/quakeml" print("Downloading %s..." % url) r = urllib.request.urlopen(url) if r.code != 200: r.close() msg = "Error Downloading file!" raise Exception(msg) # For some reason the quakeml file is escaped HTML. h = html.parser.HTMLParser() data = h.unescape(r.read().decode('utf-8')) r.close() data = StringIO(data) try: cat = read_events(data) except BaseException: msg = "Could not read downloaded event data" raise ValueError(msg) cat.events = cat.events[:1] ev = cat[0] # Parse the event and get the preferred focal mechanism. Then get the # origin and magnitude associated with that focal mechanism. All other # focal mechanisms, origins and magnitudes will be removed. Just makes it # simpler and less error prone. if ev.preferred_focal_mechanism(): ev.focal_mechanisms = [ev.preferred_focal_mechanism()] else: ev.focal_mechanisms = [ev.focal_mechanisms[:1]] # Set the origin and magnitudes of the event. mt = ev.focal_mechanisms[0].moment_tensor ev.magnitudes = [mt.moment_magnitude_id.get_referred_object()] ev.origins = [mt.derived_origin_id.get_referred_object()] event_name = get_event_filename(ev, "GCMT") if output_folder: event_name = os.path.join(output_folder, event_name) cat.write(event_name, format="quakeml", validate=True) print("Written file", event_name)
def iris2quakeml(url, output_folder=None): if "/spudservice/" not in url: url = url.replace("/spud/", "/spudservice/") if url.endswith("/"): url += "quakeml" else: url += "/quakeml" print "Downloading %s..." % url r = urllib2.urlopen(url) if r.code != 200: r.close() msg = "Error Downloading file!" raise Exception(msg) # For some reason the quakeml file is escaped HTML. h = HTMLParser.HTMLParser() data = h.unescape(r.read()) r.close() data = StringIO(data) try: cat = readEvents(data) except: msg = "Could not read downloaded event data" raise ValueError(msg) cat.events = cat.events[:1] ev = cat[0] # Parse the event and get the preferred focal mechanism. Then get the # origin and magnitude associated with that focal mechanism. All other # focal mechanisms, origins and magnitudes will be removed. Just makes it # simpler and less error prone. if ev.preferred_focal_mechanism(): ev.focal_mechanisms = [ev.preferred_focal_mechanism()] else: ev.focal_mechanisms = [ev.focal_mechanisms[:1]] # Set the origin and magnitudes of the event. mt = ev.focal_mechanisms[0].moment_tensor ev.magnitudes = [mt.moment_magnitude_id.getReferredObject()] ev.origins = [mt.derived_origin_id.getReferredObject()] event_name = get_event_filename(ev, "GCMT") if output_folder: event_name = os.path.join(output_folder, event_name) cat.write(event_name, format="quakeml", validate=True) print "Written file", event_name
def iris2quakeml(url, output_folder=None): if "/spudservice/" not in url: url = url.replace("/spud/", "/spudservice/") if url.endswith("/"): url += "quakeml" else: url += "/quakeml" print(f"Downloading {url}...") r = urllib.request.urlopen(url) if r.code != 200: r.close() msg = "Error Downloading file!" raise Exception(msg) data = r.read() r.close() try: cat = read_events(data) except: msg = "Could not read downloaded event data" raise ValueError(msg) cat.events = cat.events[:1] ev = cat[0] # Parse the event and get the preferred focal mechanism. Then get the # origin and magnitude associated with that focal mechanism. All other # focal mechanisms, origins and magnitudes will be removed. Just makes it # simpler and less error prone. if ev.preferred_focal_mechanism(): ev.focal_mechanisms = [ev.preferred_focal_mechanism()] else: ev.focal_mechanisms = [ev.focal_mechanisms[:1]] # Set the origin and magnitudes of the event. mt = ev.focal_mechanisms[0].moment_tensor ev.magnitudes = [mt.moment_magnitude_id.get_referred_object()] ev.origins = [mt.derived_origin_id.get_referred_object()] event_filename = get_event_filename(ev, "GCMT") if output_folder: event_filename = os.path.join(output_folder, event_filename) with pyasdf.ASDFDataSet(event_filename) as ds: ds.add_quakeml(cat) print("Written file", event_filename)
def add_new_events(comm, count, min_magnitude, max_magnitude, min_year=None, max_year=None, threshold_distance_in_km=50.0): min_magnitude = float(min_magnitude) max_magnitude = float(max_magnitude) # Get the catalog. cat = _read_GCMT_catalog(min_year=min_year, max_year=max_year) # Filter with the magnitudes cat = cat.filter("magnitude >= %.2f" % min_magnitude, "magnitude <= %.2f" % max_magnitude) # Filtering catalog to only contain events in the domain. print("Filtering to only include events inside domain...") # Coordinates and the Catalog will have the same order! temp_cat = Catalog() coordinates = [] for event in cat: org = event.preferred_origin() or event.origins[0] if not comm.query.point_in_domain(org.latitude, org.longitude): continue temp_cat.events.append(event) coordinates.append((org.latitude, org.longitude)) cat = temp_cat chosen_events = [] print("%i valid events remain. Starting selection process..." % len(cat)) existing_events = comm.events.get_all_events().values() # Get the coordinates of all existing events. existing_coordinates = [(_i["latitude"], _i["longitude"]) for _i in existing_events] existing_origin_times = [_i["origin_time"] for _i in existing_events] # Special case handling in case there are no preexisting events. if not existing_coordinates: idx = random.randint(0, len(cat) - 1) chosen_events.append(cat[idx]) del cat.events[idx] existing_coordinates.append(coordinates[idx]) del coordinates[idx] _t = cat[idx].preferred_origin() or cat[idx].origins[0] existing_origin_times.append(_t.time) count -= 1 while count: if not coordinates: print("\tNo events left to select from. Stoping here.") break # Build kdtree and query for the point furthest away from any other # point. kdtree = SphericalNearestNeighbour(np.array(existing_coordinates)) distances = kdtree.query(np.array(coordinates), k=1)[0] idx = np.argmax(distances) event = cat[idx] coods = coordinates[idx] del cat.events[idx] del coordinates[idx] # Actual distance. distance = EARTH_RADIUS * distances[idx] if distance < threshold_distance_in_km: print("\tNo events left with distance to the next closest event " "of more then %.1f km. Stoping here." % threshold_distance_in_km) break # Make sure it did not happen within one day of an existing event. # This should also filter out duplicates. _t = event.preferred_origin() or event.origins[0] origin_time = _t.time if min([abs(origin_time - _i) for _i in existing_origin_times]) < \ 86400: print("\tSelected event temporally to close to existing event. " "Will not be chosen. Skipping to next event.") continue print("\tSelected event with the next closest event being %.1f km " "away." % distance) chosen_events.append(event) existing_coordinates.append(coods) count -= 1 print("Selected %i events." % len(chosen_events)) folder = comm.project.paths["events"] for event in chosen_events: filename = os.path.join(folder, get_event_filename(event, "GCMT")) Catalog(events=[event]).write(filename, format="quakeml", validate=True) print("Written %s" % (os.path.relpath(filename)))
def add_new_events(comm, count, min_magnitude, max_magnitude, min_year=None, max_year=None, threshold_distance_in_km=50.0): min_magnitude = float(min_magnitude) max_magnitude = float(max_magnitude) # Get the catalog. cat = _read_GCMT_catalog(min_year=min_year, max_year=max_year) # Filter with the magnitudes cat = cat.filter("magnitude >= %.2f" % min_magnitude, "magnitude <= %.2f" % max_magnitude) # Filtering catalog to only contain events in the domain. print("Filtering to only include events inside domain...") # Coordinates and the Catalog will have the same order! temp_cat = Catalog() coordinates = [] for event in cat: org = event.preferred_origin() or event.origins[0] if not comm.query.point_in_domain(org.latitude, org.longitude): continue temp_cat.events.append(event) coordinates.append((org.latitude, org.longitude)) cat = temp_cat chosen_events = [] print("%i valid events remain. Starting selection process..." % len(cat)) existing_events = comm.events.get_all_events().values() # Get the coordinates of all existing events. existing_coordinates = [ (_i["latitude"], _i["longitude"]) for _i in existing_events] existing_origin_times = [_i["origin_time"] for _i in existing_events] # Special case handling in case there are no preexisting events. if not existing_coordinates: idx = random.randint(0, len(cat) - 1) chosen_events.append(cat[idx]) del cat.events[idx] existing_coordinates.append(coordinates[idx]) del coordinates[idx] _t = cat[idx].preferred_origin() or cat[idx].origins[0] existing_origin_times.append(_t.time) count -= 1 while count: if not coordinates: print("\tNo events left to select from. Stoping here.") break # Build kdtree and query for the point furthest away from any other # point. kdtree = SphericalNearestNeighbour(np.array(existing_coordinates)) distances = kdtree.query(np.array(coordinates), k=1)[0] idx = np.argmax(distances) event = cat[idx] coods = coordinates[idx] del cat.events[idx] del coordinates[idx] # Actual distance. distance = EARTH_RADIUS * distances[idx] if distance < threshold_distance_in_km: print("\tNo events left with distance to the next closest event " "of more then %.1f km. Stoping here." % threshold_distance_in_km) break # Make sure it did not happen within one day of an existing event. # This should also filter out duplicates. _t = event.preferred_origin() or event.origins[0] origin_time = _t.time if min([abs(origin_time - _i) for _i in existing_origin_times]) < \ 86400: print("\tSelected event temporally to close to existing event. " "Will not be chosen. Skipping to next event.") continue print("\tSelected event with the next closest event being %.1f km " "away." % distance) chosen_events.append(event) existing_coordinates.append(coods) count -= 1 print("Selected %i events." % len(chosen_events)) folder = comm.project.paths["events"] for event in chosen_events: filename = os.path.join(folder, get_event_filename(event, "GCMT")) Catalog(events=[event]).write(filename, format="quakeml", validate=True) print("Written %s" % (os.path.relpath(filename)))