利用python的xclim计算极端气候指标

您所在的位置:网站首页 如何计算卡路里 利用python的xclim计算极端气候指标

利用python的xclim计算极端气候指标

2023-06-21 14:43| 来源: 网络整理| 查看: 265

目录

0、导入包

1、加载数据

2、计算指数

3、其他设置

4、plot

xcilm是针对xarray的Dataarray对象的气候指数的集合。

xcilm提供两个计算层,一个是用来计算和单位处理的:indices;另一个是用来检查数据和元数据格式的层(CF:check formatting):indicators。计算层的函数可以从xclim.indices找到,CF层的指数可以从xclim.atmos, xclim.land和xclim.seaIce找到。用户应该始终使indicators,如果indicators太麻烦的话,则使用indices作为最后的手段。

0、导入包 from __future__ import annotations import xarray as xr import xclim from xclim import testing 1、加载数据 # Normally, we would use xarray to open a dataset, e.g.: # ds = xr.open_dataset("your_file.nc") # For this example, let's use a test dataset from xclim: ds = testing.open_dataset("ERA5/daily_surface_cancities_1990-1993.nc") ds.tas # The more common method ds = xr.open_mfdataset("D:\\extreme-carbon\\result\\tas_1979-2020_global_daliy_max.nc", parallel=True) ds.t2m 2、计算指数 gdd = xclim.atmos.growing_degree_days(tas=ds.tas, thresh="10.0 degC", freq="YS") gdd gdd = xclim.indices.growing_degree_days(tas=ds.tas, thresh="10.0 degC", freq="YS") gdd

可以通过两种方法(indicators和indices)计算气候指数,二者的区别就是indices结果的元数据属性信息更少。

xclim.indices.growing_degree_days会首先检查输入数据的单位是不是温度,然后进行计算,然后设置合适的输出数据的单位。而 Indicator不仅有更多的元数据的属性信息,还会对数据进行检查。indices假设用户已经检查输入数据是否具有预期的时间频率并且没有缺失值,所以不执行任何检查,因此输出数据可能是错误的(这就是为什么使用来自 CF 层的 ``Indicator`` 对象总是更安全的原因)。

编写indices是为了在数据的频率和单位方面具有灵活性。例如,您可以对 6 小时数据使用 growing_degree_days,但输出将以度小时为单位(“K h”)。此外,所有单位,即使未被计算影响,也将被重新格式化为符合 CF 的符号格式。选择此行为是为了确保所有索引之间的一致性。

简单来说,Indicator = indices+Health Checks +Metadata Attributes

3、其他设置

使用Indicator会产生许多warnings。为了减少warning的输出,可以设置 xclim 将这些警告发送到日志,而不是引发警告或错误。

xclim.set_options(cf_compliance="log")

我们还可以设置 data_validation='warn' 以便能够在非每日数据上运行指标。这些选项可以全局设置或在带有 set_options 的某个代码块中设置,区别就是:是不是使用with。

with xclim.set_options( check_missing="pct", missing_options={"pct": dict(tolerance=0.1)}, cf_compliance="log", ): # Change the missing method to "percent", instead of the default "any" # Set the tolerance to 10%, periods with more than 10% of missing data # in the input will be masked in the ouput. gdd = xclim.atmos.growing_degree_days(daily_ds.air, thresh="10.0 degC", freq="MS")

一些指标还允许在某个时间子集上去运行函数。例如特定的季节,月份,以及一段时间。

with xclim.set_options(cf_compliance="log"): gdd = xclim.atmos.growing_degree_days( tas=daily_ds.air, thresh="10 degC", freq="YS", date_bounds=("04-01", "09-30") ) gdd 4、plot

当输出的结果有时间系列时,可以绘制时间序列。

import matplotlib.pyplot as plt gdd.isel(lon=20, lat=10).plot() plt.suptitle("Time Series at a Given Geographical Coordinate") plt.show()

或者绘制空间图

gdd.sel(time="2013-07-01").plot() plt.suptitle("Spatial Pattern at a Specific Time Period") plt.show()



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3