-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataToCSV.py
56 lines (50 loc) · 1.65 KB
/
DataToCSV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/usr/bin/python3
from matplotlib import pyplot as plt
import urllib.request as web
import xml.etree.ElementTree as ElementTree
import time
import pandas as pd
import DirectoryTools
import WeatherData
from datetime import datetime
source = "https://w1.weather.gov/xml/current_obs/KLEX.xml"
savepath = "Data/mycsv.csv"
interval = 120
wxdata = WeatherData.WeatherDataStruct()
df = pd.DataFrame()
# download the weather data xml from the web
def download_source(src):
try:
data = web.urlopen(src)
except:
print('something went wrong while attempting to load the web data')
data = None
return data
# parse the xml data and return an object with the data
def parse_xml(data):
xmldata = ElementTree.parse(data)
temp = xmldata.find("temp_f").text
dew = xmldata.find("dewpoint_f").text
pressure = xmldata.find("pressure_mb").text
datatime = xmldata.find("observation_time_rfc822").text
wxdatapoint = WeatherData.WeatherDataPoint(temp, dew, pressure, datatime)
return wxdatapoint
# save the data to the csv
def save_data(data):
wxdatapoint = parse_xml(data)
wxdatapoint.show()
# append the data to the lists to be used for the csv
wxdata.appendWeatherData(wxdatapoint)
# Ensure the directory exists
DirectoryTools.EnsureDirectory(savepath)
# write the csv
df = pd.DataFrame.from_dict(wxdata.to_dict())
df.to_csv(savepath, index=False)
# main loop
while True:
# download and store the weather data
data = download_source(source)
#if there was an issue downloading the data
if data != None: save_data(data)
# wait for the next time to update
time.sleep(interval)