# ------------------------------------------------------------------------------------------------------------------ ## This code is for checking Overloading transformers conditions ## Input Files : >> CSV of Timestamp and Simulated measured Power (VA) ## Output >> Overloaded Transformers depend on % of Overloaded condition provided with the duration of Overloading ## To be able to check overloading conditions, the user must specify the rated value of the distribution transformers # ------------------------------------------------------------------------------------------------------------------ import pandas as pd import argparse import time import numpy as np import os def get_power_rates(cols): power_rates = [] # compare against the given Y value for i in cols: power_rates.append(float(input(f"enter {i} Power Rate: "))) return power_rates def get_file_names(): files = [] for c in cols: files.append(input(f"enter name of output file for {c}: ")) return files def query_csv(): vals = [] for y,c in zip(power_rates,cols): vals.append(csv.query(f"`{c}` >= {y}")) return vals def first_version(): ''' first verison just writes the query results to their respective files ''' for v,f,c in zip(query_results,output_files,cols): v[["# timestamp",f"{c}"]].to_csv(f"{f}",index=False) return def query(df, col, t_delta, ep, pr,step): sz = csv.shape[0] res = [] result = [] df = csv[['# timestamp',col,'timestamp']] # change from csv to df i = 0 steps = pd.Timedelta(step,unit='m') if i 0: res = sum(res,[]) df = pd.DataFrame(res) print(df) specs[f"#{50*'-'} {FILE}--(200%) {50*'-'}"] = df # ------------- 150% ------------- x = 1.5 * pr # 10% of x ep = 0.1 * x print(f"{50*'-'} calculating 150% {50*'-'}") res = query(csv,col,60 ,ep,x, 10) # 60 minutes and 10 minutes step ( in case that changes in the future) if len(res) > 0: res = sum(res,[]) df = pd.DataFrame(res) print(df) specs[f"#{50*'-'} {FILE}--(150%) {50*'-'}"] = df # ------------- 125% ------------- x = 1.25 * pr # 10% of x ep = 0.1 * x print(f"{50*'-'} calculating 125% {50*'-'}") res = query(csv,col,240 ,ep,x, 10) # 240 minutes and 10 minutes step ( in case that changes in the future) if len(res) > 0: res = sum(res,[]) df = pd.DataFrame(res) print(df) specs[f"#{50*'-'} {FILE}--(125%) {50*'-'}"] = df return specs def write_output(fname,output): if os.path.exists(fname): os.remove(fname) for DICT in output: if DICT: for k,v in DICT.items(): with open(fname,'a+') as f: f.write(k+'\n') del v['timestamp'] v.to_csv(fname,mode='a',index=False) return parser = argparse.ArgumentParser(description='.') parser.add_argument('-f', type=str, help='input file name',default='') parser.add_argument('-d', type=str, help='input dir name',default='') parser.add_argument('-m', type=int, help='mode of operation (0: old version; 1: percentage mode)', default=1) args = parser.parse_args() FILE = args.f # grab file name mode = args.m # grab the mode (version of file) DIR = args.d # grab dir if FILE == '' and DIR=='': parser.print_help() exit(0) if mode == 0: if FILE == '': parser.print_help() exit(0) (csv,cols) = open_file(f'{FILE}') power_rates = get_power_rates(cols) # get power rates query_results = query_csv() output_files = get_file_names() # change columns to floats for i in cols: csv[f"{i}"] = csv[f"{i}"].astype(float) first_version() elif mode == 1: output = [] if DIR == '': parser.print_help() exit(0) # read all files in the given directory if DIR[-1] != '/': DIR += '/' for FILE in os.listdir(DIR): print(f"{'*'*50} {FILE} {'*'*50}") (csv,cols) = open_file(f'{DIR}{FILE}') power_rates = get_power_rates(cols) # get power rates res = second_version(csv) output.append(res) write_output('output.csv',output) # second_version() #--------------------------------------------------------------------------------------------------------