IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 5, 2013, 2:20:46 PM (13 years ago)
Author:
eugene
Message:

stats is basically working now

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20130419/pswarp/src/pswarpLoop.c

    r35434 r35512  
    3636    if (!pswarpSetMaskBits(config)) {
    3737        psError(psErrorCodeLast(), false, "failed to set mask bits");
    38         return NULL;
     38        return false;
    3939    }
    4040
     
    8383    // XXX pmFPAfileActivate(config->files, true, "PSWARP.OUTPUT.SOURCES");
    8484
    85     // Done with the detector side of things
     85    // the loops below load the input data
    8686    pswarpFileActivation(config, detectorFiles, true);
    87     pswarpFileActivation(config, independentFiles, true);
     87
     88    // pswarpFileActivation(config, independentFiles, true);
    8889
    8990    pmFPAview *view = pmFPAviewAlloc(0);
     
    9596    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
    9697        psError(psErrorCodeLast(), false, "Unable to read files.");
    97         goto DONE;
     98        goto FAIL;
    9899    }
    99100
     
    107108        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
    108109            psError(psErrorCodeLast(), false, "Unable to read files.");
    109             goto DONE;
     110            goto FAIL;
    110111        }
    111112
     
    116117            if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
    117118                psError(psErrorCodeLast(), false, "Unable to read files.");
    118                 goto DONE;
     119                goto FAIL;
    119120            }
    120121
     
    124125                if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
    125126                    psError(psErrorCodeLast(), false, "Unable to read files.");
    126                     goto DONE;
     127                    goto FAIL;
    127128                }
    128129                if (!readout->data_exists) {
     
    145146                if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    146147                    psError(psErrorCodeLast(), false, "Unable to write files.");
    147                     goto DONE;
     148                    goto FAIL;
    148149                }
    149150            }
    150151            if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    151152                psError(psErrorCodeLast(), false, "Unable to write files.");
    152                 goto DONE;
     153                goto FAIL;
    153154            }
    154155        }
    155156        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    156157            psError(psErrorCodeLast(), false, "Unable to write files.");
    157             goto DONE;
     158            goto FAIL;
    158159        }
    159160    }
    160161    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
    161162        psError(psErrorCodeLast(), false, "Unable to write files.");
    162         goto DONE;
     163        goto FAIL;
    163164    }
    164165
     
    167168    pswarpFileActivation(config, independentFiles, false);
    168169
    169     // XXXX turn these on later .. pswarpUpdateStatistics (output);
     170    if (!pswarpUpdateStatistics (output->fpa, stats, input->fpa, astrom->fpa, config)) {
     171      psError(psErrorCodeLast(), false, "problem generating statistics.");
     172      goto FAIL;
     173    }
     174
    170175    // XXXX turn these on later .. pswarpMakePSF (output);
    171 
    172176    // XXX have not yet written the output to disk
    173 
    174177    psFree(view);
    175 
    176  DONE:
    177178    return true;
     179
     180 FAIL:
     181    psFree (view);
     182    return false;
    178183}
    179184
    180 # if (0)
    181185// once the output fpa elements have been built, loop over the fpa and generate stats
    182186// for each readout
    183 bool pswarpUpdateStatistics (pmFPA *output, pmFPA *input, pmConfig *config)  {
    184 
    185     // output mask bits
    186     psImageMaskType maskValue = psMetadataLookupImageMask(&status, recipe, "MASK.OUTPUT");
    187     psAssert (status, "MASK.OUTPUT was not defined");
     187bool pswarpTransformToTarget (pmFPA *output, pmReadout *input, pmConfig *config)  {
    188188
    189189    pmFPAview *view = pmFPAviewAlloc(0);
     
    195195
    196196        pmCell *cell;
    197         while ((cell = pmFPAviewNextCell (view, output->fpa, 1)) != NULL) {
    198             psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    199             if (!cell->process || !cell->file_exists) { continue; }
    200 
    201             // process each of the readouts
    202             pmReadout *readout;
    203             while ((readout = pmFPAviewNextReadout(view, output->fpa, 1)) != NULL) {
    204                 if (!readout->data_exists) {
    205                     psWarning("No overlap between input and skycell.");
    206                     if (stats) {
    207                         psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE,
    208                                          "No overlap between input and skycell", PSWARP_ERR_NO_OVERLAP);
    209                     }
    210                     psphotFilesActivate(config, false);
    211                     psFree(cells);
    212                     psFree(view);
    213                     goto DONE;
    214                 }
    215    
    216                 if (!pswarpPixelsLit(readout, stats, config)) {
    217                     psError(psErrorCodeLast(), false, "Unable to calculate pixel regions.");
    218                     psFree(view);
    219                     goto DONE;
    220                 }
    221                 bool doStats = psMetadataLookupBool(&mdok,recipe,"MASK.STATS");
    222                 if (doStats) {
    223                     if (!pswarpMaskStats(readout, stats, config)) {
    224                         psError(psErrorCodeLast(), false, "Unable to calculate mask stats.");
    225                         psFree(cells);
    226                         psFree(view);
    227                         goto DONE;
    228                     }
    229                 }
    230 
    231                 // Set covariance matrix for output
    232                 {
    233                     psList *covariances = psMetadataLookupPtr(&mdok, output->analysis,
    234                                                               PSWARP_ANALYSIS_COVARIANCES); // Covariance matrices
    235                     psAssert(covariances, "Should be there");
    236                     psArray *covars = psListToArray(covariances); // Array of covariance matrices
    237                     psKernel *covar = psImageCovarianceAverage(covars);
    238                     psFree(covars);
    239                     psMetadataRemoveKey(output->analysis, PSWARP_ANALYSIS_COVARIANCES);
    240 
    241                     // Correct covariance matrix scale for the mean (square root of the) Jacobian
    242                     double jacobian = psMetadataLookupF64(NULL, output->analysis, PSWARP_ANALYSIS_JACOBIAN); // Jacobian
    243                     int goodPixels = psMetadataLookupS32(NULL, output->analysis, PSWARP_ANALYSIS_GOODPIX);   // Good pixels
    244                     jacobian /= goodPixels;
    245                     output->covariance = psImageCovarianceScale(covar, jacobian);
    246                     psFree(covar);
    247 
    248                     if (output->variance) {
    249                         psImageCovarianceTransfer(output->variance, output->covariance);
    250                     }
    251                 }
    252             }
    253 
    254             // XXX need to get the input Cells for this output cell (save on the analysis somewhere
    255             // XXX skip this for now
    256 # if (0)
    257             psArray *inputCells = psMetadataLookupPtr (NULL, cell->analysis, "something");
    258             if (!pmConceptsAverageCells(cell, inputCells, NULL, NULL, false)) {
    259                 psError(psErrorCodeLast(), false, "Unable to average cell concepts.");
    260                 psFree(view);
    261                 goto DONE;
    262             }
    263 # endif
    264             // XXX Is this too ad-hoc?
    265             psRegion *trimsec = psMetadataLookupPtr(NULL, cell->concepts, "CELL.TRIMSEC"); ///< Trim section
    266             trimsec->x0 = trimsec->x1 = trimsec->y0 = trimsec->y1 = 0; ///< All pixels
    267 
    268         }
    269 
    270         // Perform statistics on the output image
    271         if (stats) {
    272             // XXX this should be done at each level...
    273             if (!ppStatsFPA(stats, output->parent->parent->parent, view, maskValue, config)) {
    274                 psWarning("Unable to perform statistics on warped image.");
    275             }
    276         }
    277     }
    278     if (!psMetadataCopy(output->concepts, input->concepts)) {
    279         psError(psErrorCodeLast(), false, "Unable to copy FPA concepts from input to output.");
    280         psFree(view);
    281         goto DONE;
    282     }
    283 
    284     // Update ZP from the astrometry
    285     {
    286         psMetadataItem *item = psMetadataLookup(outFPA->concepts, "FPA.ZP");
    287         item->data.F32 = psMetadataLookupF32(NULL, astrom->fpa->concepts, "FPA.ZP");
    288     }
    289 
    290     // need to update the chip and fpa level astrometry appropriate, see
    291     // code in psastro
    292    
    293 # if (0)
    294     // Add MD5 information for readout
    295     const char *chipName = psMetadataLookupStr(NULL, output->parent->parent->concepts, "CHIP.NAME");
    296     const char *cellName = psMetadataLookupStr(NULL, output->parent->concepts, "CELL.NAME");
    297     psString headerName = NULL; ///< Header name for MD5
    298     psStringAppend(&headerName, "MD5_%s_%s_%d", chipName, cellName, view->readout);
    299     psVector *md5 = psImageMD5(output->image); ///< md5 hash
    300     psString md5string = psMD5toString(md5); ///< String
    301     psFree(md5);
    302     psMetadataAddStr(hdu->header, PS_LIST_TAIL, headerName, PS_META_REPLACE,
    303                      "Image MD5", md5string);
    304     psFree(md5string);
    305     psFree(headerName);
    306 # endif
    307 
    308 # if (0)
    309     if (!pmAstromWriteWCS(hdu->header, outFPA, outChip, WCS_NONLIN_TOL)) {
    310         psError(psErrorCodeLast(), false, "Unable to generate WCS header.");
    311         goto DONE;
    312     }
    313 # endif
    314     return true;
    315 }
    316 # endif
    317 
    318 # if (0)
    319 // We need a new PSF model for the warped frame.  It would be good to generate this analytically, but
    320 // that's going to be tricky.  We have a list of sources, so we use those to redetermine the PSF model.
    321 bool pswarpMakePSF (pmConfig *config) {
    322 
    323     // load the recipe
    324     psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSWARP_RECIPE);
    325     if (!recipe) {
    326         psError(PSWARP_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
    327         return false;
    328     }
    329 
    330     if (psMetadataLookupBool(&mdok, recipe, "PSF")) {
    331         pswarpFileActivation(config, photFiles, true);
    332         if (!pswarpIOChecksBefore(config)) {
    333             psError(psErrorCodeLast(), false, "Unable to read files.");
    334             goto DONE;
    335         }
    336 
    337         // supply the readout and fpa of interest to psphot
    338         pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");
    339         pmFPACopy(photFile->fpa, outFPA);
    340 
    341         pmFPAview *view = pmFPAviewAlloc(0); ///< View into skycell
    342         view->chip = view->cell = view->readout = 0;
    343 
    344         // grab the sources of interest from the storage location (pmFPAfile PSPHOT.INPUT.CMF)
    345         psArray *sources = psphotLoadPSFSources (config, view);
    346         if (!sources) {
    347             psError(psErrorCodeLast(), false, "No sources supplied to measure PSF");
    348             goto DONE;
    349         }
    350 
    351         pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT);
    352 
    353         // measure the PSF using these sources
    354         if (!psphotReadoutFindPSF(config, view, "PSPHOT.INPUT", sources)) {
    355             // This is likely a data quality issue
    356             // XXX Split into multiple cases using error codes?
    357             psErrorStackPrint(stderr, "Unable to determine PSF");
    358             psWarning("Unable to determine PSF --- suspect bad data quality.");
    359             if (stats && psMetadataLookupS32(NULL, stats, "QUALITY") == 0) {
    360                 psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE,
    361                                  "Unable to determine PSF", psErrorCodeLast());
    362             }
    363             psErrorClear();
    364             psphotFilesActivate(config, false);
    365         }
    366 
    367         // Ensure seeing is carried over
    368         pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa);                 // Chip with seeing
    369         psMetadataItem *item = psMetadataLookup(outChip->concepts, "CHIP.SEEING"); // Concept with seeing
    370         item->data.F32 = psMetadataLookupF32(NULL, photChip->concepts, "CHIP.SEEING");
    371 
    372 // XXX EAM : put this in a visualization function
    373 #if (TESTING)
    374         {
    375             #define PSF_SIZE 20         ///< Half-size of PSF
    376             #define PSF_FLUX 10000      ///< Central flux for PSF
    377             pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa);
    378             pmPSF *psf = psMetadataLookupPtr(NULL, photChip->analysis, "PSPHOT.PSF");
    379             psImage *image = psImageAlloc(2 * PSF_SIZE + 1, 2 * PSF_SIZE + 1, PS_TYPE_F32);
    380             psImageInit(image, 0);
    381             pmModel *model = pmModelFromPSFforXY(psf, PSF_SIZE, PSF_SIZE, PSF_FLUX);
    382             pmModelAdd(image, NULL, model, PM_MODEL_OP_FULL, 0);
    383             psFree(model);
    384             psFits *fits = psFitsOpen("psf.fits", "w");
    385             psFitsWriteImage(fits, NULL, image, 0, NULL);
    386             psFitsClose(fits);
    387             psFree(image);
    388         }
    389 #endif
    390 
    391         psFree(view);
    392     }
    393     return true;
    394 }
    395 # endif
    396 
    397 // once the output fpa elements have been built, loop over the fpa and generate stats
    398 // for each readout
    399 bool pswarpTransformToTarget (pmFPA *output, pmReadout *input, pmConfig *config)  {
    400 
    401     pmFPAview *view = pmFPAviewAlloc(0);
    402    
    403     pmChip *chip;
    404     while ((chip = pmFPAviewNextChip (view, output, 1)) != NULL) {
    405         psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    406         if (!chip->process || !chip->file_exists) { continue; }
    407 
    408         pmCell *cell;
    409197        while ((cell = pmFPAviewNextCell (view, output, 1)) != NULL) {
    410198            psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
Note: See TracChangeset for help on using the changeset viewer.