首页 > 解决方案 > 为什么 nc 文件中的 lat 和 lon 维度会因组装两个 nc 文件而改变?

问题描述

我有两个.nc文件,它们具有相似的纬度和经度尺寸、CRS 和其他所有内容。

Dimensions:  (lat: 62, lon: 94)
Coordinates:
  * lat      (lat) float64 58.46 58.79 59.12 59.44 ... 77.45 77.78 78.11 78.44
  * lon      (lon) float64 -150.0 -149.4 -148.9 -148.3 ... -98.56 -98.0 -97.43

然后当我将这两个文件添加在一起时,整个维度都会发生变化:

dem = xr.open_dataset(path + 'DEM.nc')
dc = xr.open_dataset(path + 'DC.nc')

X = 1 / (1 + np.exp(-(-2.2432e+01 + (dem.Band1 * 1.97521e-06) + (dc.Band1 * 5.97414e-04))))

print(X)

<xarray.DataArray 'Band1' (lat: 11, lon: 94)>
  * lat      (lat) float64 65.01 65.34 65.67 65.99 ... 73.52 74.18 77.78 78.44
  * lon      (lon) float64 -150.0 -149.4 -148.9 -148.3 ... -98.56 -98.0 -97.43

怎么了?为什么这个添加会改变整个维度?

注意。该dc文件有一些没有数据的网格单元nan。这会影响它吗?

编辑:给出答案后,我仍然认为这不仅仅是因为缺少数据,请单独查看数据集的图像并将数据集组合起来:

在此处输入图像描述

标签: pythonnetcdfpython-xarray

解决方案


你已经给自己答案了

the dc file has some grid cells without data nan. Does that influence it? 是的,因为 xarray 也在寻找坐标,而不仅仅是进行矢量化操作。查看文档以了解更多信息。

解决方法很简单:让 numpy 为您完成这项工作!

X = 1 / (1 + np.exp(-(-2.2432e+01 + (dem.Band1.values * 1.97521e-06) + (dc.Band1.values * 5.97414e-04))))

data_array = xarray.DataArray(X, coords={'lat': dem.lat, 'lon': dem.lon}, dims=['lat', 'lon'])

dem['combined'] = data_array

推荐阅读