So I've been starting to use python recently, and i am working on a project of calculating wind exposure. I've managed my code in matlab and it runs very fast(can be done in 3 minutes), but after I translated my code into python, i am getting the same result but it takes 3hours to finish it's job. I really need a hand on checking what's causing such a huge difference...
So here's my python code. I can give out my matlab code if anyone need it.
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()
here the mu and mv are the monthly average of the u and v wind, while the spu and spv are the spline interpulated u and v wind to fit the resolution of my dem data set.