Like is says.
Additions: 2017-12-20
Added the hillshade calculation that was missing from the original.
Modified: 2016-12-05
Simplified aspect calculation and added the ability of how to classify 'flat' during aspect calculations. this should clean up those pesky random seemingly random aspect values in areas of small slope.
This is simply an implementation of the 3rd order finite difference method of determining slope and aspect (Horton 1981, Burrough et al. various) and used by the Spatial Analyst extension. It is also the base for a variety of terrain derivatives. I have improved the base algorithm to facilitate using larger arrays/rasters in a shorter period of time. This implementation doesn't account for nodata values... I will leave that for the Code Sharing site. I will leae the code documentation and discussion attached, but bear in mind it is much slower than the code below... but the fundamentals are the same.
This approach provides an estimate of slope which attempts to account for the cell values (ie elevations) in the 8 neighbours surrounding a core cell. A measure of the slope in both the X and Y directions is made and averaged.
Alternate measures of slope can be simply implemented using variants of the fundamental algorithms documented in the attachment. The author has also worked with implementations of simple hillshading and related neighbour functions which employ the same or similar methods of parsing 2D arrays into a 4D array.
The block or tiling process that is used to produce the subarrays has been documented in previous threads and here.
- 
| Sample surface... |
|---|
Slopes form a pyramid. Edges trimmed... <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> Dem<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
Slope<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">11.31</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">15.79</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">13.9</SPAN> <SPAN class="number token">8.53</SPAN> <SPAN class="number token">13.9</SPAN> <SPAN class="number token">15.79</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">11.31</SPAN> <SPAN class="number token">8.53</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">8.53</SPAN> <SPAN class="number token">11.31</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">13.9</SPAN> <SPAN class="number token">8.53</SPAN> <SPAN class="number token">13.9</SPAN> <SPAN class="number token">15.79</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">11.31</SPAN> <SPAN class="number token">15.79</SPAN> <SPAN class="number token">15.79</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
Aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN> <SPAN class="number token">315</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">315</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">45</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">45</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">315</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">315</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">45</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">45</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">270</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">270</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">270</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">90</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">90</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">225</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">225</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">180</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">135</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN> <SPAN class="number token">225</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">225</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">180</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">135</SPAN><SPAN class="punctuation token">.</SPAN> <SPAN class="number token">135</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
| Slope calculations... | Aspect calculations |
|---|
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># ---- Slope calculation using</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># finite difference ----</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>Slope face<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">5</SPAN> <SPAN class="number token">5</SPAN> <SPAN class="number token">5</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
N dx deg<SPAN class="punctuation token">.</SPAN> <SPAN class="operator token">%</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">0.5</SPAN> <SPAN class="number token">71.6</SPAN> <SPAN class="number token">400.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">1.0</SPAN> <SPAN class="number token">56.3</SPAN> <SPAN class="number token">200.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">2.0</SPAN> <SPAN class="number token">36.9</SPAN> <SPAN class="number token">100.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">4.0</SPAN> <SPAN class="number token">20.6</SPAN> <SPAN class="number token">50.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">6.0</SPAN> <SPAN class="number token">14.0</SPAN> <SPAN class="number token">33.3</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">6</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">8.0</SPAN> <SPAN class="number token">10.6</SPAN> <SPAN class="number token">25.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">10.0</SPAN> <SPAN class="number token">8.5</SPAN> <SPAN class="number token">20.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">8</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">12.5</SPAN> <SPAN class="number token">6.8</SPAN> <SPAN class="number token">16.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">9</SPAN> <SPAN class="punctuation token">)</SPAN> <SPAN class="number token">15.0</SPAN> <SPAN class="number token">5.7</SPAN> <SPAN class="number token">13.3</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">20.0</SPAN> <SPAN class="number token">4.3</SPAN> <SPAN class="number token">10.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">25.0</SPAN> <SPAN class="number token">3.4</SPAN> <SPAN class="number token">8.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">12</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">40.0</SPAN> <SPAN class="number token">2.1</SPAN> <SPAN class="number token">5.0</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">13</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">80.0</SPAN> <SPAN class="number token">1.1</SPAN> <SPAN class="number token">2.5</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">14</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="number token">100.0</SPAN> <SPAN class="number token">0.9</SPAN> <SPAN class="number token">2.0</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># ---- dx is the cell width ----</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># It is a north facing slope,</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># with dx=4 and dz=2, you get a</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># 20.6 deg. or 50% slope</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> | <SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># ---- Some windows with</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN> <SPAN class="comment token"># aspect determination ----</SPAN>
<SPAN class="operator token">>></SPAN><SPAN class="operator token">></SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">0.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">315.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">270.0</SPAN>
<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> snip <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">180.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">135.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN> <SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">6</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">90.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">3</SPAN> <SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="punctuation token">(</SPAN><SPAN class="number token">7</SPAN><SPAN class="punctuation token">)</SPAN> Array aspect<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="number token">45.0</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN> <SPAN class="number token">1</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> etc<SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN> etc <SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN> |
Now for some functions
The code below provides a check on the arrays then a few helper functions before the slope_a and aspect_a functions. A demo function is used to show the results for a 3xN window with know properties. I have used this easily on arrays in the 1000's in both x and y.
<SPAN class="keyword token">import</SPAN> sys
<SPAN class="keyword token">import</SPAN> numpy <SPAN class="keyword token">as</SPAN> np
<SPAN class="keyword token">from</SPAN> numpy<SPAN class="punctuation token">.</SPAN>lib<SPAN class="punctuation token">.</SPAN>stride_tricks <SPAN class="keyword token">import</SPAN> as_strided
<SPAN class="keyword token">from</SPAN> textwrap <SPAN class="keyword token">import</SPAN> dedent<SPAN class="punctuation token">,</SPAN> indent
<SPAN class="keyword token">import</SPAN> matplotlib<SPAN class="punctuation token">.</SPAN>pyplot <SPAN class="keyword token">as</SPAN> plt
ft <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">{</SPAN><SPAN class="string token">'bool'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">lambda</SPAN> x<SPAN class="punctuation token">:</SPAN> repr<SPAN class="punctuation token">(</SPAN>x<SPAN class="punctuation token">.</SPAN>astype<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'int32'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="string token">'float'</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="string token">'{: 0.1f}'</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">}</SPAN>
np<SPAN class="punctuation token">.</SPAN>set_printoptions<SPAN class="punctuation token">(</SPAN>edgeitems<SPAN class="operator token">=</SPAN><SPAN class="number token">10</SPAN><SPAN class="punctuation token">,</SPAN> linewidth<SPAN class="operator token">=</SPAN><SPAN class="number token">100</SPAN><SPAN class="punctuation token">,</SPAN> precision<SPAN class="operator token">=</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN>
suppress<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> threshold<SPAN class="operator token">=</SPAN><SPAN class="number token">200</SPAN><SPAN class="punctuation token">,</SPAN>
formatter<SPAN class="operator token">=</SPAN>ft<SPAN class="punctuation token">)</SPAN>
np<SPAN class="punctuation token">.</SPAN>ma<SPAN class="punctuation token">.</SPAN>masked_print_option<SPAN class="punctuation token">.</SPAN>set_display<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'-'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">_check</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="punctuation token">,</SPAN> subok<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Performs the array checks necessary for stride and block.
: see arr_tools
"""</SPAN>
<SPAN class="keyword token">if</SPAN> isinstance<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>int<SPAN class="punctuation token">,</SPAN> float<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
r_c <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> int<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
r<SPAN class="punctuation token">,</SPAN> c <SPAN class="operator token">=</SPAN> r_c
<SPAN class="keyword token">if</SPAN> a<SPAN class="punctuation token">.</SPAN>ndim <SPAN class="operator token">==</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>atleast_2d<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
r<SPAN class="punctuation token">,</SPAN> c <SPAN class="operator token">=</SPAN> r_c <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>min<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> min<SPAN class="punctuation token">(</SPAN>c<SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> copy<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> subok<SPAN class="operator token">=</SPAN>subok<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">,</SPAN> tuple<SPAN class="punctuation token">(</SPAN>r_c<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">stride</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Provide a 2D sliding/moving view of an array.
: see arr_tools
"""</SPAN>
a<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">,</SPAN> c<SPAN class="punctuation token">,</SPAN> r_c <SPAN class="operator token">=</SPAN> _check<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="punctuation token">)</SPAN>
shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> r <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> a<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> c <SPAN class="operator token">+</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> r_c
strides <SPAN class="operator token">=</SPAN> a<SPAN class="punctuation token">.</SPAN>strides <SPAN class="operator token">*</SPAN> <SPAN class="number token">2</SPAN>
a_s <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>as_strided<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> shape<SPAN class="operator token">=</SPAN>shape<SPAN class="punctuation token">,</SPAN> strides<SPAN class="operator token">=</SPAN>strides<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">#.squeeze()</SPAN>
<SPAN class="keyword token">return</SPAN> a_s
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">filter_a</SPAN><SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">,</SPAN> filter<SPAN class="operator token">=</SPAN>None<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Used by aspect, slope and hillshade to filter a raster/array
:Requires:
:--------
: a_s - a r*c*3x3 strided array
: filter - a 3x3 filter to apply to a_s
: cell_size - for slope (actual size*8, aspect (8 is required)
"""</SPAN>
f_dxyz <SPAN class="operator token">=</SPAN> filter
cell_size<SPAN class="operator token">=</SPAN> cell_size<SPAN class="operator token">*</SPAN><SPAN class="number token">8.0</SPAN>
<SPAN class="keyword token">if</SPAN> filter <SPAN class="keyword token">is</SPAN> None<SPAN class="punctuation token">:</SPAN>
f_dxyz <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">"float64"</SPAN><SPAN class="punctuation token">)</SPAN>
a_s <SPAN class="operator token">=</SPAN> a_s <SPAN class="operator token">*</SPAN> f_dxyz
dz_dx <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> a_s<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>sum<SPAN class="punctuation token">(</SPAN>axis<SPAN class="operator token">=</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN>cell_size
dz_dy <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">-</SPAN> a_s<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>sum<SPAN class="punctuation token">(</SPAN>axis<SPAN class="operator token">=</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="operator token">/</SPAN>cell_size
<SPAN class="keyword token">return</SPAN> dz_dx<SPAN class="punctuation token">,</SPAN> dz_dy
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">slope_a</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> degrees<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> verbose<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> keepdims<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Return slope in degrees for an input array using 3rd order
: finite difference method for a 3x3 moing window view into the array.
:
:Requires:
:--------
: a - an input 2d array. X and Y represent coordinates of the Z values
: cell_size - must be in the same units as X and Y
: degrees - True, returns degrees otherwise radians
: verbose - True, to print results
: keepdims - False, to remove/squeeze extra dimensions
: filter - np.array([[1, 2, 1], [2, 0, 2], [1, 2, 1]]) **current default
:
:Notes: dzdx: sum(col2 - col0)/8*cellsize
:----- dzdy: sum(row2 - row0)/8*celsize
: Assert the array is ndim=4 even if (1,z,y,x)
: general dzdx + dzdy = dxyz
: [[a, b, c], [[1, 0, 1], [[1, 2, 1] [[1, 2, 1]
: [d, e, f] [2, 0, 2], + [0, 0, 0] = [2, 0, 2],
: [g, h, i] [1, 0, 1]] [1, 2, 1]] [1, 2, 1]]
:
"""</SPAN>
frmt <SPAN class="operator token">=</SPAN> <SPAN class="string token">"""\n :----------------------------------------:
:{}
:input array...\n {}
:slope values...\n {!r:}
:----------------------------------------:
"""</SPAN>
<SPAN class="comment token"># ---- stride the data and calculate slope for 3x3 sliding windows</SPAN>
a_s <SPAN class="operator token">=</SPAN> stride<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> a_s<SPAN class="punctuation token">.</SPAN>ndim <SPAN class="operator token"><</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">:</SPAN>
new_shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="operator token">-</SPAN>len<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape
a_s <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>reshape<SPAN class="punctuation token">(</SPAN>new_shape<SPAN class="punctuation token">)</SPAN>
r <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
c <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
f_dxyz <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">"float64"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># ---- default filter, apply the filter to the array ----</SPAN>
<SPAN class="comment token">#</SPAN>
dz_dx<SPAN class="punctuation token">,</SPAN> dz_dy <SPAN class="operator token">=</SPAN> filter_a<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">,</SPAN> filter<SPAN class="operator token">=</SPAN>f_dxyz<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN>cell_size<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#</SPAN>
s <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sqrt<SPAN class="punctuation token">(</SPAN>dz_dx<SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">+</SPAN> dz_dy<SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> degrees<SPAN class="punctuation token">:</SPAN>
s <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>rad2deg<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>arctan<SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> keepdims<SPAN class="punctuation token">:</SPAN>
s <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>squeeze<SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> verbose<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">from</SPAN> textwrap <SPAN class="keyword token">import</SPAN> indent<SPAN class="punctuation token">,</SPAN> dedent <SPAN class="comment token"># if not imported</SPAN>
p <SPAN class="operator token">=</SPAN> <SPAN class="string token">" "</SPAN>
args <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Results for slope_a... "</SPAN><SPAN class="punctuation token">,</SPAN>
indent<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> indent<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>s<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>dedent<SPAN class="punctuation token">(</SPAN>frmt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> s
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">aspect_a</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> flat<SPAN class="operator token">=</SPAN><SPAN class="number token">0.1</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Return the aspect of a slope in degrees from North.
:Requires:
:--------
: a - an input 2d array. X and Y represent coordinates of the Z values
: flat - degree value, e.g. flat surface <= 0.05 deg
: 0.05 deg => 8.7e-04 0.10 deg => 1.7e-02
:
"""</SPAN>
<SPAN class="keyword token">if</SPAN> <SPAN class="operator token">not</SPAN> isinstance<SPAN class="punctuation token">(</SPAN>flat<SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">(</SPAN>int<SPAN class="punctuation token">,</SPAN> float<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
flat <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.1</SPAN>
a_s <SPAN class="operator token">=</SPAN> stride<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> r_c<SPAN class="operator token">=</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="number token">3</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> a_s<SPAN class="punctuation token">.</SPAN>ndim <SPAN class="operator token"><</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">:</SPAN>
new_shape <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">4</SPAN><SPAN class="operator token">-</SPAN>len<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape
a_s <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>reshape<SPAN class="punctuation token">(</SPAN>new_shape<SPAN class="punctuation token">)</SPAN>
r <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN>
c <SPAN class="operator token">=</SPAN> a_s<SPAN class="punctuation token">.</SPAN>shape<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN>
f_dxyz <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">"float64"</SPAN><SPAN class="punctuation token">)</SPAN>
a_s <SPAN class="operator token">=</SPAN> a_s <SPAN class="operator token">*</SPAN> f_dxyz
<SPAN class="comment token"># filter the array, using default filter f_dxyz above</SPAN>
<SPAN class="comment token">#</SPAN>
dz_dx<SPAN class="punctuation token">,</SPAN> dz_dy <SPAN class="operator token">=</SPAN> filter_a<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">,</SPAN> filter<SPAN class="operator token">=</SPAN>f_dxyz<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token">#</SPAN>
asp <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>arctan2<SPAN class="punctuation token">(</SPAN>dz_dy<SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN>dz_dx<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># relative to East</SPAN>
s <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>sqrt<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>dz_dx<SPAN class="operator token">*</SPAN>cell_size<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="punctuation token">(</SPAN>dz_dy<SPAN class="operator token">*</SPAN>cell_size<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">**</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># get the slope</SPAN>
asp <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>rad2deg<SPAN class="punctuation token">(</SPAN>asp<SPAN class="punctuation token">)</SPAN>
asp <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>mod<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="number token">450.0</SPAN> <SPAN class="operator token">-</SPAN> asp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">360</SPAN><SPAN class="punctuation token">.</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># simplest way to get azimuth</SPAN>
asp <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>where<SPAN class="punctuation token">(</SPAN>s <SPAN class="operator token"><=</SPAN> flat<SPAN class="punctuation token">,</SPAN> <SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> asp<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> asp
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">_demo</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Demonstration of calculations
:
"""</SPAN>
frmt <SPAN class="operator token">=</SPAN> <SPAN class="string token">"""
:------------------------------------------------------------------
:{}
:input array
{}
:slope values
{}
:zero?
{}
:aspect values
{}
:
:------------------------------------------------------------------
"""</SPAN>
p <SPAN class="operator token">=</SPAN> <SPAN class="string token">" "</SPAN>
t <SPAN class="operator token">=</SPAN> <SPAN class="number token">1.0e-12</SPAN>
d <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN>
<SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> t<SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">4</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">2</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">]</SPAN>
a <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>array<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">,</SPAN> dtype<SPAN class="operator token">=</SPAN><SPAN class="string token">'float64'</SPAN><SPAN class="punctuation token">)</SPAN>
sl <SPAN class="operator token">=</SPAN> slope_a<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">5</SPAN><SPAN class="punctuation token">,</SPAN> degrees<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">,</SPAN> verbose<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">,</SPAN> keepdims<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
sboo <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>where<SPAN class="punctuation token">(</SPAN>sl<SPAN class="operator token">==</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" T"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">" F"</SPAN><SPAN class="punctuation token">)</SPAN>
asp <SPAN class="operator token">=</SPAN> aspect_a<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
z <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>ma<SPAN class="punctuation token">.</SPAN>asarray<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">)</SPAN>
z<SPAN class="punctuation token">.</SPAN>mask <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>z<SPAN class="operator token">==</SPAN>t<SPAN class="punctuation token">)</SPAN>
args <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="string token">"Surface properties"</SPAN><SPAN class="punctuation token">,</SPAN>
z<SPAN class="punctuation token">,</SPAN>
indent<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>sl<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
indent<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>sboo<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN>
indent<SPAN class="punctuation token">(</SPAN>str<SPAN class="punctuation token">(</SPAN>asp<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> p<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">for</SPAN> i <SPAN class="keyword token">in</SPAN> d<SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>i<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>dedent<SPAN class="punctuation token">(</SPAN>frmt<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN><SPAN class="operator token">*</SPAN>args<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> a<SPAN class="punctuation token">,</SPAN> sl<SPAN class="punctuation token">,</SPAN> asp<SPAN class="punctuation token">,</SPAN> d
<SPAN class="comment token"># ---------------------------------------------------------------------</SPAN>
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">"__main__"</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Main section... """</SPAN>
<SPAN class="comment token">#print("Script... {}".format(script))</SPAN>
a<SPAN class="punctuation token">,</SPAN> sl<SPAN class="punctuation token">,</SPAN> asp<SPAN class="punctuation token">,</SPAN> d <SPAN class="operator token">=</SPAN> _demo<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>That's all for now.
Once slope and aspect are obtained, the hillshade can be derived. The code is fairly self-explanatory
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">hillshade_a</SPAN><SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">,</SPAN> sun_azim<SPAN class="operator token">=</SPAN><SPAN class="number token">315</SPAN><SPAN class="punctuation token">,</SPAN> sun_elev<SPAN class="operator token">=</SPAN><SPAN class="number token">45</SPAN><SPAN class="punctuation token">,</SPAN> degrees<SPAN class="operator token">=</SPAN><SPAN class="token boolean">True</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="string token">"""Hillshade calculation as outlined in Burrough and implemented by
: esri in ArcMap and ArcGIS Pro. All measures in radians.
: z, az => sun's zenith angle an azimuth
: sl, asp => surface properties
: 255.0 * ((cos(z) * cos(sl)) + (sin(z) * sin(sl) * cos(az-asp)))
:
"""</SPAN>
s_azi <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>deg2rad<SPAN class="punctuation token">(</SPAN>sun_azim<SPAN class="punctuation token">)</SPAN>
s_elev <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>deg2rad<SPAN class="punctuation token">(</SPAN><SPAN class="number token">90.0</SPAN> <SPAN class="operator token">-</SPAN> sun_elev<SPAN class="punctuation token">)</SPAN>
a_a <SPAN class="operator token">=</SPAN> aspect_a<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> degrees<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
a_s <SPAN class="operator token">=</SPAN> slope_a<SPAN class="punctuation token">(</SPAN>a<SPAN class="punctuation token">,</SPAN> cell_size<SPAN class="operator token">=</SPAN>cell_size<SPAN class="punctuation token">,</SPAN> degrees<SPAN class="operator token">=</SPAN><SPAN class="token boolean">False</SPAN><SPAN class="punctuation token">)</SPAN>
out <SPAN class="operator token">=</SPAN> <SPAN class="number token">255</SPAN><SPAN class="operator token">*</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>s_elev<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> np<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">+</SPAN>
<SPAN class="punctuation token">(</SPAN>np<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>s_elev<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> np<SPAN class="punctuation token">.</SPAN>sin<SPAN class="punctuation token">(</SPAN>a_s<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> np<SPAN class="punctuation token">.</SPAN>cos<SPAN class="punctuation token">(</SPAN>s_azi <SPAN class="operator token">-</SPAN> a_a<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
out <SPAN class="operator token">=</SPAN> np<SPAN class="punctuation token">.</SPAN>where<SPAN class="punctuation token">(</SPAN>out <SPAN class="operator token"><</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> out<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> out<SPAN class="punctuation token">.</SPAN>astype<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'int'</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
Other terrain derivates will be added soon.