Changeset 1592 in lmdz_wrf for trunk/tools/generic_tools.py


Ignore:
Timestamp:
Aug 2, 2017, 9:18:38 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `InsertVal_list_sign': Insert values into a sorrted list according to its sign of growth
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1588 r1592  
    8585# index_mat: Function to provide the coordinates of a given value inside a matrix
    8686# index_vec: Function to provide the coordinates of a given value inside a vector
     87# InsertVal_list_sign: Insert values into a sorrted list according to its sign of growth
    8788# int_to_roman: Convert an integer to Roman numerals
    8889# julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
     
    1224312244#quit()
    1224412245
     12246def InsertVal_list_sign(listv, newvalues):
     12247    """ Insert values into a sorrted list according to its sign of growth
     12248      listv= inital values of the list
     12249      newvalues= values to add
     12250      >>> InsertVal_list_sign(range(0,100,10), [0,1,2,3,5,8,13,21,34,55,89,144])
     12251      [0, 1, 2, 3, 5, 8, 10, 13, 20, 21, 30, 34, 40, 50, 55, 60, 70, 80, 89, 90, 144]
     12252    """
     12253    fname = 'InsertVal_list_sign'
     12254
     12255    newlist = []
     12256
     12257    # Number of values
     12258    Nold = len(listv)
     12259    Nnew = len(newvalues)
     12260
     12261    lsign = np.float(listv[1]) - np.float(listv[0])
     12262
     12263    # Number of values
     12264    Lcoinvals = len(set(listv).intersection(set(newvalues)))
     12265    Ldiffvals = len(set(listv).symmetric_difference(set(newvalues)))
     12266
     12267    Ltot = Lcoinvals + Ldiffvals
     12268
     12269    inew = 0
     12270    iold = 0
     12271    if lsign > 0.:
     12272        for il in range(Ltot):
     12273            if listv[iold] < newvalues[inew]:
     12274                newval = listv[iold]
     12275                iold = iold + 1
     12276            else:
     12277                newval = newvalues[inew]
     12278                inew = np.min([inew + 1, Nnew-1])
     12279
     12280            # Not repeating values
     12281            if not searchInlist(newlist,newval): newlist.append(newval)
     12282            # breaking just in case ...
     12283            if iold > Nold -1:
     12284                newlist = newlist + newvalues[inew:Nnew]
     12285                break
     12286            if inew > Nnew -1:
     12287                newlist = newlist + listv[iold:Nold]
     12288                break
     12289    else:
     12290        for il in range(Ltot+1):
     12291            if listv[iold] > newvalues[inew]:
     12292                newval = listv[iold]
     12293                iold = iold + 1
     12294            else:
     12295                newval = newvalues[inew]
     12296                inew = inew + 1
     12297            # Not repeating values
     12298            if not searchInlist(newlist,newval):  newlist.append(newval)
     12299            # breaking just in case ...
     12300            if iold > Nold -1:
     12301                newlist = newlist + newvalues[inew:Nnew]
     12302                break
     12303            if inew > Nnew -1:
     12304                newlist = newlist + listv[iold:Nold]
     12305                break
     12306
     12307    return newlist
    1224512308
    1224612309#quit()
Note: See TracChangeset for help on using the changeset viewer.