def write_defaults_fields_to_latex(path=os.curdir): """Write LaTeX table of default fields, as defined in defaults. Information regarding the default fields for Python accessible QTCM variables are written out to a LaTeX table. Two tables are output, one with scalars and the other with arrays. The two files written out are named defaults_scalars.tex and defaults_arrays.tex, in the path path. 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. Numerical values are rounded using the %g format code. 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. """ #- Set LaTeX file headers: header_scalars = """ \\begin{longtable}{l|c|c|p{0.30\\linewidth}} \\textbf{Field} & \\textbf{Value} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ header_arrays = """ \\begin{longtable}{l|c|c|c|c|p{0.37\\linewidth}} \\textbf{Field} & \\textbf{Shape} & \\textbf{Max} & \\textbf{Min} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ #- Write body for table: qtcm_fields_ids_sorted = copy.copy(qtcm_fields_ids) qtcm_fields_ids_sorted.sort() body_scalars = '' body_arrays = '' for ikey in qtcm_fields_ids_sorted: long_name = qtcm_fields[ikey]['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:] if N.rank(qtcm_fields[ikey]['value']) == 0: value = qtcm_fields[ikey]['value'] if type(value) == type('a'): #+ Round values as needed value = value.strip() else: value = ('%g' % value).strip() addline = '\\vars{' + ikey.strip() + '} & ' + \ value + ' & ' + \ str(qtcm_fields[ikey]['units']).strip() + ' & ' + \ long_name + ' \\\\\n' body_scalars = body_scalars + addline else: shpval = N.shape(qtcm_fields[ikey]['value']) maxval = N.max(qtcm_fields[ikey]['value']) minval = N.min(qtcm_fields[ikey]['value']) addline = '\\vars{' + ikey.strip() + '} & ' + \ str(shpval).strip() + ' & ' + \ ('%g' % maxval).strip() + ' & ' + \ ('%g' % minval).strip() + ' & ' + \ str(qtcm_fields[ikey]['units']).strip() + ' & ' + \ long_name + ' \\\\\n' body_arrays = body_arrays + addline #- 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: body_scalars = body_scalars.replace(istr, replace_dict[istr]) body_arrays = body_arrays.replace(istr, replace_dict[istr]) header_comment = """% This file is automatically generated by the script % code_to_latex.py in the doc/latex directory. It is based upon % the values found in the qtcm.defaults submodule, and should % not be hand-edited if you want the values to correspond to % the values in the qtcm.defaults submodule. """ #- Write footer for table: footer = """\\end{longtable} """ #- Check there are no occurences of ^ that are not math-related: for istr in [ header_comment, header_scalars, body_scalars, header_arrays, body_arrays, footer ]: numcaret = istr.count('^') nummath = istr.count('$^') if numcaret != nummath: raise ValueError, 'unhandled carets exist' #- Write out to files: fn = os.path.join(path, 'defaults_scalars.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_scalars + body_scalars + footer) fileobj.close() fn = os.path.join(path, 'defaults_arrays.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_arrays + body_arrays + footer) fileobj.close()
def write_parts_init_to_latex(path=os.curdir): """Write LaTeX table of initial field values. The "initial field values" are the state of all the fields that are Python-accessible prior to the beginning of looping at the atmosphere-ocean coupling step. Two tables are output, one with scalars and the other with arrays. The two files written out are init_scalars.tex and init_arrays.tex, in the path path. Note that this method assumes that model boundary condition files are in ../../test/bnddir, relative to path, which is the directory configuration if this script is in doc/latex and bnddir is in ../../test. This method conducts the beginnings of a model run with output written to a temporary file generated by tempfile.mkdtemp(). The temporary directory is deleted at the end of this partial model run. 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. Numerical values are rounded using the %g format code. 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 "run", at least as far as to get initialization. # 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 = 0 model.run_session() if os.path.exists(dirbasepath): shutil.rmtree(dirbasepath) if os.path.exists('qtcm_00010101.restart'): os.remove('qtcm_00010101.restart') #- Set LaTeX file headers: header_scalars = """ \\begin{longtable}{l|c|c|p{0.42\\linewidth}} \\textbf{Field} & \\textbf{Value} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ header_arrays = """ \\begin{longtable}{l|c|c|c|c|p{0.3\\linewidth}} \\textbf{Field} & \\textbf{Shape} & \\textbf{Max} & \\textbf{Min} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ #- Write body for table: qtcm_fields_ids_sorted = copy.copy(qtcm_fields_ids) qtcm_fields_ids_sorted.sort() body_scalars = '' body_arrays = '' for ikey in qtcm_fields_ids_sorted: ifield = getattr(model, ikey) long_name = str(ifield.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:] if N.rank(ifield.value) == 0: if type(ifield.value) == type('a'): if ifield.id != 'SSTmode': value = 'value varies' else: value = ifield.value.strip() else: value = '%g' % ifield.value #+ round value value = value.strip() addline = '\\vars{' + ikey.strip() + '} & ' + \ value + ' & ' + \ str(ifield.units).strip() + ' & ' + \ long_name + ' \\\\\n' body_scalars = body_scalars + addline else: shpval = N.shape(ifield.value) maxval = N.max(ifield.value) minval = N.min(ifield.value) addline = '\\vars{' + ikey.strip() + '} & ' + \ str(shpval).strip() + ' & ' + \ ('%g' % maxval).strip() + ' & ' + \ ('%g' % minval).strip() + ' & ' + \ str(ifield.units).strip() + ' & ' + \ long_name + ' \\\\\n' body_arrays = body_arrays + addline #- 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: body_scalars = body_scalars.replace(istr, replace_dict[istr]) body_arrays = body_arrays.replace(istr, replace_dict[istr]) header_comment = """% This file is automatically generated by the script % code_to_latex.py in the doc/latex directory. It is based % upon the values found after model initialization, and should % not be hand-edited if you want the values to correspond to % the values in a Qtcm instance, for compiled_form='parts'. """ #- Write footer for table: footer = """\\end{longtable} """ #- Check there are no occurences of ^ that are not math-related: for istr in [ header_comment, header_scalars, body_scalars, header_arrays, body_arrays, footer ]: numcaret = istr.count('^') nummath = istr.count('$^') if numcaret != nummath: raise ValueError, 'unhandled carets exist' #- Write out to files and delete model instance: fn = os.path.join(path, 'init_scalars.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_scalars + body_scalars + footer) fileobj.close() fn = os.path.join(path, 'init_arrays.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_arrays + body_arrays + footer) fileobj.close() del model
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()
def write_defaults_fields_to_latex(path=os.curdir): """Write LaTeX table of default fields, as defined in defaults. Information regarding the default fields for Python accessible QTCM variables are written out to a LaTeX table. Two tables are output, one with scalars and the other with arrays. The two files written out are named defaults_scalars.tex and defaults_arrays.tex, in the path path. 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. Numerical values are rounded using the %g format code. 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. """ #- Set LaTeX file headers: header_scalars = """ \\begin{longtable}{l|c|c|p{0.30\\linewidth}} \\textbf{Field} & \\textbf{Value} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ header_arrays = """ \\begin{longtable}{l|c|c|c|c|p{0.37\\linewidth}} \\textbf{Field} & \\textbf{Shape} & \\textbf{Max} & \\textbf{Min} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ #- Write body for table: qtcm_fields_ids_sorted = copy.copy(qtcm_fields_ids) qtcm_fields_ids_sorted.sort() body_scalars = '' body_arrays = '' for ikey in qtcm_fields_ids_sorted: long_name = qtcm_fields[ikey]['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:] if N.rank(qtcm_fields[ikey]['value']) == 0: value = qtcm_fields[ikey]['value'] if type(value) == type('a'): #+ Round values as needed value = value.strip() else: value = ('%g' % value).strip() addline = '\\vars{' + ikey.strip() + '} & ' + \ value + ' & ' + \ str(qtcm_fields[ikey]['units']).strip() + ' & ' + \ long_name + ' \\\\\n' body_scalars = body_scalars + addline else: shpval = N.shape(qtcm_fields[ikey]['value']) maxval = N.max(qtcm_fields[ikey]['value']) minval = N.min(qtcm_fields[ikey]['value']) addline = '\\vars{' + ikey.strip() + '} & ' + \ str(shpval).strip() + ' & ' + \ ('%g' % maxval).strip() + ' & ' + \ ('%g' % minval).strip() + ' & ' + \ str(qtcm_fields[ikey]['units']).strip() + ' & ' + \ long_name + ' \\\\\n' body_arrays = body_arrays + addline #- 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: body_scalars = body_scalars.replace(istr, replace_dict[istr]) body_arrays = body_arrays.replace(istr, replace_dict[istr]) header_comment = """% This file is automatically generated by the script % code_to_latex.py in the doc/latex directory. It is based upon % the values found in the qtcm.defaults submodule, and should % not be hand-edited if you want the values to correspond to % the values in the qtcm.defaults submodule. """ #- Write footer for table: footer = """\\end{longtable} """ #- Check there are no occurences of ^ that are not math-related: for istr in [header_comment, header_scalars, body_scalars, header_arrays, body_arrays, footer]: numcaret = istr.count('^') nummath = istr.count('$^') if numcaret != nummath: raise ValueError, 'unhandled carets exist' #- Write out to files: fn = os.path.join(path, 'defaults_scalars.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_scalars + body_scalars + footer) fileobj.close() fn = os.path.join(path, 'defaults_arrays.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_arrays + body_arrays + footer) fileobj.close()
def write_parts_init_to_latex(path=os.curdir): """Write LaTeX table of initial field values. The "initial field values" are the state of all the fields that are Python-accessible prior to the beginning of looping at the atmosphere-ocean coupling step. Two tables are output, one with scalars and the other with arrays. The two files written out are init_scalars.tex and init_arrays.tex, in the path path. Note that this method assumes that model boundary condition files are in ../../test/bnddir, relative to path, which is the directory configuration if this script is in doc/latex and bnddir is in ../../test. This method conducts the beginnings of a model run with output written to a temporary file generated by tempfile.mkdtemp(). The temporary directory is deleted at the end of this partial model run. 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. Numerical values are rounded using the %g format code. 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 "run", at least as far as to get initialization. # 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 = 0 model.run_session() if os.path.exists(dirbasepath): shutil.rmtree(dirbasepath) if os.path.exists('qtcm_00010101.restart'): os.remove('qtcm_00010101.restart') #- Set LaTeX file headers: header_scalars = """ \\begin{longtable}{l|c|c|p{0.42\\linewidth}} \\textbf{Field} & \\textbf{Value} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ header_arrays = """ \\begin{longtable}{l|c|c|c|c|p{0.3\\linewidth}} \\textbf{Field} & \\textbf{Shape} & \\textbf{Max} & \\textbf{Min} & \\textbf{Units} & \\textbf{Description} \\\\ \\hline \\endhead """ #- Write body for table: qtcm_fields_ids_sorted = copy.copy(qtcm_fields_ids) qtcm_fields_ids_sorted.sort() body_scalars = '' body_arrays = '' for ikey in qtcm_fields_ids_sorted: ifield = getattr(model, ikey) long_name = str(ifield.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:] if N.rank(ifield.value) == 0: if type(ifield.value) == type('a'): if ifield.id != 'SSTmode': value = 'value varies' else: value = ifield.value.strip() else: value = '%g' % ifield.value #+ round value value = value.strip() addline = '\\vars{' + ikey.strip() + '} & ' + \ value + ' & ' + \ str(ifield.units).strip() + ' & ' + \ long_name + ' \\\\\n' body_scalars = body_scalars + addline else: shpval = N.shape(ifield.value) maxval = N.max(ifield.value) minval = N.min(ifield.value) addline = '\\vars{' + ikey.strip() + '} & ' + \ str(shpval).strip() + ' & ' + \ ('%g' % maxval).strip() + ' & ' + \ ('%g' % minval).strip() + ' & ' + \ str(ifield.units).strip() + ' & ' + \ long_name + ' \\\\\n' body_arrays = body_arrays + addline #- 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: body_scalars = body_scalars.replace(istr, replace_dict[istr]) body_arrays = body_arrays.replace(istr, replace_dict[istr]) header_comment = """% This file is automatically generated by the script % code_to_latex.py in the doc/latex directory. It is based % upon the values found after model initialization, and should % not be hand-edited if you want the values to correspond to % the values in a Qtcm instance, for compiled_form='parts'. """ #- Write footer for table: footer = """\\end{longtable} """ #- Check there are no occurences of ^ that are not math-related: for istr in [header_comment, header_scalars, body_scalars, header_arrays, body_arrays, footer]: numcaret = istr.count('^') nummath = istr.count('$^') if numcaret != nummath: raise ValueError, 'unhandled carets exist' #- Write out to files and delete model instance: fn = os.path.join(path, 'init_scalars.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_scalars + body_scalars + footer) fileobj.close() fn = os.path.join(path, 'init_arrays.tex') fileobj = open(fn, mode='w') fileobj.write(header_comment + header_arrays + body_arrays + footer) fileobj.close() del model
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()