Пример #1
0
def snapshot_vars_to_latex(path=os.curdir):
    """Write LaTeX code describing the variables of a snapshot.

    The file snapshot_vars.tex is outputted by this function, which
    lists as a table the variables saved in a snapshot.

    This method works best if this module is located in the same
    directory as path, i.e., that the output written out from the
    method is also in path.

    Keyword Input Parameter:
    * path:  Path of the location to write out the files containing
      the LaTeX tables to, specified in file.  String.  Default is
      os.curdir.
    """
    #- Make a model 2 day run to get variables to take a snapshot of.
    #  Use the boundary conditions in ../../test/bnddir:

    model = qtcm.Qtcm(compiled_form='parts')
    rundirname = 'test'
    dirbasepath = tempfile.mkdtemp()
    model.outdir.value = dirbasepath
    model.runname.value = rundirname
    model.bnddir.value = \
        os.path.join( path, os.pardir, os.pardir, 'test',
                      'bnddir', 'r64x42' )
    model.SSTdir.value = \
        os.path.join( path, os.pardir, os.pardir, 'test',
                      'bnddir', 'r64x42', 'SST_Reynolds' )
    model.lastday.value = 2
    model.run_session()
    model.make_snapshot()

    if os.path.exists(dirbasepath): shutil.rmtree(dirbasepath)
    if os.path.exists('qtcm_00010102.restart'):
        os.remove('qtcm_00010102.restart')

    #- Create table:

    header = """
\\begin{longtable}{l|c|c|p{0.4\\linewidth}}
\\textbf{Field} & \\textbf{Shape} &
                                \\textbf{Units} & \\textbf{Description} \\\\
\\hline
\\endhead
    """

    body = ''
    sorted_keys = model.snapshot.keys()
    sorted_keys.sort()
    for ikey in sorted_keys:
        ivar = model.snapshot[ikey]  #+ Set variable
        long_name = str(ivar.long_name).strip()  #+ Capitalize
        if len(long_name) > 0:  #  first letter
            long_name0 = long_name[0].capitalize()  #  of long_name
            long_name = long_name0 + long_name[1:]

        body = body \
            + "\\vars{" + ivar.id + "} & "

        if N.isscalar(ivar.value):  #+ Add value shape
            body = body + "     & "
        else:
            body = body + str(N.shape(ivar.value)).strip() + " & "

        body = body \
            + ivar.units.strip() + " & " \
            + long_name + " \\\\\n"

    footer = """\\end{longtable}"""

    output = header + body + footer

    #- Replace special LaTeX characters.  The characters must be
    #  replaced in the order given in replace_list, otherwise there
    #  will be errors.  Set header comment line:

    replace_dict = {
        '%': '\\%',
        '$': '\\$',
        '#': '\\#',
        '_': '\\_',
        '^2': '$^2$',
        '^3': '$^3$',
        '~': '$\\sim$',
        '<': '$<$',
        '>': '$>$'
    }
    replace_list = ['%', '$', '#', '_', '^2', '^3', '~', '<', '>']

    for istr in replace_list:
        output = output.replace(istr, replace_dict[istr])

    #- Add comment line at top and write file:

    output = """% This file is automatically generated by
% code_to_latex.py.  It lists all the snapshot variables.

""" + output

    fn = os.path.join(path, 'snapshot_vars.tex')
    fileobj = open(fn, mode='w')
    fileobj.write(output)
    fileobj.close()
Пример #2
0
def snapshot_vars_to_latex(path=os.curdir):
    """Write LaTeX code describing the variables of a snapshot.

    The file snapshot_vars.tex is outputted by this function, which
    lists as a table the variables saved in a snapshot.

    This method works best if this module is located in the same
    directory as path, i.e., that the output written out from the
    method is also in path.

    Keyword Input Parameter:
    * path:  Path of the location to write out the files containing
      the LaTeX tables to, specified in file.  String.  Default is
      os.curdir.
    """
    #- Make a model 2 day run to get variables to take a snapshot of.
    #  Use the boundary conditions in ../../test/bnddir:

    model = qtcm.Qtcm(compiled_form='parts')
    rundirname = 'test'
    dirbasepath = tempfile.mkdtemp()
    model.outdir.value = dirbasepath
    model.runname.value = rundirname
    model.bnddir.value = \
        os.path.join( path, os.pardir, os.pardir, 'test', 
                      'bnddir', 'r64x42' )
    model.SSTdir.value = \
        os.path.join( path, os.pardir, os.pardir, 'test',
                      'bnddir', 'r64x42', 'SST_Reynolds' )
    model.lastday.value = 2
    model.run_session()
    model.make_snapshot()

    if os.path.exists(dirbasepath):  shutil.rmtree(dirbasepath)
    if os.path.exists('qtcm_00010102.restart'):
        os.remove('qtcm_00010102.restart')


    #- Create table:

    header = """
\\begin{longtable}{l|c|c|p{0.4\\linewidth}}
\\textbf{Field} & \\textbf{Shape} &
                                \\textbf{Units} & \\textbf{Description} \\\\
\\hline
\\endhead
    """

    body = ''
    sorted_keys = model.snapshot.keys()
    sorted_keys.sort()
    for ikey in sorted_keys:
        ivar = model.snapshot[ikey]                    #+ Set variable
        long_name = str(ivar.long_name).strip()        #+ Capitalize
        if len(long_name) > 0:                         #  first letter
            long_name0 = long_name[0].capitalize()     #  of long_name
            long_name = long_name0 + long_name[1:]

        body = body \
            + "\\vars{" + ivar.id + "} & "

        if N.isscalar(ivar.value):                     #+ Add value shape
            body = body + "     & "
        else:
            body = body + str(N.shape(ivar.value)).strip() + " & "

        body = body \
            + ivar.units.strip() + " & " \
            + long_name + " \\\\\n"

    footer = """\\end{longtable}"""

    output = header + body + footer


    #- Replace special LaTeX characters.  The characters must be
    #  replaced in the order given in replace_list, otherwise there
    #  will be errors.  Set header comment line:

    replace_dict = {'%':'\\%', '$':'\\$', '#':'\\#', '_':'\\_', 
                    '^2':'$^2$', '^3':'$^3$',
                    '~':'$\\sim$', '<':'$<$', '>':'$>$'}
    replace_list = ['%', '$', '#', '_', '^2', '^3', '~', '<', '>']

    for istr in replace_list:
        output = output.replace(istr, replace_dict[istr])


    #- Add comment line at top and write file:

    output = """% This file is automatically generated by
% code_to_latex.py.  It lists all the snapshot variables.

""" + output

    fn = os.path.join(path, 'snapshot_vars.tex')
    fileobj = open(fn, mode='w')
    fileobj.write(output)
    fileobj.close()