Mustafa
Mustafa

Reputation: 13

Converting data from fortran to python

I'm new to programming and now I'm trying to rewrite the Fortran code in Python script.

In original fortran code, it declares real number as:

Begin
c----
      data (A(0,i), i=0,17)   
     &  /
     &    9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
     &    7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
     &    2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0
     &  /
c----
c---- End

Here how can I convert this A_ul(0,i) in python code? And how can i rewrite A(0,i) to calculate :

B = (p**2)*(c**3)*A_ul(0,i)

Such that p and c are constants

Upvotes: 1

Views: 452

Answers (1)

DarrylG
DarrylG

Reputation: 17156

Two Choices

Option 1: Using Normal Python Lists

Python List Comprehension Tutorial

A_ul = [9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
        7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
        2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0]

# some values for p, c (assuming scalars)
p = 2.95
c = 3.41

# generate computation of scalar with array using list comprehension
B = [(p**2)*(c**3)*x for x in A_ul] 

print(B)

Out

[328713655.56773156, 488619080.70954007, 417534666.42552507, 266877116.54008356, 139753338.76226252, 63872451.863937765, 27412358.595738605, 11670266.461579552, 4948303.402100851, 2008652.308481803, 750182.1196769351, 255075.72348904808, 81056.93648211227, 25400.60065750653, 8298.932832672626, 2906.524376282808, 1076.6183134278003, 405.80228736894003]

Option 2: Using Python Numpy Arrays

Numpy allows easy array and matrix expressions in Python (i.e. an alternative to Matlab)

Numpy tutorial

import numpy as np

# A_ul as Numpy array
A_ul = np.array([9.526e5, 1.416e6, 1.210e6, 7.734e5, 4.050e5, 1.851e5,
        7.944e4, 3.382e4, 1.434e4, 5.821e3, 2.174e3, 7.392e2,
        2.349e2, 7.361e1, 2.405e1, 8.423e0, 3.120e0, 1.176e0])
# some values for p, c (assuming scalars)
p = 2.95
c = 3.41

# numpy understands multiplying scalars by arrays
B = (p**2)*(c**3)*A_ul 

print(B)

Out

[3.28713656e+08 4.88619081e+08 4.17534666e+08 2.66877117e+08
 1.39753339e+08 6.38724519e+07 2.74123586e+07 1.16702665e+07
 4.94830340e+06 2.00865231e+06 7.50182120e+05 2.55075723e+05
 8.10569365e+04 2.54006007e+04 8.29893283e+03 2.90652438e+03
 1.07661831e+03 4.05802287e+02]

Upvotes: 1

Related Questions