#!/usr/bin/perl -w # get_all_epics.perl [filename] # # by: Jaideep Singh # # date: 12/17/2005 # # about: reads a raw data file and looks for all possible EPICS variables # creates a file: # (1) the first line is the prescalar declaration for that run: # # ps1=2,ps2=3,ps3=9999,ps4=9999,ps5=65535,ps6=65535,ps7=65535,ps8=65534 # # (2) contains 4 columns which contain the timestamp, epics variable name, and epics value: # # timestampseconds from Jan 1 2003EPICS variable namedata value # 2003-08-16 15:41:56 19669316 IPM1H04B.XPOS -0.0408508 # # Note that some EPICS-like stuff still leaks through... # # EPICS info is injected into the data stream at certain times invertals. # More important stuff is logged more often. # Immediately preceding one log cycle, a timestamp is grabbed # Each variable is recorded in ASCII format on its own line: # # [variable name, upto 30 characters][at least one blank space][upto 20 characters for value] # # for example: # # IPM1H04B.XPOS -0.0408508 # 000000000111111111122222222223333333333444444444455 # 123456789012345678901234567890123456789012345678901 # # Variable names can have ".", ":","+","_" as well as alphanumeric characters use strict "subs"; sub jtime { ($sec, $min, $hr, $day, $mon) = @_; # 1041397200 in unix time, the number of seconds from midnight Jan 1 1970 to Jan 1 2003 # (31,28,31,30,31,30,31,31,30,31,30,31); @mon2day = (0,31,59,90,120,151,181,212,243,273,304,334); $totsec = $sec + 60*($min + 60*($hr + 24*($day -1 + $mon2day[$mon-1]))); return $totsec; }; #month look up hash %mon2num = ( "January" => "01", "February" => "02", "March" => "03", "April" => "04", "May" => "05", "June" => "06", "July" => "07", "August" => "08", "September" => "09", "October" => "10", "November" => "11", "December" => "12", "Jan" => "01", "Feb" => "02", "Mar" => "03", "Apr" => "04", "May" => "05", "Jun" => "06", "Jul" => "07", "Aug" => "08", "Sep" => "09", "Oct" => "10", "Nov" => "11", "Dec" => "12" ); #getting name of file to search + ARGUMENT ERROR HANDLING $filename = $ARGV[0]; print "Uh, what file do you want me to look at dude?: Too few arguments\n" and die unless ($filename); print "Scared and confused: Too many arguments\n" and die unless (($#ARGV+1) < 2); open(DIE_FILE,">>dead_file_list.txt"); open(FILE,$filename) or print DIE_FILE "$filename\n" and die "d'oh!: - $filename - not found\n"; #print "\nSearching raw data file:\t$filename\n"; #initializing variables $count = 0; $line_count = 0; #searching line by line, grabbing the timestamp before and after searching #print "\nstart\t"; #print scalar localtime; #print "\n"; while() { $line_count++; 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; } # 1 2 3 4 5 6 # Mon Day Hour Min Sec Year if (/[MTWFS][ouehra][neduit]\s+([JFMASOND][aepuco][nbrylgptvc])\s+(\d+)\s+(\d+)\:(\d+)\:(\d+)\s+E[DS]T\s+(2003)/) { chomp; $current_year = $6; $current_month = $mon2num{$1}; $current_day = $2; $current_day = "0".$2 unless (length($current_day) == 2); $current_hour = $3; $current_hour = "0".$2 unless (length($current_hour) == 2); $current_minute = $4; $current_minute = "0".$2 unless (length($current_minute) == 2); $current_second = $5; $current_second = "0".$2 unless (length($current_second) == 2); $current_time_stamp = $current_year."-".$current_month."-".$current_day." ".$current_hour."\:".$current_minute."\:".$current_second; $current_number_of_seconds = jtime($current_second,$current_minute,$current_hour,$current_day,$current_month); # print "\n $_ -----> $current_time_stamp \n" and die; } if (/^([\w\-.:+]{5,30})(\s{1,26})([\w+\-.]{1}[\w+\-.\s:]{0,19})$/) { if (length($_) < 52) { $count++; $data{$1.$current_time_stamp}[0] = $current_time_stamp; $data{$1.$current_time_stamp}[1] = $current_number_of_seconds; $data{$1.$current_time_stamp}[2] = $1; $data{$1.$current_time_stamp}[3] = $3; chomp $data{$1.$current_time_stamp}[3]; } } }; close(FILE); #print "end\t"; #print scalar localtime; #print "\n\nFound $count total instances of possible EPICS variables\n\n"; #setting up output file @path_name_parts = split(/\//,$filename); @file_name_parts = split(/\./,$path_name_parts[$#path_name_parts]); $file_out = $file_name_parts[0]."_".$file_name_parts[$#file_name_parts]."_epics_data.txt"; #print "OUTPUT filename : $file_out\n\n"; open(OUTPUT,">$file_out"); print OUTPUT "$prescalars\n"; foreach $variable_name (sort keys %data) { print OUTPUT "$data{$variable_name}[0]\t$data{$variable_name}[1]\t$data{$variable_name}[2]\t$data{$variable_name}[3]\n"; } close(OUTPUT); close(DIE_FILE);