【问题标题】:Calculate netcdf files' variables correlation and plot on the map with Python使用 Python 计算 netcdf 文件的变量相关性并在地图上绘制
【发布时间】:2021-04-15 00:27:47
【问题描述】:

给定两个 nc 文件here:

data/CMIP6_UKESM1-0-LL_Lmon_piControl_r1i1p1f2_gpp_1960-3059.nc
data/CMIP6_UKESM1-0-LL_Amon_piControl_r1i1p1f2_tas_1960-3059.nc

读取第一个文件:

from netCDF4 import Dataset
import numpy as np

ds1 = Dataset('data/CMIP6_UKESM1-0-LL_Lmon_piControl_r1i1p1f2_gpp_1960-3059.nc')
print(ds1.variables.keys()) # get all variable names

输出:

odict_keys(['gpp', 'time', 'time_bnds', 'lat', 'lat_bnds', 'lon', 'lon_bnds', 'clim_season', 'season_year'])

读取第二个文件:

ds2 = Dataset('data/CMIP6_UKESM1-0-LL_Amon_piControl_r1i1p1f2_tas_1960-3059.nc')
print(ds2.variables.keys()) 

输出:

odict_keys(['tas', 'time', 'time_bnds', 'lat', 'lat_bnds', 'lon', 'lon_bnds', 'clim_season', 'height', 'season_year'])

检查gpp变量:

gpp = ds1.variables['gpp'] # gpp variable
print(gpp)

输出:

<class 'netCDF4._netCDF4.Variable'>
float32 gpp(time, lat, lon)
    _FillValue: 1e+20
    standard_name: gross_primary_productivity_of_biomass_expressed_as_carbon
    long_name: Carbon Mass Flux out of Atmosphere Due to Gross Primary Production on Land [kgC m-2 s-1]
    units: kg m-2 s-1
    cell_methods: area: mean where land time: mean
    coordinates: clim_season season_year
unlimited dimensions: 
current shape = (3300, 144, 192)
filling on

检查tas变量:

tas = ds2.variables['tas'] # tas variable
print(tas)

输出:

<class 'netCDF4._netCDF4.Variable'>
float32 tas(time, lat, lon)
    _FillValue: 1e+20
    standard_name: air_temperature
    long_name: Near-Surface Air Temperature
    units: K
    cell_methods: area: time: mean
    coordinates: clim_season height season_year
unlimited dimensions: 
current shape = (3300, 144, 192)
filling on

现在我想计算gpptas 之间的相关性,然后在地图上绘制它们的相关值。

我怎么能这样做?谢谢。

【问题讨论】:

    标签: python-3.x correlation netcdf matplotlib-basemap netcdf4


    【解决方案1】:

    您应该可以使用我的包 nctoolkit (https://nctoolkit.readthedocs.io/en/latest/) 轻松完成此操作。

    我的理解是你想绘制每个网格单元的时间相关系数。在这种情况下:

    import nctoolkit as nc
    files = ["data/CMIP6_UKESM1-0-LL_Lmon_piControl_r1i1p1f2_gpp_1960-3059.nc",
    "data/CMIP6_UKESM1-0-LL_Amon_piControl_r1i1p1f2_tas_1960-3059.nc"]
    ds = nc.open_data(files)
    ds.merge()
    ds.cor_time(var1 = "gpp", var2 =  "tas")
    ds.plot()
    

    如果您想要每个时间步长的变量之间的空间相关系数,可以使用以下行代替倒数第二行:

    ds.cor_space(var1 = "gpp", var2 =  "tas")
    

    【讨论】:

    • 对不起,它会引发错误:AttributeError: 'LGEOS360' object has no attribute 'GEOSBufferWithParams'
    • 这听起来像是 conda stackoverflow.com/questions/65027227/… 的卡托普问题
    • ds.to_xarray().cor.plot() 应该代替最后一行快速绘制,以解决任何卡托普问题
    • 第一行代码出现错误:nc.open_data.
    • 您是指import nctoolkit 行吗? open_data 中没有任何内容会引发这样的错误。上周我正在使用 UKESM cmip6 数据,因此该软件包应该能够毫无问题地处理它
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-06
    相关资源
    最近更新 更多