Mercurial > repos > ecology > srs_pca
annotate prosail-master/R/Lib_PROSAIL_HybridInversion.R @ 1:9300a492b634 draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit 0f331043178bbfbe5cda0e31d887971464b3071c
| author | ecology | 
|---|---|
| date | Fri, 07 Apr 2023 15:58:46 +0000 | 
| parents | 79bcb8c6dff0 | 
| children | 
| rev | line source | 
|---|---|
| 0 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 1 # ============================================================================= = | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 2 # prosail | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 3 # Lib_PROSAIL_HybridInversion.R | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 4 # ============================================================================= = | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 5 # PROGRAMMERS: | 
| 
79bcb8c6dff0
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> | 
| 
79bcb8c6dff0
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> | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 9 # ============================================================================= = | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 11 # approach based on SVM regression | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 12 # ============================================================================= = | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 13 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 14 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 16 #" vegetation biophysical properties | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 17 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 27 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 28 #" @return None | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 29 #" @importFrom progress progress_bar | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 30 #" @importFrom stars read_stars | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 32 #" @import rgdal | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 33 #" @export | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 35 selectedbands, bandname, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 36 maskraster = FALSE, multiplyingfactor = 10000) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 37 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 39 bpvar <- names(hybridmodel) | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 41 print(bpvar) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 42 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 43 # get image dimensions | 
| 
79bcb8c6dff0
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") { | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 47 } else { | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
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])) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 50 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 51 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 53 for (parm in bpvar) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 54 print(paste("Computing", parm, sep = " ")) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 56 blk <- blockSize(brick(raster_path)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 57 # reflectance file | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 59 # mask file | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 60 r_inmask <- FALSE | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 61 if (maskraster == FALSE) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 62 selectpixels <- "ALL" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 63 } else if (!maskraster == FALSE) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 64 if (file.exists(maskraster)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 65 r_inmask <- readStart(raster(maskraster)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 66 } else if (!file.exists(maskraster)) { | 
| 
79bcb8c6dff0
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:") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 68 print(maskraster) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 69 message("Processing all image") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 70 selectpixels <- "ALL" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 71 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 72 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 73 # initiate progress bar | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 75 pb <- progress_bar$new( | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 78 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 79 # output files | 
| 
79bcb8c6dff0
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 = "_")) | 
| 
79bcb8c6dff0
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 = "_")) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 84 selbands <- match(selectedbands[[parm]], bandname) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 85 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 86 # loop over blocks | 
| 
79bcb8c6dff0
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)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 88 # read values for block | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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]) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 91 fulllength <- dim(blockval)[1] | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 92 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 93 if (typeof(r_inmask) == "logical") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 94 blockval <- blockval[, selbands] | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 96 selectpixels <- which(blockval[, 1] > 0) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 97 blockval <- blockval[selectpixels, ] | 
| 
79bcb8c6dff0
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") { | 
| 
79bcb8c6dff0
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]) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 100 selectpixels <- which(maskval == 1) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 101 blockval <- blockval[selectpixels, selbands] | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 102 } | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 105 if (length(selectpixels) > 0) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 106 blockval <- blockval / multiplyingfactor | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 107 modelsvr_estimate <- list() | 
| 
79bcb8c6dff0
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]])) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 109 pb$tick() | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 111 } | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 114 mean_estimate <- rowMeans(modelsvr_estimate) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 116 std_estimate <- rowSds(modelsvr_estimate) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 117 mean_estimatefull[selectpixels] <- mean_estimate | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 118 std_estimatefull[selectpixels] <- std_estimate | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 119 } else { | 
| 
79bcb8c6dff0
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]])) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 121 pb$tick() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 122 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 123 } | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 126 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 127 # close files | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 128 r_in <- readStop(r_in) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 129 if (typeof(r_inmask) == "S4") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 130 r_inmask <- readStop(r_inmask) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 131 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 132 r_outmean <- writeStop(r_outmean) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 133 r_outsd <- writeStop(r_outsd) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
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 = "") | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 138 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 139 print("processing completed") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 140 return(invisible()) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 141 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 142 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 144 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 146 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 147 #" @return corresponding hdr | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 149 #" @export | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 150 get_hdr_name <- function(impath) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 151 if (tools::file_ext(impath) == "") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 152 impathhdr <- paste(impath, ".hdr", sep = "") | 
| 
79bcb8c6dff0
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") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 154 impathhdr <- gsub(".bil", ".hdr", impath) | 
| 
79bcb8c6dff0
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") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 156 impathhdr <- gsub(".zip", ".hdr", impath) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 157 } else { | 
| 
79bcb8c6dff0
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 = "") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 159 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 160 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 161 if (!file.exists(impathhdr)) { | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 163 print(impathhdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 164 message("Process may stop") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 165 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 166 return(impathhdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 167 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 168 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 170 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 173 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 177 #" @importFrom stats predict | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 178 #" @importFrom matrixStats rowSds | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 179 #" @importFrom progress progress_bar | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 180 #" @export | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 181 | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 183 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 185 refl <- t(refl) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 186 nbfeatures <- regressionmodels[[1]]$dim | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 188 refl <- t(refl) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 189 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 190 nbensemble <- length(regressionmodels) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 191 estimatedval <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 192 pb <- progress_bar$new( | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 195 for (i in 1:nbensemble) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 196 pb$tick() | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 198 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 199 estimatedval <- do.call(cbind, estimatedval) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 200 meanestimate <- rowMeans(estimatedval) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 201 stdestimate <- rowSds(estimatedval) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 203 return(hybridres) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 204 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 205 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 207 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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? | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 213 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 215 #" @importFrom liquidSVM svmRegression | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 216 #" @importFrom stats predict | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 217 #" @importFrom progress progress_bar | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 218 #" @importFrom graphics par | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 219 #" @importFrom expandFunctions reset.warnings | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 220 #" @importFrom stringr str_split | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 221 #" @importFrom simsalapar tryCatch.W.E | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 222 #" @import dplyr | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 223 #" @import ggplot2 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 224 # @" @import caret | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 225 #" @export | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 226 | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 228 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 231 nbsamples <- length(inputvar) | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 233 brf_lut <- t(brf_lut) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 234 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 235 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 237 if (withreplacement == TRUE) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 238 subsets <- list() | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 240 for (run in (1:nbensemble)) { | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 242 } | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 244 } else if (withreplacement == FALSE) { | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 246 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 247 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 248 # run training for each subset | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 249 modelssvr <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 250 predictedyall <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 251 tunedmodelyall <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 252 pb <- progress_bar$new( | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 255 for (i in 1:nbensemble) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 256 pb$tick() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 257 Sys.sleep(1 / 100) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 258 trainingset <- list() | 
| 
79bcb8c6dff0
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]], ] | 
| 
79bcb8c6dff0
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]]] | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 261 # liquidSVM | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 263 if (!is.null(r1$warning)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 264 msg <- r1$warning$message | 
| 
79bcb8c6dff0
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] | 
| 
79bcb8c6dff0
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] | 
| 
79bcb8c6dff0
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))) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 268 message("Adjusting Gamma accordingly") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 269 valgamma <- as.numeric(valgamma) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 271 } | 
| 
79bcb8c6dff0
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))) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 273 message("Adjusting Lambda accordingly") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 274 vallambda <- as.numeric(vallambda) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 276 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 277 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 278 modelssvr[[i]] <- tunedmodel | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 279 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 280 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 281 # if scatterplots needed | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 282 if (figplot == TRUE) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 283 # predict for full brf_lut | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 284 for (i in 1:nbensemble) { | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 287 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 288 # plot prediction | 
| 
79bcb8c6dff0
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))) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 290 df_summary <- df %>% | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 291 dplyr::group_by(x) %>% | 
| 
79bcb8c6dff0
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), | 
| 
79bcb8c6dff0
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), | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 294 ymean = mean(y)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 295 par(mar = rep(.1, 4)) | 
| 
79bcb8c6dff0
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)) + | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 297 geom_point(size = 2) + | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 300 print(p) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 301 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 302 return(modelssvr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 303 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 304 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 305 #" Reads ENVI hdr file | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 306 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 308 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 310 #" @export | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 311 read_envi_header <- function(hdrpath) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 312 if (!grepl(".hdr$", hdrpath)) { | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 314 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 315 hdr <- readLines(hdrpath) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 317 if (!grepl("ENVI", hdr[1])) { | 
| 
79bcb8c6dff0
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)") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 319 } else { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 320 hdr <- hdr [-1] | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 321 } | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 323 hdr <- gsub("\\{([^}]*)\\}", "\\1", hdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 324 l <- grep("\\{", hdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 325 r <- grep("\\}", hdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 326 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 327 if (length(l) != length(r)) { | 
| 
79bcb8c6dff0
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).") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 329 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 330 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 331 if (any(r <= l)) { | 
| 
79bcb8c6dff0
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.") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 333 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 334 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 335 hdr[l] <- sub("\\{", "", hdr[l]) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 336 hdr[r] <- sub("\\}", "", hdr[r]) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 337 | 
| 
79bcb8c6dff0
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))) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 339 hdr <- c( | 
| 
79bcb8c6dff0
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)], | 
| 
79bcb8c6dff0
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"), | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 342 hdr [-seq_len(r [i])] | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 343 ) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 344 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 345 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 348 names(hdr) <- tolower(names(hdr)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 349 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 350 ## process numeric values | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 351 tmp <- names(hdr) %in% c( | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 354 "wavelength", "fwhm", "data gain values" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 355 ) | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 357 as.numeric(unlist(strsplit(x, ","))) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 358 }) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 359 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 360 return(hdr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 361 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 362 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 363 #" ENVI functions | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 364 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 367 #" Title | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 368 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 369 #" @param x character. | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 370 #" @param separator character | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 371 #" @param trim_blank boolean. | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 372 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 373 #" @return list. | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 374 #" @export | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 376 tmp <- regexpr(separator, x) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 379 if (trim_blank) { | 
| 
79bcb8c6dff0
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:]]*$" | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 383 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 384 value <- as.list(value) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 385 names(value) <- key | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 386 return(value) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 387 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 388 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 390 #" values for default parameters | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 391 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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? | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 411 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 412 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 414 #" @export | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 415 | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 419 sailversion = "4SAIL", | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 423 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 424 ###===================================================================### | 
| 
79bcb8c6dff0
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 ### | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 426 ###===================================================================### | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 427 # Define sensor characteristics | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 428 if (is.null(specprospect)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 429 specprospect <- prosail::specprospect | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 430 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 431 if (is.null(specsoil)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 432 specsoil <- prosail::specsoil | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 433 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 434 if (is.null(specprospect)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 435 specatm <- prosail::specatm | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 436 } | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 438 if (is.null(typedistrib)) { | 
| 
79bcb8c6dff0
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", | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 441 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 442 if (is.null(gaussiandistrib)) { | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 444 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 445 if (is.null(minval)) { | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 448 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 449 if (is.null(maxval)) { | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 452 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 453 # define min and max values | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 454 # fixed parameters | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 455 if (is.null(parmset)) { | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 457 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 458 # produce input parameters distribution | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 459 if (sailversion == "4SAIL") { | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 461 typedistrib = typedistrib, | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 463 force4lowlai = force4lowlai) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 464 } else if (sailversion == "4SAIL2") { | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 466 typedistrib = typedistrib, | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 468 force4lowlai = force4lowlai) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 469 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 470 if (sailversion == "4SAIL2") { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 471 # Definition of Cv && update LAI | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 473 inputprosail$Cv <- NA * inputprosail$lai | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 484 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 485 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 489 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 490 # write parameters LUT | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 499 | 
| 
79bcb8c6dff0
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? | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 501 if (is.null(bands2select)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 502 bands2select <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 503 for (parm in parms2estimate) { | 
| 
79bcb8c6dff0
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)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 505 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 506 } | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 508 if (is.null(noiselevel)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 509 noiselevel <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 510 for (parm in parms2estimate) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 511 noiselevel[[parm]] <- 0.01 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 512 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 513 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 514 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 515 # produce LIT with noise | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 516 brf_lut_noise <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 517 for (parm in parms2estimate) { | 
| 
79bcb8c6dff0
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]], ]), | 
| 
79bcb8c6dff0
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]], ])) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 520 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 521 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 522 ###===================================================================### | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 523 ### PERFORM HYBRID INVERSION ### | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 524 ###===================================================================### | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 526 modelsvr <- list() | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 527 for (parm in parms2estimate) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 528 colparm <- which(parm == names(inputprosail)) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 529 inputvar <- inputprosail[[colparm]] | 
| 
79bcb8c6dff0
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, | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 532 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 533 return(modelsvr) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 534 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 535 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 536 #" writes ENVI hdr file | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 537 #" | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
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 | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 540 #" | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 541 #" @return None | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 542 #" @importFrom stringr str_count | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 543 #" @export | 
| 
79bcb8c6dff0
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) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 545 h <- lapply(hdr, function(x) { | 
| 
79bcb8c6dff0
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)) { | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 547 x <- paste0("{", paste(x, collapse = ","), "}") | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 548 } | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 549 # convert last numerics | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 550 x <- as.character(x) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 551 }) | 
| 
79bcb8c6dff0
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) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 553 return(invisible()) | 
| 
79bcb8c6dff0
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 554 } | 
