Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.13 KB | None | 0 0
  1. import pandas as pd
  2. import time
  3. import os
  4. import argparse
  5.  
  6.  
  7. def process(logdir, filename):
  8.     chunksize = 1000000
  9.  
  10.     print ('processing {}...'.format(filename))
  11.     for df in pd.read_csv(filename, chunksize=chunksize, iterator=True, #skiprows=skip_idx,
  12.                           names=['namespace', 'one', 'timestamp', 'name', 'four', 'five', 'six', 'value']):
  13.         print ('\tchunk.. - logdir: {}'.format(logdir))
  14.  
  15.         # get relevant columns
  16.         #     0      1      2        3   4  5  6    7
  17.         # namespace, ?, timestamp, name, ?, ?, ?, value
  18.         #foo = '/left_arm_joint_trajectory_controller/command_module/root_body_wrench_d_wx'
  19.         # director,6381427,1527102254571862641,/left_arm_joint_trajectory_controller/command_module/root_body_wrench_d_wx,4,306,1759263548,0
  20.         df = df[['timestamp', 'name', 'value']]
  21.  
  22.         # downsample
  23.         #df = df.iloc[::10, :]
  24.  
  25.         # get unique names to collect relevant signals
  26.         dfs = dict(tuple(df.groupby('name')))
  27.  
  28.         # these are the only values we actually want
  29.         long2short = {'_elmo_thruster_aft_port_actuator_current':'current',
  30.                       '_elmo_thruster_aft_port_actuator_vel_act':'actual_velocity',
  31.                       '_elmo_thruster_aft_port_actuator_vel_des':'desired_velocity',
  32.                       '_elmo_thruster_aft_port_actuator_voltage':'voltage',
  33.                       '_elmo_thruster_aft_star_actuator_motor_temp':'thrust'}
  34.  
  35.         # append to file
  36.         excel_path = os.path.join(logdir, 'parsed.xlsx')
  37.         writer = pd.ExcelWriter(excel_path, engine='xlsxwriter')
  38.         for name in dfs.keys():
  39.             logfilename = name.replace('/','_')
  40.             logfile = os.path.join(logdir, logfilename)
  41.             noname = dfs[name][['timestamp', 'value']]
  42.             # noname.to_csv(logfile)
  43.             if logfilename in long2short:
  44.                 noname.to_excel(writer, sheet_name=long2short[logfilename])
  45.         writer.save()
  46.  
  47. if __name__ == '__main__':
  48.  
  49.     parser = argparse.ArgumentParser(description='Parse Thruster data')
  50.     parser.add_argument('input', help='Directory containing Lumberjack data')
  51.     parser.add_argument('--logdir', default='', help='Where to save the log files to')
  52.     args = parser.parse_args()
  53.  
  54.     #path = '/home/pdinh/thruster/16-07-2018'
  55.     path = args.input
  56.  
  57.     # get list of all the valid logfile names (crap that ends in .gz)
  58.     logfiles = []
  59.     for root, dirs, files in os.walk(path):
  60.         for filename in files:
  61.             if filename.endswith('.gz'):
  62.                 newfile = os.path.join(root, filename)
  63.                 logfiles.append(newfile)
  64.  
  65.     for filename in logfiles:
  66.  
  67.         # get output directory, use same directory where file exists if --logdir parameter not passed in
  68.         dirname = os.path.dirname(filename)
  69.         basename = os.path.basename(dirname)
  70.         logdir = os.path.join(args.logdir, basename)
  71.         if logdir == '':
  72.             logdir = dirname
  73.         if not os.path.exists(logdir):
  74.             os.makedirs(logdir)
  75.  
  76.         # parse the data with pandas and output it
  77.         process(logdir, filename)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement