#!/usr/bin/env python3 from pathlib import Path from argparse import ArgumentParser parser = ArgumentParser(description='Loop an LTspice PWL file a given number of times.') parser.add_argument('pwl_file', type=Path, help='Path to PWL file') parser.add_argument('num_loops', type=int, help='Number of times to loop the PWL file') parser.add_argument('--output', "-o", type=Path, help='Path to output file', default=None) args = parser.parse_args() output_file = args.output if args.output else args.pwl_file.parent / (args.pwl_file.stem + '-looped' + args.pwl_file.suffix) with open(args.pwl_file, 'r') as f: input_data = f.readline().lstrip("(").rstrip(")").strip() input_data = input_data.split() time_value_pairs = [] for i in range(0, len(input_data), 2): time = input_data[i] if not time.endswith('m'): if time.isnumeric(): time = f"{int(time) * 1000}m" value = float(input_data[i+1]) time_value_pairs.append((time, value)) for i in range(args.num_loops - 1): last_time = float(time_value_pairs[-1][0][:-1]) for time, value in time_value_pairs[:len(input_data) // 2]: new_time = last_time + float(time[:-1]) time_value_pairs.append((f"{int(new_time) if new_time.is_integer() else new_time}m", value)) with open(output_file, 'w') as f: content = " ".join([f"{time} {int(value) if value.is_integer() else format(value, '.15f').rstrip('0').rstrip('.')}" for time, value in time_value_pairs]) f.write(f"({content})")