annotate Lib_preprocess_S2.r @ 0:33a1e15f7252 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:05 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1 # == == == == == == == == == == == == == == == == == == == == == == == == == == ==
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
2 # preprocS2
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
3 # Lib_preprocess_S2.R
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
4 # == == == == == == == == == == == == == == == == == == == == == == == == == == ==
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
5 # PROGRAMMERS:
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
6 # Jean-Baptiste FERET <jb.feret@teledetection.fr>
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
7 # Copyright 2021/08 Jean-Baptiste FERET
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
8 # == == == == == == == == == == == == == == == == == == == == == == == == == == ==
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
9 # This Library contains functions to preprocess Sentinel-2 images downloaded from
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
10 # different data hubs, such as THEIA, PEPS or SCIHUB
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
11 # == == == == == == == == == == == == == == == == == == == == == == == == == == ==
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
12
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
13 #" This function adjusts information from ENVI header
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
14 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
15 #" @param dsn character. path where to store the stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
16 #" @param bands list. should include "bandname", and if possible "wavelength"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
17 #" @param sensor character. Name of the sensor used to acquire the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
18 #" @param stretch boolean. Set TRUE to get 10% stretching at display for reflectance, mentioned in hdr only
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
19 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
20 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
21 #" @importFrom utils read.table
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
22 #" @importFrom raster hdr raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
23 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
24 adjust_envi_hdr <- function(dsn, bands, sensor = "Unknown", stretch = FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
25
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
26 # Edit hdr file to add metadata
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
27 hdr <- read_envi_header(get_hdr_name(dsn))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
28 hdr$`band names` <- bands$bandname
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
29 if (length(bands$wavelength) == length(bands$bandname)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
30 hdr$wavelength <- bands$wavelength
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
31 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
32 hdr$wavelength <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
33 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
34 if (stretch == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
35 hdr$`default stretch` <- "0.000000 1000.000000 linear"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
36 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
37 hdr$`z plot range` <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
38 hdr$`data ignore value` <- "-Inf"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
39 hdr$`sensor type` <- sensor
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
40 write_envi_header(hdr = hdr, hdrpath = get_hdr_name(dsn))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
41
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
42 # remove unnecessary files
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
43 file2remove <- paste(dsn, ".aux.xml", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
44 if (file.exists(file2remove)) file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
45 file2remove <- paste(dsn, ".prj", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
46 if (file.exists(file2remove)) file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
47 file2remove <- paste(dsn, ".stx", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
48 if (file.exists(file2remove)) file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
49 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
50 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
51
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
52 #" This function saves reflectance files
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
53 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
54 #" @param s2sat character. Sentinel-2 mission ("2A" or "2B")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
55 #" @param tile_s2 character. S2 tile name (2 numbers + 3 letters)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
56 #" @param dateacq_s2 double. date of acquisition
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
57 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
58 #" @return s2mission character. name of the S2 mission (2A or 2B)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
59 #" @importFrom sen2r safe_getMetadata check_scihub_connection s2_list
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
60 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
61 check_s2mission <- function(s2sat, tile_s2, dateacq_s2) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
62
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
63 # is mission already defined by user?
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
64 if (!is.null(s2sat)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
65 if (s2sat == "2A") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
66 s2mission <- "2A"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
67 }else if (s2sat == "2B") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
68 s2mission <- "2B"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
69 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
70 message("Could not identify if image from Sentinel-2A or -2B")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
71 message("Defining central wavelength of spectral bands based on S2A")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
72 s2mission <- "2A"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
73 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
74 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
75 message("Could not identify if image from Sentinel-2A or -2B")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
76 message("Defining central wavelength of spectral bands based on S2A")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
77 s2mission <- "2A"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
78 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
79 return(s2mission)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
80 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
81
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
82 #" this function aims at computing directory size
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
83 #" @param path character. path for directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
84 #" @param recursive boolean . set T if recursive
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
85 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
86 #" @return size_files numeric. size in bytes
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
87 #" - image stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
88 #" - path for individual band files corresponding to the stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
89 #" - path for vector (reprojected if needed)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
90 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
91 #" @importFrom raster raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
92 #" @importFrom tools file_path_sans_ext file_ext
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
93 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
94 dir_size <- function(path, recursive = TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
95 stopifnot(is.character(path))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
96 files <- list.files(path, full.names = TRUE, recursive = recursive)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
97 vect_size <- sapply(files, function(x) file.size(x))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
98 size_files <- sum(vect_size)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
99 return(size_files)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
100 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
101
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
102 #" This function reads S2 data from L2A directories downloaded from
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
103 #" various data hubs including THEIA, PEPS & SCIHUB (SAFE format & LaSRC)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
104 #" @param path_dir_s2 character. path for S2 directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
105 #" @param path_vector character. path for vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
106 #" @param s2source character. type of directory format (depends on atmospheric correction: SAFE produced from Sen2Cor)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
107 #" @param resolution numeric. buffer applied to vector file (in meters)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
108 #" @param interpolation character. method for resampling. default = "bilinear"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
109 #" @param fre_sre character. SRE or FRE products from THEIA
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
110 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
111 #" @return listout list.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
112 #" - image stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
113 #" - path for individual band files corresponding to the stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
114 #" - path for vector (reprojected if needed)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
115 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
116 #" @importFrom raster raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
117 #" @importFrom tools file_path_sans_ext file_ext
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
118 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
119 extract_from_s2_l2a <- function(path_dir_s2, path_vector = NULL, s2source = "SAFE",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
120 resolution = 10, interpolation = "bilinear", fre_sre = "FRE") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
121 # Get list of paths corresponding to S2 bands and depending on S2 directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
122 s2_bands <- get_s2_bands(path_dir_s2 = path_dir_s2,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
123 s2source = s2source,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
124 resolution = resolution,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
125 fre_sre = fre_sre)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
126
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
127 if (length(s2_bands$s2bands_10m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
128 rastmp <- raster::raster(s2_bands$s2bands_10m[[1]])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
129 } else if (length(s2_bands$s2bands_20m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
130 rastmp <- raster::raster(s2_bands$s2bands_20m[[1]])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
131 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
132 # check if vector and raster share the same projection. if not, re-project vector
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
133 if (!is.null(path_vector)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
134 raster_proj <- raster::projection(rastmp)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
135 path_vector_reproj <- paste(tools::file_path_sans_ext(path_vector), "_reprojected.shp", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
136 path_vector <- reproject_shp(path_vector_init = path_vector,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
137 newprojection = raster_proj,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
138 path_vector_reproj = path_vector_reproj)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
139 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
140 # Extract data corresponding to the vector footprint (if provided) & resample data if needed
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
141 if (length(s2_bands$s2bands_10m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
142 stack_10m <- read_s2bands(s2_bands = s2_bands$s2bands_10m, path_vector = path_vector,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
143 resampling = 1, interpolation = interpolation)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
144 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
145 if (length(s2_bands$s2bands_20m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
146 if (resolution == 10 && s2source != "LaSRC") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
147 resampling <- 2
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
148 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
149 resampling <- 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
150 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
151 stack_20m <- read_s2bands(s2_bands = s2_bands$s2bands_20m, path_vector = path_vector,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
152 resampling = resampling, interpolation = interpolation)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
153 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
154 # get full stack including 10m and 20m spatial resolution
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
155 if (length(s2_bands$s2bands_10m) > 0 && length(s2_bands$s2bands_20m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
156 diffxstart <- attributes(stack_10m)$dimensions[[1]]$from - attributes(stack_20m)$dimensions[[1]]$from
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
157 diffxstop <- attributes(stack_10m)$dimensions[[1]]$to - attributes(stack_20m)$dimensions[[1]]$to
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
158 diffystart <- attributes(stack_10m)$dimensions[[2]]$from - attributes(stack_20m)$dimensions[[2]]$from
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
159 diffystop <- attributes(stack_10m)$dimensions[[2]]$to - attributes(stack_20m)$dimensions[[2]]$to
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
160 if (!diffxstop == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
161 # size of 20m > size of 10m --> reduce 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
162 # size of 10m > size of 20m --> reduce 10m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
163 if (diffxstop > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
164 stack_10m <- stack_10m[, 1:(dim(stack_10m)[1] - diffxstop), , ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
165 }else if (diffxstop < 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
166 stack_20m <- stack_20m[, 1:(dim(stack_20m)[1] + diffxstop), , ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
167 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
168 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
169 if (!diffystop == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
170 if (diffystop > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
171 stack_10m <- stack_10m[, , 1:(dim(stack_10m)[2] - diffystop), ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
172 }else if (diffystop < 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
173 stack_20m <- stack_20m[, , 1:(dim(stack_20m)[2] + diffystop), ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
174 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
175 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
176 if (!diffxstart == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
177 if (diffxstart > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
178 stack_20m <- stack_20m[, (1 + diffxstart):dim(stack_20m)[1], , ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
179 }else if (diffxstart < 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
180 stack_10m <- stack_10m[, (1 - diffxstart):dim(stack_10m)[1], , ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
181 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
182 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
183 if (!diffystart == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
184 if (diffystart > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
185 stack_20m <- stack_20m[, , (1 + diffystart):dim(stack_20m)[2], ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
186 }else if (diffystart < 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
187 stack_10m <- stack_10m[, , (1 - diffystart):dim(stack_10m)[2], ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
188 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
189 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
190 # reorder bands with increasing wavelength
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
191 s2bands <- c("B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12", "Cloud")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
192 namebands <- c(names(s2_bands$s2bands_10m), names(s2_bands$s2bands_20m))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
193 reorder_bands <- match(s2bands, namebands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
194 namebands <- namebands[reorder_bands]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
195 listfiles <- c(stack_10m$attr, stack_20m$attr)[reorder_bands]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
196
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
197 # adjust size to initial vector footprint without buffer
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
198 # --> buffer is needed in order to ensure that extraction following
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
199 # footprint of vector matches for images of different spatial resolution
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
200 # get bounding box corresponding to footprint of image or image subset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
201 bb_xycoords <- get_bb(path_raster = listfiles[1],
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
202 path_vector = path_vector, buffer = 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
203
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
204 # prepare reading data for extent defined by bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
205 nxoff <- bb_xycoords$UL$col
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
206 nyoff <- bb_xycoords$UL$row
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
207 nxsize <- bb_xycoords$UR$col - bb_xycoords$UL$col + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
208 nysize <- bb_xycoords$LR$row - bb_xycoords$UR$row + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
209 nbufxsize <- nxsize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
210 nbufysize <- nysize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
211 s2_stack <- stars::read_stars(listfiles, along = "band",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
212 RasterIO = list(nXOff = nxoff, nYOff = nyoff,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
213 nXSize = nxsize, nYSize = nysize,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
214 nBufXSize = nbufxsize, nBufYSize = nbufysize,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
215 resample = "nearest_neighbour"), proxy = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
216
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
217
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
218 names(s2_stack$attr) <- namebands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
219 }else if (length(s2_bands$s2bands_10m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
220 s2_stack <- stack_10m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
221 namebands <- names(s2_bands$s2bands_10m)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
222 names(s2_stack$attr) <- namebands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
223 }else if (length(s2_bands$s2bands_20m) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
224 s2_stack <- stack_20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
225 namebands <- names(s2_bands$s2bands_20m)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
226 names(s2_stack$attr) <- namebands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
227 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
228
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
229 listout <- list("s2_stack" = s2_stack, "s2_bands" = s2_bands, "path_vector" = path_vector,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
230 "namebands" = namebands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
231 return(listout)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
232 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
233
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
234 #" This function gets coordinates of a bounding box defined by a vector (optional) and a raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
235 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
236 #" @param path_raster character. path for raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
237 #" @param path_vector character. path for vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
238 #" @param buffer numeric. buffer applied to vector file (in meters)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
239 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
240 #" @return bb_xycoords list. Coordinates (in pixels) of the upper/lower right/left corners of bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
241 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
242 get_bb <- function(path_raster, path_vector = NULL, buffer = 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
243
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
244 if (!is.null(path_vector)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
245 # get bounding box with a 50m buffer in order to allow for interpolation
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
246 bb_xycoords <- get_bb_from_vector(path_raster = path_raster,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
247 path_vector = path_vector,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
248 buffer = buffer)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
249 }else if (is.null(path_vector)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
250 bb_xycoords <- get_bb_from_fullimage(path_raster)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
251 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
252 return(bb_xycoords)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
253 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
254
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
255 #" This function gets extreme coordinates of a bounding box corresponding to a full image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
256 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
257 #" @param path_raster character. path for raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
258 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
259 #" @return bb_xycoords list. Coordinates (in pixels) of the upper/lower right/left corners of bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
260 #" @importFrom raster raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
261 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
262 get_bb_from_fullimage <- function(path_raster) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
263 # get raster coordinates corresponding to Full image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
264 rasterobj <- raster::raster(path_raster)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
265 bb_xycoords <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
266 bb_xycoords[["UL"]] <- data.frame("row" = 1, "col" = 1)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
267 bb_xycoords[["UR"]] <- data.frame("row" = 1, "col" = dim(rasterobj)[2])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
268 bb_xycoords[["LL"]] <- data.frame("row" = dim(rasterobj)[1], "col" = 1)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
269 bb_xycoords[["LR"]] <- data.frame("row" = dim(rasterobj)[1], "col" = dim(rasterobj)[2])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
270 return(bb_xycoords)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
271 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
272
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
273 #" This gets bounding box corresponding to a vector from a raster (UL, UR, LL, LR corners)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
274 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
275 #" @param path_raster character. path for raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
276 #" @param path_vector character. path for vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
277 #" @param buffer numeric. buffer applied to vector file (in meters)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
278 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
279 #" @return bb_xycoords list. Coordinates (in pixels) of the upper/lower right/left corners of bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
280 #" @importFrom sf st_read st_bbox st_crop
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
281 #" @importFrom rgeos gbuffer bbox2SP
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
282 #" @importFrom sp SpatialPoints bbox
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
283 #" @importFrom raster projection extract extent raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
284 #" @importFrom methods as
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
285 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
286 get_bb_from_vector <- function(path_raster, path_vector, buffer = 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
287
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
288 data_raster <- raster::raster(path_raster)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
289 # extract BB coordinates from vector
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
290 bb_vector <- rgeos::gbuffer(spgeom = as(sf::st_read(dsn = path_vector, quiet = TRUE), "Spatial"),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
291 width = buffer, byid = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
292 # extract BB coordinates from raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
293 bb_raster <- rgeos::bbox2SP(bbox = bbox(data_raster))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
294 # compute intersection
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
295 intersect <- rgeos::gIntersection(bb_vector, bb_raster)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
296 bbext <- raster::extent(intersect)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
297 xmin <- bbext[1]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
298 xmax <- bbext[2]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
299 ymin <- bbext[3]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
300 ymax <- bbext[4]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
301 # get coordinates of bounding box corresponding to vector
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
302 corners <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
303 corners[["UR"]] <- sp::SpatialPoints(coords = cbind(xmax, ymax))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
304 corners[["LR"]] <- sp::SpatialPoints(coords = cbind(xmax, ymin))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
305 corners[["UL"]] <- sp::SpatialPoints(coords = cbind(xmin, ymax))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
306 corners[["LL"]] <- sp::SpatialPoints(coords = cbind(xmin, ymin))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
307 raster::projection(corners[["UL"]]) <- raster::projection(corners[["UR"]]) <-
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
308 raster::projection(corners[["LL"]]) <- raster::projection(corners[["LR"]]) <-
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
309 raster::projection(sf::st_read(dsn = path_vector, quiet = TRUE))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
310 # get coordinates for corners of bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
311 bb_xycoords <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
312 for (corner in names(corners)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
313 ex_df <- as.data.frame(raster::extract(data_raster, corners[[corner]], cellnumbers = TRUE))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
314 colrow <- ind2sub(data_raster, ex_df$cell)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
315 bb_xycoords[[corner]] <- data.frame("row" = colrow$row, "col" = colrow$col)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
316 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
317 return(bb_xycoords)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
318 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
319
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
320 #" get hdr name from image file name, assuming it is BIL format
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
321 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
322 #" @param impath path of the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
323 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
324 #" @return corresponding hdr
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
325 #" @import tools
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
326 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
327 get_hdr_name <- function(impath) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
328 if (tools::file_ext(impath) == "") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
329 impathhdr <- paste(impath, ".hdr", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
330 }else if (tools::file_ext(impath) == "bil") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
331 impathhdr <- gsub(".bil", ".hdr", impath)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
332 }else if (tools::file_ext(impath) == "zip") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
333 impathhdr <- gsub(".zip", ".hdr", impath)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
334 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
335 impathhdr <- paste(tools::file_path_sans_ext(impath), ".hdr", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
336 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
337
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
338 if (!file.exists(impathhdr)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
339 message("WARNING : COULD NOT FIND hdr FILE")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
340 print(impathhdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
341 message("Process may stop")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
342 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
343 return(impathhdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
344 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
345
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
346 #" This function returns path for the spectral bands to be used
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
347 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
348 #" @param path_dir_s2 character. Path for the directory containing S2 data. either L2A .SAFE S2 file or THEIA directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
349 #" @param s2source character. defines if data comes from SciHub as SAFE directory, from THEIA or from LaSRC
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
350 #" @param resolution numeric. spatial resolution of the final image: 10m or 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
351 #" @param fre_sre character. SRE or FRE products from THEIA
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
352 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
353 #" @return listbands list. contains path for spectral bands corresponding to 10m and 20m resolution
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
354 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
355 get_s2_bands <- function(path_dir_s2, s2source = "SAFE", resolution = 10, fre_sre = "FRE") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
356
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
357 if (s2source == "SAFE" || s2source == "Sen2Cor") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
358 listbands <- get_s2_bands_from_sen2cor(path_dir_s2 = path_dir_s2, resolution = resolution)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
359 }else if (s2source == "THEIA") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
360 listbands <- get_s2_bands_from_theia(path_dir_s2 = path_dir_s2, resolution = resolution,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
361 fre_sre = fre_sre)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
362 }else if (s2source == "LaSRC") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
363 listbands <- get_s2_bands_from_lasrc(path_dir_s2 = path_dir_s2, resolution = resolution)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
364 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
365 message("The data source (Atmospheric correction) for Sentinel-2 image is unknown")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
366 message("Please provide S2 images from one of the following data sources:")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
367 message("- LaSRC (atmospheric correction: LaSRC)")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
368 message("- THEIA (atmospheric correction: MAJA)")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
369 message("- SAFE (atmospheric correction: Sen2Cor)")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
370 s2bands_10m <- s2bands_20m <- granule <- mtdfile <- metadata_msi <- metadata_lasrc <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
371 listbands <- list("s2bands_10m" = s2bands_10m, "s2bands_20m" = s2bands_20m, "GRANULE" = granule,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
372 "metadata" = mtdfile, "metadata_MSI" = metadata_msi,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
373 "metadata_lasrc" = metadata_lasrc)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
374 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
375 return(listbands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
376 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
377
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
378 #" This function returns path for the spectral bands in SAFE / sen2Cor directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
379 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
380 #" @param path_dir_s2 character. Path for the SAFE directory containing S2 data
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
381 #" @param resolution numeric. spatial resolution of the final image: 10m or 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
382 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
383 #" @return listbands list. contains path for spectral bands corresponding to 10m and 20m resolution, as well name of as granule
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
384 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
385 get_s2_bands_from_sen2cor <- function(path_dir_s2, resolution = 10) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
386 # build path for all bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
387 if (resolution == 10) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
388 b10m <- c("B02", "B03", "B04", "B08")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
389 b20m <- c("B05", "B06", "B07", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
390 }else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
391 b10m <- c()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
392 b20m <- c("B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
393 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
394 # get granule directory & path for corresponding metadata XML file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
395 granule <- list.dirs(list.dirs(path_dir_s2, recursive = FALSE)[grep(pattern = "GRANULE",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
396 x = list.dirs(path_dir_s2, recursive = FALSE))], recursive = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
397 mtdfile <- file.path(granule, "MTD_TL.xml")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
398 if (file.exists(file.path(path_dir_s2, "MTD_MSIL2A.xml"))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
399 mtd_msi_file <- file.path(path_dir_s2, "MTD_MSIL2A.xml")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
400 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
401 mtd_msi_file <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
402 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
403
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
404 # Define path for bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
405 s2bands_20m_dir <- file.path(granule, "IMG_DATA", "R20m")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
406 s2bands_10m_dir <- file.path(granule, "IMG_DATA", "R10m")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
407 s2bands_10m <- s2bands_20m <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
408 for (band in b20m) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
409 s2bands_20m[[band]] <- file.path(s2bands_20m_dir, list.files(s2bands_20m_dir, pattern = band))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
410 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
411 for (band in b10m) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
412 s2bands_10m[[band]] <- file.path(s2bands_10m_dir, list.files(s2bands_10m_dir, pattern = band))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
413 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
414 # get cloud mask
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
415 cloud <- "MSK_CLDPRB_20m"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
416 cloud_20m_dir <- file.path(granule, "QI_DATA")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
417 s2bands_20m[["Cloud"]] <- file.path(cloud_20m_dir, list.files(cloud_20m_dir, pattern = cloud))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
418 listbands <- list("s2bands_10m" = s2bands_10m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
419 "s2bands_20m" = s2bands_20m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
420 "GRANULE" = granule,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
421 "metadata" = mtdfile,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
422 "metadata_MSI" = mtd_msi_file,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
423 "metadata_lasrc" = NULL)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
424 return(listbands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
425 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
426
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
427 #" This function returns path for the spectral bands in LaSRC directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
428 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
429 #" @param path_dir_s2 character. Path for the SAFE directory containing S2 data
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
430 #" @param resolution numeric. spatial resolution of the final image: 10m or 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
431 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
432 #" @return listbands list. contains path for spectral bands corresponding to 10m and 20m resolution, as well name of as granule
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
433 #" @importFrom stringr str_subset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
434 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
435 get_s2_bands_from_lasrc <- function(path_dir_s2, resolution = 10) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
436
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
437 # get granule directory & path for corresponding metadata XML file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
438 granule <- path_dir_s2
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
439 mtdfile <- file.path(granule, "MTD_TL.xml")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
440 if (file.exists(file.path(path_dir_s2, "MTD_MSIL1C.xml"))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
441 mtd_msi_file <- file.path(path_dir_s2, "MTD_MSIL1C.xml")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
442 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
443 mtd_msi_file <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
444 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
445
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
446 # build path for all bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
447 b10m <- c("band2", "band3", "band4", "band5", "band6", "band7", "band8", "band8a", "band11", "band12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
448 b10m_standard <- c("B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
449 # Define path for bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
450 s2bands_10m <- s2bands_20m <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
451 for (i in 1:seq_along(b10m)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
452 s2bands_10m[[b10m_standard[i]]] <- file.path(path_dir_s2,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
453 list.files(path_dir_s2,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
454 pattern = paste(b10m[i], ".tif", sep = "")))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
455 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
456
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
457 # get metadata file containing offset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
458 mtd_lasrc <- str_subset(list.files(path_dir_s2, pattern = "S2"), ".xml$")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
459 if (file.exists(file.path(path_dir_s2, mtd_lasrc))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
460 metadata_lasrc <- file.path(path_dir_s2, mtd_lasrc)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
461 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
462 metadata_lasrc <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
463 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
464 # get cloud mask
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
465 cloud <- "CLM"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
466 s2bands_10m[["Cloud"]] <- file.path(path_dir_s2, list.files(path_dir_s2, pattern = cloud))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
467 listbands <- list("s2bands_10m" = s2bands_10m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
468 "s2bands_20m" = s2bands_20m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
469 "GRANULE" = granule,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
470 "metadata" = mtdfile,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
471 "metadata_MSI" = mtd_msi_file,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
472 "metadata_lasrc" = metadata_lasrc)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
473 return(listbands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
474 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
475
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
476 #" This function returns path for the spectral bands in THEIA directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
477 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
478 #" @param path_dir_s2 character. Path for the SAFE directory containing S2 data
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
479 #" @param resolution numeric. spatial resolution of the final image: 10m or 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
480 #" @param fre_sre character. SRE or FRE products from THEIA
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
481 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
482 #" @return listbands list. contains path for spectral bands corresponding to 10m and 20m resolution, as well name of as granule
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
483 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
484 get_s2_bands_from_theia <- function(path_dir_s2, resolution = 10, fre_sre = "FRE") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
485
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
486 # build path for all bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
487 if (resolution == 10) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
488 b10m <- c("B02", "B03", "B04", "B08")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
489 b20m <- c("B05", "B06", "B07", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
490 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
491 b10m <- c()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
492 b20m <- c("B02", "B03", "B04", "B05", "B06", "B07", "B08", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
493 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
494
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
495 # get path_tile_s2 directory & path for corresponding metadata XML file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
496 path_tile_s2 <- list.dirs(path_dir_s2, recursive = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
497 files_tile_s2 <- list.files(path_tile_s2, recursive = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
498 mtdfile <- file.path(path_tile_s2, files_tile_s2[grep(pattern = "MTD_ALL.xml", x = files_tile_s2)])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
499
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
500 # Define path for bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
501 s2bands_10m_dir <- s2bands_20m_dir <- path_tile_s2
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
502 s2bands_10m <- s2bands_20m <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
503 for (band in b20m) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
504 band_20m_pattern <- paste0(gsub("0", "", band), ".tif") # for THEAI band 2 is "B2" ("B02" for SAFE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
505 list_files_20m <- list.files(s2bands_20m_dir, pattern = band_20m_pattern)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
506 s2bands_20m[[band]] <- file.path(s2bands_20m_dir, list_files_20m)[grep(pattern = fre_sre,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
507 x = file.path(s2bands_20m_dir, list_files_20m))]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
508 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
509 for (band in b10m) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
510 band_10m_pattern <- paste0(gsub("0", "", band), ".tif") # for THEAI band 2 is "B2" ("B02" for SAFE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
511 list_files_10m <- list.files(s2bands_10m_dir, pattern = band_10m_pattern)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
512 s2bands_10m[[band]] <- file.path(s2bands_10m_dir, list_files_10m)[grep(pattern = fre_sre,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
513 x = file.path(s2bands_10m_dir, list_files_10m))]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
514 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
515
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
516 # get cloud mask 10m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
517 cloud_10m <- "CLM_R1"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
518 cloud_10m_dir <- file.path(path_tile_s2, "MASKS")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
519 s2bands_10m[["Cloud"]] <- file.path(cloud_10m_dir, list.files(cloud_10m_dir, pattern = cloud_10m))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
520
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
521 # get cloud mask 20m
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
522 cloud_20m <- "CLM_R2"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
523 cloud_20m_dir <- file.path(path_tile_s2, "MASKS")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
524 s2bands_20m[["Cloud"]] <- file.path(cloud_20m_dir, list.files(cloud_20m_dir, pattern = cloud_20m))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
525
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
526 # return list bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
527 listbands <- list("s2bands_10m" = s2bands_10m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
528 "s2bands_20m" = s2bands_20m,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
529 "path_tile_s2" = path_tile_s2,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
530 "metadata" = mtdfile)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
531 return(listbands)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
532 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
533
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
534 #" This function check S2 data level:
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
535 #" - L2A: already atmospherically corrected
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
536 #" - L1C: requires atmospheric corrections with sen2cor
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
537 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
538 #" @param prodname character. original name for the S2 image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
539 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
540 #" @return s2level character. S2 level: L1C or L2A
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
541 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
542 get_s2_level <- function(prodname) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
543 prodname <- basename(prodname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
544 if (length(grep(pattern = "L1C_", x = prodname)) == 1) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
545 s2level <- "L1C"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
546 } else if (length(grep(pattern = "L2A_", x = prodname)) == 1) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
547 s2level <- "L2A"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
548 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
549 return(s2level)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
550 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
551
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
552 #" This function gets tile from S2 image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
553 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
554 #" @param prodname character. original name for the S2 image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
555 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
556 #" @return tilename character
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
557 #" @importFrom tools file_path_sans_ext
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
558 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
559 get_tile <- function(prodname) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
560 prodname <- basename(prodname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
561 tilename <- tools::file_path_sans_ext(gsub("_.*", "", gsub(".*_T", "", prodname)))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
562 return(tilename)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
563 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
564
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
565 #" This function gets acquisition date from S2 image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
566 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
567 #" @param prodname character. original name for the S2 image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
568 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
569 #" @return dateacq character
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
570 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
571 get_date <- function(prodname) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
572 prodname <- basename(prodname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
573 dateacq <- as.Date(gsub("T.*", "", gsub(".*_20", "20", prodname)), format = "%Y%m%d")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
574 return(dateacq)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
575 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
576
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
577 #" download S2 L1C data from Copernicus hub or Google cloud
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
578 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
579 #" @param list_safe safe object. produced with sen2r::s2_list
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
580 #" @param l1c_path character. path for storage of L1C image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
581 #" @param path_vector path for a vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
582 #" @param time_interval dates. time interval for S2 query
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
583 #" @param googlecloud boolean. set to TRUE if google cloud SDK is installed and
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
584 #" @param forcegoogle boolean. set to TRUE if only google requested
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
585 #" sen2r configured as an alternative hub for S2 download
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
586 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
587 #" @return prodname character. S2 Product name
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
588 #" @importFrom sen2r safe_is_online s2_list s2_download s2_order check_gcloud
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
589 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
590 get_s2_l1c_image <- function(list_safe, l1c_path, path_vector, time_interval,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
591 googlecloud = FALSE, forcegoogle = FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
592 # Check if available from Copernicus hub first
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
593 copernicus_avail <- sen2r::safe_is_online(list_safe)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
594 # if available: download
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
595 prodname <- attr(list_safe, which = "name")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
596 if (file.exists(file.path(l1c_path, prodname))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
597 message("L1C file already downloaded")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
598 message(file.path(l1c_path, prodname))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
599 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
600 if (copernicus_avail == TRUE && forcegoogle == FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
601 sen2r::s2_download(list_safe, outdir = l1c_path)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
602 } else if (copernicus_avail == FALSE || forcegoogle == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
603 # if not available and googlecloud==TRUE
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
604 if (googlecloud == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
605 # check if google cloud SDK available from this computer
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
606 ggc <- sen2r::check_gcloud()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
607 if (ggc == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
608 message("downloading from Google cloud")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
609 list_safe_ggc <- sen2r::s2_list(spatial_extent = sf::st_read(dsn = path_vector),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
610 time_interval = time_interval,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
611 server = "gcloud")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
612 prodname <- attr(list_safe_ggc, which = "name")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
613 if (file.exists(file.path(l1c_path, prodname))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
614 message("L1C file already downloaded")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
615 message(file.path(l1c_path, prodname))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
616 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
617 sen2r::s2_download(list_safe_ggc, outdir = l1c_path)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
618 # check if QI_DATA exists in DATASTRIP, and create it if not the case
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
619 datastrip_path <- file.path(l1c_path, prodname, "DATASTRIP")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
620 dsdir <- list.dirs(datastrip_path, recursive = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
621 if (length(match(list.dirs(dsdir, recursive = FALSE, full.names = FALSE), "QI_DATA")) == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
622 dir.create(file.path(dsdir, "QI_DATA"))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
623 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
624 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
625 } else if (ggc == FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
626 message("googlecloud set to TRUE but missing")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
627 message("Please install Google cloud SDK")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
628 message("https://cloud.google.com/sdk/docs/install")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
629 message("and/or set configuration of sen2r following instructions")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
630 message("https://www.r-bloggers.com/2021/06/downloading-sentinel-2-archives-from-google-cloud-with-sen2r/")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
631 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
632 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
633 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
634 if (copernicus_avail == FALSE && googlecloud == FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
635 message("S2 image in Long Term Archive (LTA)")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
636 message("Ordering image from LTA")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
637 message("This may take 1 day, please run your script later")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
638 orders2 <- sen2r::s2_order(list_safe)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
639 message("An alternative is possible with Google cloud SDK")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
640 message("https://cloud.google.com/sdk/docs/install")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
641 message("and/or set configuration of sen2r following instructions")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
642 message("https://www.r-bloggers.com/2021/06/downloading-sentinel-2-archives-from-google-cloud-with-sen2r/")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
643 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
644 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
645 return(prodname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
646 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
647
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
648 #" download S2 L2A data from Copernicus hub or convert L1C to L2A
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
649 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
650 #" @param l2a_path character. path for storage of L2A image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
651 #" @param spatial_extent path for a vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
652 #" @param dateacq character. date of acquisition
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
653 #" @param deletel1c Boolean. set TRUE to delete L1C images
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
654 #" @param Sen2Cor Boolean. set TRUE to automatically perform atmospheric corrections using sen2Cor
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
655 #" @param googlecloud boolean. set to TRUE if google cloud SDK is installed and
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
656 #" sen2r configured as an alternative hub for S2 download
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
657 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
658 #" @return pathl2a character. Path for L2A image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
659 #" @importFrom sen2r s2_list s2_download
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
660 #" @importFrom R.utils getAbsolutePath
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
661
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
662 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
663 get_s2_l2a_image <- function(l2a_path, spatial_extent, dateacq,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
664 deletel1c = FALSE, sen2cor = TRUE,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
665 googlecloud = FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
666
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
667 # Needs to be updated: define path for L1c data
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
668 l1c_path <- l2a_path
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
669 # define time interval
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
670 time_interval <- as.Date(c(dateacq, dateacq))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
671 # get list S2 products corresponding to study area and date of interest using sen2r package
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
672 if (googlecloud == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
673 server <- c("scihub", "gcloud")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
674 } else if (googlecloud == FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
675 server <- "scihub"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
676 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
677 list_safe <- sen2r::s2_list(spatial_extent = sf::st_read(dsn = spatial_extent),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
678 time_interval = time_interval,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
679 server = server, availability = "check")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
680 # download products
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
681 sen2r::s2_download(list_safe, outdir = l2a_path)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
682 # name all products
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
683 prodname <- attr(list_safe, which = "name")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
684 prodfullpath <- file.path(l2a_path, prodname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
685 if (sen2cor == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
686 for (imgname in prodname) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
687 s2level <- get_s2_level(imgname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
688 if (s2level == "L1C") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
689 datepattern <- gsub(pattern = "-", replacement = "", x = dateacq)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
690 pathl2a <- s2_from_l1c_to_l2a(prodname = imgname, l1c_path = l2a_path, l2a_path = l2a_path,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
691 datepattern = datepattern, tmp_path = NULL)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
692 if (deletel1c == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
693 unlink(x = R.utils::getAbsolutePath(file.path(l1c_path, prodname)),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
694 recursive = TRUE, force = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
695 # delete from full path and add atmospherically corrected
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
696 whichimg <- grep(x = prodfullpath, pattern = imgname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
697 dateacq <- get_date(imgname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
698 tilename <- get_tile(imgname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
699 pathl2a <- list.files(path = l2a_path, pattern = tilename, full.names = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
700 pathl2a <- pathl2a[grep(x = pathl2a, pattern = dateacq)]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
701 pathl2a <- pathl2a[grep(x = basename(pathl2a), pattern = "L2A")]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
702 prodfullpath[whichimg] <- pathl2a
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
703 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
704 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
705 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
706 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
707
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
708 return(prodfullpath)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
709 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
710
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
711 #" convert image coordinates from index to X-Y
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
712 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
713 #" @param Raster image raster object
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
714 #" @param image_index coordinates corresponding to the raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
715 ind2sub <- function(data_raster, image_index) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
716 c <- ((image_index - 1) %% data_raster@ncols) + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
717 r <- floor((image_index - 1) / data_raster@ncols) + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
718 my_list <- list("col" = c, "row" = r)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
719 return(my_list)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
720 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
721
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
722 #" mosaicing a set of rasters
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
723 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
724 #" @param list_rasters character. list of paths corresponding to rasters to mosaic
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
725 #" @param dst_mosaic character. path and name of mosaic produced
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
726 #" @param stretch boolean. Set TRUE to get 10% stretching at display for reflectance, mentioned in hdr only
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
727 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
728 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
729 #" @importFrom gdalUtils mosaic_rasters
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
730 #" @importFrom raster hdr raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
731 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
732 mosaic_rasters <- function(list_rasters, dst_mosaic, stretch = FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
733
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
734 # produce mosaic
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
735 gdalUtils::mosaic_rasters(gdalfile = list_rasters, dst_dataset = dst_mosaic,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
736 separate = FALSE, of = "Ehdr", verbose = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
737
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
738 # convert hdr to ENVI format
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
739 raster::hdr(raster(dst_mosaic), format = "ENVI")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
740 # add info to hdr based on initial rasters
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
741 hdr_init <- read_envi_header(get_hdr_name(list_rasters[1]))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
742 hdr <- read_envi_header(get_hdr_name(dst_mosaic))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
743 hdr$`band names` <- hdr_init$`band names`
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
744 hdr$wavelength <- hdr_init$wavelength
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
745 if (stretch == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
746 hdr$`default stretch` <- "0.000000 1000.000000 linear"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
747 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
748 hdr$`z plot range` <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
749 hdr$`data ignore value` <- "-Inf"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
750 hdr$`sensor type` <- hdr_init$`sensor type`
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
751 hdr$`coordinate system string` <- read.table(paste(file_path_sans_ext(dst_mosaic), ".prj", sep = ""))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
752 write_envi_header(hdr = hdr, hdrpath = get_hdr_name(dst_mosaic))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
753 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
754 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
755
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
756 #" Reads ENVI hdr file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
757 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
758 #" @param hdrpath Path of the hdr file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
759 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
760 #" @return list of the content of the hdr file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
761 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
762 read_envi_header <- function(hdrpath) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
763 if (!grepl(".hdr$", hdrpath)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
764 stop("File extension should be .hdr")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
765 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
766 hdr <- readLines(hdrpath)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
767 ## check ENVI at beginning of file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
768 if (!grepl("ENVI", hdr[1])) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
769 stop("Not an ENVI header (ENVI keyword missing)")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
770 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
771 hdr <- hdr [-1]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
772 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
773 ## remove curly braces and put multi-line key-value-pairs into one line
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
774 hdr <- gsub("\\{([^}]*)\\}", "\\1", hdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
775 l <- grep("\\{", hdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
776 r <- grep("\\}", hdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
777
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
778 if (length(l) != length(r)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
779 stop("Error matching curly braces in header (differing numbers).")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
780 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
781
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
782 if (any(r <= l)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
783 stop("Mismatch of curly braces in header.")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
784 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
785
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
786 hdr[l] <- sub("\\{", "", hdr[l])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
787 hdr[r] <- sub("\\}", "", hdr[r])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
788
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
789 for (i in rev(seq_along(l))) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
790 hdr <- c(
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
791 hdr [seq_len(l [i] - 1)],
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
792 paste(hdr [l [i]:r [i]], collapse = "\n"),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
793 hdr [-seq_len(r [i])]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
794 )
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
795 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
796
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
797 ## split key = value constructs into list with keys as names
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
798 hdr <- sapply(hdr, split_line, "=", USE.NAMES = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
799 names(hdr) <- tolower(names(hdr))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
800
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
801 ## process numeric values
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
802 tmp <- names(hdr) %in% c(
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
803 "samples", "lines", "bands", "header offset", "data type",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
804 "byte order", "default bands", "data ignore value",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
805 "wavelength", "fwhm", "data gain values"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
806 )
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
807 hdr [tmp] <- lapply(hdr [tmp], function(x) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
808 as.numeric(unlist(strsplit(x, ", ")))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
809 })
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
810
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
811 return(hdr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
812 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
813
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
814 #" This function reads a list of files corresponding to S2 bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
815 #" S2 bands are expected to have uniform spatial resolution and footprint
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
816 #" @param s2_bands list. list of S2 bands obtained from get_s2_bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
817 #" @param path_vector path for a vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
818 #" @param resampling numeric. resampling factor (default = 1, set to resampling = 2 to convert 20m into 10m resolution)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
819 #" @param interpolation character. method for resampling. default = "bilinear"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
820 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
821 #" @return stack_s2 list. contains stack of S2 bands
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
822 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
823 #" @importFrom stars read_stars
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
824 #" @importFrom sf st_bbox st_read st_crop
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
825 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
826
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
827 read_s2bands <- function(s2_bands, path_vector = NULL,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
828 resampling = 1, interpolation = "bilinear") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
829 # get bounding box corresponding to footprint of image or image subset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
830 bb_xycoords <- get_bb(path_raster = s2_bands[[1]],
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
831 path_vector = path_vector, buffer = 50)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
832
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
833 # prepare reading data for extent defined by bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
834 nxoff <- bb_xycoords$UL$col
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
835 nyoff <- bb_xycoords$UL$row
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
836 nxsize <- bb_xycoords$UR$col - bb_xycoords$UL$col + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
837 nysize <- bb_xycoords$LR$row - bb_xycoords$UR$row + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
838 nbufxsize <- resampling * nxsize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
839 nbufysize <- resampling * nysize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
840 if (resampling == 1) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
841 interpolation <- "nearest_neighbour"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
842 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
843 # write interpolated individual bands in temp directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
844 tmpdir <- tempdir()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
845 tmpfile <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
846 for (band in names(s2_bands)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
847 stack_s2_tmp <- stars::read_stars(s2_bands[[band]], along = "band",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
848 RasterIO = list(nXOff = nxoff, nYOff = nyoff,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
849 nXSize = nxsize, nYSize = nysize,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
850 nBufXSize = nbufxsize, nBufYSize = nbufysize,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
851 resample = interpolation), proxy = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
852 if (!is.null(path_vector)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
853 stack_s2_tmp <- sf::st_crop(x = stack_s2_tmp, y = st_bbox(st_read(dsn = path_vector, quiet = TRUE)))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
854 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
855 tmpfile[[band]] <- file.path(tmpdir, tools::file_path_sans_ext(basename(s2_bands[[band]])))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
856 if (band == "Cloud") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
857 stars::write_stars(obj = stack_s2_tmp, dsn = tmpfile[[band]],
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
858 driver = "ENVI", type = "Byte", overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
859 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
860 stars::write_stars(obj = stack_s2_tmp, dsn = tmpfile[[band]],
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
861 driver = "ENVI", type = "Int16", overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
862 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
863 gc()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
864 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
865
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
866 stack_s2 <- stars::read_stars(tmpfile, along = "band", proxy = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
867 return(stack_s2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
868 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
869
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
870 #" This function reads a raster stack, and gets footprint as pixel coordinates or vector file as input
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
871 #" @param path_raster character. path for raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
872 #" @param path_vector character. path for vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
873 #" @param bbpix list. coordinates of pixels corresponding to a bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
874 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
875 #" @return starsobj stars object corresponding to raster or raster subset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
876 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
877 #" @importFrom stars read_stars
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
878 #" @importFrom sf st_bbox st_read st_crop
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
879 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
880 read_raster <- function(path_raster, path_vector = NULL, bbpix = NULL) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
881 # get bounding box corresponding to footprint of image or image subset
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
882 if (is.null(bbpix)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
883 bb_xycoords <- get_bb(path_raster = path_raster,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
884 path_vector = path_vector, buffer = 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
885 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
886 bb_xycoords <- bbpix
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
887 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
888 # prepare reading data for extent defined by bounding box
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
889 nxoff <- bb_xycoords$UL$col
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
890 nyoff <- bb_xycoords$UL$row
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
891 nxsize <- bb_xycoords$UR$col - bb_xycoords$UL$col + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
892 nysize <- bb_xycoords$LR$row - bb_xycoords$UR$row + 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
893 nbufxsize <- nxsize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
894 nbufysize <- nysize
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
895 starsobj <- stars::read_stars(path_raster, along = "band",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
896 RasterIO = list(nXOff = nxoff, nYOff = nyoff,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
897 nXSize = nxsize, nYSize = nysize,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
898 nBufXSize = nbufxsize, nBufYSize = nbufysize),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
899 proxy = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
900 return(starsobj)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
901 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
902
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
903 #" This function reprojects a shapefile and saves reprojected shapefile
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
904 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
905 #" @param path_vector_init character. path for a shapefile to be reprojected
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
906 #" @param newprojection character. projection to be applied to path_vector_init
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
907 #" @param path_vector_reproj character. path for the reprojected shapefile
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
908 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
909 #" @return path_vector character. path of the shapefile
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
910 #" - path_vector_init if the vector did not need reprojection
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
911 #" - path_vector_reproj if the vector needed reprojection
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
912 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
913 #" @importFrom rgdal readOGR writeOGR
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
914 #" @importFrom sp spTransform
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
915 #" @importFrom raster projection
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
916 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
917 reproject_shp <- function(path_vector_init, newprojection, path_vector_reproj) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
918
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
919 dir_vector_init <- dirname(path_vector_init)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
920 # shapefile extension
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
921 fileext <- file_ext(basename(path_vector_init))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
922 if (fileext == "shp") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
923 name_vector_init <- file_path_sans_ext(basename(path_vector_init))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
924 vector_init_ogr <- rgdal::readOGR(dir_vector_init, name_vector_init, verbose = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
925 } else if (fileext == "kml") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
926 vector_init_ogr <- rgdal::readOGR(path_vector_init, verbose = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
927 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
928 vector_init_proj <- raster::projection(vector_init_ogr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
929
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
930 if (!vector_init_proj == newprojection) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
931 dir_vector_reproj <- dirname(path_vector_reproj)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
932 name_vector_reproj <- file_path_sans_ext(basename(path_vector_reproj))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
933 vector_reproj <- sp::spTransform(vector_init_ogr, newprojection)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
934 rgdal::writeOGR(obj = vector_reproj, dsn = dir_vector_reproj, layer = name_vector_reproj,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
935 driver = "ESRI Shapefile", overwrite_layer = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
936 path_vector <- path_vector_reproj
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
937 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
938 path_vector <- path_vector_init
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
939 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
940 return(path_vector)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
941 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
942
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
943
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
944 #" perform atmospheric corrections to convert L1C to L2A data with Sen2cor
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
945 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
946 #" @param prodname character. produced with sen2r::s2_list
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
947 #" @param l1c_path character. path of directory where L1C image is stored
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
948 #" @param l2a_path character. path of directory where L2A image is stored
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
949 #" @param datepattern character. pattern corresponding to date of acquisition to identify L2A directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
950 #" @param tmp_path character. path of temporary directory where L2A image is stored
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
951 #" sen2r configured as an alternative hub for S2 download
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
952 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
953 #" @return pathl2a character. S2 Product name
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
954 #" @importFrom sen2r safe_is_online s2_list s2_download s2_order
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
955 #" @importFrom R.utils getAbsolutePath
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
956 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
957 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
958 s2_from_l1c_to_l2a <- function(prodname, l1c_path, l2a_path, datepattern, tmp_path = NULL) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
959
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
960 # define path for tmp directory
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
961 if (is.null(tmp_path)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
962 tmp_path <- tempdir(check = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
963 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
964 tmp_prodlist <- prodname
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
965 # perform Sen2Cor atmospheric corrections
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
966 binpath <- sen2r::load_binpaths()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
967 # 2- open a command prompt and directly run sen2cor with following command line
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
968 cmd <- paste(binpath$sen2cor,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
969 "--output_dir", R.utils::getAbsolutePath(l2a_path),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
970 R.utils::getAbsolutePath(file.path(l1c_path, prodname)), sep = " ")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
971 system(cmd)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
972 pathl2a <- list.files(path = l2a_path, pattern = datepattern, full.names = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
973
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
974 return(pathl2a)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
975 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
976
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
977 #" This function saves cloud masks.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
978 #" "cloudMask_Binary" is default binary mask with 0 where clouds are detected and 1 for clean pixels
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
979 #" "cloudMask_RAW" is the original cloud layer produced by atmospheric correction algorithm
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
980 #" --> may be useful to refine cloud mask
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
981 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
982 #" @param s2_stars list. stars object containing raster data. Can be produced with function extract_from_s2_l2a
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
983 #" @param cloud_path character.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
984 #" @param s2source character.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
985 #" @param footprint character. path for vector file defining footprint of interest in the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
986 #" @param saveraw boolean. should the original cloud mask layer be saved?
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
987 #" @param maxchunk numeric. Size of individual chunks to be written (in Mb)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
988 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
989 #" @return list of cloudmasks (binary mask, and raw mask if required)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
990 #" @importFrom sf st_read
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
991 #" @importFrom stars write_stars
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
992 #" @importFrom raster raster
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
993 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
994 save_cloud_s2 <- function(s2_stars, cloud_path, s2source = "SAFE",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
995 footprint = NULL, saveraw = FALSE, maxchunk = 256) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
996
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
997 whichcloud <- which(names(s2_stars$attr) == "Cloud")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
998 # Save cloud mask
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
999 if (saveraw == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1000 cloudraw <- file.path(cloud_path, "CloudMask_RAW")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1001 obj <- stars::read_stars(s2_stars$attr[whichcloud], proxy = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1002 sizeobj <- dim(obj)[1] * dim(obj)[2] / (1024**2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1003 nbchunks <- ceiling(sizeobj / maxchunk)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1004 stars::write_stars(obj,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1005 dsn = cloudraw,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1006 driver = "ENVI",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1007 type = "Byte",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1008 chunk_size = c(dim(obj)[1], dim(obj)[2] / nbchunks),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1009 progress = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1010 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1011 cloudraw <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1012 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1013 # Save cloud mask as in biodivMapR (0 = clouds, 1 = pixel ok)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1014 cloudmask <- stars::read_stars(s2_stars$attr[whichcloud], proxy = FALSE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1015 if (s2source == "SAFE" || s2source == "THEIA") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1016 cloudy <- which(cloudmask[[1]] > 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1017 sunny <- which(cloudmask[[1]] == 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1018 } else if (s2source == "LaSRC") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1019 cloudy <- which(is.na(cloudmask[[1]]))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1020 sunny <- which(cloudmask[[1]] == 1)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1021 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1022
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1023 cloudmask[[1]][cloudy] <- 0
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1024 cloudmask[[1]][sunny] <- 1
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1025 cloudbin <- file.path(cloud_path, "CloudMask_Binary")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1026 stars::write_stars(cloudmask, dsn = cloudbin, driver = "ENVI", type = "Byte", overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1027 cloudmasks <- list("BinaryMask" = cloudbin, "RawMask" = cloudraw)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1028 # delete temporary file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1029 file.remove(s2_stars$attr[whichcloud])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1030 if (file.exists(paste(s2_stars$attr[whichcloud], ".hdr", sep = ""))) file.remove(paste(s2_stars$attr[whichcloud], ".hdr", sep = ""))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1031 gc()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1032 return(cloudmasks)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1033 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1034
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1035 #" This function saves reflectance files
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1036 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1037 #" @param s2_stars list. stars object containing raster data. Can be produced with function extract_from_s2_l2a
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1038 #" @param refl_path character. path for reflectance file to be stored
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1039 #" @param format character. file format for reflectance data
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1040 #" @param datatype character. data type (integer, float, 16bits, 32bits...)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1041 #" @param s2sat character. Sentinel-2 mission ("2A" or "2B")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1042 #" @param tile_s2 character. S2 tile name (2 numbers + 3 letters)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1043 #" @param dateacq_s2 double. date of acquisition
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1044 #" @param MTD character. path for metadata file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1045 #" @param MTD_MSI character. path for metadata MSI file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1046 #" @param mtd_lasrc character. path for metadata LaSRC file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1047 #" @param maxchunk numeric. Size of individual chunks to be written (in Mb)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1048 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1049 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1050 #" @importFrom stars write_stars st_apply
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1051 #" @importFrom XML xml
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1052 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1053 save_reflectance_s2 <- function(s2_stars, refl_path, format = "ENVI", datatype = "Int16",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1054 s2sat = NULL, tile_s2 = NULL, dateacq_s2 = NULL,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1055 mtd = NULL, mtd_msi = NULL, mtd_lasrc = NULL,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1056 maxchunk = 256) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1057 # identify if S2A or S2B, if possible
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1058 s2mission <- check_s2mission(s2sat = s2sat, tile_s2 = tile_s2, dateacq_s2 = dateacq_s2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1059
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1060 # define central wavelength corresponding to each spectral band
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1061 if (s2mission == "2A") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1062 wl_s2 <- list("B02" = 496.6, "B03" = 560.0, "B04" = 664.5,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1063 "B05" = 703.9, "B06" = 740.2, "B07" = 782.5, "B08" = 835.1,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1064 "B8A" = 864.8, "B11" = 1613.7, "B12" = 2202.4)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1065 } else if (s2mission == "2B") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1066 wl_s2 <- list("B02" = 492.1, "B03" = 559.0, "B04" = 665.0,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1067 "B05" = 703.8, "B06" = 739.1, "B07" = 779.7, "B08" = 833.0,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1068 "B8A" = 864.0, "B11" = 1610.4, "B12" = 2185.7)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1069 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1070 if (s2mission == "2A") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1071 sensor <- "Sentinel_2A"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1072 } else if (s2mission == "2B") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1073 sensor <- "Sentinel_2B"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1074 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1075
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1076 # apply offset when necessary
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1077 listbands_bis <- c("B2", "B3", "B4", "B5", "B6", "B7", "B8", "B8A", "B11", "B12")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1078 if (!is.null(mtd_msi) && is.null(mtd_lasrc)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1079 # read XML file containing info about geometry of acquisition
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1080 s2xml <- XML::xmlToList(mtd_msi)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1081 xml_offset <- s2xml$General_Info$Product_Image_Characteristics$BOA_ADD_offset_VALUES_LIST
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1082 bands <- lapply(s2xml$General_Info$Product_Image_Characteristics$Spectral_Information_List, "[[", 4)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1083 if (!is.null(xml_offset) && !is.null(bands)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1084 bandid <- lapply(bands, "[[", 1)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1085 bandname <- lapply(bands, "[[", 2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1086 offset <- data.frame("bandname" = unlist(bandname),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1087 "bandid" = unlist(bandid),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1088 "offset" = unlist(lapply(xml_offset, "[[", 1)))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1089 selbands <- match(listbands_bis, offset$bandname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1090 offset <- offset[selbands, ]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1091 boa_quantval <- as.numeric(s2xml$General_Info$Product_Image_Characteristics$QUANTIFICATION_VALUES_LIST$BOA_QUANTIFICATION_VALUE[1])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1092 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1093 offset <- data.frame("bandname" = listbands_bis,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1094 "bandid" = c(1, 2, 3, 4, 5, 6, 7, 8, 11, 12),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1095 "offset" = 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1096 boa_quantval <- 10000
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1097 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1098 } else if (!is.null(mtd_lasrc)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1099 # read XML file containing info about geometry of acquisition
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1100 s2xml <- XML::xmlToList(mtd_lasrc)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1101 attributes_lasrc <- s2xml$bands[[14]]$.attrs
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1102 attributes_lasrc_df <- data.frame(attributes_lasrc)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1103 if (match("add_offset", rownames(attributes_lasrc_df)) > 0 && match("scale_factor", rownames(attributes_lasrc_df)) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1104 xml_offset <- as.numeric(attributes_lasrc[["add_offset"]])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1105 boa_quantval <- 1 / as.numeric(attributes_lasrc[["scale_factor"]])
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1106 offset <- data.frame("bandname" = listbands_bis,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1107 "bandid" = c(1, 2, 3, 4, 5, 6, 7, 8, 11, 12),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1108 "offset" = xml_offset)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1109 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1110 offset <- data.frame("bandname" = listbands_bis,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1111 "bandid" = c(1, 2, 3, 4, 5, 6, 7, 8, 11, 12),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1112 "offset" = 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1113 boa_quantval <- 10000
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1114 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1115 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1116 offset <- data.frame("bandname" = listbands_bis,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1117 "bandid" = c(1, 2, 3, 4, 5, 6, 7, 8, 11, 12),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1118 "offset" = 0)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1119 boa_quantval <- 10000
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1120 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1121
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1122 # identify where spectral bands are in the stars object
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1123 stars_spectral <- list()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1124 starsnames <- names(s2_stars$attr)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1125 stars_spectral$bandname <- starsnames[which(!starsnames == "Cloud")]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1126 stars_spectral$wavelength <- wl_s2[stars_spectral$bandname]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1127
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1128 sortedwl <- names(wl_s2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1129 reorder <- match(sortedwl, stars_spectral$bandname)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1130 elim <- which(is.na(reorder))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1131 if (length(elim) > 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1132 reorder <- reorder[-elim]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1133 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1134 pathr <- s2_stars$attr[reorder]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1135
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1136 names(pathr) <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1137 s2_stars2 <- stars::read_stars(pathr, along = "band", proxy = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1138 stars_spectral$bandname <- stars_spectral$bandname[reorder]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1139 stars_spectral$wavelength <- stars_spectral$wavelength[reorder]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1140
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1141 uniqueoffset <- as.numeric(unique(offset$offset))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1142 if (length(uniqueoffset) > 1) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1143 message("Warning: BOA offset differs between bands.")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1144 message("offset will not be applied to the final S2 reflectance raster")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1145 message("check metadata file to identify the offset applied on each band")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1146 print(mtd_msi)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1147 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1148 message("applying offset to reflectance data")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1149 if (is.null(mtd_lasrc) || uniqueoffset == 0) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1150 offsets2 <- function(x) (round(x + uniqueoffset) * (10000 / boa_quantval))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1151 s2_stars2 <- stars::st_apply(X = s2_stars2, MARGIN = "band", FUN = offsets2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1152 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1153 offsets2 <- function(x) (round(10000 * ((x + uniqueoffset * boa_quantval) / boa_quantval)))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1154 s2_stars2 <- stars::st_apply(X = s2_stars2, MARGIN = "band", FUN = offsets2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1155 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1156 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1157 write_stack_s2(stars_s2 = s2_stars2, stars_spectral = stars_spectral, refl_path = refl_path,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1158 format = format, datatype = datatype, sensor = sensor, maxchunk = maxchunk)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1159 # save metadata file as well if available
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1160 if (!is.null(mtd)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1161 if (file.exists(mtd)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1162 file.copy(from = mtd, to = file.path(dirname(refl_path), basename(mtd)), overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1163 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1164 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1165 # save metadata file as well if available
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1166 if (!is.null(mtd_msi)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1167 if (file.exists(mtd_msi)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1168 file.copy(from = mtd_msi, to = file.path(dirname(refl_path), basename(mtd_msi)), overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1169 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1170 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1171 # save LaSRC metadata file as well if available
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1172 if (!is.null(mtd_lasrc)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1173 if (file.exists(mtd_lasrc)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1174 file.copy(from = mtd_lasrc, to = file.path(dirname(refl_path), basename(mtd_lasrc)), overwrite = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1175 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1176 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1177 # delete temporary file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1178 for (pathtemp in pathr) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1179 file.remove(pathtemp)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1180 if (file.exists(paste(pathtemp, ".hdr", sep = ""))) file.remove(paste(pathtemp, ".hdr", sep = ""))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1181 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1182 gc()
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1183 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1184 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1185
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1186 #" ENVI functions
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1187 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1188 #" based on https://github.com/cran/hyperSpec/blob/master/R/read.ENVI.R
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1189 #" added wavelength, fwhm, ... to header reading
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1190 #" Title
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1191 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1192 #" @param x character.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1193 #" @param separator character
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1194 #" @param trim_blank boolean.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1195 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1196 #" @return list.
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1197 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1198 split_line <- function(x, separator, trim_blank = TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1199 tmp <- regexpr(separator, x)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1200 key <- substr(x, 1, tmp - 1)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1201 value <- substr(x, tmp + 1, nchar(x))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1202 if (trim_blank) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1203 blank_pattern <- "^[[:blank:]]*([^[:blank:]]+.*[^[:blank:]]+)[[:blank:]]*$"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1204 key <- sub(blank_pattern, "\\1", key)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1205 value <- sub(blank_pattern, "\\1", value)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1206 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1207 value <- as.list(value)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1208 names(value) <- key
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1209 return(value)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1210 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1211
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1212 #" save raster footprint as vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1213 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1214 #" @param path_raster character. path for a raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1215 #" @param path_vector character. path for a vector file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1216 #" @param driver character. driver for vector
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1217 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1218 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1219 #" @importFrom raster raster extent projection
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1220 #" @importFrom sf st_as_sf st_write
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1221 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1222 vectorize_raster_extent <- function(path_raster, path_vector, driver = "ESRI Shapefile") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1223 rast <- raster(path_raster)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1224 e <- extent(rast)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1225 # coerce to a SpatialPolygons object
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1226 p <- as(e, "SpatialPolygons")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1227 projection(p) <- projection(rast)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1228 p <- sf::st_as_sf(p)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1229 sf::st_write(obj = p, path_vector, driver = driver) # create to a shapefile
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1230 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1231 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1232
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1233 #" writes ENVI hdr file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1234 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1235 #" @param hdr content to be written
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1236 #" @param hdrpath Path of the hdr file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1237 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1238 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1239 #" @importFrom stringr str_count
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1240 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1241 write_envi_header <- function(hdr, hdrpath) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1242 h <- lapply(hdr, function(x) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1243 if (length(x) > 1 || (is.character(x) && stringr::str_count(x, "\\w+") > 1)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1244 x <- paste0("{", paste(x, collapse = ", "), "}")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1245 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1246 # convert last numerics
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1247 x <- as.character(x)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1248 })
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1249 writeLines(c("ENVI", paste(names(hdr), h, sep = " = ")), con = hdrpath)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1250 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1251 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1252
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1253 #" This function writes a raster Stack object into a ENVI raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1254 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1255 #" @param stackobj list. raster stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1256 #" @param stackpath character. path where to store the stack
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1257 #" @param bands list. should include "bandname", and if possible "wavelength"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1258 #" @param datatype character. should be INT2S or FLT4S for example
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1259 #" @param sensor character. Name of the sensor used to acquire the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1260 #" @param stretch boolean. Set TRUE to get 10% stretching at display for reflectance, mentioned in hdr only
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1261 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1262 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1263 #" @importFrom utils read.table
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1264 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1265 write_rasterstack_envi <- function(stackobj, stackpath, bands, datatype = "INT2S",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1266 sensor = "Unknown", stretch = FALSE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1267
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1268 r <- raster::writeRaster(x = stackobj, filename = stackpath, format = "Ehdr", overwrite = TRUE, datatype = datatype)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1269 raster::hdr(r, format = "ENVI")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1270 # Edit hdr file to add metadata
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1271 hdr <- read_envi_header(get_hdr_name(stackpath))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1272 hdr$`band names` <- bands$bandname
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1273 if (length(bands$wavelength) == length(bands$bandname)) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1274 hdr$wavelength <- bands$wavelength
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1275 } else {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1276 hdr$wavelength <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1277 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1278 if (stretch == TRUE) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1279 hdr$`default stretch` <- "0.000000 1000.000000 linear"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1280 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1281 hdr$`z plot range` <- NULL
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1282 hdr$`data ignore value` <- "-Inf"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1283 hdr$`coordinate system string` <- read.table(paste(stackpath, ".prj", sep = ""))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1284 proj <- strsplit(x = strsplit(x = projection(stackobj), split = " ")[[1]][1], split = "=")[[1]][2]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1285 zone <- strsplit(x = strsplit(x = projection(stackobj), split = " ")[[1]][2], split = "=")[[1]][2]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1286 datum <- strsplit(x = strsplit(x = projection(stackobj), split = " ")[[1]][3], split = "=")[[1]][2]
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1287 oldproj <- hdr$`map info`
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1288 newproj <- gsub(pattern = "projection", replacement = proj, x = oldproj)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1289 newproj <- paste(newproj, zone, datum, sep = ", ")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1290 hdr$`map info` <- newproj
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1291 hdr$`sensor type` <- sensor
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1292 write_envi_header(hdr = hdr, hdrpath = get_hdr_name(stackpath))
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1293
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1294 # remove unnecessary files
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1295 file2remove <- paste(stackpath, ".aux.xml", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1296 file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1297 file2remove <- paste(stackpath, ".prj", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1298 file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1299 file2remove <- paste(stackpath, ".stx", sep = "")
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1300 file.remove(file2remove)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1301 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1302 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1303
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1304
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1305 #" This function writes a stars object into a raster file
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1306 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1307 #" @param stars_s2 list. stars object containing raster data. Can be produced with function Crop_n_resample_S2
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1308 #" @param stars_spectral list. band name to be saved in the stack and spectral bands corresponding to the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1309 #" @param refl_path character. path where to store the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1310 #" @param format character. default = ENVI BSQ. otherwise use gdal drivers
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1311 #" @param datatype character. should be Int16 or Float64 for example
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1312 #" @param sensor character. Name of the sensor used to acquire the image
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1313 #" @param maxchunk numeric. Size of individual chunks to be written (in Mb)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1314 #"
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1315 #" @return None
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1316 #" @export
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1317 write_stack_s2 <- function(stars_s2, stars_spectral, refl_path, format = "ENVI",
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1318 datatype = "Int16", sensor = "Unknown", maxchunk = 256) {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1319
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1320 # write raster file from proxy using chunks
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1321 sizeobj <- 2 * dim(stars_s2)[1] * dim(stars_s2)[2] * dim(stars_s2)[3] / (1024**2)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1322 nbchunks <- ceiling(sizeobj / maxchunk)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1323 stars::write_stars(obj = stars_s2,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1324 dsn = refl_path,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1325 driver = format,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1326 type = datatype,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1327 chunk_size = c(dim(stars_s2)[1], ceiling(dim(stars_s2)[2] / nbchunks)),
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1328 progress = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1329
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1330 if (format == "ENVI") {
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1331 adjust_envi_hdr(dsn = refl_path, bands = stars_spectral,
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1332 sensor = sensor, stretch = TRUE)
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1333 }
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1334 return(invisible())
33a1e15f7252 planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff changeset
1335 }