计算两个不同地理网格大小的数组之间的值的偏差或异常
Calculate departure or anomaly of a value between two arrays of different geographic grid sizes
我有一个技术问题,我整整一周都在努力解决这个问题。
我根据沿着特定轨道的地理网格 (lat/lon) 上的空气质量测量值创建了一个 netcdf 文件。现在我想计算这些值与更大网格的偏离(或异常)(来自具有大面积平均值的计算机模型的数据)。
我的两个netcdf文件结构如下:
观察(仪器测量):
维度:
lat: 1321, lon: 1321
数据变量:
Longitude (lon) float64 8.413 8.411 8.409 ... 4.904 4.905
Latitude (lat) float64 47.4 47.4 47.41 ... 52.37 52.37
obs_data (lat, lon) float64 ...
模型数据:
维度:
latitude: 140, level: 1, longitude: 215, time: 24
坐标:
longitude (longitude) float32 357.55 357.65 ... 18.85 18.95
latitude (latitude) float32 55.95 55.85 55.75 ... 42.15 42.05
level (level) float32 0.0
time (time) timedelta64[ns] 00:00:00 01:00:00 ... 23:00:00
数据变量:
model_data (time, level, latitude, longitude) float32 ...
我尝试了各种不同的方法,但每次我 运行 遇到某种似乎没有解决方案的错误,我最终不得不尝试不同的方法。我得到的最接近的是跟随this great tutorial,但在这里我也碰壁了。
当我试图为两个数据集找到最近的纬度和经度时,
lat_idx = np.abs(model_lat - obs_lat).argmin() #subtract train lat from model lat
lon_idx = np.abs(model_lon - obs_lon).argmin() #subtract train lon from model lon
我收到以下错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-437-9396b00ba22f> in <module>
18
19 # Find the nearest latitude and longitude for the train data
---> 20 lat_idx = np.abs(model_lat - obs_lat).argmin()
21 lon_idx = np.abs(model_lon - obs_lon).argmin()
22
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __sub__(self, other)
4115 if self._delegate_binop(other):
4116 return NotImplemented
-> 4117 return subtract(self, other)
4118
4119 def __rsub__(self, other):
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __call__(self, a, b, *args, **kwargs)
1024 with np.errstate():
1025 np.seterr(divide='ignore', invalid='ignore')
-> 1026 result = self.f(da, db, *args, **kwargs)
1027 # Get the mask for the result
1028 (ma, mb) = (getmask(a), getmask(b))
ValueError: operands could not be broadcast together with shapes (140,) (1321,)
有没有简单计算的方法:
anomaly = model_data[lat, lon] - obs_data[lat, lon]
?
我最新的希望是 xarray
,但我真的很难阅读他们的文档,我花了几天时间寻找前进的方向。
你们中有人找到解决这个问题的办法了吗?非常感谢任何提示。
编辑:
应 V. Ayrat 的要求:
In: type(model_data)
Out: xarray.core.dataset.Dataset
obs_data
是同类型
如果两个 obs_data
值落入同一个 model_data
单元格,则应从同一个 model_data
单元格中减去 obs_data
。
不完全清楚您要做什么或使用什么数据结构。如果稍后有更多信息,我将编辑 post。但是,我认为这可以解决问题:
如果您想要 obs_lat
中最接近 model_lat
的 lat/lon,请使用:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=0)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=0)
如果您想要 model_lat
中最接近 obs_lat
的 lat/lon,请使用:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=1)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=1)
我有一个技术问题,我整整一周都在努力解决这个问题。 我根据沿着特定轨道的地理网格 (lat/lon) 上的空气质量测量值创建了一个 netcdf 文件。现在我想计算这些值与更大网格的偏离(或异常)(来自具有大面积平均值的计算机模型的数据)。
我的两个netcdf文件结构如下:
观察(仪器测量):
维度:
lat: 1321, lon: 1321
数据变量:
Longitude (lon) float64 8.413 8.411 8.409 ... 4.904 4.905
Latitude (lat) float64 47.4 47.4 47.41 ... 52.37 52.37
obs_data (lat, lon) float64 ...
模型数据:
维度:
latitude: 140, level: 1, longitude: 215, time: 24
坐标:
longitude (longitude) float32 357.55 357.65 ... 18.85 18.95
latitude (latitude) float32 55.95 55.85 55.75 ... 42.15 42.05
level (level) float32 0.0
time (time) timedelta64[ns] 00:00:00 01:00:00 ... 23:00:00
数据变量:
model_data (time, level, latitude, longitude) float32 ...
我尝试了各种不同的方法,但每次我 运行 遇到某种似乎没有解决方案的错误,我最终不得不尝试不同的方法。我得到的最接近的是跟随this great tutorial,但在这里我也碰壁了。 当我试图为两个数据集找到最近的纬度和经度时,
lat_idx = np.abs(model_lat - obs_lat).argmin() #subtract train lat from model lat
lon_idx = np.abs(model_lon - obs_lon).argmin() #subtract train lon from model lon
我收到以下错误
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-437-9396b00ba22f> in <module>
18
19 # Find the nearest latitude and longitude for the train data
---> 20 lat_idx = np.abs(model_lat - obs_lat).argmin()
21 lon_idx = np.abs(model_lon - obs_lon).argmin()
22
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __sub__(self, other)
4115 if self._delegate_binop(other):
4116 return NotImplemented
-> 4117 return subtract(self, other)
4118
4119 def __rsub__(self, other):
~/opt/anaconda3/lib/python3.7/site-packages/numpy/ma/core.py in __call__(self, a, b, *args, **kwargs)
1024 with np.errstate():
1025 np.seterr(divide='ignore', invalid='ignore')
-> 1026 result = self.f(da, db, *args, **kwargs)
1027 # Get the mask for the result
1028 (ma, mb) = (getmask(a), getmask(b))
ValueError: operands could not be broadcast together with shapes (140,) (1321,)
有没有简单计算的方法:
anomaly = model_data[lat, lon] - obs_data[lat, lon]
?
我最新的希望是 xarray
,但我真的很难阅读他们的文档,我花了几天时间寻找前进的方向。
你们中有人找到解决这个问题的办法了吗?非常感谢任何提示。
编辑:
应 V. Ayrat 的要求:
In: type(model_data)
Out: xarray.core.dataset.Dataset
obs_data
是同类型
如果两个 obs_data
值落入同一个 model_data
单元格,则应从同一个 model_data
单元格中减去 obs_data
。
不完全清楚您要做什么或使用什么数据结构。如果稍后有更多信息,我将编辑 post。但是,我认为这可以解决问题:
如果您想要 obs_lat
中最接近 model_lat
的 lat/lon,请使用:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=0)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=0)
如果您想要 model_lat
中最接近 obs_lat
的 lat/lon,请使用:
lat_idx = np.abs(model_lat - obs_lat[:,None]).argmin(axis=1)
lon_idx = np.abs(model_lon - obs_lon[:,None]).argmin(axis=1)