Created
July 26, 2011 14:27
-
-
Save axeda/1106885 to your computer and use it in GitHub Desktop.
Get Precipitation by a given latitude and Longitude
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.httpclient.methods.* | |
import org.apache.commons.httpclient.* | |
import java.text.SimpleDateFormat | |
def location = parameters.location.toString() | |
def locparts = location.split(',') | |
def lat = locparts[0] | |
def lon = locparts[1] | |
def hostname = " www.weather.gov" | |
def url = "/forecasts/xml/sample_products/browser_interface/ndfdXMLclient.php" | |
String ndfdElement = "pop12" // see http://www.weather.gov/forecasts/xml/docs/elementInputNames.php | |
def perceptTimeFormat = new SimpleDateFormat ("yyyy-MM-dd'T'HH:mm:ss"); | |
def cal = Calendar.getInstance(); | |
Date startDate = cal.getTime() | |
cal.add(Calendar.HOUR,12) | |
Date endDate = cal.getTime() | |
def client = new HttpClient () | |
HostConfiguration host = client.getHostConfiguration() | |
host.setHost(hostname, 80, "http") | |
GetMethod get = new GetMethod (url) | |
NameValuePair [] params = new NameValuePair [6] | |
params[0] = new NameValuePair ("lat", lat); | |
params[1] = new NameValuePair ("lon", lon); | |
params[2] = new NameValuePair ("product", 'time-series'); | |
params[3] = new NameValuePair ("begin", perceptTimeFormat.format(startDate)); | |
params[4] = new NameValuePair ("end", perceptTimeFormat.format(endDate)); | |
params[5] = new NameValuePair (ndfdElement, ndfdElement); | |
get.setQueryString(params) | |
client.executeMethod(host, get); | |
message = "Status:" + get.getStatusText() | |
content = get.getResponseBodyAsString() | |
get.releaseConnection() | |
// parse result XML and compute average | |
def dwml = new XmlSlurper ().parseText(content) | |
readings = dwml.data.parameters."probability-of-precipitation".value.collect { Integer.parseInt(it.toString()) } | |
average = readings.sum() / readings.size() | |
//logger.info "Expected precipitation for $location is $readings" | |
return readings[0] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment