-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwave_process
More file actions
152 lines (120 loc) · 4.4 KB
/
wave_process
File metadata and controls
152 lines (120 loc) · 4.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import pandas as pd
import re
import pandas as pd
import os
import pandas as pd
import os
import numpy as np
import re
import sqlite3
conn = sqlite3.connect("site_reporting.db")
for table in ["maintenance_reports", "daily_safety_patrol", "qc_activities"]:
print(f"Sample from {table}:")
for row in conn.execute(f"SELECT * FROM {table} LIMIT 3;"):
print(row)
print()
conn.close()
'''
# --- CONFIGURE THESE ---
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
PROJECT_ROOT = os.path.abspath(os.path.join(BASE_DIR, ".."))
RAW_DATA_PATH = os.path.join(PROJECT_ROOT, "raw_data")
# path to your WAVE file (you already have RAW_DATA_PATH in your script)
wave_file = os.path.join(RAW_DATA_PATH, "WAVE-I_Daily_Activity_Report_30_June_2025.xlsx")
MAINTENANCE_SECTIONS = [
"Rotating", "Static", "Electrical", "Instrument",
"Scaffolding", "Boom Truck", "Crane", "Insulation"
]
PATROL_SECTIONS = ["HSE Activities", "Daily Safety Patrol"]
QC_SECTIONS = ["QC Activities"]
ALL_SECTION_LABELS = MAINTENANCE_SECTIONS + PATROL_SECTIONS + QC_SECTIONS
# 1) pick the sheet you want to test
xls = pd.ExcelFile(wave_file)
print("[INFO] Available sheets:", xls.sheet_names)
target_date = "June 19 2025"
for s in xls.sheet_names:
if target_date in s:
sheet_name = s.strip()
break
else:
raise ValueError(f"Could not find any sheet with '{target_date}'")
print(f"[INFO] → Parsing sheet: {sheet_name}")
raw = pd.read_excel(wave_file, sheet_name=sheet_name, header=None)
# extract report_date from the sheet name
m = re.search(r"\((.*?)\)", sheet_name)
report_date = pd.to_datetime(m.group(1), errors="coerce").strftime("%Y-%m-%d") if m else "N/A"
# 2) locate where each section starts
section_starts = {}
for idx, cell in raw[0].items():
if isinstance(cell, str):
low = cell.lower()
for label in ALL_SECTION_LABELS:
if label.lower() in low:
section_starts[label] = idx
print("[INFO] Found sections:", section_starts)
# 3) sort by row
ordered = sorted(section_starts.items(), key=lambda x: x[1])
# 4) containers
maintenance_parts = []
patrol_parts = []
qc_parts = []
# 5) loop
for i, (sec, start_row) in enumerate(ordered):
end_row = raw.shape[0]
if i + 1 < len(ordered):
end_row = ordered[i+1][1]
block = raw.iloc[start_row+1:end_row].copy()
block.dropna(how="all", inplace=True)
if block.empty:
continue
# detect the real header row *per category*
if sec in MAINTENANCE_SECTIONS:
# look for row that has both "Area" and "Unit"
mask = block.apply(lambda r:
r.astype(str).str.contains("area", case=False, na=False).any() and
r.astype(str).str.contains("unit", case=False, na=False).any()
, axis=1)
elif sec in PATROL_SECTIONS:
# look for "Permit No"
mask = block.apply(lambda r:
r.astype(str).str.contains("permit no", case=False, na=False).any()
, axis=1)
elif sec in QC_SECTIONS:
# look for "S/N"
mask = block.apply(lambda r:
r.astype(str).str.contains(r"s/n", case=False, na=False).any()
, axis=1)
else:
continue
if not mask.any():
print(f"[WARN] no header row found for section {sec}, skipping")
continue
hdr_idx = mask[mask].index[0]
block = block.loc[hdr_idx:] # keep from header downward
block.columns = block.iloc[0] # set header
block = block.iloc[1:].reset_index(drop=True)
# normalize column names
block.columns = [
str(c).strip().lower().replace(" ", "_") for c in block.columns
]
block["section"] = sec
block["report_date"] = report_date
# assign to the right list
if sec in MAINTENANCE_SECTIONS:
maintenance_parts.append(block)
elif sec in PATROL_SECTIONS:
patrol_parts.append(block)
else:
qc_parts.append(block)
# 6) combine
df_maintenance = pd.concat(maintenance_parts, ignore_index=True) if maintenance_parts else pd.DataFrame()
df_patrol = pd.concat(patrol_parts, ignore_index=True) if patrol_parts else pd.DataFrame()
df_qc = pd.concat(qc_parts, ignore_index=True) if qc_parts else pd.DataFrame()
# 7) inspect
print("\n[INFO] --- MAINTENANCE ---")
print(df_maintenance.head(), "\n")
print("[INFO] --- PATROL ---")
print(df_patrol.head(), "\n")
print("[INFO] --- QC ---")
print(df_qc.head())
'''