TempEst-NEXT 0.2.0__tar.gz → 0.2.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {tempest_next-0.2.0/src/TempEst_NEXT.egg-info → tempest_next-0.2.2}/PKG-INFO +5 -1
- {tempest_next-0.2.0 → tempest_next-0.2.2}/README.md +4 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/setup.cfg +3 -1
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/__init__.py +1 -1
- tempest_next-0.2.2/src/NEXT/__main__.py +15 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/coef_est.py +38 -38
- tempest_next-0.2.2/src/NEXT/interactive.py +303 -0
- tempest_next-0.2.2/src/NEXT/model.pickle +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2/src/TempEst_NEXT.egg-info}/PKG-INFO +5 -1
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/TempEst_NEXT.egg-info/SOURCES.txt +1 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/TempEst_NEXT.egg-info/entry_points.txt +3 -0
- tempest_next-0.2.0/src/NEXT/__main__.py +0 -10
- tempest_next-0.2.0/src/NEXT/interactive.py +0 -173
- {tempest_next-0.2.0 → tempest_next-0.2.2}/LICENSE +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/pyproject.toml +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/data.py +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/datatools.py +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/model_manager.py +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/reach_prep.py +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/NEXT/wforecast.py +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/TempEst_NEXT.egg-info/dependency_links.txt +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/TempEst_NEXT.egg-info/requires.txt +0 -0
- {tempest_next-0.2.0 → tempest_next-0.2.2}/src/TempEst_NEXT.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: TempEst-NEXT
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: TempEst-NEXT, Stream Temperature Estimation - Near-term Expected Temperatures: a statistical stream temperature model with coefficient estimation for ungaged watersheds
|
|
5
5
|
Home-page: https://rivertempest.org/next/readme.html
|
|
6
6
|
Author: Daniel Philippus
|
|
@@ -52,6 +52,10 @@ Install from PyPI: `pip install tempest-next`. The installed module is called N
|
|
|
52
52
|
|
|
53
53
|
Dependencies: pandas, numpy>=2, TempEst-NEWT, pygam>=0.10, pydaymet>=0.19, pynldas2, dataretrieval, geopandas, pynhd>=0.19, pygeohydro, py3dep, xarray-spatial, s3fs, zarr, cartopy, metpy, getgfs, dask
|
|
54
54
|
|
|
55
|
+
### GUI
|
|
56
|
+
|
|
57
|
+
`NEXTapp` or `NEXT --gui` launches a graphical interface.
|
|
58
|
+
|
|
55
59
|
### Instant Command Line Use
|
|
56
60
|
|
|
57
61
|
All of the below requirements are automated under certain assumptions and can be used directly through the command line or interactive use. After installing TempEst-NEXT as above, just open a terminal/command line and run: `tempest-next` (or `NEXT`, or, if those aren't on your path, `python -m NEXT`). This will interactively request all required information to automatically run the model for a given site (currently, pour point coordinates or a USGS gage ID), including forecasts. (Note that forecast data retrieval is rather slow, and the model does need at least a few years of data to predict well. Future updates will include the option to combine forecast data with climatology-adjusted historical estimates, which are much faster to retrieve.)
|
|
@@ -14,6 +14,10 @@ Install from PyPI: `pip install tempest-next`. The installed module is called N
|
|
|
14
14
|
|
|
15
15
|
Dependencies: pandas, numpy>=2, TempEst-NEWT, pygam>=0.10, pydaymet>=0.19, pynldas2, dataretrieval, geopandas, pynhd>=0.19, pygeohydro, py3dep, xarray-spatial, s3fs, zarr, cartopy, metpy, getgfs, dask
|
|
16
16
|
|
|
17
|
+
### GUI
|
|
18
|
+
|
|
19
|
+
`NEXTapp` or `NEXT --gui` launches a graphical interface.
|
|
20
|
+
|
|
17
21
|
### Instant Command Line Use
|
|
18
22
|
|
|
19
23
|
All of the below requirements are automated under certain assumptions and can be used directly through the command line or interactive use. After installing TempEst-NEXT as above, just open a terminal/command line and run: `tempest-next` (or `NEXT`, or, if those aren't on your path, `python -m NEXT`). This will interactively request all required information to automatically run the model for a given site (currently, pour point coordinates or a USGS gage ID), including forecasts. (Note that forecast data retrieval is rather slow, and the model does need at least a few years of data to predict well. Future updates will include the option to combine forecast data with climatology-adjusted historical estimates, which are much faster to retrieve.)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[metadata]
|
|
2
2
|
name = TempEst-NEXT
|
|
3
|
-
version = 0.2.
|
|
3
|
+
version = 0.2.2
|
|
4
4
|
author = Daniel Philippus
|
|
5
5
|
author_email = daniel@dphilippus.com
|
|
6
6
|
description = TempEst-NEXT, Stream Temperature Estimation - Near-term Expected Temperatures: a statistical stream temperature model with coefficient estimation for ungaged watersheds
|
|
@@ -53,6 +53,8 @@ where = src
|
|
|
53
53
|
console_scripts =
|
|
54
54
|
tempest-next = NEXT:cmdrun
|
|
55
55
|
NEXT = NEXT:cmdrun
|
|
56
|
+
gui_scripts =
|
|
57
|
+
NEXTapp = NEXT:gui
|
|
56
58
|
|
|
57
59
|
[egg_info]
|
|
58
60
|
tag_build =
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Created on Thu Jun 11 09:35:04 2026
|
|
5
|
+
|
|
6
|
+
@author: daniel
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from NEXT.interactive import cmdrun, gui
|
|
10
|
+
import sys
|
|
11
|
+
|
|
12
|
+
if len(sys.argv) > 1 and sys.argv[1] == "--gui":
|
|
13
|
+
gui()
|
|
14
|
+
else:
|
|
15
|
+
cmdrun()
|
|
@@ -56,52 +56,52 @@ def preprocess(data, allow_no_id=True):
|
|
|
56
56
|
return predictors
|
|
57
57
|
|
|
58
58
|
var_sets = [
|
|
59
|
-
{"name": "PCA0", "vars": ['tmax', 'prcp', 'vp', '
|
|
60
|
-
{"name": "PCA1", "vars": ['tmax', '
|
|
61
|
-
{"name": "PCA2", "vars": ['tmax', '
|
|
62
|
-
{"name": "PCA3", "vars": ['tmax', '
|
|
63
|
-
{"name": "PCA4", "vars": ['tmax', '
|
|
64
|
-
{"name": "PCA5", "vars": ['tmax', '
|
|
59
|
+
{"name": "PCA0", "vars": ['tmax', 'prcp', 'vp', 'ice_snow', 'water', 'snowfrac', 'vp_sd', 'prcp_index', 'tmax_phi', 'elev', 'frozen', 'tmax_index', 'canopy', 'ws_canopy'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + s(5) + s(6) + s(7) + s(8) + s(9) + te(10, 11) + te(12, 13), "lam": 30, "noise": 0.818, "scale": 1.19},
|
|
60
|
+
{"name": "PCA1", "vars": ['tmax', 'prcp', 'vp', 'vp_sd', 'prcp_index', 'tmax_phi', 'elev_min', 'frozen', 'tmax_index'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + s(5) + s(6) + te(7, 8), "lam": 3, "noise": 0.501, "scale": 1.13},
|
|
61
|
+
{"name": "PCA2", "vars": ['tmax', 'prcp', 'wetland', 'water', 'snowfrac', 'vp_sd', 'prcp_index', 'tmax_phi', 'elev', 'ws_canopy', 'frozen', 'tmax_index'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + s(5) + s(6) + s(7) + s(8) + s(9) + te(10, 11), "lam": 21, "noise": 0.622, "scale": 1.45},
|
|
62
|
+
{"name": "PCA3", "vars": ['tmax', 'ice_snow', 'snowfrac', 'tmax_index', 'elev_min', 'canopy', 'ws_canopy'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + te(5, 6), "lam": 4, "noise": 0.555, "scale": 1.52},
|
|
63
|
+
{"name": "PCA4", "vars": ['tmax', 'prcp', 'vp', 'area', 'wetland', 'snowfrac', 'vp_sd', 'tmax_index', 'elev', 'elev_min'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + s(5) + s(6) + s(7) + te(8, 9), "lam": 30, "noise": 0.601, "scale": 1.86},
|
|
64
|
+
{"name": "PCA5", "vars": ['tmax', 'vp', 'area', 'vp_sd', 'prcp_phi', 'tmax_phi', 'elev_min', 'ws_canopy', 'frozen', 'tmax_index'], "eq": s(0) + s(1) + s(2) + s(3) + s(4) + s(5) + s(6) + s(7) + te(8, 9), "lam": 3, "noise": 0.248, "scale": 1.28},
|
|
65
65
|
]
|
|
66
66
|
|
|
67
67
|
coef_names = ["PCA" + str(i) for i in range(9)]
|
|
68
68
|
col_order = ['Intercept', 'Amplitude', 'FallDay', 'WinterDay', 'SpringDay',
|
|
69
69
|
'SummerDay', 'SpringSummer', 'FallWinter', 'at_coef']
|
|
70
|
-
offset = pd.Series([
|
|
71
|
-
|
|
72
|
-
0.
|
|
70
|
+
offset = pd.Series([ 13.0056967 , 8.20795656, 326.31318681, 74.14044198,
|
|
71
|
+
156.8 , 214.73186813, 0.58916219, 1.84876426,
|
|
72
|
+
0.72359814],
|
|
73
73
|
index=col_order)
|
|
74
|
-
scale = pd.Series([ 3.
|
|
75
|
-
1. ,
|
|
74
|
+
scale = pd.Series([ 3.99160561, 3.43586619, 1. , 25.38558154, 1. ,
|
|
75
|
+
1. , 1.20308955, 1.52486907, 0.50466527],
|
|
76
76
|
index=col_order)
|
|
77
77
|
|
|
78
|
-
pca_components = np.array([[
|
|
79
|
-
|
|
80
|
-
-
|
|
81
|
-
[
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
[-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
[
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
[
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
[-
|
|
94
|
-
-1.
|
|
95
|
-
-
|
|
96
|
-
[
|
|
97
|
-
-
|
|
98
|
-
|
|
99
|
-
[
|
|
100
|
-
-1.
|
|
101
|
-
|
|
102
|
-
[-
|
|
103
|
-
|
|
104
|
-
|
|
78
|
+
pca_components = np.array([[ 3.41756961e-01, -2.61355435e-01, -5.55111512e-17,
|
|
79
|
+
4.20259492e-01, 1.73472348e-18, 3.12560562e-30,
|
|
80
|
+
-3.63303205e-01, 4.70312769e-01, 5.33942091e-01],
|
|
81
|
+
[ 4.71762540e-01, 6.30409106e-01, 1.11022302e-16,
|
|
82
|
+
1.92012911e-01, -0.00000000e+00, 2.16840434e-19,
|
|
83
|
+
-4.64511826e-01, -1.61613840e-01, -3.18221994e-01],
|
|
84
|
+
[-3.87150503e-01, 3.94110963e-01, -1.11022302e-16,
|
|
85
|
+
-4.69134836e-01, 0.00000000e+00, -2.29213584e-29,
|
|
86
|
+
-3.07138668e-01, 6.13781957e-01, 6.03414407e-02],
|
|
87
|
+
[ 6.89037889e-01, 2.42842441e-02, -1.38777878e-16,
|
|
88
|
+
-6.16784244e-01, -5.55111512e-17, 6.93889390e-18,
|
|
89
|
+
3.14530607e-01, 8.58458389e-02, 1.94718384e-01],
|
|
90
|
+
[-3.22430482e-02, 5.40764079e-01, 1.73472348e-17,
|
|
91
|
+
4.14137910e-01, -0.00000000e+00, 1.11022302e-16,
|
|
92
|
+
6.59558509e-01, 2.61545280e-01, 1.77766779e-01],
|
|
93
|
+
[-1.86933003e-01, 2.93153083e-01, 1.66533454e-16,
|
|
94
|
+
-1.20384780e-01, 0.00000000e+00, -1.73472348e-18,
|
|
95
|
+
-1.54785186e-01, -5.47889709e-01, 7.35175653e-01],
|
|
96
|
+
[ 2.34342554e-18, -1.11613983e-16, 5.53604827e-02,
|
|
97
|
+
-7.70407156e-17, 1.84730811e-02, 9.98295529e-01,
|
|
98
|
+
-1.31345009e-16, -5.62176760e-17, -3.77791731e-17],
|
|
99
|
+
[ 4.39134634e-17, -6.00026241e-17, 9.97848118e-01,
|
|
100
|
+
-1.55562006e-16, 3.41576381e-02, -5.59677458e-02,
|
|
101
|
+
3.23505183e-17, 7.93158315e-17, 5.34770085e-17],
|
|
102
|
+
[-2.19955299e-18, -1.44874609e-17, -3.51333141e-02,
|
|
103
|
+
3.71401416e-17, 9.99245716e-01, -1.65423459e-02,
|
|
104
|
+
1.85164759e-17, -1.66091718e-17, -4.95729384e-18]])
|
|
105
105
|
|
|
106
106
|
|
|
107
107
|
def build_model_from_data(tr_data):
|
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
# -*- coding: utf-8 -*-
|
|
3
|
+
"""
|
|
4
|
+
Created on Wed Jun 10 12:05:09 2026
|
|
5
|
+
|
|
6
|
+
@author: Daniel Philippus
|
|
7
|
+
|
|
8
|
+
This module supports interactive and command-line use.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import urllib.request as request
|
|
12
|
+
import os
|
|
13
|
+
from NEXT import NEXT
|
|
14
|
+
import pickle
|
|
15
|
+
from NEXT import data
|
|
16
|
+
import pandas as pd
|
|
17
|
+
import argparse
|
|
18
|
+
import sys
|
|
19
|
+
import tkinter as tk
|
|
20
|
+
from tkinter.filedialog import asksaveasfilename
|
|
21
|
+
import matplotlib.pyplot as plt
|
|
22
|
+
|
|
23
|
+
next_url = "https://github.com/mines-ciroh/TempEst-NEXT/raw/refs/heads/master/coefs.pickle"
|
|
24
|
+
|
|
25
|
+
def get_model(cache=None, url=next_url, overwrite=False):
|
|
26
|
+
"""
|
|
27
|
+
Retrieve and return a pre-trained model.
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
cache : str, optional
|
|
32
|
+
If provided, retrieve from or write to a local file. The default is None.
|
|
33
|
+
url : str, optional
|
|
34
|
+
URL from which to retrieve model file. The default is next_url.
|
|
35
|
+
overwrite : bool, optional
|
|
36
|
+
Overwrite cached model if it exists. The default is False.
|
|
37
|
+
|
|
38
|
+
Returns
|
|
39
|
+
-------
|
|
40
|
+
NEXT.NEXT
|
|
41
|
+
The pre-trained model.
|
|
42
|
+
"""
|
|
43
|
+
# Check if a stored model exists
|
|
44
|
+
if not overwrite and cache is not None:
|
|
45
|
+
if os.path.exists(cache):
|
|
46
|
+
return NEXT.from_pickle(cache)
|
|
47
|
+
# No stored model.
|
|
48
|
+
req = request.urlopen(url)
|
|
49
|
+
model = pickle.loads(req.read())
|
|
50
|
+
req.close()
|
|
51
|
+
if cache is not None:
|
|
52
|
+
if overwrite or not os.path.exists(cache):
|
|
53
|
+
model.to_pickle(cache)
|
|
54
|
+
return model
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def iget(arg, query, options=None, default=None):
|
|
58
|
+
"""
|
|
59
|
+
Interactively update parameter if needed.
|
|
60
|
+
|
|
61
|
+
Parameters
|
|
62
|
+
----------
|
|
63
|
+
arg : str
|
|
64
|
+
Pre-specified value. Will only ask if arg is None.
|
|
65
|
+
query : str
|
|
66
|
+
What to ask the user for.
|
|
67
|
+
options : [str], optional
|
|
68
|
+
List of options for categorical questions. The default is None. Will
|
|
69
|
+
be set to all lowercase.
|
|
70
|
+
default : str, optional
|
|
71
|
+
The default option if user enters nothing. The default is None. If
|
|
72
|
+
None, it will just complain and ask again.
|
|
73
|
+
|
|
74
|
+
Returns
|
|
75
|
+
-------
|
|
76
|
+
str
|
|
77
|
+
New argument value.
|
|
78
|
+
"""
|
|
79
|
+
oq = query
|
|
80
|
+
if arg is not None:
|
|
81
|
+
return arg
|
|
82
|
+
if options is not None:
|
|
83
|
+
options = [x.lower() for x in options]
|
|
84
|
+
query = query + f" ({', '.join(options)})"
|
|
85
|
+
if default is not None:
|
|
86
|
+
query = query + f" [default: {default}]"
|
|
87
|
+
arg = input(query + ': ')
|
|
88
|
+
if not arg.strip():
|
|
89
|
+
if default is not None:
|
|
90
|
+
return default
|
|
91
|
+
print("Error: a value must be provided.")
|
|
92
|
+
return iget(None, oq, options, default)
|
|
93
|
+
if options is not None:
|
|
94
|
+
if arg.lower() in options:
|
|
95
|
+
return arg
|
|
96
|
+
print(f"Error: invalid answer {arg}")
|
|
97
|
+
return iget(None, oq, options, default)
|
|
98
|
+
return arg.lower()
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
# Query, options, default. We separate these out as a dict so they are usable
|
|
102
|
+
# for multiple interaction modes.
|
|
103
|
+
descs = {
|
|
104
|
+
"site_type": ("Site type", list(data.geom_fns), None),
|
|
105
|
+
"site": ("Numeric site ID (no prefix; e.g., 10343500)", None, None),
|
|
106
|
+
"lat": ("Pour point latitude (Decimal degrees North, e.g. 40.623)", None, None),
|
|
107
|
+
"lon": ("Pour point longitude (Decimal degrees East, e.g. -120.1)", None, None),
|
|
108
|
+
"start": ("Start date (yyyy-mm-dd)", None, None),
|
|
109
|
+
"end": ("End date (yyyy-mm-dd)", None, None),
|
|
110
|
+
"weather": ("Weather data source", list(data.weather_fns), "gridmet"),
|
|
111
|
+
"model": ("Pre-trained model URL (optional)", None,
|
|
112
|
+
next_url),
|
|
113
|
+
"modpath": ("Pre-trained model cache file (optional)", None, "./model.pickle"),
|
|
114
|
+
"datafile": ("Location to cache input data (optional)", None, ""),
|
|
115
|
+
"output": ("Output file location", None, None)
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
|
|
119
|
+
def run(site_type=None, site=None, lat=None, lon=None, start=None, end=None,
|
|
120
|
+
weather=None, model=None, modpath=None, datafile=None, output=None):
|
|
121
|
+
# Validate inputs
|
|
122
|
+
site_type = iget(site_type, *descs['site_type'])
|
|
123
|
+
if site_type == 'coordinates':
|
|
124
|
+
lat = iget(lat, *descs['lat'])
|
|
125
|
+
lon = iget(lon, *descs['lon'])
|
|
126
|
+
site = lon + ":" + lat
|
|
127
|
+
else:
|
|
128
|
+
site = iget(site, *descs['site'])
|
|
129
|
+
start = iget(start, *descs['start'])
|
|
130
|
+
end = iget(end, *descs['end'])
|
|
131
|
+
weather = iget(weather, *descs['weather'])
|
|
132
|
+
model = iget(model, *descs['model'])
|
|
133
|
+
modpath = iget(modpath, *descs['modpath'])
|
|
134
|
+
datafile = iget(datafile, *descs['datafile'])
|
|
135
|
+
output = iget(output, *descs['output'])
|
|
136
|
+
# Actually run the model, retrieve data, etc
|
|
137
|
+
print("Retrieving model")
|
|
138
|
+
nxt = get_model(modpath, model, False)
|
|
139
|
+
print("Downloading prediction data")
|
|
140
|
+
if datafile and os.path.exists(datafile):
|
|
141
|
+
inpdata = pd.read_csv(datafile, parse_dates=['date'])
|
|
142
|
+
else:
|
|
143
|
+
inpdata = data.full_data(site, start, end, site_type=site_type,
|
|
144
|
+
weather=weather)
|
|
145
|
+
if datafile:
|
|
146
|
+
inpdata.to_csv(datafile)
|
|
147
|
+
print("Running model")
|
|
148
|
+
predictions = nxt.run(inpdata, reset=True)
|
|
149
|
+
print("Results preview")
|
|
150
|
+
print(predictions)
|
|
151
|
+
predictions.to_csv(output)
|
|
152
|
+
print(f"Done; results are in {output}")
|
|
153
|
+
return predictions
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
class GUI(tk.Frame):
|
|
159
|
+
browse = {k: None for k in ['modpath', 'datafile', 'output']}
|
|
160
|
+
def __init__(self, master):
|
|
161
|
+
super().__init__(master)
|
|
162
|
+
self.master = master
|
|
163
|
+
self.pack()
|
|
164
|
+
self.setup()
|
|
165
|
+
def setup(self):
|
|
166
|
+
self.entries = {}
|
|
167
|
+
self.values = {}
|
|
168
|
+
self.createEntries()
|
|
169
|
+
def createEntries(self):
|
|
170
|
+
self.entryFrame = tk.Frame(self)
|
|
171
|
+
tk.Label(self.entryFrame, text="Note: lat/lon are only needed if site_type=coordinates")
|
|
172
|
+
self.gridFrame = tk.Frame(self.entryFrame)
|
|
173
|
+
for (ix, (name, (desc, opts, default))) in enumerate(descs.items()):
|
|
174
|
+
tk.Label(self.gridFrame, text=f"[{name}] " + desc + ":").grid(row=ix)
|
|
175
|
+
if opts is None:
|
|
176
|
+
self.entries[name] = tk.StringVar(self.master)
|
|
177
|
+
entry = tk.Entry(self.gridFrame, width=30, textvariable=self.entries[name])
|
|
178
|
+
entry.grid(row=ix, column=1)
|
|
179
|
+
if name in self.values and self.values[name] is not None:
|
|
180
|
+
# Keep previous value
|
|
181
|
+
self.entries[name].set(self.values[name])
|
|
182
|
+
if name in self.browse:
|
|
183
|
+
self.browse[name] = ix
|
|
184
|
+
else:
|
|
185
|
+
self.entries[name] = tk.StringVar(self.master)
|
|
186
|
+
if name in self.values:
|
|
187
|
+
self.entries[name].set(self.values[name])
|
|
188
|
+
else:
|
|
189
|
+
self.entries[name].set(opts[0] if default is None else default)
|
|
190
|
+
tk.OptionMenu(self.gridFrame, self.entries[name], *opts).grid(row=ix, column=1)
|
|
191
|
+
# For some reason, building the browse buttons with a for loop messes
|
|
192
|
+
# up the targets. We have to hard-code them.
|
|
193
|
+
def browseButton(row, target):
|
|
194
|
+
(tk.Button(self.gridFrame, text="Browse",
|
|
195
|
+
command=lambda: target.set(asksaveasfilename(confirmoverwrite=False))).
|
|
196
|
+
grid(row=row, column=2))
|
|
197
|
+
for k in self.browse:
|
|
198
|
+
browseButton(self.browse[k], self.entries[k])
|
|
199
|
+
self.gridFrame.pack()
|
|
200
|
+
tk.Button(self.entryFrame, text="Run [Note: GUI will become unresponsive until model run is complete]",
|
|
201
|
+
command=self.run).pack(side="bottom")
|
|
202
|
+
self.entryFrame.pack()
|
|
203
|
+
def run(self):
|
|
204
|
+
for (k, v) in self.entries.items():
|
|
205
|
+
self.values[k] = res if (res := v.get()) else descs[k][2]
|
|
206
|
+
self.entryFrame.destroy()
|
|
207
|
+
self.validate()
|
|
208
|
+
if self.ok:
|
|
209
|
+
self.resultFrame = tk.Frame(self)
|
|
210
|
+
tk.Label(self.resultFrame,
|
|
211
|
+
text=f"Done. Results are in {self.values['output']}. Args:").pack()
|
|
212
|
+
varGrid = tk.Frame(self.resultFrame)
|
|
213
|
+
for (ix, (k, v)) in enumerate(self.values.items()):
|
|
214
|
+
tk.Label(varGrid, text=k + ": ").grid(row=ix, column=0)
|
|
215
|
+
tk.Label(varGrid, text=v).grid(row=ix, column=1)
|
|
216
|
+
varGrid.pack()
|
|
217
|
+
def reset():
|
|
218
|
+
self.resultFrame.destroy()
|
|
219
|
+
self.createEntries()
|
|
220
|
+
tk.Button(self.resultFrame, text="Return", command=reset).pack()
|
|
221
|
+
self.resultFrame.pack()
|
|
222
|
+
result = run(**self.values)
|
|
223
|
+
fig, axes = plt.subplots(1, 2, layout='compressed',
|
|
224
|
+
figsize=(8, 6))
|
|
225
|
+
result.plot(x='date', y='prediction', ax=axes[0])
|
|
226
|
+
axes[0].set_xlabel('Date')
|
|
227
|
+
axes[0].set_ylabel('Mean Stream Temperature [degC]')
|
|
228
|
+
axes[0].set_title('Full Predicted Timeseries')
|
|
229
|
+
result.iloc[-30:].plot(x='date', y='prediction', ax=axes[1])
|
|
230
|
+
axes[1].set_xlabel('Date')
|
|
231
|
+
axes[1].set_title('Last 30 Days')
|
|
232
|
+
plt.show()
|
|
233
|
+
def validate(self):
|
|
234
|
+
# Check that all required inputs are present.
|
|
235
|
+
bad = False
|
|
236
|
+
missing = []
|
|
237
|
+
# Things that are unconditionally required
|
|
238
|
+
simple_mandatory = ["output", "start", "end"]
|
|
239
|
+
for entry in simple_mandatory:
|
|
240
|
+
if self.values[entry] is None:
|
|
241
|
+
bad = True
|
|
242
|
+
missing.append(entry)
|
|
243
|
+
if (self.values['site_type'] != 'coordinates' and
|
|
244
|
+
self.values['site'] is None):
|
|
245
|
+
bad = True
|
|
246
|
+
missing.append('site')
|
|
247
|
+
if (self.values['site_type'] == 'coordinates' and
|
|
248
|
+
(self.values['lat'] is None or self.values['lon'] is None)):
|
|
249
|
+
bad = True
|
|
250
|
+
missing.append('lat')
|
|
251
|
+
missing.append('lon')
|
|
252
|
+
self.ok = not bad
|
|
253
|
+
if bad:
|
|
254
|
+
# If not bad, we don't do anything.
|
|
255
|
+
def reentry():
|
|
256
|
+
self.missingFrame.destroy()
|
|
257
|
+
self.createEntries()
|
|
258
|
+
self.missingFrame = tk.Frame(self)
|
|
259
|
+
tk.Label(self.missingFrame,
|
|
260
|
+
text="Error: the following values are missing. Please "
|
|
261
|
+
"fill in all required fields.").pack()
|
|
262
|
+
for entry in missing:
|
|
263
|
+
tk.Label(self.missingFrame, text=f"{entry} ({descs[entry][0]})").pack()
|
|
264
|
+
tk.Button(self.missingFrame, text="OK - Return", command=reentry).pack()
|
|
265
|
+
self.missingFrame.pack()
|
|
266
|
+
|
|
267
|
+
|
|
268
|
+
def parser():
|
|
269
|
+
ap = argparse.ArgumentParser(
|
|
270
|
+
description="TempEst-NEXT Command Line\n"
|
|
271
|
+
"Carry out full TempEst-NEXT model runs from the "
|
|
272
|
+
"terminal/command prompt with no code. "
|
|
273
|
+
"All arguments are optional, with missing values requested "
|
|
274
|
+
"interactively. Site ID and lat/lon are mutually exclusive ("
|
|
275
|
+
"lat/lon will be ignored if site ID is provided).\n"
|
|
276
|
+
"To launch a GUI instead, run NEXT --gui or NEXTapp")
|
|
277
|
+
for (name, (desc, opts, default)) in descs.items():
|
|
278
|
+
name = "--" + name
|
|
279
|
+
ap.add_argument(name, choices=opts, required=False, default=default,
|
|
280
|
+
help=desc)
|
|
281
|
+
return ap
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def cmdrun():
|
|
285
|
+
if len(sys.argv) > 1 and sys.argv[1] == "--gui":
|
|
286
|
+
gui()
|
|
287
|
+
else:
|
|
288
|
+
args = parser().parse_args()
|
|
289
|
+
run(**vars(args))
|
|
290
|
+
sys.exit()
|
|
291
|
+
|
|
292
|
+
def gui():
|
|
293
|
+
mainframe = tk.Frame(tk.Tk())
|
|
294
|
+
gui = GUI(mainframe)
|
|
295
|
+
mainframe.master.title("TempEst-NEXT GUI")
|
|
296
|
+
mainframe.pack()
|
|
297
|
+
gui.mainloop()
|
|
298
|
+
|
|
299
|
+
# if __name__ == "__main__":
|
|
300
|
+
# # Test args
|
|
301
|
+
# # --site_type usgs --site 10343500 --start 2024-01-01 --end 2025-12-31 --modpath cache.pickle --datafile sagehen.csv --output sagehen_pred.csv
|
|
302
|
+
# cmdrun()
|
|
303
|
+
|
|
Binary file
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: TempEst-NEXT
|
|
3
|
-
Version: 0.2.
|
|
3
|
+
Version: 0.2.2
|
|
4
4
|
Summary: TempEst-NEXT, Stream Temperature Estimation - Near-term Expected Temperatures: a statistical stream temperature model with coefficient estimation for ungaged watersheds
|
|
5
5
|
Home-page: https://rivertempest.org/next/readme.html
|
|
6
6
|
Author: Daniel Philippus
|
|
@@ -52,6 +52,10 @@ Install from PyPI: `pip install tempest-next`. The installed module is called N
|
|
|
52
52
|
|
|
53
53
|
Dependencies: pandas, numpy>=2, TempEst-NEWT, pygam>=0.10, pydaymet>=0.19, pynldas2, dataretrieval, geopandas, pynhd>=0.19, pygeohydro, py3dep, xarray-spatial, s3fs, zarr, cartopy, metpy, getgfs, dask
|
|
54
54
|
|
|
55
|
+
### GUI
|
|
56
|
+
|
|
57
|
+
`NEXTapp` or `NEXT --gui` launches a graphical interface.
|
|
58
|
+
|
|
55
59
|
### Instant Command Line Use
|
|
56
60
|
|
|
57
61
|
All of the below requirements are automated under certain assumptions and can be used directly through the command line or interactive use. After installing TempEst-NEXT as above, just open a terminal/command line and run: `tempest-next` (or `NEXT`, or, if those aren't on your path, `python -m NEXT`). This will interactively request all required information to automatically run the model for a given site (currently, pour point coordinates or a USGS gage ID), including forecasts. (Note that forecast data retrieval is rather slow, and the model does need at least a few years of data to predict well. Future updates will include the option to combine forecast data with climatology-adjusted historical estimates, which are much faster to retrieve.)
|
|
@@ -1,173 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env python3
|
|
2
|
-
# -*- coding: utf-8 -*-
|
|
3
|
-
"""
|
|
4
|
-
Created on Wed Jun 10 12:05:09 2026
|
|
5
|
-
|
|
6
|
-
@author: Daniel Philippus
|
|
7
|
-
|
|
8
|
-
This module supports interactive and command-line use.
|
|
9
|
-
"""
|
|
10
|
-
|
|
11
|
-
import urllib.request as request
|
|
12
|
-
import os
|
|
13
|
-
from NEXT import NEXT
|
|
14
|
-
import pickle
|
|
15
|
-
from NEXT import data
|
|
16
|
-
import pandas as pd
|
|
17
|
-
import argparse
|
|
18
|
-
|
|
19
|
-
next_url = "https://github.com/mines-ciroh/TempEst-NEXT/raw/refs/heads/master/coefs.pickle"
|
|
20
|
-
|
|
21
|
-
def get_model(cache=None, url=next_url, overwrite=False):
|
|
22
|
-
"""
|
|
23
|
-
Retrieve and return a pre-trained model.
|
|
24
|
-
|
|
25
|
-
Parameters
|
|
26
|
-
----------
|
|
27
|
-
cache : str, optional
|
|
28
|
-
If provided, retrieve from or write to a local file. The default is None.
|
|
29
|
-
url : str, optional
|
|
30
|
-
URL from which to retrieve model file. The default is next_url.
|
|
31
|
-
overwrite : bool, optional
|
|
32
|
-
Overwrite cached model if it exists. The default is False.
|
|
33
|
-
|
|
34
|
-
Returns
|
|
35
|
-
-------
|
|
36
|
-
NEXT.NEXT
|
|
37
|
-
The pre-trained model.
|
|
38
|
-
"""
|
|
39
|
-
# Check if a stored model exists
|
|
40
|
-
if not overwrite and cache is not None:
|
|
41
|
-
if os.path.exists(cache):
|
|
42
|
-
return NEXT.from_pickle(cache)
|
|
43
|
-
# No stored model.
|
|
44
|
-
with request.urlopen(url) as req:
|
|
45
|
-
model = pickle.loads(req.read())
|
|
46
|
-
if cache is not None:
|
|
47
|
-
if overwrite or os.path.exists(cache):
|
|
48
|
-
model.to_pickle(cache)
|
|
49
|
-
return model
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
def iget(arg, query, options=None, default=None):
|
|
53
|
-
"""
|
|
54
|
-
Interactively update parameter if needed.
|
|
55
|
-
|
|
56
|
-
Parameters
|
|
57
|
-
----------
|
|
58
|
-
arg : str
|
|
59
|
-
Pre-specified value. Will only ask if arg is None.
|
|
60
|
-
query : str
|
|
61
|
-
What to ask the user for.
|
|
62
|
-
options : [str], optional
|
|
63
|
-
List of options for categorical questions. The default is None. Will
|
|
64
|
-
be set to all lowercase.
|
|
65
|
-
default : str, optional
|
|
66
|
-
The default option if user enters nothing. The default is None. If
|
|
67
|
-
None, it will just complain and ask again.
|
|
68
|
-
|
|
69
|
-
Returns
|
|
70
|
-
-------
|
|
71
|
-
str
|
|
72
|
-
New argument value.
|
|
73
|
-
"""
|
|
74
|
-
oq = query
|
|
75
|
-
if arg is not None:
|
|
76
|
-
return arg
|
|
77
|
-
if options is not None:
|
|
78
|
-
options = [x.lower() for x in options]
|
|
79
|
-
query = query + f" ({', '.join(options)})"
|
|
80
|
-
if default is not None:
|
|
81
|
-
query = query + f" [default: {default}]"
|
|
82
|
-
arg = input(query + ': ')
|
|
83
|
-
if not arg.strip():
|
|
84
|
-
if default is not None:
|
|
85
|
-
return default
|
|
86
|
-
print("Error: a value must be provided.")
|
|
87
|
-
return iget(None, oq, options, default)
|
|
88
|
-
if options is not None:
|
|
89
|
-
if arg.lower() in options:
|
|
90
|
-
return arg
|
|
91
|
-
print(f"Error: invalid answer {arg}")
|
|
92
|
-
return iget(None, oq, options, default)
|
|
93
|
-
return arg.lower()
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
# Query, options, default. We separate these out as a dict so they are usable
|
|
97
|
-
# for multiple interaction modes.
|
|
98
|
-
descs = {
|
|
99
|
-
"site_type": ("Site type", list(data.geom_fns), None),
|
|
100
|
-
"site": ("Numeric site ID (no prefix; e.g., 10343500)", None, None),
|
|
101
|
-
"lat": ("Pour point latitude (Decimal degrees North, e.g. 40.623)", None, None),
|
|
102
|
-
"lon": ("Pour point longitude (Decimal degrees East, e.g. -120.1)", None, None),
|
|
103
|
-
"start": ("Start date (yyyy-mm-dd)", None, "2001-01-01"),
|
|
104
|
-
"end": ("End date (yyyy-mm-dd)", None, "2020-12-31"),
|
|
105
|
-
"weather": ("Weather data source", list(data.weather_fns), "gridmet"),
|
|
106
|
-
"model": ("Pre-trained model URL (optional)", None,
|
|
107
|
-
next_url),
|
|
108
|
-
"modpath": ("Pre-trained model cache file (optional)", None, "./model.pickle"),
|
|
109
|
-
"datafile": ("Location to cache input data (optional)", None, ""),
|
|
110
|
-
"output": ("Output file location", None, None)
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
def run(site_type=None, site=None, lat=None, lon=None, start=None, end=None,
|
|
115
|
-
weather=None, model=None, modpath=None, datafile=None, output=None):
|
|
116
|
-
# Validate inputs
|
|
117
|
-
site_type = iget(site_type, *descs['site_type'])
|
|
118
|
-
if site_type == 'coordinates':
|
|
119
|
-
lat = iget(lat, *descs['lat'])
|
|
120
|
-
lon = iget(lon, *descs['lon'])
|
|
121
|
-
site = lon + ":" + lat
|
|
122
|
-
else:
|
|
123
|
-
site = iget(site, *descs['site'])
|
|
124
|
-
start = iget(start, *descs['start'])
|
|
125
|
-
end = iget(end, *descs['end'])
|
|
126
|
-
weather = iget(weather, *descs['weather'])
|
|
127
|
-
model = iget(model, *descs['model'])
|
|
128
|
-
modpath = iget(modpath, *descs['modpath'])
|
|
129
|
-
datafile = iget(datafile, *descs['datafile'])
|
|
130
|
-
output = iget(output, *descs['output'])
|
|
131
|
-
# Actually run the model, retrieve data, etc
|
|
132
|
-
print("Retrieving model")
|
|
133
|
-
nxt = get_model(modpath, model, False)
|
|
134
|
-
print("Downloading prediction data")
|
|
135
|
-
if datafile and os.path.exists(datafile):
|
|
136
|
-
inpdata = pd.read_csv(datafile, parse_dates=['date'])
|
|
137
|
-
else:
|
|
138
|
-
inpdata = data.full_data(site, start, end, site_type=site_type,
|
|
139
|
-
weather=weather)
|
|
140
|
-
if datafile:
|
|
141
|
-
inpdata.to_csv(datafile)
|
|
142
|
-
print("Running model")
|
|
143
|
-
predictions = nxt.run(inpdata, reset=True)
|
|
144
|
-
print("Results preview")
|
|
145
|
-
print(predictions)
|
|
146
|
-
predictions.to_csv(output)
|
|
147
|
-
print(f"Done; results are in {output}")
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
def parser():
|
|
151
|
-
ap = argparse.ArgumentParser(
|
|
152
|
-
description="TempEst-NEXT Command Line\n"
|
|
153
|
-
"Carry out full TempEst-NEXT model runs from the "
|
|
154
|
-
"terminal/command prompt with no code. "
|
|
155
|
-
"All arguments are optional, with missing values requested "
|
|
156
|
-
"interactively. Site ID and lat/lon are mutually exclusive ("
|
|
157
|
-
"lat/lon will be ignored if site ID is provided).")
|
|
158
|
-
for (name, (desc, opts, default)) in descs.items():
|
|
159
|
-
name = "--" + name
|
|
160
|
-
ap.add_argument(name, choices=opts, required=False, default=default,
|
|
161
|
-
help=desc)
|
|
162
|
-
return ap
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
def cmdrun():
|
|
166
|
-
args = parser().parse_args()
|
|
167
|
-
run(**vars(args))
|
|
168
|
-
|
|
169
|
-
if __name__ == "__main__":
|
|
170
|
-
# Test args
|
|
171
|
-
# --site_type usgs --site 10343500 --start 2024-01-01 --end 2025-12-31 --modpath cache.pickle --datafile sagehen.csv --output sagehen_pred.csv
|
|
172
|
-
cmdrun()
|
|
173
|
-
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|