Skip to content

Commit 2830ee2

Browse files
updating the package version
1 parent 65fadba commit 2830ee2

38 files changed

Lines changed: 11520 additions & 0 deletions

GeoGLOWS/GeoGLOWS.ipynb

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 1,
6+
"id": "4f8cad8a",
7+
"metadata": {},
8+
"outputs": [],
9+
"source": [
10+
"#Import Libraries\n",
11+
"import os\n",
12+
"import numpy as np\n",
13+
"import pandas as pd\n",
14+
"from pathlib import Path\n",
15+
"import s3fs\n",
16+
"import xarray"
17+
]
18+
},
19+
{
20+
"cell_type": "code",
21+
"execution_count": 8,
22+
"id": "5cda56c6",
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"#Hydrotable of the HUC8 with Spatial Joined GeoGLOWS Flowlines reaches\n",
27+
"hydrotable = Path('./hydrotable/fim45geoglows_12060202.csv')\n",
28+
"output_dir = Path('./streamflow')\n",
29+
"huc = '12060202'\n",
30+
"\n",
31+
"#start and end date\n",
32+
"start_date = '2016-01-01'\n",
33+
"end_date = '2016-12-30'\n",
34+
"value_time = '2016-10-15'"
35+
]
36+
},
37+
{
38+
"cell_type": "markdown",
39+
"id": "4bcde6bc",
40+
"metadata": {},
41+
"source": [
42+
"**Get all the Streamflow for all feature ID based on LINKNO within specified date**"
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 9,
48+
"id": "cf622efb",
49+
"metadata": {},
50+
"outputs": [],
51+
"source": [
52+
"def get_geoglowsdatafromS3():\n",
53+
" bucket_uri = 's3://geoglows-v2-retrospective/retrospective.zarr'\n",
54+
" region_name = 'us-west-2'\n",
55+
" s3 = s3fs.S3FileSystem(anon=True, client_kwargs=dict(region_name=region_name))\n",
56+
" s3store = s3fs.S3Map(root=bucket_uri, s3=s3, check=False)\n",
57+
" \n",
58+
" #All data\n",
59+
" ds = xarray.open_zarr(s3store)\n",
60+
" return ds\n",
61+
"\n",
62+
"def get_rivID(hydrotable):\n",
63+
" df = pd.read_csv(hydrotable)\n",
64+
" return df\n",
65+
"\n",
66+
"def GetGLOWSStreamflow(start_date, end_date, value_time, hydrotable, output_dir, huc, time_column='time'):\n",
67+
" # Get the retrospective dataset\n",
68+
" ds = get_geoglowsdatafromS3()\n",
69+
" hydro_df = pd.read_csv(hydrotable)\n",
70+
" \n",
71+
" # Map LINKNO to feature_id\n",
72+
" linkno_to_featureid = hydro_df.set_index('LINKNO')['feature_id'].to_dict()\n",
73+
" riv_ids = hydro_df['LINKNO'].tolist()\n",
74+
" filtered_ds = ds['Qout'].sel(rivid=riv_ids).to_dataframe()\n",
75+
" filtered_ds.reset_index(inplace=True)\n",
76+
" filtered_ds['time'] = pd.to_datetime(filtered_ds['time'])\n",
77+
" filtered_df = filtered_ds[(filtered_ds['time'] >= start_date) & (filtered_ds['time'] <= end_date)]\n",
78+
" \n",
79+
" # Map rivid (LINKNO) to feature_id\n",
80+
" filtered_df['feature_id'] = filtered_df['rivid'].map(linkno_to_featureid)\n",
81+
" output_df = filtered_df[['feature_id', 'Qout', 'time']]\n",
82+
" \n",
83+
" output_df.rename(columns={'Qout': 'discharge'}, inplace=True)\n",
84+
" \n",
85+
" # Export the filtered data to a CSV file\n",
86+
" out_dir = Path(output_dir) / 'combinedStreamflow'\n",
87+
" out_dir.mkdir(parents=True, exist_ok=True)\n",
88+
" output_file = out_dir / f'{huc}_{start_date}_{end_date}.csv'\n",
89+
" output_df.to_csv(output_file, index=False)\n",
90+
" \n",
91+
" #Filter based on value_time\n",
92+
" value_time_df = output_df[output_df['time'] == value_time]\n",
93+
" value_time_df = value_time_df[['feature_id', 'discharge']]\n",
94+
" \n",
95+
" # Export the value_time data to a separate CSV file\n",
96+
" value_timeSTR = pd.to_datetime(value_time).strftime('%Y%m%d')\n",
97+
" value_time_file = Path(output_dir) / f'{value_timeSTR}_{huc}.csv'\n",
98+
" value_time_df.to_csv(value_time_file, index=False)\n",
99+
" "
100+
]
101+
},
102+
{
103+
"cell_type": "code",
104+
"execution_count": 10,
105+
"id": "e4515e36",
106+
"metadata": {},
107+
"outputs": [
108+
{
109+
"name": "stderr",
110+
"output_type": "stream",
111+
"text": [
112+
"/var/folders/3g/sycd83_j0fb1l3sf8r5n5j000000gn/T/ipykernel_2243/122790853.py:29: SettingWithCopyWarning: \n",
113+
"A value is trying to be set on a copy of a slice from a DataFrame.\n",
114+
"Try using .loc[row_indexer,col_indexer] = value instead\n",
115+
"\n",
116+
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
117+
" filtered_df['feature_id'] = filtered_df['rivid'].map(linkno_to_featureid)\n",
118+
"/var/folders/3g/sycd83_j0fb1l3sf8r5n5j000000gn/T/ipykernel_2243/122790853.py:32: SettingWithCopyWarning: \n",
119+
"A value is trying to be set on a copy of a slice from a DataFrame\n",
120+
"\n",
121+
"See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n",
122+
" output_df.rename(columns={'Qout': 'discharge'}, inplace=True)\n"
123+
]
124+
}
125+
],
126+
"source": [
127+
"GetGLOWSStreamflow(start_date, end_date, value_time, hydrotable, output_dir, huc)"
128+
]
129+
}
130+
],
131+
"metadata": {
132+
"kernelspec": {
133+
"display_name": "OWPHANDFIM",
134+
"language": "python",
135+
"name": "python3"
136+
},
137+
"language_info": {
138+
"codemirror_mode": {
139+
"name": "ipython",
140+
"version": 3
141+
},
142+
"file_extension": ".py",
143+
"mimetype": "text/x-python",
144+
"name": "python",
145+
"nbconvert_exporter": "python",
146+
"pygments_lexer": "ipython3",
147+
"version": "3.10.14"
148+
}
149+
},
150+
"nbformat": 4,
151+
"nbformat_minor": 5
152+
}

0 commit comments

Comments
 (0)