# Motus Getting Started # This is a short script for getting started with Motus data. # For complete & detailed information, see https://beta.motus.org/MotusRBook/ # This script explains how to download a Motus database and # convert a subset to a data frame. # Once data is stored in a data frame, familiar dplyr functions # can be used for data wrangling. # Set system time zone to GMT (or UTC) Sys.setenv(TZ='GMT') # Load the necessary libraries. library(motus) #motusLogout() # run to logout library(tidyverse) # includes ggplot2, dplyr library(lubridate) # for manipulation of dates and times library(RSQLite) # need for some sql manipulation (.packages()) # display loaded packages getwd() # display your current working directory setwd("C:/Users/AFAR/Documents/R") # change wd if desired # Define project number proj.num <- 20 # when downloading data for the first time... # You will be prompted for your motus.org username and password. # This creates an offline .motus file and a working object # named sql.motus. sql.motus <- tagme(projRecv = proj.num, new = TRUE, update = TRUE) # Alternatively, to open and update a detections database that already exists # Just change your project number. sql.motus <- dbConnect(SQLite(), "project-20.motus") # Check for new records and download # sql.motus <- tagme(projRecv = proj.num, new = FALSE, update = TRUE) dbListTables(sql.motus) # list tables in sql.motus # create table for all tags (i.e., the alltags view). tbl.alltags <- tbl(sql.motus, "alltags") tbl_vars(tbl.alltags) # list tables in tbl.alltags # fyi...create table and list all species in sql.motus # Note the species in your sql.motus database. tbl.species <- tbl(sql.motus, "species") tbl_vars(tbl.species) tbl.species %>% select(scientific) %>% as.data.frame() # fyi...create table and list all years in sql.motus # Note the years in your sql.motus database. tbl.tagDeps <- tbl(sql.motus, "tagDeps") tbl_vars(tbl.tagDeps) tbl.tagDeps %>% as.data.frame() %>% mutate(year = year(as_datetime(tsStart, tz = "UTC", origin = "1970-01-01"))) %>% select(year) %>% distinct() # fyi...create table and list all tags in sql.motus # Note the mfgID's in your sql.motus database. tbl.id <- tbl(sql.motus, "tags") tbl_vars(tbl.id) tbl.id %>% select(mfgID) %>% as.data.frame() # Convert tbl to data frame for easier processing. # Add filters if desired, to limit what is extracted from tbl.alltags. # For example, I was interested in the data for yellow-rumped warblers. # I also show code to filter by a single year. # If you filter by mfgID, be careful if you have duplicate tag deployments (see # Motus R book). # This step is slow. Be patient. # To produce test with 1943562 rows, my computer took 15 min. test <- tbl.alltags %>% filter(speciesSci == "Setophaga coronata") %>% distinct() %>% collect() %>% as.data.frame() # This is a method to filter by year (e.g., 2017). year0 <- as.numeric(ymd_hms("2017-01-01 00:00:00")) year1 <- as.numeric(ymd_hms("2018-01-01 00:00:00")) test1 <- tbl.alltags %>% filter(ts > year0 & ts < year1) %>% distinct() %>% collect() %>% as.data.frame() # at this point, you may want to save your data as an *.rds file # saveRDS(test, "yrwa.rds") # load data from a saved *.rds file # If you've done this, start here: # test <- readRDS("yrwa.rds") str(test) # check your data # if you are overloading system memory, some options are: # help(memory.size) # memory.limit(size=NA) # gc() # Also... delete any objects in the environment that are no longer needed. # create data and time variables # convert ts to your local time zone (e.g., EDT is UTC-4h) # often it is useful to create a new data frame rather than overwrite one # ts is in seconds, so 4 h = 4*60*60 s. # ts can be rounded for faster data exploration. # lubridate has many more date and time options. test2 <- test %>% mutate(ts = as_datetime(ts), ts = ts - 60*60*4, ts_5min = round_date(ts,unit="5 mins"), year = year(ts), # year yday = yday(ts), # day of year hour = hour(ts)) # hour of day # check the times and new date & time variables to ensure # they make sense! head(test2) # Now you are ready for data filtering & exploration. # For preliminary exploration, condense your data frame by time bin. # This is useful for mapping purposes. # In my example, test2 has 1943710 obs, test3 has 84394 obs. test3 <- test2 %>% select(c(mfgID,year, yday, ts_5min,recvDeployName,recvDeployLat,recvDeployLon)) %>% distinct() %>% as.data.frame() # An example of how to get and save receiver deployments within a specified # time period and geographical range. tbl.recvDeps <- tbl(sql.motus, "recvDeps") df.recvDeps <- tbl.recvDeps %>% collect() %>% as.data.frame() %>% mutate(tsStart = as_datetime(tsStart, tz = "UTC", origin = "1970-01-01"), tsEnd = as_datetime(tsEnd, tz = "UTC", origin = "1970-01-01")) df.recvDeps2 <- df.recvDeps %>% filter(latitude > 40 & latitude < 48 & longitude > -90 & longitude < -75) %>% filter(tsStart < "2017-05-03") %>% filter(is.na(tsEnd)==T | tsEnd > "2017-06-15") df.recvDeps2 %>% select(name,tsStart,tsEnd) %>% View() # if desired, save deployments to *.csv write.csv(df.recvDeps2,file="recv_deployments.csv")