简体   繁体   English

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

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

There are several tutorials ( example 1 , example 2 , example 3 ) about masking NetCDF using shapefile and calculating average measures.有几个关于使用 shapefile 屏蔽 NetCDF 和计算平均度量的教程(示例 1示例 2示例 3 )。 However, I was confused with those workflows about masking NetCDF and extracting measures such as average, and those tutorials did not include extract anomaly (for example, the difference between temperature in 2019 and a baseline average temperature).但是,我对那些关于屏蔽 NetCDF 和提取平均值等度量的工作流程感到困惑,并且这些教程不包括提取异常(例如,2019 年的温度与基线平均温度之间的差异)。

I make an example here.我在这里做一个例子。 I have downloaded monthly temperature ( download temperature file ) from 2000 to 2019 and the state-level US shapefile ( download shapefile ).我已经下载了 2000 年到 2019 年的月度温度( 下载温度文件)和美国州级 shapefile( 下载 shapefile )。 I want to get the state-level average temperature based on the monthly average temperature from 2000 to 2019 and the temperature anomaly of year 2019 relative to baseline temperature from 2000 to 2010. Specifically, the final dataframe looks as follow:我想根据 2000 年至 2019 年的月平均温度以及 2019 年相对于 2000 年至 2010 年的基准温度的温度异常来获得州级平均温度。具体而言,最终的 dataframe 如下所示:

state state avg_temp平均温度 anom_temp2019 anom_temp2019
AL xx xx xx xx
AR增强现实 xx xx xx xx
... ... ... ... ... ...
WY怀俄明 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')
......

I really appreciate your help that providing an explicit workflow that could do the above task.我非常感谢您提供可以完成上述任务的明确工作流程的帮助。 Thanks a lot.非常感谢。

This can be achieved using regionmask.这可以使用区域掩码来实现。 I don't use your files but the xarray tutorial data and naturalearth data for the US states.我不使用您的文件,而是使用美国各州的 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")

You can find info how to use your own shapefile on the regionmask docs ( Working with geopandas ).您可以在regionmask 文档Working with geopandas )上找到如何使用自己的 shapefile 的信息。

disclaimer: I am the main author of regionmask.免责声明:我是 regionmask 的主要作者。

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

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