|
@@ -17,9 +17,10 @@ class QueryParams(NamedTuple):
|
|
|
|
|
|
|
|
|
def _get_sql_query(params: QueryParams) -> str:
|
|
|
- sta = '' if params.sta.lower() == 'all' else params.sta
|
|
|
- from_dt = datetime.strptime(params.from_dt, '%Y-%m-%d %H:%M:%S').timestamp()
|
|
|
- to_dt = datetime.strptime(params.to_dt, '%Y-%m-%d %H:%M:%S').timestamp()
|
|
|
+ from_dt = datetime.strptime(params.from_dt + '+0000', '%Y-%m-%d %H:%M:%S%z')
|
|
|
+ from_dt_timestamp = from_dt.timestamp()
|
|
|
+ to_dt = datetime.strptime(params.to_dt + '+0000', '%Y-%m-%d %H:%M:%S%z')
|
|
|
+ to_dt_timestamp = to_dt.timestamp()
|
|
|
key_words = params.comment.split()
|
|
|
key_words_count = len(key_words)
|
|
|
if key_words_count > 1:
|
|
@@ -29,23 +30,22 @@ def _get_sql_query(params: QueryParams) -> str:
|
|
|
else:
|
|
|
comment_query = ""
|
|
|
return f"SELECT" \
|
|
|
- f" o.EVENTID, o.ORIGINTIME, o.LAT, o.LON," \
|
|
|
+ f" o.EVENTID, o.ORIGINTIME, ROUND(o.LAT, 2), ROUND(o.LON, 2)," \
|
|
|
f" o.`DEPTH`," \
|
|
|
f" CONCAT(SUBSTR(o.COMMENTS, 1, INSTR(o.COMMENTS, '.') - 3),"\
|
|
|
f" SUBSTR(o.COMMENTS, 20))," \
|
|
|
- f" a.ITIME, a.STA, ROUND(a.DIST, 3)," \
|
|
|
- f" ROUND(a.AZIMUTH, 3), a.IPHASE, CONCAT(a.IM_EM, a.FM)," \
|
|
|
- f" ROUND(a.AMPL, 4), ROUND(a.PER, 3)," \
|
|
|
- f" a.ML, a.MPSP " \
|
|
|
+ f" a.ITIME, a.STA, ROUND(a.DIST, 2)," \
|
|
|
+ f" ROUND(a.AZIMUTH, 2), a.IPHASE, CONCAT(a.IM_EM, a.FM)," \
|
|
|
+ f" ROUND(a.AMPL, 4), ROUND(a.PER, 2)," \
|
|
|
+ f" ROUND(a.ML, 1), ROUND(a.MPSP, 1) " \
|
|
|
f"FROM origin o " \
|
|
|
f"INNER JOIN arrival a ON a.EVENTID = o.EVENTID " \
|
|
|
f"WHERE" \
|
|
|
f" (o.COMMENTS LIKE '%{comment_query}%')" \
|
|
|
f" AND" \
|
|
|
- f" (o.ORIGINTIME BETWEEN '{from_dt}' AND " \
|
|
|
- f" '{to_dt}')" \
|
|
|
- f" AND (a.STA LIKE '%{sta}%')" \
|
|
|
- f" ORDER BY a.ITIME"
|
|
|
+ f" (o.ORIGINTIME BETWEEN '{from_dt_timestamp}' AND " \
|
|
|
+ f" '{to_dt_timestamp}')" \
|
|
|
+ f" ORDER BY o.ORIGINTIME"
|
|
|
|
|
|
|
|
|
def get_data(params: QueryParams) -> List[tuple]:
|
|
@@ -72,7 +72,7 @@ def get_quakes(params: QueryParams) -> Tuple[Quake, ...]:
|
|
|
for quake_record in quake_records:
|
|
|
if quake_record[0] != _id:
|
|
|
if len(stations) != 0:
|
|
|
- stations = sorted(stations, key=sort_stations)
|
|
|
+ stations = _filter_stations(stations)
|
|
|
quake = Quake(_id, origin_dt, lat,
|
|
|
lon, depth, reg, tuple(stations))
|
|
|
quakes.append(quake)
|
|
@@ -83,23 +83,26 @@ def get_quakes(params: QueryParams) -> Tuple[Quake, ...]:
|
|
|
|
|
|
sta_dt = datetime.utcfromtimestamp(quake_record[6])
|
|
|
sta = Sta(sta_dt, *quake_record[7:])
|
|
|
- prev_sta = _add_sta(sta, stations, prev_sta)
|
|
|
- stations = sorted(stations, key=sort_stations)
|
|
|
+ stations.append(sta)
|
|
|
+ stations = _filter_stations(stations)
|
|
|
quakes.append(Quake(_id, origin_dt, lat, lon,
|
|
|
depth, reg, tuple(stations)))
|
|
|
- return tuple(_filter_magnitude(quakes, params))
|
|
|
+ return tuple(_filter_quakes(quakes, params))
|
|
|
|
|
|
|
|
|
-def _add_sta(sta: Sta, stations: List[Sta], prev_sta: Sta | None):
|
|
|
+def _add_sta(sta: Sta, stations: List[Sta], prev_sta: Sta | None) -> Sta:
|
|
|
if sta.name in config.STA_RENAME:
|
|
|
sta.name += 'R'
|
|
|
+ if prev_sta is not None and sta.name == prev_sta.name:
|
|
|
+ if sta.dist is not None:
|
|
|
+ prev_sta.dist = sta.dist
|
|
|
+ else:
|
|
|
+ sta.dist = prev_sta.dist
|
|
|
if prev_sta is None or (sta.phase_dt != prev_sta.phase_dt) \
|
|
|
or (sta.name != prev_sta.name):
|
|
|
stations.append(sta)
|
|
|
out_sta = sta
|
|
|
else:
|
|
|
- if sta.dist is not None:
|
|
|
- prev_sta.dist = sta.dist
|
|
|
if sta.azimuth is not None:
|
|
|
prev_sta.azimuth = sta.azimuth
|
|
|
if sta.ampl is not None:
|
|
@@ -114,12 +117,25 @@ def _add_sta(sta: Sta, stations: List[Sta], prev_sta: Sta | None):
|
|
|
return out_sta
|
|
|
|
|
|
|
|
|
-def sort_stations(sta: Sta) -> float:
|
|
|
- return sta.dist if sta.dist else 0.0
|
|
|
+def _filter_stations(stations: List[Sta]) -> List[Sta]:
|
|
|
+ sorted_by_time = sorted(stations, key=lambda x: x.phase_dt)
|
|
|
+ sorted_by_name = sorted(sorted_by_time, key=lambda x: x.name)
|
|
|
+ res = []
|
|
|
+ prev_sta: Sta | None = None
|
|
|
+ for sta in sorted_by_name:
|
|
|
+ prev_sta = _add_sta(sta, res, prev_sta)
|
|
|
+ sorted_by_dist = sorted(res,
|
|
|
+ key=lambda x: x.dist if x.dist is not None else 0.0)
|
|
|
+ return sorted_by_dist
|
|
|
|
|
|
|
|
|
-def _filter_magnitude(quakes: List[Quake], params: QueryParams) -> List[Quake]:
|
|
|
+def _filter_quakes(quakes: List[Quake], params: QueryParams) -> List[Quake]:
|
|
|
+ sta_set = set(params.sta.split())
|
|
|
from_mag, to_mag = float(params.from_mag), float(params.to_mag)
|
|
|
- return [quake for quake in quakes
|
|
|
- if (from_mag <= quake.magnitude.ML <= to_mag)
|
|
|
- or (from_mag <= quake.magnitude.MPSP <= to_mag)]
|
|
|
+ res = [quake for quake in quakes
|
|
|
+ if ((from_mag <= quake.magnitude.ML <= to_mag)
|
|
|
+ or (from_mag <= quake.magnitude.MPSP <= to_mag))]
|
|
|
+ if params.sta.lower() != 'all':
|
|
|
+ res = [quake for quake in res
|
|
|
+ if sta_set.issubset(quake.stations_name)]
|
|
|
+ return res
|