Changeset 1602 in lmdz_wrf for trunk


Ignore:
Timestamp:
Aug 8, 2017, 4:41:38 PM (8 years ago)
Author:
lfita
Message:

Adding:

  • `IsNumber?': Function to determine if a given String is a given type of number
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1601 r1602  
    8787# InsertVal_list_sign: Insert values into a sorrted list according to its sign of growth
    8888# int_to_roman: Convert an integer to Roman numerals
     89# IsNumber: Function to determine if a given String is a given type of number
    8990# julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
    9091# latex_fig_array: Function to add an array of figures to an existing tex file
     
    1244112442    return newarray
    1244212443
     12444def IsNumber(stringS, kind):
     12445    """ Function to determine if a given String is a given type of number
     12446      stringS: string to check
     12447      kind: kind of number to check
     12448        'I': integer
     12449        'R': float
     12450        'D': float64
     12451    >>> IsNumber('3.14', 'R')
     12452    True
     12453    >>> IsNumber('3.14', 'I')
     12454    False
     12455    """
     12456    fname = 'IsNumber'
     12457    availkind = ['I', 'R', 'D']
     12458
     12459    # Checking for conversion
     12460    if kind == 'I':
     12461        try:
     12462            newnum = int(stringS)
     12463        except:
     12464            return False
     12465    elif kind == 'R':
     12466        try:
     12467            newnum = np.float(stringS)
     12468        except:
     12469            return False
     12470    elif kind == 'D':
     12471        try:
     12472            newnum = np.float64(stringS)
     12473        except:
     12474            return False
     12475    else:
     12476        print errormsg
     12477        print '  ' +  fname + ": provided type '" + kind + "' to check is not ready!!"
     12478        print '    available ones:', availkind
     12479        quit(-1)
     12480
     12481    return True
     12482
    1244312483#quit()
    1244412484
Note: See TracChangeset for help on using the changeset viewer.