[R] Download daily weather data

Chris Stubben stubben at lanl.gov
Fri Feb 27 20:13:53 CET 2009



tlevine wrote:
> 
> The NOAA has very promising tabular forecasts
> (http://forecast.weather.gov/MapClick.php?CityName=Ithaca&state=NY&site=BGM&textField1=42.4422&textField2=-76.5002&e=0&FcstType=digital),
> but I can't figure out how to import them.
> 

Sometimes you can just use gsub to get html into R.   Try this.  

## read html 
wf<-readLines("http://forecast.weather.gov/MapClick.php?CityName=Ithaca&state=NY&site=BGM&textField1=42.4422&textField2=-76.5002&e=0&FcstType=digital",
warn=FALSE) 

## you do need to find the rows with data (rows 18 and 19), grep may help
here
## mark end of lines
x<-gsub("</tr>", "\n", wf[18:19])
## remove white space...
x<-gsub(" ", "_", x)
## remove degree symbol or add comment.char="" to read.table below
x<-gsub("°", "", x)
## remove html tags
x<-gsub("<[^>]*>", " ", x)

y<-read.table(con<-textConnection(x), fill=TRUE)
close(con)

#now just reshape this mess 
# join rows 1,16 and 17-32 (skip 1st two rows with day and hour)

z<-data.frame(rbind( t(y[3:16,-1]), t(y[19:32,-1]) ), row.names=NULL,
stringsAsFactors=FALSE)
names(z)<-substr(y[3:16,1], 1,4)

head(z)
  Temp Dewp Wind Wind.1 Wind.2 Gust Sky_ Pcpn Rel. Thun Rain Snow Free Slee
1   49   43   43     18      S        99   40   80   --  Chc   --   --   --
2   50   43   44     18    SSW        99   40   77   --  Chc   --   --   --
3   50   43   44     17     SW        99   40   77   --  Chc   --   --   --
4   48   43   42     16     SW        99   40   83   --  Chc   --   --   --
5   45   43   38     15    WSW       100   40   93   --  Chc   --   --   --
6   42   42   35     14      W       100   35  100   --  Chc   --   --   --

# plot
plot(z["Temp"])


Chris Stubben
-- 
View this message in context: http://www.nabble.com/Download-daily-weather-data-tp22233373p22252517.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list