POST
|
Thanks Dan, I appreciate this. I am looking into this. I'll feed everyone back on my progress. Thanks all
... View more
12-05-2017
05:42 AM
|
0
|
0
|
809
|
POST
|
Thanks for looking into it. Actually I found a possible way to do this, just that I technically dont know how to write the code. My brother has written something for me in C sharp but it is extremely slow because it is basically calling my python script and it is doing a lot of round tripping between the programs. It is in order of a day for a single set of files and I have 46 set of files. The solution is as follows perhaps someone can help write a few lines in python to do this; I converted the three input rasters to ASCII. Hence I have 4064 rows by 4876 columns. The program then calls each row and column at a time (i.e x,y), which essentially is a single value and computes fsolve for it. It then writes the answer for E, H and G from fsolve into 3 different .txt file for each x,y position. I imagine this is doable in python as well, unfortunately, my brother doesnt know code in python and the non linear solvers in C Sharp have been problematic hence my conundrum. IF anyone can help write something along the lines of what I explained, it will be much appreciated. import arcpy
import scipy
import numpy as np
def myFunction(z):
E = z[0]
G = z[1]
H = z[2]
F = np.empty((3))
F[0] = (3.356/1.024)*3*H*pow(abs(H),-(1/6))-G
F[1] = 300-E-H-G
F[2] = 3.356*H-E
return F
zGuess= np.array([1,1,1])
z = fsolve(myFunction,zGuess)
print z
... View more
12-04-2017
07:06 PM
|
0
|
1
|
809
|
POST
|
Hi Dan, Thanks for your suggestion. But I honestly am struggling to fully understand it as I am still a newbie to python. I have included some sample data in the link below. If you have sometime. I'll appreciate if you can put this suggestion in a script as I really have no clue on how to write it. Thanks https://drive.google.com/file/d/1dE32qKVm0V83d4pvmPUD0nWvDmrOaz3w/view?usp=sharing
... View more
12-04-2017
02:34 PM
|
0
|
0
|
809
|
POST
|
Hi Randy, Thanks for taking the shot. I tried this but it returned the same error. Pretty much not wanting to take in arrays or rasters. I have also included some sample raster data above.
... View more
12-04-2017
02:05 PM
|
0
|
0
|
1452
|
POST
|
Please this is still an on going challenge. If you have any other suggestions. It will be very much appreciated. The flattening of the arrays did not work.
... View more
12-04-2017
07:03 AM
|
0
|
2
|
1279
|
POST
|
I think I get your point. However I have no idea on how to possibly attempt to extract one variable at a time. Could you possibly write something that I can insert into the code to do the suggestion you made?
... View more
12-01-2017
01:22 AM
|
0
|
0
|
1279
|
POST
|
Xander BakkerDan Patterson think I am getting closer to concluding that the Fsolve function may not be capable of doing what I need it to do. I ran my code for a single set of rasters below and got the error below. It appears that just as Xander Bakker suggested in line 50 of his code, maybe this cant be done with numpy arrays. Any other ideas of how to get this solved? Runtime error Traceback (most recent call last): File "<string>", line 29, in <module> File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve res = _root_hybr(func, x0, args, jac=fprime, **options) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 197, in _root_hybr shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 20, in _check_func res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) File "<string>", line 23, in myFunction ValueError: setting an array element with a sequence. import os
import arcpy
import scipy
import numpy as np
def myFunction(z):
inRas1 = arcpy.Raster("C:\\Workspace\\test geowrite\\Betasigma\\Rad_meantiff_001.tif")
lowerleft = arcpy.Point(inRas1.extent.XMin,inRas1.extent.YMin)
cellSize = inRas1.meanCellWidth
np_file = arcpy.RasterToNumPyArray(inRas1,nodata_to_value=0)
inRas2 = arcpy.Raster("C:\\Workspace\\test geowrite\\sigma\\Rad_meantiff_001.tif")
lowerleft = arcpy.Point(inRas2.extent.XMin,inRas2.extent.YMin)
cellSize = inRas2.meanCellWidth
np_fize = arcpy.RasterToNumPyArray(inRas2,nodata_to_value=0)
inRas3 = arcpy.Raster("C:\\Workspace\\test geowrite\\Netrad\\Rad_meantiff_001.tif")
lowerleft = arcpy.Point(inRas3.extent.XMin,inRas3.extent.YMin)
cellSize = inRas3.meanCellWidth
np_fixe = arcpy.RasterToNumPyArray(inRas3,nodata_to_value=0)
E = z[0]
G = z[1]
H = z[2]
F=np.empty((3,))
F[0] = (np_file/np_fize)*3*H*pow(abs(H),-(1/6))-G
F[1] = np_fixe-E-H-G
F[2] = np_file*H-E
return F
zGuess= np.array([1,1,1])
z = fsolve(myFunction,zGuess)
... View more
11-30-2017
11:57 PM
|
0
|
2
|
1279
|
POST
|
I fixed that and ran the code again and got the same result.
... View more
11-30-2017
08:02 PM
|
0
|
0
|
1452
|
POST
|
I ran the code you wrote with corrections to the path as shown below and got the error Runtime error Traceback (most recent call last): File "<string>", line 56, in <module> File "<string>", line 36, in main File "<string>", line 50, in myFunction TypeError: only integer arrays with one element can be converted to an index import os
import arcpy
import scipy
import numpy as np
def main():
arcpy.env.workspace = r"D:\GIS Data\Sixth Creek ET.gdb"
path1 = r"C:\Workspace\test geowrite\Betasigma"
path2 = r"C:\Workspace\test geowrite\sigma"
path3 = r"C:\Workspace\test geowrite\Netrad"
zGuess= np.array([1,1,1])
rasters = arcpy.ListRasters("*", "All") # where do you use this?
lst_files = [f for f in os.listdir(path1) if f.endswith(".tif")]
lst_fizes = [f for f in os.listdir(path2) if f.endswith(".tif")]
lst_fixes = [f for f in os.listdir(path3) if f.endswith(".tif")]
for i in range(0, 46):
# very risky to do this.
# does each folder have the same 46 raster?
# is the order the same so that the correct rasters will be used together?
path_ras_file = os.path.join(path1, lst_files[i])
path_ras_fize = os.path.join(path2, lst_fizes[i])
path_ras_fixe = os.path.join(path3, lst_fixes[i])
# I guess you will have to convert to numpy
# asuming all have the same dimensiones
np_file = arcpy.RasterToNumPyArray(arcpy.Raster(path_ras_file))
np_fize = arcpy.RasterToNumPyArray(arcpy.Raster(path_ras_fize))
np_fixe = arcpy.RasterToNumPyArray(arcpy.Raster(path_ras_fixe))
# your function uses a parameter. What is this parameter?
param ="define parameter!"
result_function = myFunction(param, np_file, np_fixe, np_fize)
# are you sure that all the necessary parameters are provided?
z = scipy.optimize.fsolve(result_function, zGuess)
# do something with the z
def myFunction(z, np_file, np_fixe, np_fize):
# what is z?
E = z[0]
G = z[1]
H = z[2]
F = np.empty((3))
F[0] = (np_file/np_fize) * 3 * H * pow(abs(H), -(1/6)) - G # can this be done with numpy arrays?
F[1] = np_fixe - E - H - G
F[2] = np_file * H - E
return F
if __name__ == '__main__':
main()
... View more
11-30-2017
11:21 AM
|
0
|
2
|
1452
|
POST
|
Sorry for the late response, it's still in the very early hours of the morning in my part of the world. I guess this is what you requested. The code below was giving me error when I ran it. import os
import arcpy
import scipy
import numpy as np
def myFunction(z):
E = z[0]
G = z[1]
H = z[2]
F = np.empty((3))
F[0] = (file/fize)*3*H*pow(abs(H),-(1/6))-G
F[1] = fixe-E-H-G
F[2] = file*H-E
return F
zGuess= np.array([1,1,1])
arcpy.env.workspace ="D:/GIS Data/Sixth Creek ET.gdb"
rasters = arcpy.ListRasters("*", "All")
path = "C:\\Workspace\\test geowrite\\Betasigma\\"
path2 = "C:\\Workspace\\test geowrite\\sigma\\"
path3 = "C:\\Workspace\\test geowrite\\Netrad\\"
files= [f for f in os.listdir(path) if f.endswith(".tif")]
fizes= [f for f in os.listdir(path2) if f.endswith(".tif")]
fixes= [f for f in os.listdir(path2) if f.endswith(".tif")]
for i in range(0,46):
file = path+files[i]
fize = path2+fizes[i]
fixe = path3+fixes[i]
z = fsolve(myFunction,zGuess)
... View more
11-30-2017
11:01 AM
|
0
|
0
|
1279
|
POST
|
I think I understand what you mean. I'll post both codes I've worked with in the format you requested. First the little code that ran the equation and printed values for E,H and G. import arcpy
import scipy
import numpy as np
def myFunction(z):
E = z[0]
G = z[1]
H = z[2]
F = np.empty((3))
F[0] = (3.356/1.024)*3*H*pow(abs(H),-(1/6))-G
F[1] = 300-E-H-G
F[2] = 3.356*H-E
return F
zGuess= np.array([1,1,1])
z = fsolve(myFunction,zGuess)
print z
... View more
11-30-2017
10:50 AM
|
0
|
0
|
1279
|
POST
|
import os
... import arcpy
... import scipy
... import numpy as np
... def myFunction(z):
... E = z[0]
... G = z[1]
... H = z[2]
... F = np.empty((3))
... F[0] = (file/fize)*3*H*pow(abs(H),-(1/6))-G
... F[1] = fixe-E-H-G
... F[2] = file*H-E
... return F
...
... zGuess= np.array([1,1,1])
... arcpy.env.workspace ="D:/GIS Data/Sixth Creek ET.gdb"
... rasters = arcpy.ListRasters("*", "All")
... path = "C:\\Workspace\\test geowrite\\Betasigma\\"
... path2 = "C:\\Workspace\\test geowrite\\sigma\\"
... path3 = "C:\\Workspace\\test geowrite\\Netrad\\"
... files= [f for f in os.listdir(path) if f.endswith(".tif")]
... fizes= [f for f in os.listdir(path2) if f.endswith(".tif")]
... fixes= [f for f in os.listdir(path2) if f.endswith(".tif")]
... for i in range(0,46):
... file = path+files[i]
... fize = path2+fizes[i]
... fixe = path3+fixes[i]
... z = fsolve(myFunction,zGuess)
...
Runtime error
Traceback (most recent call last):
File "<string>", line 28, in <module>
File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve
res = _root_hybr(func, x0, args, jac=fprime, **options)
File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 197, in _root_hybr
shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,))
File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 20, in _check_func
res = atleast_1d(thefunc(*((x0[:numinputs],) + args)))
File "<string>", line 10, in myFunction
TypeError: unsupported operand type(s) for /: 'str' and 'str'
... View more
11-30-2017
07:35 AM
|
0
|
5
|
1452
|
POST
|
https://drive.google.com/file/d/1dE32qKVm0V83d4pvmPUD0nWvDmrOaz3w/view?usp=sharing Hi Xander, thanks for looking at the problem, I have included sample data in my google drive link above (3 each as against 46). A few things are over the top of my head really. I see I don't use the ListRasters command in the script at all, I guess not needed. Yes the rasters are arranged in order i.e the 46 in the same manner in each folder. I don't know about arrays either, just reading about them and the fsolve function. But I am sure the fsolve is able to solve the non linear equations as I tested it earlier using the code below but I replaced the rasters with actual numbers. I am not sure what you mean by parameter in line 32 as I did not add any such in the code below. import arcpy import scipy import numpy as np def myFunction(z): ... E = z[0] ... G = z[1] ... H = z[2] ... F = np.empty((3)) ... F[0] = (3.356/1.024)*3*H*pow(abs(H),-(1/6))-G ... F[1] = 300-E-H-G ... F[2] = 3.356*H-E ... return F zGuess= np.array([1,1,1]) z = fsolve(myFunction,zGuess) print z [ 70.96121951 207.89419779 21.14458269]
... View more
11-30-2017
07:24 AM
|
0
|
2
|
1452
|
POST
|
Xander Bakker,@Dan Patterson, Randy burton, @kevin Dunlop, @Joshua Bixby After getting the code to run using just numbers to replace the rasters, I tried this code but it didn't quite work. Please help. This is doing my head in. I tried this code which I basically tried to cut out a chunk of a code that once worked in iteration. Please help. I am running out of time for a dealine import os ... import arcpy ... import scipy ... import numpy as np ... def myFunction(z): ... E = z[0] ... G = z[1] ... H = z[2] ... F = np.empty((3)) ... F[0] = (file/fize)*3*H*pow(abs(H),-(1/6))-G ... F[1] = fixe-E-H-G ... F[2] = file*H-E ... return F ... ... zGuess= np.array([1,1,1]) ... arcpy.env.workspace ="D:/GIS Data/Sixth Creek ET.gdb" ... rasters = arcpy.ListRasters("*", "All") ... path = "C:\\Workspace\\test geowrite\\Betasigma\\" ... path2 = "C:\\Workspace\\test geowrite\\sigma\\" ... path3 = "C:\\Workspace\\test geowrite\\Netrad\\" ... files= [f for f in os.listdir(path) if f.endswith(".tif")] ... fizes= [f for f in os.listdir(path2) if f.endswith(".tif")] ... fixes= [f for f in os.listdir(path2) if f.endswith(".tif")] ... for i in range(0,46): ... file = path+files ... fize = path2+fizes ... fixe = path3+fixes ... z = fsolve(myFunction,zGuess) ... Runtime error Traceback (most recent call last): File "<string>", line 28, in <module> File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve res = _root_hybr(func, x0, args, jac=fprime, **options) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 197, in _root_hybr shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 20, in _check_func res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) File "<string>", line 10, in myFunction TypeError: unsupported operand type(s) for /: 'str' and 'str'
... View more
11-29-2017
10:23 PM
|
0
|
14
|
3283
|
POST
|
After getting the code to run using just numbers to replace the rasters, I tried this code but it didn't quite work. Please help. This is doing my head in. I tried this code which I basically tried to cut out a chunk of a code that once worked in iteration. Please help. I am running out of time for a dealine import os ... import arcpy ... import scipy ... import numpy as np ... def myFunction(z): ... E = z[0] ... G = z[1] ... H = z[2] ... F = np.empty((3)) ... F[0] = (file/fize)*3*H*pow(abs(H),-(1/6))-G ... F[1] = fixe-E-H-G ... F[2] = file*H-E ... return F ... ... zGuess= np.array([1,1,1]) ... arcpy.env.workspace ="D:/GIS Data/Sixth Creek ET.gdb" ... rasters = arcpy.ListRasters("*", "All") ... path = "C:\\Workspace\\test geowrite\\Betasigma\\" ... path2 = "C:\\Workspace\\test geowrite\\sigma\\" ... path3 = "C:\\Workspace\\test geowrite\\Netrad\\" ... files= [f for f in os.listdir(path) if f.endswith(".tif")] ... fizes= [f for f in os.listdir(path2) if f.endswith(".tif")] ... fixes= [f for f in os.listdir(path2) if f.endswith(".tif")] ... for i in range(0,46): ... file = path+files ... fize = path2+fizes ... fixe = path3+fixes ... z = fsolve(myFunction,zGuess) ... Runtime error Traceback (most recent call last): File "<string>", line 28, in <module> File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 140, in fsolve res = _root_hybr(func, x0, args, jac=fprime, **options) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 197, in _root_hybr shape, dtype = _check_func('fsolve', 'func', func, x0, args, n, (n,)) File "C:\Program Files (x86)\python27\ArcGIS10.4\lib\site-packages\scipy\optimize\minpack.py", line 20, in _check_func res = atleast_1d(thefunc(*((x0[:numinputs],) + args))) File "<string>", line 10, in myFunction TypeError: unsupported operand type(s) for /: 'str' and 'str'
... View more
11-29-2017
10:14 PM
|
0
|
15
|
3283
|
Title | Kudos | Posted |
---|---|---|
1 | 11-21-2017 02:54 AM | |
1 | 11-19-2017 03:43 PM |
Online Status |
Offline
|
Date Last Visited |
11-11-2020
02:24 AM
|