-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c68758
commit e8d8827
Showing
2 changed files
with
34 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import requests | ||
import pandas as pd | ||
import io | ||
|
||
UCD_IPM_URL = "https://ipm.ucanr.edu/weather/weather-data-export.cfm" | ||
|
||
|
||
def get_weather_data(start_date, end_date, stnKey=281): | ||
url = f"{UCD_IPM_URL}?stnKey={stnKey}&startDate={start_date}&endDate={end_date}&unit=f&interval=1440&weatherApp=caWeatherApp&export=text" | ||
response = requests.get(url) | ||
assert response.status_code == 200 | ||
df = pd.read_csv(io.StringIO(response.text), skiprows=6, delimiter="\t") | ||
df.index = pd.to_datetime(df["Date"]) | ||
df.index.freq = pd.infer_freq(df.index) | ||
df.drop(columns=["Date"], inplace=True) | ||
return df | ||
|
||
|
||
# argparse for calling get_weather_data from command line | ||
if __name__ == "__main__": | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("start_date", help="Start date in format YYYY-MM-DD") | ||
parser.add_argument("end_date", help="End date in format YYYY-MM-DD") | ||
parser.add_argument("--stnKey", help="Station key", default=281) | ||
args = parser.parse_args() | ||
df = get_weather_data(args.start_date, args.end_date) | ||
df.to_csv(f"ucdipm_{args.stnKey}.csv") |