Package: comtrade
Python ComtradePython Comtradeis a module for Python 3 designed to readCommon 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.StandardRevisionIEEE C37.111(TM)-19911991IEEE C37.111(TM)-19991999IEC 60255-24:20012001IEEE C37.111(TM)-2013 / IEC 60255-24:20132013Installationpip install comtradeOr just copycomtrade.pyfrom this repository.RequirementsPython 3.6 or newer(optional) numpy for speed improvement(optional) pandas for data frame supportHow to UseThe examples below shows how to open both CFG and DAT files or the new CFF file to plot (usingpyplot) analog channel oscillography.CFG and DAT files (all revisions)Comtrade files separated in CFG and DAT formats can also be read withloadfunction. ACFGfile path must be passed as an argument and, optionally, aDATfile path too (if the file name is not equal of the CFG file).importmatplotlib.pyplotaspltimportcomtraderec=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 throughComtrade.hdrandComtrade.infproperties.CFF files (2013 revision)importmatplotlib.pyplotaspltimportcomtraderec=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 functionloadmust be called with theCFFfile path specified.Comtrade.analogandComtrade.statuslists stores analog and status channel sample lists respectively. These can be accessed through zero-based indexes, i.e.,Comtrade.analog[0]. The listComtrade.timestores each sample time in seconds.More information can be accessed throughComtrade.cfgobject, which stores data such as detailed channel information.Data of additional sections, such as HDR and INF, can be accessed throughhdrandinfproperties, respectively.Data framesThe module supports the use ofpandas.DataFrameas a data structure. Use the conveninent functionload_as_dataframeor callComtrade.to_dataframe()function after loading the data.importcomtradedf=comtrade.load_as_dataframe("sample_files/sample_ascii.cfg")print(df.head())# Or, alternativelyrec=comtrade.load("sample_files/sample_ascii.cfg")df=rec.to_dataframe()print(df.head())FeaturesThis 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 SupportCFG file formatxxxxxDAT file formatxxxxxHDR file formatxxxxnoINF file formatxxxnoCFF file formatxxASCII data file formatxxxxxBinary data file formatxxxxxBinary32 data file formatxxFloat32 data file formatxxSchema for phasor dataxnoFeatures1991199920012013Module SupportCOMTRADE standard revisionxxxxTimestamp multiplication factorxxxxTime code and local codexxTime quality of the samplesxxAnalog channel time skewxxxPartialAnalog channel primary and secondary VT or CT ratioxxxxStatus channel phase and monitored circuitxxxxMultiple sample ratesxxxxPartialNanoseconds scalexxxUnsupported featuresNanoseconds time base within Python'sdatetimeobjects (such asstart_timestampandtrigger_timestampproperties). 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 featuresNumpy arrays as data structuresThe use ofnumpy.arrayas a data structure to hold time, analog and status data can be enforced
inComtradeobject constructor:obj=Comtrade(use_numpy_arrays=True)It may improve performance for computations after loading data.Replace missing data withfloat('nan')Missing analog values are replaced withfloat('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 Value1991ascii""(empty string)1999, 2013ascii999991991binary0xFFFF1999, 2013binary0x8000or-327682013binary320x80000000or-21474836482013float32minimum negative float valueFile encodingsSpecify theencodingas a keyword argument on all load methods as you'd specify for common file loading:importcomtradecomtrade.load("sample_files/sample_ascii.cff",encoding="iso-8859-1")Documentationhttps://github.com/dparrini/python-comtradeSupportFeel free to report any bugs you find. You are welcome to fork and submit pull requests.DevelopmentTo run tests, use Python'sunittest. From a clone of the GitHub repository, run the command:python3-munittest./tests/tests.pyLicenseThe module is available atGitHubunder the MIT license.