tlist_diagnostics.py - pism - [fork] customized build of PISM, the parallel ice sheet model (tillflux branch)
 (HTM) git clone git://src.adamsgaard.dk/pism
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) LICENSE
       ---
       tlist_diagnostics.py (2381B)
       ---
            1 #!/usr/bin/env python3
            2 
            3 import json
            4 import argparse
            5 
            6 # file header
            7 header = """.. -*- mode: rst -*-
            8 
            9 .. DO NOT EDIT. This file was generated using list_diagnostics.py.
           10 """
           11 
           12 # section start
           13 section = """
           14 .. _{label}:
           15 
           16 {title}
           17 {underline}"""
           18 
           19 # start of an entry corresponding to a diagnostic
           20 entry_start = """
           21 #. ``{name}``"""
           22 
           23 # template used when a diagnostic corresponds to one NetCDF variable
           24 template_single = """
           25    :Units: {units}
           26    :Description: {long_name}"""
           27 
           28 # template used when a diagnostic corresponds to multiple NetCDF variables
           29 template_many = """
           30    - ``{var_name}``
           31 
           32      :Units: {units}
           33      :Description: {long_name}"""
           34 
           35 # standard_name line
           36 std_name = """{padding}:Standard name: ``{standard_name}``"""
           37 
           38 # comment line
           39 comment = """{padding}:Comment: {comment}"""
           40 
           41 def print_diagnostics(title, label, diagnostics):
           42 
           43     print(section.format(label=label, title=title, underline="-" * len(title)))
           44 
           45     for name in sorted(diagnostics.keys()):
           46         print(entry_start.format(name=name))
           47 
           48         if len(diagnostics[name]) == 1:
           49             template = template_single
           50             padding = " " * 3
           51         else:
           52             template = template_many
           53             padding = " " * 5
           54 
           55         for data in diagnostics[name]:
           56             var_name, units, long_name, standard_name, comment_string = data
           57             if len(units) == 0:
           58                 units = "---"
           59 
           60             print(template.format(var_name=var_name,
           61                                   units=units,
           62                                   long_name=long_name))
           63 
           64             if len(standard_name) > 0:
           65                 print(std_name.format(padding=padding, standard_name=standard_name))
           66 
           67             if len(comment_string) > 0:
           68                 print(comment.format(padding=padding, comment=comment_string))
           69 
           70 if __name__ == "__main__":
           71 
           72     parser = argparse.ArgumentParser()
           73     parser.description = '''Generate an RST list documenting PISM's diagnostics.'''
           74     parser.add_argument("FILE", nargs="*")
           75     options = parser.parse_args()
           76 
           77     spatial = {}
           78     scalar = {}
           79     for f in options.FILE:
           80         with open(f) as f:
           81             data = json.load(f)
           82             spatial.update(data["spatial"])
           83             scalar.update(data["scalar"])
           84 
           85     print(header)
           86 
           87     print_diagnostics("Spatially-variable fields", "sec-extra_vars", spatial)
           88     print_diagnostics("Scalar time-series",  "sec-ts_vars", scalar)