From 2ca7a4458e0048f7cc1463968db2b06a5b147a81 Mon Sep 17 00:00:00 2001 From: Ujaval Gandhi Date: Tue, 17 Oct 2023 17:45:07 +0400 Subject: [PATCH] update --- .../computing_indices.ipynb | 583 +++++++++++++++++- 1 file changed, 560 insertions(+), 23 deletions(-) diff --git a/code/python_remote_sensing/computing_indices.ipynb b/code/python_remote_sensing/computing_indices.ipynb index fadf989e..1526032e 100644 --- a/code/python_remote_sensing/computing_indices.ipynb +++ b/code/python_remote_sensing/computing_indices.ipynb @@ -35,12 +35,12 @@ "id": "ZUMFpnKjR5Or" }, "id": "ZUMFpnKjR5Or", - "execution_count": 1, + "execution_count": 3, "outputs": [] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 4, "id": "fe6c71dd-37df-47af-9019-d012cc9b5e59", "metadata": { "id": "fe6c71dd-37df-47af-9019-d012cc9b5e59" @@ -69,7 +69,7 @@ "id": "5zB54v7PT1kT" }, "id": "5zB54v7PT1kT", - "execution_count": 3, + "execution_count": 5, "outputs": [] }, { @@ -88,23 +88,11 @@ "download(data_url + filename)" ], "metadata": { - "id": "0ckP_6VIT4Li", - "outputId": "8ff26f96-4dae-41be-8eb1-6629e0443a6e", - "colab": { - "base_uri": "https://localhost:8080/" - } + "id": "0ckP_6VIT4Li" }, "id": "0ckP_6VIT4Li", - "execution_count": 4, - "outputs": [ - { - "output_type": "stream", - "name": "stdout", - "text": [ - "Downloaded data/S2A_MSIL1C_20230212T050931_N0509_R019_T43PGQ_20230212T065641.SAFE.zip\n" - ] - } - ] + "execution_count": 6, + "outputs": [] }, { "cell_type": "markdown", @@ -121,7 +109,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "id": "8bb938d8-46f1-4206-ac59-badb3d6eb58c", "metadata": { "id": "8bb938d8-46f1-4206-ac59-badb3d6eb58c" @@ -146,13 +134,562 @@ }, { "cell_type": "code", - "source": [], + "source": [ + "import glob\n", + "s2_folder = s2_filepath[:-4]\n", + "\n", + "bands = []\n", + "\n", + "for filepath in glob.glob(os.path.join(s2_folder, 'GRANULE', '*', 'IMG_DATA', '*B*.jp2')):\n", + " band = rxr.open_rasterio(filepath, chunks={'x':2048, 'y':2048})\n", + " filename = os.path.basename(filepath)\n", + " # Extract the part of the filename containing band name such as 'B01'\n", + " name = os.path.splitext(filename)[0].split('_')[-1]\n", + " band.name = name\n", + " band = band.assign_coords(band=[name])\n", + " bands.append(band)\n", + "\n", + "scene = xr.concat(bands, dim='band')\n", + "scene.name = 'S2'" + ], "metadata": { - "id": "SDHk8ApFyBDO" + "id": "Rf0m1vXtsLQd" }, - "id": "SDHk8ApFyBDO", - "execution_count": null, + "id": "Rf0m1vXtsLQd", + "execution_count": 24, "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "scene.sel(band=['B04', 'B03', 'B02'])" + ], + "metadata": { + "id": "B_NZNYdD2iAy", + "outputId": "06a421df-d55c-4b48-d650-fc0858ddd7fb", + "colab": { + "base_uri": "https://localhost:8080/", + "height": 448 + } + }, + "id": "B_NZNYdD2iAy", + "execution_count": 25, + "outputs": [ + { + "output_type": "execute_result", + "data": { + "text/plain": [ + "\n", + "dask.array\n", + "Coordinates:\n", + " * x (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.098e+05 8.098e+05\n", + " * y (y) float64 1.39e+06 1.39e+06 1.39e+06 ... 1.5e+06 1.5e+06\n", + " * band (band) \n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "\n", + "
<xarray.DataArray 'S2' (band: 3, y: 16470, x: 16470)>\n",
+              "dask.array<getitem, shape=(3, 16470, 16470), dtype=float32, chunksize=(1, 3072, 3072), chunktype=numpy.ndarray>\n",
+              "Coordinates:\n",
+              "  * x            (x) float64 7e+05 7e+05 7e+05 ... 8.097e+05 8.098e+05 8.098e+05\n",
+              "  * y            (y) float64 1.39e+06 1.39e+06 1.39e+06 ... 1.5e+06 1.5e+06\n",
+              "  * band         (band) <U3 'B04' 'B03' 'B02'\n",
+              "    spatial_ref  int64 0\n",
+              "Attributes:\n",
+              "    scale_factor:  1.0\n",
+              "    add_offset:    0.0
" + ] + }, + "metadata": {}, + "execution_count": 25 + } + ] } ], "metadata": {