Skip to content

Commit

Permalink
Randomise coordinate lists
Browse files Browse the repository at this point in the history
  • Loading branch information
burggraaff committed Feb 23, 2024
1 parent 5d3a7ff commit c070b8c
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions fpcup/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Site-related stuff: load data etc
"""
from itertools import product
from random import shuffle

import numpy as np

Expand All @@ -18,11 +19,13 @@ def example(*args, **kwargs) -> PCSESiteDataProvider:
sitedata = WOFOST72SiteDataProvider(WAV=10)
return sitedata

def grid_coordinate_range(latitude: tuple[RealNumber], longitude: tuple[RealNumber]) -> list[tuple[float]]:
def grid_coordinate_range(latitude: tuple[RealNumber], longitude: tuple[RealNumber], *,
shuffle_result=True) -> list[tuple[float]]:
"""
Generate all pairs of latitude/longitude coordinates in given latitude/longitude ranges.
Inputs should be tuples that can be passed to np.arange as parameters.
The output is given as a list so it can be iterated over multiple times.
If `shuffle_result` is True (default: True), the list is shuffled randomly, so that incomplete runs will still provide adequate coverage over the whole area.
Example:
coords = grid_coordinate_range(latitudes=(50, 52, 0.1), longitudes=(5, 10, 1))
Expand All @@ -31,14 +34,21 @@ def grid_coordinate_range(latitude: tuple[RealNumber], longitude: tuple[RealNumb
longitude_range = np.arange(*longitude)
coordinates = product(latitude_range, longitude_range)
coordinates = list(coordinates)

# Randomise order if desired
if shuffle_result:
shuffle(coordinates) # In-place

return coordinates

def grid_coordinate_linspace(latitude: tuple[RealNumber], longitude: tuple[RealNumber], n: int) -> list[tuple[float]]:
def grid_coordinate_linspace(latitude: tuple[RealNumber], longitude: tuple[RealNumber], n: int, *,
shuffle_result=True) -> list[tuple[float]]:
"""
Generate n pairs of latitude/longitude coordinates in a given area bound by latitude/longitude ranges.
Inputs should be tuples of (min, max) latitude/longitude.
The output is given as a list so it can be iterated over multiple times.
Note: the true size of the output may be smaller than n due to rounding.
If `shuffle_result` is True (default: True), the list is shuffled randomly, so that incomplete runs will still provide adequate coverage over the whole area.
Example:
coords = grid_coordinate_range(latitudes=(50, 52), longitudes=(5, 10), n=1000)
Expand All @@ -48,4 +58,9 @@ def grid_coordinate_linspace(latitude: tuple[RealNumber], longitude: tuple[RealN
longitude_range = np.linspace(*longitude, n_each)
coordinates = product(latitude_range, longitude_range)
coordinates = list(coordinates)

# Randomise order if desired
if shuffle_result:
shuffle(coordinates) # In-place

return coordinates

0 comments on commit c070b8c

Please sign in to comment.