Ask Your Question

chip's profile - activity

2018-09-13 13:55:55 -0500 received badge  Nice Question (source)
2017-10-30 20:29:23 -0500 answered a question ImportError: DLL Load failed:...

Troubleshooting DLL imports

In general the error message:

ImportError: DLL load failed: The specified module could not be found.

Indicates that the python interpreter can not find the library it is looking for. There are a couple steps I take to debug this issue.

Does the path actually exist

Be paranoid and check in the python script that the path actually exists:

import os 
PSSE_PATH = r"C:\Program Files (x86)\PTI\PSSExplore34\PSSPY27" 
if not os.path.exists(PSSE_PATH):    
    print "Bad path"

Is the dll/pyd in the path?

There is a file out there called psspy.dll or psspy.pyd that contains the psspy library. It's like this for any library. Make sure the directory contains the psspy library file. I would just do this on the file system, but lets be paranoid and do it in the script:

# continued from previous script
import glob
fns = glob.glob(os.path.join(PSSE_PATH, 'psspy.*'))
if len(fns) == 0:
   print "Who moved my libraries"

Broken libraries

This is not what is happening to you, but I include it because it is another common class of problems you have when importing a compiled library. You may get an error message which indicates that the library load failed for another reason, like:

ImportError: DLL load failed: %1 is not a valid Win32 application.

In most cases these are caused by a 32/64 bit issue.

2017-10-30 20:08:20 -0500 commented question ImportError: DLL Load failed:...

Is that the correct path? I would expect it to be `C:\Program Files (x86)\PTI\PSSExplore34\PSSBIN`. And there should be a some libraries in it (`.pyd` or `.dll` files)

2016-05-09 21:11:38 -0500 marked best answer Interacting with PSSE GUI

I'm trying to determine what hooks the PSSE API has into the PSSE GUI. The only API functions I can find are related to the Graphical Analysis Output.

I would like to:

  1. Select a bus on the Diagram canvas
  2. Get the bus number from an API function called from PSSE

Actually, any flow[1] that lets a user choose something in PSSE and use that choice in the API would be appreciated. Is anything like this possible with PSSE?

EDIT: [1] Detecting selected items from the network spreadsheets (bus, branch, machine, etc) would also be helpful, but that ability seems even more unlikely.

2016-04-11 00:40:02 -0500 marked best answer No API for pssplt?

I can't find any documentation in the PSSE32 docs about an API for pssplt, so I can only conclude that there isn't one. Does anyone know any differently?

Which means I'll be using subprocess.Popen or similar if I want to run a command like:

pssplt32 -inpdev "some_math.idv"

from a larger python script. Which really isn't that bad, but I'd rather not spawn a subprocess unless I have too.

2016-04-11 00:39:58 -0500 marked best answer How to detect if Python or PSSE is running the show.

I have a script that is normally launched from the shell with PSSE on top like so:

psse32 -buses 150000 -pyfile "MyScript.py" -embed

I am converting the script to run with Python on top as part of a larger python program. Does anyone know how to detect if I am running as an embedded session in PSSE or standalone?

I found have an indirect route by explicitly setting the mode:

def main(embedded=True):
    ...
    if embedded:
        psspy.stop_2()
    else:
        # do something else to finish up.

if __name__ == '__main__':
    # PSSE embedded scripts enter here 
    main()

And invoke the script using:

import MyScript
MyScript.main(embedded=False)
2016-04-11 00:39:57 -0500 marked best answer Silencing PSSE STDOUT

PSSE is rather talkative and makes liberal use of STDOUT just to let you know its happy. Does anyone know how to silence PSSE and just make it report important things like errors?

For example just setting up psse with the command:

psspy.psseinit(80000)

Causes PSSE to dump a copyright notice and other info to STDOUT:

 PSS«E Version 32
 Copyright (c) 1976-2012
 Siemens Energy, Inc.,
 Power Technologies International                            (PTI)
 This program is a confidential  unpublished  work  created  and  first
 licensed in 1976.  It is a trade secret which is the property of  PTI

