PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/other/convert_to_geogrid/namelist.py

http://github.com/jbeezley/wrf-fire
Python | 143 lines | 125 code | 0 blank | 18 comment | 1 complexity | c779cd591e0bd6be574c23815283eedc MD5 | raw file
Possible License(s): AGPL-1.0
  1. #!/usr/bin/python
  2. """
  3. Manipulates Fortran Namelists
  4. Defines
  5. Namelist class
  6. TODO: == As a Stand alone program ==
  7. Print info about Fortran Namelists
  8. Usage: namelist.py -f FILENAME [-n NAMELIST [-p PARNAME]]
  9. FILENAME: path/name of config file
  10. NAMELIST: namelist name (since a file may contain many namelists)
  11. PARNAME: Name of parameter to print value for
  12. if NAMELIST is not provided,
  13. print the list of namelist present in file in list form:
  14. ['name1','name2',...]
  15. if NAMELIST is provided (PARNAME not provided),
  16. print all namelist param sin a "param=value" format, one per line
  17. for multiple values of same param, print each one
  18. as "param=value" format, one per line
  19. if NAMELIST and PARNAME is provided,
  20. if not specify, print all param in a "param=value" format
  21. if specify and exist, print PARNAME's value only
  22. for multiple values of same param, print each PARNAME's
  23. values, one per line
  24. This script is a generic Fortan namelist parser
  25. and will recognize all namelist in a file with the following format,
  26. and ignores the rest.
  27. &namelistname
  28. opt1 = value1
  29. ...
  30. /
  31. """
  32. __author__ = 'Stephane Chamberland (stephane.chamberland@ec.gc.ca)'
  33. __version__ = '$Revision: 1.0 $'[11:-2]
  34. __date__ = '$Date: 2006/09/05 21:16:24 $'
  35. __copyright__ = 'Copyright (c) 2006 RPN'
  36. __license__ = 'LGPL'
  37. import sys
  38. #sys.path.append("/usr/local/env/armnlib/modeles/SURF/python")
  39. import re
  40. from settings import Settings
  41. # import sys
  42. # import getopt
  43. # import string
  44. class Namelist(Settings):
  45. """
  46. Namelist class
  47. Scan a Fortran Namelist file and put Section/Parameters into a dictionary
  48. Intentiation:
  49. foo = Namelist(NamelistFile)
  50. where NamelistFile can be a filename, an URL or a string
  51. Functions:
  52. [Pending]
  53. This is a generic Fortan namelist parser
  54. it will recognize all namelist in a file with the following format,
  55. and ignores the rest.
  56. &namelistname
  57. opt1 = value1
  58. ...
  59. /
  60. """
  61. def parse(self):
  62. """Config file parser, called from the class initialization"""
  63. varname = r'\b[a-zA-Z][a-zA-Z0-9_]*\b'
  64. valueInt = re.compile(r'[+-]?[0-9]+')
  65. valueReal = re.compile(r'[+-]?([0-9]+\.[0-9]*|[0-9]*\.[0-9]+)')
  66. valueNumber = re.compile(r'\b(([\+\-]?[0-9]+)?\.)?[0-9]*([eE][-+]?[0-9]+)?')
  67. valueBool = re.compile(r"(\.(true|false|t|f)\.)",re.I)
  68. valueTrue = re.compile(r"(\.(true|t)\.)",re.I)
  69. spaces = r'[\s\t]*'
  70. quote = re.compile(r"[\s\t]*[\'\"]")
  71. namelistname = re.compile(r"^[\s\t]*&(" + varname + r")[\s\t]*$")
  72. paramname = re.compile(r"[\s\t]*(" + varname+r')[\s\t]*=[\s\t]*')
  73. namlistend = re.compile(r"^" + spaces + r"/" + spaces + r"$")
  74. #split sections/namelists
  75. mynmlfile = {}
  76. mynmlname = ''
  77. for item in self.clean(self._setContent.split("\n"),cleancomma=1):
  78. if re.match(namelistname,item):
  79. mynmlname = re.sub(namelistname,r"\1",item)
  80. mynmlfile[mynmlname] = {
  81. 'raw' : [],
  82. 'par' : [{}]
  83. }
  84. elif re.match(namlistend,item):
  85. mynmlname = ''
  86. else:
  87. if mynmlname:
  88. mynmlfile[mynmlname]['raw'].append(item)
  89. #parse param in each section/namelist
  90. for mynmlname in mynmlfile.keys():
  91. #split strings
  92. bb = []
  93. for item in mynmlfile[mynmlname]['raw']:
  94. bb.extend(self.splitstring(item))
  95. #split comma and =
  96. aa = []
  97. for item in bb:
  98. if not re.match(quote,item):
  99. aa.extend(re.sub(r"[\s\t]*=",r" =\n",re.sub(r",+",r"\n",item)).split("\n"))
  100. else:
  101. aa.append(item)
  102. del(bb)
  103. aa = self.clean(aa,cleancomma=1)
  104. myparname = ''
  105. for item in aa:
  106. if re.search(paramname,item):
  107. myparname = re.sub(paramname,r"\1",item).lower()
  108. mynmlfile[mynmlname]['par'][0][myparname] = []
  109. elif paramname:
  110. #removed quotes, spaces (then how to distinguish .t. of ".t."?)
  111. if re.match(valueBool,item):
  112. if re.match(valueTrue,item):
  113. mynmlfile[mynmlname]['par'][0][myparname].append('.true.')
  114. else:
  115. mynmlfile[mynmlname]['par'][0][myparname].append('.false.')
  116. else:
  117. mynmlfile[mynmlname]['par'][0][myparname].append(re.sub(r"(^[\'\"]|[\'\"]$)",r"",item.strip()).strip())
  118. return mynmlfile