- Name: Ben Hill
- Date: 3/16/2022
See noaa_sdk for details on package used
This project will roll out updated versions weekly until completion. Week 3 is the start of the actual code base.
from noaa_sdk import noaa
import sqlite3
import datetime
zipCode = "90808" # change to your postal code
country = "US"
# date-time format is yyyy-mm-ddThh:mm:ssZ, times are Zulu time (GMT)
# gets the most recent 14 days of data
today = datetime.datetime.now()
past = today - datetime.timedelta(days=14)
startDate = past.strftime("%Y-%m-%dT00:00:00Z")
endDate = today.strftime("%Y-%m-%dT23:59:59Z")
print("Preparing database...")
dbFile = "weather.db"
conn = sqlite3.connect(dbFile)
cur = conn.cursor()
dropTableCmd = "DROP TABLE IF EXISTS observations;"
cur.execute(dropTableCmd)
createTableCmd = """ CREATE TABLE IF NOT EXISTS observations (
timestamp TEXT NOT NULL PRIMARY KEY,
windSpeed REAL,
temperature REAL,
relativeHumidity REAL,
windDirection INTEGER,
barometricPressure INTEGER,
visibility INTEGER,
textDescription TEXT
) ; """
cur.execute(createTableCmd)
print("Database prepared")
print("Getting weather data...")
n = noaa.NOAA()
observations = n.get_observations(zipCode,country,startDate,endDate)
print("Inserting rows...")
insertCmd = """ INSERT INTO observations
(timestamp, windSpeed, temperature, relativeHumidity,
windDirection, barometricPressure, visibility, textDescription)
VALUES
(?, ?, ?, ?, ?, ?, ?, ?) """
count = 0
for obs in observations:
insertValues = (obs["timestamp"],
obs["windSpeed"]["value"],
obs["temperature"]["value"],
obs["relativeHumidity"]["value"],
obs["windDirection"]["value"],
obs["barometricPressure"]["value"],
obs["visibility"]["value"],
obs["textDescription"])
cur.execute(insertCmd, insertValues)
count += 1
if count > 0:
cur.execute("COMMIT;")
print(count, "rows inserted")
print("Database load complete!")