Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible bug when downloading Sentinel 2 L1C with rsat_download() #19

Open
smckenzie1986 opened this issue Jul 29, 2024 · 2 comments
Open

Comments

@smckenzie1986
Copy link

smckenzie1986 commented Jul 29, 2024

Thanks so much for a great package with such intuitive syntax! This has been a lifeline with the retirement of sen2r. I am getting an error when I attempt to use the rsat_download() function:

Ordering on ESPA platform...
Error in (new("refMethodDef", .Data = function (espa_orders, db_path,  : 
  argument "espa_orders" is missing, with no default

It appears that there is an argument missing from an internal function and no argument to pass it directly from rsat_download()

Here is a reproducible example:

library(rsat)
library(terra)
library(sf)
library(tmap)
library(tmaptools)


xmin<-538078.1
xmax<-866945.5  
ymin<-773531.3
ymax<-1436377.3

AOI_ext<-ext(xmin, xmax, ymin, ymax)

AOI<-AOI_ext %>% st_bbox() %>% st_as_sfc() %>% st_as_sf(crs=6557) 


tmap_mode("view")

tm_shape(AOI)+
  tm_polygons(border.col="magenta", alpha=0, col=NA, lwd=2)+
  tm_basemap(server = providers$Esri.WorldImagery)

toi<-as.Date("2023-04-27")

db.path <- file.path(tempdir(),"database_test")
ds.path <- file.path(tempdir(),"datasets_test")

if(!dir.exists(db.path)){
  dir.create(db.path)
}

if(!dir.exists(ds.path)){
  dir.create(ds.path)
}

April<-new_rtoi(name="April_test2", region=AOI, db_path=db_path, rtoi_path = ds_path)
searchit<-rsat_search(region=April, product="S2MSI1C", dates=toi)
rsat_download(searchit, db_path=db_path)
@smckenzie1986
Copy link
Author

I looked at the source code, and was able to get the function work by removing the negated subsets when filtering by the API sources. Here is the working function:

rsat_download2<-function(x, db_path, verbose = FALSE, parallel=FALSE, ...) {
  require(rsat)
  
  args <- list(...)

  if (missing(db_path)){
    db_path <- get_database(x)
    if(db_path==""){
      stop("db_path or global environment database needed for image downloading.")
    }
  }

  # filter records
  
  x<-records(x)
  dataspace <- x[get_api_name(x)%in%"dataspace"]
  usgs <- x[get_api_name(x)%in%"usgs"]
  lpdaac <- x[get_api_name(x)%in%"lpdaac"]
  
  if(parallel){
    functions_list <- list(
     list(func = connection$getApi("lpdaac")$download_lpdaac_records,
           args = list(lpdaac_records=lpdaac,db_path=db_path,verbose=verbose,...)),
      list(func = rsat:::connection$getApi("dataspace")$dataspace_download_records,
           args = list(records=dataspace,db_path=db_path,verbose=verbose,...)),
      list(func = connection$getApi("usgs")$espa_order_and_download,
           args = list(usgs=usgs,db_path=db_path,verbose=verbose,...))
    )
    null.list <-mclapply(functions_list, function(entry) {
      do.call(entry$func, entry$args)
    }, mc.cores = 3)
  }else{
    functions_list <- list(
      list(func = rsat:::connection$getApi("usgs")$order_usgs_records,
           args = list(espa_orders=usgs,db_path=db_path,verbose=verbose,...)),
      list(func = rsat:::connection$getApi("lpdaac")$download_lpdaac_records,
           args = list(lpdaac_records=lpdaac,db_path=db_path,verbose=verbose,...)),
      list(func = rsat:::connection$getApi("dataspace")$dataspace_download_records,
           args = list(records=dataspace,db_path=db_path,verbose=verbose,...)),
      list(func = rsat:::connection$getApi("usgs")$download_espa_orders,
          args = list(espa.orders=usgs,db_path=db_path,verbose=verbose,...))
    )
    null.list <- lapply(functions_list, function(entry) {
      do.call(entry$func, entry$args)
    })
  }
}

The function now works without error:

library(rsat)
library(terra)
library(sf)
library(tmap)
library(tmaptools)


xmin<-538078.1
xmax<-866945.5  
ymin<-773531.3
ymax<-1436377.3

AOI_ext<-ext(xmin, xmax, ymin, ymax)

AOI<-AOI_ext %>% st_bbox() %>% st_as_sfc() %>% st_as_sf(crs=6557) 


tmap_mode("view")

tm_shape(AOI)+
  tm_polygons(border.col="magenta", alpha=0, col=NA, lwd=2)+
  tm_basemap(server = providers$Esri.WorldImagery)

toi<-as.Date("2023-04-27")

db.path <- file.path(tempdir(),"database_test")
ds.path <- file.path(tempdir(),"datasets_test")

if(!dir.exists(db.path)){
  dir.create(db.path)
}

if(!dir.exists(ds.path)){
  dir.create(ds.path)
}

April<-new_rtoi(name="April_test2", region=AOI, db_path=db_path, rtoi_path = ds_path)
rsat_search(region=April, product="S2MSI1C", dates=toi)
rsat_download2(April, db_path=db_path)

@olliewearn
Copy link

olliewearn commented Aug 14, 2024

I had the same "argument "espa_orders" is missing" bug and the function provided by @smckenzie1986 fixed it.

However, now I have a new potential bug.

> rsat_download2(gurvan)
Ordering on ESPA platform...
Downloading from lpdaac service...
Downloading from datasepace platform...
Downloading S2A_MSIL2A_20221017T040751_N0400_R047_T47TQJ_20221017T082302.SAFE image.
Error in curl_download(url = url, handle = handle, destfile = out.path) : 
  HTTP error 401.

I also tried the following, with a different error message:

> rsat_download2(records(gurvan), out.dir = get_database(gurvan))
Error in (function (classes, fdef, mtable)  : 
  unable to find an inherited method for function ‘get_database’ for signature ‘"records"’

Had anyone encountered these issues and know of a fix?

Addendum: when I try to download MODIS data using rsat_download2(), it works successfully. So the issue seems to be with Sentinel.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants