diff --git a/pp2psdm/grid.py b/pp2psdm/grid.py index d9e7347..f6d7ee9 100644 --- a/pp2psdm/grid.py +++ b/pp2psdm/grid.py @@ -46,64 +46,35 @@ def convert_grid( return net, uuid_idx def convert_nodes(grid): - nodes_data = {} - df = grid.bus - - list_node_id = [] - nodes_geo_position_list = [] - list_subnets = [] - v_rated_list = [] - v_target_list = [] - volt_lvl_list = [] - operates_from_list = [] - operates_until_list = [] - slack_list = [] - - for idx, row in df.iterrows(): - - # Get operation times - operates_from, operates_until = get_operation_times(row) - - # Determine the voltage level (vlt_lvl or vn_kv) - if "vlt_lvl" in df.columns and not pd.isna(row["vlt_lvl"]): - volt_lvl = row["vlt_lvl"] - else: - volt_lvl = row["vn_kv"] - # Determine the subnet (subnet, zone, or fixed number) - if "zone" in df.columns and not pd.isna(row["zone"]): - subnet = row["zone"] - else: - subnet = 101 + def get_default(value, default): + return value if not pd.isna(value) else default - # In case there is a uuid column use it else generate uuids - if "uuid" in df.columns and not pd.isna(row["uuid"]): - uuid = row["uuid"] - else: - uuid = uuid4().__str__() - - list_node_id = list_node_id + [row["name"]] - nodes_geo_position_list = nodes_geo_position_list + [None] - list_subnets = list_subnets + [subnet] - v_rated_list = v_rated_list + [row["vn_kv"]] - v_target_list = v_target_list + [row["vn_kv"]] - volt_lvl_list = volt_lvl_list+[volt_lvl] - operates_from_list = operates_from_list + [operates_from] - operates_until_list = operates_until_list + [operates_until] - slack_list=slack_list + ['false'] - - data_dict = { - "id": list_node_id, - "geo_position": nodes_geo_position_list, - "subnet": list_subnets, - "v_rated": v_rated_list, - "v_target": v_target_list, - "volt_lvl": volt_lvl_list, - "slack": slack_list, + "id": df["name"].tolist(), + "geo_position": [None] * len(df), + "subnet": [ + get_default(row.get("zone"), 101) + for _, row in df.iterrows() + ], + "v_rated": df["vn_kv"].tolist(), + "v_target": df["vn_kv"].tolist(), + "volt_lvl": [ + get_default(row.get("vlt_lvl"), row["vn_kv"]) + for _, row in df.iterrows() + ], + "slack": ['false'] * len(df), + "operates_from": [ + get_operation_times(row)[0] + for _, row in df.iterrows() + ], + "operates_until": [ + get_operation_times(row)[1] + for _, row in df.iterrows() + ] } - + return create_nodes(data_dict)