test/sa_tests.R

# Disclaimer::::
## This will only work if you have an admin acount with NABat.
##   This test will build all possible SA reports across all
##     NABat projects in the database

# TEST all Stationary Acoustic projects in database by building reports

####### CHANGE THESE VALUES ####### ####### #######
username_ = 'nabat_username'
dir_ = '/path/to/output/reports'
####### ####### ####### ####### ####### ####### ###
branch_   = 'prod'

# Login and get NABat GQL token
token_ = get_nabat_gql_token(username = username_, branch = branch_)
poject_types_df = get_all_project_types(token_, branch = branch_)
# 7 is the surveyType ID in the database for Stationary Acoustic data
all_sa_projects = sort(unique(subset(poject_types_df, poject_types_df$survey_type_id == 7)$project_id))

####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######
####### if you want to run just some and not all you can use this code instead (first ten) ####### ####### ####
# all_sa_projects = sort(unique(subset(poject_types_df, poject_types_df$survey_type_id == 7)$project_id))[1:10]
####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### ####### #######

# Set completed projects to empty
completed_sa_projects = c()
# Set failed projects to empty
failed_sa_projects = list()

# Loop through all projects that have SA data and build reports
for (project_id_ in all_sa_projects){
  my_var = tryCatch(
    {
      message(paste0("Running sa report builder for project: ", project_id_))

      token_ = get_refresh_token(token_, branch = branch_)
      # Import the newest version of the species_df
      species_df_ = get_species(token = token_, branch = branch_)
      # Get your projects lookup table
      project_df_ = get_projects(token  = token_,
        branch = branch_)

      # Get survey dataframe, This function also sets the global variable pkg.env$grts_df
      #   to the appropriate grts_frame.  ex: 'Canada'|'CONUS'
      token_ = get_refresh_token(token_, branch = branch_)
      sa_survey_df_r = get_sa_project_summary(token = token_,
        project_id = project_id_,
        project_df = project_df_,
        branch     = branch_,
        return_t = TRUE)
      sa_survey_df_ = sa_survey_df_r$df
      token_ = sa_survey_df_r$token

      if (dim(sa_survey_df_)[1] == 0){
        message(paste0('No Data Available at project id: ',project_id_))
      } else{
        # Add loop through year so we grab all years for a project and create
        ## a unique report for each  year
        proj_dates = unique(sa_survey_df_$year)

        year_ = proj_dates[1]
        # Get stationary acoustic bulk upload format dataframe
        token_ = get_refresh_token(token_, branch = branch_)
        sa_bulk_wav_r = get_sa_bulk_wavs(token      = token_,
          survey_df  = sa_survey_df_,
          branch = branch_,
          year = year_,
          return_t = TRUE)
        sa_bulk_df_ = sa_bulk_wav_r$df
        token_ = sa_bulk_wav_r$token

        # Get Acoustic stationary bulk dataframe
        nightly_observed_list_ = get_observed_nights(sa_bulk_df_)

        # Build Report
        token_ = get_refresh_token(token_, branch = branch_)
        sa_doc = build_sa_doc(out_dir = dir_,
          file_name = paste0('sa_report_',project_id_,'_',Sys.Date(),'.docx'),
          project_df = project_df_,
          project_id = project_id_,
          sa_bulk_df = sa_bulk_df_,
          sa_survey_df = sa_survey_df_,
          species_df = species_df_,
          selected_year = year_,
          nightly_observed_list = nightly_observed_list_,
          date = format(Sys.time(), "%B %d, %Y"),
          range_maps = FALSE)

        # Save out report
        print(sa_doc, target = paste0(dir_, '/', paste0(year_, '_sa_report_',project_id_,'_',format(Sys.time(), '%Y_%m_%d_%H%M%S'),'.docx')))
        completed_sa_projects = c(completed_sa_projects, project_id_)

      }
    },
    # Catch errors
    error=function(cond) {
      message(paste("Project seems to Fail:", project_id_))
      message(cond)
      my_var = as.character(cond)

    },
    finally={
      message(paste0('finished project: ', project_id_))
    }
  )
  if (!is.integer(my_var)){
    # Put failed project and the error message into the failed project list
    failed_sa_projects[[as.character(project_id_)]] = my_var
  }
}

print (paste0('Completed Stationary Acoustic Project Reports: ', completed_sa_projects))
print (paste0('Failed Stationary Acoustic Reports and their errors: ', failed_sa_projects))
usgs/nabatr documentation built on Jan. 28, 2024, 7:10 a.m.