IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 32695


Ignore:
Timestamp:
Nov 17, 2011, 2:17:35 PM (14 years ago)
Author:
eugene
Message:

fix psphotStack 2nd pass (subtract 1st pass sources from the correct image); fix PS color plotting; plug a leak in psphotStack

Location:
trunk
Files:
43 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/Ohana/src/kapa2/include/prototypes.h

    r30606 r32695  
    126126void          PSPixmap24          PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
    127127void          PSPixmap32          PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
     128void          PSPixmap_3byte      PROTO((Graphic *graphic, KapaImageWidget *image, FILE *f));
    128129
    129130/* kapa bDraw Functions */
  • trunk/Ohana/src/kapa2/include/structures.h

    r29938 r32695  
    2626  XFontStruct   *font;
    2727  Cursor         cursor;
    28   int            x,  y;
    29   unsigned int   dx, dy;
     28  int            x,  y;       // corner coord in X world coords
     29  unsigned int   dx, dy;      // size of window in X coords
     30  int            xwin, ywin;  // corner coord of display subregion (eg, png, ps plot)
     31  int            dxwin, dywin; // corner coord of display subregion (eg, png, ps plot)
    3032  CCNode        *cube;
    3133  XColor        *cmap;
  • trunk/Ohana/src/kapa2/src/PSPixmap.c

    r16256 r32695  
    11# include "Ximage.h"
    2 
    3 // XXX this stuff has been broken by the conversion to the pixmap stuff
    42
    53void PSPixmap8 (Graphic *graphic, KapaImageWidget *image, FILE *f) {
     
    146144}
    147145
    148 # if (0)
    149 // XXX needs work!
    150 void PSPixmap32_RGB (Graphic *graphic, KapaImageWidget *image, FILE *f) {
    151 
    152   int i, k, m, val;
    153   double Nchar, Npix, start, slope, frac;
    154   unsigned int *buff;
    155   unsigned long back;
    156   unsigned char
    157 
    158   ALLOCATE (pixelR, unsigned char, graphic[0].Npixels);
    159   ALLOCATE (pixelG, unsigned char, graphic[0].Npixels);
    160   ALLOCATE (pixelB, unsigned char, graphic[0].Npixels);
     146# define WHITE_R 255
     147# define WHITE_G 255
     148# define WHITE_B 255
     149
     150void PSPixmap_3byte (Graphic *graphic, KapaImageWidget *image, FILE *f) {
     151
     152  int i, j, ii, jj;
     153  int i_start, i_end, j_start, j_end;
     154  int I_start, J_start;
     155  int dropback;  /* this is a bit of a kludge... */
     156  int dx, dy, DX, DY, inDX, inDY, Xs, Ys;
     157  int expand_in, expand_out;
     158  double expand, Ix, Iy;
     159  unsigned short *in_pix, *in_pix_ref;
     160  unsigned char *pixel1, *pixel2, *pixel3;
     161
     162  if (image == NULL) return;
     163
     164  ALLOCATE (pixel1, unsigned char, graphic[0].Npixels);
     165  ALLOCATE (pixel2, unsigned char, graphic[0].Npixels);
     166  ALLOCATE (pixel3, unsigned char, graphic[0].Npixels);
    161167
    162168  /** cmap[i].pixel must be defined even if X is not used **/
    163169  for (i = 0; i < graphic[0].Npixels; i++) { /* set up pixel array */
    164     pixelR[i] = graphic[0].cmap[i].red >> 8;
    165     pixelG[i] = graphic[0].cmap[i].green >> 8;
    166     pixelB[i] = graphic[0].cmap[i].blue >> 8;
    167   }
    168 
    169   for (i = 0; i < image[0].picture.dy; i++) {
    170     for (k = 0; k < image[0].picture.dx; k++, buff++) {
    171       if (*buff == back)
    172         val = Nchar;
    173       else {
    174         for (m = 0; (graphic[0].cmap[m].pixel != *buff) && (m < Npix); m++);
    175         val = Nchar - frac * MIN (MAX (start + m * slope, 0), Npix);
    176       }
    177       fprintf (f, "%02x", val);
    178       if (!((k+1) % 40)) fprintf (f, "\n");
    179     }
    180     fprintf (f, "\n");
    181     buff -= 2*image[0].picture.dx;
    182   }
    183   return;
    184 }
    185 # endif
     170    pixel1[i] = graphic[0].cmap[i].red >> 8;
     171    pixel2[i] = graphic[0].cmap[i].green >> 8;
     172    pixel3[i] = graphic[0].cmap[i].blue >> 8;
     173  }
     174
     175  assert ((image[0].picture.expand >= 1) || (image[0].picture.expand <= -2));
     176  expand = expand_in = expand_out = 1.0;
     177  if (image[0].picture.expand > 0) {
     178    expand = 1 / (1.0*image[0].picture.expand);
     179    expand_out = image[0].picture.expand;
     180    expand_in  = 1;
     181  }
     182  if (image[0].picture.expand < 0) {
     183    expand = fabs((double)image[0].picture.expand);
     184    expand_out = 1;
     185    expand_in  = -image[0].picture.expand;
     186  }
     187
     188  Xs = image[0].picture.x;
     189  Ys = image[0].picture.y;
     190  dx = image[0].picture.dx;
     191  dy = image[0].picture.dy;
     192  DX = image[0].image[0].matrix.Naxis[0];
     193  DY = image[0].image[0].matrix.Naxis[1];
     194
     195  // i_start, j_start are the closest lit screen pixel to 0,0
     196  // I_start, J_start are the image pixel corresponding to i_start, j_start
     197  Picture_Lower (&i_start, &j_start, &I_start, &J_start, &image[0].image[0].matrix, &image[0].picture);
     198
     199  // i_end, j_end are the closest lit screen pixel to dx, dy
     200  // I_end, J_end are the image pixel corresponding to i_end, j_end
     201  Picture_Upper (&i_end, &j_end, i_start, j_start, &image[0].image[0].matrix, &image[0].picture);
     202
     203  assert (i_start <= i_end);
     204  assert (j_start <= j_end);
     205
     206  Ix = image[0].picture.flipx ? I_start - 1 : I_start;
     207  Iy = image[0].picture.flipy ? J_start - 1 : J_start;
     208
     209  inDX = image[0].picture.flipx ? -1 : +1;
     210  inDY = image[0].picture.flipy ? -1 : +1;
     211
     212  dropback = expand_out - (i_end - i_start) % expand_out;
     213  if ((i_end - i_start) % expand_out == 0) dropback = 0;
     214
     215  in_pix_ref  = &image[0].pixmap[DX*(int)MAX(Iy,0) + (int)MAX(Ix,0)];
     216
     217  /********** below we do the mapping from buffer pixels (in) to picture pixels (out) **********/
     218
     219  // add in occasional return chars
     220
     221  /**** fill in bottom area ****/
     222  for (j = 0; j < j_start; j++) {
     223    for (i = 0; i < dx; i++) {
     224      fprintf (f, "%02x%02x%02x", WHITE_R, WHITE_G, WHITE_B);
     225    }
     226  }
     227 
     228  // probably could do this all smarter with scale operations in PS...
     229
     230  /*** fill in the image data region ***/
     231  for (j = j_start; j < j_end; j+= expand_out, in_pix_ref += inDY*expand_in*DX) {
     232   
     233    // repeat the section below 'expand_out' times
     234    for (jj = 0; jj < expand_out; jj++) {
     235
     236      /* create one output image line */
     237      in_pix = in_pix_ref;
     238
     239      /**** fill in area to the left of the picture ****/
     240      for (i = 0; i < i_start; i++) {
     241        fprintf (f, "%02x%02x%02x", WHITE_R, WHITE_G, WHITE_B);
     242      }
     243   
     244      /*** fill in the picture region ***/
     245      for (i = i_start; i < i_end; i+=expand_out, in_pix += inDX*expand_in) {
     246        for (ii = 0; ii < expand_out; ii++) {
     247          fprintf (f, "%02x%02x%02x", pixel1[*in_pix], pixel2[*in_pix], pixel3[*in_pix]);
     248        }
     249      }
     250   
     251      /**** fill in area to the right of the picture ****/
     252      for (i = i_end; i < dx; i++) {
     253        fprintf (f, "%02x%02x%02x", WHITE_R, WHITE_G, WHITE_B);
     254      }
     255    }
     256  }
     257
     258  /**** fill in top area ****/
     259  for (j = j_end; j < dy; j++) {
     260    for (i = 0; i < dx; i++) {
     261      fprintf (f, "%02x%02x%02x", WHITE_R, WHITE_G, WHITE_B);
     262    }
     263  }
     264
     265  free (pixel1);
     266  free (pixel2);
     267  free (pixel3);
     268
     269  return;
     270}
  • trunk/Ohana/src/kapa2/src/PSimage.c

    r29938 r32695  
    1313  graphic = GetGraphic();
    1414
     15  // Update this to generate color PS images: I need to test if the image if color or BW (based
     16  // on the colormap).  If it is a color image, I need to generate a 24 bit image. the header
     17  // should look the same (Nx Ny 8 [1 0 0 1 0 0]) but then it should finish with "false 3
     18  // colorimage" instead of just "image" (see ../doc/color.image.s for an example).  the hex
     19  // string needs to have RGB represented as chars (2 hex chars per R,G,B).
     20  // the values come from the following map: image[0].pixmap[i] -> cmap[pixel].red -> R (0-255)
     21
    1522  fprintf (f, " newpath %d %d moveto %d %d lineto %d %d lineto %d %d lineto closepath\n\n",
    1623           (int) image[0].picture.x,                       graphic->dy - (int) image[0].picture.y,
     
    2128  fprintf (f, "%d %d translate\n", (int) image[0].picture.x, graphic->dy - (int) image[0].picture.y - image[0].picture.dy);
    2229  fprintf (f, "%d %d 8\n", image[0].picture.dx, image[0].picture.dy);
    23   fprintf (f, "[1 0 0 1 0 0]\n");
     30  fprintf (f, "[1 0 0 -1 0 %d]\n", image[0].picture.dy);
     31  // write out the image in normal order, but flip in PS
     32
     33# if (0)
     34
    2435  fprintf (f, "{currentfile %d string readhexstring pop} image\n\n", image[0].picture.dx);
     36  PSPixmap_1byte (graphic, image, f);
    2537
    26   /******** First we draw the picture itself ********/
    27   /* in !USE_XWINDOW, we'll have to change this to use the JPEG function */
    28   switch (graphic[0].Nbits) {
    29   case 8:
    30     PSPixmap8 (graphic, image, f);
    31     break;
    32   case 16:
    33     PSPixmap16 (graphic, image, f);
    34     break;
    35   case 24:
    36     PSPixmap24 (graphic, image, f);
    37     break;
    38   case 32:
    39     PSPixmap32 (graphic, image, f);
    40     break;
    41   }
     38# else
     39
     40  fprintf (f, "{currentfile %d string readhexstring pop} false 3 colorimage\n\n", 3*image[0].picture.dx);
     41  PSPixmap_3byte (graphic, image, f);
     42
     43# endif
    4244
    4345  fprintf (f, "grestore %% end of image\n");
  • trunk/Ohana/src/kapa2/src/Sections.c

    r27790 r32695  
    237237  return (TRUE);
    238238}
     239
     240int SectionMinBoundary (Graphic *graphic) {
     241
     242    int i;
     243    int Xs = graphic->dx;
     244    int Ys = graphic->dy;
     245    int Xe = 0;
     246    int Ye = 0;
     247
     248    // the boundary for a single section should probably be adjusted depending on the
     249    // image status (should not include the imtool portion)
     250    for (i = 0; i < Nsections; i++) {
     251        Xs = MIN (Xs, sections[i][0].x);
     252        Ys = MIN (Ys, sections[i][0].y);
     253        Xe = MAX (Xe, sections[i][0].x + sections[i][0].dx);
     254        Ye = MAX (Ye, sections[i][0].y + sections[i][0].dy);
     255    }
     256
     257    if ((Xs >= Xe) || (Ys >- Ye)) {
     258        // default values for the region window
     259        graphic->xwin  = 0;
     260        graphic->ywin  = 0;
     261        graphic->dxwin = graphic->dx;
     262        graphic->dywin = graphic->dy;
     263    }   
     264   
     265    // set min/max boundary (min window containing max range of sections)
     266    graphic->xwin = Xs;
     267    graphic->ywin = Ys;
     268    graphic->dxwin = Xe - Xs;
     269    graphic->dywin = Ye - Ys;
     270    return TRUE;
     271}
  • trunk/Ohana/src/kapa2/src/SetUpGraphic.c

    r25757 r32695  
    1616  graphic->dx = 512;
    1717  graphic->dy = 512;
     18
     19  // default values for the region window
     20  graphic->xwin  = 0;
     21  graphic->ywin  = 0;
     22  graphic->dxwin = graphic->dx;
     23  graphic->dywin = graphic->dy;
    1824
    1925  if (!USE_XWINDOW) {
  • trunk/Ohana/src/kapa2/src/bDrawImage.c

    r29938 r32695  
    4747  }
    4848
    49   Xs = image[0].picture.x;
    50   Ys = image[0].picture.y;
     49  // Xs,Ys are in full-frame coords.  can we trim here?
     50  Xs = image[0].picture.x - graphic[0].xwin;
     51  Ys = image[0].picture.y - graphic[0].ywin;
    5152  dx = image[0].picture.dx;
    5253  dy = image[0].picture.dy;
     
    5455  DY = image[0].image[0].matrix.Naxis[1];
    5556
     57  // the created buffer is supposed to contain the output windows
    5658  if (buffer[0].Nx < Xs + dx) {
    5759    fprintf (stderr, "invalid condition\n");
  • trunk/Ohana/src/kapa2/src/bDrawIt.c

    r29938 r32695  
    1313  black = KapaColorByName ("black");
    1414
    15   buffer = bDrawBufferCreate (graphic->dx, graphic->dy, Nbyte, palette, Npalette);
     15  // SectionMinBoundary (graphic);
     16
     17  // if we want to trim, we'll need to carry about the start in graphic coords and
     18  // the dx,dy size. 
     19  buffer = bDrawBufferCreate (graphic->dxwin, graphic->dywin, Nbyte, palette, Npalette);
    1620  bDrawSetStyle (buffer, black, 0, 0);
    1721 
  • trunk/Ohana/src/libdvo/src/dbExtractMeasures.c

    r31635 r32695  
    245245      break;
    246246    case MEAS_PAR: /* OK */
    247       value.Flt = average[0].R;
     247      value.Flt = average[0].P;
    248248      break;
    249249    case MEAS_PAR_ERR: /* OK */
    250       value.Flt = average[0].D;
     250      value.Flt = average[0].dP;
    251251      break;
    252252    case MEAS_CHISQ_POS: /* OK */
  • trunk/Ohana/src/libkapa/src/PSRotFont.c

    r30603 r32695  
    3232  dY = currentfont[65].ascent;
    3333 
     34  /***
     35
     36      update this to use the following PS code:
     37
     38      /ceshow { % (string) fontsize fontname x y
     39      gsave
     40      moveto findfont exch scalefont setfont % s
     41      gsave
     42      dup false charpath flattenpath pathbbox % s x0 y0 x1 y1
     43      grestore
     44      3 -1 roll sub % s x0 x1 dy
     45      3 1 roll sub % s dy -dx
     46      2 div exch % s -dx/2 dy
     47      -2 div % s -dx/2 -dy/2
     48      rmoveto show
     49      grestore
     50      } bind def
     51
     52   ***/
     53
    3454  /* apply appropriate offset */
    3555  Xoff = Yoff = 0;
  • trunk/Ohana/src/relastro/include/relastro.h

    r32346 r32695  
    317317int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon);
    318318int ParFactor (double *pR, double *pD, double R, double D, double T, double Tmean);
    319 int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts);
     319int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts, int XVERB);
    320320int FitPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *pR, double *pD, int Npts);
    321 int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts);
     321int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts, int XVERB);
    322322
    323323Mosaic *getMosaicForImage (off_t N);
  • trunk/Ohana/src/relastro/src/FitPM.c

    r32346 r32695  
    22
    33/* do we want an init function which does the alloc and a clear function to free? */
    4 int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts) {
     4int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, int Npts, int XVERB) {
    55
    66  int i;
     
    7777    chisq += SQ(X[i] - Xf) / SQ(dX[i]);
    7878    chisq += SQ(Y[i] - Yf) / SQ(dY[i]);
     79    if (XVERB) fprintf (stderr, "chisq contrib : %f %f : %f %f : %f %f : %f %f : %f\n", Xf, Yf, X[i] - Xf, Y[i] - Yf, dX[i], dY[i], (X[i] - Xf) / dX[i], (Y[i] - Yf) / dY[i], chisq);
    7980  }
    8081  fit[0].Nfit = Npts;
  • trunk/Ohana/src/relastro/src/FitPMandPar.c

    r32346 r32695  
    22
    33/* do we want an init function which does the alloc and a clear function to free? */
    4 int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts) {
     4int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, double *T, double *pR, double *pD, int Npts, int XVERB) {
    55
    66  int i;
     
    9999  chisq = 0.0;
    100100  for (i = 0; i < Npts; i++) {
    101     Xf = fit[0].Ro + fit[0].uR*T[i] + fit[0].dp*pR[i];
    102     Yf = fit[0].Do + fit[0].uD*T[i] + fit[0].dp*pD[i];
     101    Xf = fit[0].Ro + fit[0].uR*T[i] + fit[0].p*pR[i];
     102    Yf = fit[0].Do + fit[0].uD*T[i] + fit[0].p*pD[i];
    103103    chisq += SQ(X[i] - Xf) / SQ(dX[i]);
    104104    chisq += SQ(Y[i] - Yf) / SQ(dY[i]);
     105    if (XVERB) fprintf (stderr, "chisq contrib : %f %f : %f %f : %f %f : %f %f : %f\n", Xf, Yf, X[i] - Xf, Y[i] - Yf, dX[i], dY[i], (X[i] - Xf) / dX[i], (Y[i] - Yf) / dY[i], chisq);
     106
    105107  }
    106108  fit[0].Nfit = Npts;
  • trunk/Ohana/src/relastro/src/ImageOps.c

    r32346 r32695  
    913913  if (!finite(measure[0].dR) || !finite(measure[0].dD)) return FALSE;
    914914  if (!finite(measure[0].M)) return FALSE; //XXX is this necessary for all relastro tasks?
     915  if (!finite(measure[0].dM)) return FALSE; //XXX is this necessary for all relastro tasks?
    915916 
    916917  /* select measurements by photcode, or equiv photcode, if specified */
  • trunk/Ohana/src/relastro/src/UpdateObjects.c

    r32346 r32695  
    8484      /* calculate the average value of R,D for a single star */
    8585
     86      XVERB = FALSE;
     87
    8688      // skip objects which are known to be problematic
    8789      // XXX include this code or not?
     
    196198      }
    197199     
    198       XVERB = (catalog[i].measure[m].dM < 0.01) && (N == 6) && (mode == FIT_PM_ONLY);
     200      // XVERB |= (catalog[i].averge[j].objID == 0xc90) && (catalog[i].average[j].catID == 0x2a1e);
     201      XVERB |= (catalog[i].average[j].objID == OBJ_ID_SRC) && (catalog[i].average[j].catID == CAT_ID_SRC);
     202      XVERB |= (catalog[i].average[j].objID == OBJ_ID_DST) && (catalog[i].average[j].catID == CAT_ID_DST);
     203      // XVERB = (catalog[i].measure[m].dM < 0.01) && (N == 6) && (mode == FIT_PM_ONLY);
    199204
    200205      // to judge the quality of the PM and PAR fits, we need to fit all three models and compare Chisq
     
    211216        }         
    212217
    213         FitPM (&fitPM, X, dX, Y, dY, T, N);
    214 
    215         if (XVERB) fprintf (stderr, "fitted:  %f - %f : %f %f : %f %f : %f vs %f\n", Tmin, Tmax, fitPM.Ro, fitPM.Do, fitPM.uR, fitPM.uD, fitPM.chisq, fitAve.chisq);
     218        FitPM (&fitPM, X, dX, Y, dY, T, N, XVERB);
     219
     220        if (XVERB) fprintf (stderr, "fitted PM:  %f - %f : %f %f : %f %f : %f vs %f\n", Tmin, Tmax, fitPM.Ro, fitPM.Do, fitPM.uR, fitPM.uD, fitPM.chisq, fitAve.chisq);
    216221
    217222        // project Ro, Do back to RA,DEC
     
    230235          ParFactor (&pX[k], &pY[k], R[k], D[k], T[k], Tmean);
    231236        }
    232         FitPMandPar (&fitPAR, X, dX, Y, dY, T, pX, pY, N);
     237        FitPMandPar (&fitPAR, X, dX, Y, dY, T, pX, pY, N, XVERB);
    233238        XY_to_RD (&fitPAR.Ro, &fitPAR.Do, fitPAR.Ro, fitPAR.Do, &coords);
    234239        catalog[i].average[j].flags |= ID_STAR_FIT_PAR;
     
    275280        break;
    276281      }
    277       if (XVERB) fprintf (stderr, "A%f %f -> %f %f (%f,%f) pm=(%f %f)\n",
     282      if (XVERB) fprintf (stderr, "%f %f -> %f %f (%f,%f) pm=(%f %f)\n",
    278283                          catalog[i].average[j].R,
    279284                          catalog[i].average[j].D,
  • trunk/Ohana/src/relastro/src/args.c

    r32346 r32695  
    1414  FIT_MODE = FIT_AVERAGE;
    1515
     16  OBJ_ID_SRC = OBJ_ID_DST = 0;
     17  CAT_ID_SRC = CAT_ID_DST = 0;
     18
    1619  if ((N = get_argument (argc, argv, "-merge-source"))) {
    1720    if (N > argc - 6) usage_merge_source();
     
    4043    remove_argument (N, &argc, argv);
    4144    FIT_TARGET = TARGET_OBJECTS;
     45  }
     46
     47  if ((N = get_argument (argc, argv, "-testobj1"))) {
     48    if (N > argc - 3) usage ();
     49    remove_argument (N, &argc, argv);
     50    OBJ_ID_SRC = strtol(argv[N], &endptr, 0);
     51    if (*endptr) usage ();
     52    remove_argument (N, &argc, argv);
     53    CAT_ID_SRC = strtol(argv[N], &endptr, 0);
     54    if (*endptr) usage ();
     55    remove_argument (N, &argc, argv);
     56  }
     57
     58  if ((N = get_argument (argc, argv, "-testobj2"))) {
     59    if (N > argc - 3) usage ();
     60    remove_argument (N, &argc, argv);
     61    OBJ_ID_DST = strtol(argv[N], &endptr, 0);
     62    if (*endptr) usage ();
     63    remove_argument (N, &argc, argv);
     64    CAT_ID_DST = strtol(argv[N], &endptr, 0);
     65    if (*endptr) usage ();
     66    remove_argument (N, &argc, argv);
    4267  }
    4368
  • trunk/ippMonitor

  • trunk/ippScripts/scripts/ipp_apply_burntool_single.pl

  • trunk/ippTools/share/camtool_find_pendingimfile.sql

  • trunk/ippTools/share/pxadmin_create_tables.sql

  • trunk/ippTools/src

  • trunk/ippTools/src/magictool.c

  • trunk/ippconfig

  • trunk/ippconfig/recipes/reductionClasses.mdc

  • trunk/ppImage/src

  • trunk/psModules/src/imcombine/pmSubtraction.c

    r30738 r32695  
    14871487            }
    14881488        }
     1489        psFree(out1->covariance);
    14891490        out1->covariance = psImageCovarianceAverage(covars);
    14901491        psFree(covars);
     
    15271528            }
    15281529        }
     1530        psFree(out2->covariance);
    15291531        out2->covariance = psImageCovarianceAverage(covars);
    15301532        psFree(covars);
  • trunk/psModules/src/objects/pmSource.c

    r32633 r32695  
    121121    source->modelFits = NULL;
    122122    source->extFitPars = NULL;
     123
    123124    source->type = PM_SOURCE_TYPE_UNKNOWN;
    124125    source->mode = PM_SOURCE_MODE_DEFAULT;
     
    138139    source->apFluxErr        = NAN;
    139140
     141    source->windowRadius     = NAN;
    140142    source->skyRadius        = NAN;
    141143    source->skyFlux          = NAN;
     
    203205
    204206    source->imageID          = in->imageID;
     207
    205208    source->type             = in->type;
    206209    source->mode             = in->mode;
    207210    source->mode2            = in->mode2;
    208211    source->tmpFlags         = in->tmpFlags;
     212
    209213    source->psfMag           = in->psfMag;
    210214    source->psfMagErr        = in->psfMagErr;
     
    217221    source->apFlux           = in->apFlux;
    218222    source->apFluxErr        = in->apFluxErr;
     223
     224    source->windowRadius     = in->windowRadius;
     225    source->skyRadius        = in->skyRadius;   
     226    source->skyFlux          = in->skyFlux;     
     227    source->skySlope         = in->skySlope;   
     228
    219229    source->pixWeightNotBad  = in->pixWeightNotBad;
    220230    source->pixWeightNotPoor = in->pixWeightNotPoor;
     231
    221232    source->psfChisq         = in->psfChisq;
    222233    source->crNsigma         = in->crNsigma;
     
    259270    }
    260271    mySource->region   = srcRegion;
     272    mySource->windowRadius = Radius;
    261273
    262274    return true;
     
    328340        mySource->psfImage = NULL;
    329341    }
     342    mySource->windowRadius = Radius;
    330343    return extend;
    331344}
  • trunk/psModules/src/objects/pmSource.h

    r32633 r32695  
    9797    float apFluxErr;                    ///< apFluxErr corresponding to psfMag or extMag (depending on type)
    9898
     99    float windowRadius;                 ///< size of box used for full analysis
    99100    float skyRadius;                    ///< radius at which profile hits local sky (or goes flat)
    100101    float skyFlux;                      ///< mean flux per pixel in aperture at which profile hits local sky (or goes flat)
  • trunk/psphot

  • trunk/psphot/src/psphot.h

    r32633 r32695  
    108108bool            psphotBlendFit_Threaded (psThreadJob *job);
    109109
    110 bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule);
    111 bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     110bool            psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState);
     111bool            psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool ignoreState);
    112112
    113113bool            psphotAddNoise (pmConfig *config, const pmFPAview *view, const char *filerule);
     
    176176
    177177// in psphotReplaceUnfit.c:
    178 bool            psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe);
     178bool            psphotRemoveAllSourcesByArray (const psArray *sources, const psMetadata *recipe);
     179bool            psphotRemoveAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState);
     180bool            psphotRemoveAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState);
    179181bool            psphotReplaceUnfitSources (psArray *sources, psImageMaskType maskVal);
    180182
     
    453455psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc);
    454456
     457bool psphotSourceParents (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
     458bool psphotSourceParentsReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
     459
     460bool psphotCopyPeaks (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
     461bool psphotCopyPeaksReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
     462
    455463bool psphotSersicModelClassGuessPCM (pmPCMdata *pcm, pmSource *source);
    456464void psphotSersicModelClassInit ();
  • trunk/psphot/src/psphotBlendFit.c

    r32348 r32695  
    236236        pmSource *source = sources->data[i];
    237237
    238 # define TEST_X -420.0
    239 # define TEST_Y 300.0
     238# if (0)
     239# define TEST_X 34
     240# define TEST_Y 28
    240241   
    241242        if ((fabs(source->peak->xf - TEST_X) < 5) && (fabs(source->peak->yf - TEST_Y) < 5)) {
    242             fprintf (stderr, "test galaxy\n");
     243            fprintf (stderr, "test object\n");
    243244        }
    244245
    245246# undef TEST_X
    246247# undef TEST_Y
     248# endif
    247249
    248250        // skip non-astronomical objects (very likely defects)
  • trunk/psphot/src/psphotEfficiency.c

    r32348 r32695  
    255255
    256256    // remove all sources, adding noise for subtracted sources
    257     psphotRemoveAllSources(realSources, recipe);
     257    psphotRemoveAllSourcesByArray(realSources, recipe);
    258258
    259259#if TESTING
  • trunk/psphot/src/psphotFake.c

    r26894 r32695  
    166166
    167167    // remove all sources, adding noise for subtracted sources
    168     psphotRemoveAllSources(realSources, recipe);
     168    psphotRemoveAllSourcesByArray(realSources, recipe);
    169169    psphotAddNoise(readout, realSources, recipe);
    170170
  • trunk/psphot/src/psphotFitSourcesLinear.c

    r32633 r32695  
    342342    psLogMsg ("psphot.ensemble", PS_LOG_MINUTIA, "solve matrix: %f sec (%d elements)\n", psTimerMark ("psphot.linear"), sparse->Nelem);
    343343
    344     // XXXX **** philosophical question:
    345     // we measure bright objects in three passes: 1) linear fit; 2) non-linear fit; 3) linear fit:
    346     // should retain the chisq and errors from the intermediate non-linear fit?
    347     // the non-linear fit provides better values for the position errors, and for
    348     // extended sources, the shape errors
     344    // Philosophical question: we measure bright objects in three passes: 1) linear fit; 2)
     345    // non-linear fit; 3) linear fit: should we retain the chisq and errors from the
     346    // intermediate non-linear fit?  the non-linear fit provides better values for the position
     347    // errors, and for extended sources, the shape errors
    349348
    350349    // adjust I0 for fitSources and subtract
  • trunk/psphot/src/psphotMergeSources.c

    r31452 r32695  
    520520}
    521521
     522// copy the newPeaks from the detections of one pmFPAfile to another
     523bool psphotCopyPeaks (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     524{
     525    bool status = true;
     526
     527    int num = psphotFileruleCount(config, ruleSrc);
     528
     529    // skip the chisq image because it is a duplicate of the detection version
     530    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     531    if (!status) chisqNum = -1;
     532
     533    // loop over the available readouts
     534    for (int i = 0; i < num; i++) {
     535        if (i == chisqNum) continue; // skip chisq image
     536        if (!psphotCopyPeaksReadout (config, view, ruleOut, ruleSrc, i)) {
     537            psError (PSPHOT_ERR_CONFIG, false, "failed to copy peaks from %s to %s entry %d", ruleSrc, ruleOut, i);
     538            return false;
     539        }
     540    }
     541    return true;
     542}
     543
     544// add newly detected peaks to the existing list of sources
     545bool psphotCopyPeaksReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
     546
     547    bool status;
     548
     549    // find the currently selected readout
     550    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
     551    psAssert (fileSrc, "missing file?");
     552
     553    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
     554    psAssert (readoutSrc, "missing readout?");
     555
     556    pmDetections *detections = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
     557    psAssert (detections, "missing detections?");
     558
     559    // find the currently selected readout
     560    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
     561    psAssert (fileOut, "missing file?");
     562
     563    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
     564    psAssert (readoutOut, "missing readout?");
     565
     566    // generate a new detection structure for the output filerule
     567    pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
     568    psAssert (detectionsOut, "missing PSPHOT.DETECTIONS?");
     569
     570    psAssert (detectionsOut->peaks, "programming error");
     571    psAssert (!detectionsOut->oldPeaks, "programming error");
     572    psAssert (detections->peaks, "programming error");
     573
     574    // save the OUT existing peaks on oldPeaks
     575    detectionsOut->oldPeaks = detectionsOut->peaks;
     576    detectionsOut->peaks = psArrayAllocEmpty(detections->peaks->n);
     577
     578    for (int i = 0; i < detections->peaks->n; i++) {
     579        psAssert (detections->peaks->data[i], "programming error");
     580        pmPeak *peak = pmPeakAlloc (0, 0, 0.0, PM_PEAK_LONE);
     581        pmPeakCopy(peak, detections->peaks->data[i]);
     582        psArrayAdd (detectionsOut->peaks, 100, peak);
     583        psFree (peak);
     584    }
     585    return true;
     586}
     587
     588// create source parents children from ruleSrc for ruleOut for orphans
     589bool psphotSourceParents (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     590{
     591    bool status = true;
     592
     593    int num = psphotFileruleCount(config, ruleSrc);
     594
     595    // skip the chisq image because it is a duplicate of the detection version
     596    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     597    if (!status) chisqNum = -1;
     598
     599    // loop over the available readouts
     600    for (int i = 0; i < num; i++) {
     601        if (i == chisqNum) continue; // skip chisq image
     602        if (!psphotSourceParentsReadout (config, view, ruleOut, ruleSrc, i)) {
     603            psError (PSPHOT_ERR_CONFIG, false, "failed to copy sources from %s to %s entry %d", ruleSrc, ruleOut, i);
     604            return false;
     605        }
     606    }
     607    return true;
     608}
     609
     610// create source parents from ruleSrc for ruleOut for orphaned children for this readout. 
     611bool psphotSourceParentsReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
     612
     613    bool status;
     614    int nParents = 0;
     615    int nNonOrphans = 0;
     616
     617    // find the currently selected readout
     618    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
     619    psAssert (fileSrc, "missing file?");
     620
     621    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
     622    psAssert (readoutSrc, "missing readout?");
     623
     624    pmDetections *detectionsSrc = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
     625    psAssert (detectionsSrc, "missing detections?");
     626
     627    psArray *sourcesSrc = detectionsSrc->allSources;
     628    psAssert (sourcesSrc, "missing sources?");
     629
     630    // find the currently selected readout
     631    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
     632    psAssert (fileOut, "missing file?");
     633
     634    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
     635    psAssert (readoutOut, "missing readout?");
     636
     637    // generate a new detection structure for the output filerule
     638    pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
     639    psAssert (detectionsOut, "missing PSPHOT.DETECTIONS?");
     640
     641    // loop over the sources, redefine their pixels to point at the new filerule image,
     642    // copy the source data, and add a reference back to the original source
     643   
     644    // copy the sources from sourceSrcs to the new detection structure
     645    for (int i = 0; i < sourcesSrc->n; i++) {
     646      pmSource *sourceSrc = sourcesSrc->data[i];
     647      if (sourceSrc->parent) {
     648          nNonOrphans ++;
     649          continue; // Not an orphan
     650      }
     651
     652      pmSource *sourceOut = pmSourceCopy(sourceSrc);
     653      sourceOut->parent = sourceSrc;
     654     
     655      // keep the original source flags
     656      sourceOut->seq      = sourceSrc->seq;
     657      sourceOut->type     = sourceSrc->type;
     658      sourceOut->mode     = sourceSrc->mode;
     659      sourceOut->mode2    = sourceSrc->mode2;
     660      sourceOut->tmpFlags = sourceSrc->tmpFlags;
     661
     662      // does this copy all model data? (NO)
     663      sourceOut->modelPSF = pmModelCopy(sourceSrc->modelPSF);
     664      sourceOut->modelEXT = pmModelCopy(sourceSrc->modelEXT);
     665
     666      if (sourceSrc->modelFits) {
     667          sourceOut->modelFits = psArrayAlloc(sourceSrc->modelFits->n);
     668          for (int j = 0; j < sourceSrc->modelFits->n; j++) {
     669              sourceOut->modelFits->data[j] = pmModelCopy(sourceSrc->modelFits->data[j]);
     670          }
     671      }
     672
     673      // drop the references to the original image pixels:
     674      pmSourceFreePixels (sourceOut);
     675
     676      // allocate image, weight, mask for the new image for each peak
     677      if (sourceOut->modelPSF) {
     678        pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
     679      }
     680
     681      // child sources have not been subtracted in this image, but this flag may be raised if
     682      // they were subtracted in the parent's image
     683      sourceOut->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     684
     685      nParents ++;
     686      psArrayAdd (detectionsOut->allSources, 100, sourceOut);
     687      psFree (sourceOut);
     688    }
     689    psLogMsg ("psphot", 3, "%d parents created, %d unorphaned children, %ld input vs %ld output", nParents, nNonOrphans, sourcesSrc->n, detectionsOut->allSources->n);
     690
     691    return true;
     692}
     693
    522694// create source children from ruleSrc for ruleOut
    523695bool psphotSourceChildren (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc)
     
    542714}
    543715
    544 // create source children from ruleSrc for ruleOut for this entry.  XXX currently, this is only
     716// Create source children from ruleSrc for ruleOut for this entry.  Currently, this is only
    545717// used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be called
    546 // repeatedly)
     718// repeatedly).
    547719bool psphotSourceChildrenReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
    548720
     
    569741    psAssert (readoutOut, "missing readout?");
    570742
    571     // generate a new detection structure for the output filerule
    572     pmDetections *detectionsOut = psMetadataLookupPtr (&status, readoutOut->analysis, "PSPHOT.DETECTIONS");
    573     if (!detectionsOut) {
    574         detectionsOut = pmDetectionsAlloc();
    575         detectionsOut->allSources = psArrayAllocEmpty (100);
    576         // save detections on the readout->analysis
    577         if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detectionsOut)) {
    578             psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
    579             return false;
    580         }
    581         psFree(detectionsOut); // a copy remains on the analysis metadata
     743    // replace any existing DETECTION container on readoutOut->analysis with the new one
     744    pmDetections *detectionsOut = pmDetectionsAlloc();
     745    detectionsOut->allSources = psArrayAllocEmpty (100);
     746    if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detectionsOut)) {
     747        psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     748        return false;
    582749    }
    583750
     
    593760     
    594761      // keep the original source flags
     762      sourceOut->seq      = sourceSrc->seq;
    595763      sourceOut->type     = sourceSrc->type;
    596764      sourceOut->mode     = sourceSrc->mode;
     
    612780      pmSourceFreePixels (sourceOut);
    613781
     782      // XXX do we need to skip the Chisq image sources?
     783
    614784      // allocate image, weight, mask for the new image for each peak
    615       if (sourceOut->modelPSF) {
    616         pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
    617       }
     785      pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->windowRadius);
    618786
    619787      // child sources have not been subtracted in this image, but this flag may be raised if
     
    624792      psFree (sourceOut);
    625793    }
    626     psLogMsg ("psphot", 3, "%ld known sources supplied", detectionsOut->allSources->n);
    627 
    628 
    629     return true;
    630 }
    631 
    632 // create source children associated with 'filerule' from the objectsSrc.  XXX currently, this
    633 // is only used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be
    634 // called repeatedly)
     794    psLogMsg ("psphot", 3, "created %ld children", detectionsOut->allSources->n);
     795
     796    psFree(detectionsOut); // a copy remains on the analysis metadata
     797
     798    return true;
     799}
     800
     801// create source children associated with 'filerule' from the objectsSrc.  returns a new object
     802// array containing the child sources.  XXX currently, this is only used by psphotStackReadout
     803// (sources go on allSources so that psphotChoosePSF can be called repeatedly)
    635804psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc) {
    636805
     
    652821        psAssert (readout, "missing readout?");
    653822
     823        // create DETECTIONS containers for each image, in case one lacks it
    654824        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    655825        if (!detections) {
     
    665835            psAssert (detections, "missing detections?");
    666836        }
     837
     838        // we need to save the new sources on the detection arrays of the appropriate image
    667839        detArrays->data[i] = psMemIncrRefCounter(detections);
    668840        readouts->data[i] = psMemIncrRefCounter(readout);
     
    695867
    696868            pmSource *sourceOut = pmSourceCopy(sourceSrc);
     869            sourceOut->parent = sourceSrc;
     870
     871            // save on the output object array at the same location
    697872            objectOut->sources->data[i] = sourceOut;
    698 
    699             sourceOut->parent = sourceSrc;
    700873
    701874            // keep the original source flags and sequence ID (if set)
     
    720893            pmSourceFreePixels (sourceOut);
    721894
    722             // set the output readotu
     895            // set the output readout
    723896            int index = sourceOut->imageID;
    724897            if (index >= readouts->n) continue; // skip the sources generated by the chisq image
  • trunk/psphot/src/psphotOutput.c

    r32348 r32695  
    407407    return true;
    408408}
     409
     410// for now, let's store the detections on the readout->analysis for each readout
     411bool psphotDumpTest (pmConfig *config, const pmFPAview *view, const char *filerule)
     412{
     413    static int npass = 0;
     414    char filename[64];
     415
     416    // XXX dump tests are disabled unless this is commented out:
     417    return true;
     418
     419    bool status = true;
     420
     421    int num = psphotFileruleCount(config, filerule);
     422
     423    snprintf (filename, 64, "testdump.%02d.dat", npass);
     424    FILE *f = fopen (filename, "w");
     425
     426    // loop over the available readouts
     427    for (int i = 0; i < num; i++) {
     428
     429        // find the currently selected readout
     430        pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
     431        psAssert (file, "missing file?");
     432
     433        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     434        psAssert (readout, "missing readout?");
     435
     436        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     437        psAssert (detections, "missing detections?");
     438
     439        psArray *sources = detections->newSources ? detections->newSources : detections->allSources;
     440        psAssert (sources, "missing sources?");
     441
     442        if (detections->newSources) {
     443            fprintf (f, "## --- from new sources ---\n");
     444        } else {
     445            fprintf (f, "## --- from all sources ---\n");
     446        }
     447
     448        for (int i = 0; i < sources->n; i++) {
     449            pmSource *source = sources->data[i];
     450            if (!source) continue;
     451
     452            pmPeak *peak = source->peak;
     453            if (!peak) continue;
     454
     455            // XXX only dump a given region
     456            // if (peak->xf < 20) continue;
     457            // if (peak->yf < 20) continue;
     458            // if (peak->xf > 40) continue;
     459            // if (peak->yf > 70) continue;
     460
     461            float Msum = source->moments ? source->moments->Sum : NAN;
     462            float Mx   = source->moments ? source->moments->Mx : NAN;
     463            float My   = source->moments ? source->moments->My : NAN;
     464            // float Npix = source->moments ? source->moments->nPixels : NAN;
     465            float Io = source->modelPSF ? source->modelPSF->params->data.F32[PM_PAR_I0] : NAN;
     466            fprintf (f, "%d %f %f  : %f %f : %f %f\n", source->imageID, peak->xf, peak->yf, Mx, My, Msum, Io);
     467        }
     468    }
     469    fclose (f);
     470    npass ++;
     471
     472    return true;
     473}
     474
     475# if (0)
    409476bool psphotDumpTest (pmConfig *config, const pmFPAview *view, const char *filerule, char *filename) {
    410477
     
    449516    return true;
    450517}
    451 
     518# endif
  • trunk/psphot/src/psphotReadout.c

    r32633 r32695  
    99}
    1010
    11 // for now, let's store the detections on the readout->analysis for each readout
    12 bool psphotDumpChisqs (pmConfig *config, const pmFPAview *view, const char *filerule)
    13 {
    14     static int npass = 0;
    15     char filename[64];
    16 
    17     return true;
    18 
    19     bool status = true;
    20 
    21     int num = psphotFileruleCount(config, filerule);
    22 
    23     snprintf (filename, 64, "chisq.%02d.dat", npass);
    24     FILE *f = fopen (filename, "w");
    25 
    26     // loop over the available readouts
    27     for (int i = 0; i < num; i++) {
    28 
    29         // find the currently selected readout
    30         pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
    31         psAssert (file, "missing file?");
    32 
    33         pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    34         psAssert (readout, "missing readout?");
    35 
    36         pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    37         psAssert (detections, "missing detections?");
    38 
    39         psArray *sources = detections->allSources;
    40         psAssert (sources, "missing sources?");
    41 
    42         for (int i = 0; i < sources->n; i++) {
    43             pmSource *source = sources->data[i];
    44             if (!source) continue;
    45 
    46             pmModel *model = pmSourceGetModel (NULL, source);
    47             if (!model) continue;
    48        
    49             if (source->mode & PM_SOURCE_MODE_NONLINEAR_FIT) {
    50                 fprintf (f, "%f %f %f %d %d %f  1 NONLINEAR\n", model->mag, model->params->data.F32[1], model->chisq, model->nDOF, model->nPix, model->chisqNorm);
    51             } else {
    52                 fprintf (f, "%f %f %f %d %d %f  0 LINEAR\n", model->mag, model->params->data.F32[1], model->chisq, model->nDOF, model->nPix, model->chisqNorm);
    53             }
    54         }
    55     }
    56     fclose (f);
    57     npass ++;
    58 
    59     return true;
    60 }
    61 
    6211bool psphotReadout(pmConfig *config, const pmFPAview *view, const char *filerule) {
    6312
     
    188137    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    189138    psphotFitSourcesLinear (config, view, filerule, false); // pass 1 (detections->allSources)
    190     psphotDumpChisqs (config, view, filerule);
    191139
    192140    // measure the radial profiles to the sky
     
    208156    // replace model flux, adjust mask as needed, fit, subtract the models (full stamp)
    209157    psphotBlendFit (config, view, filerule); // pass 1 (detections->allSources)
    210     psphotDumpChisqs (config, view, filerule);
    211158
    212159    // replace all sources
    213     psphotReplaceAllSources (config, view, filerule); // pass 1 (detections->allSources)
     160    psphotReplaceAllSources (config, view, filerule, false); // pass 1 (detections->allSources)
    214161
    215162    // linear fit to include all sources (subtract again)
    216163    // NOTE : apply to ALL sources (extended + psf)
    217164    psphotFitSourcesLinear (config, view, filerule, true); // pass 2 (detections->allSources)
    218     psphotDumpChisqs (config, view, filerule);
    219165
    220166    // if we only do one pass, skip to extended source analysis
     
    253199        // replace all sources so fit below applies to all at once
    254200        // NOTE: apply only to OLD sources (which have been subtracted)
    255         psphotReplaceAllSources (config, view, filerule); // pass 2
     201        psphotReplaceAllSources (config, view, filerule, false); // pass 2
    256202
    257203        // merge the newly selected sources into the existing list
     
    262208        // NOTE: apply to ALL sources
    263209        psphotFitSourcesLinear (config, view, filerule, true); // pass 3 (detections->allSources)
    264         psphotDumpChisqs (config, view, filerule);
    265210    }
    266211
     
    295240        // replace all sources so fit below applies to all at once
    296241        // NOTE: apply only to OLD sources (which have been subtracted)
    297         psphotReplaceAllSources (config, view, filerule); // pass 2
     242        psphotReplaceAllSources (config, view, filerule, false); // pass 2
    298243
    299244        // merge the newly selected sources into the existing list
  • trunk/psphot/src/psphotReplaceUnfit.c

    r31452 r32695  
    99
    1010    for (int i = 0; i < sources->n; i++) {
    11       source = sources->data[i];
    12 
    13       // replace other sources?
    14       if (source->mode & PM_SOURCE_MODE_FAIL) goto replace;
    15       continue;
     11        source = sources->data[i];
     12
     13        // replace other sources?
     14        if (source->mode & PM_SOURCE_MODE_FAIL) goto replace;
     15        continue;
    1616
    1717    replace:
     
    2323
    2424// for now, let's store the detections on the readout->analysis for each readout
    25 bool psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule)
     25bool psphotReplaceAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState)
    2626{
    2727    bool status = true;
     
    3535    // loop over the available readouts
    3636    for (int i = 0; i < num; i++) {
    37         if (!psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe)) {
     37        if (!psphotReplaceAllSourcesReadout (config, view, filerule, i, recipe, ignoreState)) {
    3838            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
    3939            return false;
     
    4343}
    4444
    45 bool psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe) {
    46 
    47     bool status;
    48     pmSource *source;
     45bool psphotReplaceAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState) {
     46
     47    bool status;
    4948
    5049    psTimerStart ("psphot.replace");
     
    7574
    7675    for (int i = 0; i < sources->n; i++) {
    77       source = sources->data[i];
    78 
    79       // replace other sources?
    80       if (!(source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
    81 
    82       pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     76        pmSource *source = sources->data[i];
     77
     78        if (ignoreState) {
     79            // rely on the type of source to decide if we subtract it or not
     80
     81            // skip non-astronomical objects (very likely defects)
     82            if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     83            if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     84     
     85            // do not include CRs in the full ensemble fit
     86            if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
     87       
     88            // do not include MOMENTS_FAILURES in the fit
     89            if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
     90        } else {
     91            // if we respect the state, do not replace unsubtracted sources
     92            if (!(source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
     93        }
     94
     95        pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    8396    }
    8497
     
    88101}
    89102
    90 bool psphotRemoveAllSources (const psArray *sources, const psMetadata *recipe) {
     103// for now, let's store the detections on the readout->analysis for each readout
     104bool psphotRemoveAllSources (pmConfig *config, const pmFPAview *view, const char *filerule, bool ignoreState)
     105{
     106    bool status = true;
     107
     108    // select the appropriate recipe information
     109    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     110    psAssert (recipe, "missing recipe?");
     111
     112    int num = psphotFileruleCount(config, filerule);
     113
     114    // loop over the available readouts
     115    for (int i = 0; i < num; i++) {
     116        if (!psphotRemoveAllSourcesReadout (config, view, filerule, i, recipe, ignoreState)) {
     117            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
     118            return false;
     119        }
     120    }
     121    return true;
     122}
     123
     124bool psphotRemoveAllSourcesReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index, psMetadata *recipe, bool ignoreState) {
     125
     126    bool status;
     127
     128    psTimerStart ("psphot.replace");
     129
     130    // find the currently selected readout
     131    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     132    psAssert (file, "missing file?");
     133
     134    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     135    psAssert (readout, "missing readout?");
     136
     137    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     138    psAssert (detections, "missing detections?");
     139
     140    psArray *sources = detections->allSources;
     141    psAssert (sources, "missing sources?");
     142
     143    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     144    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     145    psAssert (maskVal, "missing mask value?");
     146
     147    // bit-mask to mark pixels not used in analysis
     148    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT");
     149    assert (markVal);
     150
     151    // maskVal is used to test for rejected pixels, and must include markVal
     152    maskVal |= markVal;
     153
     154    for (int i = 0; i < sources->n; i++) {
     155        pmSource *source = sources->data[i];
     156
     157        if (ignoreState) {
     158            // rely on the type of source to decide if we subtract it or not
     159
     160            // skip non-astronomical objects (very likely defects)
     161            if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     162            if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     163     
     164            // do not include CRs in the full ensemble fit
     165            if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue;
     166       
     167            // do not include MOMENTS_FAILURES in the fit
     168            if (source->mode & PM_SOURCE_MODE_MOMENTS_FAILURE) continue;
     169        } else {
     170            // if we respect the state, only remove unsubtracted sources
     171            if ((source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
     172        }
     173
     174        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     175    }
     176
     177    psphotVisualShowImage(readout);
     178    psLogMsg ("psphot.replace", PS_LOG_INFO, "replaced models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
     179    return true;
     180}
     181
     182bool psphotRemoveAllSourcesByArray (const psArray *sources, const psMetadata *recipe) {
    91183
    92184    bool status;
     
    100192
    101193    for (int i = 0; i < sources->n; i++) {
    102       source = sources->data[i];
    103 
    104       // replace other sources?
    105       if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) continue;
    106 
    107       pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     194        source = sources->data[i];
     195
     196        // replace other sources?
     197        if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) continue;
     198
     199        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    108200    }
    109201    psLogMsg ("psphot.replace", PS_LOG_INFO, "replaced models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
     
    158250
    159251    for (int i = 0; i < sources->n; i++) {
    160       source = sources->data[i];
    161 
    162       // sources have not yet been subtracted in this image (but this flag may be raised)
    163       source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
    164       if (!source->modelPSF) continue;
    165 
    166       float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
    167       float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
    168       float radius = source->modelPSF->fitRadius;
    169 
    170       // force a redefine to this image
    171       pmSourceFreePixels(source);
    172       pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
     252        source = sources->data[i];
     253
     254        // sources have not yet been subtracted in this image (but this flag may be raised)
     255        source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     256        if (!source->modelPSF) continue;
     257
     258        float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     259        float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     260        float radius = source->modelPSF->fitRadius;
     261
     262        // force a redefine to this image
     263        pmSourceFreePixels(source);
     264        pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
    173265    }
    174266    return true;
     
    229321
    230322    for (int i = 0; i < sources->n; i++) {
    231       source = sources->data[i];
    232 
    233       // *** we need to cache the 'best' model, and we have 3 cases:
    234       // 1) model is the psf model --> generate from the new psf
    235       // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
    236       // 3) model is a convolved extended model --> re-generate
    237 
    238       // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
    239       bool isPSF = false;
    240       pmModel *model = pmSourceGetModel(&isPSF, source);
    241       if (!model) continue;
    242 
    243       float radius = model->fitRadius; // save for future use below
    244 
    245       // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
    246       if (isPSF || model->isPCM) {
    247           // the guess central intensity comes from the peak:
    248           float Io = source->peak->rawFlux;
    249           float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
    250           float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
    251 
    252           // generate a model for this object with Io = 1.0
    253           pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
    254           if (modelPSF == NULL) {
    255               psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
    256               continue;
    257           }
    258 
    259           // set the source PSF model
    260           psFree (source->modelPSF);
    261           source->modelPSF = modelPSF;
    262           source->modelPSF->fitRadius = radius;
    263       }
    264 
    265       if (model->isPCM) {
    266           psAssert(false, "this section is not complete");
    267 
    268           pmSourceCachePSF (source, maskVal);
    269 
    270           psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
    271           if (!psfKernel) {
    272               psWarning ("no psf kernel");
    273           }
    274 
    275           // generate an image of the right size
    276           psImage *rawModelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    277           psImageInit (rawModelFlux, 0.0);
     323        source = sources->data[i];
     324
     325        // *** we need to cache the 'best' model, and we have 3 cases:
     326        // 1) model is the psf model --> generate from the new psf
     327        // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
     328        // 3) model is a convolved extended model --> re-generate
     329
     330        // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
     331        bool isPSF = false;
     332        pmModel *model = pmSourceGetModel(&isPSF, source);
     333        if (!model) continue;
     334
     335        float radius = model->fitRadius; // save for future use below
     336
     337        // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
     338        if (isPSF || model->isPCM) {
     339            // the guess central intensity comes from the peak:
     340            float Io = source->peak->rawFlux;
     341            float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     342            float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     343
     344            // generate a model for this object with Io = 1.0
     345            pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
     346            if (modelPSF == NULL) {
     347                psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
     348                continue;
     349            }
     350
     351            // set the source PSF model
     352            psFree (source->modelPSF);
     353            source->modelPSF = modelPSF;
     354            source->modelPSF->fitRadius = radius;
     355        }
     356
     357        if (model->isPCM) {
     358            psAssert(false, "this section is not complete");
     359
     360            pmSourceCachePSF (source, maskVal);
     361
     362            psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
     363            if (!psfKernel) {
     364                psWarning ("no psf kernel");
     365            }
     366
     367            // generate an image of the right size
     368            psImage *rawModelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     369            psImageInit (rawModelFlux, 0.0);
    278370         
    279           // insert the model image normalized to 1.0
    280           pmModelAdd (rawModelFlux, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
    281 
    282           psImageConvolveFFT (source->modelFlux, rawModelFlux, NULL, 0, psfKernel);
     371            // insert the model image normalized to 1.0
     372            pmModelAdd (rawModelFlux, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
     373
     374            psImageConvolveFFT (source->modelFlux, rawModelFlux, NULL, 0, psfKernel);
    283375         
    284           psFree (psfKernel);
    285           psFree (rawModelFlux);
    286       }
    287 
    288       pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
     376            psFree (psfKernel);
     377            psFree (rawModelFlux);
     378        }
     379
     380        pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
    289381    }
    290382
  • trunk/psphot/src/psphotSourceFits.c

    r32633 r32695  
    257257        okDBL  = psphotEvalDBL (tmpSrc, DBL->data[0]);
    258258        okDBL &= psphotEvalDBL (tmpSrc, DBL->data[1]);
     259        okDBL = false; // XXX this is failing badly...
    259260        // XXX should I keep / save the flags set in the eval functions?
    260261
  • trunk/psphot/src/psphotStackChisqImage.c

    r31154 r32695  
    1313    psAssert (chisqFile, "missing chisq image FPA?");
    1414
     15    // the readout containing the chisq image is generated in the first pass of this loop and
     16    // used by the successive passes
    1517    pmReadout *chiReadout = NULL;
    1618
  • trunk/psphot/src/psphotStackReadout.c

    r32633 r32695  
    8282    }
    8383
    84     // XXX I think this is not defined correctly for an array of images.
    85     // XXX I probably need to subtract the model (same model?) for both RAW and OUT.
    86     // XXX But, probably not a problem in practice since the stacks are constructed with 0.0 mean level.
    87 
    8884    // generate a background model (median, smoothed image)
    8985    if (!psphotModelBackground (config, view, STACK_DET)) {
     
    9187    }
    9288    if (!psphotSubtractBackground (config, view, STACK_DET)) {
     89        return psphotReadoutCleanup (config, view, STACK_SRC);
     90    }
     91    if (!psphotSubtractBackground (config, view, STACK_SRC)) {
    9392        return psphotReadoutCleanup (config, view, STACK_SRC);
    9493    }
     
    114113    }
    115114
    116     // if DET and SRC are different images, copy the detections from DET to SRC
     115    // If DET and SRC are different images, copy the detections from DET to SRC.  This 'copy'
     116    // is just a copy of the container pointer; the sources on both DET and SRC are the same
     117    // memory objects
    117118    if (strcmp(STACK_SRC, STACK_DET)) {
    118119        if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     
    130131        return psphotReadoutCleanup (config, view, STACK_SRC);
    131132    }
     133    // psphotDumpTest (config, view, STACK_SRC);
    132134    psMemDump("sourcestats");
    133135
     
    166168
    167169    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    168     // XXX why do this as a stack operation?
    169     // psphotFitSourcesLinearStack (config, objects, false);
    170170    psphotFitSourcesLinear (config, view, STACK_SRC, false);
    171171    psphotStackVisualFilerule(config, view, STACK_SRC);
     
    186186    psphotBlendFit (config, view, STACK_SRC); // pass 1 (detections->allSources)
    187187
    188     // replace all sources
    189     psphotReplaceAllSources (config, view, STACK_SRC); // pass 1 (detections->allSources)
     188    // replace all sources (do NOT ignore subtraction state)
     189    psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 1 (detections->allSources)
    190190
    191191    // if we only do one pass, skip to extended source analysis
     
    194194    // linear fit to include all sources (subtract again)
    195195    // NOTE : apply to ALL sources (extended + psf)
     196    // NOTE 2 : this function subtracts the models from the given filerule (SRC), not DET
    196197    psphotFitSourcesLinear (config, view, STACK_SRC, true); // pass 2 (detections->allSources)
    197198
     
    200201    // NOTE: this block performs the 2nd pass low-significance PSF detection stage
    201202    {
     203        // if DET and SRC are different images, generate children sources for all sources in
     204        // the SRC image.  This operation replaces the existing DETECTION container on DET
     205        // which is currently a view to the one on SRC).  children sources go to
     206        // det->allSources
     207        if (strcmp(STACK_SRC, STACK_DET)) {
     208            psphotSourceChildren (config, view, STACK_DET, STACK_SRC);
     209
     210            //  subtract all sources from DET (this will subtract using the psf model for SRC, which
     211            //  will somewhat oversubtract the sources -- this is OK
     212            psphotRemoveAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     213        }
     214
    202215        // add noise for subtracted objects
    203216        psphotAddNoise (config, view, STACK_DET); // pass 1 (detections->allSources)
     
    212225
    213226        // if DET and SRC are different images, copy the detections from DET to SRC
     227        // (this operation just ensures the metadata container has a view on SRC as well
    214228        if (strcmp(STACK_SRC, STACK_DET)) {
    215             // XXX how does this handle 1st vs 2nd pass sources?
    216             if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     229            // replace all sources in DET
     230            psphotReplaceAllSources (config, view, STACK_DET, false); // ignore subtraction state for sources
     231
     232            // copy the newly detected peaks from DET to SRC so SourceStats below can operate on them
     233            if (!psphotCopyPeaks (config, view, STACK_SRC, STACK_DET)) {
    217234                psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    218235                return psphotReadoutCleanup (config, view, STACK_SRC);
     
    237254        // replace all sources so fit below applies to all at once
    238255        // NOTE: apply only to OLD sources (which have been subtracted)
    239         psphotReplaceAllSources (config, view, STACK_SRC); // pass 2
     256        psphotReplaceAllSources (config, view, STACK_SRC, false); // pass 2
    240257
    241258        // merge the newly selected sources into the existing list
     
    312329
    313330        // replace the flux in the image so it is returned to its original state
    314         psphotReplaceAllSources (config, view, STACK_OUT);
     331        psphotReplaceAllSources (config, view, STACK_OUT, false);
    315332
    316333        // smooth to the next FWHM, or set 'smoothAgain' to false if no more
  • trunk/psvideophot

Note: See TracChangeset for help on using the changeset viewer.