#!/usr/bin/perl use File::Spec; use Getopt::Std; #use strict; use IO::File; use Switch; use Time::HiRes qw(gettimeofday tv_interval); #get better than 1 second resolution $| = 1; #Force output my $VERSION = 2.00; my $timerStart = gettimeofday(); # ---------------------------------------------------------------------- # Process Inputs # ---------------------------------------------------------------------- my %inputs = (); my %params = (); getopts('o:r:p:t:n:f:g:d', \%inputs); &get_params(\%inputs, \%params) || die "Error Getting Parameters\n"; &check_params(\%params); # ---------------------------------------------------------------------- # Find and check input files - extract sss type # ---------------------------------------------------------------------- my $infile = $params{filename}; # ---------------------------------------------------------------------- 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 "Input SSS File: [$infile]\n"; print "Output SSS File: [$outfile]\n"; print "Output Names File: [$outnamefile]\n"; # ---------------------------------------------------------------------- my $hdrmap; my %hdrTypes = ( "fields"=>\&get_fields_area, #"flow"=>\&get_fields_flow, #"gather"=>\&get_fields_gather, "regions"=>\&get_fields_region, "plan"=>\&get_fields_plan); die "Unable to match sss type. " unless defined($hdrTypes{$params{ssstype}}); $hdrmap = $hdrTypes{$params{ssstype}}->(); my $namemap = &get_fields_names($params{ssstype}); # ----------------------------------------------- # Load and Process Headers # ----------------------------------------------- open (I,"<$infile") or die "Unable to open file $infile.\n"; open (O,">$outfile") or die "Unable to open file $outfile.\n"; open (ON,">$outnamefile") or die "Unable to open file $outnamefile.\n"; open (OLCT, ">$outLineCount") or die "Unable to open file $outLineCount.\n"; # ----------------------------------------------- #----------------------------------------------------------------------- # Concatinate headers #----------------------------------------------------------------------- for ($i=0; $i<3; $i++) { $line = ; chomp ($line); $line =~ s/^\s+//; $line =~ s/\s+$//; @sp = split (/\s+/,$line); for ($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(); # ----------------------------------------------- # Map Unit Conversions if needed # ----------------------------------------------- my $l_units = ; chomp ($l_units); print "UNITS -- > [$l_units]\n"; sub get_params($$) { # ------------------------------------------- # Input Hash from getopts and Run parameter hash # ------------------------------------------- my $inr = shift(@_); my $pr = shift(@_); # ------------------------------------------- # Setup default parameters # ------------------------------------------- $pr->{'debug'} = 0; #Extra output $pr->{'runid'} = -1000; #Dummy ID $pr->{'filter'} = ".*"; $pr->{'datefilter'} = ""; $pr->{'filename'} = "NA"; #$pr->{'runfolder'} = "."; $pr->{'outfolder'} = "."; $pr->{'ssstype'} = ""; # interpreted from filename # ------------------------------------------- # Get Values from inputs # ------------------------------------------- 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->{'runfolder'} = $inr->{$key} if $key eq 'p'; $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} == 0 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,my $dirs,my $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; # Default for tab separation $$sptagref = '\s*,\s*' if $$headline=~ m/,/; my @hdrs = split(/$$sptagref/, $$headline); foreach my $hdr (@hdrs){trim(\$hdr)} # ----------------------------------------------- # Associate Header with matching index # ----------------------------------------------- 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); } } # -------------------------------------------------------------- # Perl trim function to remove whitespace from the start and end of the string # -------------------------------------------------------------- sub trim() { my $sref = shift; $$sref =~ s/^\s+//; $$sref =~ s/\s+$//; }