Changeset 553 in lmdz_wrf for trunk/tools
- Timestamp:
- Jul 2, 2015, 5:37:52 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/create_OBSnetcdf.py
r332 r553 113 113 114 114 MissingValue='|' list of ASCII values for missing values within the data 115 (as they appear! )115 (as they appear!, 'empty' for no value at all) 116 116 comment=comments 117 117 … … 121 121 varBUFR='|' list BUFR code of the variables 122 122 varTYPE='|' list of variable types ('D', 'F', 'I', 'I64', 'S') 123 varOPER='|' list of operations to do to the variables to meet their units ([oper],[val]) 124 [oper]=-,sumc,subc,mulc,divc (nothing, add, rest, multiply and divide) 123 125 124 126 NAMElon=name of the variable with the longitude (x position) … … 154 156 elif descn[0:3] == 'FMT': 155 157 desc[descn] = descv 158 if not desc.has_key('varOPER'): desc['varOPER'] = None 156 159 157 160 if dbg: … … 168 171 varunits = desc['varU'][ivar] 169 172 if desc.has_key('varBUFR'): 170 varbufr=[ivar] 171 print ' ', ivar, varname + ':',varLname,'[',varunits, \ 172 ']','bufr code:',varbufr 173 varbufr = desc['varBUFR'][ivar] 173 174 else: 174 print ' ', ivar, varname + ':',varLname,'[',varunits,']' 175 varbufr = None 176 if desc['varOPER'] is not None: 177 opv = desc['varOPER'][ivar] 178 else: 179 opv = None 180 print ' ', ivar, varname+':',varLname,'[',varunits, \ 181 ']','bufr code:',varbufr,'oper:',opv 175 182 176 183 descobj.close() … … 178 185 return desc 179 186 180 def value_fmt(val, miss, fmt):187 def value_fmt(val, miss, op, fmt): 181 188 """ Function to transform an ASCII value to a given format 182 189 val= value to transform 183 190 miss= list of possible missing values 191 op= operation to perform to the value 184 192 fmt= format to take: 185 193 'D': float double precission … … 194 202 fname = 'value_fmt' 195 203 204 aopers = ['sumc','subc','mulc','divc'] 205 196 206 fmts = ['D', 'F', 'I', 'I64', 'S'] 197 207 Nfmts = len(fmts) 198 208 199 209 if not searchInlist(miss,val): 200 if not searchInlist(fmts, fmt): 201 print errormsg 202 print ' ' + fname + ": format '" + fmt + "' not ready !!" 203 quit(-1) 210 if searchInlist(miss,'empty') and len(val) == 0: 211 newval = None 204 212 else: 205 if fmt == 'D': 206 newval = np.float32(val) 207 elif fmt == 'F': 208 newval = np.float(val) 209 elif fmt == 'I': 210 newval = int(val) 211 elif fmt == 'I64': 212 newval = np.int64(val) 213 elif fmt == 'S': 214 newval = val 213 print 'op:',op 214 if op != '-': 215 opern = op.split(',')[0] 216 operv = np.float(op.split(',')[1]) 217 218 if not searchInlist(aopers,opern): 219 print errormsg 220 print ' ' + fname + ": operation '" + opern + "' not ready!!" 221 print ' availables:',aopers 222 quit(-1) 223 else: 224 opern = 'sumc' 225 operv = 0. 226 227 if not searchInlist(fmts, fmt): 228 print errormsg 229 print ' ' + fname + ": format '" + fmt + "' not ready !!" 230 quit(-1) 231 else: 232 if fmt == 'D': 233 opv = np.float32(operv) 234 if opern == 'sumc': newval = np.float32(val) + opv 235 elif opern == 'subc': newval = np.float32(val) - opv 236 elif opern == 'mulc': newval = np.float32(val) * opv 237 elif opern == 'divc': newval = np.float32(val) / opv 238 elif fmt == 'F': 239 opv = np.float(operv) 240 if opern == 'sumc': newval = np.float(val) + opv 241 elif opern == 'subc': newval = np.float(val) - opv 242 elif opern == 'mulc': newval = np.float(val) * opv 243 elif opern == 'divc': newval = np.float(val) / opv 244 elif fmt == 'I': 245 opv = int(operv) 246 if opern == 'sumc': newval = int(val) + opv 247 elif opern == 'subc': newval = int(val) - opv 248 elif opern == 'mulc': newval = int(val) * opv 249 elif opern == 'divc': newval = int(val) / opv 250 elif fmt == 'I64': 251 opv = np.int64(operv) 252 if opern == 'sumc': newval = np.int64(val) + opv 253 elif opern == 'subc': newval = np.int64(val) - opv 254 elif opern == 'mulc': newval = np.int64(val) * opv 255 elif opern == 'divc': newval = np.int64(val) / opv 256 elif fmt == 'S': 257 newval = val 215 258 else: 216 259 newval = None … … 218 261 return newval 219 262 220 def read_datavalues(dataf, comchar, colchar, fmt, miss, varns, dbg):263 def read_datavalues(dataf, comchar, colchar, fmt, oper, miss, varns, dbg): 221 264 """ Function to read from an ASCII file values in column 222 265 dataf= data file … … 225 268 dbg= debug mode or not 226 269 fmt= list of kind of values to be found 270 oper= list of operations to perform 227 271 miss= missing value 228 272 varns= list of name of the variables to find … … 233 277 ofile = open(dataf, 'r') 234 278 Nvals = len(fmt) 279 280 if oper is None: 281 opers = [] 282 for ioper in range(Nvals): 283 opers.append('-') 284 else: 285 opers = oper 235 286 236 287 finalvalues = {} … … 281 332 for ivar in range(Nvals): 282 333 if dbg: 283 print iline, varns[ivar],'value:',values[ivar],miss,fmt[ivar] 334 print iline, varns[ivar],'value:',values[ivar],miss,opers[ivar], \ 335 fmt[ivar] 284 336 285 337 if iline == 0: 286 338 listvals = [] 287 listvals.append(value_fmt(values[ivar], miss, fmt[ivar])) 339 listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ 340 fmt[ivar])) 288 341 finalvalues[varns[ivar]] = listvals 289 342 else: 290 343 listvals = finalvalues[varns[ivar]] 291 listvals.append(value_fmt(values[ivar], miss, fmt[ivar])) 344 listvals.append(value_fmt(values[ivar], miss, opers[ivar], \ 345 fmt[ivar])) 292 346 finalvalues[varns[ivar]] = listvals 293 347 else: … … 309 363 310 364 Nvals = len(values) 365 311 366 for iv in range(Nvals): 312 367 stringv=values[iv] … … 587 642 """ Function to add a station description in a netCDF file 588 643 onc= netCDF object 589 stdesc= station description lon, lat, height644 stdesc= station description name, lon, lat, height 590 645 """ 591 646 fname = 'adding_station_desc' 592 647 593 648 newdim = onc.createDimension('nst',1) 649 650 newvar = objfile.createVariable( 'station', 'c', ('nst','StrLength')) 651 writing_str_nc(newvar, [stdesc[0].replace('!', ' ')], StringLength) 652 653 newvar = objfile.createVariable( 'lonstGDM', 'c', ('nst','StrLength')) 654 Gv = int(stdesc[1]) 655 Dv = int((stdesc[1] - Gv)*60.) 656 Mv = int((stdesc[1] - Gv - Dv/60.)*3600.) 657 writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) 594 658 595 659 if onc.variables.has_key('lon'): … … 603 667 newvar = objfile.createVariable( lonname, 'f4', ('nst')) 604 668 basicvardef(newvar, lonname, 'longitude', 'degrees_West' ) 605 newvar[:] = stdesc[0] 669 newvar[:] = stdesc[1] 670 671 newvar = objfile.createVariable( 'latstGDM', 'c', ('nst','StrLength')) 672 Gv = int(stdesc[2]) 673 Dv = int((stdesc[2] - Gv)*60.) 674 Mv = int((stdesc[2] - Gv - Dv/60.)*3600.) 675 writing_str_nc(newvar, [str(Gv)+"d" + str(Dv)+"m" + str(Mv)+'s'], StringLength) 606 676 607 677 if onc.variables.has_key('lat'): … … 615 685 newvar = objfile.createVariable( latname, 'f4', ('nst')) 616 686 basicvardef(newvar, lonname, 'latitude', 'degrees_North' ) 617 newvar[:] = stdesc[ 1]687 newvar[:] = stdesc[2] 618 688 619 689 if onc.variables.has_key('height'): … … 627 697 newvar = objfile.createVariable( heightname, 'f4', ('nst')) 628 698 basicvardef(newvar, heightname, 'height above sea level', 'm' ) 629 newvar[:] = stdesc[ 2]699 newvar[:] = stdesc[3] 630 700 631 701 return 702 703 def oper_values(dvals, opers): 704 """ Function to operate the values according to given parameters 705 dvals= datavalues 706 opers= operations 707 """ 708 fname = 'oper_values' 709 710 newdvals = {} 711 varnames = dvals.keys() 712 713 aopers = ['sumc','subc','mulc','divc'] 714 715 Nopers = len(opers) 716 for iop in range(Nopers): 717 vn = varnames[iop] 718 print vn,'oper:',opers[iop] 719 if opers[iop] != '-': 720 opern = opers[iop].split(',')[0] 721 operv = np.float(opers[iop].split(',')[1]) 722 723 vvals = np.array(dvals[vn]) 724 725 if opern == 'sumc': 726 newvals = np.where(vvals is None, None, vvals+operv) 727 elif opern == 'subc': 728 newvals = np.where(vvals is None, None, vvals-operv) 729 elif opern == 'mulc': 730 newvals = np.where(vvals is None, None, vvals*operv) 731 elif opern == 'divc': 732 newvals = np.where(vvals is None, None, vvals/operv) 733 else: 734 print errormsg 735 print ' ' + fname + ": operation '" + opern + "' not ready!!" 736 print ' availables:',aopers 737 quit(-1) 738 739 newdvals[vn] = list(newvals) 740 else: 741 newdvals[vn] = dvals[vn] 742 743 return newdvals 632 744 633 745 ####### ###### ##### #### ### ## # … … 645 757 help="':', list of characters used for comments", metavar="VALUES") 646 758 parser.add_option("-d", "--descriptionfile", dest="fdesc", 647 help="description file to use" , metavar="FILE")759 help="description file to use" + read_description.__doc__, metavar="FILE") 648 760 parser.add_option("-e", "--end_column", dest="endcol", 649 761 help="character to indicate end of the column ('space', for ' ')", metavar="VALUE") … … 653 765 help="whther debug is required ('false', 'true')", metavar="VALUE") 654 766 parser.add_option("-k", "--kindObs", dest="obskind", type='choice', choices=kindobs, 655 help=strkObs, metavar=" FILE")767 help=strkObs, metavar="VALUE") 656 768 parser.add_option("-s", "--stationLocation", dest="stloc", 657 help=" longitude, latitude and height of the station (only for 'single-station')",769 help="name ('!' for spaces), longitude, latitude and height of the station (only for 'single-station')", 658 770 metavar="FILE") 659 771 parser.add_option("-t", "--CFtime", dest="CFtime", help=strCFt, metavar="VALUE") … … 734 846 print " assuming 'single-station': single station on a fixed position at 0,0,0" 735 847 obskind = 'single-station' 736 stationdesc = [0.0, 0.0, 0.0 ]848 stationdesc = [0.0, 0.0, 0.0, 0.0] 737 849 else: 738 850 obskind = opts.obskind … … 743 855 quit(-1) 744 856 else: 745 stationdesc = [np.float(opts.stloc.split(',')[0]), \ 746 np.float(opts.stloc.split(',')[1]), np.float(opts.stloc.split(',')[2])] 857 stvals = opts.stloc.split(',') 858 stationdesc = [stvals[0], np.float(stvals[1]), np.float(stvals[2]), \ 859 np.float(stvals[3])] 747 860 else: 748 861 obskind = opts.obskind … … 778 891 ## 779 892 datavalues = read_datavalues(opts.obsfile, charcomments, endcol, formats, 780 description[' MissingValue'], description['varN'], debug)893 description['varOPER'], description['MissingValue'], description['varN'], debug) 781 894 782 895 # Total number of values
Note: See TracChangeset
for help on using the changeset viewer.