Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathancallahan committed Jan 4, 2018
2 parents 52f11b2 + 01ac16d commit c0b70af
Show file tree
Hide file tree
Showing 7 changed files with 438 additions and 0 deletions.
127 changes: 127 additions & 0 deletions localExecutables/airsis_getYearlyData_exec.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
#!/usr/bin/env Rscript

# Updated Jan 3, 2018
#
# This script will download data for all airsis monitors for the specified time frame
#
# # Example
# airsis_getYearlyData_exec.R --startdate=20150101 --enddate=2016010223 --outputDir="~/Data/airsis" --fileName="airsis_2015"

VERSION <- "0.0.1"

suppressPackageStartupMessages(
{
library(methods)
library(optparse)
library(PWFSLSmoke)
library(MazamaSpatialUtils)
}
)

##########################################

saveAirsisData <- function(opt) {

# Get all unitids
usfs_unitids <- airsis_availableUnits(opt$startdate, opt$enddate, provider = 'USFS')
apcd_unitids <- airsis_availableUnits(opt$startdate, opt$enddate, provider = 'APCD')

# for usfs and apcd, make a list of ws_monitor objects for all units
monitors <- list()

for ( provider in c("USFS", "APCD", "ARB2", "EPA")) {

unitids <- airsis_availableUnits(opt$startdate, opt$enddate, provider = provider)

for ( unitid in unitids ) {
result <- try (monitor <- airsis_createMonitorObject(startdate = opt$startdate, enddate = opt$enddate, provider = provider, unitID = unitid))
if ("try-error" %in% class(result)) {
print(paste0("error loading ", provider, " ", unitid, ": ", geterrmessage()))
} else {
monitors[[paste0(unitid, "_", provider)]] <- monitor
}
}

}

all_monitors <- monitor_combine(monitors)

# Set the name of all_monitors to fileName
if (is.null(opt$fileName)) {
fileName <- paste0('airsis_', opt$startdate, "_", opt$enddate)
} else {
fileName <- opt$fileName
}

assign(fileName, all_monitors)
filePath <- paste0(opt$outputDir, '/', fileName, '.RData')

# Save the RData file
save(list = fileName, file = filePath)

}

################################################################################
# Main program

# ----- Parse command line options ---------------------------------------------

# Set up option parser
option_list <- list(
make_option(c("--startdate"),default=NULL, help="starting date of the data to be downloaded"),
make_option(c("--enddate"), default=NULL, help="ending date of the data to be downloaded"),
make_option(c("--outputDir"), default=getwd(), help="Output directory for generated RData files [default=\"%default\"]"),
make_option(c("--fileName"), default=NULL, help="name for the RData file"),
make_option(c("--logDir"), default=getwd(), help="Output directory for generated .log file [default=\"%default\"]"),
make_option(c("--spatialDataDir"), default='~/Data/Spatial', help="Directory containing spatial datasets used by MazamaSpatialUtils [default=\"%default\"]"),
make_option(c("-V","--version"), action="store_true", default=FALSE, help="Print out version number [default\"%default\"]")
)

# Parse arguments
opt <- parse_args(OptionParser(option_list=option_list))

# Print out version and quit
if ( opt$version ) {
cat(paste0('createCSV_exec.R ',VERSION,'\n'))
quit()
}

# Sanity checks
if ( is.null(opt$startdate) ) stop(paste0("startdate is required"))
if ( is.null(opt$enddate) ) stop(paste0("enddate is required"))
if ( !file.exists(opt$outputDir) ) stop(paste0("outputDir not found: ",opt$outputDir))
if ( !file.exists(opt$logDir) ) stop(paste0("logDir not found: ",opt$logDir))

# Assign log file names
debugLog <- file.path(opt$logDir, paste0('airsis_getYearlyData_', '_DEBUG.log'))
infoLog <- file.path(opt$logDir, paste0('airsis_getYearlyData_', '_INFO.log'))
errorLog <- file.path(opt$logDir, paste0('airsis_getYearlyData_', '_ERROR.log'))

# Set up logging
logger.setup(debugLog=debugLog, infoLog=infoLog, errorLog=errorLog)

# Silence other warning messages
options(warn=-1) # -1=ignore, 0=save/print, 1=print, 2=error

# Set up MazamaSpatialUtils
setSpatialDataDir(opt$spatialDataDir) ##FOR bash

loadSpatialData("NaturalEarthAdm1")


# ----- Save airnow ws_monitor object as a RData file ------

result <- try( saveAirsisData(opt) )

if ( "try-error" %in% class(result) ) {
msg <- paste("Error saving airsis data: ", geterrmessage())
logger.fatal(msg)
} else {
# Guarantee that the errorLog exists
if ( !file.exists(errorLog) ) dummy <- file.create(errorLog)
logger.info("Completed successfully!")
}




File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
81 changes: 81 additions & 0 deletions localNotebooks/airsis_yearlyData.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
---
title: "Yearly AIRSIS Data"
author: "Mazama Science"
date: "`r Sys.Date()`"
output: html_document
---

```{r, echo=FALSE}
knitr::opts_chunk$set(fig.width=7, fig.height=5)
```

# Setup

Start by loading the PWFSLSmoke package and loading the data.

```{r Setup, message=FALSE}
suppressPackageStartupMessages({
library(PWFSLSmoke)
library(ggplot2)
library(gridExtra)
}
)
logger.setLevel(ERROR)
load("~/Data/airsis/airsis_2015.RData")
load("~/Data/airsis/airsis_2016.Rdata")
```

# Orientation

```{r mapping}
monitorLeaflet(airsis_2015)
monitorLeaflet(airsis_2016)
```

```{r}
days2015 <- sum(!is.na(airsis_2015$data[-1]))/24
days2016 <- sum(!is.na(airsis_2016$data[-1]))/24
deployments2015 <- ncol(airsis_2015$data) - 1
deployments2016 <- ncol(airsis_2016$data) - 1
```

Let's see how they were distributed through time:

```{r}
counts2015 <- data.frame(time = airsis_2015$data$datetime, count = apply(airsis_2015$data[-1], 1, function(x) sum(!is.na(x))))
counts2016 <- data.frame(time = airsis_2016$data$datetime, count = apply(airsis_2016$data[-1], 1, function(x) sum(!is.na(x))))
plot1 <- ggplot(counts2015, aes(time, count))+
geom_bar(stat="identity")+
labs(title='2015', subtitle='number of deployments each hour')
plot2 <- ggplot(counts2016, aes(time, count))+
geom_bar(stat="identity")+
labs(title='2016', subtitle='number of deployments each hour')
grid.arrange(plot1, plot2, nrow = 2)
```

```{r monthly_maps}
basemap <- esriMap_getMap(bboxString = '-168.9,20.18,-58.68,70.51')
layout(matrix(c(3,3,1,2), 2, 2, byrow = TRUE), widths=c(1,1), heights=c(1,3))
for ( month in 1:12) {
# First 2015
tlim2015 <- if (month < 12) {
paste0('2015', stringr::str_pad(as.character(c(month, month+1)), 2, side = 'left', "0"), '01')
} else {
c('20151201', '20160101')
}
ws_monitor2015 <- monitor_subset(airsis_2015, tlim = tlim2015)
month_str <- strftime(ws_monitor2015$data$datetime[30], format = "%B")
tlim2016 <- if (month < 12) {
paste0('2016', stringr::str_pad(as.character(c(month, month+1)), 2, side = 'left', "0"), '01')
} else {
c('20161201', '20170101')
}
ws_monitor2016 <- monitor_subset(airsis_2016, tlim = tlim2016)
monitorEsriMap(ws_monitor2015, mapRaster = basemap, main = '2015')
monitorEsriMap(ws_monitor2016, mapRaster = basemap, main = '2016')
plot(0,0,axes = F, col = 'transparent')
text(0,0,month_str, cex = 3)
}
```


230 changes: 230 additions & 0 deletions localNotebooks/airsis_yearlyData.html

Large diffs are not rendered by default.

0 comments on commit c0b70af

Please sign in to comment.