Changeset 42724 for trunk/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c
- Timestamp:
- Oct 15, 2024, 10:57:32 AM (21 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c
r42392 r42724 14 14 #include "burnparams.h" 15 15 16 STATIC double ybuf[MAXSIZE], zbuf[MAXSIZE], wbuf[MAXSIZE], wbuf0[MAXSIZE] ;16 STATIC double ybuf[MAXSIZE], zbuf[MAXSIZE], wbuf[MAXSIZE], wbuf0[MAXSIZE],xbuf[MAXSIZE]; 17 17 18 18 /****************************************************************/ … … 92 92 STATIC int fit_trail(int nx, int ny, int NX, 93 93 DTYPE *data, MTYPE *mask, OBJBOX *box, 94 int up, int bckgnd, int rms, int fitfunc )94 int up, int bckgnd, int rms, int fitfunc,char *camera) 95 95 { 96 int i, j, k, err, nfit,firstfitpix ;97 int xs, xe, y0, y1, y2, dy, yfit ;98 double slope, zero, zsum, wsum, trial,zfun ;96 int i, j, k, err, nfit,firstfitpix,secondfitpix, xwidth; 97 int xs, xe, y0, y1, y2, dy, yfit,x0,x1; 98 double slope, zero, zsum, wsum, trial,zfun,xsum,xmean,xstd; 99 99 char fooname[60]; 100 100 FILE *fp=stdout; … … 104 104 return(-1); 105 105 } 106 106 107 107 /*xs = box->sxfit; */ 108 108 /*xe = box->exfit; */ … … 110 110 xs = box->sx; 111 111 xe = box->ex; 112 112 113 113 114 if(up) { … … 119 120 yfit = box->sy; /* Start of validity, TBD later by col */ 120 121 firstfitpix = MAX((box->ey - box->sy)*2,20) ; /* Number of pixels to include in the initial fit*/ 122 secondfitpix = 200; /* Number of pixels to include in the second fit*/ 121 123 } else { 122 124 //y0 = (box->y0m + box->y1m + box->y0p + box->y1p + 2) / 4; … … 127 129 yfit = box->ey; /* Start of validity, TBD later by col */ 128 130 firstfitpix = 100; /* Number of pixels to include in the initial fit*/ 129 131 secondfitpix = 200; /* Number of pixels to include in the second fit*/ 132 } 133 134 if(!strcmp(camera,"gpc2")) { 135 /*Use the inner third of the star width for this test. Should be brightest there*/ 136 xwidth = MAX((box->ex-box->sx)/3,10); 137 x0 = box->sx + (box->ex-box->sx)/2 - xwidth/2; 138 x1 = box->sx + (box->ex-box->sx)/2 + xwidth/2; 139 /*when fitting a burn, do not want to include part of the star*/ 140 /*add 20% of box size on top, with a maximum of ten pixels*/ 141 if(up) y1 = box->ey + MIN((box->ey-box->sy)/5,10); 142 } else { 143 //regular gpc1 setup 144 x0 = box->sx; 145 x1 = box->ex; 130 146 } 131 147 132 148 if(VERBOSE & VERB_FIT) { 133 printf("Fit requested xs,xe,y0,y1,y2,dy,up,f =%d %d %d %d %d %d %d %d\n",134 xs, xe, y0, y1, y2, dy, up, fitfunc );149 printf("Fit requested xs,xe,y0,y1,y2,dy,up,f,x0,x1= %d %d %d %d %d %d %d %d %d %d\n", 150 xs, xe, y0, y1, y2, dy, up, fitfunc,x0,x1); 135 151 } 136 152 … … 184 200 ybuf[j] = Y_SCALE * dy * (j-y0); 185 201 zbuf[j]=0.0; 186 for(i=x s, k=0; i<=xe; i++) {202 for(i=x0, k=0; i<=x1; i++) { 187 203 if(data[i+j*NX] != NODATA && (mask[i+j*NX] == MASK_NONE || mask[i+j*NX] == MASK_SAT_HALO) ) { 188 204 zbuf[j] += data[i+j*NX]; … … 221 237 /* Trim away any really big positive deviations */ 222 238 for(j=y1; j<=y2; j++) { 223 // if(zbuf[j]-(ybuf[j]*slope+zero) > MAXDEV &&224 // zbuf[j] > log(MAXRMS*rms)) wbuf[j] = 0;225 239 if(abs(zbuf[j]-(ybuf[j]*slope+zero)) > MAXDEV) wbuf[j] = 0; 226 if(VERBOSE & VERB_FITPROF) { 227 fprintf(fp, "%9.4f %9.4f %9.4f %3d\n", ybuf[j], zbuf[j], wbuf[j], j); 240 if(j < (y2-secondfitpix) && !up) wbuf[j] = 0; 241 if(j > (y1+secondfitpix) && up) wbuf[j] = 0; 242 if(VERBOSE & VERB_FIT) { 243 printf("trim j : %d %9.4f %9.4f %9.4f %d %9.4f %9.4f\n", j,ybuf[j],zbuf[j],(ybuf[j]*slope+zero),abs(zbuf[j]-(ybuf[j]*slope+zero)), wbuf[j], wbuf0[j]); 228 244 } 229 245 } … … 237 253 238 254 if(VERBOSE & VERB_FITPROF) { 239 printf(" jt %d %.3f %.3f\n", xs, zero, slope);255 printf("2nd fit nfit, zero, slope: %d %.3f %.3f\n", nfit, zero, slope); 240 256 fclose(fp); 241 257 } … … 249 265 linearrms(nfit, ybuf+y1, zbuf+y1, slope, zero, &trial); 250 266 /* 100203 JT: bad idea: appears to be a bug in read/writing ABS(nfit) */ 267 251 268 // if(trial > 2*rms) box->nfit = -box->nfit; 252 269 if(trial > 2*rms) box->func = BURN_POSSLOPE; … … 256 273 } 257 274 258 /* Reconstruct the fit for comparison with the data col by col */ 259 for(j=y1; j<=y2; j++) zbuf[j] = exp(ybuf[j]*slope); 260 /* And tag on the extras to determine the end of the fit */ 261 for(j=yfit; dy*j<dy*(up?y1:y2); j+=dy) { 262 if(fitfunc == BURN_EXP) { 263 zbuf[j] = exp(Y_SCALE*dy*(j-y0)*slope); 275 /* find where the fitted function becomes less than half the rms and cut the trail there*/ 276 for(j=(up?y1:y2); dy*j<dy*(up?y2:y1); j+=dy) { 277 //for(j=y1; j<=y2; j++) { 278 zfun = exp((ybuf[j]*slope)+zero); 279 //printf("y zfun thresh : %d %9.4f %9.4f %9.4f\n",j,zfun,NEGLIGIBLE_TRAIL*rms,NEGLIGIBLE_TRAIL*rms*0.5); 280 if(up) { 281 if(zfun < NEGLIGIBLE_TRAIL*rms*0.5) break; 264 282 } else { 265 if(dy*(j-y0) <= 0) zbuf[j] = exp(log(Y_SCALE*1.0)*slope); 266 else zbuf[j] = exp(log(Y_SCALE*dy*(j-y0))*slope); 267 } 268 } 269 270 /* find where the fitted function becomes less than half the rms and cut the trail there*/ 271 for(j=y1; j<=y2; j++) { 272 zfun = exp((ybuf[j]*slope)+zero); 273 if(zfun > NEGLIGIBLE_TRAIL*rms) break; 283 if(zfun < NEGLIGIBLE_TRAIL*rms) break; 284 } 274 285 } 275 286 //For burns, just let it go to the top 276 if(j < y2 && !up) box->eyfit = j; 277 278 /* Determine good x-ranges and Save the fit information */ 279 box->slope = slope; 280 box->nfit = 0; 281 /* Save individual scaled versions for each column */ 282 for(i=xs; i<=xe; i++) { 283 zsum = wsum = 0.0; 284 for(j=y1; j<=y2; j++) { 287 //if(j < y2 && !up) box->eyfit = j; 288 box->eyfit = j; 289 290 291 if(!strcmp(camera,"gpc2")) { 292 293 /*Conpute the mean and standard deviation of the trail along x*/ 294 for(i=xs; i<=xe; i++) { 295 xbuf[i] = 0.0; 296 for(j=y1, k=0; j<=y2; j++) { 297 if(wbuf[j] > 0 && data[i+j*NX] != NODATA && 298 (mask[i+j*NX] == MASK_NONE || mask[i+j*NX] == MASK_SAT_HALO)) { 299 xbuf[i] += data[i+j*NX]; 300 k++; 301 } 302 } 303 xbuf[i] = xbuf[i] / MAX(1,k); 304 } 305 306 xsum = wsum = 0.; 307 for(i=xs; i<=xe; i++) { 308 xsum += i*xbuf[i]; 309 wsum += xbuf[i]; 310 } 311 xmean = xsum / wsum; 312 313 xsum = wsum = 0.; 314 for(i=xs; i<=xe; i++) { 315 xsum += xbuf[i]*pow(i-xmean,2); 316 wsum += xbuf[i]; 317 } 318 xstd = sqrt(xsum / (wsum-1.)); 319 if(VERBOSE & VERB_FIT) { 320 printf("x mean xstd : %9.4f %9.4f %9.4f %9.4f\n", xmean, xstd,xsum,wsum); 321 } 322 323 /* Update fit ranges */ 324 if(xstd > 1 && xstd < 50) { 325 box->sxfit = round(xmean-(xstd*1.1)); 326 box->exfit = round(xmean+(xstd*1.1)); 327 } else { 328 box->sxfit = xs; 329 box->exfit = xs - 1; 330 box->fiterr = FIT_ALL_GONE; 331 } 332 } else { 333 /* Reconstruct the fit for comparison with the data col by col */ 334 for(j=y1; j<=y2; j++) zbuf[j] = exp((ybuf[j]*slope)); 335 /* And tag on the extras to determine the end of the fit */ 336 for(j=yfit; dy*j<dy*(up?y1:y2); j+=dy) { 337 if(fitfunc == BURN_EXP) { 338 zbuf[j] = exp((Y_SCALE*dy*(j-y0)*slope)); 339 } else { 340 if(dy*(j-y0) <= 0) zbuf[j] = exp((log(Y_SCALE*1.0)*slope)); 341 else zbuf[j] = exp((log(Y_SCALE*dy*(j-y0))*slope)); 342 } 343 } 344 345 /* Determine good x-ranges and Save the fit information */ 346 box->slope = slope; 347 box->nfit = 0; 348 /* Save individual scaled versions for each column */ 349 for(i=xs; i<=xe; i++) { 350 zsum = wsum = 0.0; 351 for(j=y1; j<=y2; j++) { 285 352 if(wbuf[j] > 0 && data[i+j*NX] != NODATA && 286 353 (mask[i+j*NX] == MASK_NONE || mask[i+j*NX] == MASK_SAT_HALO)) { 287 // if(wbuf[j] > 0 && data[i+j*NX] != NODATA) {288 // wsum += wbuf[j];289 // zsum += wbuf[j] * (data[i+j*NX] - bckgnd) / zbuf[j];290 354 wsum += zbuf[j]; 291 355 zsum += data[i+j*NX] - bckgnd; 292 356 } 293 }294 if(zsum < 0 || wsum <= 0) {357 } 358 if(zsum < 0 || wsum <= 0) { 295 359 zsum = 0.0; 296 } else {360 } else { 297 361 zsum = zsum / wsum; 298 } 299 /* FIXME: what's a really good criterion for negligible fit? */ 300 /* 100 pixels up fit is zsum or ~zsum/e */ 301 // if(zsum > NEGLIGIBLE_TRAIL*rms) { 302 /* 100113: but also evaluate at the end of the fit for stubby trails */ 303 if(zsum > NEGLIGIBLE_TRAIL*rms || 362 } 363 /* FIXME: what's a really good criterion for negligible fit? */ 364 /* 100 pixels up fit is zsum or ~zsum/e */ 365 /* 100113: but also evaluate at the end of the fit for stubby trails */ 366 //printf("scaled fit comparison x zsum zbuf[y2]*zsum thresh: %d %9.4f %9.4f %9.4f %f %d %f\n", i, zsum, zbuf[y2], zbuf[y2]*zsum, NEGLIGIBLE_TRAIL, rms, NEGLIGIBLE_TRAIL*rms); 367 368 if(zsum > NEGLIGIBLE_TRAIL*rms || 304 369 zbuf[y2]*zsum > NEGLIGIBLE_TRAIL*rms) { 305 370 /* Ascertain the starting point of where the fit is good */ … … 314 379 box->zero[box->nfit] = zsum; 315 380 box->xfit[box->nfit] = i; 381 //printf("y comparison : %d %d %9.4f %9.4f %d %d\n", i, box->yfit[box->nfit], box->zero[box->nfit],zsum, box->xfit[box->nfit],box->nfit); 316 382 box->nfit += 1; 317 } 318 } 319 /* Update fit ranges */ 320 if(box->nfit > 0) { 321 box->sxfit = box->xfit[0]; 322 box->exfit = box->xfit[box->nfit-1]; 323 } else { 324 box->sxfit = xs; 325 box->exfit = xs - 1; 326 box->fiterr = FIT_ALL_GONE; 383 } 384 } 385 /* Update fit ranges */ 386 if(box->nfit > 0) { 387 box->sxfit = box->xfit[0]; 388 box->exfit = box->xfit[box->nfit-1]; 389 } else { 390 box->sxfit = xs; 391 box->exfit = xs - 1; 392 box->fiterr = FIT_ALL_GONE; 393 } 394 } 395 396 397 398 if(VERBOSE & VERB_FIT) { 399 printf("Final fit params xsfit,xefit,y1,y2, yfit,slope,nfit = %d %d %d %d %d %.3f %d\n", 400 box->sxfit, box->exfit, y1, y2,box->eyfit, box->slope, box->nfit); 327 401 } 328 402
Note:
See TracChangeset
for help on using the changeset viewer.
