Code to count elements in SAV easier

asked 2019-11-06 04:53:15 -0500

drsgao gravatar image

This function is inspired by the subsystem_info function.

It bascially allows you get the element counts in the SAV programmatically easier. There is a feature in the GUI allows you to do very similar thing. I wrote this as an exercise.

Code:

def intGetCount(str_type, int_sid, int_flag, **kwarg):
    '''
    This function returns the element counts in a PSSE SAV file.

    This function uses the "getattr()" from Python to group the counting
    functions in "psspy". Consult PSSE documentation for how to set
    the subsystem ID and flag for each type of elements.

    Parameters
    ----------
    str_type : str
        Type of the target element. This is also used to get the right
        function in "pssspy".

        Here are some examples, consult PSSE documentation for all elements.

        * "bus" -- buses
        * "genbus" -- plant/generator buses
        * "mach" -- machines/generators
        * "loadbus" -- load buses
        * "load" -- loads
        * "fxshntbus" -- fixed shunt buses
        * "fxshunt" -- fixed shunts
        * "brn" -- branches
        * "trn" -- 2-winding transformers
        * "tr3" -- 3-winding transformers
        * "wnd" -- 3-winding transformer windings
        * "area" -- areas

    int_sid : int
        Bus subsystem ID.

    int_flag : int
        A flag indicating what subsystem buses to include.

    kwarg: named arguments accepted by the actual count functions
        This is to allow calling functions with more than just the sid
        and flag arguments, e.g., "awndcount()".

        You must used named arguments for these. Check the PSSE
        APIs for the named arguments.

    Returns
    -------
    int_count : int
        The count of the given element.

    Raises
    -------
    ValueError
        If the given element string results in None to be found in "psspy".
    '''

    # force throwing exception
    psspy.throwPsseExceptions = True

    str_type = str_type.lower()

    str_type = 'a%scount' % (str_type)

    func = getattr(psspy, str_type, None)

    if not func:

        raise ValueError("Module '%s' has no attribute '%s'" % ('psspy', str_type))

    else:

        pass

    # ignore the error since already forced throwing exceptions
    _, int_count = func(sid=int_sid, flag=int_flag, **kwarg)

    return int_count

Example usage (after the usual init and case, of course):

int_bus_count = intGetCount('bus', -1, 2)

print('Bus Count = %d' % int_bus_count)

int_3w_trf_wcount = intGetCount('wnd', int_sid=-1, int_flag=3, owner=1, ties=3)

print('3-winding transformer windings = %d' % int_3w_trf_wcount)

Feedbacks are welcome.

edit retag flag offensive close merge delete