Skip to content

Commit

Permalink
Merge pull request #48 from earthdaily/dev
Browse files Browse the repository at this point in the history
v0.0.9
  • Loading branch information
nkarasiak authored Feb 29, 2024
2 parents 01ee6c5 + 5a6593a commit 5ca8fe5
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 28 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.0.9] - 2024-02-29

### Fixed

- `_typer` has a better args management.
- `available_indices` returns only indices that can be computed.

## [0.0.8] - 2024-02-28

### Added
Expand All @@ -24,7 +31,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed

- `to_wkt`for GeometryManager.
- `to_wkt` for GeometryManager.

### Added

Expand Down
6 changes: 5 additions & 1 deletion earthdaily/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from . import earthdatastore, datasets
from .accessor import EarthDailyAccessorDataArray, EarthDailyAccessorDataset
import warnings

__version__ = "0.0.8"
# to hide warnings from rioxarray or nano seconds conversion
warnings.filterwarnings("ignore")

__version__ = "0.0.9"
54 changes: 38 additions & 16 deletions earthdaily/accessor/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,29 @@ def force(*args, **kwargs):
idx = 1
for key, val in func.__annotations__.items():
is_kwargs = key in kwargs.keys()
if val not in _SUPPORTED_DTYPE or kwargs.get(key, None) is None and is_kwargs or len(args)==1:
if not is_kwargs and idx >= len(args):
continue
if raise_mistype and (val != type(kwargs.get(key)) if is_kwargs else val != type(args[idx])):
input_value = kwargs.get(key, None) if is_kwargs else args[idx]
if type(input_value) == val:
continue
if raise_mistype and (
val != type(kwargs.get(key))
if is_kwargs
else val != type(args[idx])
):
if is_kwargs:
expected = f"{type(kwargs[key]).__name__} ({kwargs[key]})"
else:
expected = f"{type(args[idx]).__name__} ({args[idx]})"

raise MisType(
f"{key} expected a {val.__name__}, not a {expected}."
)
f"{key} expected a {val.__name__}, not a {expected}."
)
if is_kwargs:
kwargs[key] = val(kwargs[key]) if val != list else [kwargs[key]]
else:
elif len(args) >= idx:
_args[idx] = val(args[idx]) if val != list else [args[idx]]
idx+=1
idx += 1
args = tuple(_args)
return func(*args, **kwargs)

Expand Down Expand Up @@ -116,24 +123,32 @@ def _lee_filter(img, window_size: int):
img_output = xr.where(np.isnan(binary_nan), img_, img_output)
return img_output


@xr.register_dataarray_accessor("ed")
class EarthDailyAccessorDataArray:
def __init__(self, xarray_obj):
self._obj = xarray_obj

def _max_time_wrap(self, wish=5):
return np.min((wish,self._obj['time'].size))
return np.min((wish, self._obj["time"].size))

@_typer()
def plot_band(self, cmap="Greys", col="time", col_wrap=5, **kwargs):
return self._obj.plot.imshow(cmap=cmap, col=col, col_wrap=self._max_time_wrap(col_wrap), **kwargs)
return self._obj.plot.imshow(
cmap=cmap, col=col, col_wrap=self._max_time_wrap(col_wrap), **kwargs
)

@_typer()
def plot_index(
self, cmap="RdYlGn", vmin=-1, vmax=1, col="time", col_wrap=5, **kwargs
):
return self._obj.plot.imshow(
vmin=vmin, vmax=vmax, cmap=cmap, col=col, col_wrap=self._max_time_wrap(col_wrap), **kwargs
vmin=vmin,
vmax=vmax,
cmap=cmap,
col=col,
col_wrap=self._max_time_wrap(col_wrap),
**kwargs,
)


Expand All @@ -143,9 +158,8 @@ def __init__(self, xarray_obj):
self._obj = xarray_obj

def _max_time_wrap(self, wish=5):
return np.min((wish,self._obj['time'].size))


return np.min((wish, self._obj["time"].size))

@_typer()
def plot_rgb(
self,
Expand Down Expand Up @@ -173,7 +187,12 @@ def plot_index(
self, index, cmap="RdYlGn", vmin=-1, vmax=1, col="time", col_wrap=5, **kwargs
):
return self._obj[index].plot.imshow(
vmin=vmin, vmax=vmax, cmap=cmap, col=col, col_wrap=self._max_time_wrap(col_wrap), **kwargs
vmin=vmin,
vmax=vmax,
cmap=cmap,
col=col,
col_wrap=self._max_time_wrap(col_wrap),
**kwargs,
)

@_typer()
Expand Down Expand Up @@ -235,20 +254,23 @@ def _auto_mapper(self):
params[_BAND_MAPPING[v]] = self._obj[v]
return params

def available_index(self, details=False):
def available_indices(self, details=False):
mapper = list(self._auto_mapper().keys())
indices = spyndex.indices
available_indices = []
for k, v in indices.items():
needed_bands = v.bands
missing_bands = False
for needed_band in needed_bands:
if needed_band not in mapper:
missing_bands = True
break
if missing_bands is False:
available_indices.append(spyndex.indices[k] if details else k)
return available_indices

@_typer()
def add_index(self, index: list, **kwargs):
def add_indices(self, index: list, **kwargs):
"""
Uses spyndex to compute and add index.
Expand Down
4 changes: 1 addition & 3 deletions examples/field_evolution.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@
# Plots cube with SCL with at least 50% of clear data
# ----------------------------------------------------
pivot_cube = pivot_cube.load()
pivot_cube.to_array(dim="band").plot.imshow(
vmin=0, vmax=0.4, col="time", col_wrap=3
)
pivot_cube.ed.plot_rgb(col_wrap=3)

plt.title("Clear cover percent with SCL")
plt.title("Pivot evolution with SCL masks")
Expand Down
11 changes: 4 additions & 7 deletions examples/multisensors_cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
)

# Add the NDVI
datacube = datacube.ed.add_index('NDVI')
datacube = datacube.ed.add_indices(['NDVI'])

# Load in memory
datacube = datacube.load()
Expand All @@ -52,18 +52,15 @@
# See the evolution in RGB
# -------------------------------------------

datacube[["red", "green", "blue"]].to_array(dim="band").plot.imshow(
col="time", col_wrap=3, vmax=0.2
)
datacube.ed.plot_rgb(col_wrap=3)
plt.show()


##############################################################################
# See the NDVI evolution
# -------------------------------------------

datacube["NDVI"].plot.imshow(
col="time", col_wrap=3, vmin=0, vmax=0.8, cmap="RdYlGn"
)
datacube["NDVI"].ed.plot_index(col_wrap=3, vmin=0, vmax=0.8, cmap="Greens")
plt.show()

##############################################################################
Expand Down

0 comments on commit 5ca8fe5

Please sign in to comment.