From c023b5704d49679989ddae60851b82f051f4ac2f Mon Sep 17 00:00:00 2001 From: Sam Chudnick Date: Sat, 3 Jun 2023 14:02:59 -0400 Subject: Appended N/A when no data is available --- library.py | 60 ++++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 38 insertions(+), 22 deletions(-) diff --git a/library.py b/library.py index 2b9c6d1..dcb4630 100644 --- a/library.py +++ b/library.py @@ -146,8 +146,8 @@ def get_temperature(raw_data): ret = [] for val in raw_values: - val_celc = val["value"] - val_fahr = celcius_to_fahrenheit(val_celc) + val_celc = round(val["value"]) + val_fahr = round(celcius_to_fahrenheit(val_celc)) time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) @@ -162,8 +162,8 @@ def get_apparent_temperature(raw_data): for val in raw_values: - val_celc = val["value"] - val_fahr = celcius_to_fahrenheit(val_celc) + val_celc = round(val["value"]) + val_fahr = round(celcius_to_fahrenheit(val_celc)) time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) @@ -180,7 +180,7 @@ def get_humidity(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -190,8 +190,8 @@ def get_wind_chill(raw_data): ret = [] for val in raw_values: - val_celc = val["value"] - val_fahr = celcius_to_fahrenheit(val_celc) + val_celc = round(val["value"]) + val_fahr = round(celcius_to_fahrenheit(val_celc)) time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) @@ -208,7 +208,7 @@ def get_wind_speed(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -221,7 +221,7 @@ def get_wind_gust(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -234,7 +234,7 @@ def get_precip_chance(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -247,7 +247,7 @@ def get_precip_amount(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -260,7 +260,7 @@ def get_snowfall_amount(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -273,7 +273,7 @@ def get_snow_level(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -286,7 +286,7 @@ def get_visibility(raw_data): time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) - ret.append({"time":time,"duration":duration,"value":val["value"]}) + ret.append({"time":time,"duration":duration,"value":round(val["value"])}) return normalize(ret) @@ -317,8 +317,8 @@ def get_dewpoint(raw_data): ret = [] for val in raw_values: - val_celc = val["value"] - val_fahr = celcius_to_fahrenheit(val_celc) + val_celc = round(val["value"]) + val_fahr = round(celcius_to_fahrenheit(val_celc)) time_str, duration_str = val["validTime"].split('/') time = datetime.datetime.strptime(time_str, "%Y-%m-%dT%H:%M:%S%z") duration = parse_duration(duration_str) @@ -329,21 +329,37 @@ def get_dewpoint(raw_data): def get_hourly_data(raw_data, end_time): temps = get_temperature(raw_data) + humidity = get_humidity(raw_data) precip_chance = get_precip_chance(raw_data) precip_amount = get_precip_amount(raw_data) - humidity = get_humidity(raw_data) wind_speed = get_wind_speed(raw_data) wind_direction = get_wind_direction(raw_data) ret = [] i = 0 while i < len(temps) and temps[i]["time"] < end_time: + + if i >= len(temps): + temps.append({"value":"N/A"}) + if i >= len(humidity): + humidity.append({"value":"N/A"}) + if i >= len(precip_chance): + precip_chance.append({"value":"N/A"}) + if i >= len(precip_amount): + precip_amount.append({"value":"N/A"}) + if i >= len(wind_speed): + wind_speed.append({"value":"N/A"}) + if i >= len(wind_direction): + wind_direction.append({"value":"N/A"}) + + + val_dict = { "time": temps[i]["time"], - "temp": int(temps[i]["value_fahr"]), - "humidity": int(humidity[i]["value"]), - "precip_chance": int(precip_chance[i]["value"]), - "precip_amount": int(precip_amount[i]["value"]), - "wind_speed": int(wind_speed[i]["value"]), + "temp": temps[i]["value_fahr"], + "humidity": humidity[i]["value"], + "precip_chance": precip_chance[i]["value"], + "precip_amount": precip_amount[i]["value"], + "wind_speed": wind_speed[i]["value"], "wind_direction": wind_direction[i]["value"] } ret.append(val_dict) i+=1 -- cgit v1.2.3