def Spatial_Map(df, filename): lons = np.array(df['x']) #lons =longitude lats = np.array(df['y']) #lats=lattitude data = np.array(df['Difference']) plt.figure() plt.scatter(lons, lats, 15, data, cmap = plt.cm.Blues) plt.xlabel("East") plt.ylabel("North") plt.title("Data Visualization") cbar = plt.colorbar() cbar.set_label("Data", labelpad=+1) #Why "labelpad+1" #minus, for example -100 will set the label "Data" more to the left side. If you set += then the label "Data" will be set more to the right. If you set = python wil read it like you would type += plt.show() OK = OrdinaryKriging(lons, lats, data, variogram_model='spherical', nlags=25, verbose=True, enable_plotting=True) #To check spatial correlation #What does the verbose=True do? # z1, ss1 = OK.execute('grid', lons, lats) # xintrp, yintrp = np.meshgrid(grid_lon, grid_lat) # xintrp, yintrp = np.meshgrid(lons, lats) grid_x = np.linspace(150000, 250000, num = 100, endpoint = False) grid_y = np.linspace(160000, 220000, num = 100, endpoint = False) # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See OrdinaryKriging.__doc__ for more information.): z, ss = OK.execute('grid', grid_x, grid_y) # data: z and variance:ss # Writes the kriged grid to an ASCII grid file: kt.write_asc_grid(grid_x, grid_y, z, filename="kriging_ordinary.asc") #Ordinary Kriging in matrix form kt.write_asc_grid(grid_x, grid_y, ss, filename="kriging_ordinary_var.asc") #Estimation variance in matrix form asc = pd.read_csv("kriging_ordinary.asc", header=None, skiprows=7, sep="\s+") asc.shape #check that they are 70 rows and 80 columns #Why 70 rows and 80 columns? cu2 = np.array(asc) #pandas dataframe to ndarray conversion #Krigging map plot from descartes import PolygonPatch #PolygonPatch: Constructs a matplotlib patch from a geometric object, but what is a "matplotlibpatch"?? gpo = gdp.read_file("C:\\Users\\Julio\\Desktop\\IUPWARE\\ENVIRONMENTAL PROGRAMMING\\Project Groundwater\\Data\\Groundwaterbodies_Oligocene\\Groundwaterbodies_Oligocene.shp") fig = plt.figure(figsize = (12,12)) ax = fig.gca() a = plt.imshow(cu2, cmap=plt.cm.YlOrBr, extent=[150000,250000,160000,220000]) #gist_rainbow plt.grid(True) cbar = fig.colorbar(a, orientation='horizontal', pad=0.05) cbar.set_label("Anomaly in mTAW", labelpad=+1) plt.title('Anomaly of groundwater level - Kriging interpolation') plt.xlabel('East') plt.ylabel('North') ax.scatter(lons, lats, c='blue') for i in gpo['geometry']: ax.add_patch(PolygonPatch(i, fill=False, alpha=0.5, zorder=2, hatch='////')) #What does this line of code do? plt.show() #Save as PDF document fig.savefig(filename+".pdf", bbox_inches='tight')
def rain_ordinary_kriging(radar_center_x, radar_center_y, radar_radius_in_graph, radar_resolution_x, radar_resolution_y, x_measure, y_measure, z_measure_data): """rainfall ordinary kriging. Parameters ---------- center_grid_num_x: in radar map, the num in x-axis of center center_grid_num_y: in radar map, the num in y-axis of center x_measure: the x num of the points we have measured y_measure: the y num of the points we have measured z_measure_data: z_keige=z_keige(x,y) is the measurement value at point (x,y) Returns ---------- z_krige: ndarray, Z-values of all specified grid ss_krige: ndarray, Variance at specified grid points or at the specified set of points """ # show the range of interpolation # plus '1' for the open interval # the last parameter "1.0" means one grid is "1", the type has to be float, or it will cast error gridx = np.arange( radar_center_x - radar_radius_in_graph, radar_center_x - radar_radius_in_graph + radar_radius_in_graph * 2 + 1, 1.0) gridy = np.arange( radar_center_y - radar_radius_in_graph, radar_center_y - radar_radius_in_graph + radar_radius_in_graph * 2 + 1, 1.0) '''krige the rain gauge observations. Create the ordinary kriging object. Required inputs are the X-coordinates of the data points, the Y-coordinates of the data points, and the Z-values of the data points. If no variogram model is specified, defaults to a linear variogram model. If no variogram model parameters are specified, then the code automatically calculates the parameters by fitting the variogram model to the binned experimental semivariogram. The verbose kwarg controls code talk-back, and the enable_plotting kwarg controls the display of the semivariogram. ''' temp_zero = np.zeros(1) if (z_measure_data <= temp_zero).all(): z_krige = np.zeros((len(gridx), len(gridy))) ss_krige = np.zeros((len(gridx), len(gridy))) else: OK = OrdinaryKriging(x_measure, y_measure, z_measure_data, variogram_model='linear', verbose=False, enable_plotting=True) '''the enable-plotting controls the display of semivariogram, so we can check it to choose the best r(d) fitting curve ''' # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See OrdinaryKriging.__doc__ for more information.) z_krige, ss_krige = OK.execute('grid', gridx, gridy) '''z_keige is the result, and ss_krige is the variance. So, next ,we can visualize the interpolation data''' kt.write_asc_grid(gridx, gridy, z_krige, filename="output.asc") X, Y = np.meshgrid(gridx, gridy) C = plt.contour(X, Y, z_krige, 8, colors='black') # 生成等值线图 plt.contourf(X, Y, z_krige, 8) plt.clabel(C, inline=1, fontsize=10) plt.show() return z_krige, ss_krige
def interpolate(data, gridx, gridy, outAscllDir): OK = OrdinaryKriging(data[:, 1], data[:, 2], data[:, 0], variogram_model='linear', verbose=False, enable_plotting=False) # Calculates a kriged grid and the associated variance. z, ss = OK.execute('grid', gridx, gridy) # Writes the kriged grid to an ASCII grid file. kt.write_asc_grid(gridx, gridy, z, filename=outAscllDir) print('---interpolate success---') print('Save %s success' % outAscllDir) return z
def execute_Ordinary_Kriging(data, gridx, gridy, filename_to_write): # Create the ordinary kriging object. Required inputs are the X-coordinates of # the data points, the Y-coordinates of the data points, and the Z-values of the # data points. If no variogram model is specified, defaults to a linear variogram # model. If no variogram model parameters are specified, then the code automatically # calculates the parameters by fitting the variogram model to the binned # experimental semivariogram. The verbose kwarg controls code talk-back, and # the enable_plotting kwarg controls the display of the semivariogram. OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='gaussian', verbose=True, enable_plotting=True) # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See OrdinaryKriging.__doc__ for more information.) z, ss = OK.execute('grid', gridx, gridy) # Writes the kriged grid to an ASCII grid file. kt.write_asc_grid(gridx, gridy, z, filename=filename_to_write) print("\n ---> Kriging terminated, and results loaded") ascii_grid = np.loadtxt(filename_to_write, skiprows=7) data_frame = pd.DataFrame(ascii_grid, columns=gridx, index=gridy) print("\n ---> data_frame created") return z, data_frame # Cette liste comprend uniquement le territoire formé par # la partie métropolitaine continentale de la France (à l'exclusion des îles) : # nord : Bray-Dunes, Nord (51° 05′ 21″ N, 2° 32′ 43″ E) ; # est : Lauterbourg, Bas-Rhin (48° 58′ 02″ N, 8° 13′ 50″ E) ; # sud : Puig de Coma Negra, Lamanère, Pyrénées-Orientales (42° 19′ 58″ N, 2° 31′ 58″ E) ; # ouest : pointe de Corsen, Plouarzel, Finistère (48° 24′ 46″ N, 4° 47′ 44″ O).
for x in gridx: for y in gridy: point = Point(x, y) polygon = point.buffer(0.5, cap_style=3) if not polygon.intersects(boundary): z_rh[ycount][xcount] = no_data else: if not polygon.within(boundary): z_rh[ycount][xcount] = -100 - z_rh[ycount][xcount] ycount += 1 xcount += 1 ycount = 0 print('writing result......') filename = '../data/pm25.asc' kt.write_asc_grid(gridx, gridy, z_rh, filename, 2) print('finished') print('start drawing image....') scale = 800 / z_rh.shape[1] result = bilinear_interpolation(z_rh, scale) imgWidth = result.shape[1] imgHeight = result.shape[0] img = Image.new("RGB", (imgWidth, imgHeight)) draw = ImageDraw.Draw(img) for i in range(imgWidth): for j in range(imgHeight): draw.point((i, imgHeight - j), fill=getPixValue(result[j, i])) img.save('aqi.png') # make a color map of fixed colors # cmap = colors.ListedColormap(['green','yellow','orange','red','purple','grey'])
#y_max=round(all_station_Y1981_gdf['y'].max())+1000 #y_min=round(all_station_Y1981_gdf['y'].min())-1000 gridx = np.arange(210000.00, 1320000.00, 3000) gridy = np.arange(540000.00, 2300000.00, 3000) #gridx = np.arange(x_min, x_max,3000) #gridy = np.arange(y_min, y_max,3000) len(gridy) #a=(x_min,x_max,y_min,y_max) #a #gridx #data=list(all_station_Y1981_gdf.geometry.x) #data OK = OrdinaryKriging(all_station_Y1981_gdf['x'], all_station_Y1981_gdf['y'], all_station_Y1981_gdf['dailyRainfall'], variogram_model='power', variogram_parameters={ 'scale': 0.5, 'nugget': 1, 'exponent': 1 }, verbose=True, enable_plotting=False, nlags=4) z, ss = OK.execute('grid', gridx, gridy) kt.write_asc_grid(gridx, gridy, z, filename='./2014_all.asc')
data = np.array([[0.3, 1.2, 0.47], [1.9, 0.6, 0.56], [1.1, 3.2, 0.74], [3.3, 4.4, 1.47], [4.7, 3.8, 1.74]]) print(data[:, 0].shape) print(data[:, 1].shape) print(data[:, 2].shape) gridx = np.arange(0.0, 5.5, 0.5) gridy = np.arange(0.0, 5.5, 0.5) # Create the ordinary kriging object. Required inputs are the X-coordinates of # the data points, the Y-coordinates of the data points, and the Z-values of the # data points. If no variogram model is specified, defaults to a linear variogram # model. If no variogram model parameters are specified, then the code automatically # calculates the parameters by fitting the variogram model to the binned # experimental semivariogram. The verbose kwarg controls code talk-back, and # the enable_plotting kwarg controls the display of the semivariogram. OK = OrdinaryKriging(data[:, 0], data[:, 1], data[:, 2], variogram_model='linear', verbose=False, enable_plotting=False) # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. # (See OrdinaryKriging.__doc__ for more information.) z, ss = OK.execute('grid', gridx, gridy) # Writes the kriged grid to an ASCII grid file. kt.write_asc_grid(gridx, gridy, z, filename="output.asc")
def KrigeCV(P, lags, date, metric, output): #create dictionary for gridsearch to use in parameter tuning param_dict = { "method": ["ordinary", "universal"], "variogram_model": ["exponential", "gaussian", "linear", "power", "spherical"], "nlags": lags, "weight": [True, False] } estimator = GridSearchCV(Krige(), param_dict, verbose=False, cv=2) ###This cv=2 could be adjusted X = (P[:, 0:2]) #select x variables y = P[:, 2] #select y variable estimator.fit(X=X, y=y) if hasattr(estimator, 'best_score_'): print('best_score R² = {:.3f}'.format(estimator.best_score_)) print('Optimal Lags: {}'.format(estimator.best_params_['nlags'])) print('best_params = ', estimator.best_params_) #define grid dist = .15 gridx = np.arange(math.floor(min(P[:, 0])), math.ceil(max(P[:, 0])), dist) gridy = np.arange(math.floor(min(P[:, 1])), math.ceil(max(P[:, 1])), dist) ##to be used for shapefile output #gridxShape = np.arange(math.floor(min(P[:,0])) - (.5*dist), math.ceil(max(P[:,0])) + (.5*dist), dist) #gridyShape = np.arange(math.floor(min(P[:,1])) - (.5*dist), math.ceil(max(P[:,1])) + (.5*dist), dist) if estimator.best_params_[ 'method'] == 'universal': #for all universal kriging UK = UniversalKriging( P[:, 0], P[:, 1], P[:, 2], variogram_model=estimator.best_params_['variogram_model'], nlags=estimator.best_params_['nlags'], weight=estimator.best_params_['weight'], verbose=False, enable_plotting=True ) #perform kriging with params chosen by gridsearch z, ss = UK.execute('grid', gridx, gridy) if output == 'ASCII': filename = str(date) + '_' + str( metric) + '.asc' #Create unique filename kt.write_asc_grid(gridx, gridy, z, filename=filename) #write out as ASCII file elif output == 'Shapefile': geo_df = OutputShape(z, gridxShape, gridyShape) filename = str(date) + '_' + str(metric) + '.shp' geo_df.to_file(filename) else: print("output parameter must be 'ASCII' or 'Shapefile'. ") elif estimator.best_params_[ 'method'] == 'ordinary': #for all ordinary kriging OK = OrdinaryKriging( P[:, 0], P[:, 1], P[:, 2], variogram_model=estimator.best_params_['variogram_model'], nlags=estimator.best_params_['nlags'], weight=estimator.best_params_['weight'], verbose=False, enable_plotting=True ) #perform kriging with params chosen by gridsearch z, ss = OK.execute('grid', gridx, gridy) if output == 'ASCII': filename = str(date) + '_' + str( metric) + '.asc' #Create unique filename kt.write_asc_grid(gridx, gridy, z, filename=filename) #write out as ASCII file elif output == 'Shapefile': geo_df = OutputShape(z, gridxShape, gridyShape) filename = str(date) + '_' + str(metric) + '.shp' geo_df.to_file(filename) else: print("output parameter must be 'ASCII' or 'Shapefile'. ") else: print('Kriging method not recognized as Universal or Ordinary') sub = [ ] #create and fill list, to save Rsquared and other outputs/parameters sub.extend((date, metric, estimator.best_score_, estimator.best_params_)) return sub
z, variogram_model='linear', verbose=False, enable_plotting=False) # Creates the kriged grid and the variance grid. Allows for kriging on a rectangular # grid of points, on a masked rectangular grid of points, or with arbitrary points. gridx = np.arange(x.min(), x.max(), 0.0000005) gridy = np.arange(y.min(), y.max(), 0.0000005) Z, ss = OK.execute('grid', gridx, gridy) plt.figure(2) plt.pcolormesh(gridx, gridy, Z) plt.grid() plt.title("Interpolation par krigeage") plt.colorbar() plt.show() # Writes the kriged grid to an ASCII grid file. kt.write_asc_grid(gridx, gridy, Z, filename="outputKrigeage.asc") ########################################################################################### # Spline linear interpolation func = scipy.interpolate.interp2d(x, y, z) Z = func(X[0, :], Y[:, 0]) plt.figure(3) plt.pcolormesh(X, Y, Z) plt.grid() plt.title("Interpolation par Spline Lineaire") plt.colorbar() plt.show()
def plot_interpolated_map(datafile='CWB_Stations_171226.csv', drop_index=[], data="R_FACTOR", label='R Factor', grid_space=0.1, counties_plot=True, units=r'MJ mm $ha^{-1}$ $h^{-1}$ $yr^{-1}$'): #def plot_interpolated_map(datafile='R_FACTOR_WITH_ST_ALTITUDE_GTR_5.txt',drop_index=[],data="R_FACTOR_DENSITY",label='R Factor Density',grid_space=0.01,counties_plot=True,units=r'MJ $ha^{-1}$ $h^{-1}$ $yr^{-1}$'): #def plot_interpolated_map(datafile='R_FACTOR_WITH_ST_ALTITUDE_GTR_20.txt',drop_index=[],data="mn_anual_precip",label='Mean Anual Precip',grid_space=0.01,counties_plot=True,units='mm'): ''' Plot the interpolated map of the 'data' column of the 'datafile'. drop_index drops the given list of indices from the data set. 'label' labels the colorbar of the map. 'grid_space' decides the resolution of the interpolation. 'counties_plot' True tells the program to plot counties on the map else enter False 'units' plot the unit of the data. ''' print("Reading Data") #Read the data in the different columns separated by space df = pd.read_csv(datafile, delimiter=',') e = np.random.rand(len(df['id'])) df = df.assign(EI=pd.Series(e).values) # noEI = df[df['EI']<=0] # drop_index = np.array(noEI.index()) df.drop(df.index[drop_index], inplace=True) print(df) sys.exit() # del df['St.Name'],df['S.N0.'], df['ALTITUDE'], df['num_of_yrs'], df['no_typ'],df['Acc_Precip'] df_new = df[(df['St.Lon'] > 119.8) & (df['St.Lat'] < 25.7)] #exclude all the points outside the inland of taiwan lons = np.array( df_new['St.Lon'] ) #define an array containing the Longitude information of the data lats = np.array( df_new['St.Lat'] ) #define an array containing the latitude insformation of the data data = np.array( df_new[data] ) #Save the data from selected column (such as R_FACTOR or mn_precip) in the data variable # data1=np.array(df_new['R_FACTOR']) # data2=np.array(df_new['mn_precip']) # grid_lon = np.arange(lons.min()-0.05, lons.max()+0.1, grid_space) #make the grid for interpolating the data. # #The minimum and maximum of the longitude and latitude is chosen based on the data. # grid_lat = np.arange(lats.min(), lats.max()+0.1, grid_space) lonmin = 119.9 lonmax = 122.15 latmin = 21.8 latmax = 25.4 grid_lon = np.arange( lonmin, lonmax, grid_space) #make the grid for interpolating the data. #The minimum and maximum of the longitude and latitude is chosen based on the data. grid_lat = np.arange(latmin, latmax, grid_space) print("Interpolating Data") # Create ordinary kriging object # change the variogram model, nlags for obtaining different level of fits OK = OrdinaryKriging(lons, lats, data, variogram_model='gaussian', verbose=True, enable_plotting=False, nlags=20) #print(grid_lon) #print(data) #print(z1) # Obtain the interpolation on the grid points using the kriging object z1, ss1 = OK.execute('grid', grid_lon, grid_lat) write_asc_grid(grid_lon, grid_lat, z1, filename='ordinary_kriging_output_R_FACTOR_GTR_5.asc') #write_asc_grid(grid_lon,grid_lat,z1,filename='ordinary_kriging_output_MN_ANUAL_PRECIP_GTR_20.asc') #write_asc_grid(grid_lon,grid_lat,z1,filename='ordinary_kriging_R_FACTOR_DENSITY_GTR_20.asc') # Make a meshgrid for plotting the data on the map xintrp, yintrp = np.meshgrid(grid_lon, grid_lat) print('Plotting Data') fig, ax = plt.subplots(figsize=(6, 8)) #make the figure frame of given size colors = [ (0.8, 0, 0), (0, 0.8, 0), (0, 0, 0.8) ] # R -> G -> B #select the level of red, blue and green intensity cmap_name = 'my_list' ncols = 33 #select the number of colors for plotting. The whole spectrum of red,blue, green will be divided into ncols bars. ## forR factor ncols = 17; for mn_precip ncols = 13 cm = LinearSegmentedColormap.from_list( cmap_name, colors, N=ncols ) # red, blue and green will be joined to make a single colormap #Make a basemap for plotting the map m = Basemap(llcrnrlon=lonmin, llcrnrlat=latmin, urcrnrlon=lonmax, urcrnrlat=latmax, projection='merc', resolution='h', area_thresh=1000., ax=ax) m.drawcoastlines() #draw coastlines on the map x, y = m(xintrp, yintrp) # convert the coordinates into the map scales ln, lt = m(lons, lats) # cs = ax.contourf(x,y,z1,np.linspace(300,900,ncols),extend='both',cmap='jet')# mn precip cs = ax.contourf(x, y, z1, np.linspace(2000, 10000, ncols), extend='both', cmap='jet') # R_factor #cs = ax.contourf(x,y,z1,np.linspace(5,9,ncols),extend='both',cmap='jet')# R_factor DENSITY # The minimum and maximum can be changed by changing the vmin and vmax! cbar = m.colorbar(cs, location='right', pad="7%") #plot the colorbar on the map plt.title(label + '\n' + units, fontsize=14, family='times new roman', fontweight='bold') plt.xlabel('\n\nLongitude', fontsize=16) plt.ylabel('Latitude\n\n\n', fontsize=16) #cbar.set_label(units,fontsize=16,family='times new roman') # put labels of the colorbar ax.text(0.2, 0.95, '(a)', ha='center', va='center', transform=ax.transAxes, fontsize=16) ## Plotting counties if counties_plot: counties = glob.glob( "counties_polygon/polygon_*.txt" ) #read all the colunties coastline data separately for files in counties: df = pd.read_csv(files, delimiter="\s+", names=["longi", "lati"], header=None) if df.shape[ 0] > 3000: #plot only those counties with size greater than 3000 lo, la = m(df['longi'].values, df['lati'].values) m.plot( lo, la, 'k', lw=0.5 ) #plot the counties on the basemap with black lines and with linewidth 0.5 # draw parallels. #parallels = np.arange(latmin,latmax,0.5) parallels = np.arange(21.5, 26.0, 0.5) m.drawparallels(parallels, labels=[1, 0, 0, 0], fontsize=16, linewidth=0.0) #Draw the latitude labels on the map # draw meridians meridians = np.arange(119.5, 122.5, 0.5) m.drawmeridians(meridians, labels=[0, 0, 0, 1], fontsize=16, linewidth=0.0) ##getting the limits of the map x0, x1 = ax.get_xlim() y0, y1 = ax.get_ylim() map_edges = np.array([[x0, y0], [x1, y0], [x1, y1], [x0, y1]]) ##getting all polygons used to draw the coastlines of the map polys = [p.boundary for p in m.landpolygons] ##combining with map edges polys = [map_edges] + polys[:] ##creating a PathPatch codes = [[Path.MOVETO] + [Path.LINETO for p in p[1:]] for p in polys] polys_lin = [v for p in polys for v in p] codes_lin = [c for cs in codes for c in cs] path = Path(polys_lin, codes_lin) patch = PathPatch(path, facecolor='white', lw=0) ##masking the data outside the inland of taiwan ax.add_patch(patch) fignm_array = label.split() fignm = "_".join(fignm_array) fig_prefix = datafile.split(".")[0] print(fig_prefix) print("Output figures is saved as Figures/{}-{}.png".format( fig_prefix, fignm)) plt.savefig('Figures/{}-{}.png'.format(fig_prefix, fignm), dpi=100, bbox_inches='tight')