diff --git a/Sentinal_Prod_Rev1.ipynb b/Sentinal_Prod_Rev1.ipynb new file mode 100644 index 0000000..7c4a0e6 --- /dev/null +++ b/Sentinal_Prod_Rev1.ipynb @@ -0,0 +1,676 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "name": "Sentinal_Prod_Rev1.ipynb", + "provenance": [], + "collapsed_sections": [], + "authorship_tag": "ABX9TyOGKbfeePmInXecPX3GsjEB", + "include_colab_link": true + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + } + }, + "cells": [ + { + "cell_type": "markdown", + "metadata": { + "id": "view-in-github", + "colab_type": "text" + }, + "source": [ + "\"Open" + ] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dnhitYmUDvaK" + }, + "source": [ + "Install sentinalsat, geopandas and rasterio." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "USkzgZGED7iv" + }, + "source": [ + "! pip install sentinelsat" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "N49y9erYn2_9" + }, + "source": [ + "! pip install geopandas" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "whocVGN7oVsb" + }, + "source": [ + "! pip install rasterio" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "B52BVeQxEBSG" + }, + "source": [ + "Various imports." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "bmlINOZLnvRL" + }, + "source": [ + "from sentinelsat import SentinelAPI,read_geojson, geojson_to_wkt\n", + "import geopandas as gpd\n", + "import rasterio as rio\n", + "from rasterio.plot import show\n", + "from rasterio import mask\n", + "import numpy as np\n", + "import glob\n", + "import matplotlib.pyplot as plt" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "QK5U3-zQEGw2" + }, + "source": [ + "User name and Password for Sentinal." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "7CtX1njrM4BP" + }, + "source": [ + "user= 'sudhir2016'" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5gQtK6LwM_5X" + }, + "source": [ + "password= 'sudhir55'" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "V4eZfxuPFFE1" + }, + "source": [ + "Create sentinal query based on our plot coordinates, required time window and cloud cover filter. Sentinal will provide list of availalble tiles called products." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "-NW_fVGINFEw" + }, + "source": [ + "api = SentinelAPI(user, password, 'https://scihub.copernicus.eu/dhus')" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "qIp1qCYiS6MS" + }, + "source": [ + "footprint = geojson_to_wkt(read_geojson('plot.geojson'))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "A57H5HpiSpjE" + }, + "source": [ + "products = api.query(footprint,\n", + " date = ('20201101', '20201130'),\n", + " platformname = 'Sentinel-2',\n", + " processinglevel = 'Level-2A',\n", + " cloudcoverpercentage = (0,100)\n", + " )" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "sI52YLYhIn6w" + }, + "source": [ + "load products as geopandas dataframe." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "SY2AOtcGk3TZ" + }, + "source": [ + "products_gdf = api.to_geodataframe(products)\n", + "#print(products_gdf)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "jeUXr0rnGvko" + }, + "source": [ + "Obtain ID of most recent Sentinal dataset." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "BGtEXB57tzel" + }, + "source": [ + "uuid=products_gdf['uuid']" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "VWH7wM66KKte" + }, + "source": [ + "#cloud=products_gdf['cloudcoverpercentage']\n", + "#print(cloud)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "s3fOjRq2zkJQ" + }, + "source": [ + "uuid1=uuid[0]\n", + "#print(uuid1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ybHhA3esHXxo" + }, + "source": [ + "Download most recent Sentinal data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "drHGzgGcl0ot" + }, + "source": [ + "api.download(uuid1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ei4RjgIwHfuO" + }, + "source": [ + "Obtain filename of downloaded Sentilnal zip file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "usAzSam4e6qX" + }, + "source": [ + "for name in glob.glob('/content/*.zip'):\n", + " file=name\n", + "#print(file)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7vnUqBLiJEwQ" + }, + "source": [ + "Unzip Sentinal file." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "3Bc-57nmgVAN" + }, + "source": [ + "! unzip '$file'" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "s0xZr5tULSMG" + }, + "source": [ + "Obtain the name to the Sentinal data directory." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "Me81pZsNLcOL" + }, + "source": [ + "for name in glob.glob('/content/*.SAFE'):\n", + " file1=name\n", + "#print(file1)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "dl6w8k-oM2I9" + }, + "source": [ + "Obtain the path of the Sentinal image data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "2pVeY2_HMHwQ" + }, + "source": [ + "for name in glob.glob(file1+'/GRANULE/*'):\n", + " file2=name\n", + "#print(file2)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "Ou5DFzPDRAeX" + }, + "source": [ + "Obtain the file names of B2(B),B3(G),B4(R) and B8(NIR) channel images." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "PMBXmyxLXKQ-" + }, + "source": [ + "file3=[]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "DcKc6PN-Wa2l" + }, + "source": [ + "for name in glob.glob(file2+'/IMG_DATA/R10m/*'):\n", + " file3.append(name)\n", + "file3.sort()\n", + "#print(file3)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "IvCARpmMRN_N" + }, + "source": [ + "Read the B4 and B8 channel data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "5D1FtXQrZOBr" + }, + "source": [ + "b4=rio.open(file3[3])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "upgmmxvsaA4X" + }, + "source": [ + "b8=rio.open(file3[4])" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "mic5YXtH2tlA" + }, + "source": [ + "Convert plot coodinates to Sentinal crs." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "A_HK9ymkD5fc" + }, + "source": [ + "crs=b4.crs\n", + "#print(crs)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "1dk50r2T2pbV" + }, + "source": [ + "plot=gpd.read_file('plot.geojson')" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ACYzSVcQ2zbs" + }, + "source": [ + "plot1=plot.to_crs({'init': crs})" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "vHvZk8vl22lQ" + }, + "source": [ + "geo=plot1.geometry" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "xy-SCMbYSP8H" + }, + "source": [ + "Read B4(red), create red.tiff, extract red_plot.tiff and red_data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "ni_S9zbzBsrV" + }, + "source": [ + "red = b4.read()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "0Lqdb3kuw9oM" + }, + "source": [ + "meta = b4.meta\n", + "meta.update(driver='GTiff')\n", + "meta.update(dtype=rio.float32)\n", + "\n", + "with rio.open('red.tiff', 'w', **meta) as dst:\n", + " dst.write(red.astype(rio.float32))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "Oqe3frL1xmop" + }, + "source": [ + "with rio.open(\"red.tiff\") as src:\n", + " out_image, out_transform = rio.mask.mask(src,geo,crop=True)\n", + " out_meta = src.meta.copy()\n", + " out_meta.update({\"driver\": \"GTiff\",\n", + " \"height\": out_image.shape[1],\n", + " \"width\": out_image.shape[2],\n", + " \"transform\": out_transform})" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "ClDKK3WGxusl" + }, + "source": [ + "with rio.open(\"red_plot.tiff\", \"w\", **out_meta) as dest:\n", + " dest.write(out_image)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "_Ozi0VaNzDO_" + }, + "source": [ + "src = rio.open(\"red_plot.tiff\")\n", + "red_data=src.read(1)\n", + "print(red_data)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "ksX0rp4DyK7h" + }, + "source": [ + "Read B8(nir), create nir.tiff and extract nir_plot.tiff and nir_plot_data." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "u8qL16BUyk2F" + }, + "source": [ + "nir = b8.read()" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "5rA6IYYPDg6-" + }, + "source": [ + "meta = b4.meta\n", + "meta.update(driver='GTiff')\n", + "meta.update(dtype=rio.float32)\n", + "\n", + "with rio.open('nir.tiff', 'w', **meta) as dst:\n", + " dst.write(nir.astype(rio.float32))" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "G3hUR3iOywxV" + }, + "source": [ + "with rio.open(\"nir.tiff\") as src:\n", + " out_image, out_transform = rio.mask.mask(src,geo,crop=True)\n", + " out_meta = src.meta.copy()\n", + " out_meta.update({\"driver\": \"GTiff\",\n", + " \"height\": out_image.shape[1],\n", + " \"width\": out_image.shape[2],\n", + " \"transform\": out_transform})" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "RwCmrb08y3xj" + }, + "source": [ + "with rio.open(\"nir_plot.tiff\", \"w\", **out_meta) as dest:\n", + " dest.write(out_image)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "sxc_slRdzz8D" + }, + "source": [ + "src = rio.open(\"nir_plot.tiff\")\n", + "nir_data=src.read(1)\n", + "print(nir_data)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": { + "id": "7YOn68T1z4gd" + }, + "source": [ + "Calculate ndvi_plot." + ] + }, + { + "cell_type": "code", + "metadata": { + "id": "g3Yu3XhS3QOB" + }, + "source": [ + "ndvi_plot = (nir_data.astype(float)-red_data.astype(float))/(nir_data+red_data)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "TOb4GkMq3heG" + }, + "source": [ + "ndvi_plot1=np.nan_to_num(ndvi_plot)" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "pbnp52VS3pbp" + }, + "source": [ + "ndvi_plot2=ndvi_plot1[ndvi_plot1 != 0]" + ], + "execution_count": null, + "outputs": [] + }, + { + "cell_type": "code", + "metadata": { + "id": "2NRVMrl13wbd" + }, + "source": [ + "Mean_ndvi_plot=np.sum(ndvi_plot2)/ndvi_plot2.size\n", + "print(Mean_ndvi_plot)" + ], + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file