"""Parse the text-based County turning-movement count pages (hourly summary page) into data/measured/county-counts/. Source sheets (text layer): Clayton Rd (034, page 8) and Wydown (063, page 8): rows of HH:MM AM|PM then 4 approaches (From North, East, South, West) x [Right, Thru, Left, Peds, App.Total] then Int.Total followed by a 'Grand Total' row. Writes --.csv plus .totals.csv (printed totals) for verify_counts.py. Scanned sheets (Forsyth, Forest Park Pkwy) are transcribed by hand into the same format. """ import re, pathlib, datetime import pymupdf ROOT = pathlib.Path(__file__).resolve().parents[1] SRC, OUT = ROOT/"data/measured/county-timing", ROOT/"data/measured/county-counts" SHEETS = [("034-clayton-rd.pdf", 8, "034-clayton-rd"), ("063-wydown.pdf", 8, "063-wydown")] APPROACH = ["SB", "WB", "NB", "EB"] # From North, From East, From South, From West def parse(pdf, page): text = pymupdf.open(SRC/pdf)[page-1].get_text() date = re.search(r"Start Date\s*:\s*(\d+/\d+/\d+)", text).group(1) tok = text.split() i = tok.index("Int.") + 2 rows, totals = [], None while i < len(tok): if re.fullmatch(r"\d\d:\d\d", tok[i]) and tok[i+1] in ("AM", "PM"): h, m = map(int, tok[i].split(":")); h = h % 12 + (12 if tok[i+1] == "PM" else 0) nums = list(map(int, tok[i+2:i+23])); rows.append((f"{h:02d}:{m:02d}", nums)); i += 23 elif tok[i] == "Grand" and tok[i+1] == "Total": totals = list(map(int, tok[i+2:i+23])); break else: i += 1 return date, rows, totals def main(): OUT.mkdir(parents=True, exist_ok=True) for pdf, page, stem in SHEETS: date, rows, totals = parse(pdf, page) iso = datetime.datetime.strptime(date, "%m/%d/%Y").date().isoformat() f = OUT/f"{stem}-{iso}.csv" with open(f, "w") as o: o.write(f"# St. Louis County turning-movement count, {date}, hourly. Parsed from the text layer of {pdf} page {page} " f"by parse_county_counts.py.\n# approach: SB = Big Bend from North, WB = from East, NB = Big Bend from South, EB = from West\n") o.write("hour,approach,L,T,R,peds,app_total\n") for k, a in enumerate(APPROACH): for hour, n in rows: R, T, L, P, A = n[5*k:5*k+5] o.write(f"{hour},{a},{L},{T},{R},{P},{A}\n") with open(f.with_name(f.stem+".totals.csv"), "w") as o: o.write("# printed Grand Total row and hourly intersection totals\nkind,key,L,T,R,peds,app_total\n") for k, a in enumerate(APPROACH): R, T, L, P, A = totals[5*k:5*k+5] o.write(f"grand,{a},{L},{T},{R},{P},{A}\n") for hour, n in rows: o.write(f"int,{hour},,,,,{n[20]}\n") o.write(f"int,grand,,,,,{totals[20]}\n") print(f"{f.name}: {len(rows)} hours") if __name__ == "__main__": main()