import re

def parse_spec_file(file_path):
    scans = {}
    current_scan_id = None
    
    # Regular expressions for header fields
    s_pattern = re.compile(r'^#S\s+(\d+)\s+(.*)')
    d_pattern = re.compile(r'^#D\s+(.*)')
    l_pattern = re.compile(r'^#L\s+(.*)')
    
    with open(file_path, 'r') as f:
        for line in f:
            line = line.strip()
            if not line:
                continue
                
            # Detect new scan chunk
            s_match = s_pattern.match(line)
            if s_match:
                current_scan_id = int(s_match.group(1))
                scans[current_scan_id] = {
                    'command': s_match.group(2).strip(),
                    'header': {},
                    'columns': [],
                    'data': []
                }
                continue
            
            if current_scan_id is not None:
                # Parse Date
                if line.startswith('#D'):
                    scans[current_scan_id]['date'] = line[3:].strip()
                # Parse Column Labels
                elif line.startswith('#L'):
                    scans[current_scan_id]['columns'] = line[3:].split()
                # Parse other header metadata (#G, #Q, #P, #N etc.)
                elif line.startswith('#'):
                    key = line[1:3].strip() # e.g., G0, Q, P0
                    scans[current_scan_id]['header'][key] = line[3:].strip()
                # Parse Data Rows
                else:
                    try:
                        # Convert space-separated numbers to floats
                        row = [float(val) for val in line.split()]
                        if row:
                            scans[current_scan_id]['data'].append(row)
                    except ValueError:
                        # Skip lines that aren't numeric data (like #C comments within data)
                        continue
                        
    return scans

# Usage
parsed_data = parse_spec_file('comm_2025_2.spec')

# Example: Accessing Scan 1
print(f"Scan 1 Command: {parsed_data[1]['command']}")
print(f"Scan 1 Columns: {parsed_data[1]['columns']}")
print(f"First row of data: {parsed_data[1]['data'][0]}")