Other common operations like reading a raw file behave in the same way. Now I know I could redirect all STDOUT in Python, but that seems rather drastic. Any ideas?

2015-06-14 17:52:05 -0500 commented question can someone please share the IEEE 9 bus system^s matlab code finding any fault current

I think the source code for MATPOWER (or it's python port) has some test systems including the IEEE cases.

2015-04-22 17:51:31 -0500 commented question Debugging the Python code during PSSe dynamic simulation

pdb is a full debugger, you can set breakpoints. If you want a pretty IDE give PyCharm or Spyder a try. If you go the Spyder route, I recommend you get it via PythonXY.

2015-04-15 21:02:23 -0500 commented question Debugging the Python code during PSSe dynamic simulation

''pdb.set_trace()'' may work. It may not work depending how your output is being redirected.

2015-03-06 10:12:55 -0500 answered a question Suppress PSSE output entirely

I prefer to redirect STDOUT using a contextmanager so that I am sure that STDOUT goes back to normal after I am done capturing/ignoring it. The contextmanger will do the right thing if there is an exception, and plays nicely with pdb.set_trace(). If I don't use a contextmanger it still works, but invariably my code throws an exception while STDOUT is being captured, and then everything is a mess because I can't see STDOUT.

Usually if redirecting STDOUT is failing it is because I forget the line redirect.psse2py(). Here is a simple example with psseinit()

# silence.py
import sys
import os

import pssepath
pssepath.add_pssepath()    
import psspy

import redirect
redirect.psse2py()

import contextlib
@contextlib.contextmanager
def silence(new_target=None):
    # sometimes you don't care about messages.
    if new_target is None:
        new_target = open(os.devnull, 'w')
    old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
    try:
        yield new_target # run some code with the replaced stdout
    finally:
        sys.stdout = old_target # restore to the previous value

if sys.argv[1] == 'silent':
    with silence():
        psspy.psseinit(50000)
else:
    psspy.psseinit(50000)

And the results from running it in the CLI:

C:\projects\foo
>python silence.py silent

C:\projects\foo
>python silence.py verbose

 PSS(R)E Version 33
 <... snip ...>
 treaties.  All  Rights  Reserved  Under  The  Copyright  Laws.


           SIEMENS POWER TECHNOLOGIES INTERNATIONAL

      50000 BUS POWER SYSTEM SIMULATOR--PSS(R)E-33.5.2

             INITIATED ON FRI, MAR 06 2015   9:57

Without redirect.psse2py()

If you do not run the redirect.psse2py() function, STDOUT will be captured for everything except PSSE. (I do not understand how PSSE is making text appear on the screen without STDOUT).

If we use our example code above and leave out the redirect.psse2py(), we see that normal python is silenced:

In [1]: print "Checkpoint 0"
Checkpoint 0

In [2]: with silence():
   ...:     print "Checkpoint 1"
   ...:
In [3]:

But PSSE is still noisy:

In [3]: with silence():
   ...:     psspy.psseinit(10)
   ...:

 PSS(R)E Version 33
 Copyright (c) 1976-2015
 <.......... snip ..............>
2015-03-05 13:43:39 -0500 commented question Suppress PSSE output entirely

My favorite way by far is to redirect with a contextmanger like this [example](https://psspy.org/psse-help-forum/question/93/silencing-psse-stdout/). Here is my [gist](https://gist.github.com/cwebber314/0b09f870f62f96ddc57a) I created as another example.

2015-02-12 14:14:25 -0500 commented answer PSSE custom output device with python

It can be helpful to use a contextmanager for method2 when you want to make sure STDOUT always restored to the previous location. http://stackoverflow.com/a/28486632/653689

2014-08-15 15:42:13 -0500 commented question MemoryError for running module "pssarrays.accc_violations_report"

You can use the Resource Monitor and watch as the python process creeps up to 2GB and then throws the ``MemoryError`` exception. It's not that hard to hit the 2GB limit (32bit python).

2014-02-12 11:24:35 -0500 answered a question Please help me fix an error

Check out the variables in the line of code which is giving you problems:

s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)

It looks like one of the arrays is empty.

You can see this problem in a distilled case like:

import numpy as np
np.array([1,1,1]) * np.array([])

Which gives the error:

ValueError: operands could not be broadcast together with shapes (3) (0)
> <ipython-input-165-1fc37b5acfae>(1)<module>()
----> 1 np.array([1,1,1]) * np.array([])

A couple more examples:

In [169]: np.array([1,1,1]) * np.array([2.5, 1.0]) 
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-169-37890460fbb0> in <module>()
----> 1 np.array([1,1,1]) * np.array([2.5, 1.0])

ValueError: operands could not be broadcast together with shapes (3) (2)

In [170]: np.array([1,1,1]) * np.array([2.5]) 
Out[170]: array([ 2.5,  2.5,  2.5])
2014-02-12 11:14:58 -0500 edited question Please help me fix an error

I'm getting an error in python:

"File "Untitled", line 17
    s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)
