IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:50:52 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/simtest_nebulous_branches
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/simtest_nebulous_branches

  • branches/simtest_nebulous_branches/pswarp/src/pswarpLoop.c

    r25047 r27840  
    1616
    1717#define WCS_NONLIN_TOL 0.001            // Non-linear tolerance for header WCS
    18 #define PSPHOT_FIND_PSF 1               // Use psphot's findPSF function?
    1918#define TESTING 0                       // Testing output?
    2019
    2120
    22 // XXX these are generic functions which should be moved to psModules
    23 // Activate a list of files
    24 static void fileActivation(pmConfig *config, // Configuration
    25                            char **files, // Files to turn on/off
    26                            bool state   // Activation state
    27     )
    28 {
    29     for (int i = 0; files[i] != NULL; i++) {
    30         pmFPAfileActivate(config->files, state, files[i]);
    31     }
    32     return;
    33 }
    34 
    35 /**
    36  * Run down the FPA hierarchy, checking files
    37  */
    38 static bool ioChecksBefore(pmConfig *config ///< Configuration
    39                            )
    40 {
    41     pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking
    42     bool status = true;                 ///< Status of checks
    43     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    44     view->chip = 0;
    45     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    46     view->cell = 0;
    47     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    48     psFree(view);
    49     return status;
    50 }
    51 
    52 /**
    53  * Run up the FPA hierarchy, checking files
    54  */
    55 static bool ioChecksAfter(pmConfig *config ///< Configuration
    56                           )
    57 {
    58     pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking
    59     view->chip = view->cell = 0;
    60     bool status = true;                 ///< Status of checks
    61     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    62     view->cell = -1;
    63     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    64     view->chip = -1;
    65     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    66     psFree(view);
    67     return status;
    68 }
    69 
    70 
    71 /**
    72  * Loop over the inputs, warp them to the output skycell and then write out the output.
    73  */
    74 bool pswarpLoop(pmConfig *config)
     21
     22// Loop over the inputs, warp them to the output skycell and then write out the output.
     23bool pswarpLoop(pmConfig *config, psMetadata *stats)
    7524{
    7625    bool status;
     26    bool mdok;                          // Status of MD lookup
    7727
    7828    const char *skyCamera = psMetadataLookupStr(NULL, config->arguments,
    79                                                 "SKYCELL.CAMERA");  ///< Name of camera for skycell
     29                                                "SKYCELL.CAMERA");  // Name of camera for skycell
    8030    pmConfigCamerasCull(config, skyCamera);
    8131    pmConfigRecipesCull(config, "PSWARP,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
    82 
    8332
    8433    // load the recipe
    8534    psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSWARP_RECIPE);
    8635    if (!recipe) {
    87         psError(PSPHOT_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
     36        psError(PSWARP_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
    8837        return false;
    8938    }
    9039
    9140    if (!pswarpSetMaskBits(config)) {
    92         psError(PS_ERR_IO, false, "failed to set mask bits");
     41        psError(psErrorCodeLast(), false, "failed to set mask bits");
    9342        return NULL;
    9443    }
     
    11261
    11362    if (astrom->camera != input->camera) {
    114         psError(PS_ERR_UNKNOWN, true, "Input camera and astrometry camera do not match.");
     63        psError(PSWARP_ERR_DATA, true, "Input camera and astrometry camera do not match.");
    11564        return false;
    11665    }
     
    12877    psFree (view);
    12978
    130     bool mdok;                          ///< Status of MD lookup
    131     const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); ///< Filename for statistics
    132     psMetadata *stats = NULL;           ///< Container for statistics
    133     FILE *statsFile = NULL;             ///< File stream for statistics
    134     if (mdok && statsName && strlen(statsName) > 0) {
    135         psString resolved = pmConfigConvertFilename(statsName, config, true, true);
    136         statsFile = fopen(resolved, "w");
    137         if (!statsFile) {
    138             psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
    139             psFree(resolved);
    140             return false;
    141         }
    142         psFree(resolved);
    143         stats = psMetadataAlloc();
    144         psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", 0, "No problems", 0);
    145     }
    14679
    14780    // Turn all skycell files on to generate them, and then turn them off for the loop over the input images
    14881    // the input, which is in a different format.
    14982    {
    150         fileActivation(config, detectorFiles, false);
    151         fileActivation(config, photFiles, false);
    152         fileActivation(config, independentFiles, false);
    153         fileActivation(config, skycellFiles, true);
    154         ioChecksBefore(config);
    155         fileActivation(config, skycellFiles, false);
     83        pswarpFileActivation(config, detectorFiles, false);
     84        pswarpFileActivation(config, photFiles, false);
     85        pswarpFileActivation(config, independentFiles, false);
     86        pswarpFileActivation(config, skycellFiles, true);
     87        if (!pswarpIOChecksBefore(config)) {
     88            psError(psErrorCodeLast(), false, "Unable to read files.");
     89            goto DONE;
     90        }
     91        pswarpFileActivation(config, skycellFiles, false);
    15692    }
    15793
     
    16399        pmChip *chip;
    164100        pmFPAview *view = pmFPAviewAlloc(0);
    165         pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
     101        if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     102            psError(psErrorCodeLast(), false, "Unable to read files.");
     103            goto DONE;
     104        }
    166105        while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
    167106            psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    168107            if (!chip->process || !chip->file_exists) { continue; }
    169             pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
     108            if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     109                psError(psErrorCodeLast(), false, "Unable to read files.");
     110                goto DONE;
     111            }
    170112            pmCell *cell;
    171113            while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
    172114                psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    173115                if (!cell->process || !cell->file_exists) { continue; }
    174                 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
    175                 pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    176             }
    177             pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    178         }
    179         pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    180         psFree(view);
    181 
    182         fileActivation(config, detectorFiles, true);
     116                if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE) ||
     117                    !pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     118                    psError(psErrorCodeLast(), false, "Unable to read files.");
     119                    goto DONE;
     120                }
     121            }
     122            if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     123                psError(psErrorCodeLast(), false, "Unable to write files.");
     124                goto DONE;
     125            }
     126        }
     127        if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) {
     128            psError(psErrorCodeLast(), false, "Unable to write files.");
     129            goto DONE;
     130        }
     131        psFree(view);
     132
     133        pswarpFileActivation(config, detectorFiles, true);
    183134        pmFPAfileActivate(config->files, false, "PSWARP.ASTROM");
    184135    }
     
    202153    if (bilevelAstrometry) {
    203154        if (!pmAstromReadBilevelMosaic(input->fpa, phu->header)) {
    204             psError(PS_ERR_UNKNOWN, false, "Unable to read bilevel mosaic astrometry for input FPA.");
     155            psError(psErrorCodeLast(), false, "Unable to read bilevel mosaic astrometry for input FPA.");
    205156            psFree(view);
    206157            psFree(stats);
    207             return false;
     158            goto DONE;
    208159        }
    209160    }
     
    212163
    213164    // files associated with the science image
    214     pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
     165    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     166        psError(psErrorCodeLast(), false, "Unable to read files.");
     167        goto DONE;
     168    }
    215169
    216170    pmChip *chip;
     
    218172        psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    219173        if (!chip->process || !chip->file_exists) { continue; }
    220         pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
     174        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     175            psError(psErrorCodeLast(), false, "Unable to read files.");
     176            goto DONE;
     177        }
    221178
    222179        // read WCS data from the corresponding header
     
    224181        if (bilevelAstrometry) {
    225182            if (!pmAstromReadBilevelChip (chip, hdu->header)) {
    226                 psError(PS_ERR_UNKNOWN, false, "Unable to read bilevel chip astrometry for input FPA.");
     183                psError(psErrorCodeLast(), false, "Unable to read bilevel chip astrometry for input FPA.");
    227184                psFree(view);
    228185                psFree(stats);
    229                 return false;
     186                goto DONE;
    230187            }
    231188        } else {
    232189            // we use a default FPA pixel scale of 1.0
    233190            if (!pmAstromReadWCS (input->fpa, chip, hdu->header, 1.0)) {
    234                 psError(PS_ERR_UNKNOWN, false, "Unable to read WCS astrometry for input FPA.");
     191                psError(psErrorCodeLast(), false, "Unable to read WCS astrometry for input FPA.");
    235192                psFree(view);
    236193                psFree(stats);
    237                 return false;
     194                goto DONE;
    238195            }
    239196        }
     
    243200            psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    244201            if (!cell->process || !cell->file_exists) { continue; }
    245             pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
     202            if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
     203                psError(psErrorCodeLast(), false, "Unable to read files.");
     204                goto DONE;
     205            }
    246206
    247207            psListAdd(cells, PS_LIST_TAIL, cell);
     
    250210            pmReadout *readout;
    251211            while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
    252                 pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
     212                if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     213                    psError(psErrorCodeLast(), false, "Unable to read files.");
     214                    goto DONE;
     215                }
    253216                if (!readout->data_exists) {
    254217                    continue;
    255218                }
    256219
    257                 // Copy the sources from the astrometry carrier to the input, so they can be accessed by
     220                // Copy the detections from the astrometry carrier to the input, so they can be accessed by
    258221                // pswarpTransformReadout
    259222                pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry
    260                 psArray *sources = psMetadataLookupPtr(&mdok, astromRO->analysis,
    261                                                        "PSPHOT.SOURCES"); // Sources from astrometry
    262                 if (sources) {
    263                     psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,
    264                                      "Sources from input astrometry", sources);
     223                pmDetections *detections = psMetadataLookupPtr(&mdok, astromRO->analysis, "PSPHOT.DETECTIONS"); // Sources from astrometry
     224                if (detections) {
     225                    psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_DATA_ARRAY, "Sources from input astrometry", detections);
    265226                }
    266227
    267228                pswarpTransformReadout(output, readout, config);
    268229
    269                 pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    270             }
    271             pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    272         }
    273         pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
     230                if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     231                    psError(psErrorCodeLast(), false, "Unable to write files.");
     232                    goto DONE;
     233                }
     234            }
     235            if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     236                psError(psErrorCodeLast(), false, "Unable to write files.");
     237                goto DONE;
     238            }
     239        }
     240        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     241            psError(psErrorCodeLast(), false, "Unable to write files.");
     242            goto DONE;
     243        }
    274244    }
    275245
     
    291261
    292262    if (!pswarpPixelsLit(output, stats, config)) {
    293         psError(PS_ERR_UNKNOWN, false, "Unable to calculate pixel regions.");
     263        psError(psErrorCodeLast(), false, "Unable to calculate pixel regions.");
    294264        psFree(cells);
    295265        psFree(view);
    296         return false;
     266        goto DONE;
    297267    }
    298268
     
    328298
    329299    if (!pmConceptsAverageCells(outCell, cells, NULL, NULL, false)) {
    330         psError(PS_ERR_UNKNOWN, false, "Unable to average cell concepts.");
     300        psError(psErrorCodeLast(), false, "Unable to average cell concepts.");
    331301        psFree(stats);
    332302        psFree(cells);
    333303        psFree(view);
    334         return false;
     304        goto DONE;
    335305    }
    336306    psFree(cells);
     
    340310
    341311    if (!psMetadataCopy(outFPA->concepts, input->fpa->concepts)) {
    342         psError(PS_ERR_UNKNOWN, false, "Unable to copy FPA concepts from input to output.");
     312        psError(psErrorCodeLast(), false, "Unable to copy FPA concepts from input to output.");
    343313        psFree(stats);
    344314        psFree(view);
    345         return false;
     315        goto DONE;
     316    }
     317
     318    // Update ZP from the astrometry
     319    {
     320        psMetadataItem *item = psMetadataLookup(outFPA->concepts, "FPA.ZP");
     321        item->data.F32 = psMetadataLookupF32(NULL, astrom->fpa->concepts, "FPA.ZP");
    346322    }
    347323
     
    356332        pmHDU *skyHDU = pmHDUFromCell(cell); ///< HDU
    357333        if (!skyHDU) {
    358             psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find skycell HDU.");
     334            psError(PSWARP_ERR_DATA, false, "Unable to find skycell HDU.");
    359335            psFree(view);
    360             return false;
     336            goto DONE;
    361337        }
    362338        hdu->header = psMetadataCopy(hdu->header, skyHDU->header);
     
    366342
    367343    if (!pmAstromWriteWCS(hdu->header, outFPA, outChip, WCS_NONLIN_TOL)) {
    368         psError(PS_ERR_UNKNOWN, false, "Unable to generate WCS header.");
     344        psError(psErrorCodeLast(), false, "Unable to generate WCS header.");
    369345        psFree(stats);
    370         return false;
    371     }
    372 
    373     pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
     346        goto DONE;
     347    }
     348
     349    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     350        psError(psErrorCodeLast(), false, "Unable to write files.");
     351        goto DONE;
     352    }
    374353
    375354    // Done with the detector side of things
    376     fileActivation(config, detectorFiles, false);
    377     fileActivation(config, independentFiles, false);
     355    pswarpFileActivation(config, detectorFiles, false);
     356    pswarpFileActivation(config, independentFiles, false);
    378357
    379358    // We need a new PSF model for the warped frame.  It would be good to generate this analytically, but
    380359    // that's going to be tricky.  We have a list of sources, so we use those to redetermine the PSF model.
    381360
    382     if (psMetadataLookupBool(&mdok, config->arguments, "PSF")) {
    383         fileActivation(config, photFiles, true);
    384         ioChecksBefore(config);
     361    if (psMetadataLookupBool(&mdok, recipe, "PSF")) {
     362        pswarpFileActivation(config, photFiles, true);
     363        if (!pswarpIOChecksBefore(config)) {
     364            psError(psErrorCodeLast(), false, "Unable to read files.");
     365            goto DONE;
     366        }
    385367
    386368        // supply the readout and fpa of interest to psphot
     
    394376        psArray *sources = psphotLoadPSFSources (config, view);
    395377        if (!sources) {
    396             psError(PS_ERR_UNKNOWN, false, "No sources supplied to measure PSF");
    397             return false;
    398         }
     378            psError(psErrorCodeLast(), false, "No sources supplied to measure PSF");
     379            goto DONE;
     380        }
     381
     382        pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT);
    399383
    400384        // measure the PSF using these sources
     
    411395            psphotFilesActivate(config, false);
    412396        }
     397
     398        // Ensure seeing is carried over
     399        pmChip *photChip = pmFPAviewThisChip(view, photFile->fpa);                 // Chip with seeing
     400        psMetadataItem *item = psMetadataLookup(outChip->concepts, "CHIP.SEEING"); // Concept with seeing
     401        item->data.F32 = psMetadataLookupF32(NULL, photChip->concepts, "CHIP.SEEING");
    413402
    414403// XXX EAM : put this in a visualization function
     
    434423    }
    435424
    436     fileActivation(config, skycellFiles, true);
    437 
    438425    // Perform statistics on the output image
    439426    if (stats) {
     
    457444    psFree(view);
    458445
    459     // Ensure everything is written out, at every level
    460     ioChecksAfter(config);
    461 
    462     // Now done with the skycell side of things
    463 
    464446 DONE:
    465 
    466     // Write out summary statistics
    467     if (stats) {
    468         psMetadataAddF32(stats, PS_LIST_TAIL, "DT_WARP", 0, "Time for warp completion",
    469                          psTimerMark("pswarp"));
    470 
    471         const char *statsMDC = psMetadataConfigFormat(stats);
    472         if (!statsMDC) {
    473             psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
    474             psFree(stats);
    475             fclose(statsFile);
    476             return false;
    477         }
    478         fprintf(statsFile, "%s", statsMDC);
    479         psFree((void*)statsMDC);
    480         fclose(statsFile);
    481         pmConfigRunFilenameAddWrite(config, "STATS", statsName);
    482 
    483         psFree(stats);
    484     }
    485 
    486     // Dump configuration
    487     psString dump_file = psMetadataLookupStr(&status, config->arguments, "DUMP_CONFIG");
    488     if (dump_file) {
    489         pmConfigDump(config, dump_file);
    490     }
    491447
    492448    return true;
Note: See TracChangeset for help on using the changeset viewer.