Changeset 22430 for trunk/ippScripts/scripts/detrend_norm_calc.pl
- Timestamp:
- Feb 24, 2009, 12:00:25 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/ippScripts/scripts/detrend_norm_calc.pl (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/detrend_norm_calc.pl
r19374 r22430 25 25 use Pod::Usage qw( pod2usage ); 26 26 27 # Look for programs we need 28 my $missing_tools; 29 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1); 30 my $ppNormCalc = can_run('ppNormCalc') or (warn "Can't find ppNormCalc" and $missing_tools = 1); 31 if ($missing_tools) { 32 warn("Can't find required tools."); 33 exit($PS_EXIT_CONFIG_ERROR); 34 } 35 27 36 # Parse command-line arguments 28 37 my ($det_id, $iter, $detType, $outroot, $dbname, $verbose, $no_update, $no_op, $redirect ); 29 38 GetOptions( 30 'det_id|d=s' => \$det_id, # Detrend id31 'iteration|i=s' => \$iter, # Iteration32 'det_type|t=s' => \$detType, # Detrend type33 'outroot|w=s' => \$outroot, # output file base name34 'dbname|d=s' => \$dbname, # Database name35 'verbose' => \$verbose, # Print to stdout36 'no-update' => \$no_update, # Don't update the database?37 'no-op' => \$no_op, # Don't do operations39 'det_id|d=s' => \$det_id, # Detrend id 40 'iteration|i=s' => \$iter, # Iteration 41 'det_type|t=s' => \$detType, # Detrend type 42 'outroot|w=s' => \$outroot, # output file base name 43 'dbname|d=s' => \$dbname, # Database name 44 'verbose' => \$verbose, # Print to stdout 45 'no-update' => \$no_update, # Don't update the database? 46 'no-op' => \$no_op, # Don't do operations 38 47 'redirect-output' => \$redirect, 39 48 ) or pod2usage( 2 ); … … 41 50 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV; 42 51 pod2usage( -msg => "Required options --det_id --iteration --det_type --outroot", 43 -exitval => 3,44 ) unless 45 defined $det_id and 46 defined $iter and 52 -exitval => 3, 53 ) unless 54 defined $det_id and 55 defined $iter and 47 56 defined $detType and 48 57 defined $outroot; 49 58 59 # Unhandled exceptions should be passed on to my_die so they get pushed into the database 60 $SIG{__DIE__} = sub { die @_ if $^S; 61 my_die( $_[0], $det_id, $iter, $PS_EXIT_UNKNOWN_ERROR ); }; 62 50 63 my $logfile = $outroot . ".log"; 51 64 … … 56 69 # Define which detrend types we normalise 57 70 use constant NORMALIZE => { 58 'bias' => 0,59 'dark' => 0,71 'bias' => 0, 72 'dark' => 0, 60 73 'dark_premask' => 0, 61 'shutter' => 0,74 'shutter' => 0, 62 75 'flat_premask' => 1, 63 76 'domeflat_premask' => 1, … … 66 79 'domeflat_raw' => 1, 67 80 'skyflat_raw' => 1, 68 'flat' => 1, 69 'domeflat' => 1, 70 'skyflat' => 1, 71 'fringe' => 0, 72 'mask' => 0, 73 'darkmask' => 0, 74 'flatmask' => 0, 75 }; 76 77 # Look for programs we need 78 my $missing_tools; 79 my $dettool = can_run('dettool') or (warn "Can't find dettool" and $missing_tools = 1); 80 my $ppNormCalc = can_run('ppNormCalc') or (warn "Can't find ppNormCalc" and $missing_tools = 1); 81 if ($missing_tools) { 82 warn("Can't find required tools."); 83 exit($PS_EXIT_CONFIG_ERROR); 84 } 81 'flat' => 1, 82 'domeflat' => 1, 83 'skyflat' => 1, 84 'fringe' => 0, 85 'mask' => 0, 86 'darkmask' => 0, 87 'flatmask' => 0, 88 }; 85 89 86 90 &my_die("Unrecognised detrend type: $detType", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless exists NORMALIZE()->{lc($detType)}; … … 89 93 90 94 # Get the list of inputs 91 my @files; # The input files95 my @files; # The input files 92 96 { 93 97 my $command = "$dettool -processedimfile"; … … 99 103 print "Running [$command]...\n" if $verbose; 100 104 if (not run(\@command, \$stdin, \$stdout, \$stderr)) { 101 &my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?",102 $det_id, $iter, $PS_EXIT_SYS_ERROR);105 &my_die("Unable to perform dettool -processedimfile on detrend $det_id/$iter: $?", 106 $det_id, $iter, $PS_EXIT_SYS_ERROR); 103 107 } 104 108 print $stdout . "\n" if $verbose; 105 109 106 110 # Because of the length, need to split into individual metadatas --- it parses SO much quicker! 107 111 my @whole = split /\n/, $stdout; 108 112 my @single = (); 109 113 while ( scalar @whole > 0 ) { 110 my $value = shift @whole;111 push @single, $value;112 if ($value =~ /^\s*END\s*$/) {113 push @single, "\n";114 115 my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) );116 &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless117 defined $list;118 push @files, @$list;119 @single = ();120 }121 } 122 } 123 124 my $norms; # MDC with normalisations114 my $value = shift @whole; 115 push @single, $value; 116 if ($value =~ /^\s*END\s*$/) { 117 push @single, "\n"; 118 119 my $list = parse_md_list( $mdcParser->parse( join( "\n", @single ) ) ); 120 &my_die("Unable to parse output from dettool", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless 121 defined $list; 122 push @files, @$list; 123 @single = (); 124 } 125 } 126 } 127 128 my $norms; # MDC with normalisations 125 129 if (NORMALIZE()->{lc($detType)} and not $no_op) { 126 130 127 131 my %matrix; # Matrix of statistics as a function of exposures and classes 128 132 foreach my $file (@files) { 129 my $exp_id = $file->{'exp_id'}; # Exposure ID130 my $class_id = $file->{'class_id'}; # Class ID131 my $stat = $file->{STATISTIC()}; # Statistic of interest132 133 # Create matrix elements134 $matrix{$exp_id} = {} if not defined $matrix{$exp_id};135 $matrix{$exp_id}->{$class_id} = $stat;136 } 137 133 my $exp_id = $file->{'exp_id'}; # Exposure ID 134 my $class_id = $file->{'class_id'}; # Class ID 135 my $stat = $file->{STATISTIC()}; # Statistic of interest 136 137 # Create matrix elements 138 $matrix{$exp_id} = {} if not defined $matrix{$exp_id}; 139 $matrix{$exp_id}->{$class_id} = $stat; 140 } 141 138 142 # Generate the input for ppNormCalc 139 my $normData; # Normalisation data143 my $normData; # Normalisation data 140 144 foreach my $exp_id (keys %matrix) { 141 $normData .= "$exp_id\tMETADATA\n";142 foreach my $class_id (keys %{$matrix{$exp_id}}) {143 $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_id}->{$class_id} . "\n";144 }145 $normData .= "END\n\n";145 $normData .= "$exp_id\tMETADATA\n"; 146 foreach my $class_id (keys %{$matrix{$exp_id}}) { 147 $normData .= "\t" . $class_id . "\tF32\t" . $matrix{$exp_id}->{$class_id} . "\n"; 148 } 149 $normData .= "END\n\n"; 146 150 } 147 151 148 152 # Run ppNormCalc 149 153 { 150 my ( $stdout, $stderr ); # Buffers for running program151 my @command = split /\s+/, $ppNormCalc;152 print "Running [$ppNormCalc]...\n" if $verbose;153 if (not run(\@command, \$normData, \$stdout, \$stderr)) {154 &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR);155 }156 print $stdout . "\n" if $verbose;157 158 # Parse the output159 $norms = $mdcParser->parse($stdout);160 &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms;154 my ( $stdout, $stderr ); # Buffers for running program 155 my @command = split /\s+/, $ppNormCalc; 156 print "Running [$ppNormCalc]...\n" if $verbose; 157 if (not run(\@command, \$normData, \$stdout, \$stderr)) { 158 &my_die("Unable to perform ppNormCalc: $?", $det_id, $iter, $PS_EXIT_SYS_ERROR); 159 } 160 print $stdout . "\n" if $verbose; 161 162 # Parse the output 163 $norms = $mdcParser->parse($stdout); 164 &my_die("Unable to parse metadata config doc", $det_id, $iter, $PS_EXIT_PROG_ERROR) unless $norms; 161 165 } 162 166 163 167 } else { 164 168 # It's something that doesn't need normalisation --- just push in a normalisation of 1 165 my %classes; # List of unique classes169 my %classes; # List of unique classes 166 170 foreach my $file (@files) { 167 my $class_id = $file->{'class_id'}; # Class Id168 $classes{$class_id} = 1;169 } 170 171 my $class_id = $file->{'class_id'}; # Class Id 172 $classes{$class_id} = 1; 173 } 174 171 175 foreach my $class_id (keys %classes) { 172 my %mdValue;# Metadata value for this class id173 $mdValue{name} = $class_id;174 $mdValue{value} = 1.0;175 push @$norms, \%mdValue;176 my %mdValue; # Metadata value for this class id 177 $mdValue{name} = $class_id; 178 $mdValue{value} = 1.0; 179 push @$norms, \%mdValue; 176 180 } 177 181 } … … 186 190 foreach my $normItem (@$norms) { 187 191 188 my $className = $normItem->{name}; # Name of component189 my $normalisation = $normItem->{value}; # Normalisation for component190 191 if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {192 warn("Class $className has bad normalisation: $normalisation");193 exit($PS_EXIT_SYS_ERROR);194 }195 196 my $command = $commandBase;197 $command .= " -class_id $className";198 $command .= " -norm $normalisation";199 200 my @command = split /\s+/, $command;201 202 my ( $stdin, $stdout, $stderr ); # Buffers for running program203 print "Running [$command]...\n" if $verbose;204 if (not run \@command, \$stdin, \$stdout, \$stderr) {205 warn("Unable to perform dettool -addnormstat for $className: $?");206 exit($PS_EXIT_SYS_ERROR);207 }208 print $stdout . "\n" if $verbose;192 my $className = $normItem->{name}; # Name of component 193 my $normalisation = $normItem->{value}; # Normalisation for component 194 195 if ($normalisation == 0.0 or lc($normalisation) eq 'nan') { 196 warn("Class $className has bad normalisation: $normalisation"); 197 exit($PS_EXIT_SYS_ERROR); 198 } 199 200 my $command = $commandBase; 201 $command .= " -class_id $className"; 202 $command .= " -norm $normalisation"; 203 204 my @command = split /\s+/, $command; 205 206 my ( $stdin, $stdout, $stderr ); # Buffers for running program 207 print "Running [$command]...\n" if $verbose; 208 if (not run \@command, \$stdin, \$stdout, \$stderr) { 209 warn("Unable to perform dettool -addnormstat for $className: $?"); 210 exit($PS_EXIT_SYS_ERROR); 211 } 212 print $stdout . "\n" if $verbose; 209 213 } 210 214 } else { … … 212 216 foreach my $normItem (@$norms) { 213 217 214 my $className = $normItem->{name}; # Name of component215 my $normalisation = $normItem->{value}; # Normalisation for component216 217 if ($normalisation == 0.0 or lc($normalisation) eq 'nan') {218 warn("Class $className has bad normalisation: $normalisation");219 exit($PS_EXIT_SYS_ERROR);220 }221 print "$className : $normalisation\n";218 my $className = $normItem->{name}; # Name of component 219 my $normalisation = $normItem->{value}; # Normalisation for component 220 221 if ($normalisation == 0.0 or lc($normalisation) eq 'nan') { 222 warn("Class $className has bad normalisation: $normalisation"); 223 exit($PS_EXIT_SYS_ERROR); 224 } 225 print "$className : $normalisation\n"; 222 226 } 223 227 } … … 225 229 sub my_die 226 230 { 227 my $msg = shift; # Warning message on die228 my $det_id = shift; # Detrend identifier229 my $iter = shift; # Iteration230 my $exit_code = shift; # Exit code to add231 my $msg = shift; # Warning message on die 232 my $det_id = shift; # Detrend identifier 233 my $iter = shift; # Iteration 234 my $exit_code = shift; # Exit code to add 231 235 232 236 carp($msg); 233 237 if (defined $det_id and defined $iter and not $no_update) { 234 my $command = "$dettool -addnormalizedstat";235 $command .= " -det_id $det_id";236 $command .= " -iteration $iter";237 # XXX EAM : we should add this to the db : $command .= " -path_base $outroot";238 $command .= " -code $exit_code";239 $command .= " -dbname $dbname" if defined $dbname;238 my $command = "$dettool -addnormalizedstat"; 239 $command .= " -det_id $det_id"; 240 $command .= " -iteration $iter"; 241 # XXX EAM : we should add this to the db : $command .= " -path_base $outroot"; 242 $command .= " -code $exit_code"; 243 $command .= " -dbname $dbname" if defined $dbname; 240 244 system ($command); 241 245 }
Note:
See TracChangeset
for help on using the changeset viewer.
