Skip to content

Commit

Permalink
chore: create tests folder under scenario folder and cut down long lines
Browse files Browse the repository at this point in the history
  • Loading branch information
BainanXia committed Feb 21, 2020
1 parent da54b90 commit 1c71fcf
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 51 deletions.
142 changes: 91 additions & 51 deletions powersimdata/scenario/scenario_info.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from prereise.gather.constants import abv2state, state2loadzone, interconnect2loadzone
from prereise.gather.constants import abv2state, state2loadzone, \
interconnect2loadzone


class ScenarioInfo:
Expand All @@ -21,7 +22,8 @@ def __init__(self, scenario):
def area_to_loadzone(self, area):
"""Map the query area to a list of loadzones
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:return: (*set*) -- set of loadzones associated to the query area
:raise Exception: if area is invalid.
"""
Expand All @@ -41,15 +43,21 @@ def area_to_loadzone(self, area):
return loadzone_set

def check_time_range(self, start_time, end_time):
"""Check if the start_time and end_time define a valid time range of the given scenario
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:return: (*tuple*) -- a pair of integer indicates the index of the start timestamp and end timestamp in self.pg
"""Check if the start_time and end_time define a valid time range of
the given scenario
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*tuple*) -- a pair of integer indicates the index of
the start timestamp and end timestamp in self.pg
:raise Exception: if the time range is invalid.
"""
if (start_time not in self.pg.index) or (end_time not in self.pg.index):
print('Available time range [%s, %s]' % (str(self.pg.index[0]), str(self.pg.index[-1])))
if (start_time not in self.pg.index) or \
(end_time not in self.pg.index):
print('Available time range [%s, %s]' % (str(self.pg.index[0]),
str(self.pg.index[-1])))
raise Exception('Invalid time range')
start_i = self.pg.index.get_loc(start_time)
end_i = self.pg.index.get_loc(end_time)
Expand All @@ -59,51 +67,67 @@ def check_time_range(self, start_time, end_time):
return start_i, end_i

def get_capacity(self, gentype, area):
"""Calculate the total capacity of the query gentype in the query area of the given scenario
"""Calculate the total capacity of the query gentype in the query area
of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:return: (*float*) -- total capacity (in MW) based on the specified parameters
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:return: (*float*) -- total capacity (in MW) based on the
specified parameters
"""
loadzone_set = self.area_to_loadzone(area)
total_capacity = self.grid.plant[(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].isin(loadzone_set))]['Pmax'].sum()
total_capacity = self.grid.plant[
(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].isin(loadzone_set))]['Pmax'].sum()
if total_capacity == 0:
print('Warning: no such type of generator in the area specified!')
return float(total_capacity)

def get_generation(self, gentype, area, start_time, end_time):
"""Calculate the total generation of the query gentype in the query area during the query time range of the
given scenario
"""Calculate the total generation of the query gentype in the query
area during the query time range of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- total generation (in MWh) based on the specified parameters
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- total generation (in MWh)
based on the specified parameters
"""
loadzone_set = self.area_to_loadzone(area)
plant_id_list = list(self.grid.plant[(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].isin(loadzone_set))].index)
plant_id_list = list(self.grid.plant
[(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].
isin(loadzone_set))].index)
query_pg_df = self.pg[plant_id_list]
self.check_time_range(start_time, end_time)
total_generation = query_pg_df.loc[start_time:end_time].sum().sum()
return float(total_generation)

def get_profile_resource(self, gentype, area, start_time, end_time):
"""Calculate the total resource from profile of the query gentype in the query area during the query time
range of the given scenario
"""Calculate the total resource from profile of the query gentype in
the query area during the query time range of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- total resource from profile (in MWh) based on the specified parameters
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- total resource from profile (in MWh)
based on the specified parameters
:raise Exception: if the resource type is invalid
"""
loadzone_set = self.area_to_loadzone(area)
plant_id_list = list(self.grid.plant[(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].isin(loadzone_set))].index)
plant_id_list = list(self.grid.plant
[(self.grid.plant['type'] == gentype) &
(self.grid.plant['zone_name'].
isin(loadzone_set))].index)
if gentype not in self.profile:
raise Exception('Invalid resource type')
query_profile_df = self.profile[gentype][plant_id_list]
Expand All @@ -112,50 +136,66 @@ def get_profile_resource(self, gentype, area, start_time, end_time):
return float(total_resource)

def get_curtailment(self, gentype, area, start_time, end_time):
"""Calculate the curtailment of the query gentype in the query area during the query time range of the
given scenario
"""Calculate the curtailment of the query gentype in the query
area during the query time range of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- curtailment percentage (rounded up to two decimals) based on the specified parameters
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- curtailment percentage (rounded up to
two decimals) based on the specified parameters
"""
total_generation = self.get_generation(gentype, area, start_time, end_time)
total_profile_resource = self.get_profile_resource(gentype, area, start_time, end_time)
total_generation = self.get_generation(gentype, area,
start_time, end_time)
total_profile_resource = self.get_profile_resource(gentype, area,
start_time,
end_time)
if total_profile_resource == 0 and total_generation == 0:
return 0
curtailment = round(1 - (total_generation / total_profile_resource), 4)
return float(curtailment)

def get_capacity_factor(self, gentype, area, start_time, end_time):
"""Calculate the capacity factor of the query gentype in the query area during the query time range of the
given scenario
"""Calculate the capacity factor of the query gentype in the
query area during the query time range of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- capacity factor based on the specified parameters
"""
start_i, end_i = self.check_time_range(start_time, end_time)
total_hours = end_i - start_i + 1
total_capacity = self.get_capacity(gentype, area)
if total_capacity == 0:
raise ZeroDivisionError('Division by zero')
total_generation = self.get_generation(gentype, area, start_time, end_time)
total_generation = self.get_generation(gentype, area,
start_time, end_time)
cf = round(total_generation / (total_hours * total_capacity), 4)
return float(cf)

def get_no_congest_capacity_factor(self, gentype, area, start_time, end_time):
"""Calculate the no congestion capacity factor of the query gentype in the query area during the query time
range of the given scenario
def get_no_congest_capacity_factor(self, gentype, area,
start_time, end_time):
"""Calculate the no congestion capacity factor of the query gentype
in the query area during the query time range of the given scenario
:param str gentype: type of generator
:param str area: one of: *loadzone*, *state*, *state abbreviation*, *interconnect*, *'all'*
:param str start_time: start timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format *'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- no congestion capacity factor based on the specified parameters
:param str area: one of: *loadzone*, *state*, *state abbreviation*,
*interconnect*, *'all'*
:param str start_time: start timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:param str end_time: end timestamp in the format
*'YYYY-MM-DD HH:MM:SS'*
:return: (*float*) -- no congestion capacity factor based
on the specified parameters
"""
cf = self.get_capacity_factor(gentype, area, start_time, end_time)
curtailment = self.get_curtailment(gentype, area, start_time, end_time)
Expand Down
Empty file.
File renamed without changes.

0 comments on commit 1c71fcf

Please sign in to comment.