text:
"""
@package mi.dataset.parser
@file /mi/dataset/parser/velpt_ab_particles.py
@author Chris Goodrich
@brief Particle definitions for the velpt_ab recovered dataset driver
Release notes:

initial release
"""
__author__ = 'Chris Goodrich'
__license__ = 'Apache 2.0'

import struct
import calendar
from mi.core.log import get_logger
log = get_logger()
from mi.core.common import BaseEnum
from mi.core.instrument.data_particle import DataParticle
import ntplib

class VelptAbDataParticleType(BaseEnum):
    VELPT_AB_INSTRUMENT_METADATA_RECOVERED = 'velpt_ab_instrument_metadata_recovered'
    VELPT_AB_INSTRUMENT_RECOVERED = 'velpt_ab_instrument_recovered'
    VELPT_AB_DIAGNOSTICS_METADATA_RECOVERED = 'velpt_ab_diagnostics_metadata_recovered'
    VELPT_AB_DIAGNOSTICS_RECOVERED = 'velpt_ab_diagnostics_recovered'

class VelptAbDataParticleKey(BaseEnum):

    # Particle field enums for extracting from Velocity and Diagnostics Data records
    DATE_TIME_STRING = 'date_time_string'                                        # PD93
    ERROR_CODE = 'error_code'                                                    # PD433
    ANALOG1 = 'analog1'                                                          # PD434
    BATTERY_VOLTAGE_DV = 'battery_voltage_dV'                                    # PD3242
    SOUND_SPEED_DMS = 'sound_speed_dms'                                          # PD3243
    HEADING_DECIDEGREE = 'heading_decidegree'                                    # PD3244
    PITCH_DECIDEGREE = 'pitch_decidegree'                                        # PD3246
    ROLL_DECIDEGREE = 'roll_decidegree'                                          # PD3245
    PRESSURE_MBAR = 'pressure_mbar'                                              # PD3248
    STATUS = 'status'                                                            # PD439
    TEMPERATURE_CENTIDEGREE = 'temperature_centidegree'        # PD3247
    VELOCITY_BEAM1 = 'velocity_beam1'                                            # PD441
    VELOCITY_BEAM2 = 'velocity_beam2'                                            # PD442
    VELOCITY_BEAM3 = 'velocity_beam3'                                            # PD443
    AMPLITUDE_BEAM1 = 'amplitude_beam1'                                          # PD444
    AMPLITUDE_BEAM2 = 'amplitude_beam2'                                          # PD445
    AMPLITUDE_BEAM3 = 'amplitude_beam3'                                          # PD446

    # Particle field enums for extracting from Diagnostics Header records
    RECORDS_TO_FOLLOW = 'records_to_follow'                                      # PD447
    CELL_NUMBER_DIAGNOSTICS = 'cell_number_diagnostics'                          # PD448
    NOISE_AMPLITUDE_BEAM1 = 'noise_amplitude_beam1'                              # PD449
    NOISE_AMPLITUDE_BEAM2 = 'noise_amplitude_beam2'                              # PD450
    NOISE_AMPLITUDE_BEAM3 = 'noise_amplitude_beam3'                              # PD451
    NOISE_AMPLITUDE_BEAM4 = 'noise_amplitude_beam4'                              # PD452
    PROCESSING_MAGNITUDE_BEAM1 = 'processing_magnitude_beam1'                    # PD453
    PROCESSING_MAGNITUDE_BEAM2 = 'processing_magnitude_beam2'                    # PD454
    PROCESSING_MAGNITUDE_BEAM3 = 'processing_magnitude_beam3'                    # PD455
    PROCESSING_MAGNITUDE_BEAM4 = 'processing_magnitude_beam4'                    # PD456
    DISTANCE_BEAM1 = 'distance_beam1'                                            # PD457
    DISTANCE_BEAM2 = 'distance_beam2'                                            # PD458
    DISTANCE_BEAM3 = 'distance_beam3'                                            # PD459
    DISTANCE_BEAM4 = 'distance_beam4'                                            # PD460

    # Particle field enums for extracting from the Hardware Configuration record
    INSTRUMENT_TYPE_SERIAL_NUMBER = 'instrmt_type_serial_number'                 # PD461
    PIC_VERSION = 'pic_version'                                                  # PD465
    HARDWARE_REVISION = 'hardware_revision'                                      # PD466
    RECORDER_SIZE = 'recorder_size'                                              # PD467
    VELOCITY_RANGE = 'velocity_range'                                            # PD468
    FIRMWARE_VERSION = 'firmware_version'                                        # PD113

    # Particle field enums for extracting from the Head Configuration record
    PRESSURE_SENSOR = 'pressure_sensor'                                          # PD470
    MAGNETOMETER = 'magnetometer_sensor'                                         # PD471
    TILT_SENSOR = 'tilt_sensor'                                                  # PD472
    TILT_SENSOR_MOUNTING = 'tilt_sensor_mounting'                                # PD473
    HEAD_FREQUENCY = 'head_frequency'                                            # PD474
    HEAD_TYPE = 'head_type'                                                      # PD475
    HEAD_SERIAL_NUMBER = 'head_serial_number'                                    # PD476
    NUMBER_OF_BEAMS = 'number_beams'                                             # PD478

    # Particle field enums for extracting from the User Configuration record
    TRANSMIT_PULSE_LENGTH = 'transmit_pulse_length'                              # PD479
    BLANKING_DISTANCE = 'blanking_distance'                                      # PD480
    RECEIVE_LENGTH = 'receive_length'                                            # PD481
    TIME_BETWEEN_PINGS = 'time_between_pings'                                    # PD482
    TIME_BETWEEN_BURSTS = 'time_between_bursts'                                  # PD483
    NUMBER_OF_BEAM_SEQUENCES = 'number_of_beam_sequences'                        # PD2931
    AVERAGE_INTERVAL = 'average_interval'                                        # PD485
    COMPASS_UPDATE_RATE = 'compass_update_rate'                                  # PD496
    COORDINATE_SYSTEM = 'coordinate_system'                                      # PD497
    NUMBER_CELLS = 'number_cells'                                                # PD498
    MEASUREMENT_INTERVAL = 'measurement_interval'                                # PD500
    DEPLOYMENT_NAME = 'deployment_name'                                          # PD501
    DIAGNOSTICS_INTERVAL = 'diagnostics_interval'                                # PD504
    USE_SPECIFIED_SOUND_SPEED = 'use_specified_sound_speed'                      # PD505
    DIAGNOSTICS_MODE_ENABLE = 'diagnostics_mode_enable'                          # PD506
    ANALOG_OUTPUT_ENABLE = 'analog_output_enable'                                # PD507
    OUTPUT_FORMAT_NORTEK = 'output_format_nortek'                                # PD508
    SCALING = 'scaling'                                                          # PD509
    SERIAL_OUTPUT_ENABLE = 'serial_output_enable'                                # PD510
    RESERVED_BIT_EASYQ = 'reserved_bit_easyq'                                    # PD2933
    STAGE_ENABLE = 'stage_enable'                                                # PD511
    ANALOG_POWER_OUTPUT = 'analog_power_output'                                  # PD512
    SOUND_SPEED_ADJUST_FACTOR = 'sound_speed_adjust_factor'                      # PD513
    NUMBER_DIAGNOSTIC_SAMPLES = 'number_diagnostics_samples'                      # PD514
    NUMBER_OF_BEAMS_IN_DIAGNOSTICS_MODE = 'number_of_beams_in_diagnostics_mode'  # PD2932
    NUMBER_PINGS_DIAGNOSTIC = 'number_pings_diagnostic'                          # PD516
    SOFTWARE_VERSION = 'software_version'                                        # PD520
    CORRELATION_THRESHOLD = 'correlation_threshold'                              # PD533

class VelptAbDataParticle(DataParticle):
    """
    Class for creating the metadata & data particles for velpt_ab

    Note that data in the velpt_ab instrument is little endian and
    the bits within a word are numbered right to left.
    (See the System Integrator Manual, page 11.)
    """
    # Offsets for date-time group in velocity and diagnostics data records
    minute_offset = 4
    second_offset = 5
    day_offset = 6
    hour_offset = 7
    year_offset = 8
    month_offset = 9

    # Offsets for Velocity and Diagnostics Data records
    error_code_offset = 10
    analog1_offset = 12
    battery_voltage_offset = 14
    sound_speed_analog2_offset = 16
    heading_offset = 18
    pitch_offset = 20
    roll_offset = 22
    pressure_msb_offset = 24
    status_offset = 25
    pressure_lsw_offset = 26
    temperature_offset = 28
    velocity_beam1_offset = 30
    velocity_beam2_offset = 32
    velocity_beam3_offset = 34
    amplitude_beam1_offset = 36
    amplitude_beam2_offset = 37
    amplitude_beam3_offset = 38

    # Offsets for Diagnostics Header records
    records_to_follow_offset = 4
    cell_number_diagnostics_offset = 6
    noise_amplitude_beam1_offset = 8
    noise_amplitude_beam2_offset = 9
    noise_amplitude_beam3_offset = 10
    noise_amplitude_beam4_offset = 11
    processing_magnitude_beam1_offset = 12
    processing_magnitude_beam2_offset = 14
    processing_magnitude_beam3_offset = 16
    processing_magnitude_beam4_offset = 18
    distance_beam1_offset = 20
    distance_beam2_offset = 22
    distance_beam3_offset = 24
    distance_beam4_offset = 26

    # Offsets for the Hardware Configuration record
    instrument_type_serial_number_offset = 4
    end_instrument_serial_number_offset = 18
    pic_version_offset = 22
    hardware_revision_offset = 24
    recorder_size_offset = 26
    velocity_range_offset = 28
    end_velocity_range_offset = 30
    firmware_version_offset = 42       # 4 bytes

    # Bit masks for the velocity range word
    velocity_range_mask = 0x0001       # Bit 0

    # Offsets for the Head Configuration record
    config_offset = 4
    head_frequency_offset = 6
    head_type_offset = 8
    head_serial_number_offset = 10
    end_head_serial_number_offset = 22
    number_of_beams_offset = 220        # 2 bytes

    # Bit masks for the config word
    pressure_sensor_mask = 0x0001       # Bit 0
    magnetometer_mask = 0x0002          # Bit 1
    tilt_sensor_mask = 0x0004           # Bit 2
    tilt_sensor_mounting_mask = 0x0008  # Bit 3

    # Offsets for the User Configuration record
    transmit_pulse_length_offset = 4
    blanking_distance_offset = 6
    receive_length_offset = 8
    time_between_pings_offset = 10
    time_between_bursts_offset = 12
    number_of_beam_sequences_offset = 14
    average_interval_offset = 16
    end_average_interval_offset = 18
    compass_update_rate_offset = 30
    coordinate_system_offset = 32
    number_cells_offset = 34
    end_number_cells_offset = 36
    measurement_interval_offset = 38
    deployment_name_offset = 40
    end_deployment_name_offset = 46
    diagnostics_interval_offset = 54
    mode_offset = 58
    sound_speed_adjust_factor_offset = 60
    number_diagnostic_samples_offset = 62
    number_of_beams_in_diagnostics_mode_offset = 64
    number_pings_diagnostic_offset = 66
    end_number_pings_diagnostic_offset = 68
    software_version_offset = 72
    end_software_version_offset = 74
    correlation_threshold_offset = 458      # 2 bytes

    # Bit masks for the mode word
    use_specified_sound_speed_mask = 0x0001  # Bit 0
    diagnostics_mode_enable_mask = 0x0002    # Bit 1
    analog_output_enable_mask = 0x0004       # Bit 2
    output_format_nortek_mask = 0x0008       # Bit 3
    scaling_mask = 0x0010                    # Bit 4
    serial_output_enable_mask = 0x0020       # Bit 5
    reserved_bit_easyq_mask = 0x0040         # Bit 6
    stage_enable_mask = 0x0080               # Bit 7
    analog_power_output_mask = 0x0100        # Bit 8

    @staticmethod
    def _rstrip_non_ascii(in_string):
        return ''.join(c for c in in_string if 0 < ord(c) < 127)

    @staticmethod
    def _convert_bcd_to_decimal(in_val):
        """
        Converts Binary Coded Decimal to a decimal value
        :param in_val: The value to convert
        :return: The decimal value
        """
        tens = (struct.unpack('B', in_val)[0]) >> 4
        actual = struct.unpack('B', in_val)[0]
        low_byte = tens << 4
        return (tens*10) + (actual-low_byte)

    @staticmethod
    def _convert_bcd_to_string(in_val):
        """
        Converts Binary Coded Decimal to a string
        :param in_val: The value to convert
        :return: The string value
        """
        tens = (struct.unpack('B', in_val)[0]) >> 4
        part1 = struct.pack('B', tens+48)
        actual = struct.unpack('B', in_val)[0]
        low_byte = tens << 4
        part2 = struct.pack('B', (actual-low_byte)+48)
        return part1 + part2

    @staticmethod
    def get_date_time_string(record):
        """
        Convert the date and time from the record to the standard string YYYY/MM/DD HH:MM:SS
        :param record: The record read from the file which contains the date and time
        :return: The date time string
        """
        year = '20' + VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.year_offset])
        month = VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.month_offset])
        day = VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.day_offset])
        hour = VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.hour_offset])
        minute = VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.minute_offset])
        second = VelptAbDataParticle._convert_bcd_to_string(record[VelptAbDataParticle.second_offset])
        return year+'/'+month+'/'+day+' '+hour+':'+minute+':'+second

    @staticmethod
    def get_timestamp(record):
        """
        Convert the date and time from the record to a Unix timestamp
        :param record: The record read from the file which contains the date and time
        :return: the Unix timestamp
        """
        year = 2000 + VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.year_offset])
        month = VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.month_offset])
        day = VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.day_offset])
        hour = VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.hour_offset])
        minute = VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.minute_offset])
        second = VelptAbDataParticle._convert_bcd_to_decimal(record[VelptAbDataParticle.second_offset])
        timestamp = (year, month, day, hour, minute, second, 0, 0, 0)
        elapsed_seconds = calendar.timegm(timestamp)

        return float(ntplib.system_to_ntp_time(elapsed_seconds))

    @staticmethod
    def get_diagnostics_count(record):
        """
        Read the expected number of diagnostics records to follow the header
        :param record: The record read from the file which contains the date and time
        :return: The number of expected diagnostics records.
        """
        return struct.unpack('<h', record[VelptAbDataParticle.records_to_follow_offset:
                                          VelptAbDataParticle.cell_number_diagnostics_offset])[0]

    @staticmethod
    def generate_data_dict(record):
        """
        Pull the needed fields from the data file and convert them
        to the format needed for the particle per the IDD. Then put
        that data in a dictionary
        :param record: The record read from the file which contains the date and time
        :return: The dictionary
        """

        date_time_string = VelptAbDataParticle.get_date_time_string(record)

        error_code = struct.unpack_from('<H', record, VelptAbDataParticle.error_code_offset)[0]
        analog_1 = struct.unpack_from('<H', record, VelptAbDataParticle.analog1_offset)[0]
        battery_voltage = struct.unpack_from('<H', record, VelptAbDataParticle.battery_voltage_offset)[0]
        sound_speed_analog_2 = struct.unpack_from('<H', record, VelptAbDataParticle.sound_speed_analog2_offset)[0]
        heading = struct.unpack_from('<h', record, VelptAbDataParticle.heading_offset)[0]
        pitch = struct.unpack_from('<h', record, VelptAbDataParticle.pitch_offset)[0]
        roll = struct.unpack_from('<h', record, VelptAbDataParticle.roll_offset)[0]

        pressure_mbar = (struct.unpack_from('B', record, VelptAbDataParticle.pressure_msb_offset)[0] << 16) + \
                        (struct.unpack_from('<H', record, VelptAbDataParticle.pressure_lsw_offset)[0])

        status = struct.unpack_from('B', record, VelptAbDataParticle.status_offset)[0]
        temperature = struct.unpack_from('<h', record, VelptAbDataParticle.temperature_offset)[0]
        velocity_beam_1 = struct.unpack_from('<h', record, VelptAbDataParticle.velocity_beam1_offset)[0]
        velocity_beam_2 = struct.unpack_from('<h', record, VelptAbDataParticle.velocity_beam2_offset)[0]
        velocity_beam_3 = struct.unpack_from('<h', record, VelptAbDataParticle.velocity_beam3_offset)[0]
        amplitude_beam_1 = struct.unpack_from('B', record, VelptAbDataParticle.amplitude_beam1_offset)[0]
        amplitude_beam_2 = struct.unpack_from('B', record, VelptAbDataParticle.amplitude_beam2_offset)[0]
        amplitude_beam_3 = struct.unpack_from('B', record, VelptAbDataParticle.amplitude_beam3_offset)[0]

        return {VelptAbDataParticleKey.DATE_TIME_STRING: date_time_string,
                VelptAbDataParticleKey.ERROR_CODE: error_code,
                VelptAbDataParticleKey.ANALOG1: analog_1,
                VelptAbDataParticleKey.BATTERY_VOLTAGE_DV: battery_voltage,
                VelptAbDataParticleKey.SOUND_SPEED_DMS: sound_speed_analog_2,
                VelptAbDataParticleKey.HEADING_DECIDEGREE: heading,
                VelptAbDataParticleKey.PITCH_DECIDEGREE: pitch,
                VelptAbDataParticleKey.ROLL_DECIDEGREE: roll,
                VelptAbDataParticleKey.PRESSURE_MBAR: pressure_mbar,
                VelptAbDataParticleKey.STATUS: status,
                VelptAbDataParticleKey.TEMPERATURE_CENTIDEGREE: temperature,
                VelptAbDataParticleKey.VELOCITY_BEAM1: velocity_beam_1,
                VelptAbDataParticleKey.VELOCITY_BEAM2: velocity_beam_2,
                VelptAbDataParticleKey.VELOCITY_BEAM3: velocity_beam_3,
                VelptAbDataParticleKey.AMPLITUDE_BEAM1: amplitude_beam_1,
                VelptAbDataParticleKey.AMPLITUDE_BEAM2: amplitude_beam_2,
                VelptAbDataParticleKey.AMPLITUDE_BEAM3: amplitude_beam_3}

    @staticmethod
    def generate_diagnostics_header_dict(date_time_string, record):
        """
        Pull the needed fields from the data file and convert them
        to the format needed for the particle per the IDD. Then put
        that data in a dictionary
        :param record: The record read from the file which contains the date and time
        :return: The dictionary
        """
        records_to_follow = struct.unpack_from('<H', record, VelptAbDataParticle.records_to_follow_offset)[0]
        cell_number_diagnostics = struct.unpack_from('<H', record,
                                                     VelptAbDataParticle.cell_number_diagnostics_offset)[0]
        noise_amplitude_beam1 = struct.unpack_from('B', record, VelptAbDataParticle.noise_amplitude_beam1_offset)[0]
        noise_amplitude_beam2 = struct.unpack_from('B', record, VelptAbDataParticle.noise_amplitude_beam2_offset)[0]
        noise_amplitude_beam3 = struct.unpack_from('B', record, VelptAbDataParticle.noise_amplitude_beam3_offset)[0]
        noise_amplitude_beam4 = struct.unpack_from('B', record, VelptAbDataParticle.noise_amplitude_beam4_offset)[0]
        processing_magnitude_beam1 = struct.unpack_from('<H', record,
                                                        VelptAbDataParticle.processing_magnitude_beam1_offset)[0]
        processing_magnitude_beam2 = struct.unpack_from('<H', record,
                                                        VelptAbDataParticle.processing_magnitude_beam2_offset)[0]
        processing_magnitude_beam3 = struct.unpack_from('<H', record,
                                                        VelptAbDataParticle.processing_magnitude_beam3_offset)[0]
        processing_magnitude_beam4 = struct.unpack_from('<H', record,
                                                        VelptAbDataParticle.processing_magnitude_beam4_offset)[0]
        distance_beam1 = struct.unpack_from('<H', record, VelptAbDataParticle.distance_beam1_offset)[0]
        distance_beam2 = struct.unpack_from('<H', record, VelptAbDataParticle.distance_beam2_offset)[0]
        distance_beam3 = struct.unpack_from('<H', record, VelptAbDataParticle.distance_beam3_offset)[0]
        distance_beam4 = struct.unpack_from('<H', record, VelptAbDataParticle.distance_beam4_offset)[0]

        return {VelptAbDataParticleKey.DATE_TIME_STRING: date_time_string,
                VelptAbDataParticleKey.RECORDS_TO_FOLLOW: records_to_follow,
                VelptAbDataParticleKey.CELL_NUMBER_DIAGNOSTICS: cell_number_diagnostics,
                VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM1: noise_amplitude_beam1,
                VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM2: noise_amplitude_beam2,
                VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM3: noise_amplitude_beam3,
                VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM4: noise_amplitude_beam4,
                VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM1: processing_magnitude_beam1,
                VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM2: processing_magnitude_beam2,
                VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM3: processing_magnitude_beam3,
                VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM4: processing_magnitude_beam4,
                VelptAbDataParticleKey.DISTANCE_BEAM1: distance_beam1,
                VelptAbDataParticleKey.DISTANCE_BEAM2: distance_beam2,
                VelptAbDataParticleKey.DISTANCE_BEAM3: distance_beam3,
                VelptAbDataParticleKey.DISTANCE_BEAM4: distance_beam4}

    @staticmethod
    def generate_empty_hardware_config_dict():
        """
        In the even that no hardware configuration record was found in the recovered file,
        have a dummy dict set up to act as a placeholder in the particle.
        """
        return {VelptAbDataParticleKey.INSTRUMENT_TYPE_SERIAL_NUMBER: '',
                VelptAbDataParticleKey.PIC_VERSION: 0,
                VelptAbDataParticleKey.HARDWARE_REVISION: 0,
                VelptAbDataParticleKey.RECORDER_SIZE: 0,
                VelptAbDataParticleKey.VELOCITY_RANGE: 0,
                VelptAbDataParticleKey.FIRMWARE_VERSION: ''}

    @staticmethod
    def generate_empty_head_config_dict():
        """
        In the even that no head configuration record was found in the recovered file,
        have a dummy dict set up to act as a placeholder in the particle.
        """
        return {VelptAbDataParticleKey.PRESSURE_SENSOR: 0,
                VelptAbDataParticleKey.MAGNETOMETER: 0,
                VelptAbDataParticleKey.TILT_SENSOR: 0,
                VelptAbDataParticleKey.TILT_SENSOR_MOUNTING: 0,
                VelptAbDataParticleKey.HEAD_FREQUENCY: 0,
                VelptAbDataParticleKey.HEAD_TYPE: 0,
                VelptAbDataParticleKey.HEAD_SERIAL_NUMBER: '',
                VelptAbDataParticleKey.NUMBER_OF_BEAMS: 0}

    @staticmethod
    def generate_empty_user_config_dict():
        """
        In the even that no user configuration record was found in the recovered file,
        have a dummy dict set up to act as a placeholder in the particle.
        """
        return {VelptAbDataParticleKey.TRANSMIT_PULSE_LENGTH: 0,
                VelptAbDataParticleKey.BLANKING_DISTANCE: 0,
                VelptAbDataParticleKey.RECEIVE_LENGTH: 0,
                VelptAbDataParticleKey.TIME_BETWEEN_PINGS: 0,
                VelptAbDataParticleKey.TIME_BETWEEN_BURSTS: 0,
                VelptAbDataParticleKey.NUMBER_OF_BEAM_SEQUENCES: 0,
                VelptAbDataParticleKey.AVERAGE_INTERVAL: 0,
                VelptAbDataParticleKey.COMPASS_UPDATE_RATE: 0,
                VelptAbDataParticleKey.COORDINATE_SYSTEM: 0,
                VelptAbDataParticleKey.NUMBER_CELLS: 0,
                VelptAbDataParticleKey.MEASUREMENT_INTERVAL: 0,
                VelptAbDataParticleKey.DEPLOYMENT_NAME: '',
                VelptAbDataParticleKey.DIAGNOSTICS_INTERVAL: 0,
                VelptAbDataParticleKey.USE_SPECIFIED_SOUND_SPEED: 0,
                VelptAbDataParticleKey.DIAGNOSTICS_MODE_ENABLE: 0,
                VelptAbDataParticleKey.ANALOG_OUTPUT_ENABLE: 0,
                VelptAbDataParticleKey.OUTPUT_FORMAT_NORTEK: 0,
                VelptAbDataParticleKey.SCALING: 0,
                VelptAbDataParticleKey.SERIAL_OUTPUT_ENABLE: 0,
                VelptAbDataParticleKey.RESERVED_BIT_EASYQ: 0,
                VelptAbDataParticleKey.STAGE_ENABLE: 0,
                VelptAbDataParticleKey.ANALOG_POWER_OUTPUT: 0,
                VelptAbDataParticleKey.SOUND_SPEED_ADJUST_FACTOR: 0,
                VelptAbDataParticleKey.NUMBER_DIAGNOSTIC_SAMPLES: 0,
                VelptAbDataParticleKey.NUMBER_OF_BEAMS_IN_DIAGNOSTICS_MODE: 0,
                VelptAbDataParticleKey.NUMBER_PINGS_DIAGNOSTIC: 0,
                VelptAbDataParticleKey.SOFTWARE_VERSION: '',
                VelptAbDataParticleKey.CORRELATION_THRESHOLD: 0}

    @staticmethod
    def generate_hardware_config_dict(record):
        """
        Builds up the metadata dict from the hardware configuration record
        """
        instrument_type_serial_number = struct.unpack_from('14s', record,
                                                           VelptAbDataParticle.instrument_type_serial_number_offset)[0]
        instrument_type_serial_number = instrument_type_serial_number.rstrip()
        instrument_type_serial_number = VelptAbDataParticle._rstrip_non_ascii(instrument_type_serial_number)

        pic_version = struct.unpack_from('<H', record, VelptAbDataParticle.pic_version_offset)[0]
        hardware_revision = struct.unpack_from('<H', record, VelptAbDataParticle.hardware_revision_offset)[0]
        recorder_size = struct.unpack_from('<H', record, VelptAbDataParticle.recorder_size_offset)[0] << 16
        velocity_range = struct.unpack_from('<H', record, VelptAbDataParticle.velocity_range_offset)[0] & \
            VelptAbDataParticle.velocity_range_mask

        firmware_version = struct.unpack_from('4s', record, VelptAbDataParticle.firmware_version_offset)[0]
        firmware_version = firmware_version.rstrip()
        firmware_version = VelptAbDataParticle._rstrip_non_ascii(firmware_version)

        return {VelptAbDataParticleKey.INSTRUMENT_TYPE_SERIAL_NUMBER: instrument_type_serial_number,
                VelptAbDataParticleKey.PIC_VERSION: pic_version,
                VelptAbDataParticleKey.HARDWARE_REVISION: hardware_revision,
                VelptAbDataParticleKey.RECORDER_SIZE: recorder_size,
                VelptAbDataParticleKey.VELOCITY_RANGE: velocity_range,
                VelptAbDataParticleKey.FIRMWARE_VERSION: firmware_version}

    @staticmethod
    def generate_head_config_dict(record):
        """
        Builds up the metadata dict from the head configuration record
        """
        head_config = struct.unpack_from('<H', record, VelptAbDataParticle.config_offset)[0]
        pressure_sensor = head_config & VelptAbDataParticle.pressure_sensor_mask
        magnetometer = (head_config & VelptAbDataParticle.magnetometer_mask) >> 1
        tilt_sensor = (head_config & VelptAbDataParticle.tilt_sensor_mask) >> 2
        tilt_sensor_mounting = (head_config & VelptAbDataParticle.tilt_sensor_mounting_mask) >> 3

        head_frequency = struct.unpack_from('<H', record, VelptAbDataParticle.head_frequency_offset)[0]
        head_type = struct.unpack_from('<H', record, VelptAbDataParticle.head_type_offset)[0]
        head_serial_number = struct.unpack_from('12s', record, VelptAbDataParticle.head_serial_number_offset)[0]
        head_serial_number = head_serial_number.rstrip()
        head_serial_number = VelptAbDataParticle._rstrip_non_ascii(head_serial_number)

        number_of_beams = struct.unpack_from('<H', record, VelptAbDataParticle.number_of_beams_offset)[0]

        return {VelptAbDataParticleKey.PRESSURE_SENSOR: pressure_sensor,
                VelptAbDataParticleKey.MAGNETOMETER: magnetometer,
                VelptAbDataParticleKey.TILT_SENSOR: tilt_sensor,
                VelptAbDataParticleKey.TILT_SENSOR_MOUNTING: tilt_sensor_mounting,
                VelptAbDataParticleKey.HEAD_FREQUENCY: head_frequency,
                VelptAbDataParticleKey.HEAD_TYPE: head_type,
                VelptAbDataParticleKey.HEAD_SERIAL_NUMBER: head_serial_number,
                VelptAbDataParticleKey.NUMBER_OF_BEAMS: number_of_beams}

    @staticmethod
    def generate_user_config_dict(record):
        """
        Builds up the metadata dict from the user configuration record
        """
        transmit_pulse_length = struct.unpack_from('<H', record, VelptAbDataParticle.transmit_pulse_length_offset)[0]
        blanking_distance = struct.unpack_from('<H', record, VelptAbDataParticle.blanking_distance_offset)[0]
        receive_length = struct.unpack_from('<H', record, VelptAbDataParticle.receive_length_offset)[0]
        time_between_pings = struct.unpack_from('<H', record, VelptAbDataParticle.time_between_pings_offset)[0]
        time_between_bursts = struct.unpack_from('<H', record, VelptAbDataParticle.time_between_bursts_offset)[0]
        number_of_beam_sequences = struct.unpack_from('<H', record,
                                                      VelptAbDataParticle.number_of_beam_sequences_offset)[0]
        average_interval = struct.unpack_from('<H', record, VelptAbDataParticle.average_interval_offset)[0]
        compass_update_rate = struct.unpack_from('<H', record, VelptAbDataParticle.compass_update_rate_offset)[0]
        coordinate_system = struct.unpack_from('<H', record, VelptAbDataParticle.coordinate_system_offset)[0]
        number_cells = struct.unpack_from('<H', record, VelptAbDataParticle.number_cells_offset)[0]
        measurement_interval = struct.unpack_from('<H', record, VelptAbDataParticle.measurement_interval_offset)[0]
        deployment_name = struct.unpack_from('6s', record, VelptAbDataParticle.deployment_name_offset)[0]
        deployment_name = deployment_name.rstrip()
        deployment_name = VelptAbDataParticle._rstrip_non_ascii(deployment_name)

        diagnostics_interval = struct.unpack_from('<I', record, VelptAbDataParticle.diagnostics_interval_offset)[0]
        mode = struct.unpack_from('<H', record, VelptAbDataParticle.mode_offset)[0]
        sound_speed_adjust_factor = struct.unpack_from('<H', record,
                                                       VelptAbDataParticle.sound_speed_adjust_factor_offset)[0]
        number_diagnostic_samples = struct.unpack_from('<H', record,
                                                       VelptAbDataParticle.number_diagnostic_samples_offset)[0]
        number_of_beams_in_diagnostics_mode = struct.unpack_from('<H', record,
                                                                 VelptAbDataParticle.
                                                                 number_of_beams_in_diagnostics_mode_offset)[0]
        number_pings_diagnostic = struct.unpack_from('<H', record,
                                                     VelptAbDataParticle.number_pings_diagnostic_offset)[0]
        software_version = struct.unpack_from('2s', record, VelptAbDataParticle.software_version_offset)[0]
        software_version = software_version.rstrip()
        software_version = VelptAbDataParticle._rstrip_non_ascii(software_version)

        correlation_threshold = struct.unpack_from('<H', record, VelptAbDataParticle.correlation_threshold_offset)[0]

        use_specified_sound_speed = (mode & VelptAbDataParticle.use_specified_sound_speed_mask)
        diagnostics_mode_enable = (mode & VelptAbDataParticle.diagnostics_mode_enable_mask) >> 1
        analog_output_enable = (mode & VelptAbDataParticle.analog_output_enable_mask) >> 2
        output_format_nortek = (mode & VelptAbDataParticle.output_format_nortek_mask) >> 3
        scaling = (mode & VelptAbDataParticle.scaling_mask) >> 4
        serial_output_enable = (mode & VelptAbDataParticle.serial_output_enable_mask) >> 5
        reserved_bit_easyq = (mode & VelptAbDataParticle.reserved_bit_easyq_mask) >> 6
        stage_enable = (mode & VelptAbDataParticle.stage_enable_mask) >> 7
        analog_power_output = (mode & VelptAbDataParticle.analog_power_output_mask) >> 8

        return {VelptAbDataParticleKey.TRANSMIT_PULSE_LENGTH: transmit_pulse_length,
                VelptAbDataParticleKey.BLANKING_DISTANCE: blanking_distance,
                VelptAbDataParticleKey.RECEIVE_LENGTH: receive_length,
                VelptAbDataParticleKey.TIME_BETWEEN_PINGS: time_between_pings,
                VelptAbDataParticleKey.TIME_BETWEEN_BURSTS: time_between_bursts,
                VelptAbDataParticleKey.NUMBER_OF_BEAM_SEQUENCES: number_of_beam_sequences,
                VelptAbDataParticleKey.AVERAGE_INTERVAL: average_interval,
                VelptAbDataParticleKey.COMPASS_UPDATE_RATE: compass_update_rate,
                VelptAbDataParticleKey.COORDINATE_SYSTEM: coordinate_system,
                VelptAbDataParticleKey.NUMBER_CELLS: number_cells,
                VelptAbDataParticleKey.MEASUREMENT_INTERVAL: measurement_interval,
                VelptAbDataParticleKey.DEPLOYMENT_NAME: deployment_name,
                VelptAbDataParticleKey.DIAGNOSTICS_INTERVAL: diagnostics_interval,
                VelptAbDataParticleKey.USE_SPECIFIED_SOUND_SPEED: use_specified_sound_speed,
                VelptAbDataParticleKey.DIAGNOSTICS_MODE_ENABLE: diagnostics_mode_enable,
                VelptAbDataParticleKey.ANALOG_OUTPUT_ENABLE: analog_output_enable,
                VelptAbDataParticleKey.OUTPUT_FORMAT_NORTEK: output_format_nortek,
                VelptAbDataParticleKey.SCALING: scaling,
                VelptAbDataParticleKey.SERIAL_OUTPUT_ENABLE: serial_output_enable,
                VelptAbDataParticleKey.RESERVED_BIT_EASYQ: reserved_bit_easyq,
                VelptAbDataParticleKey.STAGE_ENABLE: stage_enable,
                VelptAbDataParticleKey.ANALOG_POWER_OUTPUT: analog_power_output,
                VelptAbDataParticleKey.SOUND_SPEED_ADJUST_FACTOR: sound_speed_adjust_factor,
                VelptAbDataParticleKey.NUMBER_DIAGNOSTIC_SAMPLES: number_diagnostic_samples,
                VelptAbDataParticleKey.NUMBER_OF_BEAMS_IN_DIAGNOSTICS_MODE: number_of_beams_in_diagnostics_mode,
                VelptAbDataParticleKey.NUMBER_PINGS_DIAGNOSTIC: number_pings_diagnostic,
                VelptAbDataParticleKey.SOFTWARE_VERSION: software_version,
                VelptAbDataParticleKey.CORRELATION_THRESHOLD: correlation_threshold}

    @staticmethod
    def generate_instrument_metadata_dict(date_time_group,
                                          hardware_config_dict,
                                          head_config_dict,
                                          user_config_dict):
        """
        Builds the metadata dict from the hardware, head and user configuration records.
        Also adds in the date time string extracted from the first instrument record.
        """
        metadata_dict = {VelptAbDataParticleKey.DATE_TIME_STRING: date_time_group}
        metadata_dict.update(hardware_config_dict)
        metadata_dict.update(head_config_dict)
        metadata_dict.update(user_config_dict)

        return metadata_dict

