Skip to content

Windows: Issue with download_midday_images() #2

@CameronBodine

Description

@CameronBodine

Hi Bijan,

Here is issue we were noting in class today:

# open a temporary directory
tmp_dir = 'C:\\Users\\csb67\\Desktop\\test'

# download a subset. Example dukehw 2017
download_midday_images(site = 'dukehw', # which site
                       y = 2017, # which year(s)
                       months = 1:12, # which month(s)
                       days = 15, # which days on month(s)
                       download_dir = tmp_dir) # where on your computer

# list of downloaded files
duke_middays_path <- dir(tmp_dir, pattern = 'dukehw*', full.names = TRUE)

head(duke_middays_path)

Results in the following error (with traceback):

Error in basename(filepaths) : a character vector argument expected
4. basename(filepaths)
3. gsub(paste(".jpg", ".tiff", ".tif", ".dng", sep = "|"), "", basename(filepaths), ignore.case = TRUE)
2. parse_phenocam_filenames(midday_list)
1. download_midday_images(site = "dukehw", y = 2017, months = 1:12, days = 15, download_dir = tmp_dir)

This was Ivan's modified function that worked on windows:

download_midday_images_T3 <- function (site, 
                                    y = year(Sys.Date()),
                                    months = 1, days = 1, 
                                    download_dir, make_dir = FALSE) 
{
  midday_list <- get_midday_list(site) #, direct = FALSE)
  midday_listD <- get_midday_list(site, direct = FALSE)
  midday_table <- parse_phenocam_filenames(midday_list)
  download_list <- midday_table[midday_table$Year == y & midday_table$Month %in% 
                                  months & midday_table$Day %in% days, ]$filepaths
  n <- length(download_list)
  if (n == 0) {
    warning("no files to download!")
    return(NULL)
  }
  if (make_dir) {
    dir.create(download_dir, recursive = TRUE)
  }
  if (!dir.exists(download_dir)) {
    stop(download_dir, " directory does not exist!")
  }
  pb <- txtProgressBar(0, n, style = 3)
  for (i in 1:n) {
    download_url <- download_list[i]
    setTxtProgressBar(pb, i)
    destfile <- paste0(download_dir, "/", basename(download_url))
    
    if (file.exists(destfile)) {
      warning(destfile, " was already in ", download_dir)
      (next)()
    }
    
    tryDownload <- tryCatch(download.file(download_url, destfile = destfile, quiet = TRUE, 
                                          mode = "wb"), error = function(e) e )
    
    if ( any(grep('[Ee]rror', class(tryDownload)) ) ) {
      warning(download_url, " does not exist!")
      (next)()
    }

    # if (!RCurl::url.exists(download_url)) {
    #   warning(download_url, " does not exist!")
    #   (next)()
    # }
    # download.file(download_url, destfile = destfile, quiet = TRUE, 
    #               mode = "wb")
  }
  cat('\n')
  download_dir
}

And here is my sessionInfo():

> sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] lubridate_1.7.9   hazer_1.1.1       xROI_0.9.17       jpeg_0.1-8.1      phenocamapi_0.1.5 RCurl_1.98-1.2    rjson_0.2.20      data.table_1.13.0

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.5         compiler_4.0.2     later_1.1.0.1      colourpicker_1.1.0 moments_0.14       bitops_1.0-6       shinyjs_2.0.0     
 [8] tools_4.0.2        digest_0.6.25      evaluate_0.14      lattice_0.20-41    rlang_0.4.7        shiny_1.5.0        yaml_2.2.1        
[15] rgdal_1.5-16       xfun_0.16          fastmap_1.0.1      stringr_1.4.0      knitr_1.29         raster_3.3-13      generics_0.0.2    
[22] htmlwidgets_1.5.1  grid_4.0.2         R6_2.4.1           rmarkdown_2.3      sp_1.4-2           magrittr_1.5       promises_1.1.1    
[29] codetools_0.2-16   htmltools_0.5.0    mime_0.9           xtable_1.8-4       tiff_0.1-5         httpuv_1.5.4       stringi_1.4.6     
[36] miniUI_0.1.1.1  

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions