#!/usr/bin/perl -w

#
#  Make INP, BAS and DIS input files
#
#  Willem A. Schreuder
#  October 9, 2003
#

use strict;
#  Number of days in month
my @ndm = (undef,31,28,31,30,31,30,31,31,30,31,30,31);

#
#  Allow usage of 12p stream network
#
my $ppt = '../data/ppt.dat';
my $hyd = '../static/12s.hyd';
my $R5 = 0;
while (@ARGV>0 && substr($ARGV[0],0,1) eq '-')
{
   my $par = shift @ARGV;
   if ($par eq '-12p')
   {
      $hyd = '../static/12.hyd';
   }
   elsif ($par eq '-5')
   {
      $R5 = 1;
   }
   elsif ($par eq '-p')
   {
      $ppt = shift @ARGV;
   }
   else
   {
      die "Unknown parameter $par\n";
   }
}

#
#  Check command line arguments
#
(@ARGV==1 || @ARGV==2) || die "Usage: $0 <year(s)> <root>\n";
my $root = pop @ARGV;
(@ARGV>0) || (push @ARGV , $root);
my ($yr0,$yr1) = split('-',$ARGV[0]);
defined($yr1) || ($yr1 = $yr0);

#
#  Write DIS file
#
open(DIS , ">$root.dis") || die "Cannot open file $root.dis\n";
printf DIS "    1  165  326  %d    1    1 NLAY,NROW,NCOL,NPER,ITMUNI,LENUNI\n" , 12*($yr1-$yr0+1);
printf DIS " 0\n";
printf DIS "CONSTANT   5280.000  DELR\n";
printf DIS "CONSTANT   5280.000  DELC\n";
printf DIS "OPEN/CLOSE ../static/12.top    1.0 (free)   -1\n";
printf DIS "OPEN/CLOSE ../static/12.bot    1.0 (free)   -1\n";
foreach my $yr ($yr0 .. $yr1)
{
   foreach my $mo (1 .. 12)
   {
      printf DIS "%10d%10d%10.4f TR %2d/%4d\n" , 24*60*60*(($yr%4==0 && $mo==2) ? 29 : $ndm[$mo]) , 2 , 1 , $mo , $yr;
   }
}
close(DIS);

#
#  Write PAR files for Colorado
#
#  Groundwater
#open(PAR , ">co.gw.par") || die "Cannot open file co.gw.par\n";
#print PAR "#  Output\n";
#print PAR "FILE co pmp rcg agw\n";
#print PAR "TRANSIENT $yr0 $yr1\n";
#print PAR "\n";
#print PAR "#  Domain\n";
#print PAR "IBOUND ../static/02.ibound\n";
#print PAR "\n";
#print PAR "#  Wells\n";
#print PAR "AREA    Cheyenne KitCarson Lincoln Logan Phillips Sedgwick Washington Yuma\n";
#print PAR "#        Jan   Feb   Mar   Apr   May   Jun   Jul   Aug   Sep   Oct   Nov   Dec\n";
#print PAR "MONTH   0.000 0.000 0.000 0.000 0.000 0.147 0.320 0.328 0.205 0.000 0.000 0.000\n";
#print PAR "PUMPING ../data/co/pumping.dat ../data/co/returns.dat ../data/co/gwacres.dat\n";
#print PAR "WELL    ../data/co/nohipl.dat\n";
#close(PAR);
#  Surface Water
open(PAR , ">co.sw.par") || die "Cannot open file co.sw.par\n";
print PAR "#  Output\n";
print PAR "FILE co rcs asw\n";
print PAR "TRANSIENT $yr0 $yr1\n";
print PAR "\n";
print PAR "#  Domain\n";
print PAR "IBOUND ../static/02.ibound\n";
print PAR "\n";
print PAR "#  County areas\n";
print PAR "AREA Laird    ../data0/co/laird.dat\n";
print PAR "AREA Pioneer  ../data0/co/pioneer.dat\n";
print PAR "AREA Hale     ../data0/co/hale.dat\n";
print PAR "\n";
print PAR "#  Irrigation returns\n";
print PAR "RETURN ../data/co/surf.dat\n";
close(PAR);

