- Timestamp:
- Oct 19, 2010, 5:41:33 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/czw_branch/20100817/psModules/src/detrend/pmNonLinear.c
r12696 r29486 45 45 } } 46 46 47 48 # define PS_BIN_INTERPOLATE(RESULT, VECTOR, BOUNDS, BIN, VALUE) { \ 49 float dX, dY, Xo, Yo, Xt; \ 50 if (BIN == BOUNDS->n - 1) { \ 51 dX = 0.5*(BOUNDS->data.F32[BIN+1] - BOUNDS->data.F32[BIN-1]); \ 52 dY = VECTOR->data.F32[BIN] - VECTOR->data.F32[BIN-1]; \ 53 Xo = 0.5*(BOUNDS->data.F32[BIN+1] + BOUNDS->data.F32[BIN]); \ 54 Yo = VECTOR->data.F32[BIN]; \ 55 } else { \ 56 dX = 0.5*(BOUNDS->data.F32[BIN+2] - BOUNDS->data.F32[BIN]); \ 57 dY = VECTOR->data.F32[BIN+1] - VECTOR->data.F32[BIN]; \ 58 Xo = 0.5*(BOUNDS->data.F32[BIN+1] + BOUNDS->data.F32[BIN]); \ 59 Yo = VECTOR->data.F32[BIN]; \ 60 } \ 61 if (dY != 0) { \ 62 Xt = (VALUE - Yo)*dX/dY + Xo; \ 63 } else { \ 64 Xt = Xo; \ 65 } \ 66 Xt = PS_MIN (BOUNDS->data.F32[BIN+1], PS_MAX(BOUNDS->data.F32[BIN], Xt)); \ 67 psTrace("pmNonLinear", 6, "(Xo, Yo, dX, dY, Xt, Yt) is (%.2f %.2f %.2f %.2f %.2f %.2f)\n", \ 68 Xo, Yo, dX, dY, Xt, VALUE); \ 69 RESULT = Xt; } 70 47 71 pmReadout *pmNonLinearityLookup(pmReadout *inputReadout, const psVector *inFlux, const psVector *outFlux) 48 72 { … … 92 116 return inputReadout; 93 117 } 118 119 bool pmNonLinearityApply(pmReadout *inputReadout, psArray *Ltab) 120 { 121 PS_ASSERT_PTR_NON_NULL(inputReadout, false); 122 PS_ASSERT_PTR_NON_NULL(inputReadout->image, false); 123 PS_ASSERT_IMAGE_TYPE(inputReadout->image, PS_TYPE_F32, false); 124 PS_ASSERT_PTR_NON_NULL(Ltab, false); 125 126 // psS32 tableSize = Ltab->n; 127 128 psImage *image = inputReadout->image; 129 psS32 numSamples = 39; 130 131 psS32 numBorder = 10; 132 // Load default data. 133 psVector *default_row_correction_fluxes = psVectorAlloc(numSamples,PS_TYPE_F32); 134 psVector *default_col_correction_fluxes = psVectorAlloc(numSamples,PS_TYPE_F32); 135 psVector *row_correction_fluxes; 136 psVector *col_correction_fluxes; 137 138 psVector *default_row_correction_factors = psVectorAlloc(numSamples,PS_TYPE_F32); 139 psVector *default_col_correction_factors = psVectorAlloc(numSamples,PS_TYPE_F32); 140 psVector *row_correction_factors; 141 psVector *col_correction_factors; 142 143 int n = 0; 144 int m = 0; 145 for (int k = 0; k < Ltab->n; k++) { // Begin load default tables 146 psMetadata *row = Ltab->data[k]; 147 if (psMetadataLookupS32(NULL,row,"POSITION") != -1) { 148 continue; 149 } 150 if (psMetadataLookupS32(NULL,row,"DIRECTION") == 0) { 151 psVectorSet(default_row_correction_fluxes,n,psMetadataLookupF32(NULL,row,"FLUX")); 152 psVectorSet(default_row_correction_factors,n,psMetadataLookupF32(NULL,row,"FACTOR")); 153 n++; 154 } 155 else { 156 psVectorSet(default_col_correction_fluxes,m,psMetadataLookupF32(NULL,row,"FLUX")); 157 psVectorSet(default_col_correction_factors,m,psMetadataLookupF32(NULL,row,"FACTOR")); 158 m++; 159 } 160 } // End load default tables 161 162 for (int i = 0; i < image->numRows; i++) { // Loop over rows : note: problem with discontinuity here 163 n = 0; 164 if ((i < numBorder)||(image->numRows - i < numBorder)) { // Load row correction data 165 row_correction_fluxes = psVectorAlloc(numSamples,PS_TYPE_F32); 166 row_correction_factors = psVectorAlloc(numSamples,PS_TYPE_F32); 167 168 for (int k = 0; k < Ltab->n; k++) { 169 psMetadata *row = Ltab->data[k]; 170 if ((psMetadataLookupS32(NULL,row,"DIRECTION") != 0)|| 171 (psMetadataLookupS32(NULL,row,"POSITION") != i + 1)) { // image data is 0 indexed, but correction data is 1 indexed. 172 continue; 173 } 174 psVectorSet(row_correction_fluxes,n,psMetadataLookupF32(NULL,row,"FLUX")); 175 psVectorSet(row_correction_factors,n,psMetadataLookupF32(NULL,row,"FACTOR")); 176 // psTrace("psModules.camera",6,"rows: %d\n",n); 177 n++; 178 } 179 } // End load specific row 180 else { 181 row_correction_fluxes = default_row_correction_fluxes; 182 row_correction_factors = default_row_correction_factors; 183 } // End load default row 184 185 for (int j = 0; j < image->numCols; j++) { // Loop over columns 186 m = 0; 187 188 if ((j < numBorder)||(image->numCols - j < numBorder)) { // Load col correction data 189 col_correction_fluxes = psVectorAlloc(numSamples,PS_TYPE_F32); 190 col_correction_factors = psVectorAlloc(numSamples,PS_TYPE_F32); 191 for (int k = 0; k < Ltab->n; k++) { 192 psMetadata *row = Ltab->data[k]; 193 if ((psMetadataLookupS32(NULL,row,"DIRECTION") != 1)|| 194 (psMetadataLookupS32(NULL,row,"POSITION") != j + 1)) { 195 continue; 196 } 197 psVectorSet(col_correction_fluxes,m,psMetadataLookupF32(NULL,row,"FLUX")); 198 psVectorSet(col_correction_factors,m,psMetadataLookupF32(NULL,row,"FACTOR")); 199 // psTrace("psModules.camera",6,"columns: %d\n",m); 200 m++; 201 } 202 } // End load specific col 203 else { 204 col_correction_fluxes = default_col_correction_fluxes; 205 col_correction_factors = default_col_correction_factors; 206 } // End load default col 207 208 // Calculate correction factor contribution for this pixel. 209 psF32 factor_row = pmNonLinearityMeasure(image->data.F32[i][j], 210 row_correction_fluxes,row_correction_factors); 211 psF32 factor_col = pmNonLinearityMeasure(image->data.F32[i][j], 212 col_correction_fluxes,col_correction_factors); 213 214 if (((i <= 9)&&(j <= 9))||((i == 9)&&(j == 5))) { // Print out if we're looking at a test case. 215 psTrace("psModules.camera",6,"Linearity: %d %d %s %f %f %f %d %d\n",i,j, 216 psMetadataLookupStr(NULL,inputReadout->parent->concepts,"CELL.NAME"), 217 image->data.F32[i][j],factor_row,factor_col,numBorder,numSamples); 218 psTrace("psModules.camera",6,"Linearity: R: %d %d %d C: %d %d %d\n", 219 i,(i < numBorder),(image->numRows - i), 220 j,(j < numBorder),(image->numCols - j)); 221 222 /* psTrace("psModules.camera",6,"Linearity: V: "); */ 223 /* for (int k = 0; k < numSamples; k++) { */ 224 /* psTrace("psModules.camera",6,"(%f %f) ", */ 225 /* col_correction_fluxes->data.F32[k],col_correction_factors->data.F32[k]); */ 226 /* } */ 227 /* psTrace("psModules.camera",6,"\n"); */ 228 } // End Test case 229 230 // Apply correction to image data 231 image->data.F32[i][j] = image->data.F32[i][j] * ( factor_row * factor_col ) ; 232 233 // Free correction if we allocated 234 if ((j < numBorder)||(image->numCols - j < numBorder)) { 235 psFree(col_correction_fluxes); 236 psFree(col_correction_factors); 237 } 238 239 } // End loop over columns 240 241 // Free correction if we allocated 242 if ((i < numBorder)||(image->numRows - i < numBorder)) { 243 psFree(row_correction_fluxes); 244 psFree(row_correction_factors); 245 } 246 247 } // End loop over rows 248 249 psFree(default_row_correction_fluxes); 250 psFree(default_row_correction_factors); 251 psFree(default_col_correction_fluxes); 252 psFree(default_col_correction_factors); 253 254 return(true); 255 } 256 257 psF32 pmNonLinearityMeasure(psF32 flux, psVector *correction_fluxes, psVector *correction_factors) { 258 // psS32 numPixels = 0; 259 psF32 result = 0; 260 psU32 bin = 0; 261 262 bin = correction_fluxes->n - 1; 263 if (flux < correction_fluxes->data.F32[0]) { 264 return(1.0); 265 } 266 if (flux > correction_fluxes->data.F32[bin]) { 267 return(1.0); 268 } 269 270 for (int i = 0; i < correction_fluxes->n - 1; i++) { 271 if ((flux >= correction_fluxes->data.F32[i])&& 272 (flux < correction_fluxes->data.F32[i+1])) { 273 result = correction_factors->data.F32[i] + 274 (flux - correction_fluxes->data.F32[i]) * 275 ((correction_factors->data.F32[i+1] - correction_factors->data.F32[i]) / 276 (correction_fluxes->data.F32[i+1] - correction_fluxes->data.F32[i])); 277 continue; 278 } 279 } 280 281 /* PS_BIN_FOR_VALUE(bin,correction_fluxes,flux); */ 282 /* if ((bin < 0)||(bin > correction_fluxes->n)) { */ 283 /* return(1.0); */ 284 /* } */ 285 286 /* PS_BIN_INTERPOLATE(result,correction_fluxes,correction_factors,bin,flux); */ 287 if (!isfinite(result)) { 288 result = 1.0; 289 } 290 if (result <= 0) { 291 result = 1.0; 292 } 293 return(result); 294 } 295 296 297
Note:
See TracChangeset
for help on using the changeset viewer.
