#!/usr/bin/perl -w
#  This is the Perl equivalent of "Script1.bas" script in the Surfer 7 Samples directory.
#  Converted by Burak Gürsoy <burakgursoy@gmx.net> <http://trperl.sourceforge.net>
#
#  This script requires an up-to-date version of ActivePerl (Windows port of the Perl Language).
#  You can download it from http://www.activestate.com for free.
#
#  Tested with ActivePerl 5.6.1 Build 633.
#
#  If Perl is in your path, you can execute it from the command line as 
#  	"perl Perl1.pl"
#  (Without the quotes)
#  And if ".pl" extension is registered and linked to perl in your system, then 
#  you can just execute it by double-clicking on the file.  
#
#  Perl1.pl creates several maps from a single data file. A contour map is
#  created from each of the nine gridding methods.  Each map is then labeled with
#  its corresponding gridding method.

use strict;               # Enable strict mode for errors
use constant True   => 1; # Create a boolean constant
use constant False  => 0; # Create a boolean constant
use Win32::OLE;           # Load the OLE module
use Win32::OLE::Const 'Surfer'; # Load Surfer Constants

	# Declares $SurferApp, $Doc, $Plotwindow, $Map, $MapTitle1, $MapTitle2, $MethodLabel, 
	# $retValue, $Data, $Grid, $Path and $Method as lexicals.
	my(
	$SurferApp,
	$Doc,
	$Plotwindow,
	$Map,
	$MapTitle1,
	$MapTitle2,
	$MethodLabel,
	$retValue,
	$Data,
	$Grid,
	$Path,
	$Method,
	);
	
	# Assigns the name of the data file to be used to the variable named "$Data"
	$Data = "Demogrid.dat";
	
	# Assigns the name of the grid file to be used to the variable named "$Grid"
	$Grid = "Demogrid.grd";
        
	# Assigns the arrayref of all nine gridding algorithms to the variable named
	# "$MethodLabel"
	$MethodLabel = ["Inverse Distance", "Kriging", "Minimum Curvature", 
			"Modified Shepard's Method", "Natural Neighbor", "Nearest Neighbor", 
			"Polynomial Regression", "Radial Basis Functions", 
			"Triangulation with Linear Interpolation"];

	# Creates an instance of the Surfer Application object and assigns it to the
	# variable named "$SurferApp"
	$SurferApp = Win32::OLE->CreateObject("Surfer.Application");

	# Assigns the location of the data and grid files to the variable named "$Path"
	$Path = $SurferApp->Path . "\\samples\\";

	# Makes Surfer visible
	$SurferApp->{Visible} = True;

        # Creates a plot document in Surfer and assigns it to the variable named "$Doc"
	$Doc = $SurferApp->Documents->Add;
	
	# Assigns the plot window to the variable named "$Plotwindow"
	$Plotwindow = $Doc->Windows(1);
	
	# Disables $Plotwindow's AutoRedraw to speed up the script
	$Plotwindow->{AutoRedraw} = False;
	
	# Increments the $Method for loop nine times
	for($Method = 1;$Method <= 9;$Method++) {
    
		# Grids the specified data file using the current gridding method
		# specified in the loop
		$retValue = $SurferApp->GridData(
						  {
						     DataFile   => $Path . $Data, 
						     Algorithm  => $Method,
						     ShowReport => False, 
						     OutGrid    => $Path . $Grid,
						   }
						 );
                
		# Creates a contour map from the gridded data and assigns it to the variable
		# named "$Map"
		$Map = $Doc->Shapes->AddContourMap($Path . $Grid);

		# Sets the size of the map
		$Map->{Width}  = 2;
		$Map->{Height} = 2;
		
		# Positions the map on the page
		$Map->{Left} = 0.5  +    (($Method - 1) % 3) * ($Map->Height + .5);
		$Map->{Top}  = 10.5 - int(($Method - 1) / 3) * ($Map->Width  + .75);
		
		# Positions the first line of the map title below the map and assigns it to
		# the variable named "$MapTitle1"
		$MapTitle1 = $Doc->Shapes->AddText($Map->Left + $Map->Width/2 - 1, 
						   $Map->Top - 2.07, $Data . " gridded using ");
		
		# Positions the second line of the map title below the map and assigns it to
		# the variable named "MapTitle2"
		$MapTitle2 = $Doc->Shapes->AddText($Map->Left + $Map->Width/2 - 1, 
						   $Map->Top - 2.22, 
						   $MethodLabel->[$Method - 1]);

	# Returns to the beginning of the for loop
	}
	
	# Enables AutoRedraw to update the plot window
	$Plotwindow->{AutoRedraw} = True;
	
	# Uncomment to send the page to the default printer to be printed
	#$Doc->PrintOut;

__END__;


