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).

  1. Launch Spyder (or your preferred method)
  2. Select the Home tab.
  3. Click the Open icon.
  4. Browse to the script file located at Install Directory\Help\stktraining\scripts.
  5. 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:

  1. Set up your Python workspace
  2. from win32api import GetSystemMetrics

  3. Now import the Com Types
  4. 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.

  1. Create a new instance of STK11.
  2. In Python, type the following code into the Command window:
  3. uiApplication=CreateObject("STK11.Application")

  4. Click Enter.
  5. uiApplication.Visible=True
    uiApplication.UserControl=True

  6. Click Enter.
  7. Grab a handle on the STK application root.
  8. In Python, type the following code into the Command window and then click Enter.
  9. root = uiApplication.Personality2

  10. Click Enter.
  11. 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.

  1. Create a new scenario, analysis period and reset the animation time.
  2. 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.
  3. ##

    #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();

  4. Click Enter.

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.

  1. Insert Target and Satellite objects into the scenario.

  2. 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.

  3. ##

    #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')

  4. Click Enter.

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.

  1. Browse to the STK Programming Interface help files.
  2. 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.
  3. If you cannot locate the code, expand the followingClosed
    1. The location of the required code snippets is STK Programming Interface > Using Core Libraries > STK Object Model > Python Code Snippets. Locate STK Objects > Access. The required snippet is Compute an access between two STK Objects (using IAgStkObject interface).
    2. access = satellite.GetAccessToObject(target)

      access.ComputeAccess()

    3. Retrieve and view the access data in Python.
    4. In Python, copy and paste all the code from the Editor window (to included comments (#)) from Task 4 and paste it to the Command Window.
    5. accessDP = access.DataProviders.Item('Access Data')

      accessDP2 = accessDP.QueryInterface(STKObjects.IAgDataPrvInterval)

      results = accessDP2.Exec(scenario2.StartTime, scenario2.StopTime)

      accessStartTimes = results.DataSets.GetDataSetByName('Start Time').GetValues()

      accessStopTimes = results.DataSets.GetDataSetByName('Stop Time').GetValues()

      print(accessStartTimes,accessStopTimes)

  4. Click Enter.

Compute Access Between Objects and Retrieve Data From STK

  1. Retrieve and view the altitude of the satellite during an access interval.
  2. 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.
  3. 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)

  4. Click Enter.

Don't forget to save your work!