docs: def _write_image_data(im, id):
      """ Write a numpy array to disk in the form of a .raw and .mhd file.
      The id is the image sequence number (1 or 2). Returns the path of
      the mhd file.
      """
      im = im* (1.0/3000)
      # Create text
      lines = [   "ObjectType = Image",
                  "NDims = <ndim>",
                  "BinaryData = True",
                  "BinaryDataByteOrderMSB = False",
                  "CompressedData = False",
                  #"TransformMatrix = <transmatrix>",
                  "Offset = <origin>",
                  "CenterOfRotation = <centrot>",
                  "ElementSpacing = <sampling>",
                  "DimSize = <shape>",
                  "ElementType = <dtype>",
                  "ElementDataFile = <fname>",
                  "" ]
      text = '\n'.join(lines)
      
      # Determine file names
      tempdir = get_tempdir()
      fname_raw_ = 'im%i.raw' % id
      fname_raw = os.path.join(tempdir, fname_raw_)
      fname_mhd = os.path.join(tempdir, 'im%i.mhd' % id)
      
      # Get shape, sampling and origin
      shape = im.shape
      if hasattr(im, 'sampling'): sampling = im.sampling
      else: sampling = [1 for s in im.shape]
      if hasattr(im, 'origin'): origin = im.origin
      else: origin = [0 for s in im.shape]
      
      # Make all shape stuff in x-y-z order and make it string
      shape = ' '.join([str(s) for s in reversed(shape)])
      sampling = ' '.join([str(s) for s in reversed(sampling)])
      origin = ' '.join([str(s) for s in reversed(origin)])
      
      # Get data type
      dtype_itk = DTYPE_NP2ITK.get(im.dtype.name, None)
      if dtype_itk is None:
          raise ValueError('Cannot convert data of this type: '+ str(im.dtype))
      
      # Set mhd text
      text = text.replace('<fname>', fname_raw_)
      text = text.replace('<ndim>', str(im.ndim))
      text = text.replace('<shape>', shape)
      text = text.replace('<sampling>', sampling)
      text = text.replace('<origin>', origin)
      text = text.replace('<dtype>', dtype_itk)
      text = text.replace('<centrot>', ' '.join(['0' for s in im.shape]))
      if im.ndim==2:
          text = text.replace('<transmatrix>', '1 0 0 1')
      elif im.ndim==3:
          text = text.replace('<transmatrix>', '1 0 0 0 1 0 0 0 1')
      elif im.ndim==4:
          pass # ???
      
      # Write data file
      f = open(fname_raw, 'wb')
      try:
          f.write(im.data)
      finally:
          f.close()
      
      # Write mhd file
      f = open(fname_mhd, 'wb')
      try:
          f.write(text.encode('utf-8'))
      finally:
          f.close()
      
      # Done, return path of mhd file
      return fname_mhd