TempEst-NEXT 0.1.4__tar.gz → 0.1.6__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: TempEst-NEXT
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -22,7 +22,7 @@ Requires-Dist: pynldas2
22
22
  Requires-Dist: dataretrieval
23
23
  Requires-Dist: geopandas
24
24
  Requires-Dist: pynhd>=0.19
25
- Requires-Dist: pygeohydro
25
+ Requires-Dist: pygeohydro>=0.19
26
26
  Requires-Dist: py3dep
27
27
  Requires-Dist: xarray-spatial
28
28
  Requires-Dist: rioxarray
@@ -33,6 +33,7 @@ Requires-Dist: metpy
33
33
  Requires-Dist: getgfs
34
34
  Requires-Dist: dask
35
35
  Requires-Dist: cfgrib
36
+ Requires-Dist: dynamical-catalog
36
37
  Dynamic: license-file
37
38
 
38
39
  # TempEst-NEXT
@@ -71,7 +72,7 @@ Column details:
71
72
 
72
73
  Note: Daymet and NLDAS2 data retrieval are currently not working due to upstream issues. Gridmet (now set to default) works. The analysis in the paper was carried out using Daymet, so results may not be identical.
73
74
 
74
- For forecast data, past and present HRRR works (except, in historical data, for humidity, which must be retrieved through Gridmet), but has become very slow for unknown reasons. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster.
75
+ For forecast data, past and present HRRR works, but is relatively slow. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster. (HRRR is now quite a bit faster using the Dynamical.org catalog, but Gridmet is still the faster option.)
75
76
 
76
77
  For forecasting only (no long-term archives), GFS works well, but is rather memory-intensive, provided that your system supports `cfgrib` (which has experimental support for Windows, but mainly tagets Linux). Otherwise, it uses a native Python implementation which may work, but is slow and rather unreliable.
77
78
 
@@ -34,7 +34,7 @@ Column details:
34
34
 
35
35
  Note: Daymet and NLDAS2 data retrieval are currently not working due to upstream issues. Gridmet (now set to default) works. The analysis in the paper was carried out using Daymet, so results may not be identical.
36
36
 
37
- For forecast data, past and present HRRR works (except, in historical data, for humidity, which must be retrieved through Gridmet), but has become very slow for unknown reasons. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster.
37
+ For forecast data, past and present HRRR works, but is relatively slow. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster. (HRRR is now quite a bit faster using the Dynamical.org catalog, but Gridmet is still the faster option.)
38
38
 
39
39
  For forecasting only (no long-term archives), GFS works well, but is rather memory-intensive, provided that your system supports `cfgrib` (which has experimental support for Windows, but mainly tagets Linux). Otherwise, it uses a native Python implementation which may work, but is slow and rather unreliable.
40
40
 
@@ -1,6 +1,6 @@
1
1
  [metadata]
2
2
  name = TempEst-NEXT
3
- version = 0.1.4
3
+ version = 0.1.6
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
@@ -30,7 +30,7 @@ install_requires =
30
30
  dataretrieval
31
31
  geopandas
32
32
  pynhd>=0.19
33
- pygeohydro
33
+ pygeohydro>=0.19
34
34
  py3dep
35
35
  xarray-spatial
36
36
  rioxarray
@@ -41,6 +41,7 @@ install_requires =
41
41
  getgfs
42
42
  dask
43
43
  cfgrib
44
+ dynamical-catalog
44
45
 
45
46
  [options.packages.find]
46
47
  where = src
@@ -38,6 +38,7 @@ import shapely as shp
38
38
  import NEXT.wforecast as wfc
39
39
  import warnings
40
40
  import os
41
+ import rioxarray
41
42
 
42
43
  tid = "10343500"
43
44
  nldi_inst = [None]
@@ -482,7 +483,7 @@ hrrr_varnames = {
482
483
  # "SPFH": ("vp", lambda x: wfc.sphum_to_vp(x.mean())) # humidity, but not available at surface.
483
484
  }
484
485
 
485
- def weather_hrrr(geom, start, end):
486
+ def weather_hrrr_archive(geom, start, end):
486
487
  start = np.datetime64(start)
487
488
  end = np.datetime64(end)
488
489
  dates = pd.Series(np.arange(start, end + np.timedelta64(1, "D")))
@@ -494,6 +495,45 @@ def weather_hrrr(geom, start, end):
494
495
  for var in hrrr_varnames
495
496
  ], axis=1).reset_index()
496
497
 
