Skip to content

Instantly share code, notes, and snippets.

View rileyhales's full-sized avatar
💧
Focusing

Riley Hales rileyhales

💧
Focusing
View GitHub Profile
@rileyhales
rileyhales / geoglows-data-to-disc.py
Created April 14, 2024 18:51
Save GEOGLOWS datasets to disc
import geoglows
# get a list of river id numbers you are interested in downloading
river_ids = [710431167, ]
# Pandas DataFrames and Xarray Datasets each support several output formats
# Use the function for the file format you want and provide a file path for the output
# example using DataFrames - works for both forecasts and retrospective data
@rileyhales
rileyhales / geoglows-archive-forecast-records.py
Created April 14, 2024 18:25
Archive GEOGLOWS forecast records efficiently
import geoglows
# example river segments from the Dominican Republic
river_ids = [780019062, 780019446, 780020214, 780021751, 780014456, ]
# as dataframes
df = geoglows.data.forecast_stats(river_id=river_ids)
df = df[['flow_avg', ]].dropna()
df = df.loc[df.index.get_level_values('time') < df.index.get_level_values('time')[0] + pd.Timedelta(days=1)]
df.to_parquet('./forecast_records_subset.parquet')
@rileyhales
rileyhales / geoglows-data-for-many-rivers.py
Last active July 8, 2024 16:30
GEOGLOWS Snippets: Data for many rivers simultaneously
import geoglows
# get a list of river IDs that you want to get data for.
# Some options include using the streams GIS files, using web maps, or writing a list in a csv file
river_id_list = [760021611, 160064246, 710431167, 441057380, 430157411, 210406913, 621010293, 130747391, 640255644, ]
# get forecast data - returns a dataframe with a multi-index of dates and river numbers
forecast_data = geoglows.data.forecast_ensembles(river_id_list)
# get retrospective data = returns a dataframe with a datetime index and 1 columns per river ID
@rileyhales
rileyhales / list-rivers-in-watershed.py
Last active April 11, 2024 13:56
GEOGLOWS Tutorials Snippets
import pandas as pd
path_to_modal_metadata_table = './v2-model-table.parquet'
metadata_table = pd.read_parquet(path_to_modal_metadata_table)
river_of_interest = 780035587
terminal_link = metadata_table.loc[metadata_table['LINKNO'] == river_of_interest, 'TerminalLink'].values[0]
rivers_in_watershed = metadata_table.loc[metadata_table['TerminalLink'] == terminal_link, 'LINKNO'].values.flatten()
@rileyhales
rileyhales / geoglows-forecasts-tutorial.ipynb
Created April 8, 2024 23:38
geoglows-forecasts-tutorial.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rileyhales
rileyhales / geoglows-data-service-homework.ipynb
Last active April 4, 2024 19:33
geoglows-data-service-homework.ipynb
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
#!/bin/bash
set -x
HOME=/data/geoglows
WORKFLOW_DIR=$HOME/output/forecasts
# create date variables
TODAY=$(date +"%Y%m%d")
DATE_LIMIT=$(date -d "$TODAY - 46 days" +"%Y%m%d")
# delete old forecasts
import xarray as xr
import numpy as np
with xr.open_dataset('./2023era5/2023_11_era5_hourly.nc') as ds:
# find the time steps where the runoff is not nan when expver=1
a = ds.ro.sel(latitude=0, longitude=0, expver=1)
expver1_timesteps = a.time[~np.isnan(a)]
# find the time steps where the runoff is not nan when expver=5
b = ds.ro.sel(latitude=0, longitude=0, expver=5)
@rileyhales
rileyhales / nwm-from-aws-s3-zarr.ipynb
Last active September 26, 2023 19:58
Accessing NWM Retrospective Data from AWS S3 Zarr
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@rileyhales
rileyhales / hydrographs.py
Created September 21, 2023 21:07
Synthetic Hydrograph Comparisons
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
x = np.arange(0, np.pi * 12, 0.025)
q_insitu = 2 * np.sin(0.5 * x) + 4
q_constant = np.ones_like(x) * 4
q_biased_high = 2 * np.sin(0.5 * x) + 4 + 1.5
q_biased_low = 2 * np.sin(0.5 * x) + 4 - 1.5