I need to increment a row in attribute table:
def func (w,v,pot):
return pot + ((pot+1 - pot)/(v+1 - v))*(w-v)
The "+1" is not the addition of number one to the variable, is the intention to increment + 1 row in that column of that variable. This is because I need to calculate the actual record using records from the next line.
How can I do that? I know this is basic, but I realy don't know python.
-
Going a little further, is it possible to search the v records immediately lower and higher than w? And use just this values to do the interpolation?AndM– AndM2015年10月05日 12:34:09 +00:00Commented Oct 5, 2015 at 12:34
-
Field calculator is great for simple functions, but once you want to start implementing look-ahead/behind, it becomes a lot simpler to just use cursors. They have a bit of a learning curve especially if you aren't familiar with Python.Paul– Paul2015年10月05日 17:43:37 +00:00Commented Oct 5, 2015 at 17:43
-
Have a look at my solution in duplicate hyperlink, with 3 rows tableFelixIP– FelixIP2015年10月05日 23:00:27 +00:00Commented Oct 5, 2015 at 23:00
1 Answer 1
I am not aware of a technique for accessing the next record. But you can access the previous record using the field calculator. Using Python and a codeblock, you need to have a global variable that keeps the value of the previous record. That would be something like:
pot_previous = None
v_previous = None
def func (w,v,pot):
global pot_previous
global v_previous
if pot_previous is None or v_previous is None:
pot_previous = pot
v_previous = v
return
result_value = pot + ((pot_previous - pot) / (v_previous - v))*(w-v)
pot_previous = pot
v_previous = v
return result_value
You can probably do what you was intending to do by sorting your records in reverse order of what you have intended initially.
Have also a look at this GIS.SE question and the ArcGIS resource center help (section "Accumulative and sequential calculations").
-
1Thank you so much for your help. Why did you put "global" before variables? And why in the end you need to write again "pot_previous = pot" and "v_previous = v? I'm sorry to ask that, but it's not intuitive to me.AndM– AndM2015年10月05日 12:26:15 +00:00Commented Oct 5, 2015 at 12:26
-
1Oh, that is to have access to the variables
pot_previous
andv_previous
defined before the function from within the function. If you don't do this, a variable with namepot_previous
inside your function is not the same as the one outside of your function, and without it you could not "remember" the values from the previous record. ArcGIS will make one run through the function for each record, but not initialize again the variables defined before the function.chkaiser– chkaiser2015年10月05日 16:41:42 +00:00Commented Oct 5, 2015 at 16:41 -
1For the
pot_previous = pot
andv_previous = v
statements that are twice: the first one is just for the first record to remember the value but without doing interpolation (you need to skip the first record because you need the value of another row). The second time it is for updating the value so that in the next run you can access the value of the previous record.chkaiser– chkaiser2015年10月05日 16:42:13 +00:00Commented Oct 5, 2015 at 16:42 -
1Thanks for the explanation! I changed to: result_value = pot_previous + ((pot - pot_previous ) / (v - v_previous ))*(w-v_previous) because in interpolation I need to add the previous value, not the actual. But it's not giving me the expected result...AndM– AndM2015年10月05日 16:54:08 +00:00Commented Oct 5, 2015 at 16:54
Explore related questions
See similar questions with these tags.