# 1. READ FILE
data <- read.table("test.txt", skip=1)

# Assign column names
colnames(data) <- c("TOF", "YO", "YI", "CHWDT")

# Ensure numeric
data <- data.frame(lapply(data, as.numeric))

# Inspect
str(data)
## 'data.frame':    4050 obs. of  4 variables:
##  $ TOF  : num  1 1 1 1 1 ...
##  $ YO   : num  516 516 527 539 518 ...
##  $ YI   : num  10199 10177 10154 10132 10111 ...
##  $ CHWDT: num  800 800 800 800 800 800 800 800 800 800 ...
head(data)
##      TOF       YO       YI CHWDT
## 1 1.0004 515.9318 10198.60   800
## 2 1.0012 516.0087 10177.04   800
## 3 1.0020 527.0089 10154.44   800
## 4 1.0028 539.0091 10132.31   800
## 5 1.0036 518.0088 10110.64   800
## 6 1.0044 506.0086 10089.41   800
# 2. NORMALIZE (CORE STEP)
data$Intensity <- (data$YO / data$YI) / data$CHWDT

# Handle invalid values
data$Intensity[!is.finite(data$Intensity)] <- NA
# 3. PLOT DIFFRACTION PATTERN
plot(data$TOF, data$Intensity, type="l",
     xlab="TOF",
     ylab="Normalized Intensity",
     main="Normalized Diffraction Pattern")

# 4. PCA (FOR MULTIPLE FILES)
# Example: simulate multiple runs (you will replace this with real files later)
# For now, treat this as one run
X <- as.matrix(data$Intensity)

# If you have multiple files, it becomes:
# X <- rbind(run1, run2, run3, ...)

# PCA requires at least 2 runs
# So here is the REAL PCA setup for batch use:

# (commented example for future use)
# X <- do.call(rbind, X_list)

# Run PCA (only works with multiple runs)
# Uncomment when you have multiple datasets

# pca <- prcomp(X, scale.=TRUE)

# Plot PCA scores
# plot(pca$x[,1], pca$x[,2],
#     xlab="PC1",
#     ylab="PC2",
#     main="PCA of Diffraction Data")

# Plot PC1 evolution (phase tracking)
# plot(pca$x[,1], type="l",
#     xlab="Run Index",
#     ylab="PC1 Score",
#     main="Phase Evolution")