USGS Water — API reference#
USGS NWIS / Water Data source subpackage — earthlens.usgs_water.
Background, usage, available data, and credentials are covered under the other
pages in this section; this page is the rendered API.
earthlens.usgs_water
#
USGS NWIS / Water Data backend.
Thin wrapper over the official dataretrieval SDK that pulls per-site
water observations from the U.S. Geological Survey's National Water
Information System — stream gauges, groundwater wells, and
water-quality sites across the United States — and returns them as a
long-format :class:pandas.DataFrame.
This is a tabular backend: the result is a table of per-site
observations, not a gridded array, so :data:USGSWater.OUTPUT_KIND is
"tabular" and the :class:earthlens.earthlens.EarthLens facade
rejects an aggregate= argument for it (use service="statistics" for
a server-side temporal rollup instead).
Request shape: variables is a list[str] of NWIS parameter codes
or friendly names — variables=["00060"], variables=["discharge",
"gage_height"] — resolved to 5-digit codes via the bundled catalog.
The NWIS / Water Data service plane is selected by service= (default
"daily"); the modern / legacy endpoint by api= (default "auto").
Authentication is an optional Personal Access Token.
Public surface (re-exported from this package):
- :class:
USGSWater— the backend; instantiate with a date range, a bbox (orsites=), andvariables=[code_or_name, ...], then call :meth:USGSWater.download. - :class:
Catalog— pydantic-backed loader for the bundledusgs_water_data_catalog.yamlparameter-code table. - :class:
Parameter— one parameter code's catalog row (code,name,units,group,services). - :class:
UsgsWaterAuth—AbstractAuthimplementation resolving the optionalAPI_USGS_PAT. - :class:
UsgsWaterCredentials— frozen pydantic value object the auth class binds to (the optional token). - :data:
CATALOG_PATH— path to the bundled parameter YAML.
Catalog
#
Bases: AbstractCatalog
Parameter-code catalog for the USGS Water backend.
Reads the bundled usgs_water_data_catalog.yaml (shipped as
package data) and exposes its parameters: block as a map of
:class:Parameter rows keyed by friendly name. Instantiate with no
arguments (Catalog()). Resolve a name or raw code with
:meth:resolve, or a single row with :meth:get_parameter.
Attributes:
| Name | Type | Description |
|---|---|---|
parameters |
dict[str, Parameter]
|
Map from the friendly parameter name to its
:class: |
Examples:
- Resolve a friendly name and a raw code:
- An unknown but close name raises with a did-you-mean hint:
Source code in src/earthlens/usgs_water/catalog.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
available_parameters
property
#
The sorted list of curated friendly parameter names.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Every curated catalog key, sorted. |
Examples:
- List the curated names and check membership:
parameters
property
#
The parameter map — alias for the base :attr:datasets field.
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
dict[str, Parameter]: The same mapping stored in
:attr: |
Examples:
- The alias and the base field are the same object:
get_catalog()
#
Return the parameter map (satisfies the abstract contract).
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
dict[str, Parameter]: Same object as :attr: |
Source code in src/earthlens/usgs_water/catalog.py
get_parameter(name)
#
Resolve a friendly name to its :class:Parameter row.
Thin wrapper over the inherited :meth:get_dataset, which raises
a ValueError with a did-you-mean hint on an unknown name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A friendly parameter name ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Parameter |
Parameter
|
The matching catalog row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Resolve a row and read its fields:
Source code in src/earthlens/usgs_water/catalog.py
load(catalog_path=None)
classmethod
#
Read the USGS Water parameter catalog from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog_path
|
Path | None
|
Path to the catalog YAML. Defaults to the
module-level :data: |
None
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A fully-populated :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file has no |
Source code in src/earthlens/usgs_water/catalog.py
model_post_init(__context)
#
Auto-load the bundled catalog when no parameters were supplied.
Catalog() with no args reads :data:CATALOG_PATH; passing
parameters=... (or datasets=...) skips the disk read.
Raises:
| Type | Description |
|---|---|
ValueError
|
Propagated from :meth: |
Source code in src/earthlens/usgs_water/catalog.py
resolve(code_or_name)
#
Resolve a friendly name or a raw 5-digit code to a code.
A raw 5-digit code ("00060") passes through unmapped — the
full USGS code table is far larger than the curated catalog, so
any valid code is accepted directly. A non-code string is
looked up as a friendly name via :meth:get_parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_or_name
|
str
|
A friendly name ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The 5-digit parameter code. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/earthlens/usgs_water/catalog.py
Parameter
#
Bases: BaseModel
One NWIS parameter code's catalog row.
The user-facing name is the parent key in
:attr:Catalog.parameters and is also stored on the row as
:attr:name so a resolved :class:Parameter is self-describing.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
The 5-digit NWIS parameter code ( |
name |
str
|
Human-readable label ( |
units |
str
|
The reporting units ( |
group |
ParameterGroup
|
Coarse USGS classification ( |
services |
list[str]
|
The :data: |
Examples:
- Build a row directly:
Source code in src/earthlens/usgs_water/catalog.py
USGSWater
#
Bases: AbstractDataSource
USGS NWIS / Water Data backend (long-format tabular output).
Fetches per-site water observations for a bbox / explicit sites /
date window / parameter-code list through the same download()
shape every other earthlens backend uses, and returns a
long-format :class:pandas.DataFrame (one row per observation).
The query is a search/fetch split: :meth:_search enumerates the
monitoring locations to pull, and :meth:_fetch pulls each
service's observations and normalises them to one tidy long schema.
The service= argument selects the NWIS / Water Data plane (see
:data:SERVICES); api= selects the modern / legacy endpoint with
a 429 auto-fallback (see the module docstring). Authentication is
an optional Personal Access Token.
Attributes:
| Name | Type | Description |
|---|---|---|
OUTPUT_KIND |
OutputKind
|
|
Source code in src/earthlens/usgs_water/backend.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
__init__(start, end, variables, lat_lim, lon_lim, temporal_resolution='daily', path='', fmt='%Y-%m-%d', api_token=None, service='daily', sites=None, api='auto', output_format='csv', stat_type='daily', limit=None)
#
Initialise a USGS Water backend instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str
|
Inclusive start of the window, parsed with |
required |
end
|
str
|
Inclusive end of the window. |
required |
variables
|
list[str]
|
List of NWIS parameter codes or friendly names
( |
required |
lat_lim
|
list[float]
|
|
required |
lon_lim
|
list[float]
|
|
required |
temporal_resolution
|
str
|
Convenience alias mapped onto
|
'daily'
|
path
|
Path | str
|
Output directory for the written table. |
''
|
fmt
|
str
|
|
'%Y-%m-%d'
|
api_token
|
str | None
|
Optional USGS Personal Access Token; falls back
to the |
None
|
service
|
str
|
The NWIS / Water Data plane to query — one of
:data: |
'daily'
|
sites
|
list[str] | str | None
|
Explicit USGS site number(s) to query, bypassing the
bbox site discovery. Required for the services that
have no spatial (bbox) filter — |
None
|
api
|
ApiFlavour
|
Endpoint selector — |
'auto'
|
output_format
|
OutputFormat
|
On-disk format — |
'csv'
|
stat_type
|
str
|
For |
'daily'
|
limit
|
int | None
|
Optional cap on the rows pulled per request (passed
through to the modern endpoint's |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
TypeError
|
When |
Source code in src/earthlens/usgs_water/backend.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
download(progress_bar=True, aggregate=None)
#
Fetch the selected service, write the table, and return it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress_bar
|
bool
|
Accepted for signature parity with the other
backends. USGS Water issues one bulk |
True
|
aggregate
|
AggregationConfig | None
|
Must be |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The long-format observation table for the
selected |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
ValueError
|
If the selected |
Source code in src/earthlens/usgs_water/backend.py
UsgsWaterAuth
#
Bases: AbstractAuth[UsgsWaterCredentials]
Resolve and hold the optional USGS Personal Access Token.
Implements the :class:earthlens.base.AbstractAuth contract for an
optional-secret backend. Construction does not touch the
environment; :meth:configure performs the resolution and is
idempotent. After configure(), the token (or None for
anonymous) is available via the :attr:token property, and — when
a token was resolved — it is exported to the API_USGS_PAT
environment variable so the dataretrieval SDK picks it up.
The class is a context manager (inherited from
:class:AbstractAuth): with UsgsWaterAuth(creds) as auth: ...
calls configure() on enter and the default no-op close() on
exit.
Attributes:
| Name | Type | Description |
|---|---|---|
_creds |
The :class: |
Examples:
- Resolve an explicit token:
- With no token,
configure()succeeds and stays anonymous:
Source code in src/earthlens/usgs_water/auth.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
token
property
#
The resolved PAT, or None for anonymous access.
Valid after :meth:configure; returns None both before
configuration and when no token was available (anonymous).
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The USGS PAT string, or |
__init__(credentials)
#
Store credentials; does not resolve the token yet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
UsgsWaterCredentials
|
The :class: |
required |
Source code in src/earthlens/usgs_water/auth.py
configure()
#
Resolve the optional token and export it to the environment.
Idempotent — short-circuits when :meth:is_authenticated
already returns True. Resolves the token in this order: the
explicit api_token on the credentials, then the
API_USGS_PAT environment variable. When a token is found it
is written back to API_USGS_PAT (the channel dataretrieval
reads) and :meth:is_authenticated flips to True. When none
is found this is a no-op — the request runs anonymously
(rate-limited) and no error is raised.
Source code in src/earthlens/usgs_water/auth.py
is_authenticated()
#
Return True when a token was resolved (token-backed mode).
Cheap predicate — does not call the network. False is a
legitimate, non-error state meaning "anonymous access".
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in src/earthlens/usgs_water/auth.py
UsgsWaterCredentials
#
Bases: BaseModel
Frozen value object holding the optional USGS PAT.
The token is optional at construction time: None means "resolve
from the API_USGS_PAT environment variable at
:meth:UsgsWaterAuth.configure time, and if that is also unset,
run anonymously".
Attributes:
| Name | Type | Description |
|---|---|---|
api_token |
SecretStr | None
|
The USGS Personal Access Token, stored as a
:class: |
Examples:
- Build from an explicit token; the secret is hidden in
repr: - The token is optional — anonymous access is the default:
Source code in src/earthlens/usgs_water/auth.py
clear_catalog_cache()
#
Empty the module-level catalog parse cache.
Useful in tests that rewrite the catalog on disk and want to force
a re-parse. Production callers do not need this — the cache key
includes the file's st_mtime_ns, so any real edit invalidates the
entry on its own.
Source code in src/earthlens/usgs_water/catalog.py
earthlens.usgs_water.backend
#
Backend that fetches USGS water data from NWIS / the USGS Water Data API.
USGSWater(AbstractDataSource) wraps the official dataretrieval
SDK to pull time-series and discrete water observations from the U.S.
Geological Survey's National Water Information System — ~10,000 active
stream gauges and many more groundwater / water-quality sites across the
United States. A request is a bbox (or explicit sites=) + a time
window + a list of NWIS parameter codes (["00060"] discharge,
["00065"] gage height, …); the backend returns a per-site time-series
as a long-format :class:pandas.DataFrame, so OUTPUT_KIND = "tabular"
and the :class:earthlens.earthlens.EarthLens facade rejects an
aggregate= argument (use the server-side service="statistics"
rollup instead).
The full NWIS / Water Data service surface is selectable via a
service= keyword argument (default "daily"): daily,
instantaneous, samples, statistics, gwlevels,
field-measurements, peaks, ratings, and sites. The USGS is
mid-migration from the legacy waterservices.usgs.gov endpoint (the
dataretrieval.nwis module) to the modern api.waterdata.usgs.gov
endpoint (the dataretrieval.waterdata module). The api= keyword
selects which:
"auto"(default) — try the modern endpoint, but because it rate-limits anonymous access aggressively (HTTP 429), transparently fall back to the legacy endpoint on a 429 when no token is set."waterdata"— force the modern endpoint (a 429 surfaces as an error)."legacy"— force the legacy endpoint.
Authentication is an optional Personal Access Token (the
API_USGS_PAT env var or the api_token= argument); anonymous access
works at lower rate limits. See :class:earthlens.usgs_water.auth.
USGSWater
#
Bases: AbstractDataSource
USGS NWIS / Water Data backend (long-format tabular output).
Fetches per-site water observations for a bbox / explicit sites /
date window / parameter-code list through the same download()
shape every other earthlens backend uses, and returns a
long-format :class:pandas.DataFrame (one row per observation).
The query is a search/fetch split: :meth:_search enumerates the
monitoring locations to pull, and :meth:_fetch pulls each
service's observations and normalises them to one tidy long schema.
The service= argument selects the NWIS / Water Data plane (see
:data:SERVICES); api= selects the modern / legacy endpoint with
a 429 auto-fallback (see the module docstring). Authentication is
an optional Personal Access Token.
Attributes:
| Name | Type | Description |
|---|---|---|
OUTPUT_KIND |
OutputKind
|
|
Source code in src/earthlens/usgs_water/backend.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
__init__(start, end, variables, lat_lim, lon_lim, temporal_resolution='daily', path='', fmt='%Y-%m-%d', api_token=None, service='daily', sites=None, api='auto', output_format='csv', stat_type='daily', limit=None)
#
Initialise a USGS Water backend instance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
start
|
str
|
Inclusive start of the window, parsed with |
required |
end
|
str
|
Inclusive end of the window. |
required |
variables
|
list[str]
|
List of NWIS parameter codes or friendly names
( |
required |
lat_lim
|
list[float]
|
|
required |
lon_lim
|
list[float]
|
|
required |
temporal_resolution
|
str
|
Convenience alias mapped onto
|
'daily'
|
path
|
Path | str
|
Output directory for the written table. |
''
|
fmt
|
str
|
|
'%Y-%m-%d'
|
api_token
|
str | None
|
Optional USGS Personal Access Token; falls back
to the |
None
|
service
|
str
|
The NWIS / Water Data plane to query — one of
:data: |
'daily'
|
sites
|
list[str] | str | None
|
Explicit USGS site number(s) to query, bypassing the
bbox site discovery. Required for the services that
have no spatial (bbox) filter — |
None
|
api
|
ApiFlavour
|
Endpoint selector — |
'auto'
|
output_format
|
OutputFormat
|
On-disk format — |
'csv'
|
stat_type
|
str
|
For |
'daily'
|
limit
|
int | None
|
Optional cap on the rows pulled per request (passed
through to the modern endpoint's |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
When |
TypeError
|
When |
Source code in src/earthlens/usgs_water/backend.py
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
download(progress_bar=True, aggregate=None)
#
Fetch the selected service, write the table, and return it.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
progress_bar
|
bool
|
Accepted for signature parity with the other
backends. USGS Water issues one bulk |
True
|
aggregate
|
AggregationConfig | None
|
Must be |
None
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
pd.DataFrame: The long-format observation table for the
selected |
Raises:
| Type | Description |
|---|---|
NotImplementedError
|
If |
ValueError
|
If the selected |
Source code in src/earthlens/usgs_water/backend.py
earthlens.usgs_water.catalog
#
Parameter-code catalog for the USGS Water backend.
USGS NWIS filters its queries by 5-digit parameter codes (00060
discharge, 00065 gage height, 00010 water temperature, …), but
users think in names. This module is the name-to-code bridge plus light
metadata (units, display name, parameter group, and which services the
code is valid on).
:class:Catalog is a thin :class:earthlens.base.AbstractCatalog
subclass that loads the bundled usgs_water_data_catalog.yaml and
exposes each row as a :class:Parameter. Resolve a single key with
:meth:Catalog.resolve — which accepts either a friendly name
("discharge") or a raw 5-digit code ("00060", passed through
unmapped), with a did-you-mean hint on an unknown name. The full ~25k
USGS parameter-code table is not hand-curated here; the curated rows
cover the common codes, and the refresh tool builds an informational
available_parameters index from the live USGS reference table.
:data:CATALOG_PATH is the path to the bundled YAML.
Catalog
#
Bases: AbstractCatalog
Parameter-code catalog for the USGS Water backend.
Reads the bundled usgs_water_data_catalog.yaml (shipped as
package data) and exposes its parameters: block as a map of
:class:Parameter rows keyed by friendly name. Instantiate with no
arguments (Catalog()). Resolve a name or raw code with
:meth:resolve, or a single row with :meth:get_parameter.
Attributes:
| Name | Type | Description |
|---|---|---|
parameters |
dict[str, Parameter]
|
Map from the friendly parameter name to its
:class: |
Examples:
- Resolve a friendly name and a raw code:
- An unknown but close name raises with a did-you-mean hint:
Source code in src/earthlens/usgs_water/catalog.py
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 | |
available_parameters
property
#
The sorted list of curated friendly parameter names.
Returns:
| Type | Description |
|---|---|
list[str]
|
list[str]: Every curated catalog key, sorted. |
Examples:
- List the curated names and check membership:
parameters
property
#
The parameter map — alias for the base :attr:datasets field.
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
dict[str, Parameter]: The same mapping stored in
:attr: |
Examples:
- The alias and the base field are the same object:
get_catalog()
#
Return the parameter map (satisfies the abstract contract).
Returns:
| Type | Description |
|---|---|
dict[str, Parameter]
|
dict[str, Parameter]: Same object as :attr: |
Source code in src/earthlens/usgs_water/catalog.py
get_parameter(name)
#
Resolve a friendly name to its :class:Parameter row.
Thin wrapper over the inherited :meth:get_dataset, which raises
a ValueError with a did-you-mean hint on an unknown name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
A friendly parameter name ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Parameter |
Parameter
|
The matching catalog row. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Examples:
- Resolve a row and read its fields:
Source code in src/earthlens/usgs_water/catalog.py
load(catalog_path=None)
classmethod
#
Read the USGS Water parameter catalog from disk.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
catalog_path
|
Path | None
|
Path to the catalog YAML. Defaults to the
module-level :data: |
None
|
Returns:
| Type | Description |
|---|---|
Catalog
|
A fully-populated :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the file has no |
Source code in src/earthlens/usgs_water/catalog.py
model_post_init(__context)
#
Auto-load the bundled catalog when no parameters were supplied.
Catalog() with no args reads :data:CATALOG_PATH; passing
parameters=... (or datasets=...) skips the disk read.
Raises:
| Type | Description |
|---|---|
ValueError
|
Propagated from :meth: |
Source code in src/earthlens/usgs_water/catalog.py
resolve(code_or_name)
#
Resolve a friendly name or a raw 5-digit code to a code.
A raw 5-digit code ("00060") passes through unmapped — the
full USGS code table is far larger than the curated catalog, so
any valid code is accepted directly. A non-code string is
looked up as a friendly name via :meth:get_parameter.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
code_or_name
|
str
|
A friendly name ( |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The 5-digit parameter code. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/earthlens/usgs_water/catalog.py
Parameter
#
Bases: BaseModel
One NWIS parameter code's catalog row.
The user-facing name is the parent key in
:attr:Catalog.parameters and is also stored on the row as
:attr:name so a resolved :class:Parameter is self-describing.
Attributes:
| Name | Type | Description |
|---|---|---|
code |
str
|
The 5-digit NWIS parameter code ( |
name |
str
|
Human-readable label ( |
units |
str
|
The reporting units ( |
group |
ParameterGroup
|
Coarse USGS classification ( |
services |
list[str]
|
The :data: |
Examples:
- Build a row directly:
Source code in src/earthlens/usgs_water/catalog.py
clear_catalog_cache()
#
Empty the module-level catalog parse cache.
Useful in tests that rewrite the catalog on disk and want to force
a re-parse. Production callers do not need this — the cache key
includes the file's st_mtime_ns, so any real edit invalidates the
entry on its own.
Source code in src/earthlens/usgs_water/catalog.py
earthlens.usgs_water.auth
#
Optional Personal Access Token resolution for the USGS Water backend.
Hosts :class:UsgsWaterAuth, an :class:earthlens.base.AbstractAuth
subclass that resolves an optional USGS Personal Access Token (PAT)
from, in priority order, an explicit api_token= argument or the
API_USGS_PAT environment variable. Unlike the OpenAQ backend (whose
key is mandatory), USGS NWIS / Water Data works anonymously — a
token only lifts the rate limit. So this auth never raises on a missing
token: when none is found, the backend runs anonymously (the modern
api.waterdata.usgs.gov endpoint then rate-limits aggressively, which
the backend handles by falling back to the legacy endpoint).
The shape:
- :class:
UsgsWaterCredentialsis a frozen pydantic value object carrying the optional token as a :class:pydantic.SecretStr. - :class:
UsgsWaterAuthbinds those credentials and, in :meth:UsgsWaterAuth.configure, resolves the token (explicit then env var) and — when one is present — exports it back to theAPI_USGS_PATenvironment variable, which is the channel thedataretrievalSDK reads.configure()is a no-op when anonymous. - :meth:
UsgsWaterAuth.is_authenticatedreports whether a token was resolved (i.e. whether the request is token-backed or anonymous); both are valid operating modes.
The resolved token is read back via the :attr:UsgsWaterAuth.token
property (which returns None when anonymous).
UsgsWaterAuth
#
Bases: AbstractAuth[UsgsWaterCredentials]
Resolve and hold the optional USGS Personal Access Token.
Implements the :class:earthlens.base.AbstractAuth contract for an
optional-secret backend. Construction does not touch the
environment; :meth:configure performs the resolution and is
idempotent. After configure(), the token (or None for
anonymous) is available via the :attr:token property, and — when
a token was resolved — it is exported to the API_USGS_PAT
environment variable so the dataretrieval SDK picks it up.
The class is a context manager (inherited from
:class:AbstractAuth): with UsgsWaterAuth(creds) as auth: ...
calls configure() on enter and the default no-op close() on
exit.
Attributes:
| Name | Type | Description |
|---|---|---|
_creds |
The :class: |
Examples:
- Resolve an explicit token:
- With no token,
configure()succeeds and stays anonymous:
Source code in src/earthlens/usgs_water/auth.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
token
property
#
The resolved PAT, or None for anonymous access.
Valid after :meth:configure; returns None both before
configuration and when no token was available (anonymous).
Returns:
| Type | Description |
|---|---|
str | None
|
str | None: The USGS PAT string, or |
__init__(credentials)
#
Store credentials; does not resolve the token yet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
credentials
|
UsgsWaterCredentials
|
The :class: |
required |
Source code in src/earthlens/usgs_water/auth.py
configure()
#
Resolve the optional token and export it to the environment.
Idempotent — short-circuits when :meth:is_authenticated
already returns True. Resolves the token in this order: the
explicit api_token on the credentials, then the
API_USGS_PAT environment variable. When a token is found it
is written back to API_USGS_PAT (the channel dataretrieval
reads) and :meth:is_authenticated flips to True. When none
is found this is a no-op — the request runs anonymously
(rate-limited) and no error is raised.
Source code in src/earthlens/usgs_water/auth.py
is_authenticated()
#
Return True when a token was resolved (token-backed mode).
Cheap predicate — does not call the network. False is a
legitimate, non-error state meaning "anonymous access".
Returns:
| Name | Type | Description |
|---|---|---|
bool |
bool
|
|
Source code in src/earthlens/usgs_water/auth.py
UsgsWaterCredentials
#
Bases: BaseModel
Frozen value object holding the optional USGS PAT.
The token is optional at construction time: None means "resolve
from the API_USGS_PAT environment variable at
:meth:UsgsWaterAuth.configure time, and if that is also unset,
run anonymously".
Attributes:
| Name | Type | Description |
|---|---|---|
api_token |
SecretStr | None
|
The USGS Personal Access Token, stored as a
:class: |
Examples:
- Build from an explicit token; the secret is hidden in
repr: - The token is optional — anonymous access is the default: