Skip to content

Instantly share code, notes, and snippets.

@adamhsparks
Created March 27, 2024 06:47
Show Gist options
  • Save adamhsparks/f13ac9046f5441c749e38d94517a979e to your computer and use it in GitHub Desktop.
Save adamhsparks/f13ac9046f5441c749e38d94517a979e to your computer and use it in GitHub Desktop.
Fetch historical weather data with Python using various packages
# Fetch historical weather data with Python using various packages
# use plotnine for graphing
from plotnine import *
import pandas as pd
import pathlib
# pynasapower
# Fetches NASA POWER data
from pynasapower.get_data import query_power
from pynasapower.geometry import point, bbox
import datetime
# Get weather data for the IRRI Zeigler Experiment Station
gpoint = point(121.255669, 14.16742, "EPSG:4326")
start = datetime.date(2000, 1, 1)
end = datetime.date(2001, 12, 31)
irri_data = query_power(geometry = gpoint,
start = start,
end = end,
to_file = False,
community = "ag",
parameters = ['T2M', 'RH2M', 'PRECTOTCORR'],
temporal_api = "daily",
spatial_api = "point",
format = "csv")
irri_data
# melt the data so we can visualise all three parameters
irri_data = pd.melt(irri_data, id_vars=['YEAR', 'DOY'],
value_vars=['T2M','RH2M','PRECTOTCORR'])
# use plotnine to visualise the data
(
ggplot(irri_data, aes("DOY", "value"))
+ geom_line(aes(colour = "factor(YEAR)"))
+ facet_wrap("variable")
)
# Fetch weather station data
# meteostat
from datetime import datetime
from meteostat import Point, Daily
from meteostat import Stations
stations = Stations()
stations = stations.nearby(lat=14.16742, lon=121.255669)
station = stations.fetch(10)
station
# Ninoy Aquino Airport, WMO 98429, appears to be the best choice for
# long-term weather data and is hourly
# Create point for IRRI (note that these are reversed from above)
# Set time period
start = datetime(2000, 1, 1)
end = datetime(2001, 12, 31)
# Get daily data for 2000 and 2001
naia_data = Daily('98429', start, end)
naia_data = naia_data.fetch()
# The naia_data are incomplete, missing many daily values highlighting
# one of the issues about good weather data from weather stations in
# many important locations
naia_data
# melt the naia_data to visualise it, note that there is no RH in this set
naia_data.index.name = 'date'
naia_data.reset_index(inplace=True)
naia_data['date'] = pd.to_datetime(naia_data['date'])
naia_data['doy'] = naia_data['date'].dt.dayofyear
naia_data['year'] = naia_data['date'].dt.year
naia_data = pd.melt(naia_data,
id_vars=['year','doy'],
value_vars=['tavg','prcp'])
# Use plotnine again to plot line chart including average, minimum and maximum temperature
(
ggplot(naia_data, aes("doy", "value"))
+ geom_line(aes(colour = "factor(year)"))
+ facet_wrap("variable")
)
# tsgettoolbox fetches many weather data sets including GHCN and GSOD
# I ended up using GSODR to identify stations closest to IRRI to specify
# them for download here as there was no clear way to do that with this
# module.
# This module doesn't appear to work with the modified example from the
# README
from tsgettoolbox import tsgettoolbox
df = tsgettoolbox.ncei_gsod(sites="02329500", startDT="2000-01-01")
# gsodpy
# This module doesn't appear to work with the modified example from the
# README
import gsodpy
gdown.get_data(directory="noaa_gsod")
print(getmembers(gsodpy), isfunction)
# open-meteo
# I couldn't get any of the clients to work with this API properly to
# fetch weather data.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment