Changeset 554 in lmdz_wrf for trunk/tools
- Timestamp:
- Jul 3, 2015, 12:44:58 PM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/read_ISD.py
r499 r554 123 123 varBUFR='|' list BUFR code of the variables 124 124 varTYPE='|' list of variable types ('D', 'F', 'I', 'I64', 'S') 125 varOPER='|' list of operations to do to the variables to meet their units ([oper],[val]) 126 [oper]=-,sumc,subc,mulc,divc (nothing, add, rest, multiply and divide) 125 127 varL='|' list of length of the sections for each variable within a given line (Fortran-like) 126 128 … … 172 174 varsec = int(desc['varL'][ivar]) 173 175 if desc.has_key('varBUFR'): 174 varbufr=[ivar] 175 print ' ', ivar, varname + ':',varLname,'[',varunits, \ 176 ']','bufr code:',varbufr, 'L:', varsec 176 varbufr = desc['varBUFR'][ivar] 177 177 else: 178 print ' ', ivar, varname + ':',varLname,'[',varunits, \ 179 '] L:',varsec 178 varbufr = None 179 if desc['varOPER'] is not None: 180 opv = desc['varOPER'][ivar] 181 else: 182 opv = None 183 print ' ', ivar, varname+':',varLname,'[',varunits, \ 184 ']','bufr code:',varbufr,'oper:',opv 180 185 181 186 descobj.close() … … 183 188 return desc 184 189 185 def value_fmt(val, miss, fmt):190 def value_fmt(val, miss, op, fmt): 186 191 """ Function to transform an ASCII value to a given format 187 192 val= value to transform 188 193 miss= list of possible missing values 194 op= operation to perform to the value 189 195 fmt= format to take: 190 196 'D': float double precission … … 199 205 fname = 'value_fmt' 200 206 201 fmts = ['D', 'F', 'I', 'I64', 'S'] 207 aopers = ['sumc','subc','mulc','divc'] 208 202 209 Nfmts = len(fmts) 203 210 204 211 if not searchInlist(miss,val): 205 if not searchInlist(fmts, fmt): 206 print errormsg 207 print ' ' + fname + ": format '" + fmt + "' not ready !!" 208 quit(-1) 212 if searchInlist(miss,'empty') and len(val) == 0: 213 newval = None 209 214 else: 210 if fmt == 'D': 211 newval = np.float32(val) 212 elif fmt == 'F': 213 newval = np.float(val) 214 elif fmt == 'I': 215 newval = int(val) 216 elif fmt == 'I64': 217 newval = np.int64(val) 218 elif fmt == 'S': 219 newval = val 215 if op != '-': 216 opern = op.split(',')[0] 217 operv = np.float(op.split(',')[1]) 218 219 if not searchInlist(aopers,opern): 220 print errormsg 221 print ' ' + fname + ": operation '" + opern + "' not ready!!" 222 print ' availables:',aopers 223 quit(-1) 224 else: 225 opern = 'sumc' 226 operv = 0. 227 228 if not searchInlist(fmts, fmt): 229 print errormsg 230 print ' ' + fname + ": format '" + fmt + "' not ready !!" 231 quit(-1) 232 else: 233 if fmt == 'D': 234 opv = np.float32(operv) 235 if opern == 'sumc': newval = np.float32(val) + opv 236 elif opern == 'subc': newval = np.float32(val) - opv 237 elif opern == 'mulc': newval = np.float32(val) * opv 238 elif opern == 'divc': newval = np.float32(val) / opv 239 elif fmt == 'F': 240 opv = np.float(operv) 241 if opern == 'sumc': newval = np.float(val) + opv 242 elif opern == 'subc': newval = np.float(val) - opv 243 elif opern == 'mulc': newval = np.float(val) * opv 244 elif opern == 'divc': newval = np.float(val) / opv 245 elif fmt == 'I': 246 opv = int(operv) 247 if opern == 'sumc': newval = int(val) + opv 248 elif opern == 'subc': newval = int(val) - opv 249 elif opern == 'mulc': newval = int(val) * opv 250 elif opern == 'divc': newval = int(val) / opv 251 elif fmt == 'I64': 252 opv = np.int64(operv) 253 if opern == 'sumc': newval = np.int64(val) + opv 254 elif opern == 'subc': newval = np.int64(val) - opv 255 elif opern == 'mulc': newval = np.int64(val) * opv 256 elif opern == 'divc': newval = np.int64(val) / opv 257 elif fmt == 'S': 258 newval = val 220 259 else: 221 260 newval = None … … 371 410 return additionals 372 411 373 def read_datavalues_conline(dataf, comchar, fmt, Lv, miss, varns, dbg):412 def read_datavalues_conline(dataf, comchar, fmt, oper, Lv, miss, varns, dbg): 374 413 """ Function to read from an ASCII file values in ISH format (continuos line) 375 414 dataf= data file … … 377 416 dbg= debug mode or not 378 417 fmt= list of kind of values to be found 418 oper= list of operations to perform 379 419 Lv= length of the value section 380 420 miss= missing value … … 385 425 ofile = open(dataf, 'r') 386 426 Nvals = len(fmt) 427 428 if oper is None: 429 opers = [] 430 for ioper in range(Nvals): 431 opers.append('-') 432 else: 433 opers = oper 387 434 388 435 finalvalues = {} … … 407 454 values.append(val) 408 455 if dbg: 409 print iline, varns[ivar],'value:',values[ivar],miss,fmt[ivar] 456 print iline, varns[ivar],'value:',values[ivar],miss,opers[ivar], \ 457 fmt[ivar] 410 458 411 459 if iline == 0: 412 460 listvals = [] 413 listvals.append(value_fmt(values[ivar], miss, fmt[ivar])) 461 listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ 462 fmt[ivar])) 414 463 finalvalues[varns[ivar]] = listvals 415 464 else: 416 465 listvals = finalvalues[varns[ivar]] 417 listvals.append(value_fmt(values[ivar], miss, fmt[ivar])) 466 listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ 467 fmt[ivar])) 418 468 finalvalues[varns[ivar]] = listvals 419 469 # Additional variables … … 441 491 442 492 newvar = onc.createVariable( 'station', 'c', ('StrLength')) 443 newvar[0:len(stdesc[0])] = stdesc[0] 493 newvar[0:len(stdesc[0])] = stdesc[0].replace('!', ' ') 444 494 445 495 newdim = onc.createDimension('nst',1) 496 497 newvar = objfile.createVariable( 'lonstGDM', 'c', ('nst','StrLength')) 498 Gv = int(stdesc[1]) 499 Dv = int((stdesc[1] - Gv)*60.) 500 Mv = int((stdesc[1] - Gv - Dv/60.)*3600.) 501 writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) 446 502 447 503 if onc.variables.has_key('lon'): … … 456 512 basicvardef(newvar, lonname, 'longitude', 'degrees_West' ) 457 513 newvar[:] = stdesc[1] 514 515 newvar = objfile.createVariable( 'latstGDM', 'c', ('nst','StrLength')) 516 Gv = int(stdesc[2]) 517 Dv = int((stdesc[2] - Gv)*60.) 518 Mv = int((stdesc[2] - Gv - Dv/60.)*3600.) 519 writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) 458 520 459 521 if onc.variables.has_key('lat'): … … 597 659 ## 598 660 datavalues = read_datavalues_conline(opts.obsfile, charcomments, formats, \ 599 description['varL'], description['MissingValue'], description['varN'], debug) 661 description['varL'], description['varOPER'], description['MissingValue'], \ 662 description['varN'], debug) 600 663 601 664 # Total number of values
Note: See TracChangeset
for help on using the changeset viewer.