class VelptAbDiagnosticsHeaderParticle(VelptAbDataParticle):
    """
    See the IDD
    """
    _data_particle_type = VelptAbDataParticleType.VELPT_AB_DIAGNOSTICS_METADATA_RECOVERED

    def _build_parsed_values(self):

        particle_parameters = []

        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DATE_TIME_STRING,
                                                      self.raw_data[VelptAbDataParticleKey.DATE_TIME_STRING], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.RECORDS_TO_FOLLOW,
                                                      self.raw_data[VelptAbDataParticleKey.RECORDS_TO_FOLLOW], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.CELL_NUMBER_DIAGNOSTICS,
                                                      self.raw_data[VelptAbDataParticleKey.CELL_NUMBER_DIAGNOSTICS],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM3], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM4,
                                                      self.raw_data[VelptAbDataParticleKey.NOISE_AMPLITUDE_BEAM4], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM1],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM2],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM3],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM4,
                                                      self.raw_data[VelptAbDataParticleKey.PROCESSING_MAGNITUDE_BEAM4],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DISTANCE_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.DISTANCE_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DISTANCE_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.DISTANCE_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DISTANCE_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.DISTANCE_BEAM3], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DISTANCE_BEAM4,
                                                      self.raw_data[VelptAbDataParticleKey.DISTANCE_BEAM4], int))

        return particle_parameters

