#!/usr/bin/perl
use Getopt::Long;
use IO::File;
#
# Make a namelist input file for tiegcm:
#
$user  = $ENV{"LOGNAME"};
($USER = $user) =~ tr/a-z/A-Z/;
$tgcmroot = $ENV{TGCMROOT}; 
$tgcmdata = $ENV{TGCMDATA}; 
#
# Defaults for mknamelist:
#
$model = "tiegcm";
$version = "";
$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,
  "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,
  "SECFLDS2=s"            => \$SECFLDS2,
  "HPSS_PATH=s"           => \$HPSS_PATH,
  "TIDE=s"                => \$TIDE,
  "TIDE2=s"               => \$TIDE2,
  "GSWM_MI_DI_NCFILE=s"   => \$GSWM_MI_DI_NCFILE,
  "GSWM_MI_SDI_NCFILE=s"  => \$GSWM_MI_SDI_NCFILE,
  "GSWM_NM_DI_NCFILE=s"   => \$GSWM_NM_DI_NCFILE,
  "GSWM_NM_SDI_NCFILE=s"  => \$GSWM_NM_SDI_NCFILE,
  "SABER_NCFILE=s"        => \$SABER_NCFILE,
  "TIDI_NCFILE=s"         => \$TIDI_NCFILE,
  "POTENTIAL_MODEL=s"     => \$POTENTIAL_MODEL,
  "GPI_NCFILE=s"          => \$GPI_NCFILE,
  "IMF_NCFILE=s"          => \$IMF_NCFILE,
  "KP=f"                  => \$KP,
  "POWER=f"               => \$POWER,
  "CTPOTEN=f"             => \$CTPOTEN,
  "F107=f"                => \$F107,
  "F107A=f"               => \$F107A,
  "BXIMF=f"               => \$BXIMF,
  "BYIMF=f"               => \$BYIMF,
  "BZIMF=f"               => \$BZIMF,
  "SWVEL=f"               => \$SWVEL,
  "SWDEN=f"               => \$SWDEN,
  "SEE_NCFILE=s"          => \$SEE_NCFILE,
  "AURORA=i"              => \$AURORA,
  "COLFAC=f"              => \$COLFAC,
  "DYNAMO=i"              => \$DYNAMO,
  "CURRENT_PG=i"          => \$CURRENT_PG,
  "CURRENT_KQ=i"          => \$CURRENT_KQ
) 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 number of lines of SECFLDS:
#
if (defined $SECFLDS) { 
  $SECFLDS0 = $SECFLDS;
  undef $SECFLDS1;
  undef $SECFLDS2;
} 
#
# 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 { 
  if ($highres) {
    $namelist_file = $model . "_dres_default.inp"; 
  } 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:
