Skip to content

Commit

Permalink
update with right coordinates
Browse files Browse the repository at this point in the history
  • Loading branch information
ptaconet committed Jun 24, 2024
1 parent 4a52a7e commit 3ed89c6
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions R/functions_import_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
if(output_class=="SpatRaster"){

files <- list.files(dir_path, full.names = T)
files <- files[!grepl("readme", files, ignore.case = T)]

rasts <- terra::rast(files)
terra::crs(rasts) <- "epsg:4326"
Expand Down Expand Up @@ -39,6 +40,7 @@
if(output_class=="SpatRaster"){

files <- list.files(dir_path, full.names = T)
files <- files[!grepl("readme", files, ignore.case = T)]

if(length(files)>1 & length(unique(substr(files,nchar(files)-9,nchar(files)-4)))>1 ){ # if there are multiple files (length(files)>1) but all with the same tile (length(unique(substr(files,nchar(files)-9,nchar(files)-4)))==1), we do not need to merge them

Expand Down
8 changes: 7 additions & 1 deletion R/mf_download_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@

mf_download_data<-function(df_to_dl,path=tempfile("tmp"),parallel=TRUE,num_workers=parallel::detectCores()-1,credentials=NULL,source="earthdata",verbose=TRUE,min_filesize=5000){

fileSize <- destfile <- fileDl <- NULL
fileSize <- destfile <- fileDl <- folders <- readme_files <- NULL

# tests
if(!inherits(verbose,"logical")){stop("verbose argument must be boolean\n")}
Expand Down Expand Up @@ -136,5 +136,11 @@ mf_download_data<-function(df_to_dl,path=tempfile("tmp"),parallel=TRUE,num_worke
if(verbose){cat("\nData were all properly downloaded under the folder(s) ",paste(as.character(unique(dirname(df_to_dl$destfile))), collapse=" and "),"\n")}
}

# write readme
folders <- unique(dirname(df_to_dl$destfile))
folders <- file.path(folders,"Readme.txt")
sentence <- "Use function modisfast::mf_import_data() to import the data in R."
readme_files <- purrr::map(folders,~writeLines(sentence, .))

return(data_dl)
}
30 changes: 28 additions & 2 deletions R/mf_get_opt_param.R
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ mf_get_opt_param<-function(collection,roi,credentials=NULL,verbose=TRUE){
purrr::map(.,~split(.,f = seq(nrow(.)))) %>%
purrr::modify_depth(.,2,~sf::st_bbox(.))

OpenDAPYVector <- purrr::flatten(OpenDAPYVector)
OpenDAPYVector <- purrr::flatten(OpenDAPYVector)
OpenDAPXVector <- purrr::flatten(OpenDAPXVector)
roi_div_bboxes <- purrr::flatten(roi_div_bboxes)
modis_tile <- purrr::flatten(modis_tile_numbers)
Expand All @@ -173,7 +173,33 @@ mf_get_opt_param<-function(collection,roi,credentials=NULL,verbose=TRUE){
# list_roiSpatialIndexBound <- purrr::map(list_roiSpatialIndexBound,~replace(.,. > 2399,2399))
#}

list_roiSpatialIndexBound <- purrr::map(list_roiSpatialIndexBound,~replace(.,.<= 10,0))

list_roiSpatialIndexBound <- map2(list_roiSpatialIndexBound, OpenDAPXVector, ~ {
x <- .x
len <- length(.y)

x[1] <- ifelse(x[1] <= 20, 0, ifelse(x[1] >= len - 20, len - 20, x[1]))
x[2] <- ifelse(x[2] <= 20, 20, ifelse(x[2] >= len - 20, len - 1, x[2]))
x[3] <- ifelse(x[3] <= 20, 0, ifelse(x[3] >= len - 20, len - 20, x[3]))
x[4] <- ifelse(x[4] <= 20, 20, ifelse(x[4] >= len - 20, len - 1, x[4]))

return(x)
})

## above is equal to :
# for(i in 1:length(list_roiSpatialIndexBound)){
#
# if(list_roiSpatialIndexBound[[i]][1] <= 20){ list_roiSpatialIndexBound[[i]][1] = 0}
# if(list_roiSpatialIndexBound[[i]][2] <= 20){ list_roiSpatialIndexBound[[i]][2] = 20}
# if(list_roiSpatialIndexBound[[i]][3] <= 20){ list_roiSpatialIndexBound[[i]][3] = 0}
# if(list_roiSpatialIndexBound[[i]][4] <= 20){ list_roiSpatialIndexBound[[i]][4] = 20}
#
# if(list_roiSpatialIndexBound[[i]][1] >= length(OpenDAPXVector[[i]])-20){ list_roiSpatialIndexBound[[i]][1] = length(OpenDAPXVector[[i]])-20}
# if(list_roiSpatialIndexBound[[i]][2] >= length(OpenDAPXVector[[i]])-20){ list_roiSpatialIndexBound[[i]][2] = length(OpenDAPXVector[[i]])-1}
# if(list_roiSpatialIndexBound[[i]][3] >= length(OpenDAPXVector[[i]])-20){ list_roiSpatialIndexBound[[i]][3] = length(OpenDAPXVector[[i]])-20}
# if(list_roiSpatialIndexBound[[i]][4] >= length(OpenDAPXVector[[i]])-20){ list_roiSpatialIndexBound[[i]][4] = length(OpenDAPXVector[[i]])-1}
#
# }

list_roiSpatialBound <- NULL
}
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ library(sf)
library(terra)
# ROI and time range of interest
roi <- st_as_sf(data.frame(id = "any_name", geom = "POLYGON ((-5.82 9.54, -5.42 9.55, -5.41 8.84, -5.81 8.84, -5.82 9.54))"), wkt="geom", crs = 4326) # a ROI of interest, format sf polygon
roi <- st_as_sf(data.frame(id = "roi_id", geom = "POLYGON ((-5.82 9.54, -5.42 9.55, -5.41 8.84, -5.81 8.84, -5.82 9.54))"), wkt="geom", crs = 4326) # a ROI of interest, format sf polygon
time_range <- as.Date(c("2017-01-01","2017-06-01")) # a time range of interest
# MODIS collections and variables (bands) of interest
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ library(sf)
library(terra)

# ROI and time range of interest
roi <- st_as_sf(data.frame(id = "any_name", geom = "POLYGON ((-5.82 9.54, -5.42 9.55, -5.41 8.84, -5.81 8.84, -5.82 9.54))"), wkt="geom", crs = 4326) # a ROI of interest, format sf polygon
roi <- st_as_sf(data.frame(id = "roi_id", geom = "POLYGON ((-5.82 9.54, -5.42 9.55, -5.41 8.84, -5.81 8.84, -5.82 9.54))"), wkt="geom", crs = 4326) # a ROI of interest, format sf polygon
time_range <- as.Date(c("2017-01-01","2017-06-01")) # a time range of interest

# MODIS collections and variables (bands) of interest
Expand Down

0 comments on commit 3ed89c6

Please sign in to comment.