"""End-to-end Thor report PDF rendering. Ingests an IDFW + .txt via save_imported_idf, runs gather_report_data (faking a minimal DB row), and renders the PDF to disk. """ from __future__ import annotations import sys import tempfile import json from pathlib import Path REPO = Path(__file__).resolve().parents[1] sys.path.insert(0, str(REPO)) from sfm.waveform_store import WaveformStore from sfm import report_pdf class FakeDb: """Stand-in for SeismoDb.get_event(); the renderer only needs a few cols.""" def __init__(self, event): self.event = event def get_event(self, _id): return self.event def main(): base = REPO / "tests/fixtures/THORDATA_example/THORDATA_example/UPMC Presby/UM11719" idfw = base / "UM11719_20231219162723.IDFW" txt = base / "TXT" / f"{idfw.name}.txt" with tempfile.TemporaryDirectory() as td: store = WaveformStore(Path(td)) ev, rec = store.save_imported_idf( idfw.read_bytes(), idfw, idf_report_text=txt.read_text(errors="replace"), ) print(f"save_imported_idf: h5={rec['hdf5_filename']}, sidecar={rec['sidecar_filename']}") # Verify sidecar has bw_report block sc_path = Path(td) / "UM11719" / f"{idfw.name}.sfm.json" sc = json.loads(sc_path.read_text()) bw = sc.get("bw_report", {}) print(f" bw_report.available: {bw.get('available')}") print(f" bw_report.peaks.tran.ppv_ips: {bw.get('peaks', {}).get('tran', {}).get('ppv_ips')}") print(f" bw_report.mic.pspl_dbl: {bw.get('mic', {}).get('pspl_dbl')}") print(f" bw_report.histogram.n_intervals: {bw.get('histogram', {}).get('n_intervals')}") # Build a DB-row-shaped dict from the Event for gather_report_data import datetime ts = ev.timestamp ts_iso = None if ts is not None: try: ts_iso = datetime.datetime(ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second).isoformat() except Exception: pass fake_row = { "serial": "UM11719", "blastware_filename": rec["filename"], "record_type": "Waveform", "timestamp": ts_iso, "sample_rate": ev.sample_rate, "project": ev.project_info.project if ev.project_info else None, "client": ev.project_info.client if ev.project_info else None, "operator": ev.project_info.operator if ev.project_info else None, "sensor_location": ev.project_info.sensor_location if ev.project_info else None, "created_at": None, } rd = report_pdf.gather_report_data(FakeDb(fake_row), store, event_id="test-1") print() print(f"=== ReportData ===") print(f" event_id: {rd.event_id}") print(f" serial: {rd.serial}") print(f" record_type: {rd.record_type}") print(f" event_datetime: {rd.event_datetime_str}") print(f" trigger: {rd.trigger_source}") print(f" geo_range: {rd.geo_range_str}") print(f" sample_rate: {rd.sample_rate_str}") print(f" firmware: {rd.firmware}") print(f" calibration: {rd.calibration_date} by {rd.calibration_by}") print(f" battery: {rd.battery_volts}") print(f" PVS: {rd.peak_vector_sum_ips} in/s at {rd.peak_vector_sum_time_s} sec") print(f" mic_pspl_dbl: {rd.mic_pspl_dbl}") print(f" mic_zc_freq_hz: {rd.mic_zc_freq_hz}") print(f" channel_stats: {len(rd.channel_stats)} rows") for cs in rd.channel_stats: print(f" {cs['name']}: PPV={cs['ppv_ips']} ZC={cs['zc_freq_hz']} ToP={cs['time_of_peak_s']} Acc={cs['peak_accel_g']} Disp={cs['peak_disp_in']} Test={cs['sensor_check']}") # Render the PDF out_path = REPO / "analysis_idf" / "thor_report.pdf" pdf_bytes = report_pdf.render_event_report_pdf(rd) out_path.write_bytes(pdf_bytes) print() print(f" PDF written: {out_path} ({len(pdf_bytes)} bytes)") if __name__ == "__main__": main()