如何在netcdf python中传递复杂的参数

时间:2014-05-29 12:54:25

标签: python optimization numpy netcdf

我想使用python读取netcdf文件。该文件包含double格式的netcdf变量。

我知道这个数量应该很复杂,我知道最后一个参数总是2个数字(真实和im)。

我想以有效的方式读取nedcdf变量并将其分配给复杂的python / numpy变量。

目前,我有以下不正常的计划:

import numpy as N
self.EIG2D = N.zeros((self.nkpt,self.nband,3,self.natom,3,self.natom),dtype=complex)
EIG2Dtmp = root.variables['second_derivative_eigenenergies'][:,:,:,:,:,:,:] #number_of_atoms, 
                                   # number_of_cartesian_directions, number_of_atoms, number_of_cartesian_directions,
                                   # number_of_kpoints, product_mband_nsppol, cplex
for ikpt in N.arange(nkpt):
  for iband in N.arange(nband):
    for icart in N.arange(3):
      for iatom in N.arange(natom):
        for jcart in N.arange(0,3):
          for jatom in N.arange(natom):
            self.EIG2D[ikpt,iband,icart,iatom,jcart,jatom] = complex(EIG2Dtmp[iatom,icart,jatom,jcart,ikpt,iband,0],\
                                                                     EIG2Dtmp[iatom,icart,jatom,jcart,ikpt,iband,1])

如何提高效率?

提前谢谢你,

塞缪尔。

1 个答案:

答案 0 :(得分:2)

感谢Spencer Hill,我的解决方案是

self.EIG2D = numpy.vectorize(complex)(EIG2Dtmp[...,0], EIG2Dtmp[...,1])

您还可以参考Numpy: Creating a complex array from 2 real ones?