ValueError: operands could not be broadcast together with shapes (3) (0) "

The code is posted below. does anybody know how i can fix this?:

B = vector(0,0,0.5e-4) # Tesla, the earth's magnetic field
q = -1.6e-19 # Coulomb, the charge on an electron
m = .00507 # kg, the mass of an electron
s = sphere(radius=2.0e-6)
s.velocity = vector(100,0,0) # m/s
w=(2*pi,0,0)
trail=curve(color=color.blue,pos=s.pos)
s.pos=(0,0,0)
t=0
dt=1e-10 # s
while t < 3e-6:
    if s.pos.x < 1e-6:
        s.velocity = s.velocity +((1.6e-22)*cos(w*t)*dt)
        trail.append(s.pos)
        t = t+dt
        rate(4e-7/dt)
    else:
        s.acceleration = q*cross(s.velocity,B)/m
        s.velocity = s.velocity + s.acceleration*dt
        s.pos = s.pos + s.velocity*dt
        trail.append(s.pos)
        t = t+dt
        rate(4e-7/dt)
2014-02-03 13:58:53 -0500 edited question Reading PSSE output files (*.dat) and saving in a useful format (CSV)

Hi I need some guidance/help on Python – Esp reading a text data file and re-arranging the information in a tabular form (probably in an Excel/CSV file) Please refer to the output of the ”Report.dat” file given below

For each branch, there is a sensitivity list for buses ( the number of buses are not fixed for all contingencies)

I would like to get these information in the following format. Bus, Branch, Sensitivity

Appreciate your help to get this done in a Pythonic way

Ta

Output of the Report.Dat file

TI INTERACTIVE POWER SYSTEM SIMULATOR--PSS(R)E     WED, DEC 29 1814  14:57
SENSITIVITY FACTORS ARE CALCULATED WITH DC NETWORK
SYSTEM SWING BUS(es) IS USED FOR OPPOSING SYSTEM FOR MW POWER DISPATCH

SENSITIVITY FACTORS OF BRANCH FLOW (MW) ON  10000 LA    275.00 TO  20000 NY    275.00 1  GREATER THAN  0.10000

<------- BUS NAME ------>   MW-SENS

 10000 LA    275.00    0.1041

 20000 NY    275.00   -0.7613

 30000 WDC   275.00   -0.2254

 40000 SF    275.00   -0.2254

 50000 AZ    275.00   -0.2144



SENSITIVITY FACTORS OF BRANCH FLOW (MW) ON  40000 SF    275.00 TO  50000 AZ    275.00 1  GREATER THAN  0.10000

<------- BUS NAME ------>   MW-SENS

 20000 NY    275.00    0.7613

 30000 WDC   275.00    0.2254

 40000 SF    275.00    0.2254

 50000 AZ    275.00    0.2144



SENSITIVITY FACTORS OF BRANCH FLOW (MW) ON  40000 SF    275.00 TO  60000 PD    275.00 1  GREATER THAN  0.10000

------- BUS NAME ------>   MW-SENS

 10000 LA    275.00    0.1613

 30000 WDC   275.00    0.3254

 40000 SF    275.00    0.1540

 50000 AZ    275.00    0.3244
