Mercurial > repos > ecology > srs_diversity_maps
annotate pca_raster.r @ 0:36ff7a0b96dc draft default tip
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
| author | ecology | 
|---|---|
| date | Mon, 09 Jan 2023 13:55:01 +0000 | 
| parents | |
| children | 
| rev | line source | 
|---|---|
| 
0
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
1 #Rscript | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
2 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
3 ########################################### | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
4 ## Getting PCA raster ## | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
5 ########################################### | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
6 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
7 #####Packages : stars | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
8 # utils | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
9 # biodivmapr | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
10 # raster | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
11 # sf | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
12 # mapview | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
13 # leafpop | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
14 # RColorBrewer | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
15 # labdsv | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
16 # rgdal | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
17 # ggplot2 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
18 # gridExtra | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
19 ## remotes::install_github("jbferet/biodivMapR") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
20 #####Load arguments | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
21 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
22 args <- commandArgs(trailingOnly = TRUE) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
23 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
24 #####Import the S2 data | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
25 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
26 if (length(args) < 1) { | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
28 }else { | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
29 data_raster <- args[1] | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
30 rasterheader <- args[2] | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
31 data <- args[3] | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
32 typepca <- as.character(args[4]) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
33 source(args[5]) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
34 } | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
35 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
36 ################################################################################ | 
| 
 
36ff7a0b96dc
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 ## | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
38 ################################################################################ | 
| 
 
36ff7a0b96dc
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 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
40 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
41 if (data_raster == "") { | 
| 
 
36ff7a0b96dc
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 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
43 dir.create("data_dir") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
44 unzip(data, exdir = "data_dir") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
45 # Path to raster | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
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]) | 
| 
 
36ff7a0b96dc
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]) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
49 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
50 } else { | 
| 
 
36ff7a0b96dc
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 = "/") | 
| 
 
36ff7a0b96dc
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 = "/") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
53 } | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
54 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
55 ################################################################################ | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
56 ## PROCESS IMAGE ## | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
57 ################################################################################ | 
| 
 
36ff7a0b96dc
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 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
59 print("PERFORM PCA ON RASTER") | 
| 
 
36ff7a0b96dc
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, | 
| 
 
36ff7a0b96dc
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) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
62 | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
63 | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
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) | 
| 
 
36ff7a0b96dc
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) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
67 | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
70 | 
| 
 
36ff7a0b96dc
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) | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
72 #### Get the raster layer files | 
| 
 
36ff7a0b96dc
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") | 
| 
 
36ff7a0b96dc
planemo upload for repository https://github.com/Marie59/Sentinel_2A/srs_tools commit b32737c1642aa02cc672534e42c5cb4abe0cd3e7
 
ecology 
parents:  
diff
changeset
 | 
74 to_dir_short <- output_dir | 
| 
 
36ff7a0b96dc
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 | 
