Single Motion View#
Open the app motion library (no project file), print one motion’s properties and tables, then save scaled copies to disk. Scaled files are not added to the motion library.
1"""
2Inspect one ground motion (Single Motion View) and write scaled copies to disk.
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_single_motion_view.py --motion ChiChi
9 python examples/example_single_motion_view.py --port 60058 --motion ChiChi --output-dir "D:\\out"
10
11Scaling writes a new .txt file. It does not add that file to the motion library.
12"""
13
14from __future__ import annotations
15
16import argparse
17import os
18import sys
19
20from rsseismic import RSSeismicApplication
21from rsseismic._client import DEFAULT_SCRIPTING_PORT
22
23
24def _print_properties(title: str, properties) -> None:
25 print(title)
26 print(f" Time step {properties.timeStep} {properties.timeStepUnit}")
27 print(f" Nyquist frequency {properties.nyquistFrequency} {properties.frequencyUnit}")
28 print(f" PGA {properties.pga} {properties.accelerationUnit}")
29 print(f" PGV {properties.pgv} {properties.velocityUnit}")
30 print(f" PGD {properties.pgd} {properties.displacementUnit}")
31 print(f" Number of points {properties.numPoints}")
32
33
34def run_single_motion_view_example(
35 motion_name: str,
36 port: int = DEFAULT_SCRIPTING_PORT,
37 output_dir: str = "",
38) -> None:
39 if not motion_name.strip():
40 raise ValueError("A motion name is required (file name without .txt).")
41
42 app = RSSeismicApplication(port=port)
43 try:
44 app.ping()
45 motion = app.MotionLibrary.getMotion(motion_name.strip())
46
47 original = motion.getProperties()
48 print(f"Using motion: {motion.name}")
49 _print_properties("Original properties", original)
50 if original.numPoints <= 0:
51 raise RuntimeError(
52 f"Motion '{motion.name}' has no time-history points. "
53 "Check that this name exists in the Motion Manager folders."
54 )
55
56 time_history = motion.getTimeHistoryData()
57 print("Time-history columns:", list(time_history.columns))
58 print(time_history.head())
59
60 fourier, spectral_acceleration = motion.getSpectralData()
61 print("Fourier columns:", list(fourier.columns))
62 print("Spectral acceleration columns:", list(spectral_acceleration.columns))
63
64 if not output_dir:
65 output_dir = os.path.join(os.getcwd(), "scaled_motions_example")
66 output_dir = os.path.abspath(output_dir)
67 os.makedirs(output_dir, exist_ok=True)
68
69 scaled_to_g_path = os.path.join(output_dir, f"{motion.name}_to_0p30g.txt")
70 scaled_by_path = os.path.join(output_dir, f"{motion.name}_x1p20.txt")
71
72 to_g = motion.scaleToG(0.30, scaled_to_g_path, overwrite=True)
73 print(f"\nSaved scale-to-g file: {to_g.filePath}")
74 print(f"Applied scale factor: {to_g.appliedScaleFactor}")
75 _print_properties("Properties after scale to 0.30 g", to_g.properties)
76
77 by_factor = motion.scaleBy(1.20, scaled_by_path, overwrite=True)
78 print(f"\nSaved scale-by file: {by_factor.filePath}")
79 print(f"Applied scale factor: {by_factor.appliedScaleFactor}")
80 _print_properties("Properties after scale by 1.20", by_factor.properties)
81
82 after_scale = motion.getProperties()
83 if after_scale.pga != original.pga:
84 raise RuntimeError(
85 "Scaling should write a new file only; the original library "
86 "motion PGA changed unexpectedly."
87 )
88
89 if not os.path.isfile(to_g.filePath) or not os.path.isfile(by_factor.filePath):
90 raise RuntimeError("Expected scaled motion files were not written.")
91
92 print("\nOriginal library motion is unchanged.")
93 print("Scaled files are on disk only; they are not in the motion library.")
94 finally:
95 app.close()
96
97
98def parse_args() -> argparse.Namespace:
99 parser = argparse.ArgumentParser(
100 description="Print Single Motion View properties and tables, then save scaled copies."
101 )
102 parser.add_argument(
103 "--motion",
104 "-m",
105 required=True,
106 help="Motion name (file name without .txt) from Motion Manager folders.",
107 )
108 parser.add_argument(
109 "--port",
110 "-p",
111 type=int,
112 default=int(os.environ.get("RSSEISMIC_SCRIPTING_PORT", DEFAULT_SCRIPTING_PORT)),
113 help=f"gRPC port (default: {DEFAULT_SCRIPTING_PORT}, or RSSEISMIC_SCRIPTING_PORT)",
114 )
115 parser.add_argument(
116 "--output-dir",
117 "-o",
118 default="",
119 help="Folder for scaled files. Default: ./scaled_motions_example",
120 )
121 return parser.parse_args()
122
123
124def main() -> int:
125 args = parse_args()
126 try:
127 run_single_motion_view_example(args.motion, port=args.port, output_dir=args.output_dir)
128 except Exception as ex:
129 print(f"Example failed: {ex}", file=sys.stderr)
130 return 1
131 print("Single Motion View example finished.")
132 return 0
133
134
135if __name__ == "__main__":
136 raise SystemExit(main())