Changeset 2192 in lmdz_wrf
- Timestamp:
- Oct 17, 2018, 10:20:41 PM (6 years ago)
- Location:
- trunk/tools
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r2191 r2192 9650 9650 return diffs, Bdiff 9651 9651 9652 9653 9652 def coincident_CFtimes(tvalB, tunitA, tunitB): 9654 9653 """ Function to make coincident times for two different sets of CFtimes … … 14562 14561 return rightCFtimeu 14563 14562 14563 def masks_mult(dimns1, mask1, dimns2, mask2, dimsuse, shapeuse, nonusemaskdims): 14564 """ Function to multiply two sets of masks on coincident diensions 14565 dimns1= list of dimensions names for mask 1 14566 mask1= mask 1 14567 dimns2= list of dimensions names for mask 2 14568 mask2= mask 2 14569 dimnuse= list of dimensions to use 14570 shapeuse: shape of being used 14571 nonusedmaskdims: dictionary with values of the given dimennsios of the masks 14572 not to be used 14573 """ 14574 fname = 'masks_mult' 14575 14576 coincdims = list(set(dimns1) & set(dimns2)) 14577 NOTcoincdims = list(set(dimns1) - set(dimns2)) 14578 print coincdims 14579 print NOTcoincdims 14580 14581 NOTsliceuse = [] 14582 idim = 0 14583 for dimn in dimsuse: 14584 if not searchInlist(dimns1, dimn): 14585 NOTsliceuse.append(dimn) 14586 continue 14587 if searchInlist(dimns2, dimn): 14588 NOTsliceuse.append(dimn) 14589 continue 14590 14591 mask = np.zeros(shapeuse, dtype=bool) 14592 slices = [] 14593 idim = 0 14594 for dimn in dimsuse: 14595 if searchInlist(NOTsliceuse, dimn): 14596 slices.append(-9) 14597 else: 14598 slices.append(slice(0,shapeuse[idim])) 14599 idim = idim+1 14600 14601 finalslc = [] 14602 idim = 0 14603 for slc in slices: 14604 if type(slc) == type(slice(0,1)): 14605 finalslc.append(slc) 14606 else: 14607 finalslc.append(range(shapeuse[idim])) 14608 idim = idim + 1 14609 14610 print 'finalslc:', finalslc 14611 14612 14613 14614 14615 return 14616 14617 mask1 = np.zeros((3,3,3), dtype=bool).reshape(3,3,3) 14618 mask1[1,:,:] = True 14619 14620 mask2 = np.zeros((3,3,3), dtype=bool).reshape(3,3,3) 14621 mask2[1,1,1] = True 14622 14623 masks_mult(['dt', 'dx', 'dy'], mask1, ['dt', 'dx', 'dy'], mask2, ['dz', 'dy', 'dx'], (2, 3, 3), {'dt': 0}) 14624 14564 14625 #quit() 14565 14626 -
trunk/tools/nc_var_tools.py
r2190 r2192 26131 26131 return filledstrings 26132 26132 26133 def compute_slice2Dstats(values, ncfile, variable): 26134 """ Function to compute stats of variables of a file following slices along 2 variables 26135 values=dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,maxvar2,slcevar2 26136 dimn[1/2]: dimension from which produce the slices of variable 1/2 ('all' 26137 for non-specific dimension to cut along) 26138 varn[1/2]: name of the variable 1/2 26139 minvar[1/2]: minimum value to start the slices for variable 1/2 26140 maxvar[1/2]: maximum value to end the slices for variable 1/2 26141 slcevar[1/2]: length of slices in variable 1/2 26142 ncfile= netCDF file to use 26143 variable: ',' list of variables ('all' for all variables) 26144 """ 26145 fname = 'compute_slice2Dstats' 26146 26147 if values == 'h': 26148 print fname + '_____________________________________________________________' 26149 print compute_slice2Dstats.__doc__ 26150 quit() 26151 26152 expectargs = 'dimn1,varn1,minvar1,maxvar1,slcevar1,dimn2,varn2,minvar2,' + \ 26153 'maxvar2,slcevar2' 26154 gen.check_arguments(fname, values, expectargs, ',') 26155 26156 dimn1 = values.split(',')[0] 26157 varn1 = values.split(',')[1] 26158 minvar1 = np.float(values.split(',')[2]) 26159 maxvar1 = np.float(values.split(',')[3]) 26160 slicevar1 = np.float(values.split(',')[4]) 26161 dimn2 = values.split(',')[5] 26162 varn2 = values.split(',')[6] 26163 minvar2 = np.float(values.split(',')[7]) 26164 maxvar2 = np.float(values.split(',')[8]) 26165 slicevar2 = np.float(values.split(',')[9]) 26166 26167 onc = NetCDFFile(ncfile, 'r') 26168 26169 if dimn1 != 'all' and not gen.searchInlist(onc.dimensions, dimn1): 26170 print errormsg 26171 print ' ' + fname + ": file '" + ncfile + "' does not have dimension 1: '" +\ 26172 dimn1 + "' !!" 26173 dimns = list(onc.dimensions) 26174 dimns.sort() 26175 print ' available ones:', dimns 26176 quit(-1) 26177 26178 if dimn2 != 'all' and not gen.searchInlist(onc.dimensions, dimn2): 26179 print errormsg 26180 print ' ' + fname + ": file '" + ncfile + "' does not have dimension 2: '" +\ 26181 dimn2 + "' !!" 26182 dimns = list(onc.dimensions) 26183 dimns.sort() 26184 print ' available ones:', dimns 26185 quit(-1) 26186 26187 if not onc.variables.has_key(varn1): 26188 print errormsg 26189 print ' ' + fname + ": file '" + ncfile + "' does not have variable 1: '" + \ 26190 varn1 + "' !!" 26191 varns = onc.variables.keys() 26192 varns.sort() 26193 print ' available ones:', varns 26194 quit(-1) 26195 26196 if not onc.variables.has_key(varn2): 26197 print errormsg 26198 print ' ' + fname + ": file '" + ncfile + "' does not have variable 2: '" + \ 26199 varn2 + "' !!" 26200 varns = onc.variables.keys() 26201 varns.sort() 26202 print ' available ones:', varns 26203 quit(-1) 26204 26205 ovar1 = onc.variables[varn1] 26206 ovar2 = onc.variables[varn2] 26207 26208 dvar1 = (maxvar1 - minvar1+slicevar1)/slicevar1 26209 slices1 = np.arange(minvar1, maxvar1+slicevar1, slicevar1) 26210 Nslices1 = slices1.shape[0] 26211 26212 dvar2 = (maxvar2 - minvar2)/slicevar2 26213 slices2 = np.arange(minvar2, maxvar2+slicevar2, slicevar2) 26214 Nslices2 = slices2.shape[0] 26215 26216 print ' ' + fname + ': slices ____' 26217 print ' var1:', varn1, ':', Nslices1, '(', minvar1, ',', maxvar1+slicevar1, \ 26218 ',', slicevar1, ')' 26219 print ' var2:', varn2, ':', Nslices2, '(', minvar2, ',', maxvar2+slicevar2, \ 26220 ',', slicevar2, ')' 26221 26222 # Slices var1 26223 sliceshape1 = [Nslices1] + list(ovar1.shape) 26224 slcvar1 = np.zeros(tuple(sliceshape1), dtype=bool) 26225 var1v = ovar1[:] 26226 for islc in range(Nslices1-1): 26227 slcvar1[islc,] = ma.masked_inside(var1v, slices1[islc], slices1[islc+1]).mask 26228 26229 # Slices var2 26230 sliceshape2 = [Nslices2] + list(ovar2.shape) 26231 slcvar2 = np.zeros(tuple(sliceshape2), dtype=bool) 26232 var2v = ovar2[:] 26233 for islc in range(Nslices2-1): 26234 slcvar2[islc,] = ma.masked_inside(var2v, slices2[islc], slices2[islc+1]).mask 26235 26236 # Looking for coincident dimensions 26237 for dn1 in ovar1.diensions: 26238 for dn2 in ovar1.diensions: 26239 print 'Hola' 26240 26241 return 26242 26243 #values='south_north,XLAT,-63.,19.,2.,all,HGT,250.,7000.,500.' 26244 26245 #compute_slice2Dstats(values, '/home/lluis/PY/wrfout_d01_1995-01-01_00:00:00', 'T2') 26246 26133 26247 #quit() 26248
Note: See TracChangeset
for help on using the changeset viewer.