Changeset 1001 in lmdz_wrf
- Timestamp:
- Aug 10, 2016, 5:53:53 PM (9 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/model_graphics.py
r994 r1001 95 95 return expers, fhs 96 96 97 class VariableInf(object): 98 """ Class which holds all information of a given variable 99 name= CF name of the variable 100 header= header of the file from which it can be computed 101 model= file-header variable from which it can be directly computed 102 diag= file-header diagnostics from which it can be computed 103 """ 104 def __init__( self, name, fheader, model, diag): 105 self.name= None 106 self.fheader= None 107 self.model= None 108 self.diag= None 109 if name is not None: 110 self.name = name 111 self.fheader= fheader 112 self.model= model 113 self.diag= diag 114 115 def variable_compute(idir,var,ftests,db): 116 """ Function to retrieve the computation way of a given variable using a series of test files 117 iwdir= directory with the test files 118 var= name of the variable to test 119 filtests= dictionary with the test files for each header 120 """ 121 fname='variable_compute' 122 123 cancompute = True 124 for headerf in ftests.keys(): 125 filen=ftests[headerf] 126 vmod, vdiag = ncvar.computevar_model(var, idir + '/' + filen) 127 128 if vmod is None and vdiag is None: 129 cancompute = False 130 else: 131 cancompute = True 132 # Should be considered that variable can also be computed by both ways? 133 break 134 135 if not cancompute: 136 print warnmsg 137 print ' ' + fname + ": there is no way to compute '" + var + \ 138 "' for model '" + mod 139 # Too extrict! 140 # quit(-1) 141 142 # Getting only the first possibility of diagnostic 143 if vdiag is not None: 144 varcomp = VariableInf(var, headerf, vmod, vdiag[0]) 145 else: 146 varcomp = VariableInf(var, headerf, vmod, vdiag) 147 148 # a ';' list 'varcompute' it is created for each variable giving: 149 # [var]|[vark]|[headerf][varmod]|[vardiag] 150 # This list will be used to compute a new file for each variable 151 152 if db: 153 print 'Variable information _______' 154 gen.printing_class(varcomp) 155 156 return varcomp 157 158 def get_operations_var(vc,db): 159 """ Function to provide the operations to make for each variable from 'VAR_' dictionary 160 vc= 'VAR_' dictionary, dictionary with all the parameters started with 'VAR_' 161 """ 162 fname = 'get_operations_var' 163 164 LH = len('VAR_') 165 iop = 1 166 # list of operations 167 doopers = {} 168 # operations by variable 169 operationsvar = {} 170 # individual operations by variable (not repeated) 171 indivoperationsvar = {} 172 173 # Variables with a calculation whic requires vertical interpolation to all the 174 # file (operations stating by 'VAR_pinterp') 175 LVp = len('VAR_pinterp') 176 varglobalp = [] 177 178 for oper in vc.keys(): 179 Loper = len(oper) 180 opn = oper[LH:Loper+1] 181 vns = vc[oper].split(':') 182 ops1 = opn.split('+') 183 doopers[opn] = ops1 184 for vn in vns: 185 if not operationsvar.has_key(vn): 186 operationsvar[vn] = [opn] 187 indivoperationsvar[vn] = ops1 188 else: 189 opers = operationsvar[vn] 190 opers.append(opn) 191 operationsvar[vn] = opers 192 opers1 = indivoperationsvar[vn] 193 for op1 in ops1: 194 if not gen.searchInlist(opers1, op1): 195 opers1.append(op1) 196 indivoperationsvar[vn] = opers1 197 198 if oper[0:LVp] == 'VAR_pinterp': 199 for vn in vns: 200 if not gen.searchInlist(varglobalp,vn): varglobalp.append(vn) 201 202 iop = iop + 1 203 204 if db: 205 print ' operations to make _______' 206 gen.printing_dictionary(doopers) 207 print ' operations by variable _______' 208 gen.printing_dictionary(operationsvar) 209 print ' individual operations by variable _______' 210 gen.printing_dictionary(indivoperationsvar) 211 print ' variables with the vertical interpolation for all the file _______' 212 print ' #', varglobalp 213 214 return doopers, operationsvar, indivoperationsvar, varglobalp 215 216 def pinterp_var(oper): 217 """ Function to retrieve characteristics of the vertical interpolation for the operation 218 oper= operation 219 Wether vertical interpolation is: 220 # 'global': for all file 221 # 'local': at a given step of the process 222 # 'none': no vertical interpolation 223 >>> pinterp_var('last+pinterp+xmean') 224 local 225 >>> pinterp_var('pinterp+tmean+xmean') 226 global 227 >>> pinterp_var('xmean') 228 none 229 """ 230 fname = 'pinterp_var' 231 232 pinn = 'pinterp' 233 234 Lpinterp = len(pinn) 235 if oper[0:Lpinterp] == pinn: 236 pkind = 'global' 237 return pkind 238 239 if oper.find(pinn) != -1: 240 pkind = 'local' 241 else: 242 pkind = 'none' 243 244 return pkind 245 246 def compvars_listconstruct(config, minf, Files, TestFiles, idir, odir, debug): 247 """ Function to construct the list of variables to compute 248 config= dictionary with the configuration of the execution 249 variables to compute are get with all values started by `VAR_' in 'module_graphics.dat', and then 250 providing a consecutive number of calculations separated by '+' 251 VAR_[calc1]+[calc2] = tas:wss 252 will compute first [calc1] and ten [calc2] for 'tas' and 'wss' 253 minf= class with information about the model 254 Files= dictionary of files for heach header 255 TestFiles= dictionary of files to test how to compute the variable 256 odir= output directory 257 """ 258 fname='compvars_listconstruct' 259 260 varcomp = gen.get_specdictionary_HMT(config,H='VAR_') 261 262 if debug: 263 print ' variables to compute ________' 264 gen.printing_dictionary(varcomp) 265 266 # Getting operations by variable 267 opers, varoper, indivaroper, varglobp = get_operations_var(varcomp,debug) 268 269 strfiles = gen.dictKeysVals_stringList(Files) 270 strtestfiles = gen.dictKeysVals_stringList(TestFiles) 271 272 Svarcompute = '' 273 274 # Main dictionary with all the variables and their calculation 275 allvarcomp = {} 276 277 ivop = 0 278 for vn in varoper: 279 vcomp = variable_compute(idir,vn,TestFiles,False) 280 for op in varoper[vn]: 281 # Creation of a String as: [CFname]|[operation]|[fheader]|[vmodel]|[vdiag]|[globalP] 282 # [CFname]: CF-name of the variable (must appear in 'variables_values.dat') 283 # [operation]: operation to compute 284 # [fheader]: header of file with the required variables 285 # [vmodel]: 286 # [vdiag]: 287 # [globalP]: Wether vertical interpolation is: 288 # 'global': for all file 289 # 'local': at a given step of the process 290 # 'none': no vertical interpolation 291 globalP = pinterp_var(op) 292 if vcomp.model is not None: 293 Smodel = ':'.join(vcomp.model) 294 else: 295 Smodel = 'None' 296 if vcomp.diag is not None: 297 Sdiag = ':'.join(vcomp.diag) 298 else: 299 Sdiag = 'None' 300 301 Svc = vcomp.name + '|' + op + '|' + vcomp.fheader + '|' + Smodel + '|' + \ 302 Sdiag + '|' + globalP 303 if ivop == 0: 304 Svarcompute = Svc 305 else: 306 Svarcompute = Svarcompute + ',' + Svc 307 308 allvarcomp[vn + '_' + op] = [vcomp.fheader, vcomp.model, vcomp.diag, globalP] 309 ivop = ivop + 1 310 311 if debug: 312 print ' Variables to compute _______' 313 gen.printing_dictionary(allvarcomp) 314 315 # Outwritting the varcompute to avoid next time (if it is not filescratch!) 316 objf = open(owdir + '/varcompute.inf', 'w') 317 objf.write('files: ' + strfiles + '\n') 318 objf.write('testfiles: ' + strtestfiles + '\n') 319 objf.write('varcompute: ' + Svarcompute + '\n') 320 objf.write('itotv: ' + str(ivop) + '\n') 321 objf.close() 322 323 return allvarcomp, ivop 324 325 def read_varcomp_file(filen): 326 """ Function to read 'varcompute.inf' and reconstruct the dictionaries 327 """ 328 fname = 'read_varcomp_file' 329 330 allvarcomp = {} 331 332 objf = open(filen, 'r') 333 334 for line in objf: 335 vals = line.split(' ') 336 if vals[0] == 'files': 337 Files = stringList_dictKeysVals(vals[1]) 338 elif vals[0] == 'testfiles': 339 TestFiles = stringList_dictKeysVals(vals[1]) 340 elif vals[0] == 'varcompute': 341 Vvals = vals[1].split('|') 342 if Vvals[3] == 'None': 343 mod = None 344 else: 345 mod = Vvals[3].split(':') 346 if Vvals[4] == 'None': 347 diag = None 348 else: 349 diag = Vvals[4].split(':') 350 351 allvarcomp[Vvals[0]+'_'+Vvals[1]] = [Vvals[2], mod, diag, Vvals[5]] 352 elif vals[0] == 'itotv': 353 ivop = int(vals[1]) 354 355 objf.close() 356 357 return Files, TestFiles, allvarcomp, ivop 358 359 def function compute_variable(minf, idir, usefiles, odir, cvar, gP, db): 360 """ Function to compute a variable 361 minf= class with the information of the model 362 idir= directory with the input files 363 usefiles= dictionary of files as dict([headerf]) = [file1], ..., [fileN] 364 odir= directory to write the output files 365 cvar= class with the 'name', 'fheader', 'varmod', 'vardiag' 366 gP= kind of vertical interpolation ('global', 'local', 'none') 367 """ 368 fname='compute_variable' 369 370 CFvarn=cvar.name 371 headerf = cvar.fheader 372 modvar = cvar.model 373 diagvar = cvar.diag 374 375 cfiles = usefiles[headerf] 376 377 # dimensions 378 dnx = minf.dimxn 379 dny = minf.dimyn 380 # var-dimensions 381 vdnx = minf.vardxn 382 vdny = minf.vardyn 383 384 # Computing separately and then joinging for all files 385 Ntotfiles = len(cfiles) 386 ifile=1 387 388 for cf in cfiles: 389 ifS = str(ifile) 390 391 # Computing variable 392 # Changing file head when it is a pressure-interpolated variable 393 if test ${vark} == 'pinterp'; then 394 fhead=${headerf}p 395 else 396 fhead=${headerf} 397 fi 398 399 filen=${odir}/${CFvarn}_${fhead}_${ifile}-${Ntotfiles}.nc 400 if ${scratch}; then 401 rm ${filen} >& /dev/null 402 rm ${odir}/${CFvarn}_${fhead}.nc >& /dev/null 403 fi 404 405 if test ! -f ${filen} && test ! -f ${odir}/${CFvarn}_${fhead}.nc; then 406 # Since model direct values are retrieved from `variables_valules.dat' which was initially coincived 407 # as a way to only give variable attributes, range and color bars, if a variable has a diagnostic 408 # way to be computed, the later one will be preferred 409 410 if test ! ${modvar} = 'None' && test ${diagvar} = 'None'; then 411 # model variable 412 values=${modvar}',0,-1,-1' 413 vs=${modvar},${vdnx},${vdny},${vdnz},${vdnt} 414 pyout=`${pyHOME}/nc_var.py -f ${cf} -o DataSetSection_multivars -v ${vs} \ 415 -S ${values} | grep succesfull | awk '{print $6}' | tr '"' ' '` 416 pyn=$? 417 Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'` 418 ferrmsg ${pyn} ${fname} "python!'DataSetSection_multivars'!failed"${Spyout} 419 mv ${pyout} ${filen} 420 421 # Keeping the operations 422 pyins=${pyHOME}"/nc_var.py -f "${cf}" -o DataSetSection_multivars -v "${vs} 423 pyins=${pyins}" -S "${values} 424 echo " " >> ${odir}/all_computevars.inf 425 echo "# ${CFvarn}" "${modvar}" >> ${odir}/all_computevars.inf 426 echo ${pyins} >> ${odir}/all_computevars.inf 427 428 pyout=`${pyHOME}/nc_var.py -f ${filen} -o chvarname -v ${modvar} -S ${CFvarn}` 429 pyn=$? 430 Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'` 431 ferrmsgF ${pyn} ${fname} "python!'chvarname'!failed"${Spyout} ${filen} 432 else 433 # diagnostic variable 434 dims=${dnt}@${vdnt},${dnz}@${vdnz},${dny}@${vdny},${dnx}@${vdnx} 435 diagn=`echo ${diagvar} | tr ':' '\n' | head -n 1` 436 Ndiagvars=`echo ${diagvar} | tr ':' ' ' | wc -w | awk '{print $1}'` 437 idiagv=2 438 diagc='' 439 while test ${idiagv} -le ${Ndiagvars}; do 440 diag1=`echo ${diagvar} | tr ':' '\n' | head -n ${idiagv} | tail -n 1` 441 if test ${idiagv} -eq 2; then 442 diagc=${diag1} 443 else 444 diagc=${diagc}'@'${diag1} 445 fi 446 idiagv=`expr ${idiagv} + 1` 447 done 448 pyout=`python ${pyHOME}/diagnostics.py -f ${cf} -d ${dims} -v ${diagn}'|'${diagc}` 449 pyn=$? 450 Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'` 451 ferrmsg ${pyn} ${fname} "python!'diagnostics'!failed#"${Spyout} 452 mv diagnostics.nc ${filen} 453 454 # Keeping the operations 455 pyins=${pyHOME}"/diagnostics.py -f "${cf}" -d "${dims}" -v '"${diagn}"|" 456 pyins=${pyins}${diagc}"'" 457 echo " " >> ${odir}/all_computevars.inf 458 echo "# ${CFvarn}" "${diagvar}" >> ${odir}/all_computevars.inf 459 echo ${pyins} >> ${odir}/all_computevars.inf 460 fi 461 462 # adding CF lon,lat,time in WRF files 463 if test ${headerf:0:3} = 'wrf'; then 464 WRF_toCF ${filen} ${vdnx} ${vdny} 465 fi 466 # Attaching necessary variables for the pressure interpolation 467 if test ${vark} == 'pinterp'; then 468 requiredinterpvars='P:PB:PSFC:PH:PHB:HGT:T:QVAPOR:' 469 rqvs=`echo ${requiredinterpvars} | tr ':' ' '` 470 echo " "${fname}": adding variables: "${rqvs}" to allow pressure interpolation" 471 for rqv in ${rqvs}; do 472 pyout=`${pyHOME}/nc_var.py -o fvaradd -S ${cf},${rqv} -f ${filen}` 473 pyn=$? 474 Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'` 475 ferrmsgF ${pyn} ${fname} "python!'fvaradd'!failed#"${Spyout} ${filen} 476 done 477 fi 478 fi 479 480 ifile=`expr ${ifile} + 1` 481 # End of files 482 done 483 484 # Joining variable files 485 filen=${odir}/${CFvarn}_${fhead}.nc 486 if ${scratch}; then rm ${filen} >& /dev/null; fi 487 488 if test ! -f ${filen}; then 489 pyout=`python ${pyHOME}/nc_var.py -f ${CFvarn}'_'${fhead}'_,-,.nc' \ 490 -o netcdf_fold_concatenation_HMT -S ./,time -v all` 491 pyn=$? 492 Spyout=`echo ${pyout} | tr '\n' '#' | tr ' ' '!'` 493 ferrmsg ${pyn} ${fname} "python!'netcdf_fold_concatenation_HMT'!failed#"${Spyout} 494 mv netcdf_fold_concatenated_HMT.nc ${filen} 495 if test -f ${filen}; then 496 rm ${CFvarn}_${fhead}_*-*.nc 497 fi 498 fi 499 } 500 501 502 def compute_vars(config, Files, allvarcomp, debug): 503 """ Function to compute the variables 504 config= Configuration of the experiment 505 Files= dictionary with the header and the files' correspondence 506 allvarcomp= dictionary with all the variables to compute and their information 507 """ 508 fname = 'compute_vars' 509 510 for vopn in allvarcomp: 511 # variable & operation 512 vn = vopn.split('_')[0] 513 op = vopn.split('_')[1] 514 vopnV = allvarcomp[vopn] 515 fheader = vopnV[0] 516 model = vopnV[1] 517 diag = vopnV[2] 518 globalP = vopnV[3] 519 520 if debug: 521 print ' ' + vn + ': ' + op 522 523 # Computing CF variable 524 if model is not None and diag is not None: 525 vinf = VariableInf(vn,fheader,model,diag) 526 compute_variable ${Iwdir} ${Files} ${Owdir} ${cvar} ${Moddims} ${Modvdims} ${Filescratch} 527 else: 528 print errmsg 529 print ' ' + fname + ": neither 'model' or 'diag' variables for '" + vn \ 530 + "' !!" 531 quit(-1) 532 533 return 534 535 536 97 537 # Files with information about the configuration of the script 98 538 inffiles = ['varcompute.inf', 'all_computevars.inf', 'all_statsvars.inf'] … … 110 550 # Getting models 111 551 mods = cnf['models'].split(':') 112 113 # Getting kinds of variables114 varks = cnf['varkinds'].split(':')115 552 116 553 # Models loop … … 140 577 modvdims = vdnx + ',' + vdny + ',' + vdnz + ',' + vdnt 141 578 579 # Experiments loop 580 ## 142 581 for exp in exps: 143 582 print ' ' + exp + '...' … … 201 640 gen.printing_dictionary(testfiles) 202 641 642 allcompvar, Nvar = compvars_listconstruct(cnf, modinf, files, testfiles, \ 643 iwdir, owdir, dbg) 644 else: 645 print warnmsg 646 print ' ' + main + ": getting variables to compute already from file !!" 647 files, testfiles, allcompvar, Nvar = read_varcomp_file(owdir + \ 648 '/varcompute.inf') 649 650 print " For experiment '"+exp+"' is required to compute:", Nvar, "variables" 651 652 ### 653 # Computing variables 654 ### 655 print " Computing variables ..." 656 compute_vars(conf, files, allcompvar, dbg) 657 658 # End of avoiding to repeat all the experiment search 659 fi 660 203 661 204 662 # end of experiments loop
Note: See TracChangeset
for help on using the changeset viewer.