Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
GeoTiles - Time (clouds).ipynb
{
"nbformat": 4,
"nbformat_minor": 0,
"metadata": {
"colab": {
"name": "GeoTiles - Time (clouds).ipynb",
"provenance": [],
"collapsed_sections": [],
"authorship_tag": "ABX9TyPCnpPoF3DvIqkyB2jn5vmR",
"include_colab_link": true
},
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
},
"language_info": {
"name": "python"
}
},
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "view-in-github",
"colab_type": "text"
},
"source": [
"<a href=\"https://colab.research.google.com/gist/fwrite/268ace84946bc43e9182b7074393243d/geotiles-time-clouds.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "i9zHvyipdgNS",
"outputId": "0c1f5708-2e8c-4a0a-d973-b3839b594b8a"
},
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Requirement already satisfied: zarr in /usr/local/lib/python3.7/dist-packages (2.11.3)\n",
"Requirement already satisfied: xarray in /usr/local/lib/python3.7/dist-packages (0.20.2)\n",
"Requirement already satisfied: fsspec in /usr/local/lib/python3.7/dist-packages (2022.3.0)\n",
"Requirement already satisfied: aiohttp in /usr/local/lib/python3.7/dist-packages (3.8.1)\n",
"Requirement already satisfied: fasteners in /usr/local/lib/python3.7/dist-packages (from zarr) (0.17.3)\n",
"Requirement already satisfied: asciitree in /usr/local/lib/python3.7/dist-packages (from zarr) (0.3.3)\n",
"Requirement already satisfied: numcodecs>=0.6.4 in /usr/local/lib/python3.7/dist-packages (from zarr) (0.9.1)\n",
"Requirement already satisfied: numpy>=1.7 in /usr/local/lib/python3.7/dist-packages (from zarr) (1.21.6)\n",
"Requirement already satisfied: pandas>=1.1 in /usr/local/lib/python3.7/dist-packages (from xarray) (1.3.5)\n",
"Requirement already satisfied: typing-extensions>=3.7 in /usr/local/lib/python3.7/dist-packages (from xarray) (4.2.0)\n",
"Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from xarray) (4.11.3)\n",
"Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.1->xarray) (2.8.2)\n",
"Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas>=1.1->xarray) (2022.1)\n",
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas>=1.1->xarray) (1.15.0)\n",
"Requirement already satisfied: aiosignal>=1.1.2 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (1.2.0)\n",
"Requirement already satisfied: charset-normalizer<3.0,>=2.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (2.0.12)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (1.7.2)\n",
"Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (21.4.0)\n",
"Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (4.0.2)\n",
"Requirement already satisfied: asynctest==0.13.0 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (0.13.0)\n",
"Requirement already satisfied: frozenlist>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (1.3.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /usr/local/lib/python3.7/dist-packages (from aiohttp) (6.0.2)\n",
"Requirement already satisfied: idna>=2.0 in /usr/local/lib/python3.7/dist-packages (from yarl<2.0,>=1.0->aiohttp) (2.10)\n",
"Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->xarray) (3.8.0)\n"
]
}
],
"source": [
"# This will install the necessary packages.\n",
"# If you are running the script locally,\n",
"# you may want to do this via your package manager.\n",
"!pip install --upgrade zarr xarray fsspec aiohttp"
]
},
{
"cell_type": "code",
"source": [
"import xarray as xr, numpy as np, dask as da, tensorflow as tf\n",
"from matplotlib import pyplot as plt\n",
"from skimage import exposure\n",
"\n",
"# Progress bar\n",
"from dask.diagnostics import ProgressBar\n",
"ProgressBar().register()\n",
"\n",
"# When working in Google Colab, the output has to be made available for download.\n",
"# Locally, the output will be stored in the same directory.\n",
"try:\n",
" from google.colab.files import download\n",
"except ImportError:\n",
" download = lambda f: print('See', f)"
],
"metadata": {
"id": "oxNBiWwXdhbH"
},
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"source": [
"ROI = {'x': slice(76204, 103837), 'y': slice(428131, 415551)} # Hoeksche Waard\n",
"R051 = xr.open_zarr('simplecache::https://geotiles.nl/cube.zarr',\n",
" group='/S2/R051', consolidated=True).sel(ROI)"
],
"metadata": {
"id": "-GK6YMZYdjPh"
},
"execution_count": 3,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"The image will be made up of slices of the full time series of acquisitions. Hence, the x-axis (easting) is used as the time dimension as well. Here I chose to give each acquisition equal width, irrespective of the time till the next acquisition."
],
"metadata": {
"id": "m8MMxX-VAyCP"
}
},
{
"cell_type": "code",
"source": [
"width_day = len(R051['x'])/len(R051['time'])\n",
"\n",
"width_sel = np.ones(len(R051['time']))*width_day\n",
"start_sel = np.r_[0, width_sel.cumsum()]\n",
"\n",
"start_sel = np.rint(start_sel).astype(int)\n",
"width_sel = np.rint(start_sel[1:] -start_sel[:-1]).astype(int)"
],
"metadata": {
"id": "vtyKhdEOFFIT"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Create the RGB image, from bands 4, 3, and 2."
],
"metadata": {
"id": "MddMvTlVBdnO"
}
},
{
"cell_type": "code",
"source": [
"RGB = R051[['B04', 'B03', 'B02']].to_stacked_array('rgb', ('x', 'y', 'time'))"
],
"metadata": {
"id": "0xCK4U4957d5"
},
"execution_count": 5,
"outputs": []
},
{
"cell_type": "markdown",
"source": [
"Stack the vertical slices of imagery, and load the data. Depending on the size of the area, this step may take a while (tens of minutes or more)."
],
"metadata": {
"id": "g-qhC-nLBhu0"
}
},
{
"cell_type": "code",
"source": [
"img = xr.concat([RGB.isel({'x': slice(_s, _s+_w), 'time': _t}) for _t, _w, _s in zip(np.arange(len(R051['time'])), width_sel, start_sel)], 'x')\n",
"img.load();\n"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "vrDKkRWA7U5E",
"outputId": "d3684ae4-cdf7-46d0-a6e9-e79356e38b3b"
},
"execution_count": 6,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"[########################################] | 100% Completed | 38min 38.9s\n"
]
}
]
},
{
"cell_type": "markdown",
"source": [
"Output as image. Unfortunately, the result is very cloudy."
],
"metadata": {
"id": "JBr24M4vBl3R"
}
},
{
"cell_type": "code",
"source": [
"fig, ax = plt.subplots(1, 1, constrained_layout=True, figsize=(46.8, 33.1))\n",
"\n",
"ax.imshow(exposure.equalize_adapthist(img.clip(0,1), clip_limit=0.03))\n",
"# ax.imshow(img)\n",
"\n",
"ax.set_yticks([])\n",
"ax.set_xticks(start_sel[:-1]+width_sel/2)\n",
"ax.set_xticklabels(R051['time'].to_series(), rotation=90);"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 898
},
"id": "Mhg1mebs_GtJ",
"outputId": "13cd5bce-f5d5-41aa-d46e-8904e24af144"
},
"execution_count": 7,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": [
"<Figure size 3369.6x2383.2 with 1 Axes>"
],
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment