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

Olivier Delalleau shish at keba.be
Wed May 18 20:50:14 EDT 2011


Seems like something the following would work (it's not particularly
efficient nor elegant though, and there may be some stupid bug since I
didn't actually try it). Also, there may be some more appropriate numpy
structure to handle named columns (the example below computes separately a
basic numpy array, and the map from a colunn's name to its index).

def to_float(x):
  try:
    return float(x)
  except ValueError:
    assert x == "POS-VELOCITY"
    return numpy.nan

data = []
for line_idx, line in enumerate(open("data.txt").
readlines()):
  if line_idx == 0:
     # header
     field = dict((field_name, col_idx) for col_idx, field_name in
enumerate(line.split()))
  else:
    # data line
    data.append(map(to_float, line.split())
data = numpy.array(data)
# Then to get a single column...
col_X = data[:, field['X']]

-=- Olivier


2011/5/18 Aradenatorix Veckhom Vacelaevus <aradnix at gmail.com>

> Hi everybody:
>
> 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
>     4  .600E+01  .000E+00  .000E+00  .200E+01  .200E+01  .200E+01
> 249984468.929
>     5  .800E+01  .000E+00  .000E+00  .200E+01  .200E+01  .200E+01
> 224851296.708
>
> .
>
> .
>
> .
>
> 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.
>
> Aradnix!
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110518/7cc3fc5f/attachment.html>


More information about the NumPy-Discussion mailing list