<p><b>dwj07@fsu.edu</b> 2013-03-15 08:09:09 -0600 (Fri, 15 Mar 2013)</p><p><br>
        -- BRANCH COMMIT --<br>
<br>
        Adding a new python script for parsing the new XML Registry format to generate documentation of variables, namelist options/records, and dimensions.<br>
</p><hr noshade><pre><font color="gray">Added: branches/tools/python_scripts/namelist_generation/parse_xml_registry.py
===================================================================
--- branches/tools/python_scripts/namelist_generation/parse_xml_registry.py         (rev 0)
+++ branches/tools/python_scripts/namelist_generation/parse_xml_registry.py        2013-03-15 14:09:09 UTC (rev 2617)
@@ -0,0 +1,382 @@
+#!/usr/bin/python
+from collections import defaultdict
+from optparse import OptionParser
+import xml.etree.ElementTree as ET
+
+parser = OptionParser()
+parser.add_option("-f", "--file", dest="registry_path", help="Path to Registry file", metavar="FILE")
+parser.add_option("-d", "--tex_dir", dest="latex_dir", help="Path to directory with latex addition files.", metavar="DIR")
+parser.add_option("-p", "--tex_path", dest="latex_path", help="Path to latex input files that will be written to generated latex.", metavar="PATH")
+
+options, args = parser.parse_args()
+
+if not options.registry_path:
+        parser.error("Registry file is required")
+
+if not options.latex_dir:
+        print 'Directory with group latex files is missing. Skipping addition of latex files.'
+        extra_latex = False
+else:
+        if not options.latex_path:
+                parser.error('Need latex path with latex directory.')
+        extra_latex = True
+
+
+latex_missing_string = '{\\bf \color{red} MISSING}'
+dimension_table_header = '{\\bf Name} & {\\bf Units} & {\\bf Description}'
+variable_table_header = '{\\bf Name} & {\\bf Description}'
+namelist_table_header = '{\\bf Name} & {\\bf Description}'
+
+registry_path = options.registry_path
+
+try:
+        registry_tree = ET.parse(registry_path)
+except:
+        parser.error('%s does not exist. Exiting.'%registry_path)
+
+registry = registry_tree.getroot()
+
+# Write default namelist
+namelist = open('namelist.input.generated', 'w+')
+for nml_rec in registry.iter("nml_record"):
+        namelist.write('&%s</font>
<font color="blue">'%nml_rec.attrib['name'])
+        for nml_opt in nml_rec.iter("nml_option"):
+                namelist.write('\t%s = %s</font>
<font color="blue">'%(nml_opt.attrib['name'], nml_opt.attrib['default_value']))
+
+        namelist.write('/</font>
<font color="blue">')
+
+
+# Write dimension table documentation latex file.
+latex = open('dimension_table_documentation.tex', 'w+')
+latex.write('\chapter{Dimensions}</font>
<font color="blue">')
+latex.write('\label{chap:dimensions}</font>
<font color="blue">')
+latex.write('{\small</font>
<font color="blue">')
+latex.write('\\begin{center}</font>
<font color="blue">')
+latex.write('\\begin{longtable}{| p{1.0in} || p{1.0in} | p{4.0in} |}</font>
<font color="blue">')
+latex.write('        \hline </font>
<font color="blue">')
+latex.write('        %s \\\\</font>
<font color="blue">'%dimension_table_header)
+latex.write('        \hline </font>
<font color="blue">')
+latex.write('        \hline </font>
<font color="blue">')
+for dims in registry.iter("dims"):
+        for dim in dims.iter("dim"):
+                dim_name = dim.attrib['name']
+                try:
+                        dim_description = dim.attrib['description']
+                except:
+                        dim_description = latex_missing_string
+
+                try:
+                        dim_units = dim.attrib['units']
+                except:
+                        dim_units = latex_missing_string
+
+                if dim_description == "":
+                        dim_description = latex_missing_string
+
+                if dim_units == "":
+                        dim_units = latex_missing_string
+
+                latex.write('        %s & %s & %s \\\\ </font>
<font color="blue">'%(dim_name.replace('_','\_'), dim_units.replace('_','\_'), dim_description.replace('_','\_')))
+                latex.write('        \hline</font>
<font color="blue">')
+
+latex.write('\end{longtable}</font>
<font color="blue">')
+latex.write('\end{center}</font>
<font color="blue">')
+latex.write('}</font>
<font color="blue">')
+latex.close()
+
+
+# Write namelist table documentation latex file.
+latex = open('namelist_table_documentation.tex', 'w+')
+latex.write('\chapter[Namelist options]{\hyperref[chap:namelist_sections]{Namelist options}}</font>
<font color="blue">')
+latex.write('\label{chap:namelist_tables}</font>
<font color="blue">')
+latex.write('Embedded links point to more detailed namelist information in the appendix.</font>
<font color="blue">')
+for nml_rec in registry.iter("nml_record"):
+        rec_name = nml_rec.attrib['name']
+        latex.write('\section[%s]{\hyperref[sec:nm_sec_%s]{%s}}</font>
<font color="blue">'%(rec_name.replace('_','\_'), rec_name, rec_name.replace('_','\_')))
+        latex.write('\label{sec:nm_tab_%s}</font>
<font color="blue">'%(rec_name))
+
+        # Add input line if file exists.
+        try:
+                junk_file = open('%s/%s.tex'%(options.latex_dir,rec_name), 'r')
+                latex.write('\input{%s/%s.tex}</font>
<font color="blue">'%(options.latex_path, rec_name))
+                junk_file.close()
+        except:
+                latex.write('')
+
+        latex.write('{\small</font>
<font color="blue">')
+        latex.write('\\begin{center}</font>
<font color="blue">')
+        latex.write('\\begin{longtable}{| p{2.0in} || p{4.0in} |}</font>
<font color="blue">')
+        latex.write('        \hline</font>
<font color="blue">')
+        latex.write('        %s \\\\</font>
<font color="blue">'%namelist_table_header)
+        latex.write('        \hline</font>
<font color="blue">')
+        latex.write('        \hline</font>
<font color="blue">')
+
+        for nml_opt in nml_rec.iter("nml_option"):
+                opt_name = nml_opt.attrib['name']
+                opt_description = nml_opt.attrib['description']
+
+                if opt_description == "":
+                        opt_description = latex_missing_string
+
+                latex.write('        \hyperref[subsec:nm_sec_%s]{%s} & %s \\\\</font>
<font color="blue">'%(opt_name, opt_name.replace('_','\_'), opt_description.replace('_', '\_')))
+                latex.write('        \hline</font>
<font color="blue">')
+
+        latex.write('\end{longtable}</font>
<font color="blue">')
+        latex.write('\end{center}</font>
<font color="blue">')
+        latex.write('}</font>
<font color="blue">')
+latex.close()
+                
+# Write namelist section documentation latex file.
+latex = open('namelist_section_documentation.tex', 'w+')
+latex.write('\chapter[Namelist options]{\hyperref[chap:namelist_tables]{Namelist options}}</font>
<font color="blue">')
+latex.write('\label{chap:namelist_sections}</font>
<font color="blue">')
+latex.write('Embedded links point to information in chapter \\ref{chap:namelist_tables}</font>
<font color="blue">')
+for nml_rec in registry.iter("nml_record"):
+        rec_name = nml_rec.attrib["name"]
+
+        latex.write('\section[%s]{\hyperref[sec:nm_tab_%s]{%s}}</font>
<font color="blue">'%(rec_name.replace('_','\_'),rec_name,rec_name.replace('_','\_')))
+        latex.write('\label{sec:nm_sec_%s}</font>
<font color="blue">'%rec_name)
+
+        for nml_opt in nml_rec.iter("nml_option"):
+                opt_name = nml_opt.attrib["name"]
+                opt_type = nml_opt.attrib["type"]
+                opt_value = nml_opt.attrib["default_value"]
+                opt_possible_values = nml_opt.attrib["possible_values"]
+                opt_units = nml_opt.attrib["units"]
+                opt_descrption = nml_opt.attrib["description"]
+
+                if opt_possible_values == "":
+                        opt_possible_values = latex_missing_string
+
+                if opt_units == "":
+                        opt_units = latex_missing_string
+
+                if opt_description == "":
+                        opt_description = latex_missing_string
+
+                latex.write('\\begin{center}</font>
<font color="blue">')
+                latex.write('\\begin{longtable}{| p{2.0in} | p{4.0in} |}</font>
<font color="blue">')
+                latex.write(' \hline</font>
<font color="blue">')
+                latex.write(' Type: & %s \\\\</font>
<font color="blue">'%opt_type.replace('_','\_'))
+                latex.write(' \hline</font>
<font color="blue">')
+                latex.write(' Units: & %s \\\\</font>
<font color="blue">'%opt_units.replace('_','\_'))
+                latex.write(' \hline</font>
<font color="blue">')
+                latex.write(' Default Value: & %s \\\\</font>
<font color="blue">'%opt_value.replace('_','\_'))
+                latex.write(' \hline</font>
<font color="blue">')
+                latex.write(' Possible Values: & %s \\\\</font>
<font color="blue">'%opt_possible_values.replace('_','\_'))
+                latex.write(' \hline</font>
<font color="blue">')
+                latex.write(' \caption{%s: %s}</font>
<font color="blue">'%(opt_name.replace('_','\_'), opt_description.replace('_','\_')))
+                latex.write('\end{longtable}</font>
<font color="blue">')
+                latex.write('\end{center}</font>
<font color="blue">')
+latex.close()
+
+# Write variable table documentation latex file
+latex = open('variable_table_documentation.tex', 'w+')
+latex.write('\chapter[Variable definitions]{\hyperref[chap:variable_sections]{Variable definitions}}</font>
<font color="blue">')
+latex.write('\label{chap:variable_tables}</font>
<font color="blue">')
+latex.write('Embedded links point to more detailed variable information in the appendix.</font>
<font color="blue">')
+for var_struct in registry.iter("var_struct"):
+        struct_name = var_struct.attrib['name']
+        latex.write('\section[%s]{\hyperref[sec:var_sec_%s]{%s}}</font>
<font color="blue">'%(struct_name.replace('_','\_'),struct_name,struct_name.replace('_','\_')))
+        latex.write('\label{sec:var_tab_%s}</font>
<font color="blue">'%struct_name)
+
+        try:
+                junk_file = open('%s/%s.tex'%(options.latex_dir,struct_name), 'r')
+                latex.write('\input{%s/%s.tex}</font>
<font color="blue">'%(options.latex_path, struct_name))
+                junk_file.close()
+        except:
+                latex.write('')
+
+        latex.write('{\small</font>
<font color="blue">')
+        latex.write('\\begin{center}</font>
<font color="blue">')
+        latex.write('\\begin{longtable}{| p{2.0in} | p{4.0in} |}</font>
<font color="blue">')
+        latex.write('        \hline</font>
<font color="blue">')
+        latex.write('        %s \\\\</font>
<font color="blue">'%variable_table_header)
+        latex.write('        \hline</font>
<font color="blue">')
+
+        # Extract variables from variable arrays
+        for var_arr in var_struct.iter("var_array"):
+                for var in var_arr.iter("var"):
+                        var_name = var.attrib['name']
+                        var_description = var.attrib['description']
+
+                        if var_description == "":
+                                var_description = latex_missing_string
+
+                        latex.write('        \hyperref[subsec:var_sec_%s_%s]{%s} & %s \\\\</font>
<font color="blue">'%(struct_name, var_name, var_name.replace('_','\_'), var_description.replace('_','\_')))
+                        latex.write('        \hline</font>
<font color="blue">')
+
+
+        # Parse normal variables
+        for var in var_struct.iter("var"):
+                var_name = var.attrib['name']
+                var_description = var.attrib['description']
+
+                if var_description == "":
+                        var_description = latex_missing_string
+
+                latex.write('        \hyperref[subsec:var_sec_%s_%s]{%s} & %s \\\\</font>
<font color="blue">'%(struct_name, var_name, var_name.replace('_','\_'), var_description.replace('_','\_')))
+                latex.write('        \hline</font>
<font color="blue">')
+
+        latex.write('\end{longtable}</font>
<font color="blue">')
+        latex.write('\end{center}</font>
<font color="blue">')
+        latex.write('}</font>
<font color="blue">')
+latex.close()
+
+# Write variable section documentation latex file
+latex = open('variable_section_documentation.tex', 'w+')
+latex.write('\chapter[Variable definitions]{\hyperref[chap:variable_tables]{Variable definitions}}</font>
<font color="blue">')
+latex.write('\label{chap:variable_sections}</font>
<font color="blue">')
+latex.write('Embedded links point to information in chapter \\ref{chap:variable_tables}</font>
<font color="blue">')
+for var_struct in registry.iter("var_struct"):
+        struct_name = var_struct.attrib['name']
+        struct_time_levs = var_struct.attrib['time_levs']
+        latex.write('\section[%s]{\hyperref[sec:var_tab_%s]{%s}}</font>
<font color="blue">'%(struct_name.replace('_','\_'),struct_name, struct_name.replace('_','\_')))
+        latex.write('\label{sec:var_sec_%s}</font>
<font color="blue">'%struct_name)
+
+        # Extract variables from variable arrays
+        for var_arr in var_struct.iter("var_array"):
+                for var in var_arr.iter("var"):
+                        var_arr_name = var_arr.attrib['name']
+                        var_arr_type = var_arr.attrib['type']
+                        var_arr_dims = var_arr.attrib['dimensions']
+
+                        var_name = var.attrib['name']
+                        var_arr_group = var.attrib['array_group']
+
+                        try:
+                                var_persistence = var_arr.attrib['persistence']
+                        except:
+                                var_persistence = 'persistent'
+
+                        try:
+                                var_name_in_code = var.attrib['name_in_code']
+                        except:
+                                var_name_in_code = var_name
+
+                        try:
+                                var_units = var.attrib['units']
+                        except:
+                                var_units = latex_missing_string
+
+                        try:
+                                var_description = var.attrib['description']
+                        except:
+                                var_description = latex_missing_string
+
+                        try:
+                                var_streams = var.attrib['streams'].replace('s','Sfc ').replace('i','Input ').replace('r', 'Restart ').replace('o','Output ')
+                        except:
+                                var_streams = "None"
+
+                        if int(struct_time_levs) > 1:
+                                var_index = "domain %% blocklist %% %s %% index_%s"%(struct_name, var_name)
+                                var_path = "domain %% blocklist %% %s %% time_levs(:) %% %s %% %s"%(struct_name, struct_name, var_arr_name)
+                        else:
+                                var_index = "domain %% blocklist %% %s %% index_%s"%(struct_name, var_name)
+                                var_path = "domain %% blocklist %% %s %% %s"%(struct_name, var_arr_name)
+
+                        if var_units == "":
+                                var_units = latex_missing_string
+
+                        if var_description == "":
+                                var_description = latex_missing_string
+                        
+                        latex.write('\subsection[%s]{\hyperref[sec:var_tab_%s]{%s}}</font>
<font color="blue">'%(var_name.replace('_','\_'),struct_name, var_name.replace('_','\_')))
+                        latex.write('\label{subsec:var_sec_%s_%s}</font>
<font color="blue">'%(struct_name,var_name))
+                        # Tabular Format:
+                        latex.write('\\begin{center}</font>
<font color="blue">')
+                        latex.write('\\begin{longtable}{| p{2.0in} | p{4.0in} |}</font>
<font color="blue">')
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Type: & %s \\\\</font>
<font color="blue">'%var_arr_type)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Units: & %s \\\\</font>
<font color="blue">'%var_units)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Dimension: & %s \\\\</font>
<font color="blue">'%var_arr_dims)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Persistence: & %s \\\\</font>
<font color="blue">'%var_persistence)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write('                 Default Streams: & %s \\\\</font>
<font color="blue">'%var_streams)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write('                 Index in %s Array: & %s \\\\</font>
<font color="blue">'%(var_arr_name.replace('_','\_'), var_index.replace('_','\_').replace('%','\%')))
+                        latex.write('                 \hline </font>
<font color="blue">')
+                        latex.write('                 Location in code: & %s \\\\</font>
<font color="blue">'%var_path.replace('_','\_').replace('%','\%'))
+                        latex.write('                 \hline </font>
<font color="blue">')
+                        latex.write('                 Array Group: & %s \\\\</font>
<font color="blue">'%var_arr_group)
+                        latex.write('                 \hline </font>
<font color="blue">')
+                        latex.write(' \caption{%s: %s}</font>
<font color="blue">'%(var_name.replace('_','\_'),var_description.replace('_','\_')))
+                        latex.write('\end{longtable}</font>
<font color="blue">')
+                        latex.write('\end{center}</font>
<font color="blue">')
+
+        # Parse variables not in variable arrays
+        for var in var_struct.iter("var"):
+                try:
+                        # Skip super array variables. They have different tables.
+                        var_arr_group = var.attrib['array_group']
+                except:
+                        var_name = var.attrib['name']
+                        var_type = var.attrib['type']
+                        var_dims = var.attrib['dimensions']
+
+                        try:
+                                var_persistence = var_arr.attrib['persistence']
+                        except:
+                                var_persistence = 'persistent'
+
+                        try:
+                                var_name_in_code = var.attrib['name_in_code']
+                        except:
+                                var_name_in_code = var_name
+
+                        try:
+                                var_units = var.attrib['units']
+                        except:
+                                var_units = latex_missing_string
+
+                        try:
+                                var_description = var.attrib['description']
+                        except:
+                                var_description = latex_missing_string
+
+                        try:
+                                var_streams = var.attrib['streams'].replace('s','Sfc ').replace('i','Input ').replace('r', 'Restart ').replace('o','Output ')
+                        except:
+                                var_streams = "None"
+
+                        if int(struct_time_levs) > 1:
+                                var_path = "domain %% blocklist %% %s %% time_levs(:) %% %s %% %s"%(struct_name, struct_name, var_name)
+                        else:
+                                var_path = "domain %% blocklist %% %s %% %s"%(struct_name, var_name)
+
+                        if var_units == "":
+                                var_units = latex_missing_string
+
+                        if var_description == "":
+                                var_description = latex_missing_string
+                        
+                        latex.write('\subsection[%s]{\hyperref[sec:var_tab_%s]{%s}}</font>
<font color="blue">'%(var_name.replace('_','\_'),struct_name, var_name.replace('_','\_')))
+                        latex.write('\label{subsec:var_sec_%s_%s}</font>
<font color="blue">'%(struct_name,var_name))
+                        # Tabular Format:
+                        latex.write('\\begin{center}</font>
<font color="blue">')
+                        latex.write('\\begin{longtable}{| p{2.0in} | p{4.0in} |}</font>
<font color="blue">')
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Type: & %s \\\\</font>
<font color="blue">'%var_type)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Units: & %s \\\\</font>
<font color="blue">'%var_units)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Dimension: & %s \\\\</font>
<font color="blue">'%var_dims)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write(' Persistence: & %s \\\\</font>
<font color="blue">'%var_persistence)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write('                 Default Streams: & %s \\\\</font>
<font color="blue">'%var_streams)
+                        latex.write(' \hline </font>
<font color="blue">')
+                        latex.write('                 Location in code: & %s \\\\</font>
<font color="blue">'%var_path.replace('_','\_').replace('%','\%'))
+                        latex.write('                 \hline </font>
<font color="blue">')
+                        latex.write(' \caption{%s: %s}</font>
<font color="blue">'%(var_name.replace('_','\_'),var_description.replace('_','\_')))
+                        latex.write('\end{longtable}</font>
<font color="blue">')
+                        latex.write('\end{center}</font>
<font color="blue">')
+latex.close()
+
+
+
+
Property changes on: branches/tools/python_scripts/namelist_generation/parse_xml_registry.py
___________________________________________________________________
Added: svn:executable
## -0,0 +1 ##
+*
\ No newline at end of property
</font>
</pre>