Convert dynamic OUT files to Excel XLSX files

asked 2019-05-09 10:32:52 -0500

drsgao gravatar image

I have written a function to convert dynamic OUT files to Exel XLSX files. You can then plot the curves, or, better, save the worksheets you want into CSVs and then plot them using matplotlib. I am converting them to XLSX for easy sharing with colleagues.

Any improvements are welcome.

# =============================================================================
# <Function: convert .out file to excel workbook .xlsx>
# =============================================================================
def boolOut2Xlsx(str_path_out, str_path_xlsx, bool_show=False, bool_overwritesheet=False, str_sheet='', list_chn=[]):
    '''
    .. _boolOut2Xlsx :

    This function converts the PSSE OUT file into an Excel workbook (XLSX).

    Dependency
    ----------
    dyntools : PSSE Python module

    Parameters
    ----------
    str_path_out : str
        The path of the OUT file.

    str_path_xlsx : str
        The path of the XLSX file.

    bool_overwritesheet : boolean
        Whether to overwrite the current worksheet.

        True = overwrite;
        False = not overwirte.

        Defualt = Flase

    str_sheet : str
        Name of the worksheet.

    list_chn : list
        A list containing the desired channels to be included in the XLSX.

        Empty list means all channels. Note that this could make the XLSX very large.

        Default = emtpy list (all channels)

    Returns
    -------
    list of two bools : list
        This list has two elements. If no exception (conversion assumed successful), the 
        first element will be True, else, it will be False. The second element is always
        True. This is to denote function end (end of conversion).
    '''

    try:

        # use the CHNF in dyntools, let it read the OUT file path
        obj_chnf = dyntools.CHNF(str_path_out) 

        # if not empty list
        if list_chn:

            list_chn = list_chn

        else:

            # the channel ids are return as a tuple
            tup_id = obj_chnf.get_id(str_path_out)

            # get the total id number
            # turn the tuple into a list then get the length of the list
            int_id_num = len(list(tup_id[1]))

            list_chn = range(1, int_id_num, 1)

        # this is output line
        obj_chnf.xlsout(channels=list_chn, show=bool_show, xlsfile=str_path_xlsx,
                        outfile=str_path_out, sheet=str_sheet, overwritesheet=bool_overwritesheet)

        return [True, True]

    except:

        return [False, True]
# =============================================================================
# </Function: convert .out file to excel workbook .xlsx>
# =============================================================================
edit retag flag offensive close merge delete