[英]xarray - Return data that meets certain criteria without having to use for loop
I am working with multi-dimensional data da_criteria_1or0_hourly
. 我正在使用多维数据
da_criteria_1or0_hourly
。
The data (variable) is either 1 or 0. 数据(变量)为1或0。
print(da_criteria_1or0_hourly)
Output: 输出:
<xarray.DataArray (time: 8760, latitude: 106, longitude: 193)>
dask.array<shape=(8760, 106, 193), dtype=int32, chunksize=(744, 106, 193)>
Coordinates:
* latitude (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
* longitude (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
* time (time) datetime64[ns] 2017-01-01 ... 2017-12-31T23:00:00
The data is as massive as 179212080. 数据量高达179212080。
I am unsure which method on xarray I should use to get a new xarray object that only return data which is 1
( 0
can be assigned NaN or dropped). 我不确定应该使用哪种方法在xarray上获取新的xarray对象,该对象仅返回数据为
1
(可以为NaN分配0
或将其丢弃)。
I was trying to print out data for each coordinate using sel
using FOR LOOP, but this was extremely slow. 我试图使用FOR LOOP使用
sel
打印每个坐标的数据,但这非常慢。 It could take forever. 这可能会永远。
for time_elem in da_criteria_1or0_hourly.coords['time'].values:
for lat_elem in da_criteria_1or0_hourly.coords['latitude'].values:
for lon_elem in da_criteria_1or0_hourly.coords['longitude'].values:
val = da_criteria_1or0_hourly.sel(time=time_elem,latitude=lat_elem,longitude=lon_elem).values
if (val == 1):
print(time_elem, lat_elem, lon_elem, val)
Is there any better way to do it? 有什么更好的方法吗?
If I understood your problem correctly xarray.Dataset.where
should be the way to go: 如果我正确理解了您的问题,
xarray.Dataset.where
应该使用xarray.Dataset.where
:
da_criteria_1or0_hourly.where(da_criteria_1or0_hourly == 1)
This will return a new xarray where every entry that was not 1 is now NaN. 这将返回一个新的xarray,其中每个不为1的条目现在都是NaN。
Time comparison for a 50x50x50 xarray: 50x50x50 xarray的时间比较:
for loops: ~56.9456s for循环:〜56.9456s
where
: ~00.0020s where
:〜00.0020s
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.