{ "cells": [ { "cell_type": "markdown", "id": "33082a70-1d2d-4bfa-bc45-981115829941", "metadata": {}, "source": [ "# Solar Energy Potential Analysis\n", "\n", "This is an introduction to solar radiation modeling in *GRASS GIS* in *Jupyter Notebook*. In addition to common *Python* packages, it demonstrates the usage of `grass.script`, the *Python* API for GRASS GIS, and `grass.jupyter`, an experimental *Jupyter Notebook* specific package that helps with the launch of *GRASS GIS* and with displaying maps.\n", "\n", "This interactive notebook is available online thanks to the [https://mybinder.org](Binder) service. To run the select part (called a *cell*), hit `Shift + Enter`." ] }, { "cell_type": "markdown", "id": "2b47cfa6-312b-4db9-873d-7e2333589411", "metadata": {}, "source": [ "## Starting GRASS in Jupyter Notebooks" ] }, { "cell_type": "code", "execution_count": null, "id": "66e65bb3-6401-448f-bc1e-475ac5fcf1ad", "metadata": {}, "outputs": [], "source": [ "# Import Python standard library and IPython packages we need.\n", "import os\n", "import subprocess\n", "import sys\n", "\n", "# Ask GRASS GIS where its Python packages are.\n", "sys.path.append(\n", " subprocess.check_output([\"grass\", \"--config\", \"python_path\"], text=True).strip()\n", ")\n", "\n", "# Import the GRASS GIS packages we need.\n", "import grass.script as gs\n", "import grass.jupyter as gj\n", "\n", "# Start GRASS Session\n", "gj.init(\"../../data/grassdata\", \"nc_basic_spm_grass7\", \"user1\")\n", "\n", "# Set computational region to elevation raster\n", "gs.run_command(\"g.region\", raster=\"elevation@PERMANENT\", flags=\"pg\")" ] }, { "cell_type": "markdown", "id": "70a1e6ee-39a0-4936-b656-95976c3cce44", "metadata": { "tags": [] }, "source": [ "## Solar radiation analysis" ] }, { "cell_type": "markdown", "id": "8a7d0768-80ca-4190-ba8c-d19b4719e7d2", "metadata": {}, "source": [ "Prepare input maps (slope and aspect):" ] }, { "cell_type": "code", "execution_count": null, "id": "bca26a9a-126b-41d0-a8db-c58e4ac9c58d", "metadata": {}, "outputs": [], "source": [ "gs.run_command(\"r.slope.aspect\", elevation=\"elevation\", aspect=\"aspect\", slope=\"slope\")" ] }, { "cell_type": "markdown", "id": "f3567f03-624b-4a17-8463-6715f86e6b75", "metadata": {}, "source": [ "### Incidence angles and cast shadows\n", "\n", "Compute the sun position on Dec. 22 at 4:15pm, EST (no map output expected):" ] }, { "cell_type": "code", "execution_count": null, "id": "0d01b59b-2c2e-440b-886d-bb484451a8ba", "metadata": {}, "outputs": [], "source": [ "gs.run_command(\"r.sunmask\", elevation=\"elevation\", year=\"2001\", month=\"12\", day=\"22\", hour=\"16\", minute=\"15\", sec=\"0\", timezone=\"-5\", flags=\"s\")" ] }, { "cell_type": "markdown", "id": "311fe6d4-a2d4-4295-9e86-798f0debe28d", "metadata": {}, "source": [ "Calculate incidence angles including cast shadows." ] }, { "cell_type": "code", "execution_count": null, "id": "116f5f2c-48e0-483b-b49d-3595196d8714", "metadata": {}, "outputs": [], "source": [ "gs.run_command(\"r.sun\", elevation=\"elevation\", aspect=\"aspect\", slope=\"slope\", incidout=\"incident\", day=\"356\", time=\"16.25\")\n", "gs.parse_command(\"r.info\", map=\"incident\", flags=\"g\")\n", "gs.run_command(\"r.colors\", map=\"incident\", co=\"bcyr\", flags=\"e\")" ] }, { "cell_type": "code", "execution_count": null, "id": "8374778c-339f-410a-a2c7-099cabef20f3", "metadata": {}, "outputs": [], "source": [ "# Display incident angles\n", "solar_map = gj.GrassRenderer()\n", "solar_map.d_rast(map=\"incident\")\n", "solar_map.d_legend(raster=\"incident\", fontsize=12, at=\"25,75,1,5\")\n", "solar_map.show()" ] }, { "cell_type": "markdown", "id": "b0f211b3-e5d7-4051-9a80-0f5ce0cf9665", "metadata": {}, "source": [ "Extract the cast shadow area for 4:15pm." ] }, { "cell_type": "code", "execution_count": null, "id": "d64db7b3-c22a-4a26-a32b-fccc7eb0858d", "metadata": {}, "outputs": [], "source": [ "gs.mapcalc(\"shadow = if(isnull(incident), 1, null())\")\n", "gs.run_command(\"r.colors\", map=\"shadow\", color=\"grey\")\n", "gs.run_command(\"r.colors\", map=\"elevation\", color=\"elevation\")" ] }, { "cell_type": "code", "execution_count": null, "id": "2ccf0d4e-633f-463c-bb12-6fe151a03fb1", "metadata": {}, "outputs": [], "source": [ "# Display 4:15pm Shadows\n", "shadow_map = gj.GrassRenderer()\n", "shadow_map.d_shade(shade=\"shadow\", color=\"elevation\")\n", "shadow_map.show()" ] }, { "cell_type": "markdown", "id": "30758fb8-7658-41d3-ad25-199bf8e8e4e7", "metadata": {}, "source": [ "### Solar radiation\n", "Compute global (beam+diffuse+refl) radiation for entire day during summer and winter solstice. This may take a minute to run.\n", "\n", "Display the radiation maps." ] }, { "cell_type": "code", "execution_count": null, "id": "753dcb19-a2f1-4329-ba39-912e709ff85e", "metadata": {}, "outputs": [], "source": [ "gs.run_command(\"r.sun\", elevation=\"elevation\", aspect=\"aspect\", slope=\"slope\", day=\"356\", glob_rad=\"winter\", insol_time=\"its356\")\n", "gs.run_command(\"r.colors\", map=\"winter\", co=\"gyr\", flags=\"e\")\n", "\n", "gs.run_command(\"r.sun\", elevation=\"elevation\", aspect=\"aspect\", slope=\"slope\", day=\"172\", glob_rad=\"summer\", insol_time=\"its172\")\n", "gs.run_command(\"r.colors\", map=\"summer\", co=\"gyr\", flags=\"e\")" ] }, { "cell_type": "code", "execution_count": null, "id": "0a27993b-126a-44c8-8102-dd85ee9aee96", "metadata": {}, "outputs": [], "source": [ "rad_map = gj.InteractiveMap()\n", "\n", "# Set opacity to 1.0 (default is 0.8) so that we can see colors clearly\n", "rad_map.add_raster(\"winter\", opacity=1.0)\n", "rad_map.add_raster(\"summer\", opacity=1.0)\n", "rad_map.add_layer_control()\n", "\n", "rad_map.show()" ] } ], "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.8.10" } }, "nbformat": 4, "nbformat_minor": 5 }