From 0c637492675537ca452f35f6a5c132a5e1e99c7e Mon Sep 17 00:00:00 2001 From: vanesm Date: Mon, 4 Nov 2024 10:30:42 -0500 Subject: [PATCH] combined libs/pixl work file so everything is in one place --- .../Assignment05/MatchingLIBSandPIXL.Rmd | 212 ++++++++++++++++++ 1 file changed, 212 insertions(+) create mode 100644 StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd diff --git a/StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd b/StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd new file mode 100644 index 0000000..2baafb8 --- /dev/null +++ b/StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd @@ -0,0 +1,212 @@ +--- +title: "Combining LIBS and PIXL Data" +author: "Margo VanEsselstyn" +date: "`r Sys.Date()`" +output: + pdf_document: + toc: yes + html_document: + toc: yes +subtitle: "DAR Mars" +--- + +```{r setup, include=FALSE} +# Set the default CRAN repository +local({r <- getOption("repos") + r["CRAN"] <- "http://cran.r-project.org" + options(repos=r) +}) + +if (!require("pandoc")) { + install.packages("pandoc") + library(pandoc) +} + +if (!require("rmarkdown")) { + install.packages("rmarkdown") + library(rmarkdown) +} +if (!require("tidyverse")) { + install.packages("tidyverse") + library(tidyverse) +} +if (!require("stringr")) { + install.packages("stringr") + library(stringr) +} + +if (!require("ggbiplot")) { + install.packages("ggbiplot") + library(ggbiplot) +} + +if(!require("knitr")){ + install.packages("knitr") + library(knitr) +} + +if(!require("cluster")){ + install.packages("cluster") + library(cluster) +} + +if(!require("ggtern")){ + install.packages("ggtern") + library(ggtern) +} + +if(!require("caret")){ + install.packages("caret") + library(caret) +} + +if(!require("geosphere")){ + install.packages("geosphere") + library(geosphere) +} + +knitr::opts_chunk$set(echo = TRUE) +``` + + +### Data Preparation + +Load in LIBS data +```{r, data01} +#load in LIBS data +libs.df <- readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/Data/supercam_libs_moc_loc.Rds") + +#Drop the standard deviation features, the sum of the percentages, +#the distance, and the total frequencies +libs.df <- libs.df %>% + select(!(c(distance_mm,Tot.Em.,SiO2_stdev,TiO2_stdev,Al2O3_stdev,FeOT_stdev, + MgO_stdev,Na2O_stdev,CaO_stdev,K2O_stdev,Total))) + +# Convert the points to numeric +libs.df$point <- as.numeric(libs.df$point) +libs.df[,6:13] <- sapply(libs.df[,6:13],as.numeric) + +#remove the scct/reference samples +libs.df<-libs.df%>% + filter(!(grepl("scct", target))) + +#add a column to indicate the nearest pixl +libs.df<-cbind(nearestpixl=0,libs.df) + +#make a dataframe of just the LIBS Lat/Long and target name and remove duplicates +libstargets.df<-libs.df[,c(1,3,4,5)] +libstargets.df<-distinct(libstargets.df) +``` + +Load in PIXL data + +```{r, data02} +#read in pixl data with lat/long +pixl.df<-readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/StudentData/pixl_sol_coordinates.Rds") + +#include only pixl metadata +pixl.df<-pixl.df %>% + select(c(1,2,19,20,22)) + +#convert Lat/Long to numeric +pixl.df$Lat <- as.numeric(pixl.df$Lat) +pixl.df$Long <- as.numeric(pixl.df$Long) + +#remove rows so we only have one sample per abrasion and remove atmospheric sample +pixl.df<-pixl.df[c(2,4,6,8,10,12,14,16),] +``` + + +### Combining datasets + +```{r} +distance=100 +distCosine(pixl.df[1,c(1,2)],libstargets.df[1,c(2,3)], r=3393169) +for(i in 1:nrow(pixl.df)) { + libstargets.df<-libstargets.df%>%mutate(nearestpixl = ifelse(distHaversine(pixl.df[i,c(1,2)],c(lat,lon),r=3393169),pixl.df[i,5], nearestpixl)) + +} +``` + +```{r} +libstargets.df<-cbind(libstargets.df,"Distance"=0,"Bellegrade"=0,"Dourbes"=0,"Quartier"=0,"Alfalfa"=0,"ThorntonGap"=0,"BerryHollow"=0,"Novarupta"=0,"UganikIsland"=0) +``` + +```{r} +#DistCosine(pixl.df[1,c(1,2)],libstargets.df[1,c(2,3)], r=3393169) + +for(i in 1:nrow(libstargets.df)) { + libstargets.df[i,c(6:13)]<-c(distHaversine(pixl.df[1,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[2,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[3,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[4,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[5,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[6,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[7,c(1,2)],libstargets.df[i,c(2,3)],r=3393169), + distHaversine(pixl.df[8,c(1,2)],libstargets.df[i,c(2,3)],r=3393169)) + + libstargets.df[i,1]<-which.min(libstargets.df[i,c(6:13)]) + libstargets.df[i,5]<-min(libstargets.df[i,c(6:13)]) +} + +libstargets.df$nearestpixl<-as.factor(libstargets.df$nearestpixl) + +levels(libstargets.df$nearestpixl)<-(c("Bellegrade","Dourbes","Quartier","Alfalfa","ThorntonGap","BerryHollow","Novarupta","UganikIsland")) +``` + + +```{r} +Bellegrade<-libstargets.df[libstargets.df$nearestpixl=="Bellegrade",]$target +Dourbes<-libstargets.df[libstargets.df$nearestpixl=="Dourbes",]$target +Quartier<-libstargets.df[libstargets.df$nearestpixl=="Quartier",]$target +Alfalfa<-libstargets.df[libstargets.df$nearestpixl=="Alfalfa",]$target +ThorntonGap<-libstargets.df[libstargets.df$nearestpixl=="ThorntonGap",]$target +BerryHollow<-libstargets.df[libstargets.df$nearestpixl=="BerryHollow",]$target +Novarupta<-libstargets.df[libstargets.df$nearestpixl=="Novarupta",]$target +UganikIsland<-libstargets.df[libstargets.df$nearestpixl=="UganikIsland",]$target +``` + +```{r} +meters=100 + +included.libs<-(libstargets.df%>% + filter(Distance% + filter(target %in% included.libs) + +libs.matrix <- libs.matrix[,c(5,7:14)] + +libs.tern <- as.data.frame(libs.matrix) %>% + mutate(x=(SiO2+Al2O3)/100,y=(FeOT+MgO)/100,z=(CaO+Na2O+K2O)/100) %>% + select(-c(SiO2,Al2O3,FeOT,MgO,CaO,Na2O,K2O,TiO2)) + +libs.tern<-cbind("Abrasion"=0,libs.tern) + +libs.tern<-libs.tern%>% + mutate(Abrasion = ifelse(target%in%Alfalfa,"Alfalfa", + ifelse(target %in% Bellegrade, "Belegrade", + ifelse(target %in% BerryHollow, "BerryHollow", + ifelse(target %in% Dourbes, "Dourbes", + ifelse(target %in% Novarupta, "Novarupta", + ifelse(target %in% Quartier, "Quartier", + ifelse(target %in% ThorntonGap, "ThorntonGap", + ifelse(target %in% UganikIsland, "UganikIsland",Abrasion))))))))) + +kabledf<-rbind("Distance (m)"=meters,"Targets"=length(included.libs),"Points"=nrow(libs.tern)) + +kable(kabledf) +``` + + +```{r} +ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) + + geom_point(data=libs.tern,aes(color=Abrasion,alpha=0.5)) + + theme_rgbw() + + labs(title=paste("Mars LIBS Data Within",distance,"meters of PIXL",sep=" "), + x="Si+Al", + y="Fe+Mg", + z="Ca+Na+K")+theme(legend.position="right") + + guides(alpha="none") +``` +