Changeset 2280 in lmdz_wrf for trunk/tools
- Timestamp:
- Jan 22, 2019, 7:49:47 PM (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/nc_var_tools.py
r2275 r2280 27690 27690 #area_weighted('yes:min,max,mean,stddev',fvals,'ct_values') 27691 27691 27692 def compute_slices_stats_areaweighted(values, ncfile, variable): 27693 """ Function to compute stats of variables of a file splitting variables by 27694 slices along sets of ranges for a series of variables weighting by the area 27695 covered by each grid (defined as polygon) within the slice 27696 values=[slicevarsinf]@[dimvars]@[slicestatsdim] 27697 [slicevarsinf] ';' separated list of [varn1],[minvar1],[maxvar1], 27698 [slcevar1]; ...;[varnN],[minvarN],[maxvarN],[slcevarN] 27699 varn[i]: name of the variable i 27700 minvar[i]: minimum value to start the slices for variable i 27701 maxvar[i]: maximum value to end the slices for variable i 27702 slcevar[i]: length of slices in variable i 27703 NOTE: slices will be: 27704 [minvar[i]-slicevar[i]/2., minvar[i]+slicevar[i]/2.), ..., 27705 [maxvar[i]-slicevar[i]/2., maxvar[i]+slicevar[i]/2.) 27706 [dimvars]: ':' separated list of [dimn]|[vardimn] dimension name [dimn] and 27707 its associated dimension-variable to get its values to provide values for 27708 the final file. NOTE: vardimn must have 'bounds' attribute in order to 27709 be able to compute the area-weights or it will be assumed that dimension 27710 has no extension 27711 [slicestatsdim]: ',' list of name of dimensions to do not use for computing 27712 statistics of variables at each slice ('any' for not leaving any 27713 dimension out, resulting on scalar statistics) 27714 ncfile= netCDF file to use 27715 variable: ',' list of variables ('all' for all variables) 27716 """ 27717 fname = 'compute_slices_stats_areaweighted' 27718 27719 if values == 'h': 27720 print fname + '_____________________________________________________________' 27721 print compute_slices_stats.__doc__ 27722 quit() 27723 27724 expectargs = '[slicevarsinf]@[dimvars]@[slicestatsdim]' 27725 gen.check_arguments(fname, values, expectargs, '@') 27726 27727 varvalues0 = values.split('@')[0] 27728 dimvars0 = values.split('@')[1] 27729 slicestatsdim = values.split('@')[2] 27730 27731 varvalues = varvalues0.split(';') 27732 27733 varvals = {} 27734 slcvarns = [] 27735 for varvls in varvalues: 27736 expectargs = '[varni],[minvari],[maxvari],[slcevari]' 27737 gen.check_arguments(fname, varvls, expectargs, ',') 27738 27739 varni = varvls.split(',')[0] 27740 minvari = np.float(varvls.split(',')[1]) 27741 maxvari = np.float(varvls.split(',')[2]) 27742 slicevari = np.float(varvls.split(',')[3]) 27743 varvals[varni] = [minvari, maxvari, slicevari] 27744 slcvarns.append(varni) 27745 Nslcvars = len(slcvarns) 27746 27747 dimvars = gen.stringS_dictvar(dimvars0, Dc=':', DVs='|') 27748 27749 onc = NetCDFFile(ncfile, 'r') 27750 27751 # Varibales to slice 27752 if variable == 'all': 27753 varns = onc.variables.keys() 27754 else: 27755 varns = gen.str_list(variable, ',') 27756 27757 # Getting dimensions 27758 for dn in dimvars.keys(): 27759 if not gen.searchInlist(onc.dimensions,dn): 27760 print errormsg 27761 print ' ' +fname+ ": file '" + ncfile + "' does not have dimension: '"+ \ 27762 dn + "' !!" 27763 dimns = list(onc.dimensions) 27764 dimns.sort() 27765 print ' available ones:', dimns 27766 quit(-1) 27767 27768 slcvarnS = '' 27769 slicesinf = {} 27770 # Dictionary with the dimension-variables with bounds 27771 vardimbndsvars = {} 27772 # Dictionary with the result of slicing a dimension-variable with bounds 27773 vardimbndslice = {} 27774 27775 # Preparing slices and output file 27776 for varn in slcvarns: 27777 if not onc.variables.has_key(varn): 27778 print errormsg 27779 print ' ' + fname + ": file '" + ncfile + "' does not have variable " + \ 27780 "i: '" + varn + "' !!" 27781 varns = onc.variables.keys() 27782 varns.sort() 27783 print ' available ones:', varns 27784 quit(-1) 27785 27786 ovar = onc.variables[varn] 27787 vu = ovar.units 27788 dimnv = list(ovar.dimensions) 27789 27790 vvls = varvals[varn] 27791 minvar = vvls[0] 27792 maxvar = vvls[1] 27793 slicevar = vvls[2] 27794 27795 dvar = (maxvar-minvar+slicevar)/slicevar 27796 slices = np.arange(minvar, maxvar+slicevar, slicevar) 27797 Nslices = slices.shape[0] 27798 27799 print ' ' + fname + ': slices ____' 27800 print ' var:', varn, ':', Nslices, '(', minvar, ',', maxvar+slicevar, \ 27801 ',', slicevar, ')' 27802 27803 # Slices var 27804 sliceshape = [Nslices] + list(ovar.shape) 27805 slcvar = np.zeros(tuple(sliceshape), dtype=bool) 27806 slcvalsc = np.zeros((Nslices), dtype=np.float) 27807 slcvals = np.zeros((Nslices,2), dtype=np.float) 27808 varv = ovar[:] 27809 for islc in range(Nslices): 27810 slcvalsc[islc] = slices[islc] 27811 slcvals[islc,0] = slices[islc]-slicevar/2. 27812 slcvals[islc,1] = slices[islc]+slicevar/2. 27813 27814 #slcvar[islc,] = ma.masked_inside(varv, slcvals[islc,0], slcvals[islc,1]).mask 27815 slcvar[islc,] = ma.masked_outside(varv, slcvals[islc,0], slcvals[islc,1]).mask 27816 27817 slicesinf[varn] = [Nslices, dimnv, slcvar, slcvalsc, slcvals] 27818 27819 if varn == slcvarns[0]: 27820 onewnc = NetCDFFile(fname + '.nc', 'w') 27821 newdim = onewnc.createDimension('slice_bnds', 2) 27822 slcvarnS = varn 27823 else: 27824 if varn == slcvarns[Nslcvars-1]: slcvarnS = slcvarnS + ' & ' + varn 27825 else: slcvarnS = slcvarnS + ', ' + varn 27826 27827 # dimensions 27828 newdim = onewnc.createDimension('slice_'+varn, Nslices) 27829 27830 # variable dimensions 27831 for dn in dimnv: 27832 if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn]) 27833 27834 newvar = onewnc.createVariable('slice_'+varn, 'f', ('slice_'+varn)) 27835 newvar[:] = slcvalsc[:] 27836 basicvardef(newvar, 'slice_'+varn, 'slices for variable ' + varn, vu) 27837 27838 newvar = onewnc.createVariable('slice_'+varn+'_bnds', 'f', ('slice_'+varn, \ 27839 'slice_bnds')) 27840 newvar[:] = slcvals[:] 27841 basicvardef(newvar, 'slice_'+varn+'_bnds', 'boundaries of slices for ' + \ 27842 'variable ' + varn, vu) 27843 27844 slcdims = ['slice_'+varn] + dimnv 27845 slcshape = [Nslices] + list(ovar.shape) 27846 slcv = np.zeros(tuple(slcshape), dtype=int) 27847 for islc in range(Nslices): 27848 vvslc = ~slcvar[islc,] 27849 slcv[islc,] = np.where(vvslc, 1, 0) 27850 newvar = onewnc.createVariable(varn+'sliced', 'i', tuple(slcdims)) 27851 newvar[:] = slcv[:] 27852 basicvardef(newvar, varn+'sliced', 'sliced variable ' + varn, vu) 27853 27854 onewnc.sync() 27855 27856 # Looking for boundaries 27857 varbnds = [] 27858 dn = varn 27859 vdarattrs = ovar.ncattrs() 27860 dv = ovar[:] 27861 27862 if gen.searchInlist(vdarattrs,'bounds'): 27863 boundsv = ovar.getncattr('bounds') 27864 print ' ' + infmsg 27865 print ' ' +fname+ ": slicing variable '" +dn+ "' with bounds !!" 27866 print ' bounds found:', boundsv 27867 print ' getting them to retrieve the slices' 27868 bndvarns = boundsv 27869 varbnds.append(dn) 27870 ovarbnds = onc.variables[boundsv] 27871 varbnds = ovarbnds[:] 27872 27873 # Slicing following boundaries, dimensions are 1D thus expand to 2D 27874 # filling as NW, NE, SE, SW 27875 if gen.searchInlist(slcvarns, dn) and not vardimbndslice.has_key(dn): 27876 print infmsg 27877 print ' ' + fname + ": slicing dimension '" + dn + "' ... " 27878 varslcv = slicesinf[dn] 27879 Nslices = varslcv[0] 27880 ovardims = varslcv[1] 27881 slcvar = varslcv[2] 27882 27883 if len(dv.shape) == 1: 27884 # Shapping 2D slices 27885 reflon = np.zeros((1,Nslices), dtype=np.float) 27886 reflon[0,:] = slcvalsc 27887 reflat = np.zeros((1,Nslices), dtype=np.float) 27888 xslice2D = np.zeros((4,1,Nslices), dtype=np.float) 27889 xslice2D[0,0,:] = slcvals[:,1] 27890 xslice2D[1,0,:] = slcvals[:,1] 27891 xslice2D[2,0,:] = slcvals[:,0] 27892 xslice2D[3,0,:] = slcvals[:,0] 27893 yslice2D = np.zeros((4,1,Nslices), dtype=np.float) 27894 yslice2D[0,0,:] = -1. 27895 yslice2D[1,0,:] = 1. 27896 yslice2D[2,0,:] = 1. 27897 yslice2D[3,0,:] = -1. 27898 27899 dd = len(onc.dimensions[dn]) 27900 getlon = np.zeros((1,dd), dtype=np.float) 27901 getlon[0,:] = dv 27902 getlat = np.zeros((1,dd), dtype=np.float) 27903 xdimvarbnds2D = np.zeros((4,1,dd), dtype=np.float) 27904 xdimvarbnds2D[0,0,:] = ovarbnds[:,1] 27905 xdimvarbnds2D[1,0,:] = ovarbnds[:,1] 27906 xdimvarbnds2D[2,0,:] = ovarbnds[:,0] 27907 xdimvarbnds2D[3,0,:] = ovarbnds[:,0] 27908 ydimvarbnds2D = np.zeros((4,1,dd), dtype=np.float) 27909 ydimvarbnds2D[0,0,:] = -1. 27910 ydimvarbnds2D[1,0,:] = 1. 27911 ydimvarbnds2D[2,0,:] = 1. 27912 ydimvarbnds2D[3,0,:] = -1. 27913 27914 dxref = Nslices 27915 dyref = 1 27916 dxget = dd 27917 dyget = 1 27918 27919 else: 27920 print errormsg 27921 print ' '+fname+": rank ", dv.shape, " of slicing varibale '" + \ 27922 dn + "' not ready !!" 27923 print ' function only works with 1D slicing variables' 27924 quit(-1) 27925 27926 reflont = reflon.transpose() 27927 reflatt = reflat.transpose() 27928 refvarxbndst = xslice2D.transpose() 27929 refvarybndst = yslice2D.transpose() 27930 getlont = getlon.transpose() 27931 getlatt = getlat.transpose() 27932 getvarxbndst = xdimvarbnds2D.transpose() 27933 getvarybndst = ydimvarbnds2D.transpose() 27934 27935 Ngridsint, gridsint, percenst = \ 27936 fsci.module_scientific.spacepercen(xcavals=reflont, \ 27937 ycavals=reflatt, xbavals=refvarxbndst, ybavals=refvarybndst, \ 27938 xcbvals=getlont, ycbvals=getlatt, xbbvals=getvarxbndst, \ 27939 ybbvals=getvarybndst, strict=False, dxa=dxref, dya=dyref, \ 27940 navertexmax=4, dxb=dxget, dyb=dyget, dxyb=dxget*dyget, \ 27941 nbvertexmax=4) 27942 27943 Ngridsin = Ngridsint.transpose() 27944 gridsin = gridsint.transpose() 27945 percens = percenst.transpose() 27946 27947 Ngridsinmax = np.max(Ngridsin) 27948 else: 27949 print infmsg 27950 print ' ' + fname + ": slicing variable '" + dn + "' without bounds !!" 27951 print ' directly using grid point values' 27952 27953 Ngridsin = np.zeros((1,Nslices), dtype=int) 27954 for islc in range(Nslices): 27955 Ngridsin[0,islc] = np.sum(~slcvar[islc,]) 27956 27957 Ngridsinmax = np.max(Ngridsin) 27958 gridsin = np.zeros((2,Ngridsinmax,1,Nslices), dtype=int) 27959 percens = np.zeros((Ngridsinmax,1,Nslices), dtype=np.float) 27960 for islc in range(Nslices): 27961 unmasked = slcvar[islc,] 27962 indices = gen.multi_index_mat(unmasked,False) 27963 ainds = np.array(indices) 27964 if Ngridsin[0,islc] > 0: 27965 gridsin[0,0:Ngridsin[0,islc],0,islc] = ainds[:,0] 27966 gridsin[1,0:Ngridsin[0,islc],0,islc] = ainds[:,1] 27967 percens[0:Ngridsin[0,islc],0,islc] = 1. 27968 27969 # Let's avoid memory issues... 27970 lvalues = [Ngridsin, gridsin, percens] 27971 vardimbndslice[dn] = lvalues 27972 27973 # Writting it to the file variables space-weight 27974 if not gen.searchInlist(onewnc.dimensions, dn+'slice'): 27975 newdim = onewnc.createDimension(dn+'slice',Nslices) 27976 newdim = onewnc.createDimension(dn+'bnds',4) 27977 newdim = onewnc.createDimension(dn+'gridin',Ngridsinmax) 27978 if not gen.searchInlist(onewnc.dimensions, 'coord'): 27979 newdim = onewnc.createDimension('coord',2) 27980 27981 newvar = onewnc.createVariable(dn+'Ngrid','i',(dn+'slice')) 27982 27983 newvar[:] = Ngridsin[:] 27984 basicvardef(newvar, dn+'Ngrid', "number of grids cells from " + \ 27985 ".get. laying within .ref.", '-') 27986 newvar.setncattr('coordinates',dn) 27987 27988 innewvar = onewnc.createVariable(dn+'gridin', 'i', ('coord', \ 27989 dn+'gridin',dn+'slice')) 27990 basicvardef(innewvar, dn+'gridin', "coordinates of the grids " + \ 27991 "cells from .get. laying within .ref.",'-') 27992 innewvar.setncattr('coordinates',dn+'slice') 27993 27994 pnewvar = onewnc.createVariable(dn+'gridpercen','f',(dn+'gridin', \ 27995 dn+'slice')) 27996 basicvardef(pnewvar ,dn+'gridpercen', "percentages of the " + \ 27997 "grids cells from .get. laying within .ref.", '1') 27998 pnewvar.setncattr('coordinates',dn) 27999 for j in range(1): 28000 for i in range(Nslices): 28001 innewvar[:,0:Ngridsin[j,i],i] = gridsin[:,0:Ngridsin[j,i],j,i] 28002 pnewvar[0:Ngridsin[j,i],i]= percens[0:Ngridsin[j,i],j,i] 28003 onewnc.sync() 28004 28005 for varn in varns: 28006 print ' ' + varn + ' ...' 28007 ovar = onc.variables[varn] 28008 varv = ovar[:] 28009 vdimns = list(ovar.dimensions) 28010 vshape = list(ovar.shape) 28011 vu = ovar.units 28012 28013 varbnds = [] 28014 for dn in vdimns: 28015 if not gen.searchInlist(onewnc.dimensions,dn): add_dims(onc, onewnc, [dn]) 28016 28017 odn = variables[dn] 28018 dv = odn[:] 28019 28020 # Looking for boundaries 28021 vdarattrs = odn.ncattrs() 28022 if vdarattrs.has_key('bounds'): 28023 print ' ' + infmsg 28024 print ' ' +fname+ ": dimension variable '" +dn+ "' with bounds !!" 28025 print ' bounds found:', varattrs['bounds'] 28026 print ' getting them to retrieve the slices' 28027 bndvarns = varattrs['bounds'] 28028 varbnds.append(dn) 28029 ovarbnds = onc.variables[varbnds] 28030 28031 # Slicing following boundaries, dimensions are 1D thus expand to 2D 28032 # filling as NW, NE, SE, SW 28033 if gen.searchInlist(slcvarns, dn) and not vardimbndslice.has_key(dn): 28034 print infmsg 28035 print ' ' + fname + ": slicing dimension '" + dn + "' ... " 28036 varslcv = slicesinf[dn] 28037 Nslices = varslcv[0] 28038 ovardims = varslcv[1] 28039 slcvar = varslcv[2] 28040 28041 # Shapping 2D slices 28042 reflat = np.zeros((Nslices), dtype=np.float) 28043 xslice2D = np.zeros((Nslices,4), dtype=np.float) 28044 xslice2D[:,0] = slcvar[1,:] 28045 xslice2D[:,1] = slcvar[1,:] 28046 xslice2D[:,2] = slcvar[0,:] 28047 xslice2D[:,3] = slcvar[0,:] 28048 yslice2D = np.zeros((Nslices,4), dtype=np.float) 28049 yslice2D[:,0] = -1. 28050 yslice2D[:,1] = 1. 28051 yslice2D[:,2] = 1. 28052 yslice2D[:,3] = -1. 28053 28054 dd = len(onc.dimensions[dn]) 28055 getlat = np.zeros((dd), dtype=np.float) 28056 xdimvarbnds2D = np.zeros((dd,4), dtype=np.float) 28057 xdimvarbnds2D[:,0] = ovarbnds[1,:] 28058 xdimvarbnds2D[:,1] = ovarbnds[1,:] 28059 xdimvarbnds2D[:,2] = ovarbnds[0,:] 28060 xdimvarbnds2D[:,3] = ovarbnds[0,:] 28061 ydimvarbnds2D = np.zeros((dd,4), dtype=np.float) 28062 ydimvarbnds2D[:,0] = -1. 28063 ydimvarbnds2D[:,1] = 1. 28064 ydimvarbnds2D[:,2] = 1. 28065 ydimvarbnds2D[:,3] = -1. 28066 28067 reflont = slcvar.transpose() 28068 reflatt = reflat.transpose() 28069 refvarxbndst = xslice2D.transpose() 28070 refvarybndst = yslice2D.transpose() 28071 getlont = dv.transpose() 28072 getlatt = getlat.transpose() 28073 getvarxbndst = xdimvarbnds2D.transpose() 28074 getvarybndst = ydimvarbnds2D.transpose() 28075 28076 Ngridsint, gridsint, percenst = \ 28077 fsci.module_scientific.spacepercen(xcavals=reflont, \ 28078 ycavals=reflatt, xbavals=refvarxbndst, ybavals=refvarybndst, \ 28079 xcbvals=getlont, ycbvals=getlatt, xbbvals=getvarxbndst, \ 28080 ybbvals=getvarybndst, strict=strict, dxa=Nslices, dya=1, \ 28081 navertexmax=4, dxb=dd, dyb=1, dxyb=dd, nbvertexmax=4) 28082 28083 Ngridsin = Ngridsint.transpose() 28084 gridsin = gridsint.transpose() 28085 percens = percenst.transpose() 28086 28087 Ngridsinmax = np.max(Ngridsin) 28088 28089 # Let's avoid memory issues... 28090 lvalues = [Ngridsin, gridsin, percens] 28091 vardimbndslice[dn] = lvalues 28092 28093 # Writting it to the file variables space-weight 28094 if not gen.searchInlist(onewnc.dimensions, dn+'bnds'): 28095 newdim = onewnc.createDimension(dn+'bnds',4) 28096 newdim = onewnc.createDimension(dn+'gridin',Ngridsinmax) 28097 if not gen.searchInlist(onewnc.dimensions, 'coord'): 28098 newdim = onewnc.createDimension('coord',2) 28099 28100 newvar = onewnc.createVariable(dn + 'Ngrid','i',(dn)) 28101 newvar[:] = Ngridsin[:] 28102 basicvardef(newvar, dn+'Ngrid', "number of grids cells from " + \ 28103 ".get. laying within .ref.", '-') 28104 newvar.setncattr('coordinates',dn) 28105 28106 innewvar = onewnc.createVariable(dn+'gridin', 'i', ('coord', \ 28107 'gridin',dn)) 28108 basicvardef(innewvar, dn+'gridin', "coordinates of the grids " + \ 28109 "cells from .get. laying within .ref.",'-') 28110 innewvar.setncattr('coordinates',dn) 28111 28112 pnewvar = onewnc.createVariable(dn+'gridpercen','f',('gridin',dn)) 28113 basicvardef(pnewvar ,dn+'gridpercen', "percentages of the " + \ 28114 "grids cells from .get. laying within .ref.", '1') 28115 pnewvar.setncattr('coordinates',dn) 28116 28117 for j in range(1): 28118 for i in range(dd): 28119 innewvar[:,0:Ngridsin[j,i],j,i] = \ 28120 gridsin[:,0:Ngridsin[j,i],j,i] 28121 pnewvar[0:Ngridsin[j,i],j,i]= percens[0:Ngridsin[j,i],j,i] 28122 onewnc.sync() 28123 28124 # mask in var slice 28125 maskvarslcs = {} 28126 allNslices = [] 28127 slicedimn = [] 28128 for vslcn in slcvarns: 28129 varslcv = slicesinf[vslcn] 28130 Nslices = varslcv[0] 28131 ovardims = varslcv[1] 28132 slcvar = varslcv[2] 28133 28134 NOTcoinc = list(set(ovardims) - set(vdimns)) 28135 if len(NOTcoinc) > 0: 28136 notcoincdim = {} 28137 for dn in NOTcoinc: notcoincdim[dn] = 0 28138 else: 28139 notcoincdim = None 28140 maskvarslice = [] 28141 for islc in range(Nslices): 28142 varmask = gen.mat_dimreshape(list(ovardims), slcvar[islc,], vdimns, \ 28143 vshape, notcoincdim) 28144 maskvarslice.append(varmask) 28145 28146 maskvarslcs[vslcn] = maskvarslice 28147 allNslices.append(Nslices) 28148 slicedimn.append('slice_'+vslcn) 28149 28150 newvarshape = allNslices + vshape 28151 print ' Shape final sliced variable:', newvarshape 28152 28153 # variables 28154 28155 # This can get quite huge !! 28156 #newvard = slicedimn + vdimns 28157 #newvar = onewnc.createVariable(varn+'sliced', 'f', tuple(newvard), \ 28158 # fill_value=gen.fillValueF) 28159 #basicvardef(newvar, varn+'sliced', varn + 'sliced by '+ slcvarnS, vu) 28160 #add_varattrs(onc, onewnc, [varn], [varn+'sliced']) 28161 28162 masknodims = [] 28163 vrank = np.arange(len(vdimns)) 28164 if slicestatsdim == 'any': 28165 slcmin = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 28166 slcmax = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 28167 slcmean = np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 28168 slcstd= np.ones(tuple(allNslices), dtype=ovar.dtype)*gen.fillValueF 28169 newvard = slicedimn 28170 else: 28171 nodims = gen.str_list(slicestatsdim, ',') 28172 stvardims = vdimns + [] 28173 stvdn = [] 28174 stvid = [] 28175 idd = 0 28176 vrank = list(vrank) 28177 for nod in nodims: 28178 if not gen.searchInlist(onc.dimensions, nod): 28179 print errormsg 28180 print ' ' +fname+ ": file '" + ncfile + "' does not have " + \ 28181 "dimension: '" + dn + "' !!" 28182 dimns = list(onc.dimensions) 28183 dimns.sort() 28184 print ' available ones:', dimns 28185 quit(-1) 28186 28187 if gen.searchInlist(vdimns, nod): 28188 stvdn.append(nod) 28189 stvid.append(vshape[idd]) 28190 stvardims.remove(nod) 28191 vrank.pop(idd) 28192 masknodims.append(slice(0,vshape[idd])) 28193 idd = idd + 1 28194 vrank = np.array(vrank) 28195 28196 slcmin = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 28197 slcmax = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 28198 slcmean = np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 28199 slcstd= np.ones(tuple(allNslices+stvid), dtype=ovar.dtype)*gen.fillValueF 28200 newvard = slicedimn + stvdn 28201 28202 newvarN = onewnc.createVariable(varn+'Nsliced', 'f', tuple(newvard), \ 28203 fill_value=gen.fillValueF) 28204 basicvardef(newvarN, varn+'Nsliced','number of values in slice of ' + varn + \ 28205 'sliced by '+ slcvarnS, vu) 28206 add_varattrs(onc, onewnc, [varn], [varn+'Nsliced']) 28207 28208 newvarn = onewnc.createVariable(varn+'minsliced', 'f', tuple(newvard), \ 28209 fill_value=gen.fillValueF) 28210 basicvardef(newvarn, varn+'minsliced', 'minimum value of '+varn+'sliced by '+\ 28211 slcvarnS, vu) 28212 add_varattrs(onc, onewnc, [varn], [varn+'minsliced']) 28213 28214 newvarx = onewnc.createVariable(varn+'maxsliced', 'f', tuple(newvard), \ 28215 fill_value=gen.fillValueF) 28216 basicvardef(newvarx, varn+'maxsliced', 'maximum value of '+varn+'sliced by '+\ 28217 slcvarnS, vu) 28218 add_varattrs(onc, onewnc, [varn], [varn+'maxsliced']) 28219 28220 newvarm = onewnc.createVariable(varn+'meansliced', 'f', tuple(newvard), \ 28221 fill_value=gen.fillValueF) 28222 basicvardef(newvarm, varn+'meansliced', 'mean value of '+varn+'sliced by ' + \ 28223 slcvarnS, vu) 28224 add_varattrs(onc, onewnc, [varn], [varn+'meansliced']) 28225 28226 newvars = onewnc.createVariable(varn+'stdsliced', 'f', tuple(newvard), \ 28227 fill_value=gen.fillValueF) 28228 basicvardef(newvars, varn+'stdsliced', 'standard deviation of '+varn+ \ 28229 'sliced by ' + slcvarnS, vu) 28230 add_varattrs(onc, onewnc, [varn], [varn+'stdsliced']) 28231 28232 # Obviously we get memory problems... proceed slide by slide to fill the file 28233 #newvarNmasked = np.ones(tuple(newvarshape), dtype=ovar.dtype)*gen.fillValueF 28234 newvarNmasked = np.ones(tuple(vshape), dtype=ovar.dtype)*gen.fillValueF 28235 28236 # Masking! 28237 allslices = gen.all_consecutive_combs(allNslices) 28238 Nallslices = len(allslices) 28239 28240 for iallslc in range(Nallslices): 28241 islcN = allslices[iallslc] 28242 slcnewvar = [] 28243 iv = 0 28244 for vslcn in slcvarns: 28245 masksv = maskvarslcs[vslcn] 28246 slcnewvar.append(islcN[iv]) 28247 if iv == 0: newmask = masksv[islcN[iv]] 28248 else: newmask = newmask+masksv[islcN[iv]] 28249 iv = iv + 1 28250 28251 for iid in vshape: 28252 slcnewvar.append(slice(0,iid)) 28253 28254 mavals = ma.array(varv, mask=newmask) 28255 #newvarNmasked[tuple(slcnewvar)] = mavals.filled(gen.fillValueF) 28256 newvarNmasked[:] = mavals.filled(gen.fillValueF) 28257 28258 # This can get quite huge !!! 28259 #newvar[tuple(slcnewvar)] = newvarNmasked[:] 28260 28261 manewvar = ma.masked_equal(newvarNmasked, gen.fillValueF) 28262 28263 if len(masknodims) > 0: 28264 islcst = list(islcN) + list(masknodims) 28265 if iallslc == 0: 28266 print ' variable slice statisitcs not using:', nodims 28267 print ' size statistics variables:', allNslices+stvid 28268 print ' slice statistics variables:', islcst 28269 print ' running shapes of the variable:', vrank 28270 else: islcst = islcN 28271 28272 newvarN[tuple(islcst)]=np.sum(~manewvar.mask, axis=tuple(vrank)) 28273 28274 slcmin[tuple(islcst)] = np.min(manewvar, axis=tuple(vrank)) 28275 slcmax[tuple(islcst)] = np.max(manewvar, axis=tuple(vrank)) 28276 slcmean[tuple(islcst)] = np.mean(manewvar, axis=tuple(vrank)) 28277 slcstd[tuple(islcst)] = np.std(manewvar, axis=tuple(vrank)) 28278 28279 newvarn[:] = slcmin 28280 newvarx[:] = slcmax 28281 newvarm[:] = slcmean 28282 newvars[:] = slcstd 28283 28284 onewnc.sync() 28285 # Adding dimension-variables 28286 for dn in vdimns: 28287 if not dimvars.has_key(dn): 28288 print errormsg 28289 print ' ' + fname + ": no dimension-variable provided for " + \ 28290 "dimension '" + dn + "' !!" 28291 print ' provided ones _______' 28292 gen.printing_dictionary(dimvars) 28293 quit(-1) 28294 if not onewnc.variables.has_key(dimvars[dn]): 28295 add_vars(onc, onewnc, [dimvars[dn]]) 28296 onewnc.sync() 28297 28298 # Add global attributes 28299 add_global_PyNCplot(onewnc, 'nc_var_tools', fname, '1.0') 28300 add_globattrs(onc,onewnc,'all') 28301 onewnc.sync() 28302 onc.close() 28303 onewnc.close() 28304 print fname + ": successfull written of file '" + fname + ".nc' !!" 28305 28306 return 28307 28308 values='lon,286.,323.6,4.;lat,-63.,19.,4.;orog,250.,7000.,500.@time|time:lon|lon:lat|lat@time' 28309 compute_slices_stats_areaweighted(values, '/media/lluis/ExtDiskC_ext3/DATA/estudios/Andes/DATA/concatenated/historical/tasmin/tasmin_Amon_ACCESS1-0_historical_r1i1p1_185001-200512_Andes_19600101000000-19900101000000.nc', 'tasmin') 27692 28310 27693 28311 #quit()
Note: See TracChangeset
for help on using the changeset viewer.