<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import xml.etree.ElementTree as ET
from collections import defaultdict
import pdb

# Parse the XML file
tree = ET.parse('cal/BYL4_28JAN21.XMLCON')
root = tree.getroot()

def find_elements_by_tag(data, tag):
    matches = []
    if data['tag'] == tag:
        matches.append(data)
    for child in data['children']:
        matches.extend(find_elements_by_tag(child, tag))
    return matches

# Recursive function to process each element
def parse_element(element):
    data = {
        "tag": element.tag,
        "attributes": element.attrib,
        "text": element.text.strip() if element.text else "",
        "children": []
    }
    # Process child elements recursively
    for child in element:
        data["children"].append(parse_element(child))
    return data

# Parse the root element
xml_data = parse_element(root)

# initialise a list to store the results
sensor_data = []

file_version = xml_data['attributes'].get('SB_ConfigCTD_FileVersion')
instrument_name = xml_data['children'][0]['children'][0]['text']

for sensor in xml_data['children'][0]['children'][-1]['children']:
    sensor_type = sensor['children'][0]['tag']
    sensor_id = sensor['attributes'].get('SensorID')
    sensor_index = sensor['attributes'].get('index')
    calibration_date = next(
        (child['text'] for child in sensor['children'][0]['children'] if child['tag'] == 'CalibrationDate'), None
    )
    #print(f"Sensor Type: {sensor_type}, Sensor ID: {sensor_id}, Calibration Date: {calibration_date}")
    ScaleFactor = next(
        (child['text'] for child in sensor['children'][0]['children'] if child['tag'] == 'ScaleFactor'), None
    )
    #print(f"Sensor Type: {sensor_type}, Sensor ID: {sensor_id}, ScaleFactor: {ScaleFactor}")
    DarkVoltage = next(
        (child['text'] for child in sensor['children'][0]['children'] if child['tag'] == 'DarkVoltage'), None
    )
    Vblank = next(        
        (child['text'] for child in sensor['children'][0]['children'] if child['tag'] == 'Vblank'), None
    )
    #print(f"Sensor Type: {sensor_type}, Sensor ID: {sensor_id}, DarkVoltage: {DarkVoltage}")

    # Create a dictionary to hold this sensor's data
    sensor_info = {
        'sensor_type': sensor_type,
        'sensor_id': sensor_id,
        'sensor_index': sensor_index,
        'calibration_date': calibration_date,
        'scale_factor': ScaleFactor,
        'dark_voltage': DarkVoltage,
        'Vblank': Vblank
    }

    # Append the sensor's data to the sensor_data list
    sensor_data.append(sensor_info)

pdb.set_trace()
</pre></body></html>