Advertisement
Guest User

metar using city

a guest
Mar 1st, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.67 KB | None | 0 0
  1. #!/usr/bin/python
  2. import urllib2
  3.  
  4. r = 0
  5. y = 1
  6. incase = [ 'SKC','CLR','FEW','SCT','BKN','OVC']
  7.  
  8. def extract(icao):
  9.  
  10.     try:
  11.  
  12.         url = 'http://weather.noaa.gov/pub/data/observations/metar/stations/%s.TXT' %icao
  13.         f = urllib2.urlopen(url)
  14.         content = f.read()
  15.         print content
  16.         c = content.split()
  17.         c.pop(0)
  18.         c.pop(0)
  19.         return c
  20.  
  21.     except urllib2.HTTPError:
  22.         exit('sorry we are out of service. Please try again later.')
  23.  
  24.  
  25.  
  26. def data_decode(data):
  27.     incase = range(0,10)
  28.     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'}
  29.     sky  = {'SKC','CLR','FEW','SCT', 'BKN', 'OVC' }
  30.     print data
  31.     assign =''
  32.     for i in range(0,len(data)):
  33.         if i <= 1:
  34.             print globals()['Decoded_%d' % (int(i))](data[i])
  35.  
  36.     for i in range(2,len(data)):
  37.         assign =data[i]
  38.         if assign=='AUTO':
  39.             print 'The report has been prepared by an automated observing system. '
  40.             continue
  41.         if assign[2:4]=='SM' or assign == '9999':
  42.             print globals()['Decoded_%d' % (int(3))](data[i])
  43.             continue
  44.         if len(assign)==7:
  45.             print globals()['Decoded_%d' % (int(2))](data[i])
  46.             continue
  47.         if assign[0:3] in sky:
  48.             print globals()['Decoded_%d' % (int(5))](data[i])
  49.             continue
  50.         if assign[0:2] in weather_library:
  51.             print globals()['Decoded_%d' % (int(4))](data[i])
  52.             continue
  53.         if assign[0] == 'A':
  54.             print globals()['Decoded_%d' % (int(8))](data[i])
  55.             break
  56.         if assign[0] == 'Q' and r == 0:
  57.             print globals()['Decoded_%d' % (int(7))](data[i])
  58.             continue
  59.         if int(assign[0]) in incase:
  60.             print globals()['Decoded_%d' % (int(6))](data[i])
  61.             continue
  62.  
  63.  
  64.  
  65.  
  66. '''This is for finding the location of the station from the source:http://weather.rap.ucar.edu/surface/stations.txt'''
  67. def Decoded_0(data_parts):
  68.     url='http://weather.rap.ucar.edu/surface/stations.txt'
  69.     f = urllib2.urlopen(url)
  70.     content = f.read()
  71.     c = content
  72.     str(data_parts)
  73.     start = c.find('CD  STATION ')
  74.     c = c[start:len(c)]
  75.     #print c
  76.  
  77.     if c.find(data_parts):
  78.         end = c.find(data_parts)+63
  79.         start = c.find(data_parts)-17
  80.         found= c[start:end]
  81.         print 'City::',found[0:17]
  82.     else:
  83.         print 'not found it'
  84.  
  85. '''This part is for decoding the date and time of the report taken. '''
  86. def Decoded_1(data_parts):
  87.     if (len(data_parts) == 7):
  88.         print 'Date of report taken :', data_parts[0:2],'th of this month'
  89.         print 'Time of report taken :', data_parts[2:4],':',data_parts[4:6]
  90.  
  91.  
  92.  
  93. '''This function decodes the wind condition from the given data codes'''
  94. def Decoded_2(data_parts):
  95.     if (data_parts == '00000KT'):
  96.         print 'It will be calm, no winds.'
  97.  
  98.     if data_parts[0:3]=='VRB':
  99.         print 'Direction of wind :', 'The direction of the wind is not determined.'
  100.     else:
  101.         print 'Direction of wind :', data_parts[0:3],'Degrees'
  102.     if data_parts.find('G'):
  103.         print 'Gust of air : ', data_parts[data_parts.find('G'):data_parts.find('KT')]
  104.     else:
  105.         print 'Gust of air : None'
  106.     print 'Speed of air : ', data_parts[3:5],'knot.'
  107.  
  108.  
  109. '''This function is to decode the data code for visibility  '''
  110. def Decoded_3(data_parts):
  111.     if data_parts == '9999':
  112.         print 'Visibility is more clear than 30 miles.'
  113.     else:
  114.         end = data_parts.find('SM')
  115.         print 'Visibility is',data_parts[0:end],'statute miles'
  116.  
  117.  
  118. '''This function is for the weather condition report from the data code.'''
  119. def Decoded_4(data_parts):
  120.     intensity = ['-', '+']
  121.     weather_library = {'MI' : 'Shallow',
  122.                        'BC' :'Patches',
  123.                        'DR' :'Low Drifting',
  124.                        'BL' : 'Blowing',
  125.                        'SH':'Showers',
  126.                        'TS': 'Thunderstorm',
  127.                        'FZ': 'Freezing',
  128.                        'DZ' : 'Drizzle',
  129.                        'RA' :'Rain',
  130.                        'SN' : 'Snow',
  131.                        'SG' : 'Snow Grains',
  132.                        'IC' : 'Ice Crystals',
  133.                        'PL' : 'Ice Pellets',
  134.                        'GR' : 'Hail',
  135.                        'GS' : 'Small Hail',
  136.                        'UP' : 'Unknown Preciptation',
  137.                        'BR' : 'Mist',
  138.                        'FG': 'Fog',
  139.                        'FU' : 'Smoke',
  140.                        'VA' : 'Volcanic Ash',
  141.                        'DU' : 'Widespread Dust',
  142.                        'SA' : 'Sand',
  143.                        'HZ' : 'Haze',
  144.                        'PY' : 'Spray',
  145.                        'PO' : 'Well-Developed Dust/Sand Whirls',
  146.                        'SQ' : 'Squalls',
  147.                        'FC' : 'Funnel Cloud Tornado Waterspout',
  148.                        'SS' : 'Sandstorm',
  149.                        'DS' :'Duststorm'
  150.                        }
  151.     output = ''
  152.     condition=''
  153.     if data_parts[0] in intensity:
  154.         if data_parts[0] == '-':
  155.             output = output + 'Light'
  156.         elif data_parts[0] == '+':
  157.             output = output + 'Heavy'
  158.         else:
  159.             output = output + 'Near the place'
  160.  
  161.     for i in range(1, len(data_parts)):
  162.         try:
  163.             int(data_parts[i])
  164.         except ValueError:
  165.             for x in range(i,len(data_parts)):
  166.                 if len(condition) == 2:
  167.                     output += 'with '+ weather_library[condition]
  168.                     condition = ''
  169.                 condition += data_parts[x]
  170.     print output
  171.  
  172.  
  173.  
  174. '''This part decodes the condition for sky.'''
  175. def Decoded_5(data_parts):
  176.     sky_condition=[]
  177.     sky  = {
  178.         'SKC' : 'Clear',
  179.         'CLR' : 'Clear (automated report)',
  180.         'FEW' : 'Few clouds are in the sky but most part is clear',
  181.         'SCT' : 'Clouds are scattered  all over the sky',
  182.         'BKN' : 'Sky seems broken',
  183.         'OVC' : 'sky is overcast',
  184.         }
  185.     print 'Sky condition:',sky[data_parts[0:3]],'after',data_parts[3:6]+' ft. of ceiling.'
  186.  
  187.  
  188. '''This part is for the decoding the temperature and the dewpoint'''
  189. def Decoded_6(data_parts):
  190.     if data_parts[0]=='M':
  191.         print 'Temperature is -%s' %data_parts[1:2],'degree Celsius'
  192.     else:
  193.         print 'Temperature is %s'%data_parts[0:2],'degree Celsius'
  194.     if data_parts[3]=='M':
  195.         print 'Dewpoint is -%s'%data_parts[4:6],'degree Celsius'
  196.     else:
  197.         print 'Dewpoint is %s'%data_parts[3:6],'degree Celsius'
  198.  
  199.  
  200. '''This part decodes the pressure from the sea level '''
  201. def Decoded_7(data_parts):
  202.     print 'Pressure from sea level :%s' %data_parts[1:6],'hegta pascal'
  203.  
  204.  
  205. '''This part returns the alimeter'''
  206. def Decoded_8(data_parts):
  207.     print 'Altimeter :%s' %data_parts[1:3]+'.%s "Hg'%data_parts[3:6]
  208.  
  209.  
  210.  
  211. '''This is for finding the ICAO of the station from the user input.'''
  212. def find_by_city(data_parts):
  213.     url='http://weather.rap.ucar.edu/surface/stations.txt'
  214.     f = urllib2.urlopen(url)
  215.     c = f.read()
  216.     str(data_parts)
  217.     start = c.find('CD  STATION ')
  218.     c = c[start:len(c)]
  219.  
  220.     if c.find(data_parts):
  221.         end = c.find(data_parts)+21
  222.         start = c.find(data_parts)+17
  223.         found= c[start:end]
  224.         icao = found
  225.         return extract(icao)
  226.     else:
  227.         print 'Cannot find it. Please try again.'
  228.         pass
  229.  
  230. if __name__ == "__main__":
  231.     report= raw_input('please enter the City::')
  232.     report= report.upper()
  233.     data = find_by_city(report)
  234.     data_decode(data)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement