Mercurial > repos > ecology > srs_spectral_indices
annotate pca_raster.r @ 1:0c6db524ae70 draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit 0f331043178bbfbe5cda0e31d887971464b3071c
| author | ecology | 
|---|---|
| date | Fri, 07 Apr 2023 16:00:20 +0000 | 
| parents | 20604db47df5 | 
| children | 
| rev | line source | 
|---|---|
| 0 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 1 #Rscript | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 2 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 3 ########################################### | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 4 ## Getting PCA raster ## | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 5 ########################################### | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 6 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 7 #####Packages : stars | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 8 # utils | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 9 # biodivmapr | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 10 # raster | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 11 # sf | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 12 # mapview | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 13 # leafpop | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 14 # RColorBrewer | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 15 # labdsv | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 16 # rgdal | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 17 # ggplot2 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 18 # gridExtra | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 19 ## remotes::install_github("jbferet/biodivMapR") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 20 #####Load arguments | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 21 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 22 args <- commandArgs(trailingOnly = TRUE) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 23 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 24 #####Import the S2 data | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 25 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 26 if (length(args) < 1) { | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 27 stop("This tool needs at least 1 argument") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 28 }else { | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 29 data_raster <- args[1] | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 30 rasterheader <- args[2] | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 31 data <- args[3] | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 32 typepca <- as.character(args[4]) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 33 source(args[5]) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 34 } | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 35 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 36 ################################################################################ | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 37 ## DEFINE PARAMETERS FOR DATASET TO BE PROCESSED ## | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 38 ################################################################################ | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 39 # expected to be in ENVI HDR | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 40 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 41 if (data_raster == "") { | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 42 #Create a directory where to unzip your folder of data | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 43 dir.create("data_dir") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 44 unzip(data, exdir = "data_dir") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 45 # Path to raster | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 46 data_raster <- list.files("data_dir/results/Reflectance", pattern = "_Refl") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 47 input_image_file <- file.path("data_dir/results/Reflectance", data_raster[1]) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 48 input_header_file <- file.path("data_dir/results/Reflectance", data_raster[2]) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 49 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 50 } else { | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 51 input_image_file <- file.path(getwd(), data_raster, fsep = "/") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 52 input_header_file <- file.path(getwd(), rasterheader, fsep = "/") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 53 } | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 54 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 55 ################################################################################ | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 56 ## PROCESS IMAGE ## | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 57 ################################################################################ | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 58 # 1- Filter data in order to discard non vegetated / shaded / cloudy pixels | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 59 print("PERFORM PCA ON RASTER") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 60 pca_output <- biodivMapR::perform_PCA(Input_Image_File = input_image_file, Input_Mask_File = input_mask_file, | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 61 Output_Dir = output_dir, TypePCA = typepca, FilterPCA = filterpca, nbCPU = nbcpu, MaxRAM = maxram) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 62 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 63 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 64 pca_path <- file.path(output_dir, basename(data_raster), typepca, "PCA", "OutputPCA_8_PCs") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 65 pca_raster <- raster::raster(pca_path) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 66 get_pca <- convert_raster(pca_raster) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 67 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 68 colnames(get_pca) <- c("PCA", "longitude", "latitude") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 69 plot_indices(get_pca, titre = "PCA") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 70 | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 71 write.table(get_pca, file = "PCA.tabular", sep = "\t", dec = ".", na = " ", row.names = FALSE, col.names = TRUE, quote = FALSE) | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 72 #### Get the raster layer files | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 73 pca_files <- file.path("RESULTS", basename(data_raster), typepca, "PCA") | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 74 to_dir_short <- output_dir | 
| 
20604db47df5
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 ecology parents: diff
changeset | 75 file.copy(pca_files, to_dir_short) #copy files from long to short paths | 