#
#  One each for reference and four impact runs
#
foreach my $ver ('','a','b','c','d')
{
   #
   #  Write BAS file
   #
   open(BAS , ">$root$ver.bas") || die "Cannot open file $root$ver.bas\n";
   print BAS "FREE\n";
   print BAS "OPEN/CLOSE ../static/02.ibound 1 (326I2) -1\n";
   print BAS "   -999.00  HNOFLO\n";
   print BAS "OPEN/CLOSE $yr0$ver.shead 1.0 (BINARY) -1\n";
   close(BAS);

   #
   #  Write NAM file
   #
   open(NAM , ">$root$ver.nam") || die "Cannot open file $root$ver.nam\n";
   printf NAM "LIST          6 $root$ver.out\n";
   printf NAM "BAS6          1 $root$ver.bas\n";
   printf NAM "LPF           2 ../static/annual.lpf\n";
   printf NAM "DIS           3 $root.dis\n";
   printf NAM "OC           11 ../static/annual.oc\n";
   printf NAM "PCG          12 ../static/12p.pcg\n";
   printf NAM "STR          13 $root.str\n";
   printf NAM "DRN          14 ../static/annual.drn\n";
   printf NAM "EVT          15 $root.evt\n";
   printf NAM "RCH          16 $root$ver.rch\n";
   printf NAM "WEL          18 $root$ver.wel\n";
   printf NAM "HYD          19 $hyd\n";
   printf NAM "DATA(BINARY) 30 $root$ver.head\n";
   printf NAM "DATA(BINARY) 40 $root$ver.cbc\n";
   printf NAM "DATA(BINARY) 50 $root$ver.sfi\n";
   close(NAM);

   #
   #  Write PAR file for rrpp
   #
   open(PAR , ">$root$ver.par") || die "Cannot open file $root$ver.par\n";
   print PAR "# Input files\n";
   print PAR "DIR co ks ne\n";
   if ($ver eq 'a')
   {
      print PAR "NOPUMP CO\n";
      $R5 && print PAR "MOUND ../data0/moundarea.flg\n";
   }
   elsif ($ver eq 'b')
   {
      print PAR "NOPUMP KS\n";
      $R5 && print PAR "MOUND ../data0/moundarea.flg\n";
   }
   elsif ($ver eq 'c')
   {
      print PAR "NOPUMP NE\n";
      $R5 && print PAR "MOUND ../data0/moundarea.flg\n";
   }
   elsif ($ver eq 'd')
   {
      print PAR "MOUND ../data0/moundarea.flg\n";
   }
   else
   {
      print PAR "FILE ppt ppt irr rcp\n";
   }
   print PAR "\n";
   print PAR "# Output files\n";
   print PAR "RECHARGE  $root$ver.rch\n";
   print PAR "WELL      $root$ver.wel\n";
   print PAR "\n";
   print PAR "# Limit to active cells\n";
   print PAR "IBOUND ../static/02.ibound\n";
   print PAR "\n";
   print PAR "# Precip stations, totals, and drift\n";
   print PAR "STATIONS ../data/loc.dat $ppt 1\n";
   print PAR "\n";
   print PAR "# Soil flags\n";
   print PAR "SOIL ../data0/soil.12o\n";
   print PAR "\n";
   print PAR "# Recharge function by soil\n";
   print PAR "FUNCTION  /dev/null  1\n";
   print PAR "#    <Coarse>       <Medium>       <Fine>         <AlluvX>       <AlluvY>\n";
   print PAR "#    Non    Irr     Non    Irr     Non    Irr     Non    Irr     Non    Irr\n";
   print PAR " 0   0.000  0.000   0.000  0.000   0.000  0.000   0.000  0.000   0.000  0.000\n";
   print PAR " 3   0.000  0.150   0.000  0.150   0.000  0.150   0.000  0.000   0.000  0.000\n";
   print PAR "10   0.190  0.690   0.000  0.500   0.000  0.500   0.000  0.000   0.000  0.000\n";
   print PAR "12   0.380  0.980   0.000  0.600   0.000  0.600   0.000  0.000   0.135  0.162\n";
   print PAR "13   0.500  1.150   0.000  0.650   0.000  0.650   0.000  0.000   0.203  0.243\n";
   print PAR "14   0.690  1.390   0.080  0.780   0.000  0.700   0.000  0.000   0.270  0.324\n";
   print PAR "15   0.940  1.690   0.160  0.910   0.000  0.750   0.000  0.000   0.338  0.405\n";
   print PAR "16   1.320  2.120   0.240  1.040   0.050  0.850   0.000  0.000   0.405  0.486\n";
   print PAR "17   1.740  2.590   0.320  1.170   0.100  0.950   0.000  0.000   0.473  0.567\n";
   print PAR "18   2.150  3.050   0.400  1.300   0.150  1.050   0.000  0.000   0.540  0.648\n";
   print PAR "19   2.520  3.470   0.480  1.430   0.200  1.150   0.400  0.420   0.760  0.912\n";
   print PAR "20   2.880  3.880   0.750  1.750   0.250  1.250   0.800  0.840   0.980  1.176\n";
   print PAR "21   3.250  4.300   1.100  2.150   0.300  1.350   1.200  1.260   1.200  1.440\n";
   print PAR "22   3.630  4.730   1.500  2.600   0.350  1.450   1.600  1.680   1.420  1.704\n";
   print PAR "24   4.440  5.640   2.250  3.450   0.530  1.730   2.400  2.520   1.860  2.232\n";
   print PAR "26   5.270  6.570   3.050  4.350   0.780  2.080   3.600  3.780   2.560  3.072\n";
   print PAR "28   6.100  7.500   3.850  5.250   1.040  2.440   4.800  5.040   3.260  3.912\n";
   print PAR "30   7.000  8.500   4.650  6.150   1.300  2.800   6.000  6.300   3.960  4.752\n";
   print PAR "36  10.100 11.900   7.050  8.850   2.070  3.870  14.400 15.120   5.832  6.998\n";
   print PAR "40  12.300 14.300   8.650 10.650   2.600  4.600  17.600 18.480   7.248  8.697\n";
   print PAR "50  17.900 19.900  12.650 14.650   3.870  5.870  26.500 27.825  10.900 13.080\n";
   print PAR "60  23.000 25.000  16.650 18.650   5.200  7.200  39.000 40.950  15.000 18.000\n";
   print PAR "\n";
   print PAR "#  Terrain\n";
   print PAR "TERRAIN ../data0/terrain.flg ../data0/terrain.12p /dev/null LIMIT 1.0 1.5\n";
   print PAR "\n";
   print PAR "#        Jan      Feb   Mar     Apr     May     Jun     Jul     Aug     Sep     Oct     Nov     Dec\n";
   print PAR "MONTH  0.0262  0.0224  0.0566  0.1033  0.1716  0.1586  0.1624  0.1184  0.0736  0.0514  0.0334  0.0221\n";
   print PAR "\n";
   print PAR "# Annual budget by state\n";
   print PAR "BUDGET ../data0/states.flg\n";
   print PAR "\n";
   print PAR "# Output\n";
   print PAR "TRANSIENT $yr0 $yr1\n";
   close(PAR);
}
