Changeset 658 in lmdz_wrf for trunk


Ignore:
Timestamp:
Nov 16, 2015, 5:16:38 PM (9 years ago)
Author:
lfita
Message:

Adding:

  • string_dicvals_char: Function to provide a string taking values from a given [string] where each single character
  • substring: Function to substitute a character of a given string
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/nc_var_tools.py

    r656 r658  
    183183
    184184    return newstring
     185
     186def string_dicvals_char(dictionary, string, char):
     187    """ Function to provide a string taking values from a given [string] where each single character
     188      coincide with the key of the dictionary [dictionary], separated by a given set of characters [char]
     189      dictionary= dictionary of the values dim([c]) = {[value]}
     190      string = [c1][...[cN]] String with the combination of [c]s
     191      >>> dictvals = {'i': 'dimx', 'j': 'dimy', 'k': 'dimz', 't': 'dimt'}
     192      >>> string_dicvals_char(dictvals, 'ijkt', ', ')
     193      dimx, dimy, dimz, dimt
     194    """
     195    fname = 'string_dicvals_char'
     196
     197    Lstring = len(string)
     198    newstring = ''
     199    for ic in range(Lstring):
     200        cc = string[ic:ic+1]
     201        if not dictionary.has_key(cc):
     202            print errormsg
     203            print '  ' + fname + ": dictinoary of values does not have character '" +\
     204              cc + "' !!"
     205            print '    it has:',dictionary
     206
     207        if ic == 0:
     208            newstring = dictionary[cc]
     209        else:
     210            newstring = newstring + char + dictionary[cc]
     211
     212    return newstring
     213
     214def substring(stringv,pos,char):
     215    """ Function to substitute a character of a given string
     216      stringv= string to change
     217      pos= position to change
     218      char= characters to use as substitution
     219    >>> substring('0123456',2,'ii')
     220    01ii3456
     221    """
     222    fname = 'substring'
     223
     224# From: http://stackoverflow.com/questions/1228299/change-one-character-in-a-string-in-python
     225    newstring = stringv[:pos] + char + stringv[pos+1:]
     226
     227    return newstring
     228
     229####### ###### ##### #### ### ## #
     230
    185231
    186232def variables_values(varName):
Note: See TracChangeset for help on using the changeset viewer.