def setUp(self): # Sample auto-generated Comtrade file. timebase = 1e+6 # seconds to microseconds timemult = 1 maxtime = 2 self.samples = 10000 sample_freq = maxtime / self.samples # Create temporary cfg file. cfg_contents = COMTRADE_SAMPLE_3_CFG.format(samples=self.samples, seconds=maxtime, format=self.datformat) with open("sample_files/{}.cfg".format(self.filename), 'w') as file: file.write(cfg_contents) # Struct object to write data. datawriter = struct.Struct(self.getFormat()) # Create temporary binary dat file, with one analog and one status # channel. maxtime = 2.0 analog = lambda t: math.cos(2 * math.pi * 60 * t) * 100 status = lambda t: t > maxtime / 2.0 and 1 or 0 with open("sample_files/{}.dat".format(self.filename), 'wb') as file: for isample in range(0, self.samples): t = isample * sample_freq t_us = t * timebase * timemult y_analog = self.parseAnalog(analog(t)) y_status = status(t) file.write( datawriter.pack(isample + 1, t_us, y_analog, y_status)) # Load file. # start = time.time() self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("sample_files/{}.cfg".format(self.filename))
class TestCfg2Reading(TestCffReading): """CFG and DAT 2013 file pair test case (same content as the CFF test). """ def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("sample_files/sample_ascii.cfg") def test_hdr(self): self.assertIsNone(self.comtrade.hdr) def test_inf(self): self.assertIsNone(self.comtrade.inf)
class TestCfgAsciiEncodingReading(TestCffReading): """CFG and DAT 2013 file pair test case (same content as the CFF test), but this time with the file using ASCII text encoding. """ def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("tests/sample_files/sample_ascii_utf-8.cfg", "tests/sample_files/sample_ascii.dat") def test_hdr(self): self.assertIsNone(self.comtrade.hdr) def test_inf(self): self.assertIsNone(self.comtrade.inf) def test_station(self): self.assertEqual(self.comtrade.station_name, "SMARTSTATION testing text encoding: hgvcj터파크387")
class TestCfg1Reading(unittest.TestCase): """String CFG and DAT 1999 pair test case.""" def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.read(COMTRADE_SAMPLE_1_CFG, COMTRADE_SAMPLE_1_DAT) def test_station(self): self.assertEqual(self.comtrade.station_name, "STATION_NAME") def test_rec_dev_id(self): self.assertEqual(self.comtrade.rec_dev_id, "EQUIPMENT") def test_rev_year(self): self.assertEqual(self.comtrade.rev_year, "2001") def test_1a(self): self.assertEqual(self.comtrade.analog_count, 1) def test_1d(self): self.assertEqual(self.comtrade.status_count, 1) def test_2c(self): self.assertEqual(self.comtrade.channels_count, 2) def test_frequency(self): self.assertEqual(float(self.comtrade.frequency), 60.0) def test_total_samples(self): self.assertEqual(self.comtrade.total_samples, 2) def test_timestamp(self): self.assertEqual(self.comtrade.start_timestamp, dt.datetime(2000, 1, 1, 10, 30, 0, 228000, None)) self.assertEqual(self.comtrade.trigger_timestamp, dt.datetime(2000, 1, 1, 10, 30, 0, 722000, None)) def test_time_base(self): self.assertEqual(self.comtrade.time_base, self.comtrade.cfg.TIME_BASE_MICROSEC) def test_ft(self): self.assertEqual(self.comtrade.ft, "ASCII")
class TestCfg1LazyReading(unittest.TestCase): """String CFG and DAT 1999 pair test case, abusing missing values in CFG.""" def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.read(COMTRADE_SAMPLE_1_CFG_LAZY, COMTRADE_SAMPLE_1_DAT) def test_station(self): self.assertEqual(self.comtrade.station_name, "") def test_rec_dev_id(self): self.assertEqual(self.comtrade.rec_dev_id, "") def test_rev_year(self): self.assertEqual(self.comtrade.rev_year, "1999") def test_1a(self): self.assertEqual(self.comtrade.analog_count, 1) def test_1d(self): self.assertEqual(self.comtrade.status_count, 1) def test_2c(self): self.assertEqual(self.comtrade.channels_count, 2) def test_frequency(self): self.assertEqual(float(self.comtrade.frequency), 0.0) def test_total_samples(self): self.assertEqual(self.comtrade.total_samples, 2) def test_timestamp(self): self.assertEqual(self.comtrade.start_timestamp, dt.datetime(1, 1, 1, 0, 0, 0, 0, None)) self.assertEqual(self.comtrade.trigger_timestamp, dt.datetime(1, 1, 1, 0, 0, 0, 0, None)) def test_time_base(self): self.assertEqual(self.comtrade.time_base, self.comtrade.cfg.TIME_BASE_MICROSEC) def test_ft(self): self.assertEqual(self.comtrade.ft, "ASCII")
class TestCffReading(unittest.TestCase): """CFF 2013 file test case.""" def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("sample_files/sample_ascii.cff") def test_station(self): self.assertEqual(self.comtrade.station_name, "SMARTSTATION") def test_rec_dev_id(self): self.assertEqual(self.comtrade.rec_dev_id, "IED123") def test_rev_year(self): self.assertEqual(self.comtrade.rev_year, "2013") def test_1a(self): self.assertEqual(self.comtrade.analog_count, 4) def test_1d(self): self.assertEqual(self.comtrade.status_count, 4) def test_2c(self): self.assertEqual(self.comtrade.channels_count, 8) def test_frequency(self): self.assertEqual(float(self.comtrade.frequency), 60.0) def test_total_samples(self): self.assertEqual(self.comtrade.total_samples, 40) def test_time_base(self): self.assertEqual(self.comtrade.time_base, self.comtrade.cfg.TIME_BASE_MICROSEC) def test_ft(self): self.assertEqual(self.comtrade.ft, "ASCII") def test_hdr(self): self.assertIsNone(self.comtrade.hdr) def test_inf(self): self.assertIsNone(self.comtrade.inf)
def import_files(path): dir_list = sorted(os.listdir(path)) # lista de señales en comtrade tupfiles = list() # lista de pares de archivos cfg y dat l = np.arange(0, len(dir_list), 2) recorders = list() for i in l: (cfg, dat) = (dir_list[i], dir_list[i + 1]) tupfiles.append((cfg, dat)) record = Comtrade() recorders.append(record) return tupfiles, recorders
def FindShorCircuit(file_path, phases): rec = Comtrade() rec.load(file_path) # normal_i = np.mean(np.array(rec.analog[phases])) normal_i = rec.analog[phases][0] normal_delta_i = 0.0015 time1 = 0 time2 = 0 short_circuit = False for i in range(0, len(rec.analog[phases]) - 2): roll = rec.analog[phases][i:i + 3] roll_type = Extremum(roll) if roll_type == 0 or roll_type == 1: if abs(roll[1]) - normal_i > normal_delta_i: if not short_circuit: short_circuit = True time1 = i else: if short_circuit: time2 = i break return short_circuit, rec.time[time2] - rec.time[time1]
class TestRealBinaryReading(unittest.TestCase): def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load(COMTRADE_SAMPLE_4_CFG_FILE, COMTRADE_SAMPLE_4_DAT_FILE) def test_value_conversion(self): va_4 = -23425 * 0.000361849 self.assertAlmostEqual(va_4, self.comtrade.analog[0][3]) def test_values(self): va = self.comtrade.analog[0][0] vb = self.comtrade.analog[1][0] vc = self.comtrade.analog[2][0] vn = self.comtrade.analog[3][0] # sum of phase-ground voltages is approximately 0 self.assertAlmostEqual(0.0, va + vb + vc + vn, 1) def test_time(self): time_diff = self.comtrade.time[2] - self.comtrade.time[1] sample_rate = self.comtrade.cfg.sample_rates[0][0] self.assertAlmostEqual(1.0 / sample_rate, time_diff)
def detect(path_to_cff, k_stat=1): rec = Comtrade() rec.load(path_to_cff) keys = ['t'] + rec.analog_channel_ids values = [rec.time] + rec.analog d = dict(zip(keys, values)) data = pd.DataFrame(data=d) col_pref = ['Ua', 'Ub', 'Uc', '3U0', 'Ia', 'Ib', 'Ic', '3I0'] col_name = ['t'] col_name += k_stat * col_pref data.columns = col_name data = data.dropna() results = [] for i in range(k_stat): phase = pd.concat( [data.iloc[:, 0], data.iloc[:, 1 + i * 8:9 + i * 8]], axis=1, sort=False) res = DetectPhase.time_1_faza(phase) if res[1] != 0 and res[2] != 0: if rec.digital[79] != 0: res.append(0) else: res.append(1) if res[1] == 0 and res[2] == 0: res = DetectPhase.time_3_faza(phase) if rec.digital[3] != 0: res.append(0) else: res.append(1) if res[1] == 0 and res[2] == 0: res = DetectPhase.time_2_faza(phase) if rec.digital[3] != 0: res.append(0) else: res.append(1) results.append(res) return results
def GetGraphs(way, id, type): rec = Comtrade() rec.load(way + ".cfg", way + ".dat") if (type == "digital"): plotdata = rec.digital[id] plotname = rec.digital_channel_ids[id] else: plotdata = rec.analog[id] plotname = rec.analog_channel_ids[id] cmap = get_cmap(20) # fgr = plt.figure(figsize=(18.8, 2.7)) fgr = plt.figure(figsize=(40, 2.7)) margins = { "left": 0.000, "bottom": 0.090, "right": 0.990, "top": 0.990 } fgr.subplots_adjust(**margins) plt.plot(rec.time, plotdata, color=cmap(id)) plt.legend([plotname]) return fig_to_html(fgr)
def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("sample_files/sample_ascii.cff")
def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.read(COMTRADE_SAMPLE_1_CFG, COMTRADE_SAMPLE_1_DAT)
class TestCfg2Reading(TestCffReading): """CFG and DAT 2013 file pair test case (same content as the CFF test). """ def setUp(self): self.comtrade = Comtrade() self.comtrade.load("sample_files/sample_ascii.cfg")
class TestBinaryReading(unittest.TestCase): datformat = comtrade.TYPE_BINARY filename = "temp_binary" def parseAnalog(self, analog_value): return int(analog_value) def getFormat(self): return 'Lf h H' def setUp(self): # Sample auto-generated Comtrade file. timebase = 1e+6 # seconds to microseconds timemult = 1 maxtime = 2 self.samples = 10000 sample_freq = maxtime / self.samples # Create temporary cfg file. cfg_contents = COMTRADE_SAMPLE_3_CFG.format(samples=self.samples, seconds=maxtime, format=self.datformat) with open("sample_files/{}.cfg".format(self.filename), 'w') as file: file.write(cfg_contents) # Struct object to write data. datawriter = struct.Struct(self.getFormat()) # Create temporary binary dat file, with one analog and one status # channel. maxtime = 2.0 analog = lambda t: math.cos(2 * math.pi * 60 * t) * 100 status = lambda t: t > maxtime / 2.0 and 1 or 0 with open("sample_files/{}.dat".format(self.filename), 'wb') as file: for isample in range(0, self.samples): t = isample * sample_freq t_us = t * timebase * timemult y_analog = self.parseAnalog(analog(t)) y_status = status(t) file.write( datawriter.pack(isample + 1, t_us, y_analog, y_status)) # Load file. # start = time.time() self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("sample_files/{}.cfg".format(self.filename)) # print("In {:.5f}s".format(time.time() - start)) def tearDown(self): # Remove temporary files. os.remove("sample_files/{}.cfg".format(self.filename)) os.remove("sample_files/{}.dat".format(self.filename)) def test_total_samples(self): self.assertEqual(self.comtrade.total_samples, self.samples) self.assertEqual(len(self.comtrade.analog[0]), self.samples) self.assertEqual(len(self.comtrade.status[0]), self.samples) self.assertEqual(len(self.comtrade.time), self.samples) def test_analog_channels(self): self.assertEqual(self.comtrade.analog_count, 1) self.assertEqual(len(self.comtrade.analog), 1) def test_status_channels(self): self.assertEqual(self.comtrade.status_count, 1) self.assertEqual(len(self.comtrade.status), 1) def test_max_analog_value(self): tolerance = 2 self.assertLessEqual(100 - max(self.comtrade.analog[0]), 2) def test_last_status_value(self): self.assertEqual(self.comtrade.status[0][-1], 1) def test_timestamps(self): self.assertEqual(self.comtrade.start_timestamp, dt.datetime(2019, 1, 1, 0, 0, 0, 0, None)) self.assertEqual(self.comtrade.trigger_timestamp, dt.datetime(2019, 1, 1, 0, 0, 2, 0, None)) def test_time_base(self): self.assertEqual(self.comtrade.time_base, self.comtrade.cfg.TIME_BASE_NANOSEC) def test_ft(self): self.assertEqual(self.comtrade.ft, self.datformat)
def setUp(self): self.comtrade = Comtrade() self.comtrade.read(COMTRADE_SAMPLE_1_CFG.splitlines(), COMTRADE_SAMPLE_1_DAT.splitlines())
def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load(COMTRADE_SAMPLE_4_CFG_FILE, COMTRADE_SAMPLE_4_DAT_FILE)
def setUp(self): self.comtrade = Comtrade(ignore_warnings=True) self.comtrade.load("tests/sample_files/sample_ascii_utf-8.cfg", "tests/sample_files/sample_ascii.dat")
def GetGraphInfo(way): rec = Comtrade() rec.load(way + ".cfg", way + ".dat") data = {"analog" : rec.analog_channel_ids[:10], "digital" : rec.digital_channel_ids[:6], "time": format(rec.trigger_time), "name":rec.station_name, "time_base" : rec.time_base} return data
def setUp(self): self.comtrade = Comtrade() self.comtrade.load("sample_files/sample_ascii.cff")
import matplotlib.pyplot as plt from comtrade import Comtrade rec = Comtrade() rec.load(r'B:\COMPTRADE\04JUL205.cfg') print("Trigger time = {}s".format(rec.trigger_time)) plt.figure() plt.plot(rec.time, rec.digital[4]) print(type(rec.digital[4])) plt.legend([rec.digital_channel_ids[0]]) plt.show()
from logging import Logger from injector import Injector import matplotlib.pyplot as plt from comtrade import Comtrade from oscillogram_analyzer.di import DI from oscillogram_analyzer.config import Config di = Injector(modules=[DI()]) logger = di.get(Logger) config = di.get(Config) oscillograms_dir = os.path.join(os.getcwd(), config.oscillograms_store_dir) osc = Comtrade(ignore_warnings=True) osc.load(os.path.join(oscillograms_dir, '02JUL235.cff')) logger.info("Trigger time = {}s".format(osc.trigger_time)) logger.info(osc.station_name) logger.info(osc.rec_dev_id) logger.info(osc.cfg_summary()) logger.info(osc.analog_phases) logger.info(osc.frequency) plt.figure() plt.plot(osc.time, osc.analog[0]) plt.plot(osc.time, osc.analog[1]) plt.plot(osc.time, osc.digital[0]) plt.plot(osc.time, osc.digital[1]) plt.legend([