How do I find an available bus number?
I am looking to add a bus to an existing network. Ignoring any numbering conventions my colleagues may have, how do I find a available bus number to use?
I am looking to add a bus to an existing network. Ignoring any numbering conventions my colleagues may have, how do I find a available bus number to use?
I just wrote this a couple hours ago for a similar problem. So it can probably be cleaned up:
def get_next_free_busnum():
"""
get the next free bus number
"""
# find largest bus number
ierr, all_buses = psspy.abusint(
sid=-1, # all buses in the case.
flag=2, # in-service and out-of-service buses
string=["NUMBER"]
)
# transpose from 2D array to 1D list.
all_buses = zip(*all_buses)[0]
busnum = max(all_buses) + 1
return busnum
I guess you could do something smart like:
busnum = min(set(range(15000)) - set(all_buses))
# where 15000 is highest busnum you want to use
This will return 0, which I'm not sure is a valid number or not.
I like your max + 1 approach.
you can set the range to begin at 1. range(1, 15000) your zero bus problem should go away.
Get the maximum bus number and add one would probably the easiest way:
max(psspy.abusint(flag=2, string='NUMBER')[1][0])+1
The next free bus number can be found in this easy way:
def nextfreebus(ibus):
""" Returns the first unused bus number, starting from ibus
"""
while psspy.busexs(ibus)==0: ibus += 1
return ibus
If the intent is to just print out the available bus numbers in a text report, then you can use built in psspy.busn
.
Use this API to tabulate unused bus numbers within a specified bus number range (activity BUSN).
Batch command syntax:
BAT_BUSN BUSLO BUSHI
Python syntax:
ierr = busn(buslo, bushi)
Fortran syntax:
CALL BUSNAPI(BUSLO, BUSHI, IERR)
where:
Integer BUSLO Is the low limit of bus number range (input; 1 by default).
Integer BUSHI Is the high limit of bus number range (input; 999,997 by default).
Integer IERR Is the error code (output).
IERR = 0 no error occurred.
IERR = 1 invalid starting bus number.
IERR = 2 starting bus number is greater than ending bus number.
IERR = 3 prerequisite requirements for API are not met.
Ok, first you need to know what buses exist in the system already. Then work out a bus that fits into one of the gaps.
ierr, buses = psspy.abusint(-1, flag=2, string="NUMBER")
# convert to a simple set of buses
taken_buses = set(zip(*buses)[0])
# now find the first available gap between 1 and 10,000:
# 10,000 - or any suitably high number.
available_bus = None
for bus in range(1, 10000):
if bus not in taken_buses:
available_bus = bus
break
print "You can use bus=%d if you want." % available_bus
You did say that you don't care about colleagues conventions.. They probably won't like that :)
Nice, i didn't know about the -1 subsystem trick.
Both Chip's (the selected answer) and JervisW's answers solve my problem. Thanks guys!