## =========================================================================================================== ## --- ## title: "Read Argos location data from a ZIP file downloaded from the USGS ASC Wildlife Tracking Data Collection " ## author: "David Douglas" ## date: "6/30/2020" ## --- ## =========================================================================================================== ## # R script for reading Argos processed location data. ## It is assumed that you have downloaded a tracking data-package from the USGS Alaska Science Center. ## The tracking data-packages are available for download at: https://doi.org/10.5066/P9VYSWEH ## At the web page, select a species of interest, and then from the species web page, download the ## "Processed Data" zip file. Take note of the directory where you save the ZIP file. It's also ## advised to download and read the README.pdf file, and the metadata. This script reads the ## data-package's ZIP file directly, so if you unzip it, don't delete the zip file. This script was ## developed using R version 3.6.0. ## =========================================================================================================== require(utils) require(trip) require(move) require(tidyverse) require(maps) require(ggplot2) require(sqldf) ## =========================================================================================================== ## **TWO CHANGES ARE REQUIRED BELOW** ## The following file and path names are provided for demonstration only. Replace them to exactly match the ## processedData zip file name, and the directory pathname where the zip file that you downloaded from USGS ## is located. zipFileName <- "yellowBilledLoon_USGS_ASC_argos_processedData.zip" zipFilePath <- "d:/USGS_trackingDataPackages/" ## =========================================================================================================== ## Show a list of all files in the processedData ZIP data-package.... studyName <- str_remove(zipFileName, "_processedData.zip") data_zip <- paste0(zipFilePath, zipFileName) print(paste("Try: ", data_zip, sep=" ")) if (!(file.exists(paste0(zipFilePath, zipFileName)))) + {stop("ZIP file does not exist, \nPlease correct zipFilePath or zipFileName.")} tmpdir <- tempdir(check=TRUE) setwd(paste(tmpdir)) unzip(data_zip, list=TRUE) ## Unzip two files of interest - the filtered Argos location data, and the deployment attributes.... unzip(data_zip, paste0("processedData/", studyName, "_diag_filteredLocations.csv")) unzip(data_zip, paste0("processedData/", studyName, "_deploymentAttributes.csv")) ## Read the two CSV files.... raw_df <- read.csv(paste0("processedData/", studyName, "_diag_filteredLocations.csv")) atr_df <- read.csv(paste0("processedData/", studyName, "_deploymentAttributes.csv")) ## Define date-time variables.... raw_df$Location_Timestamp_UTC<- as.POSIXct(strptime(as.character(raw_df$Location_Timestamp_UTC), "%Y/%m/%d %H:%M:%S", tz="GMT")) atr_df$Deployment_Start_Timestamp_UTC<- as.POSIXct(strptime(as.character(atr_df$Deployment_Start_Timestamp_UTC), "%Y/%m/%d %H:%M:%S", tz="GMT")) atr_df$Deployment_End_Timestamp_UTC<- as.POSIXct(strptime(as.character(atr_df$Deployment_End_Timestamp_UTC), "%Y/%m/%d %H:%M:%S", tz="GMT")) ## Exclude locations previously deemed implausible (i.e. filtered locations), ## then retain only locations from animals believed to be alive.... valid_df<-raw_df[raw_df$Location_DAF_Filter!=1,] valid_df<-valid_df[valid_df$Tracking_Status=='alive',] ## For Argos data, assign NA to any instances where Argos coded a zero (0) into variables ## describing the location error ellipse. Zero is nonsensical for these variables.... valid_df$Location_ErrorSemimajor[valid_df$Location_ErrorSemimajor == 0] <- NA valid_df$Location_ErrorSemiminor[valid_df$Location_ErrorSemiminor == 0] <- NA valid_df$Location_ErrorRadius[valid_df$Location_ErrorRadius == 0] <- NA valid_df$Location_ErrorGDOP[valid_df$Location_ErrorGDOP == 0] <- NA ## Sort chronologically by animal, then adjust any duplicated date-time stamps by 1-second. ## Argos data sometimes have location estimates with exactly the same timestamp, created when 2 satellites ## pass over the transmitter at the same time. Duplicate timestamps will cause many subsequent functions to ## fail, so applying a 1-second offset prevents such failures.... valid_df<-valid_df[order(valid_df$Animal_ID, valid_df$Location_Timestamp_UTC),] valid_df$Location_Timestamp_UTC <- trip::adjust.duplicateTimes(valid_df$Location_Timestamp_UTC, valid_df$Animal_ID) ## Drop variables with all missing data.... valid_df <- valid_df[,colSums(is.na(valid_df))= d2.Deployment_Start_Timestamp_UTC AND d1.Location_Timestamp_UTC <= d2.Deployment_End_Timestamp_UTC AND d1.Animal_ID == d2.Animal_ID AND d1.PTT_ID == d2.PTT_ID ') ## Many of the USGS ASC wildlife tracking data sets contain animal tracks that span the ## International Dateline (180 E/W longitude). The code below re-centers locations on the ## dateline so they can be mapped contiguously. ## Make a copy of the master data frame for short-term, temporary modifications... tmp_df <- pooled_df ## First shift all longitudes by 180 degrees which puts them in East Longitude notation, ## then buffer the map extent by 5 degrees.... ref <- 180 tmp_df$long.recenter <- ifelse(tmp_df$Longitude < ref - 180, tmp_df$Longitude + 360, tmp_df$Longitude) border <- 5 minLat <- min(tmp_df$Latitude) - border maxLat <- max(tmp_df$Latitude) + border minLon <- min(tmp_df$long.recenter) - border maxLon <- max(tmp_df$long.recenter) + border ## Similarly shift a world map into East Longitude coordinates.... worldmap <- map_data("world", wrap = c(0, 360)) ## Map the locations.... ggplot(aes(x = long, y = lat), data = worldmap) + geom_path(aes(group = group), colour = "grey65") + scale_y_continuous(limits = c(minLat, maxLat)) + scale_x_continuous(limits = c(minLon, maxLon)) + coord_equal() + theme_bw() + geom_point(data = tmp_df, aes(x = long.recenter, y = Latitude, col = 'red'), pch = 19, size = 2, alpha = .1, show.legend = FALSE) + xlab("East Longitude") + ylab("Latitude") + ggtitle(studyName, subtitle = "USGS ASC wildlife tracking data collection") ## =========================================================================================================== ## Create a move object from the data frame. See R Package 'move'.... pooled_moveStack <- move(x=pooled_df$Longitude, y=pooled_df$Latitude, time=pooled_df$Location_Timestamp_UTC, proj=CRS("+proj=longlat +ellps=WGS84 +datum=WGS84"), data=pooled_df, animal=pooled_df$Animal_ID, sensor="Argos") ## See 'Using the move package' vignette at: https://cran.r-project.org/web/packages/move/ ## ===========================================================================================================