Ask Your Question
0

idv command to change case title for multiple cases (many sav files)

asked 2019-09-24 23:08:44 -0500

Steady-State gravatar image

updated 2019-09-24 23:09:17 -0500

I tried to record an idv to change the case title. The resulting API is this: BATCASETITLE_DATA,'First Line','Second Line'.

Is there a way to rename multiple cases using one idv only? Thanks!

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
0

answered 2019-09-26 06:33:51 -0500

drsgao gravatar image

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

Comments

thank you for your time. I'm a complete beginner in Python programming so it will take some time to understand and apply your code. But, thanks still.

Steady-State gravatar imageSteady-State ( 2019-09-27 06:01:11 -0500 )edit
0

answered 2019-09-25 01:03:14 -0500

perolofl gravatar image

Use a loop in Python to update all saved case titles.

edit flag offensive delete link more

Comments

thanks for replying! I solved my problem using batch run idev.

Steady-State gravatar imageSteady-State ( 2019-09-27 05:57:56 -0500 )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

1 follower

Stats

Asked: 2019-09-24 23:08:44 -0500

Seen: 34 times

Last updated: Sep 26