import sys import os import requests import json from datetime import datetime from datetime import timedelta from sys import argv # # This is a python3 script. # Run this program using the following examples: # python3 nwswarningsPy3.py --help # python3 nwswarningsPy3.py -o myfile # python3 nwswarningsPy3.py --types [FLS,FFW,SVR,SMW,TOR] # def processArgs(argv): opts = {} # Empty dictionary to store key-value pairs i = 0 while i < len(argv): # print argv[i] if argv[i] == '--help': printHelp() sys.exit() elif argv[i] == '--types': print(argv[i+1]) opts[argv[i]] = argv[i+1] i += 1 elif argv[i][0] == '-': # Found a "-name value" pair opts[argv[i]] = argv[i+1] # Add key and value to the dictionary i += 1 # argv = argv[1:] # Reduce the argument list by copying it starting from index 1 i += 1 return opts def printHelp(): print('Usage: python nwswarnings.py [OPTION] ...') print('Harvest National Weather Service watches and warnings from XURL and create GeoJSON file suitable for map display.') print('Options and arguments:') print('--help : print this help message' ) print('-o filename : assign output filename (default: nws_warnings.geojson)') print('--types [FLS,FFW,SVR,SMW,TOR]') print(' : specify which products to retrieve (default is all) where') print(' FLS - Flood Warning / Flood Statement / Flood Advisory') print(' FFW - Flash Flood Warning') print(' SVR - Severe Thunderstorm Warning') print(' SMW - Special Marine Warning') print(' TOR - Tornado Warning') def main(): nwsFile = 'nws_warnings.geojson' args = processArgs(argv) types = []; if '-o' in args: print('Output file: ' + args['-o']) nwsFile = args['-o'] if '--types' in args: print('Retrieving ' + args['--types']) types = args['--types'] count = 0 warnTypes = [] warnUrl = [] warnTypes.append('FLS') # Flood Warning (FLW) / Flood Statement (FLS) warnTypes.append('FFW') # Flash Flood Warning (FFW) warnTypes.append('FLS') # Flood Advisory warnTypes.append('SVR') # Severe Thunderstorm Warning (SWR) warnTypes.append('SMW') # Special Marine Warning (SMW) warnTypes.append('TOR') # Tornado Warning (TOR) warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Flood%20Warning') warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Flash%20Flood%20Warning') warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Flood%20Advisory') warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Severe%20Thunderstorm%20Warning') warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Special%20Marine%20Warning') warnUrl.append('https://forecast.weather.gov/wwamap/wwatxtget.php?cwa=usa&wwa=Tornado%20Warning') json_file = open(nwsFile, 'w') json_file.write('{"type": "FeatureCollection",\n') json_file.write('"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },\n') json_file.write('"product":"NWS Warnings at [2018 03/23 03:00:01 UTC]",\n') json_file.write('"created_by":"Ventura County Watershed Protection District and TriLynx Systems",\n') json_file.write('"created_from":"https://forecast.weather.gov/wwamap/wwatxtget.php",\n') json_file.write('"features": [\n') for i in range(0,6): if types != [] and warnTypes[i] not in types: continue blnData = False blnLatLon = False lat = [] lon = [] lat0 = '' lon0 = '' numLatlon = 0 blankLines = 0 outLine = '' productName = '' productData = '' prod = warnTypes[i] url = warnUrl[i] place = 0 print('Requesting Product: ' + prod) # context = ssl._create_unverified_context() req = requests.get(url, headers={'User-Agent' : "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7", 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',}) try: #req = urllib.request.urlopen(req, context=context) #charset=req.info().get_content_charset() #page=req.read().decode(charset) page=req.text except requests.HTTPError as e: print(e.code()) print(e.read()) lines = page.split('\n') # Can't use # for line in lines: # because sometimes multiple lines must be processed together. # i.e. LAT...LON for line in lines: #print (line) pos = line.find('
') if (blnData == True): pos2 = line.find('') if (pos2 > 0): sub = line[0:pos2] if (sub == '$$'): blnLatLon = False #print('LatLon set to false') blnData = False data = line[:pos2] productData = productData + data + '\\n' productData = productData.replace('"','\\"') if (count > 0): outLine = ',\n' outLine = outLine + '{"type": "Feature", "properties": { "Class":"' + prod + '", "Name":"' + productName + '", "Description":"' + productData + '"\n'; if (lat0 == '' or lon0 == ''): outLine = outLine + '}}' else: outLine = outLine + '}, "geometry": { "type": "Point", "coordinates": [' + lon0 + ',' + lat0 + ']}}' json_file.write(outLine) if (lat0 != '' and lon0 != ''): outLine = ',{"type": "Feature", "properties": { "Class":"' + prod + '", "Name":"' + productName + '", "Description":"' + productData + '"}, "geometry": { "type": "LineString", "coordinates":[' llcount = 0 for ll in lat: # print(lon) lonStr = lon[llcount] latStr = ll if (llcount > 0): outLine = outLine + ',[' + lonStr + ',' + latStr + ']' else: outLine = outLine + '[' + lonStr + ',' + latStr + ']' llcount = llcount + 1 outLine = outLine + ',[' + lon0 + ',' + lat0 + ']' outLine = outLine + ']}}' json_file.write(outLine) productName = '' productData = '' lat = [] lon = [] lat0 = '' lon0 = '' count = count + 1 else: llpos = 0 if (line.strip() == ''): blankLines = blankLines + 1 else: blankLines = 0 if (blankLines < 2): productData = productData + line + '\\n' if (blnLatLon): sub = line.strip() # print(sub) if (sub == '$$' or sub == ''): blnLatLon = False elif ('TIME' in sub): blnLatLon = False elif (len(sub) > 2): #sub = line.strip() #print(sub) #print('Place=' + str(place)) #if (sub == ''): # blnLatLon = False #else: dataset = sub.split(' ') for data in dataset: #print(data) if (data != ''): deg = data[:-2] + '.' + data[-2:] if (place % 2 == 0): lat.append(deg) else: lon.append('-' + deg) place = place + 1 if ('LAT...LON' in line): #print(line) if ('MISSING' in line): line = 'LAT...LON 0000 0000' blnLatLon = True sub = line[llpos+9:].strip() dataset = sub.split(' ') #print(dataset) place = 0 for data in dataset: if (data != ''): deg = data[:-2] + '.' + data[-2:] if (place % 2 == 0): if (place == 0): lat0 = deg lat.append(deg) else: if (place == 1): lon0 = '-' + deg lon.append('-' + deg) place = place + 1 if (pos > 0): data = line[pos+5:] productName = data productData = data + '\\n' print('Product: ' + productName) blnData = True json_file.write(']}\n') json_file.close() print('Complete') ### Execute main function main()