Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

As @perolofl pointed out, this is very easy. Here is an example.

Note that the file that stores the new title names should be a txt or csv with contents like:

Test_01_line_01,Test_01_line_02
Test_02_line_01,Test_02_line_02
Test_03_line_01,Test_03_line_02
Test_04_line_01,Test_04_line_02
Test_05_line_01,Test_05_line_02
Test_06_line_01,Test_06_line_02
Test_07_line_01,Test_07_line_02

Code, tested with PSSE v33.4. Remember to replace the pointy brackets with actual paths:

import os
import sys
import glob

STR_PATH_PSSBIN         = <raw string to you PSSBIN folder path>

STR_PATH_SAV_IN_DIR     = <raw string to your SAV model folder path>

STR_PATH_SAV_OUT_DIR    = <raw string to your SAV output folder path (save as)>

STR_PATH_TITLE_TXT_FILE = <raw string to the file that stores the new case titles>

# separater for the two lines in the title
STR_SEP                 = ','


def add_environ_path(str_path):
    '''Add PSSBIN path to the environmental variable "PATH".
    You can improve this func by adding more checks.'''

    str_path = os.path.abspath(str_path)

    str_temp = os.environ['PATH']

    list_temp = str_temp.split(';')

    # Windows paths are not case sensitive
    if sys.platform == 'win32':

        str_path = str_path.lower()

        list_temp = [i.lower() for i in list_temp]

    else:

        pass

    if str_path in list_temp:

        pass

    else:

        os.environ['PATH'] = str_path + ';' + os.environ['PATH']


def insert_sys_path(str_path):
    '''Insert PSSBIN path to sys.path. Again, you can improve this func.'''

    str_path = os.path.abspath(str_path)

    list_sys_path = sys.path[:]

    if sys.platform == 'win32':

        str_path = str_path.lower()

        list_sys_path = [i.lower() for i in list_sys_path]

    else:

        pass

    if str_path in list_sys_path:

        pass

    else:

        sys.path.insert(0, str_path)


if __name__ == '__main__':

    str_path_sav_in_dir    = os.path.abspath(STR_PATH_SAV_IN_DIR)

    list_path_sav_file     = glob.glob(os.path.join(str_path_sav_in_dir, '*.sav'))

    list_path_new_sav_file = list_path_sav_file[:]

    list_path_new_sav_file = [os.path.basename(i) for i in list_path_new_sav_file]

    list_path_new_sav_file = [os.path.join(STR_PATH_SAV_OUT_DIR, i) for i in list_path_new_sav_file]

    list_new_name = []

    with open(STR_PATH_TITLE_TXT_FILE, 'r') as fin:

        list_new_name = fin.readlines()

    # strip trailing new lines
    list_new_name = [i.rstrip() for i in list_new_name]

    # remove blank lines
    list_new_name = [i for i in list_new_name if i]

    # separate each line into two title lines
    list_new_name = [i.split(STR_SEP) for i in list_new_name]

    if len(list_path_sav_file) == len(list_new_name):

        add_environ_path(STR_PATH_PSSBIN)
        insert_sys_path(STR_PATH_PSSBIN)

        import psspy

        psspy.throwPsseExceptions = True

        psspy.psseinit(50000)

        for i in xrange(0, len(list_path_sav_file)):

            str_temp_path_in   = list_path_sav_file[i]

            str_temp_path_out  = list_path_new_sav_file[i]

            list_temp_new_name = list_new_name[i]

            psspy.case(str_temp_path_in)

            psspy.case_title_data(list_temp_new_name[0], list_temp_new_name[-1])

            psspy.save(str_temp_path_out)

        print('Renaming complete. Files in : ' + STR_PATH_SAV_OUT_DIR)

    else:

        raise ValueError('SAV file number does not match the new title number.')