#!/usr/bin/env perl 

use strict;

my @dirs = ('../../ccsm_utils/Tools/per5lib', '../../ccsm_utils/Tools/perl5lib/Build');
unshift @INC, @dirs;
require Build::NamelistDefinition;
use lib "../../ccsm_utils/Tools/perl5lib";

my $image_dir  = "./images";

print <<"END_of_Start";

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>CESM Component Models Namelist Definitions </title>
  <link rel="stylesheet" type="text/css" href="/models/cesm1.0/cam/docs/namelist/nl_style_sheet.css" />
  <script src=./showinfo.js> </script>
</head>

<body>

<h2>Search or Browse POP2 Component Model Namelist Variables</h2>
<p>
This page contains the complete list of POP2 namelist variables available.  They are grouped
by categories designed to aid browsing.  Clicking on the name of a variable will display descriptive
information.  If search terms are entered in the text box below, the list will be condensed to contain
only matched variables.
</p>

<form id="filter_form" name="filter_form" style="margin: 0px; padding: 0px;" action="javascript:void(0);">
  <table border="0" cellpadding="2" cellspacing="1">
    <tbody>
      <tr>
        <td valign="top">
          <input id="filter_text" name="filter_text" size="40"
                 onkeydown="if (event.keyCode==13) applyFilter(document.getElementById('filter_text').value);"
                 type="text">
          <input id="btn_search" value="Search Variable Names"
                 onclick="applyFilter(document.getElementById('filter_text').value);"
		 type="button">
          <input id="btn_show_all" value="Show All Variable Names"
		 onclick="clearFilter();return false;"
                 type="button">
          <br>
          <label>
            <input id="logical_operator_and" name="logical_operator" value="AND" 
                   type="radio" checked> AND
          </label>
          <label>
            <input id="logical_operator_or" name="logical_operator" value="OR"
                   type="radio"> OR
          </label>
          (separate search terms with spaces)
          <br>
          <label>
            <input id="search_help_text" name="search_help_text" type="checkbox"> Also search help text
          </label>
        </td>
      </tr>
    </tbody>
  </table>
</form>

<div id="filter_matches" style="visibility: hidden; margin-bottom: 10px;">
  Found <span id="filter_matches_num"></span> standard names matching query: <span id="filter_matches_query"></span>
</div>

<p>

<center>
<input id="btn_expand_help" value="Show All Help Text" 
       onclick="expandAllHelp();return false;"
       type="button">
<input id="btn_collapse_help" value="Collapse All Help Text" 
       onclick="collapseAllHelp();return false;"
       type="button">
</center>

END_of_Start

my $nldef_file = "../../../models/ocn/pop2/bld/namelist_files/namelist_definition_pop2.xml";
my $nldef = Build::NamelistDefinition->new($nldef_file);

# Get list of categories in the definition file.
my @nldef_cats = $nldef->get_categories();

# Construct hash from @nldef_cats and use it to keep track of
# the categories that aren't treated explicitly below, then add them to the
# table at the end.
my %nldef_cats = ();
foreach my $k (@nldef_cats) {
    $nldef_cats{$k} = '';
}

# The default output will organize the namelist variables according to the
# "category" attribute which is part of the definition.  The ordering of the
# categories is set in the @categories array.  The table headings are defined
# in the %cat_heading hash (order is not important in the hash definition).

my @categories = qw/
decomp
grid
io
timemgr
init_ts
diag
restart
history
solvers
vmix
kpp
tidal
hmix
state
baroclinic
ice
presgrad
topostress
forcing
coupled
swabs
transdiag
context
overflows
tavg
passive_tracers
ecosys
/;

