Ask Your Question
0

abuschar data to excel

asked 2021-05-01 14:11:21 -0500

Eneko gravatar image

I'm trying to extract the information contained in a psse .raw file into a Excel file.

I'm doing pretty fine with data obtained from 'abusint' functions but I getting an error when I try to do the same with data obteined from ''abuschar'.

I guess the error that I'm getting is related to the data type that comes out from 'abuschar' (string type data instead of an integer).

I've not tried yet but i think i'll have the same issue when i'll use 'abrncplx' to get complex impedances of branches.

I'm using pandas to write data into Excel.

I'll be most grateful if somebody could help me with this "psse + pandas" related issue!!

Thanks.

NOTE: Find next the code that I'm using for that:

 # Base case is loaded ================================================================================================================================
psspy.case(path + "\\" + base_case)


# Data is read from .raw file ========================================================================================================================
ierr, BusNumber     = psspy.abusint(sid, string='NUMBER')
ierr, BusName       = psspy.abuschar(sid, string='NAME')
ierr, FromName      = psspy.abrnchar(sid, string='FROMNAME')
ierr, ToName        = psspy.abrnchar(sid, string='TONAME')
ierr, FromNum       = psspy.abrnint(sid, string='FROMNUMBER')
ierr, ToNum         = psspy.abrnint(sid, string='TONUMBER')
ierr, Length        = psspy.abrnreal(sid,string='LENGTH')
ierr, Impedance     = psspy.abrncplx(sid,string='RX')
ierr, Losses        = psspy.abrncplx(sid,string='PQLOSS')
ierr, MachNum       = psspy.amachint(sid, string='NUMBER')
ierr, MachStatus    = psspy.amachint(sid, string='STATUS')
ierr, MachID        = psspy.amachchar(sid, string='ID')
ierr, LoadNum       = psspy.aloadint(sid, string='NUMBER')
ierr, LoadStatus    = psspy.aloadint(sid, string='STATUS')
ierr, LoadValue     = psspy.aloadreal(sid, string='MVAACT')
ierr, LoadID        = psspy.aloadchar(sid, string='ID')


## PSSE returns a "list of lists" for each of the subsystem data retrieval functions
## We need to pull out the first element of each to make it easier to work with:
BusNumber   = BusNumber[0]
BusName     = BusName[0]
FromNum     = FromNum[0]
ToNum       = ToNum[0]
FromName    = FromName[0]
ToName      = ToName[0]
Length      = Length[0]
Impedance   = Impedance[0]
Losses      = Losses[0]
MachNum     = MachNum[0]  
MachStatus  = MachStatus[0]
MachID      = MachID[0]    
LoadNum     = LoadNum[0]   
LoadStatus  = LoadStatus[0]
LoadValue   = LoadValue[0] 
LoadID      = LoadID[0]


BusList = []
for i in range(len(BusNumber)):
    BusList.append([BusNumber[i], BusName[i]])

BranchList = []
for i in range(len(FromNum)):
    BranchList.append([FromNum[i], ToNum[i], FromName[i], ToName[i], Length[i], Impedance[i], Losses[i]])

MachineList = []
for i in range(len(MachNum)):
    MachineList.append([MachNum[i], MachStatus[i], MachID[i]])

LoadList = []
for i in range(len(LoadNum)):
    LoadList.append([LoadNum[i], LoadStatus[i], LoadValue[i], LoadID[i]])


busNumber   = []
busName     = []
fromBus     = []
toBus       = []
fromName    = []
toName      = []
length      = []
impedance   = []
losses      = []
machNum     = []
machStatus  = []
machID      = []
loadNum     = []
loadStatus  = []
loadValue   = []
loadID      = []


for i in range(len(BusList)):
    busNumber.append(BusList[i][0])
    busName.append(BusList[i][1])

for i in range(len(BranchList)):
    fromBus.append(BranchList[i][0])
    toBus.append(BranchList[i][1])
    fromName.append(BranchList[i][2])
    toName.append(BranchList[i][3])
    length.append(BranchList[i][4])
    impedance.append(BranchList[i][5])
    losses.append(BranchList[i][6])

for i in range(len(MachineList)):
    machNum.append(MachineList[i][0])
    machStatus.append(MachineList[i][1])
    machID.append(MachineList[i][2 ...
(more)
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2021-05-17 15:24:49 -0500

Eneko gravatar image

it works with a simple and plain to_csv

edit flag offensive delete link more
0

answered 2021-05-03 16:33:35 -0500

jconto gravatar image

updated 2021-05-05 14:10:25 -0500

The pandas dataframes looks OK. Make sure that "path", Excel_file" are defined and pandas import works.

Define sid before calling 'abusint' and similar functions. Set sid=-1 for all buses.

Using 'savnw.sav' and engine set to xlsxwriter, I got an xlsx file output:

#writer = pd.ExcelWriter(filename, engine='openpyxl') 
writer = pd.ExcelWriter(filename, engine='xlsxwriter') 
#wb  = writer.book

df_nodes.to_excel(writer, sheet_name = 'NODES')
df_branches.to_excel(writer, sheet_name = 'BRANCHES')

#wb.save(filename)
writer.save()

Another way is to use pandas' dataframe to_excel method:

# from https://www.geeksforgeeks.org/dataframe-to_excel-method-in-pandas/
import pandas as pd 

# dictionary of data 
dct = {'ID': {0: 23, 1: 43, 2: 12, 3: 13, 4: 67, 5: 89, 
              6: 90, 7: 56, 8: 34}, 
      'Name': {0: 'Ram', 1: 'Deep', 2: 'Yash', 3: 'Aman', 
               4: 'Arjun', 5: 'Aditya', 6: 'Divya', 7: 'Chalsea', 8: 'Akash' }, 
      'Marks': {0: 89, 1: 97, 2: 45, 3: 78, 
                4: 56, 5: 76, 6: 100, 7: 87, 8: 81}, 
      'Grade': {0: 'B', 1: 'A', 2: 'F', 3: 'C', 4: 'E', 5: 'C', 6: 'A', 7: 'B', 8: 'B'} 
    } 

# forming dataframe
data = pd.DataFrame(dct) 

# storing into the excel file
data.to_excel("output.xlsx")
edit flag offensive delete link more

Comments

Many thanks for your answer. I modified my code accordingly and it looks like I passed through the line where so far my code was crashing. Anyway, I'm still getting an error with "writer.save()" I planned to paste the error message that I got but it seems the length in this window is limited.

Eneko gravatar imageEneko ( 2021-05-05 08:11:50 -0500 )edit

should I consider something else to make the command "writer.save()" work as it should?

Eneko gravatar imageEneko ( 2021-05-05 08:12:29 -0500 )edit

is the python module "xlsxwriter" installed?

jconto gravatar imagejconto ( 2021-05-05 08:41:58 -0500 )edit

yes, it is installed and imported "import xlsxwriter"

Eneko gravatar imageEneko ( 2021-05-05 09:34:09 -0500 )edit

See my edited answer above.

jconto gravatar imagejconto ( 2021-05-05 14:11:29 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

Stats

Asked: 2021-05-01 14:11:21 -0500

Seen: 422 times

Last updated: May 17 '21