LOADING DATA INTO ARRAYS

Fernando Perez fperez528 at yahoo.com
Wed Jul 9 16:45:38 EDT 2003


satish k.chimakurthi wrote:

> 
> Hi,
> 
>      I am trying to collect the following data in X,Y,Z arrays as following:
>     
>   1.00000000000000        0.00000000000000D+000   0.00000000000000D+000
>   0.932113519778473       0.362166241174114        0.00000000000000D+000
>   0.737671227507627       0.675160099611485        0.00000000000000D+000
>   0.443073128844408       0.896485472551578        0.00000000000000D+000
>    8.83176797852179D-002  0.996092358889152        0.00000000000000D+000
>  -0.145819420809848       0.989311223283493        0.00000000000000D+000
>  -0.391263558087552       0.920278668726310        0.00000000000000D+000
>  -0.625821331717327       0.779966448488364        0.00000000000000D+000
>  -0.822296905793933       0.569058695322129        0.00000000000000D+000
>  -0.953713306870443       0.300717356163965        0.00000000000000D+000

Hi,

you might want to spare yourself some wheel re-inventing:

In [1]: cat data.dat
  1.00000000000000        0.00000000000000D+000   0.00000000000000D+000
  0.932113519778473       0.362166241174114        0.00000000000000D+000
  0.737671227507627       0.675160099611485        0.00000000000000D+000
  0.443073128844408       0.896485472551578        0.00000000000000D+000
   8.83176797852179D-002  0.996092358889152        0.00000000000000D+000
 -0.145819420809848       0.989311223283493        0.00000000000000D+000
 -0.391263558087552       0.920278668726310        0.00000000000000D+000
 -0.625821331717327       0.779966448488364        0.00000000000000D+000
 -0.822296905793933       0.569058695322129        0.00000000000000D+000
 -0.953713306870443       0.300717356163965        0.00000000000000D+000


In [2]: import scipy

In [3]: ,scipy.io.read_array data.dat
------> scipy.io.read_array ("data.dat")
Out[3]:
array([[ 1.        ,  0.        ,  0.        ],
       [ 0.93211352,  0.36216624,  0.        ],
       [ 0.73767123,  0.6751601 ,  0.        ],
       [ 0.44307313,  0.89648547,  0.        ],
       [ 8.83176798,  0.99609236,  0.        ],
       [-0.14581942,  0.98931122,  0.        ],
       [-0.39126356,  0.92027867,  0.        ],
       [-0.62582133,  0.77996645,  0.        ],
       [-0.82229691,  0.5690587 ,  0.        ],
       [-0.95371331,  0.30071736,  0.        ]])

In [4]: data = _

In [5]: data[1,0]
Out[5]: 0.93211351977847301

In [6]: data[4,:]
Out[6]: array([ 8.83176798,  0.99609236,  0.        ])

In [7]: from Numeric import *

In [8]: sin(2*pi*data)
Out[8]:
array([[ -2.44921271e-16,   0.00000000e+00,   0.00000000e+00],
       [ -4.13726328e-01,   7.61766230e-01,   0.00000000e+00],
       [ -9.97001167e-01,  -8.91462758e-01,   0.00000000e+00],
       [  3.50103955e-01,  -6.05505553e-01,   0.00000000e+00],
       [ -8.70901144e-01,  -2.45499665e-02,   0.00000000e+00],
       [ -7.93300122e-01,  -6.71090900e-02,   0.00000000e+00],
       [ -6.31286730e-01,  -4.80218587e-01,   0.00000000e+00],
       [  7.10746430e-01,  -9.82326731e-01,   0.00000000e+00],
       [  8.98588241e-01,  -4.20420374e-01,   0.00000000e+00],
       [  2.86745429e-01,   9.49654034e-01,   0.00000000e+00]])

You just need to install scipy from http://scipy.org.  And you'll get a ton of
real numerical libraries to boot, which from the format of your data (fortran)
I suspect you'll need soon.

Best,

f.




More information about the Python-list mailing list