Ask Your Question

How do I find an available bus number?

asked 2012-03-22 19:31:53 -0600

Daniel_Hillier gravatar image

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 flag offensive close merge delete


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

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

5 answers

Sort by ยป oldest newest most voted

answered 2012-03-22 19:37:13 -0600

chip gravatar image

updated 2012-03-23 16:37:18 -0600

JervisW gravatar image

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

edit flag offensive delete link more


I like your max + 1 approach.

JervisW gravatar imageJervisW ( 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.

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

answered 2016-09-26 01:15:42 -0600

SqFKYo gravatar image

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

edit flag offensive delete link more

answered 2016-09-21 01:27:10 -0600

perolofl gravatar image

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
edit flag offensive delete link more

answered 2015-01-05 16:00:40 -0600

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:


Python syntax:
ierr = busn(buslo, bushi)

Fortran syntax:

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.
edit flag offensive delete link more

answered 2012-03-22 19:39:53 -0600

JervisW gravatar image

updated 2012-03-23 16:37:52 -0600

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

print "You can use bus=%d if you want." % available_bus
edit flag offensive delete link more


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

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

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

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

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools


Asked: 2012-03-22 19:31:53 -0600

Seen: 2,038 times

Last updated: Sep 26 '16