From be0e6afa0479f7df8c8a28915bf679ddbc7df31a Mon Sep 17 00:00:00 2001 From: Ram-N Date: Wed, 7 Jun 2017 15:31:38 -0700 Subject: [PATCH] Version 0.5.0. On CRAN --- .Rbuildignore | 1 + DESCRIPTION | 3 +- NAMESPACE | 1 + NEWS.md | 3 +- R/data_description.R | 5 +- R/util_functions.R | 7 +++ R/validity_checks.R | 4 ++ R/wrapper_functions.R | 90 +++++++++++++++++++++++++++++-- README.md | 6 +-- man/IntlWxStations.Rd | 1 + man/USAirportWeatherStations.Rd | 1 + man/getWeatherForMultipleYears.Rd | 59 ++++++++++++++++++++ man/getWeatherForYear.Rd | 2 +- man/weatherData-package.Rd | 4 +- 14 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 man/getWeatherForMultipleYears.Rd diff --git a/.Rbuildignore b/.Rbuildignore index b9c851b..c8d57ed 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,3 +1,4 @@ ^.*\.Rproj$ ^\.Rproj\.user$ .travis.yml +^cran-comments\.md$ diff --git a/DESCRIPTION b/DESCRIPTION index a100cbc..9ee0154 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -6,12 +6,13 @@ Description: Functions that help in fetching weather data from weather data (temperature, pressure etc.) for any weather related analysis. URL: http://ram-n.github.io/weatherData/ BugReports: https://github.com/ram-n/weatherData/issues -Version: 0.5 +Version: 0.5.0 Date: 2017-05-16 Authors@R: person("Ram", "Narasimhan", email = "ramnarasimhan@gmail.com", role = c("aut", "cre")) Suggests: testthat +Depends: R (>= 2.10) Imports: curl, plyr, diff --git a/NAMESPACE b/NAMESPACE index 0589559..a49d249 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -8,6 +8,7 @@ export(getDetailedWeather) export(getStationCode) export(getSummarizedWeather) export(getWeatherForDate) +export(getWeatherForMultipleYears) export(getWeatherForYear) export(showAvailableColumns) import(curl) diff --git a/NEWS.md b/NEWS.md index f840aa5..6572b2b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -4,5 +4,6 @@ * Fixed station_type = "ID" bug. Many features were not working. -* `curl` option is now ready for CRAN. (This has been on github for months.) +* `curl` option takes care of 'https' (This has been on github for months.) +* Added a warning for multiple year fetches. (WeatherUnderground doesn't allow very large CSV files.) \ No newline at end of file diff --git a/R/data_description.R b/R/data_description.R index 76b8b7c..db38449 100644 --- a/R/data_description.R +++ b/R/data_description.R @@ -105,7 +105,7 @@ NULL #' \url{http://www.wunderground.com/about/faq/US_cities.asp} #' @keywords data #' -NULL +"USAirportWeatherStations" #' @title Data - International Weather Stations #' @description This is a data frame of the 1602 stations in Weather Underground's @@ -121,5 +121,4 @@ NULL #' maintained by Greg Thompson of NCAR. #' @keywords data #' -NULL - +"IntlWxStations" diff --git a/R/util_functions.R b/R/util_functions.R index e6caea4..c387aad 100644 --- a/R/util_functions.R +++ b/R/util_functions.R @@ -59,6 +59,11 @@ keepOnlyMinMax <- function(single_day_df, } + +data("IntlWxStations" , envir=environment()) +data("USAirportWeatherStations", envir=environment()) + + #' Shows all the available Weather Data Columns #' #' Displays all the columns that are available in the website, for the given @@ -137,8 +142,10 @@ showAvailableColumns<- function(station_id, #'@export getStationCode <- function(stationName, region=NULL){ + stn2 <- NULL; us_stns <- NULL intl_stn2 <- NULL; i_stns <- NULL + if(!is.null(region)){ region_matches <- grep(pattern=region, USAirportWeatherStations$State, diff --git a/R/validity_checks.R b/R/validity_checks.R index 51cc147..bdf0280 100644 --- a/R/validity_checks.R +++ b/R/validity_checks.R @@ -43,6 +43,10 @@ validYear <- function(year){ if(year <= 0){ return(0) } + if(year <= 1970){ + warning("\nWARNING: The year", year, "may not have any data.") + } + if(year > current_year){ warning("\nThe year cannot be greater than current year.") return(0) diff --git a/R/wrapper_functions.R b/R/wrapper_functions.R index 49a4c6b..1d88e08 100644 --- a/R/wrapper_functions.R +++ b/R/wrapper_functions.R @@ -352,14 +352,11 @@ getWeatherForDate <- function(station_id, - -#checkSummarizedDataAvailability("KBUF", "2012-12-12", end_date=NULL) - #' Get weather data for one full year #' #' @description Function will return a data frame with all the records #' for a given station_id and year. If the current year is supplied, -#' it will returns records until the current Sys.Date() ("today") +#' it will return records until the current Sys.Date() ("today") #' #' @details Note that this function is a light wrapper for getWeatherForDate #' with the two end dates being Jan-01 and Dec-31 of the given year. @@ -599,3 +596,88 @@ getTemperatureForDate <- function(station_id, opt_write_to_file) } + + + +#' @title For Multiple Years, fetch the weather data for a station +#' +#' @description Function will return a data frame with all the records +#' for a given station_id for all the years requested. If the current year is supplied, +#' it will return records until the current Sys.Date() ("today"). +#' This function will return a (fairly large) data frame. If you are going +#' to be using this data for future analysis, you can store the results in a CSV file +#' by setting \code{opt_write_to_file} to be TRUE +#' +#' @details Note that this function is a light wrapper for getWeatherForYear +#' +#' @param station_id is a valid Weather Station ID +#' (example: "BUF", "ORD", "VABB" for Mumbai). +#' Valid Weather Station "id" values: "KFLMIAMI75" or "IMOSCOWO2" You can look these up +#' at wunderground.com. You can get station_id's for a given location +#' by calling \code{getStationCode()} +#' @param start_year is a valid year in the past (numeric, YYYY format) +#' @param end_year is a valid year in the past (numeric, YYYY format) +#' @param station_type = "airportCode" (3 or 4 letter airport code) or "ID" (Wx call Sign) +#' @param opt_detailed Boolen flag to indicate if detailed records for the station are desired. +#' (default FALSE). By default only one records per date is returned. +#' @param opt_write_to_file If TRUE, the resulting dataframe will be stored in a CSV file. +#' Default is FALSE +#' @references For a list of valid Weather Stations, try this format +#' \url{http://www.wunderground.com/weatherstation/ListStations.asp?selectedCountry=United+States} +#' and replace with your country of interest +#' @return A data frame with each row containing: \itemize{ +#' \item Date and Time stamp (for each date specified) +#' \item Temperature and/or other weather columns sought +#' } +#'@examples +#'\dontrun{ +#' dat <- getWeatherForMultipleYears("SFO", 2013, 2017) +#' +#' # If opt_detailed is turned on, you will get a large data frame +#' wx_SF <- getWeatherForMultipleYears("SIN", 2014, 2017, opt_detailed=TRUE) +#'} +#' @export +getWeatherForMultipleYears <- function(station_id, + start_year, + end_year, + station_type="airportCode", + opt_detailed=FALSE, + opt_write_to_file=FALSE){ + + + if(!validYear(start_year)){ #check if year is valid + warning("Start Year argument is invalid. Please provide a valid 4-digit + year (numeric)") + return(NULL) + } + + if(!validYear(end_year)){ #check if year is valid + warning("End Year argument is invalid. Please provide a valid 4-digit + year (numeric)") + return(NULL) + } + + if(start_year>end_year){ #illegal time duration + warning("Start Year cannot be after End Year. Please make sure that \ +the end year is after start year.") + return(NULL) + } + + num_years <- end_year - start_year + + multi_year <- vector("list", num_years) + for(year in start_year:end_year){ + multi_year[[year]] <- getWeatherForYear(station_id, + year, + station_type=station_type, + opt_detailed=opt_detailed, + opt_write_to_file = opt_write_to_file) + + } + + #flatted the List into a data frame + multi_year_df <- do.call(rbind, multi_year) + return(multi_year_df) + +} + diff --git a/README.md b/README.md index 972f770..2830473 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,6 @@ If you want to perform weather Analysis, but don't wish to be bothered with scra The main page for weatherData (with explanations and Examples) can be found at [http://ram-n.github.io/weatherData/](http://ram-n.github.io/weatherData/) -### Shiny App - -WeatherCompare is [a Shiny App](http://spark.rstudio.com/ram/WeatherCompare/) that uses the data brought over by weatherData and then summarized in various ways # Install @@ -28,8 +25,7 @@ Load the library library(weatherData) ``` -Windows users must also first install -[Rtools](http://cran.rstudio.com/bin/windows/Rtools/). +Windows users may also need to first install *Rtools* from CRAN. (Look in bin/windows) ## Suggestions diff --git a/man/IntlWxStations.Rd b/man/IntlWxStations.Rd index 97cd3dc..d69cc3b 100644 --- a/man/IntlWxStations.Rd +++ b/man/IntlWxStations.Rd @@ -4,6 +4,7 @@ \name{IntlWxStations} \alias{IntlWxStations} \title{Data - International Weather Stations} +\format{An object of class \code{data.frame} with 9715 rows and 1 columns.} \usage{ data(IntlWxStations) } diff --git a/man/USAirportWeatherStations.Rd b/man/USAirportWeatherStations.Rd index 45007ef..3210060 100644 --- a/man/USAirportWeatherStations.Rd +++ b/man/USAirportWeatherStations.Rd @@ -4,6 +4,7 @@ \name{USAirportWeatherStations} \alias{USAirportWeatherStations} \title{Data - US Weather Stations ID's} +\format{An object of class \code{data.frame} with 1602 rows and 7 columns.} \usage{ data(USAirportWeatherStations) } diff --git a/man/getWeatherForMultipleYears.Rd b/man/getWeatherForMultipleYears.Rd new file mode 100644 index 0000000..d07e59d --- /dev/null +++ b/man/getWeatherForMultipleYears.Rd @@ -0,0 +1,59 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/wrapper_functions.R +\name{getWeatherForMultipleYears} +\alias{getWeatherForMultipleYears} +\title{For Multiple Years, fetch the weather data for a station} +\usage{ +getWeatherForMultipleYears(station_id, start_year, end_year, + station_type = "airportCode", opt_detailed = FALSE, + opt_write_to_file = FALSE) +} +\arguments{ +\item{station_id}{is a valid Weather Station ID +(example: "BUF", "ORD", "VABB" for Mumbai). +Valid Weather Station "id" values: "KFLMIAMI75" or "IMOSCOWO2" You can look these up + at wunderground.com. You can get station_id's for a given location + by calling \code{getStationCode()}} + +\item{start_year}{is a valid year in the past (numeric, YYYY format)} + +\item{end_year}{is a valid year in the past (numeric, YYYY format)} + +\item{station_type}{= "airportCode" (3 or 4 letter airport code) or "ID" (Wx call Sign)} + +\item{opt_detailed}{Boolen flag to indicate if detailed records for the station are desired. +(default FALSE). By default only one records per date is returned.} + +\item{opt_write_to_file}{If TRUE, the resulting dataframe will be stored in a CSV file. +Default is FALSE} +} +\value{ +A data frame with each row containing: \itemize{ +\item Date and Time stamp (for each date specified) +\item Temperature and/or other weather columns sought +} +} +\description{ +Function will return a data frame with all the records + for a given station_id for all the years requested. If the current year is supplied, + it will return records until the current Sys.Date() ("today"). + This function will return a (fairly large) data frame. If you are going + to be using this data for future analysis, you can store the results in a CSV file + by setting \code{opt_write_to_file} to be TRUE +} +\details{ +Note that this function is a light wrapper for getWeatherForYear +} +\examples{ +\dontrun{ +dat <- getWeatherForMultipleYears("SFO", 2013, 2017) + +# If opt_detailed is turned on, you will get a large data frame +wx_SF <- getWeatherForMultipleYears("SIN", 2014, 2017, opt_detailed=TRUE) +} +} +\references{ +For a list of valid Weather Stations, try this format + \url{http://www.wunderground.com/weatherstation/ListStations.asp?selectedCountry=United+States} + and replace with your country of interest +} diff --git a/man/getWeatherForYear.Rd b/man/getWeatherForYear.Rd index 2affb0d..aea0e47 100644 --- a/man/getWeatherForYear.Rd +++ b/man/getWeatherForYear.Rd @@ -33,7 +33,7 @@ A data frame with each row containing: \itemize{ \description{ Function will return a data frame with all the records for a given station_id and year. If the current year is supplied, - it will returns records until the current Sys.Date() ("today") + it will return records until the current Sys.Date() ("today") This function will return a (fairly large) data frame. If you are going to be using this data for future analysis, you can store the results in a CSV file diff --git a/man/weatherData-package.Rd b/man/weatherData-package.Rd index fea5519..ab236fe 100644 --- a/man/weatherData-package.Rd +++ b/man/weatherData-package.Rd @@ -12,8 +12,8 @@ The package has functions that can fetch weather data. \tabular{ll}{ Package: \tab weatherData\cr Type: \tab Package\cr -Version: \tab 0.4\cr -Date: \tab 2014-04-29\cr +Version: \tab 0.5.0\cr +Date: \tab 2017-06-05\cr License: \tab GPL\cr } These functions don't use APIs. They rely on reading URL's instead.