- Timestamp:
- May 3, 2010, 8:50:52 AM (16 years ago)
- Location:
- branches/simtest_nebulous_branches
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/simtest_nebulous_branches
- Property svn:mergeinfo changed
-
branches/simtest_nebulous_branches/ppSub/src
- Property svn:ignore
-
old new 13 13 ppSubErrorCodes.c 14 14 ppSubVersionDefinitions.h 15 ppSubConvolve
-
- Property svn:ignore
-
branches/simtest_nebulous_branches/ppSub/src/ppSubVarianceRescale.c
r24672 r27840 28 28 bool mdok; // Status of metadata lookups 29 29 30 psMetadata * ppSubRecipe = psMetadataLookupPtr(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSub31 psAssert( ppSubRecipe, "Need PPSUB recipe");30 psMetadata *recipe = psMetadataLookupPtr(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSub 31 psAssert(recipe, "Need PPSUB recipe"); 32 32 33 if (!psMetadataLookupBool(&mdok, ppSubRecipe, "VARIANCE.RESCALE")) return true; 33 if (!psMetadataLookupBool(&mdok, recipe, "RENORM")) return true; 34 35 int num = psMetadataLookupS32(&mdok, recipe, "RENORM.NUM"); 36 if (!mdok) { 37 psError(PPSUB_ERR_ARGUMENTS, true, "RENORM.NUM is not set in the recipe"); 38 return false; 39 } 40 float minValid = psMetadataLookupF32(&mdok, recipe, "RENORM.MIN"); 41 if (!mdok) { 42 psError(PPSUB_ERR_ARGUMENTS, true, "RENORM.MIN is not set in the recipe"); 43 return false; 44 } 45 float maxValid = psMetadataLookupF32(&mdok, recipe, "RENORM.MAX"); 46 if (!mdok) { 47 psError(PPSUB_ERR_ARGUMENTS, true, "RENORM.MAX is not set in the recipe"); 48 return false; 49 } 34 50 35 51 psImageMaskType maskBad = pmConfigMaskGet("BLANK", config); // Bits to mask 36 52 37 53 pmFPAview *view = ppSubViewReadout(); // View to readout 38 pmReadout * outRO= pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT"); // Output image54 pmReadout *readout = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT"); // Output image 39 55 40 psImage *image = outRO->image; // Image of interest 41 psImage *variance = outRO->variance; // Variance image 42 psImage *mask = outRO->mask; // Mask of interest 43 44 psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); 45 46 psLogMsg("psLib.psImageBackground", PS_LOG_DETAIL, "Generating the signal-to-noise image"); 47 48 // generate an image representing the signal/noise: 49 psImage *SN = psImageCopy (NULL, outRO->image, PS_TYPE_F32); 50 51 int nx = image->numCols; // Size of image 52 int ny = image->numRows; // Size of image 53 54 for (int y = 0; y < ny; y++) { 55 for (int x = 0; x < nx; x++) { 56 if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskBad) { 57 SN->data.F32[y][x] = 0.0; 58 continue; 59 } 60 if (!isfinite(image->data.F32[y][x])) { 61 SN->data.F32[y][x] = 0.0; 62 continue; 63 } 64 if (!isfinite(variance->data.F32[y][x]) || (variance->data.F32[y][x] < 0.0)) { 65 SN->data.F32[y][x] = 0.0; 66 continue; 67 } 68 SN->data.F32[y][x] = image->data.F32[y][x] / sqrt(variance->data.F32[y][x]); 69 } 56 if (!readout->variance) { 57 // Nothing to renormalise 58 psWarning("Renormalisation of the variance requested, but no variance provided."); 59 return true; 70 60 } 71 61 72 // these should not be chosen by the user: only a ROBUST version makes sense 73 psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV); 74 75 int Nsubset = psMetadataLookupS32(&mdok, ppSubRecipe, "VARIANCE.RESCALE.NSAMPLE"); 76 psAssert (mdok, "missing VARIANCE.RESCALE.NSAMPLE in ppSub recipe"); 77 78 const int Npixels = nx*ny; // Total number of pixels 79 Nsubset = PS_MIN(Nsubset, Npixels); // Number of pixels in subset 80 psLogMsg("psLib.psImageBackground", PS_LOG_DETAIL, "Searching for %d pixels in S/N image", Nsubset); 81 82 psVector *values = psVectorAllocEmpty(Nsubset, PS_TYPE_F32); // Vector containing subsample 83 84 // Minimum and maximum values 85 float min = +PS_MAX_F32; 86 float max = -PS_MAX_F32; 87 88 // select a subset of the image pixels to measure the stats 89 long n = 0; // Number of actual pixels in subset 90 if (Nsubset >= Npixels) { 91 // if we have an image smaller than Nsubset, just loop over the image pixels 92 for (int iy = 0; iy < ny; iy++) { 93 for (int ix = 0; ix < nx; ix++) { 94 if (!isfinite(image->data.F32[iy][ix]) || (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskBad)) { 95 continue; 96 } 97 98 float value = SN->data.F32[iy][ix]; 99 min = PS_MIN(value, min); 100 max = PS_MAX(value, max); 101 values->data.F32[n] = value; 102 n++; 103 } 104 } 105 } else { 106 for (long i = 0; i < Nsubset; i++) { 107 double frnd = psRandomUniform(rng); 108 int pixel = Npixels * frnd; 109 int ix = pixel % nx; 110 int iy = pixel / nx; 111 112 if (!isfinite(image->data.F32[iy][ix]) || (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskBad)) { 113 continue; 114 } 115 116 float value = SN->data.F32[iy][ix]; 117 min = PS_MIN(value, min); 118 max = PS_MAX(value, max); 119 values->data.F32[n] = value; 120 n++; 121 } 122 } 123 if (n < 0.01*Nsubset) { 124 psLogMsg("psLib.psImageBackground", PS_LOG_DETAIL, "Unable to measure image background: too few data points (%ld)", n); 125 psFree(values); 126 return false; 127 } 128 values->n = n; 129 psFree (SN); 130 131 psLogMsg("psLib.psImageBackground", PS_LOG_DETAIL, "Using for %ld pixels in S/N image", values->n); 132 133 if (!psVectorStats (stats, values, NULL, NULL, 0)) { 134 if (psTraceGetLevel("psLib.imageops") >= 5) { 135 FILE *f = fopen ("vector.dat", "w"); 136 int fd = fileno(f); 137 p_psVectorPrint (fd, values, "values"); 138 fclose (f); 139 } 140 psError(PS_ERR_UNKNOWN, false, "Unable to measure statistics for image background " 141 "(%dx%d, (row0,col0) = (%d,%d)", 142 image->numRows, image->numCols, image->row0, image->col0); 143 psFree(values); 144 return false; 145 } 146 if (psTraceGetLevel("psLib.imageops") >= 6) { 147 FILE *f = fopen ("vector.dat", "w"); 148 int fd = fileno(f); 149 p_psVectorPrint (fd, values, "values"); 150 fclose (f); 151 } 152 psFree (values); 153 psLogMsg ("ppSub", PS_LOG_INFO, "background stdev %f\n", stats->robustStdev); 154 155 float minValid = psMetadataLookupF32(&mdok, ppSubRecipe, "VARIANCE.RESCALE.MIN.VALID"); 156 psAssert (mdok, "missing VARIANCE.RESCALE.MIN.VALID in ppSub recipe"); 157 158 float maxValid = psMetadataLookupF32(&mdok, ppSubRecipe, "VARIANCE.RESCALE.MAX.VALID"); 159 psAssert (mdok, "missing VARIANCE.RESCALE.MAX.VALID in ppSub recipe"); 160 161 psLogMsg("psLib.psImageBackground", PS_LOG_DETAIL, "Stdev of S/N image: %f (mean: %f)", stats->robustStdev, stats->robustMedian); 162 163 // valid range of correction; 0.66 to 1.5 (skip if in range 0.98 to 1.02) 164 if ((stats->robustStdev < minValid) || (stats->robustStdev > maxValid)) { 165 psWarning ("background stdev (%f) is out of allowed range; not correcting\n", stats->robustStdev); 166 // XXX set quality to poor 167 psFree (stats); 168 return true; 169 } 170 171 // valid range of correction; 0.66 to 1.5 (skip if in range 0.98 to 1.02) 172 // if ((stats->robustStdev > 0.98) && (stats->robustStdev > 1.02)) { 173 // psLogMsg ("ppSub", PS_LOG_INFO, "background stdev (%f) is close to 1.0; do not modify variance\n", stats->robustStdev); 174 // // XXX set quality to poor 175 // psFree (stats); 176 // return true; 177 // } 178 179 float varianceFactor = PS_SQR(stats->robustStdev); 180 psLogMsg ("ppSub", PS_LOG_INFO, "background stdev is not 1.0: multiplying variance by %f\n", varianceFactor); 181 for (int y = 0; y < ny; y++) { 182 for (int x = 0; x < nx; x++) { 183 if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskBad) { 184 continue; 185 } 186 if (!isfinite(image->data.F32[y][x])) { 187 continue; 188 } 189 if (!isfinite(variance->data.F32[y][x]) || (variance->data.F32[y][x] < 0.0)) { 190 continue; 191 } 192 variance->data.F32[y][x] *= varianceFactor; 193 } 194 } 195 196 pmFPA *outFPA = outRO->parent->parent->parent; 197 pmHDU *outHDU = outFPA->hdu; // Output HDU 198 199 char history[80]; 200 snprintf (history, 80, "Rescaled variance by %6.4f (stdev by %6.4f)", varianceFactor, stats->robustStdev); 201 psMetadataAddStr(outHDU->header, PS_LIST_TAIL, "HISTORY", PS_META_DUPLICATE_OK, NULL, history); 202 203 psFree (stats); 204 return true; 62 return pmReadoutVarianceRenormalise(readout, maskBad, num, minValid, maxValid); 205 63 }
Note:
See TracChangeset
for help on using the changeset viewer.
