def load_actions(power, base_path, out_path, files, fmt='on', st=True, wmlf=True): """ 从文件加载并执行修改动作 :param power: Power. Power实例。 :param base_path: str. 基础数据目录。 :param out_path: str. 输出目录,输入的动作文件也在这里。 :param files: [str]. 文件列表。 :param fmt: str. 数据格式类型。 :param st: bool. 是否输出ST*文件。 :param wmlf: bool. 是否计算潮流。 :return dict. 每个文件对应目录的输出是否成功(潮流是否收敛) """ ret = {} if not power: power = Power(fmt=fmt) power.load_power(base_path, fmt=fmt, lp=False, st=st) power.set_index(idx='name') for f in files: actions = pd.read_csv(os.path.join(out_path, f), encoding='gbk', index_col=False) for _, etype, idx, dtype, value in actions.itertuples(): set_values(power.data, etype, dtype, {idx: value}) if '.' in f: name = f[:f.index('.')] path = os.path.join(out_path, name) power.save_power(path, fmt=power.fmt, lp=False, st=st) shutil.copy(os.path.join(base_path, 'LF.L0'), path) if st: shutil.copy(os.path.join(base_path, 'ST.S0'), path) if wmlf: call_wmlf(path) ret[name] = check_lfcal(path) else: ret[name] = True return ret
if not power: power = Power(fmt=fmt) power.load_power(base_path, fmt=fmt, lp=False, st=st) power.set_index(idx='name') for f in files: actions = pd.read_csv(os.path.join(out_path, f), encoding='gbk', index_col=False) for _, etype, idx, dtype, value in actions.itertuples(): set_values(power.data, etype, dtype, {idx: value}) if '.' in f: name = f[:f.index('.')] path = os.path.join(out_path, name) power.save_power(path, fmt=power.fmt, lp=False, st=st) shutil.copy(os.path.join(base_path, 'LF.L0'), path) if st: shutil.copy(os.path.join(base_path, 'ST.S0'), path) if wmlf: call_wmlf(path) ret[name] = check_lfcal(path) else: ret[name] = True return ret if __name__ == '__main__': base_path = os.path.join(os.path.expanduser('~'), 'data', 'wepri36', 'wepri36') out_path = os.path.join(os.path.expanduser('~'), 'data', 'wepri36', 'actions') fmt = 'off' power = Power(fmt) power.load_power(base_path, fmt=fmt, lp=False, st=False) power.set_index(idx='name') ret = load_actions(power, base_path, out_path, files=['1.txt', '2.txt'], st=False, wmlf=True)