Skip to content

Commit

Permalink
merge dev branch to master, v0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
luisnovaish committed Dec 21, 2020
2 parents 89d9a9a + 73f6dd3 commit c77a98d
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 41 deletions.
71 changes: 41 additions & 30 deletions ImPSCAD/PSCADVar.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,41 @@
import re
import math
import pandas as pd
import numpy as np
from functools import reduce
from pathlib import PurePath

def PSCADVar(INF_path):
def PSCADVar(path, file_name, del_out = False):

with open(INF_path) as myfile: # Open all the lines in the inf path
''' Function which will compile all the .out files exported by PSCAD in a .csv file '''


# create the extensions that will be used on the code
inf_ex = '.inf'
out_ex = '.out'
csv_ex = '.csv'

INF_path = PurePath(path, file_name + inf_ex) # create path for the .inf file, which has the header

with open(INF_path) as myfile: # Open all the lines in the inf path
lines = myfile.readlines()

n_col = 11 # Number of columns that the txt has
n_var = len(lines) # Number of PSCAD Variables
n_files = int(math.ceil(n_var / 10.0)) # Number of .out Files exported (the floating is important)
n_var_tot = n_var + n_files # Number of variables INCLUDING additional time column FOR EACH .OUT
n_var_last = n_var_tot -(n_files-1)*n_col # Number of variables in the last file
n_var_exp = n_var + 1 # Number of variables that will be exported

# Create list to store the number of columns of each file
last_col = [0]*n_files

for ii in range(0,n_files):
for file_ in range(0,n_files):

if ii != n_files-1:
b = n_col
else:
b = n_var_last

last_col[ii] = b
numb_columns = n_col if file_ != n_files-1 else n_var_last

last_col[file_] = numb_columns

pattern = "Desc=\"(.*?)\"" # Patter that holds the variable name
header = [0] * (n_var_tot)
header = [0]*(n_var_tot)

b = 0 # to "freeze" the time when storing the "TIME"
c = 1 # used in the index: from 1 to 11
Expand All @@ -39,16 +45,16 @@ def PSCADVar(INF_path):
# so this "for" writes time if it is the time column, or takes the header from
# the array "header"

for a in range(0, n_var_tot):
for var in range(0, n_var_tot):

if ((a == 0) | (a % 11 == 0)): # if it is the first value of all files, store the time name
header[a] = 'time' # to the object outside
if ((var == 0) | (var % 11 == 0)): # if it is the first value of all files, store the time name
header[var] = 'time' # to the object outside
b = b - 1
if a != 0:
if var != 0:
c = 1
else:
header[a] = re.search(pattern, lines[b]).group(1)
header[a] = header[a].replace(" ", "_")
header[var] = re.search(pattern, lines[b]).group(1)
header[var] = header[var].replace(" ", "_")

b = b + 1
c = c + 1
Expand All @@ -62,14 +68,14 @@ def PSCADVar(INF_path):

for ii in range(0, n_files):
if (ii < 9):
OUT_name[ii] = "_0" + str(ii + 1) + ".out" # Create ending "_0ii".out
OUT_path[ii] = INF_path.replace(".inf", OUT_name[ii]) # replace in INF_path the.inf for the new end
OUT_name[ii] = "_0" + str(ii + 1) + out_ex # Create ending "_0ii".out
else:
OUT_name[ii] = "_" + str(ii + 1) + ".out" # Create ending "_0ii".out
OUT_path[ii] = INF_path.replace(".inf", OUT_name[ii]) # replace in INF_path the.inf for the new end
OUT_name[ii] = "_" + str(ii + 1) + out_ex # Create ending "_ii".out

OUT_path[ii] = PurePath(path, file_name + OUT_name[ii]) # replace in INF_path the.inf for the new end

# Creates the path of the .CSV that's gonna be writen
CSV_path = INF_path.replace(".inf", ".csv") # Replaces the ending .inf with .csv
CSV_path = PurePath(path, file_name + csv_ex) # Replaces the ending .inf with .csv

