Hi everyone, I've written a cellular automata code in python that updates land uses based on each cell's adjacent neighbors. I import a text file as a dictionary with cell id: land use code. I also import a text file with each cell's adjacent neighbor. I then run the cellular automata rules on each cell and count the number of times a cell changed its land use. The first run gives a total of 7509 cells that changed their land use based on adjacent neighbors. If I comment out the import dictionary and run it again, the the number of cells changed is around 5,000. Next run, there's 3,000 and so on until the number of cells that change is negligible which I have defined as 0.0001 of the total number of cells. I want to create a loop for this and break the loop once the number of cells changed is less 0.0001 of the total cells. I've tried a while loop and the first run goes through with the right count -- 7509, but the second and subsequent runs within the while loop give me one value -- 28,476. I don't understand this because I'm starting the count at zero each time it enters the while loop. Can anyone see what I'm doing wrong in the following code? Any help would be appreciated.
import sys, string, csv
#Creating a dictionary of FID: LU_Codes from external txt file
text_file = open("H:\SWAT\NC\FID_Whole_Copy.txt", "rb")
#Lines = text_file.readlines()
FID_GC_dict = dict()
reader = csv.reader(text_file, delimiter='\t')
for line in reader:
FID_GC_dict[line[0]] = int(line[1])
text_file.close()
#Importing neighbor list file for each FID value
Neighbors_file = open("H:\SWAT\NC\Pro_NL_Copy.txt","rb")
Entries = Neighbors_file.readlines()
Neighbors_file.close()
Neighbors_List = map(string.split, Entries)
#print Neighbors_List
#creates a list of the current FID
FID = [x[0] for x in Neighbors_List]
gridList = []
for nlist in Neighbors_List:
row = []
for item in nlist:
row.append(FID_GC_dict[item])
gridList.append(row)
#print gridList
#Calculate when to end the simulations (negligible change in land use)
tot_cells = len(FID)
end_sim = tot_cells
p = 0.0001
#Setting up one sweep through for all cells
i = iter(FID)
Cur_FID = i.next()
#Performs cellular automata rules on land use grid codes
while (end_sim > tot_cells*p):
count = 0
for glist in gridList:
Cur_GC = glist[0]
glist.sort()
lr_Value = glist[-1]
if lr_Value < 6:
tie_LR = glist.count(lr_Value)
if tie_LR >= 4 and lr_Value > Cur_GC:
FID_GC_dict[Cur_FID] = lr_Value
#print "The updated gridcode for FID ", Cur_FID, "is ", FID_GC_dict[Cur_FID]
count += 1
end_sim = count
print end_sim
Thanks for any assistance!