A Thursday 29 May 2008, Milos Ilak escrigué:
> Hi all,
>
> I apologize if this has been discussed, but I could not find any
> information in the archives. I am creating HDF5 files with 3-D arrays
> in Fortran 90, and I need to read them in both Python and MATLAB.
> While MATLAB recognizes the correct dimensions of the arrays,
> PyTables gets them backwards (i.e. (x,y,z) in Fortran becomes (z,y,x)
> when PyTables reads it). I know that this is due to the fact that the
> order in which Fortran stores arrays is different than that of
> Python, C or MATLAB, and I couldn't determine how exactly MATLAB
> 'knows' that Fortran arrays are being read.

Well, it is easy: because MATLAB writes and reads arrays in *Fortran* 
order.  So, if you write your arrays with Fortran, then you are not 
going to have any problem to read them in the correct order from 
MATLAB.  However, as PyTables uses a C API to access HDF5 files, and as 
C follows a different order for matrices in memory, you will get 
inverted dimensions for your Fortran created files (as it is the case).

> I have tried using the 
>
> 'isfortran' command in numpy, but I get the following error:
> >>> hh5f.root
>
> / (RootGroup) ''
>   children := ['eta' (Array), 'u' (Array), 'w' (Array), 'v' (Array),
> 'y' (Array), 'x' (Array), 'z' (Array)]
>
> >>> hh5f.root.v
>
> /v (Array(16L, 33L, 32L)) ''
>   atom := Float64Atom(shape=(), dflt=0.0)
>   maindim := 0
>   flavor := 'numpy'
>   byteorder := 'little'
>   chunkshape := None
>
> >>> numpy.isfortran(hh5f.root.v)
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "/sw/lib/python2.5/site-packages/numpy/core/numeric.py", line
> 184, in isfortran
>     return a.flags.fnc
> AttributeError: 'Array' object has no attribute 'flags'
>
> It seems like there is perhaps some kind of flag I should add when
> writing in Fortran to indicate that the array is in Fortran order,
> but MATLAB somehow seems to know that anyway. Any advice would be
> greatly appreciated.

You are applying the numpy isfortran() function to a pytables Array and 
not a numpy object.  The correct call would be:

>>> numpy.isfortran(hh5f.root.v[:])

because the result of reading a pytables Array is a numpy object. 

However, this won't tell you anything about the actual order (Fortran or 
C) in which the array was written because this meta-information is not 
saved anywhere in the file (apparently HDF5 does not support this yet).

So, unless you want to provide this info yourself by using, say, an HDF5 
attribute, your best bet is to *deduce* the ordering by knowing that 
the file comes from a Fortran or a C program and *transpose* manually 
your arrays after reading them (if you need to).

Hope this helps,

-- 
Francesc Alted
Freelance developer
Tel +34-964-282-249

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to