{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "--------\n", "\n", "# Time series of custom index and masking\n", "\n", "--------\n", "\n", "**Short description**\n", "\n", "This notebook expands on the previous example, creating a custom water or vegetation index, by creating a time series of the index and masking non-target areas.\n", "\n", "In this notebook, you will search for, select, and obtain Sentinel-2 data over a time period. We want the selected data needs to be cloud-free in order to better analyze the index. Custom indices can then be calculated over the area. A mask only over areas with specific classifications (water for water indices, vegetation for vegetation indices) will be applied and the resulting data plotted. This example examines several water quality indices over a period of one year.\n", "\n", "--------" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "keC_14irx2wq" }, "source": [ "### 1 - Import spacesense object(s) and other dependencies" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "colab": {}, "colab_type": "code", "id": "l1WpZ6RSx2wr" }, "outputs": [], "source": [ "from spacesense import Client\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "import json\n", "\n", "import os\n", "if \"SS_API_KEY\" not in os.environ:\n", " from getpass import getpass\n", " api_key = getpass('Enter your api key : ')\n", " os.environ[\"SS_API_KEY\"] = api_key" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 2 - Define AOI and output options\n", "\n", "The GeoJSON of this AOI, containing a small portion of Lake Matano and coastline in Indonesia, can be downloaded [here](../resources/aois/lake_matano.geojson)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# Define the AOI\n", "with open(\"../resources/aois/lake_matano.geojson\", mode=\"r\") as file:\n", " aoi = json.load(file)\n", "\n", "# Get an instance of the SpaceSense Client object\n", "client = Client(id=\"s2_custom_index_ts_mask\")\n", "\n", "# Enable to save data in local files\n", "client.enable_local_output()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### 3 - Search S2" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", " | id | \n", "date | \n", "tile | \n", "valid_pixel_percentage | \n", "platform | \n", "relative_orbit_number | \n", "product_id | \n", "datetime | \n", "swath_coverage_percentage | \n", "no_data | \n", "cloud_shadows | \n", "vegetation | \n", "not_vegetated | \n", "water | \n", "cloud_medium_probability | \n", "cloud_high_probability | \n", "thin_cirrus | \n", "snow | \n", "
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
17 | \n", "S2B_51MUT_20210101_0_L2A | \n", "2021-01-01 | \n", "51MUT | \n", "99.12 | \n", "sentinel-2b | \n", "060 | \n", "S2B_MSIL2A_20210101T021349_N0214_R060_T51MUT_2... | \n", "2021-01-01T02:18:17Z | \n", "100.0 | \n", "0.0 | \n", "0.25 | \n", "24.38 | \n", "0.00 | \n", "73.22 | \n", "0.53 | \n", "0.10 | \n", "0.00 | \n", "0.0 | \n", "
16 | \n", "S2B_51MUT_20210111_0_L2A | \n", "2021-01-11 | \n", "51MUT | \n", "99.01 | \n", "sentinel-2b | \n", "060 | \n", "S2B_MSIL2A_20210111T021349_N0214_R060_T51MUT_2... | \n", "2021-01-11T02:18:18Z | \n", "100.0 | \n", "0.0 | \n", "0.09 | \n", "25.73 | \n", "0.00 | \n", "72.62 | \n", "0.00 | \n", "0.00 | \n", "0.90 | \n", "0.0 | \n", "
15 | \n", "S2A_51MUT_20210225_0_L2A | \n", "2021-02-25 | \n", "51MUT | \n", "97.55 | \n", "sentinel-2a | \n", "060 | \n", "S2A_MSIL2A_20210225T021341_N0214_R060_T51MUT_2... | \n", "2021-02-25T02:18:19Z | \n", "100.0 | \n", "0.0 | \n", "0.16 | \n", "21.80 | \n", "1.04 | \n", "73.37 | \n", "0.36 | \n", "1.93 | \n", "0.00 | \n", "0.0 | \n", "
14 | \n", "S2B_51MUT_20210315_0_L2A | \n", "2021-03-15 | \n", "51MUT | \n", "99.98 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20210315T022329_N0214_R103_T51MUT_2... | \n", "2021-03-15T02:28:12Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "25.61 | \n", "0.04 | \n", "73.67 | \n", "0.02 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
13 | \n", "S2A_51MUT_20210317_0_L2A | \n", "2021-03-17 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2a | \n", "060 | \n", "S2A_MSIL2A_20210317T021341_N0214_R060_T51MUT_2... | \n", "2021-03-17T02:18:18Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.04 | \n", "0.00 | \n", "73.09 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
12 | \n", "S2B_51MUT_20210322_0_L2A | \n", "2021-03-22 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2b | \n", "060 | \n", "S2B_MSIL2A_20210322T021349_N0214_R060_T51MUT_2... | \n", "2021-03-22T02:18:18Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "25.85 | \n", "0.00 | \n", "73.05 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
11 | \n", "S2B_51MUT_20210501_0_L2A | \n", "2021-05-01 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2b | \n", "060 | \n", "S2B_MSIL2A_20210501T021339_N0300_R060_T51MUT_2... | \n", "2021-05-01T02:18:15Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.02 | \n", "0.00 | \n", "72.95 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
10 | \n", "S2B_51MUT_20210603_0_L2A | \n", "2021-06-03 | \n", "51MUT | \n", "99.08 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20210603T022329_N0300_R103_T51MUT_2... | \n", "2021-06-03T02:28:14Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "24.69 | \n", "0.09 | \n", "73.29 | \n", "0.23 | \n", "0.69 | \n", "0.00 | \n", "0.0 | \n", "
9 | \n", "S2A_51MUT_20210608_0_L2A | \n", "2021-06-08 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2a | \n", "103 | \n", "S2A_MSIL2A_20210608T022331_N0300_R103_T51MUT_2... | \n", "2021-06-08T02:28:14Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.25 | \n", "0.00 | \n", "72.96 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
8 | \n", "S2B_51MUT_20210623_0_L2A | \n", "2021-06-23 | \n", "51MUT | \n", "99.95 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20210623T022329_N0300_R103_T51MUT_2... | \n", "2021-06-23T02:28:14Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "25.81 | \n", "0.00 | \n", "72.98 | \n", "0.00 | \n", "0.00 | \n", "0.05 | \n", "0.0 | \n", "
7 | \n", "S2A_51MUT_20210725_0_L2A | \n", "2021-07-25 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2a | \n", "060 | \n", "S2A_MSIL2A_20210725T021351_N0301_R060_T51MUT_2... | \n", "2021-07-25T02:18:22Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.08 | \n", "0.00 | \n", "73.27 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
6 | \n", "S2A_51MUT_20210817_0_L2A | \n", "2021-08-17 | \n", "51MUT | \n", "99.44 | \n", "sentinel-2a | \n", "103 | \n", "S2A_MSIL2A_20210817T022331_N0301_R103_T51MUT_2... | \n", "2021-08-17T02:28:15Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "24.06 | \n", "0.75 | \n", "73.43 | \n", "0.03 | \n", "0.53 | \n", "0.00 | \n", "0.0 | \n", "
5 | \n", "S2B_51MUT_20211001_0_L2A | \n", "2021-10-01 | \n", "51MUT | \n", "95.10 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20211001T022329_N0301_R103_T51MUT_2... | \n", "2021-10-01T02:28:13Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "25.74 | \n", "0.00 | \n", "68.32 | \n", "0.60 | \n", "0.20 | \n", "4.10 | \n", "0.0 | \n", "
4 | \n", "S2A_51MUT_20211003_0_L2A | \n", "2021-10-03 | \n", "51MUT | \n", "99.66 | \n", "sentinel-2a | \n", "060 | \n", "S2A_MSIL2A_20211003T021351_N0301_R060_T51MUT_2... | \n", "2021-10-03T02:18:24Z | \n", "100.0 | \n", "0.0 | \n", "0.02 | \n", "26.01 | \n", "0.14 | \n", "72.64 | \n", "0.32 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
3 | \n", "S2A_51MUT_20211023_0_L2A | \n", "2021-10-23 | \n", "51MUT | \n", "98.51 | \n", "sentinel-2a | \n", "060 | \n", "S2A_MSIL2A_20211023T021351_N0301_R060_T51MUT_2... | \n", "2021-10-23T02:18:24Z | \n", "100.0 | \n", "0.0 | \n", "0.35 | \n", "23.49 | \n", "0.47 | \n", "73.46 | \n", "0.30 | \n", "0.84 | \n", "0.00 | \n", "0.0 | \n", "
2 | \n", "S2B_51MUT_20211110_0_L2A | \n", "2021-11-10 | \n", "51MUT | \n", "99.97 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20211110T022329_N0301_R103_T51MUT_2... | \n", "2021-11-10T02:28:13Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.09 | \n", "0.00 | \n", "72.94 | \n", "0.00 | \n", "0.00 | \n", "0.03 | \n", "0.0 | \n", "
1 | \n", "S2B_51MUT_20211127_0_L2A | \n", "2021-11-27 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2b | \n", "060 | \n", "S2B_MSIL2A_20211127T021339_N0301_R060_T51MUT_2... | \n", "2021-11-27T02:18:15Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "26.15 | \n", "0.00 | \n", "73.05 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
0 | \n", "S2B_51MUT_20211220_0_L2A | \n", "2021-12-20 | \n", "51MUT | \n", "100.00 | \n", "sentinel-2b | \n", "103 | \n", "S2B_MSIL2A_20211220T022319_N0301_R103_T51MUT_2... | \n", "2021-12-20T02:28:07Z | \n", "100.0 | \n", "0.0 | \n", "0.00 | \n", "25.83 | \n", "0.00 | \n", "73.01 | \n", "0.00 | \n", "0.00 | \n", "0.00 | \n", "0.0 | \n", "
<xarray.Dataset>\n", "Dimensions: (time: 18, y: 2357, x: 327)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-01-01 2021-01-11 ... 2021-12-20\n", " * y (y) float32 -2.487 -2.487 -2.487 -2.487 ... -2.504 -2.504 -2.504\n", " * x (x) float64 121.3 121.3 121.3 121.3 ... 121.3 121.3 121.3 121.3\n", "Data variables:\n", " S2_B01 (time, y, x) float32 0.0 0.0 0.0 0.0 0.0 ... 0.0388 0.0388 0.0 0.0\n", " S2_B02 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0397 0.0401 0.0415 0.0\n", " S2_B03 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0277 0.0267 0.0264 0.0\n", " S2_B04 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0184 0.0179 0.0179 0.0\n", " S2_B08 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0126 0.0122 0.0129 0.0\n", " S2_SCL (time, y, x) float32 0.0 0.0 0.0 0.0 0.0 ... 6.0 6.0 6.0 6.0 6.0\n", "Attributes:\n", " crs: +init=epsg:4326\n", " transform: [ 8.98315284e-05 0.00000000e+00 1.21303645e+02 0.000...\n", " res: [8.98315284e-05 7.00728646e-06]\n", " ulx, uly: [121.30364507 -2.48706115]\n", " s2_data_lineage: {"Data origin": "S3 bucket (ARN=arn:aws:s3:::sentinel-c...\n", " additional_info: {'Info': 'You can put any additional information, such ...
<xarray.Dataset>\n", "Dimensions: (time: 18, y: 2357, x: 327)\n", "Coordinates:\n", " * time (time) datetime64[ns] 2021-01-01 2021-01-11 ... 2021-12-20\n", " * y (y) float32 -2.487 -2.487 -2.487 -2.487 ... -2.504 -2.504 -2.504\n", " * x (x) float64 121.3 121.3 121.3 121.3 ... 121.3 121.3 121.3 121.3\n", "Data variables:\n", " S2_B01 (time, y, x) float32 0.0 0.0 0.0 0.0 0.0 ... 0.0388 0.0388 0.0 0.0\n", " S2_B02 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0397 0.0401 0.0415 0.0\n", " S2_B03 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0277 0.0267 0.0264 0.0\n", " S2_B04 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0184 0.0179 0.0179 0.0\n", " S2_B08 (time, y, x) float32 0.0 0.0 0.0 0.0 ... 0.0126 0.0122 0.0129 0.0\n", " S2_SCL (time, y, x) float32 0.0 0.0 0.0 0.0 0.0 ... 6.0 6.0 6.0 6.0 6.0\n", " CHL_A (time, y, x) float32 nan nan nan nan nan ... 1.121 0.9701 nan nan\n", " SPM (time, y, x) float32 0.0 0.0 0.0 0.0 0.0 ... 13.72 13.18 14.12 0.0\n", "Attributes:\n", " crs: +init=epsg:4326\n", " transform: [ 8.98315284e-05 0.00000000e+00 1.21303645e+02 0.000...\n", " res: [8.98315284e-05 7.00728646e-06]\n", " ulx, uly: [121.30364507 -2.48706115]\n", " s2_data_lineage: {"Data origin": "S3 bucket (ARN=arn:aws:s3:::sentinel-c...\n", " additional_info: {'Info': 'You can put any additional information, such ...