Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Tools/scripts/convert_param_scale.py
Views: 1798
#!/usr/bin/env python31'''2tool to convert parameter names and scales, useful for conversion for cm -> m and cdeg -> deg34this looks for files called *.param or *.parm56example:7Tools/scripts/convert_param_scale.py --scale 0.1 TRIM_ARSPD_CM AIRSPEED_CRUISE8'''910import os11import sys1213from argparse import ArgumentParser1415parser = ArgumentParser(description="parameter conversion tool")1617parser.add_argument("--scale", default=1.0, type=float, help="scale factor")18parser.add_argument("--directory", default=".", help="directory to search")19parser.add_argument("old_name", default=None, help="old parameter name")20parser.add_argument("new_name", default=None, help="new parameter name")2122args = parser.parse_args()2324def process_file(fname):25needs_write = False26with open(fname, "r") as f:27lines = f.readlines()28for i in range(len(lines)):29line = lines[i]30if line.startswith(args.old_name):31a = line.split()32if len(a) == 2:33sep = ' '34else:35a = line.split(',')36if len(a) == 2:37sep = ','38else:39continue40v = float(a[1])41v *= args.scale42lines[i] = "%s%s%.2f\n" % (args.new_name, sep, v)43needs_write = True44if not needs_write:45return46print("Updating %s" % fname)47with open(fname, "w") as f:48for line in lines:49f.write(line)5051for root, dirs, files in os.walk(args.directory):52for file in files:53if file.endswith(".parm") or file.endswith(".param"):54process_file(os.path.join(root, file))555657