IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 27, 2008, 7:12:46 PM (18 years ago)
Author:
eugene
Message:

working on the photometry of fake sources

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branch_20080511/ppSim/src/ppSimCreate.c

    r17706 r17818  
    33pmFPAfile *ppSimCreate(pmConfig *config)
    44{
    5     bool status;
    6     bool simImage = false;
    7     PS_ASSERT_PTR_NON_NULL(config, NULL);
    8     pmFPA *fpa = NULL;
     5  bool status;
     6  bool simImage = false;
     7  PS_ASSERT_PTR_NON_NULL(config, NULL);
     8  pmFPA *fpa = NULL;
    99
    10     // the input image defines the camera.  if it is not supplied, the user must have
    11     // supplied a camera and other metadata on the command line
    12     pmFPAfile *input = pmFPAfileDefineFromArgs (&status, config, "PPIMAGE.INPUT", "INPUT");
    13     if (!input) {
    14         // if we have not specified the camera already, we need to interpolate the recipes associated with this camera, and read other command-line recipes
    15         if (!pmConfigReadRecipes(config, PM_RECIPE_SOURCE_CL)) {
    16             psError(PS_ERR_IO, false, "Error merging recipes from camera config for %s", config->cameraName);
    17             return NULL;
    18         }
    19     } else {
    20       // If an image is supplied, we still generate a fake image and merge them together downstream
    21       // (otherwise, we get the variance wrong).
    22         simImage = false;
    23         if (input->type != PM_FPA_FILE_IMAGE) {
    24             psError(PS_ERR_IO, true, "PPIMAGE.INPUT is not of type IMAGE");
    25             return NULL;
    26         }
     10  // the input image defines the camera.  if it is not supplied, the user must have
     11  // supplied a camera and other metadata on the command line
     12  pmFPAfile *input = pmFPAfileDefineFromArgs (&status, config, "PPSIM.INPUT", "INPUT");
     13  if (!input) {
     14    // if we have not specified the camera already, we need to interpolate the recipes associated with this camera, and read other command-line recipes
     15    if (!pmConfigReadRecipes(config, PM_RECIPE_SOURCE_CL)) {
     16      psError(PS_ERR_IO, false, "Error merging recipes from camera config for %s", config->cameraName);
     17      return NULL;
    2718    }
     19  } else {
     20    // If an image is supplied, we still generate a fake image and merge them together downstream
     21    // (otherwise, we get the variance wrong).
     22    simImage = false;
     23    if (input->type != PM_FPA_FILE_IMAGE) {
     24      psError(PS_ERR_IO, true, "PPSIM.INPUT is not of type IMAGE");
     25      return NULL;
     26    }
     27  }
    2828
    29     // generate the fpa structure used by the output camera (determined from INPUT or specified)
    30     assert (config->camera);
    31     fpa = pmFPAConstruct(config->camera); // FPA to contain the observation
    32     if (!fpa) {
    33       psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to construct an FPA from camera configuration.");
     29  // generate the fpa structure used by the output camera (determined from INPUT or specified)
     30  assert (config->camera);
     31  fpa = pmFPAConstruct(config->camera); // FPA to contain the observation
     32  if (!fpa) {
     33    psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to construct an FPA from camera configuration.");
     34    return NULL;
     35  }
     36
     37  // define the output image file -- this is the basis for the ppSimLoop
     38  pmFPAfile *output = pmFPAfileDefineOutput(config, fpa, "PPSIM.OUTPUT");
     39  if (!output) {
     40    psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to create output file from PPSIM.OUTPUT. Did you forget to specify the format?");
     41    return NULL;
     42  }
     43  if (output->type != PM_FPA_FILE_IMAGE) {
     44    psError(PS_ERR_BAD_PARAMETER_TYPE, true, "PPSIM.OUTPUT type is not IMAGE");
     45    psFree(fpa);
     46    return NULL;
     47  }
     48  // XXX we should not require the output image to be written
     49  output->save = true;
     50
     51  config->format = psMemIncrRefCounter (output->format);
     52  config->formatName = psStringCopy (output->formatName);
     53
     54  // the recipe is now fully realized for the desired camera
     55  psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSIM_RECIPE); // Recipe
     56
     57  char *typeStr = psMetadataLookupStr(NULL, recipe, "IMAGE.TYPE"); // Type of image to simulate
     58  ppSimType type = ppSimTypeFromString (typeStr); // Type of image to simulate
     59
     60  if (type == PPSIM_TYPE_OBJECT) {
     61    // adjust the seeing by the scale
     62    float seeing = psMetadataLookupF32(&status, recipe, "SEEING");
     63    if (isnan(seeing)) {
     64      psError(PS_ERR_BAD_PARAMETER_TYPE, true, "seeing is not defined");
     65      psFree(fpa);
     66      return NULL;
     67    }
     68    float scale = psMetadataLookupF32(&status, recipe, "PIXEL.SCALE");
     69    if (isnan(scale)) {
     70      psError(PS_ERR_BAD_PARAMETER_TYPE, true, "pixel scale is not defined");
     71      psFree(fpa);
     72      return NULL;
     73    }
     74    psMetadataAddF32(recipe, PS_LIST_TAIL, "SEEING", PS_META_REPLACE, "Seeing SIGMA (pixels)", seeing / 2.0 / sqrt(2.0 * log(2.0)) / scale);
     75  }
     76
     77  if ((type == PPSIM_TYPE_OBJECT) || (type == PPSIM_TYPE_FLAT)) {
     78    // determine the zeropoint from the filter
     79    float zp = psMetadataLookupF32(&status, recipe, "ZEROPOINT");
     80    if (isnan(zp)) {
     81      char *filter = psMetadataLookupStr(&status, recipe, "FILTER");
     82      float zp = ppSimGetZeroPoint (recipe, filter);
     83      psMetadataAddF32(recipe, PS_LIST_TAIL, "ZEROPOINT", PS_META_REPLACE, "Photometric zeropoint", zp);
     84    }
     85  }
     86
     87  // For photometry, we operate on the chip-mosaicked image.  we create a copy of the mosaicked
     88  // image for psphot so we can write out a clean image
     89  bool doPhotom = psMetadataLookupBool(&status, recipe, "PHOTOM"); // Density of fakes
     90  if (doPhotom) {
     91
     92    // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?)
     93    pmFPAfile *chipImage = pmFPAfileDefineChipMosaic(config, output->fpa, "PPSIM.CHIP");
     94    if (!chipImage) {
     95      psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.CHIP"));
     96      psFree(fpa);
     97      return NULL;
     98    }
     99    if (chipImage->type != PM_FPA_FILE_IMAGE) {
     100      psError(PS_ERR_IO, true, "PPSIM.CHIP is not of type IMAGE");
     101      psFree(fpa);
    34102      return NULL;
    35103    }
    36104
    37     // define the output image file -- this is the basis for the ppSimLoop
    38     pmFPAfile *output = pmFPAfileDefineOutput(config, fpa, OUTPUT_FILE);
    39     if (!output) {
    40         psError(PS_ERR_UNEXPECTED_NULL, false, "Unable to create output file from %s.  "
    41                 "Did you forget to specify the format?", OUTPUT_FILE);
    42         return NULL;
     105    // define associated psphot input/output files
     106    if (!ppSimPhotomFiles (config, chipImage)) {
     107      psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot");
     108      psFree(fpa);
     109      return NULL;
    43110    }
    44     if (output->type != PM_FPA_FILE_IMAGE) {
    45         psError(PS_ERR_BAD_PARAMETER_TYPE, true, "%s type is not IMAGE", OUTPUT_FILE);
     111  } else {
     112    // have we supplied a psf model?  this happens in ppSimPhotomFiles if we request a photometry
     113    // analysis.  however, even if we do not, a psf model may be used to generate the fake
     114    // sources.
     115    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
     116      // tie the psf file to the chipMosaic
     117      pmFPAfileBindFromArgs(&status, output, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
     118      if (!status) {
     119        psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
    46120        psFree(fpa);
    47         return NULL;
     121        return NULL;
     122      }
    48123    }
    49     // XXX we should not require the output image to be written
    50     output->save = true;
     124  }
    51125
    52     config->format = psMemIncrRefCounter (output->format);
    53     config->formatName = psStringCopy (output->formatName);
     126  // PPSIM.SOURCES carries the constructed, fake sources with their true parameters
     127  // XXX only invoke this code for OBJECT types of images?
     128  pmFPAfile *simSources = pmFPAfileDefineOutput (config, output->fpa, "PPSIM.SOURCES");
     129  if (!simSources) {
     130    psError(PS_ERR_UNKNOWN, false, "Cannot find a rule for PPSIM.SOURCES");
     131    return false;
     132  }
     133  simSources->save = true;
    54134
    55     // the recipe is now fully realized for the desired camera
     135  // if we have loaded an input image, we derive certain values from the image, if possible
     136  if (input) {
     137    // we need to extract certain metadata from the image and populate the recipe.
     138    // or else we need to set the fpa concepts based on the recipe options...
     139
    56140    psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSIM_RECIPE); // Recipe
    57141
    58     char *typeStr = psMetadataLookupStr(NULL, recipe, "IMAGE.TYPE"); // Type of image to simulate
    59     ppSimType type = ppSimTypeFromString (typeStr); // Type of image to simulate
     142    ppSimArgToRecipeF32(&status, recipe, "EXPTIME", input->fpa->concepts, "FPA.EXPOSURE");
     143    char *filter = ppSimArgToRecipeStr(&status, recipe, "FILTER", input->fpa->concepts, "FPA.FILTER");
    60144
    61     if (type == PPSIM_TYPE_OBJECT) {
    62         // adjust the seeing by the scale
    63         float seeing = psMetadataLookupF32(&status, recipe, "SEEING");
    64         if (isnan(seeing)) {
    65             psError(PS_ERR_BAD_PARAMETER_TYPE, true, "seeing is not defined");
    66             psFree(fpa);
    67             return NULL;
    68         }
    69         float scale = psMetadataLookupF32(&status, recipe, "PIXEL.SCALE");
    70         if (isnan(scale)) {
    71             psError(PS_ERR_BAD_PARAMETER_TYPE, true, "pixel scale is not defined");
    72             psFree(fpa);
    73             return NULL;
    74         }
    75         psMetadataAddF32(recipe, PS_LIST_TAIL, "SEEING", PS_META_REPLACE, "Seeing SIGMA (pixels)", seeing / 2.0 / sqrt(2.0 * log(2.0)) / scale);
     145    float zp = ppSimGetZeroPoint (recipe, filter);
     146    psMetadataAddF32(recipe, PS_LIST_TAIL, "ZEROPOINT", PS_META_REPLACE, "Photometric zeropoint", zp);
     147  }
     148
     149  pmFPALevel phuLevel = pmFPAPHULevel(output->format); // Level at which PHU goes
     150
     151  pmFPAview *view = pmFPAviewAlloc(0);// View for current level
     152
     153  if (phuLevel == PM_FPA_LEVEL_FPA) {
     154    if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
     155      psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
     156      psFree(fpa);
     157      psFree(view);
     158      return NULL;
     159    }
     160  }
     161
     162  pmChip *chip;                       // Chip from FPA
     163  while ((chip = pmFPAviewNextChip(view, fpa, 1))) {
     164    if (phuLevel == PM_FPA_LEVEL_CHIP) {
     165      if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
     166        psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
     167        psFree(fpa);
     168        psFree(view);
     169        return NULL;
     170      }
    76171    }
    77172
    78     if ((type == PPSIM_TYPE_OBJECT) || (type == PPSIM_TYPE_FLAT)) {
    79         // determine the zeropoint from the filter
    80         float zp = psMetadataLookupF32(&status, recipe, "ZEROPOINT");
    81         if (isnan(zp)) {
    82             char *filter = psMetadataLookupStr(&status, recipe, "FILTER");
    83             float zp = ppSimGetZeroPoint (recipe, filter);
    84             psMetadataAddF32(recipe, PS_LIST_TAIL, "ZEROPOINT", PS_META_REPLACE, "Photometric zeropoint", zp);
     173    pmCell *cell;                   // Cell from chip
     174    while ((cell = pmFPAviewNextCell(view, fpa, 1))) {
     175      if (phuLevel == PM_FPA_LEVEL_CELL) {
     176        if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
     177          psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
     178          psFree(fpa);
     179          psFree(view);
     180          return NULL;
    85181        }
     182      }
    86183    }
     184  }
    87185
    88     // For photometry, we operate on the chip-mosaicked image.  we create a copy of the mosaicked
    89     // image for psphot so we can write out a clean image
    90     bool doPhotom = psMetadataLookupBool(&status, recipe, "PHOTOM"); // Density of fakes
    91     if (doPhotom) {
     186  psFree(fpa);
     187  psFree(view);
    92188
    93         // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?)
    94         pmFPAfile *chipImage = pmFPAfileDefineChipMosaic(config, output->fpa, "PPSIM.CHIP");
    95         if (!chipImage) {
    96             psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.CHIP"));
    97             psFree(fpa);
    98             return NULL;
    99         }
    100         if (chipImage->type != PM_FPA_FILE_IMAGE) {
    101             psError(PS_ERR_IO, true, "PPSIM.CHIP is not of type IMAGE");
    102             psFree(fpa);
    103             return NULL;
    104         }
    105 
    106         // *** Define two input files:
    107         // PPSIM.REAL.SOURCES
    108 
    109         // the input image(s) are required arguments; they define the camera
    110         pmFPAfile *realSources = pmFPAfileDefineFromArgs (&status, config, "PPSIM.REAL.SOURCES", "REAL.SOURCES");
    111         if (!status) {
    112           psError(PS_ERR_UNKNOWN, false, "Failed to build FPA from PPSIM.REAL.SOURCES");
    113           return false;
    114         }
    115 
    116         // this file is just used as a carrier; output files (eg, PSPHOT.RESID) are defined by
    117         // psphotDefineFiles
    118         pmFPAfile *psphotInput = pmFPAfileDefineFromFile (config, chipImage, 1, 1, "PSPHOT.INPUT");
    119         PS_ASSERT (psphotInput, false);
    120 
    121         // define associated psphot input/output files
    122         if (!psphotDefineFiles (config, psphotInput)) {
    123             psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot");
    124             psFree(fpa);
    125             return NULL;
    126         }
    127     } else {
    128         // have we supplied a psf model?  this happens in psphotDefineFiles if we request a photometry
    129         // analysis.  however, even if we do not, a psf model may be used to generate the fake
    130         // sources.
    131         if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
    132             // tie the psf file to the chipMosaic
    133             pmFPAfileBindFromArgs(&status, output, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
    134             if (!status) {
    135                 psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
    136                 psFree(fpa);
    137                 return NULL;
    138             }
    139         }
    140     }
    141 
    142     // PPSIM.SOURCES carries the constructed, fake sources with their true parameters
    143     // XXX only invoke this code for OBJECT types of images?
    144     pmFPAfile *simSources = pmFPAfileDefineOutput (config, output->fpa, "PPSIM.SOURCES");
    145     if (!simSources) {
    146         psError(PS_ERR_UNKNOWN, false, "Cannot find a rule for PPSIM.SOURCES");
    147         return false;
    148     }
    149     simSources->save = true;
    150 
    151     // if we have loaded an input image, we derive certain values from the image, if possible
    152     if (input) {
    153         // we need to extract certain metadata from the image and populate the recipe.
    154         // or else we need to set the fpa concepts based on the recipe options...
    155 
    156         psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSIM_RECIPE); // Recipe
    157 
    158         ppSimArgToRecipeF32(&status, recipe, "EXPTIME", input->fpa->concepts, "FPA.EXPOSURE");
    159         char *filter = ppSimArgToRecipeStr(&status, recipe, "FILTER", input->fpa->concepts, "FPA.FILTER");
    160 
    161         float zp = ppSimGetZeroPoint (recipe, filter);
    162         psMetadataAddF32(recipe, PS_LIST_TAIL, "ZEROPOINT", PS_META_REPLACE, "Photometric zeropoint", zp);
    163     }
    164 
    165     pmFPALevel phuLevel = pmFPAPHULevel(output->format); // Level at which PHU goes
    166 
    167     pmFPAview *view = pmFPAviewAlloc(0);// View for current level
    168 
    169     if (phuLevel == PM_FPA_LEVEL_FPA) {
    170         if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
    171             psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
    172             psFree(fpa);
    173             psFree(view);
    174             return NULL;
    175         }
    176     }
    177 
    178     pmChip *chip;                       // Chip from FPA
    179     while ((chip = pmFPAviewNextChip(view, fpa, 1))) {
    180         if (phuLevel == PM_FPA_LEVEL_CHIP) {
    181             if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
    182                 psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
    183                 psFree(fpa);
    184                 psFree(view);
    185                 return NULL;
    186             }
    187         }
    188 
    189         pmCell *cell;                   // Cell from chip
    190         while ((cell = pmFPAviewNextCell(view, fpa, 1))) {
    191             if (phuLevel == PM_FPA_LEVEL_CELL) {
    192                 if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) {
    193                     psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA.");
    194                     psFree(fpa);
    195                     psFree(view);
    196                     return NULL;
    197                 }
    198             }
    199         }
    200     }
    201 
    202     psFree(fpa);
    203     psFree(view);
    204 
    205     return output;
     189  return output;
    206190}
Note: See TracChangeset for help on using the changeset viewer.