Mercurial > repos > ecology > srs_diversity_maps
annotate Lib_preprocess_S2.r @ 0:9adccd3da70c draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
author | ecology |
---|---|
date | Mon, 09 Jan 2023 13:37:37 +0000 |
parents | |
children |
rev | line source |
---|---|
0
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1 # == == == == == == == == == == == == == == == == == == == == == == == == == == == |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
2 # preprocS2 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
3 # Lib_preprocess_S2.R |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
4 # == == == == == == == == == == == == == == == == == == == == == == == == == == == |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
5 # PROGRAMMERS: |
9adccd3da70c
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> |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
8 # == == == == == == == == == == == == == == == == == == == == == == == == == == == |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
11 # == == == == == == == == == == == == == == == == == == == == == == == == == == == |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
12 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
14 #" |
9adccd3da70c
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 |
9adccd3da70c
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" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
19 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
20 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
21 #" @importFrom utils read.table |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
22 #" @importFrom raster hdr raster |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
23 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
25 |
9adccd3da70c
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 |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
28 hdr$`band names` <- bands$bandname |
9adccd3da70c
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)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
30 hdr$wavelength <- bands$wavelength |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
31 }else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
32 hdr$wavelength <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
33 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
34 if (stretch == TRUE) { |
9adccd3da70c
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" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
36 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
37 hdr$`z plot range` <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
38 hdr$`data ignore value` <- "-Inf" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
39 hdr$`sensor type` <- sensor |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
41 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
42 # remove unnecessary files |
9adccd3da70c
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 = "") |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
45 file2remove <- paste(dsn, ".prj", sep = "") |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
47 file2remove <- paste(dsn, ".stx", sep = "") |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
49 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
50 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
51 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
52 #" This function saves reflectance files |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
53 #" |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
57 #" |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
60 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
62 |
9adccd3da70c
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? |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
64 if (!is.null(s2sat)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
65 if (s2sat == "2A") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
66 s2mission <- "2A" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
67 }else if (s2sat == "2B") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
68 s2mission <- "2B" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
69 }else { |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
72 s2mission <- "2A" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
73 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
74 }else { |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
77 s2mission <- "2A" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
78 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
79 return(s2mission) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
80 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
81 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
85 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
87 #" - image stack |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
90 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
91 #" @importFrom raster raster |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
93 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
95 stopifnot(is.character(path)) |
9adccd3da70c
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) |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
98 size_files <- sum(vect_size) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
99 return(size_files) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
100 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
101 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
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" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
110 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
111 #" @return listout list. |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
112 #" - image stack |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
115 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
116 #" @importFrom raster raster |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
118 #" @export |
9adccd3da70c
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", |
9adccd3da70c
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") { |
9adccd3da70c
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 |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
123 s2source = s2source, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
124 resolution = resolution, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
125 fre_sre = fre_sre) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
126 |
9adccd3da70c
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) { |
9adccd3da70c
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]]) |
9adccd3da70c
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) { |
9adccd3da70c
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]]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
131 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
133 if (!is.null(path_vector)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
134 raster_proj <- raster::projection(rastmp) |
9adccd3da70c
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 = "") |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
137 newprojection = raster_proj, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
139 } |
9adccd3da70c
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 |
9adccd3da70c
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) { |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
143 resampling = 1, interpolation = interpolation) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
144 } |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
146 if (resolution == 10 && s2source != "LaSRC") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
147 resampling <- 2 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
148 }else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
149 resampling <- 1 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
150 } |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
152 resampling = resampling, interpolation = interpolation) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
153 } |
9adccd3da70c
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 |
9adccd3da70c
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) { |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
160 if (!diffxstop == 0) { |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
163 if (diffxstop > 0) { |
9adccd3da70c
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), , ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
165 }else if (diffxstop < 0) { |
9adccd3da70c
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), , ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
167 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
168 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
169 if (!diffystop == 0) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
170 if (diffystop > 0) { |
9adccd3da70c
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), ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
172 }else if (diffystop < 0) { |
9adccd3da70c
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), ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
174 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
175 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
176 if (!diffxstart == 0) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
177 if (diffxstart > 0) { |
9adccd3da70c
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], , ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
179 }else if (diffxstart < 0) { |
9adccd3da70c
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], , ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
181 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
182 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
183 if (!diffystart == 0) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
184 if (diffystart > 0) { |
9adccd3da70c
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], ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
186 }else if (diffystart < 0) { |
9adccd3da70c
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], ] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
188 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
189 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
190 # reorder bands with increasing wavelength |
9adccd3da70c
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") |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
193 reorder_bands <- match(s2bands, namebands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
194 namebands <- namebands[reorder_bands] |
9adccd3da70c
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] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
196 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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], |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
203 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
205 nxoff <- bb_xycoords$UL$col |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
206 nyoff <- bb_xycoords$UL$row |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
209 nbufxsize <- nxsize |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
210 nbufysize <- nysize |
9adccd3da70c
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", |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
213 nXSize = nxsize, nYSize = nysize, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
214 nBufXSize = nbufxsize, nBufYSize = nbufysize, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
215 resample = "nearest_neighbour"), proxy = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
216 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
217 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
218 names(s2_stack$attr) <- namebands |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
220 s2_stack <- stack_10m |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
222 names(s2_stack$attr) <- namebands |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
224 s2_stack <- stack_20m |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
226 names(s2_stack$attr) <- namebands |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
227 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
228 |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
230 "namebands" = namebands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
231 return(listout) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
232 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
233 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
235 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
239 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
241 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
243 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
244 if (!is.null(path_vector)) { |
9adccd3da70c
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 |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
247 path_vector = path_vector, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
248 buffer = buffer) |
9adccd3da70c
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)) { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
251 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
252 return(bb_xycoords) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
253 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
254 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
256 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
258 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
260 #" @importFrom raster raster |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
261 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
264 rasterobj <- raster::raster(path_raster) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
265 bb_xycoords <- list() |
9adccd3da70c
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) |
9adccd3da70c
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]) |
9adccd3da70c
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) |
9adccd3da70c
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]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
270 return(bb_xycoords) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
271 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
272 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
274 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
278 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
281 #" @importFrom rgeos gbuffer bbox2SP |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
282 #" @importFrom sp SpatialPoints bbox |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
284 #" @importFrom methods as |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
285 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
287 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
289 # extract BB coordinates from vector |
9adccd3da70c
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"), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
291 width = buffer, byid = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
292 # extract BB coordinates from raster |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
294 # compute intersection |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
296 bbext <- raster::extent(intersect) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
297 xmin <- bbext[1] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
298 xmax <- bbext[2] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
299 ymin <- bbext[3] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
300 ymax <- bbext[4] |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
302 corners <- list() |
9adccd3da70c
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)) |
9adccd3da70c
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)) |
9adccd3da70c
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)) |
9adccd3da70c
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)) |
9adccd3da70c
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"]]) <- |
9adccd3da70c
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"]]) <- |
9adccd3da70c
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)) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
311 bb_xycoords <- list() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
312 for (corner in names(corners)) { |
9adccd3da70c
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)) |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
316 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
317 return(bb_xycoords) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
318 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
319 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
321 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
323 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
324 #" @return corresponding hdr |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
325 #" @import tools |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
326 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
327 get_hdr_name <- function(impath) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
328 if (tools::file_ext(impath) == "") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
329 impathhdr <- paste(impath, ".hdr", sep = "") |
9adccd3da70c
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") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
331 impathhdr <- gsub(".bil", ".hdr", impath) |
9adccd3da70c
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") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
333 impathhdr <- gsub(".zip", ".hdr", impath) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
334 }else { |
9adccd3da70c
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 = "") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
336 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
337 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
338 if (!file.exists(impathhdr)) { |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
340 print(impathhdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
341 message("Process may stop") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
342 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
343 return(impathhdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
344 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
345 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
347 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
352 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
354 #" @export |
9adccd3da70c
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") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
356 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
357 if (s2source == "SAFE" || s2source == "Sen2Cor") { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
359 }else if (s2source == "THEIA") { |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
361 fre_sre = fre_sre) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
362 }else if (s2source == "LaSRC") { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
364 }else { |
9adccd3da70c
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") |
9adccd3da70c
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:") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
367 message("- LaSRC (atmospheric correction: LaSRC)") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
368 message("- THEIA (atmospheric correction: MAJA)") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
369 message("- SAFE (atmospheric correction: Sen2Cor)") |
9adccd3da70c
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 |
9adccd3da70c
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, |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
373 "metadata_lasrc" = metadata_lasrc) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
374 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
375 return(listbands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
376 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
377 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
379 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
382 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
384 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
386 # build path for all bands |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
387 if (resolution == 10) { |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
390 }else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
391 b10m <- c() |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
393 } |
9adccd3da70c
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 |
9adccd3da70c
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", |
9adccd3da70c
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) |
9adccd3da70c
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") |
9adccd3da70c
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"))) { |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
400 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
401 mtd_msi_file <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
402 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
403 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
404 # Define path for bands |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
407 s2bands_10m <- s2bands_20m <- list() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
408 for (band in b20m) { |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
410 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
411 for (band in b10m) { |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
413 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
414 # get cloud mask |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
415 cloud <- "MSK_CLDPRB_20m" |
9adccd3da70c
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") |
9adccd3da70c
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)) |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
419 "s2bands_20m" = s2bands_20m, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
420 "GRANULE" = granule, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
421 "metadata" = mtdfile, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
422 "metadata_MSI" = mtd_msi_file, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
423 "metadata_lasrc" = NULL) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
424 return(listbands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
425 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
426 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
428 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
431 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
433 #" @importFrom stringr str_subset |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
434 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
436 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
438 granule <- path_dir_s2 |
9adccd3da70c
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") |
9adccd3da70c
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"))) { |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
442 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
443 mtd_msi_file <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
444 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
445 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
446 # build path for all bands |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
449 # Define path for bands |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
450 s2bands_10m <- s2bands_20m <- list() |
9adccd3da70c
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)) { |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
453 list.files(path_dir_s2, |
9adccd3da70c
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 = ""))) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
455 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
456 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
457 # get metadata file containing offset |
9adccd3da70c
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$") |
9adccd3da70c
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))) { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
461 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
462 metadata_lasrc <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
463 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
464 # get cloud mask |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
465 cloud <- "CLM" |
9adccd3da70c
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)) |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
468 "s2bands_20m" = s2bands_20m, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
469 "GRANULE" = granule, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
470 "metadata" = mtdfile, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
471 "metadata_MSI" = mtd_msi_file, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
472 "metadata_lasrc" = metadata_lasrc) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
473 return(listbands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
474 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
475 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
477 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
481 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
483 #" @export |
9adccd3da70c
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") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
485 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
486 # build path for all bands |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
487 if (resolution == 10) { |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
490 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
491 b10m <- c() |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
493 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
494 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
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)]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
499 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
500 # Define path for bands |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
502 s2bands_10m <- s2bands_20m <- list() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
503 for (band in b20m) { |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
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, |
9adccd3da70c
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))] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
508 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
509 for (band in b10m) { |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
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, |
9adccd3da70c
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))] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
514 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
515 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
516 # get cloud mask 10m |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
517 cloud_10m <- "CLM_R1" |
9adccd3da70c
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") |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
520 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
521 # get cloud mask 20m |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
522 cloud_20m <- "CLM_R2" |
9adccd3da70c
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") |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
525 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
526 # return list bands |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
528 "s2bands_20m" = s2bands_20m, |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
530 "metadata" = mtdfile) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
531 return(listbands) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
532 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
533 |
9adccd3da70c
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: |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
535 #" - L2A: already atmospherically corrected |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
537 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
539 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
541 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
542 get_s2_level <- function(prodname) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
543 prodname <- basename(prodname) |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
545 s2level <- "L1C" |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
547 s2level <- "L2A" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
548 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
549 return(s2level) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
550 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
551 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
553 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
555 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
556 #" @return tilename character |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
558 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
559 get_tile <- function(prodname) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
560 prodname <- basename(prodname) |
9adccd3da70c
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))) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
562 return(tilename) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
563 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
564 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
566 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
568 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
569 #" @return dateacq character |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
570 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
571 get_date <- function(prodname) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
572 prodname <- basename(prodname) |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
574 return(dateacq) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
575 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
576 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
578 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
586 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
589 #" @export |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
591 googlecloud = FALSE, forcegoogle = FALSE) { |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
594 # if available: download |
9adccd3da70c
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") |
9adccd3da70c
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))) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
597 message("L1C file already downloaded") |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
599 } else { |
9adccd3da70c
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) { |
9adccd3da70c
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) |
9adccd3da70c
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) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
604 if (googlecloud == TRUE) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
606 ggc <- sen2r::check_gcloud() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
607 if (ggc == TRUE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
608 message("downloading from Google cloud") |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
610 time_interval = time_interval, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
611 server = "gcloud") |
9adccd3da70c
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") |
9adccd3da70c
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))) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
614 message("L1C file already downloaded") |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
616 } else { |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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) { |
9adccd3da70c
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")) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
623 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
624 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
625 } else if (ggc == FALSE) { |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
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/") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
631 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
632 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
633 } |
9adccd3da70c
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) { |
9adccd3da70c
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)") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
636 message("Ordering image from LTA") |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
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/") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
643 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
644 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
645 return(prodname) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
646 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
647 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
649 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
657 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
660 #" @importFrom R.utils getAbsolutePath |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
661 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
662 #" @export |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
664 deletel1c = FALSE, sen2cor = TRUE, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
665 googlecloud = FALSE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
666 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
668 l1c_path <- l2a_path |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
669 # define time interval |
9adccd3da70c
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)) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
672 if (googlecloud == TRUE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
673 server <- c("scihub", "gcloud") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
674 } else if (googlecloud == FALSE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
675 server <- "scihub" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
676 } |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
678 time_interval = time_interval, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
679 server = server, availability = "check") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
680 # download products |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
682 # name all products |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
685 if (sen2cor == TRUE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
686 for (imgname in prodname) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
687 s2level <- get_s2_level(imgname) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
688 if (s2level == "L1C") { |
9adccd3da70c
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) |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
691 datepattern = datepattern, tmp_path = NULL) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
692 if (deletel1c == TRUE) { |
9adccd3da70c
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)), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
694 recursive = TRUE, force = TRUE) |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
697 dateacq <- get_date(imgname) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
698 tilename <- get_tile(imgname) |
9adccd3da70c
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) |
9adccd3da70c
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)] |
9adccd3da70c
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")] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
702 prodfullpath[whichimg] <- pathl2a |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
703 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
704 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
705 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
706 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
707 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
708 return(prodfullpath) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
709 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
710 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
712 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
713 #" @param Raster image raster object |
9adccd3da70c
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 |
9adccd3da70c
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) { |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
719 return(my_list) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
720 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
721 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
722 #" mosaicing a set of rasters |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
723 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
727 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
728 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
729 #" @importFrom gdalUtils mosaic_rasters |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
730 #" @importFrom raster hdr raster |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
731 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
733 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
734 # produce mosaic |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
737 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
738 # convert hdr to ENVI format |
9adccd3da70c
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") |
9adccd3da70c
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 |
9adccd3da70c
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])) |
9adccd3da70c
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)) |
9adccd3da70c
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` |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
744 hdr$wavelength <- hdr_init$wavelength |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
745 if (stretch == TRUE) { |
9adccd3da70c
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" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
747 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
748 hdr$`z plot range` <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
749 hdr$`data ignore value` <- "-Inf" |
9adccd3da70c
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` |
9adccd3da70c
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 = "")) |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
753 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
754 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
755 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
756 #" Reads ENVI hdr file |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
757 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
759 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
761 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
762 read_envi_header <- function(hdrpath) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
763 if (!grepl(".hdr$", hdrpath)) { |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
765 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
766 hdr <- readLines(hdrpath) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
768 if (!grepl("ENVI", hdr[1])) { |
9adccd3da70c
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)") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
770 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
771 hdr <- hdr [-1] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
772 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
774 hdr <- gsub("\\{([^}]*)\\}", "\\1", hdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
775 l <- grep("\\{", hdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
776 r <- grep("\\}", hdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
777 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
778 if (length(l) != length(r)) { |
9adccd3da70c
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).") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
780 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
781 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
782 if (any(r <= l)) { |
9adccd3da70c
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.") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
784 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
785 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
786 hdr[l] <- sub("\\{", "", hdr[l]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
787 hdr[r] <- sub("\\}", "", hdr[r]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
788 |
9adccd3da70c
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))) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
790 hdr <- c( |
9adccd3da70c
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)], |
9adccd3da70c
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"), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
793 hdr [-seq_len(r [i])] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
794 ) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
795 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
796 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
799 names(hdr) <- tolower(names(hdr)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
800 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
801 ## process numeric values |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
802 tmp <- names(hdr) %in% c( |
9adccd3da70c
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", |
9adccd3da70c
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", |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
805 "wavelength", "fwhm", "data gain values" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
806 ) |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
808 as.numeric(unlist(strsplit(x, ", "))) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
809 }) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
810 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
811 return(hdr) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
812 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
813 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
820 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
822 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
823 #" @importFrom stars read_stars |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
825 #" @export |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
826 |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
828 resampling = 1, interpolation = "bilinear") { |
9adccd3da70c
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 |
9adccd3da70c
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]], |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
832 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
834 nxoff <- bb_xycoords$UL$col |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
835 nyoff <- bb_xycoords$UL$row |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
838 nbufxsize <- resampling * nxsize |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
839 nbufysize <- resampling * nysize |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
840 if (resampling == 1) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
841 interpolation <- "nearest_neighbour" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
842 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
844 tmpdir <- tempdir() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
845 tmpfile <- list() |
9adccd3da70c
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)) { |
9adccd3da70c
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", |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
849 nXSize = nxsize, nYSize = nysize, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
850 nBufXSize = nbufxsize, nBufYSize = nbufysize, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
851 resample = interpolation), proxy = FALSE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
852 if (!is.null(path_vector)) { |
9adccd3da70c
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))) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
854 } |
9adccd3da70c
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]]))) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
856 if (band == "Cloud") { |
9adccd3da70c
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]], |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
859 } else { |
9adccd3da70c
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]], |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
862 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
863 gc() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
864 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
865 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
867 return(stack_s2) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
868 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
869 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
874 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
876 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
877 #" @importFrom stars read_stars |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
879 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
882 if (is.null(bbpix)) { |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
885 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
886 bb_xycoords <- bbpix |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
887 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
889 nxoff <- bb_xycoords$UL$col |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
890 nyoff <- bb_xycoords$UL$row |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
893 nbufxsize <- nxsize |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
894 nbufysize <- nysize |
9adccd3da70c
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", |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
897 nXSize = nxsize, nYSize = nysize, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
898 nBufXSize = nbufxsize, nBufYSize = nbufysize), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
899 proxy = FALSE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
900 return(starsobj) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
901 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
902 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
904 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
908 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
912 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
913 #" @importFrom rgdal readOGR writeOGR |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
914 #" @importFrom sp spTransform |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
915 #" @importFrom raster projection |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
916 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
918 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
920 # shapefile extension |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
922 if (fileext == "shp") { |
9adccd3da70c
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)) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
925 } else if (fileext == "kml") { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
927 } |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
929 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
930 if (!vector_init_proj == newprojection) { |
9adccd3da70c
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) |
9adccd3da70c
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)) |
9adccd3da70c
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) |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
936 path_vector <- path_vector_reproj |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
937 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
938 path_vector <- path_vector_init |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
939 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
940 return(path_vector) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
941 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
942 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
943 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
945 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
952 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
955 #" @importFrom R.utils getAbsolutePath |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
956 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
957 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
959 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
960 # define path for tmp directory |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
961 if (is.null(tmp_path)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
962 tmp_path <- tempdir(check = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
963 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
964 tmp_prodlist <- prodname |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
965 # perform Sen2Cor atmospheric corrections |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
966 binpath <- sen2r::load_binpaths() |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
968 cmd <- paste(binpath$sen2cor, |
9adccd3da70c
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), |
9adccd3da70c
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 = " ") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
971 system(cmd) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
973 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
974 return(pathl2a) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
975 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
976 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
977 #" This function saves cloud masks. |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
981 #" |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
983 #" @param cloud_path character. |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
984 #" @param s2source character. |
9adccd3da70c
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 |
9adccd3da70c
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? |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
988 #" |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
990 #" @importFrom sf st_read |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
991 #" @importFrom stars write_stars |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
992 #" @importFrom raster raster |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
993 #" @export |
9adccd3da70c
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", |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
996 |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
998 # Save cloud mask |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
999 if (saveraw == TRUE) { |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1003 nbchunks <- ceiling(sizeobj / maxchunk) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1004 stars::write_stars(obj, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1005 dsn = cloudraw, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1006 driver = "ENVI", |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1007 type = "Byte", |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1009 progress = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1010 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1011 cloudraw <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1012 } |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1015 if (s2source == "SAFE" || s2source == "THEIA") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1016 cloudy <- which(cloudmask[[1]] > 0) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1017 sunny <- which(cloudmask[[1]] == 0) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1018 } else if (s2source == "LaSRC") { |
9adccd3da70c
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]])) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1020 sunny <- which(cloudmask[[1]] == 1) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1021 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1022 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1023 cloudmask[[1]][cloudy] <- 0 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1024 cloudmask[[1]][sunny] <- 1 |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1028 # delete temporary file |
9adccd3da70c
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]) |
9adccd3da70c
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 = "")) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1031 gc() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1032 return(cloudmasks) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1033 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1034 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1035 #" This function saves reflectance files |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1036 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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...) |
9adccd3da70c
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") |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1048 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1049 #" @return None |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1051 #" @importFrom XML xml |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1052 #" @export |
9adccd3da70c
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", |
9adccd3da70c
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, |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1056 maxchunk = 256) { |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1059 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1061 if (s2mission == "2A") { |
9adccd3da70c
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, |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1065 } else if (s2mission == "2B") { |
9adccd3da70c
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, |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1069 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1070 if (s2mission == "2A") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1071 sensor <- "Sentinel_2A" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1072 } else if (s2mission == "2B") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1073 sensor <- "Sentinel_2B" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1074 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1075 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1076 # apply offset when necessary |
9adccd3da70c
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") |
9adccd3da70c
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)) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1080 s2xml <- XML::xmlToList(mtd_msi) |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1084 bandid <- lapply(bands, "[[", 1) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1085 bandname <- lapply(bands, "[[", 2) |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1087 "bandid" = unlist(bandid), |
9adccd3da70c
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))) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1090 offset <- offset[selbands, ] |
9adccd3da70c
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]) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1092 } else { |
9adccd3da70c
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, |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1095 "offset" = 0) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1096 boa_quantval <- 10000 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1097 } |
9adccd3da70c
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)) { |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1100 s2xml <- XML::xmlToList(mtd_lasrc) |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
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) { |
9adccd3da70c
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"]]) |
9adccd3da70c
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"]]) |
9adccd3da70c
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, |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1108 "offset" = xml_offset) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1109 } else { |
9adccd3da70c
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, |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1112 "offset" = 0) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1113 boa_quantval <- 10000 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1114 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1115 } else { |
9adccd3da70c
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, |
9adccd3da70c
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), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1118 "offset" = 0) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1119 boa_quantval <- 10000 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1120 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1121 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1123 stars_spectral <- list() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1124 starsnames <- names(s2_stars$attr) |
9adccd3da70c
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")] |
9adccd3da70c
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] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1127 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1128 sortedwl <- names(wl_s2) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1130 elim <- which(is.na(reorder)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1131 if (length(elim) > 0) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1132 reorder <- reorder[-elim] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1133 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1134 pathr <- s2_stars$attr[reorder] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1135 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1136 names(pathr) <- NULL |
9adccd3da70c
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) |
9adccd3da70c
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] |
9adccd3da70c
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] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1140 |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1142 if (length(uniqueoffset) > 1) { |
9adccd3da70c
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.") |
9adccd3da70c
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") |
9adccd3da70c
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") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1146 print(mtd_msi) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1147 } else { |
9adccd3da70c
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") |
9adccd3da70c
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) { |
9adccd3da70c
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)) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1152 } else { |
9adccd3da70c
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))) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1155 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1156 } |
9adccd3da70c
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, |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1160 if (!is.null(mtd)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1161 if (file.exists(mtd)) { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1163 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1164 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1166 if (!is.null(mtd_msi)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1167 if (file.exists(mtd_msi)) { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1169 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1170 } |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1172 if (!is.null(mtd_lasrc)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1173 if (file.exists(mtd_lasrc)) { |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1175 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1176 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1177 # delete temporary file |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1178 for (pathtemp in pathr) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1179 file.remove(pathtemp) |
9adccd3da70c
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 = "")) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1181 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1182 gc() |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1183 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1184 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1185 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1186 #" ENVI functions |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1187 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1190 #" Title |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1191 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1192 #" @param x character. |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1193 #" @param separator character |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1194 #" @param trim_blank boolean. |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1195 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1196 #" @return list. |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1197 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1199 tmp <- regexpr(separator, x) |
9adccd3da70c
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) |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1202 if (trim_blank) { |
9adccd3da70c
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:]]*$" |
9adccd3da70c
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) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1206 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1207 value <- as.list(value) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1208 names(value) <- key |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1209 return(value) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1210 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1211 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1213 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1217 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1218 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1219 #" @importFrom raster raster extent projection |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1221 #" @export |
9adccd3da70c
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") { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1223 rast <- raster(path_raster) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1224 e <- extent(rast) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1225 # coerce to a SpatialPolygons object |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1226 p <- as(e, "SpatialPolygons") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1227 projection(p) <- projection(rast) |
9adccd3da70c
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) |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1230 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1231 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1232 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1233 #" writes ENVI hdr file |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1234 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1237 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1238 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1239 #" @importFrom stringr str_count |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1240 #" @export |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1242 h <- lapply(hdr, function(x) { |
9adccd3da70c
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)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1244 x <- paste0("{", paste(x, collapse = ", "), "}") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1245 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1246 # convert last numerics |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1247 x <- as.character(x) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1248 }) |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1250 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1251 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1252 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1254 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1255 #" @param stackobj list. raster stack |
9adccd3da70c
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 |
9adccd3da70c
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" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1261 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1262 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1263 #" @importFrom utils read.table |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1264 #" @export |
9adccd3da70c
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", |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1266 sensor = "Unknown", stretch = FALSE) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1267 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1269 raster::hdr(r, format = "ENVI") |
9adccd3da70c
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 |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1272 hdr$`band names` <- bands$bandname |
9adccd3da70c
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)) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1274 hdr$wavelength <- bands$wavelength |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1275 } else { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1276 hdr$wavelength <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1277 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1278 if (stretch == TRUE) { |
9adccd3da70c
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" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1280 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1281 hdr$`z plot range` <- NULL |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1282 hdr$`data ignore value` <- "-Inf" |
9adccd3da70c
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 = "")) |
9adccd3da70c
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] |
9adccd3da70c
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] |
9adccd3da70c
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] |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1287 oldproj <- hdr$`map info` |
9adccd3da70c
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) |
9adccd3da70c
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 = ", ") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1290 hdr$`map info` <- newproj |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1291 hdr$`sensor type` <- sensor |
9adccd3da70c
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)) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1293 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1294 # remove unnecessary files |
9adccd3da70c
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 = "") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1296 file.remove(file2remove) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1297 file2remove <- paste(stackpath, ".prj", sep = "") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1298 file.remove(file2remove) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1299 file2remove <- paste(stackpath, ".stx", sep = "") |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1300 file.remove(file2remove) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1301 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1302 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1303 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1304 |
9adccd3da70c
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 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1306 #" |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1314 #" |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1315 #" @return None |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1316 #" @export |
9adccd3da70c
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", |
9adccd3da70c
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) { |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1319 |
9adccd3da70c
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 |
9adccd3da70c
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) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1322 nbchunks <- ceiling(sizeobj / maxchunk) |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1324 dsn = refl_path, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1325 driver = format, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1326 type = datatype, |
9adccd3da70c
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)), |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1328 progress = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1329 |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1330 if (format == "ENVI") { |
9adccd3da70c
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, |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1332 sensor = sensor, stretch = TRUE) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1333 } |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1334 return(invisible()) |
9adccd3da70c
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
ecology
parents:
diff
changeset
|
1335 } |