-
Notifications
You must be signed in to change notification settings - Fork 0
/
extraction.R
141 lines (101 loc) · 4.29 KB
/
extraction.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#script for extracting environmental variables to tracks and pseudoabsences
setwd("~/OneDrive - University of Southampton/Documents/Southern Right Whales")
{
library(terra)
library(dplyr)
library(lubridate)
library(tidyterra)
library(ggplot2)
}
rm(list=ls())
#specify region
this.pop <- "NZ"
#read in tracks and background
tracks <- read.csv(paste0("data/", this.pop, "_SRW_SSM_track_data.csv"))
background <- read.csv(paste0("output/background/", this.pop, "_background.csv"))
#format for extraction - keep relevant columns
tracks <- tracks %>% select(id, date, lon, lat)
background <- background %>% select(-X) %>% filter(x<0)
#format
tracks <- tracks %>% rename(x = lon, y = lat)
tracks$date <- as_datetime(tracks$date)
background$date <- as_datetime(background$date)
#---------------
#Static Variables
###Depth###
depth <- rast("D:/Satellite_Data/static/depth/depth.nc")
#create SpatVector for tracks and background
tracks <- vect(tracks,
geom=c("x", "y"),
crs=crs(depth)) #this ensures crs are the same as rasters
background <- vect(background,
geom=c("x", "y"),
crs=crs(depth))
#extract
tracks$depth <- extract(depth, tracks, ID=F)
background$depth <- extract(depth, background, ID=F)
#remove rows where depth is NA - will be NA for every GLORYS variable
tracks <- tracks %>% drop_na(depth)
background <- background %>% drop_na(depth)
###Slope###
slope <- rast("D:/Satellite_Data/static/slope/slope.nc")
tracks$slope <- extract(slope, tracks, ID=F)
background$slope <- extract(slope, background, ID=F)
###dShelf### - does this work beyond 40 south?
dshelf <- rast("D:/Satellite_Data/static/dshelf/dshelf_resampled.nc")
tracks$dshelf <- extract(dshelf, tracks, ID=F)
background$dshelf <- extract(dshelf, background, ID=F)
#cleanup static
rm(depth, slope, dshelf)
#---------------
#Dynamic Variables
#dynamic_extract function from 05a script
source("code/dynamic_extract_function.R")
###SST###
tracks <- dynamic_extract(predictor = "sst", tracks)
background <- dynamic_extract(predictor = "sst", background)
###MLD###
tracks <- dynamic_extract(predictor = "mld", tracks)
background <- dynamic_extract(predictor = "mld", background)
###SAL###
tracks <- dynamic_extract(predictor = "sal", tracks)
background <- dynamic_extract(predictor = "sal", background)
###SSH###
tracks <- dynamic_extract(predictor = "ssh", tracks)
background <- dynamic_extract(predictor = "ssh", background)
###SIC###
tracks <- dynamic_extract(predictor = "sic", tracks)
tracks$sic[is.na(tracks$sic)] <- 0 #SIC values of 0 print as NA in GLORYS
background <- dynamic_extract(predictor = "sic", background)
background$sic[is.na(background$sic)] <- 0
###CURR###
tracks <- dynamic_extract(predictor = "uo", tracks) #uo is eastward velocity
background <- dynamic_extract(predictor = "uo", background)
tracks <- dynamic_extract(predictor = "vo", tracks) #vo is northwards velocity
background <- dynamic_extract(predictor = "vo", background)
tracks$curr <- sqrt((tracks$uo^2) + (tracks$vo^2)) #current speed
background$curr <- sqrt((background$uo^2) + (background$vo^2))
###CHL###
#uses different function for resampled files
source("code/dynamic_chlorophyll_function.R") #unique function for different file structure
tracks <- dynamic_chlorophyll(predictor = "chl", tracks)
background <- dynamic_chlorophyll(predictor = "chl", background)
###WIND###
#uses different function for monthly file structure
source("code/dynamic_wind_function.R")
tracks <- dynamic_wind(predictor = "wind", tracks = tracks, direction = "east")
tracks <- dynamic_wind(predictor = "wind", tracks = tracks, direction = "north")
tracks$wind <- sqrt(tracks$wind_east^2 + tracks$wind_north^2)
background <- dynamic_wind(predictor = "wind", background, direction = "east")
background <- dynamic_wind(predictor = "wind", background, direction = "north")
background$wind <- sqrt(background$wind_east^2 + background$wind_north^2)
#---------------
#Export
plot(tracks, pch=".")
tracks <- as.data.frame(tracks, geom="XY")
plot(background, pch=".")
background <- as.data.frame(background, geom="XY")
write.csv(tracks,
file=paste0("output/extraction/", this.pop, "/", this.pop, "_presences_extracted.csv"))
write.csv(background,
file=paste0("output/extraction/", this.pop, "/", this.pop, "_background_extracted_2.csv"))