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/StackBatch.c

    r30147 r31083  
    11#include "StackBatch.h"
    22#include "StackBatchEnums.h"
     3
     4#include "Fits.h"
    35
    46/**
     
    1315
    1416/**
    15   Does the work.
    16   */
    17 static int run(StackBatch* this) {
    18 
    19     if (this->base.exitCode != PS_EXIT_SUCCESS) return this->base.exitCode;
    20 
    21     int status = 0;
    22     fitsfile *fitsIn;
    23 
    24     if (fits_open_file(&fitsIn, this->base.inputFiles[0], READONLY, &status)) {
    25 
    26         fits_report_error(stderr, status);
    27         return PS_EXIT_SYS_ERROR;
    28     }
    29 
    30 
    31     // get primary header and pull stuff out for later
    32     int nKeys = 0;
    33     fits_get_hdrspace(fitsIn, &nKeys, NULL, &status);
    34     float exposureTime; status=0; fits_read_key(fitsIn, TFLOAT, "EXPTIME", &exposureTime, NULL, &status);
    35     char filterType[20]; status=0; fits_read_key(fitsIn, TSTRING, "FILTER", filterType, NULL, &status);
    36 
    37 
     17  Creates the StackDetection table
     18  */
     19static bool createStackDetectionTable(
     20        StackBatch* this,
     21        Fits* fitsIn,
     22        int8_t* filterIDs,
     23        int8_t* surveyIDs,
     24        long* skycellIDs,
     25        float exposureTime
     26        ) {
     27
     28    char extensionName[25];
     29    sprintf(extensionName, "SkyChip.psf");
     30    long nDet = 0;
     31    if (!fitsIn->moveToBinaryTableAndCountRows(fitsIn, extensionName, &nDet)) return false;
     32
     33
     34    // allocate stuff
    3835    char** assocDate = (char**)calloc(this->MAXDETECT, sizeof(char**));
    3936    for (uint32_t i=0; i<this->MAXDETECT;i++) assocDate[i] = (char*)calloc(20,sizeof(char));
    40 
    41 
    42 
    43     // write StackMeta
    44     ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, 1, "StackMeta", true);
    45     fits_write_col(this->base.fitsOut, TLONG, STACKMETA_SKYCELLID, 1, 1, 1, &this->skycellId, &status);
    46 
    47     int8_t filterID = -1;
    48     if (!ippToPspsConfig_getFilterId(this->base.config, filterType, &filterID)) {
    49 
    50 //        this->base.exitCode = PS_EXIT_DATA_ERROR;
    51     //    return this->base.exitCode; TODO
    52     }
    53 
    5437    long* removeList = (long*)calloc(this->MAXDETECT, sizeof(long));
    5538    float* instMag = (float*)calloc(this->MAXDETECT, sizeof(float));
    56     int8_t* filterIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
    57     int8_t* surveyIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
    58     float floatnull = -999.0;
    59     int anynull = 0;
    60 
    61     fits_write_col(this->base.fitsOut, TBYTE, STACKMETA_FILTERID, 1, 1, 1, &filterID, &status);
    62 
    63     fits_write_col(this->base.fitsOut, TBYTE, STACKMETA_SURVEYID, 1, 1, 1, &this->base.surveyID, &status);
    64 
    65 
    66     // psf detections
    67     char extensionName[15];
    68     sprintf(extensionName, "Chip.psf");
    69     if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
    70         psError(PS_ERR_IO, false, "Can't move to extension: %s\n", extensionName);
    71 
    72     }
    73     else {
    74 
    75         // some stuff is the same for all detections so we can populate here
    76         for (long s = 0; s<this->MAXDETECT; s++) {
    77 
    78             filterIDs[s] = filterID;
    79             surveyIDs[s] = this->base.surveyID;
    80             strcpy(assocDate[s], this->base.todaysDate);
     39    float* peakMag = (float*)calloc(this->MAXDETECT, sizeof(float));
     40    float* peakFlux = (float*)calloc(this->MAXDETECT, sizeof(float));
     41    int* flags1 = (int*)calloc(this->MAXDETECT, sizeof(int));
     42    int* flags2 = (int*)calloc(this->MAXDETECT, sizeof(int));
     43    uint64_t* infoFlags = (uint64_t*)calloc(this->MAXDETECT, sizeof(uint64_t));
     44
     45    bool peakFluxOk;
     46
     47    // some stuff is the same for all detections so we can populate here
     48    for (long s = 0; s<this->MAXDETECT; s++) {
     49
     50        // if running in test mode, don't use today's date
     51        if (this->base.testMode) strcpy(assocDate[s], "2010-01-01");
     52        else strcpy(assocDate[s], this->base.todaysDate);
     53    }
     54
     55    fitsIn->readColumnUsingName(fitsIn, TFLOAT, "PSF_INST_MAG", 1, 1, nDet, instMag);
     56    fitsIn->readColumnUsingName(fitsIn, TFLOAT, "PEAK_FLUX_AS_MAG", 1, 1, nDet, peakMag);
     57    fitsIn->readColumnUsingName(fitsIn, TLONG, "FLAGS", 1, 1, nDet, flags1);
     58    fitsIn->readColumnUsingName(fitsIn, TLONG, "FLAGS2", 1, 1, nDet, flags2);
     59
     60    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch",
     61            "Looping through %ld psf detection rows from '%s'\n", nDet, extensionName);
     62    float mag;
     63    long unmatched = 0, totalDetections = 0, numOfDuplicates = 0, numInvalidFlux = 0, numDetectionsOut = 0;
     64
     65    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch", 
     66            "+---------------+---------+----------+------------------+---------------+--------------+\n");
     67    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch", 
     68            "|   Extension   | Rows in | Rows out | Missing from DVO | Duplicate IDs | Invalid Flux |\n");
     69
     70    for (long s=0; s<nDet; s++) {
     71
     72        // TODO implement this match in DVO
     73        if (1) {
     74
     75            //infoFlags[s] = ((uint64_t)flags1[s] << 32) | (uint64_t)flags2[s]; TODO implement after schema change to make infoFlag 64-bit
     76            infoFlags[s] = (uint64_t)flags1[s];
     77
     78            peakFluxOk = getFlux(exposureTime, peakMag[s], &peakFlux[s], 0.0, NULL);
     79
     80            mag = instMag[s];
     81            if (!peakFluxOk || !isfinite(mag) || mag < -998.0) {
     82
     83                removeList[numOfDuplicates+numInvalidFlux] = s+1;
     84                numInvalidFlux++;
     85            }
     86
     87            totalDetections++;
    8188        }
    82 
    83 
    84         long nDet = 0;
    85         if (fits_get_num_rows(fitsIn, &nDet, &status)) {
    86             fits_report_error(stderr, status);
     89        else {
     90
     91            unmatched++;
     92            continue;
    8793        }
    88 
    89         int instMagNum;
    90         status=0;fits_get_colnum(fitsIn, CASESEN, "PSF_INST_MAG", &instMagNum, &status);
    91         if (status) psError(PS_ERR_IO, false, "Unable to read col num for PSF_INST_MAG");
    92         fits_read_col(fitsIn, TFLOAT, instMagNum, 1, 1, nDet, &floatnull, instMag, &anynull, &status);
    93 
    94 
    95         printf("Looping through %ld psf detections\n", nDet);
    96         float mag;
    97         long unmatched = 0, totalDetections = 0, numOfDuplicates = 0, numInvalidFlux = 0, numDetectionsOut = 0;
    98 
    99         for (long s = 0; s<nDet; s++) {
    100 
    101             // TODO implement this match in DVO
    102             if (1) {
    103 
    104                 mag = instMag[s];
    105                 if (!isfinite(mag) || mag < -998.0) {
    106 
    107                     removeList[numOfDuplicates+numInvalidFlux] = s+1;
    108                     numInvalidFlux++;
    109                 }
    110 
    111                 totalDetections++;
    112             }
    113             else {
    114 
    115                 unmatched++;
    116                 continue;
    117             }
    118         }
    119 
    120         numDetectionsOut = totalDetections - numInvalidFlux;
    121 
    122         if (numDetectionsOut > 0) {
    123 
    124             ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, nDet, "StackDetection", false);
    125             fits_write_col(this->base.fitsOut, TLONG, STACKDETECTION_SKYCELLID, 1, 1, 1, &this->skycellId, &status);
    126             fits_write_col(this->base.fitsOut, TBYTE, STACKDETECTION_FILTERID, 1, 1, nDet, filterIDs, &status);
    127             fits_write_col(this->base.fitsOut, TBYTE, STACKDETECTION_SURVEYID, 1, 1, nDet, surveyIDs, &status);
    128             fits_write_col(this->base.fitsOut, TSTRING, STACKDETECTION_ASSOCDATE, 1, 1, nDet, assocDate, &status);
    129 
    130             if (numInvalidFlux) fits_delete_rowlist(this->base.fitsOut, removeList, numInvalidFlux, &status);
    131 
    132         }
    133         psLogMsg("ippToPsps", PS_LOG_INFO,
    134                 "+---------------+---------+----------+------------------+---------------+--------------+\n"
    135                 "|   Extension   | Rows in | Rows out | Missing from DVO | Duplicate IDs | Invalid Flux |\n"
    136                 "|  %12s |  %5ld  |   %5ld  |      %5ld       |    %5ld      |    %5ld     |\n",
    137                 extensionName, nDet, numDetectionsOut, unmatched, numOfDuplicates, numInvalidFlux);
    138 
    139     }
    140 
    141 
    142 
    143     // extended source
    144     sprintf(extensionName, "Chip.xsrc");
    145     if (fits_movnam_hdu(fitsIn, BINARY_TBL, extensionName, 0, &status)) {
    146         psError(PS_ERR_IO, false, "Can't move to extension: %s\n", extensionName);
    147 
    148     }
    149     else {
    150 
    151         int colNum;
    152         int type;
    153         long repeat;
    154 
    155         ippToPspsConfig_getFitsColumnMeta(
    156                 "PROF_FLUX",
    157                 &colNum,
    158                 &type,
    159                 &repeat,
    160                 fitsIn);
    161         printf("PROF_FILL = %d %d %ld\n", colNum, type, repeat);
    162         float* vector = calloc(repeat, sizeof(float));
    163 
    164         ippToPspsConfig_getColumnVector(
    165                 colNum,
    166                 1,
    167                 type,
    168                 repeat,
    169                 vector,
    170                 fitsIn);
    171 
    172 
    173 
    174         free(vector);
    175 
    176         long nDet = 0;
    177         if (fits_get_num_rows(fitsIn, &nDet, &status)) {
    178             fits_report_error(stderr, status);
    179         }
    180 
    181         printf("Looping through %ld extended source detections\n", nDet);
    182         for (long s = 0; s<nDet; s++) {
    183 
    184 
    185 
    186         }
    187 
    188         ippToPspsConfig_writeTable(this->base.config, fitsIn, this->base.fitsOut, nDet, "StackApFlx", false);
    189         fits_write_col(this->base.fitsOut, TBYTE, STACKAPFLX_FILTERID, 1, 1, nDet, filterIDs, &status);
    190         fits_write_col(this->base.fitsOut, TBYTE, STACKAPFLX_SURVEYID, 1, 1, nDet, surveyIDs, &status);
    191     }
     94    }
     95
     96    numDetectionsOut = totalDetections - numInvalidFlux;
     97    if (numDetectionsOut > 0) {
     98
     99        this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nDet, "StackDetection", false);
     100        this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, STACKDETECTION_SKYCELLID, 1, 1, nDet, skycellIDs);
     101        this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKDETECTION_FILTERID, 1, 1, nDet, filterIDs);
     102        this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKDETECTION_PEAKFLUX, 1, 1, nDet, peakFlux);
     103        this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKDETECTION_SURVEYID, 1, 1, nDet, surveyIDs);
     104        this->base.fitsOut->writeColumn(this->base.fitsOut, TSTRING, STACKDETECTION_ASSOCDATE, 1, 1, nDet, assocDate);
     105        this->base.fitsOut->writeColumn(this->base.fitsOut, TLONGLONG, STACKDETECTION_INFOFLAG, 1, 1, nDet, flags1);
     106
     107        if (numInvalidFlux) this->base.fitsOut->deleteRows(this->base.fitsOut, removeList, numInvalidFlux);
     108
     109    }
     110
     111    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch",
     112            "|  %12s |  %5ld  |   %5ld  |      %5ld       |    %5ld      |    %5ld     |\n",
     113            extensionName, nDet, numDetectionsOut, unmatched, numOfDuplicates, numInvalidFlux);
     114    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch",
     115            "+---------------+---------+----------+------------------+---------------+--------------+\n");
    192116
    193117
     
    195119    free(removeList);
    196120    free(instMag);
     121    free(flags1);
     122    free(flags2);
     123    free(infoFlags);
     124    for (uint32_t i=0; i<this->MAXDETECT;i++) free(assocDate[i]);
     125    free(assocDate);
     126
     127    return true;
     128}
     129/**
     130  Structure to encapsulate block of flux values for StackApFlx table
     131  */
     132typedef struct {
     133
     134    float* flx;
     135    float* flxErr;
     136    float* flxStd;
     137    float* flxFill;
     138
     139} StackApFlxFluxes;
     140
     141/**
     142  Creates the StackApFlx table for extended source attributes
     143  */
     144static bool createStackApFlxTable(
     145        StackBatch* this,
     146        Fits* fitsIn,
     147        int8_t* filterIDs,
     148        int8_t* surveyIDs
     149        ) {
     150
     151    long nDet = 0;
     152    char extensionName[15];
     153    sprintf(extensionName, "SkyChip.xrad");
     154    if (!fitsIn->moveToBinaryTableAndCountRows(fitsIn, extensionName, &nDet)) return false;
     155
     156
     157    int aperFluxColNum, aperFluxErrColNum, aperFluxStDevColNum, aperFluxFillColNum;
     158    int type; // all the same type
     159    long repeat;
     160
     161    fitsIn->getColumnMeta(fitsIn, "APER_FLUX", &aperFluxColNum, &type, &repeat);
     162    fitsIn->getColumnMeta(fitsIn, "APER_FLUX_ERR", &aperFluxErrColNum, &type, &repeat);
     163    fitsIn->getColumnMeta(fitsIn, "APER_FLUX_STDEV", &aperFluxStDevColNum, &type, &repeat);
     164    fitsIn->getColumnMeta(fitsIn, "APER_FILL", &aperFluxFillColNum, &type, &repeat);
     165    float* vector = calloc(repeat, sizeof(float));
     166
     167    // we store 10 different flux values
     168    StackApFlxFluxes* stackApFlxFluxes = (StackApFlxFluxes*)calloc(11, sizeof(StackApFlxFluxes));
     169    for (long i=0; i<11; i++) {
     170
     171        stackApFlxFluxes[i].flx = (float*)calloc(nDet, sizeof(float));
     172        stackApFlxFluxes[i].flxErr = (float*)calloc(nDet, sizeof(float));
     173        stackApFlxFluxes[i].flxStd = (float*)calloc(nDet, sizeof(float));
     174        stackApFlxFluxes[i].flxFill = (float*)calloc(nDet, sizeof(float));
     175    }
     176
     177    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch",
     178            "Looping through %ld extended source detections from extension '%s'\n", nDet, extensionName);
     179    for (long s=0; s<nDet; s++) {
     180
     181        fitsIn->getColumnVector(fitsIn, aperFluxColNum,s, type, repeat, vector);
     182        for (int i=0; i<repeat; i++) stackApFlxFluxes[i].flx[s] = vector[i];
     183
     184        fitsIn->getColumnVector(fitsIn, aperFluxErrColNum,s, type, repeat, vector);
     185        for (int i=0; i<repeat; i++) stackApFlxFluxes[i].flxErr[s] = vector[i];
     186
     187        fitsIn->getColumnVector(fitsIn, aperFluxStDevColNum,s, type, repeat, vector);
     188        for (int i=0; i<repeat; i++) stackApFlxFluxes[i].flxStd[s] = vector[i];
     189
     190        fitsIn->getColumnVector(fitsIn, aperFluxFillColNum,s, type, repeat, vector);
     191        for (int i=0; i<repeat; i++) stackApFlxFluxes[i].flxFill[s] = vector[i];
     192
     193    }
     194
     195    free(vector);
     196
     197    //int status = 0;
     198    this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nDet, "StackApFlx", false);
     199    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKAPFLX_FILTERID, 1, 1, nDet, filterIDs);
     200    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKAPFLX_SURVEYID, 1, 1, nDet, surveyIDs);
     201
     202    // R1->r10 flux values
     203    // 1
     204    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR1, 1, 1, nDet, stackApFlxFluxes[0].flx);
     205    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR1ERR, 1, 1, nDet, stackApFlxFluxes[0].flxErr);
     206    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR1STD, 1, 1, nDet, stackApFlxFluxes[0].flxStd);
     207    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR1FILL, 1, 1, nDet, stackApFlxFluxes[0].flxFill);
     208    // 2
     209    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR2, 1, 1, nDet, stackApFlxFluxes[1].flx);
     210    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR2ERR, 1, 1, nDet, stackApFlxFluxes[1].flxErr);
     211    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR2STD, 1, 1, nDet, stackApFlxFluxes[1].flxStd);
     212    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR2FILL, 1, 1, nDet, stackApFlxFluxes[1].flxFill);
     213    // 3
     214    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR3, 1, 1, nDet, stackApFlxFluxes[2].flx);
     215    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR3ERR, 1, 1, nDet, stackApFlxFluxes[2].flxErr);
     216    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR3STD, 1, 1, nDet, stackApFlxFluxes[2].flxStd);
     217    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR3FILL, 1, 1, nDet, stackApFlxFluxes[2].flxFill);
     218    // 4
     219    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR4, 1, 1, nDet, stackApFlxFluxes[3].flx);
     220    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR4ERR, 1, 1, nDet, stackApFlxFluxes[3].flxErr);
     221    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR4STD, 1, 1, nDet, stackApFlxFluxes[3].flxStd);
     222    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR4FILL, 1, 1, nDet, stackApFlxFluxes[3].flxFill);
     223    // 5
     224    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR5, 1, 1, nDet, stackApFlxFluxes[4].flx);
     225    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR5ERR, 1, 1, nDet, stackApFlxFluxes[4].flxErr);
     226    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR5STD, 1, 1, nDet, stackApFlxFluxes[4].flxStd);
     227    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR5FILL, 1, 1, nDet, stackApFlxFluxes[4].flxFill);
     228    // 6
     229    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR6, 1, 1, nDet, stackApFlxFluxes[5].flx);
     230    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR6ERR, 1, 1, nDet, stackApFlxFluxes[5].flxErr);
     231    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR6STD, 1, 1, nDet, stackApFlxFluxes[5].flxStd);
     232    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR6FILL, 1, 1, nDet, stackApFlxFluxes[5].flxFill);
     233    // 7
     234    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR7, 1, 1, nDet, stackApFlxFluxes[6].flx);
     235    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR7ERR, 1, 1, nDet, stackApFlxFluxes[6].flxErr);
     236    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR7STD, 1, 1, nDet, stackApFlxFluxes[6].flxStd);
     237    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR7FILL, 1, 1, nDet, stackApFlxFluxes[6].flxFill);
     238    // 8
     239    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR8, 1, 1, nDet, stackApFlxFluxes[7].flx);
     240    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR8ERR, 1, 1, nDet, stackApFlxFluxes[7].flxErr);
     241    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR8STD, 1, 1, nDet, stackApFlxFluxes[7].flxStd);
     242    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR8FILL, 1, 1, nDet, stackApFlxFluxes[7].flxFill);
     243    // 9
     244    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR9, 1, 1, nDet, stackApFlxFluxes[8].flx);
     245    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR9ERR, 1, 1, nDet, stackApFlxFluxes[8].flxErr);
     246    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR9STD, 1, 1, nDet, stackApFlxFluxes[8].flxStd);
     247    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR9FILL, 1, 1, nDet, stackApFlxFluxes[8].flxFill);
     248    // 10
     249    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR10, 1, 1, nDet, stackApFlxFluxes[9].flx);
     250    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR10ERR, 1, 1, nDet, stackApFlxFluxes[9].flxErr);
     251    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR10STD, 1, 1, nDet, stackApFlxFluxes[9].flxStd);
     252    this->base.fitsOut->writeColumn(this->base.fitsOut, TFLOAT, STACKAPFLX_FLXR10FILL, 1, 1, nDet, stackApFlxFluxes[9].flxFill);
     253
     254    for (long i=0; i<11; i++) {
     255
     256        free(stackApFlxFluxes[i].flx);
     257        free(stackApFlxFluxes[i].flxErr);
     258        free(stackApFlxFluxes[i].flxStd);
     259        free(stackApFlxFluxes[i].flxFill);
     260    }
     261
     262    free(stackApFlxFluxes);
     263
     264    return true;
     265}
     266
     267/**
     268  Creates the StackModelFit table
     269  */
     270static bool createStackModelFitTable(
     271        StackBatch* this,
     272        Fits *fitsIn,
     273        int8_t* filterIDs,
     274        int8_t* surveyIDs) {
     275
     276    long nDet = 0;
     277    char extensionName[40];
     278    sprintf(extensionName, "SkyChip.xfit");
     279    if (!fitsIn->moveToBinaryTableAndCountRows(fitsIn, extensionName, &nDet)) return false;
     280
     281    long* ippIdets = (long*)calloc(this->MAXDETECT, sizeof(long));
     282    char** modelTypes = (char**)calloc(this->MAXDETECT, sizeof(char**));
     283    for (uint32_t i=0; i<this->MAXDETECT;i++) modelTypes[i] = (char*)calloc(20,sizeof(char));
     284
     285    // read whole columns
     286    fitsIn->readColumnUsingName(fitsIn, TLONG, "IPP_IDET", 1, 1, nDet, ippIdets);
     287    fitsIn->readColumnUsingName(fitsIn, TSTRING, "MODEL_TYPE", 1, 1, nDet, modelTypes);
     288
     289    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch",
     290            "Looping through %ld model fit rows from '%s'\n", nDet, extensionName);
     291    for (long i = 0; i<nDet; i++) {
     292
     293        printf("IPP_IDET %ld model %s\n", ippIdets[i], modelTypes[i]);
     294
     295    }
     296
     297    this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, nDet, "StackModelFit", false);
     298    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKMODELFIT_FILTERID, 1, 1, nDet, filterIDs);
     299    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKMODELFIT_SURVEYID, 1, 1, nDet, surveyIDs);
     300
     301    // free up
     302    free(ippIdets);
     303    for (uint32_t i=0; i<this->MAXDETECT;i++) free(modelTypes[i]);
     304    free(modelTypes);
     305
     306    return true;
     307}
     308
     309/**
     310  Creates the StackToImage table
     311  */
     312static bool createStackToImageTable(
     313        StackBatch* this,
     314        Fits* fitsIn
     315        ) {
     316
     317    this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, 3/*TODO*/, "StackToImage", false);
     318
     319    return true;
     320}
     321
     322/**
     323  Does the work. Writes all extensions to new FITS file.
     324  */
     325static int run(StackBatch* this) {
     326
     327    if (this->base.exitCode != PS_EXIT_SUCCESS) return this->base.exitCode;
     328
     329    // open input FITS file
     330    Fits* fitsIn = existing_Fits(this->base.inputFiles[0], this->base.logger);
     331    if (fitsIn->getFilePtr(fitsIn) == NULL)  return PS_EXIT_SYS_ERROR;
     332
     333    // get primary header and pull stuff out for later
     334    float exposureTime; fitsIn->getHeaderKeyValue(fitsIn, TFLOAT, "EXPTIME", &exposureTime);
     335    char filterType[20]; fitsIn->getHeaderKeyValue(fitsIn, TSTRING, "FILTER", filterType);
     336
     337    int8_t filterID = -1;
     338    if (!this->base.initData->getFilterId(this->base.initData, filterType, &filterID)) {
     339
     340        //        this->base.exitCode = PS_EXIT_DATA_ERROR;
     341        //    return this->base.exitCode; TODO
     342    }
     343
     344    exposureTime = 60.0;// TODO
     345    filterID = 3; // TODO
     346
     347    // write StackMeta
     348    this->base.fitsGenerator->createAndPopulateTable(this->base.fitsGenerator, fitsIn, this->base.fitsOut, 1, "StackMeta", true);
     349    this->base.fitsOut->writeColumn(this->base.fitsOut, TLONG, STACKMETA_SKYCELLID, 1, 1, 1, &this->skycellId);
     350    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKMETA_FILTERID, 1, 1, 1, &filterID);
     351    this->base.fitsOut->writeColumn(this->base.fitsOut, TBYTE, STACKMETA_SURVEYID, 1, 1, 1, &this->base.surveyID);
     352
     353    // allocate stuff for other extensions
     354    int8_t* filterIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
     355    int8_t* surveyIDs = (int8_t*)calloc(this->MAXDETECT, sizeof(int8_t));
     356    long* skycellIDs = (long*)calloc(this->MAXDETECT, sizeof(long));
     357
     358    // some stuff is the same for all detections so we can populate here
     359    for (long s = 0; s<this->MAXDETECT; s++) {
     360
     361        skycellIDs[s] = this->skycellId;
     362        filterIDs[s] = filterID;
     363        surveyIDs[s] = this->base.surveyID;
     364    }
     365
     366    // create all extensions
     367    createStackDetectionTable(this, fitsIn, filterIDs, surveyIDs, skycellIDs, exposureTime);
     368    createStackApFlxTable(this, fitsIn, filterIDs, surveyIDs);
     369    createStackModelFitTable(this, fitsIn, filterIDs, surveyIDs);
     370    createStackToImageTable(this, fitsIn);
     371
     372    // free stuff up
    197373    free(filterIDs);
    198374    free(surveyIDs);
    199 
    200     status=0;
    201     if (fits_close_file(fitsIn, &status)) fits_report_error(stderr, status);
    202 
     375    free(skycellIDs);
     376    fitsIn->destroy(fitsIn);
    203377
    204378    return this->base.exitCode;
    205379}
    206380
    207 
    208381/**
    209382  Print-out this class. Calls base-calls print method first.
     
    213386    this->base.print(&this->base);
    214387
    215     printf("* skycell ID      : %d\n", this->skycellId);
    216     printf("\n");
     388    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch", "skycell ID      : %d\n", this->skycellId);
     389    this->base.logger->print(this->base.logger, MSG_INFO, "StackBatch", "\n");
    217390}
    218391
     
    240413    }
    241414
    242     this->base.parseArguments(&this->base, argc, argv);
     415    char fitsOutFile[40];
     416    sprintf(fitsOutFile, "%08d.FITS", this->skycellId);
     417    this->base.parseArguments(&this->base, argc, argv, "/stack", fitsOutFile);
    243418
    244419    if (
     
    262437  Constructor
    263438  */
    264 StackBatch* new_StackBatch(int *argc, char **argv) {
    265 
     439StackBatch* new_StackBatch(Logger* logger, int *argc, char **argv) {
     440
     441    logger->print(logger, MSG_DEBUG, "StackBatch", "Constructor\n");
    266442    StackBatch *this = (StackBatch*)calloc(1, sizeof(StackBatch));
    267443
    268444    // call base-class constructor
    269     if (!new_Batch(&this->base)) {
    270 
    271         psError(PS_ERR_IO, false, "Unable to create Batch base-class object");
    272         return this;
    273     }
     445    new_Batch(logger, &this->base);
    274446
    275447    this->MAXDETECT = 150000;
    276448
     449    // method pointers
    277450    this->print = print;
    278451    this->destroy = destroy;
    279452    this->base.run = run;
    280453
    281     if (!parseArguments(this, *argc, argv)) { return this; }
    282 
    283     strcat(this->base.configsDir, "/stack");
    284     sprintf(this->base.fitsOutFile, "%08d.FITS", this->skycellId);
    285 
    286     this->base.init(&this->base);
     454    parseArguments(this, *argc, argv);
     455
     456    this->print(this);
    287457
    288458    return this;
     
    294464int main(int argc, char **argv) {
    295465
    296     psTimerStart("stackbatch");
    297 
    298     ippToPsps_VersionPrint();
    299 
    300     int exitCode;
    301 
    302     StackBatch* stackBatch = new_StackBatch(&argc, argv);
     466//    ippToPsps_VersionPrint();
     467
     468    Logger* logger = new_Logger(NULL, false);
     469    logger->print(logger, MSG_INFO, "main", "Creating new stack batch\n");
     470
     471    StackBatch* stackBatch = new_StackBatch(logger, &argc, argv);
    303472    stackBatch->base.run(stackBatch);
    304     exitCode = stackBatch->base.exitCode;
     473    int exitCode = stackBatch->base.exitCode;
    305474
    306475    stackBatch->destroy(stackBatch);
    307476
    308     double secs = psTimerMark("stackbatch");
    309 
    310     psLogMsg("stackbatch", 3, "stackbatch completed %ssuccessfully, with exit code %d, in %.1f %s\n",
    311             (exitCode == PS_EXIT_SUCCESS) ? "" : "un",
    312             exitCode,
    313             (secs<60.0) ? secs : (secs/60.0),
    314             (secs<60.0) ? "sec(s)" : "min(s)");
    315 
    316477    // tidy up
    317     psTimerStop();
     478    logger->destroy(logger);
    318479    psLibFinalize();
    319480
     
    321482}
    322483
    323 
    324 
Note: See TracChangeset for help on using the changeset viewer.