#! /usr/bin/gawk -f # Last edited on 2020-03-07 22:03:43 by jstolfi # This program performs basic cleanup of the CSV files generated by the # Vicon lock+ human motion capture system installed at the INDC-UFRJ. # Usage: {PROG} [ -v M={NMARKERS} ] {INFILE} > {OUTFILE} # The data is obtained by a system of infrared cameras that track a # certain number {M} of retroreflective spherical markers (about 1 cm # diameter) attached to specific points of the subject's body with # two-sided adhesive tape. The coordinates are obtained at a fast # sampling rate {fsmp}. The Vicon software identifies the markers with # the help of a human operator. # The data file contains two sections. The first section is data # from a force measuring plate, that was not used in this # capture. The second section starts with the word "Trajectories" # alone in one line,vfollowed by one line with the sampling rate {fsmp} (in Hz), # followed by three header, followed by the marker motion data proper. # In this section, there is one line for each measurement frame. # Each line has the indices of the frame and subframe, followed # {3*M} numbers that are the Cartesian {X,Y,Z} coordinates (in mm) # of the {M} markers, in a sequence defined by the Vicon software. # The user may specify the number {M} of markers in the file. If not # specified, it defaults to 20. BEGIN { nframes = 0; skipping = 1; if (M == "") { M = 20; } else { M = M + 0; } } # Remove ASCII CR: // { gsub("\015", "", $0); } # Check for start of second section: /^Trajectories/ { if (! skipping) { data_error("dup 'Trajectories' in file"); } skipping = 0; next; } # Ignore first section: (skipping) { next; } # Reproduce blanks and comment lines: /^[ ]*($|[#])/ { print; next; } # Turn sampling rate into comment: /^[ ]*[0-9]+[ ]*$/ { printf "# sampling_freq = %s\n", $1; next; } # Turn headers into comments: /^[,]*[A-Za-z]/ { printf "# %s\n", $0; next; } # Map commas to blanks: // { gsub("[,]", " ", $0); } # Process data frames: /[0-9]/ { if (NF != 2 * 3*M) { data_error(sprintf("bad NF = %d", NF)); } nframes++; fr = $1; sf = $2; printf "%4d %4d", fr, sf; for (k = 0; k < M; k++) { X = $(3+3*k); Y = $(4+3*k); Z = $(5+3*k); printf " %4.0f %4.0f %4.0f", X, Y, Z; } printf "\n"; next; } # Catch other errors: //{ data_error(sprintf("bad format = [%s]", $0)); } END { if (skipping) { data_error(sprintf("'Trajectories' not found", $0)); } printf "%d frames copied\n", nframes> "/dev/stderr"; } function data_error(msg) { printf "%s:%d: ** %s\n", FILENAME, FNR, msg > "/dev/stderr"; exit(1); }