diff --git a/chapter2/solutions/nyc_forecast_owm.py b/chapter2/solutions/nyc_forecast_owm.py deleted file mode 100644 index 7a84079..0000000 --- a/chapter2/solutions/nyc_forecast_owm.py +++ /dev/null @@ -1,40 +0,0 @@ -''' -Retrieve a day's forecast of New York City and create a graph -Using https://github.com/csparpa/pyowm -''' -import matplotlib.pyplot as plt -from pyowm import OWM -import pytz - -owm = OWM('') - -def get_forecast(city): - # this used to be daily_forecast - # https://github.com/csparpa/pyowm/issues/266 - fc = owm.three_hours_forecast(city) - f = fc.get_forecast() - - # three_hours_forecast() returns 5 day forecast at a granularity - # of three hours - # We plot the forecast for the next 24 hours (so, first 8 intervals) - weathers = f.get_weathers()[0:8] - data_points = ['temp', 'temp_max', 'temp_min', 'temp_kf'] - temp = [] - date_time = [] - for w in weathers: - forecast_temp = w.get_temperature('celsius') - utc_dt = datetime.utcfromtimestamp(w.get_reference_time()).replace(tzinfo=pytz.utc) - tz = pytz.timezone('America/New_York') - dt = utc_dt.astimezone(tz) - date_time.append(dt.strftime('%Y-%m-%d %H:%M')) - temp.append(forecast_temp['temp']) - x = range(1, len(temp)+1) - plt.plot(x, temp, 'o-') - plt.xticks(x, date_time, rotation=45) - plt.show() - # Uncomment below and comment the previous line to save the image - # plt.savefig('nyc_owm_forecast.png') - - -if __name__ == '__main__': - get_forecast('new york, us') diff --git a/chapter2/solutions/nyc_forecast_real.py b/chapter2/solutions/nyc_forecast_real.py new file mode 100644 index 0000000..b4158df --- /dev/null +++ b/chapter2/solutions/nyc_forecast_real.py @@ -0,0 +1,41 @@ +''' +Retrieve a day's forecast of New York City and create a graph +Using data downloaded from https://doingmathweatherforecast.ai.studio/ +''' +import csv +import matplotlib.pyplot as plt + + +def read_csv(filename): + + time_of_days = [] + forecast_temperature = [] + + with open(filename) as f: + reader = csv.reader(f) + next(reader) + for row in reader: + # Extract only the time from date and time + time_of_day = row[0].split()[1] + time_of_days.append(time_of_day) + forecast_temperature.append(float(row[1])) + + return time_of_days, forecast_temperature + + +def plot_forecast(time_of_day, forecast_temperature): + + # time_of_day = ['4 AM', '7 AM', '10 AM', '1 PM', '4 PM', '7PM', '10 PM'] + # forecast_temp = [71, 70, 74, 80, 82, 81, 76] + time_interval = range(1, len(time_of_day) + 1) + + plt.plot(time_interval, forecast_temperature, 'o-') + plt.xticks(time_interval, time_of_day) + plt.show() + # Uncomment below and comment the previous line to save the image + # plt.savefig('nyc_owm_forecast.png') + + +if __name__ == '__main__': + time_of_day, forecast_temperature = read_csv("weather_New_York_1d_3h.csv") + plot_forecast(time_of_day, forecast_temperature) diff --git a/chapter2/solutions/weather_New_York_1d_3h.csv b/chapter2/solutions/weather_New_York_1d_3h.csv new file mode 100644 index 0000000..3bed0e6 --- /dev/null +++ b/chapter2/solutions/weather_New_York_1d_3h.csv @@ -0,0 +1,9 @@ +"Local Date & Time","Temperature" +"2026-11-03 19:00","63.1" +"2026-11-03 22:00","60.3" +"2026-11-04 01:00","58.5" +"2026-11-04 04:00","57.6" +"2026-11-04 07:00","56.5" +"2026-11-04 10:00","63.7" +"2026-11-04 13:00","68.7" +"2026-11-04 16:00","67.6" \ No newline at end of file