498
+ dynhrrr_varnames = {
499
+ "temperature_2m": ("tmax", lambda x: x.max()),
500
+ "precipitation_surface": ("prcp", lambda x: x[1:].mean() * 3600 * 24), # mm/s --> mm/day
501
+ "dew_point_temperature_2m": ("vp", lambda x: wfc.dpt_to_vp(x.mean()))
502
+ }
503
+
504
+ def weather_hrrr(geom, start, end):
505
+ return wfc.weather_dynamical(geom, start, end,
506
+ "noaa-hrrr-forecast-48-hour",
507
+ dynhrrr_varnames,
508
+ projstr,
509
+ 1)
510
+
511
+ gefs_varnames = {
512
+ "temperature_2m": ("tmax", lambda x: x.max()),
513
+ "precipitation_surface": ("prcp", lambda x: x[1:].mean() * 3600 * 24), # mm/s --> mm/day
514
+ # Needs conversion, but we do that in the function since temp is relevant
515
+ "relative_humidity_2m": ("vp", lambda x: x.mean())
516
+ }
517
+
518
+ def weather_gefs(geom, start, end, _keep_going=True):
519
+ """ Uses the mean over ensemble members, for now. """
520
+ try:
521
+ result = wfc.weather_dynamical(geom, start, end,
522
+ "noaa-gefs-forecast-35-day",
523
+ gefs_varnames,
524
+ "4326",
525
+ 34,
526
+ ["latitude", "longitude", "ensemble_member"])
527
+ # RH to vapor pressure using saturated vapor pressure
528
+ result['vp'] = result['vp']/100 * wfc.dpt_to_vp(result['tmax'])
529
+ return result
530
+ except rioxarray.exceptions.NoDataInBounds:
531
+ # Try buffering for coarser datasets
532
+ return weather_gefs(
533
+ geom.to_crs(projstr).buffer(10000).to_crs(geom.crs),
534
+ start, end, False # only try once
535
+ )
536
+
497
537
 
498
538
  def weather_gfs(geom, start, end):
