-
Notifications
You must be signed in to change notification settings - Fork 31
Description
Hi,
I wrote some code to use the aircasting data for a research project where I worked with R. The purpose of the code is to have the PM2.5 measurement data for a given time and the location of the measurement.
`#Build an R database with all measures off one place
#set up
library(jsonlite)
library(foreach)
library(doParallel)
data_flat<- fromJSON("http://aircasting.org/api/sessions.json?page=0&page_size=10000&q[measurements]=true|q[year_from]=2011|q[year_to]=2019&q[location]=Tananarive&q[distance]=50&q[sensor_name]=AirBeam-PM&q[unit_symbol]=?g/m?", flatten = TRUE)
#Choose your own Location and time parameters parameters
#description here: https://github.com/HabitatMap/AirCasting/blob/master/doc/api.md
#parallel method-------
#setup parallel backend to use many processors
cores=detectCores()
cl <- makeCluster(cores[1]-1) #not to overload your computer
registerDoParallel(cl)
finalFrame <- foreach(i=1:length(data_flat$id), .combine=rbind, .packages='jsonlite') %dopar% { #to make sure if it's working correctly use first a small number instead of length(data_flat$id)
x <- data_flat$id[i]
url <- paste("http://aircasting.org/api/sessions/",x,".json",sep="")
temp <- fromJSON(url)
session <- as.vector(rep(x, length(temp$streams
VarFrame <- data.frame(temp$streams
VarFrame
#Equivalent to finalMatrix = cbind(finalFrame, tempFrame)
}
save(finalFrame, file = "finalFrame.Rda")
Loop method-------
does the same, slower but more chance to have less errors
for(i in 1:length(data_flat$id)){ #to make sure if it's working correctly use first a small number instead of length(data_flat$id)
x <- data_flat$id[i]
url <- paste("http://aircasting.org/api/sessions/",x,".json",sep="")
temp <- fromJSON(url)
session <- as.vector(rep(x, length(temp$streams
VarFrame <- data.frame(temp$streams
#as.vector(rep(x, length(json_Session$streams
if (i==1) {
MegaFrame<-VarFrame
} else {
MegaFrame<-rbind(MegaFrame,VarFrame)
}
}
`