将python内插代码转换为fortran

时间:2019-06-14 16:12:03

标签: fortran gfortran

我用python编写了这个简单的插值代码,但是我想将其转换为fortran,但我不确定该怎么做。我需要转换代码或使用fortran包装器(如果存在)的帮助。

import numpy as np
from scipy.interpolate import interp2d
import matplotlib.pyplot as plt
import pandas as pd
import time

if __name__=="__main__":
    start = time.time()

    LAMBDA = int(input("Enter the value of Lambda: "))
    ZETA = int(input("Enter the value of zeta: "))

    Lambda = np.array([140, 200, 260, 380, 440, 500])       #Grid of lambda and zeta
    Zeta = np.array([40, 60, 80, 120, 140, 160])#

    dfs = []                         #Reading in the cl files by their name (lambda and zeta)
    for i in Lambda:
        for j in Zeta:
            data_files = pd.read_csv("4e3_2048_%s_%s_3e9.ksz_cl.txt" %(i, j), sep = ' ', header = None)
            data_files.columns = ['a', 'b', 'c']
            dfs.append(data_files)
#print len(dfs)

    cls = []

    for i in range(len(dfs[0])):                          #Taking the first item in the 'c' column for all the files. This is to rearange the files since we are taking each slice of the cube by the z-axis(cl index)
        row = []
        for df in dfs:
            row.append(df['c'][i])
            cls.append(row)
#print cls[1]

    new_cls_list=[]                                   #Creating a list of 6x6. This is the Z axis. It should have len(x)=len(y)=len(z). (Format for interp2d)
    for j in range(len(cls)):
        new_cls_list_1=[]
        k=0
        while k<len(cls[j]):
            new_cls_list_1.append(cls[j][k:k+6])
            k+=6
        new_cls_list.append(new_cls_list_1)

    cl_slices=[]                                                            #all the cl slices are in this list
    for s in range(len(new_cls_list)):
        f = interp2d(Lambda, Zeta, new_cls_list[s], kind='quintic')
        cl_interpolation = f(LAMBDA, ZETA)
        cl_slices.append(cl_interpolation)

    cl_combine = []

    for h in range(len(cl_slices[0])):                        #combining the slices to get the full cls array
        cl = []
        for c in cl_slices:
            cl.append(c[h])
        cl_combine.append(cl)

    cl_combine=np.array(cl_combine)



    end = time.time()

此代码需要集成到一个已经存在的fortran代码中以进行分析,这就是为什么我需要在fortran而不是python中进行插值的原因。

0 个答案:

没有答案