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