########################################################
### Treat the data
Expand All @@ -80,20 +86,25 @@ def PSCADVar(INF_path):

# Read all of the .out files and append them to the empty list
for path in OUT_path:
dff.append(pd.read_csv(path, delim_whitespace=True, header=None))
dff.append(pd.read_csv(path, delim_whitespace = True, header = None))

if del_out:
os.remove(path)

# Create auxiliar variables
ii = 0
jj = 0

# Rename the header of each file (which is now 0,1,2,3,4...) for the names that are storage at the variable header
for file in range(0, n_files):
for ii in range(0, last_col[file]):
dff[file].rename(columns = {ii:header[ii+jj]}, inplace = True)
for file_ in range(0, n_files):
for ii in range(0, last_col[file_]):
dff[file_].rename(columns = {ii:header[ii + jj]}, inplace = True)
jj = jj + 11

# Now that the headers are ok, merge all of them (2 by 2) using the column 'time' as reference
df_merged = reduce(lambda left,right: pd.merge(left,right,on='time'), dff)
# Now that the headers are done, merge all of them (2 by 2) using the column 'time' as reference
df_merged = reduce(lambda left, right: pd.merge(left, right, on = 'time'), dff)

# Storage all the dataframe into one csv file with header
df_merged.to_csv(CSV_path,index=False)
df_merged.to_csv(CSV_path, index = False)

return
31 changes: 23 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@ __________________
## Description

In order to solve this problem, this package was created: by using the function PSCADVar, it will create a unique .csv file which contains all of the variables with
the proper header. In the previous version (ImPSCAD_v01) PSCADVar was a class, but in order to improve performance, it is now a function.
the proper header. Besides that, once the .out files can get quite big, it is possible to delete them all after reading the values.

Once one execute the function PSCADVar, the .csv file will be created in the same folder as the .inf/.out files are. It is recommended that one use the package Pandas
in order to use all this data.
in order to use all this data later.


### Parameters

**INF_path:** str, mandatory.
**path:** str, mandatory.

- It's the path of the .inf file
- Example: "C:\Users\Your_Name\PSCAD\simulation_project.gf46\noname.inf"
- It's the path of the directory where the .out file
- Example: r"C:\Users\Your_Name\PSCAD\simulation_project.gf46"

**file_name:** str, mandatory.

- It's the name of the .out file. Must be written without the extension
- Example: "svm_inv"

**delete_out:** boolean, optional.

- After reading, if True it will delete all the .out files that were read
- Default: False

___________________

Expand All @@ -43,11 +53,16 @@ $ pip install ImPSCAD
```Python
from ImPSCAD import PSCADVar

path = r"C:\Users\Your_Name\PSCAD\simulation_project.gf46\noname.inf"
PSCADVar(path) # after running this line, just read the .csv file using Pandas
```
path = r"C:\Users\Your_Name\PSCAD\simulation_project.gf46" # use r to avoid unicode problems
file_name = "svm_inv"

PSCADVar(path, file_name, delete_out = True)

# Tip: Read the .csv file using Pandas

csv_path = r"C:\Users\Your_Name\PSCAD\simulation_project.gf46\svm_inv.csv"
variables = pd.read_csv(csv_path)
```
___________________

## License
Expand Down
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
setup(
name = 'ImPSCAD',
packages = ['ImPSCAD'],
version = '0.2.1',
license='MIT',
version = '0.3.0',
license ='MIT',
description = 'Create a unique csv file from a out file from PSCAD/EMTDC simulations',
author = 'Luis Arthur Novais Haddad',
author_email = 'luis.novais@engenharia.ufjf.br',
Expand All @@ -12,7 +12,7 @@
keywords = ['PSCAD', 'Variables', 'Import','.CSV','python','MATLAB'],
install_requires=[
'pandas',
'numpy',
'pathlib',
],
classifiers=[
'Development Status :: 4 - Beta',
Expand Down

0 comments on commit c77a98d

Please sign in to comment.