forked from sewaneedata/bats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDataRead.R
147 lines (117 loc) · 5.22 KB
/
DataRead.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
141
142
143
144
145
# Hallie Rutten, Monae Scott, Shelby Cline
# DataLab 2022, Sewanee Bat Study
#--PREP--######################################################################
# load libraries -----
library(tidyverse)
library(lubridate)
library(readxl)
library(scales)
library(plotly)
library(ggthemes)
#--BATS--######################################################################
# read bat acoustic data -----
bats <- readRDS('/Data/Bats/data-bats.RData')
#bats <- read.csv('../../data-bats.csv')
# throw out noise, noID for performance
bats <- bats %>%
filter(AUTO.ID != 'no.ID' & AUTO.ID != 'Noise')
# calculate new vars -----
bats <- bats %>%
mutate(year = year(DATE),
monthN = month(DATE),
month = month.abb[monthN],
hour = hour(TIME),
siteID = paste(COMPARTMENT,SITE, sep='_'),
siteDate = paste(SITE, DATE),
AUTO.ID = gsub("CORTOW", "CORRAF", AUTO.ID),
AUTO.ID = ifelse(grepl('noID',AUTO.ID,ignore.case=TRUE),'no.ID',AUTO.ID) )
# Any bat listed as CORTOW should be listed as CORRAF (Amy text)
# cave status column -----
cave_obligate = c('MYOLEI','MYOAUS','MYOLUC','MYOSOD','MYOGRI','MYOSEP')
seasonal_cave_obligate = c('PERSUB',"EPTFUS", "CORRAF")
non_cave_obligate = c('LASBOR','NYCHUM','LASNOC','LASCIN')
bats <- bats %>%
mutate( obligate = ifelse(AUTO.ID %in% cave_obligate, "cave obligate", AUTO.ID),
obligate = ifelse(AUTO.ID %in% non_cave_obligate, "not cave obligate", obligate),
obligate = ifelse(AUTO.ID %in% seasonal_cave_obligate, "seasonal cave obligate", obligate))
# species group column -----
EPTFUS.LASNOC = c("EPTFUS", "LASNOC")
LASBOR.NYCHUM = c("LASBOR", "NYCHUM")
bats <- bats %>%
mutate(ID_group = ifelse(grepl("^MYO", AUTO.ID), "MYOTIS", AUTO.ID),
ID_group = ifelse(AUTO.ID %in% EPTFUS.LASNOC, "EPTFUS.LASNOC", ID_group),
ID_group = ifelse(AUTO.ID %in% LASBOR.NYCHUM, "LASBOR.NYCHUM", ID_group))
#--SENSORS--###################################################################
# read sensor site and date data -----
sensors <- read_csv('/Data/Bats/sensorCSdates.csv', show_col_types=FALSE)
sensors <- sensors %>%
mutate( siteID = gsub('-','_',siteID),
LONGITUDE = -abs(LONGITUDE),
LATITUDE = abs(LATITUDE),
habitat = toupper(habitat),
habitat = gsub(" FOREST","",habitat),
habitat = gsub("UNAMANAGED","UNMANAGED",habitat),
habitat = gsub("UNAMANGED","UNMANAGED",habitat) )
# make each date one entry -----
sns <- sensors %>% select(-startDate,-endDate)
sensorDates <- data.frame()
for(i in 1:nrow(sensors) ){
temp <- data.frame()
if( is.na(sensors$endDate[i]) ){ sensors$endDate[i] <- as.Date(Sys.Date()) }
DATE <- seq(sensors$startDate[i], sensors$endDate[i], by="days")
temp <- data.frame(DATE)
for(var in names(sns) ){
temp[var] <- sns[i,which(names(sns)==var)]
}
sensorDates <- rbind(sensorDates,temp)
}
sensorDates <- distinct(sensorDates)
# join bats and sensor site data -----
bats <- left_join(bats,sensorDates, by=c('siteID','DATE'))
# clean environment -----
rm(sns,temp,DATE,i,var)
#--SPECIES--###################################################################
# read basic names -----
batspecies <- read_excel("/Data/Bats/BatSpecies.xlsx")
# join bats and species names data -----
bats <- left_join(bats,batspecies, by="AUTO.ID")
# group common names -----
bats <- bats %>%
mutate( group_common = ifelse(ID_group=="MYOTIS", "Genus Myotis", Common),
group_common = ifelse(ID_group=="EPTFUS.LASNOC",
"Big Brown Bat/Silver-haired Bat",group_common),
group_common = ifelse(ID_group=="LASBOR.NYCHUM",
"Red Bat/Evening Bat", group_common) )
# group scientific names -----
bats <- bats %>%
mutate( group_species = ifelse(ID_group=="MYOTIS", "Myotis species", Scientific),
group_species = ifelse(ID_group=="EPTFUS.LASNOC",
"Eptesicus fuscus/Lasionycteris noctivagans", group_species),
group_species = ifelse(ID_group=="LASBOR.NYCHUM",
"Lasiurus borealis/Nycticeius humeralis", group_species) )
#--WEATHER--###################################################################
# hourly weather data -----
weather.hourly <- read_xlsx("/Data/Bats/SUD Weather Station.xlsx", sheet=2) %>%
rename( AvgTemp = `Air Temp Avg (C)`,
MaxWind = `wind speed (high) (m/s)`,
MinWind = `wind speed (low) (m/s)`,
rain = `Rain (mm)` ) %>%
mutate( DATE = date(Timestamp),
year=year(Timestamp),
monthN=month(Timestamp),
month=month.abb[monthN],
hour=hour(Timestamp),
AvgWind = (MaxWind+MinWind)/2 ) %>%
select( DATE, year, month, monthN, hour, AvgWind, AvgTemp)
# recorded rain data -----
rain.hourly <- read_xlsx("/Data/Bats/SUD Weather Station.xlsx", sheet = 3) %>%
rename( rain.intensity = `Rain Intensity (mm/sec)` ) %>%
mutate( DATE = date(Timestamp),
year=year(Timestamp),
monthN=month(Timestamp),
month=month.abb[monthN],
hour=hour(Timestamp) ) %>%
select(-Timestamp)
# weather join -----
weather <- left_join( weather.hourly, rain.hourly,
by=c('DATE','year','month','monthN','hour') )