IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 18, 2009, 10:23:28 AM (17 years ago)
Author:
eugene
Message:

updated vysos branch from trunk

Location:
branches/eam_branches/20090820
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090820

  • branches/eam_branches/20090820/ippTools/src/flatcorr.c

    r24140 r25870  
    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)");
     
    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_STR(note, config->args, "-set_note", false, false);
    133136
    134137    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     
    233236
    234237        // 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");
     238        psS64 chip_id = pxchipQueueByExpTag(config, exp_id, workdir, label, label, NULL, reduction, expgroup, dvodb, tess_id, "chip", note);
    236239        if (!chip_id) {
    237240            if (!psDBRollback(config->dbh)) {
     
    297300    // start a transaction so we don't end up with an exp without any associted
    298301    // imfiles
    299     if (!psDBTransaction(config->dbh)) {
    300         psError(PS_ERR_UNKNOWN, false, "database error");
    301         return false;
    302     }
     302    // if (!psDBTransaction(config->dbh)) {
     303    //     psError(PS_ERR_UNKNOWN, false, "database error");
     304    //     return false;
     305    // }
    303306
    304307    // create a new flatcorrRun
     
    333336    row->corr_id = corr_id;
    334337
     338    // if (!psDBCommit(config->dbh)) {
     339    //     psError(PS_ERR_UNKNOWN, false, "database error");
     340    //     return false;
     341    // }
     342
    335343    flatcorrRunPrintObject (stdout, row, !simple);
    336344    return true;
     
    378386}
    379387
     388static bool addcameraMode(pxConfig *config)
     389{
     390    PS_ASSERT_PTR_NON_NULL(config, false);
     391
     392    // required
     393    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     394    PXOPT_LOOKUP_S64(chip_id, config->args, "-chip_id", true, false);
     395    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
     396
     397    // add a flatcorrCamLink (initial state has include = TRUE)
     398    // XXX should add checks that the chip_id and corr_id are in ChipLink
     399    if (!flatcorrCamLinkInsert(config->dbh, corr_id, chip_id, cam_id, 1)) {
     400        psError(PS_ERR_UNKNOWN, false, "database error");
     401        return false;
     402    }
     403
     404    return true;
     405}
     406
     407static bool dropcameraMode(pxConfig *config)
     408{
     409    PS_ASSERT_PTR_NON_NULL(config, false);
     410
     411    // required
     412    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     413    PXOPT_LOOKUP_S64(cam_id, config->args, "-cam_id", true, false);
     414
     415    // UPDATE flatcorrCamLink set include = 0 where corr_id = %lld AND cam_id = %lld
     416    psString query = pxDataGet("flatcorr_dropcamera.sql");
     417    if (!query) {
     418        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     419        return false;
     420    }
     421
     422    if (!p_psDBRunQueryF(config->dbh, query, (long long) corr_id, (long long) cam_id)) {
     423        psError(PS_ERR_UNKNOWN, false, "database error");
     424        psFree(query);
     425        return false;
     426    }
     427
     428    return true;
     429}
     430
    380431// select the flatcorr chip runs that have completed and for which there is no camera entry
    381432// queue a new camera run for them
    382 static bool addcameraMode(pxConfig *config)
     433static bool advancecameraMode(pxConfig *config)
    383434{
    384435    PS_ASSERT_PTR_NON_NULL(config, false);
     
    387438    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
    388439    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
     440
     441    psMetadata *where = psMetadataAlloc();
     442    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
    389443
    390444    psString query = pxDataGet("flatcorr_chiprundone.sql");
     
    393447        return false;
    394448    }
     449
     450    if (where && psListLength(where->list)) {
     451        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     452        psStringAppend(&query, " AND %s", whereClause);
     453        psFree(whereClause);
     454    }
     455    psFree(where);
    395456
    396457    // treat limit == 0 as "no limit"
     
    463524                row->workdir,
    464525                row->label,
     526                row->data_group,
     527                row->dist_group,
    465528                row->reduction,
    466529                row->expgroup,
    467530                row->dvodb,
    468531                row->tess_id,
    469                 "camera")) {
     532                "camera",
     533                NULL // note does not propragate
     534                )) {
    470535            if (!psDBRollback(config->dbh)) {
    471536                psError(PS_ERR_UNKNOWN, false, "database error");
     
    502567}
    503568
    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)
     569// Select the flatcorr camera runs that have completed and for which there is no addstar
     570// entry.  Queue a new addstar run for them
     571static bool advanceaddstarMode(pxConfig *config)
    531572{
    532573    PS_ASSERT_PTR_NON_NULL(config, false);
     
    534575    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
    535576    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     577    PXOPT_LOOKUP_BOOL(pretend, config->args, "-pretend", false);
    536578
    537579    psMetadata *where = psMetadataAlloc();
    538     pxAddLabelSearchArgs (config, where, "-label", "label", "==");
    539 
    540     psString query = pxDataGet("flatcorr_pendingprocess.sql");
     580    pxAddLabelSearchArgs (config, where, "-label", "flatcorrRun.label", "==");
     581
     582    psString query = pxDataGet("flatcorr_camerarundone.sql");
    541583    if (!query) {
    542584        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     
    575617    }
    576618
     619    if (pretend) {
     620        // negative simple so the default is true
     621        if (!ippdbPrintMetadatas(stdout, output, "flatcorr_addcamera", !simple)) {
     622            psError(PS_ERR_UNKNOWN, false, "failed to print array");
     623            psFree(output);
     624            return false;
     625        }
     626    }
     627
     628    // start a transaction so we don't end up with an exp without any associted
     629    // imfiles
     630    if (!psDBTransaction(config->dbh)) {
     631        psError(PS_ERR_UNKNOWN, false, "database error");
     632        psFree(output);
     633        return false;
     634    }
     635
     636    // loop over our list of chipRun rows
     637    for (long i = 0; i < psArrayLength(output); i++) {
     638        psMetadata *md = output->data[i];
     639
     640        bool status;
     641        psS64 corr_id = psMetadataLookupS64(&status, md, "corr_id");
     642        if (!status) {
     643            if (!psDBRollback(config->dbh)) {
     644                psError(PS_ERR_UNKNOWN, false, "database error");
     645            }
     646            psError(PS_ERR_UNKNOWN, false, "failed to lookup value for corr_id");
     647            psFree(output);
     648            return false;
     649        }
     650
     651        camRunRow *row = camRunObjectFromMetadata(md);
     652        if (!row) {
     653            psError(PS_ERR_UNKNOWN, false, "failed to convert metadata into chipRun");
     654            psFree(output);
     655            return false;
     656        }
     657
     658        // queue the exp : force image_only to be false (flatcorr is meaningless with just image info)
     659        if (!pxaddQueueByCamID(
     660                config,
     661                row->cam_id,
     662                row->workdir,
     663                row->reduction,
     664                row->label,
     665                row->data_group,
     666                row->dvodb,
     667                NULL,       // note is not propagated
     668                0)) {
     669            if (!psDBRollback(config->dbh)) {
     670                psError(PS_ERR_UNKNOWN, false, "database error");
     671            }
     672            psError(PS_ERR_UNKNOWN, false,
     673                    "failed to trying to queue cam_id: %" PRId64, row->cam_id);
     674            psFree(row);
     675            psFree(output);
     676            return false;
     677        }
     678
     679        // figure out the ID of the flatcorrRun we just created
     680        psS64 add_id = psDBLastInsertID(config->dbh);
     681
     682        // add the addRun entry to the flatcorrAddstarLink table (include is TRUE)
     683        if (!flatcorrAddstarLinkInsert(config->dbh, corr_id, row->cam_id, add_id, 1)) {
     684            if (!psDBRollback(config->dbh)) {
     685                psError(PS_ERR_UNKNOWN, false, "database error");
     686            }
     687            psError(PS_ERR_UNKNOWN, false, "database error");
     688            return false;
     689        }
     690        psFree(row);
     691    }
     692    psFree(output);
     693
     694    if (!psDBCommit(config->dbh)) {
     695        psError(PS_ERR_UNKNOWN, false, "database error");
     696        return false;
     697    }
     698
     699    return false;
     700}
     701
     702// select the flatcorr chip runs that have completed and for which there is no camera entry
     703// queue a new camera run for them
     704static bool pendingprocessMode(pxConfig *config)
     705{
     706    PS_ASSERT_PTR_NON_NULL(config, false);
     707
     708    PXOPT_LOOKUP_BOOL(simple,  config->args, "-simple",  false);
     709    PXOPT_LOOKUP_BOOL(limit,   config->args, "-limit",   false);
     710
     711    psMetadata *where = psMetadataAlloc();
     712    pxAddLabelSearchArgs (config, where, "-label", "label", "==");
     713
     714    psString query = pxDataGet("flatcorr_pendingprocess.sql");
     715    if (!query) {
     716        psError(PXTOOLS_ERR_DATA, false, "failed to retreive SQL statement");
     717        return false;
     718    }
     719
     720    if (where && psListLength(where->list)) {
     721        psString whereClause = psDBGenerateWhereConditionSQL(where, NULL);
     722        psStringAppend(&query, " AND %s", whereClause);
     723        psFree(whereClause);
     724    }
     725    psFree(where);
     726
     727    // treat limit == 0 as "no limit"
     728    if (limit) {
     729        psString limitString = psDBGenerateLimitSQL(limit);
     730        psStringAppend(&query, " %s", limitString);
     731        psFree(limitString);
     732    }
     733
     734    if (!p_psDBRunQuery(config->dbh, query)) {
     735        psError(PS_ERR_UNKNOWN, false, "database error");
     736        psFree(query);
     737        return false;
     738    }
     739
     740    psArray *output = p_psDBFetchResult(config->dbh);
     741    if (!output) {
     742        psError(PS_ERR_UNKNOWN, false, "database error");
     743        return false;
     744    }
     745    if (!psArrayLength(output)) {
     746        psTrace("flatcorr", PS_LOG_INFO, "no rows found");
     747        psFree(output);
     748        return true;
     749    }
     750
    577751    if (!ippdbPrintMetadatas(stdout, output, "flatcorrPending", !simple)) {
    578752        psError(PS_ERR_UNKNOWN, false, "failed to print array");
    579753        psFree(output);
    580754        return false;
     755    }
     756
     757    return true;
     758}
     759
     760// XXX need a fault state
     761static bool addprocessMode(pxConfig *config)
     762{
     763    PS_ASSERT_PTR_NON_NULL(config, false);
     764
     765    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     766    PXOPT_LOOKUP_STR(hostname, config->args, "-hostname", false, false);
     767    PXOPT_LOOKUP_S16(code, config->args, "-code", false, false);
     768
     769    char *query = "UPDATE flatcorrRun SET state = 'full', hostname = '%s', fault = '%hd' WHERE corr_id = %" PRId64;
     770
     771    if (!p_psDBRunQueryF(config->dbh, query, hostname, code, corr_id)) {
     772        psError(PS_ERR_UNKNOWN, false, "failed to change state for corr_id %" PRId64, corr_id);
     773        return false;
     774    }
     775
     776    return true;
     777}
     778
     779static bool updaterunMode(pxConfig *config)
     780{
     781    PS_ASSERT_PTR_NON_NULL(config, false);
     782
     783    PXOPT_LOOKUP_S64(corr_id, config->args, "-corr_id", true, false);
     784    PXOPT_LOOKUP_STR(state, config->args, "-state", true, false);
     785
     786    if (!setflatcorrRunState(config, corr_id, state)) {
     787        if (!psDBRollback(config->dbh)) {
     788            psError(PS_ERR_UNKNOWN, false, "database error");
     789        }
     790        psError(PS_ERR_UNKNOWN, false, "failed to set run state");
     791        return false;
    581792    }
    582793
     
    688899}
    689900
    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");
    721         return false;
    722     }
    723 
    724     return true;
    725 }
    726 
    727901static bool setflatcorrRunState(pxConfig *config, psS64 corr_id, const char *state)
    728902{
     
    748922    return true;
    749923}
    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.