Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python
- import urllib2
- r = 0
- y = 1
- incase = [ 'SKC','CLR','FEW','SCT','BKN','OVC']
- def extract(icao):
- try:
- url = 'http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' %icao
- f = urllib2.urlopen(url)
- content = f.read()
- print content
- c = content.split()
- c.pop(0)
- c.pop(0)
- return c
- except urllib2.HTTPError:
- exit('sorry we are out of service. Please try again later.')
- def data_decode(data):
- incase = range(0,10)
- weather_library = {'MI', 'BC', 'DR', 'BL', 'SH', 'TS', 'FZ', 'DZ', 'RA', 'SN', 'SG', 'IC', 'PL','GR', 'GS', 'UP', 'BR', 'FG', 'FU', 'VA','DU', 'SA', 'HZ', 'PY', 'PO', 'SQ', 'FC', 'SS', 'DS'}
- sky = {'SKC','CLR','FEW','SCT', 'BKN', 'OVC' }
- print data
- assign =''
- for i in range(0,len(data)):
- if i <= 1:
- print globals()['Decoded_%d' % (int(i))](data[i])
- for i in range(2,len(data)):
- assign =data[i]
- if assign=='AUTO':
- print 'The report has been prepared by an automated observing system. '
- continue
- if assign[2:4]=='SM' or assign == '9999':
- print globals()['Decoded_%d' % (int(3))](data[i])
- continue
- if len(assign)==7:
- print globals()['Decoded_%d' % (int(2))](data[i])
- continue
- if assign[0:3] in sky:
- print globals()['Decoded_%d' % (int(5))](data[i])
- continue
- if assign[0:2] in weather_library:
- print globals()['Decoded_%d' % (int(4))](data[i])
- continue
- if assign[0] == 'A':
- print globals()['Decoded_%d' % (int(8))](data[i])
- break
- if assign[0] == 'Q' and r == 0:
- print globals()['Decoded_%d' % (int(7))](data[i])
- continue
- if int(assign[0]) in incase:
- print globals()['Decoded_%d' % (int(6))](data[i])
- continue
- '''This is for finding the location of the station from the source:http://weather.rap.ucar.edu/surface/stations.txt'''
- def Decoded_0(data_parts):
- url='http://weather.rap.ucar.edu/surface/stations.txt'
- f = urllib2.urlopen(url)
- content = f.read()
- c = content
- str(data_parts)
- start = c.find('CD STATION ')
- c = c[start:len(c)]
- #print c
- if c.find(data_parts):
- end = c.find(data_parts)+63
- start = c.find(data_parts)-17
- found= c[start:end]
- print 'City::',found[0:17]
- else:
- print 'not found it'
- '''This part is for decoding the date and time of the report taken. '''
- def Decoded_1(data_parts):
- if (len(data_parts) == 7):
- print 'Date of report taken :', data_parts[0:2],'th of this month'
- print 'Time of report taken :', data_parts[2:4],':',data_parts[4:6]
- '''This function decodes the wind condition from the given data codes'''
- def Decoded_2(data_parts):
- if (data_parts == '00000KT'):
- print 'It will be calm, no winds.'
- if data_parts[0:3]=='VRB':
- print 'Direction of wind :', 'The direction of the wind is not determined.'
- else:
- print 'Direction of wind :', data_parts[0:3],'Degrees'
- if data_parts.find('G'):
- print 'Gust of air : ', data_parts[data_parts.find('G'):data_parts.find('KT')]
- else:
- print 'Gust of air : None'
- print 'Speed of air : ', data_parts[3:5],'knot.'
- '''This function is to decode the data code for visibility '''
- def Decoded_3(data_parts):
- if data_parts == '9999':
- print 'Visibility is more clear than 30 miles.'
- else:
- end = data_parts.find('SM')
- print 'Visibility is',data_parts[0:end],'statute miles'
- '''This function is for the weather condition report from the data code.'''
- def Decoded_4(data_parts):
- intensity = ['-', '+']
- weather_library = {'MI' : 'Shallow',
- 'BC' :'Patches',
- 'DR' :'Low Drifting',
- 'BL' : 'Blowing',
- 'SH':'Showers',
- 'TS': 'Thunderstorm',
- 'FZ': 'Freezing',
- 'DZ' : 'Drizzle',
- 'RA' :'Rain',
- 'SN' : 'Snow',
- 'SG' : 'Snow Grains',
- 'IC' : 'Ice Crystals',
- 'PL' : 'Ice Pellets',
- 'GR' : 'Hail',
- 'GS' : 'Small Hail',
- 'UP' : 'Unknown Preciptation',
- 'BR' : 'Mist',
- 'FG': 'Fog',
- 'FU' : 'Smoke',
- 'VA' : 'Volcanic Ash',
- 'DU' : 'Widespread Dust',
- 'SA' : 'Sand',
- 'HZ' : 'Haze',
- 'PY' : 'Spray',
- 'PO' : 'Well-Developed Dust/Sand Whirls',
- 'SQ' : 'Squalls',
- 'FC' : 'Funnel Cloud Tornado Waterspout',
- 'SS' : 'Sandstorm',
- 'DS' :'Duststorm'
- }
- output = ''
- condition=''
- if data_parts[0] in intensity:
- if data_parts[0] == '-':
- output = output + 'Light'
- elif data_parts[0] == '+':
- output = output + 'Heavy'
- else:
- output = output + 'Near the place'
- for i in range(1, len(data_parts)):
- try:
- int(data_parts[i])
- except ValueError:
- for x in range(i,len(data_parts)):
- if len(condition) == 2:
- output += 'with '+ weather_library[condition]
- condition = ''
- condition += data_parts[x]
- print output
- '''This part decodes the condition for sky.'''
- def Decoded_5(data_parts):
- sky_condition=[]
- sky = {
- 'SKC' : 'Clear',
- 'CLR' : 'Clear (automated report)',
- 'FEW' : 'Few clouds are in the sky but most part is clear',
- 'SCT' : 'Clouds are scattered all over the sky',
- 'BKN' : 'Sky seems broken',
- 'OVC' : 'sky is overcast',
- }
- print 'Sky condition:',sky[data_parts[0:3]],'after',data_parts[3:6]+' ft. of ceiling.'
- '''This part is for the decoding the temperature and the dewpoint'''
- def Decoded_6(data_parts):
- if data_parts[0]=='M':
- print 'Temperature is -%s' %data_parts[1:2],'degree Celsius'
- else:
- print 'Temperature is %s'%data_parts[0:2],'degree Celsius'
- if data_parts[3]=='M':
- print 'Dewpoint is -%s'%data_parts[4:6],'degree Celsius'
- else:
- print 'Dewpoint is %s'%data_parts[3:6],'degree Celsius'
- '''This part decodes the pressure from the sea level '''
- def Decoded_7(data_parts):
- print 'Pressure from sea level :%s' %data_parts[1:6],'hegta pascal'
- '''This part returns the alimeter'''
- def Decoded_8(data_parts):
- print 'Altimeter :%s' %data_parts[1:3]+'.%s "Hg'%data_parts[3:6]
- '''This is for finding the ICAO of the station from the user input.'''
- def find_by_city(data_parts):
- url='http://weather.rap.ucar.edu/surface/stations.txt'
- f = urllib2.urlopen(url)
- c = f.read()
- str(data_parts)
- start = c.find('CD STATION ')
- c = c[start:len(c)]
- if c.find(data_parts):
- end = c.find(data_parts)+21
- start = c.find(data_parts)+17
- found= c[start:end]
- icao = found
- return extract(icao)
- else:
- print 'Cannot find it. Please try again.'
- pass
- if __name__ == "__main__":
- report= raw_input('please enter the City::')
- report= report.upper()
- data = find_by_city(report)
- data_decode(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement