diff --git a/analog.go b/analog.go index 736f9c4..4913c85 100644 --- a/analog.go +++ b/analog.go @@ -1,69 +1,70 @@ package comtrade -type ComtradeAnalogChan struct { - An uint32 //模拟通道索引号 an - ChId string //通道标识 ch_id - Ph string //通道相别标识 ph - Ccbm string //被监视的电路元件 ccbm - Uu string //通道单位 uu - A float32 //通道增益系数 a - B float32 //通道偏移量 b - Skew float32 //通道时滞 skew - Min float32 //通道最小值 min - Max float32 //通道最大值 max - Primary float32 //一次系数 primary - Secondary float32 //二次系数 secondary - PS string //一次二次标识 ps +// AnalogChan 模拟通道 +type AnalogChan struct { + An uint32 `json:"an"` //模拟通道索引号 + ChId string `json:"ch_id"` //通道标识 + Ph string `json:"ph"` //通道相别标识 + Ccbm string `json:"ccbm"` //被监视的电路元件 + Uu string `json:"uu"` //通道单位 + A float32 `json:"a"` //通道增益系数 + B float32 `json:"b"` //通道偏移量 + Skew float32 `json:"skew"` //通道时滞 + Min float32 `json:"min"` //通道最小值 + Max float32 `json:"max"` //通道最大值 + Primary float32 `json:"primary"` //一次系数 + Secondary float32 `json:"secondary"` //二次系数 + PS string `json:"ps"` //一次二次标识 } -func (c *ComtradeAnalogChan) GetAn() uint32 { +func (c *AnalogChan) GetAn() uint32 { return c.An } -func (c *ComtradeAnalogChan) GetChId() string { +func (c *AnalogChan) GetChId() string { return c.ChId } -func (c *ComtradeAnalogChan) GetPh() string { +func (c *AnalogChan) GetPh() string { return c.Ph } -func (c *ComtradeAnalogChan) GetCcbm() string { +func (c *AnalogChan) GetCcbm() string { return c.Ccbm } -func (c *ComtradeAnalogChan) GetUu() string { +func (c *AnalogChan) GetUu() string { return c.Uu } -func (c *ComtradeAnalogChan) GetA() float32 { +func (c *AnalogChan) GetA() float32 { return c.A } -func (c *ComtradeAnalogChan) GetB() float32 { +func (c *AnalogChan) GetB() float32 { return c.B } -func (c *ComtradeAnalogChan) GetSkew() float32 { +func (c *AnalogChan) GetSkew() float32 { return c.Skew } -func (c *ComtradeAnalogChan) GetMin() float32 { +func (c *AnalogChan) GetMin() float32 { return c.Min } -func (c *ComtradeAnalogChan) GetMax() float32 { +func (c *AnalogChan) GetMax() float32 { return c.Max } -func (c *ComtradeAnalogChan) GetPrimary() float32 { +func (c *AnalogChan) GetPrimary() float32 { return c.Primary } -func (c *ComtradeAnalogChan) GetSecondary() float32 { +func (c *AnalogChan) GetSecondary() float32 { return c.Secondary } -func (c *ComtradeAnalogChan) GetPS() string { +func (c *AnalogChan) GetPS() string { return c.PS } diff --git a/ascii_config.go b/ascii_config.go index 8c0e475..b7320e7 100644 --- a/ascii_config.go +++ b/ascii_config.go @@ -61,12 +61,12 @@ func ParseComtradeCfg(filePath string) (*ComtradeCfg, error) { if value, err := strconv.ParseUint(string(bytes.TrimSuffix(bytes.TrimSpace(tempList[2]), []byte("D"))), 10, 64); err != nil { return nil, err } else { - comtradeCfg.DigtalNum = uint32(value) + comtradeCfg.DigitalNum = uint32(value) } // initialize analog and digital channels - comtradeCfg.Analog = make([]ComtradeAnalogChan, comtradeCfg.AnalogNum) - comtradeCfg.Digital = make([]ComtradeDigitalChan, comtradeCfg.DigtalNum) + comtradeCfg.Analog = make([]AnalogChan, comtradeCfg.AnalogNum) + comtradeCfg.Digital = make([]DigitalChan, comtradeCfg.DigitalNum) // 读取模拟通道 read analog channels for i := 0; i < int(comtradeCfg.AnalogNum); i++ { @@ -130,7 +130,7 @@ func ParseComtradeCfg(filePath string) (*ComtradeCfg, error) { } // read digit channels - for i := 0; i < int(comtradeCfg.DigtalNum); i++ { + for i := 0; i < int(comtradeCfg.DigitalNum); i++ { tempList = bytes.Split(lines[2+int(comtradeCfg.AnalogNum)+i], []byte(",")) if len(tempList) < 3 { return nil, ErrReadDigitalChannel diff --git a/ascii_data.go b/ascii_data.go index 04f67a5..da02501 100644 --- a/ascii_data.go +++ b/ascii_data.go @@ -18,8 +18,8 @@ func init() { type AsciiData struct { } -func (a *AsciiData) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*ComtradeData, error) { - comtradeData := &ComtradeData{} +func (a *AsciiData) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) { + comtradeData := &Data{} datFile, err := os.Open(filePath) if err != nil { return nil, err @@ -35,8 +35,8 @@ func (a *AsciiData) Parse(filePath string, analogNum, digitalNum, endSamp uint32 return nil, ErrInvalidFile } - comtradeData.AnalogData = make([]ComtradeAnalogData, int(endSamp)) - comtradeData.DigitalData = make([]ComtradeDigitalData, int(endSamp)) + comtradeData.AnalogData = make([]AnalogData, int(endSamp)) + comtradeData.DigitalData = make([]DigitalData, int(endSamp)) for i := 0; i < int(endSamp); i++ { comtradeData.AnalogData[i].Data = make([]int32, analogNum) diff --git a/ascii_data_test.go b/ascii_data_test.go index 9abcda7..5111022 100644 --- a/ascii_data_test.go +++ b/ascii_data_test.go @@ -14,12 +14,12 @@ func TestAsciiData_Parse(t *testing.T) { } a := &AsciiData{} - dat, err := a.Parse("testdata/test2.dat", cfg.AnalogNum, cfg.DigtalNum, cfg.EndSamp[len(cfg.EndSamp)-1]) + dat, err := a.Parse("testdata/test2.dat", cfg.AnalogNum, cfg.DigitalNum, cfg.EndSamp[len(cfg.EndSamp)-1]) if err != nil { t.Errorf("Parse() error = %v", err) return } - if reflect.TypeOf(dat) != reflect.TypeOf(&ComtradeData{}) { + if reflect.TypeOf(dat) != reflect.TypeOf(&Data{}) { t.Errorf("Parse() got = %v", dat) } t.Log(dat) diff --git a/binary32_data.go b/binary32_data.go index cc8aafb..659411d 100644 --- a/binary32_data.go +++ b/binary32_data.go @@ -10,6 +10,6 @@ func init() { type Binary32Data struct { } -func (b *Binary32Data) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*ComtradeData, error) { - return &ComtradeData{}, nil +func (b *Binary32Data) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) { + return &Data{}, nil } diff --git a/binary_data.go b/binary_data.go index d8da6ea..5fbf0aa 100644 --- a/binary_data.go +++ b/binary_data.go @@ -35,8 +35,8 @@ func init() { type BinaryData struct { } -func (b *BinaryData) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*ComtradeData, error) { - comtradeData := &ComtradeData{} +func (b *BinaryData) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) { + comtradeData := &Data{} // 打开文件 file, err := os.Open(filePath) @@ -45,8 +45,8 @@ func (b *BinaryData) Parse(filePath string, analogNum, digitalNum, endSamp uint3 } defer file.Close() - comtradeData.AnalogData = make([]ComtradeAnalogData, int(endSamp)) - comtradeData.DigitalData = make([]ComtradeDigitalData, int(endSamp)) + comtradeData.AnalogData = make([]AnalogData, int(endSamp)) + comtradeData.DigitalData = make([]DigitalData, int(endSamp)) for i := 0; i < int(endSamp); i++ { comtradeData.AnalogData[i].Data = make([]int32, analogNum) diff --git a/binary_data_test.go b/binary_data_test.go index eb9241e..77a2413 100644 --- a/binary_data_test.go +++ b/binary_data_test.go @@ -14,12 +14,12 @@ func TestBinaryData_Parse(t *testing.T) { } b := &BinaryData{} - dat, err := b.Parse("testdata/test1.dat", cfg.AnalogNum, cfg.DigtalNum, cfg.EndSamp[len(cfg.EndSamp)-1]) + dat, err := b.Parse("testdata/test1.dat", cfg.AnalogNum, cfg.DigitalNum, cfg.EndSamp[len(cfg.EndSamp)-1]) if err != nil { t.Errorf("Parse() error = %v", err) return } - if reflect.TypeOf(dat) != reflect.TypeOf(&ComtradeData{}) { + if reflect.TypeOf(dat) != reflect.TypeOf(&Data{}) { t.Errorf("Parse() got = %v", dat) } t.Log(dat) diff --git a/comtrade.go b/comtrade.go index d9ecb05..85a7a78 100644 --- a/comtrade.go +++ b/comtrade.go @@ -2,12 +2,12 @@ package comtrade type Comtrade struct { Conf *ComtradeCfg - Data *ComtradeData + Data *Data } -func ParseComtrade(configfilePath, datafilePath string) (comtrade *Comtrade, err error) { +func ParseComtrade(configurePath, datafilePath string) (comtrade *Comtrade, err error) { c := &Comtrade{} - c.Conf, err = c.parseComtradeConfig(configfilePath) + c.Conf, err = c.parseComtradeConfig(configurePath) if err != nil { return nil, err } @@ -18,18 +18,18 @@ func ParseComtrade(configfilePath, datafilePath string) (comtrade *Comtrade, err return c, nil } -func (c *Comtrade) parseComtradeConfig(configfilePath string) (*ComtradeCfg, error) { - comtradeConfig, err := ParseComtradeCfg(configfilePath) +func (c *Comtrade) parseComtradeConfig(configurePath string) (*ComtradeCfg, error) { + comtradeConfig, err := ParseComtradeCfg(configurePath) if err != nil { return nil, err } return comtradeConfig, nil } -func (c *Comtrade) parseComtradeData(datafilePath string) (*ComtradeData, error) { +func (c *Comtrade) parseComtradeData(datafilePath string) (*Data, error) { creator, ok := Parsers[c.Conf.Ft] if !ok { - return nil, ErrUnknowTypeOfData + return nil, ErrUnknownTypeOfData } parser := creator() comtradeData, err := parser.Parse(datafilePath, c.Conf.GetAnalogNum(), c.Conf.GetDigtalNum(), c.Conf.GetEndSamp()[len(c.Conf.GetEndSamp())-1]) diff --git a/config.go b/config.go index 5f7f6a7..81192df 100644 --- a/config.go +++ b/config.go @@ -5,21 +5,21 @@ import ( ) type ComtradeCfg struct { - StationName string //厂站名称 station_name - RecDevID string //记录设备ID rec_dev_id - RevYear uint16 //COMTRADE版本年份 rev_year 1991、1999、2013 - Total uint32 //总通道数 total - AnalogNum uint32 //模拟通道数 analog_num - DigtalNum uint32 //数字/状态通道数 digital_num - Analog []ComtradeAnalogChan //模拟通道 analog - Digital []ComtradeDigitalChan //数字/状态通道 digital - Lf float32 //标称频率 lf - Nrates uint16 //采样率个数 nrates - Samp []float32 //采样率 samp - EndSamp []uint32 //最末采样序号 end_samp - FirstDataTime time.Time //第一条数据时间 first_data_time - TriggerTime time.Time //采样触发时间 trigger_time - Ft string //数据文件类型,ASCII、INARY、BINARY32、FLOAT32 ft + StationName string //厂站名称 station_name + RecDevID string //记录设备ID rec_dev_id + RevYear uint16 //COMTRADE版本年份 rev_year 1991、1999、2013 + Total uint32 //总通道数 total + AnalogNum uint32 //模拟通道数 analog_num + DigitalNum uint32 //数字/状态通道数 digital_num + Analog []AnalogChan //模拟通道 analog + Digital []DigitalChan //数字/状态通道 digital + Lf float32 //标称频率 lf + Nrates uint16 //采样率个数 nrates + Samp []float32 //采样率 samp + EndSamp []uint32 //最末采样序号 end_samp + FirstDataTime time.Time //第一条数据时间 first_data_time + TriggerTime time.Time //采样触发时间 trigger_time + Ft string //数据文件类型,ASCII、BINARY、BINARY32、FLOAT32 ft // 2017 TimeMult float32 //时间倍率因子 time_mult @@ -50,7 +50,7 @@ func (cc *ComtradeCfg) GetAnalogNum() uint32 { } func (cc *ComtradeCfg) GetDigtalNum() uint32 { - return cc.DigtalNum + return cc.DigitalNum } func (cc *ComtradeCfg) GetLf() float32 { @@ -101,10 +101,10 @@ func (cc *ComtradeCfg) GetLeapsec() uint8 { return cc.Leapsec } -func (cc *ComtradeCfg) GetAnalog() []ComtradeAnalogChan { +func (cc *ComtradeCfg) GetAnalog() []AnalogChan { return cc.Analog } -func (cc *ComtradeCfg) GetDigital() []ComtradeDigitalChan { +func (cc *ComtradeCfg) GetDigital() []DigitalChan { return cc.Digital } diff --git a/data.go b/data.go index 306f8bf..7dcd3c8 100644 --- a/data.go +++ b/data.go @@ -1,29 +1,29 @@ package comtrade -// ComtradeData is the data of a comtrade file. -type ComtradeData struct { - AnalogData []ComtradeAnalogData - DigitalData []ComtradeDigitalData +// Data is the data of a comtrade file. +type Data struct { + AnalogData []AnalogData + DigitalData []DigitalData } -// ComtradeAnalogData is the analog data of a comtrade file. -type ComtradeAnalogData struct { - N uint32 //模拟通道序号 n - Timestamp uint32 //模拟通道时标 timestamp - Data []int32 //模拟通道数据 data +// AnalogData is the analog data of a comtrade file. +type AnalogData struct { + N uint32 `json:"n"` //模拟通道序号 + Timestamp uint32 `json:"timestamp"` //模拟通道时标 + Data []int32 `json:"data"` //模拟通道数据 } -// ComtradeDigitalData is the digital data of a comtrade file. -type ComtradeDigitalData struct { - N uint32 //数字通道序号 n - Timestamp uint32 //数字通道时标 timestamp - Data []uint8 //数字通道数据 data +// DigitalData is the digital data of a comtrade file. +type DigitalData struct { + N uint32 `json:"n"` //数字通道序号 + Timestamp uint32 `json:"timestamp"` //数字通道时标 + Data []uint8 `json:"data"` //数字通道数据 } -func (cd *ComtradeData) GetAnalogData() []ComtradeAnalogData { +func (cd *Data) GetAnalogData() []AnalogData { return cd.AnalogData } -func (cd *ComtradeData) GetDigitalData() []ComtradeDigitalData { +func (cd *Data) GetDigitalData() []DigitalData { return cd.DigitalData } diff --git a/digital.go b/digital.go index a9f5d40..0c99d30 100644 --- a/digital.go +++ b/digital.go @@ -1,29 +1,30 @@ package comtrade -type ComtradeDigitalChan struct { - Dn uint32 //数字/状态通道索引号 dn - ChId string //通道标识 ch_id - Ph string //通道相别标识 ph - Ccbm string //被监视的电路元件 ccbm - Y uint8 //通道状态 y +// DigitalChan 数字通道 +type DigitalChan struct { + Dn uint32 `json:"dn"` //数字/状态通道索引号 + ChId string `json:"ch_id"` //通道标识 + Ph string `json:"ph"` //通道相别标识 + Ccbm string `json:"ccbm"` //被监视的电路元件 + Y uint8 `json:"y"` //通道状态 } -func (c *ComtradeDigitalChan) GetDn() uint32 { +func (c *DigitalChan) GetDn() uint32 { return c.Dn } -func (c *ComtradeDigitalChan) GetChId() string { +func (c *DigitalChan) GetChId() string { return c.ChId } -func (c *ComtradeDigitalChan) GetPh() string { +func (c *DigitalChan) GetPh() string { return c.Ph } -func (c *ComtradeDigitalChan) GetCcbm() string { +func (c *DigitalChan) GetCcbm() string { return c.Ccbm } -func (c *ComtradeDigitalChan) GetY() uint8 { +func (c *DigitalChan) GetY() uint8 { return c.Y } diff --git a/errors.go b/errors.go index bb94c32..46d05b5 100644 --- a/errors.go +++ b/errors.go @@ -3,7 +3,7 @@ package comtrade import "github.com/pkg/errors" var ( - ErrUnknowTypeOfData = errors.New("unkonw type of data") + ErrUnknownTypeOfData = errors.New("unknown type of data") ErrInvalidFile = errors.New("invalid file") ErrReadFirstLine = errors.New("read first line error") ErrReadSecondLine = errors.New("read second line error") diff --git a/float32_data.go b/float32_data.go index 23984b5..5757ca8 100644 --- a/float32_data.go +++ b/float32_data.go @@ -10,6 +10,6 @@ func init() { type Float32Data struct { } -func (f *Float32Data) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*ComtradeData, error) { - return &ComtradeData{}, nil +func (f *Float32Data) Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) { + return &Data{}, nil } diff --git a/types.go b/types.go index 597a3be..a961e40 100644 --- a/types.go +++ b/types.go @@ -23,5 +23,5 @@ const ( ) type Parser interface { - Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*ComtradeData, error) + Parse(filePath string, analogNum, digitalNum, endSamp uint32) (*Data, error) }