|
| 1 | +"""Add temperature and humidity data to ABR-run-data sheet.""" |
| 2 | + |
| 3 | +from abr_testing.automation import google_sheets_tool |
| 4 | +from abr_testing.automation import google_drive_tool |
| 5 | +import argparse |
| 6 | +import csv |
| 7 | +import sys |
| 8 | +import os |
| 9 | +from typing import Dict, Tuple, Any, List |
| 10 | +from statistics import mean, StatisticsError |
| 11 | + |
| 12 | + |
| 13 | +def add_robot_lifetime(abr_data: List[Dict[str, Any]]) -> None: |
| 14 | + """Add % Robot Lifetime to each run.""" |
| 15 | + # TODO: add robot lifetime to each run. |
| 16 | + |
| 17 | + |
| 18 | +def compare_run_to_temp_data( |
| 19 | + abr_data: List[Dict[str, Any]], temp_data: List[Dict[str, Any]], google_sheet: Any |
| 20 | +) -> None: |
| 21 | + """Read ABR Data and compare robot and timestamp columns to temp data.""" |
| 22 | + row_update = 0 |
| 23 | + for run in abr_data: |
| 24 | + run_id = run["Run_ID"] |
| 25 | + try: |
| 26 | + average_temp = float(run["Average Temp (oC)"]) |
| 27 | + except ValueError: |
| 28 | + average_temp = 0 |
| 29 | + if len(run_id) < 1 or average_temp > 0: |
| 30 | + continue |
| 31 | + else: |
| 32 | + # Determine which runs do not have average temp/rh data |
| 33 | + temps = [] |
| 34 | + rel_hums = [] |
| 35 | + for recording in temp_data: |
| 36 | + temp_robot = recording["Robot"] |
| 37 | + if len(recording["Timestamp"]) > 1: |
| 38 | + timestamp = recording["Timestamp"] |
| 39 | + if ( |
| 40 | + temp_robot == run["Robot"] |
| 41 | + and timestamp >= run["Start_Time"] |
| 42 | + and timestamp <= run["End_Time"] |
| 43 | + ): |
| 44 | + temps.append(float(recording["Temp (oC)"])) |
| 45 | + rel_hums.append(float(recording["Relative Humidity (%)"])) |
| 46 | + try: |
| 47 | + avg_temps = mean(temps) |
| 48 | + avg_humidity = mean(rel_hums) |
| 49 | + row_num = google_sheet.get_row_index_with_value(run_id, 2) |
| 50 | + # Write average temperature |
| 51 | + google_sheet.update_cell("Sheet1", row_num, 46, avg_temps) |
| 52 | + # Write average humidity |
| 53 | + google_sheet.update_cell("Sheet1", row_num, 47, avg_humidity) |
| 54 | + # TODO: Write averages to google sheet |
| 55 | + print(f"Updated row {row_num}.") |
| 56 | + except StatisticsError: |
| 57 | + avg_temps = None |
| 58 | + avg_humidity = None |
| 59 | + print(f"Updated {row_update} rows with temp and RH data.") |
| 60 | + |
| 61 | + |
| 62 | +def read_csv_as_dict(file_path: str) -> List[Dict[str, Any]]: |
| 63 | + """Read a CSV file and return its content as a list of dictionaries.""" |
| 64 | + with open(file_path, mode="r", newline="", encoding="utf-8") as csvfile: |
| 65 | + reader = csv.DictReader(csvfile) |
| 66 | + data = [row for row in reader] |
| 67 | + return data |
| 68 | + |
| 69 | + |
| 70 | +def connect_and_download( |
| 71 | + sheets: Dict[str, str], storage_directory: str |
| 72 | +) -> Tuple[List[str], str]: |
| 73 | + """Connect to google sheet and download.""" |
| 74 | + try: |
| 75 | + credentials_path = os.path.join(storage_directory, "credentials.json") |
| 76 | + google_drive = google_drive_tool.google_drive( |
| 77 | + credentials_path, |
| 78 | + "1W8S3EV3cIfC-ZoRF3km0ad5XqyVkO3Tu", |
| 79 | + |
| 80 | + ) |
| 81 | + print("connected to gd") |
| 82 | + except FileNotFoundError: |
| 83 | + print(f"Add credentials.json file to: {storage_directory}.") |
| 84 | + sys.exit() |
| 85 | + file_paths = [] |
| 86 | + for sheet in sheets.items(): |
| 87 | + file_name, file_id = sheet[0], sheet[1] |
| 88 | + print(file_name) |
| 89 | + file_path = google_drive.download_single_file( |
| 90 | + storage_directory, file_id, file_name, "text/csv" |
| 91 | + ) |
| 92 | + file_paths.append(file_path) |
| 93 | + return file_paths, credentials_path |
| 94 | + |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + parser = argparse.ArgumentParser( |
| 98 | + description="Adds average robot ambient conditions to run sheet." |
| 99 | + ) |
| 100 | + parser.add_argument( |
| 101 | + "--abr-data-sheet", |
| 102 | + type=str, |
| 103 | + default="1M6LSLNwvWuHQOwIwUpblF_Eyx4W5y5gXgdU3rjU2XFk", |
| 104 | + help="end of url of main data sheet.", |
| 105 | + ) |
| 106 | + parser.add_argument( |
| 107 | + "--room-conditions-sheet", |
| 108 | + type=str, |
| 109 | + default="1cIjSvK_mPCq4IFqUPB7SgdDuuMKve5kJh0xyH4znAd0", |
| 110 | + help="end fo url of ambient conditions data sheet", |
| 111 | + ) |
| 112 | + parser.add_argument( |
| 113 | + "--storage-directory", |
| 114 | + type=str, |
| 115 | + default="C:/Users/Rhyann Clarke/test_folder", |
| 116 | + help="Path to long term storage directory for run logs.", |
| 117 | + ) |
| 118 | + args = parser.parse_args() |
| 119 | + google_sheets_to_download = { |
| 120 | + "ABR-run-data": args.abr_data_sheet, |
| 121 | + "ABR Ambient Conditions": args.room_conditions_sheet, |
| 122 | + } |
| 123 | + storage_directory = args.storage_directory |
| 124 | + # Download google sheets. |
| 125 | + file_paths, credentials_path = connect_and_download( |
| 126 | + google_sheets_to_download, storage_directory |
| 127 | + ) |
| 128 | + # TODO: read csvs. |
| 129 | + abr_data = read_csv_as_dict(file_paths[0]) |
| 130 | + temp_data = read_csv_as_dict(file_paths[1]) |
| 131 | + # TODO: compare robot and timestamps. |
| 132 | + abr_google_sheet = google_sheets_tool.google_sheet( |
| 133 | + credentials_path, "ABR-run-data", 0 |
| 134 | + ) |
| 135 | + |
| 136 | + compare_run_to_temp_data(abr_data, temp_data, abr_google_sheet) |
| 137 | + # TODO: Write average for matching cells. |
0 commit comments