IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 23, 2012, 4:07:36 PM (14 years ago)
Author:
Serge CHASTEL
Message:

Preventing insertion of non-0 value from temp table for diff_skyfile_id in diffInputSkyfile

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ippTools/src/difftool.c

    r34503 r34538  
    9797        MODECASE(DIFFTOOL_MODE_DEFINEWARPWARP,        definewarpwarpMode);
    9898        MODECASE(DIFFTOOL_MODE_DEFINESTACKSTACK,      definestackstackMode);
    99         MODECASE(DIFFTOOL_MODE_TOSUMMARY,             tosummaryMode);
    100         MODECASE(DIFFTOOL_MODE_ADDSUMMARY,            addsummaryMode);
     99        MODECASE(DIFFTOOL_MODE_TOSUMMARY,             tosummaryMode);
     100        MODECASE(DIFFTOOL_MODE_ADDSUMMARY,            addsummaryMode);
    101101        MODECASE(DIFFTOOL_MODE_PENDINGCLEANUPRUN,     pendingcleanuprunMode);
    102102        MODECASE(DIFFTOOL_MODE_PENDINGCLEANUPSKYFILE, pendingcleanupskyfileMode);
     
    170170            exposure,
    171171            false,
    172             NULL, // software version
    173             0,    // mask stat npix
    174             NAN,    // static
    175             NAN,    // dynamic
    176             NAN,    // magic
    177             NAN,    // advisory
     172            NULL, // software version
     173            0,    // mask stat npix
     174            NAN,    // static
     175            NAN,    // dynamic
     176            NAN,    // magic
     177            NAN,    // advisory
    178178            diff_mode,
    179179            note
     
    579579    PXOPT_LOOKUP_F32(maskfrac_magic, config->args, "-maskfrac_magic", false, false);
    580580    PXOPT_LOOKUP_F32(maskfrac_advisory, config->args, "-maskfrac_advisory", false, false);
    581    
     581
    582582    psTrace("czw.test",1,"Received versions: pslib %s psmodules %s psphot %s ppstats %s ppsub %s streaks %s\n",
    583             ver_pslib,ver_psmodules,ver_psphot,ver_ppstats,ver_ppsub,ver_streaks);
     583            ver_pslib,ver_psmodules,ver_psphot,ver_ppstats,ver_ppsub,ver_streaks);
    584584    psString ver_code = NULL;
    585585    if ((ver_pslib)&&(ver_psmodules)) {
     
    631631                           fault,
    632632                           quality,
    633                            magicked,
    634                            ver_code,
    635                            maskfrac_npix,
    636                            maskfrac_static,
    637                            maskfrac_dynamic,
    638                            maskfrac_magic,
    639                            maskfrac_advisory
     633                           magicked,
     634                           ver_code,
     635                           maskfrac_npix,
     636                           maskfrac_static,
     637                           maskfrac_dynamic,
     638                           maskfrac_magic,
     639                           maskfrac_advisory
    640640          )) {
    641641        if (!psDBRollback(config->dbh)) {
     
    726726        psS64 magicked = psMetadataLookupS64(NULL, row, "magicked");
    727727
    728         psString software_ver = NULL;
    729         psS64 maskfrac_npix = 0;
    730         psF32 maskfrac_static = 0;
    731         psF32 maskfrac_dynamic = 0;
    732         psF32 maskfrac_magic = 0;
    733         psF32 maskfrac_advisory = 0;
    734 
    735         // Calculate run level masking and software state
    736         if (!pxCoalesceRunStatus(config,"difftool_coalesce_run.sql",diff_id,
    737                                 &software_ver,&maskfrac_npix,
    738                                 &maskfrac_static,&maskfrac_dynamic,
    739                                 &maskfrac_magic,&maskfrac_advisory)) {
    740           psError(PS_ERR_UNKNOWN, false, "failed to generate run level statistics");
    741           psFree(output);
    742           if (!psDBRollback(config->dbh)) {
    743             psError(PS_ERR_UNKNOWN, false, "database error");
    744           }
    745           return(false);
    746         }
    747         // Set diffRun.software_ver to the appropriate value
    748         if (software_ver) {
    749           if (!pxSetRunSoftware(config, "diffRun", "diff_id", diff_id, software_ver)) {
    750             psError(PS_ERR_UNKNOWN, false, "failed to set diffRun.software_ver for diff_id: %" PRId64,
    751                     diff_id);
    752             psFree(output);
    753             if (!psDBRollback(config->dbh)) {
    754               psError(PS_ERR_UNKNOWN, false, "database error");
    755             }
    756             return(false);
    757           }
    758         }
    759         // Set diffRun.maskfrac* to the appropriate values.
    760         if (maskfrac_npix) {
    761           if (!pxSetRunMaskfrac(config, "diffRun", "diff_id", diff_id, maskfrac_npix, maskfrac_static,
    762                                 maskfrac_dynamic, maskfrac_magic, maskfrac_advisory)) {
    763             psError(PS_ERR_UNKNOWN, false, "failed to set diffRun.software_ver for diff_id: %" PRId64,
    764                     diff_id);
    765             psFree(output);
    766             if (!psDBRollback(config->dbh)) {
    767               psError(PS_ERR_UNKNOWN, false, "database error");
    768             }
    769             return(false);
    770           }
    771         }
     728        psString software_ver = NULL;
     729        psS64 maskfrac_npix = 0;
     730        psF32 maskfrac_static = 0;
     731        psF32 maskfrac_dynamic = 0;
     732        psF32 maskfrac_magic = 0;
     733        psF32 maskfrac_advisory = 0;
     734
     735        // Calculate run level masking and software state
     736        if (!pxCoalesceRunStatus(config,"difftool_coalesce_run.sql",diff_id,
     737                                &software_ver,&maskfrac_npix,
     738                                &maskfrac_static,&maskfrac_dynamic,
     739                                &maskfrac_magic,&maskfrac_advisory)) {
     740          psError(PS_ERR_UNKNOWN, false, "failed to generate run level statistics");
     741          psFree(output);
     742          if (!psDBRollback(config->dbh)) {
     743            psError(PS_ERR_UNKNOWN, false, "database error");
     744          }
     745          return(false);
     746        }
     747        // Set diffRun.software_ver to the appropriate value
     748        if (software_ver) {
     749          if (!pxSetRunSoftware(config, "diffRun", "diff_id", diff_id, software_ver)) {
     750            psError(PS_ERR_UNKNOWN, false, "failed to set diffRun.software_ver for diff_id: %" PRId64,
     751                    diff_id);
     752            psFree(output);
     753            if (!psDBRollback(config->dbh)) {
     754              psError(PS_ERR_UNKNOWN, false, "database error");
     755            }
     756            return(false);
     757          }
     758        }
     759        // Set diffRun.maskfrac* to the appropriate values.
     760        if (maskfrac_npix) {
     761          if (!pxSetRunMaskfrac(config, "diffRun", "diff_id", diff_id, maskfrac_npix, maskfrac_static,
     762                                maskfrac_dynamic, maskfrac_magic, maskfrac_advisory)) {
     763            psError(PS_ERR_UNKNOWN, false, "failed to set diffRun.software_ver for diff_id: %" PRId64,
     764                    diff_id);
     765            psFree(output);
     766            if (!psDBRollback(config->dbh)) {
     767              psError(PS_ERR_UNKNOWN, false, "database error");
     768            }
     769            return(false);
     770          }
     771        }
    772772        // set diffRun.state to 'full'
    773773        if (!setdiffRunState(config, diff_id, "full", magicked)) {
     
    857857        }
    858858    }
    859            
     859
    860860    // treat limit == 0 as "no limit"
    861861    if (limit) {
     
    10921092            false,
    10931093            0,       // magicked
    1094             NULL, // software version
    1095             0,    // mask stat npix
    1096             NAN,    // static
    1097             NAN,    // dynamic
    1098             NAN,    // magic
    1099             NAN,    // advisory
     1094            NULL, // software version
     1095            0,    // mask stat npix
     1096            NAN,    // static
     1097            NAN,    // dynamic
     1098            NAN,    // magic
     1099            NAN,    // advisory
    11001100            diff_mode, // diff_mode
    11011101            note
     
    14751475        }
    14761476
    1477         psString warp_data_group = psMetadataLookupStr(&mdok, row, "data_group");
    1478         if (!mdok) {
    1479           psError(PXTOOLS_ERR_PROG, false, "warp data_group not found");
    1480           psFree(warp2Query);
    1481           psFree(stackQuery);
    1482           psFree(skycell_query);
    1483           if (!psDBRollback(config->dbh)) {
    1484             psError(PS_ERR_UNKNOWN, false, "database error");
    1485           }
    1486           return false;
    1487         }
    1488         if (!data_group && warp_data_group) {
    1489           data_group = warp_data_group;
    1490         }
    1491        
     1477        psString warp_data_group = psMetadataLookupStr(&mdok, row, "data_group");
     1478        if (!mdok) {
     1479          psError(PXTOOLS_ERR_PROG, false, "warp data_group not found");
     1480          psFree(warp2Query);
     1481          psFree(stackQuery);
     1482          psFree(skycell_query);
     1483          if (!psDBRollback(config->dbh)) {
     1484            psError(PS_ERR_UNKNOWN, false, "database error");
     1485          }
     1486          return false;
     1487        }
     1488        if (!data_group && warp_data_group) {
     1489          data_group = warp_data_group;
     1490        }
     1491
    14921492        if (!p_psDBRunQueryF(config->dbh, skycell_query, stackQuery, warp_id, filter, warp2Query)) {
    14931493            psError(PS_ERR_UNKNOWN, false, "database error");
     
    15151515        // ok we've got one create the diffRun
    15161516        diffRunRow *run = diffRunRowAlloc(
    1517                 0,            // ID
    1518                 "reg",        // state
     1517                0,            // ID
     1518                "reg",        // state
    15191519                workdir,
    15201520                label,
     
    15221522                dist_group,
    15231523                reduction,
    1524                 NULL,         // dvodb
     1524                NULL,         // dvodb
    15251525                registered,
    15261526                tess_id,
    15271527                bothways,     // bothways (default is false)
    1528                 true,         // exposure
    1529                 0,            // magicked
    1530                 NULL,         // software version
    1531                 0,            // mask stat npix
    1532                 NAN,          // static
    1533                 NAN,          // dynamic
    1534                 NAN,          // magic
    1535                 NAN,          // advisory
     1528                true,         // exposure
     1529                0,            // magicked
     1530                NULL,         // software version
     1531                0,            // mask stat npix
     1532                NAN,          // static
     1533                NAN,          // dynamic
     1534                NAN,          // magic
     1535                NAN,          // advisory
    15361536                IPP_DIFF_MODE_WARP_STACK,
    15371537                note
     
    15621562        psFree(query);
    15631563        query = NULL;
    1564         psStringAppend(&query, "INSERT INTO diffInputSkyfile SELECT * from skycellsToDiff");
     1564        psStringAppend(&query,
     1565                       "INSERT INTO diffInputSkyfile(diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id) SELECT diff_id, skycell_id, warp1, stack1, warp2, stack2, tess_id from skycellsToDiff");
    15651566        if (!p_psDBRunQuery(config->dbh, query)) {
    15661567            psError(PS_ERR_UNKNOWN, false, "database error");
     
    18681869        const char *template = psMetadataLookupStr(NULL, row, "template_warp_id");
    18691870        const char *tess_id = psMetadataLookupStr(NULL, row, "tess_id");
    1870         psString input_data_group = psMetadataLookupStr(NULL, row, "input_data_group");
    1871         if (!data_group) {
    1872           data_group = input_data_group;
    1873         }
     1871        psString input_data_group = psMetadataLookupStr(NULL, row, "input_data_group");
     1872        if (!data_group) {
     1873          data_group = input_data_group;
     1874        }
    18741875
    18751876        if (!input_id || !template || !tess_id) {
     
    18831884            return false;
    18841885        }
    1885        
     1886
    18861887        diffRunRow *run = diffRunRowAlloc(0,
    18871888                                          "reg",
     
    18971898                                          true,  // exposure
    18981899                                          false, // magicked
    1899                                           NULL, // software version
    1900                                           0,    // mask stat npix
    1901                                           NAN,    // static
    1902                                           NAN,    // dynamic
    1903                                           NAN,    // magic
    1904                                           NAN,    // advisory                                   
     1900                                          NULL, // software version
     1901                                          0,    // mask stat npix
     1902                                          NAN,    // static
     1903                                          NAN,    // dynamic
     1904                                          NAN,    // magic
     1905                                          NAN,    // advisory
    19051906                                          IPP_DIFF_MODE_WARP_WARP,
    19061907                                          note); // Run to insert
     
    19581959            // seem to be a way to pass in NULL for skycell_id, stack1 and stack2
    19591960            psString dummyQuery = NULL;
    1960             psStringAppend(&dummyQuery, 
     1961            psStringAppend(&dummyQuery,
    19611962              "INSERT INTO diffInputSkyfile VALUES(%" PRId64 ", NULL, %s, NULL, %s, NULL, '%s', 0)",
    1962                     run->diff_id, 
     1963                    run->diff_id,
    19631964                    input,                      // warp1
    19641965                    template,                   // warp2
     
    19801981            psFree(dummyQuery);
    19811982            psString finishQuery = NULL;
    1982             psStringAppend( &finishQuery, 
     1983            psStringAppend( &finishQuery,
    19831984                "UPDATE diffRun set state ='full', dist_group = NULL, note = 'dummy run - no overlap' WHERE diff_id = %" PRId64,
    19841985                run->diff_id);
     
    20632064
    20642065    if (!(label)) {
    2065         PXOPT_LOOKUP_STR(label,config->args, "-input_label",true,false);
     2066        PXOPT_LOOKUP_STR(label,config->args, "-input_label",true,false);
    20662067    }
    20672068
     
    20712072
    20722073    if (psListLength(stack1Where->list)) {
    2073         psString whereClause = psDBGenerateWhereConditionSQL(stack1Where, NULL);
    2074         psStringAppend(&stack1Query, "AND %s", whereClause);
    2075         psFree(whereClause);
    2076     } 
     2074        psString whereClause = psDBGenerateWhereConditionSQL(stack1Where, NULL);
     2075        psStringAppend(&stack1Query, "AND %s", whereClause);
     2076        psFree(whereClause);
     2077    }
    20772078    psFree(stack1Where);
    20782079
    20792080    if (psListLength(stack2Where->list)) {
    2080         psString whereClause = psDBGenerateWhereConditionSQL(stack2Where, NULL);
    2081         psStringAppend(&stack2Query, "AND %s", whereClause);
    2082         psFree(whereClause);
    2083     } 
     2081        psString whereClause = psDBGenerateWhereConditionSQL(stack2Where, NULL);
     2082        psStringAppend(&stack2Query, "AND %s", whereClause);
     2083        psFree(whereClause);
     2084    }
    20842085    psFree(stack2Where);
    2085  
     2086
    20862087    // don't queue for stacks that have already been diffed unless requested
    20872088    psString diffQuery0 = NULL;
    20882089    psString diffQuery1 = NULL;
    20892090    if (! (reRun || newTemplates) ) {
    2090         psStringAppend(&diffQuery0, "AND diffExp.diff_id IS NULL");
    2091         psStringAppend(&diffQuery1, "HAVING n_diff = 0");
    2092     } 
     2091        psStringAppend(&diffQuery0, "AND diffExp.diff_id IS NULL");
     2092        psStringAppend(&diffQuery1, "HAVING n_diff = 0");
     2093    }
    20932094
    20942095    // find the distinct set of data_groups and filters
    20952096    psString query = pxDataGet("difftool_definestackstack_part0.sql");
    20962097    if (!query) {
    2097         psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    2098         return(false);
     2098        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     2099        return(false);
    20992100    }
    21002101    psTrace("difftool",1,query,stack2Query,stack1Query,diffQuery0,stack1Query);
    21012102
    21022103    if (!psDBTransaction(config->dbh)) {
    2103         psError(PS_ERR_UNKNOWN, false, "database error");
    2104         return(false);
     2104        psError(PS_ERR_UNKNOWN, false, "database error");
     2105        return(false);
    21052106    }
    21062107
     
    21102111
    21112112    if (!p_psDBRunQuery(config->dbh, query)) {
    2112         psError(PS_ERR_UNKNOWN, false, "database error");
    2113         psFree(query);
    2114         if (!psDBRollback(config->dbh)) {
    2115             psError(PS_ERR_UNKNOWN, false, "database error");
    2116         }
    2117         return false;
     2113        psError(PS_ERR_UNKNOWN, false, "database error");
     2114        psFree(query);
     2115        if (!psDBRollback(config->dbh)) {
     2116            psError(PS_ERR_UNKNOWN, false, "database error");
     2117        }
     2118        return false;
    21182119    }
    21192120
     
    21222123    psArray *output = p_psDBFetchResult(config->dbh);
    21232124    if (!output) {
    2124         psErrorCode err = psErrorCodeLast();
    2125         switch (err) {
    2126           case PS_ERR_DB_CLIENT:
    2127             psError(PXTOOLS_ERR_SYS, false, "database error");
    2128             break;
    2129           case PS_ERR_DB_SERVER:
    2130             psError(PXTOOLS_ERR_PROG, false, "database error");
    2131             break;
    2132           default:
    2133             psError(PXTOOLS_ERR_PROG, false, "unknown error");
    2134             break;
    2135         }
    2136         if (!psDBRollback(config->dbh)) {
    2137             psError(PS_ERR_UNKNOWN, false, "database error");
    2138         }
    2139         return false;
     2125        psErrorCode err = psErrorCodeLast();
     2126        switch (err) {
     2127          case PS_ERR_DB_CLIENT:
     2128            psError(PXTOOLS_ERR_SYS, false, "database error");
     2129            break;
     2130          case PS_ERR_DB_SERVER:
     2131            psError(PXTOOLS_ERR_PROG, false, "database error");
     2132            break;
     2133          default:
     2134            psError(PXTOOLS_ERR_PROG, false, "unknown error");
     2135            break;
     2136        }
     2137        if (!psDBRollback(config->dbh)) {
     2138            psError(PS_ERR_UNKNOWN, false, "database error");
     2139        }
     2140        return false;
    21402141    }
    21412142    if (!psArrayLength(output)) {
    2142         psTrace("difftool", PS_LOG_INFO, "no rows found");
    2143         psFree(output);
    2144         if (!psDBCommit(config->dbh)) {
    2145             psError(PS_ERR_UNKNOWN, false, "database error");
    2146             return false;
    2147         }
    2148         return true;
    2149     }
    2150 
    2151    
     2143        psTrace("difftool", PS_LOG_INFO, "no rows found");
     2144        psFree(output);
     2145        if (!psDBCommit(config->dbh)) {
     2146            psError(PS_ERR_UNKNOWN, false, "database error");
     2147            return false;
     2148        }
     2149        return true;
     2150    }
     2151
     2152
    21522153    if (pretend) {
    2153         // negative simple so the default is true
    2154         if (!ippdbPrintMetadatas(stdout, output, "diffRunTemp", !simple)) {
    2155             psError(PS_ERR_UNKNOWN, false, "failed to print array");
    2156             psFree(output);
    2157             return false;
    2158         }
    2159         psFree(output);
    2160         return true;
     2154        // negative simple so the default is true
     2155        if (!ippdbPrintMetadatas(stdout, output, "diffRunTemp", !simple)) {
     2156            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     2157            psFree(output);
     2158            return false;
     2159        }
     2160        psFree(output);
     2161        return true;
    21612162    }
    21622163
     
    21652166      query = pxDataGet("difftool_definestackstack_part1.sql");
    21662167
    2167    
    2168         psMetadata *row = output->data[i]; // Output row from query
    2169         bool mdok;                      // Status of MD lookup
    2170 
    2171         // psMetadataPrint(stderr, row, 1);
    2172 
    2173         psString tess_id = psMetadataLookupStr(&mdok,row,"INPUT_tess_id");
    2174         psString this_data_group = psMetadataLookupStr(&mdok,row,"INPUT_data_group");
    2175         psString this_dist_group = psMetadataLookupStr(&mdok,row,"INPUT_dist_group");
    2176         psString this_label = psMetadataLookupStr(&mdok,row,"INPUT_label");
    2177 
    2178         psString this_stack1Query = psStringCopy(stack1Query);
    2179 
    2180         psString thisWhere = psDBGenerateWhereConditionSQL(row,NULL);
    2181         psStringSubstitute(&thisWhere,"stackRun.","INPUT_");
    2182         psStringAppend(&this_stack1Query,"AND %s", thisWhere);
    2183         psFree(thisWhere);
    2184 
    2185         psStringSubstitute(&query, this_stack1Query, "@STACK1_QUERY@");
    2186         psStringSubstitute(&query, stack2Query,      "@STACK2_QUERY@");
    2187         psStringSubstitute(&query, diffQuery1,       "@DIFF1_QUERY@");
    2188 
    2189         psTrace("difftool", 1, "%s", query);
    2190         if (!psDBTransaction(config->dbh)) {
    2191             psError(PS_ERR_UNKNOWN, false, "database error");
    2192             return(false);
    2193         }
    2194 
    2195         if (!p_psDBRunQuery(config->dbh, query)) {
    2196             psError(PS_ERR_UNKNOWN, false, "database error");
    2197             psFree(query);
    2198             if (!psDBRollback(config->dbh)) {
    2199                 psError(PS_ERR_UNKNOWN, false, "database error");
    2200             }
    2201             return false;
    2202         }
    2203         psFree(this_stack1Query);
    2204 
    2205         psArray *diff_id_output = p_psDBFetchResult(config->dbh);
    2206         if (!diff_id_output) {
    2207             psErrorCode err = psErrorCodeLast();
    2208             switch (err) {
    2209               case PS_ERR_DB_CLIENT:
    2210                 psError(PXTOOLS_ERR_SYS, false, "database error");
    2211                 break;
    2212               case PS_ERR_DB_SERVER:
    2213                 psError(PXTOOLS_ERR_PROG, false, "database error");
    2214                 break;
    2215               default:
    2216                 psError(PXTOOLS_ERR_PROG, false, "unknown error");
    2217                 break;
    2218             }
    2219             if (!psDBRollback(config->dbh)) {
    2220                 psError(PS_ERR_UNKNOWN, false, "database error");
    2221             }
    2222             return false;
    2223         }
    2224         if (!psArrayLength(diff_id_output)) {
    2225             psTrace("difftool", PS_LOG_INFO, "no rows found");
    2226             psFree(diff_id_output);
    2227             if (!psDBCommit(config->dbh)) {
    2228                 psError(PS_ERR_UNKNOWN, false, "database error");
    2229                 return false;
    2230             }
    2231             return true;
    2232         }
    2233 
    2234         // ok we've got one create the diffRun
    2235         diffRunRow *run = diffRunRowAlloc(
    2236             0,          // ID
    2237             "reg",      // state
    2238             workdir,
    2239             label ? label : this_label,
    2240             data_group ? data_group : this_data_group,
    2241             dist_group ? dist_group : this_dist_group,
    2242             reduction,
    2243             NULL,       // dvodb
    2244             registered,
    2245             tess_id,
    2246             bothways,               // bothways
    2247             false,                 // exposure
    2248             0,       // magicked
    2249             NULL, // software version
    2250             0,    // mask stat npix
    2251             NAN,    // static
    2252             NAN,    // dynamic
    2253             NAN,    // magic
    2254             NAN,    // advisory                             
    2255             IPP_DIFF_MODE_STACK_STACK, // diff_mode
    2256             note
    2257             );
    2258         // Commit to database
    2259         if (!diffRunInsertObject(config->dbh, run)) {
    2260             psError(PS_ERR_UNKNOWN, false, "database error");
    2261             psFree(run);
    2262             if (!psDBRollback(config->dbh)) {
    2263                 psError(PS_ERR_UNKNOWN, false, "database error");
    2264             }
    2265             return false;
    2266         }
    2267         // diffRunPrintObject(stdout,run,1);
    2268         run->diff_id = psDBLastInsertID(config->dbh);
    2269 
    2270         for (long j = 0; j < diff_id_output->n; j++) {
    2271             psMetadata *input_row = diff_id_output->data[j]; // Output row from query
    2272             bool mdok;
    2273             psString in_skycell_id  = psMetadataLookupStr(&mdok,input_row,"skycell_id");
    2274             psS64 in_input_stack_id = psMetadataLookupS64(&mdok,input_row,"stack_id");
    2275             psS64 in_template_stack_id = psMetadataLookupS64(&mdok,input_row,"max_stack_id");
    2276             psString in_tess_id = psMetadataLookupStr(&mdok,input_row,"tess_id");
    2277             psTrace("difftool",1,"%s %" PRId64 " %" PRId64 " %s\n",
    2278                     in_skycell_id, in_input_stack_id, in_template_stack_id, in_tess_id);
    2279             diffInputSkyfileRow *input = diffInputSkyfileRowAlloc(
    2280                 run->diff_id,   // ID
    2281                 in_skycell_id,
    2282                 PS_MAX_S64, // warp1_id -> NULL
    2283                 in_input_stack_id, // stack1
    2284                 PS_MAX_S64, // warp2_id -> NULL
    2285                 in_template_stack_id, // stack2
    2286                 in_tess_id,
    2287                 0 // diff_skyfile_id
    2288                 );
    2289 
    2290             // Commit to database the input
    2291             if (!diffInputSkyfileInsertObject(config->dbh, input)) {
    2292                 psError(PS_ERR_UNKNOWN, false, "database error");
    2293                 psFree(input);
    2294                 if (!psDBRollback(config->dbh)) {
    2295                     psError(PS_ERR_UNKNOWN, false, "database error");
    2296                 }
    2297                 return false;
    2298             }
     2168
     2169        psMetadata *row = output->data[i]; // Output row from query
     2170        bool mdok;                      // Status of MD lookup
     2171
     2172        // psMetadataPrint(stderr, row, 1);
     2173
     2174        psString tess_id = psMetadataLookupStr(&mdok,row,"INPUT_tess_id");
     2175        psString this_data_group = psMetadataLookupStr(&mdok,row,"INPUT_data_group");
     2176        psString this_dist_group = psMetadataLookupStr(&mdok,row,"INPUT_dist_group");
     2177        psString this_label = psMetadataLookupStr(&mdok,row,"INPUT_label");
     2178
     2179        psString this_stack1Query = psStringCopy(stack1Query);
     2180
     2181        psString thisWhere = psDBGenerateWhereConditionSQL(row,NULL);
     2182        psStringSubstitute(&thisWhere,"stackRun.","INPUT_");
     2183        psStringAppend(&this_stack1Query,"AND %s", thisWhere);
     2184        psFree(thisWhere);
     2185
     2186        psStringSubstitute(&query, this_stack1Query, "@STACK1_QUERY@");
     2187        psStringSubstitute(&query, stack2Query,      "@STACK2_QUERY@");
     2188        psStringSubstitute(&query, diffQuery1,       "@DIFF1_QUERY@");
     2189
     2190        psTrace("difftool", 1, "%s", query);
     2191        if (!psDBTransaction(config->dbh)) {
     2192            psError(PS_ERR_UNKNOWN, false, "database error");
     2193            return(false);
     2194        }
     2195
     2196        if (!p_psDBRunQuery(config->dbh, query)) {
     2197            psError(PS_ERR_UNKNOWN, false, "database error");
     2198            psFree(query);
     2199            if (!psDBRollback(config->dbh)) {
     2200                psError(PS_ERR_UNKNOWN, false, "database error");
     2201            }
     2202            return false;
     2203        }
     2204        psFree(this_stack1Query);
     2205
     2206        psArray *diff_id_output = p_psDBFetchResult(config->dbh);
     2207        if (!diff_id_output) {
     2208            psErrorCode err = psErrorCodeLast();
     2209            switch (err) {
     2210              case PS_ERR_DB_CLIENT:
     2211                psError(PXTOOLS_ERR_SYS, false, "database error");
     2212                break;
     2213              case PS_ERR_DB_SERVER:
     2214                psError(PXTOOLS_ERR_PROG, false, "database error");
     2215                break;
     2216              default:
     2217                psError(PXTOOLS_ERR_PROG, false, "unknown error");
     2218                break;
     2219            }
     2220            if (!psDBRollback(config->dbh)) {
     2221                psError(PS_ERR_UNKNOWN, false, "database error");
     2222            }
     2223            return false;
     2224        }
     2225        if (!psArrayLength(diff_id_output)) {
     2226            psTrace("difftool", PS_LOG_INFO, "no rows found");
     2227            psFree(diff_id_output);
     2228            if (!psDBCommit(config->dbh)) {
     2229                psError(PS_ERR_UNKNOWN, false, "database error");
     2230                return false;
     2231            }
     2232            return true;
     2233        }
     2234
     2235        // ok we've got one create the diffRun
     2236        diffRunRow *run = diffRunRowAlloc(
     2237            0,          // ID
     2238            "reg",      // state
     2239            workdir,
     2240            label ? label : this_label,
     2241            data_group ? data_group : this_data_group,
     2242            dist_group ? dist_group : this_dist_group,
     2243            reduction,
     2244            NULL,       // dvodb
     2245            registered,
     2246            tess_id,
     2247            bothways,               // bothways
     2248            false,                 // exposure
     2249            0,       // magicked
     2250            NULL, // software version
     2251            0,    // mask stat npix
     2252            NAN,    // static
     2253            NAN,    // dynamic
     2254            NAN,    // magic
     2255            NAN,    // advisory
     2256            IPP_DIFF_MODE_STACK_STACK, // diff_mode
     2257            note
     2258            );
     2259        // Commit to database
     2260        if (!diffRunInsertObject(config->dbh, run)) {
     2261            psError(PS_ERR_UNKNOWN, false, "database error");
     2262            psFree(run);
     2263            if (!psDBRollback(config->dbh)) {
     2264                psError(PS_ERR_UNKNOWN, false, "database error");
     2265            }
     2266            return false;
     2267        }
     2268        // diffRunPrintObject(stdout,run,1);
     2269        run->diff_id = psDBLastInsertID(config->dbh);
     2270
     2271        for (long j = 0; j < diff_id_output->n; j++) {
     2272            psMetadata *input_row = diff_id_output->data[j]; // Output row from query
     2273            bool mdok;
     2274            psString in_skycell_id  = psMetadataLookupStr(&mdok,input_row,"skycell_id");
     2275            psS64 in_input_stack_id = psMetadataLookupS64(&mdok,input_row,"stack_id");
     2276            psS64 in_template_stack_id = psMetadataLookupS64(&mdok,input_row,"max_stack_id");
     2277            psString in_tess_id = psMetadataLookupStr(&mdok,input_row,"tess_id");
     2278            psTrace("difftool",1,"%s %" PRId64 " %" PRId64 " %s\n",
     2279                    in_skycell_id, in_input_stack_id, in_template_stack_id, in_tess_id);
     2280            diffInputSkyfileRow *input = diffInputSkyfileRowAlloc(
     2281                run->diff_id,   // ID
     2282                in_skycell_id,
     2283                PS_MAX_S64, // warp1_id -> NULL
     2284                in_input_stack_id, // stack1
     2285                PS_MAX_S64, // warp2_id -> NULL
     2286                in_template_stack_id, // stack2
     2287                in_tess_id,
     2288                0 // diff_skyfile_id
     2289                );
     2290
     2291            // Commit to database the input
     2292            if (!diffInputSkyfileInsertObject(config->dbh, input)) {
     2293                psError(PS_ERR_UNKNOWN, false, "database error");
     2294                psFree(input);
     2295                if (!psDBRollback(config->dbh)) {
     2296                    psError(PS_ERR_UNKNOWN, false, "database error");
     2297                }
     2298                return false;
     2299            }
    22992300
    23002301/*       diffInputSkyfilePrintObject(stdout,input,1); */
    2301             psFree(input);
    2302         }
    2303 
    2304         if (!setdiffRunState(config, run->diff_id, "new", false)) {
    2305             psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, run->diff_id);
    2306             psFree(stack1Query);
    2307             psFree(stack2Query);
    2308             psFree(diffQuery0);
    2309             psFree(diffQuery1);
    2310             psFree(run);
    2311             psFree(list);
    2312             if (!psDBRollback(config->dbh)) {
    2313                 psError(PS_ERR_UNKNOWN, false, "database error");
    2314             }
    2315             return false;
    2316         }
    2317         psArrayAdd(list, list->n, run);
    2318         psFree(run);
     2302            psFree(input);
     2303        }
     2304
     2305        if (!setdiffRunState(config, run->diff_id, "new", false)) {
     2306            psError(PS_ERR_UNKNOWN, false, "failed to change diffRun.state for diff_id: %" PRId64, run->diff_id);
     2307            psFree(stack1Query);
     2308            psFree(stack2Query);
     2309            psFree(diffQuery0);
     2310            psFree(diffQuery1);
     2311            psFree(run);
     2312            psFree(list);
     2313            if (!psDBRollback(config->dbh)) {
     2314                psError(PS_ERR_UNKNOWN, false, "database error");
     2315            }
     2316            return false;
     2317        }
     2318        psArrayAdd(list, list->n, run);
     2319        psFree(run);
    23192320    }
    23202321
    23212322    if (!psDBCommit(config->dbh)) {
    2322         psError(PS_ERR_UNKNOWN, false, "database error");
    2323         psFree(list);
    2324         return false;
     2323        psError(PS_ERR_UNKNOWN, false, "database error");
     2324        psFree(list);
     2325        return false;
    23252326    }
    23262327
    23272328    if (!diffRunPrintObjects(stdout, list, !simple)) {
    2328         psError(PS_ERR_UNKNOWN, false, "failed to print object");
    2329         psFree(list);
    2330         return false;
     2329        psError(PS_ERR_UNKNOWN, false, "failed to print object");
     2330        psFree(list);
     2331        return false;
    23312332    }
    23322333
     
    23402341
    23412342    if (!psDBCommit(config->dbh)) {
    2342         psError(PS_ERR_UNKNOWN, false, "database error");
    2343         psFree(list);
    2344         return false;
     2343        psError(PS_ERR_UNKNOWN, false, "database error");
     2344        psFree(list);
     2345        return false;
    23452346    }
    23462347
     
    23502351static bool tosummaryMode(pxConfig *config) {
    23512352  PS_ASSERT_PTR_NON_NULL(config, NULL);
    2352  
     2353
    23532354  psMetadata *where = psMetadataAlloc();
    23542355  PXOPT_COPY_S64(config->args, where, "-warp_id",    "diffSkyfile.warp_id", "==");
     
    23662367  PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    23672368  PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    2368  
     2369
    23692370  // find all rawImfiles matching the default query
    23702371  psString query = pxDataGet("difftool_tosummary.sql");
     
    23812382    return false;
    23822383  }
    2383  
     2384
    23842385  if (psListLength(where->list)) {
    23852386    psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     
    23902391    return false;
    23912392  }
    2392  
     2393
    23932394  if (where2) {
    23942395    if (psListLength(where->list)) {
     
    24062407    psFree(limitString);
    24072408  }
    2408  
     2409
    24092410  if (!p_psDBRunQuery(config->dbh, query)) {
    24102411    psError(PS_ERR_UNKNOWN, false, "database error");
     
    24252426      psError(PXTOOLS_ERR_PROG, false, "unknown error");
    24262427    }
    2427    
     2428
    24282429    return false;
    24292430  }
     
    24332434    return true;
    24342435  }
    2435  
     2436
    24362437  if (psArrayLength(output)) {
    24372438    // negative simple so the default is true
     
    24422443    }
    24432444  }
    2444  
     2445
    24452446  psFree(output);
    24462447  return(true);
     
    24642465  }
    24652466  psS64 numUpdated = psDBAffectedRows(config->dbh);
    2466  
     2467
    24672468  if (numUpdated != 1) {
    24682469    psError(PS_ERR_UNKNOWN, false, "should have affected 1 row");
     
    24702471    return false;
    24712472  }
    2472  
     2473
    24732474  psFree(query);
    24742475
    24752476  // Print anything here?
    2476  
     2477
    24772478  return(true);
    24782479}
     
    25142515        psString limitString = psDBGenerateLimitSQL(limit);
    25152516        psStringAppend(&query, " %s", limitString);
    2516         psStringSubstitute(&query,limitString,"@INNERLIMITS@");
     2517        psStringSubstitute(&query,limitString,"@INNERLIMITS@");
    25172518        psFree(limitString);
    25182519
     
    30283029
    30293030    PXOPT_COPY_S64(config->args, where, "-template_exp_id", "rawTemplate.exp_id", "==");
    3030    
     3031
    30313032    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
    30323033    if (!template) {
     
    30833084        }
    30843085    }
    3085            
     3086
    30863087    // treat limit == 0 as "no limit"
    30873088    if (limit) {
     
    31483149    PXOPT_LOOKUP_F64(mjd_obs_begin, config->args, "-mjd_obs_begin", false, false);
    31493150    PXOPT_LOOKUP_F64(mjd_obs_end, config->args, "-mjd_obs_end", false, false);
    3150    
     3151
    31513152    PXOPT_LOOKUP_BOOL(template, config->args, "-template", false);
    31523153
     
    32003201        }
    32013202    }
    3202            
     3203
    32033204    // treat limit == 0 as "no limit"
    32043205    if (limit) {
Note: See TracChangeset for help on using the changeset viewer.