Changeset 17958 for branches/eam_branch_20080511/ppSim/src/ppSimCreate.c
- Timestamp:
- Jun 5, 2008, 5:16:44 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branch_20080511/ppSim/src/ppSimCreate.c
r17935 r17958 3 3 pmFPAfile *ppSimCreate(pmConfig *config) 4 4 { 5 bool status; 6 bool simImage = false; 7 PS_ASSERT_PTR_NON_NULL(config, NULL); 8 pmFPA *fpa = NULL; 9 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; 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, "PPSIM.INPUT is not of type IMAGE"); 25 return NULL; 26 } 27 } 28 29 // generate the fpa structure used by the output camera (determined from INPUT or specified) 30 assert (config->camera); 31 fpa = pmFPAConstruct(config->camera, config->cameraName); // 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); 102 return NULL; 103 } 104 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; 110 } 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"); 5 bool status; 6 bool simImage = false; 7 PS_ASSERT_PTR_NON_NULL(config, NULL); 8 pmFPA *fpa = NULL; 9 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; 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, "PPSIM.INPUT is not of type IMAGE"); 25 return NULL; 26 } 27 } 28 29 // generate the fpa structure used by the output camera (determined from INPUT or specified) 30 assert (config->camera); 31 fpa = pmFPAConstruct(config->camera, config->cameraName); // 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"); 120 45 psFree(fpa); 121 46 return NULL; 122 } 123 } 124 } 125 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; 134 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 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 140 55 psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSIM_RECIPE); // Recipe 141 56 142 ppSimArgToRecipeF32(&status, recipe, "EXPTIME", input->fpa->concepts, "FPA.EXPOSURE"); 143 char *filter = ppSimArgToRecipeStr(&status, recipe, "FILTER", input->fpa->concepts, "FPA.FILTER"); 144 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 } 171 } 172 173 pmCell *cell; // Cell from chip 174 while ((cell = pmFPAviewNextCell(view, fpa, 1))) { 175 if (phuLevel == PM_FPA_LEVEL_CELL) { 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 // XXX optionally select fake and/or force 93 94 // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?) 95 pmFPAfile *fakeImage = pmFPAfileDefineChipMosaic(config, output->fpa, "PPSIM.FAKE.CHIP"); 96 if (!fakeImage) { 97 psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.FAKE.CHIP")); 98 psFree(fpa); 99 return NULL; 100 } 101 if (fakeImage->type != PM_FPA_FILE_IMAGE) { 102 psError(PS_ERR_IO, true, "PPSIM.FAKE.CHIP is not of type IMAGE"); 103 psFree(fpa); 104 return NULL; 105 } 106 107 // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?) 108 pmFPAfile *forceImage = NULL; 109 if (input) { 110 forceImage = pmFPAfileDefineChipMosaic(config, input->fpa, "PPSIM.FORCE.CHIP"); 111 if (!forceImage) { 112 psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.FORCE.CHIP")); 113 psFree(fpa); 114 return NULL; 115 } 116 if (forceImage->type != PM_FPA_FILE_IMAGE) { 117 psError(PS_ERR_IO, true, "PPSIM.FORCE.CHIP is not of type IMAGE"); 118 psFree(fpa); 119 return NULL; 120 } 121 } 122 123 // define associated psphot input/output files 124 if (!ppSimPhotomFiles (config, fakeImage, forceImage)) { 125 psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot"); 126 psFree(fpa); 127 return NULL; 128 } 129 } else { 130 // have we supplied a psf model? this happens in ppSimPhotomFiles if we request a photometry 131 // analysis. however, even if we do not, a psf model may be used to generate the fake 132 // sources. 133 if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) { 134 // tie the psf file to the chipMosaic 135 pmFPAfileBindFromArgs(&status, output, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF"); 136 if (!status) { 137 psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD"); 138 psFree(fpa); 139 return NULL; 140 } 141 } 142 } 143 144 // PPSIM.SOURCES carries the constructed, fake sources with their true parameters 145 // XXX only invoke this code for OBJECT types of images? 146 pmFPAfile *simSources = pmFPAfileDefineOutput (config, output->fpa, "PPSIM.SOURCES"); 147 if (!simSources) { 148 psError(PS_ERR_UNKNOWN, false, "Cannot find a rule for PPSIM.SOURCES"); 149 return false; 150 } 151 simSources->save = true; 152 153 // if we have loaded an input image, we derive certain values from the image, if possible 154 if (input) { 155 // we need to extract certain metadata from the image and populate the recipe. 156 // or else we need to set the fpa concepts based on the recipe options... 157 158 psMetadata *recipe = psMetadataLookupMetadata(&status, config->recipes, PPSIM_RECIPE); // Recipe 159 160 ppSimArgToRecipeF32(&status, recipe, "EXPTIME", input->fpa->concepts, "FPA.EXPOSURE"); 161 char *filter = ppSimArgToRecipeStr(&status, recipe, "FILTER", input->fpa->concepts, "FPA.FILTER"); 162 163 float zp = ppSimGetZeroPoint (recipe, filter); 164 psMetadataAddF32(recipe, PS_LIST_TAIL, "ZEROPOINT", PS_META_REPLACE, "Photometric zeropoint", zp); 165 } 166 167 pmFPALevel phuLevel = pmFPAPHULevel(output->format); // Level at which PHU goes 168 169 pmFPAview *view = pmFPAviewAlloc(0);// View for current level 170 171 if (phuLevel == PM_FPA_LEVEL_FPA) { 176 172 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; 181 } 182 } 183 } 184 } 185 186 psFree(fpa); 187 psFree(view); 188 189 return output; 173 psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA."); 174 psFree(fpa); 175 psFree(view); 176 return NULL; 177 } 178 } 179 180 pmChip *chip; // Chip from FPA 181 while ((chip = pmFPAviewNextChip(view, fpa, 1))) { 182 if (phuLevel == PM_FPA_LEVEL_CHIP) { 183 if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) { 184 psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA."); 185 psFree(fpa); 186 psFree(view); 187 return NULL; 188 } 189 } 190 191 pmCell *cell; // Cell from chip 192 while ((cell = pmFPAviewNextCell(view, fpa, 1))) { 193 if (phuLevel == PM_FPA_LEVEL_CELL) { 194 if (!pmFPAAddSourceFromView(fpa, "Simulation", view, output->format)) { 195 psError(PS_ERR_UNKNOWN, false, "Unable to add PHU to FPA."); 196 psFree(fpa); 197 psFree(view); 198 return NULL; 199 } 200 } 201 } 202 } 203 204 psFree(fpa); 205 psFree(view); 206 207 return output; 190 208 }
Note:
See TracChangeset
for help on using the changeset viewer.
