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 |
---|
5 | from optparse import OptionParser |
---|
6 | import urllib.request |
---|
7 | from urllib.error import URLError, HTTPError |
---|
8 | from 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 | |
---|
14 | regions = {'europe': 'Europe', 'mideast': 'Middle East', 'samer': 'South America'} |
---|
15 | |
---|
16 | parser = OptionParser() |
---|
17 | parser.add_option("-y", "--year", dest="year", help="year to retrieve from", |
---|
18 | metavar="VALUE") |
---|
19 | parser.add_option("-m", "--month", dest="mon", help="month to retrieve from", |
---|
20 | metavar="VALUE") |
---|
21 | parser.add_option("-i", "--InitialDate", dest="idate", help="Initial date of the period to retrieve from [DD][HH]", |
---|
22 | metavar="VALUE") |
---|
23 | parser.add_option("-e", "--EndDate", dest="edate", help="End date of the period to retrieve from [DD][HH]", |
---|
24 | metavar="VALUE") |
---|
25 | parser.add_option("-r", "--Region", dest="region", help="region", |
---|
26 | metavar="VALUE") |
---|
27 | parser.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 | |
---|
43 | iurl = "http://weather.uwyo.edu/cgi-bin/sounding?" |
---|
44 | ftype = "TYPE=TEXT%3ALIST&" |
---|
45 | |
---|
46 | monS = str(int(opts.mon)).zfill(2) |
---|
47 | |
---|
48 | reg = "region=" + opts.region + "&" |
---|
49 | yr = "YEAR=" + opts.year + "&" |
---|
50 | mon = "MONTH=" + monS + "&" |
---|
51 | beg = "FROM=" + opts.idate + "&" |
---|
52 | end = "TO=" + opts.edate + "&" |
---|
53 | st = "STNM=" + opts.stat |
---|
54 | |
---|
55 | url = iurl + reg + ftype + yr + mon + beg + end + st |
---|
56 | |
---|
57 | # function to read html code from url ================================================================= |
---|
58 | def 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 | |
---|
80 | pagina = gets_html(url) |
---|
81 | |
---|
82 | soup = BeautifulSoup(pagina, 'html.parser') |
---|
83 | |
---|
84 | ofilen = 'UWyoming_' + opts.year + monS + '_' + opts.stat + '.snd' |
---|
85 | |
---|
86 | Nvals = len(soup.find_all("h2")) |
---|
87 | print ("Found:", Nvals, " soundngs") |
---|
88 | |
---|
89 | of = open(ofilen, 'w') |
---|
90 | for 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 | |
---|
100 | of.close() |
---|
101 | |
---|
102 | print ("Successfull writting of '" + ofilen + "' !!") |
---|