Skip to content

Instantly share code, notes, and snippets.

@saslaw
Last active April 24, 2025 15:14
Show Gist options
  • Select an option

  • Save saslaw/0877fe0813631cc5ecdcf2753f09c97c to your computer and use it in GitHub Desktop.

Select an option

Save saslaw/0877fe0813631cc5ecdcf2753f09c97c to your computer and use it in GitHub Desktop.
Tidy Thermo-Phenom SEM data
# Tidy Thermo-Phenom SEM data
# Mae Saslaw, 2025
# Before using this script, rename .emsa file to .csv.
# All caps indicates placeholder text
library(dplyr)
library(stringr)
library(tinyplot)
csv.to.spectrum <- function(emsa) {
# Create dataframe from SEM output
xperchan <- as.numeric(str_remove(emsa$V1[12], "#XPERCHAN : "))
counts <- emsa |> filter(!row_number() %in% 1:33)
eV <- seq(from = 0, by = xperchan, length.out = as.numeric(nrow(counts)))
spectrum <- data.frame(eV, counts = unlist(counts))
# Plot spectral data
plt(
counts ~ eV,
data = spectrum,
type = "l",
xlim = c(0, 10000)
)
return(spectrum)
}
# Sample 1
emsa <- read.csv("DATA/SAMPLE1.csv", header = FALSE)
SAMPLE1 <- csv.to.spectrum(emsa)
# Suggested fields to add
SAMPLE1 <- SAMPLE1 |>
mutate(SampleID = "SAMPLE1") |>
mutate(Analysis = "Area 1 Spot 1") |>
mutate(Mineral = "QUARTZ")
# Repeat for all samples and join sample(n) tables to create a frame for plotting
samples <- rbind(SAMPLE1, SAMPLE2)
# Plot spectra together
plt(
counts ~ eV | SampleID,
data = samples,
type = "l",
palette = "Dark 2",
main = "title"
)
# Dataframe can also be used with ggplot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment