#!/usr/bin/perl -w
#  This is the Perl equivalent of "Script2.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 Perl2.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.  
#
#  Perl2.pl demonstrates a rudimentary template script.  A data
#  file is specified at the start of the script.  A contour map and
#  wireframe map are then generated.  This can be applied to any data
#  file by changing the value of the variable "$InFile" appropriately.

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, $ContourMapFrame, $ContourMap,
	# $WireframeMapFrame, $Wireframe, $InFile, $OutFile and $retValue as lexicals.
	my(
	$SurferApp,
	$Doc,
	$Plotwindow,
	$ContourMapFrame,
	$ContourMap,
	$WireframeMapFrame,
	$Wireframe,
	$InFile,
	$OutFile,
	$retValue,
	);
	
	# Creates an instance of the Surfer Application object and assigns it to the
	# variable named "$SurferApp"
	$SurferApp = Win32::OLE->CreateObject("Surfer.Application");
	
	# Assigns the name of the data file to be used and its location to the
	# variable named "$InFile"
	$InFile = $SurferApp->Path . "\\Samples\\" . "demogrid.dat";
	
	# Assigns the name of the grid file to be used and its location to the
	# variable named "$OutFile"
	$OutFile = $SurferApp->Path . "\\Samples\\" . "Sample";
	
	#Makes Surfer visible
	$SurferApp->{Visible} = True;
	
	# Grids the specified data file using the Kriging algorithm and assigns
	# the return value to the variable named "$retValue"
	$retValue = $SurferApp->GridData(
					{
					   DataFile   => $InFile, 
					   Algorithm  => "srfKriging",
					   ShowReport => False, 
					   OutGrid    => $OutFile,
					 }
					);
					
	# Creates a plot document in Surfer and assigns it to the variable named "$Doc"
	$Doc = $SurferApp->{Documents}->Add(srfDocPlot);
	
	# Assigns the new plot window to the variable named "$PlotWindow"
	$Plotwindow = $Doc->Windows(1);
	
	# Disables Plotwindow's AutoRedraw to speed up the script
	$Plotwindow->{AutoRedraw} = False;
	
	# Creates a contour map and assigns the map coordinate system to the variable
	# named "$ContourMapFrame"
	$ContourMapFrame = $Doc->Shapes->AddContourMap($OutFile);

	# Assigns the contour map attributes to the variable named "$ContourMap"
	$ContourMap = $ContourMapFrame->Overlays(1);

	# Displays the fill of the contour map
	$ContourMap->{FillContours} = True;
        
	# Positions the contour map to the lower half of the page
	$ContourMapFrame->{Left} = 0.75;
	$ContourMapFrame->{Top}  = 5.5;
	
	# Sets the size of the contour map
	$ContourMapFrame->{Width}  = 7;
	$ContourMapFrame->{Height} = 5;
	
	# Creates a wireframe map and assigns the map coordinate system to the
	# variable named "$WireframeMapFrame"
	$WireframeMapFrame = $Doc->Shapes->AddWireframe($OutFile);

	# Assigns the wireframe map attributes to the variable named "$Wireframe"
	$Wireframe = $WireframeMapFrame->Overlays(1);

	# Changes the XY mesh color to blue
	$Wireframe->{xLine}{ForeColor} = RGB(0, 0, 255);
	$Wireframe->{yLine}{ForeColor} = RGB(0, 0, 255);
        
	# Positions the wireframe map to the upper half of the page
	$WireframeMapFrame->{Left} = 0.75;
	$WireframeMapFrame->{Top}  = 10.75;
	
	# Sets the size of the wireframe map
	$WireframeMapFrame->{Width}  = 7;
	$WireframeMapFrame->{Height} = 5;
	
	# Enables AutoRedraw to update the plot window
	$Plotwindow->{AutoRedraw} = True;
	
	# Uncomment to send the page to the default printer to be printed
	#$Doc->PrintOut;

sub RGB { $_[0] + ($_[1] *256) + ($_[2] *256** 2) }

__END__;
