Changeset 1600 in lmdz_wrf for trunk/tools/generic_tools.py
- Timestamp:
- Aug 3, 2017, 8:26:35 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tools/generic_tools.py
r1599 r1600 100 100 # multi_index_mat: Function to provide the multiple coordinates of a given value inside a matrix 101 101 # multi_index_string: Function to provide the indeces of every repetition of a group of characters within a string 102 # Nomasked: Function to brin back a given array wthout the masked values reducing a given dimension102 # Nomasked: Function to bring back a given array wthout the masked values reducing a given dimension 103 103 # num_chainSnum: Function to pass a value to a `ChainStrNum' number 104 104 # num_ordinal: Function to provide the ordinal of a given number, language, format and gender … … 122 122 # same_shape: Function to check if two matrices have the same shape 123 123 # search_sec_list: Function to provide the values and indices on a list which matches a section of a string 124 # shrinkarray_dim: Function to shrink a given array with a vector of True/False along a given dimension 124 125 # significant_decomposition: Function to decompose a given number by its signifcant potencies 125 126 # singleline_printing_class: Function to print all the values of a given class in a single line to be parseavel … … 12321 12322 12322 12323 def Nomasked(vals, dim): 12323 """ Function to brin back a given array wthout the masked values reducing a given dimension12324 vals= arr tay of values12324 """ Function to bring back a given array wthout the masked values reducing a given dimension 12325 vals= array of values 12325 12326 dim= which dimension along which if all values are maskd reduce 12326 12327 >>> arrayv = np.arange(32).reshape(4,8) … … 12337 12338 fname = 'Nomasked' 12338 12339 12340 if type(vals) != type(mamat): 12341 print errormsg 12342 print ' ' + fname + ': only works with masked arrays !!' 12343 print ' provided type:', type(vals) 12344 quit(-1) 12345 if dim >= len(vals.shape): 12346 print errormsg 12347 print ' ' + fname + ': dimension provided too big !!' 12348 print ' shape of the values:', vals.shape 12349 quit(-1) 12350 12339 12351 varshape = vals.shape 12340 12352 dimns = [] … … 12372 12384 12373 12385 return newarray, allmasked 12386 12387 def shrinkarray_dim(vals, vector, dim): 12388 """ Function to shrink a given array with a vector of True/False along a given dimension 12389 vals= array of values 12390 vector= vector of booleans of the same length as dim (True: remove) 12391 dim= which dimension along which if all values are maskd reduce 12392 >>> arrayv = np.arange(32).reshape(4,8) 12393 >>> maskv = np.zeros((4), dtype=bool) 12394 >>> maskv[1:3] = True 12395 >>> shrinkarray_dim(arrayv, maskv, 0) 12396 [[ 0 1 2 3 4 5 6 7] 12397 [24 25 26 27 28 29 30 31]] 12398 """ 12399 fname = 'shrinkarray_dim' 12400 12401 if dim >= len(vals.shape): 12402 print errormsg 12403 print ' ' + fname + ': dimension provided too big !!' 12404 print ' shape of the values:', vals.shape 12405 quit(-1) 12406 if len(vector) != vals.shape[dim]: 12407 print errormsg 12408 print ' ' + fname + ': provided vector and length of the dimension differ !!' 12409 print ' shape of the array:', vals.shape[dim], 'on dimension:', dim 12410 print ' length of the vector:', len(vector) 12411 quit(-1) 12412 12413 varshape = vals.shape 12414 dimns = [] 12415 for id in range(len(varshape)): 12416 dimns.append('d' + str(id)) 12417 12418 # Looking for the slices along the given dimension 12419 slices = provide_slices(dimns, varshape, ['d'+str(dim)]) 12420 12421 Nslices = len(slices) 12422 # First looking for the slices 12423 Novals = np.sum(vector) 12424 12425 # Definition of the new array 12426 newvarshape = list(varshape) 12427 newvarshape[dim] = varshape[dim]-Novals 12428 12429 newarray = np.zeros(tuple(newvarshape), dtype=vals.dtype) 12430 12431 imask = 0 12432 for islc in range(Nslices-1,-1,-1): 12433 if not vector[islc]: 12434 slicev = list(slices[islc]) 12435 slicev[dim] = imask 12436 newarray[tuple(slicev)] = vals[tuple(slices[islc])] 12437 imask = imask + 1 12438 12439 return newarray 12440 12374 12441 #quit() 12375 12442
Note: See TracChangeset
for help on using the changeset viewer.