Package noaadata :: Module waterlevel_dap
[hide private]
[frames] | no frames]

Source Code for Module noaadata.waterlevel_dap

  1  #!/usr/bin/env python 
  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 # FIX: remove this when pydap protects the seqReq 
 25   
 26  # FIX: document the datums 
 27  datumList = ['MLLW','MSL','MHW','STND','IGLD','NGVD','NAVD'] 
 28  unitList = ['Meters','Feet'] 
 29   
 30  import datetime 
 31   
 32  # Try to hang on to the dataset in case the user wants to do multiple requests 
 33  # FIX: how do I prevent epydoc from talking to the NOAA server when documentation is generated? 
 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',  # Is this in the right location on the map??? 
 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   
71 -def getWaterLevelNow(stationId,verbose=False, returnDict=True,datum='MSL'):
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 #print 'FIX: do this in seconds space!!!! This is crap!' 82 83 startD = d + datetime.timedelta(minutes=-20) 84 endD = d + datetime.timedelta(minutes=10) 85 #startMin = int(d.minute) - 6 # or 5? 86 #endMin = int(d.minute) + 1 87 #if verbose: print startD,endD,d 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 = '_STATION_ID="'+str(stationId)+'"&_BEGIN_DATE="'+beginDate+'"&_END_DATE="'+endDate+'"&_DATUM="'+datum+'"' 94 if verbose: 95 print 'plain text, then quoted' 96 print 'getWaterLevelNow reqStr:\n ',reqStr 97 reqStr = urllib.quote(reqStr) # FIX: remove this step when pydap updated 98 if verbose: 99 print 'getWaterLevelNow reqStr:\n ',reqStr 100 filt_seq=waterlevelSeq.filter(reqStr) 101 if verbose: print 'sending data request...' 102 data = filt_seq._get_data() 103 #if len(data) != 1: print 'WARNING: retrieved more than one point!' 104 if not returnDict: return data[-1][:] 105 106 data = data[-1][:] # get just the row and drop the surrounding "[]" 107 keys = filt_seq.keys() 108 #print len(keys),': ',keys 109 #print len(data),': ',data 110 #print 111 assert len(keys) == len(data) 112 r = {} # Results 113 for i in range(len(keys)): 114 r[keys[i]] = data[i] 115 return r
116 117 ###################################################################### 118 119 if __name__ == '__main__': 120 from optparse import OptionParser 121 parser = OptionParser(usage="%prog [options]",version="%prog "+__version__) 122 parser.add_option('-a','--all-stations',dest='allStations',default=False,action='store_true', 123 help='print values for all the stations in the Southern Chesapeake Bay region') 124 parser.add_option('-s','--station',dest='station',default='8639348', 125 help='Specify the station to print. (Default is Money Point) [default: %default]') 126 parser.add_option('--test','--doc-test',dest='doctest',default=False,action='store_true', 127 help='run the documentation tests') 128 parser.add_option('-v','--verbose',dest='verbose',default=False,action='store_true', 129 help='Make the test output verbose') 130 131 (options,args) = parser.parse_args() 132 133 success=True 134 135 if options.doctest: 136 import os; print os.path.basename(sys.argv[0]), 'doctests ...', 137 sys.argv= [sys.argv[0]] 138 if options.verbose: sys.argv.append('-v') 139 import doctest 140 numfail,numtests=doctest.testmod() 141 if numfail==0: print 'ok' 142 else: 143 print 'FAILED' 144 success=False 145 146 if options.allStations: 147 for station in stationsWaterLevel: 148 # FIX: probably better to pull all the stations together somehow 149 print station,':',getWaterLevelNow(station,options.verbose) 150 sys.stdout.flush() # Get the data out as soon as possible. This get is SLOW! 151 else: 152 print getWaterLevelNow(options.station,options.verbose) 153 154 if not success: 155 sys.exit('Something Failed') 156