#!/usr/bin/env perl # -*- perl -*- #----------------------------------------------------------------------- # Module Name: $Source: /Users/goeke/programming/perl/crater/RCS/ar_zero,v $ # Purpose: Receive CCSDS packets and strip out data # Language: Perl 5 # Assumptions: Consistent with Data ICD, 32-02001 # Part Number: # Author: Robert F. Goeke (goeke@space.mit.edu # References: # Copyright: Massachusetts Institute of Technology 2008 #----------------------------------------------------------------------- ######################################################################## # Check the zero crossing value of the science transfer function ######################################################################## $DIR = $ENV{CRATERTOOLS}; $LOWAMP = 64; # command is 8:64 $HIGHAMP = 128; # command is 8:128 # $ECHO = 2; $CMDVALUE = 'LastValue'; # This is rtlm standard ASCII output $STARTECHO = "19200"; # 0x4B00; $STARTDATA = "512"; # 0x0200; # $ENDLOW = 0x00F1; # $ENDHIGH = 0x00F2; $STOPECHO = 0xFFFF; $EVENTS = 'EventAmp'; $Verbose = 0; $InputFiles = ''; $SAMPLES = 4000; # In 10 sec we get about 20,000 samples $OFF = 20; # noise offset/margin ######################################################################## # The master program is here ######################################################################## &Setup; &Run; exit 0; ######################################################################## ######################################################################## ######################################################################## # Help message ######################################################################## sub Help { print STDOUT "Usage: $0 [-h] [-q] [-v] file1 [file2 ...] Processes LRO spacecraft data to extract zero-crossing results flag -h prints out this help message -q quick scan of data; ignores initial echo tags -v verbose operation "; } ######################################################################## # Run the program ######################################################################## sub Run { my (@high,@low,@noise,$ii); if (!$Quick) { while () { # skip to beginning of the test next if ($_ !~ /$CMDVALUE\s+$STARTECHO/); print STDERR "### Found zero_cross test\n" if $Verbose; last; } if (!$_) { print STDERR "ERR: Test title marker $STARTECHO not found\n"; exit 5; } } while () { # skip to first data collection next if ($_ !~ /$CMDVALUE\s+$STARTDATA/); print STDERR "### Start data low collection\n" if $Verbose; last; } if (!$_) { print STDERR "ERR: Low Cal data sync $STARTDATA not found\n"; exit 3; } # @low = &Stat; # extra pause does not seem necessary @low = &Stat; @noise = splice(@low,6,6); while () { next if ($_ !~ /$CMDVALUE\s*$STARTDATA/); print STDERR "### Start data high collection\n" if $Verbose; last; } if (!$_) { print STDERR "ERR: High Cal data sync $STARTDATA not found\n"; exit 3; } # @high = &Stat; @high = &Stat; print "Detector: 1 2 3 4 5 6\n"; printf STDOUT "High: %8.1f%8.1f%8.1f%8.1f%8.1f%8.1f\n", @high; printf STDOUT "Low: %8.1f%8.1f%8.1f%8.1f%8.1f%8.1f\n", @low; printf STDOUT "Noise: %8.1f%8.1f%8.1f%8.1f%8.1f%8.1f\n", @noise; printf STDOUT "Offset:"; for ($ii=0; $ii<6; $ii++) { printf STDOUT "%8.1f", ($LOWAMP*$high[$ii]-$HIGHAMP*$low[$ii])/($LOWAMP-$HIGHAMP); } printf STDOUT "\n"; } ######################################################################## # Setup the program ######################################################################## sub Setup { $Quick = 0; my $foo; while ($foo = shift(@ARGV)) { if ($foo =~ /^-[hH]/) { &Help; exit 0; } if ($foo =~ /^-[qQ]/) { $Quick++; # don't insist on Title marker next; } if ($foo =~ /^-[vV]/) { $Verbose++; next; } if ($foo =~ /^-/) { print STDERR "Unknown flag $foo\n"; &Help; exit 1; } if (-r $foo) { $InputFiles = $InputFiles . "$foo "; next; } else { print STDERR "Could not read $foo as input\n"; exit 2; } } if (!$InputFiles) { print STDERR "$0 requires at least one input file name\n"; &Help; exit 2; } open(GET,"$DIR/browse -f - $InputFiles | $DIR/rtlm -r - -a -t |"); } ######################################################################## # Calculate the means with noise rejection # Cribbed from "statistics" ######################################################################## sub Stat { my $index = $_[1]; my ($ii,$jj,$q,$name,$one,$two,$three,$four,$five,$six); my (@sum,@square,$rejects,$base); $jj = 0; # total event counter $q = $OFF; # match requirement for noise rejection $base = 0; # when 0 we calulate the current mean # to be used following time for noise rejection $rejects = 0; while () { next if ($_ !~ /$EVENTS/); $jj++; my ($name,$one,$two,$three,$four,$five,$six) = split; if (!$base) { # First time through $sum[1] += $one; $sum[2] += $two; $sum[3] += $three; $sum[4] += $four; $sum[5] += $five; $sum[6] += $six; if ($jj>=$SAMPLES) { # End of reference work print STDERR "### Done with baseline\n" if $Verbose; $mone = $sum[1]/$SAMPLES - $OFF; $mtwo = $sum[2]/$SAMPLES - $OFF; $mthree = $sum[3]/$SAMPLES - $OFF; $mfour = $sum[4]/$SAMPLES - $OFF; $mfive = $sum[5]/$SAMPLES - $OFF; $msix = $sum[6]/$SAMPLES - $OFF; $sum[1]=$sum[2]=$sum[3]=$sum[4]=$sum[5]=$sum[6]=0; if ($Verbose) { print STDERR "### means-$OFF: $mone $mtwo $mthree $mfour $mfive $msix\n"; } $jj = 0; # reset event counter $base++; # quit conditioning phase } } else { # only do this second time through if ( $one<$mone || $two<$mtwo || $three<$mthree || $four<$mfour || $five<$mfive || $six<$msix ) { $jj--; $rejects++; next; # noise rejection } $sum[1] += $one; $sum[2] += $two; $sum[3] += $three; $sum[4] += $four; $sum[5] += $five; $sum[6] += $six; $square[1] += $one * $one; $square[2] += $two * $two; $square[3] += $three * $three; $square[4] += $four * $four; $square[5] += $five * $five; $square[6] += $six * $six; last if ($jj>=$SAMPLES); # keep adding until we get enough data } } # end of while() printf STDERR "### %d samples/%d rejected\n", $jj, $rejects if ($Verbose); return if (!$jj); # Avoids divide-by-zero error for ($ii=1; $ii<=6; $ii++) { $mean[$ii] = $sum[$ii]/$jj; $sigma[$ii] = sqrt($square[$ii]/$jj - $mean[$ii]*$mean[$ii]); } splice(@mean,0,1); # because above we ran 1..6 splice(@sigma,0,1); # and we want to pass 0..5 splice(@mean,6,0,@sigma); # tacks @sigma on end of @mean return(@mean); } ######################################################################## # Pod follows ######################################################################## =for html CRaTER -- lf_zero =head2 NAME B -- Calculate zero offset of each CRaTER detector chain =head2 USAGE lf_zero [-h] [-v] filename1 [filename2 ...] =head2 FLAGS -h provides help message -q quick mode; ignores initial echo tags -v verbose mode =head2 DESCRIPTION This script assumes the existance of one or more files which contain the data from a zero-crossing test. The script collects the data file with B and pipes the data through B before searching for echo data markers and then doing the math. The script takes 4000 science data points (about 2 seconds worth) at two different internal cal values and calculates the extrapolated zero crossing point (which, because the electronics is detecting the peak signal + noise, tends always to be positive). The RMS noise present on the (low) internal cal signal is also displayed. =head2 DATA MARKERS required =over =item 0x4B00 -- start of test =item 0x0200 -- start of each data run =back =head2 ENVIRONMENT perl5.002 Minimum version of Perl interpreter required CRATERTOOLS Environment variable containing path to CRaTER utilities CRATER_GSE Environment variable containing EGSE machine name =head2 BUGS The internal calibration signals have non-linearities, Cal High much worse than Cal Low. We therefore base the zero offset calculations on two values below 10% of full scale. =head2 SEE ALSO lf_zero, browse, rtlm =head2 AUTHOR Bob Goeke =head2 RCS Information $Id: ar_zero,v 1.4 2008/04/11 18:42:31 goeke Exp goeke $ =cut ######################################################################## # history follows ######################################################################## # $Log: ar_zero,v $ # Revision 1.4 2008/04/11 18:42:31 goeke # Added rms noise calculations # # Revision 1.3 2008/04/11 14:15:01 goeke # Take out start/stop flags for browse # Fix help and documentation # Add -q flag and fix logic # # Revision 1.2 2008/04/09 19:02:47 goeke # Working version # # Revision 1.1 2008/04/08 19:04:44 goeke # Initial revision # # Revision 1.4 2007/11/14 16:43:40 goeke # Reverse order of high/low report # # Revision 1.3 2007/08/17 14:40:58 goeke # Minor cleanups # # Revision 1.2 2007/04/17 12:16:35 goeke # Bug fixes; add POD # # Revision 1.1 2007/04/13 19:22:38 goeke # Initial revision # #