Part 16:
Integrating STK with Python
Watch the following video, then follow the steps below incorporating the systems and missions you work on (sample inputs provided).A recorded PowerPoint Presentation and Python script accompanies this lesson. It is recommended that you follow the presentation while performing the tasks. If you don't want to use the Python Script, you can type the commands right in the Python Command Window.
Integrating STK and Python
The STK Programming Interface has several code snippets to assist you. Visit our FAQ site to learn more about Python-STK COM Integration. Additional Python Resources are on Github.
Create a New Instance of STK from Inside Python
This lesson utilizes Python 3.5. Before starting this lesson verify that you have a working Python environment. In this example, Spyder and Anaconda are used. You may also use your preferred python environment (i.e. WinPython and Juypter or others).
- Launch Spyder (or your preferred method)
- Select the Home tab.
- Click the Open icon.
- Browse to the script file located at Install Directory\Help\stktraining\scripts.
- In the Current Folder field, double click the file named STK_PYTHON_Script.py.
Python is up and running. You will use the Python script file to automate building a simple STK scenario from which you will extract data into Python.
If you are building your script from scratch, use the following commands:
- Set up your Python workspace
- Now import the Com Types
from win32api import GetSystemMetrics
from comtypes.client import CreateObject
When connected to STK via Python, while creating your variable, using the Tab key after periods enables IntelliSense which displays all of the options available off of the current interface. Try it.
- Create a new instance of STK11.
- In Python, type the following code into the Command window:
- Click Enter.
- Click Enter.
- Grab a handle on the STK application root.
- In Python, type the following code into the Command window and then click Enter.
- Click Enter.
uiApplication=CreateObject("STK11.Application")
uiApplication.Visible=True
uiApplication.UserControl=True
root = uiApplication.Personality2
When 'root=uiApplication.Personality2' is executed, the comtypes library automatically creates a gen folder that contains STKUtil and STK Objects. After running this at least once on your computer, the following two lines should be moved before the 'uiApplication=CreateObject("STK11.Application")'
from comtypes.gen import STKUtil
from comtypes.gen import STKObjects
Create a New STK Scenario from Inside Python
Now that you have launched STK via the Python interface, let's see if we can create a new scenario and set the time period via Python.
- Create a new scenario, analysis period and reset the animation time.
- In Python, copy and paste all the code from the Editor window (to included comments (#)) from Task 2 and paste it to the Command Window.
- Click Enter.
##
#TASK 2
# 1. Create a new scenario.
root.NewScenario("Python_Starter")
scenario = root.CurrentScenario
# 2. Set the analytical time period.
scenario2 = scenario.QueryInterface(STKObjects.IAgScenario)
scenario2.SetTimePeriod('Today','+24hr')
# 3. Reset the animation time.
root.Rewind();
Insert and Configure Objects
With a new scenario created, it's time to populate the scenario with objects. Take a moment to create a facility and a LEO satellite using Python.
Insert Target and Satellite objects into the scenario.
In Python, copy and paste all the code from the Editor window (to included comments (#)) from Task 3 and paste it to the Command Window.
- Click Enter.
##
#TASK 3
#1. Add a target object to the scenario.
target = scenario.Children.New(STKObjects.eTarget,"GroundTarget");
target2 = target.QueryInterface(STKObjects.IAgTarget)
#2. Move the Target object to a desired location.
target2.Position.AssignGeodetic(50,-100,0)
#3. Add a Satellite object to the scenario.
satellite = scenario.Children.New(STKObjects.eSatellite, "LeoSat")
#4. Propagate the Satellite object's orbit.
root.ExecuteCommand('SetState */Satellite/LeoSat Classical TwoBody "' + scenario2.StartTime + '" "'+ scenario2.StopTime +'" 60 ICRF "'+ scenario2.StartTime + '" 7200000.0 0.0 90 0.0 0.0 0.0')
The SetState Classical Connect command and syntax can be found here.
Compute Access Between Objects and Retrieve Data From STK
You now have a scenario with a Target object and a Satellite object. Determine when the Satellite object can access the Target object.
- Browse to the STK Programming Interface help files.
- Locate and manually enter code into Python to compute an access between two STK Objects using the IAgStkObject interface. The access is between the Satellite object and the Target object.
- If you cannot locate the code, expand the following
- Click Enter.
Compute Access Between Objects and Retrieve Data From STK
- Retrieve and view the altitude of the satellite during an access interval.
- In Python, copy and paste all the code from the Editor window (to included comments (#)) from Task 5 and paste it to the Command Window.
- Click Enter.
satelliteDP = satellite.DataProviders.Item('LLA State')
satelliteDP2 = satelliteDP.QueryInterface(STKObjects.IAgDataProviderGroup)
satelliteDP3 = satelliteDP2.Group.Item('Fixed')
satelliteDP4 = satelliteDP3.QueryInterface(STKObjects.IAgDataPrvTimeVar)
rptElements = ['Time', 'Lat', 'Lon', 'Alt']
satellitDPTimeVar = satelliteDP4.ExecElements(accessStartTimes,accessStopTimes, 60, rptElements)
satelliteAltitude = satellitDPTimeVar.DataSets.GetDataSetByName('Alt').GetValues()
print(satelliteAltitude)
Don't forget to save your work!