-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add mangroves visualiser and update the testing notebook
- Loading branch information
1 parent
2dcd4df
commit 9f87c11
Showing
3 changed files
with
212 additions
and
1,337 deletions.
There are no files selected for viewing
This file contains 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,20 @@ | ||
# Digital Earth Pacific Mangroves | ||
|
||
## Running the test notebook | ||
|
||
First install dependencies: | ||
|
||
```bash | ||
pip install \ | ||
git+https://github.com/digitalearthpacific/dep-tools/ \ | ||
git+https://github.com/jessjaco/azure-logger | ||
``` | ||
|
||
Now clone the repo: | ||
|
||
```bash | ||
git clone https://github.com/digitalearthpacific/dep-mangroves | ||
``` | ||
|
||
Now run the notebook "Test_Mangroves.ipynb". Data is written to the `data` | ||
folder in the same directory. |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains 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,163 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from pystac_client import Client\n", | ||
"from odc.stac import load\n", | ||
"import xarray as xr\n", | ||
"import numpy as np\n", | ||
"\n", | ||
"import odc.geo # noqa" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"catalog = \"https://stac.staging.digitalearthpacific.org\"\n", | ||
"collection = \"dep_s2_mangroves\"" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Coordinates grabbed from Google Maps\n", | ||
"# Southeast PNG\n", | ||
"ll = (-10.590125, 149.844629)\n", | ||
"ur = (-10.360110, 150.195631)\n", | ||
"\n", | ||
"# # Ba river mouth, Fiji\n", | ||
"# ll = (-17.500881, 177.608558)\n", | ||
"# ur = (-17.420771, 177.702546)\n", | ||
"\n", | ||
"bbox = (ll[1], ll[0], ur[1], ur[0])\n", | ||
"\n", | ||
"# Find STAC items\n", | ||
"client = Client.open(catalog)\n", | ||
"items = client.search(collections=[collection], bbox=bbox).items()\n", | ||
"items = [i for i in items]\n", | ||
"\n", | ||
"print(f\"Found {len(items)} items\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"config = {\n", | ||
" collection: {\n", | ||
" \"assets\": {\n", | ||
" \"mangroves\": {\"data_type\": \"int16\"}\n", | ||
" }\n", | ||
" }\n", | ||
"}\n", | ||
"\n", | ||
"data = load(items, bbox=bbox, bands=[\"mangroves\"], stac_cfg=config)\n", | ||
"data" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"data.mangroves.plot.imshow(\n", | ||
" col=\"time\",\n", | ||
" col_wrap=4,\n", | ||
" levels=[0, 1, 2, 3],\n", | ||
" colors=[\"white\", \"yellow\", \"green\", \"darkgreen\"],\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# Define the values you want to count (0, 1, and 2)\n", | ||
"values_to_count = [0, 1, 2]\n", | ||
"\n", | ||
"# Initialize an empty DataArray to store the counts\n", | ||
"count_array = xr.DataArray(\n", | ||
" np.zeros((len(data[\"time\"]), len(values_to_count))),\n", | ||
" coords={\"time\": data[\"time\"], \"values\": values_to_count},\n", | ||
" dims=[\"time\", \"values\"],\n", | ||
")\n", | ||
"\n", | ||
"# Loop through each value and count occurrences in each year\n", | ||
"for time in data.time:\n", | ||
" year = time.values.astype(\"datetime64[Y]\")\n", | ||
" one_year_data = data.sel(time=time)\n", | ||
" count = one_year_data.mangroves.groupby(one_year_data.mangroves).count()\n", | ||
" for i, v in enumerate(values_to_count):\n", | ||
" if v not in count.mangroves:\n", | ||
" # Add the missing count to the array\n", | ||
" count_array.loc[{\"time\": time, \"values\": v}] = 0\n", | ||
" else:\n", | ||
" # Add the count to the array\n", | ||
" val = count.sel(mangroves=v)\n", | ||
" count_array.loc[{\"time\": time, \"values\": v}] = val * 10 / 10000\n", | ||
"\n", | ||
"# Rename the count variable\n", | ||
"count_array = count_array.rename(\"count\")\n", | ||
"count_array" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"count_array.plot.line(x=\"time\", hue=\"values\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# for time in data.time:\n", | ||
"# year = time.values.astype(\"datetime64[Y]\")\n", | ||
"# one_year_data = data.sel(time=time)\n", | ||
"# one_year_data.mangroves.odc.write_cog(f\"mangroves_{year}.tif\")" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.11.5" | ||
}, | ||
"orig_nbformat": 4 | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |