1
2 __version__ = '$Revision: 4762 $'.split()[1]
3 __date__ = '$Date: 2006-09-19 14:56:22 -0400 (Tue, 19 Sep 2006) $'.split()[1]
4 __author__ = 'Kurt Schwehr'
5
6 __doc__='''
7 Retrieve 6 minute raw water level data from NOAA CO-OPS server.
8
9 @see: U{NOAA DODS/OPeNDAP page<http://opendap.co-ops.nos.noaa.gov/dods/>}
10 @requires: U{pydap/dap-py<http://pydap.org/>}
11 @requires: U{epydoc<http://epydoc.sourceforge.net/>}
12
13 @author: U{'''+__author__+'''<http://schwehr.org/>}
14 @license: GPL v2
15 @copyright: (C) 2006 Kurt Schwehr
16 @version: ''' + __version__ +'''
17
18 @var __date__: Date of last svn commit
19 @undocumented: __version__ __author__ __doc__ parser success
20
21 '''
22
23 import sys, httplib, dap.client
24 import urllib
25
26
27 datumList = ['MLLW','MSL','MHW','STND','IGLD','NGVD','NAVD']
28 unitList = ['Meters','Feet']
29
30 import datetime
31
32
33
34 datasetURL='http://opendap.co-ops.nos.noaa.gov/dods/IOOS/Raw_Water_Level'
35 "OPeNDAP URL for NOAA CO-OPS database"
36 waterlevelDataset=dap.client.open(datasetURL)
37 "This set only contains raw 6 minute water level data"
38 waterlevelSeq = waterlevelDataset['WATERLEVEL_RAW_PX']
39 "This is a sequence containter for waterlevels"
40
41 stationsAll = {
42 '8639348':'Money Point',
43 '8638595':'South Craney Island',
44 '8638610':'Sewells Point',
45 'cb0402':'NSN LB 7',
46 'cb0601':'Newport News Channel LB 14' ,
47 '8638511':'Dom. Term. Assoc. Pier 11',
48 '8638614':'Willoughby Degaussing Station',
49 'cb0301':'Thimble Shoal Channel LB 18',
50 '8638863':'CBBT',
51 'cb0102':'Cape Henry LB 2CH',
52 '8638999':'Cape Henry',
53 'cb0201':'York Spit Channel LBB 22',
54 '8632200':'Kiptopeke Beach',
55 '8637611':'York River East Rear Range Light',
56 '8637689':'Yorktown USCG Training Center'
57 }
58
59 stationsWaterLevel = {
60 '8638610':'Sewells Point',
61 '8639348':'Money Point',
62 '8638863':'CBBT',
63 '8632200':'Kiptopeke Beach',
64 '8637689':'Yorktown USCG Training Center'
65 }
66
67
68 '''Convenience table. Should really get the stations from the web,
69 soap, or dap. These stations are in the Southern Chesapeake Bay.'''
70
72 '''
73 Fetch the dictionary for the current water level
74
75 @see: U{Southern Chesapeak Bay Stations<http://tidesandcurrents.noaa.gov/cbports/cbports_south.shtml?port=cs>}
76
77 '''
78
79 d = datetime.datetime.utcnow()
80
81
82
83 startD = d + datetime.timedelta(minutes=-20)
84 endD = d + datetime.timedelta(minutes=10)
85
86
87
88
89 beginDate = str(startD.year)+('%02d' % startD.month)+('%02d' % startD.day)+' '+ ('%02d' % (startD.hour))+':'+('%02d' % (startD.minute))
90 endDate = str(endD.year)+('%02d' % endD.month)+('%02d' % endD.day)+' '+ ('%02d' % (endD.hour))+':'+('%02d' % (endD.minute))
91
92 reqStr = '_STATION_ID="'+str(stationId)+'"&_BEGIN_DATE="'+beginDate+'"&_END_DATE="'+endDate+'"&_DATUM="MLLW"'
93 reqStr = urllib.quote(reqStr)
94 if verbose: print 'getWaterLevelNow reqStr:\n ',reqStr
95 filt_seq=waterlevelSeq.filter(reqStr)
96 if verbose: print 'sending data request...'
97 data = filt_seq._get_data()
98
99 if not returnDict: return data[-1][:]
100
101 data = data[-1][:]
102 keys = filt_seq.keys()
103
104
105
106 assert len(keys) == len(data)
107 r = {}
108 for i in range(len(keys)):
109 r[keys[i]] = data[i]
110 return r
111
112
113
114 if __name__ == '__main__':
115 from optparse import OptionParser
116 parser = OptionParser(usage="%prog [options]",version="%prog "+__version__)
117 parser.add_option('-a','--all-stations',dest='allStations',default=False,action='store_true',
118 help='print values for all the stations in the Southern Chesapeake Bay region')
119 parser.add_option('-s','--station',dest='station',default='8639348',
120 help='Specify the station to print. (Default is Money Point) [default: %default]')
121 parser.add_option('--test','--doc-test',dest='doctest',default=False,action='store_true',
122 help='run the documentation tests')
123 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true',
124 help='Make the test output verbose')
125
126 (options,args) = parser.parse_args()
127
128 success=True
129
130 if options.doctest:
131 import os; print os.path.basename(sys.argv[0]), 'doctests ...',
132 sys.argv= [sys.argv[0]]
133 if options.verbose: sys.argv.append('-v')
134 import doctest
135 numfail,numtests=doctest.testmod()
136 if numfail==0: print 'ok'
137 else:
138 print 'FAILED'
139 success=False
140
141 if options.allStations:
142 for station in stationsWaterLevel:
143
144 print station,':',getWaterLevelNow(station,options.verbose)
145 sys.stdout.flush()
146 else:
147 print getWaterLevelNow(options.station,options.verbose)
148
149 if not success:
150 sys.exit('Something Failed')
151