{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "b7ca7102-5fd9-481f-90cd-3ba60e288649", "metadata": { "tags": [] }, "outputs": [], "source": [ "# $ pip install sentinelhub\n", "# pip install gdal\n", "\n", "import os\n", "import datetime\n", "import numpy as np\n", "import matplotlib.pyplot as plt\n", "from pathlib import Path\n", "from osgeo import gdal\n", "\n", "from sentinelhub import MimeType, CRS, BBox, SentinelHubRequest, SentinelHubDownloadClient, \\\n", " DataCollection, bbox_to_dimensions, DownloadRequest, SHConfig, BBoxSplitter, read_data\n", "\n", "config = SHConfig()\n", "\n", "import time" ] }, { "cell_type": "code", "execution_count": 2, "id": "330c967c-2742-4a7a-9a61-28bfdaf8eeca", "metadata": { "tags": [] }, "outputs": [], "source": [ "#pip install pipreqs" ] }, { "cell_type": "code", "execution_count": null, "id": "8f2ec814-76b2-47e6-b74b-a4ffddd908f1", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 3, "id": "49f8496a-a267-4b74-9500-a168e031ed68", "metadata": {}, "outputs": [], "source": [ "#import pipreqs\n", "#pipreqs Resilience BV/4002 CMD App - General/4002 CMD Team/4002 TechnicalData/04 WP2 technical/python/Chemba_download.ipynb" ] }, { "cell_type": "code", "execution_count": 4, "id": "5491a840-779c-4f0c-8164-c3de738b3298", "metadata": { "tags": [] }, "outputs": [], "source": [ "config.sh_client_id = '6e542a89-b040-40ca-8352-8b979ee3067a'\n", "config.sh_client_secret = 'PiNaFQfP<.)59]rS.*}OF+hlrrzf#k~uqlhy-_F%'" ] }, { "cell_type": "code", "execution_count": 5, "id": "eb1fb662-0e25-4ca9-8317-c6953290842b", "metadata": { "tags": [] }, "outputs": [], "source": [ "collection_id = 'c691479f-358c-46b1-b0f0-e12b70a9856c'\n", "byoc = DataCollection.define_byoc(\n", " collection_id,\n", " name='planet_data2',\n", " is_timeless=True)" ] }, { "cell_type": "code", "execution_count": 6, "id": "244b5752-4f02-4347-9278-f6a0a46b88f4", "metadata": { "tags": [] }, "outputs": [], "source": [ "\"\"\"\n", "Utilities used by example notebooks\n", "\"\"\"\n", "import matplotlib.pyplot as plt\n", "import numpy as np\n", "\n", "\n", "def plot_image(image, factor=1.0, clip_range=None, **kwargs):\n", " \"\"\"\n", " Utility function for plotting RGB images.\n", " \"\"\"\n", " fig, ax = plt.subplots(nrows=1, ncols=1, figsize=(15, 15))\n", " if clip_range is not None:\n", " ax.imshow(np.clip(image * factor, *clip_range), **kwargs)\n", " else:\n", " ax.imshow(image * factor, **kwargs)\n", " ax.set_xticks([])\n", " ax.set_yticks([]) \n", "\n", "\n", "#evalscript_true_color = \"\"\"\n", "# //VERSION=3\n", "#\n", "# function setup() {\n", "# return {\n", "# input: [{\n", "# bands: [\"B1\", \"B2\", \"B3\", \"B4\", \"UDM\"]\n", "# }],\n", "# output: {\n", "# bands: 5\n", "# }\n", "# };\n", "# }\n", "#\n", "# function evaluatePixel(sample) {\n", "# return [2.5 * sample.B1 / 10000, 2.5 * sample.B2 / 10000, 2.5 * sample.B3 / 10000, 2.5 * sample.B4 / 10000, sample.UDM];\n", "# }\n", "#\"\"\"\n", "\n", "evalscript_true_color = \"\"\"\n", " //VERSION=3\n", "\n", " function setup() {\n", " return {\n", " input: [{\n", " bands: [\"Blue\", \"Green\", \"Red\", \"NIR\", \"UDM\"]\n", " }],\n", " output: {\n", " bands: 5\n", " }\n", " };\n", " }\n", "\n", " function evaluatePixel(sample) {\n", " return [2.5 * sample.Blue / 10000, 2.5 * sample.Green / 10000, 2.5 * sample.Red / 10000, 2.5 * sample.NIR / 10000, sample.UDM];\n", " }\n", "\"\"\"\n", "BASE_PATH_SINGLE_IMAGES = Path('../laravel_app/storage/app/chemba/single_images')\n", "BASE_PATH_WEEKLY = Path('../laravel_app/storage/app/chemba/weekly_img')\n", "\n", "def get_true_color_request(time_interval): \n", " return SentinelHubRequest(\n", " evalscript=evalscript_true_color,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data2,\n", " time_interval=time_interval\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.TIFF)\n", " ],\n", " bbox=chemba_bbox,\n", " size=chemba_size,\n", " config=config,\n", " data_folder=str(BASE_PATH_SINGLE_IMAGES / date),\n", "\n", " )\n", "\n", "def get_true_color_request_day_east(time_interval):\n", " base_path = '../laravel_app/storage/app/chemba/single_images/'\n", " \n", " return SentinelHubRequest(\n", " evalscript=evalscript_true_color,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data2,\n", " time_interval=(time_interval, time_interval)\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.TIFF)\n", " ],\n", " bbox=chemba_bbox_east,\n", " size=chemba_size_east,\n", " config=config,\n", " data_folder=str(BASE_PATH_SINGLE_IMAGES / time_interval),\n", "\n", " )\n", "\n", "def get_true_color_request_day_west(time_interval):\n", " base_path = '../laravel_app/storage/app/chemba/single_images/'\n", " return SentinelHubRequest(\n", " evalscript=evalscript_true_color,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data2,\n", " time_interval=(time_interval, time_interval)\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.TIFF)\n", " ],\n", " bbox=chemba_bbox_west,\n", " size=chemba_size_west,\n", " config=config,\n", " data_folder=str(BASE_PATH_SINGLE_IMAGES / time_interval),\n", "\n", " )\n", "\n", "def get_true_color_request_week(time_interval):\n", " base_path = '../laravel_app/storage/app/chemba/weekly_img/'\n", " return SentinelHubRequest(\n", " evalscript=evalscript_true_color,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data2,\n", " time_interval=time_interval\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.TIFF)\n", " ],\n", " bbox=chemba_bbox,\n", " size=chemba_size,\n", " config=config,\n", " data_folder=str(BASE_PATH_SINGLE_IMAGES / date),\n", " )" ] }, { "cell_type": "code", "execution_count": 7, "id": "5abb2cc0-5e2f-46cd-9676-3093d07b0624", "metadata": { "tags": [] }, "outputs": [], "source": [ "\n", "#end = datetime.date.today() - datetime.timedelta(days=1)\n", "#start = end - datetime.timedelta(days=6)\n", "\n", "#end = datetime.date(2022, 7, 1)\n", "#start = datetime.date(2022, 7, 21)\n", "\n", "#n_chunks = start - end\n", "#n_chunks.days\n" ] }, { "cell_type": "code", "execution_count": 8, "id": "a4937240-27f9-44c3-ad9c-cec6f2ffe4c8", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Monthly time windows:\n", "\n", "2023-09-30\n", "2023-10-01\n", "2023-10-02\n", "2023-10-03\n", "2023-10-04\n", "2023-10-05\n", "2023-10-06\n" ] } ], "source": [ "#end = datetime.date(2022, 8, 4)\n", "#start = datetime.date(2023, 3, 1)\n", "days_needed = 6\n", "\n", "end = datetime.date.today() - datetime.timedelta(days=days_needed - 1)\n", "start = end - datetime.timedelta(days=1)\n", "\n", "\n", "n_chunks = days_needed + 1\n", "tdelta = datetime.timedelta(days=1)\n", "edges = [(start + i*tdelta).isoformat() for i in range(n_chunks)]\n", "#slots = [(edges[i], edges[i]) for i in range(len(edges))]\n", "slots = [(edges[i]) for i in range(len(edges))]\n", "\n", "#date = start.strftime(\"%Y-%m-%d\")\n", "\n", "print('Monthly time windows:\\n')\n", "for slot in slots:\n", " print(slot)" ] }, { "cell_type": "markdown", "id": "0c18e312-8421-47d7-84f9-ed7d5e47e7ee", "metadata": {}, "source": [ "### Chemba east side" ] }, { "cell_type": "code", "execution_count": 9, "id": "2335139b-dfb1-4371-ae2c-c2b9c8cbf10c", "metadata": { "tags": [] }, "outputs": [], "source": [ "chemba_east = [34.8830, -17.3516, 34.9380, -17.2917]\n", "resolution = 3\n", "chemba_bbox_east = BBox(bbox=chemba_east, crs=CRS.WGS84)\n", "chemba_size_east = bbox_to_dimensions(chemba_bbox_east, resolution=resolution)\n", "\n", "chemba_west = [34.9460, -17.3500, 34.9839, -17.3110]\n", "chemba_bbox_west = BBox(bbox=chemba_west, crs=CRS.WGS84)\n", "chemba_size_west = bbox_to_dimensions(chemba_bbox_west, resolution=resolution)" ] }, { "cell_type": "code", "execution_count": 10, "id": "77513576-2fed-4d17-a665-d11267b42390", "metadata": { "tags": [] }, "outputs": [], "source": [ "def download_function(slot):\n", " ### Chemba east side\n", " # create a list of requests \n", " list_of_requests = [get_true_color_request_day_east(slot)]\n", " list_of_requests = [request.download_list[0] for request in list_of_requests]\n", "\n", " # download data chemba east with multiple threads\n", " data = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=15)\n", " print(f' East downloaded ' +slot)\n", "\n", " ### Chemba west side\n", " # create a list of requests\n", " list_of_requests = [get_true_color_request_day_west(slot)]\n", " list_of_requests = [request.download_list[0] for request in list_of_requests]\n", "\n", " # download data chemba west with multiple threads\n", " data = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=15)\n", " print(f' West downloaded ' +slot)\n", " \n", " time.sleep(.5)\n", "\n", "def merge_files(slot):\n", " base_path = Path('../laravel_app/storage/app/chemba')\n", " # List the downloaded Tiffs in the different subfolders with pathlib (native library)\n", " file_list = [f\"{x}/response.tiff\" for x in Path(base_path / 'single_images' / slot).iterdir()]\n", " print(file_list)\n", "\n", " folder_for_merged_tifs = str(base_path / 'merged_tif' / f\"{slot}.tif\")\n", " folder_for_virtual_raster = str(base_path / 'merged_virtual' / f\"merged{slot}.vrt\")\n", "\n", " # Create a virtual raster\n", " vrt_all = gdal.BuildVRT(folder_for_virtual_raster, file_list)\n", " vrt_all = gdal.BuildVRT(folder_for_virtual_raster, file_list)\n", "\n", " # Convert to JPEG\n", " gdal.Translate(folder_for_merged_tifs,folder_for_virtual_raster)" ] }, { "cell_type": "code", "execution_count": 11, "id": "d5830b6e-da0a-416f-867e-cbca4bd434f5", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " East downloaded 2023-09-30\n", " West downloaded 2023-09-30\n", " East downloaded 2023-10-01\n", " West downloaded 2023-10-01\n", " East downloaded 2023-10-02\n", " West downloaded 2023-10-02\n", " East downloaded 2023-10-03\n", " West downloaded 2023-10-03\n", " East downloaded 2023-10-04\n", " West downloaded 2023-10-04\n", " East downloaded 2023-10-05\n", " West downloaded 2023-10-05\n", " East downloaded 2023-10-06\n", " West downloaded 2023-10-06\n" ] } ], "source": [ "for slot in slots:\n", " download_function(slot)" ] }, { "cell_type": "code", "execution_count": 12, "id": "68db3c15-6f94-432e-b315-c329e4251b21", "metadata": { "tags": [] }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-09-30/369e30117c87ea5a93d4b1fdc1e200f5/response.tiff', 'chemba_single_images/2023-09-30/19f952f0b4dc03e82b31ddda504eca37/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-09-30/369e30117c87ea5a93d4b1fdc1e200f5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-09-30/19f952f0b4dc03e82b31ddda504eca37/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-09-30/369e30117c87ea5a93d4b1fdc1e200f5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-09-30/19f952f0b4dc03e82b31ddda504eca37/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-09-30/369e30117c87ea5a93d4b1fdc1e200f5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-09-30/19f952f0b4dc03e82b31ddda504eca37/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-01/52579ecea6e4c674f3f15a70692b2470/response.tiff', 'chemba_single_images/2023-10-01/199af70a0a9bdf56a6938888f8ad073c/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-01/52579ecea6e4c674f3f15a70692b2470/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-01/199af70a0a9bdf56a6938888f8ad073c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-01/52579ecea6e4c674f3f15a70692b2470/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-01/199af70a0a9bdf56a6938888f8ad073c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-01/52579ecea6e4c674f3f15a70692b2470/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-01/199af70a0a9bdf56a6938888f8ad073c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-02/131056d1de92580e74203e14cda16072/response.tiff', 'chemba_single_images/2023-10-02/f16e88a64ddff761b1c5b534c18588c5/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-02/131056d1de92580e74203e14cda16072/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-02/f16e88a64ddff761b1c5b534c18588c5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-02/131056d1de92580e74203e14cda16072/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-02/f16e88a64ddff761b1c5b534c18588c5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-02/131056d1de92580e74203e14cda16072/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-02/f16e88a64ddff761b1c5b534c18588c5/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-03/7a15a778f1bef721f997fc4c7071a022/response.tiff', 'chemba_single_images/2023-10-03/0cf952e5d972cd1b001c3ca1e0f286fc/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-03/7a15a778f1bef721f997fc4c7071a022/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-03/0cf952e5d972cd1b001c3ca1e0f286fc/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-03/7a15a778f1bef721f997fc4c7071a022/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-03/0cf952e5d972cd1b001c3ca1e0f286fc/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-03/7a15a778f1bef721f997fc4c7071a022/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-03/0cf952e5d972cd1b001c3ca1e0f286fc/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-04/37292b2973437bbcd8164d4b0cb5ce09/response.tiff', 'chemba_single_images/2023-10-04/a3957aa1c1478fed498c3f1e94b8703a/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-04/37292b2973437bbcd8164d4b0cb5ce09/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-04/a3957aa1c1478fed498c3f1e94b8703a/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-04/37292b2973437bbcd8164d4b0cb5ce09/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-04/a3957aa1c1478fed498c3f1e94b8703a/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-04/37292b2973437bbcd8164d4b0cb5ce09/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-04/a3957aa1c1478fed498c3f1e94b8703a/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-05/1ab274c42d49578ad25c592bc16c5c17/response.tiff', 'chemba_single_images/2023-10-05/ca219fceac18e287d129aaca803998f0/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-05/1ab274c42d49578ad25c592bc16c5c17/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-05/ca219fceac18e287d129aaca803998f0/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-05/1ab274c42d49578ad25c592bc16c5c17/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-05/ca219fceac18e287d129aaca803998f0/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-05/1ab274c42d49578ad25c592bc16c5c17/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-05/ca219fceac18e287d129aaca803998f0/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "['chemba_single_images/2023-10-06/a1c4a82635c5b3379cac01c44428a46c/response.tiff', 'chemba_single_images/2023-10-06/cae35e70e26ce81aff15dcea89246b07/response.tiff']\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "Warning 1: chemba_single_images/2023-10-06/a1c4a82635c5b3379cac01c44428a46c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-06/cae35e70e26ce81aff15dcea89246b07/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-06/a1c4a82635c5b3379cac01c44428a46c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-06/cae35e70e26ce81aff15dcea89246b07/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-06/a1c4a82635c5b3379cac01c44428a46c/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: chemba_single_images/2023-10-06/cae35e70e26ce81aff15dcea89246b07/response.tiff: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n", "Warning 1: TIFFReadDirectory:Sum of Photometric type-related color channels and ExtraSamples doesn't match SamplesPerPixel. Defining non-color channels as ExtraSamples.\n" ] } ], "source": [ "for slot in slots:\n", " merge_files(slot)" ] }, { "cell_type": "code", "execution_count": 13, "id": "4f80e84e-95b5-41dc-83d8-720cd699a79a", "metadata": {}, "outputs": [ { "ename": "SyntaxError", "evalue": "invalid syntax (4067800170.py, line 1)", "output_type": "error", "traceback": [ "\u001b[0;36m Cell \u001b[0;32mIn[13], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m stop here\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n" ] } ], "source": [ "raise SystemExit(\"Stop right there!\")" ] }, { "cell_type": "code", "execution_count": null, "id": "a8d367da-3eb5-419a-9a8a-d315f8ae8d89", "metadata": {}, "outputs": [], "source": [ "days = 1\n", "start = datetime.datetime(2021,3,12)\n", "end = start + datetime.timedelta(days=days)\n", "n_chunks = 1\n", "tdelta = (end - start) / n_chunks\n", "edges = [(start + i*tdelta).date().isoformat() for i in range(n_chunks)]\n", "#slots = [(edges[i], edges[i]) for i in range(len(edges))]\n", "slots = [(edges[i], edges[i]) for i in range(len(edges))]\n", "\n", "date = start.strftime(\"%Y-%m-%d\")\n", "\n", "print('Monthly time windows:\\n')\n", "for slot in slots:\n", " print(slot)" ] }, { "cell_type": "code", "execution_count": null, "id": "461cc3fa-4b7a-48aa-abee-25eb97689707", "metadata": {}, "outputs": [], "source": [ "days = 1\n", "start = datetime.datetime(2022,4,27)\n", "end = start + datetime.timedelta(days=days)\n", "n_chunks = 1\n", "tdelta = (end - start) / n_chunks\n", "edges = [(start + i*tdelta).date().isoformat() for i in range(n_chunks)]\n", "#slots = [(edges[i], edges[i]) for i in range(len(edges))]\n", "slots = [(edges[i], edges[i]) for i in range(len(edges))]\n", "\n", "date = start.strftime(\"%Y-%m-%d\")\n", "\n", "### Chemba east side\n", "chemba_east = [34.8830, -17.3516, 34.9380, -17.2917]\n", "resolution = 3\n", "chemba_bbox = BBox(bbox=chemba_east, crs=CRS.WGS84)\n", "chemba_size = bbox_to_dimensions(chemba_bbox, resolution=resolution)\n", "\n", "# create a list of requests \n", "list_of_requests = [get_true_color_request(slot) for slot in slots]\n", "list_of_requests = [request.download_list[0] for request in list_of_requests]\n", "\n", "# download data with multiple threads\n", "data = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=15)\n", "\n", "\n", "\n", "### Chemba west side\n", "chemba_west = [34.9460, -17.3500, 34.9839, -17.3110]\n", "chemba_bbox = BBox(bbox=chemba_west, crs=CRS.WGS84)\n", "chemba_size = bbox_to_dimensions(chemba_bbox, resolution=resolution)\n", "\n", "print(f'Image shape at {resolution} m resolution: {chemba_size} pixels')\n", "\n", "# create a list of requests\n", "list_of_requests = [get_true_color_request(slot) for slot in slots]\n", "list_of_requests = [request.download_list[0] for request in list_of_requests]\n", "\n", "# download data with multiple threads\n", "data = SentinelHubDownloadClient(config=config).download(list_of_requests, max_threads=15)\n", "print(f'data downloaded')\n", "\n", "\n" ] }, { "cell_type": "markdown", "id": "52b5f742-3db0-4290-a467-688f8aec36d5", "metadata": {}, "source": [ "### Chemba west side" ] }, { "cell_type": "code", "execution_count": null, "id": "d14b7a1e-87a6-4344-859c-5e602bd2e006", "metadata": {}, "outputs": [], "source": [ "a" ] }, { "cell_type": "code", "execution_count": null, "id": "e9daf5c9-39f6-44ea-92dd-dee355669b19", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "b7f42def-21c2-439c-9e96-a24ca1eb2176", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "6c5d6f71-e7af-450f-b6c2-151810220159", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "5e6f8a1d-bb36-4e96-b06d-6967ea8f3d98", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "id": "4a3c9cc9-ad37-405a-b8bf-996fd474540e", "metadata": {}, "outputs": [], "source": [ "a " ] }, { "cell_type": "code", "execution_count": null, "id": "d2c127e0-eaf6-46b8-af5d-6f8000bf3df3", "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "markdown", "id": "3432ffcf-606f-4c34-8f8c-a21bee6326b4", "metadata": {}, "source": [ "## Visualise the image" ] }, { "cell_type": "code", "execution_count": null, "id": "0d6b8db8-a33b-45bc-80a9-877ee1b8b80c", "metadata": {}, "outputs": [], "source": [ "evalscript_true_color_png = \"\"\"\n", " //VERSION=3\n", "\n", " function setup() {\n", " return {\n", " input: [{\n", " bands: [\"B1\", \"B2\", \"B3\", \"UDM\"]\n", " }],\n", " output: {\n", " bands: 3\n", " }\n", " };\n", " }\n", "\n", " function evaluatePixel(sample) {\n", " return [2.5 * sample.B1 / 10000, 2.5 * sample.B2 / 10000, 2.5 * sample.B3 / 10000, sample.UDM];\n", " }\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": null, "id": "e498acb8-8e14-41f4-84cd-54f4648c1716", "metadata": {}, "outputs": [], "source": [ "def get_true_color_request_png(time_interval):\n", " return SentinelHubRequest(\n", " evalscript=evalscript_true_color_png,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data,\n", " time_interval=('2022-02-10', '2022-02-17')\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.PNG)\n", " ],\n", " bbox=chemba_bbox,\n", " size=chemba_size,\n", " config=config\n", " )" ] }, { "cell_type": "code", "execution_count": null, "id": "19fd091a-dd67-484f-a1a7-3bce61bc9517", "metadata": {}, "outputs": [], "source": [ "# create a list of requests\n", "list_of_requests_png = [get_true_color_request_png(slot) for slot in slots]\n", "list_of_requests_png = [request.download_list[0] for request in list_of_requests_png]\n", "\n", "# download data with multiple threads\n", "data_png = SentinelHubDownloadClient(config=config).download(list_of_requests_png, max_threads=5)" ] }, { "cell_type": "code", "execution_count": null, "id": "6d8abc2e-e245-490a-83eb-36f6a5ec0418", "metadata": {}, "outputs": [], "source": [ "# some stuff for pretty plots\n", "ncols = 4\n", "nrows = 7\n", "aspect_ratio = chemba_size[0] / chemba_size[1]\n", "subplot_kw = {'xticks': [], 'yticks': [], 'frame_on': False}\n", "\n", "fig, axs = plt.subplots(ncols=ncols, nrows=nrows, figsize=(5 * ncols * aspect_ratio, 5 * nrows),\n", " subplot_kw=subplot_kw)\n", "\n", "for idx, image in enumerate(data_png):\n", " ax = axs[idx // ncols][idx % ncols]\n", " ax.imshow(np.clip(image * 2.5/255, 0, 1))\n", " ax.set_title(f'{slots[idx][0]} ', fontsize=10)\n", "\n", "plt.tight_layout()" ] }, { "cell_type": "code", "execution_count": null, "id": "7803cfac-ce53-4461-8702-88b872ef64aa", "metadata": {}, "outputs": [], "source": [ "evalscript_true_color = \"\"\"\n", " //VERSION=3\n", "\n", " function setup() {\n", " return {\n", " input: [{\n", " bands: [\"B1\", \"B2\", \"B3\"]\n", " }],\n", " output: {\n", " bands: 3\n", " }\n", " };\n", " }\n", "\n", " function evaluatePixel(sample) {\n", " return [sample.B04, sample.B03, sample.B02];\n", " }\n", "\"\"\"\n", "\n", "request_true_color = SentinelHubRequest(\n", " evalscript=evalscript_true_color,\n", " input_data=[\n", " SentinelHubRequest.input_data(\n", " data_collection=DataCollection.planet_data2,\n", " time_interval=('2022-02-01', '2022-02-10'),\n", " )\n", " ],\n", " responses=[\n", " SentinelHubRequest.output_response('default', MimeType.PNG)\n", " ],\n", " bbox=chemba_bbox,\n", " size=chemba_size,\n", " config=config\n", ")" ] }, { "cell_type": "code", "execution_count": null, "id": "ab26df2a-f780-4f6b-8d09-0eead9d6db2b", "metadata": {}, "outputs": [], "source": [ "true_color_imgs = request_true_color.get_data()\n" ] }, { "cell_type": "markdown", "id": "a51f41f5-f5e1-4fa7-aef2-53371d1386ce", "metadata": {}, "source": [ "## change file name\n" ] }, { "cell_type": "code", "execution_count": null, "id": "49a52f5b-2626-4dd2-9e57-6b44691f9952", "metadata": {}, "outputs": [], "source": [ "import pandas as pd\n", "import glob\n", "import json\n", "import datetime as dt\n", "from urllib.parse import unquote\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "id": "b3720a99-2906-41d9-ac1b-9c9993cd8308", "metadata": {}, "outputs": [], "source": [ "def get_request_dt(request_file):\n", " with open(request_file, 'r') as req:\n", " request = json.load(req)\n", " url = unquote(request['url'])\n", " time_parameter = [t for t in url.split('&') if t.startswith('TIME=')][0]\n", " time = time_parameter.split('TIME=')[1].split('/')[0]\n", " return dt.datetime.strptime(time, \"%Y-%m-%dT%H:%M:%S\")\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1f4e5844-34bc-4cb9-9365-46356b1db552", "metadata": {}, "outputs": [], "source": [ "if __name__ == \"__main__\":\n", " folders = glob.glob('.\\\\test_dir2\\\\*')\n", " dates = [get_request_dt(f'{folder}/request.json') for folder in folders]\n", " df = pd.DataFrame(zip(folders, dates), columns=['folder', 'datetime'])\n", " df.sort_values(by='datetime', ascending=True, inplace=True)\n", " print(df.head().to_markdown())" ] }, { "cell_type": "code", "execution_count": null, "id": "4319580b-f0b7-4370-91f4-bc4fcf068bc3", "metadata": {}, "outputs": [], "source": [ "def get_request_dt(request_file):\n", " with open(request_file, 'r') as req:\n", " request = json.load(req)\n", " url = unquote(request['url'])\n", " return url.split('/')" ] }, { "cell_type": "code", "execution_count": null, "id": "87580f26-0ccc-4061-a460-52d941a90b6a", "metadata": {}, "outputs": [], "source": [ "folders = glob.glob('.\\\\test_dir2\\\\*')\n", "[get_request_dt(f'{folder}/request.json') for folder in folders]" ] }, { "cell_type": "code", "execution_count": null, "id": "6569956e-91c5-42c9-acfa-74fde22ee1ce", "metadata": { "tags": [] }, "outputs": [], "source": [ "folders = glob.glob('.\\\\test_dir2\\\\*')\n", "dates = [get_request_dt(f'{folder}/request.json') for folder in folders]\n", "dates" ] }, { "cell_type": "code", "execution_count": null, "id": "2608ac51-fa24-4fd6-8758-cf27bda8860e", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.4" } }, "nbformat": 4, "nbformat_minor": 5 }