繁体   English   中英

使用 shapefile 屏蔽 NetCDF 并计算 shapefile 中所有多边形的平均值和异常值

[英]mask NetCDF using shapefile and calculate average and anomaly for all polygons within the shapefile

有几个关于使用 shapefile 屏蔽 NetCDF 和计算平均度量的教程(示例 1示例 2示例 3 )。 但是,我对那些关于屏蔽 NetCDF 和提取平均值等度量的工作流程感到困惑,并且这些教程不包括提取异常(例如,2019 年的温度与基线平均温度之间的差异)。

我在这里做一个例子。 我已经下载了 2000 年到 2019 年的月度温度( 下载温度文件)和美国州级 shapefile( 下载 shapefile )。 我想根据 2000 年至 2019 年的月平均温度以及 2019 年相对于 2000 年至 2010 年的基准温度的温度异常来获得州级平均温度。具体而言,最终的 dataframe 如下所示:

state 平均温度 anom_temp2019
xx xx
增强现实 xx xx
... ... ...
怀俄明 xx xx
# Load libraries
%matplotlib inline

import regionmask
import numpy as np
import xarray as xr
import pandas as pd
import geopandas as gpd
import matplotlib.pyplot as plt

# Read shapefile
us = gpd.read_file('./shp/state_cus.shp')

# Read gridded data
ds = xr.open_mfdataset('./temp/monthly_mean_t2m_*.nc')
......

我非常感谢您提供可以完成上述任务的明确工作流程的帮助。 非常感谢。

这可以使用区域掩码来实现。 我不使用您的文件,而是使用美国各州的 xarray 教程数据和naturalearth数据。

import numpy as np
import regionmask
import xarray as xr

# load polygons of US states
us_states_50 = regionmask.defined_regions.natural_earth.us_states_50

# load an example dataset
air = xr.tutorial.load_dataset("air_temperature")

# turn into monthly time resolution
air = air.resample(time="M").mean()

# create a mask
mask3D = us_states_50.mask_3D(air)

# latitude weights
wgt = np.cos(np.deg2rad(air.lat))

# calculate regional averages
reg_ave = air.weighted(mask3D * wgt).mean(("lat", "lon"))

# calculate the average temperature (over 2013-2014)
avg_temp = reg_ave.sel(time=slice("2013", "2014")).mean("time")

# calculate the anomaly (w.r.t. 2013-2014)
reg_ave_anom = reg_ave - avg_temp

# select a single timestep (January 2013)
reg_ave_anom_ts = reg_ave_anom.sel(time="2013-01")

# remove the time dimension
reg_ave_anom_ts = reg_ave_anom_ts.squeeze(drop=True)

# convert to a pandas dataframe so it's in tabular form
df = reg_ave_anom_ts.air.to_dataframe()

# set the state codes as index
df = df.set_index("abbrevs")

# remove other columns
df = df.drop(columns="names")

您可以在regionmask 文档Working with geopandas )上找到如何使用自己的 shapefile 的信息。

免责声明:我是 regionmask 的主要作者。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM