Skip to content

R Code to download all measurements #42

@FrozoneAirPol

Description

@FrozoneAirPol

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$AirBeam-PM$measurements[,1])))
VarFrame <- data.frame(temp$streams$AirBeam-PM$measurements,session)
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$AirBeam-PM$measurements[,1])))
VarFrame <- data.frame(temp$streams$AirBeam-PM$measurements,session)
#as.vector(rep(x, length(json_Session$streams$AirBeam-PM$measurements[,1])))
if (i==1) {
MegaFrame<-VarFrame
} else {
MegaFrame<-rbind(MegaFrame,VarFrame)
}

}
`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions