IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

updates from trunk

Location:
branches/tap_branches
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

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

    r25847 r27838  
    1919
    2020
    21 // XXX these are generic functions which should be moved to psModules
    22 // Activate a list of files
    23 static void fileActivation(pmConfig *config, // Configuration
    24                            char **files, // Files to turn on/off
    25                            bool state   // Activation state
    26     )
    27 {
    28     for (int i = 0; files[i] != NULL; i++) {
    29         pmFPAfileActivate(config->files, state, files[i]);
    30     }
    31     return;
    32 }
    33 
    34 /**
    35  * Run down the FPA hierarchy, checking files
    36  */
    37 static bool ioChecksBefore(pmConfig *config ///< Configuration
    38                            )
    39 {
    40     pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking
    41     bool status = true;                 ///< Status of checks
    42     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    43     view->chip = 0;
    44     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    45     view->cell = 0;
    46     status &= pmFPAfileIOChecks(config, view, PM_FPA_BEFORE);
    47     psFree(view);
    48     return status;
    49 }
    50 
    51 /**
    52  * Run up the FPA hierarchy, checking files
    53  */
    54 static bool ioChecksAfter(pmConfig *config ///< Configuration
    55                           )
    56 {
    57     pmFPAview *view = pmFPAviewAlloc(0); ///< View for checking
    58     view->chip = view->cell = 0;
    59     bool status = true;                 ///< Status of checks
    60     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    61     view->cell = -1;
    62     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    63     view->chip = -1;
    64     status &= pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    65     psFree(view);
    66     return status;
    67 }
    68 
    69 
    70 /**
    71  * Loop over the inputs, warp them to the output skycell and then write out the output.
    72  */
    73 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)
    7424{
    7525    bool status;
     26    bool mdok;                          // Status of MD lookup
    7627
    7728    const char *skyCamera = psMetadataLookupStr(NULL, config->arguments,
    78                                                 "SKYCELL.CAMERA");  ///< Name of camera for skycell
     29                                                "SKYCELL.CAMERA");  // Name of camera for skycell
    7930    pmConfigCamerasCull(config, skyCamera);
    8031    pmConfigRecipesCull(config, "PSWARP,PPSTATS,PSPHOT,PSASTRO,MASKS,JPEG");
    81 
    8232
    8333    // load the recipe
    8434    psMetadata *recipe = psMetadataLookupPtr (&status, config->recipes, PSWARP_RECIPE);
    8535    if (!recipe) {
    86         psError(PSPHOT_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
     36        psError(PSWARP_ERR_CONFIG, false, "missing recipe %s", PSWARP_RECIPE);
    8737        return false;
    8838    }
    8939
    9040    if (!pswarpSetMaskBits(config)) {
    91         psError(PS_ERR_IO, false, "failed to set mask bits");
     41        psError(psErrorCodeLast(), false, "failed to set mask bits");
    9242        return NULL;
    9343    }
     
    11161
    11262    if (astrom->camera != input->camera) {
    113         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.");
    11464        return false;
    11565    }
     
    12777    psFree (view);
    12878
    129     bool mdok;                          ///< Status of MD lookup
    130     const char *statsName = psMetadataLookupStr(&mdok, config->arguments, "STATS"); ///< Filename for statistics
    131     psMetadata *stats = NULL;           ///< Container for statistics
    132     FILE *statsFile = NULL;             ///< File stream for statistics
    133     if (mdok && statsName && strlen(statsName) > 0) {
    134         psString resolved = pmConfigConvertFilename(statsName, config, true, true);
    135         statsFile = fopen(resolved, "w");
    136         if (!statsFile) {
    137             psError(PS_ERR_IO, true, "Unable to open statistics file %s for writing.\n", resolved);
    138             psFree(resolved);
    139             return false;
    140         }
    141         psFree(resolved);
    142         stats = psMetadataAlloc();
    143         psMetadataAddS32(stats, PS_LIST_TAIL, "QUALITY", 0, "No problems", 0);
    144     }
    14579
    14680    // Turn all skycell files on to generate them, and then turn them off for the loop over the input images
    14781    // the input, which is in a different format.
    14882    {
    149         fileActivation(config, detectorFiles, false);
    150         fileActivation(config, photFiles, false);
    151         fileActivation(config, independentFiles, false);
    152         fileActivation(config, skycellFiles, true);
    153         ioChecksBefore(config);
    154         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);
    15592    }
    15693
     
    16299        pmChip *chip;
    163100        pmFPAview *view = pmFPAviewAlloc(0);
    164         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        }
    165105        while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) {
    166106            psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    167107            if (!chip->process || !chip->file_exists) { continue; }
    168             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            }
    169112            pmCell *cell;
    170113            while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) {
    171114                psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    172115                if (!cell->process || !cell->file_exists) { continue; }
    173                 pmFPAfileIOChecks (config, view, PM_FPA_BEFORE);
    174                 pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    175             }
    176             pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    177         }
    178         pmFPAfileIOChecks (config, view, PM_FPA_AFTER);
    179         psFree(view);
    180 
    181         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);
    182134        pmFPAfileActivate(config->files, false, "PSWARP.ASTROM");
    183135    }
     
    201153    if (bilevelAstrometry) {
    202154        if (!pmAstromReadBilevelMosaic(input->fpa, phu->header)) {
    203             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.");
    204156            psFree(view);
    205157            psFree(stats);
    206             return false;
     158            goto DONE;
    207159        }
    208160    }
     
    211163
    212164    // files associated with the science image
    213     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    }
    214169
    215170    pmChip *chip;
     
    217172        psTrace ("pswarp", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
    218173        if (!chip->process || !chip->file_exists) { continue; }
    219         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        }
    220178
    221179        // read WCS data from the corresponding header
     
    223181        if (bilevelAstrometry) {
    224182            if (!pmAstromReadBilevelChip (chip, hdu->header)) {
    225                 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.");
    226184                psFree(view);
    227185                psFree(stats);
    228                 return false;
     186                goto DONE;
    229187            }
    230188        } else {
    231189            // we use a default FPA pixel scale of 1.0
    232190            if (!pmAstromReadWCS (input->fpa, chip, hdu->header, 1.0)) {
    233                 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.");
    234192                psFree(view);
    235193                psFree(stats);
    236                 return false;
     194                goto DONE;
    237195            }
    238196        }
     
    242200            psTrace ("pswarp", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
    243201            if (!cell->process || !cell->file_exists) { continue; }
    244             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            }
    245206
    246207            psListAdd(cells, PS_LIST_TAIL, cell);
     
    249210            pmReadout *readout;
    250211            while ((readout = pmFPAviewNextReadout(view, input->fpa, 1)) != NULL) {
    251                 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                }
    252216                if (!readout->data_exists) {
    253217                    continue;
    254218                }
    255219
    256                 // 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
    257221                // pswarpTransformReadout
    258222                pmReadout *astromRO = pmFPAviewThisReadout(view, astrom->fpa); // Readout for astrometry
    259                 psArray *sources = psMetadataLookupPtr(&mdok, astromRO->analysis,
    260                                                        "PSPHOT.SOURCES"); // Sources from astrometry
    261                 if (sources) {
    262                     psMetadataAddPtr(readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY,
    263                                      "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);
    264226                }
    265227
    266228                pswarpTransformReadout(output, readout, config);
    267229
    268                 pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    269             }
    270             pmFPAfileIOChecks(config, view, PM_FPA_AFTER);
    271         }
    272         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        }
    273244    }
    274245
     
    290261
    291262    if (!pswarpPixelsLit(output, stats, config)) {
    292         psError(PS_ERR_UNKNOWN, false, "Unable to calculate pixel regions.");
     263        psError(psErrorCodeLast(), false, "Unable to calculate pixel regions.");
    293264        psFree(cells);
    294265        psFree(view);
    295         return false;
     266        goto DONE;
    296267    }
    297268
     
    327298
    328299    if (!pmConceptsAverageCells(outCell, cells, NULL, NULL, false)) {
    329         psError(PS_ERR_UNKNOWN, false, "Unable to average cell concepts.");
     300        psError(psErrorCodeLast(), false, "Unable to average cell concepts.");
    330301        psFree(stats);
    331302        psFree(cells);
    332303        psFree(view);
    333         return false;
     304        goto DONE;
    334305    }
    335306    psFree(cells);
     
    339310
    340311    if (!psMetadataCopy(outFPA->concepts, input->fpa->concepts)) {
    341         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.");
    342313        psFree(stats);
    343314        psFree(view);
    344         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");
    345322    }
    346323
     
    355332        pmHDU *skyHDU = pmHDUFromCell(cell); ///< HDU
    356333        if (!skyHDU) {
    357             psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to find skycell HDU.");
     334            psError(PSWARP_ERR_DATA, false, "Unable to find skycell HDU.");
    358335            psFree(view);
    359             return false;
     336            goto DONE;
    360337        }
    361338        hdu->header = psMetadataCopy(hdu->header, skyHDU->header);
     
    365342
    366343    if (!pmAstromWriteWCS(hdu->header, outFPA, outChip, WCS_NONLIN_TOL)) {
    367         psError(PS_ERR_UNKNOWN, false, "Unable to generate WCS header.");
     344        psError(psErrorCodeLast(), false, "Unable to generate WCS header.");
    368345        psFree(stats);
    369         return false;
    370     }
    371 
    372     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    }
    373353
    374354    // Done with the detector side of things
    375     fileActivation(config, detectorFiles, false);
    376     fileActivation(config, independentFiles, false);
     355    pswarpFileActivation(config, detectorFiles, false);
     356    pswarpFileActivation(config, independentFiles, false);
    377357
    378358    // We need a new PSF model for the warped frame.  It would be good to generate this analytically, but
     
    380360
    381361    if (psMetadataLookupBool(&mdok, recipe, "PSF")) {
    382         fileActivation(config, photFiles, true);
    383         ioChecksBefore(config);
     362        pswarpFileActivation(config, photFiles, true);
     363        if (!pswarpIOChecksBefore(config)) {
     364            psError(psErrorCodeLast(), false, "Unable to read files.");
     365            goto DONE;
     366        }
    384367
    385368        // supply the readout and fpa of interest to psphot
     
    393376        psArray *sources = psphotLoadPSFSources (config, view);
    394377        if (!sources) {
    395             psError(PS_ERR_UNKNOWN, false, "No sources supplied to measure PSF");
    396             return false;
     378            psError(psErrorCodeLast(), false, "No sources supplied to measure PSF");
     379            goto DONE;
    397380        }
    398381
     
    412395            psphotFilesActivate(config, false);
    413396        }
     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");
    414402
    415403// XXX EAM : put this in a visualization function
     
    435423    }
    436424
    437     fileActivation(config, skycellFiles, true);
    438 
    439425    // Perform statistics on the output image
    440426    if (stats) {
     
    458444    psFree(view);
    459445
    460     // Ensure everything is written out, at every level
    461     ioChecksAfter(config);
    462 
    463     // Now done with the skycell side of things
    464 
    465446 DONE:
    466 
    467     // Write out summary statistics
    468     if (stats) {
    469         psMetadataAddF32(stats, PS_LIST_TAIL, "DT_WARP", 0, "Time for warp completion",
    470                          psTimerMark("pswarp"));
    471 
    472         const char *statsMDC = psMetadataConfigFormat(stats);
    473         if (!statsMDC) {
    474             psError(PS_ERR_IO, false, "Unable to get statistics MDC file.\n");
    475             psFree(stats);
    476             fclose(statsFile);
    477             return false;
    478         }
    479         fprintf(statsFile, "%s", statsMDC);
    480         psFree((void*)statsMDC);
    481         fclose(statsFile);
    482         pmConfigRunFilenameAddWrite(config, "STATS", statsName);
    483 
    484         psFree(stats);
    485     }
    486 
    487     // Dump configuration
    488     psString dump_file = psMetadataLookupStr(&status, config->arguments, "DUMP_CONFIG");
    489     if (dump_file) {
    490         if (!pmConfigDump(config, dump_file)) {
    491             psError(PS_ERR_IO, false, "Unable to dump configuration");
    492             return false;
    493         }
    494     }
    495447
    496448    return true;
Note: See TracChangeset for help on using the changeset viewer.