How to Read CI Data Files in Python

Introduction
The ATFX API C# DLL files can be used in three supported coding languages: Python, Matlab and LabVIEW. A specific package that allows Python to integrate with .NET Common Language Runtime needs to be installed for the C# DLL files to work in Python. Several packages accomplish this, such as IronPython and Python.NET. In this example, Python.NET will be used.

Please refer to the ATFX API manual and the provided C# & Python Demo code for more detailed information on how to implement the following code sections and properties in a class. Download the package from our Programming Corner.

There are times when the script is reading the correct file path and the DLL files exist, but the script shows an error stating that it cannot find the DLL files. One solution is to click a box labeled Unblock in the DLL file properties accompanying the following description, “This file came from another computer and might be blocked to help protect this computer.”. Unblocking the DLL file should allow the scripts relying on the DLL files to locate and read them.

CI ATFX Reader properties

Installing Python.NET to import C# DLL Files
To import C# DLL files to Python, install Python.NET so that Python can communicate with .NET Common Language Runtime.

The package can be installed via “pip install pythonnet” or from their github page: https://github.com/pythonnet/pythonnet. Please note the Python version needed to run Python.NET on the github page.

After Python.NET has been installed, the following “import clr” must be used to add references to .NET assemblies and ATFX API DLL files. Then the namespaces from the ATFX API can be imported.

 

#---Pythonnet clr import
import clr
parentPath = "C:\\Users\\KevinCheng\\ATFX API Package v1.4\\"

clr.AddReference(parentPath + "CI.ATFX.Reader.dll")
clr.AddReference(parentPath + "Common.dll")

#---C# .NET imports & dll imports
from ASAM.ODS.ATFXML import *
from ASAM.ODS.NVH import *
from Common import *
from Common.Spider import *
from EDM.Recording import *
from EDM.RecordingInterface import *
from EDM.Utils import *

 

Opening an ATFX File and Extracting Various Data
Much of the code shown here or in the provided Python Demo Script are reapplied from the C# Demo code. The code samples in ATFX API Reading a ATFX File in C# will be reapplied here to show how to open an ATFX file and extract data in Python.

 

recordingPath = "C:\\Users\\KevinCheng\\Downloads\\gps test example\\"
recordingPathRegular = recordingPath + "SIG0020.atfx"

#OpenRecording(string, out IRecording)
# openRecordSucceed data is required for the OpenRecording as it is the returned boolean

openRecordSucceed, recording = RecordingManager.Manager.OpenRecording(recordingPathRegular, None)

print("\nRecording Properties\n")
print(recording.RecordingProperty.CreateTime)
print(recording.RecordingProperty.Instruments)
print(recording.RecordingProperty.RecordingName)

print("\nSignal 1 Properties\n")
print(recording.Signals[0].Properties.SamplingRate)
print(recording.Signals[0].Properties.SignalName)
print(recording.Signals[0].Properties.SoftwareVersion)

print("\nRecording GPS Properties\n")
recording = ODSNVHATFXMLRecording(recordingPathRegular)
nvhMeasurement = recording.Measurement
nvhEnvironment = recording.Environment

print("GPS Enabled: " nvhMeasurement.GPSEnabled)
print ("Longitude: ", nvhMeasurement.Longitude)
print ("Latitude: ", nvhMeasurement.Latitude)
print ("Altitude: ", nvhMeasurement.Altitude)
print ("Nanoseconds Elapsed: ", nvhMeasurement.NanoSecondElapsed)

print("Created Time (UTC): ",
recording.Environment.GetUTCTime(recording.RecordingProperty.CreateTime))

 

The above code should print the following screenshot:

How to read CI Data Files in Python

The following screenshot is an example of the provided Python Demo Script print statements:

How to read CI Data Files in Python
How to read CI Data Files in Python
How to read CI Data Files in Python