-
Notifications
You must be signed in to change notification settings - Fork 6
Added a plugin for processing level3 Land Cover product. #151
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c19c33b
Added a plugin for land cover level 3 and a unit test for this level.
tebadi 6950f8f
Add the level3 unit test
tebadi dda7629
Applied formatting.
tebadi d91706a
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] f7906ad
Added comments on classes and passed cultivated classification as is.
tebadi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
""" | ||
Land Cover Level3 classification | ||
""" | ||
|
||
from typing import Tuple | ||
import xarray as xr | ||
from ._registry import StatsPluginInterface, register | ||
|
||
NODATA = 255 | ||
|
||
|
||
class StatsLccsLevel3(StatsPluginInterface): | ||
NAME = "ga_ls_lccs_level3" | ||
SHORT_NAME = NAME | ||
VERSION = "0.0.1" | ||
PRODUCT_FAMILY = "lccs" | ||
|
||
@property | ||
def measurements(self) -> Tuple[str, ...]: | ||
_measurements = ["level3_class"] | ||
return _measurements | ||
|
||
def reduce(self, xx: xr.Dataset) -> xr.Dataset: | ||
|
||
l34_dss = xx.classes_l3_l4 | ||
urban_dss = xx.urban_classes | ||
cultivated_dss = xx.cultivated_class | ||
|
||
# Cultivated pipeline applies a mask which feeds only terrestrial veg (110) to the model | ||
# Just exclude no data (255) and apply the cultivated results | ||
cultivated_mask = cultivated_dss != int(NODATA) | ||
l34_cultivated_masked = xr.where(cultivated_mask, cultivated_dss, l34_dss) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. cultivated is masked by the veg in input already, so it's just matter of merging valid data ( |
||
|
||
# Urban is classified on l3/4 surface output (210) | ||
urban_mask = l34_dss == 210 | ||
l34_urban_cultivated_masked = xr.where( | ||
urban_mask, urban_dss, l34_cultivated_masked | ||
) | ||
|
||
attrs = xx.attrs.copy() | ||
attrs["nodata"] = NODATA | ||
l34_urban_cultivated_masked = l34_urban_cultivated_masked.squeeze(dim=["spec"]) | ||
dims = l34_urban_cultivated_masked.dims | ||
|
||
data_vars = { | ||
"level3_class": xr.DataArray( | ||
l34_urban_cultivated_masked.data, dims=dims, attrs=attrs | ||
) | ||
} | ||
|
||
coords = dict((dim, xx.coords[dim]) for dim in dims) | ||
level3 = xr.Dataset(data_vars=data_vars, coords=coords, attrs=attrs) | ||
|
||
return level3 | ||
|
||
|
||
register("lccs_level3", StatsLccsLevel3) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import xarray as xr | ||
|
||
from odc.stats.plugins.lc_level3 import StatsLccsLevel3 | ||
import pytest | ||
|
||
expected_l3_classes = [ | ||
[111, 112, 215], | ||
[124, 112, 215], | ||
[221, 215, 216], | ||
[223, 255, 223], | ||
] | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
def image_groups(): | ||
l34 = np.array( | ||
[ | ||
[ | ||
[110, 110, 210], | ||
[124, 110, 210], | ||
[221, 210, 210], | ||
[223, 255, 223], | ||
] | ||
], | ||
dtype="int", | ||
) | ||
|
||
urban = np.array( | ||
[ | ||
[ | ||
[215, 215, 215], | ||
[216, 216, 215], | ||
[116, 215, 216], | ||
[216, 216, 216], | ||
] | ||
], | ||
dtype="int", | ||
) | ||
|
||
cultivated = np.array( | ||
[ | ||
[ | ||
[111, 112, 255], | ||
[255, 112, 255], | ||
[255, 255, 255], | ||
[255, 255, 255], | ||
] | ||
], | ||
dtype="int", | ||
) | ||
|
||
tuples = [ | ||
(np.datetime64("2000-01-01T00"), np.datetime64("2000-01-01")), | ||
] | ||
index = pd.MultiIndex.from_tuples(tuples, names=["time", "solar_day"]) | ||
coords = { | ||
"x": np.linspace(10, 20, l34.shape[2]), | ||
"y": np.linspace(0, 5, l34.shape[1]), | ||
"spec": index, | ||
} | ||
|
||
data_vars = { | ||
"classes_l3_l4": xr.DataArray( | ||
l34, dims=("spec", "y", "x"), attrs={"nodata": 255} | ||
), | ||
"urban_classes": xr.DataArray( | ||
urban, dims=("spec", "y", "x"), attrs={"nodata": 255} | ||
), | ||
"cultivated_class": xr.DataArray( | ||
cultivated, dims=("spec", "y", "x"), attrs={"nodata": 255} | ||
), | ||
} | ||
xx = xr.Dataset(data_vars=data_vars, coords=coords) | ||
return xx | ||
|
||
|
||
def test_urban_class(image_groups): | ||
|
||
lc_level3 = StatsLccsLevel3() | ||
level3_classes = lc_level3.reduce(image_groups) | ||
assert (level3_classes.level3_class.values == expected_l3_classes).all() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you either give the integer a name or comment what the number means?