Baseline correction and timestep reduction#
Open the app motion library (no project file), print one motion’s properties, then save a baseline-corrected copy and a timestep-reduced copy. Output files are not added to the motion library.
1"""
2Apply baseline correction and timestep reduction to one Motion Manager library motion.
3
4Requires a running RSSeismic scripting server. No project file is needed.
5Motion names are the .txt file names (without extension) in the Motion Manager
6folders from RSSeismic Preferences.
7
8 python examples/example_baseline_correction_timestep_reduction.py --motion ChiChi
9 python examples/example_baseline_correction_timestep_reduction.py --port 60058 --motion ChiChi --output-dir "D:\\out"
10"""
11
12from __future__ import annotations
13
14import argparse
15import os
16import sys
17
18from rsseismic import RSSeismicApplication
19from rsseismic._client import DEFAULT_SCRIPTING_PORT
20
21
22def _print_properties(title: str, properties) -> None:
23 print(title)
24 print(f" Time step {properties.timeStep} {properties.timeStepUnit}")
25 print(f" Nyquist frequency {properties.nyquistFrequency} {properties.frequencyUnit}")
26 print(f" Number of points {properties.numPoints}")
27 print(f" PGA {properties.pga} {properties.accelerationUnit}")
28
29
30def run_correction_example(
31 motion_name: str,
32 port: int = DEFAULT_SCRIPTING_PORT,
33 output_dir: str = "",
34) -> None:
35 if not motion_name.strip():
36 raise ValueError("A motion name is required (file name without .txt).")
37
38 app = RSSeismicApplication(port=port)
39 try:
40 app.ping()
41 motion = app.MotionLibrary.getMotion(motion_name.strip())
42 original = motion.getProperties()
43 print(f"Using motion: {motion.name}")
44 _print_properties("Original properties", original)
45
46 if not output_dir:
47 output_dir = os.path.join(os.getcwd(), "corrected_motions_example")
48 output_dir = os.path.abspath(output_dir)
49 os.makedirs(output_dir, exist_ok=True)
50
51 corrected_path = os.path.join(output_dir, f"{motion.name}_baseline.txt")
52 reduced_path = os.path.join(output_dir, f"{motion.name}_dt2.txt")
53
54 correction = motion.baselineCorrection(corrected_path, overwrite=True)
55 print(f"\nSaved baseline-corrected file: {correction.filePath}")
56 print(f"Offset: {correction.offsetSeconds} s ({correction.offsetPoints} data points)")
57 _print_properties("Properties after baseline correction", correction.properties)
58
59 reduction = motion.timestepReduction(2, reduced_path, overwrite=True)
60 print(f"\nSaved timestep-reduced file: {reduction.filePath}")
61 print(f"Reduction factor: {reduction.reductionFactor}")
62 _print_properties("Properties after timestep reduction", reduction.properties)
63
64 after = motion.getProperties()
65 if after.timeStep != original.timeStep or after.numPoints != original.numPoints:
66 raise RuntimeError(
67 "Correction should write a new file only; the original library motion changed unexpectedly."
68 )
69
70 if not os.path.isfile(correction.filePath) or not os.path.isfile(reduction.filePath):
71 raise RuntimeError("Expected corrected motion files were not written.")
72
73 print("\nOriginal library motion is unchanged.")
74 print("Corrected files are on disk only; they are not in the motion library.")
75 finally:
76 app.close()
77
78
79def parse_args() -> argparse.Namespace:
80 parser = argparse.ArgumentParser(
81 description="Apply baseline correction and timestep reduction to one library motion."
82 )
83 parser.add_argument(
84 "--motion",
85 "-m",
86 required=True,
87 help="Motion name (file name without .txt) from Motion Manager folders.",
88 )
89 parser.add_argument(
90 "--port",
91 "-p",
92 type=int,
93 default=int(os.environ.get("RSSEISMIC_SCRIPTING_PORT", DEFAULT_SCRIPTING_PORT)),
94 help=f"gRPC port (default: {DEFAULT_SCRIPTING_PORT}, or RSSEISMIC_SCRIPTING_PORT)",
95 )
96 parser.add_argument(
97 "--output-dir",
98 "-o",
99 default="",
100 help="Folder for output files. Default: ./corrected_motions_example",
101 )
102 return parser.parse_args()
103
104
105def main() -> int:
106 args = parse_args()
107 try:
108 run_correction_example(args.motion, port=args.port, output_dir=args.output_dir)
109 except Exception as ex:
110 print(f"Example failed: {ex}", file=sys.stderr)
111 return 1
112 print("Baseline correction / timestep reduction example finished.")
113 return 0
114
115
116if __name__ == "__main__":
117 raise SystemExit(main())