Feb 11, 2009

Import water data from National Weather Service

Besides real-time water data from USGS in the previous post, we like to import more data from National Weather Service, it has the forecast data on water stage.

The data is distributed in XML format.

NWSxml=Import[http://www.crh.noaa.gov/ahps2/xml/nori3_hgirg.xml];

(* function to extract data from xml *)

extractdata[xml_,group_]:=Module[{dataxml,groupdata},dataxml=Cases[NWSxml,XMLElement[group,_,_],Infinity];
groupdata=Transpose[Cases[dataxml,XMLElement["datum",{},{___,XMLElement[#,_,{x_}],___}]:>x,Infinity]&/@{"valid","primary","secondary"}];
groupdata /. {x_,y_,z_}:> {StringReplace[StringDrop[x,-9],{"T"->" "}],ToExpression[y],ToExpression[z]*1000}];

(* extract data*)

observeddata = extractdata[NWSxml, "observed"];
forecastdata = extractdata[NWSxml, "forecast"];

(* produce the graph *)

importNWSxml

Flood stage is at 11.0 feet, it seems the white river may be in the flood stage in next several days.

1 comment:

Dem_z said...

Tried running. Get an error --

PS LOVE YOUR BLOG. Post more, will be an avid reader!!

NWSxml = Import[http : // www.crh.noaa.gov/ahps2/xml/nori3_hgirg.xml];

Syntax::sntxf: "http:" cannot be followed by "//www.crh.noaa.gov/ahps2/xml/nori3_hgirg.xml".

Syntax::tsntxi: "http:" is incomplete; more input is needed.

Syntax::sntxi: Incomplete expression; more input is needed.