Subversion Repositories lagranto.ecmwf

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 michaesp 1
# coding: utf-8
2
 
3
try:
4
    from urllib.request import urlopen
5
except:
6
    from urllib2 import urlopen
7
import numpy as np
8
 
9
 
10
def create_sounding_url(station, year, month, day, hour):
11
    '''
12
    create the url to acces sounding based on
13
    the University of Wyoming website:
14
        http://weather.uwyo.edu/upperair/sounding.html
15
    usage:
16
         create_url('06610', 1993, 12, 12, 12)
17
 
18
    return:
19
        url as string
20
    input:
21
    station = Number of the station
22
    year = yyyy
23
    month = mm
24
    day = dd
25
    hour = hh
26
    station numbers:
27
                    10410 Essen (D)
28
                    10618 Idar-Oberstein (D)
29
                    07145 Trappes (F)
30
                    06260 De Bilt (NL)
31
                    10238 Bergen (D)
32
                    06610 Payerne (CH)
33
                    16080 Milan (I)
34
                    10868 Munich (D)
35
     '''
36
 
37
    soundinginfo = {
38
        'station': station,
39
        'yyyy': year,
40
        'mm': month,
41
        'dd': day,
42
        'hh': hour,
43
        }
44
 
45
    template_url = 'http://weather.uwyo.edu/cgi-bin/sounding?TYPE=TEXT%3ALIST&'
46
    template_url += 'YEAR={yyyy}&MONTH={mm:02d}&FROM={dd:02d}{hh:02d}'
47
    template_url += '&STNM={station}'
48
 
49
    return template_url.format(**soundinginfo)
50
 
51
 
52
def get_sounding(file_or_url):
53
    '''
54
    input:
55
        file_or_url (string): the name of the sounding file
56
                              or the URL of the sounding file
57
    output:
58
        sounding (dictionary): dictionary containing the sounding data
59
    '''
60
    # load the data from a sounding, using the university of wyoming website
61
    try:
62
        soundingfile = urlopen(file_or_url).read()
63
    except:
64
        soundingfile = open(file_or_url, 'r').read()
65
 
66
    filelines = soundingfile.splitlines()
67
 
68
    # The first line is found by detecting the first line
69
    # where all entries are numbers
70
    i_firstrow = None
71
    for n, line in enumerate(filelines):
72
        line = line.split()
73
        if line:
74
            if line[0] == 'PRES':
75
                i_colnames = n
76
            cond1 = False not in [is_number(element) for element in line]
77
            cond2 = i_firstrow is None
78
            if cond1 and cond2:
79
                i_firstrow = n
80
            if '</PRE><H3>' in line:
81
                i_lastrow = n
82
 
83
    indices = range(7, 11*8, 7)
84
    indices.insert(0, 0)
85
 
86
    splitindices = [(a, a+7) for a in indices]
87
    columndict = dict((key, ncol) for ncol, key in
88
                      zip(splitindices, filelines[i_colnames].split()))
89
 
90
    variables = filelines[i_colnames].split()
91
    datadict = dict((key, []) for key in filelines[i_colnames].split()
92
                    if key in variables)
93
 
94
    for line in filelines[i_firstrow:i_lastrow]:
95
        for key in datadict:
96
            colstart, colend = columndict[key]
97
            data = line[colstart:colend]
98
            if data.isspace():
99
                data = 'nan'
100
            datadict[key].append(data)
101
    for key in datadict:
102
        datadict[key] = np.array(datadict[key], dtype=float)
103
 
104
    # convert wind from polar to carthesian coordinates
105
    if 'DRCT' in datadict:
106
        datadict['DRCT'] = datadict['DRCT']*np.pi/180
107
    if 'SKNT' in datadict and 'DRCT' in datadict:
108
        datadict['U'], datadict['V'] = (
109
            datadict['SKNT']*np.sin(-datadict['DRCT']),
110
            -datadict['SKNT']*np.cos(datadict['DRCT'])
111
        )
112
 
113
    return datadict