def convert_flac_file(input_filename, output_filename, calibration_filename, correct_phase):
    wavfile = uncompress_and_load(input_filename)
    samples_per_degree = (wavfile.getframerate() / config.MAINS_HZ) / 360
    calibration = scpm.load_calibration_file(filename=calibration_filename,
                                             samples_per_degree=samples_per_degree)
    if calibration is None:
        raise Exception("No calibration data loaded.")

    adc_data = get_adc_data(wavfile)
    time = os.path.split(input_filename)[1]
    time = time.replace('vi-','')
    time = time.replace('_', '.')
    time = float(time)
    while adc_data.data:
        adc_data.sample_width = wavfile.getsampwidth()
        adc_data = scpm.join_frames_and_widen(adc_data)
        split_adc_data = scpm.split_channels(adc_data)
        adc_rms = scpm.calculate_adc_rms(split_adc_data)
        voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)
        if correct_phase:
            voltage, current = scpm.shift_phase(voltage, current, calibration)
        calcd_data = scpm.calculate_calibrated_power(split_adc_data, 
                                                adc_rms, calibration)

        # Dump power data to disk
        with open(output_filename, 'a') as data_file:
            data_file.write('{:.1f} {:.2f} {:.2f} {:.2f}\n'
                            .format(time, calcd_data.real_power,
                                    calcd_data.apparent_power, 
                                    calcd_data.volts_rms))

        adc_data = get_adc_data(wavfile)
        time += len(voltage) / wavfile.getframerate()

    wavfile.close()
예제 #2
0
def main():
    scpm.init_logger()

    sampler = Sampler()
    try:
        sampler.open()
        sampler.start()
        try:
            calibration = scpm.load_calibration_file()
        except:
            calibration = None
        adc_data = sampler.adc_data_queue.get()
    except KeyboardInterrupt:
        sampler.terminate()
    else:
        sampler.terminate()
        adc_data = scpm.join_frames_and_widen(adc_data)
        split_adc_data = scpm.split_channels(adc_data)
        adc_rms = scpm.calculate_adc_rms(split_adc_data)
        if calibration is not None:
            calcd_data = scpm.calculate_calibrated_power(
                split_adc_data, adc_rms, calibration)
            print("")
            scpm.print_power(calcd_data)
        voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)
        voltage, current = scpm.shift_phase(voltage, current, calibration)
        scpm.plot(voltage, current, calibration)
        logging.shutdown()
예제 #3
0
def main():
    scpm.init_logger()
    
    sampler = Sampler()
    try:
        sampler.open()
        sampler.start()
        try:
            calibration = scpm.load_calibration_file()
        except:
            calibration = None
        adc_data = sampler.adc_data_queue.get()
    except KeyboardInterrupt:
        sampler.terminate()
    else:
        sampler.terminate()
        adc_data = scpm.join_frames_and_widen(adc_data)
        split_adc_data = scpm.split_channels(adc_data)
        adc_rms = scpm.calculate_adc_rms(split_adc_data)
        if calibration is not None:
            calcd_data = scpm.calculate_calibrated_power(split_adc_data, 
                                                         adc_rms, calibration)
            print("")
            scpm.print_power(calcd_data)
        voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)
        voltage, current = scpm.shift_phase(voltage, current, calibration)
        scpm.plot(voltage, current, calibration)
        logging.shutdown()
예제 #4
0
def convert_flac_file(input_filename, output_filename, calibration_filename,
                      correct_phase):
    wavfile = uncompress_and_load(input_filename)
    samples_per_degree = (wavfile.getframerate() / config.MAINS_HZ) / 360
    calibration = scpm.load_calibration_file(
        filename=calibration_filename, samples_per_degree=samples_per_degree)
    if calibration is None:
        raise Exception("No calibration data loaded.")

    adc_data = get_adc_data(wavfile)
    time = os.path.split(input_filename)[1]
    time = time.replace('vi-', '')
    time = time.replace('_', '.')
    time = float(time)
    while adc_data.data:
        adc_data.sample_width = wavfile.getsampwidth()
        adc_data = scpm.join_frames_and_widen(adc_data)
        split_adc_data = scpm.split_channels(adc_data)
        adc_rms = scpm.calculate_adc_rms(split_adc_data)
        voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)
        if correct_phase:
            voltage, current = scpm.shift_phase(voltage, current, calibration)
        calcd_data = scpm.calculate_calibrated_power(split_adc_data, adc_rms,
                                                     calibration)

        # Dump power data to disk
        with open(output_filename, 'a') as data_file:
            data_file.write('{:.1f} {:.2f} {:.2f} {:.2f}\n'.format(
                time, calcd_data.real_power, calcd_data.apparent_power,
                calcd_data.volts_rms))

        adc_data = get_adc_data(wavfile)
        time += len(voltage) / wavfile.getframerate()

    wavfile.close()
