IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33286


Ignore:
Timestamp:
Feb 16, 2012, 2:42:17 PM (14 years ago)
Author:
bills
Message:

build response file when all of the jobs for a request have finished

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/pstamp/scripts/dquery_finish.pl

    r33141 r33286  
    1313use File::Temp qw( tempfile );
    1414use File::Copy;
     15use Astro::FITS::CFITSIO qw( :constants );
     16Astro::FITS::CFITSIO::PerlyUnpacking(1);
    1517
    1618use PS::IPP::Metadata::Config;
     
    6971    exit 0;
    7072}
    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
    8074if (! -e $outdir ) {
    8175    # something must have gone wrong at the parse stage
     
    9791{
    9892    my $command = "$pstamptool -listjob -req_id $req_id";
     93    $command .= " -jobType detect_query";   # temporary
    9994    $command .= " -dbname $dbname" if $dbname;
    10095    $command .= " -dbserver $dbserver" if $dbserver;
     
    117112}
    118113
    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 
     114my ($REGLIST, $reg_list) = tempfile("$outdir/reglist.XXXX", UNLINK => !$save_temps);
     115my $response_file = "response.$req_id.fits";
     116my $response_path = "$outdir/$response_file";
     117
     118print $REGLIST "$response_file|||table|\n";
     119
     120my @columns = qw(ROWNUM PROC_ERROR NPIX QFACTOR FLUX FLUX_SIG FPA_ID STAGE STAGE_ID COMPONENT CMFFILE);
     121my %colData;
     122foreach my $col (@columns) {
     123    $colData{$col} = [];
     124}
     125   
     126my $i = 0;
    122127foreach my $job (@jobs) {
    123     next if $job->{parent_id};
    124128    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++;
    141139}
    142140close $REGLIST;
    143141
     142write_response_file($response_path, $req_name, \%colData);
     143
    144144if (-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";
    146146    $command .= " --copy --datapath $outdir";
    147147    $command .= " --type MOPS_DETECTABILITY_RESPONSE";
     
    162162exit 0;
    163163
     164sub 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
     191sub 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
     355sub 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}
    164366sub update_request {
    165367    my $req_id = shift;
     
    181383    }
    182384}
     385
     386sub 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.