class VelptAbDiagnosticsDataParticle(VelptAbDataParticle):
    """
    See the IDD
    """
    _data_particle_type = VelptAbDataParticleType.VELPT_AB_DIAGNOSTICS_RECOVERED

    def _build_parsed_values(self):

        particle_parameters = []

        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DATE_TIME_STRING,
                                                      self.raw_data[VelptAbDataParticleKey.DATE_TIME_STRING], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ERROR_CODE,
                                                      self.raw_data[VelptAbDataParticleKey.ERROR_CODE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ANALOG1,
                                                      self.raw_data[VelptAbDataParticleKey.ANALOG1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.BATTERY_VOLTAGE_DV,
                                                      self.raw_data[VelptAbDataParticleKey.BATTERY_VOLTAGE_DV], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SOUND_SPEED_DMS,
                                                      self.raw_data[VelptAbDataParticleKey.SOUND_SPEED_DMS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HEADING_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.HEADING_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PITCH_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.PITCH_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ROLL_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.ROLL_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PRESSURE_MBAR,
                                                      self.raw_data[VelptAbDataParticleKey.PRESSURE_MBAR], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.STATUS,
                                                      self.raw_data[VelptAbDataParticleKey.STATUS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TEMPERATURE_CENTIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.TEMPERATURE_CENTIDEGREE],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM3], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM3], int))

        return particle_parameters

