Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DAR-Mars-F24/StudentNotebooks/Assignment05/MatchingLIBSandPIXL.Rmd
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
264 lines (205 sloc)
8.23 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
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))) | |
#make a dataframe of just the LIBS Lat/Long and target name and remove duplicates | |
libstargets.df<-cbind("nearestpixl"=0,libs.df[,1:4]) | |
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} | |
# Create a new dataframe with the LIBS metatdata as well as features corresponding to each PIXL Abrasion | |
libstargets.df<-cbind(libstargets.df,"Distance"=0,"Bellegrade"=0,"Dourbes"=0,"Quartier"=0,"Alfalfa"=0,"ThorntonGap"=0,"BerryHollow"=0,"Novarupta"=0,"UganikIsland"=0) | |
``` | |
```{r} | |
#Calculate the distance between each LIBS target and each PIXL abrasion, then fill in the minimum distances | |
for(i in 1:nrow(libstargets.df)) { | |
libstargets.df[i,c(7:14)]<-c(distHaversine(pixl.df[1,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[2,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[3,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[4,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[5,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[6,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[7,c(1,2)],libstargets.df[i,c(3,4)],r=3393169), | |
distHaversine(pixl.df[8,c(1,2)],libstargets.df[i,c(3,4)],r=3393169)) | |
libstargets.df[i,1]<-which.min(libstargets.df[i,c(7:14)]) | |
libstargets.df[i,6]<-min(libstargets.df[i,c(7:14)]) | |
} | |
libstargets.df$nearestpixl<-as.factor(libstargets.df$nearestpixl) | |
levels(libstargets.df$nearestpixl)<-(c("Bellegrade","Dourbes","Quartier","Alfalfa","ThorntonGap","BerryHollow","Novarupta","UganikIsland")) | |
``` | |
```{r} | |
#Create vectors of LIBS targets corresponding to each PIXL Abrasion | |
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} | |
#Add the nearest Abrasion to the LIBS data | |
libs.pixl.merged <- cbind("Abrasion"=0,libs.df) | |
libs.pixl.merged<-libs.pixl.merged%>% | |
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, "Uganik Island",Abrasion))))))))) | |
targetdistance<-libstargets.df[,2:6] | |
libs.pixl.merged<-merge(libs.pixl.merged,targetdistance,by=c("target","sol","lat","lon"),all.x=T) | |
``` | |
```{r} | |
#Add back some pixl features | |
pix<-pixl.df[,c(1,2,4,5)] | |
libs.pixl <- merge(libs.pixl.merged, pix, by.x="Abrasion",by.y="abrasion",all.x=TRUE) | |
#rename and reorder columns | |
libs.pixl<-cbind("LIBS.Target"=libs.pixl$target,libs.pixl[,4:5],"LIBS.Sol"=libs.pixl$sol,"LIBS.Point"=libs.pixl$point,"Distance"=libs.pixl$Distance,"PIXL.Abrasion"=libs.pixl$Abrasion,libs.pixl[,16:18],libs.pixl[,7:14]) | |
colnames(libs.pixl)<-c("LIBS.Target","LIBS.Lat","LIBS.Lon","LIBS.Sol","LIBS.Point", "Distance","PIXL.Abrasion","PIXL.Lat","PIXL.Lon","PIXL.Campaign","LIBS.SiO2","LIBS.TiO2","LIBS.Al2O3","LIBS.FeOT","LIBS.MgO","LIBS.CaO","LIBS.Na2O","LIBS.K2O") | |
``` | |
```{r} | |
#setwd("~/DAR-Mars-F24/StudentData") | |
#saveRDS(libs.pixl,"PIXL_LIBS_Combined.Rds") | |
``` | |
The libs.pixl dataframe is now saved to PIXL_LIBS_Combined.Rds, which can be found in the StudentData folder. | |
### Ternary Diagram | |
Example of creating a ternary plot filtering the combined LIBS and PIXL data | |
```{r} | |
libs.tern <- libs.pixl %>% | |
mutate(x=(LIBS.SiO2+LIBS.Al2O3)/100,y=(LIBS.FeOT+LIBS.MgO)/100,z=(LIBS.CaO+LIBS.Na2O+LIBS.K2O)/100) | |
libs.tern<-libs.tern[,c(6,7,19:21)] | |
``` | |
```{r} | |
meters=7 | |
ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) + | |
geom_point(data=subset(libs.tern,Distance<=meters),aes(color=PIXL.Abrasion,alpha=0.5)) + | |
theme_rgbw() + | |
labs(title=paste("Mars LIBS Data Within",meters,"meters of PIXL",sep=" "), | |
x="Si+Al", | |
y="Fe+Mg", | |
z="Ca+Na+K")+theme(legend.position="right") + | |
guides(alpha="none") | |
meters=100 | |
ggtern(libs.tern, ggtern::aes(x=x,y=y,z=z)) + | |
geom_point(data=subset(libs.tern,Distance<=meters),aes(color=PIXL.Abrasion,alpha=0.5)) + | |
theme_rgbw() + | |
labs(title=paste("Mars LIBS Data Within",meters,"meters of PIXL",sep=" "), | |
x="Si+Al", | |
y="Fe+Mg", | |
z="Ca+Na+K")+theme(legend.position="right") + | |
guides(alpha="none") | |
``` | |
### Line plot | |
Example of how to add the pixl data as extra rows to the LIBS data, so that it can be graphed on the same plot | |
```{r} | |
#remove certain features from the combined pixl libs dataset | |
libs.matrix <- libs.pixl[,c(7,1,11,13:18,12)] | |
#rename columns | |
libs.matrix<-cbind(libsorpixl=1,libs.matrix) | |
colnames(libs.matrix)<-c("libsorpixl","Abrasion","targets+names","SiO2","Al2O3","FeOT","MgO","CaO","Na2O","K2O","TiO2") | |
``` | |
```{r, data03} | |
#read in pixl data with lat/long | |
pixl.df<-readRDS("/academics/MATP-4910-F24/DAR-Mars-F24/StudentData/pixl_sol_coordinates.Rds") | |
pixl.df<-pixl.df %>% | |
select(c(5:8,12:14,17,19,18,22)) | |
#reorder pixl columns so that it matches libs data organization | |
pixl.df<-pixl.df[,c(11,10,4,3,8,2,6,1,5,7)] | |
#remove atmospheric sample | |
pixl.df<-pixl.df[2:16,] | |
pixl.df<-cbind(libsorpixl=0,pixl.df) | |
``` | |
```{r} | |
#rename pixl columns and combine dataframes | |
colnames(pixl.df)<-colnames(libs.matrix) | |
pixllibs.df<-rbind(pixl.df,libs.matrix) | |
``` |