将matlab代码翻译成python,但python是

2024-04-18 07:06:38 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我最近开始使用python,我正在做一个计算风暴露的项目。我已经在matlab中管理了我的代码,它运行得非常快(可以在3分钟内完成),但是在我将代码翻译成python之后,我得到了相同的结果,但是需要3小时才能完成它的工作。我真的需要一只手来检查是什么导致了如此巨大的差异。。。你知道吗

下面是我的python代码。如果有人需要的话,我可以把我的matlab代码发出去。你知道吗

from netCDF4 import Dataset, num2date
import numpy as np
import matplotlib.pyplot as plt
from scipy import interpolate
#import pylab as py
#input data
dem = Dataset('comparearea_fill.nc','r')
lon = np.array(dem.variables['lon'])
lat = np.array(dem.variables['lat'])
DEM = np.array(dem.variables['elevation'])
carea = Dataset('carea.nc','r')
u = np.array(carea.variables['u10'])
v = np.array(carea.variables['v10'])
mu = np.mean(u, axis=0)
mv = np.mean(v, axis=0)
x = np.linspace(1,21,21)
y = np.linspace(1,11,11)

newu = interpolate.interp2d(x, y, mu, kind='cubic')
newv = interpolate.interp2d(x, y, mv, kind='cubic')

spu = newu(lon,lat)
spv = newv(lon,lat)

A = np.zeros((4951,9451))
B = np.zeros((4951,9451))
for i in range(100,4850):
    for j in range(100,9350):
        for n in range(20):
            A[i,j] = (DEM[i,j]-np.max(DEM[np.floor(n*spv[i,j]).astype(int),j-np.floor(n*spu[i,j]).astype(int)]))/DEM[i,j]
            if  A[i,j] < 0:
                A[i,j] = 0
            B[i,j] = (DEM[i,j]-np.max(DEM[i-np.ceil(n*spv[i,j]).astype(int),j-np.ceil(n*spu[i,j]).astype(int)]))/DEM[i,j]
            if B[i,j] < 0:
               B[i,j] = 0

C = A+B
plt.contourf(lon,lat,C); plt.colorbar()

这里的mu和mv是u和v风的月平均值,而spu和spv是拟合dem数据集分辨率的样条插值u和v风。你知道吗


Tags: 代码importnpvariablesarraydatasetintlon