my %cat_heading = (
    'decomp'     => '<a name="decomp"   >POP2:  Decomp Settings</a>',
    'grid'       => '<a name="grid"     >POP2:  Grid Settings</a>',
    'io'         => '<a name="ioset"    >POP2:  IO Settings</a>',
    'timemgr'    => '<a name="tman"     >POP2:  Time Manager Settings</a>',
    'init_ts'    => '<a name="ts"       >POP2:  TS Initialization</a>',
    'diag'       => '<a name="diag"     >POP2:  Diagnostics</a>',
    'restart'    => '<a name="rest"     >POP2:  Restart Control</a>',
    'history'    => '<a name="hist"     >POP2:  History Output</a>',
    'solvers'    => '<a name="solv"     >POP2:  Solvers</a>',
    'vmix'       => '<a name="vmix"     >POP2:  Vertical Mixing</a>',
    'kpp'        => '<a name="kpp"      >POP2:  KPP</a>',
    'tidal'      => '<a name="tidal"    >POP2:  Tidal</a>',
    'advect'     => '<a name="advect"   >POP2:  Advect</a>',
    'hmix'       => '<a name="hmix"     >POP2:  Horizontal Mixing</a>',
    'state'      => '<a name="state"    >POP2:  State Settings</a>',
    'baroclinic' => '<a name="baro"     >POP2:  Baroclinic</a>',
    'ice'        => '<a name="ice"      >POP2:  Ice</a>',
    'presgrad'   => '<a name="press"    >POP2:  Pressure Gradient</a>',
    'topostress' => '<a name="topos"    >POP2:  Topo Stress</a>',
    'forcing'    => '<a name="forc"     >POP2:  Forcing</a>',
    'coupled'    => '<a name="coupling" >POP2:  Coupling Control</a>', 
    'swabs'      => '<a name="swave"    >POP2:  Shortwave Absorbtion</a>', 
    'transdiag'  => '<a name="transport">POP2:  Transport Diagnostic Control</a>',
    'context'    => '<a name="context"  >POP2:  Context for Coupling POP2</a>',
    'overflows'  => '<a name="overflows">POP2:  Overflows</a>',
    'tavg'       => '<a name="tavg"     >POP2:  TAVG Settings</a>',
    'ecosys'     => '<a name="eco"      >POP2:  Ecosystem Settings</a>',
    'passive_tracers' =>'<a name="passive" >POP2: Passive Tracer Settings</a>',
    );

foreach my $cat (@categories) {

    unless ($cat_heading{$cat} eq 'exclude') {

	# Print table
	print_start_table($cat, $cat_heading{$cat});

	# get alphabetized list all the variable names in the category
	my @vars = $nldef->get_var_names('category'=>$cat);

	# get corresponding type and documentation
	foreach my $var (@vars) {
	    my $var_local = $var;
	    my $type = $nldef->get_var_type($var);
	    my $doc  = $nldef->get_var_doc_html($var);
	    my $grp  = $nldef->get_group_name($var);
	    if ($grp eq 'derived') {
		my @broken = split(/&/,$var);
		$var_local = $broken[0];
		$grp = $broken[1];
	    }
	    print_row($var_local, $type, $doc, $grp);
	}

	# Finish table
	print_end_table();
    }

    $nldef_cats{$cat} = 'done';

}


# Finish
print <<"END_of_html";
</body>
</html>
END_of_html

#--------------------------------------------------------------------------------------------

sub print_start_table {
    my $category = shift;
    my $hdr      = shift;

print <<"START_table";
<h3><span style="background-color: #00FFFF" font color="purple">$hdr</h3></span>
<table id="${category}_table" border="1" width="100%" cellpadding="2" cellspacing="0">
  <th width="80%">Namelist Variable</th>
  <th width="10%">Type</th>
  <th width="10%">Group</th>
START_table
}

#--------------------------------------------------------------------------------------------

sub print_row {

    my $name = shift;
    my $type = shift;
    my $doc  = shift;
    my $grp  = shift;

print <<"END_of_row";
  <tr id="${name}_tr">
    <td><a name="${name}_${grp}"></a>
        <img id="${name}_arrow" src="$image_dir/arrow_right.gif">
        <code class="varname">
          <a href="javascript:void(0)"
             onclick="toggleHelp('${name}_${grp}')">$name</a>
        </code>
        <div id="${name}_${grp}_help" style="display: none;
             padding-left: 16px; margin-top: 4px; border-top: 1px dashed
             #cccccc;">
	     <pre>$doc</pre>
        </div>
    </td>
    <td>$type</td>
    <td>$grp</td>
  </tr>
END_of_row
}

#--------------------------------------------------------------------------------------------

sub print_end_table {

print <<"END_table";
</table>
END_table
}

#--------------------------------------------------------------------------------------------