2014-01-31 12:16:56 -0500 commented answer import pssexcel

you should post this as new question. Also how do you try to import pssecel? From system python? Are you invoking psse with a script as an argument? From within PSSE?

2013-12-16 14:03:18 -0500 commented answer import pssexcel

Regarding python on top see the Whit [post](http://www.whit.com.au/blog/2012/10/import-psse-into-your-python-script/) for a nice description of how/why to run python on top

2013-10-03 17:56:36 -0500 commented answer Can I run PSSE on mac or Linux?

Out of curiosity, are you using a network license?

2013-09-17 16:45:20 -0500 received badge  Great Answer (source)
2013-08-09 11:34:09 -0500 commented answer Help Refining Multiple ACCC Excel Report

excelpy.pyc is the excelpy module. It is common for software to ship with compiled .pyc instead of the source .py file.

2013-08-05 14:35:08 -0500 answered a question Help Refining Multiple ACCC Excel Report

I ran the tool on a accc file I had lying around and here's the profile dump:

accc_report.profile% sort cumtime
accc_report.profile% stats 5
Mon Aug 05 14:07:37 2013    accc_report.profile

         44061406 function calls (44061393 primitive calls) in 160.941 seconds

   Ordered by: cumulative time
   List reduced from 2713 to 5 due to restriction <5>

   ncalls  tottime  percall  cumtime  percall filename:lineno(function)
        1   64.068   64.068  161.037  161.037 accc_report.py:15(<module>)
  5786418   47.012    0.000   47.012    0.000 {method 'index' of 'tuple' objects}
     4802    2.327    0.000   25.199    0.005 .\pssarrays.py:1239(accc_solution)
     1200    0.646    0.001   11.804    0.010 accc_report.py:544(GetN1results)
        1    0.048    0.048   10.357   10.357 accc_report.py:517(BuildMasterConlist)

Things i noticed:

  • Most of the time is spent on the 5786418 calls {method 'index' of 'tuple' objects} at 47sec.
  • Actually gettting the data using pssarrays.accc_solution() is quicker than I remembered at 25sec.
  • excelpy is only 7sec which may be completely inaccurate, excel via COM threw some warnings when writing the xls file. I just grabbed a random accc file so the comparisons were probably meaningless.

I'm not sure what is hammering the {method 'index' of 'tuple' objects}. A small refactor to break up some of the main loop into functions would give us some hints, or you could figure it out just by staring at the code for a while.

2013-04-22 20:36:46 -0500 commented question ACCC Output to Excel

Are you on the latest point release of PSSE? I ran into some win32com bugs with the early versions of the v33 releases while working with the accc functions. An upgrade to the latest PSSE release fixed it up.

2013-01-14 04:13:19 -0500 received badge  Famous Question (source)
2012-11-30 13:15:50 -0500 commented answer Import matlab model in PSS/E

Porting MATLAB models to another simulator seems to be a chore you just have to deal with. I've validated my share of models against their MATLAB equivalents.

2012-11-21 09:04:46 -0500 answered a question Can you call python functions from VBA?

Calling python from VBA-land

You asked about calling python from a macro, but anything that's in VBA you should be able to get to from a macro or provide a functional equivalent.

Using win32com you can implement a COM object and through some machinations add this as a reference to your VBA code. Even thinking about this is a bit painful for me.

Instead treat your python program as a separate tool that does one thing, and does it well. The excel spreadsheet is a easy-to-use front-end for you python script. Maybe one day you'll want to grab data directly from a PSSE run and shove it into your script and just skip excel?

Moving data around

You have a couple options gluing your python scripts to your MSOffice VBA:

  1. Pass data in on the command-line invocation of your script
  2. Temporary files
  3. STDIO pipes

From past investigations, STDIO is too much of a pain to use in VBA so we can ignore that option.

Options 1 and 2 are good ways to get data into your script. For example if you want to run some python math on data in excel you might invoke your python program like this

uber_math.py "C:\TEMP\excel_data.csv" "C:\TEMP\excel_prime.csv"

I like to use standard windows environment variables like %TEMP% so the VBA will work anywhere. If you haven't yet, spend some time learning about argparse

For other users

If you are the only one using your VBA-python spreadsheet, then you can invoke the python script with python. You may also be able to get away with this if everyone you are working with has a similar environment.

If your spreadsheet users don't have python installed you can build your python script as an executable and place it in a shared location.

Reference

Helpful functions on the VBA side to call external programs:

2012-11-20 16:24:56 -0500 commented question Can you call python functions from VBA?

Is there anything stopping you from using ``shell()`` to call the python script (compiled as an .exe if needed) and use command-line args + fileio to move data back and forth?

2012-11-13 13:16:11 -0500 received badge  Famous Question (source)
2012-11-13 13:16:11 -0500 received badge  Notable Question (source)
2012-11-13 12:36:14 -0500 answered a question excelpy reading excel files

I actually prefer to work with xlrd if I need to muck about with excel spreadsheets. It's also much easier to use than the COM API from win32com.

xlrd Pros:

  • Fully documented
  • Lots of examples
  • You don't need a PSSE license

xlrd Cons:

  • It's an extra package you have to install.

I know this isn't a direct answer to your question. I went to grab an excelpy example from my scripts, but I realized I am not using it because of the other tools available. If memory serves, I tried once and decided to stick with xlrd/xlwt. There is also openpyxl. It looks like a nice package, but I haven't tried it yet.

xlrd Example

Here's an example of workbook navigation from the docs::

from xlrd import open_workbook
wb = open_workbook('simple.xls')
for s in wb.sheets():
    print 'Sheet:',s.name
    for row in range(s.nrows):
        values = []
        for col in range(s.ncols):
            values.append(s.cell(row,col).value)
        print ','.join(values)
    print
2012-11-10 09:07:35 -0500 received badge  Taxonomist
2012-10-26 09:41:51 -0500 commented answer Can we create a single line diagram(sld) from raw data using python?

@JervisW and @amaity I've been curious about using networkx, graphviz, etc to draw electrical networks. In my experience, creating meaningful schematic from a netlist / raw-file has been a task largely left to users, but I'd like to see if there is value in a generated network diagram.

2012-10-10 10:48:13 -0500 received badge  Citizen Patrol (source)
2012-09-07 15:15:40 -0500 commented answer What is SID

Don't forget about the useful sid=-1 which includes all elements in a case.

2012-08-12 13:30:45 -0500 commented answer what is psspy ierr

@amaity, that is my assumption too - the prefix `i` indicates the data type of `integer` which is the essence of systems Hungarian notation. It's not very pythonic, but the PSSE API docs use `ierr` all over the place so I'm still conflicted about the right name to give the variable.

2012-08-11 18:05:01 -0500 answered a question what is psspy ierr

The PSSE API documentation uses a variable named ierr to hold the error code that a function returns.

ierr, buses = psspy.abuscount(sid=1)

You can call the variable whatever you want, I suspect the i in ierr comes from applying Hungarian Notation.

It's usually a good idea to check the error code. By convention (not just in PSSE) if an operation completes without an error a zero is returned. So you can do something like:

error, buses = psspy.abuscount(sid=1)
if error:
    print "WARNING: Something went wrong"

You can send the error code all the way up to the top level of you program and return it exit which can be useful if your program is part of a larger flow:

if __name__ == '__main__':
    result = main(sys.argv)
    sys.exit(result)

Now a batch file calling your script can know that something went awry too.

2012-07-27 11:21:56 -0500 edited answer Problem with numpy and matplotlib

PSSE ships with its own Python distribution which doesn't include numpy and matplotlib. In this case you probably want to run with Python on top.

The Whit guys have also written a nice article on doing this.

It's possible to get a version of numpy and matplotlib to run with the PSSE Python distribution but it's probably more work than it's worth. PSSE maintains build packages of scientific tools for the correct PSSE Python versions.

For me, running the system Python on top is usually more convenient.

  • Things like IDE's play nicely with it.
  • Python install is orthogonal to PSSE install.
  • The C:\Python27\ install tree is comforting.
  • Only one Python install to maintain.