diff --git a/modules/data.remote/inst/download_updated.R b/modules/data.remote/inst/download_updated.R new file mode 100644 index 00000000000..5075e8dc98f --- /dev/null +++ b/modules/data.remote/inst/download_updated.R @@ -0,0 +1,34 @@ +#DAAC_Set_Credential(replace = TRUE) + +# 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") diff --git a/modules/meta.analysis/inst/magic/extract_rows_traitID.R b/modules/meta.analysis/inst/magic/extract_rows_traitID.R new file mode 100644 index 00000000000..564c9b33464 --- /dev/null +++ b/modules/meta.analysis/inst/magic/extract_rows_traitID.R @@ -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) +} + + diff --git a/modules/meta.analysis/inst/magic/get_stats.R b/modules/meta.analysis/inst/magic/get_stats.R new file mode 100644 index 00000000000..071ce63f754 --- /dev/null +++ b/modules/meta.analysis/inst/magic/get_stats.R @@ -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) +} + + diff --git a/modules/meta.analysis/inst/magic/initialize_planting.R b/modules/meta.analysis/inst/magic/initialize_planting.R new file mode 100644 index 00000000000..badc6e9a613 --- /dev/null +++ b/modules/meta.analysis/inst/magic/initialize_planting.R @@ -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 + +} \ No newline at end of file diff --git a/modules/meta.analysis/inst/magic/main.R b/modules/meta.analysis/inst/magic/main.R new file mode 100644 index 00000000000..489ac7e48c1 --- /dev/null +++ b/modules/meta.analysis/inst/magic/main.R @@ -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) +} + diff --git a/modules/meta.analysis/inst/magic/run_program.R b/modules/meta.analysis/inst/magic/run_program.R new file mode 100644 index 00000000000..34dc3462a7f --- /dev/null +++ b/modules/meta.analysis/inst/magic/run_program.R @@ -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) + +} +