IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 30, 2011, 9:36:02 AM (15 years ago)
Author:
eugene
Message:

merging changes from trunk

Location:
branches/eam_branches/ipp-20110213/ippToPsps/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20110213/ippToPsps/src

    • Property svn:ignore
      •  

        old new  
        99config.h.in
        1010stamp-h1
        11 ippToPspsVersionDefinitions.h
         11VersionDefinitions.h
         12detectionbatch
         13initbatch
         14stackbatch
         15
  • branches/eam_branches/ipp-20110213/ippToPsps/src/DetectionBatch.c

    r30193 r31083  
    11#include "DetectionBatch.h"
    22#include "DetectionBatchEnums.h"
     3
     4#include "Fits.h"
     5
    36
    47/**
     
    1821
    1922/**
     23  Writes results to XML file
     24  */
     25static bool writeResults(DetectionBatch* this, long minObjID, long maxObjID, long totalDetectionsOut) {
     26
     27    if (!this->base.resultsXmlDoc) return false;
     28
     29    xmlNodePtr rootNode = xmlDocGetRootElement(this->base.resultsXmlDoc);
     30    char tmp[100];
     31    sprintf(tmp, "%ld", minObjID);
     32    xmlNewChild(rootNode, NULL, BAD_CAST "minObjID", BAD_CAST tmp);
     33    sprintf(tmp, "%ld", maxObjID);
     34    xmlNewChild(rootNode, NULL, BAD_CAST "maxObjID", BAD_CAST tmp);
     35    sprintf(tmp, "%ld", totalDetectionsOut);
     36    xmlNewChild(rootNode, NULL, BAD_CAST "totalDetections", BAD_CAST tmp);
     37
     38    return true;
     39}
     40
     41/**
    2042  Does the work. Loops through the provided smf files, tallies the contents with the DVO database already connected to and generates a new FITS file of the fomat required by PSPS (i.e. a binary table per PSPS database table)
    2143  */
     
    2446    if (this->base.exitCode != PS_EXIT_SUCCESS) return this->base.exitCode;
    2547
    26     int status = 0;
    27     fitsfile *fitsIn;
    28 
    29     if (fits_open_file(&fitsIn, this->base.inputFiles[0], READONLY, &status)) {
    30 
    31         fits_report_error(stderr, status);
    32         return PS_EXIT_SYS_ERROR;
    33     }
    34 
    35     // get primary header and pull stuff out for later
    36     int nKeys;
    37     fits_get_hdrspace(fitsIn, &nKeys, NULL, &status);
    38 
    39     float zptObs, exposureTime;
    40     char filterType[20];
    41     double obsTime;
    42     double expStart;
    43     double expTime;
    44     status=0; fits_read_key(fitsIn, TFLOAT, "ZPT_OBS", &zptObs, NULL, &status);
    45     status=0; fits_read_key(fitsIn, TFLOAT, "EXPREQ", &exposureTime, NULL, &status);
    46     status=0; fits_read_key(fitsIn, TSTRING, "FILTERID", filterType, NULL, &status);
    47     status=0; fits_read_key(fitsIn, TDOUBLE, "MJD-OBS", &expStart, NULL, &status);
    48     status=0; fits_read_key(fitsIn, TDOUBLE, "EXPTIME", &expTime, NULL, &status);
    49     obsTime = expStart + (expTime/172800.0); // exp start plus half exp time (converted from secs to days)
    50 
    51     ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, 1, "FrameMeta", true);
     48    // open input FITS file
     49    Fits* fitsIn = existing_Fits(this->base.inputFiles[0], this->base.logger);
     50    if (fitsIn->getFilePtr(fitsIn) == NULL)  return PS_EXIT_SYS_ERROR;
     51
     52    // read some header values
     53    float zptObs; fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "ZPT_OBS", &zptObs);
     54    float exposureTime; fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "EXPREQ", &exposureTime);
     55    char filterType[20]; fitsIn->getHeaderKeyValue(fitsIn, TSTRING, "FILTERID", filterType);
     56    double expStart; fitsIn->getHeaderKeyValue(fitsIn, TDOUBLE, "MJD-OBS", &expStart);
     57    double expTime; fitsIn->getHeaderKeyValue(fitsIn, TDOUBLE, "EXPTIME", &expTime);
     58    double obsTime = expStart + (expTime/172800.0); // exp start plus half exp time (converted from secs to days)
     59
     60
     61    this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, 1, "FrameMeta", true);
    5262
    5363    // FrameMeta values
    54     fits_write_col(this->base.fitsOut, TLONG, FRAMEMETA_FRAMEID, 1, 1, 1, &this->expId, &status);
    55     fits_write_col(this->base.fitsOut, TSTRING, FRAMEMETA_FRAMENAME, 1, 1, 1, &(this->expName), &status);
    56     fits_write_col(this->base.fitsOut, TBYTE, FRAMEMETA_SURVEYID, 1, 1, 1, &this->base.surveyID, &status);
    57 
    5864    int8_t filterID = -1;
    59     if (!ippToPspsConfig_getFilterId(this->base.config, filterType, &filterID)) {
     65    if (!this->base.initData->getFilterId(this->base.initData, filterType, &filterID)) {
    6066   
    6167        this->base.exitCode = PS_EXIT_DATA_ERROR;
    6268        return this->base.exitCode;
    6369    }
    64 
    65     fits_write_col(this->base.fitsOut, TBYTE, FRAMEMETA_FILTERID, 1, 1, 1, &filterID, &status);
     70    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, FRAMEMETA_FILTERID, 1, 1, 1, &filterID);
     71    this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, FRAMEMETA_FRAMEID, 1, 1, 1, &this->expId);
     72    this->base.fitsOut->writeColumn(this->base.fitsOut, TSTRING, FRAMEMETA_FRAMENAME, 1, 1, 1, &(this->expName));
     73    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, FRAMEMETA_SURVEYID, 1, 1, 1, &this->base.surveyID);
     74
    6675
    6776    int16_t cameraID = 1; // TODO
    68     fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_CAMERAID, 1, 1, 1, &cameraID, &status);
    69 
    7077    int16_t cameraConfigID = 1; // TODO
    71     fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_CAMERACONFIGID, 1, 1, 1, &cameraConfigID, &status);
    72 
    7378    int16_t telescopeID = 1; // TODO
    74     fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_TELESCOPEID, 1, 1, 1, &telescopeID, &status);
     79    this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, FRAMEMETA_CAMERAID, 1, 1, 1, &cameraID);
     80    this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, FRAMEMETA_CAMERACONFIGID, 1, 1, 1, &cameraConfigID);
     81    this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, FRAMEMETA_TELESCOPEID, 1, 1, 1, &telescopeID);
    7582
    7683    // stuff to keep from psf.hdr header
     
    8895    uint32_t s,d, invalidDvoRows, nChipDetectionsOut = 0;
    8996
    90     long longnull = -999;
    91     float floatnull = -999.0;
    92     int anynull = 0;
    93 
    9497    char ccdNumber[3], extensionName[15];
    95     // for storing FITS column data
     98    // for storing FITS column data - do this once, to avoid expension free/calloc for each chip
    9699    long* ippIDet = (long*)calloc(this->MAXDETECT, sizeof(long));
    97100    float* instMag = (float*)calloc(this->MAXDETECT, sizeof(float));
     
    110113    int8_t* filterIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
    111114    int8_t* surveyIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
    112 
    113115    char** assocDate = (char**)calloc(this->MAXDETECT, sizeof(char*));
    114116    for (uint32_t i=0; i<this->MAXDETECT;i++) assocDate[i] = (char*)malloc(20*sizeof(char));
     
    119121        filterIDs[s] = filterID;
    120122        surveyIDs[s] = this->base.surveyID;
    121         strcpy(assocDate[s], this->base.todaysDate);
     123
     124        // if running in test mode, don't use today's date
     125        if (this->base.testMode) strcpy(assocDate[s], "2010-01-01");
     126        else strcpy(assocDate[s], this->base.todaysDate);
    122127    }
    123128
     
    133138    int ippIDetNum, instMagNum, instMagErrNum, peakMagNum;
    134139    bool error = false;
    135 
    136     // loop round all 60 chips
    137     for (int x=0; x<8; x++) {
    138         for (int y=0; y<8; y++) {
     140    int startX, stopX, startY, stopY;
     141
     142    // in test mode, only run for chip 33
     143    if (this->base.testMode) {
     144
     145        startX = 3;
     146        stopX = 4;
     147        startY = 3;
     148        stopY = 4;
     149    }
     150    // in 'normal' mode, run for all chips
     151    else {
     152   
     153        startX = 0;
     154        stopX = 8;
     155        startY = 0;
     156        stopY = 8;
     157    }
     158
     159    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", 
     160            "+-----------+---------+----------+----------------+--------------+--------------+\n");
     161    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", 
     162            "| Extension | Rows in | Rows out |  Duplicate IDs | Invalid Flux | Bogus det ID |\n");
     163
     164    // loop round chips
     165    for (int x=startX; x<stopX; x++) {
     166        for (int y=startY; y<stopY; y++) {
    139167
    140168            // dodge the corners
     
    146174            sprintf(ccdNumber, "%d%d", x, y);
    147175
    148             // check we can move to detections table in smf
     176            // check we CAN move to detections table in smf
    149177            sprintf(extensionName, "XY%s.psf", ccdNumber);
    150             status=0;
    151             if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
    152                 psError(PS_ERR_IO, false, "Can't move to extension: %s skipping this chip\n", extensionName);
    153                 continue;
    154             }
     178            if (!fitsIn->moveToBinaryTable(fitsIn, extensionName)) continue;
    155179
    156180            // move to header extension
    157181            sprintf(extensionName, "XY%s.hdr", ccdNumber);
    158             status=0;
    159             if (fits_movnam_hdu(fitsIn, IMAGE_HDU, extensionName, 0, &status)) {
    160                 psError(PS_ERR_IO, false, "Can't move to extension: %s skipping this chip\n", extensionName);
    161                 continue;
    162             }
     182            if (!fitsIn->moveToHeader(fitsIn, extensionName)) continue;
    163183
    164184            // stuff to save from psf.hdr
    165             status=0; fits_read_key(fitsIn, TFLOAT, "FWHM_MAJ", &fwhmMaj, NULL, &status);
    166             status=0; fits_read_key(fitsIn, TFLOAT, "FWHM_MIN", &fwhmMin, NULL, &status);
    167             status=0; fits_read_key(fitsIn, TFLOAT, "IQ_FW1", &momentMaj, NULL, &status);
    168             status=0; fits_read_key(fitsIn, TFLOAT, "IQ_FW2", &momentMin, NULL, &status);
    169             status=0; fits_read_key(fitsIn, TLONG, "IMAGEID", &imageId, NULL, &status);
    170             status=0; fits_read_key(fitsIn, TLONG, "SOURCEID", &sourceId, NULL, &status);
    171             status=0; fits_read_key(fitsIn, TLONG, "NASTRO", &numPhotoRef, NULL, &status);
     185            fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "FWHM_MAJ", &fwhmMaj);
     186            fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "FWHM_MIN", &fwhmMin);
     187            fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "IQ_FW1", &momentMaj);
     188            fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "IQ_FW2", &momentMin);
     189            fitsIn->getHeaderKeyValue(fitsIn, TLONG, "IMAGEID", &imageId);
     190            fitsIn->getHeaderKeyValue(fitsIn, TLONG, "SOURCEID", &sourceId);
     191            fitsIn->getHeaderKeyValue(fitsIn, TLONG, "NASTRO", &numPhotoRef);
    172192            totalNumPhotoRef += numPhotoRef; // total up for storing in FrameMeta
     193
    173194            // access DVO database
    174195            skylist = dvoSkyListByExternID(this->base.dvoConfig, sourceId, imageId, &image);
    175196            if (skylist == NULL) {
    176                 psError(PS_ERR_IO, false,
     197                this->base.logger->print(this->base.logger, MSG_ERROR, "DetectionBatch", 
    177198                        "DVO: can't find SkyList for sourceId='%d' imageId='%d' (CCD = XY%s): skipping this chip\n",
    178199                        sourceId, imageId, ccdNumber);
     
    189210            if (numDvoDetections > this->MAXDETECT ) {
    190211
    191                 psError(PS_ERR_IO, false,
    192                         " Number of detections (%d) exceeds max limit (%ld)\n",
     212                this->base.logger->print(this->base.logger, MSG_ERROR, "DetectionBatch", "Number of detections (%d) exceeds max limit (%ld)\n",
    193213                        numDvoDetections, this->MAXDETECT);
    194214                error = true;
     
    197217
    198218            // create ImageMeta
    199             ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, 1, "ImageMeta", true);
     219            this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, 1, "ImageMeta", true);
    200220            psfFwhm = (fwhmMaj+fwhmMin)/2;
    201221            momentFwhm = (momentMaj+momentMin)/2;
    202222            imageFlags = (uint64_t)image->flags;
    203             fits_write_col(this->base.fitsOut, TLONGLONG, IMAGEMETA_IMAGEID, 1, 1, 1, &pspsImageId, &status);
    204             fits_write_col(this->base.fitsOut, TLONG, IMAGEMETA_FRAMEID, 1, 1, 1, &this->expId, &status);
    205             fits_write_col(this->base.fitsOut, TSHORT, IMAGEMETA_CCDID, 1, 1, 1, &image->ccdnum, &status);
    206             fits_write_col(this->base.fitsOut, TLONG, IMAGEMETA_PHOTOCALID, 1, 1, 1, &image->photcode, &status);
    207             fits_write_col(this->base.fitsOut, TBYTE, IMAGEMETA_FILTERID, 1, 1, 1, &filterID, &status);
    208             fits_write_col(this->base.fitsOut, TFLOAT, IMAGEMETA_PHOTOSCAT, 1, 1, 1, &zptObs, &status);
    209             fits_write_col(this->base.fitsOut, TFLOAT, IMAGEMETA_PSFFWHM, 1, 1, 1, &psfFwhm, &status);
    210             fits_write_col(this->base.fitsOut, TFLOAT, IMAGEMETA_MOMENTFWHM, 1, 1, 1, &momentFwhm, &status);
    211             fits_write_col(this->base.fitsOut, TFLOAT, IMAGEMETA_PHOTOZERO, 1, 1, 1, &zptObs, &status);
    212             fits_write_col(this->base.fitsOut, TLONGLONG, IMAGEMETA_QAFLAGS, 1, 1, 1, &imageFlags, &status);
     223            this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, IMAGEMETA_IMAGEID, 1, 1, 1, &pspsImageId);
     224            this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, IMAGEMETA_FRAMEID, 1, 1, 1, &this->expId);
     225            this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, IMAGEMETA_CCDID, 1, 1, 1, &image->ccdnum);
     226            this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, IMAGEMETA_PHOTOCALID, 1, 1, 1, &image->photcode);
     227            this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, IMAGEMETA_FILTERID, 1, 1, 1, &filterID);
     228            this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, IMAGEMETA_PHOTOSCAT, 1, 1, 1, &zptObs);
     229            this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, IMAGEMETA_PSFFWHM, 1, 1, 1, &psfFwhm);
     230            this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, IMAGEMETA_MOMENTFWHM, 1, 1, 1, &momentFwhm);
     231            this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, IMAGEMETA_PHOTOZERO, 1, 1, 1, &zptObs);
     232            this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, IMAGEMETA_QAFLAGS, 1, 1, 1, &imageFlags);
    213233
    214234            // now move BACK to detections table in smf
    215235            sprintf(extensionName, "XY%s.psf", ccdNumber);
    216             status=0;
    217             if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
    218                 psError(PS_ERR_IO, false, "Can't move to extension: %s skipping\n", extensionName);
    219                 continue;
    220             }
    221 
    222             // keep a running count of 'images' we find in order to write total to FrameMeta at the end
     236            long nChipDetectionsIn = 0;
     237            if (!fitsIn->moveToBinaryTableAndCountRows(fitsIn, extensionName, &nChipDetectionsIn)) continue;
     238
     239            // keep a running count of 'images' we find in order to write total into FrameMeta at the end
    223240            nOta++;
    224 
    225             long nChipDetectionsIn = 0;
    226             if (fits_get_num_rows(fitsIn, &nChipDetectionsIn, &status)) {
    227                 fits_report_error(stderr, status);
    228             }
    229241
    230242            // loop round detections to populate some extra stuff
    231243            s = d = nChipDetectionsOut = invalidDvoRows = 0;
    232244
    233             // determine column numbers for certain IPP detection columns
     245            // determine column numbers for certain IPP detection columns - do this only once to save time
    234246            if (firstTimeIn) {
    235247
    236                 status=0;fits_get_colnum(fitsIn, CASESEN, "IPP_IDET", &ippIDetNum, &status);
    237                 if (status) psError(PS_ERR_IO, false, "Unable to read col num for IPP_IDET");
    238                 status=0;fits_get_colnum(fitsIn, CASESEN, "PSF_INST_MAG", &instMagNum, &status);
    239                 if (status) psError(PS_ERR_IO, false, "Unable to read col num for PSF_INST_MAG");
    240                 status=0;fits_get_colnum(fitsIn, CASESEN, "PSF_INST_MAG_SIG", &instMagErrNum, &status);
    241                 if (status) psError(PS_ERR_IO, false, "Unable to read col num for PSF_INST_MAG_SIG");
    242                 status=0;fits_get_colnum(fitsIn, CASESEN, "PEAK_FLUX_AS_MAG", &peakMagNum, &status);
    243                 if (status) psError(PS_ERR_IO, false, "Unable to read col num for PEAK_FLUX_AS_MAG");
     248                fitsIn->getColumnNumber(fitsIn, "IPP_IDET", &ippIDetNum);
     249                fitsIn->getColumnNumber(fitsIn, "PSF_INST_MAG", &instMagNum);
     250                fitsIn->getColumnNumber(fitsIn, "PSF_INST_MAG_SIG", &instMagErrNum);
     251                fitsIn->getColumnNumber(fitsIn, "PEAK_FLUX_AS_MAG", &peakMagNum);
    244252
    245253                firstTimeIn=false;
    246254            }
    247255
    248             anynull = 0;
    249             fits_read_col(fitsIn, TLONG, ippIDetNum, 1, 1, nChipDetectionsIn, &longnull, ippIDet, &anynull, &status);
    250             fits_read_col(fitsIn, TFLOAT, instMagNum, 1, 1, nChipDetectionsIn, &floatnull, instMag, &anynull, &status);
    251             fits_read_col(fitsIn, TFLOAT, instMagErrNum, 1, 1, nChipDetectionsIn, &floatnull, instMagErr, &anynull, &status);
    252             fits_read_col(fitsIn, TFLOAT, peakMagNum, 1, 1, nChipDetectionsIn, &floatnull, peakMag, &anynull, &status);
     256            //anynull = 0;
     257            fitsIn->readColumn(fitsIn, TLONG, ippIDetNum, 1, 1, nChipDetectionsIn, ippIDet);
     258            fitsIn->readColumn(fitsIn, TFLOAT, instMagNum, 1, 1, nChipDetectionsIn, instMag);
     259            fitsIn->readColumn(fitsIn, TFLOAT, instMagErrNum, 1, 1, nChipDetectionsIn, instMagErr);
     260            fitsIn->readColumn(fitsIn, TFLOAT, peakMagNum, 1, 1, nChipDetectionsIn, peakMag);
    253261
    254262            // DVO detections are ordered by IPP_IDET, which increments from 0 in SMF table
     
    260268            // loop through all detections in smf file extension for this chip.
    261269            // there are three ways for a detection to be ommitted from the final FITS output:
     270            //
    262271            // 1. it has a duplicate obj ID to a previous detection
    263272            // 2. it has an invalid detection ID (< 0 or > max DVO det ID)
     
    312321
    313322                    // check for invalid flux values, and skip them
    314                     if ( !peakFluxOk || !instFluxOk) {
     323                    if (!peakFluxOk || !instFluxOk) {
    315324                        removeList[numOfDuplicates+numInvalidFlux+numOfInvalidIppIDet] = s+1;
    316325                        numInvalidFlux++;
     
    327336
    328337            // write number of rows (detections) to ImageMeta
    329             fits_write_col(this->base.fitsOut, TLONG, IMAGEMETA_NDETECT, 1, 1, 1, &nChipDetectionsOut, &status);
     338            this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, IMAGEMETA_NDETECT, 1, 1, 1, &nChipDetectionsOut);
    330339
    331340            int totalRemove = numOfDuplicates+numInvalidFlux+numOfInvalidIppIDet;
     
    335344
    336345                // detections
    337                 ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, nChipDetectionsIn, "Detection", false);
    338                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_OBJID, 1, 1, nChipDetectionsIn, objID, &status);
    339                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_DETECTID, 1, 1, nChipDetectionsIn, detectID, &status);
    340                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID, &status);
    341                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_IPPDETECTID, 1, 1, nChipDetectionsIn, ippDetectID, &status);
    342                 fits_write_col(this->base.fitsOut, TBYTE, DETECTION_FILTERID, 1, 1, nChipDetectionsIn, filterIDs, &status);
    343                 fits_write_col(this->base.fitsOut, TBYTE, DETECTION_SURVEYID, 1, 1, nChipDetectionsIn, surveyIDs, &status);
    344                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_IMAGEID, 1, 1, nChipDetectionsIn, imageID, &status);
    345                 fits_write_col(this->base.fitsOut, TDOUBLE, DETECTION_OBSTIME, 1, 1, nChipDetectionsIn, obsTimes, &status);
    346                 fits_write_col(this->base.fitsOut, TFLOAT, DETECTION_INSTFLUX, 1, 1, nChipDetectionsIn, instFlux, &status);
    347                 fits_write_col(this->base.fitsOut, TFLOAT, DETECTION_INSTFLUXERR, 1, 1, nChipDetectionsIn, instFluxErr, &status);
    348                 fits_write_col(this->base.fitsOut, TFLOAT, DETECTION_PEAKADU, 1, 1, nChipDetectionsIn, peakFlux, &status);
    349                 fits_write_col(this->base.fitsOut, TSTRING, DETECTION_ASSOCDATE, 1, 1, nChipDetectionsIn, assocDate, &status);
    350                 fits_write_col(this->base.fitsOut, TLONGLONG, DETECTION_INFOFLAG, 1, 1, nChipDetectionsIn, flags, &status);
    351                 if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet)
    352                     fits_delete_rowlist(this->base.fitsOut, removeList, totalRemove, &status);
     346                this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nChipDetectionsIn, "Detection", false);
     347                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_OBJID, 1, 1, nChipDetectionsIn, objID);
     348                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_DETECTID, 1, 1, nChipDetectionsIn, detectID);
     349                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID);
     350                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_IPPDETECTID, 1, 1, nChipDetectionsIn, ippDetectID);
     351                this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, DETECTION_FILTERID, 1, 1, nChipDetectionsIn, filterIDs);
     352                this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, DETECTION_SURVEYID, 1, 1, nChipDetectionsIn, surveyIDs);
     353                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_IMAGEID, 1, 1, nChipDetectionsIn, imageID);
     354                this->base.fitsOut->writeColumn(this->base.fitsOut, TDOUBLE, DETECTION_OBSTIME, 1, 1, nChipDetectionsIn, obsTimes);
     355                this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, DETECTION_INSTFLUX, 1, 1, nChipDetectionsIn, instFlux);
     356                this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, DETECTION_INSTFLUXERR, 1, 1, nChipDetectionsIn, instFluxErr);
     357                this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, DETECTION_PEAKADU, 1, 1, nChipDetectionsIn, peakFlux);
     358                this->base.fitsOut->writeColumn(this->base.fitsOut, TSTRING, DETECTION_ASSOCDATE, 1, 1, nChipDetectionsIn, assocDate);
     359                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, DETECTION_INFOFLAG, 1, 1, nChipDetectionsIn, flags);
     360                if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet) this->base.fitsOut->deleteRows(this->base.fitsOut, removeList, totalRemove);
    353361
    354362                // skinny object
    355                 ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, nChipDetectionsIn, "SkinnyObject", false);
    356                 fits_write_col(this->base.fitsOut, TLONGLONG, SKINNYOBJECT_OBJID, 1, 1, nChipDetectionsIn, objID, &status);
    357                 fits_write_col(this->base.fitsOut, TLONGLONG, SKINNYOBJECT_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID, &status);
    358                 fits_write_col(this->base.fitsOut, TBYTE, SKINNYOBJECT_SURVEYID, 1, 1, nChipDetectionsIn, surveyIDs, &status);
    359                 if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet)
    360                     fits_delete_rowlist(this->base.fitsOut, removeList, totalRemove, &status);
     363                this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nChipDetectionsIn, "SkinnyObject", false);
     364                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, SKINNYOBJECT_OBJID, 1, 1, nChipDetectionsIn, objID);
     365                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, SKINNYOBJECT_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID);
     366                this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, SKINNYOBJECT_SURVEYID, 1, 1, nChipDetectionsIn, surveyIDs);
     367                if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet) this->base.fitsOut->deleteRows(this->base.fitsOut, removeList, totalRemove);
    361368
    362369                // object calibration color
    363                 ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, nChipDetectionsIn, "ObjectCalColor", false);
    364                 fits_write_col(this->base.fitsOut, TLONGLONG, OBJECTCALCOLOR_OBJID, 1, 1, nChipDetectionsIn, objID, &status);
    365                 fits_write_col(this->base.fitsOut, TLONGLONG, OBJECTCALCOLOR_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID, &status);
    366                 fits_write_col(this->base.fitsOut, TBYTE, OBJECTCALCOLOR_FILTERID, 1, 1, nChipDetectionsIn, filterIDs, &status);
    367                 if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet)
    368                     fits_delete_rowlist(this->base.fitsOut, removeList, totalRemove, &status);
     370                this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nChipDetectionsIn, "ObjectCalColor", false);
     371                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, OBJECTCALCOLOR_OBJID, 1, 1, nChipDetectionsIn, objID);
     372                this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, OBJECTCALCOLOR_IPPOBJID, 1, 1, nChipDetectionsIn, ippObjID);
     373                this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, OBJECTCALCOLOR_FILTERID, 1, 1, nChipDetectionsIn, filterIDs);
     374                if (numOfDuplicates||numInvalidFlux||numOfInvalidIppIDet) this->base.fitsOut->deleteRows(this->base.fitsOut, removeList, totalRemove);
    369375            }
    370376
    371             psLogMsg("ippToPsps", PS_LOG_INFO,
    372                     "+-----------+---------+----------+----------------+--------------+--------------+\n"
    373                     "| Extension | Rows in | Rows out |  Duplicate IDs | Invalid Flux | Bogus det ID |\n"
     377            this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch",
    374378                    "|  %s |  %5ld  |   %5d  |     %5d      |    %5d     |    %5d     |\n",
    375379                    extensionName, nChipDetectionsIn, nChipDetectionsOut, numOfDuplicates, numInvalidFlux, numOfInvalidIppIDet);
     
    384388    }
    385389
    386     psLogMsg("ippToPsps", PS_LOG_INFO, "Total detections for this exposure = %ld\n", totalDetectionsOut);
     390    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", 
     391            "+-----------+---------+----------+----------------+--------------+--------------+\n");
     392
     393    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", "Total detections for this exposure = %ld\n", totalDetectionsOut);
    387394
    388395    // free-up memory
     
    408415
    409416    // write number of images we have found into FrameMeta table
    410     status=0;
    411     if (fits_movnam_hdu(this->base.fitsOut, BINARY_TBL, "FrameMeta", 0, &status))
    412         psError(PS_ERR_IO, false, "Can't move back to FrameMeta extension to write number of OTAs\n");
    413     else {
    414         fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_NOTA, 1, 1, 1, &nOta, &status);
    415         fits_write_col(this->base.fitsOut, TSHORT, FRAMEMETA_NUMPHOTOREF, 1, 1, 1, &totalNumPhotoRef, &status);
    416     }
    417     status=0;
    418     if (fits_close_file(fitsIn, &status)) fits_report_error(stderr, status);
    419     // write results
    420     if (this->base.resultsXmlDoc) {
    421 
    422         xmlNodePtr rootNode = xmlDocGetRootElement(this->base.resultsXmlDoc);
    423         char tmp[100];
    424         sprintf(tmp, "%ld", minObjID);
    425         xmlNewChild(rootNode, NULL, BAD_CAST "minObjID", BAD_CAST tmp);
    426         sprintf(tmp, "%ld", maxObjID);
    427         xmlNewChild(rootNode, NULL, BAD_CAST "maxObjID", BAD_CAST tmp);
    428         sprintf(tmp, "%ld", totalDetectionsOut);
    429         xmlNewChild(rootNode, NULL, BAD_CAST "totalDetections", BAD_CAST tmp);
    430     }
    431 
    432     psLogMsg("ippToPsps", PS_LOG_INFO, "Data written for a total of %d chips/OTAs", nOta);
     417    if (this->base.fitsOut->moveToBinaryTable(this->base.fitsOut, "FrameMeta")) {
     418
     419        this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, FRAMEMETA_NOTA, 1, 1, 1, &nOta);
     420        this->base.fitsOut->writeColumn(this->base.fitsOut, TSHORT, FRAMEMETA_NUMPHOTOREF, 1, 1, 1, &totalNumPhotoRef);
     421    }
     422
     423    fitsIn->destroy(fitsIn);
     424
     425    writeResults(this, minObjID, maxObjID, totalDetectionsOut);
     426
     427    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", "Data written for a total of %d chips/OTAs\n", nOta);
    433428
    434429    if (error || nOta < 1) this->base.exitCode = PS_EXIT_DATA_ERROR;
     
    438433}
    439434
    440 
    441435/**
    442436  Print-out this class. Calls base-calls print method first.
     
    446440    this->base.print(&this->base);
    447441
    448     printf("* exp ID          : %d\n", this->expId);
    449     printf("* exp name        : '%s'\n",  this->expName ? this->expName : "undef");
    450     printf("\n");
     442    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", "exp ID          : %d\n", this->expId);
     443    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", "exp name        : %s\n",  this->expName ? this->expName : "undef");
     444    this->base.logger->print(this->base.logger, MSG_INFO, "DetectionBatch", "\n");
    451445}
    452446
     
    480474    }
    481475
    482     this->base.parseArguments(&this->base, argc, argv);
     476    char fitsOutFile[40];
     477    sprintf(fitsOutFile, "%08d.FITS", this->expId);
     478    this->base.parseArguments(&this->base, argc, argv, "/detection", fitsOutFile);
    483479
    484480    if (
     
    491487            !haveExpName) {
    492488
    493         printf("\n* ERROR with supplied arguments:");
     489        this->base.logger->print(this->base.logger, MSG_ERROR, "DetectionBatch", "Problem with supplied arguments\n");
    494490        this->print(this);
    495491        this->base.exitCode = PS_EXIT_CONFIG_ERROR;
     
    503499  Constructor. Returns a new DetectionBatch object.
    504500  */
    505 DetectionBatch* new_DetectionBatch(int *argc, char **argv) {
    506 
     501DetectionBatch* new_DetectionBatch(Logger* logger, int *argc, char **argv) {
     502
     503    logger->print(logger, MSG_DEBUG, "DetectionBatch", "Constructor\n");
    507504    DetectionBatch *this = (DetectionBatch*)calloc(1, sizeof(DetectionBatch));
    508505
    509506    // call base-class constructor
    510     if (!new_Batch(&this->base)) {
    511 
    512         psError(PS_ERR_IO, false, "Unable to create Batch base-class object");
    513         return this;
    514     }
     507    new_Batch(logger, &this->base);
    515508
    516509    this->MAXDETECT = 200000;
    517510
     511    // method pointers
    518512    this->print = print;
    519513    this->destroy = destroy;
     
    522516    this->expName = (char*)calloc(100, sizeof(char));
    523517
    524     if (!parseArguments(this, *argc, argv)) { return this; }
    525 
    526     strcat(this->base.configsDir, "/detection");
    527     sprintf(this->base.fitsOutFile, "%08d.FITS", this->expId);
    528 
    529     this->base.init(&this->base);
     518    parseArguments(this, *argc, argv);
     519
     520    this->print(this);
    530521
    531522    return this;
     
    537528int main(int argc, char **argv) {
    538529
    539     psTimerStart("detectionbatch");
    540 
    541     ippToPsps_VersionPrint();
    542 
    543     int exitCode;
    544 
    545     DetectionBatch* detectionBatch = new_DetectionBatch(&argc, argv);
     530//    ippToPsps_VersionPrint();
     531
     532    Logger* logger = new_Logger(NULL, false);
     533//    Logger* logger = new_Logger("./detBatchLog.txt", false);
     534    logger->print(logger, MSG_INFO, "main", "Creating new detection batch\n");
     535
     536    DetectionBatch* detectionBatch = new_DetectionBatch(logger, &argc, argv);
    546537    detectionBatch->base.run(detectionBatch);
    547     exitCode = detectionBatch->base.exitCode;
     538    int exitCode = detectionBatch->base.exitCode;
    548539
    549540    detectionBatch->destroy(detectionBatch);
    550541
    551     double secs = psTimerMark("ippToPsps");
    552 
    553     psLogMsg("detectionbatch", 3, "detectionbatch completed %ssuccessfully, with exit code %d, in %.1f %s\n",
    554             (exitCode == PS_EXIT_SUCCESS) ? "" : "un",
    555             exitCode,
    556             (secs<60.0) ? secs : (secs/60.0),
    557             (secs<60.0) ? "sec(s)" : "min(s)");
    558 
    559542    // tidy up
    560     psTimerStop();
    561543    psLibFinalize();
     544    logger->destroy(logger);
    562545
    563546    return exitCode;
Note: See TracChangeset for help on using the changeset viewer.