rockhoundmatt
rockhoundmatt

Reputation: 11

Ignore subsequent instances of a condition (for loop and if statement)

I posted some code previously regarding an index out of range error; however, after I resolved that error, I realized that there is a flaw in the process for obtaining the necessary information.

I wrote a for loop in Python to search through a file of data for instances when an identifier matches and a significant change to another parameter occurs. When that instance occurs, the pertinent data is output to a text file. I neglected to realize that I want to ignore any following occurrence(s) of the same condition. I only want to record the first occurrence and then proceed through the file until the situation arises with a different identifier, continuing the process until the end of the file.

The input data looks like this:

0000 0.0000000000 185263.9890 105232.4887 0.0000000000 345.0928095 0.2982502454

0001 0.0000000000 185163.8872 105235.6420 0.0000000000 344.4662790 0.5691114931

0001 218.1599792 185187.4241 105332.8326 100.0000000 30.21264632 0.4583792149

0001 2070.891273 185259.6501 105401.9948 200.0000000 342.4030385 0.05397436763

0002 0.0000000000 185073.8381 105257.5082 0.0000000000 289.0103714 0.1765702856

0002 548.7104537 185007.5650 105182.6224 100.0000000 283.1207050 0.1822454800

0002 4339.891390 184907.6061 105179.7550 200.0000000 235.0171868 0.02637700539

0003 0.0000000000 185044.6468 105355.0609 0.0000000000 141.5931561 0.5491341407

0004 0.0000000000 185037.7703 105455.3306 0.0000000000 148.8916916 0.9710776916

0004 140.8620875 185106.3138 105382.5172 100.0000000 122.1786717 0.7099142273

0004 602.2794100 185206.2952 105384.4448 200.0000000 96.31412264 0.2167235496

0004 2841.576352 185251.6140 105424.2290 260.3040892 139.3786179 0.02692992078

0005 0.0000000000 185879.1193 105481.7119 0.0000000000 240.1629722 0.1176913655

0005 155.2441594 185779.6779 105492.2674 100.0000000 289.7377882 0.6441466166

0005 305.7366429 185688.7618 105533.9118 200.0000000 296.1130146 0.6644850139

0005 632.1957592 185600.2198 105580.3909 300.0000000 298.9600016 0.3063170700

0005 889.3161045 185516.5649 105549.4496 389.1936581 32.05095338 0.3468945953

0005 1114.532354 185538.1521 105576.9088 424.1223441 8.425251603 0.1550895465

0005 1282.115239 185545.3480 105477.1680 524.1223441 3.718447942 0.5967196476

The first item in each line is the identifier. The third and fourth items of each line are coordinate points. The sixth item in each line is a direction angle value (and this is the parameter of interest). The goal is to find two lines with the same identifier and compare the direction angle values for a significant change (in this case greater than 90 degrees) and then record the identifier and coordinate points. For example:

0001 0.0000000000 185163.8872 105235.6420 0.0000000000 344.4662790 0.5691114931

0001 218.1599792 185187.4241 105332.8326 100.0000000 30.21264632 0.4583792149

In these two lines, the identifier 0001 matches, and the direction angle changes by more than 90 degrees. This condition gets flagged by an if statement in the for loop and the identifier and coordinate points are output to a text file. I have that process working. But...

0001 0.0000000000 185163.8872 105235.6420 0.0000000000 344.4662790 0.5691114931

0001 218.1599792 185187.4241 105332.8326 100.0000000 30.21264632 0.4583792149

0001 2070.891273 185259.6501 105401.9948 200.0000000 342.4030385 0.05397436763

A third line in the sequence also matches the identifier AND has a direction angle change greater than 90 degrees, so it also gets flagged by the conditional statement and recorded to the output file. I want to ignore any repeated instances of the condition and only record the first occurrence.

Also, I want to flag and record the coordinate data when two lines being compared do not have a matching identifier, though I think I can handle that as part of an if/else combination with the other conditional. The more important matter is being able to ignore any repeated occurrences of a direction angle change greater than 90 degrees for an already flagged and recorded identifier. Being mostly unfamiliar with Python (and programming in general), I cannot seem to find an existing solution for this scenario. I'm not necessarily looking for a sophisticated solution. I just need to trap a particular set of data to reuse in an ArcGIS project.

Thank you for your suggestions.

line1 = trackdata.readline()
line2 = trackdata.readline()

for i in range(l-1):

    file_no1 = line1.split()[0]
    time1 = line1.split()[1]
    x1 = line1.split()[2]
    y1 = line1.split()[3]
    length1 = line1.split()[4]
    flow_dir1 = float(line1.split()[5])
    flow_mag1 = line1.split()[6]

    file_no2 = line2.split()[0]
    time2 = line2.split()[1]
    x2 = line2.split()[2]
    y2 = line2.split()[3]
    length2 = line2.split()[4]
    flow_dir2 = float(line2.split()[5])
    flow_mag2 = line2.split()[6]

    if file_no1 == file_no2:
        if abs(flow_dir2 - flow_dir1) > 90.0:
            print >> coordinates1, file_no1, x1, y1

    line1 = line2
    line2 = trackdata.readline()

Upvotes: 1

Views: 183

Answers (2)

tdelaney
tdelaney

Reputation: 77337

Just keep a memo of values you've seen

line1 = trackdata.readline()
line2 = trackdata.readline()

memo = set()

for i in range(l-1):

    file_no1 = line1.split()[0]
    time1 = line1.split()[1]
    x1 = line1.split()[2]
    y1 = line1.split()[3]
    length1 = line1.split()[4]
    flow_dir1 = float(line1.split()[5])
    flow_mag1 = line1.split()[6]

    file_no2 = line2.split()[0]
    time2 = line2.split()[1]
    x2 = line2.split()[2]
    y2 = line2.split()[3]
    length2 = line2.split()[4]
    flow_dir2 = float(line2.split()[5])
    flow_mag2 = line2.split()[6]

    if file_no1 == file_no2:
        val = abs(flow_dir2 - flow_dir1)
        if val > 90.0 and val not in memo:
            memo.add(val)
            print >> coordinates1, file_no1, x1, y1

    line1 = line2
    line2 = trackdata.readline()

Upvotes: 0

David
David

Reputation: 696

You could try something like this;

if file_no1 == file_no2:
    if abs(flow_dir2 - flow_dir1) > 90.0:
        print >> coordinates1, file_no1, x1, y1
        tempLine = trackdata.readline() # grabs next line
        temp_no = tempLine.split()[0] # grabs file id
        while temp_no == file_no1: # runs while its the same as the file
                                   #  id that gave us the condition
            tempLine = trackdata.readline()
            temp_no = tempLine.split()[0]
        line1 = tempLine                # set new lines
        line2 = trackdata.readline()

if file_no1 = line1.split()[0]:
    line1 = line2
    line2 = trackdata.readline()

The thing to check for here, is to make sure that you dont try to read past the end of the file, so you will want to add checks for that

Upvotes: 1

Related Questions