Guest User

Untitled

a guest
Jan 23rd, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. # This function downloads the studies you provide in a list:
  2.  
  3. # library('skimr')
  4. library('tidyverse')
  5. library(ggmap)
  6. library(move)
  7. library(ggplot2)
  8. loginStored <- movebankLogin(username = "Diego_Ellis", password = "")
  9. login = loginStored
  10. getMovebank("tag_type", loginStored)
  11. # Show me all studies
  12. # all_studies <- getMovebank(entity_type = "study", login=loginStored)
  13. # Look at MPIO studies:
  14. MPIO_studies = searchMovebankStudies(x="MPIO", login=loginStored)
  15. # For eobs function need to find out who the PIs are
  16. # Martin -> Can I ask Movebnak folks to run a query -> All studies with eobs tags of MPIO
  17. # Ask for access for these to Martin:
  18. eobs_studies = searchMovebankStudies(x="eobs", login=loginStored)
  19. e_obs_studies = searchMovebankStudies(x="e-obs", login=loginStored)
  20. e_Obs_studies = searchMovebankStudies(x="e-Obs", login=loginStored)
  21. extra_studies <- c('High-altitude flights of Himalayan vultures (data from Sherub et al. 2016)',
  22. 'Corvus splendens Translocation BHUTAN', 'Livestock Movement in Bhutan', 'Steppe Eagle Bhutan',
  23. 'Takin Sangay Bhutan', 'Tragopans in Bhutan')
  24. studies_to_check <- c(MPIO_studies, eobs_studies, e_Obs_studies, extra_studies)
  25. # Is there a way to see what studies have e-obs temperature as a column? Or to see privacy
  26. # studies_to_check <- studies_to_check[1:3]
  27.  
  28. ind_studies_with_eobs_temperature <- function(login, studylist, plot_this_data = NULL){
  29. # Check which of the MPIO studies has e-obs data:
  30. df_out <- data.frame(matrix(NA, length(studylist), 9))
  31. names(df_out) <- c('study_name', 'has_eobs_temperature',
  32. 'private_data', 'number_of_fixes', 'has_acc', "movebank_id",
  33. "license_terms", "main_location_long", "main_location_lat")
  34. all_studies <- getMovebank(entity_type = "study", login=loginStored)
  35.  
  36. # as.data.frame(t(sapply(studylist, function(x){
  37.  
  38. for(x in 1:length(unique(studylist))){
  39.  
  40. # x <- studylist[1]
  41. # x <- studylist[10]
  42. x <- studylist[x]
  43. studyID <- x
  44. # study_index_id <- which(studyID %in% studylist)
  45. study_index_id <- which(studylist %in% studyID)
  46. print(study_index_id)
  47. df_out[study_index_id,]$study_name <- studylist[study_index_id]
  48. ## get GPS and accelerometer data within a time period
  49. ## create a vector with attributes for all sensors included in the study
  50. attrib <- unique(c(as.character(getMovebankSensorsAttributes(studyID,
  51. login=loginStored)$short_name),
  52. 'sensor_type_id', 'deployment_id', 'event_id',
  53. 'individual_id', 'tag_id'))
  54. # indID <- getMovebank("individual", login=login, study_id=studyID)$id[50]
  55. # ## get a summary of information about the two studies
  56. # getMovebank("study", login=login, study_id=c(studyID,studyID2))
  57. # Check e-obs temp
  58. if(length(grep("eobs_temperature", attrib)) > 0){
  59. df_out[study_index_id,]$has_eobs_temperature <- 1
  60. print(paste0('study ', studyID, ' has e-obs temperature'))
  61. }else{
  62. df_out[study_index_id,]$has_eobs_temperature <- 0
  63. print(paste0('study ', studyID, ' has no e-obs temperature'))
  64. }
  65. # Check if has ACC
  66. if( any(getMovebankSensorsAttributes(study=studyID,login=loginStored)$sensor_type_id == "2365683") ){
  67. print(paste0('study ', studyID, ' has accelerometers'))
  68. df_out[study_index_id,]$has_acc <- 1
  69. }else{
  70. print(paste0('study ', studyID, ' has no accelerometers'))
  71. df_out[study_index_id,]$has_acc <- 0
  72. }
  73. # Data privacy:
  74. if( length(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]) > 0 ){
  75. df_out[study_index_id,]$private_data <- as.character(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]$there_are_data_which_i_cannot_see)
  76. }else{df_out[study_index_id,]$private_data <- NA}
  77. ## return(df_out)
  78. # Get the Movebank ID
  79. if( length(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]) > 0 ){
  80. df_out[study_index_id,]$movebank_id <- as.numeric(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]$id)
  81. }else{df_out[study_index_id,]$movebank_id <- NA}
  82. # main_location_long
  83. if( length(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]) > 0 ){
  84. df_out[study_index_id,]$main_location_long <- as.numeric(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]$main_location_long)
  85. }else{df_out[study_index_id,]$main_location_long <- NA}
  86. # main_location_lat
  87. if( length(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]) > 0 ){
  88. df_out[study_index_id,]$main_location_lat <- as.numeric(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]$main_location_lat)
  89. }else{df_out[study_index_id,]$main_location_lat <- NA}
  90. # license_terms
  91. if( length(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]) > 0 ){
  92. df_out[study_index_id,]$license_terms <- as.character(all_studies[all_studies$name %in% df_out[study_index_id,]$study_name,]$license_terms)
  93. }else{df_out[study_index_id,]$license_terms <- NA}
  94.  
  95. # return(df_out)
  96. # Check permission status
  97. } # End of for loop
  98. rownames(df_out) <- NULL
  99. if(!is.null(plot_this_data)){
  100. # Subset all studies by the ones I found that have e-obs temperature:
  101. all_studies_I_want <- all_studies[all_studies$name %in% df_out[df_out$has_eobs_temperature == 1,]$study_name,]
  102. world <- map_data("world")
  103. # Blue means there are private data and restricitons.
  104. print( ggplot() +
  105. geom_polygon(data = world,
  106. fill = "grey38",
  107. aes(x = long, y = lat, group = group)) +
  108. geom_point(data = all_studies_I_want,
  109. aes(x = main_location_long, y = main_location_lat, color = as.factor(there_are_data_which_i_cannot_see))) +
  110. scale_color_discrete(guide = guide_legend(title = "Data Restrictions"))
  111. )
  112. }
  113.  
  114. print(paste0('A total of ', sum(df_out[df_out$has_eobs_temperature == 1,]$has_eobs_temperature, na.rm = TRUE), ' out of ', nrow(df_out), ' studies has e-obs temperature data'))
  115.  
  116. return(df_out)
  117.  
  118. # Sarah: Ask them to do a query:
  119. # ))) # End of sapply
  120. } # End of function
  121. # ind_studies_with_eobs_temperature(login = login, studylist = MPIO_studies)
  122. study_info <- ind_studies_with_eobs_temperature(login = login, studylist = studies_to_check, plot_this_data = TRUE)
  123.  
  124.  
  125. # Function:
  126. # Things to add: Need permision!!!
  127. ## get deployments reference information from the study
  128. #getMovebank(entity_type='deployment' , login= login,studyID = studyID )
  129. ## get a summary of information about the two studies
  130. #getMovebank(entity_type="study", login=login, study_id=studyID)
  131. ## get animal reference information from the study
  132. # head(getMovebank("individual", login=login, study_id=studyID))
  133.  
  134. all_studies <- getMovebank(entity_type = "study", login=loginStored)
  135. # Subset all studies by the ones I found that have e-obs temperature:
  136. all_studies_I_want <- all_studies[all_studies$name %in% study_info[study_info$has_eobs_temperature == 1,]$study_name,]
  137. write.csv(all_studies_I_want, file = '/Users/diegoellis/Dropbox/Wikelski_Temperature/all_studies_I_want.csv')
  138.  
  139. # Use recursive? -> microhabitats?
  140.  
  141. # Get temperature, extract SRTM, get elevation above ellipsoid, get wind direction
  142.  
  143. #
  144. # Extract from this guy: 'High-altitude flights of Himalayan vultures (data from Sherub et al. 2016)'
  145. # Elephants Java FZG MPIO DAMN
  146. # e-Obs GPRS Himalayan Griffon, Sherub, Bhutan
Add Comment
Please, Sign In to add comment