getStations: A function to get a list of Personal Weather Station names

Description Usage Arguments Details Value Note Author(s) References See Also Examples

Description

This function looks on Wunderground to see if there are any PWS at a given lat/lon.

Usage

1
getStations(lldf, radius = 10, numofStatLimit = 150)

Arguments

lldf

A data frame of locations specified in lat/lon

radius

A distance / radius specified in miles

numofStatLimit

A cap on the number of stations

Details

This function is not meant to be called directly by users

Value

A list of PWS stations

Note

None

Author(s)

Steve Pittard <wsp@emory.edu>

References

STATS290 Website

See Also

None

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
## The function is currently defined as
function (lldf, radius = 10, numofStatLimit = 150) 
{
    library(XML)
    library(RCurl)
    library(svMisc)
    dflength = length(lldf)
    if (dflength == 0) {
        stop("Hey. You passed an empty data frame. Unable to generate a list of stations !")
    }
    rlist = list()
    wunderString = "http://api.wunderground.com/auto/wui/geo/GeoLookupXML/index.xml?query="
    nrowdf = nrow(lldf)
    pStr = paste("Now checking Wunderground to find stations")
    print(pStr)
    for (ii in 1:nrowdf) {
        progress(ii, nrowdf - 1)
        lat = lldf[ii, c("geo_latitude")]
        lon = lldf[ii, c("geo_longitude")]
        coords = paste(lat, lon, sep = ",")
        urlstr = paste(wunderString, coords, sep = "")
        if (ii == (nrowdf - 1)) 
            cat("Done!\n")
        if (radius < 10) {
            halfradius = 3
        }
        else {
            halfradius = radius/6
        }
        qstring = paste("//location/nearby_weather_stations/pws/station[distance_mi<", 
            halfradius, "]/ancestor-or-self::station/id", sep = "")
        txt2parse = getURL(urlstr)
        hold = xmlTreeParse(txt2parse, useInternalNodes = TRUE)
        stationsXml = getNodeSet(hold, qstring)
        stations = sapply(stationsXml, xmlValue)
        rlist[[ii]] = stations
    }
    finalStationList = unique(unlist(rlist))
    if (length(finalStationList) == 0) {
        stop("No stations found")
    }
    else {
        if (length(finalStationList) > numofStatLimit) {
            length(finalStationList) = numofStatLimit
        }
        pStr = paste("Found", length(finalStationList), "Personal Weather Stations within", 
            radius, "miles", sep = " ")
        print(pStr)
        return(finalStationList)
    }
  }

steviep42/PWS documentation built on May 30, 2019, 5:39 p.m.