xarray/rioxarray apply linear regression with apply_ufunc

I have an xarray dataset called stacked which looks like this:

Dimensions:      (time: 42, longitude: 1383, latitude: 586)
Coordinates:
  * time         (time) datetime64[ns] 1979-01-01 1980-01-01 ... 2020-01-01
  * longitude    (longitude) float64 -1.732e+07 -1.73e+07 ... 1.73e+07 1.732e+07
  * latitude     (latitude) float64 7.332e+06 7.307e+06 ... -7.332e+06
    spatial_ref  int32 0
Data variables:
    tmean        (time, latitude, longitude) float32 nan nan nan ... nan nan nan

It represents a time series of yearly temperature (42 years). I want to see how temperature has changed over time (for each pixel location individually) using linear regression and the apply_ufunc command. This should be very similar to this detailed answer here, Applying numpy.polyfit to xarray Dataset by AWilliams3142. I specifically want to use the chunking aspect of the answer.

I am trying this:

from scipy import stats
import xarray as xr

def linear_trend(x, y):
    # Wrapper around scipy linregress to use in apply_ufunc
    slope, intercept, r_value, p_value, std_err = stats.linregress(x, y)
    return np.array([slope])


# #set chunks to speed things up, tmean = y, time = x.
dask_y  = stacked.tmean.chunk({'latitude':10, 'longitude':10})
dask_x  = stacked.time.chunk({'latitude':10, 'longitude':10})

#regress input variablee against time, e.g. variable depends on time
slopes_dask = xr.apply_ufunc(linear_trend,
                             dask_x, dask_y, #this is x,y in regression
                             vectorize=True,
                             dask='parallelized',
                             input_core_dims=[['time'], ['time']], 
                             output_dtypes=['d'],
                             )

print(slopes_dask)

but this returns:

File ~\Anaconda3\envs\gdal\lib\site-packages\xarray\core\dataset.py:2157, in Dataset.chunk(self, chunks, name_prefix, token, lock)
   2155 bad_dims = chunks.keys() - self.dims.keys()
   2156 if bad_dims:
-> 2157     raise ValueError(
   2158         f"some chunks keys are not dimensions on this object: {bad_dims}"
   2159     )
   2161 variables = {
   2162     k: _maybe_chunk(k, v, chunks, token, lock, name_prefix)
   2163     for k, v in self.variables.items()
   2164 }
   2165 return self._replace(variables)

ValueError: some chunks keys are not dimensions on this object: {'latitude', 'longitude'}


from Recent Questions - Stack Overflow https://ift.tt/KUBR67v
https://ift.tt/t4nY2pl

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)