IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 15, 2024, 10:57:32 AM (21 months ago)
Author:
tdeboer
Message:

updated working for gpc2 burntool, inclduing adding camera keyword

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/extsrc/gpcsw/gpcsrc/fits/burntool/trailfit.c

    r42392 r42724  
    1414#include "burnparams.h"
    1515
    16 STATIC double ybuf[MAXSIZE], zbuf[MAXSIZE], wbuf[MAXSIZE], wbuf0[MAXSIZE];
     16STATIC double ybuf[MAXSIZE], zbuf[MAXSIZE], wbuf[MAXSIZE], wbuf0[MAXSIZE],xbuf[MAXSIZE];
    1717
    1818/****************************************************************/
     
    9292STATIC int fit_trail(int nx, int ny, int NX,
    9393                    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)
    9595{
    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;
    9999   char fooname[60];
    100100   FILE *fp=stdout;
     
    104104      return(-1);
    105105   }
    106 
     106   
    107107   /*xs = box->sxfit; */
    108108   /*xe = box->exfit; */
     
    110110   xs = box->sx;
    111111   xe = box->ex;
     112
    112113   
    113114   if(up) {
     
    119120      yfit = box->sy;      /* Start of validity, TBD later by col */
    120121      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*/
    121123   } else {
    122124      //y0 = (box->y0m + box->y1m + box->y0p + box->y1p + 2) / 4;
     
    127129      yfit = box->ey;      /* Start of validity, TBD later by col */
    128130      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;
    130146   }
    131147
    132148   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);
    135151   }
    136152
     
    184200      ybuf[j] = Y_SCALE * dy * (j-y0);
    185201      zbuf[j]=0.0;
    186       for(i=xs, k=0; i<=xe; i++) {
     202      for(i=x0, k=0; i<=x1; i++) {
    187203             if(data[i+j*NX] != NODATA && (mask[i+j*NX] == MASK_NONE || mask[i+j*NX] == MASK_SAT_HALO) ) {
    188204              zbuf[j] += data[i+j*NX];
     
    221237/* Trim away any really big positive deviations */
    222238   for(j=y1; j<=y2; j++) {
    223 //      if(zbuf[j]-(ybuf[j]*slope+zero) > MAXDEV &&
    224 //       zbuf[j] > log(MAXRMS*rms)) wbuf[j] = 0;
    225239      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]);
    228244      }
    229245   }
     
    237253
    238254   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);
    240256      fclose(fp);
    241257   }
     
    249265      linearrms(nfit, ybuf+y1, zbuf+y1, slope, zero, &trial);
    250266/* 100203 JT: bad idea: appears to be a bug in read/writing ABS(nfit) */
     267
    251268//      if(trial > 2*rms) box->nfit = -box->nfit;
    252269      if(trial > 2*rms) box->func = BURN_POSSLOPE;
     
    256273   }
    257274
    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;
    264282      } 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      }
    274285   }   
    275286   //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++) {
    285352             if(wbuf[j] > 0 && data[i+j*NX] != NODATA &&
    286353             (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];
    290354               wsum += zbuf[j];
    291355               zsum += data[i+j*NX] - bckgnd;
    292356             }
    293       }
    294       if(zsum < 0 || wsum <= 0) {
     357       }
     358       if(zsum < 0 || wsum <= 0) {
    295359             zsum = 0.0;
    296       } else {
     360       } else {
    297361             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 ||
    304369           zbuf[y2]*zsum > NEGLIGIBLE_TRAIL*rms) {
    305370        /* Ascertain the starting point of where the fit is good */
     
    314379             box->zero[box->nfit] = zsum;
    315380             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); 
    316382             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);
    327401   }
    328402
Note: See TracChangeset for help on using the changeset viewer.