@@ -24,14 +24,27 @@ class MisType(Warning):
24
24
def _typer (raise_mistype = False ):
25
25
def decorator (func ):
26
26
def force (* args , ** kwargs ):
27
+ _args = list (args )
28
+ idx = 1
27
29
for key , val in func .__annotations__ .items ():
28
- if val not in _SUPPORTED_DTYPE or kwargs .get (key , None ) is None :
30
+ is_kwargs = key in kwargs .keys ()
31
+ if val not in _SUPPORTED_DTYPE or kwargs .get (key , None ) is None and is_kwargs or len (args )== 1 :
29
32
continue
30
- if raise_mistype and val != type (kwargs .get (key )):
31
- raise MisType (
32
- f"{ key } expected a { val .__name__ } , not a { type (kwargs [key ]).__name__ } ({ kwargs [key ]} )"
33
+ if raise_mistype and (val != type (kwargs .get (key )) if is_kwargs else val != type (args [idx ])):
34
+ if is_kwargs :
35
+ expected = f"{ type (kwargs [key ]).__name__ } ({ kwargs [key ]} )"
36
+ else :
37
+ expected = f"{ type (args [idx ]).__name__ } ({ args [idx ]} )"
38
+
39
+ raise MisType (
40
+ f"{ key } expected a { val .__name__ } , not a { expected } ."
33
41
)
34
- kwargs [key ] = val (kwargs [key ]) if val != list else [kwargs [key ]]
42
+ if is_kwargs :
43
+ kwargs [key ] = val (kwargs [key ]) if val != list else [kwargs [key ]]
44
+ else :
45
+ _args [idx ] = val (args [idx ]) if val != list else [args [idx ]]
46
+ idx += 1
47
+ args = tuple (_args )
35
48
return func (* args , ** kwargs )
36
49
37
50
return force
@@ -103,22 +116,24 @@ def _lee_filter(img, window_size: int):
103
116
img_output = xr .where (np .isnan (binary_nan ), img_ , img_output )
104
117
return img_output
105
118
106
-
107
119
@xr .register_dataarray_accessor ("ed" )
108
120
class EarthDailyAccessorDataArray :
109
121
def __init__ (self , xarray_obj ):
110
122
self ._obj = xarray_obj
123
+
124
+ def _max_time_wrap (self , wish = 5 ):
125
+ return np .min ((wish ,self ._obj ['time' ].size ))
111
126
112
127
@_typer ()
113
128
def plot_band (self , cmap = "Greys" , col = "time" , col_wrap = 5 , ** kwargs ):
114
- return self ._obj .plot .imshow (cmap = cmap , col = col , col_wrap = col_wrap , ** kwargs )
129
+ return self ._obj .plot .imshow (cmap = cmap , col = col , col_wrap = self . _max_time_wrap ( col_wrap ) , ** kwargs )
115
130
116
131
@_typer ()
117
132
def plot_index (
118
133
self , cmap = "RdYlGn" , vmin = - 1 , vmax = 1 , col = "time" , col_wrap = 5 , ** kwargs
119
134
):
120
135
return self ._obj .plot .imshow (
121
- vmin = vmin , vmax = vmax , cmap = cmap , col = col , col_wrap = col_wrap , ** kwargs
136
+ vmin = vmin , vmax = vmax , cmap = cmap , col = col , col_wrap = self . _max_time_wrap ( col_wrap ) , ** kwargs
122
137
)
123
138
124
139
@@ -127,6 +142,10 @@ class EarthDailyAccessorDataset:
127
142
def __init__ (self , xarray_obj ):
128
143
self ._obj = xarray_obj
129
144
145
+ def _max_time_wrap (self , wish = 5 ):
146
+ return np .min ((wish ,self ._obj ['time' ].size ))
147
+
148
+
130
149
@_typer ()
131
150
def plot_rgb (
132
151
self ,
@@ -140,21 +159,21 @@ def plot_rgb(
140
159
return (
141
160
self ._obj [[red , green , blue ]]
142
161
.to_array (dim = "bands" )
143
- .plot .imshow (col = col , col_wrap = col_wrap , ** kwargs )
162
+ .plot .imshow (col = col , col_wrap = self . _max_time_wrap ( col_wrap ) , ** kwargs )
144
163
)
145
164
146
165
@_typer ()
147
166
def plot_band (self , band , cmap = "Greys" , col = "time" , col_wrap = 5 , ** kwargs ):
148
167
return self ._obj [band ].plot .imshow (
149
- cmap = cmap , col = col , col_wrap = col_wrap , ** kwargs
168
+ cmap = cmap , col = col , col_wrap = self . _max_time_wrap ( col_wrap ) , ** kwargs
150
169
)
151
170
152
171
@_typer ()
153
172
def plot_index (
154
173
self , index , cmap = "RdYlGn" , vmin = - 1 , vmax = 1 , col = "time" , col_wrap = 5 , ** kwargs
155
174
):
156
175
return self ._obj [index ].plot .imshow (
157
- vmin = vmin , vmax = vmax , cmap = cmap , col = col , col_wrap = col_wrap , ** kwargs
176
+ vmin = vmin , vmax = vmax , cmap = cmap , col = col , col_wrap = self . _max_time_wrap ( col_wrap ) , ** kwargs
158
177
)
159
178
160
179
@_typer ()
@@ -216,7 +235,7 @@ def _auto_mapper(self):
216
235
params [_BAND_MAPPING [v ]] = self ._obj [v ]
217
236
return params
218
237
219
- def list_available_index (self , details = False ):
238
+ def available_index (self , details = False ):
220
239
mapper = list (self ._auto_mapper ().keys ())
221
240
indices = spyndex .indices
222
241
available_indices = []
@@ -248,9 +267,7 @@ def add_index(self, index: list, **kwargs):
248
267
"""
249
268
250
269
params = {}
251
- bands_mapping = self ._auto_mapper ()
252
- for k , v in bands_mapping .items ():
253
- params [k ] = self ._obj [v ]
270
+ params = self ._auto_mapper ()
254
271
params .update (** kwargs )
255
272
idx = spyndex .computeIndex (index = index , params = params , ** kwargs )
256
273
0 commit comments