Changeset 20761 for trunk/ippScripts/scripts/magic_destreak.pl
- Timestamp:
- Nov 16, 2008, 1:38:19 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/ippScripts/scripts/magic_destreak.pl (modified) (12 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/ippScripts/scripts/magic_destreak.pl
r20732 r20761 16 16 use IPC::Cmd 0.36 qw( can_run run ); 17 17 use File::Temp qw( tempfile ); 18 use File::Basename qw( basename ); 18 19 use PS::IPP::Metadata::Config; 19 20 use PS::IPP::Metadata::List qw( parse_md_list ); … … 27 28 28 29 # Parse the command-line arguments 29 my ($magic_id, $camera, $streaks, $stage, $class_id, $skycell_id, $tmpdir, $recoverydir); 30 my ($magic_ds_id, $camera, $streaks, $stage, $stage_id, $component, $uri, $path_base, $cam_path_base); 31 my ($outroot, $recoveryroot); 30 32 my ($replace, $remove); 31 33 my ($dbname, $save_temps, $verbose, $no_update, $no_op, $logfile); 32 34 33 35 GetOptions( 34 'magic_ id=s' => \$magic_id, # Magicidentifier35 'camera=s' => \$camera, 36 'magic_ds_id=s' => \$magic_ds_id,# Magic destreak run identifier 37 'camera=s' => \$camera, # camera for evaluating file rules 36 38 'streaks=s' => \$streaks, # file containing the list of streaks 37 39 'stage=s' => \$stage, # raw, chip, warp, or diff 38 'class_id=s' => \$class_id, 39 'skycell_id=s' => \$skycell_id, 40 'tmpdir=s' => \$tmpdir, # "directory" for temporary images (may be nebulous) 41 'recoverydir=s' => \$recoverydir,# "directory" for saving the images of excised pixels 42 'replace' => \$replace, # replace the input images with the results. 43 'remove' => \$remove, # remove the original images when done YIKES! 40 'stage_id=s' => \$stage_id, # exp_id, chip_id, warp_id, or diff_id 41 'component=s' => \$component, # the class_id or skycell_id 42 'uri=s' => \$uri, # uri of the input image 43 'path_base=s' => \$path_base, # path_base of the input 44 'cam_path_base=s'=> \$cam_path_base, # path_base of the associated camera run 45 'outroot=s' => \$outroot, # "directory" for temporary images (may be nebulous) 46 'recoveryroot=s' => \$recoveryroot,# "directory" for saving the images of excised pixels 47 'replace=s' => \$replace, # replace the input images with the results. 48 'remove=s' => \$remove, # remove the original images when done YIKES! 44 49 'save-temps' => \$save_temps, # Save temporary files? 45 50 'dbname=s' => \$dbname, # Database name … … 51 56 52 57 pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV; 53 pod2usage( -msg => "Required options: --magic_ id --camera --streaks --stage --tmpdir",58 pod2usage( -msg => "Required options: --magic_ds_id --camera --streaks --stage --outroot", 54 59 -exitval => 3) unless 55 defined $magic_ id and60 defined $magic_ds_id and 56 61 defined $camera and 57 62 defined $streaks and 58 63 defined $stage and 59 defined $tmpdir; 64 defined $stage_id and 65 defined $component and 66 defined $outroot; 60 67 61 if (!defined($class_id) and (($stage eq "raw") or ($stage eq "chip"))) { 62 carp ("--class_id is required for stage $stage"); 63 exit $PS_EXIT_PROG_ERROR; 64 } elsif (!defined($skycell_id) and (($stage eq "warp") or ($stage eq "diff"))) { 65 carp ("--skycell_id is required for stage $stage"); 66 exit $PS_EXIT_PROG_ERROR; 68 69 my ($skycell_args, $class_id, $skycell_id); 70 71 if (($stage eq "raw") or ($stage eq "chip")) { 72 $class_id = $component; 73 $skycell_args = " -class_id $component"; 74 } elsif ($stage eq "warp") { 75 $skycell_id = $component; 76 $skycell_args = " -skycell_id $component"; 77 } elsif ($stage eq "diff") { 78 $skycell_id = $component; 79 } else { 80 &my_die("Invalid value for stage: $stage", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR); 67 81 } 68 82 … … 74 88 # Look for programs we need 75 89 my $missing_tools; 76 my $magic tool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1);90 my $magicdstool = can_run('magicdstool') or (warn "Can't find magicdstool" and $missing_tools = 1); 77 91 my $streaksremove = can_run('streaksremove') or (warn "Can't find streaksremove" and $missing_tools = 1); 92 # todo get rid of this dependence on magic_id 93 my $magictool = can_run('magictool') or (warn "Can't find magictool" and $missing_tools = 1); 78 94 if ($missing_tools) { 79 95 warn("Can't find required tools."); … … 83 99 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files 84 100 85 my $magictool_args; 86 my $skycell_args; 87 my $component; 88 if ($stage eq "raw") { 89 $magictool_args = "-rawimfile -class_id $class_id"; 90 $skycell_args = " -class_id $class_id"; 91 $component = $class_id; 92 } elsif ($stage eq "chip") { 93 $magictool_args = "-chipprocessedimfile -class_id $class_id"; 94 $skycell_args = " -class_id $class_id"; 95 $component = $class_id; 96 } elsif ($stage eq "warp") { 97 $magictool_args = "-warpskyfile -skycell_id $skycell_id"; 98 $skycell_args = " -skycell_id $skycell_id"; 99 $component = $skycell_id; 100 } elsif ($stage eq "diff") { 101 $magictool_args = "-diffskyfile -skycell_id $skycell_id"; 102 $component = $skycell_id; 103 } else { 104 carp("invalid stage value found: $stage\n"); 105 exit $PS_EXIT_PROG_ERROR; 106 } 107 108 ### Get the input information 109 my $input; 110 { 111 my $command = "$magictool -magic_id $magic_id $magictool_args"; 101 my $basename = basename($path_base); 102 103 # 104 # parse the replace and remove arguments check for errors and set upt 105 # the appropriate paths 106 if (defined($replace) and ($replace eq "F")) { 107 $replace = 0; 108 } 109 110 if (($stage eq "raw") and $replace and ! $recoveryroot) { 111 &my_die("Can not replace raw files without defining recoveryroot.", 112 $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR); 113 } 114 115 if (defined($remove) and ($remove eq "F")) { 116 $remove = 0; 117 } 118 119 if ($remove and !$replace) { 120 &my_die("cannot remove without replace", $magic_ds_id, $component, $PS_EXIT_CONFIG_ERROR); 121 } 122 123 my $backup_uri; 124 if (! $remove) { 125 $backup_uri = "$outroot/$basename"; 126 } 127 128 my $recovery_uri; 129 if ($recoveryroot) { 130 $recovery_uri = "$recoveryroot/$basename"; 131 } 132 133 # get skycell list if needed 134 my ($sfh, $skycell_list); 135 if ($skycell_args) { 136 my $command = "$magicdstool -magic_ds_id $magic_ds_id -getskycells $skycell_args"; 112 137 $command .= " -dbname $dbname" if defined $dbname; 113 138 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = … … 115 140 unless ($success) { 116 141 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 117 &my_die("Unable to perform magic tool $magictool_args: $error_code", $magic_id, $component, $error_code);142 &my_die("Unable to perform magicdstool -diffskyfile $skycell_args: $error_code", $magic_ds_id, $component, $error_code); 118 143 } 119 144 120 145 my $metadata = $mdcParser->parse(join "", @$stdout_buf) or 121 &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR); 122 123 my $input_list = parse_md_list($metadata) or 124 &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR); 125 126 $input = $input_list->[0]; 127 } 128 129 # get skycell list if needed 130 my ($sfh, $skycell_list); 131 if ($skycell_args) { 132 my $command = "$magictool -magic_id $magic_id -diffskyfile $skycell_args"; 133 $command .= " -class_id $class_id" if $class_id; 134 $command .= " -dbname $dbname" if defined $dbname; 135 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 136 run(command => $command, verbose => $verbose); 137 unless ($success) { 138 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 139 &my_die("Unable to perform magictool -diffskyfile $skycell_args: $error_code", $magic_id, $component, $error_code); 140 } 141 142 my $metadata = $mdcParser->parse(join "", @$stdout_buf) or 143 &my_die("Unable to parse metadata config doc", $magic_id, $component, $PS_EXIT_PROG_ERROR); 144 145 my $input_list = parse_md_list($metadata) or 146 &my_die("Unable to parse metadata list", $magic_id, $component, $PS_EXIT_PROG_ERROR); 146 &my_die("Unable to parse metadata config doc", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR); 147 148 my $skycells = parse_md_list($metadata) or 149 &my_die("Unable to parse metadata list", $magic_ds_id, $component, $PS_EXIT_PROG_ERROR); 147 150 ($sfh, $skycell_list) = tempfile( "/tmp/skycell_list.XXXX", UNLINK => !$save_temps); 148 151 149 foreach my $skycell (@$ input_list) {152 foreach my $skycell (@$skycells) { 150 153 print $sfh "$skycell->{uri}\n" 151 154 } … … 153 156 } 154 157 155 my $image = $input->{uri}; 156 my $path_base = $input->{path_base}; 158 my $image = $uri; 157 159 158 160 my ($mask, $weight, $astrom); 159 161 160 162 if ($stage eq "raw") { 161 $astrom = $ipprc->filename("PSASTRO.OUTPUT", $ input->{cam_path_base});163 $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base); 162 164 } elsif ($stage eq "chip") { 163 165 $mask = $ipprc->filename("PSASTRO.OUTPUT.MASK", $path_base, $class_id); 164 166 $weight = $ipprc->filename("PPIMAGE.CHIP.WEIGHT", $path_base, $class_id); 165 $astrom = $ipprc->filename("PSASTRO.OUTPUT", $ input->{cam_path_base});167 $astrom = $ipprc->filename("PSASTRO.OUTPUT", $cam_path_base); 166 168 } elsif ($stage eq "warp") { 167 169 $mask = $ipprc->filename("PSWARP.OUTPUT.MASK", $path_base); … … 173 175 174 176 { 175 my $command = "$streaksremove -stage $stage -tmproot $ tmpdir-streaks $streaks -image $image";177 my $command = "$streaksremove -stage $stage -tmproot $outroot -streaks $streaks -image $image"; 176 178 $command .= " -class_id $class_id" if defined $class_id; 177 $command .= " -recovery $recovery dir" if defined $recoverydir;179 $command .= " -recovery $recoveryroot" if defined $recoveryroot; 178 180 $command .= " -astrom $astrom" if defined $astrom; 179 # skip mask and weight images for now181 # XXX: remove this skip mask and weight images for now 180 182 # $command .= " -mask $mask" if defined $mask; 181 183 # $command .= " -weight $weight" if defined $weight; … … 187 189 unless ($success) { 188 190 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 189 &my_die("Unable to perform streaksremove: $error_code", $magic_ id, $component, $error_code);191 &my_die("Unable to perform streaksremove: $error_code", $magic_ds_id, $component, $error_code); 190 192 } 191 193 } else { … … 194 196 } 195 197 196 ### Input result into database 197 #{ 198 # my $command = "$magictool -addresult"; 199 # $command .= " -magic_id $magic_id"; 200 # $command .= " -node $node"; 201 # $command .= " -uri $outroot"; 202 # $command .= " -dbname $dbname" if defined $dbname; 203 # 204 # # Add the processed file to the database 205 # unless ($no_update) { 206 # my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 207 # run(command => $command, verbose => $verbose); 208 # unless ($success) { 209 # $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 210 # &my_die("Unable to perform magictool -addresult: $error_code", $magic_id, $node, $error_code); 211 # } 212 # } else { 213 # print "Skipping command: $command\n"; 214 # } 215 #} 198 # if recovery and/or backup files were expected make sure they exist 199 200 # Input result into database 201 { 202 my $command = "$magicdstool -adddestreakedfile"; 203 $command .= " -magic_ds_id $magic_ds_id"; 204 $command .= " -component $component"; 205 $command .= " -backup_uri $backup_uri" if $backup_uri; 206 $command .= " -recovery_uri $recovery_uri" if $recovery_uri; 207 $command .= " -dbname $dbname" if defined $dbname; 208 209 # Add the processed file to the database 210 unless ($no_update) { 211 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 212 run(command => $command, verbose => $verbose); 213 unless ($success) { 214 $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR); 215 &my_die("Unable to perform magicdstool -addresult: $error_code", $magic_ds_id, $component, 216 $error_code); 217 } 218 } else { 219 print "Skipping command: $command\n"; 220 } 221 } 216 222 217 223 … … 223 229 { 224 230 my $file = shift; # Name of file 225 &my_die("Unable to find output file: $file", $magic_ id, $component, $PS_EXIT_SYS_ERROR) unless231 &my_die("Unable to find output file: $file", $magic_ds_id, $component, $PS_EXIT_SYS_ERROR) unless 226 232 $ipprc->file_exists($file); 227 233 } … … 230 236 { 231 237 my $msg = shift; # Warning message on die 232 my $magic_ id = shift; # Magicidentifier238 my $magic_ds_id = shift; # Magic DS identifier 233 239 my $component = shift; # class_id or skycell_id 234 240 my $exit_code = shift; # Exit code to add 235 241 242 my $command = "$magicdstool -adddestreakedfile"; 243 $command .= " -magic_ds_id $magic_ds_id"; 244 $command .= " -component $component"; 245 $command .= " -code $exit_code"; 246 $command .= " -dbname $dbname" if defined $dbname; 247 248 # Add the processed file to the database 249 unless ($no_update) { 250 my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) = 251 run(command => $command, verbose => $verbose); 252 unless ($success) { 253 carp("failed to update database for $magic_ds_id $component"); 254 } 255 } else { 256 print "Skipping command: $command\n"; 257 } 258 236 259 carp($msg); 237 260 exit $exit_code;
Note:
See TracChangeset
for help on using the changeset viewer.
