Skip to content

Commit

Permalink
Add scripts for downloading CIMIS and UC IPM weather data
Browse files Browse the repository at this point in the history
  • Loading branch information
dwr-psandhu committed Jan 30, 2025
1 parent 984f0de commit 16343f7
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
34 changes: 34 additions & 0 deletions test_download/ex_cimis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# %%
from dms_datastore import download_cimis
import pandas as pd

# %%
with open("cimis_password.secret", "r") as f:
password = f.read().strip()
# %%
cx = download_cimis.CIMIS(password=password)
# %%
dfcat = cx.get_stations_info()
dfcat["Connect"] = pd.to_datetime(dfcat["Connect"])
min_year = dfcat["Connect"].dt.year.min()
active_stations = list(dfcat[dfcat["Status"] == "Active"]["Station Number"])
dfcat.to_csv("cimis_stations.csv", index="Station Number")
# %%
current_year = pd.to_datetime("today").year
# %%
cx.download_all_hourly_zipped(min_year, current_year - 2)
# %%
for year in range(current_year - 2, current_year):
cx.download_hourly_unzipped(year, active_stations)
# %%
cx.download_current_year(active_stations)
# %%
import tqdm

for station in tqdm.tqdm(dfcat["Station Number"], total=len(dfcat)):
try:
dfs = cx.load_station(station)
dfs.to_csv(f"cimis_{station:03d}.csv", index="Date")
except Exception as e:
print(f"Error: {e}")
continue
24 changes: 24 additions & 0 deletions test_download/ex_ucdipm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# 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
# %%
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, searchStr="lodi"):
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


# %%
df = get_weather_data("2024-10-27", "2024-11-30")
df.to_csv("lodi_weather.csv")
# %%

0 comments on commit 16343f7

Please sign in to comment.