#!/usr/bin/perl
use Getopt::Long;
use IO::File;
#
# Make a namelist input file for vtgcm:
#
#&tgcm_input
# LABEL = 'vtgcm'
# START_YEAR = 1992
# START_DAY  = 211
# CALENDAR_ADVANCE = 0
# SOURCE= '$TGCMDATA/ABRECHT.V10.pequmin035nr2.ubc18.nc'
# SOURCE_START = 211,0,0
# DATAVOL = '$TGCMDATA/BOUGHER.SWBV97_zlb-16.IVNMAX.nc'
# START= 211,0,0
# STOP=  212,0,0
# HIST=  1,0,0
# STEP= 30
# OUTPUT='FOSTER.V10.pequmin035nr2.ubc18.nc'
# SECSTART =  211,1,0
# SECSTOP  =  212,0,0
# SECHIST  =  0,1,0
# SECOUT='FOSTER.V10.sequmin035nr2.ubc18.nc'
# SECFLDS = 'TN','UN','VN','O2','O1','CO','N4S','N2D','ENO','W','Z'
# MXHIST_SECH = 25
# MAG=0.,0.,0.,0.
# DIFHOR=0
# DYNAMO = 0
# TIDE=0.,0.,0.,0.,0.,0.,0.,0.,0.,0.
# TIDE2 = 0.,0.
# TIDEANN=0
# AURORA=0
# COLFAC=1.5
# F107=70.
# F107A=70.
# POWER=0.
# CTPOTEN=0.
# BYIMF=0.
# BZIMF=0.
#/
#
$user  = $ENV{"LOGNAME"};
($USER = $user) =~ tr/a-z/A-Z/;
$tgcmroot = $ENV{TGCMROOT}; 
$tgcmdata = $ENV{TGCMDATA}; 
#
# Defaults for mknamelist:
#
$model = "vtgcm";
$modelres  = 5.0;
#
# Set default namelist parameters:
#
&set_defaults;
#
# Get user options:
#
&GetOptions(
  "model=s"       => \$model,
  "fileout=s"     => \$fileout,
  "modelres=f"    => \$modelres,
  "comment=s@"    => \@comment,
  "uncomment=s@"  => \@uncomment,
  "h|help"        => \$help,
#
# Begin namelist parameter options:
#
  "LABEL=s"               => \$LABEL,
  "START_YEAR=i"          => \$START_YEAR,
  "START_DAY=i"           => \$START_DAY,
  "CALENDAR_ADVANCE=i"    => \$CALENDAR_ADVANCE,
  "SOURCE=s"              => \$SOURCE,
  "SOURCE_START=s"        => \$SOURCE_START,
  "DATAVOL=s"             => \$DATAVOL,
  "START=s"               => \$START,
  "STOP=s"                => \$STOP,
  "STEP=i"                => \$STEP,
  "HIST=s"                => \$HIST,
  "OUTPUT=s"              => \$OUTPUT,
  "MXHIST_PRIM=i"         => \$MXHIST_PRIM,
  "SECSTART=s"            => \$SECSTART,
  "SECSTOP=s"             => \$SECSTOP,
  "SECHIST=s"             => \$SECHIST,
  "SECOUT=s"              => \$SECOUT,
  "MXHIST_SECH=s"         => \$MXHIST_SECH,
  "SECFLDS=s"             => \$SECFLDS,
  "SECFLDS0=s"            => \$SECFLDS0,
  "SECFLDS1=s"            => \$SECFLDS1,
  "HPSS_PATH=s"           => \$HPSS_PATH,
  "DYNAMO=i"              => \$DYNAMO,
  "TIDE=s"                => \$TIDE,
  "TIDE2=s"               => \$TIDE2,
  "AURORA=i"              => \$AURORA,
  "COLFAC=f"              => \$COLFAC,
  "POWER=f"               => \$POWER,
  "CTPOTEN=f"             => \$CTPOTEN,
  "F107=f"                => \$F107,
  "F107A=f"               => \$F107A,
  "BYIMF=f"               => \$BYIMF,
  "BZIMF=f"               => \$BZIMF,
) or usage();
if ($help) { usage(); } # provide usage if help was requested
#
# Check for unparsed arguments:
#
if (@ARGV) {
    print STDERR ">>> $0: unrecognized arguments: @ARGV\n";
    usage();
}
#
# Validate resolution option:
#
if ($modelres != 5.0 and $modelres != 2.5) { 
  print STDERR "\n>>> Bad modelres: $modelres (must be either 5.0 or 2.5)\n";
  exit;
}
#
# Set resolution sensitive parameters:
#
$highres = 0;
if ($modelres == 2.5) { $highres = 1; }
&set_res_params($highres);
#
# Set 1 or 2 lines of SECFLDS:
#
if (defined $SECFLDS) { 
  $SECFLDS0 = $SECFLDS;
  undef $SECFLDS1;
} 
#
# Namelist parameters to be commented or uncommented:
#
foreach $usr_comment (@comment) {
  if (! defined $comments{$usr_comment}) {
    print ">>> WARNING: Cannot find parameter $usr_comment to set comment.\n"; 
  } else {
    $comments{$usr_comment} = ";";
  }
}
foreach $usr_uncomment (@uncomment) {
  if (! defined $comments{$usr_uncomment}) {
    print ">>> WARNING: Cannot find parameter $usr_uncomment to set uncomment.\n"; 
  } else {
    $comments{$usr_uncomment} = " ";
  }
}
#
# Name of output namelist file:
#
if (defined $fileout) {
  $namelist_file = $fileout;
} else { $namelist_file = $model . "_default.inp"; }
#
# Write the namelist output file:
#
&write_namelist($namelist_file);
#
print "$namelist_file\n";
exit;
#-------------------------------------------------------------------------
sub set_defaults {
#
# Default namelist parameters:
#
$LABEL="'$model res=$modelres'";
$START_YEAR = 1992;
$START_DAY  = 211;
$CALENDAR_ADVANCE = 0;
$SOURCE_START = "211,0,0";
$DATAVOL = "'\$TGCMDATA/BOUGHER.SWBV97_zlb-16.IVNMAX.nc'";
$START= "211,0,0";
$STOP=  "212,0,0";
$STEP= 30;
$HIST=  "1,0,0";
$MXHIST_PRIM = 10;
$SECSTART = "211,0,0";
$SECSTOP  = "212,0,0";
$SECHIST  = "1,0,0";
$MXHIST_SECH = 24;
$SECFLDS0 = "'TN','UN','VN','O2','O1','CO','N4S','N2D','ENO','W','Z'";
$SECFLDS1 = "";
$HPSS_PATH = "'/home/$user/$model'";
$DYNAMO = 0;
$TIDE   = "0.,0.,0.,0.,0.,0.,0.,0.,0.,0.";
$TIDE2  = "0.,0.";
$AURORA = 1;
$COLFAC = "1.5";
$F107   = "70.";
$F107A  = "70.";
$POWER  = "0.";
$CTPOTEN="0.";
$BYIMF  = "0.";
$BZIMF  = "0.";
#
# Set default comments:
#
%comments = (
  LABEL               =>" ",
  START_YEAR          =>" ",
  START_DAY           =>" ",
  CALENDAR_ADVANCE    =>" ",
  SOURCE              =>" ",
  SOURCE_START        =>" ",
  DATAVOL             =>" ",
  START               =>" ",
  STOP                =>" ",
  STEP                =>" ",
  HIST                =>" ",
  OUTPUT              =>" ",
  MXHIST_PRIM         =>" ",
  SECSTART            =>" ",
  SECSTOP             =>" ",
  SECHIST             =>" ",
  SECOUT              =>" ",
  MXHIST_SECH         =>" ",
  SECFLDS             =>" ",
  SECFLDS0            =>" ",
  SECFLDS1            =>" ",
  HPSS_PATH           =>";",
  DYNAMO              =>" ",
  TIDE                =>" ",
  TIDE2               =>" ",
  POWER               =>" ",
  CTPOTEN             =>" ",
  AURORA              =>" ",
  COLFAC              =>" ",
  F107                =>" ",
  F107A               =>" ", 
  BYIMF               =>" ",
  BZIMF               =>" ",
);

} # end sub set_defaults
#-------------------------------------------------------------------------
sub set_res_params {
  my ($highres) = @_;
  if (! defined($SOURCE)) {
    if ($highres) {
      $SOURCE = "'\$TGCMDATA/ABRECHT.V10.pequmin035nr2.ubc18.dres.nc'";
    } else {
      $SOURCE = "'\$TGCMDATA/ABRECHT.V10.pequmin035nr2.ubc18.nc'";
    }
  }
  if (! defined($OUTPUT)) {
    if ($highres) {
      $OUTPUT = "'" . $USER . "." . $model . "_dres.ptest001.nc'";
    } else {
      $OUTPUT = "'" . $USER . "." . $model . ".ptest001.nc'";
    }
  }
  if (! defined($SECOUT)) {
    if ($highres) {
      $SECOUT   = "'" . $USER . "." . $model . "_dres.stest001.nc'";
    } else {
      $SECOUT   = "'" . $USER . "." . $model . ".stest001.nc'";
    }
  }
  if (! defined($HPSS_PATH)) {
    $HPSS_PATH = "/home/$user/$model";
  }
} # end sub set_res_params
#-------------------------------------------------------------------------
sub write_namelist {
my ($namelist_file) = @_;
my $fh_out = new IO::File;
$fh_out->open(">$namelist_file") or die ">>> Can't open new file $namelist_file\n";

print $fh_out <<"EOF";
&tgcm_input
;
; Namelist input file for model $model
;
$comments{LABEL}LABEL = $LABEL
$comments{START_YEAR}START_YEAR = $START_YEAR
$comments{START_DAY}START_DAY  = $START_DAY
$comments{CALENDAR_ADVANCE}CALENDAR_ADVANCE = $CALENDAR_ADVANCE
;
; SOURCE:       Start-up history file (for initial runs only)
; SOURCE_START: Time of history on start-up file (initial runs only)
;
$comments{SOURCE}SOURCE = $SOURCE
$comments{SOURCE_START}SOURCE_START = $SOURCE_START
$comments{DATAVOL}DATAVOL = $DATAVOL
;
; START: Start time (day,hour,minute)
; STOP:  Stop time (day,hour,minute)
; STEP:  Timestep (seconds)
;
$comments{START}START = $START
$comments{STOP}STOP  = $STOP
$comments{STEP}STEP  = $STEP
;
; Primary History parameters:
;
$comments{HIST}HIST = $HIST
$comments{OUTPUT}OUTPUT = $OUTPUT
$comments{MXHIST_PRIM}MXHIST_PRIM = $MXHIST_PRIM
;
; Secondary History parameters:
;
$comments{SECSTART}SECSTART = $SECSTART
$comments{SECSTOP}SECSTOP  = $SECSTOP
$comments{SECHIST}SECHIST  =  $SECHIST
$comments{SECOUT}SECOUT   = $SECOUT
$comments{MXHIST_SECH}MXHIST_SECH = $MXHIST_SECH
$comments{SECFLDS}SECFLDS = $SECFLDS0
$comments{SECFLDS}          $SECFLDS1
;
; If HPSS_PATH is set, an hsi dispose script will be made (not executed)
; to copy history files to the NCAR HPSS in the directory HPSS_PATH.
;
$comments{HPSS_PATH}HPSS_PATH = $HPSS_PATH
;
$comments{DYNAMO}DYNAMO = $DYNAMO
$comments{TIDE}TIDE = $TIDE
$comments{TIDE2}TIDE2 = $TIDE2
$comments{AURORA}AURORA = $AURORA
$comments{COLFAC}COLFAC = $COLFAC
$comments{POWER}POWER   = $POWER
$comments{CTPOTEN}CTPOTEN = $CTPOTEN
$comments{F107}F107    = $F107
$comments{F107A}F107A   = $F107A
$comments{BYIMF}BYIMF   = $BYIMF
$comments{BZIMF}BZIMF   = $BZIMF
/
EOF

$fh_out->close;
#print STDERR "mknamelist: Wrote namelist file $namelist_file\n";
} # end sub write_namelist
#-------------------------------------------------------------------------