예제 #5
0
    def start(self):
        calibration = scpm.load_calibration_file()
        self.sampler.open()
        self.sampler.start()
        log.info("Recording power data. Press CTRL+C to stop.")

        # Bind to port
        self.socket.bind("tcp://*:%s" % config.BROADCAST_PORT)

        prev_conv_time = None # the last time sox was run
        while True:
            raw_adc_data = self.sampler.adc_data_queue.get()
            if raw_adc_data is None:
                log.warn("adc_data was None!")
                continue
            
            # Calculate real power, apparent power, v_rms
            adc_data = scpm.join_frames_and_widen(raw_adc_data) 
            split_adc_data = scpm.split_channels(adc_data)
            adc_rms = scpm.calculate_adc_rms(split_adc_data)
            power = scpm.calculate_calibrated_power(split_adc_data, adc_rms, 
                                                    calibration)

            # Dump power data to disk
            with open(config.DAT_FILENAME, 'a') as data_file:
                data_file.write('{:.1f} {:.2f} {:.2f} {:.2f}\n'
                                .format(adc_data.time, power.real_power,
                                        power.apparent_power, power.volts_rms))

            # Broadcast latest values
            self.socket.send_string('{:.1f} {:.2f} {:.2f} {:.2f}'
                                    .format(adc_data.time, power.real_power,
                                            power.apparent_power, power.volts_rms))
            
            # Check if it's time to create a new FLAC file
            t = datetime.datetime.utcfromtimestamp(adc_data.time)
            if self.wavfile is None or t.hour == (prev_conv_time.hour+1)%24:
                self._close_and_compress_wavfile()
                self.wavfile_name = (config.FLAC_FILENAME_PREFIX + 
                                     '{:.6f}'.format(adc_data.time)
                                     .replace('.', '_'))
                self.wavfile = scpm.get_wavfile(self.wavfile_name)
            else:
                # Poll the sox process to see if it has finished.
                # We don't really need to do this but it should
                # prevent the sox process shell from appearing as a 
                # zombie process.
                self._check_sox_return_code_no_wait()
                
            prev_conv_time = t
            
            # Write uncompressed wav data to disk
            self.wavfile.writeframes(b''.join(raw_adc_data.data))
예제 #6
0
    def start(self):
        calibration = scpm.load_calibration_file()
        self.sampler.open()
        self.sampler.start()
        log.info("Recording power data. Press CTRL+C to stop.")

        prev_conv_time = None  # the last time sox was run
        while True:
            raw_adc_data = self.sampler.adc_data_queue.get()
            if raw_adc_data is None:
                log.warn("adc_data was None!")
                continue

            # Calculate real power, apparent power, v_rms
            adc_data = scpm.join_frames_and_widen(raw_adc_data)
            split_adc_data = scpm.split_channels(adc_data)
            adc_rms = scpm.calculate_adc_rms(split_adc_data)
            power = scpm.calculate_calibrated_power(split_adc_data, adc_rms,
                                                    calibration)

            # Dump power data to disk
            with open(config.DAT_FILENAME, 'a') as data_file:
                data_file.write('{:.1f} {:.2f} {:.2f} {:.2f}\n'.format(
                    adc_data.time, power.real_power, power.apparent_power,
                    power.volts_rms))

            # Check if it's time to create a new FLAC file
            t = datetime.datetime.utcfromtimestamp(adc_data.time)
            if self.wavfile is None or t.hour == (prev_conv_time.hour +
                                                  1) % 24:
                self._close_and_compress_wavfile()
                self.wavfile_name = (
                    config.FLAC_FILENAME_PREFIX +
                    '{:.6f}'.format(adc_data.time).replace('.', '_'))
                self.wavfile = scpm.get_wavfile(self.wavfile_name)
            else:
                # Poll the sox process to see if it has finished.
                # We don't really need to do this but it should
                # prevent the sox process shell from appearing as a
                # zombie process.
                self._check_sox_return_code_no_wait()

            prev_conv_time = t

            # Write uncompressed wav data to disk
            self.wavfile.writeframes(b''.join(raw_adc_data.data))
