#!/usr/bin/perl
#
# Simple perl script for creating fvgcm.h; see usage()
#
#------------------------------------------------------------------

use Getopt::Std;         # command line options

 @levels = ( 96, 72, 55, 32, 30, 18 ); # supported levels

# Command line options
# --------------------
  getopts('t:');

  usage() if ( $#ARGV < 0 );

$res  = $ARGV[0];
$hres = substr($res,0,1);
$vres = substr($res,1,3);

$tracer = 1 unless ( $tracer = $opt_t );

# Figure out horizontal resolution
# --------------------------------
if ( lc "$hres" eq "a" ) {
     $idim = 72;
     $jdim = 46;
   } elsif ( lc "$hres" eq "b" ) {
     $idim = 144;
     $jdim = 91;
   } elsif ( lc "$hres" eq "c" ) {
     $idim = 288;
     $jdim = 181;
   } elsif ( lc "$hres" eq "d" ) {
     $idim = 576;
     $jdim = 361;
   } else {
     die "Invalid FVGCM resolution $res";
   }

# Figure out vertical resolution
# ------------------------------
   $ok = 0;
   foreach $lev ( @levels ) {
     if ( $vres == $lev ) {
          $ok = 1;
	}
   }
   die "Unsupported number of FVGCM levels $vres" unless ( $ok );

#  Create fvgcm.h
#  ---------------
   open(FH,">fvgcm.h") or  die "Cannot create file fvgcm.h"; 
    print  FH <<"EOF";
! 
! fvgcm.h: File automatically generated by fvsetdim; do not edit
!
#ifndef FVGCM_SET
#  define FVGCM_SET
!  define FVGCM_RESOLUTION  $res
#  define FVGCM_LON         $idim
#  define FVGCM_LAT         $jdim
#  define FVGCM_LEV         $vres
#  define FVGCM_TRACER      $tracer
#endif
EOF

   exit(0);

#...................................................................
sub usage {

  print <<"EOF";
NAME
     fvsetdim - Creates fvgcm.h for a given resolution
          
SYNOPSIS

     fvsetdim  [-t ntracers]  resolution
          
DESCRIPTION
     Given a resolution string (b55, c55, etc) creates the corresponding
     fvgcm.h include file. The resolution is a 3 character string
     of the form "[a-d]nn" where the first character specifies the
     horizontal dimensions:

         letter    FVGCM_LON    FVGCM_LAT      resolution (deg)
         ------    ---------    ---------    -------------------
          a          72            46            4  x 5
          b         144            91            2  x 2.5
          c         288           181            1  x 1.25
          d         576           361           0.5 x 0.625

    The following vertical levels are currently supported:

     @levels 
     
    The optional parameter "-t" allows you to specify the number of
    tracers. The default is 1 tracer.

EXAMPLES

     fvsetdim b55
     fvsetdim -t 2 d32

AUTHOR
     Arlindo da Silva, dasilva\@dao.gsfc.nasa.gov

EOF

exit(1);

}
