Skip to content

Commit

Permalink
Merge pull request #280 from NREL/excl_cleanup
Browse files Browse the repository at this point in the history
Exclusions refactor
  • Loading branch information
MRossol authored Mar 8, 2021
2 parents 8428e2f + df945f1 commit cb17227
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
3 changes: 3 additions & 0 deletions reV/handlers/exclusions.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ def __getitem__(self, keys):

return out

def __contains__(self, layer):
return layer in self.layers

def close(self):
"""
Close h5 instance
Expand Down
2 changes: 1 addition & 1 deletion reV/supply_curve/cli_sc_aggregation.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ def direct(ctx, excl_fpath, gen_fpath, tm_dset, econ_fpath, res_fpath,
dsets = list(f)
if tm_dset not in dsets:
try:
TechMapping.run(excl_fpath, res_fpath, tm_dset)
TechMapping.run(excl_fpath, res_fpath, dset=tm_dset)
except Exception as e:
logger.exception('TechMapping process failed. Received the '
'following error:\n{}'.format(e))
Expand Down
29 changes: 16 additions & 13 deletions reV/supply_curve/tech_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
class TechMapping:
"""Framework to create map between tech layer (exclusions), res, and gen"""

def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03,
def __init__(self, excl_fpath, res_fpath, distance_upper_bound=0.03,
map_chunk=2560, max_workers=None):
"""
Parameters
Expand All @@ -38,8 +38,6 @@ def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03,
created in excl_fpath.
res_fpath : str
Filepath to .h5 resource file that we're mapping to.
dset : str
Dataset name in excl_fpath to save mapping results to.
distance_upper_bound : float | None
Upper boundary distance for KNN lookup between exclusion points and
resource points. None will calculate a good distance based on the
Expand All @@ -54,7 +52,6 @@ def __init__(self, excl_fpath, res_fpath, dset, distance_upper_bound=0.03,
self._distance_upper_bound = distance_upper_bound
self._excl_fpath = excl_fpath
self._res_fpath = res_fpath
self._dset = dset
self._check_fout()
self._map_chunk = map_chunk

Expand Down Expand Up @@ -408,7 +405,7 @@ def save_tech_map(lats, lons, ind, fpath_out, res_fpath, dset,
.format(dset, fpath_out))

@classmethod
def run(cls, excl_fpath, res_fpath, dset, save_flag=True,
def run(cls, excl_fpath, res_fpath, dset=None,
distance_upper_bound=0.03, map_chunk=2560, max_workers=None):
"""Run parallel mapping and save to h5 file.
Expand All @@ -419,12 +416,18 @@ def run(cls, excl_fpath, res_fpath, dset, save_flag=True,
created in excl_fpath.
res_fpath : str
Filepath to .h5 resource file that we're mapping to.
dset : str
Dataset name in excl_fpath to save mapping results to.
save_flag : bool
Flag to write techmap to excl_fpath.
kwargs : dict
Keyword args to initialize the TechMapping object.
dset : str, optional
Dataset name in excl_fpath to save mapping results to, if None
do not save tech_map to excl_fpath, by default None
distance_upper_bound : float | None
Upper boundary distance for KNN lookup between exclusion points and
resource points. None will calculate a good distance based on the
resource meta data coordinates. 0.03 is a good value for a 4km
resource grid and finer.
map_chunk : int | None
Calculation chunk used for the tech mapping calc.
max_workers : int | None
Number of cores to run mapping on. None uses all available cpus.
Returns
-------
Expand All @@ -440,11 +443,11 @@ def run(cls, excl_fpath, res_fpath, dset, save_flag=True,
"""
kwargs = {"distance_upper_bound": distance_upper_bound,
"map_chunk": map_chunk, "max_workers": max_workers}
with cls(excl_fpath, res_fpath, dset, **kwargs) as mapper:
with cls(excl_fpath, res_fpath, **kwargs) as mapper:
lats, lons, ind = mapper._parallel_resource_map()
distance_upper_bound = mapper._distance_upper_bound

if save_flag:
if dset:
mapper.save_tech_map(lats, lons, ind, excl_fpath, res_fpath,
dset, distance_upper_bound)

Expand Down
3 changes: 1 addition & 2 deletions tests/test_supply_curve_tech_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
def test_resource_tech_mapping():
"""Run the supply curve technology mapping and compare to baseline file"""

lats, lons, ind = TechMapping.run(EXCL, RES, TM_DSET, max_workers=2,
save_flag=False)
lats, lons, ind = TechMapping.run(EXCL, RES, dset=None, max_workers=2)

with ExclusionLayers(EXCL) as ex:
lat_truth = ex.latitude
Expand Down

0 comments on commit cb17227

Please sign in to comment.