简体   繁体   English

如何在 3D 阵列中的特定(变化)时间步长处剪辑多个点?

[英]How to clip multiple points at specific (varying) timesteps in a 3D Array?

I want to clip a multidimensional array with points (shapefile).我想用点(shapefile)剪辑一个多维数组。 The points are specific events and have a lat, lon and time value.这些点是特定事件,具有纬度、经度和时间值。 I lcreate from the columns of the shapefile: lat, lon and time a list (for each), to then in a next step select/clip with these lists the multidimensional array (using the function xarray.sel):我从 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")

With this split of the lat, lon, time to separated lists, they lose their relation to one another, which means that all points are cut out for each timestep and not for the specific dates they occured.. Do you have any ideas how I could clip the lat and lon at the specific timesteps in a 3D array?随着纬度,经度,时间到分开的列表的这种拆分,它们失去了彼此的关系,这意味着所有点都被删除了每个时间步,而不是它们发生的特定日期。你有什么想法我是怎么做到的可以在 3D 阵列中的特定时间步长剪辑纬度和经度吗?

Rather than converting x, y, and time to a list, convert them to an xarray.DataArray using pd.Series.to_xarray() .与其将 x、y 和时间转换为列表,不如使用pd.Series.to_xarray()将它们转换为xarray.DataArray This allows you to make use of xarray's Advanced Indexing mode, where you don't just filter the (lat, lon, time) dimensions you actually reshape the array to conform to the index of the selectors.这使您可以使用 xarray 的高级索引模式,在这种模式下,您不仅可以过滤(lat, lon, time)维度,还可以实际重塑数组以符合选择器的索引。 The following will reshape the lat, lon, and time dimensions to pull the points you request out of the array, maintaining their relationship to each other and setting a new dimension matching the index of your dataframe:以下将重塑 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM