Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 7 additions & 45 deletions rex/resource_extraction/resource_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -1716,18 +1716,18 @@ class WindX(ResourceX):

DEFAULT_RES_CLS = WindResource

def get_SAM_gid(self, hub_height, gid, out_path=None, write_time=True,
def get_SAM_gid(self, gid, hub_height, out_path=None, write_time=True,
extra_meta_data=None, **kwargs):
"""
Extract time-series of all variables needed to run SAM for nearest
site to given resource gid and hub height

Parameters
----------
hub_height : int
Hub height of interest
gid : int | list
Resource gid(s) of interset
hub_height : int
Hub height of interest
out_path : str, optional
Path to save SAM data to in SAM .csv format, by default None
write_time : bool
Expand Down Expand Up @@ -1761,18 +1761,18 @@ def get_SAM_gid(self, hub_height, gid, out_path=None, write_time=True,

return SAM_df

def get_SAM_lat_lon(self, hub_height, lat_lon, check_lat_lon=True,
def get_SAM_lat_lon(self, lat_lon, hub_height, check_lat_lon=True,
out_path=None, **kwargs):
"""
Extract time-series of all variables needed to run SAM for nearest
site to given lat_lon and hub height

Parameters
----------
hub_height : int
Hub height of interest
lat_lon : tuple
(lat, lon) coordinate of interest
hub_height : int
Hub height of interest
check_lat_lon : bool, optional
Flag to check to make sure the requested lat lons are inside the
resource grid. This is done by comparing with the bounding box of
Expand All @@ -1794,48 +1794,10 @@ def get_SAM_lat_lon(self, hub_height, lat_lon, check_lat_lon=True,
returned
"""
gid = self.lat_lon_gid(lat_lon, check_lat_lon=check_lat_lon)
SAM_df = self.get_SAM_gid(hub_height, gid, out_path=out_path, **kwargs)
SAM_df = self.get_SAM_gid(gid, hub_height, out_path=out_path, **kwargs)

return SAM_df

@classmethod
def make_SAM_files(cls, hub_height, res_h5, gids, out_path,
write_time=True, extra_meta_data=None, max_workers=1,
n_chunks=36, **kwargs):
"""A performant parallel entry point for making many SAM csv
files for many gids

Parameters
----------
hub_height : int
Hub height of interest
res_h5 : str
Filepath to resource h5 file.
gids : list | tuple | np.ndarray
Resource gid(s) of interset
out_path : str, optional
Path to save SAM data to in SAM .csv format. A gid index
"*_{gid}.csv" will be appended to the file path
write_time : bool
Flag to write the time columns (Year, Month, Day, Hour, Minute)
extra_meta_data : dict, optional
Dictionary that maps the names and values of extra meta
info. For example, extra_meta_data={'TMY Year': '2020'}
will add a column 'TMY Year' to the meta data with
a value of '2020'.
max_workers : int | None
Number of parallel workers. None for all workers.
n_chunks : int
Number of chunks to split gids into for parallelization
kwargs : dict
Internal kwargs for get_SAM_df
"""
kwargs['height'] = hub_height
super().get_SAM_gid(res_h5, gids, out_path, write_time=write_time,
extra_meta_data=extra_meta_data,
max_workers=max_workers, n_chunks=n_chunks,
**kwargs)


class MultiFileWindX(MultiFileResourceX):
"""
Expand Down
8 changes: 4 additions & 4 deletions rex/resource_extraction/wind_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,21 @@ def sam_datasets(ctx, hub_height, lat_lon, gid, sites):
if lat_lon is not None:
out_path = os.path.join(ctx.obj['OUT_DIR'],
'SAM_{}.csv'.format(lat_lon))
f.get_SAM_lat_lon(hub_height, lat_lon,
f.get_SAM_lat_lon(lat_lon, hub_height,
out_path=out_path)
elif gid is not None:
out_path = os.path.join(ctx.obj['OUT_DIR'],
'SAM_{}.csv'.format(gid))
f.get_SAM_gid(hub_height, gid, out_path=out_path)
f.get_SAM_gid(gid, hub_height, out_path=out_path)

else:
name, gid, lat_lon = _parse_sites(sites)
with ctx.obj['CLS'](ctx.obj['H5'], **ctx.obj['CLS_KWARGS']) as f:
meta = f['meta']
if lat_lon is not None:
SAM_df = f.get_SAM_lat_lon(hub_height, lat_lon)
SAM_df = f.get_SAM_lat_lon(lat_lon, hub_height)
elif gid is not None:
SAM_df = f.get_SAM_gid(hub_height, gid)
SAM_df = f.get_SAM_gid(gid, hub_height)

gids = []
for df in SAM_df:
Expand Down
2 changes: 1 addition & 1 deletion rex/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""rex Version number"""

__version__ = "0.3.5"
__version__ = "0.4.0"
54 changes: 53 additions & 1 deletion tests/test_resource_extraction.py
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,7 @@ def test_cli_SAM(runner, WindX_cls):
path = os.path.join(TESTDATADIR, 'wtk/ri_100_wtk_2012.h5')
with tempfile.TemporaryDirectory() as td:
truth_path = os.path.join(td, 'truth.csv')
WindX_cls.get_SAM_gid(100, gid, out_path=truth_path)
WindX_cls.get_SAM_gid(gid, 100, out_path=truth_path)
truth = pd.read_csv(truth_path, skiprows=1)

result = runner.invoke(main, ['-h5', path,
Expand All @@ -869,6 +869,58 @@ def test_cli_SAM(runner, WindX_cls):
LOGGERS.clear()


def test_windx_make_SAM_files(WindX_cls):
"""
Test WindX make_SAM_files method
"""
lat_lon = WindX_cls.lat_lon
gids = np.random.choice(len(lat_lon), 3)

path = os.path.join(TESTDATADIR, 'wtk/ri_100_wtk_2012.h5')
with tempfile.TemporaryDirectory() as td:
for gid in gids:
truth_path = os.path.join(td, f'truth_{gid}.csv')
WindX_cls.get_SAM_gid(gid, 100, out_path=truth_path)

test_path = os.path.join(td, 'test.csv')
WindX.make_SAM_files(path, gids, hub_height=100, out_path=test_path)
for gid in gids:
test_path = os.path.join(td, f'test_{gid}.csv')
truth_path = os.path.join(td, f'truth_{gid}.csv')
test = pd.read_csv(test_path, skiprows=1)
truth = pd.read_csv(truth_path, skiprows=1)
assert_frame_equal(truth, test)

WindX_cls.close()
LOGGERS.clear()


def test_nsrdbx_make_SAM_files(NSRDBX_cls):
"""
Test nsrdbx make_SAM_files method
"""
lat_lon = NSRDBX_cls.lat_lon
gids = np.random.choice(len(lat_lon), 3)

path = os.path.join(TESTDATADIR, 'nsrdb/ri_100_nsrdb_2012.h5')
with tempfile.TemporaryDirectory() as td:
for gid in gids:
truth_path = os.path.join(td, f'truth_{gid}.csv')
NSRDBX_cls.get_SAM_gid(gid, out_path=truth_path)

test_path = os.path.join(td, 'test.csv')
NSRDBX.make_SAM_files(path, gids, out_path=test_path)
for gid in gids:
test_path = os.path.join(td, f'test_{gid}.csv')
truth_path = os.path.join(td, f'truth_{gid}.csv')
test = pd.read_csv(test_path, skiprows=1)
truth = pd.read_csv(truth_path, skiprows=1)
assert_frame_equal(truth, test)

NSRDBX_cls.close()
LOGGERS.clear()


def test_cli_region(runner, WindX_cls):
"""
Test rex CLI region get
Expand Down