<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: stack 2D array using numpy dstack memory error in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664226#M51599</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You haven't defined the nrows or ncols variables (number of rows, number of columns).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/rastertonumpyarray-function.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/rastertonumpyarray-function.htm"&gt;RasterToNumPyArray&lt;/A&gt; function accepts lower_left_corner, ncols, nrows parameters which you can use to extract a subset of each raster.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 24 Sep 2015 01:48:36 GMT</pubDate>
    <dc:creator>Luke_Pinner</dc:creator>
    <dc:date>2015-09-24T01:48:36Z</dc:date>
    <item>
      <title>stack 2D array using numpy dstack memory error</title>
      <link>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664223#M51596</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;My intention is to stack my 2D array created from my MODIS rasters using &lt;EM&gt;&lt;STRONG&gt;numpy dstack&lt;/STRONG&gt;&lt;/EM&gt;.&amp;nbsp; After that, I hope to implement savitzky-golay filtering algorithm from &lt;EM&gt;&lt;STRONG&gt;scipy signal processing&lt;/STRONG&gt;&lt;/EM&gt; so I think it is better to stack my 2D array to access the time series values of a particular pixel. Right now, I am just testing for 10 MODIS rasters (&lt;EM&gt;but later on will use my whole dataset -- approx 800 rasters (15 years of data)&lt;/EM&gt;) only to test how should I create my script. So my initial workflow is to: &lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;1) Access my raster files&lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;2) For each file I need to convert it to a 2D array using &lt;EM&gt;&lt;STRONG&gt;RasterToNumPyArray&lt;/STRONG&gt; &lt;/EM&gt;&lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;3) Append all those created 2D arrays to a list, and &lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;4) Implement &lt;EM&gt;&lt;STRONG&gt;numpy.dstack()&lt;/STRONG&gt;&lt;/EM&gt; to stack my 2D arrays and hopefully I can begin some analysis.&lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;I am getting an error in the 4th step (it says memory error) -- converting my list of 2D arrays to a stack (please see script 1 below) but if I revise my script and load in dstack using the index of each element in my 2D array it runs okay. Could you guide how to approach my problem. I am just building my script so for now its not so much. please see below. Thanks.&lt;/P&gt;&lt;P style="margin-bottom: 1em; font-size: 15px; color: #222222; font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; background-color: rgba(248, 248, 248, 0.6);"&gt;&lt;STRONG&gt;Script 1&lt;/STRONG&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy.sa import *
import os, sys
import numpy as np
from scipy.signal import savgol_filter

arcpy.CheckOutExtension("Spatial")

rasters = []

ws = 'G:/Test/Raster'
for folder, subs, files in os.walk(ws):
&amp;nbsp; for filename in files:
&amp;nbsp; aSrc = arcpy.RasterToNumPyArray(os.path.join(folder,filename))
&amp;nbsp; rasters.append(aSrc)

stackRast = np.dstack(rasters)&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Script 2&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
from arcpy.sa import *
import os, sys
import numpy as np
from scipy.signal import savgol_filter

arcpy.CheckOutExtension("Spatial")

rasters = []

ws = 'G:/Test/Raster'
for folder, subs, files in os.walk(ws):
&amp;nbsp; for filename in files:
&amp;nbsp; aSrc = arcpy.RasterToNumPyArray(os.path.join(folder,filename))
&amp;nbsp; rasters.append(aSrc)

stack = np.dstack((rasters[0], rasters[1], rasters[2], rasters[3], rasters[4], rasters[5],rasters[6], rasters[7], rasters[8], rasters[9]))&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:04:53 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664223#M51596</guid>
      <dc:creator>Leo_KrisPalao</dc:creator>
      <dc:date>2021-12-12T04:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: stack 2D array using numpy dstack memory error</title>
      <link>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664224#M51597</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;How much RAM have you got?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Make sure you're using 64bit python, 32bit can't make use of much of your RAM.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Prebuild the stack then populate it rather than using a list so less memory is required (due to the way numpy makes copies of arrays) i.e&lt;/P&gt;&lt;P&gt;stack = numpy.zeroes((nrasters, nrows, ncols), dtype = numpy.some_appropriate_dtype)&lt;/P&gt;&lt;P&gt;i=0&lt;/P&gt;&lt;P&gt;&lt;SPAN class="keyword"&gt;for folder, subs, files &lt;SPAN class="keyword"&gt;in&lt;/SPAN&gt; os.walk(ws):&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="keyword"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for filename &lt;SPAN class="keyword"&gt;in&lt;/SPAN&gt; files:&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stack[i, nrows, ncols] = arcpy.RasterToNumPyArray(os.path.join(folder,filename))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Even with 64bit and loads of RAM, you'll probably still have issues with memory, you may need to process the timeseries in chunks, i.e the full time series for a portion of the&amp;nbsp; image.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Sep 2015 03:38:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664224#M51597</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2015-09-23T03:38:49Z</dc:date>
    </item>
    <item>
      <title>Re: stack 2D array using numpy dstack memory error</title>
      <link>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664225#M51598</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Luke,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Currently I am using 12GB RAM but my Python is only 32bit. The reason why is that I have a problem before installing 64-bit GDAL for python.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I tried your suggestion but I get errors in nrows saying not defined. Did I get the syntax/expression right?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;import arcpy&lt;/P&gt;&lt;P&gt;from arcpy.sa import *&lt;/P&gt;&lt;P&gt;import os, sys&lt;/P&gt;&lt;P&gt;import numpy as np&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;arcpy.CheckOutExtension("Spatial")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;stack = np.zeros((2,4800,4800),dtype=np.int)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;i=0&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;ws = 'G:/Test/Raster'&lt;/P&gt;&lt;P&gt;for folder, subs, files in os.walk(ws):&lt;/P&gt;&lt;P&gt;&amp;nbsp; for fname in files:&lt;/P&gt;&lt;P&gt;&amp;nbsp; stack[i,nrows,ncols] = arcpy.RasterToNumPyArray(os.path.join(folder,fname))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By the way Luke, dividing the time series by tile is a good way. I have seen some of the script in R they do such technique for signal processing. Can you guide me how to approach this tiling method in Python?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;-Leo&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Sep 2015 01:32:49 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664225#M51598</guid>
      <dc:creator>Leo_KrisPalao</dc:creator>
      <dc:date>2015-09-24T01:32:49Z</dc:date>
    </item>
    <item>
      <title>Re: stack 2D array using numpy dstack memory error</title>
      <link>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664226#M51599</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You haven't defined the nrows or ncols variables (number of rows, number of columns).&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The &lt;A href="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/rastertonumpyarray-function.htm" title="http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-functions/rastertonumpyarray-function.htm"&gt;RasterToNumPyArray&lt;/A&gt; function accepts lower_left_corner, ncols, nrows parameters which you can use to extract a subset of each raster.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Sep 2015 01:48:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/stack-2d-array-using-numpy-dstack-memory-error/m-p/664226#M51599</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2015-09-24T01:48:36Z</dc:date>
    </item>
  </channel>
</rss>

