I need to run 252 equations to calculate freeze probability using lat, lon, and altitude as raster inputs, multiplied by a, b, c and d regression coeficients. The equations are in format a+b*lat+c*lon+d*alt.
The equations are in an Excel spreedsheet (attached) and I need to write them in a python script. The lat, lon and lat raster datasets are in the same folder and the output tif files have a standardized name (also in the Excel file).
Thank you!
Solved! Go to Solution.
eq is a list currently, since you are calculating in a list comprehension
eq[0] …. would be the first entry in the list (maybe your answer? Why not throw a print statement in before the save to see what it is returning before you try to save it
All I need is:
eq[0] -> save as filenames[0]
eq[1] -> save as filenames[1]
eq[2] -> save as filenames[2]
....
Ah, sorry, didn't notice you had abandoned the numpy array approach.
Just replace the last lines with:
Thank you all!
The final code is:
import numpy as np
import arcpy
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True
wksp = "D:\Vianna\Dados\Raster\Clima\ETP"
arcpy.env.workspace = wksp
lat = arcpy.Raster("D:\Vianna\Dados\Raster\Topografia\latit.tif")
lon = arcpy.Raster("D:\Vianna\Dados\Raster\Topografia\longit.tif")
alt = arcpy.Raster("D:\Vianna\Dados\Raster\Topografia\mde_sc_90.tif")
tbl = "D:\Vianna\Dados\Raster\Clima\Raster_Clima.gdb\TB_EQUAC_ETP"
arr = arcpy.da.TableToNumPyArray(tbl, "*")
eqs = arr['DS_EQUAC']
filenames = ["{}{}{}{}".format(*i) for i in arr[['SG_PREFIX', 'SG_SUFIX', 'SG_PERIO', 'SG_FORMA']]]
a = arr['VL_A']
b = arr['VL_B']
c = arr['VL_C']
d = arr['VL_D']
for i in range(len(filenames)):
print(filenames[i])
eq = a[i] + b[i] * lat + c[i] * lon + d[i] * alt
eq.save(filenames[i])
Still several things going on here:
- Seems you have forgot to cast the numpy array to raster before saving:
- The list comprehension on line 29 runs inside the save loop. If you want to calculate everything into the big list of arrays you should do it at line 21.
- There are no georeferencing of the results.
Sorry, I think I´m not clear about my issue. My english in not good. I want to run "n" regression equations using lat, lon, and alt as input (a+lat*b + lon*c+alt*d). lat, lon and alt are rasters (tif), georreferenced and with the same pixel size. a, b, c and d are the coeficients of the regression equations. The coefficients are in a table were each line will generate an raster output. Each output have lat, lon and alt as inputs and each field (VL_A, VL_B, VL_C and VL_D) have the coefficients. The inputs lat, lon and alt doesn't change, only the coefficients changes. I need to loop the inputs trhough the lines just to change the coefficients. For each line of the table, I will have an output.
I don't need to create lat and lon arrays because they are rasters:
Lat
Lon
Alt