Reputation: 3515
I have a xarray DataArray object da_criteria_daily
where are generated from a netCDF file.
<xarray.DataArray (time: 365, latitude: 106, longitude: 193)>
dask.array<shape=(365, 106, 193), dtype=bool, chunksize=(1, 106, 193)>
Coordinates:
* time (time) datetime64[ns] 2017-01-01 2017-01-02 ... 2017-12-31
* latitude (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
* longitude (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
It's a daily data over a year for a geographic extent. The variable is a Boolean type.
I would like to get all coordinates (latitude, longitude) values for a particular date where the variable is True.
new_da = da_criteria_daily.where(da_criteria_daily==True, drop=True)
print(new_da)
I got:
<xarray.DataArray (time: 161, latitude: 106, longitude: 193)>
dask.array<shape=(161, 106, 193), dtype=float64, chunksize=(1, 106, 193)>
Coordinates:
* time (time) datetime64[ns] 2017-01-01 2017-01-02 ... 2017-12-31
* latitude (latitude) float32 -39.2 -39.149525 ... -33.950478 -33.9
* longitude (longitude) float32 140.8 140.84792 140.89584 ... 149.95209 150.0
Are you any functions with xarray.DataArray that can be used to retrieve coordinates from variables?
Upvotes: 5
Views: 3241
Reputation: 338
You can do it with numpy this way:
numpy.argwhere(numpy.array(da_criteria_daily))
numpy.argwhere()
gives you the indexes of the non-zero (True) elements. Then you can use the longitude, latitude and time coord arrays to get the real values.
Using xarray's .where()
will only replace the values that do not meet the condition with nans, that is, your False values will now be nans. I don't know of any method of xarray to get the coordinates of certain values. It would definitively be useful but I guess no one has developed it yet.
Upvotes: 6