class VelptAbInstrumentMetadataParticle(VelptAbDataParticle):
    """
    See the IDD
    """
    _data_particle_type = VelptAbDataParticleType.VELPT_AB_INSTRUMENT_METADATA_RECOVERED

    def _build_parsed_values(self):

        particle_parameters = []

        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DATE_TIME_STRING,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.DATE_TIME_STRING], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.INSTRUMENT_TYPE_SERIAL_NUMBER,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.INSTRUMENT_TYPE_SERIAL_NUMBER], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PIC_VERSION,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.PIC_VERSION], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HARDWARE_REVISION,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.HARDWARE_REVISION], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.RECORDER_SIZE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.RECORDER_SIZE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_RANGE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.VELOCITY_RANGE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.FIRMWARE_VERSION,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.FIRMWARE_VERSION], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PRESSURE_SENSOR,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.PRESSURE_SENSOR], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.MAGNETOMETER,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.MAGNETOMETER], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TILT_SENSOR,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.TILT_SENSOR], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TILT_SENSOR_MOUNTING,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.TILT_SENSOR_MOUNTING], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HEAD_FREQUENCY,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.HEAD_FREQUENCY], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HEAD_TYPE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.HEAD_TYPE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HEAD_SERIAL_NUMBER,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.HEAD_SERIAL_NUMBER], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_OF_BEAMS,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_OF_BEAMS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TRANSMIT_PULSE_LENGTH,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.TRANSMIT_PULSE_LENGTH], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.BLANKING_DISTANCE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.BLANKING_DISTANCE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.RECEIVE_LENGTH,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.RECEIVE_LENGTH], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TIME_BETWEEN_PINGS,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.TIME_BETWEEN_PINGS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TIME_BETWEEN_BURSTS,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.TIME_BETWEEN_BURSTS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_OF_BEAM_SEQUENCES,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_OF_BEAM_SEQUENCES], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AVERAGE_INTERVAL,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.AVERAGE_INTERVAL], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.COMPASS_UPDATE_RATE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.COMPASS_UPDATE_RATE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.COORDINATE_SYSTEM,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.COORDINATE_SYSTEM], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_CELLS,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_CELLS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.MEASUREMENT_INTERVAL,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.MEASUREMENT_INTERVAL], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DEPLOYMENT_NAME,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.DEPLOYMENT_NAME], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DIAGNOSTICS_INTERVAL,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.DIAGNOSTICS_INTERVAL], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.USE_SPECIFIED_SOUND_SPEED,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.USE_SPECIFIED_SOUND_SPEED], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DIAGNOSTICS_MODE_ENABLE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.DIAGNOSTICS_MODE_ENABLE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ANALOG_OUTPUT_ENABLE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.ANALOG_OUTPUT_ENABLE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.OUTPUT_FORMAT_NORTEK,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.OUTPUT_FORMAT_NORTEK], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SCALING,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.SCALING], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SERIAL_OUTPUT_ENABLE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.SERIAL_OUTPUT_ENABLE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.RESERVED_BIT_EASYQ,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.RESERVED_BIT_EASYQ], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.STAGE_ENABLE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.STAGE_ENABLE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ANALOG_POWER_OUTPUT,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.ANALOG_POWER_OUTPUT], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SOUND_SPEED_ADJUST_FACTOR,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.SOUND_SPEED_ADJUST_FACTOR], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_DIAGNOSTIC_SAMPLES,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_DIAGNOSTIC_SAMPLES], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_OF_BEAMS_IN_DIAGNOSTICS_MODE,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_OF_BEAMS_IN_DIAGNOSTICS_MODE],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.NUMBER_PINGS_DIAGNOSTIC,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.NUMBER_PINGS_DIAGNOSTIC], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SOFTWARE_VERSION,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.SOFTWARE_VERSION], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.CORRELATION_THRESHOLD,
                                                      self.raw_data[
                                                          VelptAbDataParticleKey.CORRELATION_THRESHOLD], int))

        return particle_parameters

class VelptAbInstrumentDataParticle(VelptAbDataParticle):
    """
    See the IDD
    """
    _data_particle_type = VelptAbDataParticleType.VELPT_AB_INSTRUMENT_RECOVERED

    def _build_parsed_values(self):

        particle_parameters = []

        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.DATE_TIME_STRING,
                                                      self.raw_data[VelptAbDataParticleKey.DATE_TIME_STRING], str))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ERROR_CODE,
                                                      self.raw_data[VelptAbDataParticleKey.ERROR_CODE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ANALOG1,
                                                      self.raw_data[VelptAbDataParticleKey.ANALOG1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.BATTERY_VOLTAGE_DV,
                                                      self.raw_data[VelptAbDataParticleKey.BATTERY_VOLTAGE_DV], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.SOUND_SPEED_DMS,
                                                      self.raw_data[VelptAbDataParticleKey.SOUND_SPEED_DMS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.HEADING_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.HEADING_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PITCH_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.PITCH_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.ROLL_DECIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.ROLL_DECIDEGREE], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.PRESSURE_MBAR,
                                                      self.raw_data[VelptAbDataParticleKey.PRESSURE_MBAR], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.STATUS,
                                                      self.raw_data[VelptAbDataParticleKey.STATUS], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.TEMPERATURE_CENTIDEGREE,
                                                      self.raw_data[VelptAbDataParticleKey.TEMPERATURE_CENTIDEGREE],
                                                      int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.VELOCITY_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.VELOCITY_BEAM3], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM1,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM1], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM2,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM2], int))
        particle_parameters.append(self._encode_value(VelptAbDataParticleKey.AMPLITUDE_BEAM3,
                                                      self.raw_data[VelptAbDataParticleKey.AMPLITUDE_BEAM3], int))

        return particle_parameters


text:
namespace LionFire.Orleans_.Collections;

public interface IListGrain<TValue, TMetadata>
    where TValue : IGrain
{
    Task<bool> Remove(string id);
    Task<(string id, TValue newValue)> Create(Type type);

    Task<IEnumerable<Type>> CreateableTypes();

    Task<IEnumerable<GrainListItem<TValue, TMetadata>>> Items();

}

public interface IEnumerableGrain<TValue>
    where TValue : class
{
    Task<IEnumerable<TValue>> Items();
}

public interface IListGrain<TValue> : IEnumerableGrain<TValue>
    where TValue : class
{
    Task<bool> Remove(string id);
    Task<TValue> Create(Type type);

    Task<IEnumerable<Type>> CreateableTypes();

}

redpajama_set_name: RedPajamaGithub


text:
package com.apigee.utils;

import java.util.logging.ConsoleHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class StringUtils {

	private static final Logger LOGGER = Logger.getLogger(StringUtils.class.getName());

	static {
		LOGGER.setLevel(Level.WARNING);
		ConsoleHandler handler = new ConsoleHandler();
		// PUBLISH this level
		handler.setLevel(Level.WARNING);
		LOGGER.addHandler(handler);
	}

	public static String lastWordAfterDot(String word) {
		int index = word.lastIndexOf(".");
		return word.substring(index + 1, word.length());
	}

	public static KeyValue<String, String> proxyNameAndBasePath(String url) {

        final String lowercaseUrl = url.toLowerCase();
        KeyValue<String, String> map;
		int endIndex = lowercaseUrl.indexOf(".wsdl");

		if (endIndex == -1) {
			endIndex = lowercaseUrl.indexOf("?wsdl");
			if (endIndex == -1) {
				LOGGER.warning("Unable to get proxy name from WSDL. Generating default name");
				map = new KeyValue<String,String>("SOAP2REST","/soap2rest");
                return map;
			}
		}

		int beginIndex = lowercaseUrl.lastIndexOf("/");

		map = new KeyValue<String,String>(url.substring(beginIndex+1, endIndex), url.substring(beginIndex, endIndex).toLowerCase());

		return map;
	}
}


text:
package com.luck.csv

import org.apache.spark._
import org.apache.spark.sql.SparkSession

/**
  * Sample application that reads a CSV
  * and display its contents
  */

case class SomeData(id: Integer, timestamp: String)

object CsvApplication {

  def main(args: Array[String]): Unit = {

    println("Starting CSV Application") //scalastyle:ignore

    var sparkConf: SparkConf = new SparkConf()
      .setAppName("Spark-CSV")
      .set("spark.serializer", "org.apache.spark.serializer.KryoSerializer")

    // check Spark configuration for master URL, set it to local if not configured
    if (! sparkConf.contains("spark.master")) {
      println(">>> will set master") //scalastyle:ignore
      sparkConf.setMaster("local[2]")
    }

    val sparkSession: SparkSession = SparkSession.builder
      .config(sparkConf)
      .getOrCreate

    import sparkSession.implicits._

    val df = sparkSession.read
                       .option("header", "true") // Use first line of all files as header
                       .option("inferSchema", "true") // Automatically infer data types
                       .option("delimiter", " ") // define the delimiter to use
                       .csv("/users/lresende/data.csv") // location relative to hdfs root
                       .as[SomeData]  // this will return DataSet[SomeData]
    df.printSchema()

    df.show(50, false)
  }
}


text:
/*
K.N.King "C Programming. A Modern Approach."
Programming project 5 p.71

Rewrite the upc.c program of Section 4.1 so that the user
enters 11 digits at one time, instead of entering one digit,
then five digits, and then another five digits.

"Enter the first 11 digits of a UPC: 01380015173
Check digit: 5
"

*/

#include <stdlib.h>
#include <ctype.h>
#include <stdio.h>

#define UPC_DIGIT_COUNT 11

int UpcComputeCheckDigit(const char *upc, int digitCount);

int main(void)
{
    // note that the upcDigitsArray to store the upc
    // code is NOT intended to be null-terminated
    char upcDigitsArray[UPC_DIGIT_COUNT] = { 0 };
    int isInputValid = 0;
    int checkDigit = -1;

    printf("Please enter the first %d digits of a UPC: ", UPC_DIGIT_COUNT);

    while (!isInputValid)
    {
        isInputValid = 1;

        int i = 0;
        char c = '\0';
        while ((c = getchar()) != '\n' && c != EOF)
        {
            if (isdigit(c) && i < UPC_DIGIT_COUNT) {
                upcDigitsArray[i] = c;
                ++i;
            } else {
                // we cannot break out on the spot
                // since we still need to discard
                // the rest of invalid input
                isInputValid = 0;
            }
        }
        if (i != UPC_DIGIT_COUNT) {
            isInputValid = 0;
        }
        if (!isInputValid) {
            puts("Input invalid. Please try again.");
        }
    }
    checkDigit = UpcComputeCheckDigit(upcDigitsArray, UPC_DIGIT_COUNT);
    printf("Check digit: %d\n", checkDigit);
    getchar();

    return EXIT_SUCCESS;
}

int UpcComputeCheckDigit(const char *upc, int digitCount) {
    if (!upc || digitCount < 1) {
        return -1;
    }
    int sum1 = 0;
    int sum2 = 0;
    int total = 0;

    int i = 0;
    while (1) {
        if (i >= digitCount) break;
        sum1 += upc[i] - '0';
        ++i;

        if (i >= digitCount) break;
        sum2 += upc[i] - '0';
        ++i;
    }
    total = 3 * sum1 + sum2;
    return 9 - ((total - 1) % 10);
}


text:
using System;
using System.ComponentModel;
using System.Globalization;

namespace Log4JDash.Web.Models
{
    public sealed class LogLevelInputConverter : TypeConverter
    {
        /// <inheritdoc />
        public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
        {
            return sourceType == typeof (String) ||
                   base.CanConvertFrom (context, sourceType);
        }

        /// <inheritdoc />
        public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
        {
            return destinationType == typeof (String) ||
                   base.CanConvertTo (context, destinationType);
        }

