#!/usr/bin/perl use strict; use warnings; use File::Spec; use Getopt::Std; use IO::File; use Switch; use Time::HiRes qw(gettimeofday tv_interval); use version 0.77; our $VERSION = qv("v2.0.0"); use English qw(-no_match_vars); local $OUTPUT_AUTOFLUSH = 1; print my $timer_start = gettimeofday(), "\n"; my(%inputs) = (); my(%params) = (); getopts('o:r:p:t:n:f:g:d', \%inputs); die "Error Getting Parameters\n" unless get_params(\%inputs, \%params); check_params(\%params); my $outfile = File::Spec->catfile($params{'outfolder'},$params{'rootname'}); my $outnamefile = File::Spec->catfile($params{'outfolder'}, $params{'rootname'} ); my $outlinecount = $outfile . '_db_proc_linecount.txt'; $outfile = $outfile . '_db_proc_data.csv'; $outnamefile = $outnamefile . '_db_proc_names.csv'; print "Output SSS File: $outfile\n"; print "Output Names File: $outnamefile\n"; my $hdrmap; my(%hdr_types) = ( fields =>\&get_fields_area, regions =>\&get_fields_region, plan =>\&get_fields_plan ); die 'Unable to match sss type. ' unless defined($hdr_types{$params{ssstype}}); $hdrmap = $hdr_types{$params{ssstype}}(); my $namemap = get_fields_plan($params{ssstype}); #open I, '<', $infile or die "Unable to open file $infile.\n"; open my $O, '>', $outfile or die "Unable to open file $outfile.\n"; open my $ON, '>', $outnamefile or die "Unable to open file $outnamefile.\n"; #open OLCT, '<', $outlinecount or die "Unable to open file $outlinecount.\n"; foreach (my $i=0; $i<3; $i++) { my $line = <$O>; chomp ($line); $line =~ s/^\s+//; $line =~ s/\s+$//; my @sp = split (/\s+/,$line); foreach (my $j=0; $j{$key}[0] } 0..$#hdrs; if (defined ($index)) { $hdrmap->{$key}[1] = $index; print "HEADER MATCH: " . "\t" . $key . "\t" . $hdrmap->{$key}[0] . "\t[" . $hdrmap->{$key}[1]. "]\n"; } else { print "HEADER NOT FOUND: " . "\t" . $key . "\t" . $hdrmap->{$key}[0] . "\n"; } } print "-----------------------------------------------\n"; my @flist = (); my @fnamelist = (); assign_names($hdrmap, \@flist); assign_names($namemap, \@fnamelist); print "[" . join(",", @flist) . "]\n"; print $O join(",", @flist) . ",RUN_ID\n"; print $ON join(",", @fnamelist) . ",RUN_ID\n"; exit(); my $l_units = <$O>; chomp ($l_units); print "UNITS -- > [$l_units]\n"; use autodie qw(:close); close($O); close($ON); sub get_params { my $inr = shift; my $pr = shift; $pr->{'debug'} = 1; $pr->{'runid'} = -1000; $pr->{'filter'} = ".*"; $pr->{'datefilter'} = ""; $pr->{'filename'} = "NA"; $pr->{'outfolder'} = "."; $pr->{'ssstype'} = ""; foreach my $key (keys %{$inr}) { $pr->{'debug'} = $inr->{$key} if $key eq 'd'; $pr->{'runid'} = $inr->{$key} if $key eq 'n'; $pr->{'filter'} = $inr->{$key} if $key eq 'f'; $pr->{'datefilter'} = $inr->{$key} if $key eq 'g'; $pr->{'filename'} = $inr->{$key} if $key eq 'r'; $pr->{'outfolder'} = $inr->{$key} if $key eq 'o'; $pr->{'ssstype'} = $inr->{$key} if $key eq 't'; } return 1; } sub check_params { my $pr = shift; die "Require RunID if not in debug mode\n" if ($pr->{debug} == 1 and $pr->{runid} == -1000); (-e $pr->{filename}) or die "Unable to find input filename: $pr->{filename}\n"; (-e $pr->{outfolder}) or die "Unable to find outfolder: $pr->{outfolder}\n"; $pr->{filter} = ".*" if $pr->{filter} eq ""; my ($volume, $dirs, $rootname) = File::Spec->splitpath($params{filename}); $rootname =~ s/\.sss$//; my @sp = split(/_/,$rootname); $params{rootname} = $rootname; $params{ssstype} = $sp[-1] if $params{ssstype} eq ""; print "Run Parameters: \n"; foreach my $key (keys %{$pr}) { print "$key -> [$pr->{$key}]\n"; } return 1; } sub get_fields_plan { my $tblFields = shift; $tblFields->{'DATE_EN'} = ["__________ __________ Date",-1]; $tblFields->{'HEADER1'} = ["Header1 Header1 Header1",-1]; $tblFields->{'HEADER2'} = ["Header2 Header2 Header2",-1]; $tblFields->{'HEADER3'} = ["Header3 Header3 Header3",-1]; $tblFields->{'HEADER4'} = ["Header4 Header4 Header4",-1]; $tblFields->{'HEADER5'} = ["Header4 Header5 Header5",-1]; $tblFields->{'HEADER6'} = ["Header6 Header6 Header6",-1]; return $tblFields; } sub assign_headers { my $hdrmap = shift; my $headline = shift; my $sptagref = shift; $$sptagref = '\s*,\s*' if $$headline=~ m/,/; my @hdrs = split(/$$sptagref/, $$headline); foreach my $hdr (@hdrs){ trim(\$hdr); print "CHECKING HEADERS ------------\n"; } foreach my $key (sort keys %{$hdrmap}) { my ( $index )= grep { $hdrs[$_] eq $hdrmap->{$key}[0] } 0..$#hdrs; if (defined ($index)) { $hdrmap->{$key}[1] = $index; print "HEADER MATCH: " . "\t" . $key . "\t" . $hdrmap->{$key}[0] . "\t[" . $hdrmap->{$key}[1]. "]\n"; } else { print "HEADER NOT FOUND: " . "\t" . $key . "\t" . $hdrmap->{$key}[0] . "\n"; } } print "-----------------------------------------------\n"; } sub assign_names { my $hdrmap = shift; my $flist = shift; @$flist = (); foreach my $key (sort keys %{$hdrmap}) { my $i = $hdrmap->{$key}[1]; push(@{$flist}, $key) if ($i != -1); } } sub trim { my $sref = shift; $$sref =~ s/^\s+//; $$sref =~ s/\s+$//; }