<s> """<STR_LIT>""" <EOL> import os , errno <EOL> import re <EOL> import httplib <EOL> from pyspatialite import dbapi2 as spatialite <EOL> HOST = '<STR_LIT>' <EOL> URL_PROTO = '<STR_LIT>' <EOL> _SRS = int ( <NUM_LIT> ) <EOL> _BUFF_LEN = <NUM_LIT> * <NUM_LIT:10> <EOL> def findStationsWithinBoundingBox ( config , bbox ) : <EOL> """<STR_LIT>""" <EOL> stations = [ ] <EOL> pattern = re . compile ( "<STR_LIT>" ) <EOL> ghcnDB = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> conn = spatialite . connect ( ghcnDB ) <EOL> cursor = conn . cursor ( ) <EOL> sql = u"<STR_LIT>" % ( bbox [ '<STR_LIT>' ] , bbox [ '<STR_LIT>' ] , bbox [ '<STR_LIT>' ] , bbox [ '<STR_LIT>' ] ) <EOL> cursor . execute ( sql ) <EOL> results = cursor . fetchall ( ) <EOL> conn . close ( ) <EOL> for result in results : <EOL> match = pattern . match ( result [ <NUM_LIT:1> ] ) <EOL> assert ( match ) <EOL> lon = float ( match . group ( <NUM_LIT:1> ) ) <EOL> lat = float ( match . group ( <NUM_LIT:2> ) ) <EOL> stations . append ( [ result [ <NUM_LIT:0> ] , lat , lon , result [ <NUM_LIT:2> ] , result [ <NUM_LIT:3> ] ] ) <EOL> return stations <EOL> def findStationNearestToCoordinates ( config , longitude , latitude ) : <EOL> """<STR_LIT>""" <EOL> ghcnDB = config . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> conn = spatialite . connect ( ghcnDB ) <EOL> cursor = conn . cursor ( ) <EOL> sql = u"<STR_LIT>" % ( float ( longitude ) , float ( latitude ) , _SRS ) <EOL> cursor . execute ( sql ) <EOL> nearest = cursor . fetchone ( ) <EOL> conn . close ( ) <EOL> if nearest : <EOL> pattern = re . compile ( "<STR_LIT>" ) <EOL> match = pattern . match ( nearest [ <NUM_LIT:1> ] ) <EOL> assert ( match ) <EOL> lon = float ( match . group ( <NUM_LIT:1> ) ) <EOL> lat = float ( match . group ( <NUM_LIT:2> ) ) <EOL> return ( nearest [ <NUM_LIT:0> ] , lon , lat , nearest [ <NUM_LIT:2> ] , nearest [ <NUM_LIT:3> ] , nearest [ <NUM_LIT:4> ] ) <EOL> return None <EOL> def getClimateDataForStation ( config , outputDir , outFilename , stationID , overwrite = True ) : <EOL> """<STR_LIT>""" <EOL> dataFetched = False <EOL> if not os . path . isdir ( outputDir ) : <EOL> raise IOError ( errno . ENOTDIR , "<STR_LIT>" % ( outputDir , ) ) <EOL> if not os . access ( outputDir , os . W_OK ) : <EOL> raise IOError ( errno . EACCES , "<STR_LIT>" % ( outputDir , ) ) <EOL> outputDir = os . path . abspath ( outputDir ) <EOL> outFilepath = os . path . join ( outputDir , outFilename ) <EOL> if os . path . exists ( outFilepath ) : <EOL> if overwrite : <EOL> os . unlink ( outFilepath ) <EOL> else : <EOL> raise IOError ( errno . EEXIST , "<STR_LIT>" % outFilepath ) <EOL> url = URL_PROTO . format ( station_id = stationID ) <EOL> conn = httplib . HTTPConnection ( HOST ) <EOL> conn . request ( '<STR_LIT:GET>' , url ) <EOL> res = conn . getresponse ( buffering = True ) <EOL> assert ( <NUM_LIT:200> == res . status ) <EOL> data = res . read ( _BUFF_LEN ) <EOL> if data : <EOL> dataOut = open ( outFilepath , '<STR_LIT:wb>' ) <EOL> dataFetched = True <EOL> while data : <EOL> dataOut . write ( data ) <EOL> data = res . read ( _BUFF_LEN ) <EOL> dataOut . close ( ) <EOL> return dataFetched </s>
