Select to view content in your preferred language

Attribute table update using standalone table

260
1
06-13-2023 03:39 AM
KELVINKARANJA
New Contributor

I have a river gauge point layer whose attribute table has gaugid and average_reading fields. I also have a standalone table the has morning and evening readings field for each river gauge. The standalone table is fed readings using Field maps twice a day. I have published the layers on ArcGIS online.

I want a script that gets daily average for each gauge and then updates the average reading to the point attribute table in the average_reading field.

0 Kudos
1 Reply
JohannesLindner
MVP Frequent Contributor

Untested, supply your table and field names in lines 13 & 17.

import arcpy

def mean(values):
    values = [v for v in values if v is not None]
    try:
        return sum(values) / len(values)
    except ZeroDivisionError:
        raise ValueError("No non-null values in input list!")

# get the morning and evening readings from the update table (dict with id as key)
update_rows = {
    i: [m, e]
    for i, m, e in arcpy.da.SearchCursor("UpdateTable", ["GaugeID", "Morning", "Evening"])
    }

# update the gauge layer
with arcpy.da.UpdateCursor("Gauges", ["GaugeID", "AverageReading"]) as cursor:
    for i, avg in cursor:
        try:
            avg = mean(update_rows[i])
        except KeyError:
            print("GaugeID not found in update table: " + str(i))
            continue
        except ValueError:
            print("Could not calculate average for GaugeID " + str(i))
            continue
        cursor.updateRow([i, avg])

Have a great day!
Johannes
0 Kudos