#!/usr/bin/perl -w # combine_epics_data_files [path] # # by: Jaideep Singh # # date: 01/30/2006 # # description: combines all files (in the [path] directory) with the same run number into one file # does this for all the runs # *only* works on files with the format *[run number]_[split file number]* # does not write over the original files # # this one also sorts # # creates the "comined" directory to put the new files in # #getting name of path to files + ARGUMENT ERROR HANDLING $path = $ARGV[0]; die "error: Uh, what path do you want me to look at dude?: Too few arguments\n" unless ($path); die "error: Scared and confused: Too many arguments\n" unless (($#ARGV+1) < 2); #getting all sorted filenames in path and building run hash array $raw_files = qx(ls $path); @file_array = split(/\n/,$raw_files); $file_count = 0; $run_count = 0; foreach $file (@file_array) { $file_count++; chomp $file; if ($file =~ /gdh_(\d+)_(\d+)_epics_data.txt/) { $run_hash{$1}{$2} = $file; $run_count++ } } #creating a "combined" subdirectory to the "path" directory $new_dir = $path."combined\/"; $poop = qx(mkdir $new_dir); #looping over each run foreach $run_num (sort keys %run_hash) { # getting the split file numbers for each run @run_split_nums = sort keys %{$run_hash{$run_num}}; print "$run_num : "; # clearing data variable at the start of each new run undef %data; # looping over each split number foreach $split_number (@run_split_nums) { print " $split_number"; $line_count = 0; open(IN_FILE,"$path$run_hash{$run_num}{$split_number}"); # looping for each line in the file while () { $line_count++; chomp; # pulling out the prescalar string if (/(ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+\,ps\d\=\d+)/) { $prescalars = $1 } elsif ($_ ne "") { # adding the current line to the data hash only if it is not blank and not the prescalar string # $data is a hash of a hash of an array # first key = lower case variable name # second key = number of seconds, # array = line parts array @line_parts = split(/\t/); $sort_key = lc($line_parts[2]." ".$line_parts[1]); $data{$sort_key} = $_; } } close(IN_FILE); } print "\n"; # creating and opening up file that will record the total run information $total_run = $run_hash{$run_num}{$run_split_nums[0]}; $total_run =~ s/_[0123456789]_/_/; open(OUT_FILE,">$new_dir$total_run"); print OUT_FILE "$prescalars\n" if ($prescalars); foreach $var_name (sort keys %data) { print OUT_FILE "$data{$var_name}\n" } close(OUT_FILE); }