-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject_coralNet_envGrid.R
74 lines (69 loc) · 3.25 KB
/
project_coralNet_envGrid.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
library(raster)
library(stringr)
### VARIABLES
# Path Env data
env.dir <- "R:/IMAS/Antarctic_Seafloor/environmental_data/"
# Path Bio data
bio.dir <- "R:/IMAS/Antarctic_Seafloor/Clean_Data_For_Permanent_Storage"
# Survey
survey <- "PS96"
# Metadata suffix
metadata.suffix <- "_1_raw_images_and_metadata/metadata"
# CoralNet suffix
coralnet.suffix <- "_4_annotation_library_cover"
# Metadata fname
metadata.fname <- "PS96_dat_FINAL.Rdata"
metadata.path <- paste0(c(bio.dir, survey, paste0(survey, metadata.suffix),
metadata.fname), collapse="/")
# CSV fname
coralnet.csv.fname <- "PS96_annotations_03_201912.csv"
coralnet.csv.path <- paste0(c(bio.dir, survey, paste0(survey, coralnet.suffix),
coralnet.csv.fname), collapse="/")
# CSV labels of interest
labels.fname <- "C:/Users/cgros/code/biigle_scripts/coralnet_to_biigle.csv"
labels.name <- cbind(read.csv(labels.fname)[,1])
labels.unscorable <- c("NoID", "NoID_ExpOp", "NoID_TBD", "Unscorable")
# Output file
output.fname <- "R:/IMAS/Antarctic_Seafloor/Clean_Data_For_Permanent_Storage/PS96/PS96_6_VMEs_annotation_library_counts/coralNet_count_envGrid.csv"
### BATHYMETRY
depth <- raster(paste0(env.dir,"Circumpolar_EnvData_bathy_fulldepth.gri"))$depth
stereo <- as.character(depth@crs)
# BIOLOGICAL DATA
dat.coralnet.full <- read.csv(coralnet.csv.path, header=TRUE)
dat.metadata.full <- get(load(metadata.path))
# Get metadata data of interest, ie discard images not included in labelling
coralnet.fname <- dat.coralnet.full$Name
image.fname <- str_split_fixed(str_split_fixed(dat.coralnet.full$Name, "__", 2)[,-1],
".jpg", 2)[, 1]
image.fname <- cbind(image.fname)
dat.metadata <- subset(dat.metadata.full, Filename %in% image.fname)
# Match column names betweem the two dataframes
dat.coralnet.full$Filename <- image.fname
dat.coralnet.clean <- dat.coralnet.full[c("Filename", "Label")]
# Get coralNet data of interest, ie discard images not included in labelling
dat.coralnet <- subset(dat.coralnet.clean, Label %in% c(labels.name, labels.unscorable))
# Create biological dataframe
col.names <- c(c("Filename", "Longitude", "Latitude"),
c(labels.name, labels.unscorable))
dat.bio <- data.frame(matrix(ncol = length(col.names), nrow = 0))
colnames(dat.bio) <- col.names
for (row in 1:nrow(dat.metadata)) {
metadata.current <- dat.metadata[row, c("Filename", "Longitude", "Latitude")]
filename.current <- dat.metadata[row, "Filename"]
coralnet.current <- dat.coralnet[dat.coralnet$Filename == filename.current, ]
occurences <- data.frame(rbind(table(coralnet.current$Label)))
labels.missing <- setdiff(c(labels.name, labels.unscorable), colnames(occurences))
occurences[labels.missing] <- 0
dat.bio <- rbind(dat.bio, cbind(metadata.current, occurences))
}
# PROJECTION on environmental grid
spatial.dat <- data.frame(cbind(dat.bio$Longitude, dat.bio$Latitude))
names(spatial.dat) <- c("Longitude","Latitude")
coordinates(spatial.dat) <- c("Longitude","Latitude")
proj4string(spatial.dat) <- CRS("+proj=longlat +datum=WGS84")
polar.dat.bio <- spTransform(spatial.dat, CRS(stereo))
cell.id <- extract(depth, polar.dat.bio, cellnumbers=TRUE)[,1]
cell=cell.id
dat.bio <- cbind(cell, dat.bio)
# SAVE RESULT
write.csv(dat.bio, output.fname, row.names = FALSE)