#!/usr/bin/perl
use Getopt::Long;
#
# Make namelist input files for control (seasonal for solar min,max) test runs, by calling mknamelist.
# Because source and output files are constructed for each of the 8 cases, they are not options.
# If you need to change source or output files, do it below, inside the case loop.
#
$tgcmdata   = $ENV{TGCMDATA};                   # $TGCMDATA env var (for source files)
$model      = "tiegcm";                         # model name (used to construct source and output files)
$version    = "${model}_trunk";                 # used to construct output files (arbitrary)
$modeldir   = "../..";                          # model root dir used to find default mknamelist script
$mknamelist = "$modeldir/scripts/mknamelist";   # path to mknamelist script
$modelres   = "5.0";                            # either 5.0 or 2.5 degree resolution
$source_version = "tiegcm1.95"; # to construct source files for each case (change below if necessary)
#
# Process options provided by user to override defaults:
#
&GetOptions(
  "h|help"        => \$help,
  "model=s"       => \$model,
  "version=s"     => \$version,
  "modeldir=s"    => \$modeldir,
  "mknamelist=s"  => \$mknamelist,
  "modelres=s"    => \$modelres,
) or usage();
if ($help) { usage(); }
#
# Check for existence of mknamelist. Also check $modeldir/scripts/mknamelist in case
# the user changed modeldir but not mknamelist. 
#
if (not -e $mknamelist) { 
  if (not -e "$modeldir/scripts/mknamelist") {
    die "Could not find mknamelist $mknamelist or $modeldir/scripts/mknamelist\n";
  } else {
    $mknamelist = "$modeldir/scripts/mknamelist";
  }
}
if (not -e $modeldir   ) { die "Could not find modeldir $modeldir\n"; }
if (not -e "$modeldir/src")     { die "Could not find model src directory $modeldir/src\n"; }
if (not -e "$modeldir/scripts") { die "Could not find model scripts directory $modeldir/scripts\n"; }
#
# Set solar min and solar max cases:
#
$smax = "-POWER=39. -CTPOTEN=60. -F107=200. -F107A=200.";
$smin = "-POWER=18. -CTPOTEN=30. -F107=70. -F107A=70.";
#
# Secondary history fields:
#
$secflds0 ="\"TN\",\"UN\",\"VN\",\"O2\",\"O1\",\"N2\",\"NO\",\"N4S\",\"HE\",\"NE\",\"TE\",\"TI\",";
$secflds1 ="\"TEC\",\"O2P\",\"OMEGA\",\"POTEN\",\"UI_ExB\",\"VI_ExB\",\"WI_ExB\",\"DEN\",";
$secflds2 ="\"QJOULE\",\"Z\",\"ZG\"";
#
# Case names for both equinoxes and solstices, each with smin and smax.
# Case names are used with version to set source and output file names.
#
@cases = ("decsol_smax","decsol_smin","junsol_smax","junsol_smin",
          "mareqx_smax","mareqx_smin","sepeqx_smax","sepeqx_smin");
#
# Set start,stop times for each case:
#
foreach $case (@cases) {
  if ($case eq "decsol_smax" or $case eq "decsol_smin") {
    $source_start = "355,0,0";
    $start_day = "355";
    $start = "355,0,0";
    $stop  = "360,0,0";
    $secstart = "355,1,0";
    $secstop = "360,0,0";
  } elsif ($case eq "junsol_smax" or $case eq "junsol_smin") {
    $source_start = "172,0,0";
    $start_day = "172";
    $start = "172,0,0";
    $stop  = "177,0,0";
    $secstart = "172,1,0";
    $secstop = "177,0,0";
  } elsif ($case eq "mareqx_smax" or $case eq "mareqx_smin") {
    $source_start = "80,0,0";
    $start_day = "80";
    $start = "80,0,0";
    $stop  = "85,0,0";
    $secstart = "80,1,0";
    $secstop = "85,0,0";
  } elsif ($case eq "sepeqx_smax" or $case eq "sepeqx_smin") {
    $source_start = "264,0,0";
    $start_day = "264";
    $start = "264,0,0";
    $stop  = "269,0,0";
    $secstart = "264,1,0";
    $secstop = "269,0,0";
  } else {
    die "Could not set start,stop times for case $case\n";
  }

  if ($case =~ /smax/) {
    $solar = "$smax";
  } elsif ($case =~ /smin/) {
    $solar = "$smin";
  } else {
    die "Could not set solar for case $case\n";
  }
#
  $label    = "\"$model $case seasonal control run\"";
  $source   = "\"$tgcmdata/$source_version/TGCM.${source_version}.pcntr_${case}.nc\"";
  $output   = "\"${version}.pcntr_${case}_001.nc\"";
  $secout   ="\"${version}.scntr_${case}_001.nc\",\"to\",\"${version}.scntr_${case}_005.nc\",\"by\",\"1\"";
  if ($modelres eq "2.5") {
    $source = "\"$tgcmdata/${source_version}/TGCM.${source_version}_dres.pcntr_${case}.nc\"";
    $output = "\"${version}_dres.pcntr_${case}_001.nc\"";
    $secout ="\"${version}_dres.scntr_${case}_001.nc\",\"to\",\"${version}_dres.scntr_${case}_005.nc\",\"by\",\"1\"";
  }

  $fileout = "${model}_${case}.inp";

  $command = "$mknamelist -fileout=$fileout -modelres=$modelres";
  $command = $command . " -LABEL=\'$label\' -START_DAY=$start_day -SOURCE=\'$source\'";
  $command = $command . " -SOURCE_START=$source_start -START=$start -STOP=$stop";
  $command = $command . " -HIST='1,0,0' -OUTPUT=\'$output\'";
  $command = $command . " -SECSTART=$secstart -SECSTOP=$secstop -SECHIST='0,1,0'";
  $command = $command . " -SECOUT=\'$secout\' -MXHIST_SECH=24";
  $command = $command . " -SECFLDS0=\'$secflds0\' -SECFLDS1=\'$secflds1\' -SECFLDS2=\'$secflds2\'";
  $command = $command . " $solar";


# print "$command\n";
  print "Making job script ${model}_${case}.inp\n";

  `$command`;

} # foreach $case (@cases)

#-----------------------------------------------------------------------
sub usage {
  print "\nThe perl script 'make_inputs' makes a series of namelist input files for the model.\n";
  print "This version makes 8 input files for seasonal control runs at solar min and solar max conditions.\n";
  print "\nDescription of options:\n";
  print <<EOF;
  -h|help       Print usage message and quit
  -model	Name of model (usually tiegcm or timegcm)
  -version      Version string (used only to construct source file)
  -modeldir     Root directory of model source
  -modelres     Model resolution (either 5.0 or 2.5)
  -mknamelist   Full path to the perl script 'mknamelist' (default $modeldir/scripts/mknamelist)
EOF
  print "\nHere are the options and their current values\n";
  print "(note there must be no white space before or after the '=' sign)\n";
  print "  -model=$model\n"; 
  print "  -version=$version\n"; 
  print "  -modeldir=$modeldir\n"; 
  print "  -modelres=$modelres\n"; 
  print "  -mknamelist=$mknamelist\n\n"; 
  exit;
}
