import sys,os,time,string

#######################################################################
#
# reads amc file into a list of lists
# self.rawdata[][] - first [] indexes frame number starting at 0
#                  - second [] indexes pos/joint value 0 to 61
#
#######################################################################

class amcReader:
  def __init__(self,fname):
    f = open(fname, 'r')
    f.readline()
    f.readline()
    f.readline()

    self.rawdata = []
    self.numFrames = 0
    while (f.readline() != ''):
      f.read(5) # root

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata.append( [float(f.read(countSpace))] )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # lowerback

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # upperback

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # thorax

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # lowerneck

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # upperneck

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(5) # head

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # rclavicle

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(9) # rhumerus

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(8) # rradius

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # rwrist

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # rhand

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(9) # rfingers

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # rthumb

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(10) # lclavicle

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(9) # lhumerus

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(8) # lradius

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # lwrist

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # lhand

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(9) # lfingers

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # lthumb

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # rfemur

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # rtibia

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # rfoot

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # rtoes

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # lfemur

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(7) # ltibia

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # lfoot

      countSpace = 0
      while (f.read(1) != ' '):
        countSpace += 1
      f.seek(-countSpace-1,1)
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      f.read(6) # ltoes

      countSpace = 0
      while (f.read(1) != '\n'):
        countSpace += 1
      f.seek(-countSpace-2,1) # seems like there's 2 for newline
      self.rawdata[self.numFrames].append( float(f.read(countSpace)) )
      f.read(1)

      self.numFrames += 1

    f.close()

  def ret_rawdata(self):
    return self.rawdata

  def ret_numFrames(self):
    return self.numFrames

  def assign_rootpos(self,framenum,x,y,z):
    self.rawdata[framenum-1][0] = x
    self.rawdata[framenum-1][1] = y
    self.rawdata[framenum-1][2] = z

  def assign_rootori(self,framenum,x,y,z):
    self.rawdata[framenum-1][3] = x
    self.rawdata[framenum-1][4] = y
    self.rawdata[framenum-1][5] = z

  def ret_rootpos(self,framenum):
    temp = [0.0,0.0,0.0]
    temp[0] = self.rawdata[framenum-1][0]
    temp[1] = self.rawdata[framenum-1][1]
    temp[2] = self.rawdata[framenum-1][2]
    return temp

  def ret_rootori(self,framenum):
    temp = [0.0,0.0,0.0]
    temp[0] = self.rawdata[framenum-1][3]
    temp[1] = self.rawdata[framenum-1][4]
    temp[2] = self.rawdata[framenum-1][5]
    return temp
