title: Best practices with reading and operating on fortran ordered arrays with numpy
body: <p>I'm reading ascii and binary files that all specify 3 dimensional arrays in fortran order.  I want to perform some arbitrary manipulations on these arrays, then export them to the same ascii or binary format.</p>
  
  <p>I'm confused on the best ways to deal with these arrays in my library.  My current design seems prone to error because I have to keep reshaping things from the default C order if any new array is created.</p>
  
  <p><strong>Current design:</strong></p>
  
  <p>I have a few functions that read these files and return numpy arrays.  The read functions all behave in a similar way and essentially read in the data and return something like:</p>
  
  <p><code>return array.reshape((i, j, k), order='F')</code></p>
  
  <p>The way I understand it, I'm returning a view for fortran order onto the original array.</p>
  
  <p>My code assumes all the arrays are in fortran order.  This means any new operations that might create a new array I make sure to use <code>reshape</code> to convert it back to fortran order.</p>
  
  <p>This seems very error-prone because I have to pay close attention to any operation that creates a new array and make sure to reshape it into fortran order since the default is usually C order.</p>
  
  <p>I later might have to export these arrays to binary or ascii again and need to maintain the fortran ordering.  So, I use <code>numpy.nditer</code> to write each element out in the fortran order.</p>
  
  <p><strong>Concerns:</strong></p>
  
  <ul>
  <li><p>The current approach seems very error-prone since I typically think in C order.  I'm afraid that I'll always be getting bitten by missing calls to <code>reshape</code> that forces things in C order.</p>
  
  <ul>
  <li>I'd like to not have to worry about the ordering of the array elements except when reading the input files or writing the data to the output files.</li>
  </ul></li>
  <li><p>The current approach seems messy because the indexes can be interpreted different ways and things can get confusing.</p>
  
  <ul>
  <li>When dealing with fortran arrays the tuple ordering for indexes is backwards, right?</li>
  <li>So, <code>x[(1, 2, 3)]</code> for a fortran array means k = 1, j = 2, and i = 3 whereas <code>x[(1, 2, 3)]</code> for a C order array means k = 3, j = 2, i = 1 correct?</li>
  <li>This means that me and users of my library must always think of indexes in (k, j, i) order, not what we are C/Python programmers typically think in, (i, j, k).</li>
  </ul></li>
  </ul>
  
  <p><strong>Question:</strong></p>
  
  <p>Is there a best practice for doing this type of thing?  In an ideal world I'd like to read in the fortran ordered arrays, then forget about ordering until I export to a file.  However, I'm afraid I'll keep misinterpreting the indexes, etc.</p>
  
  <p>I've read through the only numpy documentation on this that I can find, <a href="http://docs.scipy.org/doc/numpy/reference/internals.html#multidimensional-array-indexing-order-issues" rel="nofollow noreferrer">http://docs.scipy.org/doc/numpy/reference/internals.html#multidimensional-array-indexing-order-issues</a>.  However, the concept still seems as clear as mud to me.  Maybe I just need a different explanation of the numpy docs, <a href="http://docs.scipy.org/doc/numpy/reference/internals.html#multidimensional-array-indexing-order-issues" rel="nofollow noreferrer">http://docs.scipy.org/doc/numpy/reference/internals.html#multidimensional-array-indexing-order-issues</a>.</p>