IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14657


Ignore:
Timestamp:
Aug 23, 2007, 2:52:55 PM (19 years ago)
Author:
eugene
Message:

finish code re-org, add loading of external psf model

Location:
trunk/ppSim
Files:
4 added
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/ppSim/src/Makefile.am

    r14463 r14657  
    1313        ppSimMakeDark.c         \
    1414        ppSimMakeSky.c          \
    15         ppSimInsertStars.c      \
     15        ppSimInsertSources.c    \
    1616        ppSimAddOverscan.c      \
    1717        ppSimAddNoise.c         \
     
    1919        ppSimBounds.c           \
    2020        ppSimStars.c            \
     21        ppSimSetPSF.c           \
    2122        ppSimUtils.c            \
    2223        ppSimLoop.c
  • trunk/ppSim/src/ppSim.c

    r14463 r14657  
    44{
    55    psLibInit(NULL);
     6    if (!pmModelClassInit ()) abort();
     7
    68    pmConfig *config = pmConfigRead(&argc, argv, NULL); // Configuration
    79    if (!config) {
     
    1820    }
    1921
    20     psExit loopError;
    21     if ((loopError = ppSimLoop(config))) {
     22    if (!ppSimLoop(config)) {
    2223        psErrorStackPrint(stderr, "Unable to generate data.");
    2324        psFree(config);
    24         exit(loopError);
     25        exit(PS_EXIT_SYS_ERROR);
    2526    }
    2627
  • trunk/ppSim/src/ppSim.h

    r14463 r14657  
    7272
    7373/// Loop over the output file, generating simulated data
    74 psExit ppSimLoop(pmConfig *config ///< Configuration
     74bool ppSimLoop(pmConfig *config ///< Configuration
    7575                 );
    7676
     
    8989
    9090psVector *ppSimMakeBiassec (pmCell *cell, pmConfig *config);
    91 psVector *ppSimMakeBias (psImage *signal, psImage *variance, pmCell *cell, pmConfig *config, const psRandom *rng) ;
    92 bool ppSimMakeDark (psImage *signal, psImage *variance, pmConfig *config);
    93 bool ppSimMakeSky (psImage *signal, psImage *variance, psImage *expCorr, ppSimType type, pmConfig *config, pmFPA *fpa, pmChip *chip, pmCell *cell);
     91psVector *ppSimMakeBias (pmReadout *readout, pmConfig *config, const psRandom *rng) ;
     92bool ppSimMakeDark (pmReadout *readout, pmConfig *config);
     93bool ppSimMakeSky (pmReadout *readout, psImage *expCorr, ppSimType type, pmConfig *config);
    9494bool ppSimInsertStars (psImage *signal, psImage *variance, psImage *expCorr, psArray *stars, pmConfig *config, pmChip *chip, pmCell *cell);
     95bool ppSimInsertSources (pmReadout *readout, psImage *expCorr, psArray *stars, pmConfig *config);
    9596
    9697bool ppSimInitHeader(pmConfig *config,
     
    99100                     pmCell *cell);
    100101
    101 bool ppSimSaturate(psImage *image,      // Image to apply saturation
    102                    const pmConfig *config,                     
    103                    const pmCell *cell); // Saturation level
     102bool ppSimSaturate(pmReadout *readout,  // Image to apply saturation
     103                   const pmConfig *config); // Saturation level
    104104
    105105bool ppSimUpdateConceptsFPA (pmFPA *fpa, pmConfig *config);
     
    108108bool ppSimAddOverscan (pmReadout *readout, pmConfig *config, psVector *biasCols, psVector *biasRows, psRandom *rng);
    109109
    110 psImage *ppSimAddNoise(psImage *signal, // Signal image, modified and returned
    111                        const psImage *variance, // Variance image
    112                        const pmConfig *config,                 
    113                        const pmCell *cell,
    114                        const psRandom *rng // Random number generator
    115                        );
     110bool ppSimAddNoise(psImage *signal,
     111                   psImage *variance,
     112                   const pmCell *cell,                 
     113                   const pmConfig *config,                     
     114                   const psRandom *rng // Random number generator
     115    );
     116
     117bool ppSimSetPSF (pmChip *chip, pmConfig *config);
    116118
    117119#endif
  • trunk/ppSim/src/ppSimAddNoise.c

    r14463 r14657  
    22
    33// Add noise to an image
    4 psImage *ppSimAddNoise(psImage *signal, // Signal image, modified and returned
    5                        const psImage *variance, // Variance image
    6                        const pmConfig *config,                 
    7                        const pmCell *cell,
    8                        const psRandom *rng // Random number generator
     4bool ppSimAddNoise(psImage *signal, // Signal image, modified and returned
     5                   psImage *variance,
     6                   const pmCell *cell,
     7                   const pmConfig *config,                     
     8                   const psRandom *rng // Random number generator
    99    )
    1010{
     
    2222        if (!mdok) {
    2323            psError(PS_ERR_BAD_PARAMETER_VALUE, false, "Unable to find GAIN in recipe.");
    24             return NULL;
     24            return false;
    2525        }
    2626    }
     
    3838
    3939    // XXX why return this??
    40     return signal;
     40    return true;
    4141}
    4242
  • trunk/ppSim/src/ppSimAddOverscan.c

    r14463 r14657  
    2929        psImageInit(variance, PS_SQR(readnoise));
    3030
    31         ppSimAddNoise(signal, variance, config, readout->parent, rng);
     31        ppSimAddNoise(signal, variance, readout->parent, config, rng);
    3232        psListAdd(readout->bias, PS_LIST_TAIL, signal);
    3333
  • trunk/ppSim/src/ppSimArguments.c

    r14531 r14657  
    6666    psMetadataAddF32(arguments, PS_LIST_TAIL, "-starsmag", 0, "Brightest magnitude for fake stars", NAN);
    6767    psMetadataAddF32(arguments, PS_LIST_TAIL, "-starsdensity", 0, "Density of fake stars at magnitude", NAN);
     68    psMetadataAddStr(arguments, PS_LIST_TAIL, "-psfclass", 0, "Type of PSF model", NULL);
    6869    psMetadataAddS32(arguments, PS_LIST_TAIL, "-bin", 0, "Binning in x and y", 1);
    6970
     
    194195        psMetadataAddF32(config->arguments, PS_LIST_TAIL, "PA", 0, "Boresight position angle (radians)",
    195196                         pa * M_PI / 180.0);
     197
     198
     199        const char *psfClass = psMetadataLookupStr(NULL, arguments, "-psfclass"); // Filter name
     200        psMetadataAddStr(config->arguments, PS_LIST_TAIL, "PSF.MODEL", 0, "PSF model class", psfClass);
    196201    }
    197202
  • trunk/ppSim/src/ppSimCreate.c

    r14531 r14657  
    11# include "ppSim.h"
     2
     3// XXX this function forces us to define the camera (on the command line) and format (via the
     4// PPSIM.OUTPUT entry).  In this case, we need to set config->format,formatName based on these
     5// values.  This will be a problem when we want to load an input image (in order to add fake
     6// stars).  We will need to add some logic in ppSimArguments to distinguish the cases of 1)
     7// input image and 2) specified camera
    28
    39pmFPAfile *ppSimCreate(pmConfig *config)
     
    1218
    1319    pmFPAfile *file = pmFPAfileDefineOutput(config, fpa, OUTPUT_FILE);
    14     psFree(fpa);
    1520    if (!file) {
    1621        psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to create output file from %s.  "
     
    2025    if (file->type != PM_FPA_FILE_IMAGE) {
    2126        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "%s type is not IMAGE", OUTPUT_FILE);
     27        psFree(fpa);
    2228        psFree(file);
    2329        return NULL;
     
    2531    file->save = true;
    2632
     33    config->format = psMemIncrRefCounter (file->format);
     34    config->formatName = psStringCopy (file->formatName);
     35
    2736    // have we supplied a psf model?
    2837    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
    2938        bool status = false;
     39
     40        // tie the psf file to the chipMosaic
    3041        pmFPAfileBindFromArgs(&status, file, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
    3142        if (!status) {
    3243            psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
    33             return status;
     44            psFree(fpa);
     45            psFree(file);
     46            return NULL;
    3447        }
    3548    }
     49
     50    // XXX only invoke this code for OBJECT types of images
     51    // PPSIM.SOURCES carries the constructed, fake sources with their true parameters
     52    pmFPAfile *simSources = pmFPAfileDefineOutput (config, file->fpa, "PPSIM.SOURCES");
     53    if (!simSources) {
     54        psError(PS_ERR_UNKNOWN, false, "Cannot find a rule for PPSIM.SOURCES");
     55        return false;
     56    }
     57    simSources->save = true;
    3658
    3759    pmFPALevel phuLevel = pmFPAPHULevel(file->format); // Level at which PHU goes
     
    4466            psFree(fpa);
    4567            psFree(view);
     68            psFree(file);
    4669            return NULL;
    4770        }
     
    5578                psFree(fpa);
    5679                psFree(view);
     80                psFree(file);
    5781                return NULL;
    5882            }
     
    6690                    psFree(fpa);
    6791                    psFree(view);
     92                    psFree(file);
    6893                    return NULL;
    6994                }
     
    7196        }
    7297    }
     98
     99    psFree(fpa);
    73100    psFree(view);
    74101
  • trunk/ppSim/src/ppSimInsertSource.c

    r14531 r14657  
    11# include "ppSim.h"
    22
    3 bool ppSimInsertSources (psImage *signal, psImage *variance, psImage *expCorr, psArray *stars, pmConfig *config, pmChip *chip, pmCell *cell) {
     3bool ppSimInsertSource (psImage *signal, psImage *variance, psImage *expCorr, psArray *stars, pmConfig *config, pmChip *chip, pmCell *cell) {
    44
    55    bool mdok;
  • trunk/ppSim/src/ppSimInsertSources.c

    r14531 r14657  
    11# include "ppSim.h"
    22
    3 bool ppSimInsertSources (psImage *signal, psImage *variance, psImage *expCorr, psArray *stars, pmConfig *config, pmChip *chip, pmCell *cell) {
     3bool ppSimInsertSources (pmReadout *readout, psImage *expCorr, psArray *stars, pmConfig *config) {
    44
    55    bool mdok;
    66
    77    assert (stars);
     8    assert (readout);
    89   
     10    // XXX is this needed?
     11    // pmFPAfile *simSources = psMetadataLookupPtr(NULL, config->files, "PPSIM.SOURCES"); // Output sources
     12
     13    pmCell *cell = readout->parent;
     14    pmChip *chip = cell->parent;
     15
     16    // XXX this is an estimate of the sky noise based on the inputs to the image simulation.
     17    // XXX update this to allow the estimate based on the measured sky background
     18    // XXX this is missing the gain.
    919    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSIM_RECIPE); // Recipe
    1020
    11     float seeing = psMetadataLookupF32(NULL, config->arguments, "SEEING"); // Seeing sigma (pix)
    1221    float expTime = psMetadataLookupF32(NULL, config->arguments, "EXPTIME"); // Exposure time
    1322    float skyRate = psMetadataLookupF32(NULL, config->arguments, "SKY.RATE"); // Sky rate
     
    2332    }
    2433
     34    // Rough noise estimate, appropriate for entire cell (use for source radius?)
     35    float roughNoise = sqrtf(PS_SQR(readnoise) + (darkRate + skyRate) * expTime);
     36
    2537    int x0Chip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.X0");
    2638    int y0Chip = psMetadataLookupS32(NULL, chip->concepts, "CHIP.Y0");
     
    3547    int binning = psMetadataLookupS32(NULL, config->arguments, "BINNING"); // Binning in x and y
    3648
    37     // Rough noise estimate, appropriate for entire cell
    38     float roughNoise = sqrtf(PS_SQR(readnoise) + (darkRate + skyRate) * expTime);
     49    pmPSF *psf = psMetadataLookupPtr (&mdok, chip->analysis, "PSPHOT.PSF");
     50    assert (psf);
    3951
    40     pmPSF *psf = psMetadataLookupPtr (NULL, chip->analysis, "PSF");
     52    int dX = PM_CELL_TO_CHIP (0.0, x0Cell, xParityCell, binning);
     53    int dY = PM_CELL_TO_CHIP (0.0, y0Cell, yParityCell, binning);
    4154
     55    psArray *sources = psArrayAllocEmpty (stars->n);
     56
     57    // add sources to the readout image & weight
    4258    for (long i = 0; i < stars->n; i++) {
    4359        ppSimStar *star = stars->data[i];
    4460
     61        // star->x,y are in fpa coordinates
     62
    4563        // Position on the cell and peak flux
    46         float x = PPSIM_FPA_TO_CELL(star->x, x0Cell, xParityCell, binning, x0Chip, xParityChip);
    47         float y = PPSIM_FPA_TO_CELL(star->y, y0Cell, yParityCell, binning, y0Chip, yParityChip);
     64        float xChip = PM_FPA_TO_CHIP(star->x, x0Chip, xParityChip);
     65        float yChip = PM_FPA_TO_CHIP(star->y, y0Chip, yParityChip);
    4866
    49         // instantiate a model for the PSF at this location (normalized or not?)
    50         // XXX
    51         pmModel *model = pmModelFromPSFforXY (psf, star->x, star->y, star->flux);
     67        // Position on the cell and peak flux
     68        float xCell = PM_CHIP_TO_CELL(xChip, x0Cell, xParityCell, binning);
     69        float yCell = PM_CHIP_TO_CELL(yChip, y0Cell, yParityCell, binning);
     70
     71        if (xCell < 0) continue;
     72        if (yCell < 0) continue;
     73        if (xCell > readout->image->numCols) continue;
     74        if (yCell > readout->image->numRows) continue;
     75        // XXX need to apply col0, row0 if readout is a subarray
     76
     77        // XXX apply the expCorr to the star->flux before setting the model flux
     78
     79        // instantiate a model for the PSF at this location, set desired flux
     80        pmModel *model = pmModelFromPSFforXY (psf, xChip, yChip, 1.0);
     81        pmModelSetFlux (model, star->flux);
     82
     83        // XXX let the flux limit be a user-defined number of sky sigmas (not just 1.0)
     84        float radius = model->modelRadius (model->params, roughNoise);
     85        radius = PS_MAX (radius, 1.0);
    5286
    5387        // construct a source, with model flux pixels set, based on the model
    54         pmSource *source = pmSourceFromModel (model);
     88        pmSource *source = pmSourceFromModel (model, readout, radius, PM_SOURCE_TYPE_STAR);
    5589
    56         ppSimInsertSource(signal, variance, x, y, star->flux, roughNoise, seeing, expCorr);
     90        // XXX add the sources to a source array
     91
     92        // insert the source flux in the image
     93        pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
     94        psArrayAdd (sources, 100,source);
    5795    }
     96
     97    // NOTE: readout must be part of the pmFPAfile named "PPSIM.OUTPUT"
     98    psMetadataAdd (readout->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_DATA_ARRAY, "psphot sources", sources);
     99
     100    // XXX many leaks in here, i think
    58101    return true;
    59102}
  • trunk/ppSim/src/ppSimLoop.c

    r14531 r14657  
    11#include "ppSim.h"
    22
    3 psExit ppSimLoop(pmConfig *config)
     3bool ppSimLoop(pmConfig *config)
    44{
    55    PS_ASSERT_PTR_NON_NULL(config, PS_EXIT_PROG_ERROR);
     
    2929    pmFPAview *view = pmFPAviewAlloc(0);// View for iterating over FPA
    3030
     31    // XXX if we include the psphot analysis, we will need to activate the correct pmFPAfiles
     32    // at the correct times.  ppSim operates on PPSIM.OUTPUT and PPSIM.SOURCES
     33
    3134    // load any needed files (eg, input image, PSF)
    3235    if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
    33         psError(PSPHOT_ERR_DATA, false, "failed IO for fpa in ppSim\n");
     36        psError(PS_ERR_UNKNOWN, false, "failed IO for fpa in ppSim\n");
    3437        psFree(view);
    3538        return false;
     
    4346        // load any needed files (eg, input image, PSF)
    4447        if (!pmFPAfileIOChecks (config, view, PM_FPA_BEFORE)) {
    45             psError(PSPHOT_ERR_DATA, false, "failed IO for chip %d in ppSim\n", view->chip);
     48            psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in ppSim\n", view->chip);
    4649            psFree (view);
    4750            return false;
    4851        }
    4952
    50         pmPSF *psf = NULL;
    5153        if (type == PPSIM_TYPE_OBJECT) {
    52             psf = ppSimSetPSF (config);
     54            if (!ppSimSetPSF (chip, config)) {
     55                psError(PS_ERR_UNKNOWN, false, "failed IO for chip %d in ppSim\n", view->chip);
     56                psFree (view);
     57                return false;
     58            }
    5359        }
    5460
     
    6470                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "CELL.READDIR = 1 is the only supported mode.");
    6571                psFree(rng);
    66                 return PS_EXIT_CONFIG_ERROR;
     72                return false;
    6773            }
    6874
     
    7480
    7581                // TO DO: Decide if cell is to be windowed, reduce numCols, numRows appropriately
    76                 psImage *signal = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Signal in pixels
    77                 psImage *variance = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Noise in pixels
     82                readout->image = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Signal in pixels
     83                readout->weight = psImageAlloc(numCols, numRows, PS_TYPE_F32); // Noise in pixels
    7884
    7985                psImage *expCorr = NULL; // Exposure correction per pixel, for adding objects
     
    8288                }
    8389
    84                 psVector *biasRows = ppSimMakeBias (signal, variance, cell, config, rng);
     90                psVector *biasRows = ppSimMakeBias (readout, config, rng);
    8591                if (type == PPSIM_TYPE_BIAS) goto done;
    8692               
    87                 ppSimMakeDark (signal, variance, config);
     93                ppSimMakeDark (readout, config);
    8894                if (type == PPSIM_TYPE_DARK) goto done;
    8995
    90                 ppSimMakeSky (signal, variance, expCorr, type, config, fpa, chip, cell);
     96                ppSimMakeSky (readout, expCorr, type, config);
    9197                if (type == PPSIM_TYPE_FLAT) goto done;
    9298
    9399                if (type == PPSIM_TYPE_OBJECT) {
    94                     ppSimInsertStars (signal, variance, expCorr, stars, config, chip, cell);
     100                    ppSimInsertSources (readout, expCorr, stars, config);
    95101                }
    96102
    97103            done:
    98                 ppSimAddNoise(signal, variance, config, cell, rng);
    99                 ppSimSaturate(signal, config, cell);
    100 
    101                 readout->image = signal;
    102                 psFree(variance);
    103                 psFree(expCorr);
     104                ppSimAddNoise(readout->image, readout->weight, cell, config, rng);
     105                ppSimSaturate(readout, config);
    104106
    105107                ppSimAddOverscan (readout, config, biasCols, biasRows, rng);
     
    123125                psFree(rng);
    124126                psFree(view);
    125                 return PS_EXIT_SYS_ERROR;
     127                // return PS_EXIT_SYS_ERROR;
     128                return false;
    126129            }
    127130        }
     
    135138            psFree(rng);
    136139            psFree(view);
    137             return PS_EXIT_SYS_ERROR;
     140            // return PS_EXIT_SYS_ERROR;
     141            return false;
    138142        }
    139143
     
    148152        psFree(rng);
    149153        psFree(view);
    150         return PS_EXIT_SYS_ERROR;
     154        // return PS_EXIT_SYS_ERROR;
     155        return false;
    151156    }
    152157
     
    154159    psFree(view);
    155160
    156     return 0;
     161    return true;
    157162}
  • trunk/ppSim/src/ppSimMakeBias.c

    r14463 r14657  
    11# include "ppSim.h"
    22
    3 psVector *ppSimMakeBias (psImage *signal, psImage *variance, pmCell *cell, pmConfig *config, const psRandom *rng) {
     3psVector *ppSimMakeBias (pmReadout *readout, pmConfig *config, const psRandom *rng) {
    44
    55    bool mdok;
     6
     7    pmCell *cell = readout->parent;
    68
    79    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSIM_RECIPE); // Recipe
     
    2022        }
    2123    }
     24
     25    psImage *signal = readout->image;
     26    psImage *variance = readout->weight;
    2227
    2328    int numRows = signal->numRows;
  • trunk/ppSim/src/ppSimMakeDark.c

    r14463 r14657  
    22
    33// XXX add bounds to the inputs?
    4 bool ppSimMakeDark (psImage *signal, psImage *variance, pmConfig *config) {
     4bool ppSimMakeDark (pmReadout *readout, pmConfig *config) {
     5
     6    psImage *signal = readout->image;
     7    psImage *variance = readout->weight;
    58
    69    float darkRate = psMetadataLookupF32(NULL, config->arguments, "DARK.RATE"); // Dark rate
  • trunk/ppSim/src/ppSimMakeSky.c

    r14463 r14657  
    22
    33// XXX add bounds to the inputs?
    4 bool ppSimMakeSky (psImage *signal, psImage *variance, psImage *expCorr, ppSimType type, pmConfig *config, pmFPA *fpa, pmChip *chip, pmCell *cell) {
     4bool ppSimMakeSky (pmReadout *readout, psImage *expCorr, ppSimType type, pmConfig *config) {
     5
     6    psImage *signal = readout->image;
     7    psImage *variance = readout->weight;
     8
     9    pmCell *cell = readout->parent;
     10    pmChip *chip = cell->parent;
     11    pmFPA  *fpa  = chip->parent;
    512
    613    float expTime = psMetadataLookupF32(NULL, config->arguments, "EXPTIME"); // Exposure time
  • trunk/ppSim/src/ppSimSaturate.c

    r14463 r14657  
    22
    33// Apply saturation limit to image
    4 bool ppSimSaturate(psImage *image, // Image to apply saturation
    5                    const pmConfig *config,                     
    6                    const pmCell *cell // Saturation level
     4bool ppSimSaturate(pmReadout *readout, // Image to apply saturation
     5                   const pmConfig *config // configuration data
    76    )
    87{
    98    bool mdok;
     9
     10    psImage *image = readout->image;
     11    pmCell *cell = readout->parent;
    1012
    1113    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSIM_RECIPE); // Recipe
Note: See TracChangeset for help on using the changeset viewer.