source: lmdz_wrf/trunk/tools/get_UWyoming_snd.py @ 2831

Last change on this file since 2831 was 2735, checked in by lfita, 5 years ago

Addin 'U' on output file name

File size: 3.2 KB
Line 
1# Python3 script to retrieve directly soundng data from U. Wymoning data-base
2#   after an script from A. L. Rolla, CIMA
3#
4## e.g. # get_UWyoming_snd.py -y 2019 -m 6 -i 2700 -e 2800 -r samer -s 87576
5from optparse import OptionParser
6import urllib.request
7from urllib.error import URLError, HTTPError
8from bs4 import BeautifulSoup
9
10# Aca habria que armar el string de request
11# poniendo las variables
12# YEAR , MONTH, FROM, TO , STNM, etc
13
14regions = {'europe': 'Europe', 'mideast': 'Middle East', 'samer': 'South America'}
15
16parser = OptionParser()
17parser.add_option("-y", "--year", dest="year", help="year to retrieve from", 
18  metavar="VALUE")
19parser.add_option("-m", "--month", dest="mon", help="month to retrieve from", 
20  metavar="VALUE")
21parser.add_option("-i", "--InitialDate", dest="idate", help="Initial date of the period to retrieve from [DD][HH]", 
22  metavar="VALUE")
23parser.add_option("-e", "--EndDate", dest="edate", help="End date of the period to retrieve from [DD][HH]", 
24  metavar="VALUE")
25parser.add_option("-r", "--Region", dest="region", help="region", 
26  metavar="VALUE")
27parser.add_option("-s", "--station", dest="stat", help="station number", 
28  metavar="VALUE")
29(opts, args) = parser.parse_args()
30
31#######    #######
32## MAIN
33    #######
34
35#url = "http://weather.uwyo.edu/cgi-bin/sounding?" + "region=samer&" \
36#      "TYPE=TEXT%3ALIST&" \
37#      "YEAR=2019&" \
38#      "MONTH=06&" \
39#      "FROM=2712&" \
40 #     "TO=2712&" \
41  #    "STNM=87576"
42
43iurl = "http://weather.uwyo.edu/cgi-bin/sounding?"
44ftype = "TYPE=TEXT%3ALIST&"
45
46monS = str(int(opts.mon)).zfill(2)
47
48reg = "region=" + opts.region + "&"
49yr = "YEAR=" + opts.year + "&"
50mon = "MONTH=" + monS + "&"
51beg = "FROM=" + opts.idate + "&"
52end = "TO=" + opts.edate + "&"
53st = "STNM=" + opts.stat
54
55url = iurl + reg + ftype + yr + mon + beg + end + st
56
57# function to read html code from url =================================================================
58def gets_html(url):
59    # Read  the url and return  html code
60    try:
61        req = urllib.request.Request(url,
62                                     headers={'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)'}
63                                     )
64        html = urllib.request.urlopen(req).read().decode("utf-8")
65    except HTTPError as e:
66        print('The server couldn\'t fulfill the SOUNDING requested.')
67        print('Error code: ', e.code)
68        html = ""
69        pass
70    except URLError as e:
71        print('We failed to reach the server /weather.uwyo.edu.')
72        print('Reason: ', e.reason)
73        html = ""
74        pass
75    finally:
76        pass
77    return html
78
79
80pagina = gets_html(url)
81
82soup = BeautifulSoup(pagina, 'html.parser')
83
84ofilen = 'UWyoming_' + opts.year + monS + '_' + opts.stat + '.snd'
85
86Nvals = len(soup.find_all("h2"))
87print ("Found:", Nvals, " soundngs")
88
89of = open(ofilen, 'w')
90for isnd in range(Nvals):
91    of.write(soup.find_all("h2")[isnd].text.strip()+'\n')
92    of.write('\n')
93    of.write(soup.find_all("pre")[isnd*2].text.strip()+'\n')
94    of.write('\n')
95    of.write(soup.find_all("h3")[isnd].text.strip()+'\n')
96    of.write('\n')
97    of.write(soup.find_all("pre")[isnd*2+1].text.strip()+'\n')
98    of.write('\n')
99
100of.close()
101
102print ("Successfull writting of '" + ofilen + "' !!")
Note: See TracBrowser for help on using the repository browser.