First time here? We are a friendly community of Power Systems Engineers. Check out the FAQ!

Ask Your Question
1

Import load (MW/Mvar) and Generation (MW) from CSV file to PSS/E case

asked Mar 11 '2

psse_user_88 gravatar image

updated Mar 17 '2

Hello, I am trying to make a script in python that reads from CSV files the load and generation values and updates the values in the PSS/E sav case for one timestamp. csv file structure for load looks like this:

node number, load MW, load Mvar 10004, 5, 5 10006, 5, 5 . . .

The status of the script is as follows:

import os
import sys

PSSE_LOCATION = r"C:\Program Files (x86)\PTI\PSSE33\PSSBIN"
sys.path.append(PSSE_LOCATION)
os.environ['PATH'] = os.environ['PATH'] + ';' + PSSE_LOCATION

import psspy
_i = psspy.getdefaultint()
_f = psspy.getdefaultreal()
_s = psspy.getdefaultchar()
psspy.psseinit(100000)
psspy.throwPsseExceptions = True

STUDY_CASE = 'C:\\path\\20220301_0030_FO1.sav'

psspy.case(STUDY_CASE)
#My updated code is below, changed identation
with open("1.csv", 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        loadbus = int(row[0])
        loadid = '1'  # or loaddatalst [x], x is position of load id in row
        loadp = float(row[1])
        loadq = float(row[2])
        psspy.load_data_4(loadbus, loadid, [_i, _i, _i, _i, _i, _i], [loadp, loadq, 0, 0, 0, 0])


psspy.fnsl([0, 0, 0, 0, 0, 0, 99, 0])
psspy.area_2(0, 1, 1)
psspy.save(STUDY_CASE)
psspy.rawd_2(0, 1, [1, 1, 1, 0, 0, 0, 0], 0, r"""20220301_0030_FO1.raw""")

The part I am struggling with is defining some way of importing the CSV list in python and applying the load (MW/Mvar) in each load node.

Thank you.

to follow up after JConto answer, my code for updating load and generation in PSSE case looks like this:

#read load p and q
with open('load.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
   loadbus = int(row[0])
   loadid = '1'  # or loaddatalst [x], x is position of load id in row
   loadp = float(row[1])
   loadq = float(row[2])
   psspy.load_data_4(loadbus, loadid, [_i, _i, _i, _i, _i, _i], [loadp, loadq, 0, 0, 0, 0])

#read generators
with open('gen.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
    genbus = int(row[0])
    genid = str(row[1])  # or loaddatalst [x], x is position of load id in row
    status = int(row[2])
    genp = float(row[3])
    genq = float(row[4])
    psspy.machine_chng_2(genbus, genid, [status, _i, _i, _i, _i, _i],[genp, genq, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f, _f])

1 answer

Sort by » oldest newest most voted
2

answered Mar 11 '2

jconto gravatar image

updated Mar 16 '2

Load data in a CSV file can be read as follow:

import csv
with open('loaddata.csv', 'r') as file:
     reader = csv.reader(file)
for row in reader:
    #format=node number, load MW, load Mvar
    #             10004,       5,         5
    #loaddatalst = row.split(',')
    loadbus = int(loaddatalst[0])
    loadid = '1'             #or loaddatalst [x], x is position of load id in row
    loadp = float(loaddatalst[1])
    loadq = float(loaddatalst[2])
    psspy.load_data_4(loadbus, loadid,[_i,_i,_i,_i,_i,_i],[loadp, loadq, 0, 0, 0, 0])

With a second "CSV read block" the data for generators can be updated. Search the internet for "python CSV read", converting data types and the PSSe API for fuctions and data types needed as parameters.

link

Comments

Thank you JConto, I am getting this error: Traceback (most recent call last): File "C:/path/lexon.py", line 28, in <module> for row in reader: ValueError: I/O operation on closed file Is it indentation issue?

psse_user_88 gravatar imagepsse_user_88 (Mar 13 '2)

please see updated code. I am getting: loaddatalst = row.split(",") AttributeError: 'list' object has no attribute 'split'

psse_user_88 gravatar imagepsse_user_88 (Mar 13 '2)

I was able to solve by removing loaddatalst. The for row loop had done the splitting already. Thank you JConto

psse_user_88 gravatar imagepsse_user_88 (Mar 14 '2)

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.
Want to format code in your answer? Here is a one minute demo on Youtube

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: Mar 11 '2

Seen: 629 times

Last updated: Mar 17 '22