Convert and delete library motions#

Convert a text or PEER AT2 file into the Motion Manager library, or delete motions by name. Convert adds the motion to the library (unlike scale/baseline/timestep, which write external files only).

outputDirectory must be a folder listed in RSSeismic Preferences → Motions.

  1"""
  2Convert text or PEER AT2 motion files into the Motion Manager library.
  3
  4Requires a running RSSeismic scripting server. No project file is needed.
  5``outputDirectory`` must be one of the Motion Manager preference folders
  6(RSSeismic Preferences → Motions).
  7
  8Unlike scale/baseline/timestep RPCs, convert adds the motion to the library
  9(and updates Motion Manager if it is open).
 10
 11  python examples/example_convert_motion.py --output-dir "D:\\Motions" --text-file "D:\\in\\raw.txt" --name Kobe
 12  python examples/example_convert_motion.py --output-dir "D:\\Motions" --at2-file "D:\\in\\record.AT2" --name ChiChi
 13  python examples/example_convert_motion.py --output-dir "D:\\Motions" --delete Kobe OldMotion
 14"""
 15
 16from __future__ import annotations
 17
 18import argparse
 19import os
 20import sys
 21
 22from rsseismic import (
 23    RSSeismicApplication,
 24    MotionAccelerationUnit,
 25    MotionConvertDelimiter,
 26    MotionConvertLayout,
 27)
 28from rsseismic._client import DEFAULT_SCRIPTING_PORT
 29
 30
 31def _print_convert_result(label: str, result) -> None:
 32    props = result.properties
 33    print(f"\n{label}")
 34    print(f"  Motion name   {result.motionName}")
 35    print(f"  Output file   {result.filePath}")
 36    print(f"  Time step     {props.timeStep} {props.timeStepUnit}")
 37    print(f"  PGA           {props.pga} {props.accelerationUnit}")
 38    print(f"  Num points    {props.numPoints}")
 39
 40
 41def run_convert_example(
 42    output_dir: str,
 43    port: int = DEFAULT_SCRIPTING_PORT,
 44    text_file: str = "",
 45    at2_file: str = "",
 46    motion_name: str = "",
 47    time_step: float = 0.02,
 48    overwrite: bool = False,
 49    delete_names: list[str] | None = None,
 50) -> None:
 51    output_dir = os.path.abspath(output_dir)
 52    if not os.path.isdir(output_dir):
 53        raise ValueError(f"output-dir is not a folder: {output_dir}")
 54
 55    app = RSSeismicApplication(port=port)
 56    try:
 57        app.ping()
 58        library = app.MotionLibrary
 59
 60        if delete_names:
 61            deleted = library.deleteMotions(delete_names)
 62            print(f"Deleted: {deleted.deletedMotionNames}")
 63            if deleted.failedMotionNames:
 64                print(f"Failed:  {deleted.failedMotionNames}")
 65
 66        if text_file:
 67            if not motion_name.strip():
 68                raise ValueError("--name is required with --text-file")
 69            result = library.convertMotionText(
 70                sourceFilePath=os.path.abspath(text_file),
 71                motionName=motion_name.strip(),
 72                outputDirectory=output_dir,
 73                accelerationUnit=MotionAccelerationUnit.G,
 74                layout=MotionConvertLayout.ACCELERATION_ONLY,
 75                delimiter=MotionConvertDelimiter.SPACE,
 76                timeStep=time_step,
 77                hasTimeStep=True,
 78                accelerationColumn=2,
 79                timeColumn=1,
 80                firstLine=1,
 81                lastLine=0,
 82                overwrite=overwrite,
 83            )
 84            _print_convert_result("Text convert result", result)
 85
 86        if at2_file:
 87            at2_name = motion_name.strip() or os.path.splitext(os.path.basename(at2_file))[0]
 88            result = library.convertMotionAT2(
 89                sourceFilePath=os.path.abspath(at2_file),
 90                motionName=at2_name,
 91                outputDirectory=output_dir,
 92                overwrite=overwrite,
 93                # accelerationUnit=None → auto-detect from AT2 header
 94            )
 95            _print_convert_result("AT2 convert result", result)
 96
 97        if not delete_names and not text_file and not at2_file:
 98            raise ValueError("Provide --text-file, --at2-file, and/or --delete")
 99    finally:
100        app.close()
101
102
103def parse_args() -> argparse.Namespace:
104    parser = argparse.ArgumentParser(
105        description="Convert or delete Motion Manager library motions."
106    )
107    parser.add_argument(
108        "--output-dir",
109        "-o",
110        required=True,
111        help="Motion Manager preference folder.",
112    )
113    parser.add_argument("--text-file", help="Plain-text motion file to convert.")
114    parser.add_argument("--at2-file", help="PEER AT2 file to convert.")
115    parser.add_argument(
116        "--name",
117        "-n",
118        default="",
119        help="Output motion name (without .txt). Required for --text-file.",
120    )
121    parser.add_argument(
122        "--time-step",
123        type=float,
124        default=0.02,
125        help="Timestep for acceleration-only text files.",
126    )
127    parser.add_argument(
128        "--overwrite",
129        action="store_true",
130        help="Overwrite existing library motion.",
131    )
132    parser.add_argument(
133        "--delete",
134        nargs="+",
135        default=[],
136        help="Motion names to delete from the library.",
137    )
138    parser.add_argument(
139        "--port",
140        "-p",
141        type=int,
142        default=int(os.environ.get("RSSEISMIC_SCRIPTING_PORT", DEFAULT_SCRIPTING_PORT)),
143        help=f"gRPC port (default: {DEFAULT_SCRIPTING_PORT}, or RSSEISMIC_SCRIPTING_PORT)",
144    )
145    return parser.parse_args()
146
147
148def main() -> int:
149    args = parse_args()
150    try:
151        run_convert_example(
152            output_dir=args.output_dir,
153            port=args.port,
154            text_file=args.text_file or "",
155            at2_file=args.at2_file or "",
156            motion_name=args.name,
157            time_step=args.time_step,
158            overwrite=args.overwrite,
159            delete_names=args.delete or None,
160        )
161    except Exception as ex:
162        print(f"Example failed: {ex}", file=sys.stderr)
163        return 1
164    print("\nConvert motion example finished.")
165    return 0
166
167
168if __name__ == "__main__":
169    raise SystemExit(main())