Package: pytcs
pytcsA Python package for reading exported TwinCAT Scope Files.Export your TwinCAT Scope files.svdxto.csvand read them into Python.quickstartOpen a file and create apandas.DataFrame:frompytcsimportScopeFilesf=ScopeFile("example.csv")# open file and read metadatadf=sf.as_pandas()# convert to pandas DataFrameuser guideinstallationInstall usingpiporconda/mamba:pip install pytcsconda install pytcsloading dataTo get started, open a file usingpytcs.ScopeFile:frompytcsimportScopeFilesf=ScopeFile("example.csv")sf# > <TwinCAT Scope File at 0x2a157ca9310># > name:    example# > runtime: 0:00:00.999000# > start:   2022-05-02T13:56:24.376000+00:00# ># > Channels:# >   *var_REAL64: 1.0 ms [None]# >   *var_UINT64: 1.0 ms [None]# >   *var_UINT32: 1.0 ms [None]# >   *var_UINT16: 1.0 ms [None]# >   *func_units_scaled: 1.0 ms [dV]You can see the list of channels contained in the file together with the sample time and the unit.
When creating aScopeFileinstance only the metadata about the channels is read from the file header.
The actual channel data is not loaded, indicated by the*in front of the channels.You can load all or only a list of channels by usingScopeFile.load()sf.load()sf# > <TwinCAT Scope File at 0x2a157ca9310># > name:    example# > runtime: 0:00:00.999000# > start:   2022-05-02T13:56:24.376000+00:00# ># > Channels:# >   var_REAL64: 1.0 ms [None]# >   var_UINT64: 1.0 ms [None]# >   var_UINT32: 1.0 ms [None]# >   var_UINT16: 1.0 ms [None]# >   func_units_scaled: 1.0 ms [dV]accessing individual channelsIndividual channels can be accessed by their name:sf["func_units_scaled"]# > ScopeChannel(name='func_units_scaled',# >   time=array([  0.,   1.,   2., ..., 997., 998., 999.]),# >   values=array([   10.,     0.,   -10., ..., -9960., -9970., -9980.]),# >   sample_time=1.0, time_offset=0.0, units='dV')CSV backendsThe default implementation ofpytcsusespandas.read_csvfor parsing CSV files.
Thepandasaims to provide the most flexible support for the various formatting options provided by the TwinCAT Scope export tool.To improve performance for large files,datatablecan be set as an alternative CSV backend.
Datatable can be selected by usingScopeFile.read(..., backend="datatable").
However it should be consideredexperimentalsince some CSV formats can run into known issues and errors.
If you want to use thedatatablebackend it is recommended run detailed tests with the target format (or change the target format).exporting to pandas and xarrayTo work with the data, convert them to a pandas or xarray object. Channels will automatically be loaded form the file when exporting to other formats.
You can select individual channels to export.sf.as_pandas(channels=["var_REAL64","var_UINT16"])# >                          var_REAL64  var_UINT16# > time# > 2022-05-02 13:56:24.376         0.0         0.0# > 2022-05-02 13:56:24.377         1.0         1.0# > 2022-05-02 13:56:24.378         2.0         2.0# > 2022-05-02 13:56:24.379         3.0         3.0# > 2022-05-02 13:56:24.380         4.0         4.0# > ...                             ...         ...# > 2022-05-02 13:56:25.371       995.0       995.0# > 2022-05-02 13:56:25.372       996.0       996.0# > 2022-05-02 13:56:25.373       997.0       997.0# > 2022-05-02 13:56:25.374       998.0       998.0# > 2022-05-02 13:56:25.375       999.0       999.0# ># > [1000 rows x 2 columns]Exporting to anxarray.Datasetwill preserve the metadata as attributes.sf.as_xarray(channels=["var_REAL64","var_UINT16"])# > <xarray.Dataset># > Dimensions:     (time: 1000)# > Coordinates:# >   * time        (time) datetime64[ns] 2022-05-02T13:56:24.376000 ... 2022-05-...# > Data variables:# >     var_REAL64  (time) float64 0.0 1.0 2.0 3.0 4.0 ... 996.0 997.0 998.0 999.0# >     var_UINT16  (time) float64 0.0 1.0 2.0 3.0 4.0 ... 996.0 997.0 998.0 999.0# > Attributes:# >     ScopeName:   tc3_scope_3_4_3145_3# >     File:        C:\Python\weldx-dev\pytcs\tests\data\tc3_scope_3_4_3145_3-Co...# >     StartTime:   132959733843760000# >     EndTime:     132959733853750000# >     start_time:  2022-05-02T13:56:24.376000+00:00# >     run_time:    0:00:00.999000dtype supportBy default, all data will be read asnp.float64.
When importing data withScopeFile.loadusing the optionnative_dtypes=True,  imported data will be converted to their native dtypes.TwinCAT ScopenumpyIEC61131-3BITnp.bool_BOOLINT8np.int8SINTINT16np.int16INTINT32np.int32DINTINT64np.int64LINTUINT8np.uint8USINTUINT16np.uint16UINTUINT32np.uint32UDINTUINT64np.uint64ULINTREAL32np.float32REALREAL64np.float64LREALexport options supportThe following table lists the compatible ✅ and currently uncompatible ❌ options of the ScopeExporter:file and value formatsScaleValuestrue✅false❌DecimalMark.✅,✅SeperatorTab✅Blank (space)❌Colon✅Semicolon✅Comma❌ExcludeDoubleTimestamptrue✅false✅SortChannelstrue✅false✅FullTimeStamptrue❌false✅AdditionalEmptyLinetrue❌false✅ContainEOFtrue✅false✅HeaderKonfigurationFull Header✅ArraySeperatorTab✅AdditionalArraySeperatortrue❌false✅IncludeTriggerInfostrue❌false✅IncludeMarkerTablesNone✅MarkerTableOnlyIncludedChannelstrue❌false✅MarkerTableOnlyIncludedMarkertrue❌false✅