<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(&quot;-f&quot;, &quot;--file&quot;, dest=&quot;registry_path&quot;, help=&quot;Path to Registry file&quot;, metavar=&quot;FILE&quot;)
+parser.add_option(&quot;-d&quot;, &quot;--tex_dir&quot;, dest=&quot;latex_dir&quot;, help=&quot;Path to directory with latex addition files.&quot;, metavar=&quot;DIR&quot;)
+parser.add_option(&quot;-p&quot;, &quot;--tex_path&quot;, dest=&quot;latex_path&quot;, help=&quot;Path to latex input files that will be written to generated latex.&quot;, metavar=&quot;PATH&quot;)
+
+options, args = parser.parse_args()
+
+if not options.registry_path:
+        parser.error(&quot;Registry file is required&quot;)
+
+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} &amp; {\\bf Units} &amp; {\\bf Description}'
+variable_table_header = '{\\bf Name} &amp; {\\bf Description}'
+namelist_table_header = '{\\bf Name} &amp; {\\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(&quot;nml_record&quot;):
+        namelist.write('&amp;%s</font>
<font color="blue">'%nml_rec.attrib['name'])
+        for nml_opt in nml_rec.iter(&quot;nml_option&quot;):
+                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(&quot;dims&quot;):
+        for dim in dims.iter(&quot;dim&quot;):
+                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 == &quot;&quot;:
+                        dim_description = latex_missing_string
+
+                if dim_units == &quot;&quot;:
+                        dim_units = latex_missing_string
+
+                latex.write('        %s &amp; %s &amp; %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(&quot;nml_record&quot;):
+        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(&quot;nml_option&quot;):
+                opt_name = nml_opt.attrib['name']
+                opt_description = nml_opt.attrib['description']
+
+                if opt_description == &quot;&quot;:
+                        opt_description = latex_missing_string
+
+                latex.write('        \hyperref[subsec:nm_sec_%s]{%s} &amp; %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(&quot;nml_record&quot;):
+        rec_name = nml_rec.attrib[&quot;name&quot;]
+
+        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(&quot;nml_option&quot;):
+                opt_name = nml_opt.attrib[&quot;name&quot;]
+                opt_type = nml_opt.attrib[&quot;type&quot;]
+                opt_value = nml_opt.attrib[&quot;default_value&quot;]
+                opt_possible_values = nml_opt.attrib[&quot;possible_values&quot;]
+                opt_units = nml_opt.attrib[&quot;units&quot;]
+                opt_descrption = nml_opt.attrib[&quot;description&quot;]
+
+                if opt_possible_values == &quot;&quot;:
+                        opt_possible_values = latex_missing_string
+
+                if opt_units == &quot;&quot;:
+                        opt_units = latex_missing_string
+
+                if opt_description == &quot;&quot;:
+                        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: &amp; %s \\\\</font>
<font color="blue">'%opt_type.replace('_','\_'))
+                latex.write('    \hline</font>
<font color="blue">')
+                latex.write('    Units: &amp; %s \\\\</font>
<font color="blue">'%opt_units.replace('_','\_'))
+                latex.write('    \hline</font>
<font color="blue">')
+                latex.write('    Default Value: &amp; %s \\\\</font>
<font color="blue">'%opt_value.replace('_','\_'))
+                latex.write('    \hline</font>
<font color="blue">')
+                latex.write('    Possible Values: &amp; %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(&quot;var_struct&quot;):
+        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(&quot;var_array&quot;):
+                for var in var_arr.iter(&quot;var&quot;):
+                        var_name = var.attrib['name']
+                        var_description = var.attrib['description']
+
+                        if var_description == &quot;&quot;:
+                                var_description = latex_missing_string
+
+                        latex.write('        \hyperref[subsec:var_sec_%s_%s]{%s} &amp; %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(&quot;var&quot;):
+                var_name = var.attrib['name']
+                var_description = var.attrib['description']
+
+                if var_description == &quot;&quot;:
+                        var_description = latex_missing_string
+
+                latex.write('        \hyperref[subsec:var_sec_%s_%s]{%s} &amp; %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(&quot;var_struct&quot;):
+        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(&quot;var_array&quot;):
+                for var in var_arr.iter(&quot;var&quot;):
+                        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 = &quot;None&quot;
+
+                        if int(struct_time_levs) &gt; 1:
+                                var_index = &quot;domain %% blocklist %% %s %% index_%s&quot;%(struct_name, var_name)
+                                var_path = &quot;domain %% blocklist %% %s %% time_levs(:) %% %s %% %s&quot;%(struct_name, struct_name, var_arr_name)
+                        else:
+                                var_index = &quot;domain %% blocklist %% %s %% index_%s&quot;%(struct_name, var_name)
+                                var_path = &quot;domain %% blocklist %% %s %% %s&quot;%(struct_name, var_arr_name)
+
+                        if var_units == &quot;&quot;:
+                                var_units = latex_missing_string
+
+                        if var_description == &quot;&quot;:
+                                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: &amp; %s \\\\</font>
<font color="blue">'%var_arr_type)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Units: &amp; %s \\\\</font>
<font color="blue">'%var_units)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Dimension: &amp; %s \\\\</font>
<font color="blue">'%var_arr_dims)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Persistence: &amp; %s \\\\</font>
<font color="blue">'%var_persistence)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('                 Default Streams: &amp; %s \\\\</font>
<font color="blue">'%var_streams)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('                 Index in %s Array: &amp; %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: &amp; %s \\\\</font>
<font color="blue">'%var_path.replace('_','\_').replace('%','\%'))
+                        latex.write('                 \hline </font>
<font color="blue">')
+                        latex.write('                 Array Group: &amp; %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(&quot;var&quot;):
+                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 = &quot;None&quot;
+
+                        if int(struct_time_levs) &gt; 1:
+                                var_path = &quot;domain %% blocklist %% %s %% time_levs(:) %% %s %% %s&quot;%(struct_name, struct_name, var_name)
+                        else:
+                                var_path = &quot;domain %% blocklist %% %s %% %s&quot;%(struct_name, var_name)
+
+                        if var_units == &quot;&quot;:
+                                var_units = latex_missing_string
+
+                        if var_description == &quot;&quot;:
+                                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: &amp; %s \\\\</font>
<font color="blue">'%var_type)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Units: &amp; %s \\\\</font>
<font color="blue">'%var_units)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Dimension: &amp; %s \\\\</font>
<font color="blue">'%var_dims)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('        Persistence: &amp; %s \\\\</font>
<font color="blue">'%var_persistence)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('                 Default Streams: &amp; %s \\\\</font>
<font color="blue">'%var_streams)
+                        latex.write('        \hline </font>
<font color="blue">')
+                        latex.write('                 Location in code: &amp; %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>