def run_stk_v2(scenario_path, study_name, orbit_data, stk_data_path): # This function opens an instance of STK, loads the desired scenario, and executes the # connect commands written by the previous functions from win32api import GetSystemMetrics # from IPython.display import Image, display, SVG import os import comtypes from comtypes.client import CreateObject from comtypes.client import GetActiveObject print('Opening STK...') # Open new instance of STK # app = CreateObject("STK11.Application") # Pass open instance of STK app = GetActiveObject('svchost.Application') app.Visible = True app.UserControl = True app.Top = 0 app.Left = 0 app.Width = int(GetSystemMetrics(0) / 2) app.Height = int(GetSystemMetrics(1) - 30) root = app.Personality2 comtypes.client.gen_dir os.listdir(comtypes.client.gen_dir) from comtypes.gen import STKObjects print('Loading scenario...') # Load predefined scenario, which contains satellite for which the orbit is varied # and the target for which the access time is calculated root.LoadScenario(r'{}'.format(scenario_path)) sc = root.CurrentScenario sc2 = sc.QueryInterface(STKObjects.IAgScenario) # sc2.SetTimePeriod("1 Jul 2008 10:00:00", "30 Jun 2010 10:00:00") print('Executing connect commands...') # Open file with connect commands, and execute them sequentially connect_command_file = 'CC_{}_OrbitStudy.txt'.format(study_name) with open(connect_command_file, 'r') as fh: commands = fh.readlines() size = len(commands) loop_start = time.time() duration = np.zeros(size) j = 0 for i in range(size): time_start = time.time() if j == 0: print('Adjusting Satellite orbit...') root.ExecuteCommand(commands[i]) j += 1 elif j == 1: print('Generating SPS access report...') if not os.path.exists('{}\DVP_{}_{}perigee{}apogee_{}meananom_access.csv'.format(stk_data_path, study_name, orbit_data[i + 1][0], orbit_data[i + 1][1], 180.0)): root.ExecuteCommand(commands[i]) else: print('Access report for {} x {} km orbit already exists'.format(orbit_data[i + 1][0], orbit_data[i + 1][1])) j += 1 elif j == 2: print('Generating SPS range report...') if not os.path.exists('{}\DVP_{}_{}perigee{}apogee_{}meananom_range.txt'.format(stk_data_path, study_name, orbit_data[i + 1][0], orbit_data[i + 1][1], 180.0)): root.ExecuteCommand(commands[i]) else: print('Range report for {} x {} km orbit already exists'.format(orbit_data[i + 1][0], orbit_data[i + 1][1])) j += 1 elif j == 3: print('Generating SPS lighting report...') if not os.path.exists('{}\DVP_{}_{}perigee{}apogee_{}meananom_lighting.csv'.format(stk_data_path, study_name, orbit_data[i + 1][0], orbit_data[i + 1][1], 180.0)): root.ExecuteCommand(commands[i]) else: print('Lighting for {} x {} km orbit already exists'.format(orbit_data[i + 1][0], orbit_data[i + 1][1])) j = 0 time_end = time.time() # Print progress update print('Progress: {}%, Execution Time: {} seconds'.format(round(i * 100.0 / (size - 1), 2), round(time_end - time_start, 5))) duration[i] = time_end - time_start loop_end = time.time() print('Total time to generate data: {} minutes'.format((loop_end - loop_start) / 60.0)) print('Average command execution time: {} seconds'.format(np.mean(duration)))