workflow_manager
¶
Classes:
-
WorkflowManager–The WorkflowManager class assists with the construction of more specialized WorkflowManagers,
-
WorkflowQueue–The WorkflowQueue object allows for the queueing of multiple RESKit workflow simulations
Functions:
-
distribute_workflow–Distributes a RESKit simulation workflow across multiple CPUs
-
execute_workflow_iteratively–The function executes the indicated workflow iteratively, iterating over weather tiles. The appropriate weather
WorkflowManager
¶
The WorkflowManager class assists with the construction of more specialized WorkflowManagers, such as the WindWorkflowManager or the SolarWorkflowManager. In addition to providing the general structure for simulation workflow management, the WorkflowManager also defines functionalities which should be common across all WorkflowManagers.
This includes: - Basic initialization - Time domain management - Reading weather data - Adjusting variables by a long-run-average value - Applying simple loss factors - Saving the state of WorkflowManagers to XArray datasets, either in memory or on disc
Initialization:
WorkflowManager( placements )
Methods:
-
adjust_variable_to_long_run_average–Adjusts the average mean of the specified variable to a known long-run-average
-
apply_loss_factor–Applies a loss factor onto a specified variable
-
extract_raster_values_at_placements–Extracts pixel values at each of the configured placements from the specified raster file
-
get_scalar_values_from_raster–Auxiliary function to extract raster values with NaN fallback options.
-
read–Reads the specified variables from the NetCDF4-style weather dataset, and then extracts
-
register_workflow_parameter–Add a parameter to the WorkflowManager which will be included in the output XArray dataset
-
set_time_index–Sets the time index of the WorkflowManager
-
spatial_disaggregation–[summary]
-
to_netcdf–Saves an XArray dataset to netCDF4 format
-
to_xarray–Generates an XArray dataset from the data currently contained in the WorkflowManager
Source code in reskit/workflow_manager.py
adjust_variable_to_long_run_average
¶
adjust_variable_to_long_run_average(
variable: str,
source_long_run_average: Union[str, float, ndarray],
real_long_run_average: Union[str, float, ndarray],
real_lra_scaling: float = 1,
spatial_interpolation: str = "linear-spline",
nodata_fallback: str = "nan",
nodata_fallback_scaling: float = 1,
allow_nans: bool = True,
)
Adjusts the average mean of the specified variable to a known long-run-average
Note:
uses the equation: variable[t] = variable[t] * real_long_run_average / source_long_run_average
Parameters:
-
(variable¶str) –The variable to be adjusted
-
(source_long_run_average¶Union[str, float, ndarray]) –The variable's native long run average (the average in the weather file) - If a string is given, it is expected to be a path to a raster file which can be used to look up the average values from using the coordinates in
.placements- If a numpy ndarray (or derivative) is given, the shape must be one of (time, placements) or at least (placements) -
(real_long_run_average¶Union[str, float, ndarray]) –The variables 'true' long run average - If a string is given, it is expected to be a path to a raster file which can be used to look up the average values from using the coordinates in
.placements- If a numpy ndarray (or derivative) is given, the shape must be one of (time, placements) or at least (placements) -
(real_lra_scaling¶float, default:1) –An optional scaling factor to apply to the values derived from
real_long_run_average. - This is primarily useful whenreal_long_run_averageis a path to a raster file - By default 1 -
(spatial_interpolation¶str, default:'linear-spline') –When either
source_long_run_averageorreal_long_run_averageare a path to a raster file, this input specifies which interpolation algorithm should be used - Options are: "near", "linear-spline", "cubic-spline", "average" - By default "linear-spline" - See for more info: geokit.raster.interpolateValues -
(nodata_fallback¶str, default:'nan') –When real_long_run_average has no data, one can decide between different fallback options, by default np.nan: - np.nan or None : return np.nan for missing values in real_long_run_average - float : Apply this float value as a scaling factor for all no-data locations only: source_long_run_average * nodata_fallback. NOTE: A value of 1.0 will return the source lra value in case of missing real lra values (no additional nodata_fallback_scaling applied). - str : Will be interpreted as a filepath to a raster with alternative real_long_run_average values, scaled by nodata_fallback_scaling. - callable : any callable method taking the arguments (all iterables): 'locs' and 'source_long_run_average_value' (the locations as gk.geom.point objects and original value from source data). The output values will be considered as the new real_long_run_average for missing locations only (absolute data, no additional nodata_fallback_scaling applied).
NOTE: np.nan will also be returned in case that the nodata fallback does not yield values either.
-
(nodata_fallback_scaling¶float, default:1) –An optional scaling factor to apply to the values derived from
nodata_fallback. - This is primarily useful whennodata_fallbackis a path to a raster file - By default 1 -
(allow_nans¶boolean, default:True) –If True, NaN values may remain after scaling, else an error will raised. By default True.
Returns:
-
WorkflowManager–Returns the invoking WorkflowManager (for chaining)
Source code in reskit/workflow_manager.py
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 | |
apply_loss_factor
¶
apply_loss_factor(
loss: Union[float, ndarray, FunctionType],
variables: Union[str, List[str]] = ["capacity_factor"],
)
Applies a loss factor onto a specified variable
Parameters:
-
(loss¶Union[float, ndarray, FunctionType]) –The loss factor(s) to be applied - If a float or a numpy ndarray is given, then the following operation is performed:
variable = variable * (1 - loss) - If a function is given, then the following operation is performed: variable = variable * (1 - loss(variable) ) - If a numpy ndarray is given, it must be broadcastable to the variable's shape in
.sim_data -
(variables¶Union[str, List[str]], default:['capacity_factor']) –The variable or variables to apply the loss factor to - By default ["capacity_factor"]
Returns:
-
WorkflowManager–Returns the invoking WorkflowManager (for chaining)
Source code in reskit/workflow_manager.py
extract_raster_values_at_placements
¶
Extracts pixel values at each of the configured placements from the specified raster file
get_scalar_values_from_raster
¶
Auxiliary function to extract raster values with NaN fallback options.
Source code in reskit/workflow_manager.py
read
¶
read(
variables: Union[str, List[str]],
source_type: str,
source: str,
set_time_index: bool = False,
spatial_interpolation_mode: str = "bilinear",
temporal_reindex_method: str = "nearest",
time_index_from=None,
**kwargs,
)
Reads the specified variables from the NetCDF4-style weather dataset, and then extracts
those variables for each of the coordinates configured in .placements. The resulting
data is then available in .sim_data.
Parameters:
-
(variables¶str or list of strings) –The variables (or variables) to be read from the specified source - If a path to a weather source is given, then only the 'standard' variables configured for that source type are available (see the doc string for the weather source you are interested in) - If either 'elevated_wind_speed' or 'surface_wind_speed' is included in the variable list, then the members
.elevated_wind_speed_heightand.surface_wind_speed_height, respectfully, are also added. These are constants which specify what the 'native' wind speed height is, which depends on the source - A pre-loaded NCSource can also be given, thus allowing for any variable in the source to be specified in thevariableslist. But the user needs to take care of initializing the NCSource and loading the data they want -
(source_type¶str) –The type of weather datasource which is to be loaded. Can be one of: "ERA5", "SARAH", "MERRA", or 'user' - If a pre-loaded NCSource is given for the
sourceobject, then thesource_typeshould be "user" -
(source¶str or NCSource) –The source to read weather variables from
-
(set_time_index¶bool, default:False) –If True, instructs the workflow manager to set the time index to that which is read from the weather source - By default False
-
(spatial_interpolation_mode¶str, default:'bilinear') –The spatial interpolation mode to use while reading data from the weather source at each of the placement coordinates - By default "bilinear"
-
(temporal_reindex_method¶str, default:'nearest') –In the event of missing data, this algorithm is used to fill in the missing data. - Can be, for example, "nearest", "ffill", "bfill", "interpolate" - By default "nearest"
Returns:
-
WorkflowManager–Returns the invoking WorkflowManager (for chaining)
Raises:
-
RuntimeError–If set_time_index is False but no
.time_indexexists -
RuntimeError–If source_type is unknown
Source code in reskit/workflow_manager.py
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 | |
register_workflow_parameter
¶
Add a parameter to the WorkflowManager which will be included in the output XArray dataset
Parameters:
-
(key¶str) –The workflow parameter's access key
-
(value¶Union[str, float]) –The workflow parameter's value. Only strings and floats are allowed
Source code in reskit/workflow_manager.py
set_time_index
¶
Sets the time index of the WorkflowManager
Source code in reskit/workflow_manager.py
spatial_disaggregation
¶
spatial_disaggregation(
variable: str,
source_high_resolution: Union[str, float, ndarray],
source_low_resolution: Union[str, float, ndarray],
real_lra_scaling: float = 1,
spatial_interpolation: str = "linear-spline",
)
[summary]
Parameters:
-
(variable¶str) –[description]
-
(source_long_run_average¶Union[str, float, ndarray]) –[description]
-
(real_long_run_average¶Union[str, float, ndarray]) –[description]
-
(real_lra_scaling¶float, default:1) –[description], by default 1
-
(spatial_interpolation¶str, default:'linear-spline') –[description], by default "linear-spline"
Source code in reskit/workflow_manager.py
to_netcdf
¶
to_netcdf(
xds: Dataset,
output_netcdf_path: str = None,
output_variables: List[str] = None,
custom_attributes: dict = None,
_intermediate_dict=False,
) -> str
Saves an XArray dataset to netCDF4 format
Note:
- The .placements data is automatically added to the XArray dataset along the 'locations' dimension
- The workflow_parameters data is automatically added as dimensionless variables
- The .sim_data is automatically added along the dimensions (time, locations)
- The .time_index is automatically added along the dimension 'time'
Parameters:
-
(xds¶Dataset) –The XArray dataset to save
-
(output_netcdf_path¶str, default:None) –If given, the XArray dataset will be written to disc at the specified path - By default None
-
(output_variables¶List[str], default:None) –If given, specifies the variables which should be included in the resulting dataset. Otherwise all suitable variables found in
.placements,.workflow_parameters,.sim_data, and.time_indexwill be included - Only variables of numeric or string type are suitable due to NetCDF4 limitations - By default None -
(custom_attributes¶dict, default:None) –If given, adds the key-value pairs as attributes to the XArray dataset before saving - These will be added in addition to existing attributes - By default None
Returns:
-
output_netcdf_path–The resulting output_netcdf_path
Source code in reskit/workflow_manager.py
to_xarray
¶
to_xarray(
output_netcdf_path: str = None,
output_variables: List[str] = None,
custom_attributes: dict = None,
_intermediate_dict=False,
) -> Dataset
Generates an XArray dataset from the data currently contained in the WorkflowManager
Note:
- The .placements data is automatically added to the XArray dataset along the 'locations' dimension
- The workflow_parameters data is automatically added as dimensionless variables
- The .sim_data is automatically added along the dimensions (time, locations)
- The .time_index is automatically added along the dimension 'time'
Parameters:
-
(output_netcdf_path¶str, default:None) –If given, the XArray dataset will be written to disc at the specified path - By default None
-
(output_variables¶List[str], default:None) –If given, specifies the variables which should be included in the resulting dataset. Otherwise all suitable variables found in
.placements,.workflow_parameters,.sim_data, and.time_indexwill be included - Only variables of numeric or string type are suitable due to NetCDF4 limitations - By default None -
(custom_attributes¶dict, default:None) –If given, adds the key-value pairs as attributes to the XArray dataset - These will be added in addition to the workflow_parameters - By default None
Returns:
-
Dataset–The resulting XArray dataset
Source code in reskit/workflow_manager.py
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 | |
WorkflowQueue
¶
The WorkflowQueue object allows for the queueing of multiple RESKit workflow simulations which are then executed in parallel
Initialize:
WorkflowFunction( workflow:FunctionType, **kwargs )
Parameters:
-
(workflow¶FunctionType) – -
–The¶ -
–and¶ -
–else¶ -
–**kwargs¶All key word arguments are passed on as constants to each simulation Use these to set the required arguments for the given
workflow
Methods:
-
append–Appends a simulation set the current queue
-
execute–Executes all of the simulation sets that are currently in the queue
Source code in reskit/workflow_manager.py
append
¶
execute
¶
execute(jobs: int = 1) -> OrderedDict[str, Dataset]
Executes all of the simulation sets that are currently in the queue
Parameters:
-
(jobs¶int, default:1) –The number of parallel jobs, by default 1
Returns:
-
OrderedDict[Dataset]–The results of each simulation set, accessible via their access keys
Source code in reskit/workflow_manager.py
distribute_workflow
¶
distribute_workflow(
workflow_function: FunctionType,
placements: DataFrame,
jobs: int = 2,
max_batch_size: int = None,
intermediate_output_dir: str = None,
**kwargs,
) -> Dataset
Distributes a RESKit simulation workflow across multiple CPUs
Parallelism is achieved by breaking up the placements dataframe into placement groups via KMeans grouping
Parameters:
-
(workflow_function¶FunctionType) –The workflow function to be parallelized - All RESKit workflow functions should be suitable here - If you want to make your own function, the only requirement is that its first argument should be a pandas DataFrame in the form of a placements table (i.e. has a 'lat' and 'lon' column) - Don't forget that that all inputs required for the workflow function are still required, and are passed on as constants through any specified
kwargs -
(placements¶DataFrame) –A DataFrame describing the placements to be simulated For example, if you are simulating wind turbines, the following columns are likely required: ['lon','lat','capacity','hub_height','rotor_diam',]
-
(jobs¶int, default:2) –The number of parallel jobs - By default 2
-
(max_batch_size¶int, default:None) –If given, limits the maximum number of total placements which are simulated in parallel - Use this to reduce the memory requirements of the simulations (in turn increasing overall simulation time) - By default None
-
(intermediate_output_dir¶str, default:None) –In case of very large outputs (which are too large to be joined into a singular XArray dataset), use this to write the individual simulation results to the specified directory - By default None
-
–**kwargs¶All all key word arguments are passed on as constants to each simulation - Use these to set the required arguments for the given
workflow_function
Returns:
-
Dataset–An XArray Dataset which contains the combined results of the distributed simulations
Source code in reskit/workflow_manager.py
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 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 | |
execute_workflow_iteratively
¶
The function executes the indicated workflow iteratively, iterating over weather tiles. The appropriate weather tile per placement is extracted automatically and placements are batched together based on weather tile.
workflow : RESkit workflow Callable workflow function, e.g. reskit.wind.wind_era5_2023
weather_path_varname : str Str formatted name of the weather path variable in this workflow, e.g. 'era5_path' for reskit.wind.wind_era5_2023. Must must be a key of workflow_args.
zoom : int, optional
The zoom level of the weather tiles, required only if
**workflow_args Passed on to the workflow specified above. Must contain ''placements'' and the above weather_path_varname as keys.
Source code in reskit/workflow_manager.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 | |