This is the first time I am asking a question here, so let me know if you require more information to suggest a solution.
I have a three dimensional boolean data array (time, lat, lon), which I have processed using the xarray library in python 3. The data array I am working with includes data for one year and it has daily timesteps (365-366 days depending on whether it is a leap year or not).
The dimensions of a sample data array are shown here, and a sample netcdf file can be downloaded here and load as da = xr.open_dataset('data.nc')
. This sample includes five time steps only.
I would like to know how long is the longest sequence of True
values for each cell (or pixel). The output containing the longest sequence should be a two dimensional data array or data frame. So, for example, if a cell has values of [True, True, True, False, True]
, the result I want to get for that pixel is 3
as this represents the three consecutive True
values.
I have tried using cumulative sums across time using da.cumsum('time')
, but this adds up all values, even if they are not consecutive, but this is not what I want.
Similar questions have been asked before for two dimensional dataframes, for example here and here. But I have not been able to implement these solutions successfully in a three dimensional dataframe.
Since I am fairly new to Python and xarray in particular, I cannot figure out how I could achieve this. Any ideas would be appreciated.