-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathebird_pre_processing.R
More file actions
86 lines (79 loc) · 3.63 KB
/
ebird_pre_processing.R
File metadata and controls
86 lines (79 loc) · 3.63 KB
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
library(scales)
############################################################################
# Setup environment and variables
############################################################################
# Variable designiation
# Workspace directory
workspace = "D:/ebird/datadownload"
# Input ArcGIS Model csv file
birdlist = list( "agwtea1", "ambduc", "amewig", "bargol", "blksco2", "buwtea", "buffle", "canvas", "cintea", "comeid", "comgol", "commer", "kineid", "gadwal", "gresca", "harduc", "hoomer", "lessca", "lotduc", "mallar", "norpin", "norsho", "rebmer", "redhea", "rinduc", "rudduc", "sursco", "whwsco", "wooduc")
for (sp in 1:length(birdlist)) {
# if (any(c("buffle", "comeid", "kineid", "lotduc") == birdlist[[sp]])){
# inbird = paste("ebd_",birdlist[[sp]], "_relMay-2017.txt", sep="")
# } else {
# inbird = paste("ebd_",birdlist[[sp]], "_relNov-2016.txt", sep="")
# }
inbird = paste("ebd_",birdlist[[sp]], "_relAug-2017.txt", sep="")
bcr = "BCR.csv"
############################################################################
# Read in ebird data (16 species)
ebird <- read.delim(paste(workspace,inbird,sep="/"), sep="\t", header=TRUE, quote = "", stringsAsFactors = FALSE, na.strings=c(""))
bcr = read.csv(paste(workspace, bcr, sep="/"), header=TRUE)
# Join arcData to coverData based on cover_type
ebird = merge(ebird, bcr, by.x = "BCR.CODE", by.y = "BCR")
setwd(workspace)
# Drop extra columns and data
#ebird = subset(ebird, ebird$COUNTRY_CODE == "US")
ebird = subset(ebird, ebird$APPROVED == "1")
ebird$PROJECT.CODE = NULL
ebird$PROTOCOL.TYPE = NULL
ebird$SAMPLING.EVENT.IDENTIFIER = NULL
ebird$FIRST.NAME = NULL
ebird$LAST.NAME = NULL
ebird$OBSERVER.ID = NULL
ebird$BREEDING.BIRD.ATLAS.CODE = NULL
ebird$GLOBAL.UNIQUE.IDENTIFIER = NULL
ebird$SUBSPECIES.COMMON.NAME = NULL
ebird$SUBSPECIES.SCIENTIFIC.NAME = NULL
ebird$AGE.SEX = NULL
ebird$COUNTRY = NULL
ebird$IBA.CODE = NULL
ebird$SUBNATIONAL1_CODE = NULL
ebird$SUBNATIONAL2_CODE = NULL
ebird$ATLAS.BLOCK = NULL
ebird$LOCALITY = NULL
ebird$LOCALITY.ID = NULL
ebird$LOCALITY.TYPE = NULL
ebird$REVIEWED = NULL
ebird$REASON = NULL
ebird$TRIP.COMMENTS = NULL
ebird$EFFORT.AREA.HA = NULL
ebird$EFFORT.DISTANCE.KM = NULL
ebird$SPECIES.COMMENTS = NULL
ebird$X = NULL
ebird$APPROVED = NULL
ebird$TIME.OBSERVATIONS.STARTED = NULL
ebird$DURATION.MINUTES = NULL
ebird$NUMBER.OBSERVERS = NULL
ebird$ALL.SPECIES.REPORTED = NULL
ebird$GROUP.IDENTIFIER = NULL
ebird$TAXONOMIC.ORDER = NULL
ebird$CATEGORY = NULL
# Set date field and divide up by year, month, week for plotting
ebird$Date = as.Date(ebird$OBSERVATION.DATE, "%Y-%m-%d")
ebird$Year = strtoi(format(ebird$Date, "%Y"))
to.month = function(x) as.integer(format(x, "%m"))
to.day <- function(x) as.integer(format(x, "%d"))
ebird$Month = to.month(ebird$Date)
ebird$Day = to.day(ebird$Date)
ebird$MonthDay = paste(ebird$Month, ebird$Day, sep="/")
# Remove years < 2005 and months 6,7,8
ebird = subset(ebird, ebird$Year > 2000 & ebird$Year <= 2016)
ebird = subset(ebird, ebird$Month >= 9 | ebird$Month <= 4)
ebird$Winter = ifelse(ebird$Month <= 4, paste(ebird$Year - 1,ebird$Year, sep = "/"),paste(ebird$Year, ebird$Year + 1, sep = "/"))
ebird$Week = as.numeric(format(ebird$Date, "%U"))
ebird = subset(ebird, ebird$Winter != "2000/2001" & ebird$Winter != "2016/2017")
ebird$OBSERVATION.COUNT = ifelse(ebird$OBSERVATION.COUNT == "X", 1, ebird$OBSERVATION.COUNT)
ebird$OBSERVATION.COUNT = as.numeric(ebird$OBSERVATION.COUNT)
write.csv(ebird, file=paste(workspace, "/post/", paste(inbird, "post.csv", sep="_"), sep=""), quote=FALSE, row.names=FALSE)
}