Changeset 33286
- Timestamp:
- Feb 16, 2012, 2:42:17 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/pstamp/scripts/dquery_finish.pl (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/pstamp/scripts/dquery_finish.pl
r33141 r33286 13 13 use File::Temp qw( tempfile ); 14 14 use File::Copy; 15 use Astro::FITS::CFITSIO qw( :constants ); 16 Astro::FITS::CFITSIO::PerlyUnpacking(1); 15 17 16 18 use PS::IPP::Metadata::Config; … … 69 71 exit 0; 70 72 } 71 if (0) { 72 my $prod_dir = "$outputDataStoreRoot/$product"; 73 if (! -e $prod_dir ) { 74 # something must have gone wrong at the parse stage 75 update_request($req_id, $PS_EXIT_SYS_ERROR, $verbose); 76 die "product directory does not exist $prod_dir"; 77 } 78 my $outdir = "$prod_dir/$req_name"; 79 } 73 80 74 if (! -e $outdir ) { 81 75 # something must have gone wrong at the parse stage … … 97 91 { 98 92 my $command = "$pstamptool -listjob -req_id $req_id"; 93 $command .= " -jobType detect_query"; # temporary 99 94 $command .= " -dbname $dbname" if $dbname; 100 95 $command .= " -dbserver $dbserver" if $dbserver; … … 117 112 } 118 113 119 # XXX: have the jobs produce the reglist as with postage stamp requests 120 my ($REGLIST, $reg_list) = tempfile("$outdir/reqlist.XXXX", UNLINK => !$save_temps); 121 114 my ($REGLIST, $reg_list) = tempfile("$outdir/reglist.XXXX", UNLINK => !$save_temps); 115 my $response_file = "response.$req_id.fits"; 116 my $response_path = "$outdir/$response_file"; 117 118 print $REGLIST "$response_file|||table|\n"; 119 120 my @columns = qw(ROWNUM PROC_ERROR NPIX QFACTOR FLUX FLUX_SIG FPA_ID STAGE STAGE_ID COMPONENT CMFFILE); 121 my %colData; 122 foreach my $col (@columns) { 123 $colData{$col} = []; 124 } 125 126 my $i = 0; 122 127 foreach my $job (@jobs) { 123 next if $job->{parent_id};124 128 my $job_id = $job->{job_id}; 125 my $response_file = "response.${job_id}.fits"; 126 my $response_path = "$outdir/$response_file"; 127 128 if (-e $response_path) { 129 # the job generated a response file put it into the Data Store 130 print $REGLIST "$response_file|||table|\n"; 131 132 # do the same if we have an error file. Should the parse data be uploaded as well? 133 my $err_file = "parse_error.txt"; 134 if (-e "$outdir/$err_file") { 135 print $REGLIST "$err_file|||text|\n"; 136 } 137 } else { 138 print STDERR "detect_query response file for job $job_id not found\n" if $verbose; 139 $request_fault = $PS_EXIT_UNKNOWN_ERROR; 140 } 129 130 my $outputBase = $job->{outputBase}; 131 my $results = "${outputBase}results.txt"; 132 133 read_results($results, \%colData); 134 135 my $cmf = $colData{CMFFILE}->[$i]; 136 print $REGLIST "$cmf|||table|\n"; 137 138 $i++; 141 139 } 142 140 close $REGLIST; 143 141 142 write_response_file($response_path, $req_name, \%colData); 143 144 144 if (-s $reg_list) { 145 my $command = " $dsreg --add $req_name --product $product --list $reg_list";145 my $command = "echo Skipping $dsreg --add $req_name --product $product --list $reg_list"; 146 146 $command .= " --copy --datapath $outdir"; 147 147 $command .= " --type MOPS_DETECTABILITY_RESPONSE"; … … 162 162 exit 0; 163 163 164 sub read_results { 165 my $results_file = shift; 166 my $results = shift; 167 168 open IN, "<$results_file" or my_die("failed to open $results_file", $PS_EXIT_UNKNOWN_ERROR); 169 170 foreach my $line (<IN>) { 171 # skip header 172 next if $line =~ /ROWNUM/; 173 chomp $line; 174 my @words = split " ", $line; 175 my ($rownum, $proc_error, $npix, $qfactor, $flux, $flux_sig, $fpa_id, $stage, $stage_id, $component, $cmf) = @words; 176 push @{$results->{ROWNUM}}, $rownum; 177 push @{$results->{PROC_ERROR}}, $proc_error; 178 push @{$results->{NPIX}}, $npix; 179 push @{$results->{QFACTOR}}, $qfactor; 180 push @{$results->{FLUX}}, $flux; 181 push @{$results->{FLUX_SIG}}, $flux_sig; 182 push @{$results->{FPA_ID}}, $fpa_id; 183 push @{$results->{STAGE}}, $stage; 184 push @{$results->{STAGE_ID}}, $stage_id; 185 push @{$results->{COMPONENT}}, $component; 186 push @{$results->{CMFFILE}}, $cmf; 187 } 188 # XXX: make sure that the size of each of these arrays is the same 189 } 190 191 sub write_response_file { 192 my $output = shift; 193 my $query_id = shift; 194 my $results = shift; 195 196 my $columns; 197 my $headers; 198 199 my $EXTVER_IS_1 = 0 ; # (scalar(keys(%query)) == 1); 200 # print "EXTVER: $EXTVER_IS_1\n"; 201 my ($FPA_ID,$MJD_OBS,$filter,$obscode,$status); 202 if ($EXTVER_IS_1 == 1) { 203 # Specification of columns to write 204 $columns = [ 205 # matching rownum from detectability original request 206 { name => 'ROWNUM', type => '20A', writetype => TSTRING }, 207 # any errors that occurred during processing 208 { name => 'ERROR_CODE', type => 'V', writetype => TULONG }, 209 # number of pixels used in hypothetical PSF for the query detection 210 { name => 'DETECT_N', type => 'V', writetype => TULONG }, 211 # detectibility, indicating the fraction of PSF pixels detetable by IPP 212 { name => 'DETECT_F', type => 'D', writetype => TDOUBLE }, 213 # flux of the target source 214 { name => 'TARGET_FLUX', type => 'D', writetype => TDOUBLE }, 215 # error in the flux of the target source 216 { name => 'TARGET_FLUX_SIG', type => 'D', writetype => TDOUBLE }, 217 ]; 218 219 # Header translation table 220 $headers = { 221 'QUERY_ID' => { name => 'QUERY_ID', type => TSTRING, 222 comment => 'MOPS Query ID for this batch query' }, 223 'FPA_ID' => { name => 'FPA_ID', type => TSTRING, 224 comment => 'original FPA_ID used at ingest' }, 225 # 'MJD-OBS' => { name => 'FPA_ID', type => TSTRING, 226 # comment => 'original FPA_ID used at ingest' }, 227 # 'FILTER' => { name => 'FPA_ID', type => TSTRING, 228 # comment => 'original FPA_ID used at ingest' }, 229 # 'OBSCODE' => { name => 'FPA_ID', type => TSTRING, 230 # comment => 'original FPA_ID used at ingest' }, 231 }; 232 } 233 else { 234 # Specification of columns to write 235 $columns = [ 236 # matching rownum from detectability original request 237 { name => 'ROWNUM', type => '20A', writetype => TSTRING}, 238 # any errors that occurred during processing 239 { name => 'ERROR_CODE', type => 'V', writetype => TULONG }, 240 # number of pixels used in hypothetical PSF for the query detection 241 { name => 'DETECT_N', type => 'V', writetype => TULONG }, 242 # detectibility, indicating the fraction of PSF pixels detetable by IPP 243 { name => 'DETECT_F', type => 'D', writetype => TDOUBLE }, 244 # flux of the target source 245 { name => 'TARGET_FLUX', type => 'D', writetype => TDOUBLE }, 246 # error in the flux of the target source 247 { name => 'TARGET_FLUX_SIG', type => 'D', writetype => TDOUBLE }, 248 # The FPA That would be in the header if it were to be there. 249 { name => 'FPA_ID', type => '20A', writetype => TSTRING }, 250 ]; 251 252 # Header translation table 253 $headers = { 254 'QUERY_ID' => { name => 'QUERY_ID', type => TSTRING, 255 comment => 'MOPS Query ID for this batch query' }, 256 }; 257 } 258 259 # Parse the list of columns 260 my @colNames; # Names of columns 261 my @colTypes; # Types of columns 262 my %colData; # Data for each column 263 my @colWriteType; # type to use to write 264 foreach my $colSpec ( @$columns) { 265 push @colNames, $colSpec->{name}; 266 push @colTypes, $colSpec->{type}; 267 push @colWriteType, $colSpec->{writetype}; 268 $colData{$colSpec->{name}} = []; 269 } 270 271 my $inHeader = { }; 272 273 # Hack to force the data to match the detect_response_create formats 274 275 $inHeader->{QUERY_ID}->{value} = $query_id; 276 if ($EXTVER_IS_1 == 1) { 277 my $fpa_id = $results->{FPA_ID}->[0]; 278 $inHeader->{FPA_ID}->{value} = $fpa_id; 279 } 280 281 # Fill the table columns with the data, making sure the flux is defined 282 283 @{$colData{'ROWNUM'}} = @{ $results->{ROWNUM} }; 284 @{$colData{'ERROR_CODE'}} = @{ $results->{PROC_ERROR} }; 285 @{$colData{'DETECT_N'}} = @{ $results->{NPIX} }; 286 @{$colData{'DETECT_F'}} = @{ $results->{QFACTOR} }; 287 @{$colData{'TARGET_FLUX'}} = @{ $results->{FLUX} }; 288 @{$colData{'TARGET_FLUX_SIG'}} = @{ $results->{FLUX_SIG} }; 289 @{$colData{'FPA_ID'}} = @{ $results->{FPA_ID} }; 290 291 my $numRows = 0; 292 # Back to detect_response_create: 293 $status = 0; 294 # print "$output\n"; 295 if (-e $output) { 296 unlink $output or die "failed to unlink existing response file $output\n"; 297 } 298 my $outFits = Astro::FITS::CFITSIO::create_file( $output, $status ); 299 check_fitsio( $status ); 300 $outFits->create_img( 16, 0, undef, $status ); 301 check_fitsio( $status ); 302 my $EXTNAME = 'MOPS_DETECTABILITY_RESPONSE'; 303 304 $outFits->create_tbl( BINARY_TBL(), $numRows, scalar @colNames, \@colNames, \@colTypes, undef, $EXTNAME, $status); 305 check_fitsio( $status ); 306 307 # TODO: get the extension version number from somewhere common 308 $outFits->write_key( TINT, 'EXTVER', 2, 'Version of this Extension', $status ); 309 check_fitsio( $status ); 310 311 # Write the Extension keywords 312 foreach my $keyword ( keys %$headers ) { 313 my $value = $inHeader->{$keyword}->{value}; # Header keyword value 314 unless (defined $value) { 315 print "Can't find header keyword $keyword\n"; 316 next; 317 } 318 $value =~ s/\'//g; 319 my $name = $headers->{$keyword}->{name}; # New name 320 my $type = $headers->{$keyword}->{type}; # Type 321 my $comment = $headers->{$keyword}->{comment}; # Comment 322 $outFits->write_key( $type, $name, $value, $comment, $status ); 323 check_fitsio( $status ); 324 } 325 326 for (my $i = 0; $i < scalar @colNames; $i++) { 327 my $colName = $colNames[$i];# Column name 328 my $writeType = $colWriteType[$i]; 329 my $numRows = scalar(@{$colData{$colName}}); 330 unless(defined($writeType)) { 331 print "write type undefined for $colName\n"; 332 } 333 unless(defined($numRows)) { 334 print "num Rows undefined for $colName\n"; 335 } 336 unless(defined($status)) { 337 print "status undefined for $colName\n"; 338 } 339 unless(defined($colData{$colName})) { 340 print "col data undefined for $colName\n"; 341 } 342 unless(defined($colName)) { 343 print "column name undefined for $i\n"; 344 } 345 print STDERR "$writeType $i $numRows $colName $status @{ $colData{$colName} }\n"; 346 $outFits->write_col( $writeType, $i + 1, 1, 1, $numRows, $colData{$colName}, $status ); 347 print STDERR "$writeType $i $numRows $colName $status\n"; 348 check_fitsio( $status ); 349 } 350 $outFits->close_file( $status ); 351 return($status); 352 } 353 354 # From Astro::FITS::CFITSIO demo 355 sub check_fitsio 356 { 357 my $status = shift; # Status of FITSIO calls 358 359 if ($status != 0) { 360 my $msg; # Message to output 361 Astro::FITS::CFITSIO::fits_get_errstatus( $status , $msg ); 362 carp("CFITSIO error: $status => $msg"); 363 die "CFITSIO error: $msg\n"; 364 } 365 } 164 366 sub update_request { 165 367 my $req_id = shift; … … 181 383 } 182 384 } 385 386 sub my_die { 387 my $msg = shift; 388 my $fault = shift; 389 390 print STDERR $msg; 391 392 update_request($req_id, $fault); 393 394 exit $fault; 395 }
Note:
See TracChangeset
for help on using the changeset viewer.
