IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Sep 2, 2008, 9:37:06 AM (18 years ago)
Author:
eugene
Message:

substantial work on flatcorr; reworked the sequence somewhat

File:
1 edited

Legend:

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

    r18561 r19325  
    33 *
    44 * Copyright (C) 2006-2007  Joshua Hoblitt
     5 * Copyright (C) 2008  Eugene Magnier
    56 *
    67 * This program is free software; you can redistribute it and/or modify it
     
    3132
    3233#include "pxtools.h"
     34#include "pxchip.h"
    3335#include "flatcorr.h"
    3436
    35 static bool newrunMode(pxConfig *config);
    36 static bool addexpMode(pxConfig *config);
    37 static bool pendingMode(pxConfig *config);
    38 static bool flatcorrimfileMode(pxConfig *config);
    39 static bool doneMode(pxConfig *config);
     37static bool definebyqueryMode(pxConfig *config);
     38static bool definerunMode(pxConfig *config);
     39static bool addchipMode(pxConfig *config);
     40static bool addcameraMode(pxConfig *config);
     41static bool pendingprocessMode(pxConfig *config);
     42static bool addprocessMode(pxConfig *config);
    4043static bool updaterunMode(pxConfig *config);
     44static bool inputexpMode(pxConfig *config);
     45static bool inputimfileMode(pxConfig *config);
    4146
    4247static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state);
     
    6065
    6166    switch (config->mode) {
    62         MODECASE(FLATCORR_MODE_NEWRUN,      newrunMode);
    63         MODECASE(FLATCORR_MODE_ADDEXP,      addexpMode);
    64         MODECASE(FLATCORR_MODE_PENDING,     pendingMode);
    65         MODECASE(FLATCORR_MODE_FLATCORRIMFILE, flatcorrimfileMode);
    66         MODECASE(FLATCORR_MODE_DONE,        doneMode);
    67         MODECASE(FLATCORR_MODE_UPDATERUN,   updaterunMode);
     67        MODECASE(FLATCORR_MODE_DEFINEBYQUERY,  definebyqueryMode);
     68        MODECASE(FLATCORR_MODE_DEFINERUN,      definerunMode);
     69        MODECASE(FLATCORR_MODE_ADDCHIP,        addchipMode);
     70        MODECASE(FLATCORR_MODE_ADDCAMERA,      addcameraMode);
     71        MODECASE(FLATCORR_MODE_PENDINGPROCESS, pendingprocessMode);
     72        MODECASE(FLATCORR_MODE_ADDPROCESS,     addprocessMode);
     73        MODECASE(FLATCORR_MODE_UPDATERUN,      updaterunMode);
     74        MODECASE(FLATCORR_MODE_INPUTEXP,       inputexpMode);
     75        MODECASE(FLATCORR_MODE_INPUTIMFILE,    inputimfileMode);
    6876        default:
    6977            psAbort("invalid option (this should not happen)");
     
    8896
    8997
    90 static bool newrunMode(pxConfig *config)
     98static bool definebyqueryMode(pxConfig *config)
    9199{
    92100    PS_ASSERT_PTR_NON_NULL(config, false);
    93101
    94102    psMetadata *where = psMetadataAlloc();
    95 
    96     PXOPT_COPY_S64(config->args, where, "-exp_id", "exp_id", "==");
    97     PXOPT_COPY_STR(config->args, where, "-exp_name", "exp_name", "==");
    98     PXOPT_COPY_STR(config->args, where, "-inst", "camera", "==");
    99     PXOPT_COPY_STR(config->args, where, "-telescope", "telescope", "==");
    100     PXOPT_COPY_TIME(config->args, where, "-dateobs_begin", "dateobs", ">=");
    101     PXOPT_COPY_TIME(config->args, where, "-dateobs_end", "dateobs", "<=");
    102     PXOPT_COPY_STR(config->args, where, "-exp_tag", "exp_tag", "==");
    103     PXOPT_COPY_STR(config->args, where, "-exp_type", "exp_type", "==");
    104     PXOPT_COPY_STR(config->args, where, "-filelevel", "filelvel", "==");
    105     PXOPT_COPY_STR(config->args, where, "-reduction", "reduction", "==");
    106     PXOPT_COPY_STR(config->args, where, "-filter", "filter", "==");
    107     PXOPT_COPY_F32(config->args, where, "-airmass_min", "airmass", ">=");
    108     PXOPT_COPY_F32(config->args, where, "-airmass_max", "airmass", "<");
    109     PXOPT_COPY_F64(config->args, where, "-ra_min", "ra", ">=");
    110     PXOPT_COPY_F64(config->args, where, "-ra_max", "ra", "<");
    111     PXOPT_COPY_F64(config->args, where, "-decl_min", "decl", ">=");
    112     PXOPT_COPY_F64(config->args, where, "-decl_max", "decl", "<");
    113     PXOPT_COPY_F32(config->args, where, "-exp_time_min", "exp_time", ">=");
    114     PXOPT_COPY_F32(config->args, where, "-exp_time_max", "exp_time", "<");
    115     PXOPT_COPY_F32(config->args, where, "-sat_pixel_frac_min", "sat_pixel_frac", ">=");
    116     PXOPT_COPY_F32(config->args, where, "-sat_pixel_frac_max", "sat_pixel_frac", "<");
    117     PXOPT_COPY_F64(config->args, where, "-bg_min", "bt", ">=");
    118     PXOPT_COPY_F64(config->args, where, "-bg_max", "bt", "<");
    119     PXOPT_COPY_F64(config->args, where, "-bg_stdev_min", "bg_stdev", ">=");
    120     PXOPT_COPY_F64(config->args, where, "-bg_stdev_max", "bg_stdev", "<");
    121     PXOPT_COPY_F64(config->args, where, "-bg_mean_stdev_min", "bg_mean_stdev", ">=");
    122     PXOPT_COPY_F64(config->args, where, "-bg_mean_stdev_max", "bg_mean_stdev", "<");
    123     PXOPT_COPY_F64(config->args, where, "-alt_min", "alt", ">=");
    124     PXOPT_COPY_F64(config->args, where, "-alt_max", "alt", "<");
    125     PXOPT_COPY_F64(config->args, where, "-az_min", "az", ">=");
    126     PXOPT_COPY_F64(config->args, where, "-az_max", "az", "<");
    127     PXOPT_COPY_F32(config->args, where, "-ccd_temp_min", "ccd_temp", ">=");
    128     PXOPT_COPY_F32(config->args, where, "-ccd_temp_max", "ccd_temp", "<");
    129     PXOPT_COPY_F64(config->args, where, "-posang_min", "posang", ">=");
    130     PXOPT_COPY_F64(config->args, where, "-posang_max", "posang", "<");
    131     PXOPT_COPY_STR(config->args, where, "-object", "object", "==");
    132     PXOPT_COPY_F32(config->args, where, "-solang_min", "solang", ">=");
    133     PXOPT_COPY_F32(config->args, where, "-solang_max", "solang", "<");
    134 
    135     if (where->list->n < 1) {
     103    pxchipGetSearchArgs (config, where);
     104
     105    if (!psListLength(where->list)) {
    136106        psFree(where);
    137107        psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
     
    146116    PXOPT_LOOKUP_STR(filter, config->args, "-set_filter", false, false);
    147117    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
    148     PXOPT_LOOKUP_STR(end_stage, config->args, "-set_end_stage", false, false);
    149118    PXOPT_LOOKUP_STR(region, config->args, "-set_region", false, false);
     119    // XXX probably should make the region in -set_region match ra_min, ra_max, etc
    150120
    151121    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     
    156126    if (!query) {
    157127        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    158         return false;
    159     }
    160 
    161     if (where) {
     128        psFree(where);
     129        return false;
     130    }
     131
     132    if (where && psListLength(where->list)) {
    162133        psString whereClause = psDBGenerateWhereConditionSQL(where, "rawExp");
    163         psFree(where);
    164134        psStringAppend(&query, " AND %s", whereClause);
    165135        psFree(whereClause);
    166136    }
     137    psFree(where);
    167138
    168139    if (!p_psDBRunQuery(config->dbh, query)) {
     
    179150    }
    180151    if (!psArrayLength(output)) {
    181         psTrace("chiptool", PS_LOG_INFO, "no rows found");
     152        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
    182153        psFree(output);
    183154        return true;
     
    185156
    186157    if (pretend) {
    187         // negative simple so the default is true
    188     for (int i = 0; i < output->n; i++) {
    189         psMetadata *md = output->data[i];
    190         psMetadataConfigPrint (stdout, md);
    191     }
    192 //        if (!rawExpPrintObjects(stdout, output, !simple)) {
    193 //            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    194 //            psFree(output);
    195 //            return false;
    196 //        }
     158        for (long i = 0; i < psArrayLength(output); i++) {
     159            // negative simple so the default is true
     160            if (!ippdbPrintMetadataRaw(stdout, output->data[i], !simple)) {
     161                psError(PS_ERR_UNKNOWN, false, "failed to print array");
     162                psFree(output);
     163                return false;
     164            }
     165        }
    197166        psFree(output);
    198167        return true;
     
    216185            label,
    217186            NULL,   // stats
     187            reduction,
    218188            region
    219189        )) {
     
    243213        }
    244214
    245         // queue the exp
    246         psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, reduction, expgroup, dvodb, tess_id, end_stage);
     215        // queue the exp : force this to stop at the chip stage
     216        psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, reduction, expgroup, dvodb, tess_id, "chip");
    247217        if (!chip_id) {
    248218            if (!psDBRollback(config->dbh)) {
     
    255225        }
    256226
    257         // add a flatcorrExp to the flatcorr Run we just created
    258         if (!flatcorrExpInsert(config->dbh,
    259                 corr_id,
    260                 chip_id
    261             )) {
     227        // add a flatcorrChipLink to the flatcorr Run we just created
     228        if (!flatcorrChipLinkInsert(config->dbh, corr_id, chip_id)) {
    262229            if (!psDBRollback(config->dbh)) {
    263230                psError(PS_ERR_UNKNOWN, false, "database error");
     
    270237    psFree(output);
    271238
    272     // set the flatcorrRun to a state of 'run'
    273     if (!setflatcorrRunState(config, corr_id, "run")) {
     239    // set the flatcorrRun to a state of 'new'
     240    if (!setflatcorrRunState(config, corr_id, "new")) {
    274241        if (!psDBRollback(config->dbh)) {
    275242            psError(PS_ERR_UNKNOWN, false, "database error");
     
    287254}
    288255
    289 
    290 static bool addexpMode(pxConfig *config)
     256static bool definerunMode(pxConfig *config)
     257{
     258    PS_ASSERT_PTR_NON_NULL(config, false);
     259
     260    PXOPT_LOOKUP_STR(workdir, config->args, "-set_workdir", false, false);
     261    PXOPT_LOOKUP_STR(label, config->args, "-set_label", false, false);
     262    PXOPT_LOOKUP_STR(reduction, config->args, "-set_reduction", false, false);
     263    PXOPT_LOOKUP_STR(expgroup, config->args, "-set_expgroup", false, false);
     264    PXOPT_LOOKUP_STR(dvodb, config->args, "-set_dvodb", false, false);
     265    PXOPT_LOOKUP_STR(filter, config->args, "-set_filter", false, false);
     266    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
     267    PXOPT_LOOKUP_STR(region, config->args, "-set_region", false, false);
     268    // XXX probably should make the region in -set_region match ra_min, ra_max, etc
     269
     270    PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
     271
     272    // start a transaction so we don't end up with an exp without any associted
     273    // imfiles
     274    if (!psDBTransaction(config->dbh)) {
     275        psError(PS_ERR_UNKNOWN, false, "database error");
     276        return false;
     277    }
     278
     279    // create a new flatcorrRun
     280    flatcorrRunRow *row = flatcorrRunRowAlloc(
     281        0,      // corr_id
     282        dvodb,
     283        filter,
     284        "reg",  // state
     285        workdir,
     286        label,
     287        NULL,   // stats
     288        reduction,
     289        region
     290        );
     291
     292    // create a new flatcorrRun
     293    if (!flatcorrRunInsertObject(config->dbh,row)) {
     294        if (!psDBRollback(config->dbh)) {
     295            psError(PS_ERR_UNKNOWN, false, "database error");
     296        }
     297        psError(PS_ERR_UNKNOWN, false, "database error");
     298        return false;
     299    }
     300   
     301    // figure out the ID of the flatcorrRun we just created
     302    psS64 corr_id = psDBLastInsertID(config->dbh);
     303    row->corr_id = corr_id;
     304
     305    flatcorrRunPrintObject (stdout, row, !simple);
     306    return true;
     307}
     308
     309static bool addchipMode(pxConfig *config)
    291310{
    292311    PS_ASSERT_PTR_NON_NULL(config, false);
    293312
    294313    // required
    295     PXOPT_LOOKUP_STR(corr_id, config->args, "-corr_id", true, false);
    296     PXOPT_LOOKUP_STR(chip_id, config->args, "-chip_id", true, false);
    297 
    298     if (!flatcorrExpInsert(config->dbh,
    299             (psS64)atoll(corr_id),
    300             (psS64)atoll(chip_id)
    301         )) {
    302         psError(PS_ERR_UNKNOWN, false, "database error");
    303         return false;
    304     }
    305 
    306     return true;
    307 }
    308 
    309 
    310 static bool pendingMode(pxConfig *config)
    311 {
    312     PS_ASSERT_PTR_NON_NULL(config, false);
    313 
    314     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    315     PXOPT_LOOKUP_BOOL(limit, config->args, "-limit", false);
    316 
    317     // create a temp table to hold completely processed chipRuns
    318     {
    319         psString query = pxDataGet("flatcorr_create_tmp_chiprundone.sql");
    320         if (!query) {
    321             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    322             return false;
    323         }
    324 
    325         if (!p_psDBRunQuery(config->dbh, query)) {
    326             psError(PS_ERR_UNKNOWN, false, "database error");
    327             psFree(query);
    328             return false;
    329         }
    330 
    331         psFree(query);
    332     }
    333    
    334     // find all completed chipRuns
    335     {
    336         psString query = pxDataGet("flatcorr_completely_processed_chiprun.sql");
    337         if (!query) {
    338             psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    339             return false;
    340         }
    341 
    342         if (!p_psDBRunQuery(config->dbh, query)) {
    343             psError(PS_ERR_UNKNOWN, false, "database error");
    344             psFree(query);
    345             return false;
    346         }
    347 
    348         psFree(query);
    349     }
    350 
    351     // find flatcorrRun's that have had all of their chipRun's completed by
    352     // comparing against the temp table
    353     psString query = pxDataGet("flatcorr_find_completed_floatcorruns.sql");
     314    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     315    PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", true, false);
     316
     317    if (!flatcorrChipLinkInsert(config->dbh, corr_id, chip_id)) {
     318        psError(PS_ERR_UNKNOWN, false, "database error");
     319        return false;
     320    }
     321
     322    return true;
     323}
     324
     325// select the flatcorr chip runs that have completed and for which there is no camera entry
     326// queue a new camera run for them
     327static bool addcameraMode(pxConfig *config)
     328{
     329    PS_ASSERT_PTR_NON_NULL(config, false);
     330
     331    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     332    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     333    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     334
     335    psString query = pxDataGet("flatcorr_chiprundone.sql");
    354336    if (!query) {
    355         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    356         return false;
     337        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     338        return false;
    357339    }
    358340
     
    372354    psArray *output = p_psDBFetchResult(config->dbh);
    373355    if (!output) {
    374         psErrorCode err = psErrorCodeLast();
    375         switch (err) {
    376           case PS_ERR_DB_CLIENT:
    377             psError(PXTOOLS_ERR_SYS, false, "database error");
    378           case PS_ERR_DB_SERVER:
    379             psError(PXTOOLS_ERR_PROG, false, "database error");
    380           default:
    381             psError(PXTOOLS_ERR_PROG, false, "unknown error");
    382         }
    383 
     356        psError(PS_ERR_UNKNOWN, false, "database error");
    384357        return false;
    385358    }
    386359    if (!psArrayLength(output)) {
    387         psTrace("regtool", PS_LOG_INFO, "no rows found");
     360        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
    388361        psFree(output);
    389362        return true;
    390363    }
    391364
    392     if (psArrayLength(output)) {
     365    if (pretend) {
    393366        // negative simple so the default is true
    394         if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
     367        if (!ippdbPrintMetadatas(stdout, output, "flatcorr_addcamera", !simple)) {
    395368            psError(PS_ERR_UNKNOWN, false, "failed to print array");
    396369            psFree(output);
     
    399372    }
    400373
     374    // start a transaction so we don't end up with an exp without any associted
     375    // imfiles
     376    if (!psDBTransaction(config->dbh)) {
     377        psError(PS_ERR_UNKNOWN, false, "database error");
     378        psFree(output);
     379        return false;
     380    }
     381
     382    // loop over our list of chipRun rows
     383    for (long i = 0; i < psArrayLength(output); i++) {
     384        psMetadata *md = output->data[i];
     385
     386        bool status;
     387        psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id");
     388        if (!status) {
     389            if (!psDBRollback(config->dbh)) {
     390                psError(PS_ERR_UNKNOWN, false, "database error");
     391            }
     392            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for corr_id");
     393            psFree(output);
     394            return false;
     395        }
     396
     397        chipRunRow *row = chipRunObjectFromMetadata(md);
     398        if (!row) {
     399            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
     400            psFree(output);
     401            return false;
     402        }
     403
     404        // queue the exp
     405        if (!pxcamQueueByChipID(
     406                config,
     407                row->chip_id,
     408                row->workdir,
     409                row->label,
     410                row->reduction,
     411                row->expgroup,
     412                row->dvodb,
     413                row->tess_id,
     414                "camera")) {
     415            if (!psDBRollback(config->dbh)) {
     416                psError(PS_ERR_UNKNOWN, false, "database error");
     417            }
     418            psError(PS_ERR_UNKNOWN, false,
     419                    "failed to trying to queue chip_id: %" PRId64, row->chip_id);
     420            psFree(row);
     421            psFree(output);
     422            return false;
     423        }
     424
     425        // figure out the ID of the flatcorrRun we just created
     426        psS64 cam_id = psDBLastInsertID(config->dbh);
     427
     428        // add the camRun entry to the flatcorrCamLink table:
     429        if (!flatcorrCamLinkInsert(config->dbh, corr_id, row->chip_id, cam_id)) {
     430            if (!psDBRollback(config->dbh)) {
     431                psError(PS_ERR_UNKNOWN, false, "database error");
     432            }
     433            psError(PS_ERR_UNKNOWN, false, "database error");
     434            return false;
     435        }
     436       
     437        psFree(row);
     438    }
    401439    psFree(output);
    402440
     441    if (!psDBCommit(config->dbh)) {
     442        psError(PS_ERR_UNKNOWN, false, "database error");
     443        return false;
     444    }
     445
    403446    return false;
    404447}
    405448
    406 
    407 static bool flatcorrimfileMode(pxConfig *config)
    408 {
    409     PS_ASSERT_PTR_NON_NULL(config, false);
    410 
    411     psMetadata *where = psMetadataAlloc();
    412     PXOPT_COPY_S64(config->args, where, "-corr_id", "corr_id", "==");
    413 
    414     PXOPT_LOOKUP_U64(limit, config->args, "-limit", false, false);
    415     PXOPT_LOOKUP_BOOL(simple, config->args, "-simple", false);
    416 
    417     // find all rawImfiles matching the default query
    418     psString query = pxDataGet("flatcorr_find_processedimfiles.sql");
     449// select the flatcorr chip runs that have completed and for which there is no camera entry
     450// queue a new camera run for them
     451static bool pendingprocessMode(pxConfig *config)
     452{
     453    PS_ASSERT_PTR_NON_NULL(config, false);
     454
     455    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     456    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     457
     458    psString query = pxDataGet("flatcorr_pendingprocess.sql");
    419459    if (!query) {
    420         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    421         return false;
    422     }
    423 
    424     if (psListLength(where->list)) {
    425         psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
    426         psStringAppend(&query, " AND %s", whereClause);
    427         psFree(whereClause);
    428     }
    429     psFree(where);
     460        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     461        return false;
     462    }
    430463
    431464    // treat limit == 0 as "no limit"
    432465    if (limit) {
    433         psString limitString = psDBGenerateLimitSQL(limit);
    434         psStringAppend(&query, " %s", limitString);
    435         psFree(limitString);
     466        psString limitString = psDBGenerateLimitSQL(limit);
     467        psStringAppend(&query, " %s", limitString);
     468        psFree(limitString);
    436469    }
    437470
     
    441474        return false;
    442475    }
    443     psFree(query);
    444    
     476
    445477    psArray *output = p_psDBFetchResult(config->dbh);
    446478    if (!output) {
    447         psErrorCode err = psErrorCodeLast();
    448         switch (err) {
    449             case PS_ERR_DB_CLIENT:
    450                 psError(PXTOOLS_ERR_SYS, false, "database error");
    451             case PS_ERR_DB_SERVER:
    452                 psError(PXTOOLS_ERR_PROG, false, "database error");
    453             default:
    454                 psError(PXTOOLS_ERR_PROG, false, "unknown error");
    455         }
    456    
     479        psError(PS_ERR_UNKNOWN, false, "database error");
    457480        return false;
    458481    }
    459482    if (!psArrayLength(output)) {
    460         psTrace("warptool", PS_LOG_INFO, "no rows found");
    461         psFree(output);
    462         return true;
    463     }
    464 
    465     if (psArrayLength(output)) {
    466         // negative simple so the default is true
    467         if (!ippdbPrintMetadatas(stdout, output, "chipProcessedImfile", !simple)) {
    468             psError(PS_ERR_UNKNOWN, false, "failed to print array");
    469             psFree(output);
    470             return false;
    471         }
    472     }
    473 
    474     psFree(output);
    475 
    476     return true;
    477 }
    478 
    479 
    480 static bool doneMode(pxConfig *config)
    481 {
    482     PS_ASSERT_PTR_NON_NULL(config, false);
    483 
    484     PXOPT_LOOKUP_STR(corr_id, config->args, "-corr_id", true, false);
    485     PXOPT_LOOKUP_STR(stats, config->args, "-stats", true, false);
    486 
    487     if (!psDBTransaction(config->dbh)) {
    488         psError(PS_ERR_UNKNOWN, false, "database error");
    489         return false;
    490     }
    491 
    492     if (!setflatcorrRunState(config, (psS64)atoll(corr_id), "stop")) {
     483        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     484        psFree(output);
     485        return true;
     486    }
     487
     488    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
     489        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     490        psFree(output);
     491        return false;
     492    }
     493
     494    return true;
     495}
     496
     497// select the flatcorr chip runs that have completed and for which there is no camera entry
     498// queue a new camera run for them
     499static bool inputexpMode(pxConfig *config)
     500{
     501    PS_ASSERT_PTR_NON_NULL(config, false);
     502
     503    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     504    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     505    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     506
     507    char *query = psStringCopy ("SELECT * FROM flatcorrChipLink WHERE corr_id = %" PRId64);
     508
     509    // treat limit == 0 as "no limit"
     510    if (limit) {
     511        psString limitString = psDBGenerateLimitSQL(limit);
     512        psStringAppend(&query, " %s", limitString);
     513        psFree(limitString);
     514    }
     515
     516    if (!p_psDBRunQuery(config->dbh, query, corr_id)) {
     517        psError(PS_ERR_UNKNOWN, false, "database error");
     518        psFree(query);
     519        return false;
     520    }
     521
     522    psArray *output = p_psDBFetchResult(config->dbh);
     523    if (!output) {
     524        psError(PS_ERR_UNKNOWN, false, "database error");
     525        return false;
     526    }
     527    if (!psArrayLength(output)) {
     528        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     529        psFree(output);
     530        return true;
     531    }
     532
     533    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
     534        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     535        psFree(output);
     536        return false;
     537    }
     538
     539    return true;
     540}
     541
     542// select the flatcorr chip runs that have completed and for which there is no camera entry
     543// queue a new camera run for them
     544static bool inputimfileMode(pxConfig *config)
     545{
     546    PS_ASSERT_PTR_NON_NULL(config, false);
     547
     548    PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", true, false);
     549    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     550    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     551
     552    char *query = psStringCopy ("SELECT * FROM chipProcessedImfile WHERE chip_id = %" PRId64);
     553
     554    // treat limit == 0 as "no limit"
     555    if (limit) {
     556        psString limitString = psDBGenerateLimitSQL(limit);
     557        psStringAppend(&query, " %s", limitString);
     558        psFree(limitString);
     559    }
     560
     561    if (!p_psDBRunQuery(config->dbh, query, chip_id)) {
     562        psError(PS_ERR_UNKNOWN, false, "database error");
     563        psFree(query);
     564        return false;
     565    }
     566
     567    psArray *output = p_psDBFetchResult(config->dbh);
     568    if (!output) {
     569        psError(PS_ERR_UNKNOWN, false, "database error");
     570        return false;
     571    }
     572    if (!psArrayLength(output)) {
     573        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     574        psFree(output);
     575        return true;
     576    }
     577
     578    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
     579        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     580        psFree(output);
     581        return false;
     582    }
     583
     584    return true;
     585}
     586
     587// XXX need a fault state
     588static bool addprocessMode(pxConfig *config)
     589{
     590    PS_ASSERT_PTR_NON_NULL(config, false);
     591
     592    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     593
     594    if (!setflatcorrRunState(config, corr_id, "full")) {
    493595        if (!psDBRollback(config->dbh)) {
    494596            psError(PS_ERR_UNKNOWN, false, "database error");
     
    498600    }
    499601
    500     char *query = "UPDATE flatcorrRun SET stats = '%s' WHERE corr_id = %" PRId64;
    501     if (!p_psDBRunQuery(config->dbh, query, stats, (psS64)atoll(corr_id))) {
    502         psError(PS_ERR_UNKNOWN, false,
    503                 "failed to change state for corr_id %" PRId64, (psS64)atoll(corr_id));
    504         return false;
    505     }
    506 
    507     if (!psDBCommit(config->dbh)) {
    508         psError(PS_ERR_UNKNOWN, false, "database error");
    509         return false;
    510     }
    511 
    512     return false;
    513 }
    514 
     602    return true;
     603}
     604
     605static bool updaterunMode(pxConfig *config)
     606{
     607    PS_ASSERT_PTR_NON_NULL(config, false);
     608
     609    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     610    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     611
     612    if (!setflatcorrRunState(config, corr_id, state)) {
     613        if (!psDBRollback(config->dbh)) {
     614            psError(PS_ERR_UNKNOWN, false, "database error");
     615        }
     616        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
     617        return false;
     618    }
     619
     620    return true;
     621}
    515622
    516623static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state)
     
    520627
    521628    // check that state is a valid string value
    522     if (!(
    523             (strncmp(state, "run", 4) == 0)
    524             || (strncmp(state, "stop", 5) == 0)
    525             || (strncmp(state, "reg", 4) == 0)
    526         )
    527     ) {
     629    if (!strcmp(state, "reg") &&
     630        !strcmp(state, "new") &&
     631        !strcmp(state, "full"))
     632    {
    528633        psError(PS_ERR_UNKNOWN, false, "invalid state: %s", state);
    529634        return false;
     
    541646
    542647
    543 static bool updaterunMode(pxConfig *config)
    544 {
    545     PS_ASSERT_PTR_NON_NULL(config, false);
    546 
    547     PXOPT_LOOKUP_STR(corr_id, config->args, "-corr_id", true, false);
    548     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    549 
    550     if (state) {
    551         // set detRun.state to state
    552         return setflatcorrRunState(config, (psS64)atoll(corr_id), state);
    553     }
    554 
    555     return true;
    556 }
Note: See TracChangeset for help on using the changeset viewer.