import os import csv import json def main(): base_dir = "7-national-park-zoo" staff_path = os.path.join(base_dir, "zoo-operations/phase-1-discovery/clean_data/staff.csv") enc_path = os.path.join(base_dir, "zoo-operations/phase-1-discovery/clean_data/enclosure_metadata.csv") health_path = os.path.join(base_dir, "veterinary-care/phase-1-discovery/clean_data/health.csv") enrichment_path = os.path.join(base_dir, "nutrition-dietary/phase-1-discovery/clean_data/enrichment.csv") out_path = os.path.join(base_dir, "prtototype/operational_data.js") # 1. Parse Staff staff = [] if os.path.exists(staff_path): with open(staff_path, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: staff_id = row.get("STAFF_ID", "").strip() if staff_id: staff.append({ "id": staff_id, "name": row.get("STAFF_NAME", "").strip(), "role": row.get("ROLE", "").strip(), "dept": row.get("DEPARTMENT", "").strip(), "training": row.get("TRAINING_LEVEL", "").strip() }) # 2. Parse Enclosures (forward-fill empty IDs/names) enclosures = [] current_enc = None if os.path.exists(enc_path): with open(enc_path, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: enc_id = row.get("ENCLOSURE_ID", "").strip() enc_name = row.get("ENCLOSURE_NAME", "").strip() species = row.get("SPECIES_HOUSED", "").strip() if enc_id or enc_name: current_enc = { "id": enc_id if enc_id else "TEMP-" + str(len(enclosures)), "name": enc_name if enc_name else "Unnamed Enclosure", "type": row.get("ENCLOSURE_TYPE", "").strip(), "area": row.get("AREA_M2", "").strip(), "veg": row.get("VEGETATION_COVER_PCT", "").strip(), "shade": row.get("SHADE_TYPE", "").strip(), "water": row.get("WATER_FEATURE", "").strip(), "orientation": row.get("EXHIBIT_ORIENTATION", "").strip(), "notes": row.get("NOTES", "").strip(), "species": [species] if species else [] } enclosures.append(current_enc) else: if current_enc and species: current_enc["species"].append(species) # 3. Parse Health Reports health_records = [] total_cases = 0 severity_counts = {"Routine": 0, "Needs follow-up": 0, "Urgent": 0} if os.path.exists(health_path): with open(health_path, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: rec_id = row.get("RECORD_ID", "").strip() if not rec_id or rec_id == "RECORD_ID" or "RECORD_ID" in rec_id: continue species = row.get("SPECIES", "").strip() notes = row.get("CLINICAL_NOTES", "").strip() treatment = row.get("TREATMENTS_GIVEN", "").strip() vet = row.get("VET_ID", "").strip() date = row.get("DATE", "").strip() if not notes and not treatment: continue severity = "Routine" notes_lower = notes.lower() if any(x in notes_lower for x in ["urgent", "severe", "cancer", "fracture", "bleeding", "fip", "anemia"]): severity = "Urgent" elif any(x in notes_lower for x in ["follow up", "chronic", "injury", "wound", "diarrhea", "coughing"]): severity = "Needs follow-up" severity_counts[severity] += 1 total_cases += 1 if len(health_records) < 50: health_records.append({ "id": rec_id, "date": date, "species": species, "notes": notes, "treatment": treatment, "vet": vet, "severity": severity }) # 4. Parse Enrichment Logs enrichment_logs = [] activity_types = {} total_enrichments = 0 if os.path.exists(enrichment_path): with open(enrichment_path, 'r', encoding='utf-8') as f: reader = csv.DictReader(f) for row in reader: log_id = row.get("LOG_ID", "").strip() if not log_id or log_id == "LOG_ID" or "LOG_ID" in log_id: continue act_type = row.get("ACTIVITY_TYPE", "").strip() if act_type: activity_types[act_type] = activity_types.get(act_type, 0) + 1 total_enrichments += 1 if len(enrichment_logs) < 50: enrichment_logs.append({ "id": log_id, "date": row.get("DATE", "").strip(), "activity": act_type, "species": row.get("ENCLOSURE_ID", "").strip(), "item": row.get("ENRICHMENT_ITEM", "").strip(), "notes": row.get("NOTES", "").strip() }) # Prepare output data ops_data = { "staff": staff, "enclosures": enclosures, "health": { "total_cases": total_cases, "severity_counts": severity_counts, "recent": health_records }, "enrichment": { "total_logs": total_enrichments, "activity_counts": activity_types, "recent": enrichment_logs } } # Write JS file with open(out_path, 'w', encoding='utf-8') as f: f.write("// Auto-generated from Discovery CSV files\n") f.write("window.ZOO_OPERATIONS = ") json.dump(ops_data, f, ensure_ascii=False, indent=2) f.write(";\n") print(f"Operational data generated successfully. Saved to {out_path}") if __name__ == "__main__": main()