[Numpy-discussion] How can Import DATA from a Fortran file

Derek Homeier derek at astro.physik.uni-goettingen.de
Wed May 18 20:53:08 EDT 2011


On 19.05.2011, at 12:47AM, Aradenatorix Veckhom Vacelaevus wrote:

> I have a file in simple text with information obtained in Fortran  77 and I need to use the data inside for visualize with Mayavi. I was fighting for a while with the VTK simple legacy format. Finally I could run an small example, but now I need to select specific information from that file. Let me explain how is it.
> 
> I have a header of two lines where tell me the number of domains I have, and later the values of the position and pressure
> 
> Later for each domain I have two arrays with 125 rows, the first one has another header with two lines where we can find the name of each variable (column) and the dimension of the array for each coordinate (x, y, z). Later we have the array (125 x 8) where the first column is the number or ID for each point (row) the next three contains the coordinates for the point pnx, pny, pnz the following three the displacement Dnx, Dny, Dnz finally the last column contains the value of the pressure field in each point.
> 
> The second array has only one line as header where specifies the number of points (rows) and the names of each variable (column). This array has the dimension 125 x 11, as in the first array the first column has the number of row, the following columns contain the values of the alpha elements used for the Finite Element Analysis from where I have to find the velocity (vx = 0.5 (alpha1 + alpha2), vy = 0.5 (alpha3 + alpha4), vz = 0.5 (alpha5 + alpha6)) finally the last column is a string that says pos-velocity so we can forget it.
> 
> In an schematic form:
> 
> Header:
> 5 NUMBER OF SUBDOMAINS
>   4852108.558056722000000   4.858791656580212E+008  POSITION-PRESSURE
> 
> First array
>  10.00     10.00     10.00     LX, LY, LZ, ... #header
>          5         5         5      LS,MS,NS, - NEE,X,Y,Z,DX,DY,DZ,LAMBDA (NEE) #header
>     1  .000E+00  .000E+00  .000E+00  .200E+01  .200E+01  .200E+01       485879165.658 #125 rows with 8 columns
>     2  .200E+01  .000E+00  .000E+00  .200E+01  .200E+01  .200E+01       362994604.232
>     3  .400E+01  .000E+00  .000E+00  .200E+01  .200E+01  .200E+01       287889668.714
> .
> .
> .
> 125  .800E+01  .800E+01  .800E+01  .200E+01  .200E+01  .200E+01       192572200.800
> 
> Second array:
> 125 L, X, Y, Z, ALPHA(I1), ALPHA(I2), ALPHA(J1), ALPHA(J2), ALPHA(K1), ALPHA(K2), ... #header
>      1     1.000     1.000     1.000  .000E+00  .845E-04  .000E+00  .826E-04  .000E+00  .828E-04  POS-VELOCITY
>      2     3.000     1.000     1.000  .845E-04  .308E-04  .000E+00  .267E-04  .000E+00  .269E-04  POS-VELOCITY
>      3     5.000     1.000     1.000  .308E-04  .177E-04  .000E+00  .633E-05  .000E+00  .666E-05  POS-VELOCITY
>      4     7.000     1.000     1.000  .177E-04  .122E-04  .000E+00  .246E-05  .000E+00  .297E-05  POS-VELOCITY
>      5     9.000     1.000     1.000  .122E-04  .908E-05  .000E+00  .114E-05  .000E+00  .183E-05  POS-VELOCITY
> .
> .
> .
>  125     9.000     9.000     9.000  .102E-04  .160E-04  .133E-05  .000E+00  .457E-05  .000E+00  POS-VELOCITY
> 
> And both arrays repeat other 4 times, it means I have 5 pairs of arrays, a pair for each domain.
> 
> I want to divide the file in five pieces one for each domain, but what I really need is can manipulate the arrays by columns. I know that numpy is able to import files in many formats, and I want to believe that inside numpy I can easily manipulate an array by columns, but I don't how, so all this explanation is for ask your helping and find a way to can get from this arrays the information I need to write a new file with the info for export as vtk file and can visualize in Mayavi2 (do you have a better idea or way for visualize this?). Thanks for your helping and your time.
> 
You are right, numpy can manipulate the arrays probably in any way you need once 
they are read in; but I don't think there are tools to handle files with a complex structure 
like that right out of the box. You'll probably need to write some code for parsing the 
file on your own to split the file into the 10 data blocks (by searching for the header lines), 
but once that's done, there are a number of utilities to read the data columns into numpy 
arrays - specifically, you want to check the loadtxt(), fromstring() and genfromtxt() functions. 

As an example, consider you read in your file into a list of text strings by 
lines = open('your_file', 'r').readlines() 
and then find the first line of your data block, then 

array1 = np.loadtxt(lines[start_line:start_line+128]) would read your first block of data, and so on...

You might also want to check out the asciitable package at 
http://cxc.harvard.edu/contrib/asciitable/
which provides a framework to automatically create structured or record arrays using 
information from the table header (e.g. if you pass it a header line like "L,X,Y,Z,...." 
you'd be able to access the column data as array1['L'] etc.)

HTH,
							Derek




More information about the NumPy-Discussion mailing list