text: import re
  import numpy as np
  
  
  def readFloat(name):
      f = open(name, 'rb')
  
      if(f.readline().decode("utf-8"))  != 'float\n':
          raise Exception('float file %s did not contain <float> keyword' % name)
  
      dim = int(f.readline())
  
      dims = []
      count = 1
      for i in range(0, dim):
          d = int(f.readline())
          dims.append(d)
          count *= d
  
      dims = list(reversed(dims))
  
      data = np.fromfile(f, np.float32, count).reshape(dims)
      if dim > 2:
          data = np.transpose(data, (2, 1, 0))
          data = np.transpose(data, (1, 0, 2))
  
      return data
  
  
  def writeFloat(name, data):
      f = open(name, 'wb')
  
      dim=len(data.shape)
      if dim>3:
          raise Exception('bad float file dimension: %d' % dim)
  
      f.write(('float\n').encode('ascii'))
      f.write(('%d\n' % dim).encode('ascii'))
  
      if dim == 1:
          f.write(('%d\n' % data.shape[0]).encode('ascii'))
      else:
          f.write(('%d\n' % data.shape[1]).encode('ascii'))
          f.write(('%d\n' % data.shape[0]).encode('ascii'))
          for i in range(2, dim):
              f.write(('%d\n' % data.shape[i]).encode('ascii'))
  
      data = data.astype(np.float32)
      if dim==2:
          data.tofile(f)
  
      else:
          np.transpose(data, (2, 0, 1)).tofile(f)
  
  
  def readPFM(file):
      file = open(file, 'rb')
  
      color = None
      width = None
      height = None
      scale = None
      endian = None
  
      header = file.readline().rstrip()
      if header.decode("ascii") == 'PF':
          color = True
      elif header.decode("ascii") == 'Pf':
          color = False
      else:
          raise Exception('Not a PFM file.')
  
      dim_match = re.match(r'^(\d+)\s(\d+)\s$', file.readline().decode("ascii"))
      if dim_match:
          width, height = list(map(int, dim_match.groups()))
      else:
          raise Exception('Malformed PFM header.')
  
      scale = float(file.readline().decode("ascii").rstrip())
      if scale < 0: # little-endian
          endian = '<'
          scale = -scale
      else:
          endian = '>' # big-endian
  
      data = np.fromfile(file, endian + 'f')
      shape = (height, width, 3) if color else (height, width)
  
      data = np.reshape(data, shape)
      data = np.flipud(data)
      return data, scale
  
  
  def readFlow(name):
      if name.endswith('.pfm') or name.endswith('.PFM'):
          return readPFM(name)[0][:,:,0:2]
  
      f = open(name, 'rb')
  
      header = f.read(4)
      if header.decode("utf-8") != 'PIEH':
          raise Exception('Flow file header does not contain PIEH')
  
      width = np.fromfile(f, np.int32, 1).squeeze()
      height = np.fromfile(f, np.int32, 1).squeeze()
  
      flow = np.fromfile(f, np.float32, width * height * 2).reshape((height, width, 2))
  
      return flow.astype(np.float32)
  
  
  def writeFlow(name, flow):
      f = open(name, 'wb')
      f.write('PIEH'.encode('utf-8'))
      np.array([flow.shape[1], flow.shape[0]], dtype=np.int32).tofile(f)
      flow = flow.astype(np.float32)
      flow.tofile(f)
  