Open ghost opened 8 years ago
@wshatpud: Your issue helped me realize that I should be getting my data from MesoWest. Here's something I hacked up to parse their CSV output. Maybe you already have something better, but I figured it may help someone if I share this. Thanks.
read_mesonet_csv=function(loc) {
if(length(grep("^http:",loc))) {
str=system2("wget",c(shQuote(loc),"-O","-"),stdout=T)
} else {
str=readLines(loc);
}
# remove comments
str=str[substr(str,1,1) != "#"]
# remove "units" row
units=unlist(strsplit(str[2],","))
str=str[-2]
# read header row
header=unlist(strsplit(str[1],","))
nc=length(header)
## the last field can contain a comma, we need to replace it with
## e.g. a semicolon. example (was "Light Rain,Fog"):
# KOAK,2016-12-08T05:20:00Z,30.11,46.4,87.19,11.99,70.0,,,2493.0,,,0.04,,166.0,294.0,,,4.0,,METAR KOAK 080520Z AUTO 07012KT 4SM -RA BR FEW016 OVC029 08/06 A3011 RMK P0004,,,,2900.0,,,,,42.75,,ENE,1019.54,1019.64,,Light Rain;Fog
strcs = strsplit(str,",")
str=sapply(strcs, function(row) {
paste(
collapse=",",
c(row[1:(nc-1)],paste(row[nc:length(row)],collapse=";"))
)
})
loc=textConnection(str);
tab=read.csv(loc,comment.char="#",stringsAsFactors=F,fill=T)
# create a new "Date" column with POSIXct's
# make sure new "Date" column is first
tab=cbind(data.frame(
Date=as.POSIXct(tz="UTC",
gsub("T"," ",
gsub(
"Z","",
tab$Date_Time
)
)
)
),tab)
# delete the old "Date_Time" column
tab$Date_Time=NULL
attr(tab,"units")=units
tab
}
tab=read_mesonet_csv("http://api.mesowest.net/v2/stations/timeseries?token=demotoken&stid=KOAK&start=201612080000&end=201612090000&output=csv&units=english&obtimezone=UTC")
plot(tab$Date,tab$wind_speed_set_1,type="l")
print(cbind(names(tab),attr(tab,"units")))
Thanks. That works!
In addition to MesoWest, there are obviously other sites. See, for example, the report generator at https://www.wcc.nrcs.usda.gov/index.html (nice, because it also has precipitation increment precalculated).
Add MesoWest as a data source for weatherData. It does have a published API.
AFAIK, no other R package connects to MesoWest, so this would make a contribution.