def plot_period(list_h,list_value, area_id): list_period_value= [] list_period_h = [] d = 10 size_h = len(list_h) for x in xrange(0,int(max(list_h)//10)): mean = 0 total = 0 for y in xrange(0,size_h): if (list_h[y] > d*x) and (list_h[y] < d*(x+1)): total = total +1 mean = mean + list_value[y] if total >0: list_period_value.append(mean/total) list_period_h.append(d*x) plt.subplot(3, 2, 1) plt.plot(list_h, list_value, 'r.') plt.title('A tale of x subplots') plt.ylabel('Semi-variogram') plt.subplot(3, 2, 2) plt.plot(list_period_h, list_period_value, 'r.') #plt.plot(list_h, list_value, 'r.') plt.xlabel('Distance (km)') plt.ylabel('Semi-variogram PERIOD') #spherical a0,c0, mse_sph = opt('spherical', list_period_h, list_period_value) #a0,c0, mse_sph = opt('spherical', list_h, list_value) print 'a0=',a0, ' c0=', c0, ' mse_sph = ', mse_sph plt.subplot(3, 2, 5) list_spherical = [] list_h = sorted(list_h) for h in list_h : list_spherical.append(spherical( h, a0, c0)) #if spherical( h, a0, c0) != c0: # print spherical( h, a0, c0) plt.plot(list_h, list_spherical, 'r-') plt.xlabel('Distance (km)') plt.ylabel('Semi-variogram- SPHERICAL') #plt.savefig(os.path.splitext(os.path.basename('temp_semi_all_station_thang_2'))[0] + '.png') sph = [a0, c0] global_data[area_id] = sph if LOG: plt.show()
def krigeOne(point, neighbor, list_all_station, list_data, G, D): #print 'KRIGE ONE' list_station = list_all_station #Cal distances from current point to all others item0 = (0, point[0], point[1]) meteo_size = len(list_station) list_d = [] #cal all distance from point to others stations for i in xrange(0,meteo_size): #Cal distance one-one item1 = list_station[i] d = haversine(item0[2], item0[1] ,item1[2], item1[1]) newitem = (item1[0], d, item1[3], item1[1], item1[2])#id, distance, temp, lat, lon list_d.append(newitem) #Sort by distance, get n first nearest neighbors sorted_list = sorted(list_d, key=itemgetter(1)) list_neighbor = sorted_list[:neighbor] #print list_neighbor K1 = np.zeros([neighbor, neighbor]) K = np.zeros([neighbor, neighbor]) k = np.zeros([neighbor, 1]) k_semi = np.zeros([neighbor, 1]) T = []# list of temp of neighbor station at interpolate time string = '' i = 0 for item in list_neighbor: if LOG: f.write('getting T\n') #Nhiet do 1 thang cua tram item #print len(list_data) t = list_data[item[0]-1] trendValue = getTrendValue(trend_curve, item[3], item[4]) #print trend_curve, trendValue if LOG: f.write(str(t) + '\n') t_size = len(t) T.append(t[t_size - 1] - trendValue) j = 0 string = string + str(item[0]) #print item #Area key = 0, because of we have only one model for all a0, c0 = global_data[0][0], global_data[0][1] h = item[1]#distance k[i][0] = semiToCov( h, a0, c0) k_semi[i][0] = spherical(h, a0, c0) for sub_item in list_neighbor: string = string+' ' + str(sub_item[0]) #print i, j K1[i][j] = G_COV[item[0]-1][sub_item[0]-1] K [j][i] = K1[i][j]#Inverse of K1 j = j + 1 string = string + '\n' i = i + 1 if LOG: f.write('T: ' + str(T) + '\n') f.write('neighbors list: \n' + string + '\n') #print string #print k #print K f.write('K: \n' + str(K) +' \n') f.write('k: \n' + str(k) +' \n') weight = np.dot(K,k) if LOG: f.write('ori-weight: \n' + str(weight) +' \n') if sum(weight) == 0: if LOG: f.write('NOT RELATED\n') weight[:] = 1 #assign the same weight for all elements weight = weight/sum(weight) krige = np.dot(T, weight) f.write('nor-weight: \n' + str(weight) +' \n') f.write('T: \n' + str(T)+' \n') f.write('krige: ' + str(krige)+' \n') #print krige #print krige[0],' ', #print k weight = inverseMatrix(weight) derivation = np.dot(weight, k_semi) f.write('derivation: ' + str(derivation[0][0])+' \n') #time.sleep(2) return krige[0], derivation[0][0]