R/proj_setup.R

Defines functions proj_setup

proj_setup <- function(path, ...) {

  # Create directories
  dir.create(path, recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/1_Annual_Protocol"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/2_EVTemplate"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/3_Ping_Data"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/4_Trawl_Data"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/5_Enviro_Data"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/6_Misc"), recursive = TRUE, showWarnings = FALSE)
  dir.create(paste0(path, "/7_Annual_Summary"), recursive = TRUE, showWarnings = FALSE)

  # create some header text
  header <- c(
    "# Erie Acoustic Project",
    "*project template generated by* `erieacoustics`"
   )

  filetable <- c(
    "## Folder Description and Contents",
    "File | Description",
    "---------- | --------------------------------------------------",
    "**1_Annual_Protocol** | Annual survey protocol scripts and products",
    "**2_EVTemplate** | Echoview processing template must be stored here for access by `set_up_transect.R`",
    "**3_Ping_Data** | Uniquely named transect folders (basin_stratum_grid)",
    "**3_Ping_Data/xB_Sxx_Gxxx** | Transect specific hydroacoustic data stored within a single directory",
    "**4_Trawl_Data** | Paired midwater trawl data",
    "**5_Enviro_Data** | Water column profiles and other environmental measurements",
    "**6_Misc** | Miscellaneous files associated with survey",
    "**7_Annual_Summary** | Annual survey summary scripts and products"
  )

  instructions <- c(
    "# Erie Acoustic Project Template Intructions  ",
    "This file is best viewed by using the *Preview* feature in RStudio.  ",
    " ",
    "## What's included  ",
    "1. ReadMe.md template  ",
    "2. Instructions.md template  ",
    "3. A gitignore file. If you have git installed you will still need to run `git init` to initialize the repo.  ",
    "4. Template folder directory - directory structure is required for package functions to run as intended.  ",
    "5. Example analysis scripts to aid function use and data processing.  ",
    "  ",
    "## Instructions  ",
    "1. Run `erieacoustics::finish_setup()` to complete the project set up.  ",
    "2. In folder **1_Annual_Protocol**, use `1_select_sample_grids.R` and `2_Project_Proposal_Summary.Rmd` to generate *2_Project_Proposal_Summary.html*.  ",
    "3. Field calibrate hydroacoustic system. ([calibration instructions](6_Misc/Calibration_Instructions_Notes.html)).   ",
    "4. Collect survey data following *2_Project_Proposal_Summary.html*.   ",
    "5. Transfer hydroacoustic, trawl, and environmental data to folders.  ",
    "    * **3_Ping_Data** - houses raw transects data in transect specific folders (e.g., CB_S07_G621).  ",
    "    * **4_Trawl_Data** - format trawl data and transfer to *Trawl_Catch.csv*, *Trawl_Effort.csv*, and *Trawl_length.csv* files.  ",
    "    * **5_Enviro_Data** - format profile data and transfer to *Water_Column_Profiles.csv*.  ",
    "    * **6_Misc** - all the stuff we forgot about... put it here.  ",
    "6. Follow [Data Processing Steps] below.  ",
    "Review and update the `ReadMe.md` file as needed. View: [ReadMe](ReadMe.html).  ",
    " ",
    "## Data Processing Steps  ",
    "1. Analyze the calibration data and generate calibration report ([calibration instructions](6_Misc/Calibration_Instructions_Notes.html)).   ",
    "2. Create *.ecs file and update transducer characteristic based on calibration report.   ",
    "3. In **2_EVTemplate**, Open the EV template and edit the template to include the new calibration file and update variables in *Formula_Nv* and *Platform* objects. Save as *ErieHacTemplate_20xx_xB.Ev*.    ",
    "4. In **7_Annual_Summary**, open and edit `1_import_data_to_template.R`. This script uses the custom set up functions from `erieacoustics` to apply COM commands directly to Echoview to import and save a new EV file. Run `1_import_data_to_template.R`. *WARNING*: this process could take a long time!    ",
    "5. In **Ping_Data** sub-folders, scrutinize, clean, and edit each transects EV file. See link to detailed instructions: [Echogram Processing Instructions](6_Misc/Echogram_Processing_Instructions.html).   ",
    "6. In **7_Annual_Summary**, when data inspection and editing is complete, run `2_export_data_from_EV_EV.R` to export each transect. *WARNING*: this could take a long time!   ",
    "7. In **7_Annual_Summary**, open `3_aggregate_format_hydro_data.R` and run script to produce *hacdat.csv*, *histo.csv*, and *histohac.csv*.   ",
    "8. In **7_Annual_Summary**, open `4_aggregate_format_wcp_data.R` and run script to produce *EpiBotLineSummaries.csv* and *wcpdat.csv*.  ",
    "9. In **7_Annual_Summary**, open `5_aggregate_format_trawl_data.R` and run script to produce *trwldat.csv* and *trwllen.csv*.  ",
    "10. In **7_Annual_Summary**, open `6_Annual_Summary.R` and run script to produce *6_Annual_Summary.html*. Scrutinize report and make an necessary changes in the data processing flow. Reproduce *6_Annual_Summary.html* as needed.  ",
    "11. Move forward with survey presentation and reporting.",
    "12. Collate data across basins and prepare for data release through sciencebase.org",
    " "
  )


  writeLines(c(instructions, " ", filetable), con = file.path(path, "Instructions.md"))

  # collect inputs and paste together as 'Parameter: Value'
  dots <- list(...)
  text <- lapply(seq_along(dots), function(i) {
    key <- names(dots)[[i]]
    val <- dots[[i]]
    paste0(key, ": ", val, "<br />")
  })

  # Save input parameter for downstream use
  metadata <- lapply(seq_along(dots), function(i) {
    key <- names(dots)[[i]]
    val <- dots[[i]]
    val
  })

  names(metadata) <- c("PI", "Agency", "Basin", "Sonar",
                       "Frequency", "Year", "Vessel")
  save(metadata, file = file.path(path, "metadata.RData"))

  # collect into single text string
  contents <- paste(
    paste(header, collapse = "\n"),
    paste(text, collapse = "\n"),
    sep = "  \n"
  )

  abstract <-  c(
    "# Post Survey Notes",
    " ",
    "Record progress and describe notable deviations from *2_Annual_Survey_Protocol.html*"
  )

  filetable2 <- c(
    "## Folder Description and Contents",
    "File | Data processing notes",
    "---------- | --------------------------------------------------",
    "**1_Annual_Protocol** | Annual protocol completion data",
    "**2_EVTemplate** | Notable updates to Echoview template",
    "**3_Ping_Data** | File transfer date and total number of transects",
    "**3_Ping_Data/xB_Sxx_Gxxx** | Transect specific notes",
    "**4_Trawl_Data** | Data acquisition date and agency point of contact",
    "**5_Enviro_Data** | File transfer date and agency point of contact",
    "**6_Misc** | Associated file descriptions",
    "**7_Annual_Summary** | Completion date"
  )
  # write to index file
  readme <- c(contents, " ", abstract, " ", filetable2)
  writeLines(readme, con = file.path(path, "ReadMe.md"))

  # write gitignore file
  gitignore <- c(
    "# Gitignore file autogenerated by `erieacoustics`",
    ".Rproj.user",
    ".Rhistory",
    ".RData",
    ".Ruserdata",
    "*.html",
    "",
    "# Acoustic file types",
    "*.bot",
    "*.raw",
    "*.idx",
    "*.evi",
    "*.evwx",
    "*.evb",
    "*.dt4",
    "*.rtp",
    "*.rtpx",
    "*.dg"
  )

  writeLines(gitignore, con = file.path(path, ".gitignore"))

  transect_setup <- c(
    "# Export transect data",
    "library(erieacoustics)",
    "# Edit the template file name:",
    "templatefile <- '2_EVTemplate/ErieHacTemplate_2021_v2_simrad.EV'",
    "evtemplate <- file.path(getwd(), templatefile)",
    "dir.exists('3_Ping_Data')",
    "file.exists(evtemplate)",
    "",
    "transects <- dir('3_Ping_Data')",
    "",
    "# run all",
    "# Edit TRANSDUCER type:",
    "TRANSDUCER <- 'SIMRAD'",
    "run_all <- function(x) {set_up_transect(evtemplate, getwd(), TRANSDUCER, x)}",
    "lapply(transects, run_all)"
  )

}
HoldenJe/erieacoustics documentation built on July 17, 2024, 7:08 a.m.