Skip to content
Advertisement

How to make grid faster in python

I have 3 arrays of the same dimension alt = np.array(186 values), sza = np.array(186 values) and per = np.array(186 values). I need to make a grid with the per values over (alt,sza).

I have tried the following code and it is time-consuming process. Can anyone suggest improvisation for the code.

als = np.arange(150,510,5)
szde = np.arange(90,190,5)

val2 = []                
for h in range(len(als)-1):
    val1=[]
    for h1 in range(len(szde)-1):
        val=[]
        for h2 in range(len(per)):
            if ((als[h+1] >= alt[h2] >= als[h]) and (szde[h1+1] >= sza[h2] >= szde[h1])):  
                val.append(np.array(per[h2]).ravel())
        val1.append(np.array(np.mean(val)))        
    val2.append(val1)      
      
# val2 is gridded data

Let me know if any clarification is needed.

Thank You.

Advertisement

Answer

I am able to found an alternative method, which worked faster for me. I am posting as the result.

als = np.arange(150,510,5)
szde = np.arange(90,190,5)

res=[]
for h in range(len(als)-1):
    tempu=[]
    for h2 in range(len(szde)-1):            
        a11=np.logical_and((alt<als[h+1]),(alt>als[h]))
        a22=np.logical_and((sza<szde[h2+1]),(sza>szde[h2]))
        tempu.append(np.nanmean(per[np.where(np.logical_and(a11,a22)==True)]))
    res.append(tempu)
res=np.array(res)

res is the gridded array of (72,19)

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement