最近在学习python,发现这个语言挺有意思,写起来比较轻松。后面打算用它代替matlab来做数据分析。写了一个comtrade格式录波文件的读取类。目前仅支持读取comtrade的binary格式文件。仅读取模拟量,不解析开关量。上comtrade类代码。
#!/usr/bin/env python 3.8.0
# -*- coding: utf-8 -*-
#
''' ########################################################################'''
import matplotlib.pyplot as plt
import re
import struct
''' ########################################################################
##
## COMTRADE文件读取
##
########################################################################'''
'''------------------------- 全局变量 -------------------------'''
ERROR = -1
OK = 0
class Comtrade():
"""comtrade文件类"""
'''------------------------- 初始化变量 -------------------------'''
def __init__(self, filename=''):
#--文件名
self.file_name = filename
self.cfg_file_name = filename+'.cfg'
self.dat_file_name = filename+'.dat'
print("## The cometrade file:"+self.file_name)
# 列表,存储cfg文件每一行内容
self.cfglines = []
# 模拟量通道数
self.analog_chanel_num = 0
# 开关量word数
self.digit_chanel_num = 0
# DAT数据格式 BYNARY 或 ASCII
self.file_format = ''
# 采样点数
self.wave_points = 0
# 字典list
# cfg模拟量通道信息 字典
# An,ch-id,ph,ccbm,uu,a,b,skew,min,max,primary,secodary,ps<CR/LF>
self.ana_infos = []
# 每一帧的数据长度 单位BYTE
# 4, 4, 2, 2, 2, 2, 2, 2
# n,timestamp,A1,A2,……Ak,D1,D2,……Dm<CR/LF>
self.dataframe_lenth = 0
# 二进制数据帧
self.packed_datas = []
# 大小端
self.endian_mode = 'little-endian'
# 二进制数据解析格式
self.pack_format = ''
'''------------------------- 读取CFG文件 -------------------------'''
def cfg_read(self):
#--读取CFG文件内容,按行存储至列表
try:
with open(self.cfg_file_name) as cfg_file_obj:
lines = cfg_file_obj.readlines()
for line in lines:
line = line.rstrip()
line = line.split(',')
self.cfglines.append(line)
#print(self.cfglines[-1])
<