Skip to content

Instantly share code, notes, and snippets.

@alexrutherford
Created February 22, 2016 16:32
Show Gist options
  • Save alexrutherford/903bf3a4f604e49c2b56 to your computer and use it in GitHub Desktop.
Save alexrutherford/903bf3a4f604e49c2b56 to your computer and use it in GitHub Desktop.
Script to grab global weather data from Open Weather Map
import requests
import json,re,csv,re,sys
from secrets import *
import datetime,time
import logging
def makeFileName(n,log=False):
now=datetime.datetime.now()
timeStem='%d_%d_%d_%d_%d' % (now.year,now.month,now.day,now.hour,now.minute)
if log:
return '/home/arutherford/clouddata/brazil/weather_data/'+timeStem+'.log'
else:
return '/home/arutherford/clouddata/brazil/data/weather_data/%s_%d.json' % (timeStem,n)
logging.basicConfig(level=logging.INFO,filename='/home/arutherford/clouddata/brazil/log.log',format='%(asctime)s %(message)s')
bbox=map(str,[-99.98971, -33.74708, -28.846945, 22.264878])
bboxs=[[-180,-90,0,0],[0,0,180,90],[-180,0,0,90],[0,-90,180,0]]
bboxs=[map(str,b) for b in bboxs]
def getUrl(b):
base='http://api.openweathermap.org/data/2.5/box/city'
urlNoKey=base+'?bbox=%s,10&appid=' % (','.join(b))
url=urlNoKey+KEY2
return urlNoKey,url
for nBox,b in enumerate(bboxs):
# Lop through each quadrant
urlNoKey,url=getUrl(b)
logging.info('Querying %s' % urlNoKey)
res=requests.get(url)
logging.info('Got code %d' % res.status_code)
nAttempts=0
while not res.status_code==200:
logging.info('Sleeping then retrying')
time.sleep(10)
res=requests.get(url)
logging.info('Got code %d' % res.status_code)
logging.info('With reason %s' % res.reason)
nAttempts+=1
if nAttempts==10:
logging.info('Giving up after %d attempts' % nAttempts)
sys.exit(1)
r=res.json()
logging.info('Got %d stations' % r['cnt'])
# for n,el in enumerate(r['list']):
# temp=pd.to_datetime(el['dt'],unit='s')
# r['list'][n]['time']=temp.isoformat()
# Time already exists in iso format
fileName=makeFileName(nBox)
logging.info('Saving to file %s' % fileName)
with open(fileName,'w') as outFile:
json.dump(r,outFile)
logging.info('Complete' )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment