使用R SOAP(SSOAP)从NOAA检索数据

时间:2014-07-02 10:06:19

标签: r soap

NOAA通过http://opendap.co-ops.nos.noaa.gov/axis/

提供了多种SOAP服务
library(SSOAP)
pred.wsdl<-"http://opendap.co-ops.nos.noaa.gov/axis/webservices/predictions/wsdl/Predictions.wsdl"
pred.params<-c("8454000","20140702","20140702","MLLW",0,0,60) # from NOAA's java example
# and the docs on http://opendap.co-ops.nos.noaa.gov/axis/webservices/predictions/samples/request.xml
tmp<-processWSDL(pred.wsdl)
ff<-genSOAPClientInterface(def=tmp,verbose=TRUE)
ff@functions$getPredictions(pred.params)

这就得到了:

>     pred.wsdl<-"http://opendap.co-ops.nos.noaa.gov/axis/webservices/predictions/wsdl/Predictions.wsdl"
>     pred.params<-c("8454000","20140702","20140702","MLLW",0,0,60) # from NOAA's java example
>     tmp<-processWSDL(pred.wsdl)
>     ff<-genSOAPClientInterface(def=tmp,verbose=TRUE)
<defClass> stationId.beginDate.endDate.datum.unit.timeZone.dataInterval 
finished stationId.beginDate.endDate.datum.unit.timeZone.dataInterval 
<defClass> Data 
finished Data 
<defClass> ArrayOfData 
finished ArrayOfData 
<defClass> data 
finished data 
<defClass> stationId.stationName.latitude.longitude.state.dataSource.COOPSDisclaimer.beginDate.endDate.datum.unit.timeZone.dataInterval.data 
finished stationId.stationName.latitude.longitude.state.dataSource.COOPSDisclaimer.beginDate.endDate.datum.unit.timeZone.dataInterval.data 
Operation getPredictions 
Operation getPredictionsAndMetadata 
>     ff@functions$getPredictions(pred.params)
Error in as(Parameters, "stationId.beginDate.endDate.datum.unit.timeZone.dataInterval") : 
  no method or default for coercing “character” to “stationId.beginDate.endDate.datum.unit.timeZone.dataInterval”
> 

我似乎无法在任何地方找到stationId.beginDate.endDate.datum.unit.timeZone.dataInterval类来使用它来执行新的(....)。

> getAnywhere(stationId.beginDate.endDate.datum.unit.timeZone.dataInterval)
no object named ‘stationId.beginDate.endDate.datum.unit.timeZone.dataInterval’ was found

eta:

我找到了类定义:

getClassDef('stationId.beginDate.endDate.datum.unit.timeZone.dataInterval')

然后我可以使用:

填充S对象
pred.Sparams=new('stationId.beginDate.endDate.datum.unit.timeZone.dataInterval',stationId=pred.params[1],beginDate=pred.params[2],endDate=pred.params[3],datum=pred.params[4],unit=as.integer(pred.params[5]),timeZone=as.integer(pred.params[6]),dataInterval=as.integer(pred.params[7]))

并将其命名为:

pred.dataAOD <- ff@functions$getPredictions(pred.Sparams)

并将其变成更像R的东西:

pred.data <- t(mapply(pred.dataAOD@data,FUN=function(x){c(time=x@timeStamp,pred=x@pred)},USE.NAMES=FALSE))

head(pred.data)

         time               pred   
[1,] "07/02/2014 00:00" "0.706"
[2,] "07/02/2014 01:00" "0.866"
[3,] "07/02/2014 02:00" "1.078"
[4,] "07/02/2014 03:00" "1.266"
[5,] "07/02/2014 04:00" "1.322"
[6,] "07/02/2014 05:00" "1.192"

有关如何做得更好的任何提示?

1 个答案:

答案 0 :(得分:0)

感谢MrFlick,这里是关于检索NOAA数据的部分问题的答案,而不是使用他们的SOAP服务:

# build a URL from the page at http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/
sosURL='http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/SOS?service=SOS&request=GetObservation&version=1.0.0&observedProperty=sea_surface_height_amplitude_due_to_equilibrium_ocean_tide&offering=urn:ioos:station:NOAA.NOS.CO-OPS:8454000&responseFormat=text%2Fcsv&eventTime=2014-07-02T00:00:00Z/2014-07-02T23:59:00Z&result=VerticalDatum%3D%3Durn:ogc:def:datum:epsg::5103&dataType=HourlyTidePredictions&unit=Meters'
x=read.csv(url(sosURL))

可以将它包装在一个函数中,替换站点,开始,结束,数据和单位。

getNOAATidalPredictions<-function(
    station=8454000,start=Sys.time(), end=start+3600*24){
# Use the http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/ SOS services
# to read tidal prediction data.
sosURL='http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/SOS?service=SOS&request=GetObservation&version=1.0.0&observedProperty=sea_surface_height_amplitude_due_to_equilibrium_ocean_tide&offering=urn:ioos:station:NOAA.NOS.CO-OPS:STATIONTAG&responseFormat=text%2Fcsv&eventTime=STARTTAG/ENDTAG&result=VerticalDatum%3D%3Durn:ogc:def:datum:epsg::5103&dataType=HourlyTidePredictions&unit=Meters'
sosURL=gsub('STATIONTAG',station,sosURL)
sosURL=gsub('STARTTAG',format(start,"%Y-%m-%dT%H:%M:%SZ",tz='UTC'),sosURL)
sosURL=gsub('ENDTAG',format(end,"%Y-%m-%dT%H:%M:%SZ",tz='UTC'),sosURL)
read.csv(url(sosURL))
dt$date_time<-strptime(dt$date_time,format='%Y-%m-%dT%H:%M:%S',tz='GMT')
dt
}



> getNOAATidalPredictions()[,c(5:6)]
      date_time sea_surface_height_amplitude_due_to_equilibrium_ocean_tide..m.
1  2015-03-21T09:00:00Z                                                         -0.749
2  2015-03-21T10:00:00Z                                                         -0.474
3  2015-03-21T11:00:00Z                                                          0.005
4  2015-03-21T12:00:00Z                                                          0.575
5  2015-03-21T13:00:00Z                                                          0.981
6  2015-03-21T14:00:00Z                                                          1.017
7  2015-03-21T15:00:00Z                                                          0.660
8  2015-03-21T16:00:00Z                                                          0.074
9  2015-03-21T17:00:00Z                                                         -0.535
10 2015-03-21T18:00:00Z                                                         -0.976
11 2015-03-21T19:00:00Z                                                         -1.093
12 2015-03-21T20:00:00Z                                                         -0.939
13 2015-03-21T21:00:00Z                                                         -0.717
14 2015-03-21T22:00:00Z                                                         -0.464
15 2015-03-21T23:00:00Z                                                         -0.056
16 2015-03-22T00:00:00Z                                                          0.490
17 2015-03-22T01:00:00Z                                                          0.965
18 2015-03-22T02:00:00Z                                                          1.138
19 2015-03-22T03:00:00Z                                                          0.911
20 2015-03-22T04:00:00Z                                                          0.387
21 2015-03-22T05:00:00Z                                                         -0.234
22 2015-03-22T06:00:00Z                                                         -0.772
23 2015-03-22T07:00:00Z                                                         -1.050
24 2015-03-22T08:00:00Z                                                         -1.021
25 2015-03-22T09:00:00Z                                                         -0.860

这是一个额外的R函数,使用SOS来获取观察结果:

NOAA_water_levels_sos<-function(station,start,end){
# data grabber based on a URL built from http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/
    ts<-format(start,"%Y-%m-%dT%H:%M:%SZ")
    te<-format(end,"%Y-%m-%dT%H:%M:%SZ")
    uri<-'http://opendap.co-ops.nos.noaa.gov/ioos-dif-sos/SOS?service=SOS&request=GetObservation&version=1.0.0&observedProperty=water_surface_height_above_reference_datum&offering=urn:ioos:station:NOAA.NOS.CO-OPS:8454000&responseFormat=text%2Fcsv&eventTime=2012-10-01T00:00:00Z/2012-11-01T23:59:00Z&result=VerticalDatum%3D%3Durn:ogc:def:datum:epsg::5103&dataType=VerifiedHourlyHeight'

    uri<-gsub('offering=[^&]*&',sprintf("offering=urn:ioos:station:NOAA.NOS.CO-OPS:%s&",station),uri)
    uri<-gsub('eventTime[^&]*&',sprintf("eventTime=%s/%s&",ts,te),uri)
    #print(uri)
    x<-read.csv(url(uri))
    x$time=as.POSIXct(strptime(x$date_time,format="%Y-%m-%dT%H:%M:%SZ",tz="UTC"))
    x

}

NOAAs SOS服务似乎涵盖了其SOAP服务的功能,并且更易于打包和解析。