2012-08-09 44 views
0

有人可以幫助我使用Python。我正在嘗試運行168牛頓raphson潮流研究,以獲得不同的荷載和一族值。我在excel電子表格中列出了這些值,並希望自動將這些值上傳到PSSE,運行模擬,然後將總線電壓結果導出到另一個電子表格,以便在單獨的列中顯示所有168個SIM卡。PSSE/Python從PSSE導入excel值和導出總線電壓

我有以下的代碼,但它顯示錯誤與線47: 高清runstudy(小時值) 語法錯誤:無效的語法

我不知道如何糾正這一點。

任何幫助,將不勝感激

from __future__ import with_statement 
from collections import defaultdict 
import csv 

import psspy 


CSV_PQ_FILENAME = 'c:/Users\14088757\Desktop\EP401_Simulation Values_1.1.csv' 
OUTPUT_CSV_FILENAME = 'c:/Users\14088757\Desktop\EP401_Simulation Values_1.1.csv' 
STUDY_BASECASE = 'c:/Users\14088757\Desktop\EP401_PSSE_URA_20120716_6626.01.sav' 


with open(CSV_PQ_FILENAME) as csvfile: 
    reader = csv.reader(csvfile) 
    headers = reader.next() 
    data = list(reader) 

# now all the data from that sheet is in 'data' 
# the columns are: 

# load?, bus num, id, p or q, 6625, 6626, .... 

hourly_data = defaultdict(list) 

hour_headings = headers[4:] 

for row in data: 
    isload, busnum, _id, porq = row[:4] 
    hour_values = row[4:] 

    # group all of the hourly data together in one list. 
    for hour, value in zip(hour_headings, hour_values): 
     hourly_data[hour].append(
      (int(isload), int(busnum), _id, porq, float(value)) 

# hourly_data now looks like this: 
# { '6626': [ 
#    (1, 104, '1', 'P', 0.33243) 
#    ... 
#   ], 
# '6627': [ ... ] 
# '6628': [ ... ] 
#} 

# lets run some simulations. 

def runstudy(hour, values) 

    # reload the base case. 
    psspy.case(STUDY_BASECASE) 

    # assume CSV has columns as described in the doc string 
    for isload, bus, _id, porq, value in values: 

     if isload: 
      if porq.lower() == 'p': 
       psspy.load_data_3(bus, _id, realar1=value) 
      elif porq.lower() == 'q': 
       psspy.load_data_3(bus, _id, realar2=value) 
     else: 
      psspy.machine_data_2(bus, _id, realar1=value) 

    # solve case after adding all loads/machines. 
    psspy.fnsl() 

    # return the bus voltages. 
    # now to write the new bus voltages to a file. 
    ierr, (voltages,) = psspy.abusreal(sid=-1, string=["PU"]) 
    ierr, (buses,) = psspy.abusint(sid=-1, string=["NUMBER"]) 

    return buses, voltages 


# here we assume the buses will always be the same, no need 
# keep storing them. I'll only store the voltages. 
all_results = {} 
for hour, values in hourly_data.items(): 
    buses, voltages = runstudy(hour, values) 
    all_results[hour] = voltages 

# now write all the results to a csv file 
# CSV will have these columns: 


# bus number, voltage (pu) 2265, 2267, ... 
spreadsheet_results = [['buses'] + buses] 
for hour, voltages in all_results.items(): 
    spreadsheet_results.append([hour + ' pu'] + voltages) 

# spreadsheet_results now looks like the one we want to write to the CSV. 
with open(OUTPUT_CSV_FILENAME, 'wb') as output: 
    output.writerows(spreadsheet_results) 

回答

1

吉姆,

有一個簡單的語法錯誤。我相信你現在已經注意到了。這樣做 -

def runstudy(hours, values): 

除非您共享您的.sav和.csv文件,否則對代碼的其餘部分我不能多說。我想知道它是如何發展的,特別是因爲我對PSSE感興趣。如果您需要更多幫助,請將文件郵寄給我。