{ "cells": [ { "cell_type": "markdown", "id": "eff9f1b9-ebce-4968-aa1d-eb33efd37813", "metadata": {}, "source": [ "## Demo - BandMaths" ] }, { "cell_type": "code", "execution_count": 1, "id": "f3b2ca32-63fc-4a92-8be4-5f459be87a9f", "metadata": {}, "outputs": [], "source": [ "from snapista import Graph\n", "from snapista import Operator\n", "from snapista import read_file\n", "from snapista import TargetBand\n", "from snapista import TargetBandDescriptors\n", "\n", "#import os \n", "#os.environ['PATH'] = '/srv/conda/envs/env_snap/snap/bin:' + os.environ['PATH']" ] }, { "cell_type": "code", "execution_count": 4, "id": "d0ac6a29-c799-41b2-b028-e55a3315de63", "metadata": {}, "outputs": [], "source": [ "\n", "try:\n", " g = Graph()\n", "except Exception:\n", " # running in vs container\n", " import os \n", " os.environ['PATH'] = '/srv/conda/envs/env_snap/snap/bin:' + os.environ['PATH']\n", " \n", " g = Graph()" ] }, { "cell_type": "code", "execution_count": 5, "id": "1a94bc96-3104-4726-b01c-48fad9cfb5d6", "metadata": {}, "outputs": [], "source": [ "g.add_node(operator=Operator(\"Read\", file='/data/S2B_MSIL2A_20210608T112119_N0300_R037_T29SMC_20210608T131325.SAFE/MTD_MSIL2A.xml'), node_id=\"Read\")\n" ] }, { "cell_type": "code", "execution_count": 6, "id": "c124c723-3756-43e5-ab7e-db1efdfaa62f", "metadata": {}, "outputs": [], "source": [ "g.add_node(operator=Operator(\"Resample\", referenceBandName=\"B2\"), node_id=\"Resample\", source=\"Read\")" ] }, { "cell_type": "code", "execution_count": 7, "id": "96b93cd5-2f58-442b-88c3-ce4cbc793661", "metadata": {}, "outputs": [], "source": [ "band_maths = Operator('BandMaths')\n", "\n", "cloud_class = TargetBand(name='Cloud_Class',\n", " expression='if quality_cloud_confidence > 90 then 2 else (if quality_cloud_confidence > 50 AND B8 > 0.3 then 1 else 0)')\n", " \n", "band_maths.targetBandDescriptors = TargetBandDescriptors([cloud_class])" ] }, { "cell_type": "code", "execution_count": 8, "id": "1564302c-f7a2-47a2-a5f6-85147b88536b", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "targetBandDescriptors \n" ] } ], "source": [ "g.add_node(operator=band_maths, node_id=\"BandMaths\" ,source='Resample')" ] }, { "cell_type": "code", "execution_count": 9, "id": "dbfc1084-bb1f-4832-9b80-407c1f22f103", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "targetBandDescriptors \n" ] } ], "source": [ "band_maths = Operator('BandMaths')\n", "\n", "ndsi = TargetBand(name='NDSI',\n", " expression='(B3-B11)/(B3+B11)')\n", " \n", "band_maths.targetBandDescriptors = TargetBandDescriptors([ndsi])\n", "\n", "g.add_node(operator=band_maths, node_id=\"BandMaths_2\" ,source='Resample')" ] }, { "cell_type": "code", "execution_count": 10, "id": "6558126a-e281-497d-a365-3a6b0497762c", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Operator('BandMerge', sourceBandNames='B3,B4,B11,B12,Cloud_Class,NDSI', geographicError='1.0E-5f', source='['Resample', 'BandMaths', 'BandMaths_2']')" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "merge = Operator(\"BandMerge\", sourceBandNames=\"B3,B4,B11,B12,Cloud_Class,NDSI\", source=[\"Resample\", \"BandMaths\", 'BandMaths_2'])\n", "\n", "merge" ] }, { "cell_type": "code", "execution_count": 11, "id": "216903df-8ef4-403b-9437-5653733120fb", "metadata": {}, "outputs": [], "source": [ "g.add_node(operator=merge, node_id=\"Merge\", source=[\"Resample\", \"BandMaths\", 'BandMaths_2'])" ] }, { "cell_type": "code", "execution_count": 12, "id": "8f90a68b-d124-428e-bdf6-1f3ea070c667", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "Operator('Write', file='None', formatName='BEAM-DIMAP', deleteOutputOnFailure='true', writeEntireTileRows='false', clearCacheAfterRowWrite='false')" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "write = Operator(\"Write\")\n", "\n", "write" ] }, { "cell_type": "code", "execution_count": 13, "id": "0548a3a6-cf3c-4f26-a48d-0e92556ee09c", "metadata": {}, "outputs": [], "source": [ "g.add_node(operator=Operator(\"Write\", file='snow.dim'), node_id=\"Write\", source='Merge')" ] }, { "cell_type": "code", "execution_count": 14, "id": "825327d3-cc7f-4a1d-840d-52d40a39c4fd", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", " 1.0\n", " \n", " Read\n", " \n", " \n", " \n", " true\n", " /data/S2B_MSIL2A_20210608T112119_N0300_R037_T29SMC_20210608T131325.SAFE/MTD_MSIL2A.xml\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " Resample\n", " \n", " \n", " \n", " \n", " \n", " First\n", " First\n", " B2\n", " true\n", " \n", " \n", " \n", " \n", " Nearest\n", " \n", " \n", " \n", " BandMaths\n", " \n", " \n", " \n", " \n", " \n", " \n", " Cloud_Class\n", " if quality_cloud_confidence > 90 then 2 else (if quality_cloud_confidence > 50 AND B8 > 0.3 then 1 else 0)\n", " float32\n", " \n", " \n", " NaN\n", " \n", " \n", " \n", " \n", " \n", " \n", " BandMaths\n", " \n", " \n", " \n", " \n", " \n", " \n", " NDSI\n", " (B3-B11)/(B3+B11)\n", " float32\n", " \n", " \n", " NaN\n", " \n", " \n", " \n", " \n", " \n", " \n", " BandMerge\n", " \n", " \n", " \n", " \n", " \n", " \n", " 1.0E-5f\n", " B3,B4,B11,B12,Cloud_Class,NDSI\n", " \n", " \n", " \n", " Write\n", " \n", " \n", " \n", " \n", " false\n", " true\n", " snow.dim\n", " BEAM-DIMAP\n", " false\n", " \n", " \n", "\n", "\n" ] } ], "source": [ "g.view()" ] }, { "cell_type": "code", "execution_count": 15, "id": "3328eb96-de62-4fbe-9f05-f0266b22b69d", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Processing the graph\n", "Executing processing graph\n", "INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters\n", "....10%....20%....30%....40%....50%....60%....70%....80%....90% done.\n", "INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: GDAL not found on system. Internal GDAL 3.0.0 from distribution will be used. (f1)\n", "INFO: org.esa.s2tbx.dataio.gdal.GDALVersion: Internal GDAL 3.0.0 set to be used by SNAP.\n", "INFO: org.esa.snap.core.util.EngineVersionCheckActivator: Please check regularly for new updates for the best SNAP experience.\n" ] }, { "data": { "text/plain": [ "0" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "g.save_graph('snow.xml')\n", "g.run()" ] }, { "cell_type": "code", "execution_count": null, "id": "eaef2574-6a7d-484b-86f9-69f8f4b531e3", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "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.9.4" } }, "nbformat": 4, "nbformat_minor": 5 }