diff --git a/.gitignore b/.gitignore index c9191df..3a2f519 100644 --- a/.gitignore +++ b/.gitignore @@ -35,3 +35,5 @@ vignettes/*.pdf ConnectToOracle.R *.log /shapefiles/archive +/code/ftp.R + diff --git a/code/2022-05-29_daily.pdf b/code/2022-05-29_daily.pdf deleted file mode 100644 index 6bd40e3..0000000 Binary files a/code/2022-05-29_daily.pdf and /dev/null differ diff --git a/code/2022-06-11_daily.pdf b/code/2022-06-11_daily.pdf deleted file mode 100644 index 6bd40e3..0000000 Binary files a/code/2022-06-11_daily.pdf and /dev/null differ diff --git a/code/functions.R b/code/functions.R index 30048a3..de2e7c5 100644 --- a/code/functions.R +++ b/code/functions.R @@ -38,6 +38,7 @@ PKG <- c( "janitor", # "tinytex", + "RCurl", # tidyverse, "broom", @@ -187,13 +188,13 @@ make_varplot_wrapper <- function(maxyr, janitor::clean_names() %>% # dplyr::mutate(var = all_of(var0)) %>% dplyr::rename(#"var" = all_of(var), - "SRVY" = "srvy", - "vessel_shape" = "vessel") %>% + "SRVY" = "srvy", + "vessel_shape" = "vessel") %>% dplyr::filter( SRVY %in% SRVY1) dat$var <- as.numeric(unlist(dat[,var])) - + dat <- dat %>% dplyr::select(SRVY, stratum, station, var, date, vessel_shape) @@ -217,10 +218,10 @@ make_varplot_wrapper <- function(maxyr, for (ii in 1:length(unique(dat$SRVY))){ temp <- unique(dat$SRVY)[ii] dat_survreg$reg_dates[dat_survreg$SRVY == temp] <- paste0(#"\n(", - format(x = min(as.Date(dat$date[dat$SRVY == temp]), na.rm = TRUE), "%b %d"), - " - ", - format(x = max(as.Date(dat$date[dat$SRVY == temp]), na.rm = TRUE), "%b %d")#, ")" - ) + format(x = min(as.Date(dat$date[dat$SRVY == temp]), na.rm = TRUE), "%b %d"), + " - ", + format(x = max(as.Date(dat$date[dat$SRVY == temp]), na.rm = TRUE), "%b %d")#, ")" + ) } dat <- dat %>% dplyr::left_join( @@ -300,41 +301,41 @@ make_varplot_wrapper <- function(maxyr, ### Daily -------------------------------------------------------------------- if (plot_daily) { # Daily plot - file_end = "daily" - make_figure(SRVY = SRVY, - dat = dat, - var_breaks = var_breaks, - plot_title = plot_title, - plot_subtitle = plot_subtitle, - legend_title = legend_title, - dates0 = dates0, - survey_area = survey_area, - file_end = file_end, - dir_wd = dir_wd, - dir_out = dir_out, - dir_googledrive_upload = dir_googledrive_upload, - make_gifs = TRUE, - data_source = data_source, - show_planned_stations = show_planned_stations, - height = height) + file_end = "daily" + make_figure(SRVY = SRVY, + dat = dat, + var_breaks = var_breaks, + plot_title = plot_title, + plot_subtitle = plot_subtitle, + legend_title = legend_title, + dates0 = dates0, + survey_area = survey_area, + file_end = file_end, + dir_wd = dir_wd, + dir_out = dir_out, + dir_googledrive_upload = dir_googledrive_upload, + make_gifs = TRUE, + data_source = data_source, + show_planned_stations = show_planned_stations, + height = height) } ### Mean --------------------------------------------------------------------- dat <- dat %>% dplyr::mutate(reg_lab = paste0(region_long, "\n ")) - # googledrive::drive_mkdir(name = "anom", - # path = dir_googledrive_upload, - # overwrite = FALSE) - - dir_googledrive_upload0 <- googledrive::drive_ls(path = dir_googledrive_upload) %>% - dplyr::filter(name == "anom") %>% - dplyr::select("id") %>% - unlist() %>% - googledrive::as_id() - + # googledrive::drive_mkdir(name = "anom", + # path = dir_googledrive_upload, + # overwrite = FALSE) + + # dir_googledrive_upload0 <- googledrive::drive_ls(path = googledrive::as_id(dir_googledrive_upload)) %>% + # dplyr::filter(name == "anom") %>% + # dplyr::select("id") %>% + # unlist() %>% + # googledrive::as_id() + if (plot_mean) { - + dat <- dat %>% # Mean plot dplyr::mutate(var = mean) file_end <- "mean" @@ -422,14 +423,13 @@ make_grid_wrapper<-function(maxyr, dates0 <- "none" show_planned_stations = FALSE make_gifs = FALSE - show_planned_stations = FALSE - + SRVY1 <- SRVY if (SRVY == "BS") { SRVY1 <- c("EBS", "NBS") } - plot_title <- "Survey Grid" + plot_title <- ifelse(SRVY == "AI", "Survey Region", "Survey Grid") legend_title <- "" if (data_source == "gd") { # Temperature data from google drive @@ -604,12 +604,16 @@ make_figure <- function( iterate <- 1 } else if (dates0 == "all") { iterate <- 1:length(date_entered)# if you want to run all of plots for each date_entered: - if (sum(is.na(dat$var))!=0 & show_planned_stations){ + if (sum(is.na(dat$var))!=0 & # if the survey is not yet complete + show_planned_stations & # if we plan to show planned stations + sum(is.na(dat$var) & !is.na(dat$vessel_shape))>0) { # and if there are planned stations to show iterate <- iterate[-length(iterate)] } } else if (dates0 == "latest") { iterate <- length(date_entered) # if you want to just run todays/a specific date: - if (sum(is.na(dat$var))!=0 & show_planned_stations) { + if (sum(is.na(dat$var))!=0 & # if the survey is not yet complete + show_planned_stations & # if we plan to show planned stations + sum(is.na(dat$var) & !is.na(dat$vessel_shape))>0) { # and if there are planned stations to show iterate <- iterate-1 } } else { # if you want to run a specific date @@ -634,24 +638,32 @@ make_figure <- function( max_date <- date_entered[i] print(max_date) + next_date <- ifelse(date_entered[i]==date_entered[length(date_entered)], + as.character(as.Date(date_entered[i])+1), + date_entered[i+1]) # only use dates including this date and before this date dat_plot$var[as.Date(dat_plot$date)>as.Date(max_date)]<-NA grid_stations_plot$var_bin[as.Date(grid_stations_plot$date)>as.Date(max_date)]<-NA - # only use dates including 1 days before this date and before this date, so we can see the planned progression + # only use dates including the next day before this date and before this date, so we can see the planned progression # if (date_entered[i] != date_entered[length(date_entered)]) { - dat_plot$vessel_shape[as.Date(dat_plot$date)>as.Date(max_date)+1]<-NA - dat_plot$date[as.Date(dat_plot$date)>as.Date(max_date)+1]<-NA - grid_stations_plot$vessel_shape[as.Date(grid_stations_plot$date) > as.Date(max_date)+1]<-NA - grid_stations_plot$date[as.Date(grid_stations_plot$date) > as.Date(max_date)+1]<-NA + dat_plot$vessel_shape[as.Date(dat_plot$date)>as.Date(next_date)]<-NA + dat_plot$date[as.Date(dat_plot$date)>as.Date(next_date)]<-NA + grid_stations_plot$vessel_shape[as.Date(grid_stations_plot$date) > as.Date(next_date)]<-NA + grid_stations_plot$date[as.Date(grid_stations_plot$date) > as.Date(next_date)]<-NA # separate out the data for the temperature and planned stations if there are planned stations listed - if (show_planned_stations & - sum(is.na(dat_plot$var) & !is.na(dat_plot$vessel_shape))>0) { - + # if (show_planned_stations & + # sum(is.na(dat_plot$var) & !is.na(dat_plot$vessel_shape))>0) { + if (#sum(is.na(dat$var))!=0 & # if the survey is not yet complete + show_planned_stations & # if we plan to show planned stations + sum(is.na(dat_plot$var) & !is.na(dat_plot$vessel_shape))>0) { # and if there are any planned stations to show + # planned stations loc <- dat_plot %>% - dplyr::filter(is.na(var) & !is.na(vessel_shape)) %>% + dplyr::filter(is.na(var) & + !is.na(vessel_shape) & + as.Date(date) == as.Date(next_date)) %>% dplyr::mutate(planned = "Y") dat_planned <- as(grid_stations_plot, 'Spatial') @@ -882,75 +894,75 @@ make_figure <- function( ### Aleutian Islands ----------------------------------------------------------- # if (as.character(dates0[1]) == "none") { - # temp <-survey_area$place.labels[survey_area$place.labels$type == "bathymetry",] - gg <- gg + - ggplot2::geom_sf(data = survey_area$survey.grid1, - # mapping = aes(colour = survey_area$survey.grid$region, - # fill = survey_area$survey.grid$region), - colour = ifelse((as.character(dates0[1]) == "none"), "grey20", "grey50"), - size = ifelse((as.character(dates0[1]) == "none"), .05, .02), - show.legend = FALSE) #+ - # geom_sf(data = survey_area$bathymetry) + - # guides(colour = guide_legend(override.aes = list(fill = survey_area$survey.area$survey_reg_col))) # survey regions - # if (nrow(temp)>0) { - # gg <- gg + - # - # # geom_text(data = temp, mapping = aes(x = x, y = y, label = lab), - # # size = 4, hjust=0, vjust=1, fontface=2, angle = 90) - # annotate(geom = "text", x = temp$x, y = temp$y, label = temp$lab, - # color = "darkblue", fontface="bold", angle = 0) - # } + # temp <-survey_area$place.labels[survey_area$place.labels$type == "bathymetry",] + gg <- gg + + ggplot2::geom_sf(data = survey_area$survey.grid1, + # mapping = aes(colour = survey_area$survey.grid$region, + # fill = survey_area$survey.grid$region), + colour = ifelse((as.character(dates0[1]) == "none"), "grey20", "grey50"), + size = ifelse((as.character(dates0[1]) == "none"), .05, .02), + show.legend = FALSE) #+ + # geom_sf(data = survey_area$bathymetry) + + # guides(colour = guide_legend(override.aes = list(fill = survey_area$survey.area$survey_reg_col))) # survey regions + # if (nrow(temp)>0) { + # gg <- gg + + # + # # geom_text(data = temp, mapping = aes(x = x, y = y, label = lab), + # # size = 4, hjust=0, vjust=1, fontface=2, angle = 90) + # annotate(geom = "text", x = temp$x, y = temp$y, label = temp$lab, + # color = "darkblue", fontface="bold", angle = 0) + # } # } # now we build a plot list - lapply(unique(grid_stations_plot$region), function(x) { - # x <- unique(grid_stations_plot$region) - grid_stations_plot1 <- grid_stations_plot - grid_stations_plot1$region <- factor(grid_stations_plot1$region) - grid_stations_plot1<-grid_stations_plot1[grid_stations_plot1$region == x,] - - gg1 <- gg - - if (as.character(dates0[1]) != "none") { # If you are using any data from temp data # Add temperature squares - - gg1 <- gg1 + - ggplot2::geom_sf(data = grid_stations_plot1, - aes(fill = var_bin), - colour = "black", # "grey20" - size = .05, - show.legend = FALSE) + - ggplot2::scale_fill_manual(name = legend_title, - values = var_color, - labels = var_labels, - drop = F, - na.translate = F) - } - gg1 <- gg2 <- gg1 + - ggspatial::coord_sf( - xlim = c(extent(grid_stations_plot1)[1:2]), - ylim = c(extent(grid_stations_plot1)[3:4])) + - ggtitle(x) + - ggsn::scalebar(data = grid_stations_plot1, - location = ifelse(x == "Western Aleutians", "topright", "topleft"), - dist = 25, - dist_unit = "nm", - transform = FALSE, - st.dist = 0.06, - border.size = .25, - height = 0.03, - st.bottom = TRUE, - st.size = 3, - model = survey_area$crs) + - theme( - legend.position = "none", - plot.title = element_text(size = 10, face = "bold"), - # axis.text = element_text(size=9), - axis.title = element_blank(), - plot.margin=unit(c(0,0,0,0), "cm")) - - gg1 + lapply(unique(grid_stations_plot$region), function(x) { + # x <- unique(grid_stations_plot$region) + grid_stations_plot1 <- grid_stations_plot + grid_stations_plot1$region <- factor(grid_stations_plot1$region) + grid_stations_plot1<-grid_stations_plot1[grid_stations_plot1$region == x,] + + gg1 <- gg + + if (as.character(dates0[1]) != "none") { # If you are using any data from temp data # Add temperature squares - }) -> region_list + gg1 <- gg1 + + ggplot2::geom_sf(data = grid_stations_plot1, + aes(fill = var_bin), + colour = "black", # "grey20" + size = .05, + show.legend = FALSE) + + ggplot2::scale_fill_manual(name = legend_title, + values = var_color, + labels = var_labels, + drop = F, + na.translate = F) + } + gg1 <- gg2 <- gg1 + + ggspatial::coord_sf( + xlim = c(extent(grid_stations_plot1)[1:2]), + ylim = c(extent(grid_stations_plot1)[3:4])) + + ggtitle(x) + + ggsn::scalebar(data = grid_stations_plot1, + location = ifelse(x == "Western Aleutians", "topright", "topleft"), + dist = 25, + dist_unit = "nm", + transform = FALSE, + st.dist = 0.06, + border.size = .25, + height = 0.03, + st.bottom = TRUE, + st.size = 3, + model = survey_area$crs) + + theme( + legend.position = "none", + plot.title = element_text(size = 10, face = "bold"), + # axis.text = element_text(size=9), + axis.title = element_blank(), + plot.margin=unit(c(0,0,0,0), "cm")) + + gg1 + + }) -> region_list # var legend if (as.character(dates0[1]) != "none") { # If you are using any data from temp data # Add temperature squares @@ -973,8 +985,8 @@ make_figure <- function( axis.title = element_blank(), plot.margin=unit(c(0,0,0,0), "cm"), axis.text = element_text(size=5)) - } - + } + # } # inset map gg_insert <- ggplot() + geom_sf(data = survey_area$akland, fill = "black", color = NA) + # AK Map (thanks {akgfmaps}!) @@ -1015,24 +1027,42 @@ make_figure <- function( gg_insert <- gg_insert + geom_sf(data = poly, fill = NA, color = "black", size = .5)#+ # geom_text(mapping = aes(x = mean(a[1:2]), y = a[4]+10000, label = x)) - } gg_insert <- gg_insert + geom_text(data = bb, - mapping = aes(x = x, y = y+200000, label = lab), - size = 3) + + mapping = aes(x = x, y = y+ifelse(file_end == "grid", 100000, 200000), label = lab), + size = ifelse(file_end != "grid", 3, 5)) + ggspatial::coord_sf( xlim = c(extent(grid_stations_plot)[1:2]), ylim = c(extent(grid_stations_plot)[3], extent(grid_stations_plot)[4]+350000)) + ggtitle(gsub(pattern = "\n", replacement = " ", x = unique(survey_area$survey.area$reg_lab), fixed = TRUE)) + - theme_minimal() + - theme( - panel.border = element_rect(colour = "grey50", fill=NA, size=1), - plot.title = element_text(size = 8, face = "bold"), - axis.text.y = element_blank(), - axis.title = element_blank(), - plot.margin=unit(c(0,0,0,0), "cm") ) + theme_minimal() # + + # theme( + # panel.border = element_rect(colour = "grey50", fill=NA, size=1), + # plot.title = element_text(size = 8, face = "bold"), + # axis.text.y = element_blank(), + # axis.title = element_blank(), + # plot.margin=unit(c(0,0,0,0), "cm") ) + + if (file_end == "grid") { + gg_insert <- gg_insert + + theme( + panel.border = element_rect(colour = "grey50", fill=NA, size=1), + plot.title = element_text(size = 20, face = "bold"), + axis.text = element_text(size = 9), + axis.title = element_text(size = 14)#, + # plot.margin=unit(c(0.2,0.2,0.2,0.2), "cm") + ) + } else { + gg_insert <- gg_insert + + theme( + panel.border = element_rect(colour = "grey50", fill=NA, size=1), + plot.title = element_text(size = 8, face = "bold"), + axis.text.y = element_blank(), + axis.title = element_blank(), + plot.margin=unit(c(0,0,0,0), "cm") ) + } # put all plot peices together title_row <- cowplot::ggdraw() + @@ -1078,22 +1108,26 @@ make_figure <- function( gg1 <- cowplot::plot_grid( region_list[[1]], region_list[[2]], cowplot::get_legend(legend_temp), ncol = 3, nrow = 1, greedy = TRUE, rel_widths = c(1.1, 1.5, 0.4)) + # } else { + # gg1 <- cowplot::plot_grid( + # region_list[[1]], region_list[[2]], NULL, + # ncol = 3, nrow = 1, greedy = TRUE, rel_widths = c(1.1, 1.5, 0.4)) + # } + + gg2 <- cowplot::plot_grid( + # region_list[[1]], region_list[[2]], + region_list[[3]], region_list[[4]], gg_insert, + ncol = 3, nrow = 1, greedy = TRUE, rel_widths = c(1.25, 0.75, 0.6)) + + draw_label("Longitude", x = 0.4, y = 0, vjust = -0.5, angle = 0) + + draw_label("Latitude", x = 0, y = 0.75, vjust = 1.5, angle = 90) + + gg <- cowplot::plot_grid(header_row, gg1, gg2, + ncol = 1, nrow = 3, greedy = TRUE, rel_heights = c(0.4, 1, 1)) } else { - gg1 <- cowplot::plot_grid( - region_list[[1]], region_list[[2]], NULL, - ncol = 3, nrow = 1, greedy = TRUE, rel_widths = c(1.1, 1.5, 0.4)) + gg <- cowplot::plot_grid(header_row, gg_insert, + ncol = 1, nrow = 2, greedy = TRUE, rel_heights = c(0.4, 2)) } - gg2 <- cowplot::plot_grid( - # region_list[[1]], region_list[[2]], - region_list[[3]], region_list[[4]], gg_insert, - ncol = 3, nrow = 1, greedy = TRUE, rel_widths = c(1.25, 0.75, 0.6)) + - draw_label("Longitude", x = 0.4, y = 0, vjust = -0.5, angle = 0) + - draw_label("Latitude", x = 0, y = 0.75, vjust = 1.5, angle = 90) - - gg <- cowplot::plot_grid(header_row, gg1, gg2, - ncol = 1, nrow = 3, greedy = TRUE, rel_heights = c(0.4, 1, 1)) - } filename0 <- paste0(ifelse((as.character(dates0[1]) == "none" | file_end == "mean"), @@ -1102,30 +1136,32 @@ make_figure <- function( ifelse(file_end=="", "", paste0("_", file_end))) ### PNG ------------------------------------------------------------------------- - ggsave(filename = paste0(filename0,'.png'), + ggsave(filename = paste0(filename0,'.png'), path = dir_out, height = height, width = width, plot = gg, - dpi = 320, - device = "png") # pdfs are great for editing later + dpi = 320, + bg = "white", + device = "png") ### PDF ------------------------------------------------------------------------- # Create main PDF - if (file_end %in% c("grid", "daily")){ + if (file_end %in% c("grid", "daily", "mean")){ rmarkdown::render(paste0(dir_wd, "/code/template.Rmd"), output_dir = dir_out, output_file = paste0(filename0, ".pdf")) file.remove(list.files(path = paste0(dir_wd, "/code/"), pattern = ".log", full.names = TRUE)) - } else if (file_end %in% c("anom", "mean")){ + } else if (file_end %in% c("anom")){ ggsave(filename = paste0(filename0,'.pdf'), path = dir_out, height = height, width = width, plot = gg, dpi = 320, + bg = "white", device ="pdf") # pdfs are great for editing later } @@ -1136,13 +1172,15 @@ make_figure <- function( if (length(list.files(path = dir_out, pattern = paste0(filename0, "_bind.pdf"))) != 0) { file.remove(paste0(dir_out, filename0, "_bind.pdf")) } - - if (date_entered[1] == date_entered[i]) { # length(date_entered) == 1 | + + if (as.character(date_entered[1]) == as.character(date_entered[i])) { # qpdf::pdf_combine(input = c(paste0(dir_out, filename0,'.pdf'), ifelse(file.exists(paste0(dir_out,'_grid.pdf')), paste0(dir_out,'_grid.pdf'), "")), output = c(paste0(dir_out, filename0, "_bind.pdf"))) } else { - temp <- strsplit(x = list.files(path = dir_out, pattern = paste0("_", file_end, "_bind.pdf")), split = "_") + temp <- list.files(path = dir_out, pattern = paste0("_", file_end, "_bind.pdf")) + temp <- temp[!grepl(pattern = "current", x = temp)] + temp <- strsplit(x = temp, split = "_") temp <- as.Date(sort(sapply(temp,"[[",1))) temp <- max(temp[as.Date(temp) < as.Date(max_date)]) @@ -1167,7 +1205,15 @@ make_figure <- function( # } } - ### Upload to google drive ------------------------------------------------------ + ### rename "current" plots for easy finding ------------------------------------ + # if (i == iterate[length(iterate)] & file_end %in% c("anom", "daily")) { + temp <- list.files(path = dir_out, pattern = filename0, full.names = TRUE) + for (iiii in 1:length(temp)){ + file.copy(from = temp[iiii], overwrite = TRUE, to = gsub(pattern = max_date, replacement = "current", x = temp[iiii])) + } + # } + + ### Upload to google drive ------------------------------------------------------ # if (file_end != "anom") { # temp <- googledrive::drive_ls(path = dir_googledrive_upload, recursive = FALSE) @@ -1183,10 +1229,15 @@ make_figure <- function( if (!(is.null(dir_googledrive_upload))) { temp <- list.files(path = dir_out, pattern = filename0, full.names = TRUE) + #if (i == iterate[length(iterate)] & file_end %in% c("anom", "daily")) { + temp <- c(temp, + list.files(path = dir_out, pattern = sub(pattern = max_date, replacement = "current", x = filename0), full.names = TRUE)) + # } for (iii in 1:length(temp)) { drive_upload( media = temp[iii], - path = dir_googledrive_upload, + path = googledrive::as_id(dir_googledrive_upload), + #path = ifelse(class(dir_googledrive_upload)[1] == "drive_id", dir_googledrive_upload, googledrive::as_id(dir_googledrive_upload)), overwrite = TRUE) } } @@ -1216,6 +1267,7 @@ make_figure_gif<-function(file_end, full.names = TRUE) temp <- strsplit(x = list.files(path = dir_out, pattern = paste0("_", file_end, ".gif")), split = "_") + temp <- temp[!grepl(pattern = "current", x = temp)] if (length(temp) != 0) { temp <- as.Date(sort(sapply(temp,"[[",1))) diff --git a/code/run.R b/code/run.R index 5f0466d..7832f97 100644 --- a/code/run.R +++ b/code/run.R @@ -6,14 +6,18 @@ #' --------------------------- # Knowns ----------------------------------------------------------------------- -googledrive_dl <- TRUE -# maxyr <- 2022 #CHANGE +maxyr <- 2022 +data_source <- "gd" # google drive +dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) +shapef <- FALSE #set to TRUE to run make_grid_wrapper to run shapefiles for EBS and AI +var <- "bt" + googledrive_dl <- TRUE dir_googledrive_log <- "https://docs.google.com/spreadsheets/d/16CJA6hKOcN1a3QNpSu3d2nTGmrmBeCdmmBCcQlLVqrE/edit#gid=315914502" -dir_googledrive_upload_bs = "https://drive.google.com/drive/folders/1vWza36Dog0SpZLcTN22wD-iCEn6ooGCM" +#dir_googledrive_upload_bs = "https://drive.google.com/drive/folders/1vWza36Dog0SpZLcTN22wD-iCEn6ooGCM" dir_googledrive_upload_ai = "https://drive.google.com/drive/folders/1SeNOAh5-muQ2BDgOHWZWwYIoLl68DHWX" -# dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" +#dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" # The surveys this script will be covering dat_survreg <- data.frame(reg_shapefile = "EBS_SHELF", @@ -55,103 +59,33 @@ googledrive::drive_auth() 1 # SOURCE SUPPORT SCRIPTS ------------------------------------------------------- -## Actually we cant use the here package - it actually causes issues with the tasks scheduler, -## which has no concept of a project root folder. -dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" -dir_wd <-"C:/Users/caitlin.akselrud/Work/survey-live-temperature-map/" +## Actually we cant use the here package, here - it actually causes issues with +## the tasks scheduler, which has no concept of a project root folder. +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" #dir_wd <- "G:/EBSother/GAPsurveyTemperatureMap/" -# dir_wd <-"C:/Users/emily.markowitz/Work/Projects/GAPSurveyTemperatureMap/" -# dir_wd <- paste0(getwd(), "/") - -sink(file=paste0(dir_wd , "/output/", Sys.Date(), "_log.txt")) +#dir_wd <- paste0(getwd(), "/") +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +dir_wd <- "C:/Users/caitlin.akselrud/Work/survey-live-temperature-map/" source(file = paste0(dir_wd,"code/functions.R")) # source(file = paste0(dir_wd, "code/data_dl.R")) # you don't unnecessarily run this each time source(file = paste0(dir_wd, "code/data.R")) +if (googledrive_dl == TRUE) { + source(file = paste0(dir_wd, "code/ftp.R")) # removed in gitignore - ask for premission +} # Map -------------------------------------------------------------------------- - -## NBS + EBS Maps -------------------------------------------------------------- -maxyr <- 2022 #CHANGE -data_source <- "gd" # google drive -SRVY <- "BS" -plot_subtitle <- "NOAA Fisheries Bering Sea Bottom Trawl Survey" -dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_bs) -# dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_test) -region_akgfmaps = "bs.all" -survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") - -# Daily -var = "bt" -dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) -show_planned_stations <- TRUE -plot_anom <- FALSE -survey_area$survey.grid <- survey_area$survey.grid %>% - sf::st_transform(x = ., survey_area$crs$input) %>% - dplyr::rename(station = STATIONID) %>% - sp::merge(x = ., - y = haul %>% - dplyr::rename(station = stationid) %>% - dplyr::select(station, stratum) %>% - dplyr::distinct(), - all.x = TRUE) %>% - dplyr::mutate(region = "Bering Sea") -survey_area$place.labels$y[survey_area$place.labels$lab == "200 m"] <- -60032.7 - -# make_grid_wrapper(maxyr = maxyr, # Blank grid plot -# SRVY = SRVY, -# haul = haul, -# dat_survreg = dat_survreg, -# dir_googledrive_upload = dir_googledrive_upload, -# survey_area = survey_area, -# data_source = data_source, -# plot_subtitle = plot_subtitle, -# dir_wd = dir_wd) -make_varplot_wrapper(maxyr = maxyr, # Daily plot - SRVY = SRVY, - haul = haul, - dat_survreg = dat_survreg, - var = var, - dir_googledrive_upload = dir_googledrive_upload, - dates0 = dates0, - survey_area = survey_area, - plot_subtitle = plot_subtitle, - show_planned_stations = show_planned_stations, - data_source = data_source, - plot_anom = plot_anom, - dir_wd = dir_wd) -# make_varplot_wrapper(maxyr = maxyr, # Anom and mean plot -# SRVY = SRVY, -# haul = haul, -# dat_survreg = dat_survreg, -# var = var, -# dir_googledrive_upload = dir_googledrive_upload, -# dates0 = "latest", -# survey_area = survey_area, -# plot_subtitle = plot_subtitle, -# show_planned_stations = show_planned_stations, -# data_source = data_source, -# plot_daily = FALSE, -# plot_anom = FALSE, # anom plot here doesnt make sense to print until the end -# plot_mean = TRUE, -# dir_wd = dir_wd) # ## AI -------------------------------------------------------------------------- -maxyr <- 2022 #CHANGE -data_source <- "gd" # google drive SRVY <- "AI" -plot_anom <- FALSE -plot_subtitle = "NOAA Fisheries Aleutian Islands Bottom Trawl Survey" region_akgfmaps = "ai" -dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_ai) -#dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_test) -var = "bt" -dates0 <- "latest" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) +plot_subtitle = "NOAA Fisheries Aleutian Islands Bottom Trawl Survey" +dir_googledrive_upload <- (dir_googledrive_upload_ai) +plot_anom <- FALSE show_planned_stations <- FALSE survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") - survey_area$survey.grid <- rgdal::readOGR(dsn = paste0(dir_wd, '/shapefiles/'),# Prepare map objects - layer = "aigrid_trawable_thru2018_Emily", - verbose=F) %>% + layer = "aigrid_trawable_thru2018_Emily", + verbose=F) %>% sp::spTransform(x = ., CRS(survey_area$crs$input)) %>% st_as_sf(x = .) %>% dplyr::rename(station = ID, @@ -169,33 +103,36 @@ survey_area$survey.grid <- rgdal::readOGR(dsn = paste0(dir_wd, '/shapefiles/'),# TRUE ~ region)) %>% dplyr::select(SRVY, stratum, region) %>% dplyr::distinct(), - all.x = TRUE) %>% # , duplicateGeoms = TRUE + all.x = TRUE) %>% dplyr::arrange(region) survey_area$survey.grid1 <- survey_area$survey.grid -# make_grid_wrapper(maxyr = maxyr, # Blank grid plot -# SRVY = SRVY, -# haul = haul, -# dat_survreg = dat_survreg, -# dir_googledrive_upload = dir_googledrive_upload, -# survey_area = survey_area, -# data_source = data_source, -# plot_subtitle = plot_subtitle, -# dir_wd = dir_wd) +if(shapef == TRUE) +{ + make_grid_wrapper(maxyr = maxyr, # Blank grid plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + dir_googledrive_upload = dir_googledrive_upload, + survey_area = survey_area, + data_source = data_source, + plot_subtitle = plot_subtitle, + dir_wd = dir_wd) +} make_varplot_wrapper(maxyr = maxyr, # Daily plot - SRVY = SRVY, - haul = haul, - dat_survreg = dat_survreg, - var = var, - dir_googledrive_upload = dir_googledrive_upload, - dates0 = dates0, - survey_area = survey_area, - plot_subtitle = plot_subtitle, - show_planned_stations = show_planned_stations, - data_source = data_source, - plot_anom = plot_anom, - dir_wd = dir_wd) + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + var = var, + dir_googledrive_upload = dir_googledrive_upload, + dates0 = dates0, + survey_area = survey_area, + plot_subtitle = plot_subtitle, + show_planned_stations = show_planned_stations, + data_source = data_source, + plot_anom = plot_anom, + dir_wd = dir_wd) # make_varplot_wrapper(maxyr = maxyr, # Anom and mean plot # SRVY = SRVY, # haul = haul, @@ -211,6 +148,150 @@ make_varplot_wrapper(maxyr = maxyr, # Daily plot # plot_anom = TRUE, # plot_mean = TRUE, # dir_wd = dir_wd) -# -sink() +## send all current files to the FTP ------------------------------------------- +# vars here defined in ftp.R +dir_out <- paste0(dir_wd,"/output/",maxyr,"_",SRVY,"/") +temp <- list.files(path = dir_out, pattern = "current_daily", full.names = FALSE) +dest <- dev_ai + +for (iiii in 1:length(temp)) { + print(temp[iiii]) + + RCurl::ftpUpload( + what = paste0(dir_out, "/", temp[iiii]), + asText = FALSE, + to = paste0(glue::glue("{protocol}://STOR@{server}/{dest}/", temp[iiii])), + userpwd = paste0(user,":", pass), + .opts=curlOptions(verbose=TRUE)) +} + +# NBS + EBS Maps -------------------------------------------------------------- + +maxyr <- 2022 +data_source <- "gd" # google drive +dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) +shapef <- FALSE #set to TRUE to run make_grid_wrapper to run shapefiles for EBS and AI +var <- "bt" + +googledrive_dl <- TRUE +dir_googledrive_log <- "https://docs.google.com/spreadsheets/d/16CJA6hKOcN1a3QNpSu3d2nTGmrmBeCdmmBCcQlLVqrE/edit#gid=315914502" +dir_googledrive_upload_bs = "https://drive.google.com/drive/folders/1vWza36Dog0SpZLcTN22wD-iCEn6ooGCM" +#dir_googledrive_upload_ai = "https://drive.google.com/drive/folders/1SeNOAh5-muQ2BDgOHWZWwYIoLl68DHWX" +#dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" + +# The surveys this script will be covering +dat_survreg <- data.frame(reg_shapefile = "EBS_SHELF", + region_long = "Eastern Bering Sea", + SRVY = "EBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "May 25 - Aug 03 2022") # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "NBS_SHELF", + region_long = "Northern Bering Sea", + SRVY = "NBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "Aug 03 - Aug 28 2022")) # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "AI", + region_long = "Aleutian Islands", + SRVY = "AI", + region = "AI", + vessel_id = c(148, 176), # CHANGE + vessel_shape = c("OEX", "AP"), # CHANGE + reg_dates = "Jun 07 - Aug 17 2022")) # CHANGE +# dat_survreg <- dplyr::bind_rows(dat_survreg, +# data.frame(reg_shapefile = "GOA", +# region_long = "Gulf of Alaska", +# SRVY = "GOA", +# region = "GOA", +# vessel_id = c(148, 176), # CHANGE +# vessel_shape = c("OEX", "SS"), # CHANGE +# reg_dates = "May 25 - Aug 04")) # CHANGE + +# SIGN INTO GOOGLE DRIVE-------------------------------------------------------- +## This sign in needs to be here for the Task Scheduler to run, please do not comment out. +googledrive::drive_deauth() +googledrive::drive_auth() +1 + +# SOURCE SUPPORT SCRIPTS ------------------------------------------------------- +## Actually we cant use the here package, here - it actually causes issues with +## the tasks scheduler, which has no concept of a project root folder. +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +#dir_wd <- "G:/EBSother/GAPsurveyTemperatureMap/" +#dir_wd <- paste0(getwd(), "/") +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +dir_wd <- "C:/Users/caitlin.akselrud/Work/survey-live-temperature-map/" + +source(file = paste0(dir_wd,"code/functions.R")) +# source(file = paste0(dir_wd, "code/data_dl.R")) # you don't unnecessarily run this each time +source(file = paste0(dir_wd, "code/data.R")) +if (googledrive_dl == TRUE) { + source(file = paste0(dir_wd, "code/ftp.R")) # removed in gitignore - ask for premission +} + +SRVY <- "BS" +region_akgfmaps = "bs.all" +plot_subtitle <- "NOAA Fisheries Bering Sea Bottom Trawl Survey" +dir_googledrive_upload <- (dir_googledrive_upload_bs) +survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") +show_planned_stations <- TRUE +plot_anom <- FALSE +survey_area$survey.grid <- survey_area$survey.grid %>% + sf::st_transform(x = ., survey_area$crs$input) %>% + dplyr::rename(station = STATIONID) %>% + sp::merge(x = ., + y = haul %>% + dplyr::rename(station = stationid) %>% + dplyr::select(station, stratum) %>% + dplyr::distinct(), + all.x = TRUE) %>% + dplyr::mutate(region = "Bering Sea") +survey_area$place.labels$y[survey_area$place.labels$lab == "200 m"] <- -60032.7 + +if(shapef == TRUE) +{ + make_grid_wrapper(maxyr = maxyr, # Blank grid plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + dir_googledrive_upload = dir_googledrive_upload, + survey_area = survey_area, + data_source = data_source, + plot_subtitle = plot_subtitle, + dir_wd = dir_wd) +} + +make_varplot_wrapper(maxyr = maxyr, # Daily plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + var = var, + dir_googledrive_upload = dir_googledrive_upload, + dates0 = dates0, + survey_area = survey_area, + plot_subtitle = plot_subtitle, + show_planned_stations = show_planned_stations, + data_source = data_source, + plot_anom = plot_anom, + dir_wd = dir_wd) + +dir_out <- paste0(dir_wd,"/output/",maxyr,"_",SRVY,"/") +temp <- list.files(path = dir_out, pattern = "current_daily", full.names = FALSE) +dest <- dev_bs + +for (iiii in 1:length(temp)) { + print(temp[iiii]) + + RCurl::ftpUpload( + what = paste0(dir_out, "/", temp[iiii]), + asText = FALSE, + to = paste0(glue::glue("{protocol}://STOR@{server}/{dest}/", temp[iiii])), + userpwd = paste0(user,":", pass), + .opts=curlOptions(verbose=TRUE)) +} diff --git a/code/run_AI.R b/code/run_AI.R new file mode 100644 index 0000000..891474c --- /dev/null +++ b/code/run_AI.R @@ -0,0 +1,249 @@ +#' --------------------------- +#' title: Survey Daily and Anomaly Temperature Plot +#' OG author: Jason Conner +#' maintained: Emily Markowitz and Liz Dawson (May 2022) +#' purpose: run script +#' --------------------------- + +# Knowns ----------------------------------------------------------------------- + +maxyr <- 2022 +data_source <- "gd" # google drive +dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) +shapef <- FALSE #set to TRUE to run make_grid_wrapper to run shapefiles for EBS and AI +var <- "bt" + +googledrive_dl <- TRUE +dir_googledrive_log <- "https://docs.google.com/spreadsheets/d/16CJA6hKOcN1a3QNpSu3d2nTGmrmBeCdmmBCcQlLVqrE/edit#gid=315914502" +#dir_googledrive_upload_bs = "https://drive.google.com/drive/u/2/folders/1wvLejYW73nd0v_lOejmAdoCBbD2kUsbf" #NULL #"https://drive.google.com/drive/folders/1vWza36Dog0SpZLcTN22wD-iCEn6ooGCM" +dir_googledrive_upload_ai = "https://drive.google.com/drive/u/0/folders/17rHN6GTkw5ywMXJ6VfyOkKXr7FY7o7B3" #NULL #"https://drive.google.com/drive/folders/1SeNOAh5-muQ2BDgOHWZWwYIoLl68DHWX" +#dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" + +# The surveys this script will be covering +dat_survreg <- data.frame(reg_shapefile = "EBS_SHELF", + region_long = "Eastern Bering Sea", + SRVY = "EBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "May 25 - Aug 03 2022") # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "NBS_SHELF", + region_long = "Northern Bering Sea", + SRVY = "NBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "Aug 03 - Aug 28 2022")) # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "AI", + region_long = "Aleutian Islands", + SRVY = "AI", + region = "AI", + vessel_id = c(148, 176), # CHANGE + vessel_shape = c("OEX", "AP"), # CHANGE + reg_dates = "Jun 07 - Aug 17 2022")) # CHANGE +# dat_survreg <- dplyr::bind_rows(dat_survreg, +# data.frame(reg_shapefile = "GOA", +# region_long = "Gulf of Alaska", +# SRVY = "GOA", +# region = "GOA", +# vessel_id = c(148, 176), # CHANGE +# vessel_shape = c("OEX", "SS"), # CHANGE +# reg_dates = "May 25 - Aug 04")) # CHANGE + +# SIGN INTO GOOGLE DRIVE-------------------------------------------------------- +## This sign in needs to be here for the Task Scheduler to run, please do not comment out. +googledrive::drive_deauth() +googledrive::drive_auth() +1 + +# SOURCE SUPPORT SCRIPTS ------------------------------------------------------- +## Actually we cant use the here package, here - it actually causes issues with +## the tasks scheduler, which has no concept of a project root folder. +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +#dir_wd <- "G:/EBSother/GAPsurveyTemperatureMap/" +#dir_wd <- paste0(getwd(), "/") +dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +#dir_wd <- "C:/Users/caitlin.akselrud/Work/survey-live-temperature-map/" + +source(file = paste0(dir_wd,"code/functions.R")) +# source(file = paste0(dir_wd, "code/data_dl.R")) # you don't unnecessarily run this each time +source(file = paste0(dir_wd, "code/data.R")) +if (googledrive_dl == TRUE) { + source(file = paste0(dir_wd, "code/ftp.R")) # removed in gitignore - ask for premission +} + +# Map -------------------------------------------------------------------------- +# ## AI -------------------------------------------------------------------------- +SRVY <- "AI" +region_akgfmaps = "ai" +plot_subtitle = "NOAA Fisheries Aleutian Islands Bottom Trawl Survey" +dir_googledrive_upload <- (dir_googledrive_upload_ai) +plot_anom <- FALSE +show_planned_stations <- FALSE +survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") +survey_area$survey.grid <- rgdal::readOGR(dsn = paste0(dir_wd, '/shapefiles/'),# Prepare map objects + layer = "aigrid_trawable_thru2018_Emily", + verbose=F) %>% + sp::spTransform(x = ., CRS(survey_area$crs$input)) %>% + st_as_sf(x = .) %>% + dplyr::rename(station = ID, + stratum = STRATUM) %>% + dplyr::filter(stratum %in% unique(goa_strata0$stratum) & + stratum != 0) %>% # land + sp::merge( + x = ., + y = goa_strata0 %>% + dplyr::filter(survey == "AI") %>% + dplyr::mutate(SRVY = "AI", + region = stringr::str_to_title(inpfc_area), + region = dplyr::case_when( + region %in% c("Western Aleutians", "Chirikof") ~ "Western Aleutians", + TRUE ~ region)) %>% + dplyr::select(SRVY, stratum, region) %>% + dplyr::distinct(), + all.x = TRUE) %>% + dplyr::arrange(region) +survey_area$survey.grid1 <- survey_area$survey.grid + +if(shapef == TRUE) +{ + make_grid_wrapper(maxyr = maxyr, # Blank grid plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + dir_googledrive_upload = dir_googledrive_upload, + survey_area = survey_area, + data_source = data_source, + plot_subtitle = plot_subtitle, + dir_wd = dir_wd) +} + +make_varplot_wrapper(maxyr = maxyr, # Daily plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + var = var, + dir_googledrive_upload = dir_googledrive_upload, + dates0 = dates0, + survey_area = survey_area, + plot_subtitle = plot_subtitle, + show_planned_stations = show_planned_stations, + data_source = data_source, + plot_anom = plot_anom, + dir_wd = dir_wd) +# make_varplot_wrapper(maxyr = maxyr, # Anom and mean plot +# SRVY = SRVY, +# haul = haul, +# dat_survreg = dat_survreg, +# var = var, +# dir_googledrive_upload = dir_googledrive_upload, +# dates0 = "latest", +# survey_area = survey_area, +# plot_subtitle = plot_subtitle, +# show_planned_stations = show_planned_stations, +# data_source = data_source, +# plot_daily = FALSE, +# plot_anom = TRUE, +# plot_mean = TRUE, +# dir_wd = dir_wd) + +## send all current files to the FTP ------------------------------------------- +# vars here defined in ftp.R +dir_out <- paste0(dir_wd,"/output/",maxyr,"_",SRVY,"/") +temp <- list.files(path = dir_out, pattern = "current_daily", full.names = FALSE) +dest <- dev_ai + +for (iiii in 1:length(temp)) { + print(temp[iiii]) + + RCurl::ftpUpload( + what = paste0(dir_out, "/", temp[iiii]), + asText = FALSE, + to = paste0(glue::glue("{protocol}://STOR@{server}/{dest}/", temp[iiii])), + userpwd = paste0(user,":", pass), + .opts=curlOptions(verbose=TRUE)) +} + + +# NBS + EBS Maps -------------------------------------------------------------- +# +# SRVY <- "BS" +# region_akgfmaps = "bs.all" +# plot_subtitle <- "NOAA Fisheries Bering Sea Bottom Trawl Survey" +# dir_googledrive_upload <- (dir_googledrive_upload_bs) +# survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") +# show_planned_stations <- TRUE +# plot_anom <- TRUE +# survey_area$survey.grid <- survey_area$survey.grid %>% +# sf::st_transform(x = ., survey_area$crs$input) %>% +# dplyr::rename(station = STATIONID) %>% +# sp::merge(x = ., +# y = haul %>% +# dplyr::rename(station = stationid) %>% +# dplyr::select(station, stratum) %>% +# dplyr::distinct(), +# all.x = TRUE) %>% +# dplyr::mutate(region = "Bering Sea") +# survey_area$place.labels$y[survey_area$place.labels$lab == "200 m"] <- -60032.7 +# +# if(shapef == TRUE) +# { +# make_grid_wrapper(maxyr = maxyr, # Blank grid plot +# SRVY = SRVY, +# haul = haul, +# dat_survreg = dat_survreg, +# dir_googledrive_upload = dir_googledrive_upload, +# survey_area = survey_area, +# data_source = data_source, +# plot_subtitle = plot_subtitle, +# dir_wd = dir_wd) +# } +# +# make_varplot_wrapper(maxyr = maxyr, # Daily plot +# SRVY = SRVY, +# haul = haul, +# dat_survreg = dat_survreg, +# var = var, +# dir_googledrive_upload = dir_googledrive_upload, +# dates0 = dates0, +# survey_area = survey_area, +# plot_subtitle = plot_subtitle, +# show_planned_stations = show_planned_stations, +# data_source = data_source, +# plot_anom = plot_anom, +# dir_wd = dir_wd) +# # make_varplot_wrapper(maxyr = maxyr, # Anom and mean plot +# # SRVY = SRVY, +# # haul = haul, +# # dat_survreg = dat_survreg, +# # var = var, +# # dir_googledrive_upload = dir_googledrive_upload, +# # dates0 = "latest", +# # survey_area = survey_area, +# # plot_subtitle = plot_subtitle, +# # show_planned_stations = show_planned_stations, +# # data_source = data_source, +# # plot_daily = FALSE, +# # plot_anom = FALSE, # anom plot here doesnt make sense to print until the end +# # plot_mean = TRUE, +# # dir_wd = dir_wd) +# +## send all current files to the FTP ------------------------------------------- +# vars here defined in ftp.R +# dir_out <- paste0(dir_wd, "/output/", maxyr, "_",SRVY,"/") +# temp <- list.files(path = dir_out, pattern = "current_daily", full.names = FALSE) +# dest <- dev_bs +# +# for (iiii in 1:length(temp)) { +# print(temp[iiii]) +# +# RCurl::ftpUpload( +# what = paste0(dir_out, "/", temp[iiii]), +# asText = FALSE, +# to = paste0(glue::glue("{protocol}://STOR@{server}/{dest}/", temp[iiii])), +# userpwd = paste0(user,":", pass), +# .opts=curlOptions(verbose=TRUE)) +# } +# diff --git a/code/run_BS.R b/code/run_BS.R new file mode 100644 index 0000000..2d49aa2 --- /dev/null +++ b/code/run_BS.R @@ -0,0 +1,135 @@ +#' --------------------------- +#' title: Survey Daily and Anomaly Temperature Plot +#' OG author: Jason Conner +#' maintained: Emily Markowitz and Liz Dawson (May 2022) +#' purpose: run script +#' --------------------------- + +# Knowns ----------------------------------------------------------------------- +maxyr <- 2022 +data_source <- "gd" # google drive +dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) +shapef <- FALSE #set to TRUE to run make_grid_wrapper to run shapefiles for EBS and AI +var <- "bt" + +googledrive_dl <- TRUE +dir_googledrive_log <- "https://docs.google.com/spreadsheets/d/16CJA6hKOcN1a3QNpSu3d2nTGmrmBeCdmmBCcQlLVqrE/edit#gid=315914502" +dir_googledrive_upload_bs = "https://drive.google.com/drive/u/0/folders/1wvLejYW73nd0v_lOejmAdoCBbD2kUsbf" #NULL #"https://drive.google.com/drive/folders/1vWza36Dog0SpZLcTN22wD-iCEn6ooGCM" +#dir_googledrive_upload_ai = "https://drive.google.com/drive/u/2/folders/17rHN6GTkw5ywMXJ6VfyOkKXr7FY7o7B3" #NULL #"https://drive.google.com/drive/folders/1SeNOAh5-muQ2BDgOHWZWwYIoLl68DHWX" +#dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" + +# The surveys this script will be covering +dat_survreg <- data.frame(reg_shapefile = "EBS_SHELF", + region_long = "Eastern Bering Sea", + SRVY = "EBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "May 25 - Aug 03 2022") # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "NBS_SHELF", + region_long = "Northern Bering Sea", + SRVY = "NBS", + region = "BS", + vessel_id = c(94, 162), # CHANGE + vessel_shape = c("V", "A"), # CHANGE + reg_dates = "Aug 03 - Aug 28 2022")) # CHANGE +dat_survreg <- dplyr::bind_rows(dat_survreg, + data.frame(reg_shapefile = "AI", + region_long = "Aleutian Islands", + SRVY = "AI", + region = "AI", + vessel_id = c(148, 176), # CHANGE + vessel_shape = c("OEX", "AP"), # CHANGE + reg_dates = "Jun 07 - Aug 17 2022")) # CHANGE +# dat_survreg <- dplyr::bind_rows(dat_survreg, +# data.frame(reg_shapefile = "GOA", +# region_long = "Gulf of Alaska", +# SRVY = "GOA", +# region = "GOA", +# vessel_id = c(148, 176), # CHANGE +# vessel_shape = c("OEX", "SS"), # CHANGE +# reg_dates = "May 25 - Aug 04")) # CHANGE + +# SIGN INTO GOOGLE DRIVE-------------------------------------------------------- +## This sign in needs to be here for the Task Scheduler to run, please do not comment out. +googledrive::drive_deauth() +googledrive::drive_auth() +1 + +# SOURCE SUPPORT SCRIPTS ------------------------------------------------------- +## Actually we cant use the here package, here - it actually causes issues with +## the tasks scheduler, which has no concept of a project root folder. +#dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +#dir_wd <- "G:/EBSother/GAPsurveyTemperatureMap/" +#dir_wd <- paste0(getwd(), "/") +dir_wd <-"C:/Users/liz.dawson/Work/R/GAPSurveyTemperatureMap/" +#dir_wd <- "C:/Users/caitlin.akselrud/Work/survey-live-temperature-map/" + +source(file = paste0(dir_wd,"code/functions.R")) +# source(file = paste0(dir_wd, "code/data_dl.R")) # you don't unnecessarily run this each time +source(file = paste0(dir_wd, "code/data.R")) +if (googledrive_dl == TRUE) { + source(file = paste0(dir_wd, "code/ftp.R")) # removed in gitignore - ask for premission +} + +SRVY <- "BS" +region_akgfmaps = "bs.all" +plot_subtitle <- "NOAA Fisheries Bering Sea Bottom Trawl Survey" +dir_googledrive_upload <- (dir_googledrive_upload_bs) +survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") +show_planned_stations <- TRUE +plot_anom <- FALSE +survey_area$survey.grid <- survey_area$survey.grid %>% + sf::st_transform(x = ., survey_area$crs$input) %>% + dplyr::rename(station = STATIONID) %>% + sp::merge(x = ., + y = haul %>% + dplyr::rename(station = stationid) %>% + dplyr::select(station, stratum) %>% + dplyr::distinct(), + all.x = TRUE) %>% + dplyr::mutate(region = "Bering Sea") +survey_area$place.labels$y[survey_area$place.labels$lab == "200 m"] <- -60032.7 + +if(shapef == TRUE) +{ + make_grid_wrapper(maxyr = maxyr, # Blank grid plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + dir_googledrive_upload = dir_googledrive_upload, + survey_area = survey_area, + data_source = data_source, + plot_subtitle = plot_subtitle, + dir_wd = dir_wd) +} + +make_varplot_wrapper(maxyr = maxyr, # Daily plot + SRVY = SRVY, + haul = haul, + dat_survreg = dat_survreg, + var = var, + dir_googledrive_upload = dir_googledrive_upload, + dates0 = dates0, + survey_area = survey_area, + plot_subtitle = plot_subtitle, + show_planned_stations = show_planned_stations, + data_source = data_source, + plot_anom = plot_anom, + dir_wd = dir_wd) + +dir_out <- paste0(dir_wd,"/output/",maxyr,"_",SRVY,"/") +temp <- list.files(path = dir_out, pattern = "current_daily", full.names = FALSE) +dest <- dev_bs + +for (iiii in 1:length(temp)) { + print(temp[iiii]) + + RCurl::ftpUpload( + what = paste0(dir_out, "/", temp[iiii]), + asText = FALSE, + to = paste0(glue::glue("{protocol}://STOR@{server}/{dest}/", temp[iiii])), + userpwd = paste0(user,":", pass), + .opts=curlOptions(verbose=TRUE)) +} diff --git a/code/run_past.R b/code/run_past.R index 8162da8..6d9bc59 100644 --- a/code/run_past.R +++ b/code/run_past.R @@ -5,7 +5,13 @@ #' purpose: run script #' --------------------------- + +# source("./code/run_past.R") +# 1 + # Knowns ----------------------------------------------------------------------- +googledrive_dl <- TRUE +dir_googledrive_log <- "https://docs.google.com/spreadsheets/d/16CJA6hKOcN1a3QNpSu3d2nTGmrmBeCdmmBCcQlLVqrE/edit#gid=315914502" dir_googledrive_upload_test = "https://drive.google.com/drive/folders/1rsR0aFfFzrspTBFU48Bb26EJvdhIZSpl" # SIGN INTO GOOGLE DRIVE-------------------------------------------------------- @@ -27,22 +33,17 @@ source(file = paste0(dir_wd,"code/functions.R")) source(file = paste0(dir_wd, "code/data.R")) # Map -------------------------------------------------------------------------- +var = "bt" +data_source <- "oracle" +plot_anom <- FALSE +dates0 <- "all" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) ## NBS + EBS ------------------------------------------------------------------- -maxyr <- 2022 #CHANGE -data_source <- "gd" # google drive SRVY <- "BS" plot_subtitle <- "NOAA Fisheries Bering Sea Bottom Trawl Survey" -dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_bs) -# dir_googledrive_upload <- googledrive::as_id(dir_googledrive_upload_test) region_akgfmaps = "bs.all" survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") - -# Daily -var = "bt" -dates0 <- "latest" # "all" # latest # "all", #"2021-06-05",# Sys.Date(), # as.character(seq(as.Date("2022-07-30"), as.Date("2022-08-14"), by="days")) show_planned_stations <- TRUE -plot_anom <- FALSE survey_area$survey.grid <- survey_area$survey.grid %>% sf::st_transform(x = ., survey_area$crs$input) %>% dplyr::rename(station = STATIONID) %>% @@ -55,10 +56,6 @@ survey_area$survey.grid <- survey_area$survey.grid %>% dplyr::mutate(region = "Bering Sea") survey_area$place.labels$y[survey_area$place.labels$lab == "200 m"] <- -60032.7 -data_source = "oracle" -plot_anom = FALSE -dates0 <- "all" - dat_ebs <- data.frame(reg_shapefile = "EBS_SHELF", region_long = "Eastern Bering Sea", SRVY = "EBS", @@ -73,21 +70,16 @@ dat_nbs <- data.frame(reg_shapefile = "NBS_SHELF", vessel_id = c(94, 162), vessel_shape = c("V", "A"), reg_dates = "Aug 03 - Aug 28 2022") -dat_survdat <- dplyr::bind_rows(dat_ebs, dat_nbs) - - -yrs <- list(2021 = c("https://drive.google.com/drive/folders/1q4UN9INXFAyZcIwqy8W9UYfY3G1LuQgW"), - # dat = dat_bs), - 2019 = c("https://drive.google.com/drive/folders/1S5FyXwWyFUgFkvDlGTC6cymtISyZdd9R"), - # dat = dat_bs), - 2017 = c("https://drive.google.com/drive/folders/1nAeb9Jq9_FxUc70ZfvXaYZbXrZCQTD4u"))#, -# dat = dat_bs)) +dat_survreg <- dplyr::bind_rows(dat_ebs, dat_nbs) +yrs <- list("2021" = c("https://drive.google.com/drive/folders/1q4UN9INXFAyZcIwqy8W9UYfY3G1LuQgW"), + "2019" = c("https://drive.google.com/drive/folders/1S5FyXwWyFUgFkvDlGTC6cymtISyZdd9R"), + "2017" = c("https://drive.google.com/drive/folders/1nAeb9Jq9_FxUc70ZfvXaYZbXrZCQTD4u")) for (i in 1:length(yrs)){ maxyr <- as.numeric(names(yrs)[i]) - dir_googledrive_upload <- googledrive::as_id(yrs[i]) + dir_googledrive_upload <- googledrive::as_id(unlist(yrs[i])) make_grid_wrapper(maxyr = maxyr, # Blank grid plot SRVY = SRVY, haul = haul, @@ -144,12 +136,12 @@ survey_area$survey.grid <- survey_area$survey.grid %>% dplyr::filter(station %in% akgfmaps::get_survey_stations(select.region = region_akgfmaps)) %>% dplyr::mutate(region = "Bering Sea") -yrs <- list(2018 = "https://drive.google.com/drive/folders/1jaJrvKE729I15YnC6LhRDVI5Xxg4xEPo") +yrs <- list("2018" = "https://drive.google.com/drive/folders/1jaJrvKE729I15YnC6LhRDVI5Xxg4xEPo") for (i in 1:length(yrs)) { maxyr <- as.numeric(names(yrs)[i]) - dir_googledrive_upload <- googledrive::as_id(yrs[i]) + dir_googledrive_upload <- googledrive::as_id(unlist(yrs[i])) make_grid_wrapper(maxyr = maxyr, # Blank grid plot SRVY = SRVY, @@ -192,10 +184,8 @@ for (i in 1:length(yrs)) { ## AI -------------------------------------------------------------------------- SRVY <- "AI" -plot_anom <- FALSE plot_subtitle = "NOAA Fisheries Aleutian Islands Bottom Trawl Survey" region_akgfmaps = "ai" -var = "bt" show_planned_stations <- FALSE survey_area <- akgfmaps::get_base_layers(select.region = region_akgfmaps, set.crs = "auto") @@ -223,10 +213,6 @@ survey_area$survey.grid <- rgdal::readOGR(dsn = paste0(dir_wd, '/shapefiles/'),# dplyr::arrange(region) survey_area$survey.grid1 <- survey_area$survey.grid -data_source = "oracle" -plot_anom = FALSE -dates0 <- "all" # latest - dat_survreg <- dplyr::bind_rows(dat_survreg, data.frame(reg_shapefile = "AI", region_long = "Aleutian Islands", @@ -236,12 +222,12 @@ dat_survreg <- dplyr::bind_rows(dat_survreg, vessel_shape = c("OEX", "SS"), # CHANGE reg_dates = "\n(June 07-Aug 17 2022)")) # CHANGE -yrs <- list(2018 = "https://drive.google.com/drive/folders/1dzWwb3bXnPXlSy_JIaY4BKo6WDshru_d") +yrs <- list("2018" = "https://drive.google.com/drive/folders/1dzWwb3bXnPXlSy_JIaY4BKo6WDshru_d") for (i in 1:length(yrs)) { maxyr <- as.numeric(names(yrs)[i]) - dir_googledrive_upload <- googledrive::as_id(yrs[i]) + dir_googledrive_upload <- googledrive::as_id(unlist(yrs[i])) make_grid_wrapper(maxyr = maxyr, # Blank grid plot SRVY = SRVY, diff --git a/code/template.Rmd b/code/template.Rmd index 55de15a..b67a423 100644 --- a/code/template.Rmd +++ b/code/template.Rmd @@ -27,11 +27,21 @@ knitr::opts_chunk$set(echo = FALSE, comment = FALSE, message = FALSE, warning = vess <- text_list(x = paste0("F/V \\textit{", unique(gsub(pattern = "F/V ", replacement = "", fixed = TRUE, x = dat$vessel_name[!is.na(dat$vessel_name)])),"}")) - -if (as.character(dates0[1]) == "none") { +if (file_end == "mean") { + + temp1<-unique(dat_plot$reg_lab[!is.na(dat$reg_shapefile)]) + + fig_cap <- fig_alt <- paste0( + "Mean bottom temperatures from the NOAA Fisheries ", + gsub(pattern = "\n", replacement = " and ", x = plot_title), + " in ", maxyr, + ". ") + +} else if (file_end == "grid") { # (as.character(dates0[1]) == "none") { temp1<-unique(dat_plot$reg_lab[!is.na(dat$reg_shapefile)]) + if (SRVY != "AI") { fig_cap <- fig_alt <- paste0( "NOAA Fisheries conducted the ", text_list(x = gsub(pattern = "\n", replacement = "", x = unique(dat_plot$reg_lab[!is.na(dat$reg_shapefile)]))), @@ -44,6 +54,10 @@ if (as.character(dates0[1]) == "none") { ifelse(length(temp1)>1, "s", ""), ifelse(SRVY != "BS", ". Note that only a random sample of these stations are surveyed each year. ", paste0(" and ",text_list(x = paste0(unique(survey_area$bathymetry$METERS), "m"))," bathymetry. "))) + } else { + fig_cap <- fig_alt <- paste0("Aleutian Islands survey region, ", maxyr, ". ") + } + # } else if (file_end = "mean") { # @@ -62,11 +76,12 @@ if (as.character(dates0[1]) == "none") { # ifelse(SRVY != "BS", ". Note that only a random sample of these stations are surveyed each year. ", # paste0(" and ",text_list(x = paste0(unique(survey_area$bathymetry$METERS), "m"))," bathymetry. "))) -} else { +} else if (file_end == "daily") { loc <- dat_plot %>% dplyr::filter(!is.na(var) & date == max_date) + if (nrow(loc) == 0) { stations1 <- "no stations were sampled " } else { @@ -91,15 +106,21 @@ if (as.character(dates0[1]) == "none") { dplyr::mutate(lon = round(x = lon, digits = 3), lat = round(x = lat, digits = 3)) + # TOLEDO because there are issues in the shapefile... again + dat_max_date <- dat_max_date %>% + dplyr::filter(!is.na(lon)) + temp <- c() for (iii in 1:length(unique(dat_max_date$vessel_shape))) { temp1 <- dat_max_date[which(dat_max_date$vessel_shape == unique(dat_max_date$vessel_shape)[iii]),] temp <- c(temp, - paste0(nrow(temp1), " station", - ifelse(nrow(temp1)>1, "s", "")," with ", - ifelse(nrow(temp1)>1, "", "a "), "station ID", ifelse(nrow(temp1)>1, "s", ""), " of ", + paste0(# nrow(temp1), + # " station", + # ifelse(nrow(temp1)>1, "s", "")," with ", + ifelse(nrow(temp1)>1, "", "a "), "station", # ID", + ifelse(nrow(temp1)>1, "s", ""), " ", #" of ", text_list(x = paste0(temp1$station, - " (near ",temp1$lat,"°N and ",temp1$lon,"°W, measuring ", + " (",temp1$lat,"°N, ",temp1$lon,"°W; ", gsub(pattern = " ", replacement = "", x = temp1$var_bin, fixed = TRUE),"°C)")), " ", ifelse(nrow(temp1)>1, "were", "was"), " surveyed by the F/V \\textit{", gsub(pattern = "F/V ", replacement = "", fixed = TRUE, @@ -131,24 +152,26 @@ if (as.character(dates0[1]) == "none") { } fig_cap <- fig_alt <- paste0( - "NOAA Fisheries near-real time ocean bottom temperatures collected ", + "Near real-time ocean bottom temperatures collected ", ifelse(min(as.Date(dat$date), na.rm = TRUE) == max_date, - paste0("on ", format(x = min(as.Date(dat$date), na.rm = TRUE), "%B %d, %Y")), - paste0("between ", format(x = min(as.Date(dat$date), na.rm = TRUE), "%B %d"), - " and ", - format(x = as.Date(max_date), format = "%B %d, %Y"))), + paste0("on ", format(x = min(as.Date(dat$date), na.rm = TRUE), "%B %d %Y")), + paste0(" ", format(x = min(as.Date(dat$date), na.rm = TRUE), "%B %d"), + "-", + format(x = as.Date(max_date), format = "%B %d %Y"))), " during the ", - text_list(x = gsub(pattern = "\n", replacement = " ", x = unique(dat_plot$reg_lab[!is.na(dat$reg_shapefile)]))), - " survey", - ifelse(length(unique(dat$SRVY))>1, "s", ""), + text_list(x = gsub(pattern = " - ", + replacement = "-", + x = gsub(pattern = "\n", replacement = " ", + x = unique(dat_plot$reg_lab[!is.na(dat$reg_shapefile)])))), + " survey", ifelse(length(unique(dat$SRVY))>1, "s", ""), " aboard the ", vess, ". On ", format(x = as.Date(max_date), "%B %d"), ", ", stations1, ifelse(data_source == "gd", - "These data have not been through final review. Final, validated survey data (e.g., temperature and species collected at each station among other observations), will be publicly accessible on the Fisheries One Stop Shop platform (FOSS; https://www.fisheries.noaa.gov/foss/f?p=215:200:1899488600966:Mail:NO:::). ", - "These data have been through final review. Final, validated survey data (e.g., temperature and species collected at each station among other observations), are publicly accessible on the Fisheries One Stop Shop platform (FOSS; https://www.fisheries.noaa.gov/foss/f?p=215:200:1899488600966:Mail:NO:::). ")) + "Data have NOT been through final review. Final, validated data (e.g., temperature and species collected at each station), will be publicly accessible on FOSS platform (https://www.fisheries.noaa.gov/foss/f?p=215:200:1899488600966:Mail:NO:::). ", + "Data have been through final review. Final, validated data (e.g., temperature and species collected at each station), are be publicly accessible on FOSS platform (https://www.fisheries.noaa.gov/foss/f?p=215:200:1899488600966:Mail:NO:::). ")) } @@ -156,6 +179,9 @@ fig_cap <- fig_alt <- paste0(fig_cap, "\\textit{Credit: NOAA Fisheries}") fig_alt <- gsub(pattern = "\\textit{", replacement = "", x = fig_alt, fixed = TRUE) fig_alt <- gsub(pattern = "}", replacement = "", x = fig_alt, fixed = TRUE) +fig_alt <- gsub(pattern = " (https://www.fisheries.noaa.gov/foss/f?p=215:200:1899488600966:Mail:NO:::)", + replacement = "", + x = fig_alt, fixed = TRUE) readr::write_lines(x = fig_alt, file = paste0(dir_out, filename0, ".txt"))