I am trying to collocate 2 datasets of different resolution using haversine's formula in python.
This loop taking too much time to compute the distance using the haversine formula. In order to reduce the unwanted collocating 2 very distant data points, I gave a condition to skip to next grid point
for i in range(len(lati1)):
for j in range (len(long1)):
for x in range(len(lati2)):
for y in range(len(long2)):
lat1 = radians(lati1[i])
lon1 = radians(long1[j])
lat2 = radians(lati2[x])
lon2 = radians(long2[y])
temp[0][i][j]
dlat = lat2 - lat1
dlon = lon2 - lon1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance = R * c
print distance
if ( distance <= 3.5):
print distance, lati1[i], long1[j],lati2[x], long2[y], temp[i][j], preci[x][y]
writer.writelines("\r")
writer.writelines( (' ').join([distance, lati1[i], long1[j], temp[i][j], preci[x][y]]))
elif (distance < 5.0):
break
break
j=j+1
after running this program for a long time, it is showing like this
572
572
3.2412380942 0.13999939 60.100006 0.125 60.125 287.77 0.0
Traceback (most recent call last):
File "<ipython-input-36-ac8328bf9abd>", line 40, in <module>
writer.writelines( (' ').join([distance, btlat[i], btlon[j], btlat[0,i,j], predata[x,y]]))
File "/home/krishna/.local/lib/python2.7/site-packages/numpy/ma/core.py", line 3174, in __getitem__
dout = self.data[indx]
IndexError: too many indices for array
~ I can change the array indices but is there any better solutions? to collocate?
Some could you help me to resolve this issue? Thanks