Parameters#
Parameters#
hapi.rrm.parameters.Parameters
#
Parameter distribution class for hydrological model calibration.
The Parameters class distributes values from a parameter vector during the calibration process into a 3D array, handling lumped parameters and hydrologic response units (HRUs).
Source code in src/hapi/rrm/parameters.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |
__init__(raster: Dataset, no_parameters: int, no_lumped_par: int = 0, lumped_par_pos: list[int] | None = None, lake: bool = False, snow: bool = False, hru: bool = False, function: int = 1, k_upper_bound: int = 1, k_lower_bound: int = 50, muskingum: bool = False)
#
Initialize the Parameters class.
To initiate the Parameters class, you have to provide the Flow Acc raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster
|
Dataset
|
A pyramids |
required |
no_parameters
|
int
|
Number of parameters in the HBV model. |
required |
no_lumped_par
|
int
|
Number of lumped parameters. You have to enter the value of the lumped parameter at the end of the list. Defaults to 0 (no lumped parameters). |
0
|
lumped_par_pos
|
list[int] | None
|
List of the order or position of lumped parameters among all the parameters of the lumped model (order starts from 0 to the length of the model parameters). Defaults to None (empty). The following order of parameters is used for the lumped HBV model: [ltt, utt, rfcf, sfcf, ttm, cfmax, cwh, cfr, fc, beta, e_corr, etf, lp, c_flux, k, k1, alpha, perc, pcorr, Kmuskingum, Xmuskingum]. |
None
|
lake
|
bool
|
True if there is a lake, False otherwise. Defaults to False. |
False
|
snow
|
bool
|
True to run the snow-related processes, False otherwise. When True, parameters related to snow simulation have to be provided. Defaults to False. |
False
|
hru
|
bool
|
True if the parameters will consider using HRUs. Defaults to False. |
False
|
function
|
int
|
Which parameter-distribution strategy to bind to
:attr: |
1
|
k_upper_bound
|
int
|
Upper bound of K value (traveling time in muskingum routing method). Defaults to 1 hour. |
1
|
k_lower_bound
|
int
|
Lower bound of K value (traveling time in muskingum routing method). Defaults to 50. |
50
|
muskingum
|
bool
|
True if the routing function is muskingum. Defaults to False. |
False
|
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If |
AssertionError
|
If |
ValueError
|
If |
Note
Cells outside the catchment are identified by pyramids via
read_array(masked=True) and stored as NaN in
:attr:raster_array, which is promoted to floating point so it can hold
them. no_elem, celli/cellj and the width of :attr:Par2d all
derive from that mask, so the parameter vector length follows the raster's
real domain.
Examples:
- Build the distributor from a small raster and inspect the domain it
derived. The bottom-right cell is no-data, leaving three cells to
parameterise:
>>> import numpy as np >>> from pyramids.dataset import Dataset >>> from hapi.rrm.parameters import Parameters >>> raster = Dataset.create_from_array( ... np.array([[1, 2], [3, -9999]], dtype="int32"), ... top_left_corner=(0.0, 8000.0), cell_size=4000.0, epsg=32618, ... no_data_value=-9999, ... ) >>> distributor = Parameters(raster, 12) >>> distributor.no_elem 3 >>> distributor.Par2d.shape (12, 3) >>> list(zip(distributor.celli, distributor.cellj)) [(0, 0), (0, 1), (1, 0)] - A real value within 0.1% of the sentinel is kept, so it is treated as a
catchment cell and widens the parameter array:
>>> import numpy as np >>> from pyramids.dataset import Dataset >>> from hapi.rrm.parameters import Parameters >>> raster = Dataset.create_from_array( ... np.array([[1, 2], [3, -9990]], dtype="int32"), ... top_left_corner=(0.0, 8000.0), cell_size=4000.0, epsg=32618, ... no_data_value=-9999, ... ) >>> distributor = Parameters(raster, 12) >>> distributor.no_elem 4 >>> float(distributor.raster_array[1, 1]) -9990.0
Source code in src/hapi/rrm/parameters.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 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 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 | |
calculate_k(x: float, position: int, upper_bound: float, lower_bound: float) -> float
staticmethod
#
Calculate K parameter for Muskingum routing.
Takes the value of x parameter and generates 100 random values of the K parameter between the upper and lower constraints, then returns the value corresponding to the given position.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x
|
float
|
Weighting coefficient to determine the linearity of the water surface (one of the parameters of the Muskingum routing method). |
required |
position
|
int
|
Random position between upper and lower bounds of the K parameter. |
required |
upper_bound
|
float
|
Upper bound for the K parameter. |
required |
lower_bound
|
float
|
Lower bound for the K parameter. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The K parameter value corresponding to the given position within the constrained range. |
Source code in src/hapi/rrm/parameters.py
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 | |
hru_hand(dem: Dataset, flow_direction: Dataset, flow_path_length: Dataset, river: Dataset) -> tuple[np.ndarray, np.ndarray]
staticmethod
#
Calculate Height Above Nearest Drainage (HAND) for HRU classification.
Calculates inputs for the HAND method for land use classification by tracing flow direction from each cell to the nearest river reach, then computing the elevation difference and the flow path distance.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dem
|
Dataset
|
A pyramids |
required |
flow_direction
|
Dataset
|
A pyramids |
required |
flow_path_length
|
Dataset
|
A pyramids |
required |
river
|
Dataset
|
A pyramids |
required |
Returns:
| Type | Description |
|---|---|
A tuple of two numpy ndarrays
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the catchment boundaries contain anomalies (e.g., after cropping with a polygon). |
Source code in src/hapi/rrm/parameters.py
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 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | |
hydrologic_response_units(par_g: list | np.ndarray)
#
Distribute parameters using Hydrologic Response Units (HRUs).
Takes a list of parameters (saved as one column or generated as a 1D list from an optimization algorithm) and distributes them horizontally on the number of cells given by a raster. The input raster should be a classified raster (by numbers) into classes to define the HRUs. Each HRU receives the same set of generated parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
par_g
|
list | ndarray
|
1D list or numpy array of parameters. For HRU without
lumped parameters, the length should be
|
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
AssertionError
|
If there are lumped parameters and the length
of |
Source code in src/hapi/rrm/parameters.py
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 | |
par2d_lumped_k1_lake(par_g: list | np.ndarray, no_parameters_lake: int)
#
Distribute parameters with a lumped K1 and lake parameters.
Takes a list of parameters and distributes them horizontally on the number of cells given by a raster. All parameters are distributed except the lower zone coefficient (K1), which is lumped and appended at the end of the parameter list. Lake parameters are extracted from the end of the parameter list.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
par_g
|
list | ndarray
|
1D list or numpy array of parameters. Each cell's
distributed parameters are listed sequentially, followed
by the lumped K1 value, followed by lake parameters at
the end. For example, with 14 cells and 11 distributed
parameters: |
required |
no_parameters_lake
|
int
|
Number of lake parameters to extract
from the end of |
required |
Source code in src/hapi/rrm/parameters.py
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 | |
par3d(par_g: list | np.ndarray)
#
Distribute parameters horizontally across grid cells.
Takes a list of parameters (saved as one column or generated as a 1D list from an optimization algorithm) and distributes them horizontally on the number of cells given by a raster.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
par_g
|
list | ndarray
|
1D list or numpy array of parameters. For totally
distributed parameters, the length should be
|
required |
Raises:
| Type | Description |
|---|---|
AssertionError
|
If the length of |
Source code in src/hapi/rrm/parameters.py
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 | |
par3d_lumped(par_g: list | np.ndarray)
#
Distribute lumped parameters horizontally across grid cells.
Takes a list of parameters (saved as one column or generated as a 1D list from an optimization algorithm) and distributes them horizontally on the number of cells given by a raster, where all parameters are lumped (same value for every cell).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
par_g
|
list | ndarray
|
1D list or numpy array of lumped parameters.
The length should equal |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Source code in src/hapi/rrm/parameters.py
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 | |
parameters_number()
#
Calculate the total number of parameters for the optimization.
Calculates the number of parameters that the optimization algorithm will search for. Use this only in case of totally distributed catchment parameters. In case of lumped parameters, the number of parameters is the same as the number of parameters of the conceptual model.
The result is stored in the ParametersNO attribute.
Note
The Parameters object should have the following attributes
before calling this method: raster, no_parameters,
no_lumped_par, and HRUs.
Source code in src/hapi/rrm/parameters.py
746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 | |
save_parameters(path: str | None)
#
Save distributed parameters as raster files.
Takes the generated 3D parameter array and saves each parameter layer as a separate GeoTIFF raster file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | None
|
Path to the folder where the parameter rasters will be saved. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
|
FileNotFoundError
|
The output directory does not exist. Checked up front so the failure does not surface midway through writing. |
Note
The Parameters object should have the following attributes
set before calling this method: DistParFn, raster,
Par, no_parameters, snow, kub, and klb.
Source code in src/hapi/rrm/parameters.py
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 | |