Home Artificial Intelligence Handling Time Zones with Python Time zones Conclusion

Handling Time Zones with Python Time zones Conclusion

0
Handling Time Zones with Python
Time zones
Conclusion

Illustration of various time zones on this planet. Map © OpenStreetMap contributors licensed under Open Data Commons Open Database License (ODbl) by the OpenStreetMap foundation (OpenStreetMap, 2023). Labels added by Creator.
Image by Luis Cortes in Unsplash.

Geocoding to retrieve the coordinates of 4 cities

from geopy.geocoders import Nominatim
geolocator = Nominatim(user_agent="app")

def get_coordinates(place):
"""Return the latitude and longitude of the place."""
place_details = geolocator.geocode(place)
coordinates = (place_details[1][0], place_details[1][1])

return coordinates

Dataframe is created containing latitude and longitude values of the 4 cities. Illustration by Creator.

Accessing data using NASA Power API

base_url = r”https://power.larc.nasa.gov/api/temporal/hourly/point?parameters=ALLSKY_SFC_SW_DWN&community=RE&time-standard=UTC&longitude={longitude}&latitude={latitude}&format=JSON&start=2020&end=2020"

Parameter description

Equation 1 and a couple of check with formula for calculating the sizes of solar PV module and battery to satisfy given electricity demand based on solar irradiance and other technical parameters. Illustration by Creator.

Basic statistics of given data

Global Horizontal Irradiance data downloaded for 4 cities. Illustration by Creator.
Statistics from the downloaded data. Illustration by Creator.

Time zone Handling

1. Default pandas dataframe without “datetime” format index

Data when first downloaded from NASA Power website. Illustration by Creator.

2. Converting integer type index to “naive” datetime index

Converting integer index to datetime index. Illustration by Creator.
Plotting pandas dataframe with “naive” datetime index shows months from Jan to Dec 2020 within the xaxis. Illustration by Creator.
Checking the time zone info of the primary datetime index of dataframe. Illustration by Creator.

3. Localizing “naive” datetime object to “time zone aware” datetime object

First cell shows the present local time time_now as a naive datetime object without time zone information. Second cell shows localizing time_now to Nepali time zone. Third cell shows converting Nepali local time to German local time. Illustration by Creator.

4. Localizing timezone of pandas dataframe

Localizing naive dataframe index to UTC time zone. Illustration by Creator.

5. List of all possible time zone addresses

List of possible addresses that may be referred to for time zones using pytz module. Illustration by Creator.

6. Create latest dataframe for every city and convert UTC time zone to corresponding local time zone

Creating different dataframes out of every column of df. The time zones of recent dataframes are converted from UTC to respective time zone of the country/city.

7. Comparing the plots of solar irradiance data in numerous time zones

fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (20, 6))
fig.suptitle("Solar irradiance on October 1, 2020")

ax1.plot(df.loc["2020–10–01"])
ax1.set_title("Based on UTC time zone")
ax1.xaxis.set_ticks(ticks = df.loc["2020–10–01"].index[::4], labels = np.arange(0, 24, 4))

cities = df.columns.tolist()
handles = ax1.get_legend_handles_labels()[0]
ax1.legend(handles, labels = cities, loc = "upper right")
ax1.set_xlabel("Hour of day")
ax1.set_ylabel("W/m$^2$")

ax2.plot(df_chitwan.loc["2020–10–01"].values.tolist())
ax2.plot(df_newyork.loc["2020–10–01"].values.tolist())
ax2.plot(df_bonn.loc["2020–10–01"].values.tolist())
ax2.plot(df_sydney.loc["2020–10–01"].values.tolist())
ax2.xaxis.set_ticks(ticks = np.arange(0, 24, 4), labels = np.arange(0, 24, 4))

handles = ax2.get_legend_handles_labels()[0]
ax2.legend(handles, labels = cities)
ax2.set_title("Based on local time zone of every city/country")
ax2.set_xlabel("Hour of day")
ax2.set_ylabel("W/m$^2$")

plt.savefig("output/solar irradiance on october 1.jpeg",
dpi = 300)
plt.show()

Solar irradiance on October 1, 2020. Left: Based on UTC time zone. Right: Based on local time zone of every city/country. Illustration by Creator.
time_in_nepal =  timezone("Asia/Kathmandu”).localize(datetime.now()) 
german_timezone = timezone(“Europe/Berlin”)
time_in_germany = time_in_nepal.astimezone(german_timezone)
df_utc = df.tz_localize(tz = “UTC”)
df_nepal = df_utc.tz_convert(tz = “Asia/Kathmandu”)

LEAVE A REPLY

Please enter your comment!
Please enter your name here