Skip to content

Commit 16343f7

Browse files
committed
Add scripts for downloading CIMIS and UC IPM weather data
1 parent 984f0de commit 16343f7

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

Diff for: test_download/ex_cimis.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# %%
2+
from dms_datastore import download_cimis
3+
import pandas as pd
4+
5+
# %%
6+
with open("cimis_password.secret", "r") as f:
7+
password = f.read().strip()
8+
# %%
9+
cx = download_cimis.CIMIS(password=password)
10+
# %%
11+
dfcat = cx.get_stations_info()
12+
dfcat["Connect"] = pd.to_datetime(dfcat["Connect"])
13+
min_year = dfcat["Connect"].dt.year.min()
14+
active_stations = list(dfcat[dfcat["Status"] == "Active"]["Station Number"])
15+
dfcat.to_csv("cimis_stations.csv", index="Station Number")
16+
# %%
17+
current_year = pd.to_datetime("today").year
18+
# %%
19+
cx.download_all_hourly_zipped(min_year, current_year - 2)
20+
# %%
21+
for year in range(current_year - 2, current_year):
22+
cx.download_hourly_unzipped(year, active_stations)
23+
# %%
24+
cx.download_current_year(active_stations)
25+
# %%
26+
import tqdm
27+
28+
for station in tqdm.tqdm(dfcat["Station Number"], total=len(dfcat)):
29+
try:
30+
dfs = cx.load_station(station)
31+
dfs.to_csv(f"cimis_{station:03d}.csv", index="Date")
32+
except Exception as e:
33+
print(f"Error: {e}")
34+
continue

Diff for: test_download/ex_ucdipm.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# https://ipm.ucanr.edu/weather/weather-data-export.cfm?stnKey=281&searchStr=lodi&startDate=2024-10-27&endDate=2024-11-25&unit=f&interval=1440&weatherApp=caWeatherApp&export=text
2+
# %%
3+
import requests
4+
import pandas as pd
5+
import io
6+
7+
UCD_IPM_URL = "https://ipm.ucanr.edu/weather/weather-data-export.cfm"
8+
9+
10+
def get_weather_data(start_date, end_date, stnKey=281, searchStr="lodi"):
11+
url = f"{UCD_IPM_URL}?stnKey={stnKey}&startDate={start_date}&endDate={end_date}&unit=f&interval=1440&weatherApp=caWeatherApp&export=text"
12+
response = requests.get(url)
13+
assert response.status_code == 200
14+
df = pd.read_csv(io.StringIO(response.text), skiprows=6, delimiter="\t")
15+
df.index = pd.to_datetime(df["Date"])
16+
df.index.freq = pd.infer_freq(df.index)
17+
df.drop(columns=["Date"], inplace=True)
18+
return df
19+
20+
21+
# %%
22+
df = get_weather_data("2024-10-27", "2024-11-30")
23+
df.to_csv("lodi_weather.csv")
24+
# %%

0 commit comments

Comments
 (0)