-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_fish_data.py
More file actions
96 lines (77 loc) · 3.18 KB
/
update_fish_data.py
File metadata and controls
96 lines (77 loc) · 3.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import pandas as pd
import json
import re
def update_data():
# Read existing JS file
with open("data/fish_data.js", "r") as f:
content = f.read()
# Extract FISH_DATA JSON
# Pattern: generic match for the first object
# const FISH_DATA = { ... };
# We can split by ";\nconst PROFILES_DATA" maybe?
parts = content.split(";\nconst PROFILES_DATA")
fish_data_js = parts[0]
profiles_data_js = "const PROFILES_DATA" + parts[1] if len(parts) > 1 else ""
# Remove "const FISH_DATA = "
json_str = fish_data_js.replace("const FISH_DATA = ", "").strip()
# It might end with a semicolon
if json_str.endswith(";"): json_str = json_str[:-1]
try:
fish_data = json.loads(json_str)
except Exception as e:
print("Failed to parse existing JSON:", e)
return
# Read Excel
df = pd.read_excel("datasets/% Yields NHCS.xlsx", sheet_name="Sheet1")
for index, row in df.iterrows():
name = row["Common name"]
if pd.isna(name): continue
yield_val = row["% Yield"]
notes = row["Notes"] if not pd.isna(row["Notes"]) else ""
# Clean Yield
# If float < 1, multiply by 100
# If string "80-85%", extract range?
final_yield = 0
final_range = ""
try:
if isinstance(yield_val, (int, float)):
if yield_val < 1:
final_yield = int(yield_val * 100)
else:
final_yield = int(yield_val)
elif isinstance(yield_val, str):
# Check for range "80-85%"
yield_val = yield_val.replace("%", "").strip()
if "-" in yield_val:
r_parts = yield_val.split("-")
low = float(r_parts[0])
high = float(r_parts[1])
avg = (low + high) / 2
final_yield = int(avg)
final_range = yield_val
else:
final_yield = int(float(yield_val))
except:
print(f"Skipping {name} due to invalid yield: {yield_val}")
continue
# Add to fish_data
# Use "East Coast" prefix or just merge? User said "East coast species info".
# I'll add them as top level keys.
if name not in fish_data:
fish_data[name] = {}
# Since we don't know the product form, we'll label it "General Yield"
# or append info from Notes if useful.
product_label = "Average Yield"
if notes:
product_label += f" ({notes.strip()})"
fish_data[name][product_label] = {
"yield": str(final_yield),
"range": final_range if final_range else f"{final_yield-5}-{final_yield+5}" # synthetic range?
}
# Reconstruct file
new_content = "const FISH_DATA = " + json.dumps(fish_data, indent=4) + ";\n" + profiles_data_js
with open("data/fish_data.js", "w") as f:
f.write(new_content)
print("Successfully updated fish_data.js with East Coast species.")
if __name__ == "__main__":
update_data()