html_url,issue_url,id,node_id,user,created_at,updated_at,author_association,body,reactions,performed_via_github_app,issue
https://github.com/pydata/xarray/issues/3957#issuecomment-868260575,https://api.github.com/repos/pydata/xarray/issues/3957,868260575,MDEyOklzc3VlQ29tbWVudDg2ODI2MDU3NQ==,40410085,2021-06-25T06:33:47Z,2021-06-25T07:45:20Z,NONE,"Hello @zxdawn and @JavierRuano:
I am new to python. And I've been working on a different approach to this issue of ranking data on 3D array. I believe I am close to a solution. I am able to generate ranks on a 3D array, but I can't figure out how to map those ranks to reorder the data from lowest to highest using the produced indexes. Perhaps you might know what needs to happen next?
Cheers,
-Jonathan
code:
import xarray as xr
import os
import numpy as np
from xarray import DataArray
from dask.distributed import Client
c = Client()
I am trying to produce a simpler version:
# coding: utf-8
# In[1]:
import xarray as xr
import os
import numpy as np
from xarray import DataArray
# In[2]:
from dask.distributed import Client
c = Client()
# In[19]:
def calculate_rank(x):
return x.rank(dim='time')
# In[4]:
lat = 2
# In[5]:
lon = 3
# In[6]:
time = 5
# In[7]:
data = [[[ 29, 19, 8],
[ 12, 7, 21]],
[[ 3, 4, 2],
[ 18, 10, 24]],
[[6, 28, 14],
[15, 16, 17]],
[[9, 1, 20],
[5, 27, 26]],
[[11, 25, 23],
[22, 13, 0]]]
# In[8]:
data_xr = xr.DataArray(data, dims=['time', 'lat', 'lon'], coords={'time': np.arange(time)})
# In[9]:
data_xr.values
# Groupby on all of the data
# In[10]:
stacked_object = data_xr.stack(gridcell=['lat','lon'])#.chunk({'gridcell':500})
# In[11]:
stacked_object.load()
# In[20]:
TSA_Rank = stacked_object.groupby('gridcell').apply(calculate_rank).unstack()
# In[21]:
TSA_Rank
# In[22]:
TSA_Rank.values
array([[[5., 3., 2.],
[2., 1., 3.]],
[[1., 2., 1.],
[4., 2., 4.]],
[[2., 5., 3.],
[3., 4., 2.]],
[[3., 1., 4.],
[1., 5., 5.]],
[[4., 4., 5.],
[5., 3., 1.]]])
# In[23]:
data_xr.values
array([[[29, 19, 8],
[12, 7, 21]],
[[ 3, 4, 2],
[18, 10, 24]],
[[ 6, 28, 14],
[15, 16, 17]],
[[ 9, 1, 20],
[ 5, 27, 26]],
[[11, 25, 23],
[22, 13, 0]]])","{""total_count"": 0, ""+1"": 0, ""-1"": 0, ""laugh"": 0, ""hooray"": 0, ""confused"": 0, ""heart"": 0, ""rocket"": 0, ""eyes"": 0}",,596606599