#!/usr/bin/perl -w
#
#Copyright (C) 2007  Werner Riener
#
#This program is free software; you can redistribute it and/or
#modify it under the terms of the GNU General Public License
#as published by the Free Software Foundation; either
#version 2 of the License, or (at your option) any later version.
#
#This program is distributed in the hope that it will be
#useful, but WITHOUT ANY WARRANTY; without even the
#implied warranty of MERCHANTABILITY or FITNESS FOR A
#PARTICULAR PURPOSE. See the GNU General Public License
#for more details.
#
#You should have received a copy of the GNU General
#Public License along with this program; if not, write
#to the Free Software Foundation, Inc.,
#51 Franklin St, Fifth Floor, Boston, MA 02110, USA
#

package extract;

use strict;

use Exporter;
	our @ISA = qw(Exporter);
	our @EXPORT = qw(get_lines get_row help);
	our @EXPORT_OK = qw();



sub get_lines {
	my $workdir = $_[0];
	my $size = $_[1];
	my $outfile = $_[2];
	my $datafile = $_[3];
	
	#print "Variablen sind: $workdir, $size, $outfile $datafile\n";
	
	if (! $workdir) {
		print "Need a working directory.\n";
		exit(0);
	} elsif (! $size ) {
		print "No particle size defined.\n";
		exit(0);
	} elsif (! $outfile) {
		print "No output-file defined.\n";
		exit(0);
	} elsif ( ! $datafile) {
		print "No datafile to work with.\n";
		exit();
	} elsif (! -e $datafile) {
		print "Datafile $datafile not found.\n";
		exit(0);
	} else {
		# OUTPUT: Datei öffnen zum Schreiben
		my $micrometer = "um";
		open(INFILE, "<$datafile") || die "GETLINE:Cannot open datafile $datafile: $!\n";
		open(OUTPUT, ">>$workdir\\$outfile") || die "GETLINE:Could not open $workdir\\$outfile : $!\n";
			
		my @a = <INFILE>;
		foreach my $line (@a) {
			if ($line =~ /$size$micrometer/) {
				print OUTPUT "$line";
			}
		}#foreach
		close (OUTPUT);
	}#end else 
}#END SUB





sub get_row {
	my $workdir = $_[0];
	my $infile = $_[1];
	my $outfile = $_[2];
	
	if (! $workdir) {
		print "Need a working directory.\n";
		exit(0);
	} elsif (! $infile ) {
		print "No infile defined.\n";
		exit(0);
	} elsif (! $outfile) {
		print "No outfile defined.\n";
		exit(0);
	} else {
		#Procedure
	
		
		$, = ' ';               # set output field separator
		$\ = "\n";              # set output record separator

		my $Fld1 = "";
		my $Fld2 = "";
		my $Fld3 = "";

		#we don't want any other output than field 3
		print "Using: $workdir\\$infile \n";
		
		open(IN, "<$workdir\\$infile") || die "GETROW:Cannot open infile $workdir\\$infile: $!\n";
		while (<IN>) {
			open (OUTPUT, ">>$workdir\\$outfile") || die "Could not open $workdir\\$outfile\n";
			($Fld1,$Fld2,$Fld3) = split(' ', $_, 9999);
			print OUTPUT $Fld3;
		}#end while
	close(OUTPUT);
	close(IN);
	
	} #end else
}#END SUB



sub help {
print qq
(
Module 'extract', this exports:
	help()
	get_lines(workdir, size, outfile, datafile)
	get_row(workdir, infile, outfile)
)
}#END SUB



# TRUE
1;

# NUN DIE DOCUMENTATION
# DIESE IST MIT perldoc [Pfad]Modulname aufzurufen


__END__

=head1 NAME

extract - Extracts data from datafiles created with the PZGs from RR-Elektronik GmbH

=head1 SYNOPSIS

This module/package exports:
	help()
	get_lines(workdir,size,outfile,datafile)
	get_row(workdir, infile, outfile)

=head1 DESCRIPTION

This package provides easy and fast extraction of particle-size data.
The datafiles to work with are created by the particle collector systems from
RR-Eleketronik GmbH.

=head1  SEE ALSO

http://home.pages.at/werrie/

=cut