Exemplo n.º 1
0
def main_program(shuffi, isCrossValidation):
	list_all_test = shuffi[0]
	list_all_station = shuffi[1]
	global D
	#time.sleep(100)
	print len(list_all_station)
	trend_curve = generateGlobalMatrix(date_from, date_to, list_all_station)
	D = generateDistanceMatrix(list_all_station)
	area_id = 0
	genModelForArea(area_id, list_all_station, list_data, G, D)
	reGenerateSemiMatrix(list_all_station)
	print 'GLOBAL DATA'
	print global_data
	derivation = np.zeros([row,col])
	result = np.zeros([row,col])
	for i in xrange(0,row):
		#print i
		for j in xrange(0, col):
			#print i, j
			point = (minLat + i*shift,minLon + j*shift)
			f.write(str(i)+' '+ str(j)+ ':'+ str(point)  +'\n')
			#if isInVietnam(point):
			result[i][j], derivation[i][j] = krigeOne(point, neighbor, list_all_station, list_data, G, D)
			trendValue = getTrendValue(trend_curve, point[0], point[1])
			#print trendValue
			result[i][j] = result[i][j] + trendValue
	#exportGeotiff('filename', raster, row, col, shift, minLon, minLat)
	if isCrossValidation == False:	
		exportGeotiff('t08_GEOTIFF_simple_kriging_one_model', result, row, col, shift, minLon, minLat )
	saveAsPng(result)
	saveAsPng(derivation)
	if isCrossValidation:	
		return calculateSE(result, list_all_test, minLat, minLon, shift, date_to)
def main_program(shuffi, isCrossValidation):
    f_se = open("Moiseture_f_se.txt", "a")
    f_se.write("Moiseture_se statistic\n")
    f_da = open("Moiseture_f_da.txt", "a")
    f_da.write("Moiseture_da statistic\n")

    list_all_test = shuffi[0]
    list_all_station = shuffi[1]
    global D
    # time.sleep(100)
    print len(list_all_station)
    trend_curve = generateGlobalMatrix(date_from, date_to, list_all_station)
    D = generateDistanceMatrix(list_all_station)
    area_id = 0
    genModelForArea(area_id, list_all_station, list_data, G, D)
    reGenerateSemiMatrix(list_all_station)
    print "GLOBAL DATA"
    print global_data
    derivation = np.zeros([row, col])
    result = np.zeros([row, col])
    result_kri_backup = np.zeros([row, col])
    for i in xrange(0, row):
        # print i
        for j in xrange(0, col):
            # print i, j
            point = (minLat + i * shift, minLon + j * shift)
            f.write(str(i) + " " + str(j) + ":" + str(point) + "\n")
            result[i][j], derivation[i][j] = krigeOne(point, neighbor, list_all_station, list_data, G, D)
            trendValue = getTrendValue(trend_curve, point[0], point[1])
            # print trendValue
            result[i][j] = result[i][j] + trendValue
            result_kri_backup[i][j] = result[i][j]
            # DATA ASSIMILATION: assimilate
            # if sate_lite_value[i][j] != 0:
            # 	result[i][j] = assimilate(result[i][j], derivation[i][j], sate_lite_value[i][j], sate_lite_deri[i][j], f_da)
            # Assimilate with Utility.assimilate
    if isCrossValidation == False:
        exportGeotiff("Moiseture__GEOTIFF_universal_kriging", result, row, col, shift, minLon, minLat)
        exportGeotiff("Moiseture__GEOTIFF_universal_kriging_derivation", derivation, row, col, shift, minLon, minLat)
    saveAsPng(result)
    saveAsPng(derivation)
    f_da.close()
    f_se.close()
    if isCrossValidation:
        return calculateSE(
            result, result_kri_backup, list_all_test, minLat, minLon, shift, date_to, f, sate_lite_value, f_se
        )
Exemplo n.º 3
0
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
	'''
	#Get areaid for each station
	list_neighbor_with_areaid = []
	for item in list_neighbor:
		item1 = list_station[item[0]]
		new_item = (item[0], item[1], getAreaIdFromPoint([item1[1], item1[2]]))
		list_neighbor_with_areaid.append(new_item)
	'''
	K1 = np.zeros([neighbor, neighbor])
	K = np.zeros([neighbor, neighbor])
	k = np.zeros([neighbor, 1])
	T = []# list of temp of neighbor station at interpolate time
	string = ''
	i = 0 
	for  item in list_neighbor:
		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
		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)
		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
	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)
	f.write('ori-weight: \n' + str(weight) +' \n')
	if sum(weight) == 0:
		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)
	f.write('derivation: ' + str(derivation[0][0])+' \n')	
	#time.sleep(2)
	return krige[0], derivation[0][0]


	'''