gpt4 book ai didi

python - 在 python 中关联网格数据集

转载 作者:行者123 更新时间:2023-12-05 05:20:57 25 4
gpt4 key购买 nike

我有两个格式为(时间、纬度、经度)的液态水等效厚度月度全局网格化数据集。两者具有相同的空间和时间分辨率。我想关联它们,但 numpy.corrcoef() 仅适用于 2D 数组,不适用于 3D。所以我想关联整个时间序列的两个变量的相同网格点 (x,y)。事实上,我想要一个带有相关系数网格的新 nc 文件。

import numpy as np
from netCDF4 import Dataset

wdir = '.../Data/'

# read GRACE NCs
GRACE_GFZ = Dataset(wdir+'GRACE/GRCTellus.GFZ.200204_201607.nc','r')
GRACE_JPL = Dataset(wdir+'GRACE/GRCTellus.JPL.200204_201607.nc','r')

两个变量(gfz 和 jpl)在相同位置具有相同数量的缺失值。

GRACE_GFZ.variables['lwe_thickness']
<type 'netCDF4._netCDF4.Variable'>
float32 lwe_thickness(time, lat, lon)
long_name: Liquid_Water_Equivalent_Thickness
units: cm
_FillValue: 32767.0
missing_value: 32767.0
unlimited dimensions: time
current shape = (155, 72, 144)
filling off

GRACE_JPL.variables['lwe_thickness']
<type 'netCDF4._netCDF4.Variable'>
float32 lwe_thickness(time, lat, lon)
long_name: Liquid_Water_Equivalent_Thickness
units: cm
_FillValue: 32767.0
missing_value: 32767.0
unlimited dimensions: time
current shape = (155, 72, 144)
filling off

由于它们具有相同的时间和空间分辨率,因此两者的时间、经度和纬度都可以使用。

time = GRACE_GFZ.variables['time'][:]
lons = GRACE_GFZ.variables['lon'][:]
lats = GRACE_GFZ.variables['lat'][:]
gfz = GRACE_GFZ.variables['lwe_thickness'][:]
jpl = GRACE_JPL.variables['lwe_thickness'][:]

这里我想遍历网格并将 corrcoef 放入数组中。这给了我一堆 2x2 矩阵。

test = []
for x in range(len(lats)):
for y in range(len(lons)):
print(np.corrcoef(gfz[:,x,y],jpl[:,x,y]))

如何将每个点的相关系数放入新数组的正确位置?

最佳答案

我解决了以下问题:

temp =[]
corrcoefMatrix_gfzjpl = [[0 for i in range(len(lons))] for j in range(len(lats))]
for x in range(len(lats)):
for y in range(len(lons)):
temp = np.corrcoef(gfz[:,x,y],jpl[:,x,y])
corrcoefMatrix_gfzjpl[x][y] = temp[0,1]

corrcoefMatrix_gfzjpl = np.squeeze(np.asarray(corrcoefMatrix_gfzjpl))

基本上,我制作了一个包含零的矩阵,并将它们替换为 corrcoef 矩阵中的相关系数值。我通过对每个网格单元进行 for 循环来遍历纬度和经度来为每个网格单元执行此操作。之后我创建了一个新的 netcdf 文件,定义了所有维度和变量。

nc_data.createDimension('lat', len(lats))
nc_data.createDimension('lon', len(lons))
nc_data.createDimension('data', 1)

latitudes = nc_data.createVariable('latitude', 'f4', ('lat',))
longitudes = nc_data.createVariable('longitude', 'f4', ('lon',))
corrcoef_gfzjpl = nc_data.createVariable('corrcoef_gfzjpl', 'f4', ('lat', 'lon', 'data'), fill_value=-999.0)

latitudes.units = 'degree_north'
longitudes.units = 'degree_east'
latitudes[:] = np.arange(-90, 90, 180./len(lats))
longitudes[:] = np.arange(0, 360, 360./len(lons))
corrcoef_gfzjpl[:,:] = corrcoefMatrix_gfzjpl[:,:]

非常欢迎提出改进建议!

关于python - 在 python 中关联网格数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43864768/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com