IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 11, 2007, 3:38:56 PM (19 years ago)
Author:
jhoblitt
Message:

require ippdb 0.0.73
merge rawScienceExp & rawDetrnedExp -> rawExp table
p0tool fault handling refactoring:

rename -updateimfile -> -addprocessedimfile
rename -updatexp -> -addprocessedexp
rename -faultimfile -> -updatedprocessedimfile
rename -faultexp -> -updateprocessedexp
rename -rawifmile -> -processedimfile
add -processedexp
remove newExp.fault & newImfile.fault
add rawImfile.fault & rawExp.fault

remove pxframes.c from the build and the related prototypes from pxtools.h

File:
1 edited

Legend:

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

    r11037 r11047  
    2727#include "p0tool.h"
    2828
     29static bool pendingimfileMode(pxConfig *config);
     30static bool addprocessedimfileMode(pxConfig *config);
     31static bool processedimfileMode(pxConfig *config);
     32static bool updateprocessedimfileMode(pxConfig *config);
     33
    2934static bool pendingexpMode(pxConfig *config);
    30 static bool pendingimfileMode(pxConfig *config);
    31 static bool updateexpMode(pxConfig *config);
    32 static bool updateimfileMode(pxConfig *config);
    33 static bool faultexpMode(pxConfig *config);
    34 static bool faultimfileMode(pxConfig *config);
    35 static bool rawimfileMode(pxConfig *config);
     35static bool addprocessedexpMode(pxConfig *config);
     36static bool processedexpMode(pxConfig *config);
     37static bool updateprocessedexpMode(pxConfig *config);
     38
     39
    3640// static p1PendingExpRow *newToP1PendingExp(newExpRow *newExp);
    3741static p2PendingExpRow *newToP2PendingExp(pxConfig *config, newExpRow *newExp);
    3842static p2PendingImfileRow *rawImfileToP2PendingImfile(pxConfig *config, rawImfileRow *rawImfile);
    39 static rawScienceExpRow *newToRawScienceExp(pxConfig *config, newExpRow *exp);
    40 static rawDetrendExpRow *newToRawDetrendExp(pxConfig *config, newExpRow *exp);
     43
     44static rawExpRow *newToRawExp(pxConfig *config, newExpRow *exp);
    4145static rawImfileRow *newToRawImfile(pxConfig *config, newImfileRow *exp);
    4246//static psU32 mapCodeStrToInt(const char *codeStr);
     
    5963
    6064    switch (config->mode) {
    61         MODECASE(P0TOOL_MODE_PENDINGEXP,        pendingexpMode);
    62         MODECASE(P0TOOL_MODE_PENDINGIMFILE,     pendingimfileMode);
    63         MODECASE(P0TOOL_MODE_UPDATEEXP,         updateexpMode);
    64         MODECASE(P0TOOL_MODE_FAULTEXP,          faultexpMode);
    65         MODECASE(P0TOOL_MODE_UPDATEIMFILE,      updateimfileMode);
    66         MODECASE(P0TOOL_MODE_FAULTIMFILE,       faultimfileMode);
    67         MODECASE(P0TOOL_MODE_RAWIMFILE,         rawimfileMode);
     65        MODECASE(P0TOOL_MODE_PENDINGIMFILE,         pendingimfileMode);
     66        MODECASE(P0TOOL_MODE_ADDPROCESSEDIMFILE,    addprocessedimfileMode);
     67        MODECASE(P0TOOL_MODE_PROCESSEDIMFILE,       processedimfileMode);
     68        MODECASE(P0TOOL_MODE_UPDATEPROCESSEDIMFILE, updateprocessedimfileMode);
     69        MODECASE(P0TOOL_MODE_PENDINGEXP,            pendingexpMode);
     70        MODECASE(P0TOOL_MODE_ADDPROCESSEDEXP,       addprocessedexpMode);
     71        MODECASE(P0TOOL_MODE_PROCESSEDEXP,          processedexpMode);
     72        MODECASE(P0TOOL_MODE_UPDATEPROCESSEDEXP,    updateprocessedexpMode);
    6873        default:
    6974            psAbort(argv[0], "invalid option (this should not happen)");
     
    8893}
    8994
    90 static bool pendingexpMode(pxConfig *config)
     95
     96static bool pendingimfileMode(pxConfig *config)
    9197{
    9298    PS_ASSERT_PTR_NON_NULL(config, false);
     
    99105    }
    100106
     107    // select newImfiles that:
     108    // exp_tag is in newExp
     109    // don't have their exp_tag in rawExp
     110    // XXX having the same exp_tag in newExp and raw*Exp is probably an error
     111    // that should be checked for
     112
     113    psString query = psStringCopy(
     114        "SELECT\n"
     115        "   newImfile.*\n"
     116        " FROM newImfile\n"
     117        " LEFT JOIN newExp\n"
     118        "   USING(exp_tag)\n"
     119        " LEFT JOIN rawExp\n"
     120        "   USING(exp_tag)\n"
     121        " WHERE\n"
     122        "   newExp.exp_tag is NOT NULL\n"
     123        "   AND rawExp.exp_tag IS NULL\n"
     124    );
     125
     126    // treat limit == 0 as "no limit"
     127    if (limit) {
     128        psString limitString = psDBGenerateLimitSQL(limit);
     129        psStringAppend(&query, " %s", limitString);
     130        psFree(limitString);
     131    }
     132
     133    if (!p_psDBRunQuery(config->dbh, query)) {
     134        // XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
     135        psError(PXTOOLS_ERR_PROG, false, "database error");
     136        psFree(query);
     137        return false;
     138    }
     139    psFree(query);
     140
     141    psArray *output = p_psDBFetchResult(config->dbh);
     142    if (!output) {
     143        // XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
     144        psError(PXTOOLS_ERR_PROG, false, "database error");
     145        return false;
     146    }
     147    if (!psArrayLength(output)) {
     148        psFree(output);
     149        return true;
     150    }
     151
     152    bool simple = false;
     153    simple = psMetadataLookupBool(&status, config->args, "-simple");
     154    if (!status) {
     155        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -simple");
     156        return false;
     157    }
     158
     159    // negate simple so the default is true
     160    if (!ippdbPrintMetadatas(stdout, output, "p0PendingImfile", !simple)) {
     161        psError(PXTOOLS_ERR_PROG, false, "failed to print array");
     162        psFree(output);
     163        return false;
     164    }
     165
     166    psFree(output);
     167
     168    return true;
     169}
     170
     171
     172static bool addprocessedimfileMode(pxConfig *config)
     173{
     174    PS_ASSERT_PTR_NON_NULL(config, false);
     175
     176    // XXX search by the whole frame some imfiles without a newExp don't get
     177    // processed -- this may not be the correct thing to do
     178    psString query = psStringCopy(
     179        "SELECT\n"
     180        "   *\n"
     181        " FROM\n"
     182        "   (SELECT newImfile.* FROM newImfile\n"
     183        "       LEFT JOIN newExp USING(exp_tag)\n"
     184        "       LEFT JOIN rawExp USING(exp_tag)\n"
     185        "       WHERE newExp.exp_tag IS NOT NULL\n"
     186        "       AND rawExp.exp_tag IS NULL) as Foo\n"
     187        ); // WHERE class is generated from exp_tag, class, & class_id
     188
     189    {
     190        // build a query to search by exp_tag, class, class_id
     191        psMetadata *where = psMetadataAlloc();
     192        bool status = false;
     193        psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
     194        if (!status) {
     195            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
     196            psFree(query);
     197            return false;
     198        }
     199        if (exp_tag) {
     200            if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_tag", 0, "==", exp_tag)) {
     201                psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
     202                psFree(where);
     203                psFree(query);
     204                return false;
     205            }
     206        }
     207        psString class = psMetadataLookupStr(&status, config->args, "-class");
     208        if (!status) {
     209            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class");
     210            psFree(query);
     211            return false;
     212        }
     213        if (class) {
     214            if (!psMetadataAddStr(where, PS_LIST_TAIL, "class", 0, "==", class)) {
     215                psError(PS_ERR_UNKNOWN, false, "failed to add item class");
     216                psFree(where);
     217                psFree(query);
     218                return false;
     219            }
     220        }
     221        psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
     222        if (!status) {
     223            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
     224            psFree(query);
     225            return false;
     226        }
     227        if (class_id) {
     228            if (!psMetadataAddStr(where, PS_LIST_TAIL, "class_id", 0, "==", class_id)) {
     229                psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
     230                psFree(where);
     231                psFree(query);
     232                return false;
     233            }
     234        }
     235
     236        // there's not
     237        psString whereClaus = psDBGenerateWhereSQL(where, NULL);
     238        psFree(where);
     239        if (whereClaus) {
     240            psStringAppend(&query, " %s", whereClaus);
     241            psFree(whereClaus);
     242        }
     243    }
     244
     245    if (!p_psDBRunQuery(config->dbh, query)) {
     246        psError(PS_ERR_UNKNOWN, false, "database error");
     247        psFree(query);
     248        return false;
     249    }
     250    psFree(query);
     251
     252    psArray *output = p_psDBFetchResult(config->dbh);
     253    if (!output) {
     254        psError(PS_ERR_UNKNOWN, false, "database error");
     255        return false;
     256    }
     257    if (!psArrayLength(output)) {
     258        // XXX check psError here
     259        psError(PS_ERR_UNKNOWN, false, "no pending newImfile rows found");
     260        psFree(output);
     261        return false;
     262    }
     263
     264    // insert 'newImfile's into rawImfile
     265    if (psArrayLength(output) > 0) {
     266        // start a transaction so we don't end up half of the imfiles we were
     267        // trying to update uninserted
     268        if (!psDBTransaction(config->dbh)) {
     269            psError(PS_ERR_UNKNOWN, false, "database error");
     270            psFree(output);
     271            return false;
     272        }
     273
     274        for (long i = 0; i < psArrayLength(output); i++) {
     275            // convert newImfile metadata -> newImfile object
     276            newImfileRow *object = newImfileObjectFromMetadata(output->data[i]);
     277            // convert newImfile object -> rawImfile object
     278            rawImfileRow *imfile = newToRawImfile(config, object);
     279            if (!imfile) {
     280                // rollback
     281                if (!psDBRollback(config->dbh)) {
     282                    psError(PS_ERR_UNKNOWN, false, "database error");
     283                }
     284                psError(PS_ERR_UNKNOWN, false, "failed to create a new rawImfile row");
     285                psFree(object);
     286                psFree(output);
     287                return false;
     288            }
     289            // insert the rawImfile object into the database
     290            if (!rawImfileInsertObject(config->dbh, imfile)) {
     291                // rollback
     292                if (!psDBRollback(config->dbh)) {
     293                    psError(PS_ERR_UNKNOWN, false, "database error");
     294                }
     295                psError(PS_ERR_UNKNOWN, false, "failed to insert row into the database");
     296                psFree(imfile);
     297                psFree(object);
     298                psFree(output);
     299                return false;
     300            }
     301            psFree(imfile);
     302            // remove the neImfile object from the database
     303            if (!newImfileDeleteObject(config->dbh, object)) {
     304                // rollback
     305                if (!psDBRollback(config->dbh)) {
     306                    psError(PS_ERR_UNKNOWN, false, "database error");
     307                }
     308                psError(PS_ERR_UNKNOWN, false, "failed to delete row from the database");
     309                psFree(object);
     310                psFree(output);
     311                return false;
     312            }
     313            psFree(object);
     314        }
     315
     316        // point of no return for rawImfile
     317        if (!psDBCommit(config->dbh)) {
     318            psError(PS_ERR_UNKNOWN, false, "database error");
     319            psFree(output);
     320            return false;
     321        }
     322    }
     323
     324    psFree(output);
     325
     326    return true;
     327}
     328
     329
     330static bool processedimfileMode(pxConfig *config)
     331{
     332    PS_ASSERT_PTR_NON_NULL(config, false);
     333
     334    bool status = false;
     335    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
     336    if (!status) {
     337        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
     338        return false;
     339    }
     340
    101341    bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
    102342    if (!status) {
     
    105345    }
    106346
     347    // find all rawImfiles matching the default query
     348    psString query = psStringCopy(
     349        "SELECT\n"
     350        "   *\n"
     351        " FROM rawImfile\n"
     352        " WHERE rawImfile.exp_tag is NOT NULL\n" //bogus conditional so there is a where clause to append to
     353    );
     354
     355    if (config->where) {
     356        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawImfile");
     357        psStringAppend(&query, " AND %s", whereClause);
     358        psFree(whereClause);
     359    }
     360
     361    if (faulted) {
     362        // list only faulted rows
     363        psStringAppend(&query, " %s", "AND rawImfile.fault != 0");
     364    } else {
     365        // don't list faulted rows
     366        psStringAppend(&query, " %s", "AND rawImfile.fault = 0");
     367    }
     368
     369    // treat limit == 0 as "no limit"
     370    if (limit) {
     371        psString limitString = psDBGenerateLimitSQL(limit);
     372        psStringAppend(&query, " %s", limitString);
     373        psFree(limitString);
     374    }
     375
     376    if (!p_psDBRunQuery(config->dbh, query)) {
     377        psError(PS_ERR_UNKNOWN, false, "database error");
     378        psFree(query);
     379        return false;
     380    }
     381    psFree(query);
     382
     383    psArray *output = p_psDBFetchResult(config->dbh);
     384    if (!output) {
     385        psError(PS_ERR_UNKNOWN, false, "database error");
     386        return false;
     387    }
     388    if (!psArrayLength(output)) {
     389        psError(PS_ERR_UNKNOWN, false, "no pending rawImfile rows found");
     390        psFree(output);
     391        return true;
     392    }
     393
     394    bool simple = false;
     395    {
     396        bool status = false;
     397        simple = psMetadataLookupBool(&status, config->args, "-simple");
     398        if (!status) {
     399            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -simple");
     400            return false;
     401        }
     402    }
     403
     404    if (psArrayLength(output)) {
     405        // negative simple so the default is true
     406        if (!ippdbPrintMetadatas(stdout, output, "rawImfile", !simple)) {
     407            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     408            psFree(output);
     409            return false;
     410        }
     411    }
     412
     413    psFree(output);
     414
     415    return true;
     416}
     417
     418
     419static bool updateprocessedimfileMode(pxConfig *config)
     420{
     421    PS_ASSERT_PTR_NON_NULL(config, false);
     422
     423    bool status = false;
     424    psS8 code = psMetadataLookupS8(&status, config->args, "-code");
     425    if (!status) {
     426        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
     427        return false;
     428    }
     429
     430    if (!pxSetFaultCode(config->dbh, "rawImfile", config->where, code)) {
     431        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
     432        return false;
     433    }
     434
     435    return true;
     436}
     437
     438
     439static bool pendingexpMode(pxConfig *config)
     440{
     441    PS_ASSERT_PTR_NON_NULL(config, false);
     442
     443    bool status = false;
     444    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
     445    if (!status) {
     446        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
     447        return false;
     448    }
     449
    107450    // return only exps that:
    108     // are not in rawScienceExp
    109     // are not in rawDetrendExp
     451    // are not in rawExp
    110452    // have ALL of their imfiles in rawImfile (by count)
    111453    // and have no associated imfiles left in newImfile
     
    117459        " LEFT JOIN newImfile"
    118460        "   USING(exp_tag)"
    119         " LEFT JOIN rawScienceExp"
    120         "   USING(exp_tag)"
    121         " LEFT JOIN rawDetrendExp"
     461        " LEFT JOIN rawExp"
    122462        "   USING(exp_tag)"
    123463        " WHERE"
    124464        "   newImfile.exp_tag IS NULL"
    125         "   AND rawScienceExp.exp_tag IS NULL"
    126         "   AND rawDetrendExp.exp_tag IS NULL"
     465        "   AND rawExp.exp_tag IS NULL"
    127466        "   AND newExp.imfiles ="
    128467        "   (SELECT COUNT(exp_tag) FROM rawImfile"
    129468        "       WHERE rawImfile.exp_tag = newExp.exp_tag)"
    130469    );
    131 
    132     if (faulted) {
    133         // list only faulted rows
    134         psStringAppend(&query, " %s", "AND newExp.fault != 0");
    135     } else {
    136         // don't list faulted rows
    137         psStringAppend(&query, " %s", "AND newExp.fault = 0");
    138     }
    139470
    140471    // treat limit == 0 as "no limit"
     
    183514}
    184515
    185 static bool pendingimfileMode(pxConfig *config)
     516
     517static bool addprocessedexpMode(pxConfig *config)
    186518{
    187519    PS_ASSERT_PTR_NON_NULL(config, false);
    188520
    189     bool status = false;
    190     psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
    191     if (!status) {
    192         psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
    193         return false;
    194     }
    195 
    196     bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
    197     if (!status) {
    198         psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -faulted");
    199         return false;
    200     }
    201 
    202     // select newImfiles that:
    203     // exp_tag is in newExp
    204     // don't have their exp_tag in rawScienceExp
    205     // don't have their exp_tag in rawDetrendExp
    206     // XXX having the same exp_tag in newExp and raw*Exp is probably an error
    207     // that should be checked for
    208 
    209     psString query = psStringCopy(
    210         "SELECT newImfile.* FROM newImfile"
    211         " LEFT JOIN newExp USING(exp_tag)"
    212         " LEFT JOIN rawScienceExp USING(exp_tag)"
    213         " LEFT JOIN rawDetrendExp USING (exp_tag)"
    214         " WHERE newExp.exp_tag is NOT NULL"
    215         " AND rawScienceExp.exp_tag IS NULL"
    216         " AND rawDetrendExp.exp_tag IS NULL"
    217     );
    218 
    219     if (faulted) {
    220         // list only faulted rows
    221         psStringAppend(&query, " %s", "AND newImfile.fault != 0");
    222     } else {
    223         // don't list faulted rows
    224         psStringAppend(&query, " %s", "AND newImfile.fault = 0");
    225     }
    226 
    227     // treat limit == 0 as "no limit"
    228     if (limit) {
    229         psString limitString = psDBGenerateLimitSQL(limit);
    230         psStringAppend(&query, " %s", limitString);
    231         psFree(limitString);
    232     }
    233 
    234     if (!p_psDBRunQuery(config->dbh, query)) {
    235         // XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
    236         psError(PXTOOLS_ERR_PROG, false, "database error");
    237         psFree(query);
    238         return false;
    239     }
    240     psFree(query);
    241 
    242     psArray *output = p_psDBFetchResult(config->dbh);
    243     if (!output) {
    244         // XXX PS_EXIT_PROG_ERROR (incorrect SQL) or SYS_ERROR (database comms)
    245         psError(PXTOOLS_ERR_PROG, false, "database error");
    246         return false;
    247     }
    248     if (!psArrayLength(output)) {
    249         psFree(output);
    250         return true;
    251     }
    252 
    253     bool simple = false;
    254     simple = psMetadataLookupBool(&status, config->args, "-simple");
    255     if (!status) {
    256         psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -simple");
    257         return false;
    258     }
    259 
    260     // negate simple so the default is true
    261     if (!ippdbPrintMetadatas(stdout, output, "newImfile", !simple)) {
    262         psError(PXTOOLS_ERR_PROG, false, "failed to print array");
    263         psFree(output);
    264         return false;
    265     }
    266 
    267     psFree(output);
    268 
    269     return true;
    270 }
    271 
    272 static bool faultexpMode(pxConfig *config)
    273 {
    274     bool status = false;
    275     psS8 code = psMetadataLookupS8(&status, config->args, "-code");
    276     if (!status) {
    277         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
    278         return false;
    279     }
    280 
    281     if (!pxSetFaultCode(config->dbh, "newExp", config->where, code)) {
    282         psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    283         return false;
    284     }
    285 
    286     return true;
    287 }
    288 
    289 static bool faultimfileMode(pxConfig *config)
    290 {
    291     PS_ASSERT_PTR_NON_NULL(config, false);
    292 
    293     bool status = false;
    294     psS8 code = psMetadataLookupS8(&status, config->args, "-code");
    295     if (!status) {
    296         psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
    297         return false;
    298     }
    299 
    300     if (!pxSetFaultCode(config->dbh, "newImfile", config->where, code)) {
    301         psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
    302         return false;
    303     }
    304 
    305     return true;
    306 }
    307 
    308 
    309 static bool updateexpMode(pxConfig *config)
    310 {
    311     PS_ASSERT_PTR_NON_NULL(config, false);
    312 
    313521    // make sure that the exp_tag(s) are ready to be updated based on:
    314     // exp_tag is not in rawScienceExp
    315     // exp_tag is not in rawDetrendExp
     522    // exp_tag is not in rawExp
    316523    // exp_tag is not in newImfile
    317524    // that the correct count of imfiles is in rawImfile
     
    340547        " LEFT JOIN newImfile"
    341548        "   USING(exp_tag)"
    342         " LEFT JOIN rawScienceExp"
    343         "   USING(exp_tag)"
    344         " LEFT JOIN rawDetrendExp"
     549        " LEFT JOIN rawExp"
    345550        "   USING(exp_tag)"
    346551        " WHERE"
    347552        "   newExp.exp_tag IS NOT NULL"
    348553        "   AND newImfile.exp_tag IS NULL"
    349         "   AND rawScienceExp.exp_tag IS NULL"
     554        "   AND rawExp.exp_tag IS NULL"
    350555        "   AND newExp.imfiles ="
    351556        "   (SELECT COUNT(exp_tag) FROM rawImfile"
     
    369574    }
    370575
    371 
    372     // start a transaction so we don't end up with an exp in
    373     // rawScience/DetrendExp and in newExp
     576    // start a transaction so we don't end up with an exp in both rawExp &
     577    // newExp
    374578    if (!psDBTransaction(config->dbh)) {
    375579        psError(PS_ERR_UNKNOWN, false, "database error");
     
    378582    }
    379583
    380     // if it's a detrend exp
    381     if (detrend) {       
    382         for (long i = 0; psArrayLength(output) > i; i++) {
    383             psMetadata *row = output->data[i];
    384             // convert metadata into a newExp object
    385             newExpRow *newExp = newExpObjectFromMetadata(row);
    386             // convert newExp object into a rawDetrendExp object
    387             rawDetrendExpRow *rawExp = newToRawDetrendExp(config, newExp);
    388             if (!rawExp) {
    389                 // rollback
    390                 if (!psDBRollback(config->dbh)) {
    391                     psError(PS_ERR_UNKNOWN, false, "database error");
    392                 }
    393                 psError(PS_ERR_UNKNOWN, false, "failed to convert newExp to rawDetrendExp");
    394                 psFree(newExp);
    395                 psFree(output);
    396                 return false;
    397             }
    398             // insert the rawDetrendExp object into the database
    399             if (!rawDetrendExpInsertObject(config->dbh, rawExp)) {
    400                 // rollback
    401                 if (!psDBRollback(config->dbh)) {
    402                     psError(PS_ERR_UNKNOWN, false, "database error");
    403                 }
    404                 psError(PS_ERR_UNKNOWN, false, "database error");
    405                 psFree(rawExp);
    406                 psFree(newExp);
    407                 psFree(output);
    408                 return false;
    409             }
    410             psFree(rawExp);
    411             // delete the newExp object from the database
    412             if (!newExpDeleteObject(config->dbh, newExp)) {
    413                 // rollback
    414                 if (!psDBRollback(config->dbh)) {
    415                     psError(PS_ERR_UNKNOWN, false, "database error");
    416                 }
    417                 psError(PS_ERR_UNKNOWN, false, "database error");
    418                 psFree(newExp);
    419                 psFree(output);
    420                 return false;
    421             }
    422             psFree(newExp);
    423         }
    424 
    425         psFree(output);
    426 
    427         if (!psDBCommit(config->dbh)) {
    428             psError(PS_ERR_UNKNOWN, false, "database error");
    429             return false;
    430         }
    431 
    432         return true;
    433     }
    434 
    435     // else
    436     // it's a science exp
     584    // insert the exp into rawExp
    437585    for (long i = 0; psArrayLength(output) > i; i++) {
    438586        psMetadata *row = output->data[i];
    439587        // convert metadata into a newExp object
    440588        newExpRow *newExp = newExpObjectFromMetadata(row);
    441         // convert newExp object into a rawDetrendExp object
    442         rawScienceExpRow *rawExp = newToRawScienceExp(config, newExp);
     589        // convert newExp object into a rawExp object
     590        rawExpRow *rawExp = newToRawExp(config, newExp);
    443591        if (!rawExp) {
    444592            // rollback
     
    446594                psError(PS_ERR_UNKNOWN, false, "database error");
    447595            }
    448             psError(PS_ERR_UNKNOWN, false, "failed to convert newExp to rawScienceExp");
     596            psError(PS_ERR_UNKNOWN, false, "failed to convert newExp to rawExp");
    449597            psFree(newExp);
    450598            psFree(output);
    451599            return false;
    452600        }
    453         // insert the rawDetrendExp object into the database
    454         if (!rawScienceExpInsertObject(config->dbh, rawExp)) {
     601
     602        // insert the rawExp object into the database
     603        if (!rawExpInsertObject(config->dbh, rawExp)) {
    455604            // rollback
    456605            if (!psDBRollback(config->dbh)) {
     
    464613        }
    465614        psFree(rawExp);
     615
    466616        // delete the newExp object from the database
    467617        if (!newExpDeleteObject(config->dbh, newExp)) {
     
    475625            return false;
    476626        }
     627
     628        // if this is a detrend image don't put it in the p2 queue (and we're
     629        // done)
     630        if (detrend) {
     631            psFree(newExp);
     632            continue;
     633        }
     634
    477635        // insert an entry into the p2PendingExp table
    478636        p2PendingExpRow *p2PendingExp = newToP2PendingExp(config, newExp);
     
    483641            return false;
    484642        }
     643
    485644        // insert the p2PendingExp object into the database
    486645        if (!p2PendingExpInsertObject(config->dbh, p2PendingExp)) {
     
    496655        }
    497656        psFree(p2PendingExp);
     657
    498658        // find all of the rawImfiles associated with the p2PendingExp object
    499659        psArray *rawImfiles = NULL;
     
    570730}
    571731
    572 static bool updateimfileMode(pxConfig *config)
     732
     733static bool processedexpMode(pxConfig *config)
    573734{
    574735    PS_ASSERT_PTR_NON_NULL(config, false);
    575736
    576     // XXX search by the whole frame some imfiles without a newExp don't get
    577     // processed -- this may not be the correct thing to do
    578     psString query = psStringCopy(
    579         "SELECT * FROM"
    580             " (SELECT newImfile.* FROM newImfile"
    581                 " LEFT JOIN newExp USING(exp_tag)"
    582                 " LEFT JOIN rawScienceExp USING(exp_tag)"
    583                 " LEFT JOIN rawDetrendExp USING (exp_tag)"
    584                 " WHERE newExp.exp_tag IS NOT NULL"
    585                 " AND rawScienceExp.exp_tag IS NULL"
    586                 " AND rawDetrendExp.exp_tag IS NULL) AS foo"
    587         ); // WHERE class is generated from exp_tag, class, & class_id
    588 
    589     {
    590         // build a query to search by exp_tag, class, class_id
    591         psMetadata *where = psMetadataAlloc();
    592         bool status = false;
    593         psString exp_tag = psMetadataLookupStr(&status, config->args, "-exp_tag");
    594         if (!status) {
    595             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -exp_tag");
    596             psFree(query);
    597             return false;
    598         }
    599         if (exp_tag) {
    600             if (!psMetadataAddStr(where, PS_LIST_TAIL, "exp_tag", 0, "==", exp_tag)) {
    601                 psError(PS_ERR_UNKNOWN, false, "failed to add item exp_tag");
    602                 psFree(where);
    603                 psFree(query);
    604                 return false;
    605             }
    606         }
    607         psString class = psMetadataLookupStr(&status, config->args, "-class");
    608         if (!status) {
    609             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class");
    610             psFree(query);
    611             return false;
    612         }
    613         if (class) {
    614             if (!psMetadataAddStr(where, PS_LIST_TAIL, "class", 0, "==", class)) {
    615                 psError(PS_ERR_UNKNOWN, false, "failed to add item class");
    616                 psFree(where);
    617                 psFree(query);
    618                 return false;
    619             }
    620         }
    621         psString class_id = psMetadataLookupStr(&status, config->args, "-class_id");
    622         if (!status) {
    623             psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -class_id");
    624             psFree(query);
    625             return false;
    626         }
    627         if (class_id) {
    628             if (!psMetadataAddStr(where, PS_LIST_TAIL, "class_id", 0, "==", class_id)) {
    629                 psError(PS_ERR_UNKNOWN, false, "failed to add item class_id");
    630                 psFree(where);
    631                 psFree(query);
    632                 return false;
    633             }
    634         }
    635 
    636         // there's not
    637         psString whereClaus = psDBGenerateWhereSQL(where, NULL);
    638         psFree(where);
    639         if (whereClaus) {
    640             psStringAppend(&query, " %s", whereClaus);
    641             psFree(whereClaus);
    642         }
     737    bool status = false;
     738    psU64 limit = psMetadataLookupU64(&status, config->args, "-limit");
     739    if (!status) {
     740        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -limit");
     741        return false;
     742    }
     743
     744    bool faulted = psMetadataLookupU64(&status, config->args, "-faulted");
     745    if (!status) {
     746        psError(PXTOOLS_ERR_PROG, false, "failed to lookup value for -faulted");
     747        return false;
     748    }
     749
     750    // find all rawImfiles matching the default query
     751    psString query = psStringCopy(
     752        "SELECT\n"
     753        "   *\n"
     754        " FROM rawExp\n"
     755        " WHERE\n"
     756        "   rawExp.exp_tag IS NOT NULL\n" // bogus where clause
     757    );
     758
     759    if (config->where) {
     760        psString whereClause = psDBGenerateWhereConditionSQL(config->where, "rawExp");
     761        psStringAppend(&query, " AND %s", whereClause);
     762        psFree(whereClause);
     763    }
     764
     765    if (faulted) {
     766        // list only faulted rows
     767        psStringAppend(&query, " %s", "AND rawExp.fault != 0");
     768    } else {
     769        // don't list faulted rows
     770        psStringAppend(&query, " %s", "AND rawExp.fault = 0");
     771    }
     772
     773    // treat limit == 0 as "no limit"
     774    if (limit) {
     775        psString limitString = psDBGenerateLimitSQL(limit);
     776        psStringAppend(&query, " %s", limitString);
     777        psFree(limitString);
    643778    }
    644779
    645780    if (!p_psDBRunQuery(config->dbh, query)) {
    646781        psError(PS_ERR_UNKNOWN, false, "database error");
    647         psFree(query);
    648782        return false;
    649783    }
     
    656790    }
    657791    if (!psArrayLength(output)) {
    658         // XXX check psError here
    659         psError(PS_ERR_UNKNOWN, false, "no pending newImfile rows found");
     792        psError(PS_ERR_UNKNOWN, false, "no pending rawExp rows found");
    660793        psFree(output);
    661         return false;
    662     }
    663 
    664     // insert 'newImfile's into rawImfile
    665     if (psArrayLength(output) > 0) {
    666         // start a transaction so we don't end up half of the imfiles we were
    667         // trying to update uninserted
    668         if (!psDBTransaction(config->dbh)) {
    669             psError(PS_ERR_UNKNOWN, false, "database error");
    670             psFree(output);
    671             return false;
    672         }
    673 
    674         for (long i = 0; i < psArrayLength(output); i++) {
    675             // convert newImfile metadata -> newImfile object
    676             newImfileRow *object = newImfileObjectFromMetadata(output->data[i]);
    677             // convert newImfile object -> rawImfile object
    678             rawImfileRow *imfile = newToRawImfile(config, object);
    679             if (!imfile) {
    680                 // rollback
    681                 if (!psDBRollback(config->dbh)) {
    682                     psError(PS_ERR_UNKNOWN, false, "database error");
    683                 }
    684                 psError(PS_ERR_UNKNOWN, false, "failed to create a new rawImfile row");
    685                 psFree(object);
    686                 psFree(output);
    687                 return false;
    688             }
    689             // insert the rawImfile object into the database
    690             if (!rawImfileInsertObject(config->dbh, imfile)) {
    691                 // rollback
    692                 if (!psDBRollback(config->dbh)) {
    693                     psError(PS_ERR_UNKNOWN, false, "database error");
    694                 }
    695                 psError(PS_ERR_UNKNOWN, false, "failed to insert row into the database");
    696                 psFree(imfile);
    697                 psFree(object);
    698                 psFree(output);
    699                 return false;
    700             }
    701             psFree(imfile);
    702             // remove the neImfile object from the database
    703             if (!newImfileDeleteObject(config->dbh, object)) {
    704                 // rollback
    705                 if (!psDBRollback(config->dbh)) {
    706                     psError(PS_ERR_UNKNOWN, false, "database error");
    707                 }
    708                 psError(PS_ERR_UNKNOWN, false, "failed to delete row from the database");
    709                 psFree(object);
    710                 psFree(output);
    711                 return false;
    712             }
    713             psFree(object);
    714         }
    715 
    716         // point of no return for rawImfile
    717         if (!psDBCommit(config->dbh)) {
    718             psError(PS_ERR_UNKNOWN, false, "database error");
    719             psFree(output);
    720             return false;
    721         }
    722     }
    723 
    724     psFree(output);
    725 
    726     return true;
    727 }
    728 
    729 static bool rawimfileMode(pxConfig *config)
    730 {
    731     PS_ASSERT_PTR_NON_NULL(config, false);
    732 
    733     // find all rawImfiles matching the default query
    734     psArray *rawImfiles = rawImfileSelectRowObjects(config->dbh, config->where, 0);
    735     if (!rawImfiles) {
    736         psError(PS_ERR_UNKNOWN, false, "no rawImfile rows found");
    737         return false;
    738     }
     794        return true;
     795    }
     796
    739797    bool simple = false;
    740798    {
     
    747805    }
    748806
    749     if (psArrayLength(rawImfiles)) {
     807    if (psArrayLength(output)) {
    750808        // negative simple so the default is true
    751         if (!rawImfilePrintObjects(stdout, rawImfiles, !simple)) {
     809        if (!ippdbPrintMetadatas(stdout, output, "rawExp", !simple)) {
    752810            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    753             psFree(rawImfiles);
    754             return false;
    755         }
    756     }
    757 
    758     psFree(rawImfiles);
     811            psFree(output);
     812            return false;
     813        }
     814    }
     815
     816    psFree(output);
    759817
    760818    return true;
    761819}
     820
     821
     822static bool updateprocessedexpMode(pxConfig *config)
     823{
     824    bool status = false;
     825    psS8 code = psMetadataLookupS8(&status, config->args, "-code");
     826    if (!status) {
     827        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
     828        return false;
     829    }
     830
     831    if (!pxSetFaultCode(config->dbh, "rawExp", config->where, code)) {
     832        psError(PS_ERR_UNKNOWN, false, "failed to set set fault flag");
     833        return false;
     834    }
     835
     836    return true;
     837}
     838
     839
    762840# if 0
    763841static p1PendingExpRow *newToP1PendingExp(newExpRow *newExp)
     
    781859#endif
    782860
     861
    783862static p2PendingExpRow *newToP2PendingExp(pxConfig *config, newExpRow *exp)
    784863{
     
    804883    return p2Exp;
    805884}
     885
    806886
    807887static p2PendingImfileRow *rawImfileToP2PendingImfile(pxConfig *config, rawImfileRow *rawImfile)
     
    817897}
    818898
    819 static rawScienceExpRow *newToRawScienceExp(pxConfig *config, newExpRow *exp)
    820 {
    821     PS_ASSERT_PTR_NON_NULL(config, NULL);
    822     PS_ASSERT_PTR_NON_NULL(exp, NULL);
    823 
    824     // XXX this is dangerous but we should be able to get away with this as
    825     // long rawScienceExp & rawDetrendExp are idetnical
    826     return (rawScienceExpRow *)newToRawDetrendExp(config, exp);
    827 }
    828 
    829 static rawDetrendExpRow *newToRawDetrendExp(pxConfig *config, newExpRow *exp)
     899
     900static rawExpRow *newToRawExp(pxConfig *config, newExpRow *exp)
    830901{
    831902    PS_ASSERT_PTR_NON_NULL(config, NULL);
     
    9321003    }
    9331004
    934     rawDetrendExpRow *raw = rawDetrendExpRowAlloc(
     1005    // default
     1006    psS8 code = psMetadataLookupS8(&status, config->args, "-code");
     1007    if (!status) {
     1008        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
     1009        return false;
     1010    }
     1011
     1012    rawExpRow *raw = rawExpRowAlloc(
    9351013        exp->exp_tag,
    9361014        exp->camera,
     
    9511029        posang,
    9521030        object,
    953         dateobs
     1031        dateobs,
     1032        code
    9541033    );
    9551034
     
    9601039    return raw;
    9611040}
     1041
    9621042
    9631043static rawImfileRow *newToRawImfile(pxConfig *config, newImfileRow *imfile)
     
    10651145            dateobs = NULL;
    10661146        }
     1147    }
     1148
     1149    psS8 code = psMetadataLookupS8(&status, config->args, "-code");
     1150    if (!status) {
     1151        psError(PS_ERR_UNKNOWN, false, "failed to lookup value for -code");
     1152        return false;
    10671153    }
    10681154
     
    10861172        posang,
    10871173        object,
    1088         dateobs
     1174        dateobs,
     1175        code
    10891176    );
    10901177
Note: See TracChangeset for help on using the changeset viewer.