-
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.
Add scripts for downloading CIMIS and UC IPM weather data
- Loading branch information
1 parent
984f0de
commit 16343f7
Showing
2 changed files
with
58 additions
and
0 deletions.
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
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 |
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,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") | ||
# %% |