Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read Fortran fixed-width formatted text file in Python?

I have a Fortran formatted text file (here is 3 first rows):

00033+3251 A   B       C?      6.96    5.480" 358  9.12 F0V    0.00        2.28s  1.00: 2MASS, dJ=1.3
00033+3251 Aa  Ab  Aab S1,E    0.62    0.273m   0  9.28 F0V   11.28 K2     1.68*  0.32* SB 1469
00033+3251 Aab Ac  A   E*      4.26    0.076"   0  9.12 F0V    0.00        2.00s  0.28* 2008MNRAS.383.1506

and the file format description:

--------------------------------------------------------------------------------
Bytes Format Units   Label     Explanations
--------------------------------------------------------------------------------
 1- 10  A10   ---     WDS       WDS(J2000)
12- 14  A3    ---     Primary   Designation of the primary
16- 18  A3    ---     Secondary Designation of the secondary component
20- 22  A3    ---     Parent    Designation of the parent (1)
24- 29  A6    ---     Type      Observing technique/status (2)
31- 35  F5.2  d       logP      ? Logarithm (10) of period in days
37- 44  F8.3  ---     Sep       Separation or axis
    45  A1    ---     x_Sep     ['"m] Units of sep. (',",m)
47- 49  I3    deg     PA        Position angle
51- 55  F5.2  mag     Vmag1     V-magnitude of the primary
57- 61  A5    ---     SP1       Spectral type of the primary
63- 67  F5.2  mag     Vmag2     V-magnitude of the secondary
69- 73  A5    ---     SP2       Spectral type of the secondary
75- 79  F5.2  solMass Mass1     Mass of the primary
    80  A1    ---     MCode1    Mass estimation code for primary (3)
82- 86  F5.2  solMass Mass2     Mass of the secondary
    87  A1    ---     MCode2    Mass estimation code for secondary (3)
89-108  A20   ---     Rem       Remark

How to read my file in Python. I have found only read_fwf function from the pandas library.

import pandas as pd

filename = 'systems'
columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),(36,44),(45,45),(46,49),(50,55),(56,61),(62,67),(68,73),(74,79),(80,80),(81,86),(87,87),(88,108))
data = pd.read_fwf(filename, colspecs = columns, header=None)

Is this the only possible and effective way? I hope I can do this without pandas. Have you any suggestions?

like image 207
drastega Avatar asked May 20 '14 15:05

drastega


2 Answers

     columns = ((0,10),(11,14),(15,18),(19,22),(23,29),(30,35),
               (36,44),(44,45),(46,49),(50,55),(56,61),(62,67),
               (68,73),(74,79),(79,80),(81,86),(86,87),(88,108))
     string=file.readline()
     dataline = [ string[c[0]:c[1]] for c in columns ]

note the column indices are (startbyte-1,endbyte) so that a single character field is eg: (44,45)

this leaves you with a list of strings. You probably want to do conversion to floats, integers, etc. There are a number of questions here on that topic..

like image 95
agentp Avatar answered Sep 27 '22 22:09

agentp


This type of file can be read with astropy tables. The header you show looks a lot like a CDS-formatted ascii table, which has a specific reader implemented for it:

http://astropy.readthedocs.org/en/latest/api/astropy.io.ascii.Cds.html#astropy.io.ascii.Cds

like image 34
keflavich Avatar answered Sep 27 '22 21:09

keflavich