Changeset 928 in lmdz_wrf for trunk/tools/TS_ASCII_netCDF.py
- Timestamp:
- Jun 23, 2016, 12:51:22 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/TS_ASCII_netCDF.py
r619 r928 6 6 import numpy as np 7 7 from netCDF4 import Dataset as NetCDFFile 8 import nc_var_tools as ncvar8 import re 9 9 10 10 main = 'TS_ASCII_netCDF.py' … … 13 13 14 14 fillValue = 1.e20 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 reduce_spaces(string): 29 """ Function to give words of a line of text removing any extra space 30 """ 31 values = string.replace('\n','').split(' ') 32 vals = [] 33 for val in values: 34 if len(val) > 0: 35 vals.append(val) 36 37 return vals 38 39 def reduce_last_spaces(string): 40 """ Function to reduce the last right spaces from a string 41 string= string to remove the spaces at the righ side of the string 42 >>> reduce_last_spaces('Hola Lluis ') 43 'Hola Lluis' 44 """ 45 fname = 'reduce_last_spaces' 46 47 Lstring = len(string) 48 49 firstspace = -1 50 for istr in range(Lstring-1,0,-1): 51 if string[istr:istr+1] == ' ': 52 firstspace = istr 53 else: 54 break 55 56 if firstspace == -1: 57 print errormsg 58 print ' ' + fname + ": string '" + string + "' is not ended by spaces!!" 59 print ' char. number of last character:',ord(string[Lstring:Lstring+1]) 60 quit(-1) 61 62 newstr = string[0:firstspace] 63 64 return newstr 65 66 def set_attribute(ncv, attrname, attrvalue): 67 """ Sets a value of an attribute of a netCDF variable. Removes previous attribute value if exists 68 ncv = object netcdf variable 69 attrname = name of the attribute 70 attrvalue = value of the attribute 71 """ 72 73 attvar = ncv.ncattrs() 74 if searchInlist(attvar, attrname): 75 attr = ncv.delncattr(attrname) 76 77 attr = ncv.setncattr(attrname, attrvalue) 78 79 return attr 80 81 def rmNOnum(string): 82 """ Removing from a string all that characters which are not numbers 83 # From: http://stackoverflow.com/questions/4289331/python-extract-numbers-from-a-string 84 """ 85 fname = 'rmNOnum' 86 87 newstr = str(re.findall("[-+]?\d+[\.]?\d*", string)[0]) 88 89 return newstr 90 91 def basicvardef(varobj, vstname, vlname, vunits): 92 """ Function to give the basic attributes to a variable 93 varobj= netCDF variable object 94 vstname= standard name of the variable 95 vlname= long name of the variable 96 vunits= units of the variable 97 """ 98 attr = varobj.setncattr('standard_name', vstname) 99 attr = varobj.setncattr('long_name', vlname) 100 attr = varobj.setncattr('units', vunits) 101 102 return 103 104 def values_fortran_fmt(lin,fFMT): 105 """ Function to give back the values of an ASCII line according to its fortran printing format 106 lin= ASCII line 107 fFMT= list with the fortran FORMAT formats 108 forline = 'Natchitoches (RGNL) 1 11 0011 ( 31.733, -93.100) ( 28, 157) ( 31.761, -93.113) 41.2 meters' 109 formats = ['A26', 'I2', 'I3', 'A6', 'A2', 'F7.3', 'A1', 'F8.3', 'A3', 'I4', 'A1', 'I4', 110 'A3', 'F7.3', 'A1', 'F8.3', 'A2', 'F6.1', 'A7'] 111 >>> values_fortran_fmt(forline, formats) 112 ['Natchitoches (RGNL) ', 1, 11, ' 0011 ', ' ( ', 31.733, ', ', -93.1, ') ( ', 28, ', ', 157, ') 113 ( ', 31.761, ', ', -93.113, ') ', 41.2, ' meters'] 114 """ 115 fname = 'values_fortran_fmt' 116 117 afmts = ['A', 'D', 'F', 'I'] 118 119 if lin == 'h': 120 print fname + '_____________________________________________________________' 121 print values_fortran_fmt.__doc__ 122 quit() 123 124 fvalues = [] 125 ichar=0 126 ival = 0 127 for ft in fFMT: 128 Lft = len(ft) 129 if ft[0:1] == 'A' or ft[0:1] == 'a': 130 Lfmt = int(ft[1:Lft+1]) 131 fvalues.append(lin[ichar:ichar+Lfmt+1]) 132 ichar = ichar + Lfmt 133 elif ft[0:1] == 'F' or ft[0:1] == 'f': 134 if ft.find('.') != -1: 135 Lft = len(ft.split('.')[0]) 136 Lfmt = int(ft[1:Lft]) 137 fvalues.append(np.float(rmNOnum(lin[ichar:ichar+Lfmt+1]))) 138 ichar = ichar + Lfmt 139 elif ft[0:1] == 'D' or ft[0:1] == 'd': 140 if ft.find('.') != -1: 141 Lft = len(ft.split('.')[0]) 142 Lfmt = int(ft[1:Lft]) 143 fvalues.append(np.float64(rmNOnum(lin[ichar:ichar+Lfmt+1]))) 144 ichar = ichar + Lfmt 145 elif ft[0:1] == 'I' or ft[0:1] == 'i': 146 Lfmt = int(ft[1:Lft+1]) 147 fvalues.append(int(rmNOnum(lin[ichar:ichar+Lfmt+1]))) 148 ichar = ichar + Lfmt 149 elif ft.find('X') != -1 or ft.find('x') != -1: 150 print ' ' + fname + ': skipping space' 151 ichar = ichar + int(rmNOnum(ft)) 152 else: 153 print errormsg 154 print ' ' + fname + ": format '" + ft[0:1] + "' not ready!!" 155 print ' Available formats:',afmts 156 quit(-1) 157 158 ival = ival + 1 159 160 return fvalues 161 15 162 16 163 def ts_header(ln): … … 24 171 'A3', 'F7.3', 'A1', 'F8.3', 'A2', 'F6.1', 'A7'] 25 172 26 headervalues = ncvar.values_fortran_fmt(ln,fmts)173 headervalues = values_fortran_fmt(ln,fmts) 27 174 28 175 return headervalues 176 177 def variables_values(varName): 178 """ Function to provide values to plot the different variables values from ASCII file 179 'variables_values.dat' 180 variables_values(varName) 181 [varName]= name of the variable 182 return: [var name], [std name], [minimum], [maximum], 183 [long name]('|' for spaces), [units], [color palette] (following: 184 http://matplotlib.org/1.3.1/examples/color/colormaps_reference.html) 185 [varn]: original name of the variable 186 NOTE: It might be better doing it with an external ASII file. But then we 187 got an extra dependency... 188 >>> variables_values('WRFght') 189 ['z', 'geopotential_height', 0.0, 80000.0, 'geopotential|height', 'm2s-2', 'rainbow'] 190 """ 191 import subprocess as sub 192 193 fname='variables_values' 194 195 if varName == 'h': 196 print fname + '_____________________________________________________________' 197 print variables_values.__doc__ 198 quit() 199 200 # This does not work.... 201 # folderins = sub.Popen(["pwd"], stdout=sub.PIPE) 202 # folder = list(folderins.communicate())[0].replace('\n','') 203 # From http://stackoverflow.com/questions/4934806/how-can-i-find-scripts-directory-with-python 204 folder = os.path.dirname(os.path.realpath(__file__)) 205 206 infile = folder + '/variables_values.dat' 207 208 if not os.path.isfile(infile): 209 print errormsg 210 print ' ' + fname + ": File '" + infile + "' does not exist !!" 211 quit(-1) 212 213 # Variable name might come with a statistical surname... 214 stats=['min','max','mean','stdv', 'sum'] 215 216 # Variables with a statistical section on their name... 217 NOstatsvars = ['zmaxth', 'zmax_th', 'lmax_th', 'lmaxth'] 218 219 ifst = False 220 if not searchInlist(NOstatsvars, varName.lower()): 221 for st in stats: 222 if varName.find(st) > -1: 223 print ' '+ fname + ": varibale '" + varName + "' with a " + \ 224 "statistical surname: '",st,"' !!" 225 Lst = len(st) 226 LvarName = len(varName) 227 varn = varName[0:LvarName - Lst] 228 ifst = True 229 break 230 if not ifst: 231 varn = varName 232 233 ncf = open(infile, 'r') 234 235 for line in ncf: 236 if line[0:1] != '#': 237 values = line.replace('\n','').split(',') 238 if len(values) != 8: 239 print errormsg 240 print "problem in varibale:'", values[0], \ 241 'it should have 8 values and it has',len(values) 242 quit(-1) 243 244 if varn[0:6] == 'varDIM': 245 # Variable from a dimension (all with 'varDIM' prefix) 246 Lvarn = len(varn) 247 varvals = [varn[6:Lvarn+1], varn[6:Lvarn+1], 0., 1., \ 248 "variable|from|size|of|dimension|'" + varn[6:Lvarn+1] + "'", '1', \ 249 'rainbow'] 250 else: 251 varvals = [values[1].replace(' ',''), values[2].replace(' ',''), \ 252 np.float(values[3]), np.float(values[4]),values[5].replace(' ',''),\ 253 values[6].replace(' ',''), values[7].replace(' ','')] 254 if values[0] == varn: 255 ncf.close() 256 return varvals 257 break 258 259 print errormsg 260 print ' ' + fname + ": variable '" + varn + "' not defined !!!" 261 ncf.close() 262 quit(-1) 263 264 return 29 265 30 266 ####### ###### ##### #### ### ## # … … 44 280 45 281 tsvu = ['K', 'kg/kg', 'm/s', 'm/s', 'Pa', 'W/m2', 'W/m2', 'W/m2', 'W/m2', 'K', 'K', 'mm', 'mm', '1'] 282 46 283 47 284 ####### ####### … … 76 313 objofile.createDimension('time',None) 77 314 78 ncvar.set_attribute(objofile, 'author', 'Lluis Fita Borrell')79 ncvar.set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique')80 ncvar.set_attribute(objofile, 'university', 'University Pierre et Marie Curie')81 ncvar.set_attribute(objofile, 'center', 'Centre national de la recherche scientifique')82 ncvar.set_attribute(objofile, 'country', 'France')83 ncvar.set_attribute(objofile, 'city', 'Paris')84 ncvar.set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py')85 ncvar.set_attribute(objofile, 'version', '1.0')315 set_attribute(objofile, 'author', 'Lluis Fita Borrell') 316 set_attribute(objofile, 'institution', 'Laboratoire Meteorologique Dynamique') 317 set_attribute(objofile, 'university', 'University Pierre et Marie Curie') 318 set_attribute(objofile, 'center', 'Centre national de la recherche scientifique') 319 set_attribute(objofile, 'country', 'France') 320 set_attribute(objofile, 'city', 'Paris') 321 set_attribute(objofile, 'script', 'TS_ASCII_netCFD.py') 322 set_attribute(objofile, 'version', '1.0') 86 323 87 324 time_step = [] … … 96 333 itz = 0 97 334 for line in objlfile: 98 values = ncvar.reduce_spaces(line)335 values = reduce_spaces(line) 99 336 # print iline, values[0], dimz, Searchdimz 100 337 # Writting general information … … 103 340 valueshead = ts_header(line) 104 341 105 ncvar.set_attribute(newvar, 'name', ncvar.reduce_last_spaces(valueshead[0]))106 ncvar.set_attribute(newvar, 'acronym',valueshead[3].replace(' ',''))107 108 ncvar.set_attribute(newvar, 'real_lon', valueshead[5])109 ncvar.set_attribute(newvar, 'real_lat', valueshead[7])110 111 ncvar.set_attribute(newvar, 'x_grid_point', valueshead[9])112 ncvar.set_attribute(newvar, 'y_grid_point', valueshead[11])113 114 ncvar.set_attribute(newvar, 'model_lon', valueshead[13])115 ncvar.set_attribute(newvar, 'model_lat', valueshead[15])116 ncvar.set_attribute(newvar, 'model_height', valueshead[17])342 set_attribute(newvar, 'name', reduce_last_spaces(valueshead[0])) 343 set_attribute(newvar, 'acronym',valueshead[3].replace(' ','')) 344 345 set_attribute(newvar, 'real_lon', valueshead[5]) 346 set_attribute(newvar, 'real_lat', valueshead[7]) 347 348 set_attribute(newvar, 'x_grid_point', valueshead[9]) 349 set_attribute(newvar, 'y_grid_point', valueshead[11]) 350 351 set_attribute(newvar, 'model_lon', valueshead[13]) 352 set_attribute(newvar, 'model_lat', valueshead[15]) 353 set_attribute(newvar, 'model_height', valueshead[17]) 117 354 simstarttime = refdate 118 355 else: … … 145 382 newvar = objofile.createVariable('time','f8',('time')) 146 383 newvar[:] = time_stepv*3600. 147 newattr = ncvar.basicvardef(newvar, 'time', 'time', 'seconds since ' + \384 newattr = basicvardef(newvar, 'time', 'time', 'seconds since ' + \ 148 385 simstarttime.replace('_',' ')) 149 ncvar.set_attribute(newvar, 'calendar', 'standard')386 set_attribute(newvar, 'calendar', 'standard') 150 387 151 388 dt = time_stepv[1] - time_stepv[0] … … 155 392 if tsvn[iv] == 't' or tsvn[iv] == 'u' or tsvn[iv] == 'v' or tsvn[iv] == 'q': 156 393 varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar = \ 157 ncvar.variables_values('TS' + tsvn[iv])394 variables_values('TS' + tsvn[iv]) 158 395 tsu = unitsvar 159 396 else: 160 397 varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar = \ 161 ncvar.variables_values(tsvn[iv])398 variables_values(tsvn[iv]) 162 399 tsu = tsvu[iv] 163 400 … … 165 402 newvar[:] = tsvaluesv[:,iv] 166 403 167 newattr = ncvar.basicvardef(newvar, stdname, longname.replace('|',' '), tsu)168 newattr = ncvar.set_attribute(newvar, 'wrfTSname', tsvn[iv])169 newattr = ncvar.set_attribute(newvar, 'wrfTSdesc', tsvln[iv])404 newattr = basicvardef(newvar, stdname, longname.replace('|',' '), tsu) 405 newattr = set_attribute(newvar, 'wrfTSname', tsvn[iv]) 406 newattr = set_attribute(newvar, 'wrfTSdesc', tsvln[iv]) 170 407 171 408 # Extra vars … … 175 412 varvals[1:dimt] = pracc[1:dimt] - pracc[0:dimt-1] 176 413 varname, stdname, minvar, maxvar, longname, unitsvar, cbarvar = \ 177 ncvar.variables_values('RAINTOT')414 variables_values('RAINTOT') 178 415 179 416 newvar = objofile.createVariable(varname, 'f4', ('time')) 180 417 newvar[:] = varvals / dt 181 newattr = ncvar.basicvardef(newvar, stdname, longname.replace('|',' '), unitsvar )418 newattr = basicvardef(newvar, stdname, longname.replace('|',' '), unitsvar ) 182 419 183 420 objofile.sync()
Note: See TracChangeset
for help on using the changeset viewer.