Mercurial > repos > ecology > srs_preprocess_s2
annotate prosail-master/R/Lib_PROSAIL_HybridInversion.R @ 0:33a1e15f7252 draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
| author | ecology | 
|---|---|
| date | Mon, 09 Jan 2023 13:37:05 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 
0
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
1 # ============================================================================= = | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
2 # prosail | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
3 # Lib_PROSAIL_HybridInversion.R | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
4 # ============================================================================= = | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
5 # PROGRAMMERS: | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
6 # Jean-Baptiste FERET <jb.feret@teledetection.fr> | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
7 # Florian de BOISSIEU <fdeboiss@gmail.com> | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
8 # Copyright 2019 / 11 Jean-Baptiste FERET | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
9 # ============================================================================= = | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
10 # This Library includes functions dedicated to PROSAIL inversion using hybrid | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
11 # approach based on SVM regression | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
12 # ============================================================================= = | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
13 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
14 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
15 #" This function applies SVR model on raster data in order to estimate | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
16 #" vegetation biophysical properties | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
17 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
18 #" @param raster_path character. path for a raster file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
19 #" @param hybridmodel list. hybrid models produced from train_prosail_inversion | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
20 #" each element of the list corresponds to a set of hybrid models for a vegetation parameter | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
21 #" @param pathout character. path for directory where results are written | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
22 #" @param selectedbands list. list of spectral bands to be selected from raster (identified by name of vegetation parameter) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
23 #" @param bandname character. spectral bands corresponding to the raster | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
24 #" @param maskraster character. path for binary mask defining ON (1) and OFF (0) pixels in the raster | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
25 #" @param multiplyingfactor numeric. multiplying factor used to write reflectance in the raster | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
26 #" --> PROSAIL simulates reflectance between 0 and 1, and raster data expected in the same range | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
27 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
28 #" @return None | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
29 #" @importFrom progress progress_bar | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
30 #" @importFrom stars read_stars | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
31 #" @importFrom raster raster brick blockSize readStart readStop getValues writeStart writeStop writeValues | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
32 #" @import rgdal | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
33 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
34 apply_prosail_inversion <- function(raster_path, hybridmodel, pathout, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
35 selectedbands, bandname, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
36 maskraster = FALSE, multiplyingfactor = 10000) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
37 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
38 # explain which biophysical variables will be computed | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
39 bpvar <- names(hybridmodel) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
40 print("The following biophysical variables will be computed") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
41 print(bpvar) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
42 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
43 # get image dimensions | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
44 if (attr(rgdal::GDALinfo(raster_path, returnStats = FALSE), "driver") == "ENVI") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
45 hdr <- read_envi_header(get_hdr_name(raster_path)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
46 dimsraster <- list("rows" = hdr$lines, "cols" = hdr$samples, "bands" = hdr$bands) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
47 } else { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
48 dimsraster <- dim(read_stars(raster_path)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
49 dimsraster <- list("rows" = as.numeric(dimsraster[2]), "cols" = as.numeric(dimsraster[1]), "bands" = as.numeric(dimsraster[3])) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
50 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
51 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
52 # Produce a map for each biophysical property | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
53 for (parm in bpvar) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
54 print(paste("Computing", parm, sep = " ")) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
55 # read by chunk to avoid memory problem | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
56 blk <- blockSize(brick(raster_path)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
57 # reflectance file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
58 r_in <- readStart(brick(raster_path)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
59 # mask file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
60 r_inmask <- FALSE | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
61 if (maskraster == FALSE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
62 selectpixels <- "ALL" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
63 } else if (!maskraster == FALSE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
64 if (file.exists(maskraster)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
65 r_inmask <- readStart(raster(maskraster)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
66 } else if (!file.exists(maskraster)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
67 message("WARNING: Mask file does not exist:") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
68 print(maskraster) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
69 message("Processing all image") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
70 selectpixels <- "ALL" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
71 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
72 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
73 # initiate progress bar | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
74 pgbarlength <- length(hybridmodel[[parm]]) * blk$n | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
75 pb <- progress_bar$new( | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
76 format = "Hybrid inversion on raster [:bar] :percent in :elapsedfull, estimated time remaining :eta", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
77 total = pgbarlength, clear = FALSE, width = 100) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
78 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
79 # output files | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
80 bpvarpath <- file.path(pathout, paste(basename(raster_path), parm, sep = "_")) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
81 bpvarsdpath <- file.path(pathout, paste(basename(raster_path), parm, "STD", sep = "_")) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
82 r_outmean <- writeStart(raster(raster_path), filename = bpvarpath, format = "ENVI", overwrite = TRUE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
83 r_outsd <- writeStart(raster(raster_path), filename = bpvarsdpath, format = "ENVI", overwrite = TRUE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
84 selbands <- match(selectedbands[[parm]], bandname) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
85 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
86 # loop over blocks | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
87 for (i in seq_along(blk$row)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
88 # read values for block | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
89 # format is a matrix with rows the cells values and columns the layers | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
90 blockval <- getValues(r_in, row = blk$row[i], nrows = blk$nrows[i]) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
91 fulllength <- dim(blockval)[1] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
92 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
93 if (typeof(r_inmask) == "logical") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
94 blockval <- blockval[, selbands] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
95 # automatically filter pixels corresponding to negative values | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
96 selectpixels <- which(blockval[, 1] > 0) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
97 blockval <- blockval[selectpixels, ] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
98 } else if (typeof(r_inmask) == "S4") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
99 maskval <- getValues(r_inmask, row = blk$row[i], nrows = blk$nrows[i]) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
100 selectpixels <- which(maskval == 1) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
101 blockval <- blockval[selectpixels, selbands] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
102 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
103 mean_estimatefull <- NA * vector(length = fulllength) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
104 std_estimatefull <- NA * vector(length = fulllength) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
105 if (length(selectpixels) > 0) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
106 blockval <- blockval / multiplyingfactor | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
107 modelsvr_estimate <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
108 for (modind in 1:seq_along(hybridmodel[[parm]])) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
109 pb$tick() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
110 modelsvr_estimate[[modind]] <- predict(hybridmodel[[parm]][[modind]], blockval) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
111 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
112 modelsvr_estimate <- do.call(cbind, modelsvr_estimate) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
113 # final estimated value = mean parm value for all models | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
114 mean_estimate <- rowMeans(modelsvr_estimate) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
115 # "uncertainty" = STD value for all models | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
116 std_estimate <- rowSds(modelsvr_estimate) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
117 mean_estimatefull[selectpixels] <- mean_estimate | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
118 std_estimatefull[selectpixels] <- std_estimate | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
119 } else { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
120 for (modind in 1:seq_along(hybridmodel[[parm]])) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
121 pb$tick() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
122 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
123 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
124 r_outmean <- writeValues(r_outmean, mean_estimatefull, blk$row[i], format = "ENVI", overwrite = TRUE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
125 r_outsd <- writeValues(r_outsd, std_estimatefull, blk$row[i], format = "ENVI", overwrite = TRUE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
126 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
127 # close files | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
128 r_in <- readStop(r_in) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
129 if (typeof(r_inmask) == "S4") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
130 r_inmask <- readStop(r_inmask) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
131 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
132 r_outmean <- writeStop(r_outmean) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
133 r_outsd <- writeStop(r_outsd) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
134 # write biophysical variable name in headers | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
135 hdr <- read_envi_header(get_hdr_name(bpvarpath)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
136 hdr$`band names` <- paste("{", parm, "}", sep = "") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
137 write_envi_header(hdr, get_hdr_name(bpvarpath)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
138 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
139 print("processing completed") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
140 return(invisible()) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
141 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
142 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
143 #" get hdr name from image file name, assuming it is BIL format | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
144 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
145 #" @param impath path of the image | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
146 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
147 #" @return corresponding hdr | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
148 #" @importFrom tools file_ext file_path_sans_ext | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
149 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
150 get_hdr_name <- function(impath) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
151 if (tools::file_ext(impath) == "") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
152 impathhdr <- paste(impath, ".hdr", sep = "") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
153 } else if (tools::file_ext(impath) == "bil") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
154 impathhdr <- gsub(".bil", ".hdr", impath) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
155 } else if (tools::file_ext(impath) == "zip") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
156 impathhdr <- gsub(".zip", ".hdr", impath) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
157 } else { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
158 impathhdr <- paste(tools::file_path_sans_ext(impath), ".hdr", sep = "") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
159 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
160 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
161 if (!file.exists(impathhdr)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
162 message("WARNING : COULD NOT FIND hdr FILE") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
163 print(impathhdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
164 message("Process may stop") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
165 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
166 return(impathhdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
167 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
168 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
169 #" This function applies the regression models trained with prosail_hybrid_train | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
170 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
171 #" @param regressionmodels list. List of regression models produced by prosail_hybrid_train | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
172 #" @param refl numeric. LUT of bidirectional reflectances factors used for training | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
173 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
174 #" @return hybridres list. Estimated values corresponding to refl. Includes | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
175 #" - meanestimate = mean value for the ensemble regression model | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
176 #" - stdestimate = std value for the ensemble regression model | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
177 #" @importFrom stats predict | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
178 #" @importFrom matrixStats rowSds | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
179 #" @importFrom progress progress_bar | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
180 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
181 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
182 prosail_hybrid_apply <- function(regressionmodels, refl) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
183 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
184 # make sure refl is right dimensions | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
185 refl <- t(refl) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
186 nbfeatures <- regressionmodels[[1]]$dim | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
187 if (!ncol(refl) == nbfeatures && nrow(refl) == nbfeatures) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
188 refl <- t(refl) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
189 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
190 nbensemble <- length(regressionmodels) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
191 estimatedval <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
192 pb <- progress_bar$new( | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
193 format = "Applying SVR models [:bar] :percent in :elapsed", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
194 total = nbensemble, clear = FALSE, width = 100) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
195 for (i in 1:nbensemble) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
196 pb$tick() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
197 estimatedval[[i]] <- predict(regressionmodels[[i]], refl) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
198 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
199 estimatedval <- do.call(cbind, estimatedval) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
200 meanestimate <- rowMeans(estimatedval) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
201 stdestimate <- rowSds(estimatedval) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
202 hybridres <- list("meanestimate" = meanestimate, "stdestimate" = stdestimate) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
203 return(hybridres) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
204 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
205 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
206 #" This function trains a suppot vector regression for a set of variables based on spectral data | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
207 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
208 #" @param brf_lut numeric. LUT of bidirectional reflectances factors used for training | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
209 #" @param inputvar numeric. biophysical parameter corresponding to the reflectance | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
210 #" @param figplot Boolean. Set to TRUE if you want a scatterplot | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
211 #" @param nbensemble numeric. Number of individual subsets should be generated from brf_lut | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
212 #" @param withreplacement Boolean. should subsets be generated with or without replacement? | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
213 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
214 #" @return modelssvr list. regression models trained for the retrieval of inputvar based on brf_lut | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
215 #" @importFrom liquidSVM svmRegression | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
216 #" @importFrom stats predict | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
217 #" @importFrom progress progress_bar | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
218 #" @importFrom graphics par | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
219 #" @importFrom expandFunctions reset.warnings | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
220 #" @importFrom stringr str_split | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
221 #" @importFrom simsalapar tryCatch.W.E | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
222 #" @import dplyr | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
223 #" @import ggplot2 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
224 # @" @import caret | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
225 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
226 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
227 prosail_hybrid_train <- function(brf_lut, inputvar, figplot = FALSE, nbensemble = 20, withreplacement = FALSE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
228 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
229 x <- y <- ymean <- ystdmin <- ystdmax <- NULL | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
230 # split the LUT into nbensemble subsets | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
231 nbsamples <- length(inputvar) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
232 if (dim(brf_lut)[2] == nbsamples) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
233 brf_lut <- t(brf_lut) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
234 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
235 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
236 # if subsets are generated from brf_lut with replacement | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
237 if (withreplacement == TRUE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
238 subsets <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
239 samples_per_run <- round(nbsamples / nbensemble) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
240 for (run in (1:nbensemble)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
241 subsets[[run]] <- sample(seq(1, nbsamples), samples_per_run, replace = TRUE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
242 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
243 # if subsets are generated from brf_lut without replacement | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
244 } else if (withreplacement == FALSE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
245 subsets <- split(sample(seq(1, nbsamples, by = 1)), seq(1, nbensemble, by = 1)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
246 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
247 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
248 # run training for each subset | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
249 modelssvr <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
250 predictedyall <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
251 tunedmodelyall <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
252 pb <- progress_bar$new( | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
253 format = "Training SVR on subsets [:bar] :percent in :elapsed", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
254 total = nbensemble, clear = FALSE, width = 100) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
255 for (i in 1:nbensemble) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
256 pb$tick() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
257 Sys.sleep(1 / 100) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
258 trainingset <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
259 trainingset$X <- brf_lut[subsets[i][[1]], ] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
260 trainingset$Y <- inputvar[subsets[i][[1]]] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
261 # liquidSVM | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
262 r1 <- tryCatch.W.E(tunedmodel <- liquidSVM::svmRegression(trainingset$X, trainingset$Y)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
263 if (!is.null(r1$warning)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
264 msg <- r1$warning$message | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
265 valgamma <- str_split(string = msg, pattern = "gamma=")[[1]][2] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
266 vallambda <- str_split(string = msg, pattern = "lambda=")[[1]][2] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
267 if (!is.na(as.numeric(valgamma))) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
268 message("Adjusting Gamma accordingly") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
269 valgamma <- as.numeric(valgamma) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
270 tunedmodel <- liquidSVM::svmRegression(trainingset$X, trainingset$Y, min_gamma = valgamma) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
271 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
272 if (!is.na(as.numeric(vallambda))) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
273 message("Adjusting Lambda accordingly") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
274 vallambda <- as.numeric(vallambda) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
275 tunedmodel <- liquidSVM::svmRegression(trainingset$X, trainingset$Y, min_lambda = vallambda) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
276 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
277 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
278 modelssvr[[i]] <- tunedmodel | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
279 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
280 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
281 # if scatterplots needed | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
282 if (figplot == TRUE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
283 # predict for full brf_lut | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
284 for (i in 1:nbensemble) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
285 tunedmodely <- stats::predict(modelssvr[[i]], brf_lut) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
286 tunedmodelyall <- cbind(tunedmodelyall, matrix(tunedmodely, ncol = 1)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
287 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
288 # plot prediction | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
289 df <- data.frame(x = rep(1:nbsamples, nbensemble), y = as.numeric(matrix(tunedmodelyall, ncol = 1))) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
290 df_summary <- df %>% | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
291 dplyr::group_by(x) %>% | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
292 summarize(ymin = min(y), ystdmin = mean(y) - sd(y), | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
293 ymax = max(y), ystdmax = mean(y) + sd(y), | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
294 ymean = mean(y)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
295 par(mar = rep(.1, 4)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
296 p <- ggplot(df_summary, aes(x = inputvar, y = ymean)) + | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
297 geom_point(size = 2) + | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
298 geom_errorbar(aes(ymin = ystdmin, ymax = ystdmax)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
299 meanpredict <- rowMeans(matrix(as.numeric(tunedmodelyall), ncol = nbensemble)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
300 print(p) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
301 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
302 return(modelssvr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
303 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
304 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
305 #" Reads ENVI hdr file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
306 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
307 #" @param hdrpath Path of the hdr file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
308 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
309 #" @return list of the content of the hdr file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
310 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
311 read_envi_header <- function(hdrpath) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
312 if (!grepl(".hdr$", hdrpath)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
313 stop("File extension should be .hdr") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
314 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
315 hdr <- readLines(hdrpath) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
316 ## check ENVI at beginning of file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
317 if (!grepl("ENVI", hdr[1])) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
318 stop("Not an ENVI header (ENVI keyword missing)") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
319 } else { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
320 hdr <- hdr [-1] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
321 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
322 ## remove curly braces and put multi-line key-value-pairs into one line | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
323 hdr <- gsub("\\{([^}]*)\\}", "\\1", hdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
324 l <- grep("\\{", hdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
325 r <- grep("\\}", hdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
326 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
327 if (length(l) != length(r)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
328 stop("Error matching curly braces in header (differing numbers).") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
329 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
330 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
331 if (any(r <= l)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
332 stop("Mismatch of curly braces in header.") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
333 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
334 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
335 hdr[l] <- sub("\\{", "", hdr[l]) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
336 hdr[r] <- sub("\\}", "", hdr[r]) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
337 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
338 for (i in rev(seq_along(l))) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
339 hdr <- c( | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
340 hdr [seq_len(l [i] - 1)], | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
341 paste(hdr [l [i]:r [i]], collapse = "\n"), | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
342 hdr [-seq_len(r [i])] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
343 ) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
344 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
345 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
346 ## split key = value constructs into list with keys as names | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
347 hdr <- sapply(hdr, split_line, " = ", USE.NAMES = FALSE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
348 names(hdr) <- tolower(names(hdr)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
349 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
350 ## process numeric values | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
351 tmp <- names(hdr) %in% c( | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
352 "samples", "lines", "bands", "header offset", "data type", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
353 "byte order", "default bands", "data ignore value", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
354 "wavelength", "fwhm", "data gain values" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
355 ) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
356 hdr [tmp] <- lapply(hdr [tmp], function(x) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
357 as.numeric(unlist(strsplit(x, ","))) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
358 }) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
359 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
360 return(hdr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
361 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
362 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
363 #" ENVI functions | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
364 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
365 #" based on https: / / github.com / cran / hyperSpec / blob / master / R / read.ENVI.R | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
366 #" added wavelength, fwhm, ... to header reading | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
367 #" Title | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
368 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
369 #" @param x character. | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
370 #" @param separator character | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
371 #" @param trim_blank boolean. | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
372 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
373 #" @return list. | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
374 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
375 split_line <- function(x, separator, trim_blank = TRUE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
376 tmp <- regexpr(separator, x) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
377 key <- substr(x, 1, tmp - 1) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
378 value <- substr(x, tmp + 1, nchar(x)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
379 if (trim_blank) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
380 blank_pattern <- "^[[:blank:]]*([^[:blank:]] + .*[^[:blank:]] + )[[:blank:]]*$" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
381 key <- sub(blank_pattern, "\\1", key) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
382 value <- sub(blank_pattern, "\\1", value) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
383 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
384 value <- as.list(value) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
385 names(value) <- key | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
386 return(value) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
387 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
388 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
389 #" This function performs full training for hybrid invrsion using SVR with | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
390 #" values for default parameters | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
391 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
392 #" @param minval list. minimum value for input parameters sampled to produce a training LUT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
393 #" @param maxval list. maximum value for input parameters sampled to produce a training LUT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
394 #" @param typedistrib list. Type of distribution. Either "Uniform" or "Gaussian" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
395 #" @param gaussiandistrib list. Mean value and STD corresponding to the parameters sampled with gaussian distribution | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
396 #" @param parmset list. list of input parameters set to a specific value | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
397 #" @param nbsamples numeric. number of samples in training LUT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
398 #" @param nbsamplesperrun numeric. number of training sample per individual regression model | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
399 #" @param nbmodels numeric. number of individual models to be run for ensemble | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
400 #" @param replacement bolean. is there replacement in subsampling? | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
401 #" @param sailversion character. Either 4SAIL or 4SAIL2 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
402 #" @param parms2estimate list. list of input parameters to be estimated | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
403 #" @param bands2select list. list of bands used for regression for each input parameter | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
404 #" @param noiselevel list. list of noise value added to reflectance (defined per input parm) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
405 #" @param specprospect list. Includes optical constants required for PROSPECT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
406 #" @param specsoil list. Includes either dry soil and wet soil, or a unique soil sample if the psoil parameter is not inverted | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
407 #" @param specatm list. Includes direct and diffuse radiation for clear conditions | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
408 #" @param path_results character. path for results | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
409 #" @param figplot boolean. Set TRUE to get scatterplot of estimated biophysical variable during training step | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
410 #" @param force4lowlai boolean. Set TRUE to artificially reduce leaf chemical constituent content for low LAI | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
411 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
412 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
413 #" @return modelssvr list. regression models trained for the retrieval of inputvar based on brf_lut | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
414 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
415 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
416 train_prosail_inversion <- function(minval = NULL, maxval = NULL, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
417 typedistrib = NULL, gaussiandistrib = NULL, parmset = NULL, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
418 nbsamples = 2000, nbsamplesperrun = 100, nbmodels = 20, replacement = TRUE, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
419 sailversion = "4SAIL", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
420 parms2estimate = "lai", bands2select = NULL, noiselevel = NULL, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
421 specprospect = NULL, specsoil = NULL, specatm = NULL, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
422 path_results = "./", figplot = FALSE, force4lowlai = TRUE) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
423 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
424 ###===================================================================### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
425 ### 1- PRODUCE A LUT TO TRAIN THE HYBRID INVERSION ### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
426 ###===================================================================### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
427 # Define sensor characteristics | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
428 if (is.null(specprospect)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
429 specprospect <- prosail::specprospect | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
430 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
431 if (is.null(specsoil)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
432 specsoil <- prosail::specsoil | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
433 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
434 if (is.null(specprospect)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
435 specatm <- prosail::specatm | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
436 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
437 # define distribution for parameters to be sampled | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
438 if (is.null(typedistrib)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
439 typedistrib <- data.frame("CHL" = "Uniform", "CAR" = "Uniform", "EWT" = "Uniform", "ANT" = "Uniform", "LMA" = "Uniform", "N" = "Uniform", "BROWN" = "Uniform", | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
440 "psoil" = "Uniform", "LIDFa" = "Uniform", "lai" = "Uniform", "q" = "Uniform", "tto" = "Uniform", "tts" = "Uniform", "psi" = "Uniform") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
441 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
442 if (is.null(gaussiandistrib)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
443 gaussiandistrib <- list("Mean" = NULL, "Std" = NULL) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
444 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
445 if (is.null(minval)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
446 minval <- data.frame("CHL" = 10, "CAR" = 0, "EWT" = 0.01, "ANT" = 0, "LMA" = 0.005, "N" = 1.0, "psoil" = 0.0, "BROWN" = 0.0, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
447 "LIDFa" = 20, "lai" = 0.5, "q" = 0.1, "tto" = 0, "tts" = 20, "psi" = 80) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
448 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
449 if (is.null(maxval)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
450 maxval <- data.frame("CHL" = 75, "CAR" = 15, "EWT" = 0.03, "ANT" = 2, "LMA" = 0.03, "N" = 2.0, "psoil" = 1.0, "BROWN" = 0.5, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
451 "LIDFa" = 70, "lai" = 7, "q" = 0.2, "tto" = 5, "tts" = 30, "psi" = 110) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
452 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
453 # define min and max values | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
454 # fixed parameters | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
455 if (is.null(parmset)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
456 parmset <- data.frame("TypeLidf" = 2, "alpha" = 40) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
457 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
458 # produce input parameters distribution | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
459 if (sailversion == "4SAIL") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
460 inputprosail <- get_distribution_input_prosail(minval, maxval, parmset, nbsamples, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
461 typedistrib = typedistrib, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
462 Mean = gaussiandistrib$Mean, Std = gaussiandistrib$Std, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
463 force4lowlai = force4lowlai) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
464 } else if (sailversion == "4SAIL2") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
465 inputprosail <- get_distribution_input_prosail2(minval, maxval, parmset, nbsamples, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
466 typedistrib = typedistrib, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
467 Mean = gaussiandistrib$Mean, Std = gaussiandistrib$Std, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
468 force4lowlai = force4lowlai) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
469 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
470 if (sailversion == "4SAIL2") { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
471 # Definition of Cv && update LAI | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
472 maxlai <- min(c(maxval$lai), 4) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
473 inputprosail$Cv <- NA * inputprosail$lai | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
474 inputprosail$Cv[which(inputprosail$lai > maxlai)] <- 1 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
475 inputprosail$Cv[which(inputprosail$lai <= maxlai)] <- (1 / maxlai) + inputprosail$lai[which(inputprosail$lai <= maxlai)] / (maxlai + 1) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
476 inputprosail$Cv <- inputprosail$Cv * matrix(rnorm(length(inputprosail$Cv), mean = 1, sd = 0.1)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
477 inputprosail$Cv[which(inputprosail$Cv < 0)] <- 0 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
478 inputprosail$Cv[which(inputprosail$Cv > 1)] <- 1 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
479 inputprosail$Cv[which(inputprosail$lai > maxlai)] <- 1 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
480 inputprosail$fraction_brown <- 0 + 0 * inputprosail$lai | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
481 inputprosail$diss <- 0 + 0 * inputprosail$lai | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
482 inputprosail$Zeta <- 0.2 + 0 * inputprosail$lai | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
483 inputprosail$lai <- inputprosail$lai * inputprosail$Cv | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
484 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
485 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
486 # generate LUT of BRF corresponding to inputprosail, for a sensor | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
487 brf_lut <- Generate_LUT_BRF(sailversion = sailversion, inputprosail = inputprosail, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
488 specprospect = specprospect, specsoil = specsoil, specatm = specatm) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
489 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
490 # write parameters LUT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
491 output <- matrix(unlist(inputprosail), ncol = length(inputprosail), byrow = FALSE) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
492 filename <- file.path(path_results, "PROSAIL_LUT_InputParms.txt") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
493 write.table(x = format(output, digits = 3), file = filename, append = FALSE, quote = FALSE, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
494 col.names = names(inputprosail), row.names = FALSE, sep = "\t") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
495 # Write BRF LUT corresponding to parameters LUT | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
496 filename <- file.path(path_results, "PROSAIL_LUT_reflectance.txt") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
497 write.table(x = format(t(brf_lut), digits = 5), file = filename, append = FALSE, quote = FALSE, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
498 col.names = specprospect$lambda, row.names = FALSE, sep = "\t") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
499 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
500 # Which bands will be used for inversion? | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
501 if (is.null(bands2select)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
502 bands2select <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
503 for (parm in parms2estimate) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
504 bands2select[[parm]] <- seq(1, length(specprospect$lambda)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
505 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
506 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
507 # Add gaussian noise to reflectance LUT: one specific LUT per parameter | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
508 if (is.null(noiselevel)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
509 noiselevel <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
510 for (parm in parms2estimate) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
511 noiselevel[[parm]] <- 0.01 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
512 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
513 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
514 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
515 # produce LIT with noise | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
516 brf_lut_noise <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
517 for (parm in parms2estimate) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
518 brf_lut_noise[[parm]] <- brf_lut[bands2select[[parm]], ] + brf_lut[bands2select[[parm]], ] * matrix(rnorm(nrow(brf_lut[bands2select[[parm]], ]) * ncol(brf_lut[bands2select[[parm]], ]), | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
519 0, noiselevel[[parm]]), nrow = nrow(brf_lut[bands2select[[parm]], ])) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
520 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
521 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
522 ###===================================================================### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
523 ### PERFORM HYBRID INVERSION ### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
524 ###===================================================================### | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
525 # train SVR for each variable and each run | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
526 modelsvr <- list() | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
527 for (parm in parms2estimate) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
528 colparm <- which(parm == names(inputprosail)) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
529 inputvar <- inputprosail[[colparm]] | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
530 modelsvr[[parm]] <- prosail_hybrid_train(brf_lut = brf_lut_noise[[parm]], inputvar = inputvar, | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
531 figplot = figplot, nbensemble = nbmodels, withreplacement = replacement) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
532 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
533 return(modelsvr) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
534 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
535 | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
536 #" writes ENVI hdr file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
537 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
538 #" @param hdr content to be written | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
539 #" @param hdrpath Path of the hdr file | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
540 #" | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
541 #" @return None | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
542 #" @importFrom stringr str_count | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
543 #" @export | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
544 write_envi_header <- function(hdr, hdrpath) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
545 h <- lapply(hdr, function(x) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
546 if (length(x) > 1 || (is.character(x) && stringr::str_count(x, "\\w + ") > 1)) { | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
547 x <- paste0("{", paste(x, collapse = ","), "}") | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
548 } | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
549 # convert last numerics | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
550 x <- as.character(x) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
551 }) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
552 writeLines(c("ENVI", paste(names(hdr), h, sep = "=")), con = hdrpath) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
553 return(invisible()) | 
| 
 
33a1e15f7252
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
554 } | 
