#!/usr/bin/env perl
#--------------------------------------------------------------------
#    The MB-system:	mbm_grd2arc.perl	6/11/99
#
#    Copyright (c) 1999-2023 by
#    David W. Caress (caress@mbari.org)
#      Monterey Bay Aquarium Research Institute
#      Moss Landing, California, USA
#    Dale N. Chayes 
#      Center for Coastal and Ocean Mapping
#      University of New Hampshire
#      Durham, New Hampshire, USA
#    Christian dos Santos Ferreira
#      MARUM
#      University of Bremen
#      Bremen Germany
#      
#    MB-System was created by Caress and Chayes in 1992 at the
#      Lamont-Doherty Earth Observatory
#      Columbia University
#      Palisades, NY 10964
#
#    See README.md file for copying and redistribution conditions.
#--------------------------------------------------------------------
#
# Command:
#   mbm_grd2arc
#
# Purpose:
#   Macro to convert a GMT grid file in the GMT NetCDF grid format
#   to an ArcView ASCII grid. This allows users to import the grid
#   into Arc/Info and ArcView. The grids must have the same grid
#   interval in both longitude and latitude. If the grid was created
#   using mbgrid or mbmosaic, the -E option must have been used
#   with both dy = 0 and "!" appended (see the mbgrid and mbmosaic
#   manual pages).
#
# Basic Usage:
#   mbm_grd2arc -Igrdfile -Oarcfile [-H -Nnodata -V]
#
# Author:
#   David W. Caress
#   Monterey Bay Aquarium Research Institute
#   Moss Landing, CA 95039
#   October 5, 1999
#
#
#
#

$program_name = "mbm_grd2arc";

# Deal with command line arguments
&Getopts('I:i:N:n:O:o:VvHh');
$ifile =    ($opt_I || $opt_i);
$ofile =    ($opt_O || $opt_o);
$nodata =   ($opt_N || $opt_n || -99999);
$help =     ($opt_H || $opt_h);
$verbose =  ($opt_V || $opt_v);

#--------------------------------------------------------------------

# print out help message if required
if ($help)
	{
	print "\n$program_name:\n";
	print "\nMacro to convert a GMT grid file in the GMT NetCDF grid format \n";
	print "to an ArcView ASCII grid. This allows users to import the grid\n";
	print "into Arc/Info and ArcView. The grids must have the same grid \n";
	print "interval in both longitude and latitude. If the grid was created\n";
	print "using mbgrid or mbmosaic, the -E option must have been used \n";
	print "with both dy = 0 and \"!\" appended (see the mbgrid and mbmosaic\n";
	print "manual pages).\n";
	print "\nBasic Usage: \n";
	print "\t$program_name -Igrdfile -Oarcfile [-H -Nnodata -V]\n";
	exit 0;
	}

# check for input file
if (!$ifile)
	{
	print "\a";
	die "No input file specified\nMacro $program_name aborted\n";
	}
elsif (! -e $ifile)
	{
	print "\a";
	die "Specified input file $ifile cannot be opened\nMacro $program_name aborted\n";
	}
if (!$ofile)
	{
	print "\a";
	die "No output file specified\nMacro $program_name aborted\n";
	}
elsif (!open(OUT,">$ofile"))
	{
	print "\a";
	die "Cannot open output file $ofile\nMacro $program_name aborted.\n";
	}

# Just call the GMT module grdconvert
$command = "gmt grdconvert $ifile -G$ofile=ef+n$nodata -V";
if ($verbose) {
  print "Calling: $command\n";
}
`$command`;

exit 0;

#-----------------------------------------------------------------------
# This should be loaded from the library but its safer to
# just include it....
#
;# getopts.pl - a better getopt.pl

;# Usage:
;#      do Getopts('a:bc');  # -a takes arg. -b & -c not. Sets opt_* as a
;#                           #  side effect.

sub Getopts {
    local($argumentative) = @_;
    local(@args,$_,$first,$rest);
    local($errs) = 0;

    @args = split( / */, $argumentative );
    while(@ARGV && ($_ = $ARGV[0]) =~ /^-(.)(.*)/) {
     ($first,$rest) = ($1,$2);
     $pos = index($argumentative,$first);
     if($pos >= $[) {
         if($args[$pos+1] eq ':') {
          shift(@ARGV);
          if($rest eq '') {
              ++$errs unless @ARGV;
              $rest = shift(@ARGV);
          }
          eval "\$opt_$first = \$rest;";
         }
         else {
          eval "\$opt_$first = 1";
          if($rest eq '') {
              shift(@ARGV);
          }
          else {
              $ARGV[0] = "-$rest";
          }
         }
     }
     else {
         print STDERR "Unknown option: $first\n";
         ++$errs;
         if($rest ne '') {
          $ARGV[0] = "-$rest";
         }
         else {
          shift(@ARGV);
         }
     }
    }
    $errs == 0;
}
