create_LRA
¶
Create a long-run average (LRA) datasets for e.g. wind speeds and solar radiation. Typical usage (Python):
import reskit as rk
ds_lra = rk.create_LRA(
base_path="/path/to/ERA5",
variable="surface_solar_radiation_downwards.processed.t_adjusted",
start_year=1994,
end_year=2018,
zoom_level=4,
out_dir="./output",
temp_dir=None,
)
It will write: - per-year merged NetCDFs (optional cache) - a final LRA NetCDF - optionally, a GeoTIFF
Input layouts supported:
- tiled:
Functions:
-
calc_solar_elevation_angle–Calculate solar elevation angle using pvlib's spa module.
-
compute_dni_year–Compute time-averaged DNI for a single year, tile by tile.
-
create_DNI_LRA–Compute and write a long-run average (LRA) for Direct Normal Irradiance (DNI) using existing files for GHI and DHI.
-
create_LRA–Compute and write a long-run average (LRA) for a RESKit-processed weather variable.
-
create_long_run_average–Create a long-run average dataset across years (inclusive).
-
create_long_run_average_DNI–Create a long-run average dataset across years (inclusive).
-
expand_to_global_coverage–Expands a near global raster to full global coverage by interpolating edge
-
extract_bbox_from_mosaic–Cut out a bbox (xmin,ymin,xmax,ymax) from the mosaic.
-
interp_vertical_1d–Fill NaNs by 1D linear interpolation along the vertical axis for each column.
-
load_era5_year–Load a year of processed ERA5 data.
-
world_3x3_wrap–Build a 3x3 tiled array with correct pole-wrap and (optionally) return mosaic bounds.
-
write_geotiff_file–Write a lat/lon DataArray to a GeoTIFF.
calc_solar_elevation_angle
¶
Calculate solar elevation angle using pvlib's spa module.
Parameters:
-
(times¶array-like of datetime64, shape (T,)) – -
–lats¶ -
–lons¶ -
(temps¶np.ndarray, shape (Y, X) — surface temperature in °C) – -
(pressures¶np.ndarray, shape (Y, X) — surface pressure in Pa) –
Returns:
-
(ndarray, shape(T, Y, X))–
Source code in reskit/util/create_LRA.py
compute_dni_year
¶
compute_dni_year(
base_path: str | Path,
year: int,
direct_horizontal_irradiance_variable: str,
surface_temperature_variable: str,
surface_pressure_variable: str,
zoom_level: int = 4,
combine_mode: CombineMode = "auto",
) -> Dataset
Compute time-averaged DNI for a single year, tile by tile.
Temperature and pressure are loaded as annual means (memory-efficient). Direct horizontal irradiance tiles are streamed one at a time with the full time axis so the solar elevation angle can be computed per timestep before averaging — loading the whole year at once would exceed memory limits.
Source code in reskit/util/create_LRA.py
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 | |
create_DNI_LRA
¶
create_DNI_LRA(
*,
base_path: str | Path,
variable: str,
direct_horizontal_irradiance_variable: str,
surface_temperature_variable: str,
surface_pressure_variable: str,
start_year: int,
end_year: int,
zoom_level: int = 4,
out_dir: str | Path = Path("output"),
cache_yearly: bool = True,
combine_mode: CombineMode = "auto",
variable_name_output: Optional[str] = None,
write_geotiff: bool = True,
write_netcdf: bool = False,
log_level: str = "INFO",
temp_dir: Optional[str | Path] = None,
weather_source_prefix: Optional[str] = None,
)
Compute and write a long-run average (LRA) for Direct Normal Irradiance (DNI) using existing files for GHI and DHI.
Source code in reskit/util/create_LRA.py
create_LRA
¶
create_LRA(
*,
base_path: str | Path,
variable: str,
start_year: int,
end_year: int,
zoom_level: int = 4,
out_dir: str | Path = Path("output"),
cache_yearly: bool = True,
combine_mode: CombineMode = "auto",
data_var: Optional[str] = None,
variable_name_output: Optional[str] = None,
write_geotiff: bool = True,
write_netcdf: bool = False,
log_level: str = "INFO",
temp_dir: Optional[str | Path] = None,
weather_source_prefix: Optional[str] = None,
) -> Dataset
Compute and write a long-run average (LRA) for a RESKit-processed weather variable.
This is the programmatic entry point equivalent to the CLI in this module.
It computes an annual-mean value per year (by averaging over the time
dimension if present), then averages these annual means across
start_year..end_year.
Input data can be stored in two layouts:
- Tiled layout (RESKit default):
<base_path>/<zoom_level>/<x-tile>/<y-tile>/<year>/*.{variable}.nc
For each year, all matching tile files are loaded, time-averaged, and
merged into a global dataset.
- Non-tiled layout (one file per year):
One matching NetCDF must exist for each year (e.g.
<base_path>/<year>/*.{variable}.nc or similar). In this case nothing
is merged; the single file is loaded and time-averaged.
Intermediate yearly results:
- If cache_yearly=True (default), per-year results are written to
<out_dir>/yearly/<variable>/ unless temp_dir is provided.
- If temp_dir is provided, intermediates are written to
<temp_dir>/<variable>/ instead.
Final outputs (always written under out_dir):
- NetCDF: <out_dir>/LRA/<prefix>long_run_avg_<variable>_<start>_<end>.nc
- Optional GeoTIFF (if write_geotiff=True): same naming with .tif.
Parameters:
-
(base_path¶str | Path) –Root directory containing the processed NetCDF files.
-
(variable¶str) –Variable identifier used in filenames (expects
*.{variable}.nc). -
(start_year¶int) –Inclusive year range used for the long-run average.
-
(end_year¶int) –Inclusive year range used for the long-run average.
-
(zoom_level¶int, default:4) –Zoom level used for tiled layouts. Ignored for non-tiled layouts.
-
(out_dir¶str | Path, default:Path('output')) –Directory where final outputs are written and (by default) where yearly intermediate files are cached.
-
(cache_yearly¶bool, default:True) –If True, cache per-year merged/loaded datasets to NetCDF.
-
(combine_mode¶CombineMode, default:'auto') –How to combine tiled datasets. Use
"auto"(default) unless you have a specific reason. -
(data_var¶Optional[str], default:None) –If the resulting LRA dataset contains multiple data variables, specify which one to export when writing a GeoTIFF.
-
(variable_name_output¶Optional[str], default:None) –If provided, use this name for the variable in the output filenames instead of the input
variable. -
(write_geotiff¶bool, default:True) –If True, also export a GeoTIFF.
-
(write_netcdf¶bool, default:False) –If True, write the NetCDF output (default: always False).
-
(log_level¶str, default:'INFO') –Logging verbosity (
"DEBUG","INFO","WARNING","ERROR"). -
(temp_dir¶Optional[str | Path], default:None) –Optional directory to store intermediate yearly files.
-
(weather_source_prefix¶Optional[str], default:None) –Optional prefix prepended to output filenames. If provided,
"<weather_source_prefix>_"is used.
Returns:
-
Dataset–The computed long-run average dataset.
Source code in reskit/util/create_LRA.py
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 | |
create_long_run_average
¶
create_long_run_average(
base_path: str | Path,
start_year: int,
end_year: int,
variable: str,
out_dir: str | Path,
zoom_level: int = 4,
cache_yearly: bool = True,
combine_mode: CombineMode = "auto",
weather_source_prefix: Optional[str] = None,
) -> Dataset
Create a long-run average dataset across years (inclusive).
Source code in reskit/util/create_LRA.py
create_long_run_average_DNI
¶
create_long_run_average_DNI(
base_path: str | Path,
start_year: int,
end_year: int,
variable: str,
direct_horizontal_irradiance_variable: str,
surface_temperature_variable: str,
surface_pressure_variable: str,
out_dir: str | Path,
zoom_level: int = 4,
cache_yearly: bool = True,
combine_mode: CombineMode = "auto",
weather_source_prefix: Optional[str] = None,
) -> Dataset
Create a long-run average dataset across years (inclusive).
Source code in reskit/util/create_LRA.py
expand_to_global_coverage
¶
expand_to_global_coverage(
rstr: str,
target_bounds: tuple,
as_array: bool = True,
output_path: str | None = None,
) -> ndarray
Expands a near global raster to full global coverage by interpolating edge cells across the antimeridian/poles
Parameters:
-
(rstr¶str) –Path to the raster file that shall be expanded.
-
(target_bounds¶tuple) –(xmin, ymin, xmax, ymax) bounds of the output raster. Must align with the input bounds plus/minus an integer number of pixels.
-
(as_array¶bool, default:True) –If True, the data will be returned as array, if False, a raster will be written to disk, then the output_path must be given, by default True.
-
(output_path¶str | None, default:None) –Path to write the output raster if as_array is False, by default None.
Returns:
-
ndarray–description
Raises:
-
ValueError–description
Source code in reskit/util/create_LRA.py
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 | |
extract_bbox_from_mosaic
¶
extract_bbox_from_mosaic(
mosaic,
*,
bounds_3x3,
pixel_width,
pixel_height,
bbox,
snap_edges=True,
)
Cut out a bbox (xmin,ymin,xmax,ymax) from the mosaic.
Assumptions: - north-up grid: row 0 is at ymax, increasing rows go south - pixel_height is positive
If snap_edges=True, bbox edges are snapped to the pixel grid of the mosaic.
Source code in reskit/util/create_LRA.py
interp_vertical_1d
¶
Fill NaNs by 1D linear interpolation along the vertical axis for each column.
Rules
- Only fills NaNs that lie strictly between two valid samples in the same column.
- Leaves leading/trailing NaNs untouched (not bracketed).
- Optionally limits filling to gaps of length <= max_gap.
Parameters:
-
(arr3x3¶2D ndarray) – -
(max_gap¶int or None, default:None) –If set, only fill NaN runs up to this length (in rows). Larger gaps remain NaN.
Returns:
-
out(2D ndarray) –
Source code in reskit/util/create_LRA.py
load_era5_year
¶
load_era5_year(
base_path: str | Path,
year: int,
variable: str,
zoom_level: int = 4,
combine_mode: CombineMode = "auto",
mean_over_time: bool = True,
) -> Dataset
Load a year of processed ERA5 data.
Logic: - If the input is tiled (zoom directory exists and contains matching tiles): merge tiles. - If not tiled: expect exactly one NetCDF for the year; nothing is merged.
Notes on combining: - Some RESKit processing pipelines write tiles with identical variable names, but disjoint lat/lon coordinates. In that case, combining by coords is the correct operation. - If tiles contain distinct data variables (less common), xarray merge is ok.
Source code in reskit/util/create_LRA.py
world_3x3_wrap
¶
Build a 3x3 tiled array with correct pole-wrap and (optionally) return mosaic bounds. All maps are arranged such that they align with the antimeridian as center column, slight mismatches by resolution/bounds shift are accounted for. Missing cells to the +/-180° longitude and +/-90° latitude bounds are set to NaN. Center map cells overwride other data as long as not NaN.
Inputs
arr_center : np.ndarray array of raster data for the center tile. rInfo : object Raster info object with attributes: - pixelWidth : float - pixelHeight : float - bounds : tuple of (xmin, ymin, xmax, ymax)
Returns:
-
arr_3x3((3H, 3W) ndarray) – -
bounds_3x3((xmin, ymin, xmax, ymax)) –Outer-edge bounds of the returned 3x3 array, consistent with the padded grid (including any extra/excess cells induced by the shift/resolution).
Source code in reskit/util/create_LRA.py
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 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 | |
write_geotiff_file
¶
write_geotiff_file(
da: DataArray,
output_tiff_path: str | Path,
crs: str = "EPSG:4326",
) -> None
Write a lat/lon DataArray to a GeoTIFF.
The raster is always stored north-up (first row at the northern edge), which is the
convention the rest of RESKit reads with geokit. Bounds are derived from the
coordinates, which are taken to be pixel centers, so the written extent is the outer
edge of the corner pixels.
Parameters:
-
(da¶DataArray) –Data to write. Must carry a latitude (
latitude/lat) and a longitude (longitude/lon) coordinate, both evenly spaced. -
(output_tiff_path¶str | Path) –Destination path. Parent directories are created as needed.
-
(crs¶str, default:'EPSG:4326') –Coordinate reference system of
da, in any formgeokit.srs.loadSRSaccepts.