IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 38937


Ignore:
Timestamp:
Oct 21, 2015, 5:44:52 PM (11 years ago)
Author:
watersc1
Message:

Untested fullforce_summary handlers. addtool compiles and addstar_run.pl doesn't complain about variables, but I have not confirmed they work correctly.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippScripts/scripts/addstar_run.pl

    r38090 r38937  
    3131my $addtool = can_run('addtool') or (warn "Can't find addtool" and $missing_tools = 1);
    3232my $camtool = can_run('camtool') or (warn "Can't find camtool" and $missing_tools = 1);
     33my $fftool  = can_run('fftool') or (warn "Can't find fftool" and $missing_tools = 1);
     34
    3335my $ppConfigDump = can_run('ppConfigDump') or (warn "Can't find ppConfigDump" and $missing_tools = 1);
     36
    3437my $addstar = can_run('addstar') or (warn "Can't find addstar" and $missing_tools = 1);
     38my $loadgalphot = can_run('loadgalphot') or (warn "Can't find loadgalphot" and $missing_tools = 1);
     39
    3540if ($missing_tools) {
    3641    warn("Can't find required tools.");
     
    9297}
    9398
     99my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
     100
     101# Output products
     102$ipprc->outroot_prepare($outroot);
     103
     104
     105#
     106# Step 1: sort out reductions / recipes
     107#
     108
     109
    94110# Recipes to use based on reduction class
    95111$reduction = 'DEFAULT' unless defined $reduction;
     
    101117#}
    102118
    103 if ($stage =~/fullforce/) {
     119if ($stage eq 'fullforce') {
    104120    $reduction='DEFAULT';
    105121    #hardwired because why not
    106122}
    107 if ($stage =~/diff/) {
     123elsif ($stage eq 'fullforce_summary') {
     124    $reduction = 'DEFAULT';
     125}
     126elsif ($stage eq 'diff') {
    108127    $reduction='DEFAULT';
    109128    #hardwired because why not
     
    112131my $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR');
    113132# XXX This is now not used: do we still need it?
    114 if ($stage =~/cam/) {
     133if ($stage eq 'cam') {
    115134  $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use
    116135}
    117 if ($stage =~/stack/) {
     136elsif ($stage eq 'stack') {
    118137  $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR_STACK'); # Recipe to use
     138}
     139elsif ($stage eq 'skycal') {
     140    $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use                                                             
    119141}
    120142#if ($stage =~/staticsky/) {
    121143#  $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR_STATICSKY'); # Recipe to use
    122144#}
    123 if ($stage =~/skycal/) {
    124     $recipe_addstar = $ipprc->reduction($reduction, 'ADDSTAR'); # Recipe to use                                                             
    125 }
    126145
    127146
     
    129148&my_die("Unrecognised ADDSTAR recipe", $add_id, $PS_EXIT_CONFIG_ERROR) unless defined $recipe_addstar;
    130149
    131 my $mdcParser = PS::IPP::Metadata::Config->new; # Parser for metadata config files
    132 
    133 # Output products
    134 $ipprc->outroot_prepare($outroot);
    135150
    136151# the camera configurations should define the psastro output to be a single file (MEF), regardless of the inputs
     152
     153#
     154# Step 2: Determine where sources should come from, modify them as needed.
     155#
     156
    137157
    138158# it was PSASTRO.OUTPUT
     
    141161my $fpaObjectsAlt = $fpaObjects;
    142162
    143 if ($stage =~ /skycal/) {
     163if ($stage eq 'skycal') {
    144164    #should be ok for skycal?
    145165    print "using $fpaObjects for $stage\n";
    146166}
    147 if ($stage =~/diff/) {
     167elsif ($stage eq 'diff') {
    148168    print "using $fpaObjects for $stage\n";
    149169}
    150 if ($stage =~/fullforce/)  {
     170elsif ($stage eq 'fullforce')  {
    151171    print "using $fpaObjects for $stage\n";
    152172}
    153173
    154 
    155 
    156 if ($stage =~ /cam/) {
     174if ($stage eq 'cam') {
    157175    # if it is cam stage we need to be careful when grabbing the filename.
    158176    # This breaks down into a few steps:
     
    197215}
    198216
    199 if (($stage =~/stack/ || $stage =~ /skycal/ || $stage =~/diff/ || $stage=~/fullforce/)) {
     217if (($stage eq 'stack') ||
     218    ($stage eq 'skycal') ||
     219    ($stage eq 'diff') ||
     220    ($stage eq 'fullforce')||
     221    ($stage eq 'fullforce_summary')) {
    200222    $fpaObjects =~ s/smf$/cmf/;
    201223    $fpaObjectsAlt =~ s/smf$/cmf/;
    202 
    203224}
    204225
     
    206227my $fpaObjects2;
    207228my $checkalt = 0;
    208 if ($stage =~/staticsky/) {
     229if ($stage eq 'staticsky') {
    209230    $checkalt = 1;
    210231    my $sources   = $ipprc->filename("PSPHOT.OUT.CMF.MEF", $stageroot); #this is mostly rigtht except the .cmf needs either
     
    239260        $fpaObjects = $fpaObjects1;
    240261    }
    241      
    242 
    243 }
     262}
     263
     264
     265#
     266# Step 3 Set up addtool/loadgalphot commands.
     267#
    244268
    245269my $traceDest  = $ipprc->filename("TRACE.EXP",          $outroot) or &my_die("Missing entry from camera config", $add_id, $PS_EXIT_CONFIG_ERROR);
     
    251275    $dvodbReal = $ipprc->convert_filename_absolute( $dvodbReal ) or &my_die("can't get path for dvodb", $add_id, $PS_EXIT_CONFIG_ERROR);
    252276}
     277
    253278my $dtime_addstar = 0;
    254279if (defined $dvodbReal) {
    255280    if ($minidvodb) {
    256         my $command = "addtool -listminidvodbrun ";
     281        my $command = "$addtool -listminidvodbrun ";
    257282        $command .= " -minidvodb_group $minidvodb_group" if defined $minidvodb_group;
    258283        $command .= " -minidvodb_name $minidvodb_name" if defined $minidvodb_name;
     
    285310    unless ($no_op) {
    286311            print $dvodbReal;
    287            
    288312            ## addstar can either save the full set of detections, or just
    289313            ## the image metadata, in the dvodb.  this is set in the
     
    311335            }           
    312336
    313             # require a defined output dvo database to run addstar (ie, refuse to use the .ptolemyrc default)
    314             my $command  = "$addstar -update"; # XXX optionally set -update?
    315             $command .= " -D CAMERA $camdir";
    316             $command .= " -D CATDIR $minidvodb_path";
    317             $command .= " -D ZERO_POINT_OPTION $zeroPointOption";
    318             $command .= " $realFile";
    319             $command .= " -use-name $fpaObjects"; # DVO wants the neb-name as a file reference
    320             $command .= " -image" if $image_only;
    321             if ($stage =~ /staticsky/) {
    322                 $command .= " -accept-astrom -quick-airmass";
    323             }  #careful here - this matches staticsky and staticsky_multi
    324             if ($stage =~ /skycal/) {
    325                 $command .= " -quick-airmass";
    326             }  #careful here - this matches staticsky and staticsky_multi
    327             if ($stage =~ /diff/) {
    328                 $command .= " -accept-astrom";
     337            my $command;
     338
     339            if ($stage ne 'fullforce_summary') {
     340                # require a defined output dvo database to run addstar (ie, refuse to use the .ptolemyrc default)
     341                $command  = "$addstar -update"; # XXX optionally set -update?
     342                $command .= " -D CAMERA $camdir";
     343                $command .= " -D CATDIR $minidvodb_path";
     344                $command .= " -D ZERO_POINT_OPTION $zeroPointOption";
     345                $command .= " $realFile";
     346                $command .= " -use-name $fpaObjects"; # DVO wants the neb-name as a file reference
     347                $command .= " -image" if $image_only;
     348                if ($stage =~ /staticsky/) {
     349                    $command .= " -accept-astrom -quick-airmass";
     350                }  #careful here - this matches staticsky and staticsky_multi
     351                if ($stage =~ /skycal/) {
     352                    $command .= " -quick-airmass";
     353                }  #careful here - this matches staticsky and staticsky_multi
     354                if ($stage =~ /diff/) {
     355                    $command .= " -accept-astrom";
     356                }
     357                if ($stage =~ /fullforce/) {
     358                    $command .= " -accept-astrom -xrad";
     359                }
    329360            }
    330             if ($stage =~ /fullforce/) {
    331                 $command .= " -accept-astrom -xrad";
     361            else { # Full force summary case
     362                # We need to know the filter to set up the photcode, as the summaries do not include that in the header.
     363                # We /could/ set up the addtool stuff to pass that in to the script when needed, but that would require adding
     364                # a lot of extra hooks and option handlers.  Therefore, call out to fftool, and get the filter that way, and
     365                # construct the correct photcode.
     366
     367                my $photcode = '';
     368                {
     369                    my $ff_command = "$fftool -dbname ${dbname} -summary -ff_id ${stage_id}";
     370                    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     371                        run(command => $command, verbose => $verbose);
     372                    unless ($success) {
     373                        $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     374                        &my_die("Unable to fetch filter via fftool: $error_code", $add_id, $error_code);
     375                    }
     376                    my $MDlist = $mdcParser->parse(join "", @$stdout_buf) or
     377                        &my_die("Unable to determine ff summary information.", $add_id, $error_code);
     378                    my $metadata = parse_md_list($MDlist);
     379                    my $ffSummary = $metadata->[0];
     380                    my $filter = $ffSummary->{filter};
     381                   
     382                    $filter =~ s/\.00000//;
     383                    $photcode = "GPC1.${filter}.ForcedWarp";
     384                }
     385
     386                $command =  "$loadgalphot -v ";
     387                $command .= " -D CAMERA $camdir ";
     388                $command .= " -D CATDIR $minidvodb_path ";
     389                $command .= " $realFile ";
     390                $command .= " -p $photcode ";
    332391            }
    333 
     392                   
    334393            my $mjd_addstar_start = DateTime->now->mjd;   # MJD of starting script
    335 
     394           
    336395            my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    337396                run(command => $command, verbose => $verbose);
  • trunk/ippTasks/addstar.pro

    r38582 r38937  
    244244$addstar_revert_DB_SC = 0
    245245$addstar_revert_DB_FF = 0
     246$addstar_revert_DB_FFS = 0
    246247$addstar_revert_DB_DF = 0
     248
    247249
    248250#$LOADEXEC_ADD = 300
     
    452454        sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
    453455    end
     456    if ("$STAGE" == "fullforce_summary")
     457        sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
     458    end
    454459
    455460 
     
    468473      $run = $run --stage_id $STAGE_ID
    469474    end
    470    if ("$STAGE" == "skycal")
     475    if ("$STAGE" == "skycal")
    471476      $run = $run --stage_id $STAGE_ID
    472477    end
    473    if ("$STAGE" == "diff")
     478    if ("$STAGE" == "diff")
    474479      $run = $run --stage_id $STAGE_ID --stage_extra1 $STAGE_EXTRA1
    475480    end
    476    if ("$STAGE" == "fullforce")
     481    if ("$STAGE" == "fullforce")
    477482      $run = $run --stage_id $STAGE_ID --stage_extra1 $STAGE_EXTRA1
     483    end
     484    if ("$STAGE" == "fullforce_summary")
     485    # This shouldn't need a stage_extra1, as there are no subcomponents to the ff summary.
     486      $run = $run --stage_id
    478487    end
    479488
     
    596605        sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
    597606    end
     607    if ("$STAGE" == "fullforce_summary")
     608        sprintf outroot "%s.add.%s" $STAGEROOT $ADD_ID
     609    end
    598610
    599611 
     
    949961  end
    950962end
     963
     964task addstar.revert.fullforce_summary
     965  host         local
     966
     967  periods      -poll 5.0
     968  periods      -exec 60.0
     969  periods      -timeout 1200
     970  npending     1
     971  active        false
     972
     973  stdout NULL
     974  stderr $LOGDIR/revert.log
     975
     976  task.exec
     977    if ($LABEL:n == 0) break
     978    $run = addtool -revertprocessedexp -stage fullforce_summary
     979    if ($DB:n == 0)
     980      option DEFAULT
     981    else
     982      # save the DB name for the exit tasks                                                                   
     983      option $DB:$addstar_revert_DB_FFS
     984      $run = $run -dbname $DB:$addstar_revert_DB_FFS
     985      $addstar_revert_DB_FFS ++
     986      if ($addstar_revert_DB_FFS >= $DB:n) set addstar_revert_DB_FFS = 0
     987    end
     988     add_poll_labels run
     989    command $run
     990  end
     991
     992  # success                                                                                                   
     993  task.exit    0
     994  end
     995
     996  # locked list                                                                                               
     997  task.exit    default
     998    showcommand failure
     999  end
     1000
     1001  task.exit    crash
     1002    showcommand crash
     1003  end
     1004
     1005  # operation times out?                                                                                     
     1006  task.exit    timeout
     1007    showcommand timeout
     1008  end
     1009end
  • trunk/ippTools/share/addtool_find_ffsummary_id.sql

    r38846 r38937  
    1 SELECT
    2      fullForceRun.*
    3 FROM
    4      fullForceRun
    5 JOIN fullForceSummary using (ff_id)
    6 
    7 LEFT JOIN (SELECT ff_id       AS added_ff_id,
    8                   addRun.dvodb AS previous_dvodb
    9            FROM addRun
    10 JOIN fullForceRun on (ff_id = stage_id and addRun.stage = 'fullforce_summary')
    11           ) as foo
    12      ON ff_id = added_ff_id
    13      --AND stage = 'skycal'
     1SELECT fullForceRun.* FROM fullForceRun
     2JOIN fullForceSummary USING (ff_id)
     3LEFT JOIN (
     4     SELECT ff_id AS added_ff_id,
     5            addRun.dvodb AS previous_dvodb
     6     FROM addRun
     7     JOIN fullForceRun ON (ff_id = stage_id and addRun.stage = 'fullforce_summary')
     8     ) as foo
     9ON ff_id = added_ff_id
     10     -- AND stage = 'skycal'
    1411     -- hook for qualifying the join on the previous_dvodb
    1512     AND %s
  • trunk/ippTools/share/addtool_find_ffsummary_id_dvo.sql

    r38846 r38937  
    1 SELECT fullForceRun.* FROM fullForceRun JOIN fullForceSummary using (ff_id)
    2 JOIN skycalRun using (skycal_id) join stackRun using (stack_id)
    3 JOIN skycell using (skycell_id, tess_id)
     1SELECT fullForceRun.* FROM fullForceRun
     2JOIN fullForceSummary USING (ff_id)
     3JOIN skycalRun USING (skycal_id)
     4JOIN stackRun USING (stack_id)
     5JOIN skycell USING (skycell_id, tess_id)
    46
    5 WHERE fullForceRun.state = 'full' and fullForceSummary.quality = 0 and fullForceSummary.fault = 0
    6     AND fullForceRun.ff_id NOT IN (SELECT fullForceRun.ff_id
    7        FROM addRun
    8        JOIN fullForceRun on (fullForceRun.ff_id = addRun.stage_id and addRun.stage = 'fullforce_summary') JOIN fullForceSummary on (fullForceRun.ff_id = fullForceSummary.ff_id)
     7WHERE fullForceRun.state = 'full'
     8AND fullForceSummary.quality = 0
     9AND fullForceSummary.fault = 0
     10AND fullForceRun.ff_id NOT IN (
     11    SELECT fullForceRun.ff_id FROM addRun
     12    JOIN fullForceRun ON (fullForceRun.ff_id = addRun.stage_id and addRun.stage = 'fullforce_summary')
     13    JOIN fullForceSummary ON (fullForceRun.ff_id = fullForceSummary.ff_id)
    914       WHERE addRun.stage = 'fullforce_summary' AND %s
    10       )
     15    )
  • trunk/ippTools/src/addtool.c

    r38848 r38937  
    124124    PXOPT_COPY_S64(config->args, where,  "-diff_id",  "diffRun.diff_id", "==");
    125125    PXOPT_COPY_S64(config->args, where,  "-ff_id",  "fullForceRun.ff_id", "==");
    126 
    127     PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
    128     if (strcmp(stage, "cam")== 0) {
    129       pxcamGetSearchArgs (config, where);
    130       pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "=="); // define using camRun label
    131       pxAddLabelSearchArgs (config, where, "-data_group","camRun.data_group", "=="); // define using camRun label
    132       PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
    133     }
    134     if (strcmp(stage, "stack")== 0) {
    135       pxAddLabelSearchArgs (config, where, "-label",     "stackRun.label", "=="); // define using camRun label
    136       pxAddLabelSearchArgs (config, where, "-data_group","stackRun.data_group", "=="); // define using camRun label
    137       PXOPT_COPY_STR(config->args, where,  "-reduction", "stackRun.reduction", "==");
    138       pxAddLabelSearchArgs (config, where, "-filter",     "stackRun.filter", "=="); // define using camRun label
    139     }
    140     if (strcmp(stage, "staticsky")== 0) {
    141       pxAddLabelSearchArgs (config, where, "-label",     "staticskyRun.label", "=="); // define using camRun label
    142       pxAddLabelSearchArgs (config, where, "-data_group","staticskyRun.data_group", "=="); // define using camRun label
    143       PXOPT_COPY_STR(config->args, where,  "-reduction", "staticskyRun.reduction", "==");
    144     //no filter here
    145     }
    146     if (strcmp(stage, "skycal")==0) {
    147       pxAddLabelSearchArgs (config, where, "-label",     "skycalRun.label", "=="); //define using skycalRun label
    148       pxAddLabelSearchArgs (config, where, "-data_group","skycalRun.data_group", "==");
    149       PXOPT_COPY_STR(config->args, where,  "-reduction", "skycalRun.reduction",  "==");
    150     }
    151     if (strcmp(stage, "diff")==0) {
    152       pxAddLabelSearchArgs (config, where, "-label",     "diffRun.label", "=="); //define using skycalRun label
    153       pxAddLabelSearchArgs (config, where, "-data_group","diffRun.data_group", "==");
    154       PXOPT_COPY_STR(config->args, where,  "-reduction", "diffRun.reduction",  "==");
    155     }
    156     if (strcmp(stage, "fullforce")==0) {
    157       pxAddLabelSearchArgs (config, where, "-label",     "fullForceRun.label", "=="); //define using skycalRun label
    158       pxAddLabelSearchArgs (config, where, "-data_group","fullForceRun.data_group", "==");
    159       PXOPT_COPY_STR(config->args, where,  "-reduction", "fullForceRun.reduction",  "==");
    160     }
    161     if (strcmp(stage, "fullforce_summary")==0) {
    162       //should be nearly identical to fullforce (uses the same tables)
    163       pxAddLabelSearchArgs (config, where, "-label",     "fullForceRun.label", "==");
    164       pxAddLabelSearchArgs (config, where, "-data_group","fullForceRun.data_group", "==");
    165       PXOPT_COPY_STR(config->args, where,  "-reduction", "fullForceRun.reduction",  "==");
    166     }
    167 
    168  
    169  
    170     if (!psListLength(where->list)) {
    171         psFree(where);
    172         psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    173         return false;
    174     }
    175    
     126       
    176127    // PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
    177128    PXOPT_LOOKUP_STR(workdir,     config->args, "-set_workdir", false, false);
     
    194145    PXOPT_LOOKUP_F32(mindec,     config->args, "-set_mindec", false, false);
    195146    PXOPT_LOOKUP_F32(maxdec,     config->args, "-set_maxdec", false, false);
     147
     148    // Handle stages
     149    PXOPT_LOOKUP_STR(stage,       config->args, "-stage", false, false);
     150    if (strcmp(stage, "cam") == 0) {
     151      pxcamGetSearchArgs (config, where);
     152      pxAddLabelSearchArgs (config, where, "-label",     "camRun.label", "=="); // define using camRun label
     153      pxAddLabelSearchArgs (config, where, "-data_group","camRun.data_group", "=="); // define using camRun label
     154      PXOPT_COPY_STR(config->args, where,  "-reduction", "camRun.reduction", "==");
     155    }
     156    else if (strcmp(stage, "stack") == 0) {
     157      pxAddLabelSearchArgs (config, where, "-label",     "stackRun.label", "=="); // define using camRun label
     158      pxAddLabelSearchArgs (config, where, "-data_group","stackRun.data_group", "=="); // define using camRun label
     159      PXOPT_COPY_STR(config->args, where,  "-reduction", "stackRun.reduction", "==");
     160      pxAddLabelSearchArgs (config, where, "-filter",     "stackRun.filter", "=="); // define using camRun label
     161    }
     162    else if (strcmp(stage, "staticsky") == 0) {
     163      pxAddLabelSearchArgs (config, where, "-label",     "staticskyRun.label", "=="); // define using camRun label
     164      pxAddLabelSearchArgs (config, where, "-data_group","staticskyRun.data_group", "=="); // define using camRun label
     165      PXOPT_COPY_STR(config->args, where,  "-reduction", "staticskyRun.reduction", "==");
     166      //no filter here
     167    }
     168    else if (strcmp(stage, "skycal") == 0) {
     169      pxAddLabelSearchArgs (config, where, "-label",     "skycalRun.label", "=="); //define using skycalRun label
     170      pxAddLabelSearchArgs (config, where, "-data_group","skycalRun.data_group", "==");
     171      PXOPT_COPY_STR(config->args, where,  "-reduction", "skycalRun.reduction",  "==");
     172    }
     173    else if (strcmp(stage, "diff") == 0) {
     174      pxAddLabelSearchArgs (config, where, "-label",     "diffRun.label", "=="); //define using diffRun label
     175      pxAddLabelSearchArgs (config, where, "-data_group","diffRun.data_group", "==");
     176      PXOPT_COPY_STR(config->args, where,  "-reduction", "diffRun.reduction",  "==");
     177    }
     178    else if (strcmp(stage, "fullforce") == 0) {
     179      pxAddLabelSearchArgs (config, where, "-label",     "fullForceRun.label", "=="); //define using fullForceRun label
     180      pxAddLabelSearchArgs (config, where, "-data_group","fullForceRun.data_group", "==");
     181      PXOPT_COPY_STR(config->args, where,  "-reduction", "fullForceRun.reduction",  "==");
     182    }
     183    else if (strcmp(stage, "fullforce_summary")==0) {
     184      //should be nearly identical to fullforce (uses the same tables)
     185      pxAddLabelSearchArgs (config, where, "-label",     "fullForceRun.label", "==");
     186      pxAddLabelSearchArgs (config, where, "-data_group","fullForceRun.data_group", "==");
     187      PXOPT_COPY_STR(config->args, where,  "-reduction", "fullForceRun.reduction",  "==");
     188    }
     189 
     190    if (!psListLength(where->list)) {
     191        psFree(where);
     192        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     193        return false;
     194    }
    196195   
     196    // Select either destreaked or uncensored data.  CZW: Should this be defaulted to uncensored?
     197    if (!(destreaked && uncensored) && (destreaked || uncensored)) {
     198      //if one of destreaked and uncensored is true, but not both
     199      if (destreaked) {
     200        psTrace("addtool.c", PS_LOG_INFO, "using -destreaked\n");
     201      } else {
     202        psTrace("addtool.c", PS_LOG_INFO, "using -unscensored\n");
     203      }
     204    } else {
     205      if (destreaked && uncensored) {
     206        psError(PS_ERR_UNKNOWN, false, "Both -destreaked and -uncensored are selected. Please use one or the other, not both, \n");
     207        return false;
     208      }
     209      if (!destreaked && !uncensored) {
     210        psError(PS_ERR_UNKNOWN, false, "Neither -destreaked or -uncensored are selected. Please use one.\n");
     211        return false;
     212      }
     213    }
     214    if ((strcmp(stage,"diff") == 0) ||
     215        (strcmp(stage,"fullforce") == 0) ||
     216        (strcmp(stage,"fullforce_summary") == 0)) {
     217      if (! (dvodb)) {
     218        psError(PS_ERR_UNKNOWN, false, "The SQL is not correctly written to handle this case (-set_dvodb not specified), so stopping here.");
     219        return(false);
     220      }
     221    }
     222   
     223   
    197224    // prevent queueing an addRun if a given exposure has already been added to
    198225    // the given dvo database
    199226    psString dvodb_string = NULL;
    200227    psString bare_query = NULL;
    201 
    202     if (!(destreaked && uncensored) && (destreaked || uncensored)) {
    203       //if one of destreaked and uncensored is true, but not both
    204       if (destreaked) {
    205       psTrace("addtool.c", PS_LOG_INFO, "using -destreaked\n");
    206       } else {
    207       psTrace("addtool.c", PS_LOG_INFO, "using -unscensored\n");
    208       }
    209     } else {
    210       if (destreaked && uncensored) {
    211         psError(PS_ERR_UNKNOWN, false, "Both -destreaked and -uncensored are selected. Please use one or the other, not both, \n");
    212         return false;
    213       }
    214       if (!destreaked && !uncensored) {
    215         psError(PS_ERR_UNKNOWN, false, "Neither -destreaked or -uncensored are selected. Please use one.\n");
    216          return false;
    217       }
    218     }
    219 
    220    
    221 
    222 
     228   
    223229    if (strcmp(stage,"cam") == 0 ) {
    224     if (dvodb) {
    225       psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_cam_id_dvo.sql\n%s\n", dvodb,stage);
     230      if (dvodb) {
     231        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_cam_id_dvo.sql\n%s\n", dvodb,stage);
    226232        // find the cam_id of all the exposures that we want to queue up.
    227233        bare_query = pxDataGet("addtool_find_cam_id_dvo.sql");
    228234        // user supplied dvodb
    229235        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    230     } else {
    231       psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_cam_id.sql\n%s\n",stage);
     236      } else {
     237        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_cam_id.sql\n%s\n",stage);
    232238        // find the cam_id of all the exposures that we want to queue up.
    233239        bare_query = pxDataGet("addtool_find_cam_id.sql");
    234240        // inherit dvodb from camRun, avoid matching NULL
    235241        psStringAppend(&dvodb_string, "(camRun.dvodb IS NOT NULL AND previous_dvodb = camRun.dvodb)");
    236     }
    237     }
    238     if (strcmp(stage,"stack") == 0) {
    239     if (dvodb ) {
    240       psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_stack_id_dvo.sql\n%s\n", dvodb,stage);
     242      }
     243    }
     244
     245    else if (strcmp(stage,"stack") == 0) {
     246      if (dvodb) {
     247        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_stack_id_dvo.sql\n%s\n", dvodb,stage);
    241248        // find the cam_id of all the exposures that we want to queue up.
    242249        bare_query = pxDataGet("addtool_find_stack_id_dvo.sql");
    243250        // user supplied dvodb
    244251        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    245     } else {
    246       psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_stack_id.sql\n%s\n",stage);
     252      } else {
     253        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_stack_id.sql\n%s\n",stage);
    247254        // find the cam_id of all the exposures that we want to queue up.
    248255        bare_query = pxDataGet("addtool_find_stack_id.sql");
    249256        // inherit dvodb from camRun, avoid matching NULL
    250257        psStringAppend(&dvodb_string, "(stackRun.dvodb IS NOT NULL AND previous_dvodb = stackRun.dvodb)");
    251     }
    252     }
    253  
    254     if (strcmp(stage,"staticsky") == 0) {
     258      }
     259    }
     260
     261    else if (strcmp(stage,"staticsky") == 0) {
    255262      if (dvodb ) {
    256263        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_sky_id_multi_dvo.sql\n%s\n", dvodb,stage);
     
    267274      }
    268275    }
    269    
    270     if (strcmp(stage,"skycal") == 0) {
    271       if (dvodb ) {
     276
     277    else if (strcmp(stage,"skycal") == 0) {
     278      if (dvodb) {
    272279        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_skycal_id_dvo.sql\n%s\n", dvodb,stage);
    273         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     280        // find the skycal_id of all the exposures that we want to queue up.
    274281        bare_query = pxDataGet("addtool_find_skycal_id_dvo.sql");
    275         // user supplied dvodb                                                                                                                                             
     282        // user supplied dvodb
    276283        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    277284      } else {
    278285        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_skycal_id.sql\n%s\n",stage);
    279         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     286        // find the skycal_id of all the exposures that we want to queue up.
    280287        bare_query = pxDataGet("addtool_find_skycal_id.sql");
    281         // inherit dvodb from skycalRun, avoid matching NULL                                                                                                                   
     288        // inherit dvodb from skycalRun, avoid matching NULL
    282289        psStringAppend(&dvodb_string, "(skycalRun.dvodb IS NOT NULL AND previous_dvodb = skycalRun.dvodb)");
    283290        // this is silly, there is no dvodb in skycalRun...?
     
    285292    }
    286293
    287     if (strcmp(stage,"diff") == 0) {
    288       if (dvodb ) {
     294    else if (strcmp(stage,"diff") == 0) {
     295      if (dvodb) {
    289296        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_diff_id_dvo.sql\n%s\n", dvodb,stage);
    290         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     297        // find the skycal_id of all the exposures that we want to queue up.
    291298        bare_query = pxDataGet("addtool_find_diff_id_dvo.sql");
    292         // user supplied dvodb                                                                                                                                             
     299        // user supplied dvodb
    293300        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    294301      } else {
    295302        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_find_diff_id.sql\n%s\n",stage);
    296         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     303        // find the skycal_id of all the exposures that we want to queue up.
    297304        bare_query = pxDataGet("addtool_find_diff_id.sql");
    298         // inherit dvodb from skycalRun, avoid matching NULL                                                                                                                   
     305        // inherit dvodb from skycalRun, avoid matching NULL
    299306        psStringAppend(&dvodb_string, "(diffRun.dvodb IS NOT NULL AND previous_dvodb = diffRun.dvodb)");
    300307        // this is silly, there is no dvodb in skycalRun...?
    301308      }
    302      
    303 
    304      }
    305 
    306     if (strcmp(stage,"fullforce") == 0) {
    307       if (dvodb ) {
     309    }
     310
     311    else if (strcmp(stage,"fullforce") == 0) {
     312      if (dvodb) {
    308313        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_ff_id_dvo.sql\n%s\n", dvodb,stage);
    309         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     314        // find the skycal_id of all the exposures that we want to queue up.
    310315        bare_query = pxDataGet("addtool_find_ff_id_dvo.sql");
    311         // user supplied dvodb                                                                                                                                             
     316        // user supplied dvodb
    312317        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    313318      } else {
    314319        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_ff_id.sql\n%s\n",stage);
    315         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     320        // find the skycal_id of all the exposures that we want to queue up.
    316321        bare_query = pxDataGet("addtool_find_ff_id.sql");
    317         // inherit dvodb from skycalRun, avoid matching NULL                                                                                                                   
     322        // inherit dvodb from skycalRun, avoid matching NULL
    318323        psStringAppend(&dvodb_string, "(fullForceRun.dvodb IS NOT NULL AND previous_dvodb = fullForceRun.dvodb)");
    319324        // this is silly, there is no dvodb in skycalRun...?
    320325      }
    321326    }
    322 
    323         if (strcmp(stage,"fullforce_summary") == 0) {
    324       if (dvodb ) {
     327    else if (strcmp(stage,"fullforce_summary") == 0) {
     328      if (dvodb) {
    325329        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument found (%s) using addtool_find_ffsummary_id_dvo.sql\n%s\n", dvodb,stage);
    326         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     330        // find the skycal_id of all the exposures that we want to queue up.
    327331        bare_query = pxDataGet("addtool_find_ffsummary_id_dvo.sql");
    328         // user supplied dvodb                                                                                                                                             
     332        // user supplied dvodb
    329333        psStringAppend(&dvodb_string, "addRun.dvodb = '%s'", dvodb);
    330334      } else {
    331335        psTrace("addtool.c", PS_LOG_INFO, "dvodb argument not found using addtool_ffsummary_id.sql\n%s\n",stage);
    332         // find the skycal_id of all the exposures that we want to queue up.                                                                                                   
     336        // find the skycal_id of all the exposures that we want to queue up.
    333337        bare_query = pxDataGet("addtool_find_ffsummary_id.sql");
    334         // inherit dvodb from skycalRun, avoid matching NULL                                                                                                                   
     338        // inherit dvodb from skycalRun, avoid matching NULL
    335339        psStringAppend(&dvodb_string, "(fullForceRun.dvodb IS NOT NULL AND previous_dvodb = fullForceRun.dvodb)");
    336340        // this is silly, there is no dvodb in skycalRun...?
    337341      }
    338342    }
    339 
    340 
    341 
    342 
    343 
     343   
    344344    if (!bare_query) {
    345345        psError(PXTOOLS_ERR_SYS, false, "failed to retrieve SQL statement");
     
    347347        return false;
    348348    }
     349   
    349350    // Take the bare query and add the dvodb selector
    350351    psString query = NULL;
     
    357358        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    358359        psStringAppend(&query, " AND %s", whereClause);
    359         if (strcmp(stage,"diff") == 0) {
     360
     361        if ((strcmp(stage,"diff") == 0)||
     362            (strcmp(stage,"fullforce") == 0)||
     363            (strcmp(stage,"fullforce_summary") == 0)) {
    360364          //diff needs the ra /deg stuff
     365          //ff needs the ra /deg stuff
     366          //ff summary needs the ra /deg stuff
    361367          psStringAppend(&query, " AND radeg >= %f", minra);
    362368          psStringAppend(&query, " AND radeg <= %f", maxra);
    363369          psStringAppend(&query, " AND decdeg >= %f", mindec);
    364370          psStringAppend(&query, " AND decdeg <= %f", maxdec);
    365 
    366         }
    367         if (strcmp(stage,"fullforce") == 0) {
    368           //ff needs the ra /deg stuff
    369           psStringAppend(&query, " AND radeg >= %f", minra);
    370           psStringAppend(&query, " AND radeg <= %f", maxra);
    371           psStringAppend(&query, " AND decdeg >= %f", mindec);
    372           psStringAppend(&query, " AND decdeg <= %f", maxdec);
    373 
    374         }
    375         if (strcmp(stage,"fullforce_summary") == 0) {
    376           //ff needs the ra /deg stuff
    377           psStringAppend(&query, " AND radeg >= %f", minra);
    378           psStringAppend(&query, " AND radeg <= %f", maxra);
    379           psStringAppend(&query, " AND decdeg >= %f", mindec);
    380           psStringAppend(&query, " AND decdeg <= %f", maxdec);
    381 
    382371        }
    383372
     
    391380    if (destreaked) {
    392381      //This picks the magicked/censored ones
    393       if (strcmp(stage,"cam") == 0) {
     382      if ((strcmp(stage,"cam") == 0)||
     383          (strcmp(stage,"stack") == 0)) {
    394384        psStringAppend(&query, " AND (camRun.magicked > 0)");
    395       }
    396       if (strcmp(stage,"stack") == 0) {
    397         psStringAppend(&query, " AND (stackRun.magicked > 0)");
    398385      }
    399386      // staticSky/skycal have no magicked column.
     
    407394        psStringAppend(&query, " AND (stackRun.magicked = 0)");
    408395      }
    409 
    410     }
    411 
    412     //if we grab a group of camRun/stackRuns that have multiple camRun/stackRun pointing to the same exp_id/stack_id, and that exp_id/stack_id has never before been added to addRun, we need to group by exp_id/stack_id to ensure that only 1 of the camRun/stackRun for that exp_id is added to the addRun stage.
    413 
     396    }
     397
     398    // if we grab a group of camRun/stackRuns that have multiple camRun/stackRun pointing to the same exp_id/stack_id, and that exp_id/stack_id has never before been added to addRun, we need to group by exp_id/stack_id to ensure that only 1 of the camRun/stackRun for that exp_id is added to the addRun stage.
     399   
    414400    if (strcmp(stage,"cam") == 0) {
    415         psStringAppend(&query, " GROUP BY exp_id");
    416    }
    417       if (strcmp(stage,"stack") == 0) {
    418         psStringAppend(&query, " GROUP BY stack_id");
    419       }
    420       //   if (strcmp(stage,"staticsky") == 0) {
    421       //   psStringAppend(&query, " GROUP BY stack_id");
    422       // }
    423       if (strcmp(stage,"staticsky") == 0) {
    424         psStringAppend(&query, " GROUP BY sky_id, stack_id");  //some reason it needs this
    425       }
    426       if (strcmp(stage,"skycal") == 0) {
    427         psStringAppend(&query, " GROUP BY skycal_id, sky_id, stack_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
    428       }
    429       //needs to be checked HAF xxx
    430       if (strcmp(stage,"diff") == 0) {
    431 
    432         psStringAppend(&query, " GROUP BY diff_id, diff_skyfile_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
    433       }
    434       //needs to be checked HAF xxx
    435       if (strcmp(stage,"fullforce") == 0) {
    436         psStringAppend(&query, " GROUP BY ff_id, warp_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
    437       }
    438       if (strcmp(stage,"fullforce_summary") == 0) {
    439         psStringAppend(&query, " GROUP BY ff_id ");  //needs to be checked, but should be fine: want 1 ff summary cmf.
    440       }
    441 
    442       psError(PS_ERR_UNKNOWN, false , query);
    443 
     401      psStringAppend(&query, " GROUP BY exp_id");
     402    }
     403    else if (strcmp(stage,"stack") == 0) {
     404      psStringAppend(&query, " GROUP BY stack_id");
     405    }
     406    else if (strcmp(stage,"staticsky") == 0) {
     407      psStringAppend(&query, " GROUP BY sky_id, stack_id");  //some reason it needs this
     408    }
     409    else if (strcmp(stage,"skycal") == 0) {
     410      psStringAppend(&query, " GROUP BY skycal_id, sky_id, stack_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
     411    }
     412    //needs to be checked HAF xxx
     413    else if (strcmp(stage,"diff") == 0) {
     414      psStringAppend(&query, " GROUP BY diff_id, diff_skyfile_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
     415    }
     416    //needs to be checked HAF xxx
     417    else if (strcmp(stage,"fullforce") == 0) {
     418      psStringAppend(&query, " GROUP BY ff_id, warp_id ");  //this needs checking, but I think it shoul be fine? it groups by lots of stuff (including stack - we only want one of each stack in there
     419    }
     420    else if (strcmp(stage,"fullforce_summary") == 0) {
     421      psStringAppend(&query, " GROUP BY ff_id ");  //needs to be checked, but should be fine: want 1 ff summary cmf.
     422    }
     423    psError(PS_ERR_UNKNOWN, false , query);
    444424
    445425    psTrace("addtool.c", PS_LOG_INFO,"query: \n\n%s\n\n",query);
     
    447427    if (!p_psDBRunQuery(config->dbh, query)) {
    448428      psError(PS_ERR_UNKNOWN, false, "database error, \n%s\n", query);
    449         psFree(query);
    450         return false;
     429      psFree(query);
     430      return false;
    451431    }
    452432    psFree(query);
    453 
     433   
    454434    psArray *output = p_psDBFetchResult(config->dbh);
    455435    if (!output) {
    456         psError(PS_ERR_UNKNOWN, false, "database error");
    457         return false;
     436      psError(PS_ERR_UNKNOWN, false, "database error");
     437      return false;
    458438    }
    459439    if (!psArrayLength(output)) {
    460         psTrace("addtool", PS_LOG_INFO, "no rows found");
    461         psFree(output);
    462         return true;
    463     }
    464 
     440      psTrace("addtool", PS_LOG_INFO, "no rows found");
     441      psFree(output);
     442      return true;
     443    }
     444   
    465445    if (pretend) {
    466         // negative simple so the default is true
    467         if (!ippdbPrintMetadatas(stdout, output, "addRun", !simple)) {
    468             psError(PS_ERR_UNKNOWN, false, "failed to print array");
    469             psFree(output);
    470             return false;
    471         }
    472         psFree(output);
    473         return true;
    474     }
    475 
     446      // negative simple so the default is true
     447      if (!ippdbPrintMetadatas(stdout, output, "addRun", !simple)) {
     448        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     449        psFree(output);
     450        return false;
     451      }
     452      psFree(output);
     453      return true;
     454    }
     455   
    476456    // loop over our list of camRun rows to check the supplied and selected dvodb and workdir values:
    477457    if (strcmp(stage,"cam") == 0) {
    478     for (long i = 0; i < psArrayLength(output); i++) {
     458      for (long i = 0; i < psArrayLength(output); i++) {
    479459        psMetadata *md = output->data[i];
    480 
     460       
    481461        camRunRow *row = camRunObjectFromMetadata(md);
    482462       
    483463        if (!row) {
    484             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    485             psFree(output);
    486             return false;
    487         }
    488 
     464          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     465          psFree(output);
     466          return false;
     467        }
     468       
    489469        if (!dvodb && !row->dvodb) {
    490             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, cam_id %" PRId64, row->label, row->cam_id);
    491             psFree(output);
    492             return false;
     470          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, cam_id %" PRId64, row->label, row->cam_id);
     471          psFree(output);
     472          return false;
    493473        }
    494474        if (!workdir && !row->workdir) {
    495             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, cam_id %" PRId64, row->label, row->cam_id);
    496             psFree(output);
    497             return false;
    498         }
    499 
     475          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, cam_id %" PRId64, row->label, row->cam_id);
     476          psFree(output);
     477          return false;
     478        }
     479       
    500480        psFree(row);
    501     }
    502     }
    503     if (strcmp(stage,"stack") == 0) {
    504     for (long i = 0; i < psArrayLength(output); i++) {
     481      }
     482    }
     483    else if (strcmp(stage,"stack") == 0) {
     484      for (long i = 0; i < psArrayLength(output); i++) {
    505485        psMetadata *md = output->data[i];
    506 
     486       
    507487        stackRunRow *row = stackRunObjectFromMetadata(md);
    508488       
    509489        if (!row) {
    510             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    511             psFree(output);
    512             return false;
    513         }
    514 
     490          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     491          psFree(output);
     492          return false;
     493        }
     494       
    515495        if (!dvodb && !row->dvodb) {
    516             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, stack_id %" PRId64, row->label, row->stack_id);
    517             psFree(output);
    518             return false;
     496          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, stack_id %" PRId64, row->label, row->stack_id);
     497          psFree(output);
     498          return false;
    519499        }
    520500        if (!workdir && !row->workdir) {
    521             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, stack_id %" PRId64, row->label, row->stack_id);
    522             psFree(output);
    523             return false;
    524         }
    525 
     501          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, stack_id %" PRId64, row->label, row->stack_id);
     502          psFree(output);
     503          return false;
     504        }
     505       
    526506        psFree(row);
    527     }
    528     }
    529    
    530 
    531 
    532     if (strcmp(stage,"staticsky") == 0) {
    533     for (long i = 0; i < psArrayLength(output); i++) {
     507      }
     508    }
     509    else if (strcmp(stage,"staticsky") == 0) {
     510      for (long i = 0; i < psArrayLength(output); i++) {
    534511        psMetadata *md = output->data[i];
    535512        bool status = false;
     
    543520          return false;
    544521        }
    545 
     522       
    546523        staticskyRunRow *row = staticskyRunObjectFromMetadata(md);
    547524       
    548525        if (!row) {
    549             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    550             psFree(output);
    551             return false;
    552         }
    553 
     526          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     527          psFree(output);
     528          return false;
     529        }
     530       
    554531        if (!dvodb) {  //there's no staticsky.dvodb
    555             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, sky_id %" PRId64, row->label, row->sky_id);
    556             psFree(output);
    557             return false;
     532          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, sky_id %" PRId64, row->label, row->sky_id);
     533          psFree(output);
     534          return false;
    558535        }
    559536        if (!workdir && !row->workdir) {
    560             psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, sky_id %" PRId64, row->label, row->sky_id);
    561             psFree(output);
    562             return false;
    563         }
    564 
     537          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined workdir: label: %s, sky_id %" PRId64, row->label, row->sky_id);
     538          psFree(output);
     539          return false;
     540        }
     541       
    565542        psFree(row);
    566     }
    567     }
    568 
    569     if (strcmp(stage,"skycal") == 0) {
     543      }
     544    }
     545    else if (strcmp(stage,"skycal") == 0) {
    570546      for (long i = 0; i < psArrayLength(output); i++) {
    571547        psMetadata *md = output->data[i];
    572 
     548       
    573549        skycalRunRow *row = skycalRunObjectFromMetadata(md);
    574550
     
    578554          return false;
    579555        }
    580 
     556       
    581557        if (!dvodb) {  // there's no skycalRun.dvodb
    582558          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, skycal_id %" PRId64, row->label, row->skycal_id);
     
    589565          return false;
    590566        }
    591 
     567       
    592568        psFree(row);
    593569      }
    594570    }
    595 
    596     if (strcmp(stage,"diff") == 0) {
    597      
     571    else if (strcmp(stage,"diff") == 0) {
    598572      for (long i = 0; i < psArrayLength(output); i++) {
    599573        psMetadata *md = output->data[i];
    600 
     574       
    601575        diffRunRow *row = diffRunObjectFromMetadata(md);
    602576       
     
    606580          return false;
    607581        }
    608 
     582       
    609583        if (!dvodb) {  // there's no skycalRun.dvodb
    610584          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, diff_id %" PRId64, row->label, row->diff_id);
     
    617591          return false;
    618592        }
    619 
     593       
    620594        psFree(row);
    621595      }
    622596    }
    623 
    624     if (strcmp(stage,"fullforce") == 0) {
     597    else if (strcmp(stage,"fullforce") == 0) {
    625598      for (long i = 0; i < psArrayLength(output); i++) {
    626599        psMetadata *md = output->data[i];
    627 
     600       
    628601        fullForceRunRow *row = fullForceRunObjectFromMetadata(md);
    629 
     602       
    630603        if (!row) {
    631604          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into fullforceRun");
     
    644617          return false;
    645618        }
    646 
     619       
    647620        psFree(row);
    648621      }
    649622    }
    650 
    651     if (strcmp(stage,"fullforce_summary") == 0) {
     623    else if (strcmp(stage,"fullforce_summary") == 0) {
    652624      for (long i = 0; i < psArrayLength(output); i++) {
    653625        psMetadata *md = output->data[i];
    654626        //i believe this is correct for ff_summary as well:
    655627        fullForceRunRow *row = fullForceRunObjectFromMetadata(md);
    656 
     628       
    657629        if (!row) {
    658630          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into fullforceRun");
     
    660632          return false;
    661633        }
    662 
     634       
    663635        if (!dvodb) {  // there's no skycalRun.dvodb
    664636          psError(PS_ERR_UNKNOWN, false, "cannot queue addstar run without a defined dvodb: label: %s, ff_id %" PRId64, row->label, row->ff_id);
     
    671643          return false;
    672644        }
    673 
     645       
    674646        psFree(row);
    675647      }
    676648    }
    677 
     649   
    678650
    679651    // start a transaction so we don't end up with an exp without any associted
     
    690662    // old values in place (i.e., passing the values through).
    691663
    692     // loop over our list of camRun rows
     664   
    693665    if (strcmp(stage,"cam") == 0) {
    694     for (long i = 0; i < psArrayLength(output); i++) {
     666      // loop over our list of camRun rows
     667      for (long i = 0; i < psArrayLength(output); i++) {
    695668        psMetadata *md = output->data[i];
    696669        psS64 stage_id =0;
    697670       
    698                   camRunRow *row = camRunObjectFromMetadata(md);
    699           stage_id = row->cam_id;
     671        camRunRow *row = camRunObjectFromMetadata(md);
     672        stage_id = row->cam_id;
    700673       
    701674        if (!row) {
    702             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    703             psFree(output);
    704             return false;
    705         }
    706 
     675          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     676          psFree(output);
     677          return false;
     678        }
     679       
    707680        // queue the exp
    708681        if (!pxaddQueueByCamID(config,
     
    720693                               minidvodb_group,
    721694                               minidvodb_name
    722         )) {
    723             if (!psDBRollback(config->dbh)) {
    724                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    725             }
    726             psError(PS_ERR_UNKNOWN, false,
    727                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    728             psFree(row);
    729             psFree(output);
    730             return false;
     695                               )) {
     696          if (!psDBRollback(config->dbh)) {
     697            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     698          }
     699          psError(PS_ERR_UNKNOWN, false,
     700                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     701          psFree(row);
     702          psFree(output);
     703          return false;
    731704        }
    732705        psFree(row);
    733     }
    734       }
    735     if (strcmp(stage,"stack") == 0) {
    736     for (long i = 0; i < psArrayLength(output); i++) {
     706      }
     707    }
     708    else if (strcmp(stage,"stack") == 0) {
     709      for (long i = 0; i < psArrayLength(output); i++) {
    737710        psMetadata *md = output->data[i];
    738711        psS64 stage_id =0;
    739712       
    740                   stackRunRow *row = stackRunObjectFromMetadata(md);
    741           stage_id = row->stack_id;
     713        stackRunRow *row = stackRunObjectFromMetadata(md);
     714        stage_id = row->stack_id;
    742715       
    743716        if (!row) {
    744             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    745             psFree(output);
    746             return false;
    747         }
    748 
     717          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     718          psFree(output);
     719          return false;
     720        }
     721       
    749722        // queue the exp
    750723        if (!pxaddQueueByCamID(config,
     
    762735                               minidvodb_group,
    763736                               minidvodb_name
    764         )) {
    765             if (!psDBRollback(config->dbh)) {
    766                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    767             }
    768             psError(PS_ERR_UNKNOWN, false,
    769                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    770             psFree(row);
    771             psFree(output);
    772             return false;
     737                               )) {
     738          if (!psDBRollback(config->dbh)) {
     739            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     740          }
     741          psError(PS_ERR_UNKNOWN, false,
     742                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     743          psFree(row);
     744          psFree(output);
     745          return false;
    773746        }
    774747        psFree(row);
    775     }
    776       }
    777    
    778     if (strcmp(stage,"staticsky") == 0) {
    779     for (long i = 0; i < psArrayLength(output); i++) {
     748      }
     749    }
     750    else if (strcmp(stage,"staticsky") == 0) {
     751      for (long i = 0; i < psArrayLength(output); i++) {
    780752        psMetadata *md = output->data[i];
    781753        psS64 stage_id =0;
     
    783755        //psS32 num_inputs = psMetadataLookupS32(&status, md, "num_inputs");
    784756        psS32 stack_id = psMetadataLookupS32(&status, md, "stack_id");
    785 
     757       
    786758        if (!status) {
    787759          psError(PS_ERR_UNKNOWN, true, "failed to lookup value for item stack_id");
     
    792764       
    793765        if (!row) {
    794             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
    795             psFree(output);
    796             return false;
    797         }
    798 
     766          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into camRun");
     767          psFree(output);
     768          return false;
     769        }
     770       
    799771        // queue the exp
    800772        //for (int filter_id = 0; filter_id < num_inputs; filter_id++) { 
    801           if (!pxaddQueueByCamID(config,
     773        if (!pxaddQueueByCamID(config,
    802774                               stage,
    803775                               stage_id,
     
    813785                               minidvodb_group,
    814786                               minidvodb_name
    815                                  )) {
    816             if (!psDBRollback(config->dbh)) {
    817               psError(PS_ERR_UNKNOWN, false, "database error sfg");
    818             }
    819             psError(PS_ERR_UNKNOWN, false,
    820                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    821             psFree(row);
    822             psFree(output);
    823             return false;
     787                               )) {
     788          if (!psDBRollback(config->dbh)) {
     789            psError(PS_ERR_UNKNOWN, false, "database error sfg");
    824790          }
    825           //}
     791          psError(PS_ERR_UNKNOWN, false,
     792                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     793          psFree(row);
     794          psFree(output);
     795          return false;
     796        }
     797        //}
    826798        psFree(row);
    827     }
    828       }
    829 
    830     if (strcmp(stage,"skycal") == 0) {
    831     for (long i = 0; i < psArrayLength(output); i++) {
     799      }
     800    }
     801    else if (strcmp(stage,"skycal") == 0) {
     802      for (long i = 0; i < psArrayLength(output); i++) {
    832803        psMetadata *md = output->data[i];
    833804        psS64 stage_id =0;
    834805       
    835                   skycalRunRow *row = skycalRunObjectFromMetadata(md);
    836           stage_id = row->skycal_id;
     806        skycalRunRow *row = skycalRunObjectFromMetadata(md);
     807        stage_id = row->skycal_id;
    837808       
    838809        if (!row) {
    839             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
    840             psFree(output);
    841             return false;
    842         }
    843 
     810          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
     811          psFree(output);
     812          return false;
     813        }
     814       
    844815        // queue the exp
    845816        if (!pxaddQueueByCamID(config,
     
    857828                               minidvodb_group,
    858829                               minidvodb_name
    859         )) {
    860             if (!psDBRollback(config->dbh)) {
    861                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    862             }
    863             psError(PS_ERR_UNKNOWN, false,
    864                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    865             psFree(row);
    866             psFree(output);
    867             return false;
     830                               )) {
     831          if (!psDBRollback(config->dbh)) {
     832            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     833          }
     834          psError(PS_ERR_UNKNOWN, false,
     835                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     836          psFree(row);
     837          psFree(output);
     838          return false;
    868839        }
    869840        psFree(row);
    870     }
    871       }
    872    
    873     if (strcmp(stage,"diff") == 0) {
    874      
    875     for (long i = 0; i < psArrayLength(output); i++) {
     841      }
     842    }
     843    else if (strcmp(stage,"diff") == 0) {
     844      for (long i = 0; i < psArrayLength(output); i++) {
    876845        psMetadata *md = output->data[i];
    877846        psS64 stage_id =0;
     
    885854          return false;
    886855        }
    887 
    888856       
    889857        if (!row) {
    890             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
    891             psFree(output);
    892             return false;
     858          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
     859          psFree(output);
     860          return false;
    893861        }
    894862       
     
    908876                               minidvodb_group,
    909877                               minidvodb_name
    910         )) {
    911             if (!psDBRollback(config->dbh)) {
    912                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    913             }
    914             psError(PS_ERR_UNKNOWN, false,
    915                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    916             psFree(row);
    917             psFree(output);
    918             return false;
     878                               )) {
     879          if (!psDBRollback(config->dbh)) {
     880            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     881          }
     882          psError(PS_ERR_UNKNOWN, false,
     883                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     884          psFree(row);
     885          psFree(output);
     886          return false;
    919887        }
    920888        psFree(row);
    921     }
    922       }
    923  
    924     if (strcmp(stage,"fullforce") == 0) {
    925     for (long i = 0; i < psArrayLength(output); i++) {
     889      }
     890    }
     891    else if (strcmp(stage,"fullforce") == 0) {
     892      for (long i = 0; i < psArrayLength(output); i++) {
    926893        psMetadata *md = output->data[i];
    927894        psS64 stage_id =0;
     
    935902          return false;
    936903        }
    937 
     904       
    938905        if (!row) {
    939             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
    940             psFree(output);
    941             return false;
    942         }
    943 
     906          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into skycalRun");
     907          psFree(output);
     908          return false;
     909        }
     910       
    944911        // queue the exp
    945912        if (!pxaddQueueByCamID(config,
     
    957924                               minidvodb_group,
    958925                               minidvodb_name
    959         )) {
    960             if (!psDBRollback(config->dbh)) {
    961                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    962             }
    963             psError(PS_ERR_UNKNOWN, false,
    964                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    965             psFree(row);
    966             psFree(output);
    967             return false;
     926                               )) {
     927          if (!psDBRollback(config->dbh)) {
     928            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     929          }
     930          psError(PS_ERR_UNKNOWN, false,
     931                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     932          psFree(row);
     933          psFree(output);
     934          return false;
    968935        }
    969936        psFree(row);
    970     }
    971     }
    972     if (strcmp(stage,"fullforce_summary") == 0) {
     937      }
     938    }
     939    else if (strcmp(stage,"fullforce_summary") == 0) {
    973940      for (long i = 0; i < psArrayLength(output); i++) {
    974941        psMetadata *md = output->data[i];
     
    979946       
    980947        if (!row) {
    981             psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into fullforceRun");
    982             psFree(output);
    983             return false;
    984         }
    985 
     948          psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into fullforceRun");
     949          psFree(output);
     950          return false;
     951        }
     952       
    986953        // queue the exp
    987954        if (!pxaddQueueByCamID(config,
     
    999966                               minidvodb_group,
    1000967                               minidvodb_name
    1001         )) {
    1002             if (!psDBRollback(config->dbh)) {
    1003                 psError(PS_ERR_UNKNOWN, false, "database error sfg");
    1004             }
    1005             psError(PS_ERR_UNKNOWN, false,
    1006                     "failed to trying to queue stage %s %" PRId64,stage, stage_id);
    1007             psFree(row);
    1008             psFree(output);
    1009             return false;
     968                               )) {
     969          if (!psDBRollback(config->dbh)) {
     970            psError(PS_ERR_UNKNOWN, false, "database error sfg");
     971          }
     972          psError(PS_ERR_UNKNOWN, false,
     973                  "failed to trying to queue stage %s %" PRId64,stage, stage_id);
     974          psFree(row);
     975          psFree(output);
     976          return false;
    1010977        }
    1011978        psFree(row);
    1012     }
    1013       }
    1014 
    1015 
    1016 
    1017 
    1018 
     979      }
     980    }
    1019981    psFree(output);
    1020982
     
    10541016    }
    10551017    psString query = NULL;
    1056     if (strcmp(stage, "cam")==0) {
     1018    if (strcmp(stage, "cam") == 0) {
    10571019      query = psStringCopy("UPDATE addRun JOIN camRun on (cam_id = stage_id and stage = 'cam')  JOIN chipRun USING(chip_id) JOIN rawExp USING(exp_id)");
    10581020    }
    1059     if (strcmp(stage, "stack")==0) {
     1021    else if (strcmp(stage, "stack") == 0) {
    10601022      query = psStringCopy("UPDATE addRun JOIN stackRun on (stack_id = stage_id and stage = 'stack')");
    10611023    }
    1062     if (strcmp(stage, "staticsky")==0) {
     1024    else if (strcmp(stage, "staticsky") == 0) {
    10631025      query = psStringCopy("UPDATE addRun JOIN staticskyRun on (sky_id = stage_id and stage = 'staticsky')");
    10641026    }
    1065     if (strcmp(stage, "skycal")==0) {
     1027    else if (strcmp(stage, "skycal") == 0) {
    10661028      query = psStringCopy("UPDATE addRun JOIN skycalRun on (skycal_id = stage_id and stage = 'skycal')");
    10671029    }
    1068     if (strcmp(stage, "diff")==0) {
     1030    else if (strcmp(stage, "diff") == 0) {
    10691031      query = psStringCopy("UPDATE addRun JOIN diffInputSkyfile on (diff_id = stage_id and diff_skyfile_id = stage_extra1 and stage = 'diff') JOIN diffSkyfile on (diff_id, skycell_id) JOIN diffRun using (diff_id)");
    10701032    }
    1071     if (strcmp(stage, "fullforce")==0) {
     1033    else if (strcmp(stage, "fullforce") == 0) {
    10721034      query = psStringCopy("UPDATE addRun JOIN fullForceResult on (ff_id = stage_id and warp_id = stage_extra1 and stage = 'fullforce') JOIN fullForceRun on (ff_id)");
    10731035    }
    1074     if (strcmp(stage, "fullforce_summary")==0) {
     1036    else if (strcmp(stage, "fullforce_summary") == 0) {
    10751037      query = psStringCopy("UPDATE addRun JOIN fullForceSummary on (ff_id = stage_id and stage = 'fullforce_summary') JOIN fullForceRun on (ff_id)");
    10761038    }
     
    11121074    psString query = NULL;
    11131075   
    1114     if (strcmp(stage, "cam")==0) {
     1076    if (strcmp(stage, "cam") == 0) {
    11151077      query = pxDataGet("addtool_find_pendingexp_cam.sql");
    11161078    }
    1117     if (strcmp(stage, "stack")==0) {
     1079    else if (strcmp(stage, "stack") == 0) {
    11181080      query = pxDataGet("addtool_find_pendingexp_stack.sql");
    11191081    }
    1120     if (strcmp(stage, "staticsky")==0) {
     1082    else if (strcmp(stage, "staticsky") == 0) {
    11211083      query = pxDataGet("addtool_find_pendingexp_staticsky_multi.sql");
    11221084    }
    1123     if (strcmp(stage, "skycal")==0) {
     1085    else if (strcmp(stage, "skycal") == 0) {
    11241086      query = pxDataGet("addtool_find_pendingexp_skycal.sql");
    11251087    }
    1126     if (strcmp(stage, "diff")==0) {
     1088    else if (strcmp(stage, "diff") == 0) {
    11271089      query = pxDataGet("addtool_find_pendingexp_diff.sql");
    11281090    }
    1129     if (strcmp(stage, "fullforce")==0) {
     1091    else if (strcmp(stage, "fullforce") == 0) {
    11301092      query = pxDataGet("addtool_find_pendingexp_ff.sql");
    11311093    }
    1132     if (strcmp(stage, "fullforce_summary")==0) {
     1094    else if (strcmp(stage, "fullforce_summary") == 0) {
    11331095      query = pxDataGet("addtool_find_pendingexp_ffsummary.sql");
    11341096    }
     
    11511113      psStringAppend(&query, " GROUP BY %s", "stack_id");
    11521114    }
    1153     if (strcmp(stage, "skycal") == 0) {
     1115    else if (strcmp(stage, "skycal") == 0) {
    11541116      //this group by is needed to join against all the warps (to get camera)
    11551117      psStringAppend(&query, " GROUP BY %s", "skycal_id");
    11561118    }
    1157     if (strcmp(stage, "staticsky") == 0) {
     1119    else if (strcmp(stage, "staticsky") == 0) {
    11581120      //this group by is needed to join against all the warps (to get camera)
    11591121      psStringAppend(&query, " GROUP BY %s", "sky_id, stage_extra1");
    11601122    }
    1161     if (strcmp(stage, "diff") == 0) {
     1123    else if (strcmp(stage, "diff") == 0) {
    11621124      //this group by is needed to join against all the warps (to get camera)
    11631125      if (multiadd) {
    11641126        psStringAppend(&query, " GROUP BY %s", "diff_id");
    11651127      } else {
    1166       psStringAppend(&query, " GROUP BY %s", "diff_id, stage_extra1");
    1167       }
    1168     }
    1169     if (strcmp(stage, "fullforce") == 0) {
     1128        psStringAppend(&query, " GROUP BY %s", "diff_id, stage_extra1");
     1129      }
     1130    }
     1131    else if (strcmp(stage, "fullforce") == 0) {
    11701132      //this group by is needed to join against all the warps (to get camera)
    11711133      if (multiadd) {
     
    11751137      }
    11761138    }
    1177    
    1178     if (strcmp(stage, "fullforce_summary") == 0) {
     1139    else if (strcmp(stage, "fullforce_summary") == 0) {
    11791140      //this group by is needed to join against all the warps (to get camera)
    11801141      psStringAppend(&query, "GROUP BY %s", "ff_id");
     
    11831144    // treat limit == 0 as "no limit"
    11841145    if (limit) {
    1185         psString limitString = psDBGenerateLimitSQL(limit);
    1186         psStringAppend(&query, " %s", limitString);
    1187         psFree(limitString);
     1146      psString limitString = psDBGenerateLimitSQL(limit);
     1147      psStringAppend(&query, " %s", limitString);
     1148      psFree(limitString);
    11881149    }
    11891150    if (!p_psDBRunQuery(config->dbh, query)) {
    1190         psError(PS_ERR_UNKNOWN, false, "database error");
    1191         psFree(query);
    1192         return false;
     1151      psError(PS_ERR_UNKNOWN, false, "database error");
     1152      psFree(query);
     1153      return false;
    11931154    }
    11941155    //    printf ("%s\n", query);
    11951156    //psTrace("addtool.c", PS_LOG_INFO, "used this query:\n %s\n",query);
    11961157    psFree(query);
    1197 
     1158   
    11981159    psArray *output = p_psDBFetchResult(config->dbh);
    11991160    if (!output) {
    1200         psError(PS_ERR_UNKNOWN, false, "database error");
    1201         return false;
     1161      psError(PS_ERR_UNKNOWN, false, "database error");
     1162      return false;
    12021163    }
    12031164    if (!psArrayLength(output)) {
    1204         psTrace("addtool", PS_LOG_INFO, "no rows found");
    1205         psFree(output);
    1206         return true;
    1207     }
    1208 
     1165      psTrace("addtool", PS_LOG_INFO, "no rows found");
     1166      psFree(output);
     1167      return true;
     1168    }
     1169   
    12091170    // negate simple so the default is true
    12101171    if (!ippdbPrintMetadatas(stdout, output, "addPendingExp", !simple)) {
    1211         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    1212         psFree(output);
    1213         return false;
    1214     }
    1215 
     1172      psError(PS_ERR_UNKNOWN, false, "failed to print array");
     1173      psFree(output);
     1174      return false;
     1175    }
     1176   
    12161177    psFree(output);
    1217 
     1178   
    12181179    return true;
    12191180}
     
    12221183{
    12231184    PS_ASSERT_PTR_NON_NULL(config, false);
    1224 
     1185 
    12251186    // required
    12261187    // PXOPT_LOOKUP_S64(add_id, config->args, "-add_id", true, false);
    1227 
     1188   
    12281189    // optional
    12291190    PXOPT_LOOKUP_STR(path_base,     config->args, "-path_base", false, false);
     
    12331194    PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
    12341195    PXOPT_LOOKUP_S64(stage_extra1, config->args, "-stage_extra1", false, false);
    1235 
     1196   
    12361197    PXOPT_LOOKUP_BOOL(multiadd, config->args, "-multiadd", false);
    12371198    // generate restrictions
     
    12561217    psString query = pxDataGet("addtool_find_pendingexp.sql");
    12571218    if (!query) {
    1258         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    1259         return false;
    1260     }
    1261 
     1219      psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1220      return false;
     1221    }
     1222   
    12621223    // use psDBGenerateWhereSQL because the SQL yields an intermediate table
    12631224    if (psListLength(where->list)) {
    1264         psString whereClaus = psDBGenerateWhereConditionSQL(where, NULL);
    1265         psStringAppend(&query, " AND %s", whereClaus);
    1266         psFree(whereClaus);
     1225      psString whereClaus = psDBGenerateWhereConditionSQL(where, NULL);
     1226      psStringAppend(&query, " AND %s", whereClaus);
     1227      psFree(whereClaus);
    12671228    }
    12681229    psFree(where);
    1269 
     1230   
    12701231    //if (multiadd) {
    12711232    //  psStringAppend(&query, "group by  %s", "stage_extra1");
    12721233    //}
    1273 
     1234   
    12741235    if (!p_psDBRunQuery(config->dbh, query)) {
    1275         psError(PS_ERR_UNKNOWN, false, "database error");
    1276         psFree(query);
    1277         return false;
    1278     }
    1279 
     1236      psError(PS_ERR_UNKNOWN, false, "database error");
     1237      psFree(query);
     1238      return false;
     1239    }
     1240   
    12801241    psFree(query);
    1281 
     1242   
    12821243    psArray *output = p_psDBFetchResult(config->dbh);
    12831244    if (!output) {
    1284         psError(PS_ERR_UNKNOWN, false, "database error");
    1285         return false;
     1245      psError(PS_ERR_UNKNOWN, false, "database error");
     1246      return false;
    12861247    }
    12871248   
    12881249    if (!psArrayLength(output)) {
    1289         psTrace("addtool", PS_LOG_INFO, "no rows found");
    1290         psFree(output);
    1291         return false;
    1292     }
    1293 
     1250      psTrace("addtool", PS_LOG_INFO, "no rows found");
     1251      psFree(output);
     1252      return false;
     1253    }
     1254   
    12941255    if (!psDBTransaction(config->dbh)) {
    1295         psError(PS_ERR_UNKNOWN, false, "database error");
    1296         psFree(output);
    1297         return false;
    1298     }
    1299 
     1256      psError(PS_ERR_UNKNOWN, false, "database error");
     1257      psFree(output);
     1258      return false;
     1259    }
     1260   
    13001261    for (long i =0; i<psArrayLength(output);i++) {
     1262      addRunRow *pendingRow = addRunObjectFromMetadata(output->data[i]);
    13011263     
    1302     addRunRow *pendingRow = addRunObjectFromMetadata(output->data[i]);
    1303    
    1304     addProcessedExpRow *row = addProcessedExpRowAlloc(
    1305         pendingRow->add_id,
    1306         dtime_addstar,
    1307         path_base,
    1308         dvodb_path,
    1309         fault
    1310         );
    1311 
    1312     if (!addProcessedExpInsertObject(config->dbh, row)) {
     1264      addProcessedExpRow *row = addProcessedExpRowAlloc(
     1265                                                        pendingRow->add_id,
     1266                                                        dtime_addstar,
     1267                                                        path_base,
     1268                                                        dvodb_path,
     1269                                                        fault
     1270                                                        );
     1271     
     1272      if (!addProcessedExpInsertObject(config->dbh, row)) {
    13131273        // rollback
    13141274        if (!psDBRollback(config->dbh)) {
    1315             psError(PS_ERR_UNKNOWN, false, "database error");
     1275          psError(PS_ERR_UNKNOWN, false, "database error");
    13161276        }
    13171277        psError(PS_ERR_UNKNOWN, false, "database error");
     
    13191279        psFree(pendingRow);
    13201280        return false;
    1321     }
    1322 
    1323     //if there is a minidvodb_name, set it in addRun (it's not known until it is processed)
    1324     if (minidvodb_name) {
    1325       psString setName = NULL;
    1326       psStringAppend (&setName, "UPDATE addRun set minidvodb_name = '%s' where add_id = %" PRId64, minidvodb_name, row->add_id);
    1327       if (!p_psDBRunQuery(config->dbh, setName)) {
    1328         if (!psDBRollback(config->dbh)) {
    1329           psError(PS_ERR_UNKNOWN, false, "database error");
    1330         }
     1281      }
     1282     
     1283      //if there is a minidvodb_name, set it in addRun (it's not known until it is processed)
     1284      if (minidvodb_name) {
     1285        psString setName = NULL;
     1286        psStringAppend (&setName, "UPDATE addRun set minidvodb_name = '%s' where add_id = %" PRId64, minidvodb_name, row->add_id);
     1287        if (!p_psDBRunQuery(config->dbh, setName)) {
     1288          if (!psDBRollback(config->dbh)) {
     1289            psError(PS_ERR_UNKNOWN, false, "database error");
     1290          }
     1291          psError(PS_ERR_UNKNOWN, false, "database error");
     1292         
     1293          return false;
     1294        }
     1295      }
     1296      //if there is a stage_extra1, set it in addRun (it's not known until it is processed)
     1297      if (stage_extra1) {
     1298        psString setExtra = NULL;
     1299        psStringAppend (&setExtra, "UPDATE addRun set stage_extra1 = %" PRId64, stage_extra1);
     1300        psStringAppend (&setExtra, " where add_id = %" PRId64, row->add_id);
     1301       
     1302        if (!p_psDBRunQuery(config->dbh, setExtra)) {
     1303          if (!psDBRollback(config->dbh)) {
     1304            psError(PS_ERR_UNKNOWN, false, "database error");
     1305          }
     1306          psError(PS_ERR_UNKNOWN, false, "database error");
     1307         
     1308          return false;
     1309        }
     1310      }
     1311     
     1312     
     1313     
     1314     
     1315      // since there is only one exp per 'new' set addRun.state = 'full'
     1316      // but check to make sure there are no faults
     1317     
     1318      if (!fault) {
     1319        if (!pxaddRunSetState(config, row->add_id, "full")) {
     1320          psError(PS_ERR_UNKNOWN, false, "failed to change addRun.state for add_id: %" PRId64, row->add_id);
     1321          psFree(row);
     1322          psFree(pendingRow);
     1323          return false;
     1324        }
     1325      }
     1326      psFree(row);
     1327      psFree(pendingRow);
     1328     
     1329      if (!psDBCommit(config->dbh)) {
    13311330        psError(PS_ERR_UNKNOWN, false, "database error");
    1332 
    1333         return false;
    1334       }
    1335     }
    1336     //if there is a stage_extra1, set it in addRun (it's not known until it is processed)
    1337     if (stage_extra1) {
    1338       psString setExtra = NULL;
    1339       psStringAppend (&setExtra, "UPDATE addRun set stage_extra1 = %" PRId64, stage_extra1);
    1340       psStringAppend (&setExtra, " where add_id = %" PRId64, row->add_id);
    1341 
    1342       if (!p_psDBRunQuery(config->dbh, setExtra)) {
    1343         if (!psDBRollback(config->dbh)) {
    1344           psError(PS_ERR_UNKNOWN, false, "database error");
    1345         }
    1346         psError(PS_ERR_UNKNOWN, false, "database error");
    1347 
    1348         return false;
    1349       }
    1350     }
    1351 
    1352 
    1353 
    1354 
    1355     // since there is only one exp per 'new' set addRun.state = 'full'
    1356     // but check to make sure there are no faults
    1357 
    1358     if (!fault) {
    1359       if (!pxaddRunSetState(config, row->add_id, "full")) {
    1360         psError(PS_ERR_UNKNOWN, false, "failed to change addRun.state for add_id: %" PRId64, row->add_id);
    1361         psFree(row);
    1362         psFree(pendingRow);
    1363         return false;
    1364       }
    1365     }
    1366     psFree(row);
    1367     psFree(pendingRow);
    1368 
    1369     if (!psDBCommit(config->dbh)) {
    1370         psError(PS_ERR_UNKNOWN, false, "database error");
    1371         return false;
    1372     }
    1373     }
    1374 
     1331        return false;
     1332      }
     1333    }
     1334   
    13751335    return true;
    13761336}
     
    15071467    pxAddLabelSearchArgs (config, where, "-label",    "addRun.label",     "==");
    15081468    PXOPT_COPY_STR(config->args, where, "-reduction", "addRun.reduction",      "==");
    1509 
     1469   
    15101470    if (!psListLength(where->list) && !psMetadataLookupBool(NULL, config->args, "-all")) {
    1511         psFree(where);
    1512         psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    1513         return false;
    1514     }
    1515 
     1471      psFree(where);
     1472      psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1473      return false;
     1474    }
     1475   
    15161476    if (!psDBTransaction(config->dbh)) {
    1517         psError(PS_ERR_UNKNOWN, false, "database error");
    1518         psFree(where);
    1519         return false;
    1520     }
    1521 
     1477      psError(PS_ERR_UNKNOWN, false, "database error");
     1478      psFree(where);
     1479      return false;
     1480    }
     1481   
    15221482    {
    15231483      psString query = NULL;
    15241484      if (strcmp(stage, "cam") == 0) {
    1525          query = pxDataGet("addtool_revertprocessedexp_cam.sql");
    1526         }
    1527        
    1528     if (strcmp(stage, "stack") == 0) {
    1529          query = pxDataGet("addtool_revertprocessedexp_stack.sql");
    1530       }
    1531     if (strcmp(stage, "staticsky") == 0) {
    1532          query = pxDataGet("addtool_revertprocessedexp_staticsky_multi.sql");
    1533       }
    1534     if (strcmp(stage, "skycal") == 0) {
    1535       query = pxDataGet("addtool_revertprocessedexp_skycal.sql");
    1536     }
    1537     if (strcmp(stage, "diff") == 0) {
    1538       query = pxDataGet("addtool_revertprocessedexp_diff.sql");
    1539     }
    1540     if (strcmp(stage, "fullforce") == 0) {
    1541       query = pxDataGet("addtool_revertprocessedexp_ff.sql");
    1542     }
    1543     if (strcmp(stage, "fullforce_summary") == 0) {
    1544       query = pxDataGet("addtool_revertprocessedexp_ffsummary.sql");
    1545     }
    1546 
    1547 
     1485        query = pxDataGet("addtool_revertprocessedexp_cam.sql");
     1486      }
     1487      else if (strcmp(stage, "stack") == 0) {
     1488        query = pxDataGet("addtool_revertprocessedexp_stack.sql");
     1489      }
     1490      else if (strcmp(stage, "staticsky") == 0) {
     1491        query = pxDataGet("addtool_revertprocessedexp_staticsky_multi.sql");
     1492      }
     1493      else if (strcmp(stage, "skycal") == 0) {
     1494        query = pxDataGet("addtool_revertprocessedexp_skycal.sql");
     1495      }
     1496      else if (strcmp(stage, "diff") == 0) {
     1497        query = pxDataGet("addtool_revertprocessedexp_diff.sql");
     1498      }
     1499      else if (strcmp(stage, "fullforce") == 0) {
     1500        query = pxDataGet("addtool_revertprocessedexp_ff.sql");
     1501      }
     1502      else if (strcmp(stage, "fullforce_summary") == 0) {
     1503        query = pxDataGet("addtool_revertprocessedexp_ffsummary.sql");
     1504      }
    15481505     
    15491506      if (!query) {
    1550             // rollback
     1507        // rollback
    15511508        if (!psDBRollback(config->dbh)) {
    1552                 psError(PS_ERR_UNKNOWN, false, "database error");
    1553             }
    1554             psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    1555             psFree(where);
    1556             return false;
    1557         }
    1558 
    1559         // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
    1560         if (where && psListLength(where->list)) {
    1561             psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    1562             psStringAppend(&query, " AND %s", whereClause);
    1563             psFree(whereClause);
    1564         }
    1565 
    1566         if (!p_psDBRunQuery(config->dbh, query)) {
    1567             // rollback
    1568             if (!psDBRollback(config->dbh)) {
    1569                 psError(PS_ERR_UNKNOWN, false, "database error");
    1570             }
    1571             psError(PS_ERR_UNKNOWN, false, "database error");
    1572             psFree(query);
    1573             psFree(where);
    1574             return false;
    1575         }
    1576         psFree(query);
     1509          psError(PS_ERR_UNKNOWN, false, "database error");
     1510        }
     1511        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1512        psFree(where);
     1513        return false;
     1514      }
     1515     
     1516      // use psDBGenerateWhereConditionalSQL with AND ... because the SQL ends in a WHERE
     1517      if (where && psListLength(where->list)) {
     1518        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     1519        psStringAppend(&query, " AND %s", whereClause);
     1520        psFree(whereClause);
     1521      }
     1522     
     1523      if (!p_psDBRunQuery(config->dbh, query)) {
     1524        // rollback
     1525        if (!psDBRollback(config->dbh)) {
     1526          psError(PS_ERR_UNKNOWN, false, "database error");
     1527        }
     1528        psError(PS_ERR_UNKNOWN, false, "database error");
     1529        psFree(query);
     1530        psFree(where);
     1531        return false;
     1532      }
     1533      psFree(query);
    15771534    }
    15781535    psFree(where);
    1579 
     1536   
    15801537    if (!psDBCommit(config->dbh)) {
    1581         psError(PS_ERR_UNKNOWN, false, "database error");
    1582         return false;
    1583     }
    1584 
     1538      psError(PS_ERR_UNKNOWN, false, "database error");
     1539      return false;
     1540    }
     1541   
    15851542    return true;
    15861543}
     
    16821639
    16831640  // required
    1684 
    16851641  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-set_minidvodb_group", true, false);
    1686     //optional
     1642
     1643  //optional
    16871644  PXOPT_LOOKUP_STR(minidvodb_name, config->args, "-set_minidvodb_name", false, false);
    16881645  PXOPT_LOOKUP_STR(minidvodb_path, config->args, "-set_minidvodb_path", false, false);
     
    17031660    return false;
    17041661  }
    1705 
     1662 
    17061663  if (!minidvodbRunInsert(config->dbh,
    1707             0, // job_id
    1708             minidvodb_name,
    1709             minidvodb_group,
    1710             minidvodbpath,
    1711             "new",
    1712             0
    1713             )) {
     1664                          0, // job_id
     1665                          minidvodb_name,
     1666                          minidvodb_group,
     1667                          minidvodbpath,
     1668                          "new",
     1669                          0
     1670                          )) {
    17141671    psError(PS_ERR_UNKNOWN, false, "database error");
    17151672    return false;
    17161673  }
    1717 
     1674 
    17181675  psU64 affected = psDBAffectedRows(config->dbh);
    17191676  if (affected != 1) {
     
    17231680    return false;
    17241681  }
    1725 
     1682 
    17261683  psS64 minidvodb_id = psDBLastInsertID(config->dbh);
    17271684  printf("%" PRId64 "\n", minidvodb_id);
    17281685
    1729 
     1686 
    17301687  if (!minidvodb_name) {
    17311688    psStringAppend(&minidvodb_name, "%s.%" PRIu64,minidvodb_group,minidvodb_id);
     
    17341691    psStringAppend(&minidvodb_path,"/%s",minidvodb_name);
    17351692  }
    1736 
    17371693
    17381694  psString query = NULL;
     
    17861742    if (minidvodb_path) {
    17871743      if (cnt) {
    1788           psStringAppend(&query, "%s", comma);
    1789       }
    1790 
     1744        psStringAppend(&query, "%s", comma);
     1745      }
     1746     
    17911747      psStringAppend(&query, " minidvodb_path = '%s'", minidvodb_path);
    17921748      cnt++;
     
    19101866    }
    19111867    psFree(query);
    1912   return false;
    1913   }
    1914  psFree(query2);
    1915  psFree(where);
    1916 
    1917  return true;
    1918 
     1868    return false;
     1869  }
     1870  psFree(query2);
     1871  psFree(where);
     1872 
     1873  return true;
     1874 
    19191875}
    19201876
     
    19361892
    19371893  if (!psListLength(where->list)) {
    1938         psFree(where);
    1939         psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
    1940         return false;
     1894    psFree(where);
     1895    psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     1896    return false;
    19411897  }
    19421898
     
    19501906 
    19511907  if (!bare_query) {
    1952         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    1953         return false;
     1908    psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     1909    return false;
    19541910  }
    19551911
     
    19571913  if (psListLength(where->list)) {
    19581914    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    1959         psStringAppend(&where_string, " WHERE %s", whereClause);
    1960         psFree(whereClause);
    1961         //search params are defined above
     1915    psStringAppend(&where_string, " WHERE %s", whereClause);
     1916    psFree(whereClause);
     1917    //search params are defined above
    19621918  }
    19631919
     
    19661922  psStringAppend(&query," %s ", where_string);
    19671923
    1968 
    1969 
    1970 
    1971  
    19721924  if (!all_addrun_states) {
    19731925    psStringAppend(&query, " AND (cnt2 = cnt) ");
     
    20822034  PS_ASSERT_PTR_NON_NULL(config, false);
    20832035
    2084     // required
     2036  // required
    20852037  // PXOPT_LOOKUP_U64(minidvodb_id, config->args, "-minidvodb_id", true, false);
    20862038  PXOPT_LOOKUP_STR(minidvodb_group, config->args, "-minidvodb_group", true, false);
    20872039
    2088     // optional
     2040  // optional
    20892041  PXOPT_LOOKUP_F32(dtime_relphot, config->args, "-dtime_relphot", false, false);
    20902042  PXOPT_LOOKUP_F32(dtime_resort, config->args, "-dtime_resort", false, false);
     
    20932045  PXOPT_LOOKUP_TIME(epoch, config->args, "-epoch", false, false);
    20942046  PXOPT_LOOKUP_S16(fault,         config->args, "-fault", false, false);
    2095     //generate restrictions
     2047
     2048  //generate restrictions
    20962049  psMetadata *where = psMetadataAlloc();
    20972050  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",   "minidvodbRun.minidvodb_id",   "==");
     
    21382091  psFree(output);
    21392092  minidvodbProcessedRow *row = minidvodbProcessedRowAlloc(
    2140                pendingRow->minidvodb_id,
    2141                dtime_resort,
    2142                dtime_relphot,
    2143                dtime_script,
    2144                epoch,
    2145                fault
    2146                );
    2147 
     2093                                                          pendingRow->minidvodb_id,
     2094                                                          dtime_resort,
     2095                                                          dtime_relphot,
     2096                                                          dtime_script,
     2097                                                          epoch,
     2098                                                          fault
     2099                                                          );
     2100 
    21482101  if (!minidvodbProcessedInsertObject(config->dbh, row)) {
    21492102    // rollback
     
    21712124  psFree(row);
    21722125  psFree(pendingRow);
    2173 
    2174 
    2175 
    2176 
    21772126
    21782127  //commit the changes
     
    23422291  PXOPT_LOOKUP_F32(dtime_relphot,  config->args, "-set_dtime_relphot", false, false);
    23432292  PXOPT_LOOKUP_F32(dtime_resort,  config->args, "-set_dtime_resort", false, false);
    2344 PXOPT_LOOKUP_F32(dtime_script,  config->args, "-set_dtime_script", false, false);
     2293  PXOPT_LOOKUP_F32(dtime_script,  config->args, "-set_dtime_script", false, false);
    23452294  PXOPT_COPY_S64(config->args, where, "-minidvodb_id",     "minidvodbProcessed.minidvodb_id", "==");
    23462295  PXOPT_COPY_STR(config->args, where, "-minidvodb_name",     "minidvodbRun.minidvodb_name", "==");
Note: See TracChangeset for help on using the changeset viewer.