Created
November 18, 2017 21:15
-
-
Save mitkof6/03c887ccc867e1c8976694459a34edc3 to your computer and use it in GitHub Desktop.
A python function for reading OpenSim .sto files.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
def readMotionFile(filename): | |
""" Reads OpenSim .sto files. | |
Parameters | |
---------- | |
filename: absolute path to the .sto file | |
Returns | |
------- | |
header: the header of the .sto | |
labels: the labels of the columns | |
data: an array of the data | |
""" | |
if not os.path.exists(filename): | |
print('file do not exists') | |
file_id = open(filename, 'r') | |
# read header | |
next_line = file_id.readline() | |
header = [next_line] | |
nc = 0 | |
nr = 0 | |
while not 'endheader' in next_line: | |
if 'datacolumns' in next_line: | |
nc = int(next_line[next_line.index(' ') + 1:len(next_line)]) | |
elif 'datarows' in next_line: | |
nr = int(next_line[next_line.index(' ') + 1:len(next_line)]) | |
elif 'nColumns' in next_line: | |
nc = int(next_line[next_line.index('=') + 1:len(next_line)]) | |
elif 'nRows' in next_line: | |
nr = int(next_line[next_line.index('=') + 1:len(next_line)]) | |
next_line = file_id.readline() | |
header.append(next_line) | |
# process column labels | |
next_line = file_id.readline() | |
if next_line.isspace() == True: | |
next_line = file_id.readline() | |
labels = next_line.split() | |
# get data | |
data = [] | |
for i in range(1, nr + 1): | |
d = [float(x) for x in file_id.readline().split()] | |
data.append(d) | |
file_id.close() | |
return header, labels, data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks. How can I extract a single column? from a .sto file