        /// <inheritdoc />
        public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
        {
            if (value == null)
            {
                return null;
            }

            var s = value as string;
            if (s != null)
            {
                return new LogLevelInput (s);
            }

            return base.ConvertFrom (context, culture, value);
        }

        /// <inheritdoc />
        public override object ConvertTo (ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (value == null)
            {
                return null;
            }

            if (destinationType == typeof (string))
            {
                var logLevelInput = (LogLevelInput) value;
                return logLevelInput.Value;
            }

            return base.ConvertTo (context, culture, value, destinationType);
        }
    }
}


text:
'use strict';

var Future = require('data.future');
var url = require('url');
var _ = require('lodash');

module.exports = exports = function makeRouter(route, handler, parent) {
  var router = function routeHandler(oreq, ores, onext) {
    if (onext == null) {
      onext = function next(req, res) {
        return Future.of(res);
      };
    }

    if (parent == null) {
      if (oreq.path == null || oreq.query == null) {
        var data = url.parse(oreq.url, true);

        oreq.path = data.pathname;
        oreq.query = data.query;
      }

      return onext(oreq, ores);
    }

    return parent(oreq, ores, function next(req, res) {
      var path = req.path;

      // skip if route doesn't match
      if (path.toLowerCase().substr(0, route.length) !== route.toLowerCase()) {
        return onext(req, res);
      }

      // skip if route match does not border '/', '.' or end
      var c = path[route.length];
      if (c !== undefined && c !== '/' && c !== '.') {
        return onext(req, res);
      }

      var removed;

      // trim off the part of the url that matches the route
      if (route.length !== 0 && route !== '/') {
        removed = route;

        var fqreq = _.clone(req);
        fqreq.url = sliceAtRemoved(req.url);
        fqreq.path = sliceAtRemoved(req.path);

        return handler(fqreq, res, function next(nreq, nres) {
          var fqreq = _.clone(nreq);
          fqreq.url = insertRemoved(nreq.url);
          fqreq.path = insertRemoved(nreq.path);

          return onext(nreq, nres);
        });
      }

      return handler(req, res, onext);

      function sliceAtRemoved(path) {
        path = path.slice(removed.length);

        if (path[0] === '/') {
          return path;
        } else {
          return '/' + path;
        }
      }

      function insertRemoved(path) {
        if (removed[removed.length] === '/') {
          return removed + path.slice(1);
        } else {
          return removed + path;
        }
      }
    });
  };

  router.use = function(route, fn) {
    if (typeof route !== 'string') {
      fn = route;
      route = '/';
    }

    if (route[route.length - 1] === '/') {
      route = route.slice(0, -1);
    }

    return makeRouter(route, fn, router);
  };

  router.useAll = function(routes) {
    return reduce(function(router, route) {
      if (typeof route === 'function') {
        return router.use(route);
      } else {
        return router.use(route[0], route[1]);
      }
    }, router, routes);
  };

  return router;
};

function reduce(fn, data, array) {
  var index = -1;
  var length = array.length;

  while (index++ < length) {
    data = fn(data, array[index]);
  }

  return data;
}


text:
package com.blossomproject.ui.security;

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

@RunWith(MockitoJUnitRunner.class)
public class AuthenticationFailureListenerTest {

  @Rule
  public ExpectedException thrown = ExpectedException.none();
  private LoginAttemptsService loginAttemptService;
  private AuthenticationFailureListener listener;

  @Before
  public void setUp() throws Exception {
    this.loginAttemptService = mock(LoginAttemptsService.class);
    this.listener = new AuthenticationFailureListener(this.loginAttemptService);
  }

  @Test
  public void should_succeed_on_null_login_attempt_service() throws Exception {
  }

  @Test
  public void should_fail_on_null_login_attempt_service() throws Exception {
    thrown.expect(IllegalArgumentException.class);
    new AuthenticationFailureListener(null);
  }

  @Test
  public void should_receive_event() throws Exception {
    String principal = "principal";
    String remoteAddress = "remoteAddress";
    WebAuthenticationDetails details = mock(WebAuthenticationDetails.class);
    when(details.getRemoteAddress()).thenReturn(remoteAddress);

    Authentication authentication = mock(Authentication.class);
    when(authentication.getPrincipal()).thenReturn(principal);
    when(authentication.getDetails()).thenReturn(details);

    AuthenticationFailureBadCredentialsEvent event = mock(
      AuthenticationFailureBadCredentialsEvent.class);
    when(event.getAuthentication()).thenReturn(authentication);

    this.listener.onApplicationEvent(event);

    verify(this.loginAttemptService, times(1)).failAttempt(eq(principal), eq(remoteAddress));
  }

}


text:
namespace content {
class WebContents;
}

namespace views {
class LabelButton;
}

// Provides functionality to display information about a hung renderer.
class HungPagesTableModel : public ui::TableModel, public views::TableGrouper {
 public:
  // The Delegate is notified any time a WebContents the model is listening to
  // is destroyed.
  class Delegate {
   public:
    virtual void TabDestroyed() = 0;

   protected:
    virtual ~Delegate() {}
  };

  explicit HungPagesTableModel(Delegate* delegate);
  ~HungPagesTableModel() override;

  void InitForWebContents(content::WebContents* hung_contents);

  // Returns the first RenderProcessHost, or NULL if there aren't any
  // WebContents.
  content::RenderProcessHost* GetRenderProcessHost();

  // Returns the first RenderViewHost, or NULL if there aren't any WebContents.
  content::RenderViewHost* GetRenderViewHost();

  // Overridden from ui::TableModel:
  int RowCount() override;
  base::string16 GetText(int row, int column_id) override;
  gfx::ImageSkia GetIcon(int row) override;
  void SetObserver(ui::TableModelObserver* observer) override;

  // Overridden from views::TableGrouper:
  void GetGroupRange(int model_index, views::GroupRange* range) override;

 private:
  // Used to track a single WebContents. If the WebContents is destroyed
  // TabDestroyed() is invoked on the model.
  class WebContentsObserverImpl : public content::WebContentsObserver {
   public:
    WebContentsObserverImpl(HungPagesTableModel* model,
                            content::WebContents* tab);

    FaviconTabHelper* favicon_tab_helper() {
      return FaviconTabHelper::FromWebContents(web_contents());
    }

    // WebContentsObserver overrides:
    void RenderProcessGone(base::TerminationStatus status) override;
    void WebContentsDestroyed() override;

   private:
    HungPagesTableModel* model_;

    DISALLOW_COPY_AND_ASSIGN(WebContentsObserverImpl);
  };

  // Invoked when a WebContents is destroyed. Cleans up |tab_observers_| and
  // notifies the observer and delegate.
  void TabDestroyed(WebContentsObserverImpl* tab);

  typedef ScopedVector<WebContentsObserverImpl> TabObservers;
  TabObservers tab_observers_;

  ui::TableModelObserver* observer_;
  Delegate* delegate_;

  DISALLOW_COPY_AND_ASSIGN(HungPagesTableModel);
};

// This class displays a dialog which contains information about a hung
// renderer process.
class HungRendererDialogView : public views::DialogDelegateView,
                               public views::ButtonListener,
                               public HungPagesTableModel::Delegate {
 public:
  // Factory function for creating an instance of the HungRendererDialogView
  // class. At any given point only one instance can be active.
  static HungRendererDialogView* Create(gfx::NativeWindow context);

  // Returns a pointer to the singleton instance if any.
  static HungRendererDialogView* GetInstance();

  // Shows or hides the hung renderer dialog for the given WebContents.
  static void Show(content::WebContents* contents);
  static void Hide(content::WebContents* contents);

  // Platform specific function to kill the renderer process identified by the
  // render process host passed in.
  static void KillRendererProcess(content::RenderProcessHost* rph);

  // Returns true if the frame is in the foreground.
  static bool IsFrameActive(content::WebContents* contents);

  virtual void ShowForWebContents(content::WebContents* contents);
  virtual void EndForWebContents(content::WebContents* contents);

  // views::DialogDelegateView overrides:
  base::string16 GetWindowTitle() const override;
  void WindowClosing() override;
  int GetDialogButtons() const override;
  base::string16 GetDialogButtonLabel(ui::DialogButton button) const override;
  views::View* CreateExtraView() override;
  bool Accept(bool window_closing) override;
  bool UseNewStyleForThisDialog() const override;

  // views::ButtonListener overrides:
  void ButtonPressed(views::Button* sender, const ui::Event& event) override;

  // HungPagesTableModel::Delegate overrides:
  void TabDestroyed() override;

 protected:
  HungRendererDialogView();
  ~HungRendererDialogView() override;

  // views::View overrides:
  void ViewHierarchyChanged(
      const ViewHierarchyChangedDetails& details) override;

  static HungRendererDialogView* g_instance_;

 private:
  // Initialize the controls in this dialog.
  void Init();

  static void InitClass();

  // Controls within the dialog box.
  views::TableView* hung_pages_table_;

  // The extra button inserted into the ClientView to kill the errant process.
  views::LabelButton* kill_button_;

  // The model that provides the contents of the table that shows a list of
  // pages affected by the hang.
  scoped_ptr<HungPagesTableModel> hung_pages_table_model_;

  // Whether or not we've created controls for ourself.
  bool initialized_;

  // An amusing icon image.
  static gfx::ImageSkia* frozen_icon_;

  DISALLOW_COPY_AND_ASSIGN(HungRendererDialogView);
};

#endif  // CHROME_BROWSER_UI_VIEWS_HUNG_RENDERER_VIEW_H_


text:
from test import support
support.requires('audio')

from test.support import findfile

ossaudiodev = support.import_module('ossaudiodev')

import errno
import sys
import sunau
import time
import audioop
import unittest

# Arggh, AFMT_S16_NE not defined on all platforms -- seems to be a
# fairly recent addition to OSS.
try:
    from ossaudiodev import AFMT_S16_NE
except ImportError:
    if sys.byteorder == "little":
        AFMT_S16_NE = ossaudiodev.AFMT_S16_LE
    else:
        AFMT_S16_NE = ossaudiodev.AFMT_S16_BE

