Changeset 2219 in lmdz_wrf
- Timestamp:
- Nov 7, 2018, 4:47:40 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var.py
r2197 r2219 64 64 ## e.g. # nc_var.py -o compute_slice2Dstats -S 'XLAT,-63.,19.,2.,HGT,250.,7000.,500.,Time|Times:west_east|XLONG:south_north|XLAT' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2 65 65 ## e.g. # nc_var.py -o same_deltasign -f wrfout_d01_1995-01-01_00:00:00 -S 'Time:0|south_north:60|west_east:-1' -v T2,Q2,XLAT,XLONG 66 ## e.g. # nc_var.py -o compute_slices_stats -S 'XLONG,-74.,-36.,4.;XLAT,-63.,19.,2.;HGT,250.,7000.,500.@Time|Times:west_east|XLONG:south_north|XLAT@Time' -f wrfout_d01_1995-01-01_00:00:00 -v T2,Q2 66 67 67 68 from optparse import OptionParser … … 91 92 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps 92 93 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps 94 # compute_slices_stats: Function to compute stats of variables of a file splitting variables by 95 # slices along sets of ranges for a series of variables 93 96 # compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables 94 97 # compute_tevolboxtraj: Function to compute tevolboxtraj: temporal evolution at a given point along a box following a trajectory … … 208 211 'chgtimestep', 'chvarname', 'cleaning_varsfile', 'compute_deaccum', \ 209 212 'compute_opersvarsfiles', \ 210 'compute_opervaralltime', 'compute_opervartimes', 'compute_slice2Dstats', \ 213 'compute_opervaralltime', 'compute_opervartimes', 'compute_slices_stats', \ 214 'compute_slice2Dstats', \ 211 215 'compute_tevolboxtraj', \ 212 216 'computevar_model', 'curve_section', 'DatesFiles', \ … … 348 352 elif oper == 'compute_opervartimes': 349 353 ncvar.compute_opervartimes(opts.values, opts.ncfile, opts.varname) 354 elif oper == 'compute_slices_stats': 355 ncvar.compute_slices_stats(opts.values, opts.ncfile, opts.varname) 350 356 elif oper == 'compute_slice2Dstats': 351 357 ncvar.compute_slice2Dstats(opts.values, opts.ncfile, opts.varname) -
trunk/tools/nc_var_tools.py
r2202 r2219 46 46 # compute_opervaralltime: Function to compute opervaralltime: operation of variable successible allover the time-steps 47 47 # compute_opervartimes: Function to compute opervartimes: operation of variable for a given sub-set of time-steps 48 # compute_slices_stats: Function to compute stats of variables of a file splitting variables by 49 # slices along sets of ranges for a series of variables 48 50 # compute_slice2Dstats: Function to compute stats of variables of a file following slices along 2 variables 49 51 # computevar_model: Function to provide the way to compute a CF-variable providing its name … … 26436 26438 #compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,Q2') 26437 26439 26440 def compute_slices_stats(values, ncfile, variable): 26441 """ Function to compute stats of variables of a file splitting variables by 26442 slices along sets of ranges for a series of variables 26443 values=[slicevarsinf]@[dimvars]@[slicestatsdim] 26444 [slicevarsinf] ';' separated list of [varn1],[minvar1],[maxvar1],[slcevar1]; 26445 ...;[varnN],[minvarN],[maxvarN],[slcevarN] 26446 varn[i]: name of the variable i 26447 minvar[i]: minimum value to start the slices for variable i 26448 maxvar[i]: maximum value to end the slices for variable i 26449 slcevar[i]: length of slices in variable i 26450 [dimvars]: ':' separated list of [dimn]|[vardimn] dimension name [dimn] and 26451 its associated dimension-variable to get its values to provide values for 26452 the final file 26453 [slicestatsdim]: ',' list of name of dimensions to do not use for computing 26454 statistics of variables at each slice ('any' for not leaving any 26455 dimension out, resulting on scalar statistics) 26456 ncfile= netCDF file to use 26457 variable: ',' list of variables ('all' for all variables) 26458 """ 26459 fname = 'compute_slices_stats' 26460 26461 if values == 'h': 26462 print fname + '_____________________________________________________________' 26463 print compute_slices_stats.__doc__ 26464 quit() 26465 26466 expectargs = '[slicevarsinf]@[dimvars]@[slicestatsdim]' 26467 gen.check_arguments(fname, values, expectargs, '@') 26468 26469 varvalues0 = values.split('@')[0] 26470 dimvars0 = values.split('@')[1] 26471 slicestatsdim = values.split('@')[2] 26472 26473 varvalues = varvalues0.split(';') 26474 26475 varvals = {} 26476 slcvarns = [] 26477 for varvls in varvalues: 26478 expectargs = '[varni],[minvari],[maxvari],[slcevari]' 26479 gen.check_arguments(fname, varvls, expectargs, ',') 26480 26481 varni = varvls.split(',')[0] 26482 minvari = np.float(varvls.split(',')[1]) 26483 maxvari = np.float(varvls.split(',')[2]) 26484 slicevari = np.float(varvls.split(',')[3]) 26485 varvals[varni] = [minvari, maxvari, slicevari] 26486 slcvarns.append(varni) 26487 Nslcvars = len(slcvarns) 26488 26489 dimvars = gen.stringS_dictvar(dimvars0, Dc=':', DVs='|') 26490 26491 onc = NetCDFFile(ncfile, 'r') 26492 26493 # Varibales to slice 26494 if variable == 'all': 26495 varns = onc.variables.keys() 26496 else: 26497 varns = gen.str_list(variable, ',') 26498 26499 # Getting dimensions 26500 for dn in dimvars.keys(): 26501 if not gen.searchInlist(onc.dimensions,dn): 26502 print errormsg 26503 print ' ' +fname+ ": file '" + ncfile + "' does not have dimension: '"+ \ 26504 dn + "' !!" 26505 dimns = list(onc.dimensions) 26506 dimns.sort() 26507 print ' available ones:', dimns 26508 quit(-1) 26509 26510 slcvarnS = '' 26511 slicesinf = {} 26512 # Preparing slices and output file 26513 for varn in slcvarns: 26514 if not onc.variables.has_key(varn): 26515 print errormsg 26516 print ' ' + fname + ": file '" + ncfile + "' does not have variable " + \ 26517 "i: '" + varn + "' !!" 26518 varns = onc.variables.keys() 26519 varns.sort() 26520 print ' available ones:', varns 26521 quit(-1) 26522 26523 ovar = onc.variables[varn] 26524 vu = ovar.units 26525 dimnv = list(ovar.dimensions) 26526 26527 vvls = varvals[varn] 26528 minvar = vvls[0] 26529 maxvar = vvls[1] 26530 slicevar = vvls[2] 26531 26532 dvar = (maxvar-minvar+slicevar)/slicevar 26533 slices = np.arange(minvar, maxvar+slicevar, slicevar) 26534 Nslices = slices.shape[0] 26535 26536 print ' ' + fname + ': slices ____' 26537 print ' var:', varn, ':', Nslices, '(', minvar, ',', maxvar+slicevar, \ 26538 ',', slicevar, ')' 26539 26540 # Slices var 26541 sliceshape = [Nslices] + list(ovar.shape) 26542 slcvar = np.zeros(tuple(sliceshape), dtype=bool) 26543 slcvalsc = np.zeros((Nslices), dtype=np.float) 26544 slcvals = np.zeros((Nslices,2), dtype=np.float) 26545 varv = ovar[:] 26546 for islc in range(Nslices-1): 26547 slcvar[islc,] = ma.masked_inside(varv, slices[islc], slices[islc+1]).mask 26548 slcvalsc[islc] = (slices[islc+1]+slices[islc+1])/2. 26549 slcvals[islc,0] = slices[islc] 26550 slcvals[islc,1] = slices[islc+1] 26551 26552 slicesinf[varn] = [Nslices, dimnv, slcvar] 26553 26554 if varn == slcvarns[0]: 26555 onewnc = NetCDFFile(fname + '.nc', 'w') 26556 newdim = onewnc.createDimension('slice_bnds', 2) 26557 slcvarnS = varn 26558 else: 26559 if varn == slcvarns[Nslcvars-1]: slcvarnS = slcvarnS + ' & ' + varn 26560 else: slcvarnS = slcvarnS + ', ' + varn 26561 26562 # dimensions 26563 newdim = onewnc.createDimension('slice_'+varn, Nslices) 26564 26565 # variable dimensions 26566 for dn in dimnv: 26567 if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn]) 26568 26569 newvar = onewnc.createVariable('slice_'+varn, 'f', ('slice_'+varn)) 26570 newvar[:] = slcvalsc[:] 26571 basicvardef(newvar, 'slice_'+varn, 'slices for variable ' + varn, vu) 26572 26573 newvar = onewnc.createVariable('slice_'+varn+'_bnds', 'f', ('slice_'+varn, \ 26574 'slice_bnds')) 26575 newvar[:] = slcvals[:] 26576 basicvardef(newvar, 'slice_'+varn+'_bnds', 'boundaries of slices for ' + \ 26577 'variable ' + varn, vu) 26578 26579 slcdims = ['slice_'+varn] + dimnv 26580 slcshape = [Nslices] + list(ovar.shape) 26581 slcv = np.zeros(tuple(slcshape), dtype=int) 26582 for islc in range(Nslices): 26583 vvslc = slcvar[islc,] 26584 slcv[islc,] = np.where(vvslc, 1, 0) 26585 newvar = onewnc.createVariable(varn+'sliced', 'i', tuple(slcdims)) 26586 newvar[:] = slcv[:] 26587 basicvardef(newvar, varn+'sliced', 'sliced variable ' + varn, vu) 26588 26589 for varn in varns: 26590 print ' ' + varn + ' ...' 26591 ovar = onc.variables[varn] 26592 varv = ovar[:] 26593 vdimns = list(ovar.dimensions) 26594 vshape = list(ovar.shape) 26595 vu = ovar.units 26596 26597 for dn in vdimns: 26598 if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn]) 26599 26600 # mask in var slice 26601 maskvarslcs = {} 26602 allNslices = [] 26603 slicedimn = [] 26604 for vslcn in slcvarns: 26605 varslcv = slicesinf[vslcn] 26606 Nslices = varslcv[0] 26607 ovardims = varslcv[1] 26608 slcvar = varslcv[2] 26609 26610 NOTcoinc = list(set(ovardims) - set(vdimns)) 26611 if len(NOTcoinc) > 0: 26612 notcoincdim = {} 26613 for dn in NOTcoinc: notcoincdim[dn] = 0 26614 else: 26615 notcoincdim = None 26616 maskvarslice = [] 26617 for islc in range(Nslices): 26618 varmask = gen.mat_dimreshape(list(ovardims), slcvar[islc,], vdimns, \ 26619 vshape, notcoincdim) 26620 maskvarslice.append(varmask) 26621 26622 maskvarslcs[vslcn] = maskvarslice 26623 allNslices.append(Nslices) 26624 slicedimn.append('slice_'+vslcn) 26625 26626 newvarshape = allNslices + vshape 26627 print ' Shape final sliced variable:', newvarshape 26628 26629 # variables 26630 26631 # This can get quite huge !! 26632 #newvard = slicedimn + vdimns 26633 #newvar = onewnc.createVariable(varn+'sliced', 'f', tuple(newvard), \ 26634 # fill_value=gen.fillValueF) 26635 #basicvardef(newvar, varn+'sliced', varn + 'sliced by '+ slcvarnS, vu) 26636 #add_varattrs(onc, onewnc, [varn], [varn+'sliced']) 26637 26638 masknodims = [] 26639 vrank = np.arange(len(vdimns)) 26640 if slicestatsdim == 'any': 26641 slcmin = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 26642 slcmax = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 26643 slcmean = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 26644 slcstd= np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 26645 newvard = slicedimn 26646 else: 26647 nodims = gen.str_list(slicestatsdim, ',') 26648 stvardims = vdimns + [] 26649 stvdn = [] 26650 stvid = [] 26651 idd = 0 26652 vrank = list(vrank) 26653 for nod in nodims: 26654 if not gen.searchInlist(onc.dimensions, nod): 26655 print errormsg 26656 print ' ' +fname+ ": file '" + ncfile + "' does not have " + \ 26657 "dimension: '" + dn + "' !!" 26658 dimns = list(onc.dimensions) 26659 dimns.sort() 26660 print ' available ones:', dimns 26661 quit(-1) 26662 26663 if gen.searchInlist(vdimns, nod): 26664 stvdn.append(nod) 26665 stvid.append(vshape[idd]) 26666 stvardims.remove(nod) 26667 vrank.pop(idd) 26668 masknodims.append(slice(0,vshape[idd])) 26669 idd = idd + 1 26670 vrank = np.array(vrank) 26671 26672 slcmin = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 26673 slcmax = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 26674 slcmean = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 26675 slcstd= np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 26676 newvard = slicedimn + stvdn 26677 26678 newvarN = onewnc.createVariable(varn+'Nsliced', 'f', tuple(newvard), \ 26679 fill_value=gen.fillValueF) 26680 basicvardef(newvarN, varn+'Nsliced','number of values in slice of ' + varn + \ 26681 'sliced by '+ slcvarnS, vu) 26682 add_varattrs(onc, onewnc, [varn], [varn+'Nsliced']) 26683 26684 newvarn = onewnc.createVariable(varn+'minsliced', 'f', tuple(newvard), \ 26685 fill_value=gen.fillValueF) 26686 basicvardef(newvarn, varn+'minsliced', 'minimum value of '+varn+'sliced by '+\ 26687 slcvarnS, vu) 26688 add_varattrs(onc, onewnc, [varn], [varn+'minsliced']) 26689 26690 newvarx = onewnc.createVariable(varn+'maxsliced', 'f', tuple(newvard), \ 26691 fill_value=gen.fillValueF) 26692 basicvardef(newvarx, varn+'maxsliced', 'maximum value of '+varn+'sliced by '+\ 26693 slcvarnS, vu) 26694 add_varattrs(onc, onewnc, [varn], [varn+'maxsliced']) 26695 26696 newvarm = onewnc.createVariable(varn+'meansliced', 'f', tuple(newvard), \ 26697 fill_value=gen.fillValueF) 26698 basicvardef(newvarm, varn+'meansliced', 'mean value of '+varn+'sliced by ' + \ 26699 slcvarnS, vu) 26700 add_varattrs(onc, onewnc, [varn], [varn+'meansliced']) 26701 26702 newvars = onewnc.createVariable(varn+'stdsliced', 'f', tuple(newvard), \ 26703 fill_value=gen.fillValueF) 26704 basicvardef(newvars, varn+'stdsliced', 'standard deviation of '+varn+ \ 26705 'sliced by ' + slcvarnS, vu) 26706 add_varattrs(onc, onewnc, [varn], [varn+'stdsliced']) 26707 26708 # Obviously we get memory problems... proceed slide by slide to fill the file 26709 #newvarNmasked = np.ones(tuple(newvarshape), dtype=ovar.dtype)*gen.fillValueF 26710 newvarNmasked = np.ones(tuple(vshape), dtype=ovar.dtype)*gen.fillValueF 26711 26712 # Masking! 26713 allslices = gen.all_consecutive_combs(allNslices) 26714 Nallslices = len(allslices) 26715 26716 for iallslc in range(Nallslices): 26717 islcN = allslices[iallslc] 26718 slcnewvar = [] 26719 iv = 0 26720 for vslcn in slcvarns: 26721 masksv = maskvarslcs[vslcn] 26722 slcnewvar.append(islcN[iv]) 26723 if iv == 0: newmask = masksv[islcN[iv]] 26724 else: newmask = newmask*masksv[islcN[iv]] 26725 iv = iv + 1 26726 26727 for iid in vshape: 26728 slcnewvar.append(slice(0,iid)) 26729 26730 mavals = ma.array(varv, mask=~newmask) 26731 #newvarNmasked[tuple(slcnewvar)] = mavals.filled(gen.fillValueF) 26732 newvarNmasked[:] = mavals.filled(gen.fillValueF) 26733 26734 # This can get quite huge !!! 26735 #newvar[tuple(slcnewvar)] = newvarNmasked[:] 26736 26737 manewvar = ma.masked_equal(newvarNmasked, gen.fillValueF) 26738 26739 if len(masknodims) > 0: 26740 islcst = list(islcN) + list(masknodims) 26741 if iallslc == 0: 26742 print ' variable slice statisitcs not using:', nodims 26743 print ' size statistics variables:', allNslices+stvid 26744 print ' slice statistics variables:', islcst 26745 print ' running shapes of the variable:', vrank 26746 else: islcst = islcN 26747 26748 newvarN[tuple(islcst)]=np.sum(~manewvar.mask, axis=tuple(vrank)) 26749 26750 slcmin[tuple(islcst)] = np.min(manewvar, axis=tuple(vrank)) 26751 slcmax[tuple(islcst)] = np.max(manewvar, axis=tuple(vrank)) 26752 slcmean[tuple(islcst)] = np.mean(manewvar, axis=tuple(vrank)) 26753 slcstd[tuple(islcst)] = np.std(manewvar, axis=tuple(vrank)) 26754 26755 newvarn[:] = slcmin 26756 newvarx[:] = slcmax 26757 newvarm[:] = slcmean 26758 newvars[:] = slcstd 26759 26760 onewnc.sync() 26761 # Adding dimension-variables 26762 for dn in vdimns: 26763 if not dimvars.has_key(dn): 26764 print errormsg 26765 print ' ' + fname + ": no dimension-variable provided for " + \ 26766 "dimension '" + dn + "' !!" 26767 print ' provided ones _______' 26768 gen.printing_dictionary(dimvars) 26769 quit(-1) 26770 if not onewnc.variables.has_key(dimvars[dn]): 26771 add_vars(onc, onewnc, [dimvars[dn]]) 26772 onewnc.sync() 26773 26774 # Add global attributes 26775 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 26776 add_globattrs(onc,onewnc,'all') 26777 onewnc.sync() 26778 onc.close() 26779 onewnc.close() 26780 print fname + ": successfull written of file '" + fname + ".nc' !!" 26781 26782 return 26783 26784 #values='XLONG,-74.,-36.,4.;XLAT,-63.,19.,4.;HGT,250.,7000.,500.@Time|Times:west_east|XLONG:south_north|XLAT@Time' 26785 #compute_slices_stats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2,Q2') 26786 26438 26787 def same_deltasign(values, ncfile, variable): 26439 26788 """ Function to determine if a given series of 1D values share the same sign of
Note: See TracChangeset
for help on using the changeset viewer.