IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 8, 2009, 5:09:53 PM (17 years ago)
Author:
eugene
Message:

updates to the addstar stage; modify flatcorr to interact correctly with the addstar stage

File:
1 edited

Legend:

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

    r25784 r25816  
    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);
     44static bool dropcameraMode(pxConfig *config);
    4145static bool advancecameraMode(pxConfig *config);
    42 static bool dropchipMode(pxConfig *config);
    43 static bool dropcameraMode(pxConfig *config);
     46static bool advanceaddstarMode(pxConfig *config);
    4447static bool pendingprocessMode(pxConfig *config);
    4548static bool addprocessMode(pxConfig *config);
     
    4750static bool inputexpMode(pxConfig *config);
    4851static bool inputimfileMode(pxConfig *config);
    49 static bool exportrunMode(pxConfig *config);
    50 static bool importrunMode(pxConfig *config);
    5152
    5253static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state);
     
    7374        MODECASE(FLATCORR_MODE_DEFINERUN,      definerunMode);
    7475        MODECASE(FLATCORR_MODE_ADDCHIP,        addchipMode);
     76        MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
    7577        MODECASE(FLATCORR_MODE_ADDCAMERA,      addcameraMode);
     78        MODECASE(FLATCORR_MODE_DROPCAMERA,     dropcameraMode);
    7679        MODECASE(FLATCORR_MODE_ADVANCECAMERA,  advancecameraMode);
    77         MODECASE(FLATCORR_MODE_DROPCHIP,       dropchipMode);
    78         MODECASE(FLATCORR_MODE_DROPCAMERA,     dropcameraMode);
     80        MODECASE(FLATCORR_MODE_ADVANCEADDSTAR, advanceaddstarMode);
    7981        MODECASE(FLATCORR_MODE_PENDINGPROCESS, pendingprocessMode);
    8082        MODECASE(FLATCORR_MODE_ADDPROCESS,     addprocessMode);
     
    8284        MODECASE(FLATCORR_MODE_INPUTEXP,       inputexpMode);
    8385        MODECASE(FLATCORR_MODE_INPUTIMFILE,    inputimfileMode);
    84         MODECASE(FLATCORR_MODE_EXPORTRUN,      exportrunMode);
    85         MODECASE(FLATCORR_MODE_IMPORTRUN,      importrunMode);
    8686        default:
    8787            psAbort("invalid option (this should not happen)");
     
    404404}
    405405
     406static bool dropcameraMode(pxConfig *config)
     407{
     408    PS_ASSERT_PTR_NON_NULL(config, false);
     409
     410    // required
     411    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     412    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
     413
     414    // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
     415    psString query = pxDataGet("flatcorr_dropcamera.sql");
     416    if (!query) {
     417        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     418        return false;
     419    }
     420
     421    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
     422        psError(PS_ERR_UNKNOWN, false, "database error");
     423        psFree(query);
     424        return false;
     425    }
     426
     427    return true;
     428}
     429
    406430// select the flatcorr chip runs that have completed and for which there is no camera entry
    407431// queue a new camera run for them
     
    413437    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
    414438    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     439
     440    psMetadata *where = psMetadataAlloc();
     441    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
    415442
    416443    psString query = pxDataGet("flatcorr_chiprundone.sql");
     
    419446        return false;
    420447    }
     448
     449    if (where && psListLength(where->list)) {
     450        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     451        psStringAppend(&query, " AND %s", whereClause);
     452        psFree(whereClause);
     453    }
     454    psFree(where);
    421455
    422456    // treat limit == 0 as "no limit"
     
    528562}
    529563
    530 static bool dropcameraMode(pxConfig *config)
    531 {
    532     PS_ASSERT_PTR_NON_NULL(config, false);
    533 
    534     // required
    535     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    536     PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
    537 
    538     // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
    539     psString query = pxDataGet("flatcorr_dropcamera.sql");
    540     if (!query) {
    541         psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    542         return false;
    543     }
    544 
    545     if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
    546         psError(PS_ERR_UNKNOWN, false, "database error");
    547         psFree(query);
    548         return false;
    549     }
    550 
    551     return true;
    552 }
    553 
    554 // select the flatcorr chip runs that have completed and for which there is no camera entry
    555 // queue a new camera run for them
    556 static bool pendingprocessMode(pxConfig *config)
     564// Select the flatcorr camera runs that have completed and for which there is no addstar
     565// entry.  Queue a new addstar run for them
     566static bool advanceaddstarMode(pxConfig *config)
    557567{
    558568    PS_ASSERT_PTR_NON_NULL(config, false);
     
    560570    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
    561571    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     572    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    562573
    563574    psMetadata *where = psMetadataAlloc();
    564     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
    565 
    566     psString query = pxDataGet("flatcorr_pendingprocess.sql");
     575    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
     576
     577    psString query = pxDataGet("flatcorr_camerarundone.sql");
    567578    if (!query) {
    568579        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    601612    }
    602613
     614    if (pretend) {
     615        // negative simple so the default is true
     616        if (!ippdbPrintMetadatas(stdout, output, "flatcorr_addcamera", !simple)) {
     617            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     618            psFree(output);
     619            return false;
     620        }
     621    }
     622
     623    // start a transaction so we don't end up with an exp without any associted
     624    // imfiles
     625    if (!psDBTransaction(config->dbh)) {
     626        psError(PS_ERR_UNKNOWN, false, "database error");
     627        psFree(output);
     628        return false;
     629    }
     630
     631    // loop over our list of chipRun rows
     632    for (long i = 0; i < psArrayLength(output); i++) {
     633        psMetadata *md = output->data[i];
     634
     635        bool status;
     636        psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id");
     637        if (!status) {
     638            if (!psDBRollback(config->dbh)) {
     639                psError(PS_ERR_UNKNOWN, false, "database error");
     640            }
     641            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for corr_id");
     642            psFree(output);
     643            return false;
     644        }
     645
     646        camRunRow *row = camRunObjectFromMetadata(md);
     647        if (!row) {
     648            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
     649            psFree(output);
     650            return false;
     651        }
     652
     653        // queue the exp : force image_only to be false (flatcorr is meaningless with just image info)
     654        if (!pxaddQueueByCamID(
     655                config,
     656                row->cam_id,
     657                row->workdir,
     658                row->reduction,
     659                row->label,
     660                row->dvodb,
     661                0)) {
     662            if (!psDBRollback(config->dbh)) {
     663                psError(PS_ERR_UNKNOWN, false, "database error");
     664            }
     665            psError(PS_ERR_UNKNOWN, false,
     666                    "failed to trying to queue cam_id: %" PRId64, row->cam_id);
     667            psFree(row);
     668            psFree(output);
     669            return false;
     670        }
     671
     672        // figure out the ID of the flatcorrRun we just created
     673        psS64 add_id = psDBLastInsertID(config->dbh);
     674
     675        // add the addRun entry to the flatcorrAddstarLink table (include is TRUE)
     676        if (!flatcorrAddstarLinkInsert(config->dbh, corr_id, row->cam_id, add_id, 1)) {
     677            if (!psDBRollback(config->dbh)) {
     678                psError(PS_ERR_UNKNOWN, false, "database error");
     679            }
     680            psError(PS_ERR_UNKNOWN, false, "database error");
     681            return false;
     682        }
     683        psFree(row);
     684    }
     685    psFree(output);
     686
     687    if (!psDBCommit(config->dbh)) {
     688        psError(PS_ERR_UNKNOWN, false, "database error");
     689        return false;
     690    }
     691
     692    return false;
     693}
     694
     695// select the flatcorr chip runs that have completed and for which there is no camera entry
     696// queue a new camera run for them
     697static bool pendingprocessMode(pxConfig *config)
     698{
     699    PS_ASSERT_PTR_NON_NULL(config, false);
     700
     701    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     702    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     703
     704    psMetadata *where = psMetadataAlloc();
     705    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     706
     707    psString query = pxDataGet("flatcorr_pendingprocess.sql");
     708    if (!query) {
     709        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     710        return false;
     711    }
     712
     713    if (where && psListLength(where->list)) {
     714        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     715        psStringAppend(&query, " AND %s", whereClause);
     716        psFree(whereClause);
     717    }
     718    psFree(where);
     719
     720    // treat limit == 0 as "no limit"
     721    if (limit) {
     722        psString limitString = psDBGenerateLimitSQL(limit);
     723        psStringAppend(&query, " %s", limitString);
     724        psFree(limitString);
     725    }
     726
     727    if (!p_psDBRunQuery(config->dbh, query)) {
     728        psError(PS_ERR_UNKNOWN, false, "database error");
     729        psFree(query);
     730        return false;
     731    }
     732
     733    psArray *output = p_psDBFetchResult(config->dbh);
     734    if (!output) {
     735        psError(PS_ERR_UNKNOWN, false, "database error");
     736        return false;
     737    }
     738    if (!psArrayLength(output)) {
     739        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     740        psFree(output);
     741        return true;
     742    }
     743
    603744    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
    604745        psError(PS_ERR_UNKNOWN, false, "failed to print array");
    605746        psFree(output);
    606747        return false;
     748    }
     749
     750    return true;
     751}
     752
     753// XXX need a fault state
     754static bool addprocessMode(pxConfig *config)
     755{
     756    PS_ASSERT_PTR_NON_NULL(config, false);
     757
     758    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     759    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
     760    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
     761
     762    char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
     763
     764    if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
     765        psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
     766        return false;
     767    }
     768
     769    return true;
     770}
     771
     772static bool updaterunMode(pxConfig *config)
     773{
     774    PS_ASSERT_PTR_NON_NULL(config, false);
     775
     776    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     777    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     778
     779    if (!setflatcorrRunState(config, corr_id, state)) {
     780        if (!psDBRollback(config->dbh)) {
     781            psError(PS_ERR_UNKNOWN, false, "database error");
     782        }
     783        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
     784        return false;
    607785    }
    608786
     
    714892}
    715893
    716 // XXX need a fault state
    717 static bool addprocessMode(pxConfig *config)
    718 {
    719     PS_ASSERT_PTR_NON_NULL(config, false);
    720 
    721     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    722     PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
    723     PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
    724 
    725     char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
    726 
    727     if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
    728         psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
    729         return false;
    730     }
    731 
    732     return true;
    733 }
    734 
    735 static bool updaterunMode(pxConfig *config)
    736 {
    737     PS_ASSERT_PTR_NON_NULL(config, false);
    738 
    739     PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
    740     PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
    741 
    742     if (!setflatcorrRunState(config, corr_id, state)) {
    743         if (!psDBRollback(config->dbh)) {
    744             psError(PS_ERR_UNKNOWN, false, "database error");
    745         }
    746         psError(PS_ERR_UNKNOWN, false, "failed to set run state");
    747         return false;
    748     }
    749 
    750     return true;
    751 }
    752 
    753894static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state)
    754895{
     
    774915    return true;
    775916}
    776 
    777 bool exportrunMode(pxConfig *config)
    778 {
    779   typedef struct ExportTable {
    780     char tableName[80];
    781     char sqlFilename[80];
    782   } ExportTable;
    783  
    784   int numExportTables = 3;
    785 
    786   PS_ASSERT_PTR_NON_NULL(config, NULL);
    787 
    788   PXOPT_LOOKUP_S64(det_id, config->args, "-corr_id", true,  false);
    789   PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    790   PXOPT_LOOKUP_U64(limit,   config->args, "-limit",   false, false);
    791 
    792   FILE *f = fopen (outfile, "w");
    793   if (f == NULL) {
    794     psError(PS_ERR_UNKNOWN, false, "failed to open output file");
    795     return false;
    796   }
    797 
    798   psMetadata *where = psMetadataAlloc();
    799   PXOPT_COPY_S64(config->args, where, "-corr_id", "corr_id", "==");
    800 
    801   ExportTable tables [] = {
    802     {"flatcorrRun", "flatcorr_export_run.sql"},
    803     {"flatcorrCamLink", "flatcorr_export_cam_link.sql"},
    804     {"flatcorrChipLink", "flatcorr_export_chip_link.sql"},
    805   };
    806 
    807   for (int i=0; i < numExportTables; i++) {
    808     psString query = pxDataGet(tables[i].sqlFilename);
    809     if (!query) {
    810       psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
    811       return false;
    812     }
    813 
    814     if (where && psListLength(where->list)) {
    815       psString whereClause = psDBGenerateWhereSQL(where, NULL);
    816       psStringAppend(&query, " %s", whereClause);
    817       psFree(whereClause);
    818     }
    819 
    820     // treat limit == 0 as "no limit"
    821     if (limit) {
    822       psString limitString = psDBGenerateLimitSQL(limit);
    823       psStringAppend(&query, " %s", limitString);
    824       psFree(limitString);
    825     }
    826 
    827     if (!p_psDBRunQuery(config->dbh, query)) {
    828       psError(PS_ERR_UNKNOWN, false, "database error");
    829       psFree(query);
    830       return false;
    831     }
    832     psFree(query);
    833 
    834     psArray *output = p_psDBFetchResult(config->dbh);
    835     if (!output) {
    836       psError(PS_ERR_UNKNOWN, false, "database error");
    837       return false;
    838     }
    839     if (!psArrayLength(output)) {
    840       psTrace("regtool", PS_LOG_INFO, "no rows found");
    841       psFree(output);
    842       return true;
    843     }
    844 
    845     // we must write the export table in non-simple (true) format
    846     if (!ippdbPrintMetadatas(f, output, tables[i].tableName, true)) {
    847       psError(PS_ERR_UNKNOWN, false, "failed to print array");
    848       psFree(output);
    849       return false;
    850     }
    851     psFree(output);
    852   }
    853 
    854     fclose (f);
    855 
    856     return true;
    857 }
    858 
    859 bool importrunMode(pxConfig *config)
    860 {
    861   unsigned int nFail;
    862   psMetadataItem *item, *entry;
    863  
    864   int numImportTables = 3;
    865  
    866   char tables[3] [80] = {"flatcorrRun", "flatcorrCamLink", "flatcorrChipLink"};
    867 
    868   PS_ASSERT_PTR_NON_NULL(config, NULL);
    869  
    870   PXOPT_LOOKUP_STR(infile, config->args, "-infile", true,  false);
    871 
    872   psMetadata *input = psMetadataConfigRead (NULL, &nFail, infile, false);
    873 
    874   fprintf (stdout, "---- input ----\n");
    875   psMetadataPrint (stderr, input, 1);
    876 
    877   for (int i = 0; i < numImportTables; i++) {
    878     item = psMetadataLookup (input, tables[i]);
    879     psAssert (item, "entry not in input?");
    880     psAssert (item->type == PS_DATA_METADATA_MULTI, "entry not multi?");
    881    
    882     entry = psListGet (item->data.list, 0);
    883     assert (entry);
    884     assert (entry->type == PS_DATA_METADATA);
    885  
    886     switch (i) {
    887       case 0:
    888       {
    889         flatcorrRunRow *flatcorrRun = flatcorrRunObjectFromMetadata (entry->data.md);
    890         flatcorrRunInsertObject (config->dbh, flatcorrRun);
    891 
    892         // fprintf (stdout, "---- flatcorr run ----\n");
    893         // psMetadataPrint (stderr, entry->data.md, 1);
    894         break;
    895       }
    896       case 1:
    897       {
    898         flatcorrCamLinkRow *flatcorrCamLink = flatcorrCamLinkObjectFromMetadata (entry->data.md);
    899         flatcorrCamLinkInsertObject (config->dbh, flatcorrCamLink);
    900 
    901         // fprintf (stdout, "---- flatcorr cam link ----\n");
    902         // psMetadataPrint (stderr, entry->data.md, 1);
    903         break;
    904       }
    905       case 2:
    906       {
    907         flatcorrChipLinkRow *flatcorrChipLink = flatcorrChipLinkObjectFromMetadata (entry->data.md);
    908         flatcorrChipLinkInsertObject (config->dbh, flatcorrChipLink);
    909 
    910         // fprintf (stdout, "---- flatcorr chip link ----\n");
    911         // psMetadataPrint (stderr, entry->data.md, 1);
    912         break;
    913       }
    914     }
    915   }
    916   return true;
    917 }
Note: See TracChangeset for help on using the changeset viewer.