PLC_Generation/PLC Data Generator/extract_io_sheet.py
2025-08-05 14:38:54 +04:00

75 lines
2.2 KiB
Python

import pandas as pd
import re
# Read the Excel file
input_file = 'mcm04 very last.xlsx'
df = pd.read_excel(input_file)
# Prepare output rows
output_rows = []
io_columns = [f'IO{i}' for i in range(16)]
# First pass: collect all prefixes with JR1_PB (JAM RESET PUSHBUTTON)
jam_reset_prefixes = set()
for _, row in df.iterrows():
for io_col in io_columns:
val = row.get(io_col, '')
if pd.isna(val) or val == '':
continue
if 'JR1_PB' in str(val):
m = re.match(r'(S\d+)_', str(val))
if m:
jam_reset_prefixes.add(m.group(1))
# Second pass: build output with DESB logic
def get_prefix(tag):
m = re.match(r'(S\d+)_', str(tag))
return m.group(1) if m else None
def get_desb(desca):
if desca == 'SPARE' or pd.isna(desca) or desca == '':
return ''
tag = str(desca)
prefix = get_prefix(tag)
if 'BCN1_A' in tag:
return 'AMBER BEACON LIGHT'
if 'BCN1_B' in tag:
return 'BLUE BEACON LIGHT'
if 'BCN1' in tag:
if prefix in jam_reset_prefixes:
return '3 STACK IOLINK BEACON'
else:
return '2 STACK IOLINK BEACON'
if 'SOL' in tag:
return 'PKG RELEASE SOLENOID'
if 'PR' in tag:
return 'PKG RELEASE PUSHBUTTON'
if 'PE1' in tag:
return 'FULL PHOTOEYE 50%'
if 'PE2' in tag:
return 'FULL PHOTOEYE 100%'
if 'GS1_PB_LT' in tag or 'GS1_PB' in tag:
return 'CHUTE ENABLE PUSHBUTTON LIGHT'
if 'JR1_PB_LT' in tag:
return 'SORTER JAM RESET PUSHBUTTON LIGHT'
if 'JR1_PB' in tag:
return 'SORTER JAM RESET PUSHBUTTON'
if 'FIOH' in tag:
return 'HUB ARMOR BLOCK'
return ''
for _, row in df.iterrows():
tagname = row['P_TAG1']
for io_col in io_columns:
term = io_col
desca = row.get(io_col, '')
if pd.isna(desca) or desca == '':
desca = 'SPARE'
desb = get_desb(desca)
output_rows.append({'TAGNAME': tagname, 'TERM': term, 'DESCA': desca, 'DESB': desb})
# Output to CSV
output_df = pd.DataFrame(output_rows)
output_df.to_csv('MCM04_IO_EXPANDED.csv', index=False)
print('Output written to MCM04_IO_EXPANDED.csv')