Guest User

Untitled

a guest
Nov 20th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. import os
  2.  
  3. def readMotionFile(filename):
  4. """ Reads OpenSim .sto files.
  5.  
  6. Parameters
  7. ----------
  8.  
  9. filename: absolute path to the .sto file
  10.  
  11. Returns
  12. -------
  13.  
  14. header: the header of the .sto
  15. labels: the labels of the columns
  16. data: an array of the data
  17.  
  18. """
  19.  
  20. if not os.path.exists(filename):
  21. print('file do not exists')
  22.  
  23. file_id = open(filename, 'r')
  24.  
  25. # read header
  26. next_line = file_id.readline()
  27. header = [next_line]
  28. nc = 0
  29. nr = 0
  30. while not 'endheader' in next_line:
  31. if 'datacolumns' in next_line:
  32. nc = int(next_line[next_line.index(' ') + 1:len(next_line)])
  33. elif 'datarows' in next_line:
  34. nr = int(next_line[next_line.index(' ') + 1:len(next_line)])
  35. elif 'nColumns' in next_line:
  36. nc = int(next_line[next_line.index('=') + 1:len(next_line)])
  37. elif 'nRows' in next_line:
  38. nr = int(next_line[next_line.index('=') + 1:len(next_line)])
  39.  
  40. next_line = file_id.readline()
  41. header.append(next_line)
  42.  
  43. # process column labels
  44. next_line = file_id.readline()
  45. if next_line.isspace() == True:
  46. next_line = file_id.readline()
  47.  
  48. labels = next_line.split()
  49.  
  50. # get data
  51. data = []
  52. for i in range(1, nr + 1):
  53. d = [float(x) for x in file_id.readline().split()]
  54. data.append(d)
  55.  
  56. file_id.close()
  57.  
  58. return header, labels, data
Add Comment
Please, Sign In to add comment