Changeset 1359 in lmdz_wrf


Ignore:
Timestamp:
Nov 29, 2016, 10:56:34 AM (8 years ago)
Author:
lfita
Message:

Adding:

`int_to_roman': Convert an integer to Roman numerals
`roman_to_int': Convert a roman numeral to an integer
adding leading zeros on `coldec_hex'

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/generic_tools.py

    r1357 r1359  
    6767# index_mat: Function to provide the coordinates of a given value inside a matrix
    6868# index_vec: Function to provide the coordinates of a given value inside a vector
     69# int_to_roman: Convert an integer to Roman numerals
    6970# julday_360d: Function to provide the julian day of a date in a 360 days/yr (or 12 30-days months) calendar
    7071# latex_fig_array: Function to add an array of figures to an existing tex file
     
    9192# radius_dist: Function to generate a matrix with the distance at a given point
    9293# replace_list: Function to replace a value in a given list
     94# roman_to_int: Convert a roman numeral to an integer
    9395# same_shape: Function to check if two matrices have the same shape
    9496# search_sec_list: Function to provide the values and indices on a list which matches a section of a string
     
    86308632    fname = 'coldec_hex'
    86318633
    8632     hexred = '{0:X}'.format(int(col[0]*255))
    8633     hexgreen = '{0:X}'.format(int(col[1]*255))
    8634     hexblue = '{0:X}'.format(int(col[2]*255))
     8634    hexred = '{0:X}'.format(int(col[0]*255)).zfill(2)
     8635    hexgreen = '{0:X}'.format(int(col[1]*255)).zfill(2)
     8636    hexblue = '{0:X}'.format(int(col[2]*255)).zfill(2)
    86358637
    86368638    if hexred == '0': hexred = '00'
     
    1057810580#quit()
    1057910581
     10582def int_to_roman(input, romanchar='U'):
     10583   """
     10584   FROM: http://code.activestate.com/recipes/81611-roman-numerals/
     10585   Convert an integer to Roman numerals.
     10586     input= number to transform
     10587     romanchar= type of characters
     10588       'l': lower romans: i, v, c, ...
     10589       'U': upper romans: I, V, C, ... (default)
     10590   Examples:
     10591   >>> int_to_roman(0)
     10592   Traceback (most recent call last):
     10593   ValueError: Argument must be between 1 and 3999
     10594
     10595   >>> int_to_roman(-1)
     10596   Traceback (most recent call last):
     10597   ValueError: Argument must be between 1 and 3999
     10598
     10599   >>> int_to_roman(1.5)
     10600   Traceback (most recent call last):
     10601   TypeError: expected integer, got <type 'float'>
     10602
     10603   >>> for i in range(1, 21): print int_to_roman(i)
     10604   ...
     10605   I
     10606   II
     10607   III
     10608   IV
     10609   V
     10610   VI
     10611   VII
     10612   VIII
     10613   IX
     10614   X
     10615   XI
     10616   XII
     10617   XIII
     10618   XIV
     10619   XV
     10620   XVI
     10621   XVII
     10622   XVIII
     10623   XIX
     10624   XX
     10625   >>> print int_to_roman(2000)
     10626   MM
     10627   >>> print int_to_roman(1999)
     10628   MCMXCIX
     10629   """
     10630   if type(input) != type(1):
     10631      raise TypeError, "expected integer, got %s" % type(input)
     10632   if not 0 < input < 4000:
     10633      raise ValueError, "Argument must be between 1 and 3999"   
     10634   ints = (1000, 900,  500, 400, 100,  90, 50,  40, 10,  9,   5,  4,   1)
     10635   if romanchar == 'U':
     10636     nums = ('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I')
     10637   elif romanchar == 'l':
     10638     nums = ('m', 'cm', 'd', 'cd', 'c', 'xc', 'l', 'xl', 'x', 'ix', 'v', 'iv', 'i')
     10639   else:
     10640       print errormsg
     10641       print '  ' + fname + ": roman char '" + romanchar + "' not ready!!"
     10642       print "    authorized ones: 'U', 'l'"
     10643       quit(-1)
     10644   result = ""
     10645   for i in range(len(ints)):
     10646      count = int(input / ints[i])
     10647      result += nums[i] * count
     10648      input -= ints[i] * count
     10649   return result
     10650
     10651def roman_to_int(input, romanchar='U'):
     10652   """
     10653   FROM: http://code.activestate.com/recipes/81611-roman-numerals/
     10654   Convert a roman numeral to an integer.
     10655     input= roman number to transform
     10656     romanchar= type of characters
     10657       'l': lower romans: i, v, c, ...
     10658       'U': upper romans: I, V, C, ... (default)
     10659   >>> r = range(1, 4000)
     10660   >>> nums = [int_to_roman(i) for i in r]
     10661   >>> ints = [roman_to_int(n) for n in nums]
     10662   >>> print r == ints
     10663   1
     10664
     10665   >>> roman_to_int('VVVIV')
     10666   Traceback (most recent call last):
     10667    ...
     10668   ValueError: input is not a valid roman numeral: VVVIV
     10669   >>> roman_to_int(1)
     10670   Traceback (most recent call last):
     10671    ...
     10672   TypeError: expected string, got <type 'int'>
     10673   >>> roman_to_int('a')
     10674   Traceback (most recent call last):
     10675    ...
     10676   ValueError: input is not a valid roman numeral: A
     10677   >>> roman_to_int('IL')
     10678   Traceback (most recent call last):
     10679    ...
     10680   ValueError: input is not a valid roman numeral: IL
     10681   """
     10682   if type(input) != type(""):
     10683      raise TypeError, "expected string, got %s" % type(input)
     10684   input = input.upper()
     10685   if romanchar == 'U':
     10686       nums = ['M', 'D', 'C', 'L', 'X', 'V', 'I']
     10687   elif romanchar == 'l':
     10688       nums = ['m', 'd', 'c', 'l', 'x', 'v', 'i']
     10689   else:
     10690       print errormsg
     10691       print '  ' + fname + ": roman char '" + romanchar + "' not ready!!"
     10692       print "    authorized ones: 'U', 'l'"
     10693       quit(-1)
     10694
     10695   ints = [1000, 500, 100, 50,  10,  5,   1]
     10696   places = []
     10697   for c in input:
     10698      if not c in nums:
     10699         raise ValueError, "input is not a valid roman numeral: %s" % input
     10700   for i in range(len(input)):
     10701      c = input[i]
     10702      value = ints[nums.index(c)]
     10703      # If the next place holds a larger number, this value is negative.
     10704      try:
     10705         nextvalue = ints[nums.index(input[i +1])]
     10706         if nextvalue > value:
     10707            value *= -1
     10708      except IndexError:
     10709         # there is no next place.
     10710         pass
     10711      places.append(value)
     10712   sum = 0
     10713   for n in places: sum += n
     10714   # Easiest test for validity...
     10715   if int_to_roman(sum) == input:
     10716      return sum
     10717   else:
     10718      raise ValueError, 'input is not a valid roman numeral: %s' % input
Note: See TracChangeset for help on using the changeset viewer.