499
539
  """
@@ -511,7 +551,8 @@ def weather_gfs(geom, start, end):
511
551
  # HRRR can be used for prediction, but not to build coefficient estimation weather,
512
552
  # as HRRR-Zarr doesn't have srad.
513
553
  weather_fns = {"daymet": weather_daymet, "nldas": weather_nldas,
514
- "gfs": weather_gfs, "hrrr": weather_hrrr, "gridmet": weather_gridmet}
554
+ "gfs": weather_gfs, "hrrr": weather_hrrr, "gridmet": weather_gridmet,
555
+ "gefs": weather_gefs}
515
556
 
516
557
  # lcov requirements: forest, wetland, developed, ice_snow, water
517
558
  def lcov_nlcd(geom, start, end):
@@ -21,6 +21,8 @@ import warnings
21
21
  import getgfs as gfs
22
22
  import urllib.request as urq
23
23
  import os
24
+ import dynamical_catalog as dyn
25
+
24
26
 
25
27
 
26
28
  # Direct copy-paste from https://mesowest.utah.edu/html/hrrr/zarr_documentation/html/zarr_HowToDownload.html
@@ -124,6 +126,8 @@ def get_gfs_downloaded(basin, start, basepath='./gfs_cache/', var="t2m", new_nam
124
126
  start = pd.to_datetime(start).strftime("%Y%m%d")
125
127
  if not basepath.endswith('/'):
126
128
  basepath = basepath + '/'
129
+ if not os.path.exists(basepath):
130
+ os.mkdir(basepath)
127
131
  offset = 273 if var == "t2m" else 0
128
132
  if start == "today":
129
133
  start = pd.to_datetime(np.datetime64("today")).strftime("%Y%m%d")
@@ -197,6 +201,10 @@ def sphum_to_vp(q, p=1e5):
197
201
  # Convert specific humidity, in kg/kg, to vapor pressure.
198
202
  return q*p / (0.622 + 0.378 * q)
199
203
 
204
+ def dpt_to_vp(dpt):
205
+ # Convert dewpoint to vapor pressure
206
+ return 611 * np.exp(7.5 * dpt / (237.3 + dpt))
207
+
200
208
 
201
209
  def get_gfs(basin, date, varbs=["tmp2m", "spfh2m", "dswrfsfc", "pratesfc"],
202
210
  new_names=["tmax", "vp", "srad", "prcp"],
@@ -236,3 +244,58 @@ def hrrr_series(basin, dates, var, new_name, operator):
236
244
  raw = pd.concat([hrrr_areal_summary(basin, date, var, new_name, operator)
237
245
  for date in dates])
238
246
  return raw.groupby("date").first()
247
+
248
+
249
+ def weather_dynamical(geom, start, end, name, items, proj, maxlead,
250
+ dims=["x", "y"]):
251
+ """
252
+ Generic retriever for weather data from the dynamical.org catalog.
253
+
254
+ Parameters
255
+ ----------
256
+ geom : pd.GeoDataFrame or similar
257
+ Basin geometry
258
+ start : str | np.datetime64
259
+ Start time recognizable by numpy.
260
+ end : str | np.datetime64
261
+ End time recognizable by numpy.
262
+ name : str
263
+ Name of the dataset in dynamical.org (https://dynamical.org/catalog).
264
+ items : dict {str: (str, function)}
265
+ Variables to seek. Key is dynamical.org variable name. First value
266
+ is the output name, e.g., "tmax". Second value converts a NumPy timeseries
267
+ to a single final value (e.g., mean, max, unit conversions).
268
+ proj : str
269
+ Projection string.
270
+ maxlead : int
271
+ Maximum lead time in days.
272
+ dims : [str]
273
+ List of spatial (or other averaging) dimensions.
274
+ Returns
275
+ -------
276
+ pd.DataFrame
277
+ Date and all variables specified by "items".
278
+
279
+ """
280
+ dataset = dyn.open(name)
281
+ geom = geom.to_crs(proj).geometry # rio-friendly geometry in relevant proj
282
+ one = np.timedelta64(1, "D")
283
+ start = np.datetime64(start)
284
+ end = np.datetime64(end)
285
+ dates = pd.Series(np.arange(start, end + one))
286
+ today = np.datetime64("today")
287
+ # for each date, we want that day's HRRR run unless it is after today.
288
+ # Then we want the shortest applicable forecast.
289
+ if dates.max() > today + one * maxlead:
290
+ raise ValueError("Max lead time exceeded")
291
+ timings = [(
292
+ date, slice("0D", "0D")
293
+ ) for date in dates if date <= today] + [(
294
+ today, slice("0D", date - today))
295
+ for date in dates if date > today]
296
+ return pd.DataFrame([
297
+ {"date": init} | {
298
+ name: fn(dataset.sel(init_time=init, lead_time=lead)[k].
299
+ rio.clip(geom).mean(dims).compute().to_numpy())
300
+ for (k, (name, fn)) in items.items()}
301
+ for (init, lead) in timings])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: TempEst-NEXT
3
- Version: 0.1.4
3
+ Version: 0.1.6
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
@@ -22,7 +22,7 @@ Requires-Dist: pynldas2
22
22
  Requires-Dist: dataretrieval
23
23
  Requires-Dist: geopandas
24
24
  Requires-Dist: pynhd>=0.19
25
- Requires-Dist: pygeohydro
25
+ Requires-Dist: pygeohydro>=0.19
26
26
  Requires-Dist: py3dep
27
27
  Requires-Dist: xarray-spatial
28
28
  Requires-Dist: rioxarray
@@ -33,6 +33,7 @@ Requires-Dist: metpy
33
33
  Requires-Dist: getgfs
34
34
  Requires-Dist: dask
35
35
  Requires-Dist: cfgrib
36
+ Requires-Dist: dynamical-catalog
36
37
  Dynamic: license-file
37
38
 
38
39
  # TempEst-NEXT
@@ -71,7 +72,7 @@ Column details:
71
72
 
72
73
  Note: Daymet and NLDAS2 data retrieval are currently not working due to upstream issues. Gridmet (now set to default) works. The analysis in the paper was carried out using Daymet, so results may not be identical.
73
74
 
74
- For forecast data, past and present HRRR works (except, in historical data, for humidity, which must be retrieved through Gridmet), but has become very slow for unknown reasons. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster.
75
+ For forecast data, past and present HRRR works, but is relatively slow. It is quick enough for actual forecasting use, but for historical (coefficient estimation) data, I recommend using a shorter sample of HRRR data to adjust Gridmet climatology. Gridmet is far faster. (HRRR is now quite a bit faster using the Dynamical.org catalog, but Gridmet is still the faster option.)
75
76
 
76
77
  For forecasting only (no long-term archives), GFS works well, but is rather memory-intensive, provided that your system supports `cfgrib` (which has experimental support for Windows, but mainly tagets Linux). Otherwise, it uses a native Python implementation which may work, but is slow and rather unreliable.
77
78
 
@@ -8,7 +8,7 @@ pynldas2
8
8
  dataretrieval
9
9
  geopandas
10
10
  pynhd>=0.19
11
- pygeohydro
11
+ pygeohydro>=0.19
12
12
  py3dep
13
13
  xarray-spatial
14
14
  rioxarray
@@ -19,3 +19,4 @@ metpy
19
19
  getgfs
20
20
  dask
21
21
  cfgrib
22
+ dynamical-catalog
File without changes