Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions modules/data.remote/inst/download_updated.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#DAAC_Set_Credential(replace = TRUE)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest putting this in a subdirectory named magic, as done for the files in meta.analysis -- even though there's only one file here it should help us keep track of where things came from.

Even better but obviously more effort would be to add a short README to each subdirectory -- future viewers will be grateful for even a couple sentences explaining whose scripts they are, what project they were part of / how they're used, and how finished they are.


# California bounding box is:
# up_lat <- 42.0095082699265845
# up_lon <- -124.4820168611238245
# low_lat <- 32.5288367369123748
# low_lon <- -114.1312224747231312

ul_lat <- 42.0095082699265845 # y = 4651894 in crs
ul_lon <- -124.4820168611238245 # x = 377279.7 in crs
lr_lat <- 32.5288367369123748 # y = 3633946 in crs
lr_lon <- -114.1312224747231312 # x = 1334269 in crs

from <- "2019-01-01"
to <- "2019-12-31"
doi <- "10.5067/HLS/HLSS30.002"
outdir <- "//projectnb/dietzelab/XinyuanJi/State_of_California_HLSS/2019_Fmask"
# SWIR - Landsat (B6&7), Sentinel (B11&12)
band <- "Fmask"
credential.folder <- "~/projectnb/XinyuanJi"
paths <- NASA_DAAC_download(ul_lat = ul_lat,
ul_lon = ul_lon,
lr_lat = lr_lat,
lr_lon = lr_lon,
ncore = 16,
from = from,
to = to,
outdir = outdir,
band = band,
credential.folder = credential.folder,
doi = doi,
just_path = F)

provider_conceptID <- NASA_CMR_finder("10.5067/HLS/HLSS30.002")
30 changes: 30 additions & 0 deletions modules/meta.analysis/inst/magic/extract_rows_traitID.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
library(readxl)
library(dplyr)

extract_rows_traitID <- function(excel_path, sheet_name = 1, trait_ID) {

#reads the excel sheet
data <- readxl::read_excel(excel_path, sheet = sheet_name, col_types = NULL)

#throws an error if TraitID is not within the sheet
if (!"TraitID" %in% colnames(data)) {
stop("Column TraitID not found within the excel sheet")
}

# finds the rows with the desired TraitID
matched_rows <- which(data$TraitID == trait_ID)

if (length(matched_rows) > 0) {
filtered_rows <- data[matched_rows, ]
} else {
filtered_rows <- data.frame()
message("No rows found with TraitID of ", trait_ID)
}


# df2 now has a `common_name` column right after `SpeciesName`

return (filtered_rows)
}


26 changes: 26 additions & 0 deletions modules/meta.analysis/inst/magic/get_stats.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
library(dplyr)

get_stats <- function(data, value_column, group_column, species_column = "SpeciesName", species_name) {

# Filter by species
data_filtered <- data %>%
filter(.data[[species_column]] == species_name)

# Convert value column to numeric
data_filtered[[value_column]] <- as.numeric(data_filtered[[value_column]])

# Summarize statistics
summary_df <- data_filtered %>%
group_by(across(all_of(group_column))) %>%
summarise(
mean_value = mean(.data[[value_column]], na.rm = TRUE),
sd = sd(.data[[value_column]], na.rm = TRUE),
n = sum(!is.na(.data[[value_column]])),
.groups = "drop"
) %>%
rename(TraitID = 1)

return(summary_df)
}


24 changes: 24 additions & 0 deletions modules/meta.analysis/inst/magic/initialize_planting.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
library(dplyr)

initialize_planting <- function(species_name) {

species_stats <- data.frame(
get_stats(planting_df, "OrigValueStr", "TraitID", "SpeciesName", species_name)
)
return(species_stats)

#calculate for each trait

#3441 leafC

#128 wood/stemC

#3450 rootC

#2005 fine-rootC

#1534 coarse-rootC

#output a table with the information

}
18 changes: 18 additions & 0 deletions modules/meta.analysis/inst/magic/main.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#this will be the main file where the entire program will be run
library(readxl)
library(dplyr)

source("extract_rows_traitID.R")

main <- function(files, trait_IDs, sheet_name = 1) {
combined_data <- data.frame()

for (file in files) {
for (id in trait_IDs) {
temp <- extract_rows_traitID(file, sheet_name = 1, trait_ID = id)
combined_data <- bind_rows(combined_data, temp)
}
}
return (combined_data)
}

35 changes: 35 additions & 0 deletions modules/meta.analysis/inst/magic/run_program.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#this will run the entire program

#example run: test_df1 <- run_program(time = "planting", species = "Rubus idaeus")

library(readxl)
library(dplyr)
library(taxize)

source("get_stats.R")

run_program <- function(time, species) {
#time is harvest/planting
#pool is part of plant
#species is species

temp_plant_df <- get_stats(master_data, value_column = "OrigValueStr", trait_column = "TraitID", species_column = "SpeciesName", species_name = species)

#need to implement if NA, return species with like traits

if (time == "planting") {
temp_plant_df <- temp_plant_df[temp_plant_df$TraitID %in% c("3441", "128", "2005", "1534"),]
}
else if (time == "harvest") {
temp_plant_df <- temp_plant_df[temp_plant_df$TraitID %in% c("3962", "470"),]

}

if (nrow(temp_plant_df) == 0) {
return(paste(species, "has nothing"))
}

return (temp_plant_df)

}

Loading