7 Commits d0169a0b14 ... 4b85a9cb55

Autor SHA1 Mensagem Data
  a7exd 4b85a9cb55 Add numbers format for some columns in CatalogStorage 2 anos atrás
  a7exd 3bad78ebdb Write filter_stations instead of sort_stations 2 anos atrás
  a7exd 9ad1899b92 Change functionality: list[Quake] has all events where is stations == params.sta if params.sta != all 2 anos atrás
  a7exd 4e522d55ef Fix error getting common attrs in CatalogStorage 2 anos atrás
  a7exd 464368b28c Fix query params datetime to UTC, fix sort_stations() 2 anos atrás
  a7exd 4b637094c3 Fix sta magnitude in BulletinStorage 2 anos atrás
  a7exd 92d5f21e9c Fix _add_sta: if sta.dist not None, this one for all phases in one sta.name 2 anos atrás
2 arquivos alterados com 62 adições e 33 exclusões
  1. 21 8
      quake_storages.py
  2. 41 25
      quakes_from_db.py

+ 21 - 8
quake_storages.py

@@ -40,7 +40,7 @@ class CatalogStorage(QuakesStorage):
                 continue
             self._init_sheet(quake)
             self._add_values_in_sheet(quake)
-        self._set_alignment()
+        self._format_cells()
         self.wb.save(self._file)
 
     def _init_storage(self) -> None:
@@ -48,20 +48,30 @@ class CatalogStorage(QuakesStorage):
             wb = openpyxl.Workbook()
             wb.save(self._file)
 
-    def _add_values_in_sheet(self, quake) -> None:
-        origin_dt, lat, lon, _, avg_ml, avg_mpsp, depth = _format_common_attrs(
-            quake)
+    def _add_values_in_sheet(self, quake: Quake) -> None:
+        origin_dt = _format_common_attrs(quake)[0]
         origin_d, origin_t = origin_dt.split()
+        lat = quake.lat if quake.lat else '-'
+        lon = quake.lon if quake.lon else '-'
+        mag = quake.magnitude
+        avg_ml = mag.ML if mag.ML != 0.0 else '-'
+        avg_mpsp = mag.MPSP if mag.MPSP != 0.0 else '-'
+        depth = quake.depth if quake.depth else '-'
         stations_name = ', '.join(quake.stations_name)
-        row = (origin_d, origin_t, lat, lon, depth, quake.reg, avg_ml,
-               avg_mpsp, stations_name)
+        row = (origin_d, origin_t, lat, lon, depth,
+               quake.reg, avg_ml, avg_mpsp, stations_name)
         self.sheet.append(row)
 
-    def _set_alignment(self) -> None:
+    def _format_cells(self) -> None:
         for sheet in self.wb.sheetnames:
             rows_of_cells = self.wb[sheet][self.wb[sheet].dimensions]
             for row in rows_of_cells:
                 for cell in row:
+                    if cell.column in (3, 4, 5):
+                        # digital format for lat, lon, depth
+                        cell.number_format = '0.00'
+                    elif cell.column in (7, 8):
+                        cell.number_format = '0.0'
                     cell.alignment = Alignment(horizontal='center',
                                                vertical='center')
 
@@ -139,8 +149,11 @@ class BulletinStorage(QuakesStorage):
             az = f'{sta.azimuth:.2f}' if sta.azimuth else '-'
             ampl = f'{sta.ampl:.4f}' if sta.ampl else '-'
             period = f'{sta.period:.2f}' if sta.period else '-'
+            mag = f'{sta.mag_ML:.1f}' if sta.mag_ML else \
+                f'{sta.mag_MPSP:.1f}' if sta.mag_MPSP else '-'
+            mag_type = 'ML' if sta.mag_ML else 'MPSP' if sta.mag_MPSP else '-'
             sta_data = (sta.name, dist, az, sta.phase, sta.entry, phase_dt,
-                        ampl, period, self.mag, self.mag_type)
+                        ampl, period, mag, mag_type)
             res += _format_to_str(sta_data,
                                   config.AMNT_COLUMN_SYMBOLS['sta_hdr']) + '\n'
         return res + '\n'

+ 41 - 25
quakes_from_db.py

@@ -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