IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/ippTools/src

    • Property svn:ignore
      •  

        old new  
         1*.la
         2*.lo
        13.deps
        24.gdb_history
         
        46Makefile
        57Makefile.in
         8addtool
         9caltool
         10camtool
         11chiptool
        612config.h
        713config.h.in
        8 stamp-h1
        9 *.la
        10 *.lo
         14detselect
         15dettool
         16difftool
         17disttool
         18dqstatstool
         19faketool
         20flatcorr
         21guidetool
         22magicdstool
         23magictool
         24pstamptool
         25pubtool
         26pxadmin
         27pxdata.c
         28pxinject
        1129pxtoolsErrorCodes.c
        1230pxtoolsErrorCodes.h
        13 pxadmin
        14 pxinject
        15 pztool
        1631pzgetexp
        1732pzgetimfiles
         33pztool
         34receivetool
        1835regtool
        19 guidetool
        20 chiptool
        21 camtool
         36stacktool
         37stamp-h1
        2238warptool
        23 difftool
        24 stacktool
        25 faketool
        26 dettool
        27 detselect
        28 pxdata.c
        29 magictool
        30 magicdstool
        31 caltool
        32 flatcorr
        33 pstamptool
        34 disttool
        35 receivetool
        36 
        37 pubtool
  • branches/simtest_nebulous_branches/ippTools/src/flatcorr.c

    r24140 r27840  
    3333#include "pxtools.h"
    3434#include "pxchip.h"
     35#include "pxcam.h"
     36#include "pxadd.h"
    3537#include "flatcorr.h"
    3638
     
    3840static bool definerunMode(pxConfig *config);
    3941static bool addchipMode(pxConfig *config);
     42static bool dropchipMode(pxConfig *config);
    4043static bool addcameraMode(pxConfig *config);
    41 static bool dropchipMode(pxConfig *config);
    4244static bool dropcameraMode(pxConfig *config);
     45static bool advancecameraMode(pxConfig *config);
     46static bool advanceaddstarMode(pxConfig *config);
    4347static bool pendingprocessMode(pxConfig *config);
    4448static bool addprocessMode(pxConfig *config);
     
    4650static bool inputexpMode(pxConfig *config);
    4751static bool inputimfileMode(pxConfig *config);
    48 static bool exportrunMode(pxConfig *config);
    49 static bool importrunMode(pxConfig *config);
    5052
    5153static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state);
     
    7274        MODECASE(FLATCORR_MODE_DEFINERUN,      definerunMode);
    7375        MODECASE(FLATCORR_MODE_ADDCHIP,        addchipMode);
     76        MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
    7477        MODECASE(FLATCORR_MODE_ADDCAMERA,      addcameraMode);
    75         MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
    7678        MODECASE(FLATCORR_MODE_DROPCAMERA,     dropcameraMode);
     79        MODECASE(FLATCORR_MODE_ADVANCECAMERA,  advancecameraMode);
     80        MODECASE(FLATCORR_MODE_ADVANCEADDSTAR, advanceaddstarMode);
    7781        MODECASE(FLATCORR_MODE_PENDINGPROCESS, pendingprocessMode);
    7882        MODECASE(FLATCORR_MODE_ADDPROCESS,     addprocessMode);
     
    8084        MODECASE(FLATCORR_MODE_INPUTEXP,       inputexpMode);
    8185        MODECASE(FLATCORR_MODE_INPUTIMFILE,    inputimfileMode);
    82         MODECASE(FLATCORR_MODE_EXPORTRUN,      exportrunMode);
    83         MODECASE(FLATCORR_MODE_IMPORTRUN,      importrunMode);
    8486        default:
    8587            psAbort("invalid option (this should not happen)");
     
    113115    if (!psListLength(where->list)) {
    114116        psFree(where);
    115         psError(PXTOOLS_ERR_DATA, false, "search parameters are required");
    116         return false;
    117     }
    118 
    119     // require the camera to be defined: this analysis does not make sense 
     117        psError(PXTOOLS_ERR_CONFIG, false, "search parameters are required");
     118        return false;
     119    }
     120
     121    // require the camera to be defined: this analysis does not make sense
    120122    // across multiple cameras
    121123    PXOPT_LOOKUP_STR(camera, config->args, "-inst", true, false);
     
    131133    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
    132134    PXOPT_LOOKUP_STR(region, config->args, "-set_region", false, false);
     135    PXOPT_LOOKUP_BOOL(make_correction, config->args, "-make_correction", false);
     136    PXOPT_LOOKUP_STR(note, config->args, "-set_note", false, false);
    133137
    134138    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     
    138142    psString query = pxDataGet("chiptool_find_rawexp.sql");
    139143    if (!query) {
    140         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     144        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    141145        psFree(where);
    142146        return false;
     
    169173
    170174    if (pretend) {
    171         for (long i = 0; i < psArrayLength(output); i++) {
    172             // negative simple so the default is true
    173             if (!ippdbPrintMetadataRaw(stdout, output->data[i], !simple)) {
    174                 psError(PS_ERR_UNKNOWN, false, "failed to print array");
    175                 psFree(output);
    176                 return false;
    177             }
    178         }
     175        for (long i = 0; i < psArrayLength(output); i++) {
     176            // negative simple so the default is true
     177            if (!ippdbPrintMetadataRaw(stdout, output->data[i], !simple)) {
     178                psError(PS_ERR_UNKNOWN, false, "failed to print array");
     179                psFree(output);
     180                return false;
     181            }
     182        }
    179183        psFree(output);
    180184        return true;
     
    191195    // create a new flatcorrRun
    192196    if (!flatcorrRunInsert(
    193             config->dbh,
     197            config->dbh,
    194198            0,      // corr_id
    195             det_type,
     199            det_type,
    196200            dvodb,
    197             camera,
    198             telescope,
    199             NULL,
     201            camera,
     202            telescope,
     203            NULL,
    200204            filter,
    201205            "reg",  // state
     206            make_correction,
    202207            workdir,
    203208            label,
    204209            reduction,
    205             region,
    206             NULL,
    207             0
     210            region,
     211            NULL,
     212            0
    208213        )) {
    209214        if (!psDBRollback(config->dbh)) {
     
    233238
    234239        // queue the exp : force this to stop at the chip stage
    235         psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, reduction, expgroup, dvodb, tess_id, "chip");
     240        psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, label, NULL, reduction, expgroup, dvodb, tess_id, "chip", note);
    236241        if (!chip_id) {
    237242            if (!psDBRollback(config->dbh)) {
     
    291296    PXOPT_LOOKUP_STR(tess_id, config->args, "-set_tess_id", false, false);
    292297    PXOPT_LOOKUP_STR(region, config->args, "-set_region", false, false);
     298    PXOPT_LOOKUP_BOOL(make_correction, config->args, "-make_correction", false);
    293299    // XXX probably should make the region in -set_region match ra_min, ra_max, etc
    294300
     
    297303    // start a transaction so we don't end up with an exp without any associted
    298304    // imfiles
    299     if (!psDBTransaction(config->dbh)) {
    300         psError(PS_ERR_UNKNOWN, false, "database error");
    301         return false;
    302     }
     305    // if (!psDBTransaction(config->dbh)) {
     306    //     psError(PS_ERR_UNKNOWN, false, "database error");
     307    //     return false;
     308    // }
    303309
    304310    // create a new flatcorrRun
    305311    flatcorrRunRow *row = flatcorrRunRowAlloc(
    306         0,      // corr_id
    307         det_type,
    308         dvodb,
    309         camera,
    310         telescope,
    311         NULL,
    312         filter,
    313         "reg",  // state
    314         workdir,
    315         label,
    316         reduction,
    317         region,
    318         NULL, // hostname
    319         0 // fault
     312        0,      // corr_id
     313        det_type,
     314        dvodb,
     315        camera,
     316        telescope,
     317        NULL,
     318        filter,
     319        "reg",  // state
     320        make_correction,
     321        workdir,
     322        label,
     323        reduction,
     324        region,
     325        NULL, // hostname
     326        0 // fault
    320327        );
    321328
     
    328335        return false;
    329336    }
    330    
     337
    331338    // figure out the ID of the flatcorrRun we just created
    332339    psS64 corr_id = psDBLastInsertID(config->dbh);
    333340    row->corr_id = corr_id;
    334341
     342    // if (!psDBCommit(config->dbh)) {
     343    //     psError(PS_ERR_UNKNOWN, false, "database error");
     344    //     return false;
     345    // }
     346
    335347    flatcorrRunPrintObject (stdout, row, !simple);
    336348    return true;
     
    365377    psString query = pxDataGet("flatcorr_dropchip.sql");
    366378    if (!query) {
    367         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    368         return false;
     379        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     380        return false;
    369381    }
    370382
    371383    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) chip_id)) {
     384        psError(PS_ERR_UNKNOWN, false, "database error");
     385        psFree(query);
     386        return false;
     387    }
     388
     389    return true;
     390}
     391
     392static bool addcameraMode(pxConfig *config)
     393{
     394    PS_ASSERT_PTR_NON_NULL(config, false);
     395
     396    // required
     397    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     398    PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", true, false);
     399    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
     400
     401    // add a flatcorrCamLink (initial state has include = TRUE)
     402    // XXX should add checks that the chip_id and corr_id are in ChipLink
     403    if (!flatcorrCamLinkInsert(config->dbh, corr_id, chip_id, cam_id, 1)) {
     404        psError(PS_ERR_UNKNOWN, false, "database error");
     405        return false;
     406    }
     407
     408    return true;
     409}
     410
     411static bool dropcameraMode(pxConfig *config)
     412{
     413    PS_ASSERT_PTR_NON_NULL(config, false);
     414
     415    // required
     416    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     417    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
     418
     419    // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
     420    psString query = pxDataGet("flatcorr_dropcamera.sql");
     421    if (!query) {
     422        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     423        return false;
     424    }
     425
     426    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
    372427        psError(PS_ERR_UNKNOWN, false, "database error");
    373428        psFree(query);
     
    380435// select the flatcorr chip runs that have completed and for which there is no camera entry
    381436// queue a new camera run for them
    382 static bool addcameraMode(pxConfig *config)
     437static bool advancecameraMode(pxConfig *config)
    383438{
    384439    PS_ASSERT_PTR_NON_NULL(config, false);
     
    388443    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    389444
     445    psMetadata *where = psMetadataAlloc();
     446    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
     447
    390448    psString query = pxDataGet("flatcorr_chiprundone.sql");
    391449    if (!query) {
    392         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    393         return false;
    394     }
     450        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     451        return false;
     452    }
     453
     454    if (where && psListLength(where->list)) {
     455        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     456        psStringAppend(&query, " AND %s", whereClause);
     457        psFree(whereClause);
     458    }
     459    psFree(where);
    395460
    396461    // treat limit == 0 as "no limit"
    397462    if (limit) {
    398         psString limitString = psDBGenerateLimitSQL(limit);
    399         psStringAppend(&query, " %s", limitString);
    400         psFree(limitString);
     463        psString limitString = psDBGenerateLimitSQL(limit);
     464        psStringAppend(&query, " %s", limitString);
     465        psFree(limitString);
    401466    }
    402467
     
    459524        // queue the exp
    460525        if (!pxcamQueueByChipID(
    461                 config,
    462                 row->chip_id,
    463                 row->workdir,
    464                 row->label,
    465                 row->reduction,
    466                 row->expgroup,
    467                 row->dvodb,
    468                 row->tess_id,
    469                 "camera")) {
     526                config,
     527                row->chip_id,
     528                row->workdir,
     529                row->label,
     530                row->data_group,
     531                row->dist_group,
     532                row->reduction,
     533                row->expgroup,
     534                row->dvodb,
     535                row->tess_id,
     536                "camera",
     537                row->magicked,
     538                NULL // note does not propragate
     539                )) {
    470540            if (!psDBRollback(config->dbh)) {
    471541                psError(PS_ERR_UNKNOWN, false, "database error");
     
    478548        }
    479549
    480         // figure out the ID of the flatcorrRun we just created
    481         psS64 cam_id = psDBLastInsertID(config->dbh);
    482 
    483         // add the camRun entry to the flatcorrCamLink table (include is TRUE)
     550        // figure out the ID of the flatcorrRun we just created
     551        psS64 cam_id = psDBLastInsertID(config->dbh);
     552
     553        // add the camRun entry to the flatcorrCamLink table (include is TRUE)
    484554        if (!flatcorrCamLinkInsert(config->dbh, corr_id, row->chip_id, cam_id, 1)) {
    485555            if (!psDBRollback(config->dbh)) {
     
    489559            return false;
    490560        }
    491        
     561
    492562        psFree(row);
    493563    }
     
    502572}
    503573
    504 static bool dropcameraMode(pxConfig *config)
    505 {
    506     PS_ASSERT_PTR_NON_NULL(config, false);
    507 
    508     // required
    509     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    510     PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
    511 
    512     // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
    513     psString query = pxDataGet("flatcorr_dropcamera.sql");
    514     if (!query) {
    515         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    516         return false;
    517     }
    518 
    519     if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
    520         psError(PS_ERR_UNKNOWN, false, "database error");
    521         psFree(query);
    522         return false;
    523     }
    524 
    525     return true;
    526 }
    527 
    528 // select the flatcorr chip runs that have completed and for which there is no camera entry
    529 // queue a new camera run for them
    530 static bool pendingprocessMode(pxConfig *config)
     574// Select the flatcorr camera runs that have completed and for which there is no addstar
     575// entry.  Queue a new addstar run for them
     576static bool advanceaddstarMode(pxConfig *config)
    531577{
    532578    PS_ASSERT_PTR_NON_NULL(config, false);
     
    534580    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
    535581    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     582    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    536583
    537584    psMetadata *where = psMetadataAlloc();
    538     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
    539 
    540     psString query = pxDataGet("flatcorr_pendingprocess.sql");
     585    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
     586
     587    psString query = pxDataGet("flatcorr_camerarundone.sql");
    541588    if (!query) {
    542         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    543         return false;
     589        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     590        return false;
    544591    }
    545592
     
    553600    // treat limit == 0 as "no limit"
    554601    if (limit) {
    555         psString limitString = psDBGenerateLimitSQL(limit);
    556         psStringAppend(&query, " %s", limitString);
    557         psFree(limitString);
     602        psString limitString = psDBGenerateLimitSQL(limit);
     603        psStringAppend(&query, " %s", limitString);
     604        psFree(limitString);
    558605    }
    559606
     
    575622    }
    576623
     624    if (pretend) {
     625        // negative simple so the default is true
     626        if (!ippdbPrintMetadatas(stdout, output, "flatcorr_addcamera", !simple)) {
     627            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     628            psFree(output);
     629            return false;
     630        }
     631    }
     632
     633    // start a transaction so we don't end up with an exp without any associted
     634    // imfiles
     635    if (!psDBTransaction(config->dbh)) {
     636        psError(PS_ERR_UNKNOWN, false, "database error");
     637        psFree(output);
     638        return false;
     639    }
     640
     641    // loop over our list of chipRun rows
     642    for (long i = 0; i < psArrayLength(output); i++) {
     643        psMetadata *md = output->data[i];
     644
     645        bool status;
     646        psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id");
     647        if (!status) {
     648            if (!psDBRollback(config->dbh)) {
     649                psError(PS_ERR_UNKNOWN, false, "database error");
     650            }
     651            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for corr_id");
     652            psFree(output);
     653            return false;
     654        }
     655
     656        camRunRow *row = camRunObjectFromMetadata(md);
     657        if (!row) {
     658            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
     659            psFree(output);
     660            return false;
     661        }
     662
     663        // queue the exp : force image_only to be false (flatcorr is meaningless with just image info)
     664        if (!pxaddQueueByCamID(
     665                config,
     666                row->cam_id,
     667                row->workdir,
     668                row->reduction,
     669                row->label,
     670                row->data_group,
     671                row->dvodb,
     672                NULL,       // note is not propagated
     673                0)) {
     674            if (!psDBRollback(config->dbh)) {
     675                psError(PS_ERR_UNKNOWN, false, "database error");
     676            }
     677            psError(PS_ERR_UNKNOWN, false,
     678                    "failed to trying to queue cam_id: %" PRId64, row->cam_id);
     679            psFree(row);
     680            psFree(output);
     681            return false;
     682        }
     683
     684        // figure out the ID of the flatcorrRun we just created
     685        psS64 add_id = psDBLastInsertID(config->dbh);
     686
     687        // add the addRun entry to the flatcorrAddstarLink table (include is TRUE)
     688        if (!flatcorrAddstarLinkInsert(config->dbh, corr_id, row->cam_id, add_id, 1)) {
     689            if (!psDBRollback(config->dbh)) {
     690                psError(PS_ERR_UNKNOWN, false, "database error");
     691            }
     692            psError(PS_ERR_UNKNOWN, false, "database error");
     693            return false;
     694        }
     695        psFree(row);
     696    }
     697    psFree(output);
     698
     699    if (!psDBCommit(config->dbh)) {
     700        psError(PS_ERR_UNKNOWN, false, "database error");
     701        return false;
     702    }
     703
     704    return false;
     705}
     706
     707// select the flatcorr chip runs that have completed and for which there is no camera entry
     708// queue a new camera run for them
     709static bool pendingprocessMode(pxConfig *config)
     710{
     711    PS_ASSERT_PTR_NON_NULL(config, false);
     712
     713    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     714    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     715
     716    psMetadata *where = psMetadataAlloc();
     717    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     718
     719    psString query = pxDataGet("flatcorr_pendingprocess.sql");
     720    if (!query) {
     721        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     722        return false;
     723    }
     724
     725    if (where && psListLength(where->list)) {
     726        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     727        psStringAppend(&query, " AND %s", whereClause);
     728        psFree(whereClause);
     729    }
     730    psFree(where);
     731
     732    // treat limit == 0 as "no limit"
     733    if (limit) {
     734        psString limitString = psDBGenerateLimitSQL(limit);
     735        psStringAppend(&query, " %s", limitString);
     736        psFree(limitString);
     737    }
     738
     739    if (!p_psDBRunQuery(config->dbh, query)) {
     740        psError(PS_ERR_UNKNOWN, false, "database error");
     741        psFree(query);
     742        return false;
     743    }
     744
     745    psArray *output = p_psDBFetchResult(config->dbh);
     746    if (!output) {
     747        psError(PS_ERR_UNKNOWN, false, "database error");
     748        return false;
     749    }
     750    if (!psArrayLength(output)) {
     751        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     752        psFree(output);
     753        return true;
     754    }
     755
    577756    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
    578         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    579         psFree(output);
    580         return false;
     757        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     758        psFree(output);
     759        return false;
     760    }
     761
     762    return true;
     763}
     764
     765// XXX need a fault state
     766static bool addprocessMode(pxConfig *config)
     767{
     768    PS_ASSERT_PTR_NON_NULL(config, false);
     769
     770    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     771    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
     772    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
     773
     774    char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
     775
     776    if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
     777        psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
     778        return false;
     779    }
     780
     781    return true;
     782}
     783
     784static bool updaterunMode(pxConfig *config)
     785{
     786    PS_ASSERT_PTR_NON_NULL(config, false);
     787
     788    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     789    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     790
     791    if (!setflatcorrRunState(config, corr_id, state)) {
     792        if (!psDBRollback(config->dbh)) {
     793            psError(PS_ERR_UNKNOWN, false, "database error");
     794        }
     795        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
     796        return false;
    581797    }
    582798
     
    598814    // treat limit == 0 as "no limit"
    599815    if (limit) {
    600         psString limitString = psDBGenerateLimitSQL(limit);
    601         psStringAppend(&query, " %s", limitString);
    602         psFree(limitString);
     816        psString limitString = psDBGenerateLimitSQL(limit);
     817        psStringAppend(&query, " %s", limitString);
     818        psFree(limitString);
    603819    }
    604820
     
    621837
    622838    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
    623         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    624         psFree(output);
    625         return false;
     839        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     840        psFree(output);
     841        return false;
    626842    }
    627843
     
    638854    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
    639855    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
    640    
     856
    641857    psMetadata *where = psMetadataAlloc();
    642858    PXOPT_COPY_S64(config->args, where, "-chip_id", "chipProcessedImfile.chip_id", "==");
     
    644860    psString query = pxDataGet("flatcorr_inputimfile.sql");
    645861    if (!query) {
    646         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     862        psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
    647863        return false;
    648864    }
     
    657873    // treat limit == 0 as "no limit"
    658874    if (limit) {
    659         psString limitString = psDBGenerateLimitSQL(limit);
    660         psStringAppend(&query, " %s", limitString);
    661         psFree(limitString);
     875        psString limitString = psDBGenerateLimitSQL(limit);
     876        psStringAppend(&query, " %s", limitString);
     877        psFree(limitString);
    662878    }
    663879
     
    680896
    681897    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
    682         psError(PS_ERR_UNKNOWN, false, "failed to print array");
    683         psFree(output);
    684         return false;
    685     }
    686 
    687     return true;
    688 }
    689 
    690 // XXX need a fault state
    691 static bool addprocessMode(pxConfig *config)
    692 {
    693     PS_ASSERT_PTR_NON_NULL(config, false);
    694 
    695     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    696     PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
    697     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
    698 
    699     char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
    700 
    701     if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
    702         psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
    703         return false;
    704     }
    705 
    706     return true;
    707 }
    708 
    709 static bool updaterunMode(pxConfig *config)
    710 {
    711     PS_ASSERT_PTR_NON_NULL(config, false);
    712 
    713     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    714     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    715 
    716     if (!setflatcorrRunState(config, corr_id, state)) {
    717         if (!psDBRollback(config->dbh)) {
    718             psError(PS_ERR_UNKNOWN, false, "database error");
    719         }
    720         psError(PS_ERR_UNKNOWN, false, "failed to set run state");
     898        psError(PS_ERR_UNKNOWN, false, "failed to print array");
     899        psFree(output);
    721900        return false;
    722901    }
     
    731910
    732911    // check that state is a valid string value
    733     if (!strcmp(state, "reg") && 
    734         !strcmp(state, "new") &&
    735         !strcmp(state, "full"))
     912    if (!strcmp(state, "reg") &&
     913        !strcmp(state, "new") &&
     914        !strcmp(state, "full"))
    736915    {
    737916        psError(PS_ERR_UNKNOWN, false, "invalid state: %s", state);
     
    748927    return true;
    749928}
    750 
    751 bool exportrunMode(pxConfig *config)
    752 {
    753   typedef struct ExportTable {
    754     char tableName[80];
    755     char sqlFilename[80];
    756   } ExportTable;
    757  
    758   int numExportTables = 3;
    759 
    760   PS_ASSERT_PTR_NON_NULL(config, NULL);
    761 
    762   PXOPT_LOOKUP_S64(det_id, config->args, "-corr_id", true,  false);
    763   PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    764   PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
    765 
    766   FILE *f = fopen (outfile, "w");
    767   if (f == NULL) {
    768     psError(PS_ERR_UNKNOWN, false, "failed to open output file");
    769     return false;
    770   }
    771 
    772   psMetadata *where = psMetadataAlloc();
    773   PXOPT_COPY_S64(config->args, where, "-corr_id", "corr_id", "==");
    774 
    775   ExportTable tables [] = {
    776     {"flatcorrRun", "flatcorr_export_run.sql"},
    777     {"flatcorrCamLink", "flatcorr_export_cam_link.sql"},
    778     {"flatcorrChipLink", "flatcorr_export_chip_link.sql"},
    779   };
    780 
    781   for (int i=0; i < numExportTables; i++) {
    782     psString query = pxDataGet(tables[i].sqlFilename);
    783     if (!query) {
    784       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    785       return false;
    786     }
    787 
    788     if (where && psListLength(where->list)) {
    789       psString whereClause = psDBGenerateWhereSQL(where, NULL);
    790       psStringAppend(&query, " %s", whereClause);
    791       psFree(whereClause);
    792     }
    793 
    794     // treat limit == 0 as "no limit"
    795     if (limit) {
    796       psString limitString = psDBGenerateLimitSQL(limit);
    797       psStringAppend(&query, " %s", limitString);
    798       psFree(limitString);
    799     }
    800 
    801     if (!p_psDBRunQuery(config->dbh, query)) {
    802       psError(PS_ERR_UNKNOWN, false, "database error");
    803       psFree(query);
    804       return false;
    805     }
    806     psFree(query);
    807 
    808     psArray *output = p_psDBFetchResult(config->dbh);
    809     if (!output) {
    810       psError(PS_ERR_UNKNOWN, false, "database error");
    811       return false;
    812     }
    813     if (!psArrayLength(output)) {
    814       psTrace("regtool", PS_LOG_INFO, "no rows found");
    815       psFree(output);
    816       return true;
    817     }
    818 
    819     // we must write the export table in non-simple (true) format
    820     if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
    821       psError(PS_ERR_UNKNOWN, false, "failed to print array");
    822       psFree(output);
    823       return false;
    824     }
    825     psFree(output);
    826   }
    827 
    828     fclose (f);
    829 
    830     return true;
    831 }
    832 
    833 bool importrunMode(pxConfig *config)
    834 {
    835   unsigned int nFail;
    836   psMetadataItem *item, *entry;
    837  
    838   int numImportTables = 3;
    839  
    840   char tables[3] [80] = {"flatcorrRun", "flatcorrCamLink", "flatcorrChipLink"};
    841 
    842   PS_ASSERT_PTR_NON_NULL(config, NULL);
    843  
    844   PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
    845 
    846   psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
    847 
    848   fprintf (stdout, "---- input ----\n");
    849   psMetadataPrint (stderr, input, 1);
    850 
    851   for (int i = 0; i < numImportTables; i++) {
    852     item = psMetadataLookup (input, tables[i]);
    853     psAssert (item, "entry not in input?");
    854     psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
    855    
    856     entry = psListGet (item->data.list, 0);
    857     assert (entry);
    858     assert (entry->type == PS_DATA_METADATA);
    859  
    860     switch (i) {
    861       case 0:
    862       {
    863         flatcorrRunRow *flatcorrRun = flatcorrRunObjectFromMetadata (entry->data.md);
    864         flatcorrRunInsertObject (config->dbh, flatcorrRun);
    865 
    866         // fprintf (stdout, "---- flatcorr run ----\n");
    867         // psMetadataPrint (stderr, entry->data.md, 1);
    868         break;
    869       }
    870       case 1:
    871       {
    872         flatcorrCamLinkRow *flatcorrCamLink = flatcorrCamLinkObjectFromMetadata (entry->data.md);
    873         flatcorrCamLinkInsertObject (config->dbh, flatcorrCamLink);
    874 
    875         // fprintf (stdout, "---- flatcorr cam link ----\n");
    876         // psMetadataPrint (stderr, entry->data.md, 1);
    877         break;
    878       }
    879       case 2:
    880       {
    881         flatcorrChipLinkRow *flatcorrChipLink = flatcorrChipLinkObjectFromMetadata (entry->data.md);
    882         flatcorrChipLinkInsertObject (config->dbh, flatcorrChipLink);
    883 
    884         // fprintf (stdout, "---- flatcorr chip link ----\n");
    885         // psMetadataPrint (stderr, entry->data.md, 1);
    886         break;
    887       }
    888     }
    889   }
    890   return true;
    891 }
Note: See TracChangeset for help on using the changeset viewer.