Socket
Socket
Sign inDemoInstall

comtrade

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

comtrade

A module designed to read Common Format for Transient Data Exchange (COMTRADE) file format.


Maintainers
1

Python Comtrade

Python Comtrade is a module for Python 3 designed to read Common Format for Transient Data Exchange (COMTRADE) files. These consists of oscillography data recorded during power system outages, control systems tests, validation and tests of field equipment, protective relaying logs, etc. The COMTRADE format is defined by IEEE Standards, summarized in the table below. Some equipment vendors put additional information in proprietary versions of it. This module aims IEEE definitions but may support those proprietary versions.

StandardRevision
IEEE C37.111(TM)-19911991
IEEE C37.111(TM)-19991999
IEC 60255-24:20012001
IEEE C37.111(TM)-2013 / IEC 60255-24:20132013

Installation

pip install comtrade

Or just copy comtrade.py from this repository.

Requirements

  • Python 3.6 or newer
  • (optional) numpy for speed improvement
  • (optional) pandas for data frame support

How to Use

The examples below shows how to open both CFG and DAT files or the new CFF file to plot (using pyplot) analog channel oscillography.

CFG and DAT files (all revisions)

Comtrade files separated in CFG and DAT formats can also be read with load function. A CFG file path must be passed as an argument and, optionally, a DAT file path too (if the file name is not equal of the CFG file).

import matplotlib.pyplot as plt
import comtrade

rec = comtrade.load("sample_files/sample_ascii.cfg", "sample_files/sample_ascii.dat")
print("Trigger time = {}s".format(rec.trigger_time))

plt.figure()
plt.plot(rec.time, rec.analog[0])
plt.plot(rec.time, rec.analog[1])
plt.legend([rec.analog_channel_ids[0], rec.analog_channel_ids[1]])
plt.show()

It will read the contents of additional header (*.hdr) and information (*.inf) files. Their contents are available through Comtrade.hdr and Comtrade.inf properties.

CFF files (2013 revision)

import matplotlib.pyplot as plt
import comtrade

rec = comtrade.load("sample_files/sample_ascii.cff")
print("Trigger time = {}s".format(rec.trigger_time))

plt.figure()
plt.plot(rec.time, rec.analog[0])
plt.plot(rec.time, rec.analog[1])
plt.legend([rec.analog_channel_ids[0], rec.analog_channel_ids[1]])
plt.show()

The module function load must be called with the CFF file path specified.

Comtrade.analog and Comtrade.status lists stores analog and status channel sample lists respectively. These can be accessed through zero-based indexes, i.e., Comtrade.analog[0]. The list Comtrade.time stores each sample time in seconds.

More information can be accessed through Comtrade.cfg object, which stores data such as detailed channel information.

Data of additional sections, such as HDR and INF, can be accessed through hdr and inf properties, respectively.

Data frames

The module supports the use of pandas.DataFrame as a data structure. Use the conveninent function load_as_dataframe or call Comtrade.to_dataframe() function after loading the data.

import comtrade
df = comtrade.load_as_dataframe("sample_files/sample_ascii.cfg")
print(df.head())

# Or, alternatively
rec = comtrade.load("sample_files/sample_ascii.cfg")
df = rec.to_dataframe()
print(df.head())

Features

This module implements some of the functionality described in each of the Standard revisions. The tables below lists some features and file formats and which revision supports it. It also shows whether this module support the feature or the format.

Feel free to pull requests implementing one of these unsupported features or fixing bugs.

Formats1991199920012013Module Support
CFG file formatxxxxx
DAT file formatxxxxx
HDR file formatxxxxno
INF file formatxxxno
CFF file formatxx
ASCII data file formatxxxxx
Binary data file formatxxxxx
Binary32 data file formatxx
Float32 data file formatxx
Schema for phasor dataxno
Features1991199920012013Module Support
COMTRADE standard revisionxxxx
Timestamp multiplication factorxxxx
Time code and local codexx
Time quality of the samplesxx
Analog channel time skewxxxPartial
Analog channel primary and secondary VT or CT ratioxxxx
Status channel phase and monitored circuitxxxx
Multiple sample ratesxxxxPartial
Nanoseconds scalexxx

Unsupported features

  • Nanoseconds time base within Python's datetime objects (such as start_timestamp and trigger_timestamp properties). It warns the user but doesn't use it, truncating the numbers.
  • Use of multiple sample rates in time calculations for binary data.

Additional settings and features

Numpy arrays as data structures

The use of numpy.array as a data structure to hold time, analog and status data can be enforced in Comtrade object constructor:

obj = Comtrade(use_numpy_arrays=True)

It may improve performance for computations after loading data.

Replace missing data with float('nan')

Missing analog values are replaced with float('nan'). The value used to represent missing data varies with the Comtrade dat format and revision year. These are listed in the table next.

revisionformatMissing Data Value
1991ascii"" (empty string)
1999, 2013ascii99999
1991binary0xFFFF
1999, 2013binary0x8000 or -32768
2013binary320x80000000 or -2147483648
2013float32minimum negative float value
File encodings

Specify the encoding as a keyword argument on all load methods as you'd specify for common file loading:

import comtrade
comtrade.load("sample_files/sample_ascii.cff", encoding="iso-8859-1")

Documentation

https://github.com/dparrini/python-comtrade

Support

Feel free to report any bugs you find. You are welcome to fork and submit pull requests.

Development

To run tests, use Python's unittest. From a clone of the GitHub repository, run the command:

python3 -m unittest ./tests/tests.py

License

The module is available at GitHub under the MIT license.

FAQs


Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc