#!/usr/bin/perl -w
#  This is the Perl equivalent of "Script3.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 Perl3.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.  
#
#  Perl3.pl creates several maps from a single data file.  The data
#  file is organized with columns corresponding to X, Y, Z1, Z2, etc.
#  A contour map is created for each set of data: X,Y,Z1, X,Y,Z2, etc.
#  The first row of the data file contains column headings.  This is
#  extracted and used to title the corresponding map.

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, $Wks, $Doc, $Plotwindow, $Title, $Map and
	# $MapTitle as lexicals.
	my(
	$SurferApp,
	$Wks,
	$Doc,
	$Plotwindow,
	$Map,
	$MapTitle,
	$File,
	$Title,
	$retValue,
	$MapNumber,
	);
	
	# Creates an instance of the Surfer Application object and assigns it to the
	# variable named "$SurferApp"
	$SurferApp = Win32::OLE->CreateObject("Surfer.Application");
	
	# Makes Surfer visible
	$SurferApp->{Visible} = True;
	
	# Assigns the name of the file to be used and its location to the
	# variable named "$File"
	$File = $SurferApp->Path . "\\Samples\\Sample3";
	
	# Opens Sample3.dat in a new worksheet document and assigns it to
	# the variable named "$Wks"
	$Wks = $SurferApp->Documents->Open($File.".dat");
	
	# Creates a plot document 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;
	
	# Assigns $MapNumber to the value of zero and increments the $MapNumber
	# for loop six times
	for($MapNumber = 0;$MapNumber <= 5;$MapNumber++) {
		
		# Forms the map title by concatentating the column
		# titles in the first row of the worksheet
		$Title = $Wks->Cells(1,1)->{Value} . 
			 $Wks->Cells(1,2)->{Value} . 
			 $Wks->Cells(1,$MapNumber + 3)->{Value};

		# Grids the specified data file using the Kriging algorithm and
		# assigns the return value to the variable named "$retValue"

		$retValue = $SurferApp->GridData(
						  {
						    DataFile   => $File . ".dat",
						    xCol       => 1,		
						    yCol       => 2, 
						    zCol       => ($MapNumber +3), 
						    Algorithm  => "srfKriging",			
						    ShowReport => False, 
						    OutGrid    => $File . ".grd",
						  }
						);
		
		# Creates a contour map and assigns the map coordinate system to
		# the variable named "$Map"
		$Map = $Doc->Shapes->AddContourMap($File);
		
		# Sets the size of the map
		$Map->{Width}  = 3;
		$Map->{Height} = 3;
		
		# Positions the map on the page
		$Map->{Left} = 0.5  +    ($MapNumber % 2) * ($Map->Height + 1);
		$Map->{Top}  = 10.5 - int($MapNumber / 2) * ($Map->Width  + .5);
		
		# Positions the map title used below the map and assigns it to
		# the variable named "$MapTitle"
		$MapTitle = $Doc->Shapes->AddText($Map->Left + $Map->Width/2 - .5, 
					  	  $Map->Top - 3.1,"Plot of " . $Title);

	}
	# 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__;