#SECFLDS = 'CO2_COOL','NO_COOL','DEN','HEATING','QJOULE',
#          'SIGMA_PED','SIGMA_HAL','TEC','UI_ExB','VI_ExB','WI_ExB',
#          'LAMDA_PED','LAMDA_HAL','HMF2','NMF2','SCHT','MU_M',
#	   'KQPHI','KQLAM','JQR','JE13D','JE23D' 
#
$START_YEAR = 2002;
$START_DAY  = 80;
$CALENDAR_ADVANCE = 1;
$SOURCE_START = "80,0,0";
$START= "80,0,0";
$STOP=  "81,0,0";
$HIST=  "1,0,0";
$MXHIST_PRIM = 10;
$SECSTART = "80,1,0";
$SECSTOP  = "81,0,0";
$SECHIST  = "0,1,0";
$MXHIST_SECH = 24;
$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'";
$HPSS_PATH = "'/home/$user/$model'";
$TIDE    = "0.,0.,0.,0.,0.,0.,0.,0.,0.,0.";
$TIDE2   = "0.,0.";
$POTENTIAL_MODEL = "'HEELIS'";
$GPI_NCFILE = "'\$TGCMDATA/gpi_2000001-2013120.nc'";
$IMF_NCFILE = "'\$TGCMDATA/imf_OMNI_2002001-2002365.nc'";
$KP = "2.0";
$POWER  = "18.";
$CTPOTEN="30.";
$F107  = "70.";
$F107A = "70.";
$BXIMF = "0.";
$BYIMF = "0.";
$BZIMF = "-5.";
$SWVEL = "400.";
$SWDEN = "4.0";
$SEE_NCFILE = "'\$TGCMDATA/see__L3_merged_2005007_007.nc'";
$AURORA = 1;
$COLFAC = "1.5";
$DYNAMO = "1";
$CURRENT_PG = "1";
$CURRENT_KQ = "0";
#
# Set default comments:
#
%comments = (
  " "                 =>" ",
  LABEL               =>" ",
  START_YEAR          =>" ",
  START_DAY           =>" ",
  CALENDAR_ADVANCE    =>" ",
  SOURCE              =>" ",
  SOURCE_START        =>" ",
  START               =>" ",
  STOP                =>" ",
  STEP                =>" ",
  HIST                =>" ",
  OUTPUT              =>" ",
  MXHIST_PRIM         =>" ",
  SECSTART            =>" ",
  SECSTOP             =>" ",
  SECHIST             =>" ",
  SECOUT              =>" ",
  MXHIST_SECH         =>" ",
  SECFLDS             =>" ",
  SECFLDS0            =>" ",
  SECFLDS1            =>" ",
  SECFLDS2            =>" ",
  HPSS_PATH           =>";",
  TIDE                =>" ",
  TIDE2               =>" ",
  GSWM_MI_DI_NCFILE   =>" ",
  GSWM_MI_SDI_NCFILE  =>" ",
  GSWM_NM_DI_NCFILE   =>";",
  GSWM_NM_SDI_NCFILE  =>";",
  SABER_NCFILE        =>";",
  TIDI_NCFILE         =>";",
  POTENTIAL_MODEL     =>" ",
  GPI_NCFILE          =>";",
  IMF_NCFILE          =>";",
  KP                  =>";",
  POWER               =>" ",
  CTPOTEN             =>" ",
  F107                =>" ",
  F107A               =>" ", 
  BXIMF               =>";",
  BYIMF               =>";",
  BZIMF               =>";",
  SWVEL               =>";",
  SWDEN               =>";",
  SEE_NCFILE          =>";",
  AURORA              =>" ",
  COLFAC              =>" ",
  DYNAMO              =>" ",
  CURRENT_PG          =>" ",
  CURRENT_KQ          =>" "
);

} # end sub set_defaults
#-------------------------------------------------------------------------
sub set_res_params {
  my ($highres) = @_;

  if (! defined($LABEL)) {
    if ($highres) {
      $LABEL="'${model}${version} res=2.5'"; 
    } else {
      $LABEL="'${model}${version} res=5.0'"; 
    }
  }
  if (! defined($STEP)) {
    if ($highres) {
      $STEP=60;
    } else {
      $STEP=120;
    }
  }
  if (! defined($SOURCE)) {
    if ($highres) {
      $SOURCE = "'\$TGCMDATA/tiegcm1.95/TGCM.tiegcm1.95_dres.pcntr_mareqx_smin.nc'";
    } else {
      $SOURCE = "'\$TGCMDATA/tiegcm1.95/TGCM.tiegcm1.95.pcntr_mareqx_smin.nc'";
    }
  }
  if (! defined($OUTPUT)) {
    if ($highres) {
      $OUTPUT = "'" . $model . $version . "_dres.pcntr_mareqx_smin_001.nc'";
    } else {
      $OUTPUT = "'" . $model . $version . ".pcntr_mareqx_smin_001.nc'";
    }
  }
  if (! defined($SECOUT)) {
    if ($highres) {
      $SECOUT   = "'" . $model . $version . "_dres.scntr_mareqx_smin_001.nc'";
    } else {
      $SECOUT   = "'" . $model . $version . ".scntr_mareqx_smin_001.nc'";
    }
  }
  if (! defined($GSWM_MI_DI_NCFILE)) { 
    if ($highres) { $GSWM_MI_DI_NCFILE  = "'\$TGCMDATA/gswm_diurn_2.5d_99km.nc'"; }
    else          { $GSWM_MI_DI_NCFILE  = "'\$TGCMDATA/gswm_diurn_5.0d_99km.nc'"; }
  }
  if (! defined($GSWM_MI_SDI_NCFILE)) { 
    if ($highres) { $GSWM_MI_SDI_NCFILE = "'\$TGCMDATA/gswm_semi_2.5d_99km.nc'"; }
    else          { $GSWM_MI_SDI_NCFILE = "'\$TGCMDATA/gswm_semi_5.0d_99km.nc'"; }
  }
  if (! defined($GSWM_NM_DI_NCFILE)) { 
    if ($highres) { $GSWM_NM_DI_NCFILE  = "'\$TGCMDATA/gswm_nonmig_diurn_2.5d_99km.nc'";
                    $comments{GSWM_NM_DI_NCFILE} =" "; }
    else          { $GSWM_NM_DI_NCFILE  = "'\$TGCMDATA/gswm_nonmig_diurn_5.0d_99km.nc'"; }
  }
  if (! defined($GSWM_NM_SDI_NCFILE)) { 
    if ($highres) { $GSWM_NM_SDI_NCFILE = "'\$TGCMDATA/gswm_nonmig_semi_2.5d_99km.nc'";
                    $comments{GSWM_NM_SDI_NCFILE} =" "; }
    else          { $GSWM_NM_SDI_NCFILE = "'\$TGCMDATA/gswm_nonmig_semi_5.0d_99km.nc'"; }
  }
  if (! defined($SABER_NCFILE)) { 
    if ($highres) { $SABER_NCFILE = "'\$TGCMDATA/2002_saber_2.5deg.nc'"; }
    else          { $SABER_NCFILE = "'\$TGCMDATA/2002_saber_5.0deg.nc'"; }
  }
  if (! defined($TIDI_NCFILE)) { 
    if ($highres) { $TIDI_NCFILE  = "'\$TGCMDATA/2002_tidi_2.5deg.nc'"; }
    else { $TIDI_NCFILE  = "'\$TGCMDATA/2002_tidi_5.0deg.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;
#
# Saber-Tidi parameters are not in the public release:
# SABER (T,Z) and/or TIDI (U,V) data files for lbc perturbations:
#
#$comments{SABER_NCFILE}SABER_NCFILE = $SABER_NCFILE
#$comments{TIDI_NCFILE}TIDI_NCFILE  = $TIDI_NCFILE

$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
;
; 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
$comments{SECFLDS}          $SECFLDS2
;
; Diagnostic fields available with this release:
;SECFLDS = 'CO2_COOL','NO_COOL','DEN','HEATING','QJOULE','QJOULE_INTEG',
;          'SIGMA_PED','SIGMA_HAL','TEC','UI_ExB','VI_ExB','WI_ExB',
;          'LAMDA_PED','LAMDA_HAL','HMF2','NMF2','SCHT','MU_M','O_N2','WN',
;          'BX','BY','BZ','BMAG','EX','EY','EZ','ED1','ED2','PHIM2D','N2'
;
; These diagnostic currents are available only if icalkqlam==1 (dynamo.F)
;	   'KQPHI','KQLAM','JQR','JE13D','JE23D' 
;
; If HPSS_PATH is set, a csh script will be made in the execdir that,
; when executed, will copy history disk files to the NCAR HPSS in the 
; directory HPSS_PATH (must have an HPSS account at NCAR CISL)
;
$comments{HPSS_PATH}HPSS_PATH = $HPSS_PATH
;
$comments{TIDE}TIDE = $TIDE
$comments{TIDE2}TIDE2 = $TIDE2
;
; At 5 deg resolution, use gswm migrating tides only.
; At 2.5 deg resolution, optionally use both migrating 
;   and non-migrating tides.
;
$comments{GSWM_MI_DI_NCFILE}GSWM_MI_DI_NCFILE  = $GSWM_MI_DI_NCFILE
$comments{GSWM_MI_SDI_NCFILE}GSWM_MI_SDI_NCFILE = $GSWM_MI_SDI_NCFILE
$comments{GSWM_NM_DI_NCFILE}GSWM_NM_DI_NCFILE  = $GSWM_NM_DI_NCFILE
$comments{GSWM_NM_SDI_NCFILE}GSWM_NM_SDI_NCFILE = $GSWM_NM_SDI_NCFILE
;
; Potential model can be 'HEELIS' (optionally with GPI data),
; or 'WEIMER' (optionally with IMF data). If WEIMER, both IMF 
; and GPI may be specified, but only f10.7 will be used from GPI.
;
 POTENTIAL_MODEL = $POTENTIAL_MODEL
;POTENTIAL_MODEL = 'WEIMER'
;
; If potential model is HEELIS, GPI data can be used to calculate 
; POWER and CTPOTEN from Kp data, and to use data for f10.7 flux. 
; If GPI_NCFILE is specified, one or more of POWER,CTPOTEN,F107,F107A 
; must be commented out (data will be used for those that are commented
; out, otherwise the user-provided values will be used).
;
$comments{GPI_NCFILE}GPI_NCFILE = $GPI_NCFILE
;
; If KP is specified, and POWER and/or CTPOTEN are commented,
; then the given KP will be used to calculate POWER and/or CTPOTEN
;
$comments{KP}KP = $KP
$comments{POWER}POWER   = $POWER
$comments{CTPOTEN}CTPOTEN = $CTPOTEN
$comments{F107}F107    = $F107
$comments{F107A}F107A   = $F107A
;
; If potential model is WEIMER, data file IMF_NCFILE can be specified 
; to read one or more of BXIMF,BYIMF,BZIMF,SWVEL,SWDEN. If IMF_NCFILE 
; is specified and POWER is not provided, it will be calculated from 
; BZ,SWVEL. Also, if IMF_NCFILE is provided, user cannot provide CTPOTEN 
; (it will be calculated from the Weimer potential).
;
$comments{IMF_NCFILE}IMF_NCFILE = $IMF_NCFILE
;
$comments{BXIMF}BXIMF   = $BXIMF
$comments{BYIMF}BYIMF   = $BYIMF
$comments{BZIMF}BZIMF   = $BZIMF
$comments{SWVEL}SWVEL   = $SWVEL
$comments{SWDEN}SWDEN   = $SWDEN
$comments{SEE_NCFILE}SEE_NCFILE = $SEE_NCFILE
$comments{AURORA}AURORA = $AURORA
$comments{COLFAC}COLFAC = $COLFAC
$comments{DYNAMO}DYNAMO = $DYNAMO
;
; If CURRENT_PG=1, current due to plasma pressure gradient and gravity is calculated
; and included as a forcing term in the dynamo equation (default=1) (ignored if DYNAMO=0)
;
$comments{CURRENT_PG}CURRENT_PG = $CURRENT_PG
;
; If CURRENT_KQ=1, then height-integrated current density of current sheet, and
; upward current density at the top of the ionosphere is calculated (default=0)
; (ignored if DYNAMO=0) (see current.F90 to save JQR, JE13D, JE23D, KQPHI, KQLAM)
;
$comments{CURRENT_KQ}CURRENT_KQ = $CURRENT_KQ
/
EOF

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