{"id":59,"date":"2022-01-09T15:04:09","date_gmt":"2022-01-09T15:04:09","guid":{"rendered":"https:\/\/meteoexploration.com\/blog\/?p=59"},"modified":"2025-07-14T16:27:09","modified_gmt":"2025-07-14T14:27:09","slug":"hands-on-solar-radiation","status":"publish","type":"post","link":"https:\/\/meteoexploration.com\/blog\/index.php\/2022\/01\/09\/hands-on-solar-radiation\/","title":{"rendered":"Hands on Solar Radiation"},"content":{"rendered":"<p><a href=\"https:\/\/www.meteoexploration.com\/python\/\"><code>python implementation<\/code><\/a><\/p>\n<div style=\"width: 735px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-59-1\" width=\"735\" height=\"705\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneinsol.mp4?_=1\" \/><a href=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneinsol.mp4\">https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneinsol.mp4<\/a><\/video><\/div>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">We\u2019ll have a quick look at some of the functions in the R package insol. Here we will learn to calculate shadows and illumination intensity and to compute insolation on solar panels, windows or complex terrain. There is a basic description of the background processes which involve vector algebra. It helps to understand how it works, but it is not needed for practical applications.<\/span><\/p>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\">Background<\/span><\/h4>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">R insol follows the algorithms developed in Corripio (2003), which treat the sun and every grid cell as a vector. A usual terrain representation is a Digital Elevation Model (DEM), that is a matrix of terrain elevation (Z) at given regular spacing in the X (eastings) and Y (northings) direction. Because most modern computer languages are very efficient at dealing with matrices, these solar algorithms are quite fast.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">We define the sun vector as a unit vector pointing towards the sun in a reference system fixed on the observer, as in the figure below. Direction of the axes are arbitrary, as long as they are consistent. In this case we chose X as east-west and positive eastwards, Y as north-south and positive southwards, and Z along the Earth radius and positive upwards.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-61 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/earthsunvector2.png\" alt=\"\" width=\"511\" height=\"345\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/earthsunvector2.png 379w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/earthsunvector2-300x203.png 300w\" sizes=\"auto, (max-width: 511px) 100vw, 511px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 1.- Representation of the vector defining the position of the sun<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The terrain is treated as a matrix of unit vectors perpendicular to every grid cell in the DEM. It is calculated as the cross product\u00a0 $\\vec{a} \\times \\vec{b}$ in Figure 2, or rather the average $\\vec{a} \\times \\vec{b}$ and\u00a0 $\\vec{c} \\times \\vec{d}$ as 4 points are not necessarily on the same plane. Function <code>cgrad()<\/code> in insol makes the computation and returns a 3D Matrix with the same number of rows and columns as the DEM and the 3 layers corresponding to the x, y and z components of the normal vectors. In this way to estimate the illumination intensity we only need to compute the dot product of the sun vector with every vector normal to the grid terrain. The dot product of two unit vectors is equal the cosine of the angle between the vectors, in the same way that illumination intensity is proportional to the cosine of the angle between the sun and the surface. The illumination intensity would be $\\vec{Sv} \\cdot \\vec{n}$. Note that $X$ in Figure 2 is the opposite direction than in Figure 1, but it is shown that way for clarity.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-67 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/normalvectorterraincp.png\" alt=\"\" width=\"665\" height=\"485\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/normalvectorterraincp.png 665w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/normalvectorterraincp-300x219.png 300w\" sizes=\"auto, (max-width: 665px) 100vw, 665px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 2.- Terrain representation as a Digital Elevation Model and vector normal to every grid cell.<\/span><\/p>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\">Relief shading of a geometric shape<\/span><\/h4>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">These algorithms are ready to use in the R package insol. To produce the cone shadow animation at the beginning of the post requires only a few lines of code. First, let\u2019s create the actual cone:<\/span><\/p>\n<div class=\"enlighter\">\n<div class=\"\">\n<div class=\"enlighter\">\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\"># let's create a cone\ncone = function(x, y){sqrt(x^2+y^2)}\nx = y &lt;- seq(-1, 1,length=200)\nz = outer(x, y, cone)\n# pointing up and removing squared sides\nz = max(z)-min(z[,1])-z\nz[z&lt;0] = 0\n# vertical enhancement and proportional cell size\nz = z*300\ncellsize = 1\n#let's have a look: \npersp(x,y,z)<\/pre>\n<\/div>\n<\/div>\n<div class=\"\">\n<div><\/div>\n<\/div>\n<\/div>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">We insert that cone in the middle of a larger DEM to be able to visualise the shadows, then run an example with illumination at 45\u00b0 elevation, 315\u00b0 azimuth, as shown in Figure 3. Placing the cone in the middle let us visualise shadows both in the northern and southern hemisphere.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\"># let's create a cone\ncone = function(x, y){sqrt(x^2+y^2)}\nx = y &lt;- seq(-1, 1,length=200)\nz = outer(x, y, cone)\n# pointing up and removing squared sides\nz = max(z)-min(z[,1])-z\nz[z&lt;0] = 0\n# vertical enhancement and proportional cell size\nz = z*300\ncellsize = 1\n#let's have a look: \npersp(x,y,z)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-71 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneshadow-600x533-1.jpg\" alt=\"\" width=\"600\" height=\"533\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneshadow-600x533-1.jpg 600w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/coneshadow-600x533-1-300x267.jpg 300w\" sizes=\"auto, (max-width: 600px) 100vw, 600px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 3.- DEM of a cone with illumination at 45\u00b0 from the North West.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To replicate the initial animation we can simulate the actual illumination from 6 a.m. to 6 p.m for a cone on a flat surface located at the coordinates of Edinburgh Arthur\u2019s Seat on the spring equinox. The following code creates each individual image (145 of them), later they can be processed into a video using ffmpeg, ImageMagick or any other suitable software. For this example we used QuickTime 7 pro.<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">require(rgl)\nlat = 55.943985\nlon = -3.161953\nxx = 1:1000\nyy = 1:1000\nopen3d(zoom = 0.85, windowRect=c(20,65,755,770))\n# create an array of julian dates from 6 a.m. to 6 p.m at 5 minutes interval\njd = JD(seq(ISOdate(2019,3,20,6),ISOdate(2019,3,20,18),by=\"5 min\"))\n# set the preferred orientation of the image with the mouse before doing \n# the following loop, or use rgl.viewpoint() or userMatrix\ni = 1\nfor (jdt in jd){\n    print(paste(\"Timestep \",i,\"of\",length(jd),jdt))\n    sv = sunvector(jdt, lat, lon, 0)\n    hsh = grd[,,1]*sv[1]+grd[,,2]*sv[2]+grd[,,3]*sv[3] \n    hsh = (hsh+abs(hsh))\/2\n    sh = doshade(dem,sv,cellsize)\n    hshsh = hsh*sh\n    hshsh=(t(hshsh[nrow(sh):1,]))\n    pngname = paste('coneshadow',i,'.png',sep='')\n    clear3d()\n    terrain3d(xx,yy,dem,col=grey(hshsh),lit=FALSE)\n    rgl.snapshot(pngname)\n    i=i+1\n}<\/pre>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\">Relief shading of complex topography<\/span><\/h4>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">To apply illumination and shading to an actual DEM of complex topography, we can use the example provided for the Ordesa National Park in the Pyrenees. It is recommended to use the package raster, to get the actual geographical extent and latitude to longitude ratio. Insol requires a regular squared grid cell, that means that UTM projection is OK, but geographical latitude, longitude is not, as the actual interval in longitudes varies wth the latitude. First let\u2019s download the DEM:<\/span><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">library(raster)\ndemfile = tempfile()\ndownload.file(\"http:\/\/www.meteoexploration.com\/R\/insol\/data\/dempyrenees.tif\",demfile)\ndem = raster(demfile)\ngrd = cgrad(dem)\nlat = 42.647\nlon = -0.017\n# sunvector at 9:15 local time\njd = JD(ISOdatetime(2019,3,20,10,55,40))\nsv = sunvector(jdt, lat, lon, 0)\nhsh = grd[,,1]*sv[1]+grd[,,2]*sv[2]+grd[,,3]*sv[3]\n## remove negative incidence angles (self shading) \nhsh = (hsh+abs(hsh))\/2\nhsh = raster(hsh,crs=projection(dem))\nextent(hsh) = extent(dem)\nsh = doshade(dem,sv)\nhshsh = hsh*sh\nplot(hshsh,col=grey(1:100\/100),legend=FALSE)<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-73 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolOrdesaTSentinel-945x843-1.jpg\" alt=\"\" width=\"945\" height=\"843\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolOrdesaTSentinel-945x843-1.jpg 945w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolOrdesaTSentinel-945x843-1-300x268.jpg 300w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolOrdesaTSentinel-945x843-1-768x685.jpg 768w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 4.- Simulated insolation and shading on the Ordesa National Park in the Pyrenees in spring.<\/span><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-74 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/sentinel2Ordesa20190320-768x688-1.jpg\" alt=\"\" width=\"768\" height=\"688\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/sentinel2Ordesa20190320-768x688-1.jpg 768w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/sentinel2Ordesa20190320-768x688-1-300x269.jpg 300w\" sizes=\"auto, (max-width: 768px) 100vw, 768px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 5.- Sentinel-2 image cropped to the same region as in Figure 4.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The Sentinel image is a false colour representation where snow appears light blue. The image is taken near midday and illumination is from the South. This type of illumination is difficult to interpret and can cause relief inversion, with protruding valleys and sinking peaks, thats why a common illumination in maps is from the northwest (see more about relief inversion <a href=\"https:\/\/earthobservatory.nasa.gov\/blogs\/earthmatters\/2011\/07\/12\/are-you-ever-fooled-by-relief-inversion\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>). If we rotate the images the relief is appreciated correctly and more easily, as in Figure 6.<\/span><\/p>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-75 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/OrdesaRotated-945x450-1.jpg\" alt=\"\" width=\"945\" height=\"450\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/OrdesaRotated-945x450-1.jpg 945w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/OrdesaRotated-945x450-1-300x143.jpg 300w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/OrdesaRotated-945x450-1-768x366.jpg 768w\" sizes=\"auto, (max-width: 945px) 100vw, 945px\" \/><\/span><\/h4>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 6.- As Figure 5 and 6 rotated 180\u00b0<\/span><\/p>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\">Insolation on\u00a0solar panels or windows<\/span><\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-76 aligncenter\" src=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolGoriz.png\" alt=\"\" width=\"900\" height=\"700\" srcset=\"https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolGoriz.png 900w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolGoriz-300x233.png 300w, https:\/\/meteoexploration.com\/blog\/wp-content\/uploads\/2022\/01\/insolGoriz-768x597.png 768w\" sizes=\"auto, (max-width: 900px) 100vw, 900px\" \/><\/p>\n<p style=\"text-align: center;\"><span style=\"font-family: verdana, geneva, sans-serif;\">Figure 7.- Insolation on solar panels at a mountain hut in the Pyrenees.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">The same approach can be used to estimate insolation on a window, for energy efficiency calculations in buildings. A vertical window facing southeast would be defined by the vector<\/span><span class=\"enlighter\" style=\"font-family: verdana, geneva, sans-serif;\"><span class=\"enlighter\" style=\"font-family: verdana, geneva, sans-serif;\"><span class=\"enlighter-g1\"><code class=\"EnlighterJSRAW\" data-enlighter-language=\"r\">wv=normalvector(90,135).<\/code> <\/span><\/span><\/span><span style=\"font-family: verdana, geneva, sans-serif;\">The rest of the calculations are the same, although it should include shading from nearby buildings or the roof. R insol and climatic data have been used to produce high resolution the solar radiation maps in <a href=\"https:\/\/meteoexploration.com\/products\/solar\/solarradGR.php\">this link<\/a>.<\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\">Please check the manual, references and help in the code, either at <a href=\"https:\/\/meteoexploration.com\/R\/insol\/index.html\">our pages<\/a> or the <a href=\"http:\/\/cran.r-project.org\/web\/packages\/insol\/index.html\">CRAN<\/a> repository, for a more detailed discussion. The main algorithms are described in the paper below. We hope you can find the package useful.<\/span><\/p>\n<h4><span style=\"font-family: verdana, geneva, sans-serif;\">References<\/span><\/h4>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><strong>Corripio, J. G<\/strong>.: 2003, Vectorial algebra algorithms for calculating terrain parameters from DEMs and the position of the sun for solar radiation modelling in mountainous terrain,\u00a0<i>International Journal of Geographical Information Science<\/i> 17(1), 1\u201323. <a href=\"https:\/\/meteoexploration.com\/products\/publications\/corripio03_ijgis.pdf\" target=\"_blank\" rel=\"noopener noreferrer\">PDF<\/a><\/span><\/p>\n<p><span style=\"font-family: verdana, geneva, sans-serif;\"><strong>R project<\/strong> <a href=\"https:\/\/cran.r-project.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Manuals and Documentation<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>python implementation We&rsquo;ll have a quick look at some of the functions in the R package insol. Here we will learn to calculate shadows and illumination intensity and to compute insolation on solar panels, windows or complex terrain. There is a basic description of the background processes which involve vector algebra. It helps to understand&hellip;&nbsp;<a href=\"https:\/\/meteoexploration.com\/blog\/index.php\/2022\/01\/09\/hands-on-solar-radiation\/\" rel=\"bookmark\">Read More &raquo;<span class=\"screen-reader-text\">Hands on Solar Radiation<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":73,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"off","neve_meta_content_width":70,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[43,34,33],"tags":[40,37,38,44,36,39,35],"class_list":["post-59","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","category-r","category-solar-radiation","tag-code","tag-dem","tag-models","tag-python","tag-r","tag-simulation","tag-solar-radiation"],"_links":{"self":[{"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/59","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=59"}],"version-history":[{"count":10,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":111,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/59\/revisions\/111"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/73"}],"wp:attachment":[{"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meteoexploration.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}