1 | # -*- coding: iso-8859-15 -*- |
---|
2 | # L. Fita, LMD. Feburary 2015. Python script to generate two lists for simulation purposes |
---|
3 | # for WRF: tslits, lidarlist |
---|
4 | # for LMDZ: pointlocations.txt |
---|
5 | ## e.g. # create_stations-list_simulations.py -a -2,1.3,37.5,40.0:3,6.7,41.7,45.4 -m WRF -n 0.30 -s AEMET_sfcst.dat,sfcstations.dat,MeteoFrance_sfcst.dat -z lidarstations.dat,dropsondes.dat,soundingsstations.dat |
---|
6 | import numpy as np |
---|
7 | import os |
---|
8 | import re |
---|
9 | from optparse import OptionParser |
---|
10 | |
---|
11 | main = 'create_stations-list_simulations.py' |
---|
12 | errormsg = 'ERROR -- error -- ERROR -- error' |
---|
13 | warnmsg = 'WARNING -- warning -- WARNING -- warning' |
---|
14 | |
---|
15 | def searchInlist(listname, nameFind): |
---|
16 | """ Function to search a value within a list |
---|
17 | listname = list |
---|
18 | nameFind = value to find |
---|
19 | >>> searInlist(['1', '2', '3', '5'], '5') |
---|
20 | True |
---|
21 | """ |
---|
22 | for x in listname: |
---|
23 | if x == nameFind: |
---|
24 | return True |
---|
25 | return False |
---|
26 | |
---|
27 | def remove_NONascii(string): |
---|
28 | """ Function to remove that characters which are not in the standard 127 ASCII |
---|
29 | string= string to transform |
---|
30 | >>> remove_NONascii('LluÃs') |
---|
31 | Lluis |
---|
32 | """ |
---|
33 | fname = 'remove_NONascii' |
---|
34 | |
---|
35 | newstring = string |
---|
36 | |
---|
37 | RTFchar= ['á', 'é', 'Ã', 'ó', 'ú', 'à ', 'Ú', 'ì', 'ò', 'ù', 'â', 'ê', 'î', 'ÃŽ', \ |
---|
38 | 'û', 'À', 'ë', 'ï', 'ö', 'ÃŒ', 'ç', 'ñ','Ê', 'Å', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', \ |
---|
39 | 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã', 'Ã',\ |
---|
40 | 'Ã', 'Å', '\n', '\t'] |
---|
41 | ASCchar= ['a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', 'u', 'a', 'e', 'i', 'o', \ |
---|
42 | 'u', 'a', 'e', 'i', 'o', 'u', 'c', 'n','ae', 'oe', 'A', 'E', 'I', 'O', 'U', 'A', \ |
---|
43 | 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'A', 'E', 'I', 'O', 'U', 'C', 'N',\ |
---|
44 | 'AE', 'OE', '', ' '] |
---|
45 | |
---|
46 | Nchars = len(RTFchar) |
---|
47 | for ichar in range(Nchars): |
---|
48 | foundchar = string.find(RTFchar[ichar]) |
---|
49 | if foundchar != 0: |
---|
50 | newstring = newstring.replace(RTFchar[ichar], ASCchar[ichar]) |
---|
51 | |
---|
52 | return newstring |
---|
53 | |
---|
54 | def within(pos, pt, mdist): |
---|
55 | """ Function to determine if a given point is closer to a given distance from a |
---|
56 | series of points |
---|
57 | pos= series of positions |
---|
58 | pt= point |
---|
59 | mdist= distance |
---|
60 | >>> within([[4.0,42.0] ,[5.0,43.0]], [4.5,42,5], 0.5) |
---|
61 | True |
---|
62 | """ |
---|
63 | fname = 'within' |
---|
64 | |
---|
65 | if mindist != 0.: |
---|
66 | if type(pos) == type([]): |
---|
67 | posv = np.array(pos) |
---|
68 | else: |
---|
69 | posv = pos |
---|
70 | |
---|
71 | for ip in range(posv.shape[0]): |
---|
72 | dist = np.sqrt( (posv[ip,0]-pt[0])**2. + (posv[ip,1]-pt[1])**2.) |
---|
73 | if (dist <= mdist): |
---|
74 | return True |
---|
75 | elif mindist == 360.: |
---|
76 | return True |
---|
77 | |
---|
78 | return False |
---|
79 | |
---|
80 | def writting_stations(oout, fobs, ars, model, mindist): |
---|
81 | """ Function to write the given stations in a file |
---|
82 | oout= object of the file to fill up |
---|
83 | fobs= list of files with the observations |
---|
84 | ars= list of areas of interest |
---|
85 | model= for which model are the stations |
---|
86 | mindist= minimal distance between stations (in degrees) |
---|
87 | """ |
---|
88 | fname = 'writting_stations' |
---|
89 | |
---|
90 | Nars = ars.shape[1] |
---|
91 | |
---|
92 | iobs = 1 |
---|
93 | obsstn = [] |
---|
94 | posst = [] |
---|
95 | |
---|
96 | for filen in fobs: |
---|
97 | print ' ' + fname + ": opening '" + filen + "'" |
---|
98 | ofo = open(filen, 'r') |
---|
99 | for line in ofo: |
---|
100 | if line[0:1] != '#' and len(line) > 3: |
---|
101 | vals = line.split(' ') |
---|
102 | stlon = np.float(vals[1]) |
---|
103 | stlat = np.float(vals[2].replace('\n','')) |
---|
104 | for ia in range(Nars): |
---|
105 | if stlon>=ars[0,ia] and stlon<=ars[1,ia] and stlat>=ars[2,ia] \ |
---|
106 | and stlat<=ars[3,ia] and not searchInlist(obsstn, vals[0]) and \ |
---|
107 | not within(posst, [stlon, stlat], mindist): |
---|
108 | if len(vals[0]) > 24: |
---|
109 | stname = vals[0][0:24] |
---|
110 | else: |
---|
111 | stname = vals[0] |
---|
112 | if model == 'WRF': |
---|
113 | oout.write('{0:24s}'.format(stname) + ' h' + \ |
---|
114 | str(iobs).zfill(4) + ' ' + '{0:7.5g}'.format(stlat) +\ |
---|
115 | ' ' + '{0:6.4g}'.format(stlon) + '\n') |
---|
116 | else: |
---|
117 | oout.write('{0:24s}'.format(stname) + ' h' + \ |
---|
118 | str(iobs).zfill(3) + ' '+ '{0:6.4g}'.format(stlon) + \ |
---|
119 | ' ' + '{0:7.5g}'.format(stlat) + '\n') |
---|
120 | |
---|
121 | obsstn.append(vals[0]) |
---|
122 | posst.append([stlon, stlat]) |
---|
123 | iobs = iobs + 1 |
---|
124 | # quit() |
---|
125 | #quit() |
---|
126 | ofo.close() |
---|
127 | |
---|
128 | print ' ' + fname + ':', iobs, 'stations added' |
---|
129 | |
---|
130 | return obsstn |
---|
131 | |
---|
132 | ####### ###### ##### #### ### ## # |
---|
133 | mods = ['LMDZ', 'WRF'] |
---|
134 | |
---|
135 | parser = OptionParser() |
---|
136 | parser.add_option("-a", "--areas", dest="areas", |
---|
137 | help="':' list of areas (minLon,maxLon,minLat,maxLat)", metavar="VALUES") |
---|
138 | parser.add_option("-m", "--model", dest="model", type='choice', choices=mods, |
---|
139 | help="name of the model for the files", metavar="FILES") |
---|
140 | parser.add_option("-n", "--mindist", dest="mindist", |
---|
141 | help="accepted minimal distance betwen stations (degrees)", metavar="VALUES") |
---|
142 | parser.add_option("-s", "--sfcobs", dest="sobs", |
---|
143 | help="',' list of files with surface observations (files as obsname lon lat)", |
---|
144 | metavar="FILE") |
---|
145 | parser.add_option("-z", "--heightobs", dest="hobs", |
---|
146 | help="',' list of files with vertical-observations (files as obsname lon lat)", |
---|
147 | metavar="FILES") |
---|
148 | (opts, args) = parser.parse_args() |
---|
149 | |
---|
150 | ######## ####### |
---|
151 | ## MAIN |
---|
152 | ####### |
---|
153 | |
---|
154 | ofile = 'newlist.dat' |
---|
155 | |
---|
156 | if opts.areas is None: |
---|
157 | print errormsg |
---|
158 | print ' ' + main + ': no areas are provided!!' |
---|
159 | quit(-1) |
---|
160 | else: |
---|
161 | ars = opts.areas.split(':') |
---|
162 | Nareas = len(ars) |
---|
163 | areas = np.zeros((4,Nareas), dtype = np.float) |
---|
164 | iar = 0 |
---|
165 | for ar in ars: |
---|
166 | arvals = ar.split(',') |
---|
167 | for i in range(4): |
---|
168 | areas[i,iar] = np.float(arvals[i]) |
---|
169 | iar = iar + 1 |
---|
170 | |
---|
171 | if opts.hobs is None: |
---|
172 | print errormsg |
---|
173 | print ' ' + main + ': no height observational files are provided!!' |
---|
174 | quit(-1) |
---|
175 | else: |
---|
176 | hobs = opts.hobs.split(',') |
---|
177 | for hf in hobs: |
---|
178 | if not os.path.isfile(hf): |
---|
179 | print errormsg |
---|
180 | print ' ' + main + ": hieght obsevational file '" + hf + \ |
---|
181 | "' does not exist !!" |
---|
182 | quit(-1) |
---|
183 | |
---|
184 | if opts.sobs is None: |
---|
185 | print errormsg |
---|
186 | print ' ' + main + ': no surface observational files are provided!!' |
---|
187 | quit(-1) |
---|
188 | else: |
---|
189 | sobs = opts.sobs.split(',') |
---|
190 | for sf in sobs: |
---|
191 | if not os.path.isfile(sf): |
---|
192 | print errormsg |
---|
193 | print ' ' + main + ": surface obsevational file '" + sf + \ |
---|
194 | "' does not exist !!" |
---|
195 | quit(-1) |
---|
196 | |
---|
197 | if opts.model is None: |
---|
198 | print errormsg |
---|
199 | print ' ' + main + ': no model is provided!!' |
---|
200 | print ' available options:',mods |
---|
201 | quit(-1) |
---|
202 | else: |
---|
203 | print main + ": '" + opts.model + "' model" |
---|
204 | if opts.model == 'WRF': |
---|
205 | sfilename = 'tslist' |
---|
206 | hfilename = 'lidarlist' |
---|
207 | for filen in [sfilename, hfilename]: |
---|
208 | oscff = open(filen, 'w') |
---|
209 | oscff.write('#-----------------------------------------------#\n') |
---|
210 | oscff.write('# 24 characters for name | pfx | LAT | LON |\n') |
---|
211 | oscff.write('#-----------------------------------------------#\n') |
---|
212 | oscff.close() |
---|
213 | |
---|
214 | elif opts.model == 'LMDZ': |
---|
215 | sfilename = 'pointlocations.txt' |
---|
216 | hfilename = 'pointlocations.txt' |
---|
217 | |
---|
218 | if opts.mindist is None: |
---|
219 | print warnmsg |
---|
220 | print ' ' + main + ': no minimal distance is provided!!' |
---|
221 | print ' asuming mindist=0.' |
---|
222 | mindist = 0. |
---|
223 | else: |
---|
224 | mindist = np.float(opts.mindist) |
---|
225 | |
---|
226 | # Height observations |
---|
227 | ## |
---|
228 | objout = open(hfilename, 'a') |
---|
229 | #print main + ": creation of height observational file '" + hfilename + "' ..." |
---|
230 | hstations = writting_stations(objout, hobs, areas, opts.model, mindist) |
---|
231 | print main + "' succesfull written of height location observations '" + hfilename + \ |
---|
232 | "' !!" |
---|
233 | objout.close() |
---|
234 | |
---|
235 | # surface observations |
---|
236 | ## |
---|
237 | objout = open(sfilename, 'a') |
---|
238 | #print main + ": creation of surface observational file '" + sfilename + "' ..." |
---|
239 | sstations = writting_stations(objout, sobs, areas, opts.model, mindist) |
---|
240 | print main + "' succesfull written of height location observations '" + sfilename + \ |
---|
241 | "' !!" |
---|
242 | objout.close() |
---|
243 | quit() |
---|