IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 29, 2010, 3:55:49 PM (16 years ago)
Author:
eugene
Message:

update merges from trunk

Location:
branches/eam_branches/20100225
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20100225

  • branches/eam_branches/20100225/ppStack/src/ppStackSources.c

    r27004 r27517  
    88
    99//#define TESTING                         // Enable debugging output
     10
     11//#define ASTROMETRY                    // Correct astrometry?
    1012
    1113#ifdef TESTING
     
    6264
    6365    if (!options->matchZPs && !options->photometry) {
    64         int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs
    65         options->norm = psVectorAlloc(num, PS_TYPE_F32);
    66         psVectorInit (options->norm, 0.0);
    67 
    68         // XXX do I need to set this?
    69         // options->sumExposure = sumExpTime;
    70 
     66        options->norm = psVectorAlloc(options->num, PS_TYPE_F32);
     67        psVectorInit(options->norm, 0.0);
    7168        return true;
    7269    }
     
    9087            }
    9188            source->psfMag += 1.0;
     89#ifdef ASTROMETRY
     90            if (source->modelPSF) {
     91                source->modelPSF->params->data.F32[PM_PAR_XPOS] += 1.0;
     92                source->modelPSF->params->data.F32[PM_PAR_YPOS] += 1.0;
     93            }
     94            if (source->peak) {
     95                source->peak->xf += 1.0;
     96                source->peak->yf += 1.0;
     97            }
     98#endif
    9299        }
    93100    }
     
    119126        return false;
    120127    }
    121 
    122     int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs
     128    psMetadata *zpTargetMenu = psMetadataLookupMetadata(NULL, recipe, "ZP.TARGET"); // Target zero point terms
     129    if (!zpTargetMenu) {
     130        psError(PPSTACK_ERR_CONFIG, false, "Unable to find ZP.TARGET in recipe.");
     131        return false;
     132    }
     133
     134    int num = options->num;             // Number of inputs
    123135    psAssert(num == sourceLists->n, "Wrong number of source lists: %ld\n", sourceLists->n);
    124136
    125     psVector *zp = psVectorAlloc(num, PS_TYPE_F32); // Zero points for each image
     137    psVector *zp = psVectorAlloc(num, PS_TYPE_F32); // Relative zero points for each image
     138    psVector *zpExp = psVectorAlloc(num, PS_TYPE_F32); // Measured zero points for each image (maybe)
     139    int zpExpNum = 0;                                  // Number of measured zero points
    126140    const char *filter = NULL;          // Filter name
    127141    float airmassTerm = NAN;            // Airmass term
    128     float sumExpTime = 0.0;             // Sum of the exposure time
     142    float zpTarget = NAN;               // Target zero point
     143    int numGoodImages = 0;              // Number of good images
    129144    for (int i = 0; i < num; i++) {
     145        psArray *sources = sourceLists->data[i]; // Source list
     146        if (!sources || sources->n == 0) {
     147            psLogMsg("ppStack", PS_LOG_WARN, "Image %d has no sources for transparency measurement.", i);
     148            options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i] |= PPSTACK_MASK_CAL;
     149            zp->data.F32[i] = NAN;
     150            continue;
     151        }
     152        numGoodImages++;
     153
    130154        pmFPAfile *file = pmFPAfileSelectSingle(config->files, "PPSTACK.INPUT", i); // File of interest
    131         pmCell *cell = pmFPAviewThisCell(view, file->fpa); // Cell of interest
    132155
    133156#if defined(TESTING) && 0
     
    146169#endif
    147170
    148 
    149         float exptime = psMetadataLookupF32(NULL, cell->concepts, "CELL.EXPOSURE"); // Exposure time
     171        float exptime = options->exposures->data.F32[i]; // Exposure time
    150172        float airmass = psMetadataLookupF32(NULL, file->fpa->concepts, "FPA.AIRMASS"); // Airmass
    151173        const char *expFilter = psMetadataLookupStr(NULL, file->fpa->concepts, "FPA.FILTER"); // Filter name
     174        zpExp->data.F32[i] = psMetadataLookupF32(NULL, file->fpa->concepts, "FPA.ZP"); // Exposure zero point
     175        psLogMsg("ppStack", PS_LOG_INFO,
     176                 "Image %d: %.2f sec exposure in %s at airmass %.2f with zero point %.2f",
     177                 i, exptime, expFilter, airmass, zpExp->data.F32[i]);
    152178        if (!isfinite(exptime) || exptime == 0 || !isfinite(airmass) || airmass == 0 ||
    153179            !expFilter || strlen(expFilter) == 0) {
     
    158184            return false;
    159185        }
     186        if (isfinite(zpExp->data.F32[i])) {
     187            zpExp->data.F32[i] += 2.5 * log10(exptime);
     188            zpExpNum++;
     189        }
    160190
    161191        if (!filter) {
    162192            filter = expFilter;
    163             airmassTerm = psMetadataLookupF32(NULL, airmassZP, filter);
    164             if (!isfinite(airmassTerm)) {
     193            bool mdok;
     194            airmassTerm = psMetadataLookupF32(&mdok, airmassZP, filter);
     195            if (!mdok || !isfinite(airmassTerm)) {
    165196                psError(PPSTACK_ERR_CONFIG, false,
    166197                        "Unable to find airmass term (ZP.AIRMASS) for filter %s", filter);
     
    168199                return false;
    169200            }
     201            zpTarget = psMetadataLookupF32(&mdok, zpTargetMenu, filter);
     202            if (!mdok || !isfinite(zpTarget)) {
     203                psError(PPSTACK_ERR_CONFIG, false,
     204                        "Unable to find target zero point (ZP.TARGET) for filter %s", filter);
     205                psFree(zp);
     206                return false;
     207            }
    170208        } else if (strcmp(filter, expFilter) != 0) {
    171209            psError(PPSTACK_ERR_CONFIG, false, "Filters don't match: %s vs %s", filter, expFilter);
     
    174212        }
    175213
    176         zp->data.F32[i] = airmassTerm * airmass - 2.5 * log10(exptime);
    177         sumExpTime += exptime;
    178     }
    179 
    180     options->sumExposure = sumExpTime;
     214        zp->data.F32[i] = airmassTerm * airmass + 2.5 * log10(exptime);
     215    }
     216
     217    if (numGoodImages == 0) {
     218        psLogMsg("ppStack", PS_LOG_WARN, "No images with sources to measure transparency.");
     219        options->quality = PPSTACK_ERR_REJECTED;
     220        psFree(zp);
     221        psFree(zpExp);
     222        return true;
     223    }
     224    if (numGoodImages == 1) {
     225        psArray *sources = NULL;        // Sources
     226        for (int i = 0; i < num && !sources; i++) {
     227            if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
     228                continue;
     229            }
     230            sources = sourceLists->data[i];
     231        }
     232        options->sources = psMemIncrRefCounter(sources);
     233        psLogMsg("ppStack", PS_LOG_WARN, "Single image with sources --- no need to match transparency.");
     234        psFree(zp);
     235        psFree(zpExp);
     236        return true;
     237    }
     238
     239    if (zpExpNum == numGoodImages) {
     240        for (int i = 0; i < num; i++) {
     241            zp->data.F32[i] = zpExp->data.F32[i];
     242        }
     243    }
    181244
    182245    psArray *matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
     
    212275            options->norm = psVectorAlloc(num, PS_TYPE_F32);
    213276            for (int i = 0; i < num; i++) {
    214                 if (!isfinite(trans->data.F32[i])) {
     277                if (inputMask->data.U8[i] || !isfinite(trans->data.F32[i])) {
    215278                    continue;
    216279                }
    217280                psArray *sources = sourceLists->data[i]; // Sources of interest
    218                 float magCorr = airmassTerm - 2.5*log10(sumExpTime) - zp->data.F32[i] - trans->data.F32[i];
    219                 options->norm->data.F32[i] = magCorr;
    220                 psLogMsg("ppStack", PS_LOG_INFO, "Applying magnitude correction to image %d: %f\n",
    221                          i, magCorr);
     281                float magCorr = zp->data.F32[i] + trans->data.F32[i] - 2.5*log10(options->sumExposure);
     282                if (zpExpNum == numGoodImages) {
     283                    // Using measured zero points, so attempt to set target zero point
     284                    magCorr -= zpTarget;
     285                }
     286                options->norm->data.F32[i] = -magCorr;
     287                psLogMsg("ppStack", PS_LOG_INFO,
     288                         "Applying scale correction to image %d: %f mag (%f)\n",
     289                         i, magCorr, trans->data.F32[i]);
    222290
    223291                for (int j = 0; j < sources->n; j++) {
     
    226294                        continue;
    227295                    }
    228                     source->psfMag += magCorr;
    229                 }
    230             }
    231         }
     296                    source->psfMag -= magCorr;
     297                }
     298            }
     299        }
     300
     301        if (zpExpNum == numGoodImages) {
     302            // Producing image with target zero point
     303            options->zp = zpTarget;
     304        } else {
     305            options->zp = NAN;
     306        }
     307
    232308
    233309#ifdef TESTING
     
    239315        // Double check: all transparencies should be zero
    240316        {
    241             psArray *matches = pmSourceMatchSources(sourceLists, radius); // List of matches
     317            psArray *matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
    242318            if (!matches) {
    243319                psError(PPSTACK_ERR_DATA, false, "Unable to match sources");
     
    245321                return false;
    246322            }
    247             psVector *trans = pmSourceMatchRelphot(matches, zp, iter, tol, starLimit, transIter, transRej,
    248                                                    transThresh, starRej, starSys);
     323            psVector *trans = pmSourceMatchRelphot(matches, zp, tol, iter1, starRej1, starSys1,
     324                                                   iter2, starRej2, starSys2, starLimit,
     325                                                   transIter, transRej, transThresh); // Transparencies
    249326            for (int i = 0; i < num; i++) {
    250327                fprintf(stderr, "Transparency of image %d: %f\n", i, trans->data.F32[i]);
    251328            }
    252329            psFree(trans);
    253         }
    254 #endif
    255     }
    256 
    257 
    258 #if 0
     330            psFree(matches);
     331        }
     332#endif
     333    }
     334
     335    psFree(zp);
     336    psFree(zpExp);
     337
     338#ifdef ASTROMETRY
    259339    // Position offsets
    260340    {
     
    266346        }
    267347        for (int i = 0; i < num; i++) {
    268             psVector *offset = offsets->data[i];
    269             fprintf(stderr, "Offset %d: %f,%f\n", i, offset->data.F32[0], offset->data.F32[1]);
     348            if (options->inputMask->data.U8[i]) {
     349                continue;
     350            }
     351            psArray *sources = sourceLists->data[i]; // Sources of interest
     352            psVector *offset = offsets->data[i];                      // Offsets for image
     353            float dx = offset->data.F32[0], dy = offset->data.F32[1]; // Offsets to apply
     354            if (!isfinite(dx) || !isfinite(dy)) {
     355                continue;
     356            }
     357            psLogMsg("ppStack", PS_LOG_INFO, "Applying astrometric correction to image %d: %f,%f\n",
     358                     i, dx, dy);
     359            for (int j = 0; j < sources->n; j++) {
     360                pmSource *source = sources->data[j]; // Source of interest
     361                if (!source) {
     362                    continue;
     363                }
     364                if (source->modelPSF) {
     365                    source->modelPSF->params->data.F32[PM_PAR_XPOS] -= dx;
     366                    source->modelPSF->params->data.F32[PM_PAR_YPOS] -= dy;
     367                }
     368                if (source->peak) {
     369                    source->peak->xf -= dx;
     370                    source->peak->yf -= dy;
     371                }
     372            }
    270373        }
    271374        psFree(offsets);
    272375    }
    273376#endif
     377
     378#if (defined TESTING && defined ASTROMETRY)
     379        // Double check: all offsets should be zero
     380        {
     381            psArray *matches = pmSourceMatchSources(sourceLists, radius, true); // List of matches
     382            if (!matches) {
     383                psError(PPSTACK_ERR_DATA, false, "Unable to match sources");
     384                psFree(zp);
     385                return false;
     386            }
     387            psArray *offsets = pmSourceMatchRelastro(matches, num, tol, iter1, starRej1,
     388                                                     iter2, starRej2, starLimit); // Shifts for each image
     389            for (int i = 0; i < num; i++) {
     390                psVector *offset = offsets->data[i]; // Offsets for image
     391                fprintf(stderr, "Offset of image %d: %f,%f\n", i, offset->data.F32[0], offset->data.F32[1]);
     392            }
     393            psFree(offsets);
     394            psFree(matches);
     395        }
     396#endif
     397
    274398
    275399    if (options->photometry) {
Note: See TracChangeset for help on using the changeset viewer.