comparison preprocess_S2.r @ 0:9adccd3da70c draft default tip

planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
author ecology
date Mon, 09 Jan 2023 13:37:37 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9adccd3da70c
1 #Rscript
2
3 ###########################################
4 ## Preprocessing Sentinel 2 data ##
5 ###########################################
6
7 #####Packages : sen2r,
8 # jqr,
9 # protolite,
10 # raster,
11 # sf,
12 # rgeos,
13 # sp,
14 # raster,
15 # stars,
16 # stringr,
17 # progress,
18 # rgdal,
19 # R.utils,
20 # gdalUtils,
21 # fasterize,
22 # XML,
23 # XML2
24
25 #####Load arguments
26
27 args <- commandArgs(trailingOnly = TRUE)
28
29 if (length(args) < 1) {
30 stop("This tool needs at least 1 argument")
31 }else {
32 data <- args[1]
33 source(args[2])
34 data_source <- as.character(args[3])
35 sat_type <- as.character(args[4])
36 }
37
38 ##____________________________________________________________________##
39 ## Define where data is stored and where to write results ##
40 ##--------------------------------------------------------------------##
41
42 #Create a directory where to unzip your folder of data
43 dir.create("data_dir")
44 unzip(data, exdir = "data_dir")
45
46 # Result directory
47 result_path <- "results"
48 dir.create(path = result_path, showWarnings = FALSE, recursive = TRUE)
49
50 #Csv file for output useless but needed for linter
51 write.csv(data_source, "Mission.csv")
52
53 # define raster path
54 if (data_source == "SAFE") {
55 path_s2 <- file.path("data_dir", list.files("data_dir", pattern = ".SAFE"))
56 #To define the level and know if a correction is needed (convert not ready yet)
57 level_info <- get_s2_level(path_s2)
58 if (level_info == "L1C") {
59 stop("! This tool works for data of L2A level and NOT for the L1C level which is currently a work in progress !")
60 }
61 }else {
62 path_s2 <- file.path("data_dir")
63 }
64
65 ##____________________________________________________________________##
66 ## Extract, resample & stack data ##
67 ##--------------------------------------------------------------------##
68 # define resolution
69 resolution <- 10
70 # define source of data
71 s2source <- data_source
72
73 s2obj <- extract_from_s2_l2a(path_dir_s2 = path_s2,
74 path_vector = NULL,
75 s2source = s2source,
76 resolution = resolution)
77
78 ##____________________________________________________________________##
79 ## Write CLOUD MASK ##
80 ##--------------------------------------------------------------------##
81
82 # directory for cloud mask
83 cloud_path <- file.path(result_path, "CloudMask")
84 dir.create(path = cloud_path, showWarnings = FALSE, recursive = TRUE)
85 # Filename for cloud mask
86 cloudmasks <- save_cloud_s2(s2_stars = s2obj$s2_stack,
87 cloud_path = cloud_path,
88 s2source = s2source, saveraw = TRUE)
89
90 zip_cloud <- file.path("Cloud.zip")
91 zip::zip(zip_cloud, cloud_path)
92 ##____________________________________________________________________##
93 ## Write REFLECTANCE ##
94 ##--------------------------------------------------------------------##
95
96 # directory for Reflectance
97 refl_dir <- file.path(result_path, "Reflectance")
98 dir.create(path = refl_dir, showWarnings = FALSE, recursive = TRUE)
99
100 if (data_source == "SAFE") {
101 # filename for Reflectance
102 refl_path <- file.path(refl_dir, paste(basename(s2obj$s2_bands$GRANULE), "_Refl", sep = ""))
103
104 # Save Reflectance file as ENVI image with BIL interleaves
105 tile_s2 <- substring(strsplit(basename(s2obj$s2_bands$GRANULE), "_")[[1]][2], 2)
106 dateacq_s2 <- as.Date(substring(strsplit(basename(s2obj$s2_bands$GRANULE), "_")[[1]][4], 1, 8), format = "%Y%m%d")
107 }else {
108 # filename for Reflectance
109 refl_path <- file.path(refl_dir, paste(basename(s2obj$s2_bands$path_tile_s2), "_Refl", sep = ""))
110
111 # Save Reflectance file as ENVI image with BIL interleaves
112 tile_s2 <- substring(strsplit(basename(s2obj$s2_bands$path_tile_s2), "_")[[1]][2], 2)
113 dateacq_s2 <- as.Date(substring(strsplit(basename(s2obj$s2_bands$path_tile_s2), "_")[[1]][4], 1, 8), format = "%Y%m%d")
114 }
115
116 save_data <- save_reflectance_s2(s2_stars = s2obj$s2_stack, refl_path = refl_path,
117 s2sat = sat_type, tile_s2 = tile_s2, dateacq_s2 = dateacq_s2,
118 format = "ENVI", datatype = "Int16", mtd = s2obj$s2_bands$metadata, mtd_msi = s2obj$s2_bands$metadata_MSI)
119
120 zip_files <- file.path("Refl.zip")
121 zip::zip(zip_files, refl_dir)