前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python气象数据处理 | 克里金(Kriging)插值与可视化

Python气象数据处理 | 克里金(Kriging)插值与可视化

作者头像
郭好奇同学
发布2021-11-29 16:01:41
9K1
发布2021-11-29 16:01:41
举报
文章被收录于专栏:好奇心Log好奇心Log

克里金法(Kriging) 是依据协方差函数对随机过程/随机场进行空间建模和预测(插值)的回归算法。在特定的随机过程,例如固有平稳过程中,克里金法能够给出最优线性无偏估计(Best Linear Unbiased Prediction, BLUP),因此在地统计学中也被称为空间最优无偏估计器(spatial BLUP)

1、安装模块

代码语言:javascript
复制
!pip install PyKrige
!pip install plotnine
!pip install openpyxl

2、导入模块

代码语言:javascript
复制
import pandas as pd
from pykrige.ok import OrdinaryKriging
import plotnine
from plotnine import *
import geopandas as gpd
import shapefile
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.io.shapereader as shpreader
import cmaps
from matplotlib.path import Path
from matplotlib.patches import PathPatch

3、读取数据

代码语言:javascript
复制
df = pd.read_excel('/home/mw/input/meiyu6520/meiyu_sh_2020.xlsx')
df
代码语言:javascript
复制
# 读取站点经度
lons = df['lon']
# 读取站点纬度
lats = df['lat']
# 读取梅雨量数据
data = df['meiyu']
# 生成经纬度网格点
grid_lon = np.linspace(120.8, 122.1,1300)
grid_lat = np.linspace(30.6, 31.9,1300)

4、克里金(Kriging)插值

代码语言:javascript
复制
OK = OrdinaryKriging(lons, lats, data, variogram_model='gaussian',nlags=6)
z1, ss1 = OK.execute('grid', grid_lon, grid_lat)
z1.shape

输出:

代码语言:javascript
复制
(1300, 1300)

转换成网格

代码语言:javascript
复制
xgrid, ygrid = np.meshgrid(grid_lon, grid_lat)

将插值网格数据整理

代码语言:javascript
复制
df_grid = pd.DataFrame(dict(long=xgrid.flatten(),lat=ygrid.flatten()))

添加插值结果

代码语言:javascript
复制
df_grid["Krig_gaussian"] = z1.flatten()
df_grid

输出:

代码语言:javascript
复制
long    lat Krig_gaussian
0    121.000000  30.5    541.076134
1    121.020408  30.5    540.997891
2    121.040816  30.5    540.770766
3    121.061224  30.5    540.381399
4    121.081633  30.5    539.818047
...    ... ... ...
2495    121.918367  31.5    567.391506
2496    121.938776  31.5    562.730899
2497    121.959184  31.5    558.418169
2498    121.979592  31.5    554.460195
2499    122.000000  31.5    550.857796
2500 rows × 3 columns

5、读取上海的行政区划

代码语言:javascript
复制
sh = gpd.read_file('/home/mw/input/meiyu6520/Shanghai.shp')
sh

输出:

代码语言:javascript
复制
City    District    Province    Code    geometry
0    上海市 普陀区 上海市 310107  POLYGON ((121.35622 31.23362, 121.35418 31.237...
1    上海市 宝山区 上海市 230506  POLYGON ((121.48552 31.31156, 121.48541 31.311...
2    上海市 崇明区 上海市 310151  MULTIPOLYGON (((121.87022 31.29554, 121.86596 ...
3    上海市 奉贤区 上海市 310120  POLYGON ((121.56443 30.99643, 121.57047 30.998...
4    上海市 虹口区 上海市 310109  POLYGON ((121.46828 31.31520, 121.46831 31.316...
5    上海市 黄浦区 上海市 310101  POLYGON ((121.48781 31.24419, 121.49483 31.242...
6    上海市 嘉定区 上海市 310114  POLYGON ((121.33689 31.29506, 121.33650 31.294...
7    上海市 金山区 上海市 310116  POLYGON ((121.00206 30.95104, 121.00764 30.947...
8    上海市 静安区 上海市 310106  POLYGON ((121.46808 31.32032, 121.46809 31.320...
9    上海市 闵行区 上海市 310112  POLYGON ((121.33689 31.23674, 121.33835 31.237...
10    上海市 浦东新区    上海市 310115  MULTIPOLYGON (((121.97077 31.15756, 121.96568 ...
11    上海市 青浦区 上海市 310118  POLYGON ((121.31900 31.15867, 121.31953 31.157...
12    上海市 松江区 上海市 310117  POLYGON ((121.02906 30.94388, 121.02804 30.943...
13    上海市 徐汇区 上海市 310104  POLYGON ((121.45800 31.21929, 121.45807 31.219...
14    上海市 杨浦区 上海市 310110  POLYGON ((121.52288 31.34289, 121.52549 31.346...
15    上海市 长宁区 上海市 310105  POLYGON ((121.43938 31.21444, 121.43946 31.214...

绘图:

代码语言:javascript
复制
sh.plot()

6、插值结果可视化

代码语言:javascript
复制
fig = plt.figure(figsize=(12, 8))
ax = fig.add_subplot(1, 1, 1, projection=ccrs.PlateCarree())
ax.set_extent([120.8, 122.1, 30.6, 31.9], crs=ccrs.PlateCarree())

province = shpreader.Reader('/home/mw/input/meiyu6520/Shanghai.shp')
ax.add_geometries(province.geometries(), crs=ccrs.PlateCarree(), linewidths=0.5,edgecolor='k',facecolor='none')

cf = ax.contourf(xgrid, ygrid, z1, levels=np.linspace(0,800,21), cmap=cmaps.MPL_rainbow, transform=ccrs.PlateCarree())

def shp2clip(originfig, ax, shpfile):
    sf = shapefile.Reader(shpfile)
    vertices = []
    codes = []
    for shape_rec in sf.shapeRecords():
        pts = shape_rec.shape.points
        prt = list(shape_rec.shape.parts) + [len(pts)]
        for i in range(len(prt) - 1):
            for j in range(prt[i], prt[i + 1]):
                vertices.append((pts[j][0], pts[j][1]))
            codes += [Path.MOVETO]
            codes += [Path.LINETO] * (prt[i + 1] - prt[i] - 2)
            codes += [Path.CLOSEPOLY]
        clip = Path(vertices, codes)
        clip = PathPatch(clip, transform=ax.transData)
    for contour in originfig.collections:
        contour.set_clip_path(clip)
    return contour

shp2clip(cf, ax, '/home/mw/input/meiyu6520/Shanghai.shp')

cb = plt.colorbar(cf)
cb.set_label('Rainfall(mm)',fontsize=15)

plt.show()
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-11-25,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 好奇心Log 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、安装模块
  • 2、导入模块
  • 3、读取数据
  • 4、克里金(Kriging)插值
  • 5、读取上海的行政区划
  • 6、插值结果可视化
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档