def read_sound_file(path):
    with open(path, 'rb') as fp:
        au = sunau.open(fp)
        rate = au.getframerate()
        nchannels = au.getnchannels()
        encoding = au._encoding
        fp.seek(0)
        data = fp.read()

    if encoding != sunau.AUDIO_FILE_ENCODING_MULAW_8:
        raise RuntimeError("Expect .au file with 8-bit mu-law samples")

    # Convert the data to 16-bit signed.
    data = audioop.ulaw2lin(data, 2)
    return (data, rate, 16, nchannels)

class OSSAudioDevTests(unittest.TestCase):

    def play_sound_file(self, data, rate, ssize, nchannels):
        try:
            dsp = ossaudiodev.open('w')
        except IOError as msg:
            if msg.args[0] in (errno.EACCES, errno.ENOENT,
                               errno.ENODEV, errno.EBUSY):
                raise unittest.SkipTest(msg)
            raise

        # at least check that these methods can be invoked
        dsp.bufsize()
        dsp.obufcount()
        dsp.obuffree()
        dsp.getptr()
        dsp.fileno()

        # Make sure the read-only attributes work.
        self.assertFalse(dsp.closed)
        self.assertEqual(dsp.name, "/dev/dsp")
        self.assertEqual(dsp.mode, "w", "bad dsp.mode: %r" % dsp.mode)

        # And make sure they're really read-only.
        for attr in ('closed', 'name', 'mode'):
            try:
                setattr(dsp, attr, 42)
            except (TypeError, AttributeError):
                pass
            else:
                self.fail("dsp.%s not read-only" % attr)

        # Compute expected running time of sound sample (in seconds).
        expected_time = float(len(data)) / (ssize/8) / nchannels / rate

        # set parameters based on .au file headers
        dsp.setparameters(AFMT_S16_NE, nchannels, rate)
        self.assertTrue(abs(expected_time - 3.51) < 1e-2, expected_time)
        t1 = time.time()
        dsp.write(data)
        dsp.close()
        t2 = time.time()
        elapsed_time = t2 - t1

        percent_diff = (abs(elapsed_time - expected_time) / expected_time) * 100
        self.assertTrue(percent_diff <= 10.0,
                        "elapsed time (%s) > 10%% off of expected time (%s)" %
                        (elapsed_time, expected_time))

    def set_parameters(self, dsp):
        # Two configurations for testing:
        #   config1 (8-bit, mono, 8 kHz) should work on even the most
        #      ancient and crufty sound card, but maybe not on special-
        #      purpose high-end hardware
        #   config2 (16-bit, stereo, 44.1kHz) should work on all but the
        #      most ancient and crufty hardware
        config1 = (ossaudiodev.AFMT_U8, 1, 8000)
        config2 = (AFMT_S16_NE, 2, 44100)

        for config in [config1, config2]:
            (fmt, channels, rate) = config
            if (dsp.setfmt(fmt) == fmt and
                dsp.channels(channels) == channels and
                dsp.speed(rate) == rate):
                break
        else:
            raise RuntimeError("unable to set audio sampling parameters: "
                               "you must have really weird audio hardware")

        # setparameters() should be able to set this configuration in
        # either strict or non-strict mode.
        result = dsp.setparameters(fmt, channels, rate, False)
        self.assertEqual(result, (fmt, channels, rate),
                         "setparameters%r: returned %r" % (config, result))

        result = dsp.setparameters(fmt, channels, rate, True)
        self.assertEqual(result, (fmt, channels, rate),
                         "setparameters%r: returned %r" % (config, result))

    def set_bad_parameters(self, dsp):
        # Now try some configurations that are presumably bogus: eg. 300
        # channels currently exceeds even Hollywood's ambitions, and
        # negative sampling rate is utter nonsense.  setparameters() should
        # accept these in non-strict mode, returning something other than
        # was requested, but should barf in strict mode.
        fmt = AFMT_S16_NE
        rate = 44100
        channels = 2
        for config in [(fmt, 300, rate),       # ridiculous nchannels
                       (fmt, -5, rate),        # impossible nchannels
                       (fmt, channels, -50),   # impossible rate
                      ]:
            (fmt, channels, rate) = config
            result = dsp.setparameters(fmt, channels, rate, False)
            self.assertNotEqual(result, config,
                             "unexpectedly got requested configuration")

            try:
                result = dsp.setparameters(fmt, channels, rate, True)
            except ossaudiodev.OSSAudioError as err:
                pass
            else:
                self.fail("expected OSSAudioError")

    def test_playback(self):
        sound_info = read_sound_file(findfile('audiotest.au'))
        self.play_sound_file(*sound_info)

    def test_set_parameters(self):
        dsp = ossaudiodev.open("w")
        try:
            self.set_parameters(dsp)

            # Disabled because it fails under Linux 2.6 with ALSA's OSS
            # emulation layer.
            #self.set_bad_parameters(dsp)
        finally:
            dsp.close()
            self.assertTrue(dsp.closed)

    def test_mixer_methods(self):
        # Issue #8139: ossaudiodev didn't initialize its types properly,
        # therefore some methods were unavailable.
        with ossaudiodev.openmixer() as mixer:
            self.assertGreaterEqual(mixer.fileno(), 0)

    def test_with(self):
        with ossaudiodev.open('w') as dsp:
            pass
        self.assertTrue(dsp.closed)

    def test_on_closed(self):
        dsp = ossaudiodev.open('w')
        dsp.close()
        self.assertRaises(ValueError, dsp.fileno)
        self.assertRaises(ValueError, dsp.read, 1)
        self.assertRaises(ValueError, dsp.write, b'x')
        self.assertRaises(ValueError, dsp.writeall, b'x')
        self.assertRaises(ValueError, dsp.bufsize)
        self.assertRaises(ValueError, dsp.obufcount)
        self.assertRaises(ValueError, dsp.obufcount)
        self.assertRaises(ValueError, dsp.obuffree)
        self.assertRaises(ValueError, dsp.getptr)

        mixer = ossaudiodev.openmixer()
        mixer.close()
        self.assertRaises(ValueError, mixer.fileno)

def test_main():
    try:
        dsp = ossaudiodev.open('w')
    except (ossaudiodev.error, IOError) as msg:
        if msg.args[0] in (errno.EACCES, errno.ENOENT,
                           errno.ENODEV, errno.EBUSY):
            raise unittest.SkipTest(msg)
        raise
    dsp.close()
    support.run_unittest(__name__)

if __name__ == "__main__":
    test_main()


text:
#import "NSObject-Protocol.h"

@protocol SWRunWorkoutObserver <NSObject>
- (void)observeRunWorkoutUserEvent:(id)arg1 userEvent:(id)arg2;
- (void)observeRunWorkoutStateChange:(id)arg1 oldState:(id)arg2 newState:(id)arg3;
- (void)observeRunWorkoutControllerDataChange:(id)arg1 elapsedTime:(unsigned long long)arg2 pace:(float)arg3 distance:(float)arg4 calories:(float)arg5 location:(id)arg6;
@end


text:
#ifndef OSSL_CRYPTO_PUNYCODE_H
# define OSSL_CRYPTO_PUNYCODE_H
# if defined(__GNUC__) && (__GNUC__ > 3 || (__GNUC__ == 3 && __GNUC_MINOR__ > 3))
# pragma once
# endif

int ossl_punycode_decode (
    const char *pEncoded,
    const size_t enc_len,
    unsigned int *pDecoded,
    unsigned int *pout_length
);

int ossl_a2ulabel(const char *in, char *out, size_t *outlen);

int ossl_a2ucompare(const char *a, const char *u);
#endif


text:
package org.apache.hadoop.mapreduce.lib.input;

import java.io.*;
import java.util.*;
import junit.framework.TestCase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.task.MapContextImpl;

public class TestNLineInputFormat extends TestCase {
  private static int MAX_LENGTH = 200;

  private static Configuration conf = new Configuration();
  private static FileSystem localFs = null;

  static {
    try {
      localFs = FileSystem.getLocal(conf);
    } catch (IOException e) {
      throw new RuntimeException("init failure", e);
    }
  }

  private static Path workDir =
    new Path(new Path(System.getProperty("test.build.data", "."), "data"),
             "TestNLineInputFormat");

  public void testFormat() throws Exception {
    Job job = new Job(conf);
    Path file = new Path(workDir, "test.txt");

    int seed = new Random().nextInt();
    Random random = new Random(seed);

    localFs.delete(workDir, true);
    FileInputFormat.setInputPaths(job, workDir);
    int numLinesPerMap = 5;
    NLineInputFormat.setNumLinesPerSplit(job, numLinesPerMap);
    // for a variety of lengths
    for (int length = 0; length < MAX_LENGTH;
         length += random.nextInt(MAX_LENGTH / 10) + 1) {
      // create a file with length entries
      Writer writer = new OutputStreamWriter(localFs.create(file));
      try {
        for (int i = 0; i < length; i++) {
          writer.write(Integer.toString(i));
          writer.write("\n");
        }
      } finally {
        writer.close();
      }
      checkFormat(job, numLinesPerMap);
    }
  }

  void checkFormat(Job job, int expectedN)
      throws IOException, InterruptedException {
    NLineInputFormat format = new NLineInputFormat();
    List<InputSplit> splits = format.getSplits(job);
    // check all splits except last one
    int count = 0;
    for (int i = 0; i < splits.size() -1; i++) {
      assertEquals("There are no split locations", 0,
                   splits.get(i).getLocations().length);
      TaskAttemptContext context = MapReduceTestUtil.
        createDummyMapTaskAttemptContext(job.getConfiguration());
      RecordReader<LongWritable, Text> reader = format.createRecordReader(
        splits.get(i), context);
      Class<?> clazz = reader.getClass();
      assertEquals("reader class is LineRecordReader.",
        LineRecordReader.class, clazz);
      MapContext<LongWritable, Text, LongWritable, Text> mcontext =
        new MapContextImpl<LongWritable, Text, LongWritable, Text>(
          job.getConfiguration(), context.getTaskAttemptID(), reader, null,
          null, MapReduceTestUtil.createDummyReporter(), splits.get(i));
      reader.initialize(splits.get(i), mcontext);

      try {
        count = 0;
        while (reader.nextKeyValue()) {
          count++;
        }
      } finally {
        reader.close();
      }
      assertEquals("number of lines in split is " + expectedN ,
                   expectedN, count);
    }
  }

