#!/usr/local/bin/perl -w # # Program 123PREP Jeff Scarbrough 031028 # # This program takes output from laser data prep and matches lines by time. # # We read in each file - two at first for simplicity. Then we create a hash for file 1 using calculated DOY for the key, and the line for the element. # Next, we need to go through file two and calculate DOY...next, we get a list of keys for each hash and put them all in one file, eliminate the # duplicates, and sort the keys in chronological order. Last, we go through the list of keys, test for data in each array for each key, and # print either data or commas to simulate a blank line for each data file at each time for which there exists data. # # # B E G I N # # Set format for output file format OUTPUT_FILE= @###.#### , @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< , @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $key_sort,$array_1{$key_sort},$array_2{$key_sort} . # # Housekeeping and setup # $how_many = 2; # Number of files to match %array_1 = (); # # Open input files, read entire file as array, then close # print "\n\n"; print " This program matches $how_many files\n\n "; print " File name assumes .prn for input\n Output file has root of file 1 and _matched.out appended\n"; print "\nEnter filename for input file # 1 " ; $fileroot_1 = <>; chop $fileroot_1; $in_file[1] = $fileroot_1 . ".prn"; open(INPUT_FILE, $in_file[1]) || die "can't open $in_file[1]: $!"; @array_1 = ; #print @array_1; close(INPUT_FILE) || die "can't close $in_file[1]: $!"; print "\nEnter filename for input file # 2 " ; $fileroot_2 = <>; chop $fileroot_2; $in_file[2] = $fileroot_2 . ".prn"; open(INPUT_FILE, $in_file[2]) || die "can't open $in_file[2]: $!"; @array_2 = ; #print @array_2; close(INPUT_FILE) || die "can't close $in_file[2]: $!"; $out_file = ">" . $fileroot_1 . "_matched.out"; # Read number of elements in file and write to screen $elements = @array_1; print "\n File 1 contains $elements lines\n"; $elements = @array_2; print "\n File 2 contains $elements lines\n"; foreach (@array_1) { $_ =~ s/\\n//; # Strip carraiage return from line # $line_1 = $_; @line_1 = split (/,/); # Chop up line of data into fields where commas exist $month1 = $line_1[9]; $day1 = $line_1[10]; $hour1 = $line_1[11]; $min1 = $line_1[12]; # Calculate fraction of day based on hour and minute, then find day number and add $day_fract1 = (($hour1+($min1/60))/24); if ($month1 == 1) { $doyday = 0; } elsif ($month1 == 2) { $doyday = 31; } elsif ($month1 == 3) { $doyday = 59; } elsif ($month1 == 4) { $doyday = 90; } elsif ($month1 == 5) { $doyday = 120; } elsif ($month1 == 6) { $doyday = 151; } elsif ($month1 == 7) { $doyday = 181; } elsif ($month1 == 8) { $doyday = 212; } elsif ($month1 == 9) { $doyday = 243; } elsif ($month1 == 10) { $doyday = 273; } elsif ($month1 == 11) { $doyday = 304; } elsif ($month1 == 12) { $doyday = 334; } else { print "Bad Month Data!!! \n"; } # End of loop for Day calculation $doy1 = $doyday + $day1 + $day_fract1; @line_1 = join (',',@line_1); $array_1{$doy1} = "@line_1"; } foreach (@array_2) { $_ =~ s/\\n//; # $line_2 = $_; @line_2 = split (/,/); # Chop up line of data into fields where commas exist $month2 = $line_2[9]; $day2 = $line_2[10]; $hour2 = $line_2[11]; $min2 = $line_2[12]; # Calculate fraction of day based on hour and minute, then find day number and add $day_fract2 = (($hour2+($min2/60))/24); if ($month2 == 1) { $doyday = 0; } elsif ($month2 == 2) { $doyday = 31; } elsif ($month2 == 3) { $doyday = 59; } elsif ($month2 == 4) { $doyday = 90; } elsif ($month2 == 5) { $doyday = 120; } elsif ($month2 == 6) { $doyday = 151; } elsif ($month2 == 7) { $doyday = 181; } elsif ($month2 == 8) { $doyday = 212; } elsif ($month2 == 9) { $doyday = 243; } elsif ($month2 == 10) { $doyday = 273; } elsif ($month2 == 11) { $doyday = 304; } elsif ($month2 == 12) { $doyday = 334; } else { print "Bad Month Data!!! \n"; } # End of loop for Day calculation $doy2 = $doyday + $day2 + $day_fract2; @line_2 = join (',',@line_2); $array_2{$doy2} = "@line_2"; } # # Get output file ready # $count = 0; @key_list = (); push @key_list, (keys %array_1); push @key_list, (keys %array_2); # Eliminate duplicate entries in key file and sort in ascending order @key_dup = grep { ++$count{$_} < 2 } @key_list; @key_sort = sort {$a <=> $b} @key_dup; open(OUTPUT_FILE, $out_file) || die "can't open $out_file: $!"; foreach $key_sort(@key_sort) { $array_1{$key_sort} = " , , , , , , , , , , , , , , " unless defined ($array_1{$key_sort}); $array_2{$key_sort} = " , , , , , , , , , , , , , , " unless defined ($array_2{$key_sort}); write OUTPUT_FILE; # print OUTPUT_FILE $key_sort, $array_1{$key_sort},$array_2{$key_sort}; } close(OUTPUT_FILE); exit 0;