# 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?

edit retag close merge delete

Both Chip's (the selected answer) and JervisW's answers solve my problem. Thanks guys!

( 2012-04-08 13:27:22 -0600 )edit

Sort by ยป oldest newest most voted

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.

more

I like your max + 1 approach.

( 2012-03-22 19:43:55 -0600 )edit

you can set the range to begin at 1. range(1, 15000) your zero bus problem should go away.

( 2012-03-23 16:41:31 -0600 )edit

Get the maximum bus number and add one would probably the easiest way: max(psspy.abusint(flag=2, string='NUMBER')[1][0])+1

more

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

more

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.

more

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

more

You did say that you don't care about colleagues conventions.. They probably won't like that :)

( 2012-03-22 19:41:40 -0600 )edit

Nice, i didn't know about the -1 subsystem trick.

( 2012-03-22 19:47:50 -0600 )edit