  public static void main(String[] args) throws Exception {
    new TestNLineInputFormat().testFormat();
  }
}


text:
"""Linux script.

This module holds the commands to install necessary packages
on linux platform.

Example:
        $ wget -P downloads/ https://www.python.org/ftp/python-python-version

Todo:
    * To be completed...

"""

# --------------- START: Native Imports -------------- #
import os
# --------------- END: Native Imports -------------- #

# --------------- START: In-House Imports -------------- #
from configs import versions_config

from utils.commons import (
    ChangeDir, AbortBuild, TextMessage, build_expr, simple_command, complex_command
)
# --------------- END: In-House Imports -------------- #

__all__ = [
    # Public Symbols.
]

PY_VERSION = versions_config('build', 'python')
"""``str``: Module level variable.  Version number for the python.
"""

BUILD_COMMANDS = [

    #
    # Install Python as Virtual Environment.
    build_expr(

        desc='Downloading and installing Custom Python version'.format(PY_VERSION),

        cmd=[
            complex_command(
                if_='find usr/bin/ -name python',
                if_axn=AbortBuild('Python is already installed !, please remove the  existing one.'),
                else_axn=TextMessage('New Python will be installed !')
            ),
            complex_command(
                if_='find downloads/ -name Python-{0}.tar.xz'.format(PY_VERSION),
                if_axn=TextMessage('Download Exists -- Python-{0}.tar.xz'.format(PY_VERSION)),
                else_axn='wget -P downloads/ https://www.python.org/ftp/python/{0}/Python-{0}.tar.xz'.format(PY_VERSION)
            ),
            ChangeDir('downloads/'),
            simple_command('tar -xvJf Python-{0}.tar.xz'.format(PY_VERSION)),
            ChangeDir('downloads/Python-{0}/'.format(PY_VERSION)),
            simple_command('./configure --prefix={0}/usr/ --enable-unicode=ucs4'.format(os.getcwd())),
            simple_command('make'),
            simple_command('make install'),
        ],

        teardown=[
            ChangeDir(),
            simple_command('rm -rvf downloads/Python-{0}/'.format(PY_VERSION)),
        ],

        on_success='Command %(cmd)s ran successfully !',
        on_error='Failed while running the command %(cmd)s. ERROR: {}',
    ),

]


text:
<Workflow xmlns="urn:wexflow-schema" id="7" name="Workflow_ListEntities" description="Workflow_ListEntities">
	<Settings>
		<Setting name="launchType" value="trigger" /> <!-- startup|trigger|periodic -->
		<Setting name="enabled" value="true" /> <!-- true|false -->
	</Settings>
	<Tasks>
		<!-- This task is useful when working with custom tasks that use entities.-->
		<Task id="1" name="ListEntities" description="Listing entities" enabled="true">
		</Task>
	</Tasks>
</Workflow>


text:
/* ----------------------------------------------------------------------------
 * This file was automatically generated by SWIG (http://www.swig.org).
 * Version 4.0.2
 *
 * Do not make changes to this file unless you know what you are doing--modify
 * the SWIG interface file instead.
 * ----------------------------------------------------------------------------- */

package org.opensim.modeling;

/**
 * An class for holding and managing a set of tasks.

 *

 * @author Frank C. Anderson

 * @version 1.0
 */
public class CMC_TaskSet extends SetTrackingTasks {
  private transient long swigCPtr;

  public CMC_TaskSet(long cPtr, boolean cMemoryOwn) {
    super(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_SWIGUpcast(cPtr), cMemoryOwn);
    swigCPtr = cPtr;
  }

  public static long getCPtr(CMC_TaskSet obj) {
    return (obj == null) ? 0 : obj.swigCPtr;
  }

  @SuppressWarnings("deprecation")
  protected void finalize() {
    delete();
  }

  public synchronized void delete() {
    if (swigCPtr != 0) {
      if (swigCMemOwn) {
        swigCMemOwn = false;
        opensimActuatorsAnalysesToolsJNI.delete_CMC_TaskSet(swigCPtr);
      }
      swigCPtr = 0;
    }
    super.delete();
  }

  public static CMC_TaskSet safeDownCast(OpenSimObject obj) {
    long cPtr = opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_safeDownCast(OpenSimObject.getCPtr(obj), obj);
    return (cPtr == 0) ? null : new CMC_TaskSet(cPtr, false);
  }

  public void assign(OpenSimObject aObject) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_assign(swigCPtr, this, OpenSimObject.getCPtr(aObject), aObject);
  }

  public static String getClassName() {
    return opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getClassName();
  }

  public OpenSimObject clone() {
    long cPtr = opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_clone(swigCPtr, this);
    return (cPtr == 0) ? null : new CMC_TaskSet(cPtr, true);
  }

  public String getConcreteClassName() {
    return opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getConcreteClassName(swigCPtr, this);
  }

  public CMC_TaskSet() {
    this(opensimActuatorsAnalysesToolsJNI.new_CMC_TaskSet__SWIG_0(), true);
  }

  public CMC_TaskSet(String aFileName) {
    this(opensimActuatorsAnalysesToolsJNI.new_CMC_TaskSet__SWIG_1(aFileName), true);
  }

  public CMC_TaskSet(CMC_TaskSet aCMCTaskSet) {
    this(opensimActuatorsAnalysesToolsJNI.new_CMC_TaskSet__SWIG_2(CMC_TaskSet.getCPtr(aCMCTaskSet), aCMCTaskSet), true);
  }

  public void setModel(Model aModel) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_setModel(swigCPtr, this, Model.getCPtr(aModel), aModel);
  }

  public Model getModel() {
    long cPtr = opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getModel(swigCPtr, this);
    return (cPtr == 0) ? null : new Model(cPtr, false);
  }

  public String getDataFileName() {
    return opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getDataFileName(swigCPtr, this);
  }

  public void setFunctions(FunctionSet aFuncSet) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_setFunctions(swigCPtr, this, FunctionSet.getCPtr(aFuncSet), aFuncSet);
  }

  public void setFunctionsForVelocity(FunctionSet aFuncSet) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_setFunctionsForVelocity(swigCPtr, this, FunctionSet.getCPtr(aFuncSet), aFuncSet);
  }

  public void setFunctionsForAcceleration(FunctionSet aFuncSet) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_setFunctionsForAcceleration(swigCPtr, this, FunctionSet.getCPtr(aFuncSet), aFuncSet);
  }

  public int getNumActiveTaskFunctions() {
    return opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getNumActiveTaskFunctions(swigCPtr, this);
  }

  public ArrayDouble getTaskPositions(double aT) {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getTaskPositions(swigCPtr, this, aT), false);
  }

  public ArrayDouble getTaskVelocities(double aT) {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getTaskVelocities(swigCPtr, this, aT), false);
  }

  public ArrayDouble getTaskAccelerations(double aT) {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getTaskAccelerations(swigCPtr, this, aT), false);
  }

  public ArrayDouble getPositionGains() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getPositionGains(swigCPtr, this), false);
  }

  public ArrayDouble getVelocityGains() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getVelocityGains(swigCPtr, this), false);
  }

  public ArrayDouble getAccelerationGains() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getAccelerationGains(swigCPtr, this), false);
  }

  public ArrayDouble getPositionErrorsLast() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getPositionErrorsLast(swigCPtr, this), false);
  }

  public ArrayDouble getPositionErrors() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getPositionErrors(swigCPtr, this), false);
  }

  public ArrayDouble getVelocityErrorsLast() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getVelocityErrorsLast(swigCPtr, this), false);
  }

  public ArrayDouble getVelocityErrors() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getVelocityErrors(swigCPtr, this), false);
  }

  public ArrayDouble getWeights() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getWeights(swigCPtr, this), false);
  }

  public ArrayDouble getDesiredAccelerations() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getDesiredAccelerations(swigCPtr, this), false);
  }

  public ArrayDouble getAccelerations() {
    return new ArrayDouble(opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_getAccelerations(swigCPtr, this), false);
  }

  public void recordErrorsAsLastErrors() {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_recordErrorsAsLastErrors(swigCPtr, this);
  }

  public void computeErrors(State s, double aT) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_computeErrors(swigCPtr, this, State.getCPtr(s), s, aT);
  }

  public void computeDesiredAccelerations(State s, double aT) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_computeDesiredAccelerations__SWIG_0(swigCPtr, this, State.getCPtr(s), s, aT);
  }

  public void computeDesiredAccelerations(State s, double aTCurrent, double aTFuture) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_computeDesiredAccelerations__SWIG_1(swigCPtr, this, State.getCPtr(s), s, aTCurrent, aTFuture);
  }

  public void computeAccelerations(State s) {
    opensimActuatorsAnalysesToolsJNI.CMC_TaskSet_computeAccelerations(swigCPtr, this, State.getCPtr(s), s);
  }

}


text:
#ifndef OPTIMIZE_H
#define OPTIMIZE_H

#include "liveness.h"

#include <lacc/ir.h>

/* Set to non-zero to enable optimization. */
INTERNAL void push_optimization(int level);

/*
 * Do data flow analysis and perform optimizations on the intermediate
 * representation. Leaves the definition in a semantically equivalent,
 * and hopefully more consise, state.
 */
INTERNAL void optimize(struct definition *def);

/* Disable previously set optimization, cleaning up resources. */
INTERNAL void pop_optimization(void);

#endif


text:
using System;
using System.Collections.Generic;
using Pulse.Core;

namespace Pulse.FS
{
    public sealed class WdbMovieArchiveListing : List<WdbMovieEntry>, IArchiveListing
    {
        public readonly DbArchiveAccessor Accessor;

        public WdbMovieArchiveListing(DbArchiveAccessor accessor, int entriesCount)
            : base(entriesCount)
        {
            Accessor = accessor;
        }

        public string Name
        {
            get { return Accessor.Name; }
        }

        public string ExtractionSubpath
        {
            get { return PathEx.ChangeMultiDotExtension(Name, ".unpack"); }
        }

        public String PackagePostfix
        {
            get { return PathEx.ChangeMultiDotExtension(Name, null).EndsWith("_us") ? "_us" : String.Empty; }
        }
    }
}