[英]How to clip multiple points at specific (varying) timesteps in a 3D Array?
我想用点(shapefile)剪辑一个多维数组。 这些点是特定事件,具有纬度、经度和时间值。 我从 shapefile 的列中创建:lat、lon 和 time 一个列表(对于每个),然后在下一步中使用这些列表选择/剪辑多维数组(使用 function xarray.sel):
lons = pts.geometry.x.to_list()
lats = pts.geometry.y.to_list()
time = pts.time.to_list()
values_pts = 3D_array.sel(lon=lons, lat=lats, time=time, method="nearest")
随着纬度,经度,时间到分开的列表的这种拆分,它们失去了彼此的关系,这意味着所有点都被删除了每个时间步,而不是它们发生的特定日期。你有什么想法我是怎么做到的可以在 3D 阵列中的特定时间步长剪辑纬度和经度吗?
与其将 x、y 和时间转换为列表,不如使用pd.Series.to_xarray()
将它们转换为xarray.DataArray
。 这使您可以使用 xarray 的高级索引模式,在这种模式下,您不仅可以过滤(lat, lon, time)
维度,还可以实际重塑数组以符合选择器的索引。 以下将重塑 lat、lon 和 time 维度,以将您请求的点从数组中拉出,保持它们之间的关系并设置与 dataframe 的索引匹配的新维度:
lons = pts.geometry.x.to_xarray()
lats = pts.geometry.y.to_xarray()
time = pts.time.to_xarray()
# because lons, lats, and time all have the same indexing
# dim, which is the the index of `pts`, the following will
# pull the points you're requesting out of the array and
# reshape them into a 1-D vector indexed by the common
# indexing dimension
values_pts = 3D_array.sel(
lon=lons, lat=lats, time=time, method="nearest"
)
# you could now convert this back to a pandas.Series
# (if 3D_array is a DataArray) or pandas.DataFrame
# (if it's a Dataset) if desired:
values_pts.to_series()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.