예제 #7
0
def main():
    scpm.init_logger()
    sampler = Sampler()
    wu = WattsUp()
    
    try:
        sampler.open()
        sampler.start()
        wu.open()        
        calibration = scpm.load_calibration_file()
        log.info("Comparing SCPM data with WattsUp data..."
                 " press CTRL+C to stop.")
        while True:
            wu_data = wu.get() # blocking
            if wu_data is None:
                log.warn("wu_data is none!")
                continue
            
            time.sleep(config.RECORD_SECONDS)
            # Now get ADC data recorded at wu_data.time
            adc_data = scpm.find_time(sampler.adc_data_queue, wu_data.time)
            if adc_data:
                print("")
                adc_data = scpm.join_frames_and_widen(adc_data)
                split_adc_data = scpm.split_channels(adc_data)
                adc_rms = scpm.calculate_adc_rms(split_adc_data)
                calcd_data = scpm.calculate_calibrated_power(split_adc_data, 
                                                        adc_rms, calibration)
                
                scpm.print_power(calcd_data, wu_data)
            else:
                log.warn("Could not find ADC data for time {}"
                         .format(wu_data.time))

    except KeyboardInterrupt:
        pass
    except Exception:
        log.exception("")
        wu.terminate()
        sampler.terminate()
        raise
    
    wu.terminate()
    sampler.terminate()
예제 #8
0
def main():
    scpm.init_logger()
    sampler = Sampler()
    wu = WattsUp()

    try:
        sampler.open()
        sampler.start()
        wu.open()
        calibration = scpm.load_calibration_file()
        log.info("Comparing SCPM data with WattsUp data..."
                 " press CTRL+C to stop.")
        while True:
            wu_data = wu.get()  # blocking
            if wu_data is None:
                log.warn("wu_data is none!")
                continue

            time.sleep(config.RECORD_SECONDS)
            # Now get ADC data recorded at wu_data.time
            adc_data = scpm.find_time(sampler.adc_data_queue, wu_data.time)
            if adc_data:
                print("")
                adc_data = scpm.join_frames_and_widen(adc_data)
                split_adc_data = scpm.split_channels(adc_data)
                adc_rms = scpm.calculate_adc_rms(split_adc_data)
                calcd_data = scpm.calculate_calibrated_power(
                    split_adc_data, adc_rms, calibration)

                scpm.print_power(calcd_data, wu_data)
            else:
                log.warn("Could not find ADC data for time {}".format(
                    wu_data.time))

    except KeyboardInterrupt:
        pass
    except Exception:
        log.exception("")
        wu.terminate()
        sampler.terminate()
        raise

    wu.terminate()
    sampler.terminate()
예제 #9
0
def main():
    args = setup_argparser()

    scpm.init_logger()

    wavfile = uncompress_and_load(args.input_file)
    samples_per_degree = (wavfile.getframerate() / config.MAINS_HZ) / 360
    calibration = scpm.load_calibration_file(
        filename=args.calibration_file, samples_per_degree=samples_per_degree)
    adc_data = get_adc_data(wavfile)
    adc_data.sample_width = wavfile.getsampwidth()
    adc_data = scpm.join_frames_and_widen(adc_data)
    split_adc_data = scpm.split_channels(adc_data)
    adc_rms = scpm.calculate_adc_rms(split_adc_data)
    voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)
    print(len(voltage), len(current))

    if calibration is None:
        print("No calibration data loaded.")
    else:
        calcd_data = scpm.calculate_calibrated_power(split_adc_data, adc_rms,
                                                     calibration)

        print("")
        scpm.print_power(calcd_data)

        if args.correct_phase:
            print("Correcting phase by {} degrees = {} samples.".format(
                calibration.phase_diff, calibration.phase_diff_n_samples))
            voltage, current = scpm.shift_phase(voltage, current, calibration)
        else:
            print("Not correcting phase difference"
                  " (use --correct-phase to correct phase)")

    scpm.plot(voltage, current, calibration)

    wavfile.close()
예제 #10
0
def main():
    args = setup_argparser()

    scpm.init_logger()
    
    wavfile = uncompress_and_load(args.input_file)
    samples_per_degree = (wavfile.getframerate() / config.MAINS_HZ) / 360
    calibration = scpm.load_calibration_file(filename=args.calibration_file,
                                             samples_per_degree=samples_per_degree)
    adc_data = get_adc_data(wavfile)
    adc_data.sample_width = wavfile.getsampwidth()
    adc_data = scpm.join_frames_and_widen(adc_data)
    split_adc_data = scpm.split_channels(adc_data)
    adc_rms = scpm.calculate_adc_rms(split_adc_data)
    voltage, current = scpm.convert_adc_to_numpy_float(split_adc_data)    
    print(len(voltage), len(current))

    if calibration is None:
        print("No calibration data loaded.")
    else:
        calcd_data = scpm.calculate_calibrated_power(split_adc_data, 
                                                adc_rms, calibration)
        
        print("")
        scpm.print_power(calcd_data)
    
        if args.correct_phase:
            print("Correcting phase by {} degrees = {} samples."
                  .format(calibration.phase_diff, calibration.phase_diff_n_samples))
            voltage, current = scpm.shift_phase(voltage, current, calibration)
        else:
            print("Not correcting phase difference"
                  " (use --correct-phase to correct phase)")

    scpm.plot(voltage, current, calibration)

    wavfile.close()