Xander Bakker
Python script to calculate NDVI anomalies
https://geonet.esri.com/people/xander_bakker
I´d like to automate all my workflow, from downloaded original raster datasets to anomaly calculator:
3 years data and folder structure can be downloaded here.
Question 1: What is the best option: to run separated scripts or create a unique code?
Question 2: How to translate the model builder to python script (Python script 2)?
Question 3: If the best option is to run separated, how to make it in a logical sequence (script 1 -> script 2 -> script 3)
Summary workflow:
- Select and download NDVI filtered raster datasets;
- Store downloaded original raster datasets at X:\Modis250\Originais\
- Organizing files and folders: (Python script 1)
- Rename original raster datasets (MODIS.ndvi.YYYYjjj.yL6000.BOKU.tif) to filename standard “ndvi_YYYY_dec_dd.tif”, where “YYYY” is the year, “jjj” is the Julian day and “dd” is the decade (ten days period).
- Transfer renamed files from X:\Modis250\Originais to X:\Modis250\NDVI10dias
- Pre-processing raster datasets: (Python script 2)
- Project renamed raster datasets ("ndvi_YYYY_dec_dd.tif") from Lat/Lon WGS 1984 to Lat/Lon SIRGAS2000 using "SAD_1969_To_WGS_1984_14 + SAD_1969_To_SIRGAS_2000_1" geographic transformation.
- Overwrite spurious data values: NDVI = Con("%NDVI%"<1,0,Con("%NDVI%">9998,9999,"%NDVI%"))
- Standardize raster datasets: NDVI = (NDVI – Mean(NDVI))/Std(NDVI)
- Scale raster datasets to 0 – 100: NDVI = ((NDVI-Min(NDVI))/(Max(NDVI)-Min(NDVI)) * 100
- Mask standardized raster datasets by the area of interest (X:\Modis250\SRTM\mde_sc_90.tif) and output to X:\Modis250\NDVI_10dias_norm
- Processing data (Python script 3)
- Calculate NDVI mean for each decadal: mean_dec_dd.tif = MEAN (ndvi_YYY1_dec_dd.tif, ndvi_YYY2_dec_dd.tif, ndvi_YYY3_dec_dd.tif,…)
- Calculate NDVI anomaly for each year by decade: anom_yyyy_dec_dd.tif = ndvi_YYYY_dec_dd.tif - mean_dec_dd.tif
Python scripts 1 and 3 are working. The scrip 2 (Pre-processing) is a model in a toolbox.
Python script 1: written by myself, maybe need to be improved!
<SPAN class="comment token">#Rename original files</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">,</SPAN> datetime<SPAN class="punctuation token">,</SPAN> glob<SPAN class="punctuation token">,</SPAN> os
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace<SPAN class="operator token">=</SPAN>r<SPAN class="string token">'O:\Modis250\Originais'</SPAN>
infolder<SPAN class="operator token">=</SPAN><SPAN class="string token">'O:\Modis250\Originals'</SPAN>
outfolder<SPAN class="operator token">=</SPAN><SPAN class="string token">'O:\Modis250\NDVI_10dias'</SPAN>
list1<SPAN class="operator token">=</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"*.tif"</SPAN><SPAN class="punctuation token">)</SPAN>
list1<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> raster <SPAN class="keyword token">in</SPAN> list1<SPAN class="punctuation token">:</SPAN>
source_path <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>infolder<SPAN class="punctuation token">,</SPAN> raster<SPAN class="punctuation token">)</SPAN>
oldFilename<SPAN class="operator token">=</SPAN>raster
dd <SPAN class="operator token">=</SPAN> int<SPAN class="punctuation token">(</SPAN>oldFilename<SPAN class="punctuation token">[</SPAN><SPAN class="number token">15</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">18</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">if</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">11</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">1</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">21</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">2</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">31</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">3</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">41</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">4</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">51</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">5</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">61</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">6</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">71</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">7</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">81</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">8</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">91</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">9</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">101</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">10</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">111</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">11</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">121</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">12</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">131</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">13</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">141</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">14</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">151</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">15</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">161</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">16</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">171</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">17</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">181</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">18</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">191</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">19</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">201</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">20</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">211</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">21</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">221</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">22</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">231</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">23</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">241</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">24</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">251</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">25</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">261</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">26</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">271</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">27</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">281</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">28</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">291</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">29</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">301</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">30</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">311</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">31</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">321</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">32</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">331</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">33</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">341</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">34</SPAN>
<SPAN class="keyword token">elif</SPAN> dd <SPAN class="operator token"><</SPAN> <SPAN class="number token">351</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">35</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN> dd <SPAN class="operator token">=</SPAN> <SPAN class="number token">36</SPAN>
dd <SPAN class="operator token">=</SPAN> str<SPAN class="punctuation token">(</SPAN>dd<SPAN class="punctuation token">)</SPAN>
newFilename<SPAN class="operator token">=</SPAN><SPAN class="string token">"ndvi_"</SPAN> <SPAN class="operator token">+</SPAN> oldFilename<SPAN class="punctuation token">[</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="number token">15</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="operator token">+</SPAN> <SPAN class="string token">"_dec_"</SPAN> <SPAN class="operator token">+</SPAN> dd <SPAN class="operator token">+</SPAN> <SPAN class="string token">".tif"</SPAN>
destination_path<SPAN class="operator token">=</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>outfolder<SPAN class="punctuation token">,</SPAN> newFilename<SPAN class="punctuation token">)</SPAN>
os<SPAN class="punctuation token">.</SPAN>rename<SPAN class="punctuation token">(</SPAN>source_path<SPAN class="punctuation token">,</SPAN> destination_path<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>
Model Builder -> Python script 2

Python script 3: Python script to calculate NDVI anomalies
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">main</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> os
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># Check out SA license</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># settings</SPAN>
ws_norm <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\Modis250\NDVI_10dias_norm'</SPAN>
ws_mean <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\Modis250\NDVI_10dias_mean'</SPAN>
ws_anom <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'D:\Modis250\NDVI_10dias_anom'</SPAN>
<SPAN class="comment token"># create a list of all raster in norm folder</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> ws_norm
lst_ndvi_ras <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ListRasters<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
lst_ndvi_ras<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lst_ndvi_ras: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>lst_ndvi_ras<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># get list of years</SPAN>
decades <SPAN class="operator token">=</SPAN> GetListOfDecades<SPAN class="punctuation token">(</SPAN>lst_ndvi_ras<SPAN class="punctuation token">)</SPAN>
decades<SPAN class="punctuation token">.</SPAN>sort<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"decades: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>decades<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># loop through each year</SPAN>
<SPAN class="keyword token">for</SPAN> decade <SPAN class="keyword token">in</SPAN> decades<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># Get rasters for given year</SPAN>
lst_ndvi_decade <SPAN class="operator token">=</SPAN> GetListOfRasterForGivenDecade<SPAN class="punctuation token">(</SPAN>lst_ndvi_ras<SPAN class="punctuation token">,</SPAN> decade<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lst_ndvi_decade: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>lst_ndvi_decade<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># calculate mean raster for decade</SPAN>
lst_ndvi_decade_paths <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN>os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws_norm<SPAN class="punctuation token">,</SPAN> r<SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> lst_ndvi_decade<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">"lst_ndvi_decade_paths: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>lst_ndvi_decade_paths<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
mean_ras <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa<SPAN class="punctuation token">.</SPAN>CellStatistics<SPAN class="punctuation token">(</SPAN>lst_ndvi_decade_paths<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"MEAN"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"DATA"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># store mean ras for decade</SPAN>
<SPAN class="comment token"># mean_dec_d_.tif</SPAN>
out_name_mean <SPAN class="operator token">=</SPAN> <SPAN class="string token">"mean_dec_{}.tif"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>decade<SPAN class="punctuation token">)</SPAN> <SPAN class="comment token">###</SPAN>
out_name_path_mean <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws_mean<SPAN class="punctuation token">,</SPAN> out_name_mean<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"out_name_path_mean: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>out_name_path_mean<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
mean_ras<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>out_name_path_mean<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># loop through each raster in decade</SPAN>
<SPAN class="keyword token">for</SPAN> ndvi_ras <SPAN class="keyword token">in</SPAN> lst_ndvi_decade<SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># calculate anom for each raster in decade</SPAN>
<SPAN class="comment token"># anom_2002_dec_1.tif = norm_ndvi_2002_dec_1.tif - mean_dec_1_.tif;</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"ndvi_ras: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>ndvi_ras<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># minus_ras = arcpy.sa.Minus(ndvi_ras, out_name_path_mean)</SPAN>
out_name_path_norm <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws_norm<SPAN class="punctuation token">,</SPAN> ndvi_ras<SPAN class="punctuation token">)</SPAN>
minus_ras <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>out_name_path_mean<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">-</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>out_name_path_norm<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># store anom raster</SPAN>
out_name_anom <SPAN class="operator token">=</SPAN> ndvi_ras<SPAN class="punctuation token">.</SPAN>replace<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"norm_ndvi"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"anom"</SPAN><SPAN class="punctuation token">)</SPAN>
out_name_path_anom <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>ws_anom<SPAN class="punctuation token">,</SPAN> out_name_anom<SPAN class="punctuation token">)</SPAN>
minus_ras<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>out_name_path_anom<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">print</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="string token">"out_name_path_anom: {}"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>out_name_path_anom<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># return SA license</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckInExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"spatial"</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetListOfDecades</SPAN><SPAN class="punctuation token">(</SPAN>lst<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># norm_ndvi_yyyy_dec_d.tif</SPAN>
lst1 <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>set<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">[</SPAN>r<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string token">"_"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">4</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> lst<SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> <SPAN class="punctuation token">[</SPAN>r<SPAN class="punctuation token">.</SPAN>split<SPAN class="punctuation token">(</SPAN><SPAN class="string 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="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> lst1<SPAN class="punctuation token">]</SPAN>
<SPAN class="keyword token">def</SPAN> <SPAN class="token function">GetListOfRasterForGivenDecade</SPAN><SPAN class="punctuation token">(</SPAN>lst<SPAN class="punctuation token">,</SPAN> decade<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
lst1 <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">]</SPAN>
search <SPAN class="operator token">=</SPAN> <SPAN class="string token">"_{}.tif"</SPAN><SPAN class="punctuation token">.</SPAN>format<SPAN class="punctuation token">(</SPAN>decade<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">for</SPAN> r <SPAN class="keyword token">in</SPAN> lst<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> search <SPAN class="keyword token">in</SPAN> r<SPAN class="punctuation token">.</SPAN>lower<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
lst1<SPAN class="punctuation token">.</SPAN>append<SPAN class="punctuation token">(</SPAN>r<SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">return</SPAN> lst1
<SPAN class="keyword token">if</SPAN> __name__ <SPAN class="operator token">==</SPAN> <SPAN class="string token">'__main__'</SPAN><SPAN class="punctuation token">:</SPAN>
main<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>