0

What I did was an array with Rasters from 2000 to 2018. It's temporal Resolution is 16 days. I more or less wrote my code by combining scripts that I found matching my problem. This is what I got so far:

import arcpy
import numpy
from scipy import stats

arcpy.env.workspace = "C:/Users/..."
rasListe = arcpy.ListRasters()
for ras in rasListe:
data_array = arcpy.RasterToNumPyArray(ras, nodata_to_value=0)

time_array = (2000097, 2000113, 2000129, 2000145, 2000161, 2000177, 2000193, 2000209, 2000225, 2000241, 2000257, 2000273, 2001097, 2001113, 2001129, 2001145, 2001161, 2001177, 2001193, 2001209, 2001225, 2001241, 2001257, 2001273, 2002097, 2002113, 2002129, 2002145, 2002161, 2002177, 2002193, 2002209, 2002225, 2002241, 2002257, 2002273, 2003097, 2003113, 2003129, 2003145, 2003161, 2003177, 2003193, 2003209, 2003225, 2003241, 2003257, 2003273, 2004097, 2004113, 2004129, 2004145, 2004161, 2004177, 2004193, 2004209, 2004225, 2004241, 2004257, 2004273, 2005097, 2005113, 2005129, 2005145, 2005161, 2005177, 2005193, 2005209, 2005225, 2005241, 2005257, 2005273, 2006097, 2006113, 2006129, 2006145, 2006161, 2006177, 2006193, 2006209, 2006225, 2006241, 2006257, 2006273, 2007097, 2007113, 2007129, 2007145, 2007161, 2007177, 2007193, 2007209, 2007225, 2007241, 2007257, 2007273, 2008097, 2008113, 2008129, 2008145, 2008161, 2008177, 2008193, 2008209, 2008225, 2008241, 2008257, 2008273, 2009097, 2009113, 2009129, 2009145, 2009161, 2009177, 2009193, 2009209, 2009225, 2009241, 2009257, 2009273, 2010097, 2010113, 2010129, 2010145, 2010161, 2010177, 2010193, 2010209, 2010225, 2010241, 2010257, 2010273, 2011097, 2011113, 2011129, 2011145, 2011161, 2011177, 2011193, 2011209, 2011225, 2011241, 2011257, 2011273, 2012097, 2012113, 2012129, 2012145, 2012161, 2012177, 2012193, 2012209, 2012225, 2012241, 2012257, 2012273, 2013097, 2013113, 2013129, 2013145, 2013161, 2013177, 2013193, 2013209, 2013225, 2013241, 2013257, 2013273, 2014097, 2014113, 2014129, 2014145, 2014161, 2014177, 2014193, 2014209, 2014225, 2014241, 2014257, 2014273, 2015097, 2015113, 2015129, 2015145, 2015161, 2015177, 2015193, 2015209, 2015225, 2015241, 2015257, 2015273, 2016097, 2016113, 2016129, 2016145, 2016161, 2016177, 2016193, 2016209, 2016225, 2016241, 2016257, 2016273, 2017097, 2017113, 2017129, 2017145, 2017161, 2017177, 2017193, 2017209, 2017225, 2017241, 2017257, 2017273, 2018097, 2018113, 2018129, 2018145, 2018161, 2018177, 2018193, 2018209, 2018225, 2018241, 2018257, 2018273)

slope, intercept, r_value, p_value, std_err = stats.linregress(time_array, data_array) 

Basically the time_array picks every datetime of the Dataset but somehow the arrays don't line up.

ValueError: all the input array dimensions except for the concatenation axis must match exactly

Does somebody know what I did wrong?

EDIT: The length of my data_array is

np.shape(data_array)
(3799, 4656)

so if I want to run the code I get this Error somehow twice: ValueError: Shape of passed values is (4656, 3799), indices imply (4656, 228)

Does it mean that I first have to get it to the shape (4656, 3799, 228) to match with my time_array which is?

np.shape(time_array)
(228,)

This is the code I'm workin with so far:

import arcpy
import numpy as np
from scipy import stats
from scipy.stats import linregress

arcpy.env.workspace = "C:/Users/..."
rasListe = arcpy.ListRasters()
for ras in rasListe:
    data_array = np.array(arcpy.RasterToNumPyArray(ras))

time_array = np.array([2000097, ..., 2018273])

def lin_regress(col):
    "Mask nulls and apply stats.linregress"
    col = col.loc[~pd.isnull(col)]
    return linregress(col.index.tolist(), col)

# Build the DataFrame (each index represents a pixel)
df = pd.DataFrame(data_array.reshape(len(data_array), -1), index=time_array.tolist())

# Apply a our custom linregress wrapper to each function, split the tuple into separate columns
final_df = df.apply(lin_regress).apply(pd.Series)

# Name the index and columns to make this easier to read
final_df.columns, final_df.index.name = 'slope, intercept, r_value, p_value, std_err'.split(', '), 'pixel_number'
  • are data_array and time_array the same shape? – Nathan Thomas Feb 7 at 15:54
  • That would be my question. The number of Rasters equals the length of the time_array. But each raster is an array as I understood. – Sandro Golia Feb 7 at 16:25
  • print(data_array) [[0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] ... [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0] [0 0 0 ... 0 0 0]] – Sandro Golia Feb 7 at 16:48
  • Is data_array one raster (one array) or multiple rasters (an array of arrays)? If is is multiple rasters, time_array is 1 dimensional when data_array is 2 dimensional so their shapes are not the same – Nathan Thomas Feb 7 at 18:49
  • This might help: stackoverflow.com/questions/52108417/… – Nathan Thomas Feb 7 at 18:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.