def test_check_areas_are_in_grid_and_format_argument_value(mock_grid): arg = ( ({"county": "Kentucky"}, mock_grid), ({"state": "California"}, mock_grid), ({"loadzone": "Texas"}, mock_grid), ({"state": "El Paso"}, mock_grid), ({"interconnect": "Nebraska"}, mock_grid), ) for a in arg: with pytest.raises(ValueError): _check_areas_are_in_grid_and_format(a[0], a[1])
def calculate_curtailment_time_series_by_resources_and_areas( scenario, areas=None, resources=None): """Calculate hourly curtailment of each generator fueled by resources and located in area(s). :param powersimdata.scenario.scenario.Scenario scenario: scenario instance. :param str/tuple/list/set resources: names of resources. Default is all renewable resources. :param dict areas: keys are area types ('*loadzone*', '*state*' or '*interconnect*'), values are a list of areas. Default is the scenario interconnect(s). :return: (*dict*) -- keys are areas, values are dictionaries whose keys are resources and values are data frames indexed by (timestamp, plant id). """ curtailment = calculate_curtailment_time_series(scenario) grid = scenario.state.get_grid() areas = (_check_areas_are_in_grid_and_format(areas, grid) if areas is not None else { "interconnect": grid.interconnect }) if resources is None: resources = grid.model_immutables.plants[ "renewable_resources"].intersection(set(grid.plant.type)) else: resources = _check_resources_are_renewable_and_format( resources, grid_model=scenario.info["grid_model"]) curtailment_by_resources_and_areas = ( decompose_plant_data_frame_into_resources_and_areas( curtailment, resources, areas, grid)) return curtailment_by_resources_and_areas
def decompose_plant_data_frame_into_areas(df, areas, grid): """Take a plant-column data frame and decompose it into plant-column data frames for areas. :param pandas.DataFrame df: data frame, columns are plant id in grid. :param dict areas: areas to use for decomposition. Keys are area types ('*loadzone*', '*state*', or '*interconnect*'), values are str/list/tuple/set of areas. :param powersimdata.input.grid.Grid grid: Grid instance. :return: (*dict*) -- keys are areas, values are plant-column data frames. """ _check_data_frame(df, "PG") plant_id = set(df.columns) _check_plants_are_in_grid(plant_id, grid) areas = _check_areas_are_in_grid_and_format(areas, grid) df_areas = {} for k, v in areas.items(): if k == "interconnect": for i in v: name = "%s interconnect" % " - ".join(i.split("_")) df_areas[name] = df[get_plant_id_in_interconnects(i, grid) & plant_id] elif k == "state": for s in v: df_areas[s] = df[get_plant_id_in_states(s, grid) & plant_id] elif k == "loadzone": for l in v: df_areas[l] = df[get_plant_id_in_loadzones(l, grid) & plant_id] return df_areas
def get_plant_id_in_interconnects(interconnects, grid): """Get plant id for plants in interconnect(s). :param str/list/tuple/set interconnects: name of interconnect(s). :param powersimdata.input.grid.Grid grid: Grid instance. :return: (*set*) -- list of plant id """ areas = _check_areas_are_in_grid_and_format( {"interconnect": interconnects}, grid) return set.union(*(set(grid.plant.groupby("interconnect").groups[i]) for i in areas["interconnect"]))
def get_plant_id_in_loadzones(loadzones, grid): """Get plant id for plants in loadzone(s). :param str/list/tuple/set loadzones: name of load zone(s). :param powersimdata.input.grid.Grid grid: Grid instance. :return: (*set*) -- list of plant id. """ areas = _check_areas_are_in_grid_and_format({"loadzone": loadzones}, grid) plant = grid.plant plant_id = plant[(plant.zone_name.isin(areas["loadzone"]))].index return set(plant_id)
def test_check_areas_are_in_grid_and_format(mock_grid): assert _check_areas_are_in_grid_and_format( { "state": {"Washington", "Kentucky", "NE", "TX", "WA"}, "loadzone": ["Washington", "East", "El Paso", "Bay Area"], "interconnect": "Texas", }, mock_grid, ) == { "interconnect": {"Texas"}, "state": {"Washington", "Kentucky", "Nebraska", "Texas"}, "loadzone": {"Washington", "East", "El Paso", "Bay Area"}, }
def get_plant_id_in_states(states, grid): """Get plant id for plants in state(s). :param str/list/tuple/set states: states(s) name or abbreviation(s). :param powersimdata.input.grid.Grid grid: Grid instance. :return: (*set*) -- list of plant id. """ areas = _check_areas_are_in_grid_and_format({"state": states}, grid) loadzones = set.union(*(grid.model_immutables.zones["state2loadzone"][i] for i in areas["state"])) plant = grid.plant plant_id = plant[(plant.zone_name.isin(loadzones))].index return set(plant_id)
def get_plant_id_in_interconnects(interconnects, grid): """Get plant id for plants in interconnect(s). :param str/list/tuple/set interconnects: name of interconnect(s). :param powersimdata.input.grid.Grid grid: Grid instance. :return: (*set*) -- list of plant id """ areas = _check_areas_are_in_grid_and_format({"interconnect": interconnects}, grid) loadzones = set.union( *( grid.model_immutables.zones["interconnect2loadzone"][i] for i in areas["interconnect"] ) ) plant = grid.plant plant_id = plant[(plant.zone_name.isin(loadzones))].index return set(plant_id)
def calculate_curtailment_time_series_by_areas(scenario, areas=None): """Calculate hourly curtailment by area(s). :param powersimdata.scenario.scenario.Scenario scenario: scenario instance. :param dict areas: keys are area types ('*loadzone*', '*state*' or '*interconnect*'), values are a list of areas. Default is the interconnect of the scenario. Default is the scenario interconnect. :return: (*dict*) -- keys are areas, values are data frames indexed by (datetime, plant id). """ curtailment = calculate_curtailment_time_series(scenario) grid = scenario.state.get_grid() areas = (_check_areas_are_in_grid_and_format(areas, grid) if areas is not None else { "interconnect": grid.interconnect }) curtailment_by_areas = decompose_plant_data_frame_into_areas( curtailment, areas, grid) return curtailment_by_areas
def test_check_areas_are_in_grid_and_format_argument_type(mock_grid): arg = (({"Texas", "El Paso"}, mock_grid), ({123: "Nebraska"}, mock_grid)) for a in arg: with pytest.raises(TypeError): _check_areas_are_in_grid_and_format(a[0], a[1])