IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 30631


Ignore:
Timestamp:
Feb 14, 2011, 1:05:28 PM (15 years ago)
Author:
watersc1
Message:

Merge of trunk back into branch.

Location:
branches/czw_branch/20101203
Files:
7 deleted
296 edited
23 copied

Legend:

Unmodified
Added
Removed
  • branches/czw_branch/20101203

  • branches/czw_branch/20101203/Ohana/src/addstar/include/addstar.h

    r30118 r30631  
    265265
    266266int        InitStar               PROTO((Stars *star));
    267 short      ShortPixels            PROTO((float pixels));
    268 short      ShortDegree            PROTO((float degrees));
    269267
    270268int args_skycells (int argc, char **argv);
  • branches/czw_branch/20101203/Ohana/src/addstar/src/ReadStarsFITS.c

    r30118 r30631  
    9898    stars[i].measure.photFlags = (smpdata[i].dophot << 16);
    9999
    100     stars[i].measure.FWx       = ShortPixels (smpdata[i].fx);
    101     stars[i].measure.FWy       = ShortPixels (smpdata[i].fy);
    102     stars[i].measure.theta     = ShortDegree (smpdata[i].df);
     100    stars[i].measure.FWx       = ToShortPixels (smpdata[i].fx);
     101    stars[i].measure.FWy       = ToShortPixels (smpdata[i].fy);
     102    stars[i].measure.theta     = ToShortDegrees (smpdata[i].df);
    103103  }   
    104104  *nstars = Nstars;
     
    127127    stars[i].measure.Yccd     = ps1data[i].Y;
    128128
    129     stars[i].measure.dXccd    = ShortPixels(ps1data[i].dX);
    130     stars[i].measure.dYccd    = ShortPixels(ps1data[i].dY);
     129    stars[i].measure.dXccd    = ToShortPixels(ps1data[i].dX);
     130    stars[i].measure.dYccd    = ToShortPixels(ps1data[i].dY);
    131131   
    132132    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
     
    140140    stars[i].measure.dSky     = ps1data[i].dSky;
    141141
    142     stars[i].measure.FWx      = ShortPixels(ps1data[i].fx);
    143     stars[i].measure.FWy      = ShortPixels(ps1data[i].fy);
    144     stars[i].measure.theta    = ShortDegree(ps1data[i].df);
     142    stars[i].measure.FWx      = ToShortPixels(ps1data[i].fx);
     143    stars[i].measure.FWy      = ToShortPixels(ps1data[i].fy);
     144    stars[i].measure.theta    = ToShortDegrees(ps1data[i].df);
    145145
    146146    stars[i].measure.psfChisq = ps1data[i].psfChisq;
     
    176176    stars[i].measure.Yccd       = ps1data[i].Y;
    177177
    178     stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
    179     stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
     178    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
     179    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
    180180
    181181    if ((ps1data[i].M >= 0.0) || isnan(ps1data[i].M)) {
     
    189189    stars[i].measure.dSky       = ps1data[i].dSky;
    190190
    191     stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
    192     stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
    193     stars[i].measure.theta      = ShortDegree(ps1data[i].df);
     191    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
     192    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
     193    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
    194194
    195195    stars[i].measure.psfChisq   = ps1data[i].psfChisq;
     
    238238    stars[i].measure.Xccd       = ps1data[i].X;
    239239    stars[i].measure.Yccd       = ps1data[i].Y;
    240     stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
    241     stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
    242 
    243     stars[i].measure.posangle   = ShortDegree(ps1data[i].posangle);
     240    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
     241    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
     242
     243    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    244244    stars[i].measure.pltscale   = ps1data[i].pltscale;
    245245
     
    263263    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
    264264
    265     stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
    266     stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
    267     stars[i].measure.theta      = ShortDegree(ps1data[i].df);
    268 
    269     stars[i].measure.Mxx        = ShortPixels(ps1data[i].Mxx);
    270     stars[i].measure.Mxy        = ShortPixels(ps1data[i].Mxy);
    271     stars[i].measure.Myy        = ShortPixels(ps1data[i].Myy);
     265    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
     266    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
     267    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
     268
     269    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
     270    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
     271    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
    272272                       
    273273    stars[i].measure.photFlags  = ps1data[i].flags;
     
    314314    stars[i].measure.Xccd       = ps1data[i].X;
    315315    stars[i].measure.Yccd       = ps1data[i].Y;
    316     stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
    317     stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
    318 
    319     stars[i].measure.posangle   = ShortDegree(ps1data[i].posangle);
     316    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
     317    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
     318
     319    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    320320    stars[i].measure.pltscale   = ps1data[i].pltscale;
    321321
     
    339339    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
    340340
    341     stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
    342     stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
    343     stars[i].measure.theta      = ShortDegree(ps1data[i].df);
    344 
    345     stars[i].measure.Mxx        = ShortPixels(ps1data[i].Mxx);
    346     stars[i].measure.Mxy        = ShortPixels(ps1data[i].Mxy);
    347     stars[i].measure.Myy        = ShortPixels(ps1data[i].Myy);
     341    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
     342    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
     343    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
     344
     345    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
     346    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
     347    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
    348348                       
    349349    stars[i].measure.photFlags  = ps1data[i].flags;
     
    382382    stars[i].measure.Xccd       = ps1data[i].X;
    383383    stars[i].measure.Yccd       = ps1data[i].Y;
    384     stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
    385     stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
    386 
    387     stars[i].measure.posangle   = ShortDegree(ps1data[i].posangle);
     384    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
     385    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
     386
     387    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    388388    stars[i].measure.pltscale   = ps1data[i].pltscale;
    389389
     
    407407    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
    408408
    409     stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
    410     stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
    411     stars[i].measure.theta      = ShortDegree(ps1data[i].df);
    412 
    413     stars[i].measure.Mxx        = ShortPixels(ps1data[i].Mxx);
    414     stars[i].measure.Mxy        = ShortPixels(ps1data[i].Mxy);
    415     stars[i].measure.Myy        = ShortPixels(ps1data[i].Myy);
     409    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
     410    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
     411    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
     412
     413    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
     414    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
     415    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
    416416                       
    417417    stars[i].measure.photFlags  = ps1data[i].flags;
     
    450450    stars[i].measure.Xccd       = ps1data[i].X;
    451451    stars[i].measure.Yccd       = ps1data[i].Y;
    452     stars[i].measure.dXccd      = ShortPixels(ps1data[i].dX);
    453     stars[i].measure.dYccd      = ShortPixels(ps1data[i].dY);
    454 
    455     stars[i].measure.posangle   = ShortDegree(ps1data[i].posangle);
     452    stars[i].measure.dXccd      = ToShortPixels(ps1data[i].dX);
     453    stars[i].measure.dYccd      = ToShortPixels(ps1data[i].dY);
     454
     455    stars[i].measure.posangle   = ToShortDegrees(ps1data[i].posangle);
    456456    stars[i].measure.pltscale   = ps1data[i].pltscale;
    457457
     
    475475    stars[i].measure.extNsigma  = ps1data[i].extNsigma;
    476476
    477     stars[i].measure.FWx        = ShortPixels(ps1data[i].fx);
    478     stars[i].measure.FWy        = ShortPixels(ps1data[i].fy);
    479     stars[i].measure.theta      = ShortDegree(ps1data[i].df);
    480 
    481     stars[i].measure.Mxx        = ShortPixels(ps1data[i].Mxx);
    482     stars[i].measure.Mxy        = ShortPixels(ps1data[i].Mxy);
    483     stars[i].measure.Myy        = ShortPixels(ps1data[i].Myy);
     477    stars[i].measure.FWx        = ToShortPixels(ps1data[i].fx);
     478    stars[i].measure.FWy        = ToShortPixels(ps1data[i].fy);
     479    stars[i].measure.theta      = ToShortDegrees(ps1data[i].df);
     480
     481    stars[i].measure.Mxx        = ToShortPixels(ps1data[i].Mxx);
     482    stars[i].measure.Mxy        = ToShortPixels(ps1data[i].Mxy);
     483    stars[i].measure.Myy        = ToShortPixels(ps1data[i].Myy);
    484484                       
    485485    stars[i].measure.photFlags  = ps1data[i].flags;
  • branches/czw_branch/20101203/Ohana/src/addstar/src/ReadStarsSDSS.c

    r30118 r30631  
    159159      stars[N].measure.Xccd      = colc[N];
    160160      stars[N].measure.Yccd      = rowc[N];
    161       stars[N].measure.dXccd     = ShortPixels(colcErr[N]);
    162       stars[N].measure.dYccd     = ShortPixels(rowcErr[N]);
     161      stars[N].measure.dXccd     = ToShortPixels(colcErr[N]);
     162      stars[N].measure.dYccd     = ToShortPixels(rowcErr[N]);
    163163      stars[N].measure.M         = psfCounts[N] + ZeroPt - zeropt[j];
    164164      stars[N].measure.dM        = psfCountsErr[N];
     
    166166      stars[N].measure.Sky       = sky[N]; // adjust this to counts?
    167167      stars[N].measure.dSky      = skyErr[N];
    168       stars[N].measure.FWx       = ShortPixels(seeing[j]); // reported in arcsec?
    169       stars[N].measure.FWy       = ShortPixels(seeing[j]);
     168      stars[N].measure.FWx       = ToShortPixels(seeing[j]); // reported in arcsec?
     169      stars[N].measure.FWy       = ToShortPixels(seeing[j]);
    170170      if (prob_psf) {
    171171          stars[N].measure.psfChisq  = prob_psf[N]; // XXX not really the correct value...
  • branches/czw_branch/20101203/Ohana/src/addstar/src/ReadStarsTEXT.c

    r21508 r30631  
    9595      dparse (&df,  11, &buffer[j*BYTES_STAR]);
    9696
    97       stars[N].measure.FWx   = ShortPixels (fx);
    98       stars[N].measure.FWy   = ShortPixels (fy);
    99       stars[N].measure.theta = ShortDegree (df);
     97      stars[N].measure.FWx   = ToShortPixels (fx);
     98      stars[N].measure.FWy   = ToShortPixels (fy);
     99      stars[N].measure.theta = ToShortDegrees (df);
    100100    }
    101101  }
  • branches/czw_branch/20101203/Ohana/src/addstar/src/StarOps.c

    r21508 r30631  
    99    return TRUE;
    1010}
    11 
    12 short ShortPixels (float pixels) {
    13 
    14     short value;
    15 
    16     value = 100*pixels;
    17 
    18     return value;
    19 }
    20 
    21 short ShortDegree (float degrees) {
    22 
    23     short value;
    24 
    25     value = (0xffff/360.0)*degrees;
    26 
    27     return value;
    28 }
  • branches/czw_branch/20101203/Ohana/src/addstar/src/calibrate.c

    r27435 r30631  
    225225    image[0].Mcal = M1;
    226226    image[0].dMcal = M2 / sqrt (N);
    227     image[0].Mxxxx = N;
     227    image[0].nFitPhotom = N;
    228228  } else {
    229229    fprintf (stderr, "too few stars\n");
    230230    image[0].Mcal = 10.000;
    231231    image[0].dMcal = 10.000;
    232     image[0].Mxxxx = 0;
    233   }
    234 }
     232    image[0].nFitPhotom = 0;
     233  }
     234}
  • branches/czw_branch/20101203/Ohana/src/addstar/src/fakeimage.c

    r27435 r30631  
    134134
    135135    image[i+1].nstar = 0;
    136     image[i+1].Myyyy = 0;
    137136
    138137    /* check if chip hits outer bounds of mosaic */
     
    191190  image[0].flags = 0;
    192191  image[0].nstar = 0;
    193   image[0].Myyyy = 0;
    194192
    195193  // XXX need to set the imageID here
  • branches/czw_branch/20101203/Ohana/src/addstar/src/get2mass_full.c

    r23226 r30631  
    1717  ptr = next2MASSfield (ptr); // dec (assumed to be already set)
    1818  ptr = next2MASSfield (ptr); // err_maj
    19   star[0][0].measure.FWx = ShortPixels(strtod (ptr, NULL));
     19  star[0][0].measure.FWx = ToShortPixels(strtod (ptr, NULL));
    2020  ptr = next2MASSfield (ptr); // err_min
    21   star[0][0].measure.FWy = ShortPixels(strtod (ptr, NULL));
     21  star[0][0].measure.FWy = ToShortPixels(strtod (ptr, NULL));
    2222  ptr = next2MASSfield (ptr); // err_ang
    23   star[0][0].measure.theta = ShortPixels(strtod (ptr, NULL));
     23  star[0][0].measure.theta = ToShortPixels(strtod (ptr, NULL));
    2424
    2525  star[2][0].measure.FWx   = star[2][0].measure.FWx   = star[0][0].measure.FWx;
  • branches/czw_branch/20101203/Ohana/src/addstar/src/resort_catalog.c

    r30586 r30631  
    3939    tmp_ave  = AVE[A];  AVE[A]  = AVE[B];  AVE[B]  = tmp_ave;           \
    4040  }
    41 # define COMPARE(A,B)(MEAS[A] < MEAS[B])
     41# define COMPARE(A,B)(AVE[A] < AVE[B])
    4242  OHANA_SORT (N, COMPARE, SWAPFUNC);
    4343# undef SWAPFUNC
     
    9696  }
    9797 
     98  // check that averageSeq is now in order
     99  // for (i = 1; i < Nmeasure; i++) {
     100  //   if (averageSeq[i] < averageSeq[i-1]) {
     101  //     fprintf (stderr, "%d ", (int) i);
     102  //   }
     103  // }
     104  // fprintf (stderr, "\n");
     105
    98106  SortAveMeasMatch(measureSeq, averageSeq, Nmeasure);
    99107  // MARKTIME("sort : %f sec\n", dtime);
     108
     109  // check that averageSeq is now in order
     110  // for (i = 1; i < Nmeasure; i++) {
     111  //   if (averageSeq[i] < averageSeq[i-1]) {
     112  //     fprintf (stderr, "%d ", (int) i);
     113  //   }
     114  // }
     115  // fprintf (stderr, "\n");
    100116
    101117  // copy the measurements in the sorted order
     
    116132  for (i = 0; i < Nmeasure; i++) {
    117133    if (averageSeq[i] != currentAve) {
     134      // we have hit the next entry in the list
    118135      average[currentAve].Nmeasure = N;
    119136      N = 0;
     
    127144  // MARKTIME("update Nmeasure : %f sec\n", dtime);
    128145
     146  int NmeasureTotal = 0;
     147  int measureOffsetOK = TRUE;
     148  for (i = 0; i < catalog[0].Naverage; i++) {
     149    NmeasureTotal += catalog[0].average[i].Nmeasure;
     150    if (VERBOSE && !(NmeasureTotal <= catalog[0].Nmeasure)) {
     151      fprintf (stderr, "too few measurements: %d %d %d\n", (int) i, NmeasureTotal, (int) catalog[0].Nmeasure);
     152    }
     153    measureOffsetOK &= (catalog[0].average[i].measureOffset < catalog[0].Nmeasure);
     154    if (VERBOSE && !(catalog[0].average[i].measureOffset < catalog[0].Nmeasure)) {
     155      fprintf (stderr, "offset too large: %d %d %d\n", (int) i, catalog[0].average[i].Nmeasure, (int) catalog[0].Nmeasure);
     156    }
     157    measureOffsetOK &= (catalog[0].average[i].measureOffset + catalog[0].average[i].Nmeasure <= catalog[0].Nmeasure);
     158    if (VERBOSE && !(catalog[0].average[i].measureOffset + catalog[0].average[i].Nmeasure <= catalog[0].Nmeasure)) {
     159      fprintf (stderr, "orrset + Nmeasure too large: %d + %d > %d %d\n", (int) i, catalog[0].average[i].measureOffset, catalog[0].average[i].Nmeasure, (int) catalog[0].Nmeasure);
     160    }
     161  }
     162
     163  if (!measureOffsetOK) {
     164    fprintf (stderr, "ERROR: catalog %s has an invalid measureOffset\n", catalog[0].filename);
     165  }
     166
     167  if (NmeasureTotal != catalog[0].Nmeasure) {
     168    fprintf (stderr, "ERROR: catalog %s has an invalid Nmeasure\n", catalog[0].filename);
     169  }
     170
    129171  // MARKTIME("  match time %9.4f sec for %7lld measures, %6lld average\n", dtime, (long long) Nmeasure, (long long) Naverage);
     172
     173  catalog[0].sorted = TRUE;
    130174
    131175  FREE (measureSeq);
  • branches/czw_branch/20101203/Ohana/src/addstar/src/sky_tessalation.c

    r28673 r30631  
    338338  image[0].photcode = 1; // this needs to be set more sensibly
    339339
    340   image[0].Mx   = xv[0];  image[0].My   = yv[0];
    341   image[0].Mxxx = xv[1];  image[0].Mxyy = yv[1];
    342   image[0].Mxxy = xv[2];  image[0].Myyy = yv[2];
     340  // XXX these overload these value in a silly way
     341  image[0].dXpixSys = xv[0];       image[0].dYpixSys   = yv[0];
     342  image[0].dMagSys  = xv[1];       image[0].nFitAstrom = yv[1];
     343  image[0].photom_map_id = xv[2];  image[0].astrom_map_id = yv[2];
    343344
    344345  return (TRUE);
  • branches/czw_branch/20101203/Ohana/src/dvomerge/src/dvoverify.c

    r30118 r30631  
    99
    1010int VerifyTableFile (char *filename);
     11int CheckCatalogIndexes (char *catdir, char *filename,  SkyRegion *region);
    1112
    1213# define DEBUG 0
     14
     15int VERBOSE;
    1316
    1417int main (int argc, char **argv) {
     
    2225  SkyRegion UserPatch;
    2326  // Catalog catalog;
     27
     28  VERBOSE = FALSE;
     29  if ((N = get_argument (argc, argv, "-v"))) {
     30    VERBOSE = TRUE;
     31    remove_argument (N, &argc, argv);
     32  }
    2433
    2534  // restrict to a portion of the sky
     
    8392    if (i % 1000 == 0) fprintf (stderr, ".");
    8493
    85     sprintf (filename, "%s/%s.cpt", catdir, inlist[0].regions[i][0].name);
    86     if (!VerifyTableFile (filename)) {
     94    // sprintf (filename, "%s/%s.cpt", catdir, inlist[0].regions[i][0].name);
     95    if (!VerifyTableFile (inlist[0].filename[i])) {
    8796      Nbad ++;
    8897    }
     
    95104    sprintf (filename, "%s/%s.cpm", catdir, inlist[0].regions[i][0].name);
    96105    if (!VerifyTableFile (filename)) {
     106      Nbad ++;
     107    }
     108
     109    if (!CheckCatalogIndexes(catdir, inlist[0].filename[i], inlist[0].regions[i])){
    97110      Nbad ++;
    98111    }
     
    212225}
    213226
    214   // gfits_scan(&cpmHeaderTBL, "NAXIS1", "%d", 1, &NbytesPerRow);
    215   // gfits_scan(&cpmHeaderTBL, "NAXIS2", "%d", 1, &Nrows);
     227// CheckCatalogIndexes(catdir, inlist[0].regions[i][0].name);
     228
     229int CheckCatalogIndexes (char *catdir, char *filename,  SkyRegion *region) {
     230
     231  Catalog catalog;
     232  int i, j, m, status;
     233
     234  status = TRUE;
     235
     236  // set the parameters which guide catalog open/load/create
     237  catalog.filename  = filename;
     238  catalog.catformat = dvo_catalog_catformat (CATFORMAT);  // set the default catformat from config data
     239  catalog.catmode   = dvo_catalog_catmode (CATMODE);      // set the default catmode from config data
     240  catalog.catflags  = LOAD_AVES | LOAD_MEAS;
     241  catalog.Nsecfilt  = 0;
     242 
     243  // an error exit status here is a significant error (disk I/O or file access)
     244  if (!dvo_catalog_open (&catalog, region, VERBOSE, "r")) {
     245    fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
     246    return FALSE;
     247  }
     248
     249  // Naves_disk == 0 implies an empty catalog file, skip empty catalogs
     250  if (catalog.Naves_disk == 0) {
     251    dvo_catalog_unlock (&catalog);
     252    dvo_catalog_free (&catalog);
     253    return TRUE;
     254  }
     255
     256  // if the table is SORTED, then the following can be checked
     257  // check the following:
     258  // measure[j].averef -> average[averef]
     259  // measure[j].objID = average[averef].objID
     260  // measure[j].catID = average[averef].catID
     261  // measure[j].measureOffset < Nmeasure
     262  // \sum average[].Nmeasure = Nmeasure
     263
     264  // if the table is NOT SORTED, we have a subset of checks we can make
     265  if (!catalog.sorted) {
     266    fprintf (stderr, "!");
     267    dvo_catalog_unlock (&catalog);
     268    dvo_catalog_free (&catalog);
     269    return TRUE;
     270  }
     271
     272  int NmeasureTotal = 0;
     273  int measureOffsetOK = TRUE;
     274  for (i = 0; i < catalog.Naverage; i++) {
     275    NmeasureTotal += catalog.average[i].Nmeasure;
     276    if (VERBOSE && !(NmeasureTotal <= catalog.Nmeasure)) {
     277      fprintf (stderr, "NmeasureTotal > catalog.Nmeasure: %d %d %d\n", i, catalog.average[i].Nmeasure, (int) catalog.Nmeasure);
     278    }
     279    measureOffsetOK &= (catalog.average[i].measureOffset < catalog.Nmeasure);
     280    if (VERBOSE && !(catalog.average[i].measureOffset < catalog.Nmeasure)) {
     281      fprintf (stderr, "measureOffset >= catalog.Nmeasure: %d %d %d\n", i, catalog.average[i].measureOffset, (int) catalog.Nmeasure);
     282    }
     283    measureOffsetOK &= (catalog.average[i].measureOffset + catalog.average[i].Nmeasure <= catalog.Nmeasure);
     284    if (VERBOSE && !(catalog.average[i].measureOffset + catalog.average[i].Nmeasure <= catalog.Nmeasure)) {
     285      fprintf (stderr, "measureOffset + Nmeasure > catalog.Nmeasure : %d %d %d\n", i, catalog.average[i].Nmeasure, (int) catalog.Nmeasure);
     286    }
     287  }
     288
     289  if (!measureOffsetOK) {
     290    fprintf (stderr, "ERROR: catalog %s has an invalid measureOffset\n", catalog.filename);
     291    status = FALSE;
     292  }
     293
     294  if (NmeasureTotal != catalog.Nmeasure) {
     295    fprintf (stderr, "ERROR: catalog %s has an invalid Nmeasure\n", catalog.filename);
     296    status = FALSE;
     297  }
     298
     299  if (!status) {
     300    dvo_catalog_unlock (&catalog);
     301    dvo_catalog_free (&catalog);
     302    return (status);
     303  }
     304
     305  int objIDsOK = TRUE;
     306  int catIDsOK = TRUE;
     307  int averefOK = TRUE;
     308
     309  for (i = 0; i < catalog.Naverage; i++) {
     310    m = catalog.average[i].measureOffset;
     311    for (j = 0; j < catalog.average[i].Nmeasure; j++) {
     312      objIDsOK &= (catalog.average[i].objID == catalog.measure[m+j].objID);
     313      catIDsOK &= (catalog.average[i].catID == catalog.measure[m+j].catID);
     314      averefOK &= (catalog.measure[m+j].averef == i);
     315    }
     316  }
     317   
     318  if (!objIDsOK) {
     319    fprintf (stderr, "ERROR: catalog %s has invalid obj IDs\n", catalog.filename);
     320    status = FALSE;
     321  }
     322  if (!catIDsOK) {
     323    fprintf (stderr, "ERROR: catalog %s has invalid cat IDs\n", catalog.filename);
     324    status = FALSE;
     325  }
     326  if (!averefOK) {
     327    fprintf (stderr, "ERROR: catalog %s has invalid averef values\n", catalog.filename);
     328    status = FALSE;
     329  }
     330
     331//  for (i = 0; i < catalog.Naverage; i++) {
     332//    m = catalog.average[i].measureOffset;
     333//    for (j = 0; i < catalog.Nmeasure; i++) {
     334//      objIDsOK &= (catalog.average[i].objID == catalog.measure[m+j].objID);
     335//      catIDsOK &= (catalog.average[i].catID == catalog.measure[m+j].catID);
     336//      averefOK &= (catalog.measure[m+j].averef = i);
     337//    }
     338//  }
     339
     340  dvo_catalog_unlock (&catalog);
     341  dvo_catalog_free (&catalog);
     342
     343  return status;
     344}
     345
     346// gfits_scan(&cpmHeaderTBL, "NAXIS1", "%d", 1, &NbytesPerRow);
     347// gfits_scan(&cpmHeaderTBL, "NAXIS2", "%d", 1, &Nrows);
    216348   
    217349 
  • branches/czw_branch/20101203/Ohana/src/dvomerge/src/help.c

    r30118 r30631  
    3737  fprintf (stderr, "  dvomerge (input) into (output)\n");
    3838  fprintf (stderr, "  dvomerge (input) into (output) continue\n\n");
    39   fprintf (stderr, "  dvomerge (input) into (output) from (list)\n\n");
     39  fprintf (stderr, "  dvomerge (input) into (output) from (list)\n");
     40  fprintf (stderr, "     dvomerge list implies 'continue' : list contains, eg, n1500/1688.00.cpt (one cpt per line)\n\n");
    4041  fprintf (stderr, "  merge DVO databases\n");
    4142  fprintf (stderr, "  optional flags:\n");
  • branches/czw_branch/20101203/Ohana/src/getstar/src/select_by_image.c

    r12332 r30631  
    3838      stars[N].Mgal   = 0.001*(catalog[0].measure[i].Mgal - catalog[0].measure[i].dt);
    3939
    40       stars[N].fx     = 0.01*catalog[0].measure[i].FWx;
    41       stars[N].fy     = stars[N].fx * 0.01*catalog[0].measure[i].fwy;
    42       stars[N].df     = (360.0/255.0)*catalog[0].measure[i].theta;
     40      stars[N].fx     = FromShortPixels(catalog[0].measure[i].FWx);
     41      stars[N].fy     = FromShortPixels(catalog[0].measure[i].FWy);
     42      stars[N].df     = FromShortDegrees(catalog[0].measure[i].theta);
    4343      stars[N].found  = catalog[0].measure[i].flags;
    4444
  • branches/czw_branch/20101203/Ohana/src/imregister/imphot/dumpfits.c

    r27435 r30631  
    118118    XY_to_RD (&ra, &dec, 0.0, 0.0, &subset[0].coords);
    119119    airmass  = subset[0].secz;
    120     sky      = subset[0].Myyyy + 0x8000;
     120    sky      = 0.0; // subset[0].Myyyy + 0x8000;
    121121
    122122    /* we should get an error here if we don't construct this line correctly */
  • branches/czw_branch/20101203/Ohana/src/kapa2/include/prototypes.h

    r30118 r30631  
    175175void          DrawBitmap          PROTO((Graphic *graphic, int x, int y, int dx, int dy, char *bitmap, int mode));
    176176void          CrossHairs          PROTO((Graphic *graphic, Picture *image));
    177 void          hh_hms              PROTO((char *line, double ra, double dec, char sep));
     177void          hh_hms              PROTO((char *line, double ra, double dec, char sep, int Nchar));
    178178
    179179int           SetColormap         PROTO((char *name));
  • branches/czw_branch/20101203/Ohana/src/kapa2/src/PSObjects.c

    r27530 r30631  
    164164  float *x, *y;
    165165  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
    166   double sx0, sy0, sx1, sy1, sxa;
     166  double sx0, sy0, sx1, sy1, sxa, sya, sxo, syo;
    167167  double X0, X1, Y0, Y1;
    168168
     
    189189  for (i = 0; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
    190190  if (i >= object[0].Npts) return;
     191
     192  /* first valid data point */
     193  sx0 = x[i]*mxi + y[i]*mxj + bx;
     194  sy0 = x[i]*myi + y[i]*myj + by;
     195  sx0 = MIN (MAX (sx0, X0), X1);
     196  sy0 = MAX (MIN (sy0, Y0), Y1);
     197 
     198  /* find the second valid datapoint */
     199  for (i++; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
     200  if (i >= object[0].Npts) return;
     201
     202  /* second valid data point */
     203  sx1 = x[i]*mxi + y[i]*mxj + bx;
     204  sy1 = x[i]*myi + y[i]*myj + by;
     205  sx1 = MIN (MAX (sx1, X0), X1);
     206  sy1 = MAX (MIN (sy1, Y0), Y1);
     207 
     208  /* connect first point to second point */
     209  sxa = sx0 - 0.5*(sx1 - sx0);
     210  sya = MAX (sy0, Y0);
     211  DrawLine (sx0, sy0, sxa, sy0);
     212  DrawLine (sxa, sy0, sxa, sya);
     213 
     214  /* draw segment equal distance behind first point and down to x-axis */
     215  sxa = 0.5*(sx0 + sx1);
     216  DrawLine (sx0, sy0, sxa, sy0);
     217  DrawLine (sxa, sy0, sxa, sy1);
     218  DrawLine (sxa, sy1, sx1, sy1);
     219  sx0 = sx1;
     220  sy0 = sy1;
     221 
     222  /* continue with rest of points */
     223  sxo = syo = 0;
     224  for (i++; i < object[0].Npts; i++) {
     225    if (!(finite(x[i]) && finite(y[i]))) continue;
     226    sx1 = x[i]*mxi + y[i]*mxj + bx;
     227    sy1 = x[i]*myi + y[i]*myj + by;
     228    sx1 = MIN (MAX (sx1, X0), X1);
     229    sy1 = MAX (MIN (sy1, Y0), Y1);
     230    sxa = 0.5*(sx0 + sx1);
     231    DrawLine (sx0, sy0, sxa, sy0);
     232    DrawLine (sxa, sy0, sxa, sy1);
     233    DrawLine (sxa, sy1, sx1, sy1);
     234    sxo = sx0; syo = sy0;
     235    sx0 = sx1; sy0 = sy1;
     236  }
     237 
     238  /* draw segment equal distance after last point and down to x-axis */
     239  sxa = sx1 + 0.5*(sx1 - sxo);
     240  sya = MAX (sy1, Y0);
     241  DrawLine (sx1, sy1, sxa, sy1);
     242  DrawLine (sxa, sy1, sxa, sya);
     243
     244# if (0)
    191245  sx0 = x[i]*mxi + y[i]*mxj + bx;
    192246  sy0 = x[i]*myi + y[i]*myj + by;
     
    207261    sx0 = sx1; sy0 = sy1;
    208262  }
     263# endif
    209264}
    210265
  • branches/czw_branch/20101203/Ohana/src/kapa2/src/UpdateStatusBox.c

    r29539 r30631  
    2020 
    2121    bzero (line, 100);
    22     sprintf (line, "(%d x %d) @ %d                                         ",
     22    snprintf (line, 100, "(%d x %d) @ %d                                         ",
    2323             image[0].picture.dx, image[0].picture.dy, image[0].picture.expand);
    2424    XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
     
    2626   
    2727    bzero (line, 100);
    28     sprintf (line, "%-25s", image[0].image[0].file);
     28    snprintf (line, 100, "%-25s", image[0].image[0].file);
    2929    XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
    3030                 image[0].text_x + PAD1, image[0].text_y + 5*textpad + 5*PAD1, line, strlen(line));
    3131   
    3232    bzero (line, 100);
    33     sprintf (line, "(%s)                                          ", image[0].image[0].name);
     33    snprintf (line, 100, "(%s)                                          ", image[0].image[0].name);
    3434    XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
    3535                 image[0].text_x + PAD1, image[0].text_y + 6*textpad + 6*PAD1, line, 25);
     
    4545
    4646  if (image[0].HexValue) {
    47     sprintf (line, "%04x", (int) z);
     47      snprintf (line, 100, "%04x", (int) z);
    4848  } else {
    49     sprintf (line, "%22.3f", z);
     49      snprintf (line, 100, "%22.3f", z);
    5050  }
    5151  XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
     
    5353 
    5454  bzero (line, 100);
    55   sprintf (line, "%10.2f %10.2f", x, y);
     55  snprintf (line, 100, "%10.2f %10.2f", x, y);
    5656  XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
    5757               image[0].text_x + PAD1, image[0].text_y + 2*textpad + 2*PAD1, line, strlen(line));
     
    5959  bzero (line, 100);
    6060  if (image[0].DecimalDegrees) {
    61     sprintf (line, "%10.6f %10.6f", ra, dec);
     61    snprintf (line, 100, "%10.6f %10.6f", ra, dec);
    6262  } else {
    63     hh_hms (line, ra, dec, ':');
     63    hh_hms (line, ra, dec, ':', 100);
    6464  }
     65
    6566  XDrawString (graphic[0].display, graphic[0].window, graphic[0].gc,
    6667               image[0].text_x + PAD1, image[0].text_y + 3*textpad + 3*PAD1, line, strlen(line));
  • branches/czw_branch/20101203/Ohana/src/kapa2/src/bDrawObjects.c

    r30118 r30631  
    154154  float *x, *y;
    155155  double mxi, mxj, myi, myj, bxi, bxj, byi, byj, bx, by;
    156   double sx0, sy0, sx1, sy1, sxa;
     156  double sx0, sy0, sx1, sy1, sxa, sya, sxo, syo;
    157157  double X0, X1, Y0, Y1;
    158158
     
    179179  for (i = 0; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
    180180  if (i >= object[0].Npts) return;
     181
     182  /* first valid data point */
     183  sx0 = x[i]*mxi + y[i]*mxj + bx;
     184  sy0 = x[i]*myi + y[i]*myj + by;
     185  sx0 = MIN (MAX (sx0, X0), X1);
     186  sy0 = MAX (MIN (sy0, Y0), Y1);
     187 
     188  /* find the second valid datapoint */
     189  for (i++; (i < object[0].Npts) && !(finite(x[i]) && finite(y[i])); i++);
     190  if (i >= object[0].Npts) return;
     191
     192  /* second valid data point */
     193  sx1 = x[i]*mxi + y[i]*mxj + bx;
     194  sy1 = x[i]*myi + y[i]*myj + by;
     195  sx1 = MIN (MAX (sx1, X0), X1);
     196  sy1 = MAX (MIN (sy1, Y0), Y1);
     197 
     198  /* connect first point to second point */
     199  sxa = sx0 - 0.5*(sx1 - sx0);
     200  sya = MAX (sy0, Y0);
     201  DrawLine (buffer, sx0, sy0, sxa, sy0);
     202  DrawLine (buffer, sxa, sy0, sxa, sya);
     203 
     204  /* draw segment equal distance behind first point and down to x-axis */
     205  sxa = 0.5*(sx0 + sx1);
     206  DrawLine (buffer, sx0, sy0, sxa, sy0);
     207  DrawLine (buffer, sxa, sy0, sxa, sy1);
     208  DrawLine (buffer, sxa, sy1, sx1, sy1);
     209  sx0 = sx1;
     210  sy0 = sy1;
     211 
     212  /* continue with rest of points */
     213  sxo = syo = 0;
     214  for (i++; i < object[0].Npts; i++) {
     215    if (!(finite(x[i]) && finite(y[i]))) continue;
     216    sx1 = x[i]*mxi + y[i]*mxj + bx;
     217    sy1 = x[i]*myi + y[i]*myj + by;
     218    sx1 = MIN (MAX (sx1, X0), X1);
     219    sy1 = MAX (MIN (sy1, Y0), Y1);
     220    sxa = 0.5*(sx0 + sx1);
     221    DrawLine (buffer, sx0, sy0, sxa, sy0);
     222    DrawLine (buffer, sxa, sy0, sxa, sy1);
     223    DrawLine (buffer, sxa, sy1, sx1, sy1);
     224    sxo = sx0; syo = sy0;
     225    sx0 = sx1; sy0 = sy1;
     226  }
     227 
     228  /* draw segment equal distance after last point and down to x-axis */
     229  sxa = sx1 + 0.5*(sx1 - sxo);
     230  sya = MAX (sy1, Y0);
     231  DrawLine (buffer, sx1, sy1, sxa, sy1);
     232  DrawLine (buffer, sxa, sy1, sxa, sya);
     233
     234# if (0)
    181235  sx0 = x[i]*mxi + y[i]*mxj + bx;
    182236  sy0 = x[i]*myi + y[i]*myj + by;
     
    197251    sx0 = sx1; sy0 = sy1;
    198252  }
     253# endif
    199254}
    200255
  • branches/czw_branch/20101203/Ohana/src/kapa2/src/hh_hms.c

    r13320 r30631  
    11# include "Ximage.h"
    22
    3 void hh_hms (char *line, double ra, double dec, char sep) {
     3void hh_hms (char *line, double ra, double dec, char sep, int Nchar) {
    44
    55  int h, m, flag;
     
    1313  s = 3600*(ra - h - m / 60.0);
    1414  if (flag > 0)
    15     sprintf (line, " %02d%c%02d%c%04.1f  ", h, sep, m, sep, s);
     15    snprintf (line, Nchar, " %02d%c%02d%c%04.1f  ", h, sep, m, sep, s);
    1616  else
    17     sprintf (line, "-%02d%c%02d%c%04.1f  ", h, sep, m, sep, s);
     17    snprintf (line, Nchar, "-%02d%c%02d%c%04.1f  ", h, sep, m, sep, s);
    1818 
    1919  flag = SIGN(dec);
     
    2323  s = 3600*(dec - h - m / 60.0);
    2424  if (flag > 0)
    25     sprintf (&line[13], " %02d%c%02d%c%04.1f", h, sep, m, sep, s);
     25    snprintf (&line[13], Nchar, " %02d%c%02d%c%04.1f", h, sep, m, sep, s);
    2626  else
    27     sprintf (&line[13], "-%02d%c%02d%c%04.1f", h, sep, m, sep, s);
     27    snprintf (&line[13], Nchar, "-%02d%c%02d%c%04.1f", h, sep, m, sep, s);
    2828}
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-elixir.d

    r15034 r30631  
    4343FIELD     ccdnum,           CCDNUM,               unsigned char,  CCD ID number
    4444FIELD     dummy,            DUMMY,                char[20],       unused
    45 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    46 FIELD     Mx,               MX,                   short,          Mrel polyterm
    47 FIELD     My,               MY,                   short,          Mrel polyterm
    48 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    49 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    50 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    51 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    52 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    53 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    54 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    55 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    56 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    57 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    58 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    59 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
     45
     46FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     47FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     48FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     49
     50FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     51FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     52
     53FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     54FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     55FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     56
     57FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     58FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     59# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-loneos.d

    r15034 r30631  
    4343FIELD     ccdnum,           CCDNUM,               unsigned char,  CCD ID number
    4444FIELD     dummy,            DUMMY,                char[20],       unused
    45 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    46 FIELD     Mx,               MX,                   short,          Mrel polyterm
    47 FIELD     My,               MY,                   short,          Mrel polyterm
    48 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    49 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    50 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    51 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    52 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    53 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    54 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    55 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    56 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    57 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    58 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    59 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
     45
     46FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     47FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     48FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     49
     50FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     51FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     52
     53FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     54FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     55FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     56
     57FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     58FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     59# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-panstarrs-dev-0.d

    r15034 r30631  
    5353
    5454FIELD     dummy,            DUMMY,                char[10],       unused
    55 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    56 FIELD     Mx,               MX,                   short,          Mrel polyterm
    57 FIELD     My,               MY,                   short,          Mrel polyterm
    58 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    59 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    60 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    61 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    62 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    63 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    64 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    65 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    66 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    67 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    68 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    69 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    70 # 40 bytes
     55FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     56FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     57FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     58
     59FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     60FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     61
     62FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     63FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     64FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     65
     66FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     67FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     68# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-panstarrs-dev-1.d

    r15034 r30631  
    5353
    5454FIELD     dummy,            DUMMY,                char[10],       unused
    55 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    56 FIELD     Mx,               MX,                   short,          Mrel polyterm
    57 FIELD     My,               MY,                   short,          Mrel polyterm
    58 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    59 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    60 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    61 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    62 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    63 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    64 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    65 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    66 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    67 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    68 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    69 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    70 # 40 bytes
     55FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     56FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     57FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     58
     59FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     60FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     61
     62FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     63FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     64FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     65
     66FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     67FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     68# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-dev-1.d

    r15038 r30631  
    4949FIELD     ccdnum,           CCDNUM,               unsigned char,  CCD ID number
    5050FIELD     imageID,          IMAGE_ID,             unsigned int,   image ID
     51FIELD     dummy,            DUMMY,                char[14],       unused
    5152# 44 bytes
    5253
    53 FIELD     dummy,            DUMMY,                char[14],       unused
    54 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    55 FIELD     Mx,               MX,                   short,          Mrel polyterm
    56 FIELD     My,               MY,                   short,          Mrel polyterm
    57 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    58 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    59 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    60 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    61 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    62 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    63 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    64 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    65 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    66 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    67 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    68 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    6954# 40 bytes
     55FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     56FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     57FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     58
     59FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     60FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     61
     62FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     63FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     64FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     65
     66FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     67FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     68# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-dev-2.d

    r16810 r30631  
    5353# 48 bytes
    5454
    55 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    56 FIELD     Mx,               MX,                   short,          Mrel polyterm
    57 FIELD     My,               MY,                   short,          Mrel polyterm
    58 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    59 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    60 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    61 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    62 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    63 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    64 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    65 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    66 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    67 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    68 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    69 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    70 # 40 bytes
     55FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     56FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     57FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     58
     59FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     60FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     61
     62FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     63FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     64FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     65
     66FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     67FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     68# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-dev-3.d

    r20362 r30631  
    5454# 48 bytes
    5555
    56 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    57 FIELD     Mx,               MX,                   short,          Mrel polyterm
    58 FIELD     My,               MY,                   short,          Mrel polyterm
    59 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    60 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    61 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    62 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    63 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    64 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    65 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    66 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    67 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    68 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    69 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    70 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    71 # 40 bytes
     56FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     57FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     58FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     59
     60FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     61FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     62
     63FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     64FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     65FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     66
     67FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     68FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     69# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-ref.d

    r24746 r30631  
    5555# 48 bytes
    5656
    57 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    58 FIELD     Mx,               MX,                   short,          Mrel polyterm
    59 FIELD     My,               MY,                   short,          Mrel polyterm
    60 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    61 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    62 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    63 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    64 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    65 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    66 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    67 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    68 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    69 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    70 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    71 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    7257# 40 bytes
     58FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     59FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     60FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     61
     62FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     63FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     64
     65FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     66FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     67FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     68
     69FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     70FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     71# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
     72
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-v1.d

    r21508 r30631  
    5555# 48 bytes
    5656
    57 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    58 FIELD     Mx,               MX,                   short,          Mrel polyterm
    59 FIELD     My,               MY,                   short,          Mrel polyterm
    60 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    61 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    62 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    63 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    64 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    65 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    66 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    67 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    68 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    69 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    70 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    71 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    72 # 40 bytes
     57FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     58FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     59FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     60
     61FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     62FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     63
     64FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     65FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     66FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     67
     68FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     69FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     70# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image-ps1-v2.d

    r27579 r30631  
    6060# 48 bytes
    6161
    62 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    63 FIELD     Mx,               MX,                   short,          Mrel polyterm
    64 FIELD     My,               MY,                   short,          Mrel polyterm
    65 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    66 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    67 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    68 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    69 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    70 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    71 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    72 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    73 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    74 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    75 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    76 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
    77 # 40 bytes
     62FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     63FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     64FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     65
     66FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     67FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     68
     69FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     70FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     71FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     72
     73FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     74FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     75# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/image.d

    r27579 r30631  
    1 STRUCT       Image
     1 STRUCT       Image
    22EXTNAME      DVO_IMAGE
    33TYPE         BINTABLE
     
    66
    77# elements of the image structure
     8# careful of 8-byte boundaries
    89
    910SUBSTRUCT coords,           COORDS,               Coords,        astrometric data
     
    6061# 48 bytes
    6162
    62 FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
    63 FIELD     Mx,               MX,                   short,          Mrel polyterm
    64 FIELD     My,               MY,                   short,          Mrel polyterm
    65 FIELD     Mxx,              MXX,                  short,          Mrel polyterm
    66 FIELD     Mxy,              MXY,                  short,          Mrel polyterm
    67 FIELD     Myy,              MYY,                  short,          Mrel polyterm
    68 FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
    69 FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
    70 FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
    71 FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
    72 FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
    73 FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
    74 FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
    75 FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
    76 FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
     63FIELD     nLinkAstrom,      NLINK_ASTROM,         short,          mean number of matched measurements for astrometry
     64FIELD     nLinkPhotom,      NLINK_PHOTOM,         short,          mean number of matched measurements for astrometry
     65FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     66
     67FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     68FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     69
     70FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     71FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     72FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     73
     74FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     75FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     76# nFitPhotom lands on the old location of Mxxxx, which was used to mean nFitPhotom in some cases
     77
     78# old image structure:
     79# FIELD           order,            ORDER,                short,          Mrel 2D polynomical order
     80# FIELD           Mx,               MX,                   short,          Mrel polyterm
     81# FIELD           My,               MY,                   short,          Mrel polyterm
     82# FIELD           Mxx,              MXX,                  short,          Mrel polyterm
     83# FIELD           Mxy,              MXY,                  short,          Mrel polyterm
     84# FIELD           Myy,              MYY,                  short,          Mrel polyterm
     85# FIELD           Mxxx,             MXXX,                 short,          Mrel polyterm
     86# FIELD           Mxxy,             MXXY,                 short,          Mrel polyterm
     87# FIELD           Mxyy,             MXYY,                 short,          Mrel polyterm
     88# FIELD           Myyy,             MYYY,                 short,          Mrel polyterm
     89# FIELD           Mxxxx,            MXXXX,                short,          Mrel polyterm
     90# FIELD           Mxxxy,            MXXXY,                short,          Mrel polyterm
     91# FIELD           Mxxyy,            MXXYY,                short,          Mrel polyterm
     92# FIELD           Mxyyy,            MXYYY,                short,          Mrel polyterm
     93# FIELD           Myyyy,            MYYYY,                short,          Mrel polyterm
    7794# 40 bytes
    7895
    7996# *** 20090206 : new fields : parentID, flags (was code char), changed name to 121 bytes.
    80 # *** 20100331 : new fields :
     97# *** 20100331 : new fields : RAo, DECo. Radius
     98# *** 20110203 : replace the old zero point polynomial terms (Mx,My,... Mxxxx,Myyyy) with dummy1 - astrom_map_id
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/measure-ps1-v2.d

    r27579 r30631  
    6363FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,          1/100 of pixels
    6464FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,          1/100 of pixels
     65FIELD dRsys,          POS_SYS_ERR,  short,          systematic error from astrom,   1/100 of pixels
    6566
    66 FIELD pad,            PAD,          char[6],        padding
     67FIELD pad,            PAD,          char[4],        padding
    6768
    6869# local astrometry scales
  • branches/czw_branch/20101203/Ohana/src/libautocode/def/measure.d

    r21508 r30631  
    22EXTNAME      DVO_MEASURE
    33TYPE         BINTABLE
    4 SIZE         146
     4SIZE         148
    55DESCRIPTION  DVO Detection Measurement Table
    66
     
    5959FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,          1/100 of pixels
    6060FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,          1/100 of pixels
     61FIELD dRsys,          POS_SYS_ERR,  short,          systematic error from astrom,   1/100 of pixels
    6162
    6263FIELD posangle,       POSANGLE,     short,          position angle sky to chip,     (0xffff/360) deg
  • branches/czw_branch/20101203/Ohana/src/libdvo/Makefile

    r27580 r30631  
    5151$(SRC)/mosaic_astrom.$(ARCH).o   \
    5252$(SRC)/fits_db.$(ARCH).o         \
    53 $(SRC)/photfits.$(ARCH).o        \
    5453$(SRC)/dvo_image.$(ARCH).o       \
    5554$(SRC)/dvo_image_raw.$(ARCH).o   \
     
    7776$(SRC)/dvo_util.$(ARCH).o
    7877
    79 # $(SRC)/dvo_convert_panstarrs.$(ARCH).o \
    80 # $(SRC)/dvo_convert_pmtest.$(ARCH).o \
     78# $(SRC)/dvo_convert_panstarrs.$(ARCH).o
     79# $(SRC)/dvo_convert_pmtest.$(ARCH).o
     80# $(SRC)/photfits.$(ARCH).o       
    8181
    8282include ../libautocode/Makefile.Targets
  • branches/czw_branch/20101203/Ohana/src/libdvo/doc/dvo-images.txt

    r15240 r30631  
    11
    2 the dvo image table is not extremely efficient.  it is unsorted, and
    3 spatial searches consist of complete scans of the table.
     220110203
    43
    5 a spatial index could consist of five cardinal points for each square
    6 image (4 corners and the center) or a central point and max radius.
    7 table would consist each would be
     4  The Image table (PS1_V2) has a bunch of fields that are not really
     5  used by anything / much.  I would like to re-use those bytes to
     6  store information about the quality of the astrometric and
     7  photometric calibrations. 
     8
     9  The under-used fields are the 'photometric zero point variation'
     10  polynomial terms (Mx, My, ... Mxxxx, Myyyy).
     11
     12  (I list the sourceID, which does not change, because the word-boundaries matter)
     13FIELD     sourceID,         SOURCE_ID,            unsigned short, analysis source ID
     14FIELD     order,            ORDER,                short,          Mrel 2D polynomical order
     15FIELD     Mx,               MX,                   short,          Mrel polyterm
     16FIELD     My,               MY,                   short,          Mrel polyterm
     17
     18FIELD     Mxx,              MXX,                  short,          Mrel polyterm
     19FIELD     Mxy,              MXY,                  short,          Mrel polyterm
     20FIELD     Myy,              MYY,                  short,          Mrel polyterm
     21FIELD     Mxxx,             MXXX,                 short,          Mrel polyterm
     22
     23FIELD     Mxxy,             MXXY,                 short,          Mrel polyterm
     24FIELD     Mxyy,             MXYY,                 short,          Mrel polyterm
     25FIELD     Myyy,             MYYY,                 short,          Mrel polyterm
     26FIELD     Mxxxx,            MXXXX,                short,          Mrel polyterm
     27
     28FIELD     Mxxxy,            MXXXY,                short,          Mrel polyterm
     29FIELD     Mxxyy,            MXXYY,                short,          Mrel polyterm
     30FIELD     Mxyyy,            MXYYY,                short,          Mrel polyterm
     31FIELD     Myyyy,            MYYYY,                short,          Mrel polyterm
     32(30 bytes)
     33
     34  I am going to replace these with:
     35
     36  photom_map_id (int)   : index to a 2D map of the zp variations (table and format not yet defined)
     37  astrom_map_id (int)   : index to a 2D map of the zp variations (table and format not yet defined)
     38  dMagSys       (float) : systematic photometric error (mag)
     39  dXpixSys      (float) : systematic astrometric error (pix)
     40  dYpixSys      (float) : systematic astrometric error (pix)
     41  nFitAstrom    (short) : number of stars used for astrometric calibration (saturate at 0x7fff)
     42  nFitPhotom    (short) : number of stars used for astrometric calibration (saturate at 0x7fff)
     43(24 bytes)
     44
     45  As for the existing uses of these fields:
     46
     47  The official use can just be dropped (replaced by the maps and
     48  recalculated if needed -- no db actually uses them).  There are a
     49  few places in the code where these fields were overloaded:
     50
     51  Triangular Images : in the skycell creation code, there is an option
     52  to create images which are the triangular projection centers for a
     53  given subdivided geometric solid.  This code was overloading the Mx,
     54  My, and Mxxx through Myyy fields.  I can invalidate any existing db
     55  with triangles (probably none are needed) and repurpose some of the
     56  fields above for these values.  (addstar/src/sky_tesslation.c, opihi/dvo/images.c)
     57
     58  Mxxxx : some code was using Mxxxx to store nFitPhotom.  Again, any
     59  existing databases are unlikely to use that info (CFHT skyprobe
     60  only).  If I am clever, I can align the existing bytes so that the
     61  old Mxxxx hits the new nFitPhotom. (addstar/src/calibrate.c,
     62  dbExtractImages.c)
     63
     64  Myyyy : dbExtractImages.c expects this value to have (sky - 0x8000),
     65  but that value is not actually set by any code.  Ignore (replace?)
     66 
     67FIELD     sourceID,         SOURCE_ID,            unsigned short, analysis source ID
     68FIELD     dummy1,           DUMMY1,               short,          place holder for byte boundaries
     69FIELD     dummy2,           DUMMY2,               short,          place holder for byte boundaries
     70FIELD     dummy3,           DUMMY3,               short,          place holder for byte boundaries
     71
     72FIELD     dXpixSys,         XPIX_SYS_ERR,         float,          systematic astrometry error in X
     73FIELD     dYpixSys,         YPIX_SYS_ERR,         float,          systematic astrometry error in Y
     74
     75FIELD     dMagSys,          MAG_SYS_ERR,          float,          systematic photometry error
     76FIELD     nFitAstrom,       N_FIT_ASTROM,         short,          number of stars used for astrometry cal
     77FIELD     nFitPhotom,       N_FIT_PHOTOM,         short,          number of stars used for photometry cal
     78
     79FIELD     photom_map_id,    PHOTOM_MAP_ID,        unsigned int,   reference to 2D zero point map
     80FIELD     astrom_map_id,    ASTROM_MAP_ID,        unsigned int,   reference to 2D astrometry map
     81
     82older:
     83 
     84  the dvo image table is not extremely efficient.  it is unsorted, and
     85  spatial searches consist of complete scans of the table.
     86 
     87  a spatial index could consist of five cardinal points for each
     88  square image (4 corners and the center) or a central point and max
     89  radius.
     90
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_1.c

    r27580 r30631  
    88  Measure *out;
    99
    10   ALLOCATE (out, Measure, Nvalues);
     10  ALLOCATE_ZERO (out, Measure, Nvalues);
    1111
    1212  for (i = 0; i < Nvalues; i++) {
     
    6666  Measure_PS1_DEV_1 *out;
    6767
    68   ALLOCATE (out, Measure_PS1_DEV_1, Nvalues);
     68  ALLOCATE_ZERO (out, Measure_PS1_DEV_1, Nvalues);
    6969
    7070  for (i = 0; i < Nvalues; i++) {
     
    113113  Average *out;
    114114
    115   ALLOCATE (out, Average, Nvalues);
     115  ALLOCATE_ZERO (out, Average, Nvalues);
    116116
    117117  for (i = 0; i < Nvalues; i++) {
     
    157157  Average_PS1_DEV_1 *out;
    158158
    159   ALLOCATE (out, Average_PS1_DEV_1, Nvalues);
     159  ALLOCATE_ZERO (out, Average_PS1_DEV_1, Nvalues);
    160160
    161161  for (i = 0; i < Nvalues; i++) {
     
    191191  SecFilt *out;
    192192
    193   ALLOCATE (out, SecFilt, Nvalues);
     193  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    194194
    195195  for (i = 0; i < Nvalues; i++) {
     
    215215  SecFilt_PS1_DEV_1 *out;
    216216
    217   ALLOCATE (out, SecFilt_PS1_DEV_1, Nvalues);
     217  ALLOCATE_ZERO (out, SecFilt_PS1_DEV_1, Nvalues);
    218218
    219219  for (i = 0; i < Nvalues; i++) {
     
    232232  Image *out;
    233233
    234   ALLOCATE (out, Image, Nvalues);
     234  ALLOCATE_ZERO (out, Image, Nvalues);
    235235
    236236  for (i = 0; i < Nvalues; i++) {
     
    263263    out[i].imageID          = in[i].imageID;
    264264
    265     out[i].order            = in[i].order;
    266     out[i].Mx               = in[i].Mx;
    267     out[i].My               = in[i].My;
    268     out[i].Mxx              = in[i].Mxx;
    269     out[i].Mxy              = in[i].Mxy;
    270     out[i].Myy              = in[i].Myy;
    271     out[i].Mxxx             = in[i].Mxxx;
    272     out[i].Mxxy             = in[i].Mxxy;
    273     out[i].Mxyy             = in[i].Mxyy;
    274     out[i].Myyy             = in[i].Myyy;
    275     out[i].Mxxxx            = in[i].Mxxxx;
    276     out[i].Mxxxy            = in[i].Mxxxy;
    277     out[i].Mxxyy            = in[i].Mxxyy;
    278     out[i].Mxyyy            = in[i].Mxyyy;
    279     out[i].Myyyy            = in[i].Myyyy;
     265    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     266    // with the following.  (no real databases used those values -- see
     267    // libdvo/doc/dvo-images.txt)
     268    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     269    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     270    out[i].dummy3           = in[i].dummy3;
     271    out[i].dXpixSys         = in[i].dXpixSys;
     272    out[i].dYpixSys         = in[i].dYpixSys;
     273    out[i].dMagSys          = in[i].dMagSys;
     274    out[i].nFitAstrom       = in[i].nFitAstrom;
     275    out[i].nFitPhotom       = in[i].nFitPhotom;
     276    out[i].photom_map_id    = in[i].photom_map_id;
     277    out[i].astrom_map_id    = in[i].astrom_map_id;
    280278
    281279    // changed or added for PS1_DEV_2
     
    301299  Image_PS1_DEV_1 *out;
    302300
    303   ALLOCATE (out, Image_PS1_DEV_1, Nvalues);
     301  ALLOCATE_ZERO (out, Image_PS1_DEV_1, Nvalues);
    304302
    305303  for (i = 0; i < Nvalues; i++) {
     
    332330    out[i].imageID          = in[i].imageID;
    333331
    334     out[i].order            = in[i].order;
    335     out[i].Mx               = in[i].Mx;
    336     out[i].My               = in[i].My;
    337     out[i].Mxx              = in[i].Mxx;
    338     out[i].Mxy              = in[i].Mxy;
    339     out[i].Myy              = in[i].Myy;
    340     out[i].Mxxx             = in[i].Mxxx;
    341     out[i].Mxxy             = in[i].Mxxy;
    342     out[i].Mxyy             = in[i].Mxyy;
    343     out[i].Myyy             = in[i].Myyy;
    344     out[i].Mxxxx            = in[i].Mxxxx;
    345     out[i].Mxxxy            = in[i].Mxxxy;
    346     out[i].Mxxyy            = in[i].Mxxyy;
    347     out[i].Mxyyy            = in[i].Mxyyy;
    348     out[i].Myyyy            = in[i].Myyyy;
     332    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     333    // with the following.  (no real databases used those values -- see
     334    // libdvo/doc/dvo-images.txt)
     335    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     336    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     337    out[i].dummy3           = in[i].dummy3;
     338    out[i].dXpixSys         = in[i].dXpixSys;
     339    out[i].dYpixSys         = in[i].dYpixSys;
     340    out[i].dMagSys          = in[i].dMagSys;
     341    out[i].nFitAstrom       = in[i].nFitAstrom;
     342    out[i].nFitPhotom       = in[i].nFitPhotom;
     343    out[i].photom_map_id    = in[i].photom_map_id;
     344    out[i].astrom_map_id    = in[i].astrom_map_id;
    349345
    350346    // changed or added for PS1_V1
     
    359355  PhotCode *out;
    360356
    361   ALLOCATE (out, PhotCode, Nvalues);
     357  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    362358
    363359  for (i = 0; i < Nvalues; i++) {
     
    398394  PhotCode_PS1_DEV_1 *out;
    399395
    400   ALLOCATE (out, PhotCode_PS1_DEV_1, Nvalues);
     396  ALLOCATE_ZERO (out, PhotCode_PS1_DEV_1, Nvalues);
    401397
    402398  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_2.c

    r27580 r30631  
    88  Measure *out;
    99
    10   ALLOCATE (out, Measure, Nvalues);
     10  ALLOCATE_ZERO (out, Measure, Nvalues);
    1111
    1212  for (i = 0; i < Nvalues; i++) {
     
    6363  Measure_PS1_DEV_2 *out;
    6464
    65   ALLOCATE (out, Measure_PS1_DEV_2, Nvalues);
     65  ALLOCATE_ZERO (out, Measure_PS1_DEV_2, Nvalues);
    6666
    6767  for (i = 0; i < Nvalues; i++) {
     
    109109  Average *out;
    110110
    111   ALLOCATE (out, Average, Nvalues);
     111  ALLOCATE_ZERO (out, Average, Nvalues);
    112112
    113113  for (i = 0; i < Nvalues; i++) {
     
    151151  Average_PS1_DEV_2 *out;
    152152
    153   ALLOCATE (out, Average_PS1_DEV_2, Nvalues);
     153  ALLOCATE_ZERO (out, Average_PS1_DEV_2, Nvalues);
    154154
    155155  for (i = 0; i < Nvalues; i++) {
     
    185185  SecFilt *out;
    186186
    187   ALLOCATE (out, SecFilt, Nvalues);
     187  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    188188
    189189  for (i = 0; i < Nvalues; i++) {
     
    209209  SecFilt_PS1_DEV_2 *out;
    210210
    211   ALLOCATE (out, SecFilt_PS1_DEV_2, Nvalues);
     211  ALLOCATE_ZERO (out, SecFilt_PS1_DEV_2, Nvalues);
    212212
    213213  for (i = 0; i < Nvalues; i++) {
     
    226226  Image *out;
    227227
    228   ALLOCATE (out, Image, Nvalues);
     228  ALLOCATE_ZERO (out, Image, Nvalues);
    229229
    230230  for (i = 0; i < Nvalues; i++) {
     
    259259    out[i].sourceID         = in[i].sourceID;
    260260
    261     out[i].order            = in[i].order;
    262     out[i].Mx               = in[i].Mx;
    263     out[i].My               = in[i].My;
    264     out[i].Mxx              = in[i].Mxx;
    265     out[i].Mxy              = in[i].Mxy;
    266     out[i].Myy              = in[i].Myy;
    267     out[i].Mxxx             = in[i].Mxxx;
    268     out[i].Mxxy             = in[i].Mxxy;
    269     out[i].Mxyy             = in[i].Mxyy;
    270     out[i].Myyy             = in[i].Myyy;
    271     out[i].Mxxxx            = in[i].Mxxxx;
    272     out[i].Mxxxy            = in[i].Mxxxy;
    273     out[i].Mxxyy            = in[i].Mxxyy;
    274     out[i].Mxyyy            = in[i].Mxyyy;
    275     out[i].Myyyy            = in[i].Myyyy;
     261    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     262    // with the following.  (no real databases used those values -- see
     263    // libdvo/doc/dvo-images.txt)
     264    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     265    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     266    out[i].dummy3           = in[i].dummy3;
     267    out[i].dXpixSys         = in[i].dXpixSys;
     268    out[i].dYpixSys         = in[i].dYpixSys;
     269    out[i].dMagSys          = in[i].dMagSys;
     270    out[i].nFitAstrom       = in[i].nFitAstrom;
     271    out[i].nFitPhotom       = in[i].nFitPhotom;
     272    out[i].photom_map_id    = in[i].photom_map_id;
     273    out[i].astrom_map_id    = in[i].astrom_map_id;
    276274
    277275    // changed or added for PS1_V1
     
    292290  Image_PS1_DEV_2 *out;
    293291
    294   ALLOCATE (out, Image_PS1_DEV_2, Nvalues);
     292  ALLOCATE_ZERO (out, Image_PS1_DEV_2, Nvalues);
    295293
    296294  for (i = 0; i < Nvalues; i++) {
     
    326324    out[i].sourceID         = in[i].sourceID;
    327325
    328     out[i].order            = in[i].order;
    329     out[i].Mx               = in[i].Mx;
    330     out[i].My               = in[i].My;
    331     out[i].Mxx              = in[i].Mxx;
    332     out[i].Mxy              = in[i].Mxy;
    333     out[i].Myy              = in[i].Myy;
    334     out[i].Mxxx             = in[i].Mxxx;
    335     out[i].Mxxy             = in[i].Mxxy;
    336     out[i].Mxyy             = in[i].Mxyy;
    337     out[i].Myyy             = in[i].Myyy;
    338     out[i].Mxxxx            = in[i].Mxxxx;
    339     out[i].Mxxxy            = in[i].Mxxxy;
    340     out[i].Mxxyy            = in[i].Mxxyy;
    341     out[i].Mxyyy            = in[i].Mxyyy;
    342     out[i].Myyyy            = in[i].Myyyy;
     326    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     327    // with the following.  (no real databases used those values -- see
     328    // libdvo/doc/dvo-images.txt)
     329    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     330    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     331    out[i].dummy3           = in[i].dummy3;
     332    out[i].dXpixSys         = in[i].dXpixSys;
     333    out[i].dYpixSys         = in[i].dYpixSys;
     334    out[i].dMagSys          = in[i].dMagSys;
     335    out[i].nFitAstrom       = in[i].nFitAstrom;
     336    out[i].nFitPhotom       = in[i].nFitPhotom;
     337    out[i].photom_map_id    = in[i].photom_map_id;
     338    out[i].astrom_map_id    = in[i].astrom_map_id;
    343339
    344340    // changed or added for PS1_V1
     
    353349  PhotCode *out;
    354350
    355   ALLOCATE (out, PhotCode, Nvalues);
     351  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    356352
    357353  for (i = 0; i < Nvalues; i++) {
     
    390386  PhotCode_PS1_DEV_2 *out;
    391387
    392   ALLOCATE (out, PhotCode_PS1_DEV_2, Nvalues);
     388  ALLOCATE_ZERO (out, PhotCode_PS1_DEV_2, Nvalues);
    393389
    394390  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_DEV_3.c

    r27491 r30631  
    1010  Image *out;
    1111
    12   ALLOCATE (out, Image, Nvalues);
     12  ALLOCATE_ZERO (out, Image, Nvalues);
    1313
    1414  for (i = 0; i < Nvalues; i++) {
     
    4343    out[i].sourceID         = in[i].sourceID;
    4444
    45     out[i].order            = in[i].order;
    46     out[i].Mx               = in[i].Mx;
    47     out[i].My               = in[i].My;
    48     out[i].Mxx              = in[i].Mxx;
    49     out[i].Mxy              = in[i].Mxy;
    50     out[i].Myy              = in[i].Myy;
    51     out[i].Mxxx             = in[i].Mxxx;
    52     out[i].Mxxy             = in[i].Mxxy;
    53     out[i].Mxyy             = in[i].Mxyy;
    54     out[i].Myyy             = in[i].Myyy;
    55     out[i].Mxxxx            = in[i].Mxxxx;
    56     out[i].Mxxxy            = in[i].Mxxxy;
    57     out[i].Mxxyy            = in[i].Mxxyy;
    58     out[i].Mxyyy            = in[i].Mxyyy;
    59     out[i].Myyyy            = in[i].Myyyy;
     45    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     46    // with the following.  (no real databases used those values -- see
     47    // libdvo/doc/dvo-images.txt)
     48    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     49    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     50    out[i].dummy3           = in[i].dummy3;
     51    out[i].dXpixSys         = in[i].dXpixSys;
     52    out[i].dYpixSys         = in[i].dYpixSys;
     53    out[i].dMagSys          = in[i].dMagSys;
     54    out[i].nFitAstrom       = in[i].nFitAstrom;
     55    out[i].nFitPhotom       = in[i].nFitPhotom;
     56    out[i].photom_map_id    = in[i].photom_map_id;
     57    out[i].astrom_map_id    = in[i].astrom_map_id;
    6058
    6159    // changed or added for PS1_V1
     
    7169  Image_PS1_DEV_3 *out;
    7270
    73   ALLOCATE (out, Image_PS1_DEV_3, Nvalues);
     71  ALLOCATE_ZERO (out, Image_PS1_DEV_3, Nvalues);
    7472
    7573  for (i = 0; i < Nvalues; i++) {
     
    104102    out[i].sourceID         = in[i].sourceID;
    105103
    106     out[i].order            = in[i].order;
    107     out[i].Mx               = in[i].Mx;
    108     out[i].My               = in[i].My;
    109     out[i].Mxx              = in[i].Mxx;
    110     out[i].Mxy              = in[i].Mxy;
    111     out[i].Myy              = in[i].Myy;
    112     out[i].Mxxx             = in[i].Mxxx;
    113     out[i].Mxxy             = in[i].Mxxy;
    114     out[i].Mxyy             = in[i].Mxyy;
    115     out[i].Myyy             = in[i].Myyy;
    116     out[i].Mxxxx            = in[i].Mxxxx;
    117     out[i].Mxxxy            = in[i].Mxxxy;
    118     out[i].Mxxyy            = in[i].Mxxyy;
    119     out[i].Mxyyy            = in[i].Mxyyy;
    120     out[i].Myyyy            = in[i].Myyyy;
     104    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     105    // with the following.  (no real databases used those values -- see
     106    // libdvo/doc/dvo-images.txt)
     107    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     108    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     109    out[i].dummy3           = in[i].dummy3;
     110    out[i].dXpixSys         = in[i].dXpixSys;
     111    out[i].dYpixSys         = in[i].dYpixSys;
     112    out[i].dMagSys          = in[i].dMagSys;
     113    out[i].nFitAstrom       = in[i].nFitAstrom;
     114    out[i].nFitPhotom       = in[i].nFitPhotom;
     115    out[i].photom_map_id    = in[i].photom_map_id;
     116    out[i].astrom_map_id    = in[i].astrom_map_id;
    121117
    122118    // changed or added for PS1_V1
     
    131127  PhotCode *out;
    132128
    133   ALLOCATE (out, PhotCode, Nvalues);
     129  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    134130
    135131  for (i = 0; i < Nvalues; i++) {
     
    167163  PhotCode_PS1_DEV_3 *out;
    168164
    169   ALLOCATE (out, PhotCode_PS1_DEV_3, Nvalues);
     165  ALLOCATE_ZERO (out, PhotCode_PS1_DEV_3, Nvalues);
    170166
    171167  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_REF.c

    r27580 r30631  
    88  Measure *out;
    99
    10   ALLOCATE (out, Measure, Nvalues);
     10  ALLOCATE_ZERO (out, Measure, Nvalues);
    1111
    1212  for (i = 0; i < Nvalues; i++) {
     
    6161  Measure_PS1_REF *out;
    6262
    63   ALLOCATE (out, Measure_PS1_REF, Nvalues);
     63  ALLOCATE_ZERO (out, Measure_PS1_REF, Nvalues);
    6464
    6565  for (i = 0; i < Nvalues; i++) {
     
    8484  Average *out;
    8585
    86   ALLOCATE (out, Average, Nvalues);
     86  ALLOCATE_ZERO (out, Average, Nvalues);
    8787
    8888  for (i = 0; i < Nvalues; i++) {
     
    124124  Average_PS1_REF *out;
    125125
    126   ALLOCATE (out, Average_PS1_REF, Nvalues);
     126  ALLOCATE_ZERO (out, Average_PS1_REF, Nvalues);
    127127
    128128  for (i = 0; i < Nvalues; i++) {
     
    147147  SecFilt *out;
    148148
    149   ALLOCATE (out, SecFilt, Nvalues);
     149  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    150150
    151151  for (i = 0; i < Nvalues; i++) {
     
    169169  SecFilt_PS1_REF *out;
    170170
    171   ALLOCATE (out, SecFilt_PS1_REF, Nvalues);
     171  ALLOCATE_ZERO (out, SecFilt_PS1_REF, Nvalues);
    172172
    173173  for (i = 0; i < Nvalues; i++) {
     
    183183  Image *out;
    184184
    185   ALLOCATE (out, Image, Nvalues);
     185  ALLOCATE_ZERO (out, Image, Nvalues);
    186186
    187187  for (i = 0; i < Nvalues; i++) {
     
    218218    out[i].sourceID         = in[i].sourceID;
    219219
    220     out[i].order            = in[i].order;
    221     out[i].Mx               = in[i].Mx;
    222     out[i].My               = in[i].My;
    223     out[i].Mxx              = in[i].Mxx;
    224     out[i].Mxy              = in[i].Mxy;
    225     out[i].Myy              = in[i].Myy;
    226     out[i].Mxxx             = in[i].Mxxx;
    227     out[i].Mxxy             = in[i].Mxxy;
    228     out[i].Mxyy             = in[i].Mxyy;
    229     out[i].Myyy             = in[i].Myyy;
    230     out[i].Mxxxx            = in[i].Mxxxx;
    231     out[i].Mxxxy            = in[i].Mxxxy;
    232     out[i].Mxxyy            = in[i].Mxxyy;
    233     out[i].Mxyyy            = in[i].Mxyyy;
    234     out[i].Myyyy            = in[i].Myyyy;
     220    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     221    // with the following.  (no real databases used those values -- see
     222    // libdvo/doc/dvo-images.txt)
     223    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     224    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     225    out[i].dummy3           = in[i].dummy3;
     226    out[i].dXpixSys         = in[i].dXpixSys;
     227    out[i].dYpixSys         = in[i].dYpixSys;
     228    out[i].dMagSys          = in[i].dMagSys;
     229    out[i].nFitAstrom       = in[i].nFitAstrom;
     230    out[i].nFitPhotom       = in[i].nFitPhotom;
     231    out[i].photom_map_id    = in[i].photom_map_id;
     232    out[i].astrom_map_id    = in[i].astrom_map_id;
    235233
    236234    // changed or added for PS1_V2
     
    247245  Image_PS1_REF *out;
    248246
    249   ALLOCATE (out, Image_PS1_REF, Nvalues);
     247  ALLOCATE_ZERO (out, Image_PS1_REF, Nvalues);
    250248
    251249  for (i = 0; i < Nvalues; i++) {
     
    282280    out[i].sourceID         = in[i].sourceID;
    283281
    284     out[i].order            = in[i].order;
    285     out[i].Mx               = in[i].Mx;
    286     out[i].My               = in[i].My;
    287     out[i].Mxx              = in[i].Mxx;
    288     out[i].Mxy              = in[i].Mxy;
    289     out[i].Myy              = in[i].Myy;
    290     out[i].Mxxx             = in[i].Mxxx;
    291     out[i].Mxxy             = in[i].Mxxy;
    292     out[i].Mxyy             = in[i].Mxyy;
    293     out[i].Myyy             = in[i].Myyy;
    294     out[i].Mxxxx            = in[i].Mxxxx;
    295     out[i].Mxxxy            = in[i].Mxxxy;
    296     out[i].Mxxyy            = in[i].Mxxyy;
    297     out[i].Mxyyy            = in[i].Mxyyy;
    298     out[i].Myyyy            = in[i].Myyyy;
     282    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     283    // with the following.  (no real databases used those values -- see
     284    // libdvo/doc/dvo-images.txt)
     285    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     286    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     287    out[i].dummy3           = in[i].dummy3;
     288    out[i].dXpixSys         = in[i].dXpixSys;
     289    out[i].dYpixSys         = in[i].dYpixSys;
     290    out[i].dMagSys          = in[i].dMagSys;
     291    out[i].nFitAstrom       = in[i].nFitAstrom;
     292    out[i].nFitPhotom       = in[i].nFitPhotom;
     293    out[i].photom_map_id    = in[i].photom_map_id;
     294    out[i].astrom_map_id    = in[i].astrom_map_id;
    299295  }
    300296  return (out);
     
    306302  PhotCode *out;
    307303
    308   ALLOCATE (out, PhotCode, Nvalues);
     304  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    309305
    310306  for (i = 0; i < Nvalues; i++) {
     
    342338  PhotCode_PS1_REF *out;
    343339
    344   ALLOCATE (out, PhotCode_PS1_REF, Nvalues);
     340  ALLOCATE_ZERO (out, PhotCode_PS1_REF, Nvalues);
    345341
    346342  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_V1.c

    r27580 r30631  
    88  Measure *out;
    99
    10   ALLOCATE (out, Measure, Nvalues);
     10  ALLOCATE_ZERO (out, Measure, Nvalues);
    1111
    1212  for (i = 0; i < Nvalues; i++) {
     
    6161  Measure_PS1_V1 *out;
    6262
    63   ALLOCATE (out, Measure_PS1_V1, Nvalues);
     63  ALLOCATE_ZERO (out, Measure_PS1_V1, Nvalues);
    6464
    6565  for (i = 0; i < Nvalues; i++) {
     
    116116  Average *out;
    117117
    118   ALLOCATE (out, Average, Nvalues);
     118  ALLOCATE_ZERO (out, Average, Nvalues);
    119119
    120120  for (i = 0; i < Nvalues; i++) {
     
    156156  Average_PS1_V1 *out;
    157157
    158   ALLOCATE (out, Average_PS1_V1, Nvalues);
     158  ALLOCATE_ZERO (out, Average_PS1_V1, Nvalues);
    159159
    160160  for (i = 0; i < Nvalues; i++) {
     
    191191  SecFilt *out;
    192192
    193   ALLOCATE (out, SecFilt, Nvalues);
     193  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    194194
    195195  for (i = 0; i < Nvalues; i++) {
     
    213213  SecFilt_PS1_V1 *out;
    214214
    215   ALLOCATE (out, SecFilt_PS1_V1, Nvalues);
     215  ALLOCATE_ZERO (out, SecFilt_PS1_V1, Nvalues);
    216216
    217217  for (i = 0; i < Nvalues; i++) {
     
    232232  Image *out;
    233233
    234   ALLOCATE (out, Image, Nvalues);
     234  ALLOCATE_ZERO (out, Image, Nvalues);
    235235
    236236  for (i = 0; i < Nvalues; i++) {
     
    267267    out[i].sourceID         = in[i].sourceID;
    268268
    269     out[i].order            = in[i].order;
    270     out[i].Mx               = in[i].Mx;
    271     out[i].My               = in[i].My;
    272     out[i].Mxx              = in[i].Mxx;
    273     out[i].Mxy              = in[i].Mxy;
    274     out[i].Myy              = in[i].Myy;
    275     out[i].Mxxx             = in[i].Mxxx;
    276     out[i].Mxxy             = in[i].Mxxy;
    277     out[i].Mxyy             = in[i].Mxyy;
    278     out[i].Myyy             = in[i].Myyy;
    279     out[i].Mxxxx            = in[i].Mxxxx;
    280     out[i].Mxxxy            = in[i].Mxxxy;
    281     out[i].Mxxyy            = in[i].Mxxyy;
    282     out[i].Mxyyy            = in[i].Mxyyy;
    283     out[i].Myyyy            = in[i].Myyyy;
     269    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     270    // with the following.  (no real databases used those values -- see
     271    // libdvo/doc/dvo-images.txt)
     272    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     273    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     274    out[i].dummy3           = in[i].dummy3;
     275    out[i].dXpixSys         = in[i].dXpixSys;
     276    out[i].dYpixSys         = in[i].dYpixSys;
     277    out[i].dMagSys          = in[i].dMagSys;
     278    out[i].nFitAstrom       = in[i].nFitAstrom;
     279    out[i].nFitPhotom       = in[i].nFitPhotom;
     280    out[i].photom_map_id    = in[i].photom_map_id;
     281    out[i].astrom_map_id    = in[i].astrom_map_id;
    284282
    285283    // changed or added for PS1_V2
     
    297295  Image_PS1_V1 *out;
    298296
    299   ALLOCATE (out, Image_PS1_V1, Nvalues);
     297  ALLOCATE_ZERO (out, Image_PS1_V1, Nvalues);
    300298
    301299  for (i = 0; i < Nvalues; i++) {
     
    333331    out[i].sourceID         = in[i].sourceID;
    334332
    335     out[i].order            = in[i].order;
    336     out[i].Mx               = in[i].Mx;
    337     out[i].My               = in[i].My;
    338     out[i].Mxx              = in[i].Mxx;
    339     out[i].Mxy              = in[i].Mxy;
    340     out[i].Myy              = in[i].Myy;
    341     out[i].Mxxx             = in[i].Mxxx;
    342     out[i].Mxxy             = in[i].Mxxy;
    343     out[i].Mxyy             = in[i].Mxyy;
    344     out[i].Myyy             = in[i].Myyy;
    345     out[i].Mxxxx            = in[i].Mxxxx;
    346     out[i].Mxxxy            = in[i].Mxxxy;
    347     out[i].Mxxyy            = in[i].Mxxyy;
    348     out[i].Mxyyy            = in[i].Mxyyy;
    349     out[i].Myyyy            = in[i].Myyyy;
     333    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     334    // with the following.  (no real databases used those values -- see
     335    // libdvo/doc/dvo-images.txt)
     336    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     337    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     338    out[i].dummy3           = in[i].dummy3;
     339    out[i].dXpixSys         = in[i].dXpixSys;
     340    out[i].dYpixSys         = in[i].dYpixSys;
     341    out[i].dMagSys          = in[i].dMagSys;
     342    out[i].nFitAstrom       = in[i].nFitAstrom;
     343    out[i].nFitPhotom       = in[i].nFitPhotom;
     344    out[i].photom_map_id    = in[i].photom_map_id;
     345    out[i].astrom_map_id    = in[i].astrom_map_id;
    350346  }
    351347  return (out);
     
    357353  PhotCode *out;
    358354
    359   ALLOCATE (out, PhotCode, Nvalues);
     355  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    360356
    361357  for (i = 0; i < Nvalues; i++) {
     
    393389  PhotCode_PS1_V1 *out;
    394390
    395   ALLOCATE (out, PhotCode_PS1_V1, Nvalues);
     391  ALLOCATE_ZERO (out, PhotCode_PS1_V1, Nvalues);
    396392
    397393  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_PS1_V2.c

    r27580 r30631  
    88  Measure *out;
    99
    10   ALLOCATE (out, Measure, Nvalues);
     10  ALLOCATE_ZERO (out, Measure, Nvalues);
    1111
    1212  for (i = 0; i < Nvalues; i++) {
     
    4747    out[i].dXccd      = in[i].dXccd;
    4848    out[i].dYccd      = in[i].dYccd;
     49    out[i].dRsys      = in[i].dRsys;
    4950    out[i].posangle   = in[i].posangle;
    5051    out[i].pltscale   = in[i].pltscale;
     
    6162  Measure_PS1_V2 *out;
    6263
    63   ALLOCATE (out, Measure_PS1_V2, Nvalues);
     64  ALLOCATE_ZERO (out, Measure_PS1_V2, Nvalues);
    6465
    6566  for (i = 0; i < Nvalues; i++) {
     
    100101    out[i].dXccd      = in[i].dXccd;
    101102    out[i].dYccd      = in[i].dYccd;
    102     memset(out[i].pad, 0, sizeof(out[i].pad));
     103    out[i].dRsys      = in[i].dRsys;
    103104    out[i].posangle   = in[i].posangle;
    104105    out[i].pltscale   = in[i].pltscale;
     
    116117  Average *out;
    117118
    118   ALLOCATE (out, Average, Nvalues);
     119  ALLOCATE_ZERO (out, Average, Nvalues);
    119120
    120121  for (i = 0; i < Nvalues; i++) {
     
    156157  Average_PS1_V2 *out;
    157158
    158   ALLOCATE (out, Average_PS1_V2, Nvalues);
     159  ALLOCATE_ZERO (out, Average_PS1_V2, Nvalues);
    159160
    160161  for (i = 0; i < Nvalues; i++) {
     
    195196  SecFilt *out;
    196197
    197   ALLOCATE (out, SecFilt, Nvalues);
     198  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    198199
    199200  for (i = 0; i < Nvalues; i++) {
     
    215216  SecFilt_PS1_V2 *out;
    216217
    217   ALLOCATE (out, SecFilt_PS1_V2, Nvalues);
     218  ALLOCATE_ZERO (out, SecFilt_PS1_V2, Nvalues);
    218219
    219220  for (i = 0; i < Nvalues; i++) {
     
    235236  Image *out;
    236237
    237   ALLOCATE (out, Image, Nvalues);
     238  ALLOCATE_ZERO (out, Image, Nvalues);
    238239
    239240  for (i = 0; i < Nvalues; i++) {
     
    275276    out[i].sourceID         = in[i].sourceID;
    276277
    277     out[i].order            = in[i].order;
    278     out[i].Mx               = in[i].Mx;
    279     out[i].My               = in[i].My;
    280     out[i].Mxx              = in[i].Mxx;
    281     out[i].Mxy              = in[i].Mxy;
    282     out[i].Myy              = in[i].Myy;
    283     out[i].Mxxx             = in[i].Mxxx;
    284     out[i].Mxxy             = in[i].Mxxy;
    285     out[i].Mxyy             = in[i].Mxyy;
    286     out[i].Myyy             = in[i].Myyy;
    287     out[i].Mxxxx            = in[i].Mxxxx;
    288     out[i].Mxxxy            = in[i].Mxxxy;
    289     out[i].Mxxyy            = in[i].Mxxyy;
    290     out[i].Mxyyy            = in[i].Mxyyy;
    291     out[i].Myyyy            = in[i].Myyyy;
     278    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     279    // with the following.  (no real databases used those values -- see
     280    // libdvo/doc/dvo-images.txt)
     281    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     282    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     283    out[i].dummy3           = in[i].dummy3;
     284    out[i].dXpixSys         = in[i].dXpixSys;
     285    out[i].dYpixSys         = in[i].dYpixSys;
     286    out[i].dMagSys          = in[i].dMagSys;
     287    out[i].nFitAstrom       = in[i].nFitAstrom;
     288    out[i].nFitPhotom       = in[i].nFitPhotom;
     289    out[i].photom_map_id    = in[i].photom_map_id;
     290    out[i].astrom_map_id    = in[i].astrom_map_id;
    292291  }
    293292  return (out);
     
    299298  Image_PS1_V2 *out;
    300299
    301   ALLOCATE (out, Image_PS1_V2, Nvalues);
     300  ALLOCATE_ZERO (out, Image_PS1_V2, Nvalues);
    302301
    303302  for (i = 0; i < Nvalues; i++) {
     
    339338    out[i].sourceID         = in[i].sourceID;
    340339
    341     out[i].order            = in[i].order;
    342     out[i].Mx               = in[i].Mx;
    343     out[i].My               = in[i].My;
    344     out[i].Mxx              = in[i].Mxx;
    345     out[i].Mxy              = in[i].Mxy;
    346     out[i].Myy              = in[i].Myy;
    347     out[i].Mxxx             = in[i].Mxxx;
    348     out[i].Mxxy             = in[i].Mxxy;
    349     out[i].Mxyy             = in[i].Mxyy;
    350     out[i].Myyy             = in[i].Myyy;
    351     out[i].Mxxxx            = in[i].Mxxxx;
    352     out[i].Mxxxy            = in[i].Mxxxy;
    353     out[i].Mxxyy            = in[i].Mxxyy;
    354     out[i].Mxyyy            = in[i].Mxyyy;
    355     out[i].Myyyy            = in[i].Myyyy;
     340    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     341    // with the following.  (no real databases used those values -- see
     342    // libdvo/doc/dvo-images.txt)
     343    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     344    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     345    out[i].dummy3           = in[i].dummy3;
     346    out[i].dXpixSys         = in[i].dXpixSys;
     347    out[i].dYpixSys         = in[i].dYpixSys;
     348    out[i].dMagSys          = in[i].dMagSys;
     349    out[i].nFitAstrom       = in[i].nFitAstrom;
     350    out[i].nFitPhotom       = in[i].nFitPhotom;
     351    out[i].photom_map_id    = in[i].photom_map_id;
     352    out[i].astrom_map_id    = in[i].astrom_map_id;
    356353  }
    357354  return (out);
     
    363360  PhotCode *out;
    364361
    365   ALLOCATE (out, PhotCode, Nvalues);
     362  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    366363
    367364  for (i = 0; i < Nvalues; i++) {
     
    399396  PhotCode_PS1_V2 *out;
    400397
    401   ALLOCATE (out, PhotCode_PS1_V2, Nvalues);
     398  ALLOCATE_ZERO (out, PhotCode_PS1_V2, Nvalues);
    402399
    403400  for (i = 0; i < Nvalues; i++) {
     
    407404    out[i].code  = in[i].code;         
    408405    out[i].type  = in[i].type;         
    409     memset(out[i].dummy, 0, sizeof(out[i].dummy));
    410406    out[i].C     = in[i].C;           
    411407    out[i].dC    = in[i].dC;           
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_elixir.c

    r27580 r30631  
    77  Measure *out;
    88
    9   ALLOCATE (out, Measure, Nvalues);
     9  ALLOCATE_ZERO (out, Measure, Nvalues);
    1010
    1111  for (i = 0; i < Nvalues; i++) {
     
    7474  Measure_Elixir *out;
    7575
    76   ALLOCATE (out, Measure_Elixir, Nvalues);
     76  ALLOCATE_ZERO (out, Measure_Elixir, Nvalues);
    7777
    7878  for (i = 0; i < Nvalues; i++) {
     
    113113  Average *out;
    114114
    115   ALLOCATE (out, Average, Nvalues);
    116   ALLOCATE (*primary, SecFilt, Nvalues);
     115  ALLOCATE_ZERO (out, Average, Nvalues);
     116  ALLOCATE_ZERO (*primary, SecFilt, Nvalues);
    117117
    118118  for (i = 0; i < Nvalues; i++) {
     
    168168  Average_Elixir *out;
    169169
    170   ALLOCATE (out, Average_Elixir, Nvalues);
     170  ALLOCATE_ZERO (out, Average_Elixir, Nvalues);
    171171
    172172  for (i = 0; i < Nvalues; i++) {
     
    198198  SecFilt *out;
    199199
    200   ALLOCATE (out, SecFilt, Nvalues);
     200  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    201201
    202202  for (i = 0; i < Nvalues; i++) {
     
    225225  SecFilt_Elixir *out;
    226226
    227   ALLOCATE (out, SecFilt_Elixir, Nvalues);
     227  ALLOCATE_ZERO (out, SecFilt_Elixir, Nvalues);
    228228
    229229  for (i = 0; i < Nvalues; i++) {
     
    243243  Image *out;
    244244
    245   ALLOCATE (out, Image, Nvalues);
     245  ALLOCATE_ZERO (out, Image, Nvalues);
    246246
    247247  for (i = 0; i < Nvalues; i++) {
     
    266266    out[i].ccdnum           = in[i].ccdnum;
    267267
    268     out[i].order            = in[i].order;
    269     out[i].Mx               = in[i].Mx;
    270     out[i].My               = in[i].My;
    271     out[i].Mxx              = in[i].Mxx;
    272     out[i].Mxy              = in[i].Mxy;
    273     out[i].Myy              = in[i].Myy;
    274     out[i].Mxxx             = in[i].Mxxx;
    275     out[i].Mxxy             = in[i].Mxxy;
    276     out[i].Mxyy             = in[i].Mxyy;
    277     out[i].Myyy             = in[i].Myyy;
    278     out[i].Mxxxx            = in[i].Mxxxx;
    279     out[i].Mxxxy            = in[i].Mxxxy;
    280     out[i].Mxxyy            = in[i].Mxxyy;
    281     out[i].Mxyyy            = in[i].Mxyyy;
    282     out[i].Myyyy            = in[i].Myyyy;
     268    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     269    // with the following.  (no real databases used those values -- see
     270    // libdvo/doc/dvo-images.txt)
     271    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     272    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     273    out[i].dummy3           = in[i].dummy3;
     274    out[i].dXpixSys         = in[i].dXpixSys;
     275    out[i].dYpixSys         = in[i].dYpixSys;
     276    out[i].dMagSys          = in[i].dMagSys;
     277    out[i].nFitAstrom       = in[i].nFitAstrom;
     278    out[i].nFitPhotom       = in[i].nFitPhotom;
     279    out[i].photom_map_id    = in[i].photom_map_id;
     280    out[i].astrom_map_id    = in[i].astrom_map_id;
    283281
    284282    // added or changed for PANSTARRS_DEV_0
     
    314312  Image_Elixir *out;
    315313
    316   ALLOCATE (out, Image_Elixir, Nvalues);
     314  ALLOCATE_ZERO (out, Image_Elixir, Nvalues);
    317315
    318316  for (i = 0; i < Nvalues; i++) {
     
    338336    out[i].ccdnum           = in[i].ccdnum;
    339337
    340     out[i].order            = in[i].order;
    341     out[i].Mx               = in[i].Mx;
    342     out[i].My               = in[i].My;
    343     out[i].Mxx              = in[i].Mxx;
    344     out[i].Mxy              = in[i].Mxy;
    345     out[i].Myy              = in[i].Myy;
    346     out[i].Mxxx             = in[i].Mxxx;
    347     out[i].Mxxy             = in[i].Mxxy;
    348     out[i].Mxyy             = in[i].Mxyy;
    349     out[i].Myyy             = in[i].Myyy;
    350     out[i].Mxxxx            = in[i].Mxxxx;
    351     out[i].Mxxxy            = in[i].Mxxxy;
    352     out[i].Mxxyy            = in[i].Mxxyy;
    353     out[i].Mxyyy            = in[i].Mxyyy;
    354     out[i].Myyyy            = in[i].Myyyy;
     338    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     339    // with the following.  (no real databases used those values -- see
     340    // libdvo/doc/dvo-images.txt)
     341    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     342    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     343    out[i].dummy3           = in[i].dummy3;
     344    out[i].dXpixSys         = in[i].dXpixSys;
     345    out[i].dYpixSys         = in[i].dYpixSys;
     346    out[i].dMagSys          = in[i].dMagSys;
     347    out[i].nFitAstrom       = in[i].nFitAstrom;
     348    out[i].nFitPhotom       = in[i].nFitPhotom;
     349    out[i].photom_map_id    = in[i].photom_map_id;
     350    out[i].astrom_map_id    = in[i].astrom_map_id;
    355351
    356352    // added or changed for PANSTARRS_DEV_0
     
    372368  PhotCode *out;
    373369
    374   ALLOCATE (out, PhotCode, Nvalues);
     370  ALLOCATE_ZERO (out, PhotCode, Nvalues);
    375371
    376372  for (i = 0; i < Nvalues; i++) {
     
    412408  PhotCode_Elixir *out;
    413409
    414   ALLOCATE (out, PhotCode_Elixir, Nvalues);
     410  ALLOCATE_ZERO (out, PhotCode_Elixir, Nvalues);
    415411
    416412  for (i = 0; i < Nvalues; i++) {
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_loneos.c

    r27580 r30631  
    77  Measure *out;
    88
    9   ALLOCATE (out, Measure, Nvalues);
     9  ALLOCATE_ZERO (out, Measure, Nvalues);
    1010
    1111  for (i = 0; i < Nvalues; i++) {
     
    7676  Measure_Loneos *out;
    7777
    78   ALLOCATE (out, Measure_Loneos, Nvalues);
     78  ALLOCATE_ZERO (out, Measure_Loneos, Nvalues);
    7979
    8080  for (i = 0; i < Nvalues; i++) {
     
    104104  Average *out;
    105105
    106   ALLOCATE (out, Average, Nvalues);
    107   ALLOCATE (*primary, SecFilt, Nvalues);
     106  ALLOCATE_ZERO (out, Average, Nvalues);
     107  ALLOCATE_ZERO (*primary, SecFilt, Nvalues);
    108108
    109109  for (i = 0; i < Nvalues; i++) {
     
    160160  Average_Loneos *out;
    161161
    162   ALLOCATE (out, Average_Loneos, Nvalues);
     162  ALLOCATE_ZERO (out, Average_Loneos, Nvalues);
    163163
    164164  for (i = 0; i < Nvalues; i++) {
     
    189189  SecFilt *out;
    190190
    191   ALLOCATE (out, SecFilt, Nvalues);
     191  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    192192
    193193  for (i = 0; i < Nvalues; i++) {
     
    218218  SecFilt_Loneos *out;
    219219
    220   ALLOCATE (out, SecFilt_Loneos, Nvalues);
     220  ALLOCATE_ZERO (out, SecFilt_Loneos, Nvalues);
    221221
    222222  for (i = 0; i < Nvalues; i++) {
     
    235235  Image *out;
    236236
    237   ALLOCATE (out, Image, Nvalues);
     237  ALLOCATE_ZERO (out, Image, Nvalues);
    238238
    239239  for (i = 0; i < Nvalues; i++) {
     
    259259    out[i].ccdnum           = in[i].ccdnum;
    260260
    261     out[i].order            = in[i].order;
    262     out[i].Mx               = in[i].Mx;
    263     out[i].My               = in[i].My;
    264     out[i].Mxx              = in[i].Mxx;
    265     out[i].Mxy              = in[i].Mxy;
    266     out[i].Myy              = in[i].Myy;
    267     out[i].Mxxx             = in[i].Mxxx;
    268     out[i].Mxxy             = in[i].Mxxy;
    269     out[i].Mxyy             = in[i].Mxyy;
    270     out[i].Myyy             = in[i].Myyy;
    271     out[i].Mxxxx            = in[i].Mxxxx;
    272     out[i].Mxxxy            = in[i].Mxxxy;
    273     out[i].Mxxyy            = in[i].Mxxyy;
    274     out[i].Mxyyy            = in[i].Mxyyy;
    275     out[i].Myyyy            = in[i].Myyyy;
     261    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     262    // with the following.  (no real databases used those values -- see
     263    // libdvo/doc/dvo-images.txt)
     264    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     265    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     266    out[i].dummy3           = in[i].dummy3;
     267    out[i].dXpixSys         = in[i].dXpixSys;
     268    out[i].dYpixSys         = in[i].dYpixSys;
     269    out[i].dMagSys          = in[i].dMagSys;
     270    out[i].nFitAstrom       = in[i].nFitAstrom;
     271    out[i].nFitPhotom       = in[i].nFitPhotom;
     272    out[i].photom_map_id    = in[i].photom_map_id;
     273    out[i].astrom_map_id    = in[i].astrom_map_id;
    276274
    277275    // added or changed for PANSTARRS_DEV_0
     
    307305  Image_Loneos *out;
    308306
    309   ALLOCATE (out, Image_Loneos, Nvalues);
     307  ALLOCATE_ZERO (out, Image_Loneos, Nvalues);
    310308
    311309  for (i = 0; i < Nvalues; i++) {
     
    331329    out[i].ccdnum           = in[i].ccdnum;
    332330
    333     out[i].order            = in[i].order;
    334     out[i].Mx               = in[i].Mx;
    335     out[i].My               = in[i].My;
    336     out[i].Mxx              = in[i].Mxx;
    337     out[i].Mxy              = in[i].Mxy;
    338     out[i].Myy              = in[i].Myy;
    339     out[i].Mxxx             = in[i].Mxxx;
    340     out[i].Mxxy             = in[i].Mxxy;
    341     out[i].Mxyy             = in[i].Mxyy;
    342     out[i].Myyy             = in[i].Myyy;
    343     out[i].Mxxxx            = in[i].Mxxxx;
    344     out[i].Mxxxy            = in[i].Mxxxy;
    345     out[i].Mxxyy            = in[i].Mxxyy;
    346     out[i].Mxyyy            = in[i].Mxyyy;
    347     out[i].Myyyy            = in[i].Myyyy;
     331    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     332    // with the following.  (no real databases used those values -- see
     333    // libdvo/doc/dvo-images.txt)
     334    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     335    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     336    out[i].dummy3           = in[i].dummy3;
     337    out[i].dXpixSys         = in[i].dXpixSys;
     338    out[i].dYpixSys         = in[i].dYpixSys;
     339    out[i].dMagSys          = in[i].dMagSys;
     340    out[i].nFitAstrom       = in[i].nFitAstrom;
     341    out[i].nFitPhotom       = in[i].nFitPhotom;
     342    out[i].photom_map_id    = in[i].photom_map_id;
     343    out[i].astrom_map_id    = in[i].astrom_map_id;
    348344
    349345    // added or changed for PANSTARRS_DEV_0
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c

    r27580 r30631  
    77  Measure *out;
    88
    9   ALLOCATE (out, Measure, Nvalues);
     9  ALLOCATE_ZERO (out, Measure, Nvalues);
    1010
    1111  for (i = 0; i < Nvalues; i++) {
     
    7171  Measure_Panstarrs_DEV_0 *out;
    7272
    73   ALLOCATE (out, Measure_Panstarrs_DEV_0, Nvalues);
     73  ALLOCATE_ZERO (out, Measure_Panstarrs_DEV_0, Nvalues);
    7474
    7575  for (i = 0; i < Nvalues; i++) {
     
    120120  Average *out;
    121121
    122   ALLOCATE (out, Average, Nvalues);
     122  ALLOCATE_ZERO (out, Average, Nvalues);
    123123
    124124  for (i = 0; i < Nvalues; i++) {
     
    165165  Average_Panstarrs_DEV_0 *out;
    166166
    167   ALLOCATE (out, Average_Panstarrs_DEV_0, Nvalues);
     167  ALLOCATE_ZERO (out, Average_Panstarrs_DEV_0, Nvalues);
    168168
    169169  for (i = 0; i < Nvalues; i++) {
     
    200200  SecFilt *out;
    201201
    202   ALLOCATE (out, SecFilt, Nvalues);
     202  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    203203
    204204  for (i = 0; i < Nvalues; i++) {
     
    225225  SecFilt_Panstarrs_DEV_0 *out;
    226226
    227   ALLOCATE (out, SecFilt_Panstarrs_DEV_0, Nvalues);
     227  ALLOCATE_ZERO (out, SecFilt_Panstarrs_DEV_0, Nvalues);
    228228
    229229  for (i = 0; i < Nvalues; i++) {
     
    243243  Image *out;
    244244
    245   ALLOCATE (out, Image, Nvalues);
     245  ALLOCATE_ZERO (out, Image, Nvalues);
    246246
    247247  for (i = 0; i < Nvalues; i++) {
     
    273273    out[i].ccdnum           = in[i].ccdnum;
    274274
    275     out[i].order            = in[i].order;
    276     out[i].Mx               = in[i].Mx;
    277     out[i].My               = in[i].My;
    278     out[i].Mxx              = in[i].Mxx;
    279     out[i].Mxy              = in[i].Mxy;
    280     out[i].Myy              = in[i].Myy;
    281     out[i].Mxxx             = in[i].Mxxx;
    282     out[i].Mxxy             = in[i].Mxxy;
    283     out[i].Mxyy             = in[i].Mxyy;
    284     out[i].Myyy             = in[i].Myyy;
    285     out[i].Mxxxx            = in[i].Mxxxx;
    286     out[i].Mxxxy            = in[i].Mxxxy;
    287     out[i].Mxxyy            = in[i].Mxxyy;
    288     out[i].Mxyyy            = in[i].Mxyyy;
    289     out[i].Myyyy            = in[i].Myyyy;
     275    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     276    // with the following.  (no real databases used those values -- see
     277    // libdvo/doc/dvo-images.txt)
     278    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     279    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     280    out[i].dummy3           = in[i].dummy3;
     281    out[i].dXpixSys         = in[i].dXpixSys;
     282    out[i].dYpixSys         = in[i].dYpixSys;
     283    out[i].dMagSys          = in[i].dMagSys;
     284    out[i].nFitAstrom       = in[i].nFitAstrom;
     285    out[i].nFitPhotom       = in[i].nFitPhotom;
     286    out[i].photom_map_id    = in[i].photom_map_id;
     287    out[i].astrom_map_id    = in[i].astrom_map_id;
    290288
    291289    // changed or added for PS1_DEV_1
     
    314312  Image_Panstarrs_DEV_0 *out;
    315313
    316   ALLOCATE (out, Image_Panstarrs_DEV_0, Nvalues);
     314  ALLOCATE_ZERO (out, Image_Panstarrs_DEV_0, Nvalues);
    317315
    318316  for (i = 0; i < Nvalues; i++) {
     
    344342    out[i].ccdnum           = in[i].ccdnum;
    345343
    346     out[i].order            = in[i].order;
    347     out[i].Mx               = in[i].Mx;
    348     out[i].My               = in[i].My;
    349     out[i].Mxx              = in[i].Mxx;
    350     out[i].Mxy              = in[i].Mxy;
    351     out[i].Myy              = in[i].Myy;
    352     out[i].Mxxx             = in[i].Mxxx;
    353     out[i].Mxxy             = in[i].Mxxy;
    354     out[i].Mxyy             = in[i].Mxyy;
    355     out[i].Myyy             = in[i].Myyy;
    356     out[i].Mxxxx            = in[i].Mxxxx;
    357     out[i].Mxxxy            = in[i].Mxxxy;
    358     out[i].Mxxyy            = in[i].Mxxyy;
    359     out[i].Mxyyy            = in[i].Mxyyy;
    360     out[i].Myyyy            = in[i].Myyyy;
     344    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     345    // with the following.  (no real databases used those values -- see
     346    // libdvo/doc/dvo-images.txt)
     347    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     348    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     349    out[i].dummy3           = in[i].dummy3;
     350    out[i].dXpixSys         = in[i].dXpixSys;
     351    out[i].dYpixSys         = in[i].dYpixSys;
     352    out[i].dMagSys          = in[i].dMagSys;
     353    out[i].nFitAstrom       = in[i].nFitAstrom;
     354    out[i].nFitPhotom       = in[i].nFitPhotom;
     355    out[i].photom_map_id    = in[i].photom_map_id;
     356    out[i].astrom_map_id    = in[i].astrom_map_id;
    361357
    362358    // changed or added for PS1_DEV_1
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_1.c

    r27580 r30631  
    77  Measure *out;
    88
    9   ALLOCATE (out, Measure, Nvalues);
     9  ALLOCATE_ZERO (out, Measure, Nvalues);
    1010
    1111  for (i = 0; i < Nvalues; i++) {
     
    7171  Measure_Panstarrs_DEV_1 *out;
    7272
    73   ALLOCATE (out, Measure_Panstarrs_DEV_1, Nvalues);
     73  ALLOCATE_ZERO (out, Measure_Panstarrs_DEV_1, Nvalues);
    7474
    7575  for (i = 0; i < Nvalues; i++) {
     
    120120  Average *out;
    121121
    122   ALLOCATE (out, Average, Nvalues);
     122  ALLOCATE_ZERO (out, Average, Nvalues);
    123123
    124124  for (i = 0; i < Nvalues; i++) {
     
    165165  Average_Panstarrs_DEV_1 *out;
    166166
    167   ALLOCATE (out, Average_Panstarrs_DEV_1, Nvalues);
     167  ALLOCATE_ZERO (out, Average_Panstarrs_DEV_1, Nvalues);
    168168
    169169  for (i = 0; i < Nvalues; i++) {
     
    200200  SecFilt *out;
    201201
    202   ALLOCATE (out, SecFilt, Nvalues);
     202  ALLOCATE_ZERO (out, SecFilt, Nvalues);
    203203
    204204  for (i = 0; i < Nvalues; i++) {
     
    225225  SecFilt_Panstarrs_DEV_1 *out;
    226226
    227   ALLOCATE (out, SecFilt_Panstarrs_DEV_1, Nvalues);
     227  ALLOCATE_ZERO (out, SecFilt_Panstarrs_DEV_1, Nvalues);
    228228
    229229  for (i = 0; i < Nvalues; i++) {
     
    243243  Image *out;
    244244
    245   ALLOCATE (out, Image, Nvalues);
     245  ALLOCATE_ZERO (out, Image, Nvalues);
    246246
    247247  for (i = 0; i < Nvalues; i++) {
     
    273273    out[i].ccdnum           = in[i].ccdnum;
    274274
    275     out[i].order            = in[i].order;
    276     out[i].Mx               = in[i].Mx;
    277     out[i].My               = in[i].My;
    278     out[i].Mxx              = in[i].Mxx;
    279     out[i].Mxy              = in[i].Mxy;
    280     out[i].Myy              = in[i].Myy;
    281     out[i].Mxxx             = in[i].Mxxx;
    282     out[i].Mxxy             = in[i].Mxxy;
    283     out[i].Mxyy             = in[i].Mxyy;
    284     out[i].Myyy             = in[i].Myyy;
    285     out[i].Mxxxx            = in[i].Mxxxx;
    286     out[i].Mxxxy            = in[i].Mxxxy;
    287     out[i].Mxxyy            = in[i].Mxxyy;
    288     out[i].Mxyyy            = in[i].Mxyyy;
    289     out[i].Myyyy            = in[i].Myyyy;
     275    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     276    // with the following.  (no real databases used those values -- see
     277    // libdvo/doc/dvo-images.txt)
     278    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     279    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     280    out[i].dummy3           = in[i].dummy3;
     281    out[i].dXpixSys         = in[i].dXpixSys;
     282    out[i].dYpixSys         = in[i].dYpixSys;
     283    out[i].dMagSys          = in[i].dMagSys;
     284    out[i].nFitAstrom       = in[i].nFitAstrom;
     285    out[i].nFitPhotom       = in[i].nFitPhotom;
     286    out[i].photom_map_id    = in[i].photom_map_id;
     287    out[i].astrom_map_id    = in[i].astrom_map_id;
    290288
    291289    // changed or added for PS1_DEV_1
     
    314312  Image_Panstarrs_DEV_1 *out;
    315313
    316   ALLOCATE (out, Image_Panstarrs_DEV_1, Nvalues);
     314  ALLOCATE_ZERO (out, Image_Panstarrs_DEV_1, Nvalues);
    317315
    318316  for (i = 0; i < Nvalues; i++) {
     
    344342    out[i].ccdnum           = in[i].ccdnum;
    345343
    346     out[i].order            = in[i].order;
    347     out[i].Mx               = in[i].Mx;
    348     out[i].My               = in[i].My;
    349     out[i].Mxx              = in[i].Mxx;
    350     out[i].Mxy              = in[i].Mxy;
    351     out[i].Myy              = in[i].Myy;
    352     out[i].Mxxx             = in[i].Mxxx;
    353     out[i].Mxxy             = in[i].Mxxy;
    354     out[i].Mxyy             = in[i].Mxyy;
    355     out[i].Myyy             = in[i].Myyy;
    356     out[i].Mxxxx            = in[i].Mxxxx;
    357     out[i].Mxxxy            = in[i].Mxxxy;
    358     out[i].Mxxyy            = in[i].Mxxyy;
    359     out[i].Mxyyy            = in[i].Mxyyy;
    360     out[i].Myyyy            = in[i].Myyyy;
     344    // as of 2011.02.03, the old Mx,My,..., Mxxxx,Myyyy have been deprecated and replaced
     345    // with the following.  (no real databases used those values -- see
     346    // libdvo/doc/dvo-images.txt)
     347    out[i].nLinkAstrom      = in[i].nLinkAstrom;
     348    out[i].nLinkPhotom      = in[i].nLinkPhotom;
     349    out[i].dummy3           = in[i].dummy3;
     350    out[i].dXpixSys         = in[i].dXpixSys;
     351    out[i].dYpixSys         = in[i].dYpixSys;
     352    out[i].dMagSys          = in[i].dMagSys;
     353    out[i].nFitAstrom       = in[i].nFitAstrom;
     354    out[i].nFitPhotom       = in[i].nFitPhotom;
     355    out[i].photom_map_id    = in[i].photom_map_id;
     356    out[i].astrom_map_id    = in[i].astrom_map_id;
    361357
    362358    // changed or added for PS1_DEV_1
  • branches/czw_branch/20101203/Ohana/src/libdvo/src/photfits.c

    r15509 r30631  
    22# define MAX_ORDER 3
    33
    4 /**** XXXX warning: these functions have not been corrected to handle
    5       the change of Mcal from millimags to mags
    6       I don't think most functions are using the Mcal polynomial terms in
    7       any case... ****/
    8 
    9 /* all terms of order > 0 are stored as 16bit floats
    10    the zero-order term is stored as a short int (-32k,+32k) */
     4/*** This file contains deprecated functions which were used to implement the 2D zero point variations per image.
     5     This code has not been used for a long time, and is not up-to-date wrt the change from
     6     millimag shorts to mag floats for values in the db tables.  As of 2011.02.03, these
     7     structures are no longer used.  There is now an (as yet unused) index in the image table
     8     to a zero-point map table.  Someday, we may need similarly named functions to interact
     9     with the ZP map.  for now, this file is not used in the build.
     10***/
    1111
    1212/* convert double to low-precision short int */
  • branches/czw_branch/20101203/Ohana/src/libkapa/src/DrawRotString.c

    r10073 r30631  
    2727  unsigned char *bitmap;
    2828  char *currentname, basename[64];
    29   int i, dy, dx, N, X, Y, code;
     29  int i, dy, dx, N, X, Y, code, protect;
    3030  int dX, Xoff, dY, Yoff, YoffBase;
    3131  int currentsize, basesize;
     
    6363
    6464  code = FALSE;
     65  protect = FALSE;
    6566
    6667  YoffBase = Yoff;
     
    7071    if ((N < 0) || (N >= NROTCHARS)) continue;
    7172
     73    if (N == 39) { // single-quote
     74      protect = protect ? FALSE : TRUE;
     75    }
     76
    7277    /* check for special characters */
    73     if (!code) {
     78    if (!code && !protect) {
    7479      if (N == 94) {
    7580        SetRotFont (currentname, (int)(0.8*currentsize));
  • branches/czw_branch/20101203/Ohana/src/libkapa/src/PSRotFont.c

    r10073 r30631  
    77
    88  char *segment, basename[64], *currentname;
    9   int i, N, code;
     9  int i, N, code, protect;
    1010  int dX, dY, Xoff, Yoff, X, Y, Nseg, NSEG, YoffBase;
    1111  double cs, sn, fscale, currentscale;
     
    6060
    6161  code = FALSE;
     62  protect = FALSE;
     63
    6264  YoffBase = 0;
    6365  /* accumulate string segments with common state */
     
    6668    if ((N < 0) || (N >= NROTCHARS)) continue;
    6769
     70    if (N == 39) { // single-quote
     71      protect = protect ? FALSE : TRUE;
     72    }
     73
    6874    /* check for special characters */
    69     if (!code) {
     75    if (!code && !protect) {
    7076      /* superscript character (^) */
    7177      if (N == 94) {
  • branches/czw_branch/20101203/Ohana/src/libkapa/src/bDrawRotFont.c

    r30118 r30631  
    1414  unsigned char *bitmap;
    1515  char *currentname, basename[64];
    16   int i, dy, dx, N, X, Y, code;
     16  int i, dy, dx, N, X, Y, code, protect;
    1717  int dX, Xoff, dY, Yoff, YoffBase;
    1818  int currentsize, basesize;
     
    5353
    5454  code = FALSE;
     55  protect = FALSE;
    5556
    5657  YoffBase = Yoff;
     
    6061    if ((N < 0) || (N >= NROTCHARS)) continue;
    6162
     63    if (N == 39) { // single-quote
     64      protect = protect ? FALSE : TRUE;
     65    }
     66
    6267    /* check for special characters */
    63     if (!code) {
     68    if (!code && !protect) {
    6469      if (N == 94) {
    6570        SetRotFont (currentname, (int)(0.8*currentsize));
  • branches/czw_branch/20101203/Ohana/src/libohana/include/ohana.h

    r29537 r30631  
    283283int     ohana_str_to_radec     PROTO((double *ra, double *dec, char *str1, char *str2));
    284284double  ohana_normalize_angle  PROTO((double angle));
     285double  ohana_normalize_angle_to_midpoint  PROTO((double angle, double Rmid));
    285286
    286287int     hstgsc_hms_to_deg      PROTO((double *h0, double *h1, double *d0, double *d1, char *string));
     288
     289short   ToShortPixels          PROTO((float pixels));
     290short   ToShortDegrees         PROTO((float degrees));
     291float   FromShortPixels        PROTO((short value));
     292float   FromShortDegrees       PROTO((float value));
    287293
    288294/* IO Buffer functions */
  • branches/czw_branch/20101203/Ohana/src/libohana/include/ohana_allocate.h

    r12332 r30631  
    1717
    1818# define ALLOCATE(PTR,TYPE,SIZE) \
    19   PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE))
     19  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE)) }
     20# define ALLOCATE_ZERO(PTR,TYPE,SIZE)                                   \
     21  { PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE)); memset (PTR, 0, (SIZE)*sizeof(TYPE)); }
    2022# define REALLOCATE(PTR,TYPE,SIZE) \
    21   PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE));
    22 # define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) \
    23   if ((NCURR) >= (SIZE)) { SIZE += DELTA; \
    24   PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); }
    25 # define FREE(PTR) if (PTR != NULL) { ohana_free (__FILE__, __LINE__, PTR); }
    26 # define free(PTR) ohana_free(__FILE__, __LINE__, PTR)
     23  { PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); }
     24# define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) {                  \
     25  if ((NCURR) >= (SIZE)) { SIZE += DELTA;                               \
     26    PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); } }
     27# define FREE(PTR) { if (PTR != NULL) { ohana_free (__FILE__, __LINE__, PTR); } }
     28# define free(PTR) { ohana_free(__FILE__, __LINE__, PTR); }
    2729
    2830# else
     
    3335
    3436# ifndef ALLOCATE
    35 # define ALLOCATE(PTR,TYPE,SIZE)  \
    36   PTR = (TYPE *) malloc ((unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
    37   if (PTR == NULL) { \
    38     fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\
    39     exit (10); }
    40 # define REALLOCATE(PTR,TYPE,SIZE) \
     37# define ALLOCATE(PTR,TYPE,SIZE) {                                      \
     38  PTR = (TYPE *) malloc ((size_t)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
     39  if (PTR == NULL) {                                                    \
     40    fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);  \
     41    exit (10); } }             
     42
     43# define ALLOCATE_ZERO(PTR,TYPE,SIZE) {                                 \
     44  PTR = (TYPE *) malloc ((size_t)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
     45  if (PTR == NULL) {                                                    \
     46    fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);  \
     47    exit (10); \
     48  } memset (PTR, 0, (SIZE)*sizeof(TYPE)); }
     49
     50# define REALLOCATE(PTR,TYPE,SIZE) {                                    \
    4151  PTR = (TYPE *) realloc(PTR,(unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
    42   if (PTR == NULL) { \
    43     fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\
    44     exit (10); }
    45 # define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) \
    46   if ((NCURR) >= (SIZE)) { \
    47     SIZE += DELTA; \
     52  if (PTR == NULL) {                                                    \
     53    fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__); \
     54    exit (10); } }
     55
     56# define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) {  \
     57  if ((NCURR) >= (SIZE)) {                              \
     58    SIZE += DELTA;                                                      \
    4859    PTR = (TYPE *) realloc(PTR,(unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
    49     if (PTR == NULL) { \
    50       fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\
    51       exit (10); } }
    52 # define FREE(PTR) if (PTR != NULL) { free (PTR); }
     60    if (PTR == NULL) {                                                  \
     61      fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__); \
     62      exit (10); } } }
     63
     64# define FREE(PTR) { if (PTR != NULL) { free (PTR); } }
    5365# endif /* ALLOCATE */
    5466
  • branches/czw_branch/20101203/Ohana/src/libohana/src/time.c

    r19935 r30631  
    468468    return (result);
    469469}
     470
     471double ohana_normalize_angle_to_midpoint (double angle, double Rmid) {
     472
     473    double result;
     474
     475    // take an input angle and force the domain to be 0.0 - 360.0
     476    // there are a few ways to do this: 
     477
     478    // option 1: This is a potentially very slow method, also subject to round off errors
     479# if (0)
     480    while (angle > 360.0) angle -= 360.0;
     481    while (angle <   0.0) angle += 360.0;
     482# endif
     483   
     484    // option 2: take sin & cos, apply atan2 (y, x)
     485# if (1)   
     486    double x, y;
     487
     488    x = cos(angle*RAD_DEG);
     489    y = sin(angle*RAD_DEG);
     490
     491    result = DEG_RAD*atan2 (y, x);
     492    if (result < Rmid - 180.0) result += 360.0;
     493    if (result > Rmid + 180.0) result -= 360.0;
     494# endif
     495
     496# if (0)
     497    // option 3:
     498    int nCircle = angle / 360.0;
     499    result -= 360.0*nCircle;
     500    if (result < 0.0) result += 360.0;
     501# endif
     502
     503    return (result);
     504}
     505
     506short ToShortPixels (float pixels) {
     507
     508    short value;
     509
     510    value = 100*pixels;
     511
     512    return value;
     513}
     514
     515short ToShortDegrees (float degrees) {
     516
     517    short value;
     518
     519    value = ((float)0xffff/360.0) * degrees;
     520
     521    return value;
     522}
     523
     524float FromShortPixels (short value) {
     525
     526    float pixels;
     527
     528    pixels = value / 100.0;
     529
     530    return pixels;
     531}
     532
     533float FromShortDegrees (float value) {
     534
     535    float degrees;
     536
     537    degrees = (360.0 / (float)0xffff) * value;
     538
     539    return degrees;
     540}
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.astro/cgrid.c

    r29001 r30631  
    9898
    9999  if (argc != 1) {
    100     gprint (GP_ERR, "USAGE: cgrid [style]\n");
     100    gprint (GP_ERR, "USAGE: cgrid [style] [-ra-by-hour] [-labels]\n");
    101101    return (FALSE);
    102102  }
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.astro/region.c

    r30118 r30631  
    1818  FREE (name);
    1919
     20  NoClear = FALSE;
     21  if ((N = get_argument (argc, argv, "-no-clear"))) {
     22    remove_argument (N, &argc, argv);
     23    NoClear = TRUE;
     24  }
     25
    2026  if ((N = get_argument (argc, argv, "-image"))) {
    2127    remove_argument (N, &argc, argv);
    2228    KapaGetImageCoords (kapa, &graphmode.coords);
    2329    KapaGetImageRange (kapa, &graphmode.xmin, &graphmode.xmax, &graphmode.ymax, &graphmode.ymin);
     30
     31    set_variable ("XMIN", graphmode.xmin);
     32    set_variable ("XMAX", graphmode.xmax);
     33    set_variable ("YMIN", graphmode.ymin);
     34    set_variable ("YMAX", graphmode.ymax);
     35
     36    set_variable ("RMIN", Ra  + graphmode.xmin);
     37    set_variable ("RMAX", Ra  + graphmode.xmax);
     38    set_variable ("DMIN", Dec + graphmode.ymin);
     39    set_variable ("DMAX", Dec + graphmode.ymax);
     40
     41    // if (!NoClear) KapaClearSections (kapa);
     42    KapaSetLimits (kapa, &graphmode);
     43
    2444    SetGraph (&graphmode);
    2545    return (TRUE);
     
    4565    remove_argument (N, &argc, argv);
    4666    graphmode.flipnorth = FALSE;
    47   }
    48 
    49   NoClear = FALSE;
    50   if ((N = get_argument (argc, argv, "-no-clear"))) {
    51     remove_argument (N, &argc, argv);
    52     NoClear = TRUE;
    5367  }
    5468
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/limits.c

    r29540 r30631  
    2626
    2727  // XXX need an option to set the limits based on the current image bounds
     28  if ((N = get_argument (argc, argv, "-image"))) {
     29    remove_argument (N, &argc, argv);
     30    KapaGetImageRange (kapa, &graphmode.xmin, &graphmode.xmax, &graphmode.ymax, &graphmode.ymin);
     31
     32    set_variable ("XMIN", graphmode.xmin);
     33    set_variable ("XMAX", graphmode.xmax);
     34    set_variable ("YMIN", graphmode.ymin);
     35    set_variable ("YMAX", graphmode.ymax);
     36
     37    // if (!NoClear) KapaClearSections (kapa);
     38    SetGraph (&graphmode);
     39    KapaSetLimits (kapa, &graphmode);
     40    return (TRUE);
     41    // Set Region based on image
     42  }
    2843
    2944  if (argc == 1) {
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/match2d.c

    r27817 r30631  
    11# include "data.h"
     2
    23int find_matches2d (Vector *X1, Vector *Y1, Vector *X2, Vector *Y2, double Radius, Vector *index1, Vector *index2);
     4int find_matches2d_closest (Vector *X1, Vector *Y1, Vector *X2, Vector *Y2, double Radius, Vector *index);
    35
    46// match2d (X1) (Y1) (X2) (Y2) (Radius) [-index1 (index1)] [-index2 (index2)] [-nomatch1 nomatch1] [-nomatch2 nomatch2]
     
    68int match2d (int argc, char **argv) {
    79 
    8   int N;
     10  int N, CLOSEST;
    911  double Radius;
    1012  char *endptr;
     
    1214  Vector *index1, *index2;
    1315
     16  CLOSEST = FALSE;
     17  if ((N = get_argument (argc, argv, "-closest"))) {
     18    remove_argument (N, &argc, argv);
     19    CLOSEST = TRUE;
     20  }
     21
    1422  if ((N = get_argument (argc, argv, "-index1"))) {
    1523    remove_argument (N, &argc, argv);
     
    3038  if (argc != 6) {
    3139    gprint (GP_ERR, "USAGE: match2d X1 Y1 X2 Y2 Radius [-index1 (index1)] [-index2 (index2)] [-closest]\n");
    32     // gprint (GP_ERR, "USAGE: match2d X1 Y1 X2 Y2 Radius [-index1 (index1)] [-index2 (index2)] [-closest]\n");
    33     // gprint (GP_ERR, "  if -closest is provided, index1 & index2 will have the same length as X1 and X2 (respectively)\n");
    34     // gprint (GP_ERR, "    with either the index of the match or a value of -1 for non-matches\n");
    35     return (FALSE);
    36   }
     40    gprint (GP_ERR, "  if -closest is provided, index1 & index2 will have the same length as X1 and X2 (respectively)\n");
     41    gprint (GP_ERR, "    with either the index of the match or a value of -1 for non-matches\n");
     42    return (FALSE);
     43  }
     44
     45  /*
     46    we have two modes of operation: 
     47
     48    without -closest, we are finding all matched pairs within the match radius.  in this
     49    case, the two index vectors have the same length, one entry per matched pair.
     50    x1[index1],y1[index1] matches to x2[index2],y2[index2].
     51
     52    with -closest selected, we are finding the closest element of set 1 to each of set 2
     53    and vice versa.  in this case, index1 is always the same length as x1,y1, while index2
     54    is the same lengths as x2,y2.  x2[index1],y2[index1] matches x1,y1 while
     55    x1[index2],y1[index2] matches x2,y2
     56
     57   */
    3758
    3859  if ((X1vec = SelectVector (argv[1], OLDVECTOR, TRUE)) == NULL) return (FALSE);   
     
    6182  }
    6283
    63   find_matches2d (X1vec, Y1vec, X2vec, Y2vec, Radius, index1, index2);
     84  if (CLOSEST) {
     85      find_matches2d_closest (X1vec, Y1vec, X2vec, Y2vec, Radius, index1);
     86      find_matches2d_closest (X2vec, Y2vec, X1vec, Y1vec, Radius, index2);
     87  } else {
     88      find_matches2d (X1vec, Y1vec, X2vec, Y2vec, Radius, index1, index2);
     89  }
    6490
    6591  return (TRUE);
     
    132158  return (TRUE);
    133159}
     160
     161// find the elements of X2,Y2 which are closest to each element of X1,Y1 (-1 if no match)
     162int find_matches2d_closest (Vector *X1, Vector *Y1, Vector *X2, Vector *Y2, double Radius, Vector *index) {
     163 
     164  off_t i, j, Jmin, Ji, I, J, *N1, *N2, NMATCH;
     165  double dX, dY, dR, Radius2, Rmin;
     166
     167  NMATCH = X1->Nelements;
     168  ResetVector (index, OPIHI_INT, NMATCH);
     169
     170  ALLOCATE (N1, off_t, X1->Nelements);
     171  ALLOCATE (N2, off_t, X2->Nelements);
     172
     173  for (i = 0; i < X1->Nelements; i++) { N1[i] = i; }
     174  for (i = 0; i < X2->Nelements; i++) { N2[i] = i; }
     175
     176  sort_coords_indexonly (X1->elements.Flt, Y1->elements.Flt, N1, X1->Nelements);
     177  sort_coords_indexonly (X2->elements.Flt, Y2->elements.Flt, N2, X2->Nelements);
     178
     179  Radius2 = Radius*Radius;
     180
     181  // find the closest entry in list 2 to the current entry in list 1:
     182  for (i = j = 0; (i < X1->Nelements) && (j < X2->Nelements);) {
     183    I = N1[i];
     184    J = N2[j];
     185
     186    dX = X1->elements.Flt[I] - X2->elements.Flt[J];
     187
     188    if (dX <= -1.02*Radius) {
     189      // no match in list 2 to this entry
     190      index->elements.Int[I] = -1;
     191      i++;
     192      continue;
     193    }
     194    if (dX >= +1.02*Radius) { j++; continue; }
     195
     196    // look for closest matches of list2() to list1(i)
     197    Jmin = -1;
     198    Rmin = Radius2;
     199    for (Ji = j; (dX > -1.02*Radius) && (Ji < X2->Nelements); Ji++) {
     200      J = N2[Ji];
     201      dX = X1->elements.Flt[I] - X2->elements.Flt[J];
     202      dY = Y1->elements.Flt[I] - Y2->elements.Flt[J];
     203      dR = dX*dX + dY*dY;
     204      if (dR > Radius2) continue;
     205      if (dR < Rmin) {
     206        Rmin = dR;
     207        Jmin  = J;
     208      }
     209    }
     210
     211    // no match in list 2 to this entry
     212    if (Jmin == -1) {
     213      index->elements.Int[I] = -1;
     214      i++;
     215      continue;
     216    }
     217    index->elements.Int[I] = Jmin;
     218    i++;
     219  }
     220  index->Nelements = NMATCH;
     221
     222  free (N1);
     223  free (N2);
     224
     225  return (TRUE);
     226}
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/reindex.c

    r29540 r30631  
    55int reindex (int argc, char **argv) {
    66 
    7   int  i, Npts, Nmax, valid;
     7  int  i, Npts, Nmax;
    88  Vector *ivec, *ovec, *xvec;
    99
     
    1111  Npts = 0;
    1212
    13   valid = TRUE;
    14   valid &= (argc >= 6);
    15   valid &= !strcmp(argv[2], "=");
    16   valid &= !strcmp(argv[4], "using");
    17   if (!valid) {
    18     gprint (GP_ERR, "USAGE: reindex (out) = (in) using (index)\n");
    19     gprint (GP_ERR, "  creates a new vectors (out) from (in) based on sequence in (index)\n");
    20     return (FALSE);
    21   }
     13  if (argc != 6) goto usage;
     14  if (strcmp(argv[2], "=")) goto usage;
     15  if (strcmp(argv[4], "using")) goto usage;
    2216
    2317  if ((ovec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) goto error;
     
    6357  DeleteVector (ovec);
    6458  return (FALSE);
     59
     60usage:
     61    gprint (GP_ERR, "USAGE: reindex (out) = (in) using (index)\n");
     62    gprint (GP_ERR, "  creates a new vectors (out) from (in) based on sequence in (index)\n");
     63    return (FALSE);
    6564}
    6665
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/subset.c

    r27491 r30631  
    3434
    3535  // ovec matches ivec in type
    36   ResetVector (ovec, ivec->type, MAX (tvec[0].Nelements, 1));
     36  ResetVector (ovec, ivec->type, tvec[0].Nelements);
    3737
    3838  // we have four cases: (ivec == flt or int) and (tvec == flt or int)
     
    7575
    7676  // free up unused memory
    77   ResetVector (ovec, ivec->type, MAX (Npts, 1));
     77  ResetVector (ovec, ivec->type, Npts);
    7878
    7979  DeleteVector (tvec);
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/vstats.c

    r21508 r30631  
    33int vstats (int argc, char **argv) {
    44 
    5   int i, N;
    6   double max, min, sum, var, dvar, mean, stdev;
    7   float IgnoreValue;
     5  int i, iter, N, Nbin, Niter;
     6  double max, min, pmin, pmax, sum, var, dvar, mean, stdev;
     7  float IgnoreValue, Nsigma;
    88  int Ignore, Quiet;
    99
    10   int *Nval, bin, Nmode, Nmed;
    11   double dx, mode, median;
     10  int *Nval, bin, Nmode, Nmed, Nused;
     11  double dx, mode, median, threshold;
    1212  Vector *vec;
     13  char *mask = NULL;
    1314
    1415  IgnoreValue = 0;
     
    2122  }
    2223
     24  Niter = 1;
     25  Nsigma = 3.0;
     26
     27  if ((N = get_argument (argc, argv, "-sigma-clip"))) {
     28    remove_argument (N, &argc, argv);
     29    Nsigma = atof(argv[N]);
     30    Niter = 3;
     31    remove_argument (N, &argc, argv);
     32  }
     33
     34  if ((N = get_argument (argc, argv, "-iter"))) {
     35    remove_argument (N, &argc, argv);
     36    Niter = atoi(argv[N]);
     37    remove_argument (N, &argc, argv);
     38  }
     39
    2340  Quiet = FALSE;
    2441  if ((N = get_argument (argc, argv, "-q"))) {
     
    3249
    3350  if (argc != 2) {
    34     gprint (GP_ERR, "USAGE: vstat (vector)\n");
     51    gprint (GP_ERR, "USAGE: vstat (vector) [-ignore value] [-q] [-quiet] [-iter Niter] [-sigma-clip Nsigma]\n");
     52    gprint (GP_ERR, "  default is 1 iteration without sigma clipping or 3 with sigma clipping\n");
    3553    return (FALSE);
    3654  }
     
    3957
    4058  /* we need two passes, one for max, min, mean, sum, one for median, stdev, etc */
     59
     60  // set a good / bad mask
     61  ALLOCATE (mask, char, vec[0].Nelements);
     62  if (vec[0].type == OPIHI_FLT) {
     63    opihi_flt *X = vec[0].elements.Flt;
     64    for (i = 0; i < vec[0].Nelements; i++, X++) {
     65      mask[i] = 1;
     66      if (!finite (*X)) continue;
     67      if (Ignore && (*X == IgnoreValue)) continue;
     68      mask[i] = 0;
     69    }     
     70  } else {
     71    opihi_int *X = vec[0].elements.Int;
     72    for (i = 0; i < vec[0].Nelements; i++, X++) {
     73      mask[i] = 1;
     74      if (!finite (*X)) continue;
     75      if (Ignore && (*X == IgnoreValue)) continue;
     76      mask[i] = 0;
     77    }     
     78  }
    4179
    4280  /* calculate max, min, mean, sum, npix */
     
    4785    opihi_flt *X = vec[0].elements.Flt;
    4886    for (i = 0; i < vec[0].Nelements; i++, X++) {
    49       if (!finite (*X)) continue;
    50       if (Ignore && (*X == IgnoreValue)) continue;
     87      if (mask[i]) continue;
    5188      max = MAX (*X, max);
    5289      min = MIN (*X, min);
     
    5794    opihi_int *X = vec[0].elements.Int;
    5895    for (i = 0; i < vec[0].Nelements; i++, X++) {
    59       if (!finite (*X)) continue;
    60       if (Ignore && (*X == IgnoreValue)) continue;
     96      if (mask[i]) continue;
    6197      max = MAX (*X, max);
    6298      min = MIN (*X, min);
     
    67103  mean = sum / N;
    68104
    69   /* calculate median and mode with resolution of (max - min) / 1000 */
    70   dx = (max - min) / 1000;
    71   if (dx == 0) {
    72     median = mode = min;
    73     stdev = 0.0;
    74     goto skip;
    75   }
    76 
    77   ALLOCATE (Nval, int, 1002);
    78   bzero (Nval, 1000*sizeof(int));
    79   var = 0;
    80   if (vec[0].type == OPIHI_FLT) {
    81     opihi_flt *X = vec[0].elements.Flt;
    82     for (i = 0; i < vec[0].Nelements; i++, X++) {
    83       if (!finite (*X)) continue;
    84       if (Ignore && (*X == IgnoreValue)) continue;
    85       bin = MAX (0, MIN (1000, (*X - min) / dx));
    86       Nval[bin] ++;
    87       dvar = (*X - mean);
    88       var += dvar*dvar;
    89     }     
    90   } else {
    91     opihi_int *X = vec[0].elements.Int;
    92     for (i = 0; i < vec[0].Nelements; i++, X++) {
    93       if (!finite (*X)) continue;
    94       if (Ignore && (*X == IgnoreValue)) continue;
    95       bin = MAX (0, MIN (1000, (*X - min) / dx));
    96       Nval[bin] ++;
    97       dvar = (*X - mean);
    98       var += dvar*dvar;
    99     }     
    100   }
    101   stdev = sqrt (var / N);
    102 
    103   Nmode = 0;
    104   mode = Nval[Nmode];
    105   median = 0;
    106   Nmed = -1;
    107   for (i = 0; i < 1001; i++) {
    108     if (Nmed == -1) {
    109       median += Nval[i];
    110       if (median >= N / 2.0) {
    111         Nmed = i;
    112         median = i * dx + min;
     105  // we do Niter passes; on each pass, we exclude entries > Nsigma from the median
     106  pmin = min;
     107  pmax = max;
     108
     109  stdev = mode = median = Nused = 0.0;
     110
     111  for (iter = 0; iter < Niter; iter ++) {
     112
     113    // reduce Nbin after the first iteration?
     114    Nbin = 1000;
     115
     116    /* calculate median and mode with resolution of (max - min) / 1000 */
     117    dx = (pmax - pmin) / Nbin;
     118    if (dx == 0) {
     119      median = mode = min;
     120      stdev = 0.0;
     121      goto skip;
     122    }
     123
     124    ALLOCATE (Nval, int, Nbin + 2);
     125    bzero (Nval, Nbin*sizeof(int));
     126    var = 0;
     127    if (vec[0].type == OPIHI_FLT) {
     128      opihi_flt *X = vec[0].elements.Flt;
     129      for (i = 0; i < vec[0].Nelements; i++, X++) {
     130        if (mask[i]) continue;
     131        bin = MAX (0, MIN (Nbin, (*X - pmin) / dx));
     132        Nval[bin] ++;
     133        dvar = (*X - mean);
     134        var += dvar*dvar;
     135      }     
     136    } else {
     137      opihi_int *X = vec[0].elements.Int;
     138      for (i = 0; i < vec[0].Nelements; i++, X++) {
     139        if (mask[i]) continue;
     140        bin = MAX (0, MIN (Nbin, (*X - pmin) / dx));
     141        Nval[bin] ++;
     142        dvar = (*X - mean);
     143        var += dvar*dvar;
     144      }     
     145    }
     146    stdev = sqrt (var / N);
     147
     148    Nmode = 0;
     149    mode = Nval[Nmode];
     150    median = 0;
     151    Nmed = -1;
     152    for (i = 0; i < Nbin + 1; i++) {
     153      if (Nmed == -1) {
     154        median += Nval[i];
     155        if (median >= N / 2.0) {
     156          Nmed = i;
     157          median = i * dx + pmin;
     158        }
    113159      }
    114     }
    115     if (mode < Nval[i]) {
    116       Nmode = i;
    117       mode = Nval[Nmode];
    118     }
    119   }
    120   mode = Nmode * dx + min;
    121   free (Nval);
     160      if (mode < Nval[i]) {
     161        Nmode = i;
     162        mode = Nval[Nmode];
     163      }
     164    }
     165    mode = Nmode * dx + pmin;
     166    free (Nval);
     167
     168    threshold = Nsigma * stdev;
     169
     170    // we are going to do another pass: mark the entries to skip
     171    pmin = max;
     172    pmax = min;
     173    if (iter < Niter - 1) {
     174      if (vec[0].type == OPIHI_FLT) {
     175        opihi_flt *X = vec[0].elements.Flt;
     176        for (i = 0; i < vec[0].Nelements; i++, X++) {
     177          if (mask[i]) continue;
     178          if (fabs(*X - median) > threshold) {
     179            mask[i] = 1;
     180          } else {
     181            pmin = MIN (*X, pmin);
     182            pmax = MAX (*X, pmax);
     183          }     
     184        }
     185      } else {
     186        opihi_int *X = vec[0].elements.Int;
     187        for (i = 0; i < vec[0].Nelements; i++, X++) {
     188          if (mask[i]) continue;
     189          if (fabs(*X - median) > threshold) {
     190            mask[i] = 1;
     191          } else {
     192            pmin = MIN (*X, pmin);
     193            pmax = MAX (*X, pmax);
     194          }
     195        }     
     196      }
     197    }
     198    // gprint (GP_ERR, "iter %d, mean: %g, stdev: %g, min: %g, max: %g, median: %g, mode: %g, Npts: %d\n",
     199    // iter, mean, stdev, min, max, median, mode, N);
     200  }
     201
     202  Nused = 0;
     203  for (i = 0; i < vec[0].Nelements; i++) {
     204    if (mask[i]) continue;
     205    Nused ++;
     206  }
    122207
    123208skip:
     209  FREE(mask);
     210
    124211  if (!Quiet) {
    125212    gprint (GP_ERR, "mean: %g, stdev: %g, min: %g, max: %g, median: %g, mode: %g, Npts: %d\n",
    126              mean, stdev, min, max, median, mode, N);
    127   }
    128 
     213            mean, stdev, min, max, median, mode, N);
     214  }
     215
     216  set_variable ("PMIN",      pmin);
     217  set_variable ("PMAX",      pmax);
    129218  set_variable ("MIN",      min);
    130219  set_variable ("MAX",      max);
     
    134223  set_variable ("TOTAL",    sum);
    135224  set_int_variable ("NPIX", N);
     225  set_int_variable ("NPTS", N);
     226  set_int_variable ("NUSED", Nused);
    136227  set_variable ("SIGMA",    stdev);
    137228  return (TRUE);
  • branches/czw_branch/20101203/Ohana/src/opihi/cmd.data/write_vectors.c

    r20936 r30631  
    140140      if (fmttype[j] == 'd') {
    141141        if (vec[j][0].type == OPIHI_FLT) {
    142           fprintf (f, fmtlist[j], (int)(vec[j][0].elements.Flt[i]));
     142          fprintf (f, fmtlist[j], (opihi_int)(vec[j][0].elements.Flt[i]));
    143143        } else {
    144           fprintf (f, fmtlist[j], (int)(vec[j][0].elements.Int[i]));
     144          fprintf (f, fmtlist[j], (opihi_int)(vec[j][0].elements.Int[i]));
    145145        }
    146146      }
    147147      if (fmttype[j] == 'f') {
    148148        if (vec[j][0].type == OPIHI_FLT) {
    149           fprintf (f, fmtlist[j], (float)(vec[j][0].elements.Flt[i]));
     149          fprintf (f, fmtlist[j], (opihi_flt)(vec[j][0].elements.Flt[i]));
    150150        } else {
    151           fprintf (f, fmtlist[j], (float)(vec[j][0].elements.Int[i]));
     151          fprintf (f, fmtlist[j], (opihi_flt)(vec[j][0].elements.Int[i]));
    152152        }
    153153      }
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/avextract.c

    r28241 r30631  
    4444
    4545  // parse skyregion options
    46   if ((selection = SetRegionSelection (&argc, argv)) == NULL) goto escape;
     46  if ((selection = SetRegionSelection (&argc, argv)) == NULL) {
     47    print_error();
     48    goto escape;
     49  }
    4750
    4851  // command-line is of the form: avextract field,field, field [where (field op value)...]
     
    6871  // construct the db Boolean math stack (frees cstack)
    6972  stack = dbRPN (Ncstack, cstack, &Nstack);
     73  if (Ncstack && !Nstack) {
     74    print_error();
     75    goto escape;
     76  }
    7077
    7178  // add the skyregion limits to the where statement (or create)
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/dbExtractImages.c

    r27587 r30631  
    241241
    242242    case IMAGE_NCAL:
    243       value.Int = image[N].Mxxxx;
     243      value.Int = image[N].nFitPhotom;
    244244      break;
    245245    case IMAGE_SKY:
    246       value.Flt = image[N].Myyyy + 0x8000;
     246      value.Flt = NAN;
    247247      break;
    248248    case IMAGE_FLAGS:
     
    299299      value.Flt = (field->ID == IMAGE_X_UR_FP) ? x : y;
    300300      break;
     301
     302    case IMAGE_X_ERR_SYS:
     303      value.Flt = image[N].dXpixSys;
     304      break;
     305    case IMAGE_Y_ERR_SYS:
     306      value.Flt = image[N].dXpixSys;
     307      break;
     308    case IMAGE_MAG_ERR_SYS:
     309      value.Flt = image[N].dMagSys;
     310      break;
     311
     312    case IMAGE_NFIT_PHOTOM:
     313      value.Int = image[N].nFitPhotom;
     314      break;
     315    case IMAGE_NFIT_ASTROM:
     316      value.Int = image[N].nFitAstrom;
     317      break;
     318    case IMAGE_NLINK_PHOTOM:
     319      value.Int = image[N].nLinkPhotom;
     320      break;
     321    case IMAGE_NLINK_ASTROM:
     322      value.Int = image[N].nLinkAstrom;
     323      break;
    301324  }
    302325  return (value);
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/dbExtractMeasures.c

    r28955 r30631  
    320320      break;
    321321    case MEAS_FWHM: /* OK */
    322       value.Flt = 0.01*(measure[0].FWx + measure[0].FWy) / 2.0;
     322      value.Flt = FromShortPixels(measure[0].FWx + measure[0].FWy) / 2.0;
    323323      break;
    324324    case MEAS_FWHM_MAJ: /* OK */
    325       value.Flt = 0.01*measure[0].FWx;
     325      value.Flt = FromShortPixels(measure[0].FWx);
    326326      break;
    327327    case MEAS_FWHM_MIN: /* OK */
    328       value.Flt = 0.01*measure[0].FWy;
     328      value.Flt = FromShortPixels(measure[0].FWy);
    329329      break;
    330330    case MEAS_THETA: /* OK */
    331       value.Flt = measure[0].theta;
     331      value.Flt = FromShortDegrees(measure[0].theta);
    332332      break;
    333333    case MEAS_MXX: /* OK */
    334       value.Flt = measure[0].Mxx;
     334      value.Flt = FromShortPixels(measure[0].Mxx);
    335335      break;
    336336    case MEAS_MXY: /* OK */
    337       value.Flt = measure[0].Mxy;
     337      value.Flt = FromShortPixels(measure[0].Mxy);
    338338      break;
    339339    case MEAS_MYY: /* OK */
    340       value.Flt = measure[0].Myy;
     340      value.Flt = FromShortPixels(measure[0].Myy);
    341341      break;
    342342    case MEAS_DOPHOT: /* OK */
     
    382382      break;
    383383    case MEAS_XCCD_ERR: /* OK */
    384       value.Flt = measure[0].dXccd / 100.0;
     384      value.Flt = FromShortPixels(measure[0].dXccd);
    385385      break;
    386386    case MEAS_YCCD_ERR: /* OK */
    387       value.Flt = measure[0].dYccd / 100.0;
     387      value.Flt = FromShortPixels(measure[0].dYccd);
     388      break;
     389    case MEAS_POS_SYS_ERR: /* OK */
     390      value.Flt = FromShortPixels(measure[0].dRsys);
    388391      break;
    389392    case MEAS_XMOSAIC: /* OK */
     
    425428      value.Flt = measure[0].psfQual;
    426429      break;
     430    case MEAS_PSF_QF_PERFECT: /* OK */
     431      value.Flt = NAN;
     432      break;
    427433    case MEAS_PSF_CHISQ: /* OK */
    428434      value.Flt = measure[0].psfChisq;
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/dbFields.c

    r28955 r30631  
    194194  if (!strcasecmp (fieldName, "XCCD:ERR"))       ESCAPE (MEAS_XCCD_ERR,       MAG_NONE, OPIHI_FLT);
    195195  if (!strcasecmp (fieldName, "YCCD:ERR"))       ESCAPE (MEAS_YCCD_ERR,       MAG_NONE, OPIHI_FLT);
     196  if (!strcasecmp (fieldName, "POS_SYS_ERR"))    ESCAPE (MEAS_POS_SYS_ERR,    MAG_NONE, OPIHI_FLT);
    196197  if (!strcasecmp (fieldName, "XMOSAIC"))        ESCAPE (MEAS_XMOSAIC,        MAG_NONE, OPIHI_FLT);
    197198  if (!strcasecmp (fieldName, "YMOSAIC"))        ESCAPE (MEAS_YMOSAIC,        MAG_NONE, OPIHI_FLT);
     
    205206  if (!strcasecmp (fieldName, "IMAGEID"))        ESCAPE (MEAS_IMAGE_ID,       MAG_NONE, OPIHI_INT);
    206207  if (!strcasecmp (fieldName, "PSF_QF"))         ESCAPE (MEAS_PSF_QF,         MAG_NONE, OPIHI_FLT);
     208  if (!strcasecmp (fieldName, "PSF_QF_PERFECT")) ESCAPE (MEAS_PSF_QF_PERFECT, MAG_NONE, OPIHI_FLT);
    207209  if (!strcasecmp (fieldName, "PSF_CHISQ"))      ESCAPE (MEAS_PSF_CHISQ,      MAG_NONE, OPIHI_FLT);
    208210  if (!strcasecmp (fieldName, "PSF_NDOF"))       ESCAPE (MEAS_PSF_NDOF,       MAG_NONE, OPIHI_INT);
     
    398400  if (!strcasecmp (fieldName, "trate"    )) ESCAPE (IMAGE_TRATE,     MAG_NONE, OPIHI_FLT);
    399401
    400   // XXX : these are mapped to ridiculous things: Myyyy + 0x8000 and Mxxxx
    401402  if (!strcasecmp (fieldName, "ncal"     )) ESCAPE (IMAGE_NCAL,      MAG_NONE, OPIHI_INT);
    402   if (!strcasecmp (fieldName, "sky"      )) ESCAPE (IMAGE_SKY,       MAG_NONE, OPIHI_FLT);
     403  if (!strcasecmp (fieldName, "sky"      )) ESCAPE (IMAGE_SKY,       MAG_NONE, OPIHI_FLT); // deprecated for now
    403404
    404405  if (!strcasecmp (fieldName, "imflags"  )) ESCAPE (IMAGE_FLAGS,     MAG_NONE, OPIHI_INT);
     
    427428  if (!strcasecmp (fieldName, "Y_UR_FP"  )) ESCAPE (IMAGE_Y_UR_FP,   MAG_NONE, OPIHI_FLT);
    428429
     430  if (!strcasecmp (fieldName, "dX_SYS"  )) ESCAPE (IMAGE_X_ERR_SYS,  MAG_NONE, OPIHI_FLT);
     431  if (!strcasecmp (fieldName, "dY_SYS"  )) ESCAPE (IMAGE_Y_ERR_SYS,  MAG_NONE, OPIHI_FLT);
     432  if (!strcasecmp (fieldName, "dM_SYS"  )) ESCAPE (IMAGE_MAG_ERR_SYS,MAG_NONE, OPIHI_FLT);
     433  if (!strcasecmp (fieldName, "NFIT_PHOTOM")) ESCAPE (IMAGE_NFIT_PHOTOM,MAG_NONE, OPIHI_INT);
     434  if (!strcasecmp (fieldName, "NFIT_ASTROM")) ESCAPE (IMAGE_NFIT_ASTROM,MAG_NONE, OPIHI_INT);
     435  if (!strcasecmp (fieldName, "NLINK_PHOTOM")) ESCAPE (IMAGE_NLINK_PHOTOM,MAG_NONE, OPIHI_INT);
     436  if (!strcasecmp (fieldName, "NLINK_ASTROM")) ESCAPE (IMAGE_NLINK_ASTROM,MAG_NONE, OPIHI_INT);
     437
    429438  // for words that don't parse, try a photcode
    430439  gprint (GP_ERR, "unknown field '%s' for image table in DVO database\n", fieldName);
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/gimages.c

    r28958 r30631  
    201201    }
    202202
    203     /*** XXX we need to re-introduce the use of applyMcal
    204     Mcal = applyMcal (&image[i], 2048.0, 2048.0);
    205     ***/
     203    // XXX Mcal = applyMcal (&image[i], 2048.0, 2048.0);
    206204
    207205  got_spot:
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/gstar.c

    r28326 r30631  
    222222            gprint (GP_LOG, "%5d ",    catalog.measure[m].photcode);
    223223            gprint (GP_LOG, "%-20s ",  GetPhotcodeNamebyCode (catalog.measure[m].photcode));
    224             gprint (GP_LOG, "%5.2f ",  0.01*catalog.measure[m].FWx);
    225             gprint (GP_LOG, "%5.2f ",  0.01*catalog.measure[m].FWy);
     224            gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[m].FWx));
     225            gprint (GP_LOG, "%5.2f ",  FromShortPixels(catalog.measure[m].FWy));
    226226
    227227            if (FULL_OUTPUT) {
     
    233233                gprint (GP_LOG, "%f ", catalog.measure[m].Xccd);
    234234                gprint (GP_LOG, "%f ", catalog.measure[m].Yccd);
    235                 gprint (GP_LOG, "%d ", catalog.measure[m].dXccd);
    236                 gprint (GP_LOG, "%d ", catalog.measure[m].dYccd);
     235                gprint (GP_LOG, "%f ", FromShortPixels(catalog.measure[m].dXccd));
     236                gprint (GP_LOG, "%f ", FromShortPixels(catalog.measure[m].dYccd));
    237237                gprint (GP_LOG, "%f ", catalog.measure[m].Sky);
    238238                gprint (GP_LOG, "%f ", catalog.measure[m].dSky);
     
    244244                gprint (GP_LOG, "%f ", catalog.measure[m].crNsigma);
    245245                gprint (GP_LOG, "%f ", catalog.measure[m].extNsigma);
    246                 gprint (GP_LOG, "%f ", (360.0/(float)0xffff)*catalog.measure[m].theta);
     246                gprint (GP_LOG, "%f ", FromShortDegrees(catalog.measure[m].theta));
    247247            }
    248248            gprint (GP_LOG, "\n");
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/images.c

    r30118 r30631  
    223223      goto got_type;
    224224    }
    225     // For 'TrianglePts' (TRI-), we are using the Mx,My, etc terms to save the coordinates
    226     // this means triangular images cannot carry photometric zero-point variations
     225    // For 'TrianglePts' (TRI-), we are absurdly overloaded values otherwise not used
    227226    if (typehash == TrianglePts) {
    228227      Npts = 3;
    229       x[0] = image[i].Mx;   y[0] = image[i].My;
    230       x[1] = image[i].Mxxx; y[1] = image[i].Mxyy;
    231       x[2] = image[i].Mxxy; y[2] = image[i].Myyy;
     228      x[0] = image[0].dXpixSys;      y[0] = image[0].dYpixSys;
     229      x[1] = image[0].dMagSys;       y[1] = image[0].nFitAstrom;
     230      x[2] = image[0].photom_map_id; y[2] = image[0].astrom_map_id;
    232231      goto got_type;
    233232    }
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/imextract.c

    r28958 r30631  
    6262  // construct the db Boolean math stack (frees cstack)
    6363  stack = dbRPN (Ncstack, cstack, &Nstack);
     64  if (Ncstack && !Nstack) {
     65    print_error();
     66    goto escape;
     67  }
    6468
    6569  // add the skyregion limits to the where statement (or create)
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/imphot.c

    r29540 r30631  
    99  char bufname[64];
    1010  float *p;
    11   double fx, fy, x, y, applyMcal();
     11  double fx, fy, x, y;
    1212  Image *image;
    1313  Buffer *buf;
     
    6363    for (y = 0; y < 200; y+=1.0) {
    6464      for (x = 0; x < 100; x+=1.0, p++) {
    65         *p = applyMcal (&image[subset[0]], (fx*x), (fy*y));
     65        // *p = applyMcal (&image[subset[0]], (fx*x), (fy*y));
     66        *p = image[subset[0]].Mcal;
    6667      }
    6768    }
     
    7071  for (j = 0; j < Nsubset; j++) {
    7172    i = subset[j];
     73    gprint (GP_ERR, "%s: %f\n", image[i].name, image[i].Mcal);
     74
     75// XXX old code when we had the option of a 2D zero point model
     76# if (0)     
    7277    switch (image[i].order) {
    7378    case 0:
     
    9095      break;
    9196    }
     97# endif
    9298  }
    9399
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/mextract.c

    r28955 r30631  
    6868  // construct the db Boolean math stack (frees cstack)
    6969  stack = dbRPN (Ncstack, cstack, &Nstack);
    70   if ((Ncstack > 0) && (Nstack < 1)) {
     70  if (Ncstack && !Nstack) {
    7171    print_error ();
    7272    goto escape;
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/mmextract.c

    r28241 r30631  
    106106  // construct the db Boolean math stack (frees cstack)
    107107  stack1 = dbRPN (Ncstack1, cstack1, &Nstack1);
    108   if ((Ncstack1 > 0) && (Nstack1 < 1)) {
     108  if (Ncstack1 && !Nstack1) {
    109109    print_error ();
    110110    goto escape;
     
    113113  // construct the db Boolean math stack (frees cstack)
    114114  stack2 = dbRPN (Ncstack2, cstack2, &Nstack2);
    115   if ((Ncstack2 > 0) && (Nstack2 < 1)) {
     115  if (Ncstack2 && !Nstack2) {
    116116    print_error ();
    117117    goto escape;
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/photometry.c

    r28955 r30631  
    778778  } else {
    779779   
    780     if (0.01 * measure[0].FWx < 3.0) return (FALSE);
    781     if (0.01 * measure[0].FWx > 10.0) return (FALSE);
     780    if (FromShortPixels(measure[0].FWx < 3.0)) return (FALSE);
     781    if (FromShortPixels(measure[0].FWx > 10.0)) return (FALSE);
    782782
    783783    return (TRUE);
     
    810810    Nm = 0;
    811811    for (i = 0; i < average[0].Nmeasure; i++) {
    812       fwhm = measure[i].FWx / 100.0;
     812      fwhm = FromShortPixels(measure[i].FWx);
    813813      switch (FWHMsign) {
    814814        case 0:
     
    11661166      break;
    11671167    case MEAS_FWHM: /* OK */
    1168       value = 0.01*measure[0].FWx;
     1168      value = FromShortPixels(measure[0].FWx);
    11691169      break;
    11701170    case MEAS_DB_FLAGS: /* ? */
  • branches/czw_branch/20101203/Ohana/src/opihi/dvo/skycat.c

    r20936 r30631  
    3636
    3737  if (argc != 1) {
    38     gprint (GP_ERR, "USAGE: skycat [-all]\n");
     38    gprint (GP_ERR, "USAGE: skycat [-all] [-depth depth] [-v]\n");
    3939    return (FALSE);
    4040  }
     
    116116
    117117  r = ohana_normalize_angle (r);
     118  while (r < Rmin) { r += 360.0; }
     119  while (r > Rmax) { r -= 360.0; }
    118120
    119121  if (*leftside == -1) {
  • branches/czw_branch/20101203/Ohana/src/opihi/include/dvoshell.h

    r28958 r30631  
    107107      MEAS_XCCD_ERR,
    108108      MEAS_YCCD_ERR,
     109      MEAS_POS_SYS_ERR,
    109110      MEAS_XMOSAIC,
    110111      MEAS_YMOSAIC,
     
    116117      MEAS_IMAGE_ID,
    117118      MEAS_PSF_QF,
     119      MEAS_PSF_QF_PERFECT,
    118120      MEAS_PSF_CHISQ,
    119121      MEAS_PSF_NDOF,
     
    218220      IMAGE_Y_UL_FP,
    219221      IMAGE_Y_UR_FP,
     222      IMAGE_X_ERR_SYS,
     223      IMAGE_Y_ERR_SYS,
     224      IMAGE_MAG_ERR_SYS,
     225      IMAGE_NFIT_PHOTOM,
     226      IMAGE_NFIT_ASTROM,
     227      IMAGE_NLINK_PHOTOM,
     228      IMAGE_NLINK_ASTROM
    220229};
    221230
  • branches/czw_branch/20101203/Ohana/src/opihi/include/shell.h

    r27790 r30631  
    7676char         *expand_vars               PROTO((char *line));
    7777char         *expand_vectors            PROTO((char *line));
    78 char         *parse                     PROTO((char *line));
     78char         *parse                     PROTO((int *status, char *line));
    7979char        **parse_commands            PROTO((char *, int *));
    8080void          welcome                   PROTO((void));
  • branches/czw_branch/20101203/Ohana/src/opihi/lib.shell/VectorOps.c

    r20936 r30631  
    178178  vec[0].Nelements = Nelements;
    179179  if (type == OPIHI_FLT) {
    180     REALLOCATE (vec[0].elements.Flt, opihi_flt, Nelements);
     180    REALLOCATE (vec[0].elements.Flt, opihi_flt, MAX(1, Nelements));
    181181    vec[0].type = OPIHI_FLT;
    182182  } else {
    183     REALLOCATE (vec[0].elements.Int, opihi_int, Nelements);
     183    REALLOCATE (vec[0].elements.Int, opihi_int, MAX(1, Nelements));
    184184    vec[0].type = OPIHI_INT;
    185185  }
  • branches/czw_branch/20101203/Ohana/src/opihi/lib.shell/command.c

    r27790 r30631  
    1010  Command *cmd;
    1111
    12   // rawline = NULL;
    13   rawline = strcreate (line);
     12  // the input line is never NULL
     13  if (!line) { fprintf (stderr, "programming error\n"); abort(); }
     14
     15  rawline = strcreate (line);  // used for error messages which should echo the unparsed line
    1416
    1517  /* force a space between ! and first word: !ls becomes ! ls */
     
    2224  /* expand anything of the form $fred or $fred$sam, etc */
    2325  line = expand_vars (line);     /* line is freed here, new one allocated */
     26  if (!line) goto escape;
     27
    2428  /* expand anything of the form fred[N] */
    2529  line = expand_vectors (line);  /* line is freed here, new one allocated */
     30  if (!line) goto escape;
    2631
    2732  // print the line with the variables and vectors expanded, but before evaluating
     
    2934  if (VERBOSE_SHELL == OPIHI_VERBOSE_ON) gprint (GP_ERR, "opihi: %s\n", line);
    3035
    31   /* solve math expresions, assign variable, if needed */
    32   line = parse (line);        /* line is freed here, new one allocated */
    33   /* any entry in line of the form {foo} returns value or tmp vector / buffer */
     36  /* solve math expresions, assign variable, if needed.  any entry in line of the form {foo}
     37   * returns value or tmp vector / buffer */
     38  line = parse (&status, line);        /* line is freed here, new one allocated */
     39  if (!status) goto escape;
    3440
    3541  /* we may have reallocated line, return new pointer */
     
    3945  if (argc == 0) {
    4046      FREE (rawline);
     47      set_int_variable ("STATUS", TRUE);
    4148      return (TRUE);  /* empty command or assignment */
    4249  }
     
    7481  FREE (rawline);
    7582  return (status);
     83
     84 escape:
     85  set_int_variable ("STATUS", FALSE);
     86
     87  # if (DEBUG)
     88  gprint (GP_ERR, "command: %s, status: %d\n", line, FALSE);
     89  # endif
     90
     91  FREE (rawline);
     92
     93  if (VERBOSE_SHELL != OPIHI_VERBOSE_OFF) gprint (GP_ERR, "error on line: %s\n", rawline);
     94
     95  // return the current value of line, in case it was modified
     96  *outline = line;
     97  return FALSE;
    7698}
    7799
  • branches/czw_branch/20101203/Ohana/src/opihi/lib.shell/expand_vars.c

    r15878 r30631  
    6464    /* variable assignment (skip these variables) */
    6565    if ((L == line) && (V1 != NULL)) {
    66       if ((*V1 == '=') || !strcmp (V1, "++") || !strcmp (V1, "--")) {
     66      int isAssignment;
     67
     68      isAssignment = FALSE;
     69      isAssignment |= (V1[0] == '=');
     70      isAssignment |= (V1[0] == '+') && (V1[1] == '=');
     71      isAssignment |= (V1[0] == '-') && (V1[1] == '=');
     72      isAssignment |= (V1[0] == '+') && (V1[1] == '+');
     73      isAssignment |= (V1[0] == '-') && (V1[1] == '-');
     74
     75      if (isAssignment) {
    6776        *N = *L;
    6877        free (V0);
  • branches/czw_branch/20101203/Ohana/src/opihi/lib.shell/parse.c

    r27491 r30631  
    11# include "opihi.h"
    22
    3 char *parse (char *line) {
     3char *parse (int *status, char *line) {
    44
    55  double fval;
    66  float *fptr;
    77  char *newline, *N, *L, *val, *B, *V, *V0, *V1, *c1, *c2, *p;
    8   int Nx, Ny, Nbytes, status, size, NLINE, isBuffer, inRange;
     8  int Nx, Ny, Nbytes, filestatus, size, NLINE, isBuffer, inRange;
    99  FILE *f;
    1010  Vector *vec;
    1111  Buffer *buf;
     12
     13  *status = TRUE;
    1214
    1315  Ny = 0;
     
    4951    }
    5052
     53    if (!strncmp (V1, "+=", 2)) {
     54        V1 ++;
     55        if (*V1 == 0) goto error;
     56        V1 ++;
     57        if (*V1 == 0) goto error;
     58
     59        val = get_variable (V0);
     60        if (val == NULL) {
     61            fval = 0.0;
     62        } else {
     63            fval = atof (val);
     64        }
     65
     66        /* dvomath returns a new string, or NULL, with the result of the expression */
     67        val = dvomath (1, &V1, &size, 0);
     68        if (val == NULL) goto error;
     69        fval += atof(val);
     70        // save the result
     71        set_variable (V0, fval);
     72        goto escape;
     73    }
     74
     75    if (!strncmp (V1, "-=", 2)) {
     76        V1 ++;
     77        if (*V1 == 0) goto error;
     78        V1 ++;
     79        if (*V1 == 0) goto error;
     80
     81        val = get_variable (V0);
     82        if (val == NULL) {
     83            fval = 0.0;
     84        } else {
     85            fval = atof (val);
     86        }
     87
     88        /* dvomath returns a new string, or NULL, with the result of the expression */
     89        val = dvomath (1, &V1, &size, 0);
     90        if (val == NULL) goto error;
     91        fval -= atof(val);
     92        // save the result
     93        set_variable (V0, fval);
     94        goto escape;
     95    }
     96
    5197    /* not an assignement, syntax error */
    5298    if (*V1 != '=') goto error;
     
    73119      Nbytes = fread (val, 1, 1023, f);
    74120      val[Nbytes] = 0;
    75       status = pclose (f);
     121      filestatus = pclose (f);
    76122      free (B);
    77123
    78       if (status) gprint (GP_ERR, "warning: exit status of command %d\n", status);
     124      if (filestatus) gprint (GP_ERR, "warning: exit status of command %d\n", filestatus);
    79125
    80126      /* convert all but last return to ' '.  drop last return */
     
    88134        }
    89135      }
     136      // save the resulting value
     137      set_str_variable (V0, val);
     138      goto escape;  /* frees temp variables */
     139    }
     140
     141    /* simple variable assignment */
     142    /* dvomath returns a new string, or NULL, with the result of the expression */
     143    val = dvomath (1, &V1, &size, 0);
     144    if (val == NULL) {
     145      while (OHANA_WHITESPACE (*V1)) V1++;
     146      val = strcreate (V1);
    90147    }
    91 
    92     /* simple variable assignment */
    93     if (*V1 != '`') {
    94       /* dvomath returns a new string, or NULL, with the result of the expression */
    95       val = dvomath (1, &V1, &size, 0);
    96       if (val == NULL) {
    97         while (OHANA_WHITESPACE (*V1)) V1++;
    98         val = strcreate (V1);
    99       }
    100     }
    101     /* both dvomath and command replacement create (char *) val */
     148    // save the result
    102149    set_str_variable (V0, val);
    103150    goto escape;  /* frees temp variables */
     
    172219      if (!inRange) {
    173220        gprint (GP_ERR, "no element %d,%d\n", Nx, Ny);
    174         goto escape;
     221        goto error;
    175222      }
    176223      if (Nx < 0) Nx += buf[0].header.Naxis[0];
     
    188235      if (!inRange) {
    189236        gprint (GP_ERR, "no element %d\n", Nx);
    190         goto escape;
     237        goto error;
    191238      }
    192239      if (Nx < 0) Nx += vec[0].Nelements;
     
    197244      }
    198245    }
    199 
    200246    goto escape;
    201247  }
     
    226272    if (c1 == NULL) {
    227273      gprint (GP_ERR, "no close brackets!\n");
    228       goto escape;
     274      goto error;
    229275    }
    230276    c2 = strchr (L, '{');
    231277    if ((c2 != NULL) && (c2 < c1)) {
    232278      gprint (GP_ERR, "can't nest brackets!\n");
    233       goto escape;
     279      goto error;
    234280    }
    235281    *c1 = 0;
     
    240286    if (val == NULL) {
    241287      print_error ();
    242       goto escape;
     288      goto error;
    243289    }
    244290
     
    257303 
    258304  REALLOCATE (newline, char, strlen (newline) + 1);
     305  *status = TRUE;
    259306  return (newline);
    260307
    261308error:
    262309  gprint (GP_ERR, "syntax error\n");
    263 
    264 escape:
     310  // assignment or increment operation: free line and return NULL
    265311  if (line != (char *) NULL) free (line);
    266312  if (val != (char *) NULL) free (val);
    267313  if (V0 != (char *) NULL) free (V0);
     314  *status = FALSE;
     315  return NULL;
     316
     317escape:
     318  // assignment or increment operation: free line and return NULL
     319  if (line != (char *) NULL) free (line);
     320  if (val != (char *) NULL) free (val);
     321  if (V0 != (char *) NULL) free (V0);
     322  *status = TRUE;
    268323  return (NULL);
    269324}
  • branches/czw_branch/20101203/Ohana/src/opihi/lib.shell/stack_math.c

    r27435 r30631  
    972972        *out = OP;                                              \
    973973      }                                                         \
    974       clear_stack (V1);                                         \
    975       return (TRUE);                                            \
     974      goto escape;                                              \
    976975    }                                                           \
    977976    if ((V1->vector->type == OPIHI_INT) && (FTYPE == 'S')) {    \
     
    982981        *out = OP;                                              \
    983982      }                                                         \
    984       clear_stack (V1);                                         \
    985       return (TRUE);                                            \
     983      goto escape;                                              \
    986984    }                                                           \
    987985    if ((V1->vector->type == OPIHI_INT) && (FTYPE == 's')) {    \
     
    992990        *out = OP;                                              \
    993991      }                                                         \
    994       clear_stack (V1);                                         \
    995       return (TRUE);                                            \
     992      goto escape;                                              \
    996993    } }                                                 
    997994
     
    10351032# undef V_FUNC
    10361033
     1034escape:
     1035
    10371036  if (V1[0].type == 'v') {
    10381037    free (V1[0].vector[0].elements.Ptr);
  • branches/czw_branch/20101203/Ohana/src/photdbc/include/photdbc.h

    r30118 r30631  
    5151double DMGAIN;
    5252double CHISQ_MAX;
     53double SIGMA_MIN_KEEP;
    5354double SIGMA_MAX;
    5455double AVE_SIGMA_LIM;
    5556int    NMEAS_MIN;
     57int    NMEAS_MIN_FILTERED;
     58int    NCODE_MIN;
    5659double ZERO_POINT;
     60
     61int ExcludeByMinSigma;
    5762
    5863int ExcludeByInstMag;
     
    6570SkyRegion REGION;
    6671PhotCodeData photcodes;
     72
     73char          *PHOTCODE_DROP_LIST, *PHOTCODE_SKIP_LIST;
     74int           NphotcodesDrop,      NphotcodesSkip;
     75PhotCode     **photcodesDrop,     **photcodesSkip;
    6776
    6877# define FLAG_AREA            0X0001
  • branches/czw_branch/20101203/Ohana/src/photdbc/src/ConfigInit.c

    r28331 r30631  
    2626  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
    2727
    28   WarnConfig (config, "PHOTDBC_JOIN_RADIUS",       "%lf", 0, &JOIN_RADIUS);
     28  // XXX join_stars (in photdbc.c) is currently disabled
     29  // WarnConfig (config, "PHOTDBC_JOIN_RADIUS",       "%lf", 0, &JOIN_RADIUS);
     30
     31  // XXX unique_measures (in photdbc.c) is currently disabled
    2932  // WarnConfig (config, "UNIQ_RADIUS",            "%lf", 0, &UNIQ_RADIUS);
    3033
     34  // XXX flag_measures (in photdbc.c) is currently disabled
    3135  // WarnConfig (config, "XMIN",                   "%lf", 0, &XMIN);
    3236  // WarnConfig (config, "XMAX",                   "%lf", 0, &XMAX);
     
    3741  // WarnConfig (config, "MMIN",                   "%lf", 0, &tmp);  MMIN      = 1000*tmp;
    3842  // WarnConfig (config, "MMAX",                   "%lf", 0, &tmp);  MMAX      = 1000*tmp;
     43  // WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
     44
     45  // XXX get_mags (in photdbc.c) is currently disabled
    3946  // WarnConfig (config, "DMSYS",                  "%lf", 0, &tmp);  DMSYS     = SQ(1000*tmp);
    4047  // WarnConfig (config, "DMGAIN",                 "%lf", 0, &DMGAIN);
    41   // WarnConfig (config, "CHISQ_MAX",              "%lf", 0, &CHISQ_MAX);
    4248
    4349  ScanConfig (config, "SIGMA_MAX",              "%lf", 0, &SIGMA_MAX);
    4450  ScanConfig (config, "AVE_SIGMA_LIM",          "%lf", 0, &AVE_SIGMA_LIM);
    4551  ScanConfig (config, "NMEAS_MIN",              "%d",  0, &NMEAS_MIN);
     52  ScanConfig (config, "NMEAS_MIN_FILTERED",     "%d",  0, &NMEAS_MIN_FILTERED);
    4653
    4754  WarnConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
  • branches/czw_branch/20101203/Ohana/src/photdbc/src/args.c

    r30118 r30631  
    1919  ExcludeByInstMag = FALSE;
    2020  if ((N = get_argument (argc, argv, "-instmag"))) {
    21     remove_argument (N, &argc, argv);
    2221    ExcludeByInstMag = TRUE;
    2322    remove_argument (N, &argc, argv);
     
    2524    remove_argument (N, &argc, argv);
    2625    INST_MAG_MAX = atof(argv[N]);
     26    remove_argument (N, &argc, argv);
     27  }
     28
     29  ExcludeByMinSigma = FALSE;
     30  if ((N = get_argument (argc, argv, "-min-sigma"))) {
     31    ExcludeByMinSigma = TRUE;
     32    remove_argument (N, &argc, argv);
     33    SIGMA_MIN_KEEP = atof(argv[N]);
     34    remove_argument (N, &argc, argv);
    2735  }
    2836
    2937  ExcludeByMaxMinMag = FALSE;
    3038  if ((N = get_argument (argc, argv, "-maxminmag"))) {
    31     remove_argument (N, &argc, argv);
    3239    ExcludeByMaxMinMag = TRUE;
    3340    remove_argument (N, &argc, argv);
    3441    MAX_MIN_MAG = atof(argv[N]);
     42    remove_argument (N, &argc, argv);
    3543  }
    3644
     
    6169  }
    6270
     71  PHOTCODE_DROP_LIST = NULL;
     72  if ((N = get_argument (argc, argv, "-photcode-drop"))) {
     73    remove_argument (N, &argc, argv);
     74    PHOTCODE_DROP_LIST = strcreate(argv[N]);
     75    remove_argument (N, &argc, argv);
     76  }
     77
     78  PHOTCODE_SKIP_LIST = NULL;
     79  if ((N = get_argument (argc, argv, "-photcode-skip"))) {
     80    remove_argument (N, &argc, argv);
     81    PHOTCODE_SKIP_LIST = strcreate(argv[N]);
     82    remove_argument (N, &argc, argv);
     83  }
     84
    6385  if (argc != 2) usage();
    6486
     
    81103  fprintf (stderr, "USAGE: photdbc (output)\n\n");
    82104  fprintf (stderr, " this program takes an existing DVO database and makes a copy, applying a number of optional\n");
    83   fprintf (stderr, " filters in the process.  \n");
    84   fprintf (stderr, "\n");
    85   fprintf (stderr, " photdbc (output)\n");
    86   fprintf (stderr, "\n");
    87   fprintf (stderr, " allowed filters / restrictions include:\n");
    88   fprintf (stderr, "\n");
     105  fprintf (stderr, " filters in the process.  \n\n");
     106
     107  fprintf (stderr, " photdbc (output)\n\n");
     108
     109  fprintf (stderr, " allowed filters / restrictions include:\n\n");
     110
    89111  fprintf (stderr, " -region Rmin Rmax Dmin Dmax : limit operation to the specified region \n");
    90   fprintf (stderr, " -join                 : join measurements between stars using JOIN_RADIUS\n");
    91   fprintf (stderr, " -ccdregion X Y X Y    : only keep detections within the specified detector region\n");
    92   fprintf (stderr, "                         (can this be limited to specific photcodes? cameras? detectors?)\n");
    93   fprintf (stderr, " -photcode_limits code Mmin Mmax : allow multiples of these\n");
    94   fprintf (stderr, "\n");
    95   fprintf (stderr, " SIGMA_MAX\n");
    96   fprintf (stderr, " NMEAS_MIN\n");
    97   fprintf (stderr, " INST_MAG_MAX\n");
    98   fprintf (stderr, " INST_MAG_MIN\n");
     112 
     113  fprintf (stderr, " -photcode-drop   : remove these photcodes from the output (REF or DEP only)\n");
     114  fprintf (stderr, " -photcode-skip  : ignore these photcodes when assessing the validity (keep unless object is dropped)\n");
     115
     116  fprintf (stderr, " -instmag (min) (max) : range of valid instrumental magnitudes (or measurements are dropped)\n");
     117  fprintf (stderr, " -min-sigma (sigma)   : object must have one measurement error less than sigma or object is dropped\n");
     118
     119  fprintf (stderr, " -maxminmag (mag)   : object must have one magnitude less than this or object is dropped\n");
     120
     121  fprintf (stderr, " option options:\n");
     122  fprintf (stderr, " -v : verbose mode\n");
     123  fprintf (stderr, " -params : list the current parameters\n\n");
     124
     125  fprintf (stderr, "ptolemy.rc config values used by this program:\n");
     126  fprintf (stderr, " SIGMA_MAX : drop measurements with errors greater than this\n");
     127  fprintf (stderr, " NMEAS_MIN : drop objects with fewer measurements than this\n");
     128  fprintf (stderr, " NMEAS_MIN_FILTERED : drop objects with fewer measurements than this after filtering above\n");
     129  fprintf (stderr, " AVE_SIGMA_LIM : drop objects if all average mags are greater than this\n");
     130
    99131  exit (2);
    100132}
     133
     134// fprintf (stderr, " -join                 : join measurements between stars using JOIN_RADIUS\n");
     135// fprintf (stderr, " -ccdregion X Y X Y    : only keep detections within the specified detector region\n");
     136// fprintf (stderr, "                         (can this be limited to specific photcodes? cameras? detectors?)\n");
     137// fprintf (stderr, " -photcode_limits code Mmin Mmax : allow multiples of these\n");
  • branches/czw_branch/20101203/Ohana/src/photdbc/src/find_images.c

    r29001 r30631  
    122122     these are measurements from external sources, like USNO */
    123123
    124   assignMcal (&image[nimage], (double *) NULL, -1);
     124  // assignMcal (&image[nimage], (double *) NULL, -1);
     125  image[nimage].Mcal = 0;
    125126  image[nimage].code = ID_IMAGE_NEW;
    126127 
  • branches/czw_branch/20101203/Ohana/src/photdbc/src/initialize.c

    r30118 r30631  
    22
    33void initialize (int argc, char **argv) {
     4
     5  int NPHOTCODES;
     6  char *codename, *ptr, *list;
    47
    58  /* are these set correctly? */
     
    1114  ConfigInit (&argc, argv);
    1215  args (argc, argv);
     16
     17  NphotcodesDrop = 0;
     18  photcodesDrop = NULL;
     19  if (PHOTCODE_DROP_LIST != NULL) {
     20    NPHOTCODES = 10;
     21    ALLOCATE (photcodesDrop, PhotCode *, NPHOTCODES);
     22
     23    /* parse the comma-separated list of photcodesDrop */
     24    list = PHOTCODE_DROP_LIST;
     25    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
     26      list = NULL; // pass NULL on successive strtok_r calls
     27      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_DROP_LIST);
     28      fprintf (stderr, "codename: %s\n", codename);
     29      if ((photcodesDrop[NphotcodesDrop] = GetPhotcodebyName (codename)) == NULL) {
     30        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
     31        exit (1);
     32      }
     33      NphotcodesDrop ++;
     34      CHECK_REALLOCATE (photcodesDrop, PhotCode *, NPHOTCODES, NphotcodesDrop, 10);
     35    }
     36  }
     37
     38  NphotcodesSkip = 0;
     39  photcodesSkip = NULL;
     40  if (PHOTCODE_SKIP_LIST != NULL) {
     41    NPHOTCODES = 10;
     42    ALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES);
     43
     44    /* parse the comma-separated list of photcodesSkip */
     45    list = PHOTCODE_SKIP_LIST;
     46    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
     47      list = NULL; // pass NULL on successive strtok_r calls
     48      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_SKIP_LIST);
     49      fprintf (stderr, "codename: %s\n", codename);
     50      if ((photcodesSkip[NphotcodesSkip] = GetPhotcodebyName (codename)) == NULL) {
     51        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
     52        exit (1);
     53      }
     54      NphotcodesSkip ++;
     55      CHECK_REALLOCATE (photcodesSkip, PhotCode *, NPHOTCODES, NphotcodesSkip, 10);
     56    }
     57  }
    1358
    1459  if (SHOW_PARAMS) {
  • branches/czw_branch/20101203/Ohana/src/photdbc/src/make_subcatalog.c

    r28331 r30631  
    55int make_subcatalog (Catalog *subcatalog, Catalog *catalog) {
    66 
    7   off_t i, j, offset;
     7  int found;
     8  off_t i, j, k, offset;
    89  off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm, Nsecfilt;
    9   double mag, minMag;
    10   int keep;
     10  double mag, minMag, minSigma;
     11  int keep, *secSkip;
     12  PhotCode *photcode;
    1113 
    1214  Nsecfilt = GetPhotcodeNsecfilt ();
    1315  assert (catalog[0].Nsecfilt == Nsecfilt);
     16
     17  // set up a list of SEC entries to ignore when evaluating a source
     18  ALLOCATE (secSkip, int, Nsecfilt);
     19  for (i = 0; i < Nsecfilt; i++) {
     20      secSkip[i] = FALSE;
     21      photcode = GetPhotcodebyNsec(i);
     22      for (k = 0; k < NphotcodesSkip; k++) {
     23          if (photcodesSkip[k][0].code != photcode[0].code) continue;
     24          secSkip[i] = TRUE;
     25      }
     26  }
    1427
    1528  /* we are moving only the subset of measurements from catalog[0] to subcatalog[0] */
     
    3851    }
    3952
     53    // exclude stars with too few measurements
     54    if (NCODE_MIN) {
     55      // drop if all of the allowed average photcodes have ncode < NCODE_MIN values
     56      keep = FALSE;
     57      for (j = 0; !keep && (j < Nsecfilt); j++) {
     58          if (secSkip[j]) continue;
     59          if (catalog[0].secfilt[Nsecfilt*i+j].Ncode >= NCODE_MIN) {
     60              keep = TRUE;
     61        }
     62      }
     63      if (!keep) continue;
     64    }
     65
    4066    /* assign average and secfilt values */
    4167    subcatalog[0].average[Naverage] = catalog[0].average[i];
     
    4571    }
    4672
    47     minMag = 32;
     73    minMag   = 32;
     74    minSigma = 32;
    4875    Nm = 0;
    4976    for (j = 0; j < catalog[0].average[i].Nmeasure; j++) {
     
    5279
    5380      # if 0
    54       if (DropPhotcode) {
    55         ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode);
    56         if (ecode == photcode[0].code) continue;
    57       }
    5881      if (DropNonStellar && catalog[0].measure[offset].dophot != 1) continue;
    5982      # endif
    6083
    61       /* exclude measurements by measurement error */
     84      // remove certain photcodes from the output measurements
     85      if (NphotcodesDrop > 0) {
     86          found = FALSE;
     87          for (k = 0; (k < NphotcodesSkip) && !found; k++) {
     88              if (photcodesSkip[k][0].code == catalog[0].measure[offset].photcode) found = TRUE;
     89              if (photcodesSkip[k][0].code == GetPhotcodeEquivCodebyCode(catalog[0].measure[offset].photcode)) found = TRUE;
     90          }
     91          if (found) continue;
     92      } 
     93 
     94      // ignore certain photcodes to assess the measurements
     95      if (NphotcodesSkip > 0) {
     96          found = FALSE;
     97          for (k = 0; (k < NphotcodesSkip) && !found; k++) {
     98              if (photcodesSkip[k][0].code == catalog[0].measure[offset].photcode) found = TRUE;
     99              if (photcodesSkip[k][0].code == GetPhotcodeEquivCodebyCode(catalog[0].measure[offset].photcode)) found = TRUE;
     100          }
     101          if (found) goto keep;
     102      } 
     103 
     104      // exclude measurements by measurement error -- drop exactly this measurement
    62105      if (SIGMA_MAX && (catalog[0].measure[offset].dM > SIGMA_MAX)) continue;
    63106
    64       /* select measurements by mag limit */
     107      // select measurements by mag limit -- drop exactly this measurement 
    65108      if (ExcludeByInstMag) {
    66109        mag = PhotInst (&catalog[0].measure[offset]);
     
    69112      }
    70113
     114      // check measurements for this object -- drop object if no measurements pass
     115      if (ExcludeByMinSigma) {
     116          minSigma = MIN (minSigma, catalog[0].measure[offset].dM);
     117      }
     118
     119      // check measurements for this object -- drop object if no measurements pass
    71120      if (ExcludeByMaxMinMag) {
    72121        mag = PhotSys (&catalog[0].measure[offset], &catalog[0].average[i], &catalog[0].secfilt[i*Nsecfilt]);
    73122        minMag = MIN (minMag, mag);
    74123      }
     124
     125    keep:
    75126
    76127      subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
     
    90141    }
    91142
    92     // after measurement exclusion, exclude stars with too few measurements
    93     if (Nm < NMEAS_MIN) {
     143    // exclude faint objects
     144    if (ExcludeByMinSigma && (minSigma > SIGMA_MIN_KEEP)) {
    94145      Nmeasure -= Nm;
    95146      continue;
    96147    }
     148
     149    // after measurement exclusion, exclude stars with too few measurements
     150    if (NMEAS_MIN_FILTERED && (Nm < NMEAS_MIN_FILTERED)) {
     151      Nmeasure -= Nm;
     152      continue;
     153    }
     154
    97155    subcatalog[0].average[Naverage].Nmissing = 0;
    98156    subcatalog[0].average[Naverage].Nmeasure = Nm;
     
    122180  return (TRUE);
    123181}
     182
     183/**
     184    the purpose of this function is to create a subset database.  there are several ways this could be done:
     185
     186    * reject all measurements from all objects that fail to meet some criteria (objects may lose measurements and/or be dropped)
     187
     188    * only reject an object if all measurements fail to meet some criteria (ie, keep all measurements if any measurement passes)
     189
     190    * only apply the keep / reject criteria to certain photcodes
     191
     192
     193    ***
     194
     195    * options:
     196
     197    minimum value for nmeas : NMEAS_MIN
     198    minimum value for nmeas : NMEAS_MIN_FILTERED
     199    minimum value for ncode : NCODE_MIN (drop if all ncode < NCODE_MIN) (respect photcodes)
     200    reject faint meas       : SIGMA_MAX
     201    reject faint source     : SIGMA_MIN_KEEP (keep source source if its minimum dMag < this limit)
     202
     203    **/
  • branches/czw_branch/20101203/Ohana/src/relastro/include/relastro.h

    r29001 r30631  
    2626  double dPos;
    2727  int mask;
     28  int Nmeas;
    2829} StarData;
    2930
     
    9495
    9596double SIGMA_LIM;
    96 int SRC_MEAS_TOOFEW; //catalog objects wich fewer detections then this are ignored
     97int    SRC_MEAS_TOOFEW; //catalog objects wich fewer detections then this are ignored
    9798double MIN_ERROR;
     99
     100int    IMFIT_CLIP_NITER; // number of clipping iterations to perform in FitChip
     101double IMFIT_CLIP_NSIGMA; // number of sigma to clip in FitChip
     102double IMFIT_SYS_SIGMA_LIM; // max dMag for objects used to measure systematic scatter
    98103
    99104double RADIUS; // match radius for high-speed objects
     
    141146int FlagOutlier;
    142147int    CLIP_THRESH;
     148int USE_BASIC_CHECK;
    143149
    144150FitMode FIT_MODE;
     
    296302void fixImageRaw (Catalog *catalog, int Ncatalog, off_t im);
    297303void FlagOutliers(Catalog *catalog);
    298 int MeasFilterTest(Measure *measure);
     304int MeasFilterTest(Measure *measure, int applySigmaLim);
    299305
    300306int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon);
     
    325331int UpdateObjectOffsets (SkyList *skylist);
    326332
    327 int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj);
    328 int relastroVisualPlotScatter(double values[], double thresh, int npts);
    329 int relastroVisualPlotOutliers(Catalog *catalog, int offset, int Nmeasure,
    330                               StatType statsR, StatType statsD, double thresh);
    331 
    332 
     333int relastroVisualPlotChipFit(StarData *raw, StarData *ref, double dRmax, int numObj);
     334// int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj);
     335// int relastroVisualPlotScatter(double values[], double thresh, int npts);
     336// int relastroVisualPlotOutliers(Catalog *catalog, int offset, int Nmeasure, StatType statsR, StatType statsD, double thresh);
     337void relastroSetVisual(int state);
     338int relastroGetVisual(void);
    333339
    334340int FixProblemImages (SkyList *skylist);
     
    352358int createStarMapPoints();
    353359int checkStarMap(int N);
     360
     361int GetScatterRawRef(float *dLsig, float *dMsig, float *dRsig, int *nKeep, StarData *raw, StarData *ref, int Nstars, float SigmaLimit);
  • branches/czw_branch/20101203/Ohana/src/relastro/src/ConfigInit.c

    r29001 r30631  
    1919  if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
    2020
    21   GetConfig (config, "RELASTRO_SIGMA_LIM",     "%lf", 0, &SIGMA_LIM);
     21  GetConfig (config, "RELASTRO_SIGMA_LIM",         "%lf", 0, &SIGMA_LIM); // exclude measurements on this basis
    2222  GetConfig (config, "RELASTRO_SRC_MEAS_TOOFEW",   "%d",  0, &SRC_MEAS_TOOFEW);
     23
     24  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NITER",    "%d",  0, &IMFIT_CLIP_NITER))    IMFIT_CLIP_NITER  = 3;
     25  if (!ScanConfig (config, "RELASTRO_IMFIT_CLIP_NSIGMA",   "%lf", 0, &IMFIT_CLIP_NSIGMA))   IMFIT_CLIP_NSIGMA = 3.0;
     26  if (!ScanConfig (config, "RELASTRO_IMFIT_SYS_SIGMA_LIM", "%lf", 0, &IMFIT_SYS_SIGMA_LIM)) IMFIT_SYS_SIGMA_LIM = 0.01;
    2327
    2428  // XXX these are used in relphot to identify poor stars / images -- define
  • branches/czw_branch/20101203/Ohana/src/relastro/src/FitChip.c

    r29001 r30631  
    22# include "relastroVisual.h"
    33
    4 // XXX make these user parameters
    5 # define FIT_CHIP_NITER     3
    6 # define FIT_CHIP_NSIGMA    3.0
    7 
    8 // XXX save the fit[0].Npts value in the image table?
    9 
    10 // XXX save measurements of the fit quality (scatter, chisq) in the image table
    11 
    124int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image) {
    135
    14   int i, Nscatter, Niter, skip;
    15   CoordFit *fit;
    16   double dL, dM, dR, dRmax, *values;
    17 
    18   ALLOCATE (values, double, Nmatch);
    19   for (Niter = 0; Niter < FIT_CHIP_NITER; Niter ++) {
     6  int i, NstatFull, Nstat, Niter, skip;
     7  float dLsig, dMsig, dRsig;
     8  float dLsigFull, dMsigFull, dRsigFull;
     9  float dL, dM, dR, dRmax;
     10
     11  CoordFit *fit = NULL;
     12
     13  dRmax = 0.0;
     14
     15  for (Niter = 0; Niter < IMFIT_CLIP_NITER; Niter ++) {
     16   
     17    // measure the scatter for the unmarked (good) measurements with dM < limit
     18    // SIGMA_LIM here is redundant with ImageOps.c:915
     19    GetScatterRawRef(&dLsig, &dMsig, &dRsig, &Nstat, raw, ref, Nmatch, SIGMA_LIM);
     20    dRmax = IMFIT_CLIP_NSIGMA*dRsig;
     21
     22    // (a) skip unfittable points
     23    // (b) mark good and bad points for fit (in and outliers)
    2024
    2125    // measure the scatter distribution (use only the bright end detections)
    22     for (i = Nscatter = 0; i < Nmatch; i++) {
     26    for (i = 0; i < Nmatch; i++) {
    2327      if (raw[i].mask) continue;
    24       if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) continue;
     28      if (isnan(raw[i].dMag)) {
     29        raw[i].mask |= 0x0004;
     30        continue;
     31      }
     32      if (raw[i].dMag > SIGMA_LIM) {
     33        raw[i].mask |= 0x0008;
     34        continue;
     35      } // is this redundant with ImageOps.c:915?
    2536
    2637      dL = raw[i].L - ref[i].L;
    2738      dM = raw[i].M - ref[i].M;
    2839      dR = hypot (dL, dM);
    29 
    30       values[Nscatter] = dR;
    31       Nscatter++;
    32     }
    33 
    34     if (Nscatter > 5) {
    35       // for a 2D Gaussian, 40% of the points are within 1 sigma; dRmax is ~ 3 sigma
    36       // XXX this test is not sensible for Nscatter < XXX (5?)
    37       dsort (values, Nscatter);
    38       dRmax = FIT_CHIP_NSIGMA*values[(int)(0.40*Nscatter)];
    39       relastroVisualPlotScatter(values, dRmax, Nscatter);
    40       relastroVisualPlotRawRef(raw, ref, dRmax, Nmatch);
    41     } else {
    42       dRmax = 0;
    43     }
     40      if (dR > dRmax) {
     41        raw[i].mask |= 0x0010;
     42        continue;
     43      }
     44    }
     45
     46    // figures to assess the fitting process:
     47    // x vs dx, x vs dy, y vs dx, y vs dy :
     48    // residual vector field
     49    // dx vs mag, dy vs mag
     50    relastroVisualPlotChipFit(raw, ref, dRmax, Nmatch);
    4451
    4552    // fit the requested order polynomial
     
    4754      image[0].coords.Npolyterms = CHIPORDER;
    4855    }
     56    if (fit) fit_free (fit);
    4957    fit = fit_init (image[0].coords.Npolyterms);
    5058
    5159    // generate the fit matches
    5260    for (i = 0; i < Nmatch; i++) {
    53       if (raw[i].mask) {
    54         continue;
    55       }
    56       if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) {
    57         continue;
    58       }
    59 
    60       // only keep objects within dRmax
    61       dL = raw[i].L - ref[i].L;
    62       dM = raw[i].M - ref[i].M;
    63       dR = hypot (dL, dM);
    64 
    65       // fprintf (stderr, "fit %f %f -> %f %f : %f %f (%f vs %f) wt: %f\n", raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].L, raw[i].M, dR, dRmax, raw[i].dPos);
    66 
    67       if ((dRmax > 0.0) && (dR > dRmax)) continue;
    68 
     61      if (raw[i].mask) continue;
    6962      fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].dPos);
    7063    }
     
    8881    }
    8982    if (skip) {
    90       if (VERBOSE) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
     83      if (VERBOSE2) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
    9184      fit_free (fit);
    92       free (values);
    9385      image[0].flags |= ID_IMAGE_ASTROM_FEW;
    9486      return FALSE;
    9587    }
    96 
    97     // fprintf (stderr, "scatter limit: %f based on %d detections; using %d of %d for fit\n", dRmax, Nscatter, fit[0].Npts, Nmatch);
    9888
    9989    // measure the fit, update the coords & object coordinates
     
    110100    }
    111101
    112     fit_free (fit);
    113 
    114102    for (i = 0; i < Nmatch; i++) {
    115103      XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[0].coords);
     
    118106  }
    119107
    120   free (values);
     108  // count mask classes
     109  int nMask1 = 0;
     110  int nMask2 = 0;
     111  int nMask3 = 0;
     112  int nMask4 = 0;
     113  int nMask5 = 0;
     114  for (i = 0; i < Nmatch; i++) {
     115    if (!raw[i].mask) continue;
     116    if (raw[i].mask & 0x01) nMask1 ++;
     117    if (raw[i].mask & 0x02) nMask2 ++;
     118    if (raw[i].mask & 0x04) nMask3 ++;
     119    if (raw[i].mask & 0x08) nMask4 ++;
     120    if (raw[i].mask & 0x10) nMask5 ++;
     121  }
     122
     123  GetScatterRawRef(&dLsigFull, &dMsigFull, &dRsigFull, &NstatFull, raw, ref, Nmatch, SIGMA_LIM);
     124  GetScatterRawRef(&dLsig, &dMsig, &dRsig, &Nstat, raw, ref, Nmatch, IMFIT_SYS_SIGMA_LIM);
     125
     126  int Nm = 0;
     127  int Ns = 0;
     128  for (i = 0; i < Nmatch; i++) {
     129    if (raw[i].mask) continue;
     130    Nm += raw[i].Nmeas;
     131    Ns++;
     132  }
     133  image[0].nLinkAstrom = (Nm / Ns);
     134
     135  if (VERBOSE) fprintf (stderr, "fit sigma: %f (%f, %f) : full: %f (%f, %f), scatter limit: %f (%d full, %d bright, %d fit, %d all) (%d %d %d %d %d)\n", dRsig, dLsig, dMsig, dRsigFull, dLsigFull, dMsigFull, dRmax, NstatFull, Nstat, fit[0].Npts, Nmatch, nMask1, nMask2, nMask3, nMask4, nMask5);
     136
     137  image[0].dXpixSys = dLsig;
     138  image[0].dYpixSys = dMsig;
     139  image[0].nFitAstrom = fit[0].Npts;
     140
     141  if (fit) fit_free (fit);
     142
    121143  return TRUE;
     144}
     145
     146// measure the scatter distribution (limit selected objects by dMag)
     147int GetScatterRawRef(float *dLsig, float *dMsig, float *dRsig, int *nKeep, StarData *raw, StarData *ref, int Nstars, float SigmaLimit) {
     148
     149  int i, Ns;
     150  float dL, dM, dLsum, dLsum2, dMsum, dMsum2, *dR;
     151
     152  ALLOCATE (dR, float, Nstars);
     153
     154  Ns = 0;
     155  dLsum = dLsum2 = dMsum = dMsum2 = 0.0;
     156  for (i = 0; i < Nstars; i++) {
     157    if (raw[i].mask) continue;
     158    if (isnan(raw[i].dMag)) continue;
     159    if ((SigmaLimit > 0.0) && (raw[i].dMag > SigmaLimit)) continue;
     160   
     161    dL = raw[i].L - ref[i].L;
     162    dM = raw[i].M - ref[i].M;
     163
     164    dLsum  += dL;
     165    dLsum2 += dL*dL;
     166    dMsum  += dM;
     167    dMsum2 += dM*dM;
     168
     169    dR[Ns] = hypot (dL, dM);
     170    Ns++;
     171  }
     172
     173  *dLsig = sqrt((dLsum2 / Ns) - SQ(dLsum/Ns));
     174  *dMsig = sqrt((dMsum2 / Ns) - SQ(dMsum/Ns));
     175  *nKeep = Ns;
     176
     177  if (Ns < 5) {
     178    *dRsig = NAN;
     179    free  (dR);
     180    return (FALSE);
     181  }
     182
     183  // for a 2D Gaussian, 40% of the points are within R = 1 sigma
     184  fsort (dR, Ns);
     185  *dRsig = dR[(int)(0.40*Ns)];
     186 
     187  free  (dR);
     188  return (TRUE);
    122189}
    123190
     
    162229  fclose (f);
    163230*/
     231
  • branches/czw_branch/20101203/Ohana/src/relastro/src/GetAstromError.c

    r30118 r30631  
    11# include "relastro.h"
     2# define WEIGHTED_ERRORS 1
    23
    34float GetAstromError (Measure *measure, int mode) {
    4   //BIG HACKXXXXXXXX
    5   return 0.1;
     5
    66  PhotCode *code;
    7   float dPobs, dPsys, dPtotal, dM, AS, MS;
     7  float dPobs, dPsys, dPtotal, dM, AS, MS, dX, dY;
     8
     9  if (!WEIGHTED_ERRORS) {
     10    // if we don't understand the errors at all, this at least lets us get things roughly
     11    // right:
     12    return 0.1;
     13  }
     14
    815  switch (mode) {
    916    case ERROR_MODE_RA:
    10       dPobs = measure[0].dXccd / 100.0;  // need to redefine this as RAerr
     17      dPobs = FromShortPixels(measure[0].dXccd);  // dXccd is a value in pixels
    1118      break;
    1219    case ERROR_MODE_DEC:
    13       dPobs = measure[0].dYccd / 100.0;  // need to redefine this as RAerr
     20      dPobs = FromShortPixels(measure[0].dYccd);  // dYccd is a value in pixels
    1421      break;
    1522    case ERROR_MODE_POS:
    16       dPobs = hypot (measure[0].dXccd, measure[0].dYccd) / 100.0;  // need to redefine this as RAerr
     23      dX = FromShortPixels(measure[0].dXccd);  // dXccd is a value in pixels
     24      dY = FromShortPixels(measure[0].dYccd);  // dYccd is a value in pixels
     25      dPobs = hypot (dX, dY);
    1726      break;
    1827    default:
    1928      abort();
    2029  }
    21   /* the astrometric errors are not being carried yet (but should be!) */
    22   /* we use the photometric mag error as a weighting term */
    2330
    2431  code  = GetPhotcodebyCode (measure[0].photcode);
     
    2734  dPsys = code[0].astromErrSys;
    2835  dM    = measure[0].dM;
    29   dPtotal = sqrt(SQ(dPsys) + AS*SQ(dPobs) + MS*SQ(dM));
    30 
    31   //XXX dXccd, dYccd are now working correctly
    32   //dPtotal = AS * dPobs;
     36  dPtotal = sqrt(SQ(dPsys) + SQ(AS*dPobs) + SQ(MS*dM));
    3337
    3438  dPtotal = MAX (dPtotal, MIN_ERROR);
     
    3640}
    3741
     42/* for a long time, psphot was either not reported position errors, or was reporting
     43 * completely wrong astrometry errors.  This function lets us handle, in the
     44 * configuration, different strategies to generating a position error
     45 *
     46 * astrometry systematic error : this is the minimum expected per-position error.  You
     47 * should probably measure this from you data.  watch out for the chicken and egg problem!
     48 *
     49 * astrometry error scale : this field lets you accept position errors in (say) pixels and
     50 * convert them with this term to arcsec.  AS : pixel scale in arcsec
     51 *
     52 * astrometry mag scale : this field lets you define position errors based on the
     53 * photometry error.  the scale factor should be something like a typical seeing number
     54 * (in arcsec) for the given instrument
     55 *
     56 */
  • branches/czw_branch/20101203/Ohana/src/relastro/src/ImageOps.c

    r29001 r30631  
    146146  free (clist);
    147147  free (mlist);
     148  free (Nlist);
     149  free (NLIST);
    148150}
    149151
     
    325327  for (j = 0; j < average[0].Nmeasure; j++) {
    326328    off = average[0].measureOffset + j;
    327     fprintf (stderr, "%f, %f\n", measure[off].dR, measure[off].dD);
     329    fprintf (stderr, "dR, dD, mag, dMag: %f, %f, %f, %f\n", measure[off].dR, measure[off].dD, measure[off].M, measure[off].dM);
    328330  }
    329331  return;
     
    336338  off_t i, m, c, n, nPos;
    337339  double X, Y, L, M, P, Q, R, D, dR, dD;
    338   double dPos, DPOS_MAX_ASEC;
     340  double dPos, dPosSys, DPOS_MAX_ASEC;
    339341
    340342  Mosaic *mosaic;
     
    359361  }
    360362
    361   // accumulate the rms position offsets.  if this value, or any specific entry, is too
    362   // large, we will reset the image to the original coords at the end of the analysis
    363 
     363  // these are used to accumulate the rms position offsets.  if this value, or any
     364  // specific entry, is too large, we will reset the image to the original coords at the
     365  // end of the analysis
    364366  dPos = 0.0;
    365367  nPos = 0;
     368
     369  // convert the image systematic error in pixels to a value in arcsec
     370  {
     371    double dLsig, dMsig;
     372    double Ro, Do, Rx, Dx, dP0, dP1;
     373    Coords *coords;
     374
     375    // these values are in pixels, but we to convert to arcsec
     376    dLsig = image[0].dXpixSys;
     377    dMsig = image[0].dYpixSys;
     378
     379    if (moscoords == NULL) {
     380      coords = imcoords;
     381    } else {
     382      coords = moscoords;
     383    }
     384    XY_to_LM (&Ro, &Do, 0.0, 0.0, coords);
     385    XY_to_LM (&Rx, &Dx, dLsig, 0.0, coords);
     386    dP0 = 3600.0 * hypot(Rx - Ro, Dx - Do); // convert to arcsec
     387    XY_to_LM (&Rx, &Dx, 0.0, dLsig, coords);
     388    dP1 = 3600.0 * hypot(Rx - Ro, Dx - Do); // convert to arcsec
     389    dPosSys = 0.5 * (dP0 + dP1);
     390  }     
    366391
    367392  for (i = 0; i < Nlist[im]; i++) {
     
    390415    // complain if the new location is far from the average location
    391416    // NOTE: This should never happen, or our StarMap tests are not working
    392     if (fabs(dR) > 1.5*ADDSTAR_RADIUS) {
     417    if (fabs(dR) > 3.0*ADDSTAR_RADIUS) {
    393418      fprintf (stderr, "measurement is far from average location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
    394419      dump_measures (&catalog[c].average[n], catalog[c].measure);
    395420      // abort ();
    396421    }
    397     if (fabs(dD) > 1.5*ADDSTAR_RADIUS) {
     422    if (fabs(dD) > 3.0*ADDSTAR_RADIUS) {
    398423      fprintf (stderr, "measurement is far from average location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
    399424      dump_measures (&catalog[c].average[n], catalog[c].measure);
     
    418443    catalog[c].measure[m].dR = dR;
    419444    catalog[c].measure[m].dD = dD;
    420 
     445   
    421446    if (catalog[c].measure[m].dR > +180.0*3600.0) {
    422447      // average on high end of boundary, move star up
     
    429454      catalog[c].measure[m].dR = 3600.0*(catalog[c].average[n].R - R);
    430455    }
     456
     457    // set the systematic error for this image:
     458    catalog[c].measure[m].dRsys = ToShortPixels(dPosSys);
    431459  }
    432460
     
    544572
    545573    // an object with only one detection provides no information about the image calibration
    546     //XXX this is already taken care of in bcatalog
    547     raw[i].mask = FALSE;
    548     int mask = FALSE;
     574    // XXX this is already taken care of in bcatalog
     575    raw[i].mask = 0x0000;
    549576    if (catalog[c].average[n].Nmeasure <= SRC_MEAS_TOOFEW) {
    550       mask = TRUE;
     577      raw[i].mask |= 0x0001;
    551578    }
    552579    if (!finite(catalog[c].measure[m].dR) || !finite(catalog[c].measure[m].dD)) {
    553       mask = TRUE;
    554     }
    555 
    556     raw[i].mask = mask;
    557 
     580      raw[i].mask |= 0x0002;
     581    }
     582    raw[i].Nmeas = catalog[c].average[n].Nmeasure; // record so we can check how well connected an image is
    558583
    559584    switch (mode) {
     
    685710     
    686711      // skip measurements based on user selected criteria
    687       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     712      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    688713      R[N] = catalog[0].measure[m].dR;
    689714      D[N] = catalog[0].measure[m].dD;
     
    711736
    712737      // skip measurements based on user selected criteria
    713       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     738      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    714739     
    715740      x = catalog[0].measure[m].dR - statsR.median;
     
    725750    }
    726751
    727     //examine results
    728     relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset,
    729                                catalog[0].average[j].Nmeasure,
    730                                statsR, statsD, Ns);
     752    // examine results
     753    // relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset, catalog[0].average[j].Nmeasure, statsR, statsD, Ns);
    731754  }
    732755 
     
    784807     
    785808      // skip measurements based on user selected criteria
    786       if (!MeasFilterTest(&catalog[0].measure[m])) continue;
     809      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue;
    787810      R[N] = catalog[0].measure[m].dR;
    788811      D[N] = catalog[0].measure[m].dD;
     
    806829    for (k = 0; k < catalog[0].average[j].Nmeasure; k++, m++) {
    807830      //skip bad measurements
    808       if (!MeasFilterTest(&catalog[0].measure[m])) continue; 
     831      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue; 
    809832      x = catalog[0].measure[m].dR - statsR.median;
    810833      y = catalog[0].measure[m].dD - statsD.median;
     
    838861    for (k = 0; k < catalog[0].average[j].Nmeasure; k++, m++) {
    839862      //skip bad measurements
    840       if (!MeasFilterTest(&catalog[0].measure[m])) continue; 
     863      if (!MeasFilterTest(&catalog[0].measure[m], FALSE)) continue; 
    841864      x = catalog[0].measure[m].dR - statsR.median;
    842865      y = catalog[0].measure[m].dD - statsD.median;
     
    852875    }  //done rejecting outliers
    853876
    854     //examine results
    855     relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset,
    856                                catalog[0].average[j].Nmeasure,
    857                                statsR, statsD, Ns);
     877    // examine results
     878    // relastroVisualPlotOutliers(catalog, catalog[0].average[j].measureOffset, catalog[0].average[j].Nmeasure, statsR, statsD, Ns);
    858879   
    859880  } //done looping over objects
     
    871892
    872893/** Determine whether a measurement should be included in the analysis, based on supplied filter criteria */
    873 int MeasFilterTest(Measure *measure) {
     894// we only optionally apply the sigma limit: for object averages, this should not be used (should it?)
     895int MeasFilterTest(Measure *measure, int applySigmaLim) {
    874896  int found, k;
    875897  long mask;
     
    917939
    918940  /* select measurements by measurement error */
    919   if ((SIGMA_LIM > 0) && (measure[0].dM > SIGMA_LIM)) return FALSE;
     941  if (applySigmaLim && (SIGMA_LIM > 0) && (measure[0].dM > SIGMA_LIM)) {
     942    return FALSE;
     943  }
    920944 
    921945  /* select measurements by mag limit */
  • branches/czw_branch/20101203/Ohana/src/relastro/src/StarMaps.c

    r29001 r30631  
    7979    starmap[N].stars[ybin*NX_MAP + xbin] ++;
    8080  }
    81   MARKTIME("assign stars to starmap bins: %f sec\n", dtime);
     81  if (VERBOSE2) { MARKTIME("assign stars to starmap bins: %f sec\n", dtime); }
    8282
    8383  return (TRUE);
     
    117117    }
    118118
    119     if (VERBOSE) fprintf (stderr, "starmap: %d points for image %s\n", starmap[i].Npoints, images[i].name);
     119    if (VERBOSE2) fprintf (stderr, "starmap: %d points for image %s\n", starmap[i].Npoints, images[i].name);
    120120  }
    121121
     
    145145  }
    146146
    147   if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     147  if (VERBOSE2) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
    148148
    149   if (dLmax > DPOS_MAX) return (FALSE);
    150   if (dMmax > DPOS_MAX) return (FALSE);
     149  if (dLmax > DPOS_MAX) {
     150      if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     151      return (FALSE);
     152  }
     153  if (dMmax > DPOS_MAX) {
     154      if (VERBOSE) fprintf (stderr, "max deviations for %s using %d pts : %f, %f\n", images[N].name, starmap[N].Npoints, dLmax, dMmax);
     155      return (FALSE);
     156  }
    151157  return (TRUE);
    152158}
  • branches/czw_branch/20101203/Ohana/src/relastro/src/UpdateChips.c

    r29001 r30631  
    11# include "relastro.h"
     2int plotChipFits (double *Ro, double *Do, char *mode, int Nimage);
     3int saveCenter (Image *image, double *Ro, double *Do, int im);
    24
    35int UpdateChips (Catalog *catalog, int Ncatalog) {
    46
     7  int Nskip, Nmosaic, NnewFit, NoldFit;
     8
    59  /* we can measure new image parameters for each non-mosaic chip independently */
    6   off_t i, Nimage, Nraw, Nref;
     10  off_t i, Nimage, Nraw, Nref, nFitAstr;
    711  Image *image;
    812  StarData *raw, *ref;
    913  Coords *oldCoords;
     14  float dXpixSys, dYpixSys;
     15  double *Ro, *Do;
     16  char *mode;
     17
     18  Nskip = Nmosaic = NnewFit = NoldFit = 0;
    1019
    1120  image = getimages (&Nimage);
    1221
     22  // save fit results for summary plot
     23  ALLOCATE (Ro, double, Nimage);
     24  ALLOCATE (Do, double, Nimage);
     25  ALLOCATE (mode, char, Nimage);
     26
    1327  for (i = 0; i < Nimage; i++) {
    1428
    1529    /* skip all except WRP images */
    16     if (strcmp(&image[i].coords.ctype[4], "-WRP")) continue;
     30    if (strcmp(&image[i].coords.ctype[4], "-WRP")) {
     31      Nmosaic ++;
     32      mode[i] = 0;
     33      continue;
     34    }
    1735
    1836    /* convert measure coordinates to raw entries */
    1937    raw = getImageRaw (catalog, Ncatalog, i, &Nraw, MODE_MOSAIC);
    20     if (!raw) continue;
     38    if (!raw) {
     39      Nskip ++;
     40      mode[i] = 0;
     41      continue;
     42    }
    2143
    2244    /* convert average coordinates to ref entries */
    2345    ref = getImageRef (catalog, Ncatalog, i, &Nref, MODE_MOSAIC);
    24     if (!ref) continue;
     46    if (!ref) {
     47      Nskip ++;
     48      mode[i] = 0;
     49      continue;
     50    }
    2551
    2652    // note that Nraw & Nref must be equal: if not, we made a programming error in one of these two functions.
    2753    assert (Nraw == Nref);
    2854
     55    // save these in case of failure
    2956    saveCoords (&image[i].coords, i);
     57    dXpixSys = image[i].dXpixSys;
     58    dYpixSys = image[i].dYpixSys;
     59    nFitAstr = image[i].nFitAstrom;
    3060
    3161    // FitChip does iterative, clipped fitting
     
    3363    if (!FitChip (raw, ref, Nraw, &image[i])) {
    3464      if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     65
     66      // restore status quo ante
    3567      oldCoords = getCoords (i);
    3668      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     69      image[i].dXpixSys = dXpixSys;
     70      image[i].dYpixSys = dYpixSys;
     71      image[i].nFitAstrom = nFitAstr;
     72
     73      saveCenter (image, &Ro[i], &Do[i], i);
     74      mode[i] = 1;
     75      NoldFit ++;
    3776      free (raw);
    3877      free (ref);
     
    4281    if (!checkStarMap (i)) {
    4382      if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     83      // restore status quo ante
    4484      oldCoords = getCoords (i);
    4585      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     86      image[i].dXpixSys = dXpixSys;
     87      image[i].dYpixSys = dYpixSys;
     88      image[i].nFitAstrom = nFitAstr;
     89
     90      saveCenter (image, &Ro[i], &Do[i], i);
     91      mode[i] = 2;
    4692      image[i].flags |= ID_IMAGE_ASTROM_POOR;
    47     }
    48 
     93      NoldFit ++;
     94      free (raw);
     95      free (ref);
     96      continue;
     97    }
     98
     99    saveCenter (image, &Ro[i], &Do[i], i);
     100    mode[i] = 3;
     101    NnewFit ++;
    49102    free (raw);
    50103    free (ref);
    51104  }
    52105
     106  plotChipFits (Ro, Do, mode, Nimage);
     107
     108  fprintf (stderr, "UpdateChips: %d fitted, %d keep old, %d skipped, %d mosaic (skipped)\n", NnewFit, NoldFit, Nskip, Nmosaic);
    53109  return (TRUE);
    54110}
    55111
     112int saveCenter (Image *image, double *Ro, double *Do, int im) {
     113
     114  Mosaic *mosaic;
     115  Coords *moscoords, *imcoords;
     116  double X, Y, L, M, P, Q, R, D;
     117
     118  moscoords = NULL;
     119  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     120    mosaic = getMosaicForImage (im);
     121    if (mosaic == NULL) return FALSE;  // if we cannot find the associated image, skip it
     122    moscoords = &mosaic[0].coords;
     123  }
     124  imcoords = &image[im].coords;
     125 
     126  if (!strcmp(&image[im].coords.ctype[4], "-WRP")) {
     127    X = 0.5*image[im].NX;
     128    Y = 0.5*image[im].NY;
     129  } else {
     130    X = 0.0;
     131    Y = 0.0;
     132  }
     133
     134  if (moscoords == NULL) {
     135    // this is a Simple image (not a mosaic)
     136    // note that for a Simple image, L,M = P,Q
     137    XY_to_LM (&L, &M, X, Y, imcoords);
     138    LM_to_RD (&R, &D, L, M, imcoords);
     139  } else {
     140    XY_to_LM (&L, &M, X, Y, imcoords);
     141    XY_to_LM (&P, &Q, L, M, moscoords);
     142    LM_to_RD (&R, &D, P, Q, moscoords);
     143  }
     144
     145  double Rmid;
     146  if (UserCatalog) {
     147      Rmid = UserCatalogRA;
     148  } else {
     149      Rmid = 0.5*(UserPatch.Rmin + UserPatch.Rmax);
     150  }
     151
     152  R = ohana_normalize_angle_to_midpoint (R, Rmid);
     153
     154  *Ro = R;
     155  *Do = D;
     156
     157  return (TRUE);
     158}
     159 
     160int plotChipFits (double *Ro, double *Do, char *mode, int Nimage) {
     161
     162  static int kapa = -1;
     163
     164  int i, N;
     165  double Rmin, Rmax, Dmin, Dmax;
     166  float *xvec, *yvec;
     167  Graphdata graphdata;
     168
     169  if (!relastroGetVisual()) return (TRUE);
     170
     171  if (kapa == -1) {
     172    kapa = KapaOpenNamedSocket("kapa", "relastro");
     173    if (kapa == -1) {
     174      fprintf (stderr, "can't open kapa window\n");
     175      return FALSE;
     176    }
     177  }
     178
     179  Rmin = +720;
     180  Rmax = -720;
     181  Dmin = +90;
     182  Dmax = -90;
     183
     184  // find the R, D range
     185  for (i = 0; i < Nimage; i++) {
     186    if (!mode[i]) continue;
     187
     188    Rmin = MIN(Rmin, Ro[i]);
     189    Rmax = MAX(Rmax, Ro[i]);
     190    Dmin = MIN(Dmin, Do[i]);
     191    Dmax = MAX(Dmax, Do[i]);
     192  }
     193
     194  ALLOCATE (xvec, float, Nimage);
     195  ALLOCATE (yvec, float, Nimage);
     196
     197  bzero (&graphdata, sizeof(Graphdata));
     198  plot_defaults (&graphdata);
     199  graphdata.xmin = Rmin;
     200  graphdata.xmax = Rmax;
     201  graphdata.ymin = Dmin;
     202  graphdata.ymax = Dmax;
     203  graphdata.style = 2;
     204  graphdata.size = 1;
     205
     206  KapaSetFont (kapa, "helvetica", 14);
     207  KapaSetLimits (kapa, &graphdata);
     208  KapaBox (kapa, &graphdata);
     209
     210  // *** good images ***
     211  N = 0;
     212  for (i = 0; i < Nimage; i++) {
     213    if (mode[i] != 3) continue;
     214    xvec[N] = Ro[i];
     215    yvec[N] = Do[i];
     216    N++;
     217  }
     218  graphdata.ptype = 7;
     219  graphdata.color = KapaColorByName("black");
     220  KapaPrepPlot (kapa, N, &graphdata);
     221  KapaPlotVector (kapa, N, xvec, "x");
     222  KapaPlotVector (kapa, N, yvec, "y");
     223 
     224  // *** reject fit ***
     225  N = 0;
     226  for (i = 0; i < Nimage; i++) {
     227    if (mode[i] != 1) continue;
     228    xvec[N] = Ro[i];
     229    yvec[N] = Do[i];
     230    N++;
     231  }
     232  graphdata.ptype = 3;
     233  graphdata.color = KapaColorByName("red");
     234  KapaPrepPlot (kapa, N, &graphdata);
     235  KapaPlotVector (kapa, N, xvec, "x");
     236  KapaPlotVector (kapa, N, yvec, "y");
     237 
     238  // *** divergent fit ***
     239  N = 0;
     240  for (i = 0; i < Nimage; i++) {
     241    if (mode[i] != 2) continue;
     242    xvec[N] = Ro[i];
     243    yvec[N] = Do[i];
     244    N++;
     245  }
     246  graphdata.ptype = 2;
     247  graphdata.color = KapaColorByName("blue");
     248  KapaPrepPlot (kapa, N, &graphdata);
     249  KapaPlotVector (kapa, N, xvec, "x");
     250  KapaPlotVector (kapa, N, yvec, "y");
     251 
     252  free (xvec);
     253  free (yvec);
     254
     255  return (TRUE);
     256}
     257
     258// XXX if (!FindMosaicForImage (image, Nimage, i)) { }
  • branches/czw_branch/20101203/Ohana/src/relastro/src/UpdateObjectOffsets.c

    r27581 r30631  
    4040    UpdateMeasures (&catalog, 1);
    4141
     42    UpdateObjects (&catalog, 1);
     43
    4244    freeImageBins (1);
    4345
  • branches/czw_branch/20101203/Ohana/src/relastro/src/UpdateObjects.c

    r30118 r30631  
    103103
    104104        //does the measurement pass the supplied filtering constraints?
    105         if (!MeasFilterTest(&catalog[i].measure[m])) {
     105        if (!MeasFilterTest(&catalog[i].measure[m], FALSE)) {
    106106          catalog[i].measure[m].dbFlags &= ~ID_MEAS_USED_OBJ;
    107107          continue;
     
    134134
    135135        // dX, dY : error in arcsec --
    136         // dX[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
    137         // dY[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
    138 
    139         dX[N] = 0.1;
    140         dY[N] = 0.1;
     136        dX[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_RA);
     137        dY[N] = GetAstromError (&catalog[i].measure[m], ERROR_MODE_DEC);
     138
     139        // add systematic error in quadrature, if desired
     140        // only do this after the fit has converged (or you will never improve the poor images)
     141        // if (INCLUDE_SYS_ERR) {
     142        // float dRsys = FromShortPixels(catalog[i].measure[m].dRsys);
     143        // dX[N] = hypot(dX[N], dRsys);
     144        // dY[N] = hypot(dY[N], dRsys);
     145        // }
     146
     147        // dX[N] = 0.1;
     148        // dY[N] = 0.1;
     149
    141150        dT[N] = catalog[i].measure[m].dt;
    142151
  • branches/czw_branch/20101203/Ohana/src/relastro/src/args.c

    r28184 r30631  
    1313    remove_argument (N, &argc, argv);
    1414    FIT_TARGET = TARGET_OBJECTS;
    15 
    16     // check for object fitting modes (not valid for images)
    17     if ((N = get_argument (argc, argv, "-pm"))) {
    18         remove_argument (N, &argc, argv);
    19         FIT_MODE = FIT_PM_ONLY;
    20     }
    21     if ((N = get_argument (argc, argv, "-par"))) {
    22         remove_argument (N, &argc, argv);
    23         FIT_MODE = FIT_PAR_ONLY;
    24     }
    25     if ((N = get_argument (argc, argv, "-pmpar"))) {
    26         remove_argument (N, &argc, argv);
    27         FIT_MODE = FIT_PM_AND_PAR;
    28     }
    29   }
     15  }
     16
     17  // check for object fitting modes
     18  if ((N = get_argument (argc, argv, "-pm"))) {
     19    remove_argument (N, &argc, argv);
     20    FIT_MODE = FIT_PM_ONLY;
     21  }
     22  if ((N = get_argument (argc, argv, "-par"))) {
     23    remove_argument (N, &argc, argv);
     24    FIT_MODE = FIT_PAR_ONLY;
     25  }
     26  if ((N = get_argument (argc, argv, "-pmpar"))) {
     27    remove_argument (N, &argc, argv);
     28    FIT_MODE = FIT_PM_AND_PAR;
     29  }
     30
    3031  if ((N = get_argument (argc, argv, "-high-speed"))) {
    3132    // XXX include a parallax / no-parallax option
     
    9394      usage ();
    9495    }
     96  }
     97
     98  USE_BASIC_CHECK = FALSE;
     99  if ((N = get_argument (argc, argv, "-basic-image-search"))) {
     100    remove_argument (N, &argc, argv);
     101    USE_BASIC_CHECK = TRUE;
    95102  }
    96103
     
    144151    VERBOSE = VERBOSE2 = TRUE;
    145152    remove_argument (N, &argc, argv);
     153  }
     154
     155  if ((N = get_argument (argc, argv, "-visual"))) {
     156    remove_argument (N, &argc, argv);
     157    relastroSetVisual(TRUE);
    146158  }
    147159
  • branches/czw_branch/20101203/Ohana/src/relastro/src/bcatalog.c

    r29001 r30631  
    4545      offset = catalog[0].average[i].measureOffset + j;
    4646     
    47       // filter objects based on user supplied criteria
    48       if (!MeasFilterTest(&catalog[0].measure[offset])) {
     47      // filter objects based on user supplied criteria, including SIGMA_LIM
     48      if (!MeasFilterTest(&catalog[0].measure[offset], TRUE)) {
    4949        catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
    5050        continue;
     
    7878        subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_AREA;
    7979      }
    80      
     80
    8181      Nmeasure ++;
    8282      Nm ++;
  • branches/czw_branch/20101203/Ohana/src/relastro/src/load_catalogs.c

    r29001 r30631  
    2626    pcatalog[0].Nsecfilt  = GetPhotcodeNsecfilt ();
    2727
    28     if (!dvo_catalog_open (pcatalog, skylist[0].regions[i], VERBOSE, "w")) {
     28    if (!dvo_catalog_open (pcatalog, skylist[0].regions[i], VERBOSE2, "w")) {
    2929      fprintf (stderr, "ERROR: failure reading catalog %s\n", pcatalog[0].filename);
    3030      exit (1);
    3131    }
    32     if (VERBOSE && !pcatalog[0].Naves_disk) fprintf (stderr, "no data in %s, skipping\n", pcatalog[0].filename);
     32    if (VERBOSE2 && !pcatalog[0].Naves_disk) fprintf (stderr, "no data in %s, skipping\n", pcatalog[0].filename);
    3333
    3434    //outlier rejection
  • branches/czw_branch/20101203/Ohana/src/relastro/src/plotstuff.c

    r27588 r30631  
    146146  graphdata[0].ymax = dUNDEF;
    147147   
     148  graphdata[0].ticktextPad = NAN;
     149  graphdata[0].labelPadXm = NAN;
     150  graphdata[0].labelPadXp = NAN;
     151  graphdata[0].labelPadYm = NAN;
     152  graphdata[0].labelPadYp = NAN;
     153  graphdata[0].padXm = NAN;
     154  graphdata[0].padXp = NAN;
     155  graphdata[0].padYm = NAN;
     156  graphdata[0].padYp = NAN;
    148157}
  • branches/czw_branch/20101203/Ohana/src/relastro/src/relastro.c

    r29001 r30631  
    7878        MARKTIME("update chips: %f sec\n", dtime);
    7979      }
     80      // create summary plots of the process
     81      // relastroVisualSummaryChips();
    8082      break;
    8183
  • branches/czw_branch/20101203/Ohana/src/relastro/src/relastroVisual.c

    r24308 r30631  
    1111#define KAPAY 700
    1212
    13 static int kapa = -1;
     13static int kapa1 = -1;
    1414static int kapa2 = -1;
    1515static int kapa3 = -1;
     16static int kapa4 = -1;
    1617
    1718static int isVisual = FALSE;
    18 static int plotRawRef = FALSE;
    19 static int plotScatter = FALSE;
    20 static int plotResid = FALSE;
    21 static int plotVector = FALSE;
     19static int plotRawRef = TRUE;
     20// static int plotScatter = TRUE;
     21// static int plotResid = TRUE;
     22// static int plotVector = TRUE;
    2223static int plotOutliers = TRUE;
    2324
     
    2930      fprintf(stderr, "Failure to open kapa.\n");
    3031      isVisual = 0;
    31       return 0;
     32      return FALSE;
    3233    }
    33     //    KapaResize (*kapid, KAPAX, KAPAY);
    34   }
    35   return 1;
     34  }
     35  return TRUE;
    3636}
    3737
     
    4949    isVisual = 0;
    5050  }
    51   return 1;
     51  return TRUE;
     52}
     53
     54void relastroSetVisual(int state) {
     55  isVisual = state;
     56}
     57
     58int relastroGetVisual(void) {
     59  return isVisual;
    5260}
    5361
     
    7078    graphdata->xmax = xhi;
    7179    graphdata->ymax = yhi;
    72     return 1;
    73 }
    74 
    75 static int residPlot(float x[], float y[],
    76                      float xVec[], float yVec[],
    77                      int npts, int *kapaID) {
    78     if (!isVisual || !plotResid) return TRUE;
     80    return TRUE;
     81}
     82
     83/** 4-panel plot of x vs dx, y vs dx, x vs dy, y vs dy **/
     84int relastroVisualRawRef(int *kapaID, float *x, float *y, float *dx, float *dy, float *dPos, int npts) {
     85
    7986    Graphdata graphdata;
    8087    KapaSection section;
     
    8390
    8491    KapaInitGraph(&graphdata);
    85     KapaClearPlots(*kapaID);
     92    KapaClearSections(*kapaID);
    8693    KapaSetFont(*kapaID, "helvetica", 14);
    8794
     
    8996    section.x = 0.0; section.y = 0.0;
    9097    section.dx = .45, section.dy = .45;
     98    section.bg = KapaColorByName("white");
    9199    graphdata.ptype = 7;
    92100    graphdata.style = 2;
     101    graphdata.etype |= 0x01;
    93102
    94103    KapaSetSection(*kapaID, &section);
    95     if(!scaleGraphdata(x, xVec, &graphdata, npts)) return 0;
     104    if(!scaleGraphdata(x, dx, &graphdata, npts)) return 0;
    96105    KapaSetLimits(*kapaID, &graphdata);
    97106    KapaBox(*kapaID, &graphdata);
     
    100109    KapaPrepPlot(*kapaID, npts, &graphdata);
    101110    KapaPlotVector(*kapaID, npts, x, "x");
    102     KapaPlotVector(*kapaID, npts, xVec, "y");
     111    KapaPlotVector(*kapaID, npts, dx, "y");
     112    KapaPlotVector(*kapaID, npts, dPos, "dym");
     113    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    103114
    104115    section.x = .5; section.y = 0; section.name="1";
    105116    KapaSetSection(*kapaID, &section);
    106     if(!scaleGraphdata(x, yVec, &graphdata, npts)) return 0;
     117    if(!scaleGraphdata(x, dy, &graphdata, npts)) return 0;
    107118    KapaSetLimits(*kapaID, &graphdata);
    108119    KapaBox(*kapaID, &graphdata);
     
    111122    KapaPrepPlot(*kapaID, npts, &graphdata);
    112123    KapaPlotVector(*kapaID, npts, x, "x");
    113     KapaPlotVector(*kapaID, npts, yVec, "y");
     124    KapaPlotVector(*kapaID, npts, dy, "y");
     125    KapaPlotVector(*kapaID, npts, dPos, "dym");
     126    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    114127
    115128    section.x = .0; section.y = .5; section.name="2";
    116129    KapaSetSection(*kapaID, &section);
    117     if(!scaleGraphdata(y, xVec, &graphdata, npts)) return 0;;
     130    if(!scaleGraphdata(y, dx, &graphdata, npts)) return 0;;
    118131    KapaSetLimits(*kapaID, &graphdata);
    119132    KapaBox(*kapaID, &graphdata);
     
    122135    KapaPrepPlot(*kapaID, npts, &graphdata);
    123136    KapaPlotVector(*kapaID, npts, y, "x");
    124     KapaPlotVector(*kapaID, npts, xVec, "y");
     137    KapaPlotVector(*kapaID, npts, dx, "y");
     138    KapaPlotVector(*kapaID, npts, dPos, "dym");
     139    KapaPlotVector(*kapaID, npts, dPos, "dyp");
    125140
    126141    section.x = .5; section.y = .5; section.name="3";
    127142    KapaSetSection(*kapaID, &section);
    128     if(!scaleGraphdata(y, yVec, &graphdata, npts)) return 0;
     143    if(!scaleGraphdata(y, dy, &graphdata, npts)) return 0;
    129144    KapaSetLimits(*kapaID, &graphdata);
    130145    KapaBox(*kapaID, &graphdata);
     
    133148    KapaPrepPlot(*kapaID, npts, &graphdata);
    134149    KapaPlotVector(*kapaID, npts, y, "x");
    135     KapaPlotVector(*kapaID, npts, yVec, "y");
    136 
    137     return 1;
    138 }
    139 
    140 
    141 /**Plot a vector field*/
    142 static int plotVectorField(float x[], float y[],
    143                            float xVec[], float yVec[],
    144                            int npts, int *kapaID, double maxVecLength) {
    145 
    146     if(!plotVector) return 1;
     150    KapaPlotVector(*kapaID, npts, dy, "y");
     151    KapaPlotVector(*kapaID, npts, dPos, "dym");
     152    KapaPlotVector(*kapaID, npts, dPos, "dyp");
     153
     154    return TRUE;
     155}
     156
     157
     158/** Plot a vector field (circles at vector origin, scaled lines giving vector directions */
     159int relastroVisualVectorField(int *kapaID, float *x, float *y, float *dx, float *dy, int npts, double maxVecLength) {
    147160
    148161    Graphdata graphdata;
    149     float singleX[2], singleY[2];
     162    float *xVec, *yVec;
    150163    float vecScaleFactor;
    151164    float graphSize;
    152165    int i;
    153166    char plotTitle[50];
    154     sprintf(plotTitle, "Maximum Vector Size = %5.1e", maxVecLength);
    155 
     167
     168    if (!npts) return FALSE;
    156169    if (!initWindow(kapaID)) return 0;
    157170
     171    snprintf(plotTitle, 50, "Maximum Vector Size = %5.1e", maxVecLength);
     172
    158173    KapaInitGraph(&graphdata);
    159     KapaClearPlots(*kapaID);
    160     if(!scaleGraphdata(x, y, &graphdata, npts)) return 0;
     174    KapaClearSections(*kapaID);
     175    KapaSetFont(*kapaID, "helvetica", 14);
     176
     177    if (!scaleGraphdata(x, y, &graphdata, npts)) return 0;
    161178
    162179    graphSize = graphdata.xmax - graphdata.xmin;
     
    164181        graphSize = graphdata.ymax - graphdata.ymin;
    165182    }
    166 
    167183    vecScaleFactor = graphSize * 0.02 / (float)maxVecLength;
    168 #ifdef TESTING
    169     fprintf(stderr, "GraphSize: %e\n maxVecLength: %e\n vecScaleFactor: %e\n",
    170             graphSize, maxVecLength, vecScaleFactor);
    171 #endif
    172 
     184
     185    // fprintf(stderr, "GraphSize: %e\n maxVecLength: %e\n vecScaleFactor: %e\n", graphSize, maxVecLength, vecScaleFactor);
    173186
    174187    KapaSetFont (*kapaID, "helvetica", 14);
     
    179192    graphdata.ptype = 7;
    180193    graphdata.style = 2;
     194    graphdata.color = KapaColorByName("black");
    181195    KapaPrepPlot(*kapaID, npts, &graphdata);
    182196    KapaPlotVector(*kapaID, npts, x, "x");
    183197    KapaPlotVector(*kapaID, npts, y, "y");
    184198
    185     //plot each vector individually
    186     graphdata.ptype = 0;
    187     graphdata.style = 0;
     199    ALLOCATE (xVec, float, 2*npts);
     200    ALLOCATE (yVec, float, 2*npts);
     201    for (i = 0; i < npts; i++) {
     202      xVec[2*i + 0] = x[i];
     203      yVec[2*i + 0] = y[i];
     204      xVec[2*i + 1] = x[i] + dx[i] * vecScaleFactor;
     205      yVec[2*i + 1] = y[i] + dy[i] * vecScaleFactor;
     206    }
     207
     208    graphdata.ptype = 100; // line segements by point pair
     209    graphdata.style = 2;
    188210    graphdata.color = KapaColorByName("blue");
    189     for(i = 0; i < npts; i++) {
    190         singleX[0] = x[i];
    191         singleY[0] = y[i];
    192         singleX[1] = x[i] + xVec[i] * vecScaleFactor;
    193         singleY[1] = y[i] + yVec[i] * vecScaleFactor;
    194         KapaPrepPlot(*kapaID, 2, &graphdata);
    195         KapaPlotVector(*kapaID, 2, singleX, "x");
    196         KapaPlotVector(*kapaID, 2, singleY, "y");
    197     }
    198     return 1;
    199 }
    200 
    201 int relastroVisualPlotScatter(double values[], double thresh, int npts) {
    202     float *x, *data;
    203     int i;
    204     float xline[2], yline[2];
    205     if (!isVisual || !plotScatter) return 1;
    206     if (!initWindow(&kapa2)) return 0;
    207 
    208     ALLOCATE(x, float, npts);
    209     ALLOCATE(data, float, npts);
    210 
    211     for(i = 0; i < npts; i++) {
    212         x[i] = i;
    213         data[i] = (float) values[i];
    214     }
     211    KapaPrepPlot  (*kapaID, 2*npts, &graphdata);
     212    KapaPlotVector(*kapaID, 2*npts, xVec, "x");
     213    KapaPlotVector(*kapaID, 2*npts, yVec, "y");
     214
     215    free (xVec);
     216    free (yVec);
     217    return TRUE;
     218}
     219
     220int relastroVisualPlotScatter(int *kapaID, float *dXfit, float *dYfit, float *mag, int npts) {
    215221
    216222    Graphdata graphdata;
    217223    KapaSection section;
    218     section.x = 0; section.y = 0; section.dx = 1; section.dy = 1;
    219     section.name = "junk";
     224
     225    if (!initWindow(kapaID)) return 0;
    220226
    221227    KapaInitGraph(&graphdata);
    222     KapaClearPlots(kapa2);
    223     KapaSetSection(kapa2, &section);
    224 
    225     graphdata.ptype = 0;
    226     graphdata.style = 0;
    227     graphdata.xmin = 0;
    228     graphdata.xmax = npts;
    229     graphdata.ymin = 0;
    230     graphdata.ymax = data[npts-1];
    231 
    232     KapaSetFont(kapa2, "helvetica", 14);
    233     KapaSetLimits(kapa2, &graphdata);
    234     KapaBox(kapa2, &graphdata);
    235     KapaSendLabel( kapa2, "Object", KAPA_LABEL_XM);
    236     KapaSendLabel( kapa2, "Offset(pixels)", KAPA_LABEL_YM);
    237     KapaSendLabel( kapa2, "Astrometric Offset with fit cutoff",
    238                    KAPA_LABEL_XP);
    239     KapaPrepPlot(kapa2, npts, &graphdata);
    240     KapaPlotVector(kapa2, npts, x, "x");
    241     KapaPlotVector(kapa2, npts, data, "y");
    242 
    243     graphdata.color = KapaColorByName("red");
    244     KapaPrepPlot(kapa2, 2, &graphdata);
    245     yline[0] = (float) thresh;
    246     yline[1] = (float) thresh;
    247     xline[0] = graphdata.xmin;
    248     xline[1] = graphdata.xmax;
    249     KapaPlotVector(kapa2, 2, xline, "x");
    250     KapaPlotVector(kapa2, 2, yline, "y");
    251 
    252     askUser(&plotScatter);
    253     return 1;
    254 }
    255 
    256 
     228    KapaClearSections(*kapaID);
     229    KapaSetFont(*kapaID, "helvetica", 14);
     230
     231    section.name = "a";
     232    section.x = 0.0; section.y = 0.0; section.dx = 1.0; section.dy = 0.5;
     233    section.bg = KapaColorByName("white");
     234    KapaSetSection(*kapaID, &section);
     235
     236    graphdata.ptype = 2;
     237    graphdata.style = 2;
     238
     239    if(!scaleGraphdata(mag, dXfit, &graphdata, npts)) return 0;
     240    KapaSetLimits(*kapaID, &graphdata);
     241    KapaBox(*kapaID, &graphdata);
     242    KapaSendLabel(*kapaID, "mag", KAPA_LABEL_XM);
     243    KapaSendLabel(*kapaID, "dXfit", KAPA_LABEL_YM);
     244    KapaPrepPlot(*kapaID, npts, &graphdata);
     245    KapaPlotVector(*kapaID, npts, mag, "x");
     246    KapaPlotVector(*kapaID, npts, dXfit, "y");
     247
     248    section.name = "b";
     249    section.x = 0.0; section.y = 0.5; section.dx = 1.0; section.dy = 0.5;
     250    section.bg = KapaColorByName("white");
     251    KapaSetSection(*kapaID, &section);
     252
     253    graphdata.ptype = 2;
     254    graphdata.style = 2;
     255
     256    if(!scaleGraphdata(mag, dYfit, &graphdata, npts)) return 0;
     257    KapaSetLimits(*kapaID, &graphdata);
     258    KapaBox(*kapaID, &graphdata);
     259    KapaSendLabel(*kapaID, "mag", KAPA_LABEL_XM);
     260    KapaSendLabel(*kapaID, "dYfit", KAPA_LABEL_YM);
     261    KapaPrepPlot(*kapaID, npts, &graphdata);
     262    KapaPlotVector(*kapaID, npts, mag, "x");
     263    KapaPlotVector(*kapaID, npts, dYfit, "y");
     264
     265    return TRUE;
     266}
    257267
    258268/** plot raw vs ref (L, M). Only those whose distance is < drMax are used in fit*/
    259 int relastroVisualPlotRawRef(StarData *raw, StarData *ref, double dRmax, int numObj) {
    260 
    261   if( !isVisual || !plotRawRef) return 1;
    262   if( !initWindow(&kapa)) return 0;
     269int relastroVisualPlotFittedStars(int *kapaID, float *rawX, float *rawY, float *refX, float *refY, int numNoFit, float *rawXfit, float *rawYfit, float *refXfit, float *refYfit, int numFit) {
     270
     271  Graphdata graphdata;
     272
     273  if (!initWindow(kapaID)) return 0;
     274
     275  KapaInitGraph(&graphdata);
     276  KapaClearPlots(*kapaID);
     277
     278  graphdata.ptype = 7;
     279  graphdata.style = 2;
     280
     281  if(!scaleGraphdata(rawXfit, rawYfit, &graphdata, numFit)) {
     282      fprintf(stderr, "Not enough finite points for plotting");
     283      return 0;
     284  }
     285
     286  KapaSetFont(*kapaID, "helvetica", 14);
     287  KapaSetLimits(*kapaID, &graphdata);
     288  KapaBox(*kapaID, &graphdata);
     289  KapaSendLabel( *kapaID, "X", KAPA_LABEL_XM);
     290  KapaSendLabel( *kapaID, "Y", KAPA_LABEL_YM);
     291  KapaSendLabel( *kapaID, "orange, red, green, blue: (raw, ref), (nofit, fit)",
     292                 KAPA_LABEL_XP);
     293
     294  graphdata.color = KapaColorByName("orange");
     295  graphdata.size = 1;
     296  KapaPrepPlot(*kapaID, numNoFit, &graphdata);
     297  KapaPlotVector(*kapaID, numNoFit, rawX, "x");
     298  KapaPlotVector(*kapaID, numNoFit, rawY, "y");
     299
     300  graphdata.color = KapaColorByName("red");
     301  graphdata.size = 2;
     302  KapaPrepPlot(*kapaID, numNoFit, &graphdata);
     303  KapaPlotVector(*kapaID, numNoFit, refX, "x");
     304  KapaPlotVector(*kapaID, numNoFit, refY, "y");
     305
     306  graphdata.color = KapaColorByName("green");
     307  graphdata.size = 1;
     308  KapaPrepPlot(*kapaID, numFit,  &graphdata);
     309  KapaPlotVector(*kapaID, numFit, rawXfit, "x");
     310  KapaPlotVector(*kapaID, numFit, rawYfit, "y");
     311
     312  graphdata.color = KapaColorByName("blue");
     313  graphdata.size = 2;
     314  KapaPrepPlot(*kapaID, numFit, &graphdata);
     315  KapaPlotVector(*kapaID, numFit, refXfit, "x");
     316  KapaPlotVector(*kapaID, numFit, refYfit, "y");
     317
     318  return TRUE;
     319}
     320
     321/** create various plots using the data in raw and ref **/
     322int relastroVisualPlotChipFit(StarData *raw, StarData *ref, double dRmax, int numObj) {
    263323
    264324  float *rawX, *rawY,  *refX, *refY;
    265325  float *rawXfit, *rawYfit, *refXfit, *refYfit;
    266326  float *magRaw, *magRef, *magRawfit, *magReffit;
    267   float *xVec, *yVec;
     327  float *dXfit, *dYfit, *dPos;
    268328  int numFit = 0, numNoFit = 0;
    269329  double dL, dM, dR;
     330
     331  if (!isVisual) return TRUE;
    270332
    271333  ALLOCATE(rawX,      float, numObj);
     
    273335  ALLOCATE(refX,      float, numObj);
    274336  ALLOCATE(refY,      float, numObj);
     337  ALLOCATE(magRaw,    float, numObj);
     338  ALLOCATE(magRef,    float, numObj);
     339
    275340  ALLOCATE(rawXfit,   float, numObj);
    276341  ALLOCATE(rawYfit,   float, numObj);
    277342  ALLOCATE(refXfit,   float, numObj);
    278343  ALLOCATE(refYfit,   float, numObj);
    279   ALLOCATE(magRaw,    float, numObj);
    280   ALLOCATE(magRef,    float, numObj);
    281344  ALLOCATE(magRawfit, float, numObj);
    282345  ALLOCATE(magReffit, float, numObj);
     346  ALLOCATE(dXfit,     float, numObj);
     347  ALLOCATE(dYfit,     float, numObj);
     348  ALLOCATE(dPos,      float, numObj);
    283349
    284350  int i;
    285351  for(i = 0; i < numObj; i++) {
    286     if  (raw[i].mask) continue;
     352    if (raw[i].mask) continue; // XXX
    287353
    288354    dL = raw[i].L - ref[i].L;
    289355    dM = raw[i].M - ref[i].M;
    290356    dR = hypot (dL, dM);
     357
     358    // XXX change the selection to a mask-based thing
    291359    if (dR > dRmax) {
     360      // UNFITTED values
    292361      rawX[numNoFit] = raw[i].X;
    293362      rawY[numNoFit] = raw[i].Y;
     
    298367      numNoFit++;
    299368    } else {
    300       rawXfit[numFit] = raw[i].X;
    301       rawYfit[numFit] = raw[i].Y;
    302       refXfit[numFit] = ref[i].X;
    303       refYfit[numFit] = ref[i].Y;
    304       magRaw[numFit] = raw[i].Mag;
    305       magRef[numFit] = ref[i].Mag;
     369      // FITTED values
     370      rawXfit[numFit]   = raw[i].X;
     371      rawYfit[numFit]   = raw[i].Y;
     372      refXfit[numFit]   = ref[i].X;
     373      refYfit[numFit]   = ref[i].Y;
     374      magRawfit[numFit] = raw[i].Mag;
     375      magReffit[numFit] = ref[i].Mag;
     376      dPos[numFit]      = ref[i].dPos;
     377      dXfit[numFit]     = raw[i].X - ref[i].X;
     378      dYfit[numFit]     = raw[i].Y - ref[i].Y;
    306379      numFit++;
    307380    }
     
    310383  if (numFit == 0) return 0;
    311384
    312   Graphdata graphdata;
    313 
    314   KapaInitGraph(&graphdata);
    315   KapaClearPlots(kapa);
    316 
    317   graphdata.ptype = 7;
    318   graphdata.style = 2;
    319 
    320   if(!scaleGraphdata(rawXfit, rawYfit, &graphdata, numFit)) {
    321       fprintf(stderr, "Not enough finite points for plotting");
    322       return 0;
    323   }
    324 
    325   KapaSetFont(kapa, "helvetica", 14);
    326   KapaSetLimits(kapa, &graphdata);
    327   KapaBox(kapa, &graphdata);
    328   KapaSendLabel( kapa, "X", KAPA_LABEL_XM);
    329   KapaSendLabel( kapa, "Y", KAPA_LABEL_YM);
    330   KapaSendLabel( kapa, "orange, red, green, blue: (raw, ref), (nofit, fit)",
    331                  KAPA_LABEL_XP);
    332 
    333   graphdata.color = KapaColorByName("orange");
    334   graphdata.size = 1;
    335   KapaPrepPlot(kapa, numNoFit, &graphdata);
    336   KapaPlotVector(kapa, numNoFit, rawX, "x");
    337   KapaPlotVector(kapa, numNoFit, rawY, "y");
    338 
    339   graphdata.color = KapaColorByName("red");
    340   graphdata.size = 2;
    341   KapaPrepPlot(kapa, numNoFit, &graphdata);
    342   KapaPlotVector(kapa, numNoFit, refX, "x");
    343   KapaPlotVector(kapa, numNoFit, refY, "y");
    344 
    345   graphdata.color = KapaColorByName("green");
    346   graphdata.size = 1;
    347   KapaPrepPlot(kapa, numFit,  &graphdata);
    348   KapaPlotVector(kapa, numFit, rawXfit, "x");
    349   KapaPlotVector(kapa, numFit, rawYfit, "y");
    350 
    351   graphdata.color = KapaColorByName("blue");
    352   graphdata.size = 2;
    353   KapaPrepPlot(kapa, numFit, &graphdata);
    354   KapaPlotVector(kapa, numFit, refXfit, "x");
    355   KapaPlotVector(kapa, numFit, refYfit, "y");
    356 
    357   ALLOCATE(xVec, float, numFit);
    358   ALLOCATE(yVec, float, numFit);
    359 
    360   //plot the fitted objects as vectors
    361   for(i = 0; i < numFit; i++) {
    362       xVec[i] = rawXfit[i] - refXfit[i];
    363       yVec[i] = rawYfit[i] - refYfit[i];
    364   }
    365 
    366   plotVectorField(rawXfit, rawYfit, xVec, yVec, numFit, &kapa3, dRmax);
    367   if(!residPlot(rawXfit, rawYfit, xVec, yVec, numFit, &kapa2)) {
    368       fprintf(stderr, "Unable to plot residuals");
    369       return 0;
    370   }
    371 
    372   FREE(xVec);
    373   FREE(yVec);
     385  // 4-panel plot of x vs dx, y vs dx, x vs dy, y vs dy
     386  relastroVisualRawRef(&kapa1, rawXfit, rawYfit, dXfit, dYfit, dPos, numFit);
     387
     388  // vector line plot for the fit
     389  relastroVisualVectorField(&kapa2, rawXfit, rawYfit, dXfit, dYfit, numFit, dRmax);
     390
     391  // dXfit, dYfit vs mag
     392  relastroVisualPlotScatter(&kapa3, dXfit, dYfit, magRawfit, numFit);
     393
     394  // plot the positions of the fitted stars on the chip
     395  relastroVisualPlotFittedStars(&kapa4, rawX, rawY, refX, refY, numNoFit, rawXfit, rawYfit, refXfit, refYfit, numFit);
    374396
    375397  askUser(&plotRawRef);
    376398
     399  FREE(dXfit);
     400  FREE(dYfit);
     401  FREE(dPos);
    377402  FREE(rawX);
    378403  FREE(rawY);
     
    388413  FREE(magReffit);
    389414
    390   return 1;
     415  return TRUE;
    391416}
    392417
     
    403428  KapaSection section;
    404429 
    405   if (!isVisual || !plotOutliers) return 1;
    406   if (!initWindow(&kapa)) return 0;
     430  if (!isVisual || !plotOutliers) return TRUE;
     431  if (!initWindow(&kapa1)) return 0;
    407432 
    408433  // populate vectors
     
    426451  for(i = 0; i < Nmeasure; i++, m++) {
    427452    meas = catalog[0].measure[m];
    428     if (!MeasFilterTest(&meas)) continue;
     453    if (!MeasFilterTest(&meas, FALSE)) continue;
    429454    xmin = MIN(xmin, meas.dR);
    430455    xmax = MAX(xmax, meas.dR);
     
    459484  section.x = 0; section.y = 0; section.dx = 1; section.dy = 1;
    460485  section.name = "junk";
     486  section.bg = KapaColorByName("white");
    461487 
    462488  KapaInitGraph(&graphdata);
    463   KapaClearPlots(kapa);
    464   KapaSetFont(kapa, "helvetica", 14);
     489  KapaClearPlots(kapa1);
     490  KapaSetFont(kapa1, "helvetica", 14);
    465491 
    466492  graphdata.ptype = 7;
     
    472498  graphdata.ymax = ymax;
    473499
    474   KapaSetSection(kapa, &section);
    475   KapaSetLimits(kapa, &graphdata);
    476   KapaBox(kapa, &graphdata);
    477 
    478   KapaSendLabel( kapa, "RA (arcsec)", KAPA_LABEL_XM);
    479   KapaSendLabel( kapa, "Dec (arcsec)", KAPA_LABEL_YM);
    480   KapaSendLabel( kapa, "Points flagged as outliers (red)",
     500  KapaSetSection(kapa1, &section);
     501  KapaSetLimits(kapa1, &graphdata);
     502  KapaBox(kapa1, &graphdata);
     503
     504  KapaSendLabel( kapa1, "RA (arcsec)", KAPA_LABEL_XM);
     505  KapaSendLabel( kapa1, "Dec (arcsec)", KAPA_LABEL_YM);
     506  KapaSendLabel( kapa1, "Points flagged as outliers (red)",
    481507                 KAPA_LABEL_XP);
    482508
    483509  graphdata.color = KapaColorByName("green");
    484   KapaPrepPlot(kapa, Nin, &graphdata);
    485   KapaPlotVector(kapa, Nin, Rin, "x");
    486   KapaPlotVector(kapa, Nin, Din, "y");
     510  KapaPrepPlot(kapa1, Nin, &graphdata);
     511  KapaPlotVector(kapa1, Nin, Rin, "x");
     512  KapaPlotVector(kapa1, Nin, Din, "y");
    487513
    488514  graphdata.color = KapaColorByName("red");
    489   KapaPrepPlot(kapa, Nout, &graphdata);
    490   KapaPlotVector(kapa, Nout, Rout, "x");
    491   KapaPlotVector(kapa, Nout, Dout, "y");
     515  KapaPrepPlot(kapa1, Nout, &graphdata);
     516  KapaPlotVector(kapa1, Nout, Rout, "x");
     517  KapaPlotVector(kapa1, Nout, Dout, "y");
    492518
    493519  graphdata.color = KapaColorByName("black");
    494520  graphdata.ptype = 0;
    495521  graphdata.style = 0;
    496   KapaPrepPlot(kapa, 100, &graphdata);
    497   KapaPlotVector(kapa, 100, xCirc, "x");
    498   KapaPlotVector(kapa, 100, yCirc, "y");
    499 
    500 
    501  
     522  KapaPrepPlot(kapa1, 100, &graphdata);
     523  KapaPlotVector(kapa1, 100, xCirc, "x");
     524  KapaPlotVector(kapa1, 100, yCirc, "y");
     525
    502526  askUser(&plotOutliers);
    503527
     
    510534}
    511535 
    512  
     536# if (0)
     537int relastroVisualSummaryChips() {
     538
     539  // plot the dXsys, dYsys histograms
     540
     541  // plot x vs dx, y vs dy, etc for all mosaics
     542
     543  // plot a map of median star scatter
     544
     545}
     546# endif
     547
  • branches/czw_branch/20101203/Ohana/src/relastro/src/select_images.c

    r29001 r30631  
    1515void dsortindex (double *X, off_t *Y, int N);
    1616off_t getRegionStartByRA (double R, double *Rref, off_t Nregions);
     17
     18# define MARKTIME(MSG,...) { \
     19  float dtime; \
     20  gettimeofday (&stop, (void *) NULL); \
     21  dtime = DTIME (stop, start); \
     22  fprintf (stderr, MSG, __VA_ARGS__); }
    1723
    1824Image *select_images (SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage) {
     
    2531  Coords tcoords;
    2632  SkyRegionCoords *skycoords;
    27  
     33  struct timeval start, stop;
     34 
     35  double RmaxSkyRegion, RminSkyRegion, RmidSkyRegion, DminSkyRegion, DmaxSkyRegion;
     36
    2837  double *RmaxSky;
    2938  off_t *index;
     
    4049    return NULL;
    4150  }
     51
     52  gettimeofday (&start, (void *) NULL);
    4253
    4354  // the comparison is made in the catalog local projection. below we set crval1,2
     
    5263  ALLOCATE (RmaxSky, double, skylist[0].Nregions);
    5364  ALLOCATE (index, off_t, skylist[0].Nregions);
     65
     66  RminSkyRegion = +360.0;
     67  RmaxSkyRegion = -360.0;
     68  DminSkyRegion = +90.0;
     69  DmaxSkyRegion = -90.0;
    5470
    5571  /* compare with each region file */
     
    8096    skycoords[i].Xc[3] -= dx; skycoords[i].Yc[3] += dy;
    8197    skycoords[i].Xc[4] -= dx; skycoords[i].Yc[4] -= dy;
    82   }
     98
     99    RminSkyRegion = MIN(RminSkyRegion, skylist[0].regions[i][0].Rmin);
     100    RmaxSkyRegion = MAX(RmaxSkyRegion, skylist[0].regions[i][0].Rmax);
     101    DminSkyRegion = MIN(DminSkyRegion, skylist[0].regions[i][0].Dmin);
     102    DmaxSkyRegion = MAX(DmaxSkyRegion, skylist[0].regions[i][0].Dmax);
     103  }
     104  RmidSkyRegion = 0.5*(RminSkyRegion + RmaxSkyRegion);
     105  MARKTIME("create sky region coords: %f sec\n", dtime);
    83106
    84107  dsortindex (RmaxSky, index, skylist[0].Nregions);
     108  MARKTIME("sort sky coords: %f sec\n", dtime);
    85109
    86110  if (VERBOSE) fprintf (stderr, "finding images\n");
    87111  BuildChipMatch (timage, Ntimage);
     112  MARKTIME("build chip match: %f sec\n", dtime);
    88113
    89114  nimage = 0;
     
    124149    }
    125150
    126     /* define image corners */
    127     Xi[0] = 0;            Yi[0] = 0;
    128     Xi[1] = timage[i].NX; Yi[1] = 0;
    129     Xi[2] = timage[i].NX; Yi[2] = timage[i].NY;
    130     Xi[3] = 0;            Yi[3] = timage[i].NY;
    131     Xi[4] = 0;            Yi[4] = 0;
     151    /* define image corners - note the DIS images (mosaic phu) are special */
     152    if (!strcmp(&timage[i].coords.ctype[4], "-DIS")) {
     153      Xi[0] = -0.5*timage[i].NX; Yi[0] = -0.5*timage[i].NY;
     154      Xi[1] = +0.5*timage[i].NX; Yi[1] = -0.5*timage[i].NY;
     155      Xi[2] = +0.5*timage[i].NX; Yi[2] = +0.5*timage[i].NY;
     156      Xi[3] = -0.5*timage[i].NX; Yi[3] = +0.5*timage[i].NY;
     157      Xi[4] = -0.5*timage[i].NX; Yi[4] = -0.5*timage[i].NY;
     158    } else {
     159      Xi[0] = 0;            Yi[0] = 0;
     160      Xi[1] = timage[i].NX; Yi[1] = 0;
     161      Xi[2] = timage[i].NX; Yi[2] = timage[i].NY;
     162      Xi[3] = 0;            Yi[3] = timage[i].NY;
     163      Xi[4] = 0;            Yi[4] = 0;
     164    }
    132165    found = FALSE;
    133166
    134167    /* transform corners to ra,dec */
    135168    double RminImage = 360.0;
     169    double RmaxImage =   0.0;
     170    double DminImage = +90.0;
     171    double DmaxImage = -90.0;
     172    // int leftside = FALSE;
    136173    for (j = 0; j < 5; j++) {
    137174      XY_to_RD (&Ri[j], &Di[j], Xi[j], Yi[j], &timage[i].coords);
     175      Ri[j] = ohana_normalize_angle_to_midpoint (Ri[j], RmidSkyRegion);
     176
    138177      RminImage = MIN(RminImage, Ri[j]);
    139     }
     178      RmaxImage = MAX(RmaxImage, Ri[j]);
     179      DminImage = MIN(DminImage, Di[j]);
     180      DmaxImage = MAX(DmaxImage, Di[j]);
     181    }
     182    if (RmaxImage - RminImage > 180.0) {
     183        double tmp = RminImage;
     184        RmaxImage = RminImage;
     185        RminImage = tmp - 360.0;
     186    }
     187
     188    // check that this image is even in range of the searched region
     189    if (DminImage > DmaxSkyRegion) continue;
     190    if (DmaxImage < DminSkyRegion) continue;
     191   
     192    // the sky region RA is defined to be 0 - 360.0
     193    if (RminImage > RmaxSkyRegion) continue;
     194    if (RmaxImage < RminSkyRegion) continue;
     195
     196    // image overlaps region, keep it
     197    if (USE_BASIC_CHECK) goto found_it;
    140198
    141199    // RA(nStart) is guaranteed to be < RminImage:
     
    161219      for (j = 0; (j < 4) && !found; j++) {
    162220        found = corner_check (&Xi[j], &Yi[j], &skycoords[m].Xc[0], &skycoords[m].Yc[0]);
     221        if (found) goto found_it;
    163222      }
    164223      /* check if catalog corner inside image */
    165224      for (j = 0; (j < 4) && !found; j++) {
    166225        found = corner_check (&skycoords[m].Xc[j], &skycoords[m].Yc[j], &Xi[0], &Yi[0]);
     226        if (found) goto found_it;
    167227      }
    168228      /* check if edges cross */
     
    170230        for (k = 0; (k < 4) && !found; k++) {
    171231          found = edge_check (&Xi[j], &Yi[j], &skycoords[m].Xc[k], &skycoords[m].Yc[k]);
     232          if (found) goto found_it;
    172233        }
    173234      }
    174       if (!found) continue;
    175 
    176       image[nimage] = timage[i];
    177       /* always allow 'few' images to succeed, if possible */
    178       if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) {
    179         image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
    180       }
    181       if (RESET) {
    182         // XXX do we need / want to do this in relastro?
    183         assignMcal (&image[nimage], (double *) NULL, -1);
    184         image[nimage].dMcal = NAN;
    185         image[nimage].flags &= ~badImage;
    186       }
    187       line_number[nimage] = i;
    188       nimage ++;
    189       if (nimage == NIMAGE) {
    190         NIMAGE += 100;
    191         REALLOCATE (image, Image, NIMAGE);
    192         REALLOCATE (line_number, off_t, NIMAGE);
    193       }
    194     }
    195   }
    196      
     235    }
     236    if (!found) continue;
     237
     238  found_it:
     239    image[nimage] = timage[i];
     240    /* always allow 'few' images to succeed, if possible */
     241    if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) {
     242      image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
     243    }
     244    if (RESET) {
     245      // XXX do we need / want to do this in relastro?
     246      // assignMcal (&image[nimage], (double *) NULL, -1);
     247      // image[nimage].Mcal = NAN;
     248      // image[nimage].dMcal = NAN;
     249      image[nimage].flags &= ~badImage;
     250    }
     251    line_number[nimage] = i;
     252    nimage ++;
     253    if (nimage == NIMAGE) {
     254      NIMAGE += 100;
     255      REALLOCATE (image, Image, NIMAGE);
     256      REALLOCATE (line_number, off_t, NIMAGE);
     257    }
     258  }
     259  MARKTIME("finish image selection: %f sec\n", dtime);
     260   
    197261  if (VERBOSE) fprintf (stderr, "found "OFF_T_FMT" images\n",  nimage);
    198262
  • branches/czw_branch/20101203/Ohana/src/relphot/Makefile

    r17242 r30631  
    2929$(SRC)/StarOps.$(ARCH).o         \
    3030$(SRC)/args.$(ARCH).o            \
     31$(SRC)/help.$(ARCH).o            \
    3132$(SRC)/bcatalog.$(ARCH).o        \
    3233$(SRC)/global_stats.$(ARCH).o    \
  • branches/czw_branch/20101203/Ohana/src/relphot/include/relphot.h

    r27586 r30631  
    1313  float Mcal;
    1414  float dMcal;
     15  float dMsys;
     16  short nFitPhotom;
    1517  short Xm;
    1618  float secz;
     
    5052double STAR_CHISQ;
    5153double MIN_ERROR;
     54double IMFIT_SYS_SIGMA_LIM;
    5255
    5356int    VERBOSE;
     
    103106SkyRegion UserPatch;
    104107int UserPatchSelect;
     108
     109int USE_BASIC_CHECK;
    105110
    106111# ifdef GRID_V1
     
    215220void          write_coords        PROTO((Header *header, Coords *coords));
    216221int relphot_objects (void);
     222
     223void relphot_usage (void);
     224void relphot_help (int argc, char **argv);
  • branches/czw_branch/20101203/Ohana/src/relphot/src/ConfigInit.c

    r25757 r30631  
    2020  GetConfig (config, "MAG_LIM",                "%lf", 0, &MAG_LIM);
    2121  GetConfig (config, "SIGMA_LIM",              "%lf", 0, &SIGMA_LIM);
     22
     23  if (!ScanConfig (config, "RELPHOT_IMFIT_SYS_SIGMA_LIM", "%lf", 0, &IMFIT_SYS_SIGMA_LIM)) IMFIT_SYS_SIGMA_LIM = 0.01;
     24
    2225  GetConfig (config, "STAR_SCATTER",           "%lf", 0, &STAR_SCATTER);
    2326  GetConfig (config, "IMAGE_SCATTER",          "%lf", 0, &IMAGE_SCATTER);
  • branches/czw_branch/20101203/Ohana/src/relphot/src/ImageOps.c

    r29001 r30631  
    124124  free (clist);
    125125  free (mlist);
     126  free (Nlist);
     127  free (NLIST);
    126128}
    127129
     
    299301
    300302  off_t i, j, m, c, n, N, Nmax, mark, bad;
    301   int Nfew, Nbad, Nmos, Ngrid, Nrel, Nsys;
    302   float Msys, Mrel, Mmos, Mgrid;
     303  int Nfew, Nbad, Nmos, Ngrid, Nrel, Nsys, Nbright;
     304  float Msys, Mrel, Mmos, Mgrid, McalBright, McalBright2;
    303305  double *list, *dlist;
    304306  StatType stats;
     
    318320
    319321  Nfew = Nbad = Nmos = Ngrid = Nrel = Nsys = 0;
     322
     323  // counters to measure the bright-end scatter
     324  McalBright = McalBright2 = 0.0;
     325  Nbright = 0;
    320326
    321327  for (i = 0; i < Nimage; i++) {
     
    361367      list[N] = Msys - Mrel - Mmos - Mgrid;
    362368      dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
     369      if (catalog[c].measure[m].dM < IMFIT_SYS_SIGMA_LIM) {
     370        McalBright += list[N];
     371        McalBright2 += SQ(list[N]);
     372        Nbright ++;
     373      }
    363374      N++;
    364375    }
     
    378389    liststats (list, dlist, N, &stats);
    379390    image[i].Mcal  = stats.mean;
    380     image[i].dMcal = stats.sigma;
     391    image[i].dMcal = stats.error;
     392    image[i].dMagSys = stats.sigma;
     393    image[i].nFitPhotom = N;
    381394    image[i].Xm    = 100.0*log10(stats.chisq);
    382395  }
  • branches/czw_branch/20101203/Ohana/src/relphot/src/MosaicOps.c

    r29001 r30631  
    7373    mosaic[Nmosaic].Mcal  = 0.0;
    7474    mosaic[Nmosaic].dMcal = 0.0;
     75    mosaic[Nmosaic].dMsys = 0.0;
    7576    mosaic[Nmosaic].Xm    = 0.0;
    7677    mosaic[Nmosaic].flags  = image[i].flags;
     
    189190      image[im].dMcal = mosaic[i].dMcal;
    190191      image[im].Xm = mosaic[i].Xm;
     192      image[im].dMagSys = mosaic[i].dMsys;
     193      image[im].nFitPhotom = mosaic[i].nFitPhotom;
    191194      image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_FEW);
    192195      image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_POOR);
     
    400403      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
    401404      if (mark) {
    402         fprintf (stderr, "marked mosaic %s ("OFF_T_FMT"), ("OFF_T_FMT" < %d) || ("OFF_T_FMT" < %f*"OFF_T_FMT")\n", image[imlist[i][0]].name,  i,  N, IMAGE_TOOFEW,  N, IMAGE_GOOD_FRACTION,  Nlist[i]);
     405        if (VERBOSE2) { fprintf (stderr, "marked mosaic %s ("OFF_T_FMT"), ("OFF_T_FMT" < %d) || ("OFF_T_FMT" < %f*"OFF_T_FMT")\n", image[imlist[i][0]].name,  i,  N, IMAGE_TOOFEW,  N, IMAGE_GOOD_FRACTION,  Nlist[i]); }
    403406        mosaic[i].flags |= ID_IMAGE_PHOTOM_FEW;
    404407        Nfew ++;
     
    410413    if (PoorImages) fprintf (stderr, "Mmos: %f %f %d "OFF_T_FMT"\n", stats.mean, stats.sigma, stats.Nmeas,  N);
    411414    mosaic[i].Mcal  = stats.mean;
    412     mosaic[i].dMcal = stats.sigma;
     415    mosaic[i].dMcal = stats.error;
     416    mosaic[i].dMsys = stats.sigma;
     417    mosaic[i].nFitPhotom = N;
    413418    mosaic[i].Xm    = 100.0*log10(stats.chisq);
    414419  }
  • branches/czw_branch/20101203/Ohana/src/relphot/src/args.c

    r27586 r30631  
    11# include "relphot.h"
    2 void usage (void);
    32
    43int args (int argc, char **argv) {
     
    5352  }
    5453
     54  USE_BASIC_CHECK = FALSE;
     55  if ((N = get_argument (argc, argv, "-basic-image-search"))) {
     56    remove_argument (N, &argc, argv);
     57    USE_BASIC_CHECK = TRUE;
     58  }
     59
    5560  VERBOSE = VERBOSE2 = FALSE;
    5661  if ((N = get_argument (argc, argv, "-v"))) {
     
    228233  if (UpdateAverages && (argc == 1)) return TRUE;
    229234  if (UserPatchSelect && (argc == 2)) return TRUE;
    230   if (argc != 3) usage ();
     235  if (argc != 3) relphot_usage ();
    231236
    232237  return TRUE;
    233238}
    234 
    235 void usage () {
    236   fprintf (stderr, "ERROR: USAGE: relphot (region) (photcode)\n");
    237   fprintf (stderr, "       or:    relphot (photcode) -region RA RA DEC DEC\n");
    238   fprintf (stderr, "       or:    relphot -averages -region RA RA DEC DEC\n");
    239   fprintf (stderr, "  options: \n");
    240   fprintf (stderr, "  -time (start) (stop)\n");
    241   fprintf (stderr, "  -v : verbose output\n");
    242   fprintf (stderr, "  -vv : more verbose output\n");
    243   fprintf (stderr, "  -outroot (outroot)\n");
    244   fprintf (stderr, "  -plot\n");
    245   fprintf (stderr, "  -plotdelay (seconds)\n");
    246   fprintf (stderr, "  -statmode (mode)\n");
    247   fprintf (stderr, "  -refcode (name) : give extra weight to this photcode\n");
    248   fprintf (stderr, "  -n (nloop)\n");
    249   fprintf (stderr, "  -reset\n");
    250   fprintf (stderr, "  -update\n");
    251   fprintf (stderr, "  -params\n");
    252   fprintf (stderr, "  -mosaic (mosaic)\n");
    253   fprintf (stderr, "  -imfreeze\n");
    254   fprintf (stderr, "  -grid\n");
    255   fprintf (stderr, "  -area Xmin Xmax Ymin Ymax\n");
    256   fprintf (stderr, "  -instmag min max\n");
    257   fprintf (stderr, "  \n");
    258   exit (2);
    259 }
  • branches/czw_branch/20101203/Ohana/src/relphot/src/bcatalog.c

    r28660 r30631  
    119119
    120120  if (VERBOSE) {
    121     fprintf (stderr, "using "OFF_T_FMT" stars ("OFF_T_FMT" measures) of "OFF_T_FMT" for catalog\n",
    122               subcatalog[0].Naverage,  subcatalog[0].Nmeasure,  i);
     121    fprintf (stderr, "using "OFF_T_FMT" stars ("OFF_T_FMT" measures) of "OFF_T_FMT" for catalog %s\n",
     122             subcatalog[0].Naverage,  subcatalog[0].Nmeasure,  i, catalog[0].filename);
    123123    fprintf (stderr, "rejections: %d code, %d time, %d dophot, %d mag, %d sigma, %d imag, %d few\n",
    124124             Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew);
  • branches/czw_branch/20101203/Ohana/src/relphot/src/initialize.c

    r29001 r30631  
    55  int N;
    66
     7  relphot_help (argc, argv);
    78  ConfigInit (&argc, argv);
    89  args (argc, argv);
  • branches/czw_branch/20101203/Ohana/src/relphot/src/load_catalogs.c

    r29754 r30631  
    66  Catalog *catalog, tcatalog;
    77
    8   if (VERBOSE) fprintf (stderr, "loading catalog data\n");
     8  if (VERBOSE2) fprintf (stderr, "loading catalog data\n");
    99
    1010  ALLOCATE (catalog, Catalog, skylist[0].Nregions);
     
    2222    tcatalog.Nsecfilt  = GetPhotcodeNsecfilt ();               // set the desired number in case we need to create the catalog
    2323
    24     if (!dvo_catalog_open (&tcatalog, skylist[0].regions[i], VERBOSE, "r")) {
     24    if (!dvo_catalog_open (&tcatalog, skylist[0].regions[i], VERBOSE2, "r")) {
    2525      fprintf (stderr, "ERROR: failure reading catalog %s\n", tcatalog.filename);
    2626      exit (1);
    2727    }
    28     if (VERBOSE && !tcatalog.Naves_disk) fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename);
     28    if (!tcatalog.Naves_disk) {
     29        if (VERBOSE2) { fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename); }
     30        dvo_catalog_unlock (&tcatalog);
     31        dvo_catalog_free (&tcatalog);
     32        continue;
     33    }
     34
    2935    Nstar_total += tcatalog.Naverage;
    3036    Nmeas_total += tcatalog.Nmeasure;
  • branches/czw_branch/20101203/Ohana/src/relphot/src/load_images.c

    r29001 r30631  
    11# include "relphot.h"
     2
     3# define MARKTIME(MSG,...) { \
     4  float dtime; \
     5  gettimeofday (&stop, (void *) NULL); \
     6  dtime = DTIME (stop, start); \
     7  fprintf (stderr, MSG, __VA_ARGS__); }
    28
    39SkyList *load_images (FITS_DB *db, char *regionName, SkyRegion *region, int RegionSelect) {
     
    612  off_t      Nimage, Nsubset, Nchar;
    713  off_t     *LineNumber;
     14  struct timeval start, stop;
    815
    916  SkyTable *sky = NULL;
    1017  SkyList *skylist = NULL;
     18
     19  gettimeofday (&start, (void *) NULL);
    1120
    1221  // load the current sky table (layout of all SkyRegions)
     
    2938      exit (2);
    3039  }
     40  MARKTIME("read image table: %f sec\n", dtime);
    3141
    3242  // select the images which overlap the selected sky regions
    3343  subset = select_images (skylist, image, Nimage, &LineNumber, &Nsubset);
     44  MARKTIME("selected images: %f sec\n", dtime);
    3445
    3546  gfits_vtable_from_ftable (&db[0].ftable, &db[0].vtable, LineNumber, Nsubset);
     47  MARKTIME("converted ftable to vtable: %f sec\n", dtime);
    3648
    3749  initImages (subset, Nsubset);
     50  MARKTIME("init images: %f sec\n", dtime);
     51
    3852  initMosaics (subset, Nsubset);
     53  MARKTIME("init mosaics: %f sec\n", dtime);
    3954 
    4055  return (skylist);
  • branches/czw_branch/20101203/Ohana/src/relphot/src/relphot.c

    r27480 r30631  
    8585  if (PLOTSTUFF) {
    8686    plot_star_coords (catalog, Ncatalog);
    87     plot_mosaic_fields (catalog);
     87    // plot_mosaic_fields (catalog);
    8888  }
    8989
  • branches/czw_branch/20101203/Ohana/src/relphot/src/select_images.c

    r29001 r30631  
    1515void dsortindex (double *X, off_t *Y, int N);
    1616off_t getRegionStartByRA (double R, double *Rref, off_t Nregions);
     17
     18# define MARKTIME(MSG,...) { \
     19  float dtime; \
     20  gettimeofday (&stop, (void *) NULL); \
     21  dtime = DTIME (stop, start); \
     22  fprintf (stderr, MSG, __VA_ARGS__); }
    1723
    1824Image *select_images (SkyList *skylist, Image *timage, off_t Ntimage, off_t **LineNumber, off_t *Nimage) {
     
    2531  Coords tcoords;
    2632  SkyRegionCoords *skycoords;
    27  
     33  struct timeval start, stop;
     34 
     35  double RmaxSkyRegion, RminSkyRegion, DminSkyRegion, DmaxSkyRegion, RmidSkyRegion;
     36
    2837  double *RmaxSky;
    2938  off_t *index;
     
    3544    return NULL;
    3645  }
     46
     47  gettimeofday (&start, (void *) NULL);
    3748
    3849  // the comparison is made in the catalog local projection. below we set crval1,2
     
    4758  ALLOCATE (RmaxSky, double, skylist[0].Nregions);
    4859  ALLOCATE (index, off_t, skylist[0].Nregions);
     60
     61  RminSkyRegion = +360.0;
     62  RmaxSkyRegion = -360.0;
     63  DminSkyRegion = +90.0;
     64  DmaxSkyRegion = -90.0;
    4965
    5066  /* compare with each region file */
     
    7591    skycoords[i].Xc[3] -= dx; skycoords[i].Yc[3] += dy;
    7692    skycoords[i].Xc[4] -= dx; skycoords[i].Yc[4] -= dy;
    77   }
     93
     94    RminSkyRegion = MIN(RminSkyRegion, skylist[0].regions[i][0].Rmin);
     95    RmaxSkyRegion = MAX(RmaxSkyRegion, skylist[0].regions[i][0].Rmax);
     96    DminSkyRegion = MIN(DminSkyRegion, skylist[0].regions[i][0].Dmin);
     97    DmaxSkyRegion = MAX(DmaxSkyRegion, skylist[0].regions[i][0].Dmax);
     98  }
     99  RmidSkyRegion = 0.5*(RminSkyRegion + RmaxSkyRegion);
     100  MARKTIME("create sky region coords: %f sec\n", dtime);
    78101
    79102  dsortindex (RmaxSky, index, skylist[0].Nregions);
     103  MARKTIME("sort sky coords: %f sec\n", dtime);
    80104
    81105  if (VERBOSE) fprintf (stderr, "finding images\n");
    82106  BuildChipMatch (timage, Ntimage);
     107  MARKTIME("build chip match: %f sec\n", dtime);
    83108
    84109  nimage = 0;
     
    100125    }
    101126   
     127    // this adds 1.3 sec for 3M images
    102128    if (!FindMosaicForImage (timage, Ntimage, i)) {
    103129      fprintf (stderr, "cannot find mosaic for "OFF_T_FMT"\n", i);
     
    113139    found = FALSE;
    114140
    115     /* transform corners to ra,dec */
     141    /* transform corners to ra,dec -- costs ~3sec for 3M images */
    116142    double RminImage = 360.0;
     143    double RmaxImage =   0.0;
     144    double DminImage = +90.0;
     145    double DmaxImage = -90.0;
    117146    for (j = 0; j < 5; j++) {
    118147      XY_to_RD (&Ri[j], &Di[j], Xi[j], Yi[j], &timage[i].coords);
     148      Ri[j] = ohana_normalize_angle_to_midpoint (Ri[j], RmidSkyRegion);
     149     
    119150      RminImage = MIN(RminImage, Ri[j]);
    120     }
    121 
    122     // RA(nStart) is guaranteed to be < RminImage:
     151      RmaxImage = MAX(RmaxImage, Ri[j]);
     152      DminImage = MIN(DminImage, Di[j]);
     153      DmaxImage = MAX(DmaxImage, Di[j]);
     154    }
     155    if (RmaxImage - RminImage > 180.0) {
     156        double tmp = RminImage;
     157        RmaxImage = RminImage;
     158        RminImage = tmp - 360.0;
     159    }
     160   
     161    // check that this image is even in range of the searched region
     162    if (DminImage > DmaxSkyRegion) continue;
     163    if (DmaxImage < DminSkyRegion) continue;
     164   
     165    // the sky region RA is defined to be 0 - 360.0
     166    if (RminImage > RmaxSkyRegion) continue;
     167    if (RmaxImage < RminSkyRegion) continue;
     168
     169    // image overlaps region, keep it
     170    if (USE_BASIC_CHECK) goto found_it;
     171
     172    // RA(nStart) is guaranteed to be < RminImage: -- costs 0.5sec for 3M images
    123173    nStart = getRegionStartByRA (RminImage, RmaxSky, skylist[0].Nregions);
    124174
    125175    /* compare with each region file */
    126     for (iSky = 0; (iSky < skylist[0].Nregions) && !found; iSky++) {
     176    for (iSky = nStart; (iSky < skylist[0].Nregions) && !found; iSky++) {
    127177
    128178      m = index[iSky];
     
    142192      for (j = 0; (j < 4) && !found; j++) {
    143193        found = corner_check (&Xi[j], &Yi[j], &skycoords[m].Xc[0], &skycoords[m].Yc[0]);
     194        if (found) goto found_it;
    144195      }
    145196      /* check if catalog corner inside image */
    146197      for (j = 0; (j < 4) && !found; j++) {
    147198        found = corner_check (&skycoords[m].Xc[j], &skycoords[m].Yc[j], &Xi[0], &Yi[0]);
     199        if (found) goto found_it;
    148200      }
    149201      /* check if edges cross */
     
    151203        for (k = 0; (k < 4) && !found; k++) {
    152204          found = edge_check (&Xi[j], &Yi[j], &skycoords[m].Xc[k], &skycoords[m].Yc[k]);
     205          if (found) goto found_it;
    153206        }
    154207      }
    155       if (!found) continue;
    156 
    157       image[nimage] = timage[i];
    158       /* always allow 'few' images to succeed, if possible */
    159       if (image[nimage].flags & ID_IMAGE_PHOTOM_FEW) {
    160         image[nimage].flags &= ~(ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR);
    161       }
    162       if (RESET) {
    163         assignMcal (&image[nimage], (double *) NULL, -1);
    164         image[nimage].dMcal = NAN;
    165         image[nimage].flags &= ~ID_IMAGE_PHOTOM_POOR;
    166       }
    167       line_number[nimage] = i;
    168       nimage ++;
    169       if (nimage == NIMAGE) {
    170         NIMAGE += 100;
    171         REALLOCATE (image, Image, NIMAGE);
    172         REALLOCATE (line_number, off_t, NIMAGE);
    173       }
    174     }
    175   }
    176      
     208    }
     209    if (!found) continue;
     210
     211  found_it:
     212    image[nimage] = timage[i];
     213    /* always allow 'few' images to succeed, if possible */
     214    if (image[nimage].flags & ID_IMAGE_PHOTOM_FEW) {
     215      image[nimage].flags &= ~(ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR);
     216    }
     217    if (RESET) {
     218      // XXX assignMcal (&image[nimage], (double *) NULL, -1);
     219      // XXX this needs to be thought through a bit more
     220      image[nimage].Mcal = 0.0;
     221      image[nimage].dMcal = NAN;
     222      image[nimage].flags &= ~ID_IMAGE_PHOTOM_POOR;
     223    }
     224    line_number[nimage] = i;
     225    nimage ++;
     226    if (nimage == NIMAGE) {
     227      NIMAGE += 100;
     228      REALLOCATE (image, Image, NIMAGE);
     229      REALLOCATE (line_number, off_t, NIMAGE);
     230    }
     231  }
     232  MARKTIME("finish image selection: %f sec\n", dtime);
     233
    177234  if (VERBOSE) fprintf (stderr, "found "OFF_T_FMT" images\n", nimage);
    178235
     
    295352  return (Nlo);
    296353}
     354
     355off_t getRegionStopByRA (double R, double *Rref, off_t Nregions) {
     356
     357  // use bisection to find the overlapping mosaic
     358
     359  off_t Nlo, Nhi, N;
     360
     361  // find the last mosaic before start
     362  Nlo = 0; Nhi = Nregions;
     363  while (Nhi - Nlo > 10) {
     364    N = 0.5*(Nlo + Nhi);
     365    if (Rref[N] < R) {
     366      Nlo = MAX(N, 0);
     367    } else {
     368      Nhi = MIN(N, Nregions);
     369    }
     370  }
     371  return (Nlo);
     372}
  • branches/czw_branch/20101203/Ohana/src/skycalc/Makefile

    r19681 r30631  
    2020libskycalc: $(DESTLIB)/libskycalc.a $(DESTLIB)/libskycalc.$(DLLTYPE)
    2121
    22 all: libskycalc dusktime moondata sundata
     22all: libskycalc dusktime moondata sundata sunriseset
    2323programs: all
    2424
     
    4343
    4444dusktime : $(BIN)/dusktime.$(ARCH)
     45sunriseset : $(BIN)/sunriseset.$(ARCH)
    4546moondata : $(BIN)/moondata.$(ARCH)
    4647sundata  : $(BIN)/sundata.$(ARCH)
     
    4849
    4950DUSKTIME = $(SRC)/dusktime.$(ARCH).o
     51SUNRISESET = $(SRC)/sunriseset.$(ARCH).o
    5052MOONDATA = $(SRC)/moondata.$(ARCH).o
    5153SUNDATA  = $(SRC)/sundata.$(ARCH).o
     
    5355
    5456$(DUSKTIME) : $(MYINCS)
     57$(SUNRISESET) : $(MYINCS)
    5558$(MOONDATA) : $(MYINCS)
    5659$(SUNDATA)  : $(MYINCS)
     
    5861
    5962$(BIN)/dusktime.$(ARCH) : $(DUSKTIME)
     63$(BIN)/sunriseset.$(ARCH) : $(SUNRISESET)
    6064$(BIN)/moondata.$(ARCH) : $(MOONDATA)
    6165$(BIN)/sundata.$(ARCH)  : $(SUNDATA)
    6266$(BIN)/sunmoon.$(ARCH)  : $(SUNMOON)
    6367
    64 INSTALL = dusktime moondata sundata sunmoon
     68INSTALL = dusktime sunriseset moondata sundata sunmoon
    6569
    6670# dependancy rules for binary code #########################
  • branches/czw_branch/20101203/Ohana/src/tools/src/roc.c

    r30586 r30631  
    44# include <regex.h>
    55
    6 // # define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); abort(); } }
    7 # define myAssert(LOGIC,MSG) { if (!(LOGIC)) { fprintf (stderr, "%s\n", MSG); exit(1); } }
     6# define myAssert(LOGIC,...) { if (!(LOGIC)) { fprintf (stderr, __VA_ARGS__); abort(); } }
    87
    98# define ROC_HEADER_SIZE  0x1000
     
    1514int roc_insert (int argc, char **argv);
    1615int roc_delete (int argc, char **argv);
     16int roc_validate (int argc, char **argv);
     17
    1718int usage (void);
    1819int print_block (char *header, int *header_size, int *Noff, char *format,...);
     
    2930  if (!strcasecmp(argv[1], "-insert")) roc_insert (argc, argv);
    3031  if (!strcasecmp(argv[1], "-delete")) roc_delete (argc, argv);
     32  if (!strcasecmp(argv[1], "-validate")) roc_validate (argc, argv);
    3133
    3234  usage();
     
    302304}
    303305
     306int roc_validate (int argc, char **argv) {
     307
     308  int i, j, n, Ninput, Nblocks, header_size, Nbytes, Nread;
     309  off_t *sizes, *bytes_read;
     310  char value;
     311  char *header, *line, *ptr;
     312  char **inputName, *targetName;
     313  char **inputData;
     314  char *targetData;
     315  FILE **input, *target;
     316
     317  if (argc != 3) usage();
     318
     319
     320  /* find the md5 sum for each input file (ADD LATER)
     321   * find the size of the input files
     322   * define the output file size = MAX(sizes)
     323   * create the target file header
     324   */
     325
     326  // the output file
     327  targetName = argv[2];
     328
     329  target = fopen(targetName, "r");
     330  myAssert (target, "failed to open roc datafile %s\n",targetName);
     331 
     332  ALLOCATE (line, char, 1024);
     333
     334  scan_line (target, line);
     335  myAssert (!strncmp(line, "ROC Version 0", strlen("ROC Verion 0")), "invalid ROC file or format in %s",targetName);
     336
     337  scan_line (target, line);
     338  sscanf (line, "HEADER SIZE %x", &header_size);
     339
     340  ALLOCATE (header, char, header_size);
     341
     342  // read the full header
     343  fseeko (target, 0, SEEK_SET);
     344  Nbytes = fread (header, 1, header_size, target);
     345  myAssert (Nbytes == header_size, "failed to read header data\n");
     346
     347  ptr = header;
     348  ptr = strchr (ptr, '\n'); ptr ++; // ROC Version
     349  ptr = strchr (ptr, '\n'); ptr ++; // HEADER SIZE
     350 
     351  sscanf (ptr, "N_FILES %d", &Ninput); ptr = strchr (ptr, '\n'); ptr ++;
     352  sscanf (ptr, "N_BLOCKS %d", &Nblocks); ptr = strchr (ptr, '\n'); ptr ++;
     353
     354  // the input files
     355  ALLOCATE (inputName, char *, Ninput);
     356  ALLOCATE (sizes, off_t, Ninput);
     357  ALLOCATE (bytes_read, off_t, Ninput);
     358  for (i = 0; i < Ninput; i++) {
     359    ALLOCATE (inputName[i], char, 1024);
     360    sscanf (ptr, "%*s : %s", inputName[i]); ptr = strchr (ptr, '\n'); ptr ++;
     361    ptr = strchr (ptr, '\n'); ptr ++; // BASE
     362    sscanf (ptr, "%*s : "OFF_T_FMT, &sizes[i]); ptr = strchr (ptr, '\n'); ptr ++;
     363    ptr = strchr (ptr, '\n'); ptr ++; // MD5
     364    bytes_read[i] = 0;
     365  }
     366
     367  ALLOCATE (input, FILE *, Ninput);
     368  for (i = 0; i < Ninput; i++) {
     369    input[i] = fopen(inputName[i], "r");
     370    myAssert (input[i], "failed to open file %s in %s\n",inputName[i],targetName);
     371  }
     372
     373  ALLOCATE (inputData, char *, Ninput);
     374  ALLOCATE (targetData, char, ROC_BLOCKSIZE);
     375  for (i = 0; i < Ninput; i++) {
     376    ALLOCATE (inputData[i], char, ROC_BLOCKSIZE);
     377  }
     378
     379  for (n = 0; n < Nblocks; n++) {
     380    for (i = 0; i < Ninput; i++) {
     381      Nread = MIN (ROC_BLOCKSIZE, sizes[i] - bytes_read[i]);
     382      Nbytes = fread (inputData[i], 1, Nread, input[i]);
     383      myAssert (Nbytes == Nread, "failed to read data file: %s read: %d expect: %d prev_read: %d block: %d in %s\n",inputName[i],Nbytes,Nread,(int) bytes_read[i],n,targetName);
     384      if (Nread < ROC_BLOCKSIZE) {
     385        // if we have reached the end of the file, fill in the rest with NULLs
     386        memset (&inputData[i][Nread], 0, ROC_BLOCKSIZE - Nread);
     387      }
     388      bytes_read[i] += Nread;
     389    }
     390    Nbytes = fread (targetData, 1, ROC_BLOCKSIZE, target);
     391    myAssert (Nbytes == ROC_BLOCKSIZE, "failed to read roc file: %s read: %d expect %d\n", targetName, Nbytes, ROC_BLOCKSIZE);
     392   
     393    for (j = 0; j < ROC_BLOCKSIZE; j++) {
     394      value = targetData[j];
     395      for (i = 0; i < Ninput; i++) {
     396        value = value ^ inputData[i][j];
     397      }
     398      myAssert(value == 0, "validation failed on block %d and byte %d in %s\n",n,j,targetName);
     399    }
     400
     401  }
     402
     403  for (i = 0; i < Ninput; i++) {
     404    fclose(input[i]);
     405  }
     406
     407  fclose (target);
     408
     409  fprintf (stderr, "validation: %s appears correct.\n",targetName);
     410  exit (0);
     411}
     412
    304413int roc_insert (int argc, char **argv) {
    305414
     
    334443  fprintf (stderr, " -create (target) (input) (input) (input) ... [options]\n");
    335444  fprintf (stderr, " -repair (target) (file #) (output\n");
     445  fprintf (stderr, " -validate (target)\n");
    336446  fprintf (stderr, " -insert (target) (input)\n");
    337447  fprintf (stderr, " -delete (target) (file)\n");
     448
    338449  exit (2);
    339450}
  • branches/czw_branch/20101203/PS-IPP-Config/lib/PS/IPP/Config.pm

    r30586 r30631  
    11481148    if ($neb->storage_object_exists($output)) {
    11491149        if ($delete_existing) {
    1150             # avoid dead instances by moving the file before deleting it.
    1151             # append current time to form new name
    1152             my $todelete;
    1153             eval {
    1154                 # parse the key so that we can compute the new name
    1155                 require Nebulous::Key;
    1156             };
    1157             if ($@) {
    1158                 carp "Can't find Nebulous::Key";
    1159                 $$r_error = $PS_EXIT_CONFIG_ERROR;
    1160                 return undef;
    1161             }
    1162             eval {
    1163                 # parse the key so that we can compute the new name
    1164                 my $neb_key = Nebulous::Key::parse_neb_key($output);
    1165                 die "parse_neb_key failed" if !$neb_key;
    1166                 my $path = $neb_key->path;
    1167                 die "neb_key has no path" if !$path;
    1168                 my $ticks = time();
    1169                 $todelete = "ipp_trash/$path.$ticks";
    1170                 $neb->move($output, $todelete);
    1171             };
    1172             if ($@) {
    1173                 carp "nebulous move failed for $output";
     1150            $$r_error = $self->_kill_nebulous_file($output);
     1151            if ($$r_error) {
    11741152                $output = undef;
    1175                 $$r_error = $PS_EXIT_SYS_ERROR;
    1176             }
    1177             if ($todelete) {
    1178                 eval {
    1179                     $neb->delete($todelete);
    1180                 };
    1181                 if ($@) {
    1182                     carp "nebulous delete for $todelete failed. Ignoring.\n";
    1183                     $$r_error = $PS_EXIT_SYS_ERROR;
    1184                 }
    11851153            }
    11861154        } else {
    11871155            # Make sure that there is only 1 instance.
    1188 
    11891156            eval {
    11901157                $neb->there_can_be_only_one($output);
     
    11981165    }
    11991166    return $output;
     1167}
     1168
     1169# _kill_nebulous_file: reliably get a nebulous file out of way.
     1170# First move it to the trash then attempt to delete it. No failure if delete fails.
     1171# The file is in the trash.
     1172# Assumes that file is a nebulous file with a storage object that exists
     1173# Users should call kill_file()
     1174# Returns 0 on success otherwise and a PS_EXIT error code on failure
     1175sub _kill_nebulous_file {
     1176    my $self = shift;
     1177    my $filename = shift;
     1178    my $neb = $self->nebulous;
     1179
     1180    # avoid dead instances by moving the file before deleting it.
     1181    # append current time to form new name
     1182    my $todelete;
     1183    eval {
     1184        # parse the key so that we can compute the new name
     1185        require Nebulous::Key;
     1186    };
     1187    if ($@) {
     1188        carp "Can't find Nebulous::Key";
     1189        return $PS_EXIT_CONFIG_ERROR;
     1190    }
     1191    eval {
     1192        # parse the key so that we can compute the new name
     1193        my $neb_key = Nebulous::Key::parse_neb_key($filename);
     1194        die "parse_neb_key failed" if !$neb_key;
     1195        my $path = $neb_key->path;
     1196        die "neb_key has no path" if !$path;
     1197        my $ticks = time();
     1198        $todelete = "ipp_trash/$path.$ticks";
     1199        $neb->move($filename, $todelete);
     1200    };
     1201    if ($@) {
     1202        carp "nebulous move failed for $filename";
     1203        return $PS_EXIT_SYS_ERROR;
     1204    }
     1205    if ($todelete) {
     1206        eval {
     1207            $neb->delete($todelete);
     1208        };
     1209        if ($@) {
     1210            carp "nebulous delete for $todelete failed. Ignoring.\n";
     1211            return $PS_EXIT_SYS_ERROR;
     1212        }
     1213    }
     1214    return 0;
     1215}
     1216
     1217# user level interface to kill_file reliably get a file out of way.
     1218#
     1219# If a nebulous file, move it to the trash then attempt to delete it.
     1220# if non nebulous file just delete it.
     1221# Returns 0 on success otherwise and a PS_EXIT error code on failure
     1222
     1223sub kill_file {
     1224    my $self = shift;
     1225    my $filename = shift;
     1226    my $neb = $self->nebulous;
     1227
     1228    my $scheme = file_scheme($filename);
     1229    if (!$scheme or ($scheme ne 'neb')) {
     1230        if ($self->file_exists($filename)) {
     1231            if (!$self->file_delete($filename)) {
     1232                carp "failed to delete $filename";
     1233                return $PS_EXIT_SYS_ERROR;
     1234            }
     1235        }
     1236    } elsif ($neb->storage_object_exists($filename)) {
     1237        $self->_kill_nebulous_file($filename);
     1238    }
     1239    return 0;
    12001240}
    12011241
  • branches/czw_branch/20101203/doc/psphot/psphot.tex

    r21455 r30631  
    159159Python).
    160160
     161\note{discuss the psphot program varients}
     162
    161163\section{PSPhot Design Goals}
    162164
     
    276278\end{itemize}
    277279
    278 Note that a given run of PSPhot \note{should} allow the user to
    279 perform any of these stages as an option.  For example, the PSF model
    280 may already be available from external information, in which case the
    281 PSF modeling stage can be skipped.  Or, when used as a library
    282 function, the image may have already been loaded and the mask and
    283 weight images constructed.  In some implementations, it may be
    284 possible to skip the initial object detection stage because only
    285 supplied sources are measured.  These are only some of the possible
    286 configurations.  The use of these different configurations depends on
    287 the source of the image, the desired detail and speed of the
    288 processing, and the level of accuracy desired from the analysis.
     280Note that a given run of PSPhot allows the user to perform many of
     281these stages only if needed.  For example, the PSF model may already be
     282available from external information, in which case the PSF modeling
     283stage can be skipped.  Or, when used as a library function, the image
     284may have already been loaded and the mask and weight images
     285constructed.  In some implementations, it may be possible to skip the
     286initial object detection stage because only supplied sources are
     287measured.  These are only some of the possible configurations.  The
     288use of these different configurations depends on the source of the
     289image, the desired detail and speed of the processing, and the level
     290of accuracy desired from the analysis.
    289291
    290292\subsection{Image Preparation}
     
    292294The first step is to prepare the image for detection of the
    293295astronomical objects.  We need three separate images: the measured
    294 flux, the corresponding noise level, and a mask defining which pixels
    295 are valid and which should be ignored.  For the stand-alone program,
    296 the input flux image is a required program argument.  When it is
    297 loaded, it is converted by default to 32-bit floating point
     296flux, the corresponding variance image, and a mask defining which
     297pixels are valid and which should be ignored.  For the stand-alone
     298program, the input flux image is a required program argument.  When it
     299is loaded, it is converted by default to 32-bit floating point
    298300representation.  In the function-call form of PSPhot, the image must
    299301be supplied by the user in 32-bit floating point format.  The noise
     
    307309automatically by PSPhot.
    308310
    309 For the mask, we use an 8-bit image in which a value of 0 represents a
    310 valid pixel.  We use each of the 8 bits to define different reasons a
    311 pixel should be ignored.  This allows use to optionally respect or
     311\note{describe the use of the covariance image}
     312\note{describe the difference between 'bad' and 'suspect' pixels}
     313
     314For the mask, we use a 16-bit image in which a value of 0 represents a
     315valid pixel.  We use each of the 16 bits to define different reasons a
     316pixel should be ignored.  This allows us to optionally respect or
    312317ignore the mask depending on the circumstance.  For example, in some
    313318cases, we ignore saturated pixels completely while in other
     
    325330\code{XMIN}, \code{XMAX}, \code{YMIN}, \code{YMAX}.
    326331
    327 \note{Mask values are currently hard-wired numbers.  We need a method
    328   for user-defined mask values to be supplied.  PSLib needs to have a
    329   mask registration system.}
     332\note{discuss the mask.config file, in which the mask meanings are assigned to bit values}
    330333
    331334The noise image, if not supplied is constructed by default from the
     
    337340valid.  For example, if the input flux image is the result of an image
    338341stack with significantly variable number of input measurements per
    339 pixel, it will necessary to supply a noise image which accurately
     342pixel, it will be necessary to supply a noise image which accurately
    340343represents the noise as a function of position in the image.
    341344
     
    343346
    344347The objects are initially detected by finding the location of local
    345 peaks in the image.  The flux image is smoothed with a very small
    346 circularly symmetric kernel using a two-pass 1D Gaussian.  At this
     348peaks in the image.  The flux and variance images are smoothed with a
     349small circularly symmetric kernel using a two-pass 1D Gaussian
     350(\note{KEYWORD?}).  The smoothed flux and variance images are combined
     351to generate a significance image in signal-to-noise units
     352\note{including correction for the covariance, if known}. At this
    347353stage, the goal is only to detect the brighter sources, above a user
    348354defined S/N limit (configuration keyword: \code{PEAK_NSIGMA}).  The
    349355detection efficiency for the brighter sources is not strongly
    350356dependent on the form of this smoothing function.
    351 
    352 \note{Is this smoothing needed?  we could save time here by skipping
    353 it.}
    354357
    355358The local peaks in the smoothed image are found by first detecting
     
    364367the maximum $X$ and $Y$ corners of the region.
    365368
    366 \note{The current implementation ignores the S/N map in making the
    367 peak detection.  This code must be modified (a la Kaiser) to be used
    368 for a peak-detection pass in a difference image or to re-find peaks in
    369 the image after the modeled objects have been subtracted}.
     369\subsection{Footprints}
     370
     371\note{need to describe the process of generating the source footprints
     372  and then culling the insignificant peaks}
     373
     374\subsubsection{Moments and related}
     375
     376\note{disucss the Kron mags}
     377
     378\note{this section is wrong: we no longer use S/N clipping, but a
     379  Gaussian window function, chosed based on the measured moment}
    370380
    371381Once a collection of peaks have been identified, basic properties of
     
    391401
    392402\subsubsection{Determination of the Peak Coordinates and Errors}
     403
     404\note{this section is wrong: it is a poor estimator of the source
     405  position errors.  we gave up a reverted to using the FWHM / (S/N)}
    393406
    394407We use the 9 pixels which include the source peak to fit for the
     
    605618the minimization values.  PSPhot uses the first and second moments to
    606619make a good guess for the centroid and shape parameters for the PSF
    607 models.  In order to minimize the impact of close neighbors, the noise
    608 values used in the fit are enhanced by a fraction of the deviation of
    609 the particular pixel value from the model guess.  Any objects which
    610 fail to converge in the fit are flagged as invalid.
     620models.  \note{still true? In order to minimize the impact of close
     621  neighbors, the noise values used in the fit are enhanced by a
     622  fraction of the deviation of the particular pixel value from the
     623  model guess.}  Any objects which fail to converge in the fit are
     624flagged as invalid.
    611625
    612626\note{does the noise enhancement introduce too much bias?}
     
    10441058
    10451059\subsection{Difference Images}
    1046 
    1047 \note{much of this discussion is theoretical: PSPhot can incorporate
    1048   these modifications, but it currently does not.}
    10491060
    10501061The noise map for a difference image must be generated from the two
  • branches/czw_branch/20101203/ippScripts/scripts/diff_skycell.pl

    r28593 r30631  
    117117}
    118118
     119# ppSub does (input) - (template) after PSF-match convolution. 
     120# one of the files needs to be assigned to 'input' and the other to 'template'.
     121# if either of these is a WARP, then we can perform magic on it.  If we are going to
     122# run magic, then we need to save the convolved version of the OTHER image
     123# (see magic_process.pl:155)
     124
    119125foreach my $file (@$files) {
    120126    if (defined $file->{template} and $file->{template}) {
    121 #        $template = $file->{uri};
    122127        $templatePath = $file->{path_base};
    123128        if ($file->{warp_id} == 0) {
     
    142147            $templateSources = "PSWARP.OUTPUT.SOURCES";
    143148            $templateMagic = $file->{magicked};
    144             $saveRefConv = 1;
    145         }
    146     } else {
    147 #        $input = $file->{uri};
     149            $saveInConv = 1;
     150        }
     151    } else {
    148152        $inputPath = $file->{path_base};
    149153        $inputMagic = $file->{magicked};    # if input is a stack the output can't be "magicked"
     
    165169            $inputVariance = "PSWARP.OUTPUT.VARIANCE";
    166170            $inputSources = "PSWARP.OUTPUT.SOURCES";
    167             $saveInConv = 1;
     171            $saveRefConv = 1;
    168172        }
    169173    }
  • branches/czw_branch/20101203/ippScripts/scripts/ipp_apply_burntool_single.pl

  • branches/czw_branch/20101203/ippScripts/scripts/ipp_cleanup.pl

    r30587 r30631  
    19001900{
    19011901    my $files = shift; # reference to a list of files to unlink
    1902 #     my $test_verbose = 1;
    1903 
    1904 #     if ($test_verbose == 1) {
    1905 #       open(TMPLOG,">>/tmp/czw.cleanup.log");
    1906 #       flock(TMPLOG,2);
    1907 #     }
    1908 
    1909     # this script is, of course, very dangerous.
     1902
    19101903    foreach my $file (@$files) {
    19111904        print STDERR "unlinking $stage $stage_id $file";
    1912         unless ($ipprc->file_exists($file)) {
    1913             print STDERR "\t File not found\n";
    1914         }
    1915         else {
    1916             print STDERR "\n";
    1917         }
    1918 
    1919 #       if ($test_verbose == 1) {
    1920 #           print TMPLOG "$stage $stage_id $file";
    1921 
    1922 #           else {
    1923 #               print TMPLOG "\n";
    1924 #           }
    1925 #       }
    1926 
    1927         $ipprc->file_delete($file);
    1928     }
    1929 
    1930 #     if ($test_verbose == 1) {
    1931 #       flock(TMPLOG,8);
    1932 #       close(TMPLOG);
    1933 #     }
     1905
     1906        my $error_code = $ipprc->kill_file($file);
     1907
     1908        &my_die("failed to kill $file", $stage, $stage_id, $PS_EXIT_CONFIG_ERROR) if $error_code;
     1909    }
    19341910
    19351911    return 1;
  • branches/czw_branch/20101203/ippScripts/scripts/magic_destreak_cleanup.pl

    r30587 r30631  
    4444           'magic_ds_id=s'  => \$magic_ds_id,# Magic destreak run identifier
    4545           'camera=s'       => \$camera,     # camera for evaluating file rules
    46            'stage=s'        => \$stage,     # camera for evaluating file rules
     46           'stage=s'        => \$stage,      # ipp stage for this magicDSRun
    4747           'save-temps'     => \$save_temps, # Save temporary files?
    4848           'dbname=s'       => \$dbname,     # Database name
     
    160160        my $backup_path_base = $comp->{backup_path_base};
    161161        my ($bimage, $bmask, $bch_mask, $bweight, $bsources, $bastrom);
    162         my ($rimage, $rmask, $rch_mask, $rweight, $rsources, $rastrom);
     162#        my ($rimage, $rmask, $rch_mask, $rweight, $rsources, $rastrom);
    163163
    164164        if ($stage eq "chip") {
     
    219219        }
    220220
    221         delete_files($bimage, $bmask, $bweight, $bsources, $bastrom, $bch_mask, $rch_mask);
     221        delete_files($bimage, $bmask, $bweight, $bsources, $bastrom, $bch_mask);
    222222
    223223        if ($stage eq "diff" and $warp_warp) {
     
    252252    foreach my $file (@_) {
    253253        if ($file) {
    254             if ($ipprc->file_exists($file)) {
    255                 if (!$no_update) {
    256                     print STDERR "deleting $file\n" if $verbose;
    257                     $ipprc->file_delete($file, 1) or my_die("Failed to delete $file", $magic_ds_id, $PS_EXIT_UNKNOWN_ERROR);
    258                     } else {
    259                     print STDERR "skipping delete $file\n";
    260                 }
    261             } else {
    262                 print STDERR "$file not found\n" if $verbose;
    263             }
     254            my $error_code = $ipprc->kill_file($file);
     255            my_die("Failed to delete $file", $magic_ds_id, $error_code) if $error_code;
    264256        }
    265257    }
  • branches/czw_branch/20101203/ippScripts/scripts/magic_process.pl

    r29625 r30631  
    151151            &my_die("failed to resolve inputs", $magic_id, $node, $PS_EXIT_DATA_ERROR);
    152152        }
     153
     154        # to run magic on a diff image, we need the convolved version
     155        # of the corresponding TEMPLATE image
     156        # (see diff_skycell.pl:124)
    153157
    154158        my $diff_base = $innode->{diff_path_base}; # Base name for diff
  • branches/czw_branch/20101203/ippScripts/scripts/stack_skycell.pl

    r30419 r30631  
    270270    $command .= " -image_id $image_id" if defined $image_id;
    271271    $command .= " -source_id $source_id" if defined $source_id;
     272
     273    $command .= " -stack_id   $stack_id"   if defined $stack_id;
     274    $command .= " -skycell_id $skycell_id" if defined $skycell_id;
     275    $command .= " -tess_id    $tess_base"  if defined $tess_base;
     276
    272277    if ($run_state eq 'new') {
    273278        $command .= " -dumpconfig $configuration";
  • branches/czw_branch/20101203/ippScripts/scripts/warp_skycell.pl

    r30587 r30631  
    159159my $outputWeight = prepare_output ("PSWARP.OUTPUT.VARIANCE", $outroot, $skycell_id, 1);
    160160my $outputSources = prepare_output ("PSWARP.OUTPUT.SOURCES", $outroot, $skycell_id, 1);
    161 my $outputPSF = prepare_output ("PSPHOT.PSF.SKY.SAVE", $outroot, 1);
     161my $outputPSF = prepare_output ("PSPHOT.PSF.SKY.SAVE", $outroot, $skycell_id, 1);
    162162my $outputBin1 = prepare_output ("PSWARP.BIN1", $outroot, $skycell_id, 1);
    163163my $outputBin2 = prepare_output ("PSWARP.BIN2", $outroot, $skycell_id, 1);
  • branches/czw_branch/20101203/ippTasks/Makefile.am

    r30586 r30631  
    4848        site.manoa.pro \
    4949        site.mhpcc.pro \
    50         site.mhpcc_testing.pro \
    5150        surveys.mhpcc.config \
    5251        simtest.pro \
  • branches/czw_branch/20101203/ippTools/share/camtool_find_pendingimfile.sql

  • branches/czw_branch/20101203/ippTools/share/pxadmin_create_tables.sql

  • branches/czw_branch/20101203/ippTools/src

  • branches/czw_branch/20101203/ippTools/src/difftool.c

    r30587 r30631  
    20952095    }
    20962096
    2097     query = pxDataGet("difftool_definestackstack_part1.sql");
     2097   
    20982098    if (pretend) {
    20992099        // negative simple so the default is true
     
    21092109    psArray *list = psArrayAllocEmpty(16); // List of runs defined, to print
    21102110    for (long i = 0; i < output->n; i++) {
     2111      query = pxDataGet("difftool_definestackstack_part1.sql");
     2112
     2113   
    21112114        psMetadata *row = output->data[i]; // Output row from query
    21122115        bool mdok;                      // Status of MD lookup
  • branches/czw_branch/20101203/ippTools/src/magictool.c

  • branches/czw_branch/20101203/ippTools/src/regtool.c

    r30587 r30631  
    320320  psStringSubstitute(&query,dateobs_end,"@DATEOBS_END@");
    321321
    322   fprintf(stderr,"%s",query);
     322  // fprintf(stderr,"%s",query);
    323323
    324324  if (!p_psDBRunQuery(config->dbh, query)) {
     
    345345  }
    346346
    347   fprintf (stderr, "found %ld rows\n", output->n);
     347  // fprintf (stderr, "found %ld rows\n", output->n);
    348348  if (!psArrayLength(output)) {
    349349    psTrace("regtool", PS_LOG_INFO, "no rows found");
  • branches/czw_branch/20101203/ippconfig

  • branches/czw_branch/20101203/ippconfig/gpc1/dvo.config

    r28215 r30631  
    5252IMAGE_OFFSET            0.100  # mark images POOR if abs(delta(Mcal)) > IMAGE_OFFSET
    5353STAR_CHISQ              10.0   # mark stars POOR if Xm > STAR_CHISQ
    54 STAR_TOOFEW              3     # mark star FEW if N(good) < STAR_TOOFEW
     54STAR_TOOFEW              2     # mark star FEW if N(good) < STAR_TOOFEW
    5555GRID_TOOFEW             10     # keep grid FROZEN if N(good) < GRID_TOOFEW
    5656IMAGE_TOOFEW            10     # mark image FEW if N(good) < IMAGE_TOOFEW
  • branches/czw_branch/20101203/ippconfig/recipes/filerules-simple.mdc

    r30118 r30631  
    195195
    196196# outputs for psphotStack:
    197 PSPHOT.CHISQ.IMAGE           OUTPUT {OUTPUT}.chisq.im.fits        IMAGE           NONE       FPA        TRUE      SIMPLE
    198 PSPHOT.CHISQ.MASK            OUTPUT {OUTPUT}.chisq.mk.fits        MASK            NONE       FPA        TRUE      SIMPLE
    199 PSPHOT.CHISQ.VARIANCE        OUTPUT {OUTPUT}.chisq.wt.fits        VARIANCE        NONE       FPA        TRUE      SIMPLE
    200 PSPHOT.STACK.OUTPUT.IMAGE    OUTPUT {OUTPUT}.{FILE.ID}.im.fits    IMAGE           NONE       FPA        TRUE      SIMPLE
    201 PSPHOT.STACK.OUTPUT.MASK     OUTPUT {OUTPUT}.{FILE.ID}.mk.fits    MASK            NONE       FPA        TRUE      SIMPLE
    202 PSPHOT.STACK.OUTPUT.VARIANCE OUTPUT {OUTPUT}.{FILE.ID}.wt.fits    VARIANCE        NONE       FPA        TRUE      SIMPLE
     197PSPHOT.CHISQ.IMAGE           OUTPUT {OUTPUT}.chisq.im.fits        IMAGE           NONE       FPA        TRUE      NONE
     198PSPHOT.CHISQ.MASK            OUTPUT {OUTPUT}.chisq.mk.fits        MASK            NONE       FPA        TRUE      NONE
     199PSPHOT.CHISQ.VARIANCE        OUTPUT {OUTPUT}.chisq.wt.fits        VARIANCE        NONE       FPA        TRUE      NONE
     200PSPHOT.STACK.OUTPUT.IMAGE    OUTPUT {OUTPUT}.{FILE.ID}.im.fits    IMAGE           NONE       FPA        TRUE      NONE
     201PSPHOT.STACK.OUTPUT.MASK     OUTPUT {OUTPUT}.{FILE.ID}.mk.fits    MASK            NONE       FPA        TRUE      NONE
     202PSPHOT.STACK.OUTPUT.VARIANCE OUTPUT {OUTPUT}.{FILE.ID}.wt.fits    VARIANCE        NONE       FPA        TRUE      NONE
    203203PSPHOT.STACK.OUTPUT          OUTPUT {OUTPUT}.{FILE.ID}.cmf        CMF             NONE       FPA        TRUE      NONE
    204204                                                     
  • branches/czw_branch/20101203/ippconfig/recipes/ppSim.config

    r29005 r30631  
    128128ZEROPOINT   F32     NAN                 # exposure time (seconds)
    129129SKY.MAGS    F32     NAN                 # exposure time (seconds)
     130
     131SEEING.MAX  F32     NAN                 # exposure time (seconds)
     132SEEING.RAMP BOOL    F                 # exposure time (seconds)
    130133
    131134FILTER      STR     NONE                # exposure time (seconds)
  • branches/czw_branch/20101203/ippconfig/recipes/ppSub.config

    r30118 r30631  
    22
    33KERNEL.TYPE     STR     ISIS            # Kernel type to use (POIS|ISIS|SPAM|FRIES|GUNK|RINGS)
    4 KERNEL.SIZE     S32     15              # Kernel half-size (pixels)
     4KERNEL.SIZE     S32     20              # Kernel half-size (pixels)
    55SPATIAL.ORDER   S32     1               # Spatial polynomial order
    66REGION.SIZE     F32     0               # Iso-kernel region size (pixels)
    77SOURCE.RADIUS   F32     3.0             # Source matching radius (pixels)
    8 STAMP.SPACING   F32     300             # Typical spacing between stamps (pixels)
    9 STAMP.FOOTPRINT S32     15              # Size of stamps (pixels)
     8STAMP.SPACING   F32     200             # Typical spacing between stamps (pixels)
     9STAMP.FOOTPRINT S32     20              # Size of stamps (pixels)
    1010STAMP.THRESHOLD F32     5               # Flux threshold for stamps (stdev above background)
    1111STRIDE          S32     128             # Size of convolution patches (pixels)
     
    3232INPUT.MATCH.MIN.SN F32  10.0
    3333
     34ADD.NOT.SUBTRACT BOOL   FALSE           # add the images instead of subtracting (ie, image + ref, not image - ref)
     35
    3436LOW.THRESHOLD   F32     20.0            # Mask pixels below this threshold (std dev)
    3537POOR.FRACTION   F32     0.20            # Maximum fraction of bad weight for poor pixels
     
    4042@ISIS.ORDERS    S32     6    4    2     # Polynomial orders for ISIS kernels
    4143
     44#@ISIS.WIDTHS   F32     2.4  5.0  10.0  # Gaussian kernel FWHM values
     45#@ISIS.ORDERS   S32     6    4    2     # Polynomial orders for ISIS kernels
     46
    4247# for autoscaling, the above kernel widths are multiplied by the factor max(FWHM_1,FWHM_2) / SCALE.REF
    4348SCALE           BOOL    TRUE            # Scale kernel parameters by the seeing?
    44 SCALE.REF       F32     6.0             # FWHM reference for kernel parameter scaling
     49SCALE.REF       F32     5.0             # FWHM reference for kernel parameter scaling
    4550SCALE.MIN       F32     0.5             # Minimum scale value
    4651SCALE.MAX       F32     2.0             # Maximum scale value
     
    9398        # DUAL convolution is more sensitive to the number of kernels
    9499        # do not provide as many orders as for SINGLE
    95         @ISIS.WIDTHS    F32     2.5  4.0  7.0   # Gaussian kernel FWHM values
     100        # @ISIS.WIDTHS  F32     2.4  5.0  10.0  # Gaussian kernel FWHM values
     101        @ISIS.WIDTHS    F32     1.5  3.0  6.0   # Gaussian kernel FWHM values
     102        # @ISIS.WIDTHS  F32     2.0  3.0  5.0   # Gaussian kernel FWHM values
     103        # @ISIS.WIDTHS  F32     2.1  4.2  8.4   # Gaussian kernel FWHM values
    96104        @ISIS.ORDERS    S32     2    2    2     # Polynomial orders for ISIS kernels
    97105
    98         SCALE.REF       F32     12.0            # FWHM reference for kernel parameter scaling
    99         KERNEL.SIZE     S32     24              # Kernel half-size (pixels)
    100         STAMP.FOOTPRINT S32     24              # Size of stamps (pixels)
     106        SCALE.REF       F32     5.0             # FWHM reference for kernel parameter scaling
     107        KERNEL.SIZE     S32     15              # Kernel half-size (pixels)
     108        STAMP.FOOTPRINT S32     15              # Size of stamps (pixels)
    101109END
    102110
  • branches/czw_branch/20101203/ippconfig/recipes/psphot.config

    r30118 r30631  
    199199# @RADIAL.ANNULAR.BINS.UPPER           F32     5.0  10.0  20.0  40.0  80.0 160.0
    200200
    201 # SDSS values:
    202 @RADIAL.ANNULAR.BINS.LOWER           F32     0.00 0.56 1.69 2.59 4.41  7.51 11.58 18.58 28.55 45.50  70.51 110.53 172.49 269.52 420.51
    203 @RADIAL.ANNULAR.BINS.UPPER           F32     0.56 1.69 2.59 4.41 7.51 11.58 18.58 28.55 45.50 70.51 110.53 172.49 269.52 420.51 652.50
     201# SDSS values (in SDSS pixels)
     202#@RADIAL.ANNULAR.BINS.LOWER           F32     0.00 0.56 1.69 2.59 4.41  7.51 11.58 18.58 28.55 45.50  70.51 110.53 172.49 269.52 420.51
     203#@RADIAL.ANNULAR.BINS.UPPER           F32     0.56 1.69 2.59 4.41 7.51 11.58 18.58 28.55 45.50 70.51 110.53 172.49 269.52 420.51 652.50
     204
     205# PS1 values? (all SDSS @ PS1 0.2 arcsec pixel scales)
     206# @RADIAL.ANNULAR.BINS.LOWER           F32      0.0 1.2 3.4 5.2  8.8 15.0 23.2 37.2 57.1  91.0 141.0 221.1 345.0 539.1  841.0
     207# @RADIAL.ANNULAR.BINS.UPPER           F32      1.2 3.4 5.2 8.8 15.0 23.2 37.2 57.1 91.0 141.0 221.1 345.0 539.1 841.0 1305.0
     208
     209# PS1 values?
     210@RADIAL.ANNULAR.BINS.LOWER           F32      0.0 1.2 3.4 5.2  8.8 15.0 23.2 37.2 57.1  91.0 141.0
     211@RADIAL.ANNULAR.BINS.UPPER           F32      1.2 3.4 5.2 8.8 15.0 23.2 37.2 57.1 91.0 141.0 221.1
    204212
    205213# Extended source fit parameters
     
    311319PSPHOT.STACK.MATCH.PSF              BOOL  T    # convolved input images to common target PSF
    312320PSPHOT.STACK.MATCH.PSF.SOURCE       STR   AUTO # which inputs to convolve? (RAW, CNV, AUTO)
    313 PSPHOT.STACK.TARGET.PSF.AUTO        BOOL  T    # automatically determine target PSF size?
    314 PSPHOT.STACK.TARGET.PSF.FWHM        F32   5.0  # FWHM of target PSF (if NOT AUTO sized; pixels)
     321PSPHOT.STACK.TARGET.PSF.AUTO        BOOL  F    # automatically determine target PSF size?
     322@PSPHOT.STACK.TARGET.PSF.FWHM       F32   6.5 9.0 # FWHM of target PSF (if NOT AUTO sized; pixels)
     323PSPHOT.STACK.USE.RAW                BOOL  T
    315324
    316325RADIAL_APERTURES                    BOOL  F    # calculate flux in circular radial apertures?
     
    338347  EXTENDED_SOURCE_ANNULI              BOOL  TRUE
    339348  PSPHOT.STACK.MATCH.PSF.SOURCE       STR   AUTO # which inputs to convolve? (RAW, CNV, AUTO)
    340   PSPHOT.STACK.TARGET.PSF.AUTO        BOOL  T    # automatically determine target PSF size?
    341   PSPHOT.STACK.TARGET.PSF.FWHM        F32   7.5 # FWHM of target PSF (if NOT AUTO sized; pixels)
     349  PSPHOT.STACK.TARGET.PSF.AUTO        BOOL  F    # automatically determine target PSF size?
     350  @PSPHOT.STACK.TARGET.PSF.FWHM        F32   5.0 6.5 8.0 # FWHM of target PSF (if NOT AUTO sized; pixels)
    342351  RADIAL_APERTURES                    BOOL  T    # calculate flux in circular radial apertures?
    343352  RADIAL_APERTURES_SN_LIM             F32   0.0  # S/N limit for radial aperture calculation
  • branches/czw_branch/20101203/ippconfig/simtest/camera.config

    r30118 r30631  
    6464  CMF.XSRC STR {CHIP.NAME}.xsrc # use .PSF and .EXT?
    6565  CMF.XFIT STR {CHIP.NAME}.xfit # use .PSF and .EXT?
     66  CMF.XRAD STR {CHIP.NAME}.xrad # use .PSF and .EXT?
    6667  CMF.DETEFF STR {CHIP.NAME}.deteff
    6768
  • branches/czw_branch/20101203/ippconfig/simtest/ppSub.config

    r26902 r30631  
    11### Recipe file for ppSub
    22
    3 STAMP.SPACING   F32     100             # Typical spacing between stamps (pixels)
    4 
    5 RENORM          BOOL    FALSE           # Renormalize weight maps?
    6 
    7 DUAL            BOOL    FALSE           # Dual convolution?
     3STAMP.SPACING   F32     25              # Typical spacing between stamps (pixels)
    84
    95TEST.RADIAL METADATA
     
    5046END
    5147
    52 SPATIAL.ORDER   S32     0               # Spatial polynomial order
    53 
    54 #STAMP.FOOTPRINT S32    25              # Size of stamps (pixels)
    55 #KERNEL.SIZE     S32    25              # Kernel half-size (pixels)
  • branches/czw_branch/20101203/ppSim/src/Makefile.am

    r28125 r30631  
    4747        ppSimUtils.c              \
    4848        ppSimLoop.c               \
    49         ppSimLoadSpots.c          \
    50         ppSimPhotom.c             \
    51         ppSimPhotomFiles.c        \
    52         ppSimLoadForceSources.c   \
    5349        ppSimMergeReadouts.c      \
    5450        ppSimDetections.c         \
     
    5955        ppSimBadCTE.c             \
    6056        ppSimVersion.c
    61 
    62 # these functions have been broken by the API change in psphot (Jan 2010)
    63 #       ppSimPhotomReadoutFake.c 
    64 #       ppSimPhotomReadoutForce.c
    6557
    6658ppSimSequence_CPPFLAGS = $(PSLIB_CFLAGS) $(PSMODULE_CFLAGS) $(PSASTRO_CFLAGS) $(ppSim_CFLAGS)
  • branches/czw_branch/20101203/ppSim/src/ppSimArguments.c

    r29011 r30631  
    4949    psMetadataAddF32(arguments,  PS_LIST_TAIL, "-zp", 0, "Photometric zero point", NAN);
    5050    psMetadataAddF32(arguments,  PS_LIST_TAIL, "-seeing", 0, "Seeing FWHM (arcsec)", NAN);
     51    psMetadataAddF32(arguments,  PS_LIST_TAIL, "-seeing-max", 0, "Seeing FWHM (arcsec) at end of ramp", NAN);
     52    psMetadataAddBool(arguments,  PS_LIST_TAIL, "-seeing-ramp", 0, "Use a seeing ramp", false);
    5153    psMetadataAddF32(arguments,  PS_LIST_TAIL, "-starslum", 0, "Fake star luminosity function slope", NAN);
    5254    psMetadataAddF32(arguments,  PS_LIST_TAIL, "-starsmag", 0, "Brightest magnitude for fake stars", NAN);
     
    130132        // 'seeing' is not required: we can load a psf-model instead; but if not, it is allowed
    131133        ppSimArgToRecipeF32(&status, options, "SEEING", arguments, "-seeing"); // seeing (FWHM in arcsec)
     134        ppSimArgToRecipeF32(&status, options, "SEEING.MAX", arguments, "-seeing-max"); // seeing (FWHM in arcsec)
     135        ppSimArgToRecipeBool(&status, options, "SEEING.RAMP", arguments, "-seeing-ramp"); // seeing (FWHM in arcsec)
    132136
    133137        // 'scale' is not required: we can use the WCS instead; but if not, it is allowed
     
    227231        psMetadataAddF32(options, PS_LIST_TAIL, "PA", 0, "Boresight position angle (radians)",pa * M_PI / 180.0);
    228232
     233        if (psMetadataLookupBool(NULL, arguments, "-seeing-ramp")) {
     234            float seeingMax = psMetadataLookupF32(NULL, arguments, "-seeing-max"); // seeing at ramp end
     235            psMetadataAddF32(options, PS_LIST_TAIL, "SEEING.MAX", 0, "Seeing FWHM (arcsec)", seeingMax);
     236            psMetadataAddBool(options, PS_LIST_TAIL, "SEEING.RAMP", 0, "use seeing ramp", true);
     237        }
     238
    229239        ppSimArgToRecipeF32(&status, options, "ZEROPOINT", arguments, "-zp"); // Zero point
    230240    }
  • branches/czw_branch/20101203/ppSim/src/ppSimCreate.c

    r29125 r30631  
    103103        psMetadataAddF32(recipe, PS_LIST_TAIL, "SEEING", PS_META_REPLACE, "Seeing SIGMA (pixels)", seeing / 2.0 / sqrt(2.0 * log(2.0)) / scale);
    104104
     105        float seeingMax = psMetadataLookupF32(&status, recipe, "SEEING.MAX");
     106        psMetadataAddF32(recipe, PS_LIST_TAIL, "SEEING.MAX", PS_META_REPLACE, "Seeing SIGMA (pixels)", seeingMax / 2.0 / sqrt(2.0 * log(2.0)) / scale);
     107
    105108        // if we have been supplied an input image, but no ra & dec, use the input image values
    106109        if (input) {
     
    132135    bool doPhotom = psMetadataLookupBool(&status, recipe, "PHOTOM"); // Density of fakes
    133136    if (doPhotom) {
    134 
    135         // XXX at the moment, we can perform photometry on the fake sources and the forced
    136         // photometry positions, but not one or the other.  Also, we only support photometry in
    137         // the context of an image previously analysed by psphot.  Add support for:
    138         // * photometry of a pure fake image (requires peak detection and psf creation)
    139         // * photometry of forced source positions without fake image (this might work, it just
    140         // requires not generating any fake features).
    141 
    142         if (!input) {
    143             psError(PS_ERR_UNKNOWN, false, "input image not found; currently required for photometry");
    144             return NULL;
    145         }
    146 
    147         // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?)
    148         pmFPAfile *fakeImage = pmFPAfileDefineChipMosaic(config, output->fpa, "PPSIM.FAKE.CHIP");
    149         if (!fakeImage) {
    150             psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.FAKE.CHIP"));
    151             psFree(fpa);
    152             return NULL;
    153         }
    154         if (fakeImage->type != PM_FPA_FILE_IMAGE) {
    155             psError(PS_ERR_IO, true, "PPSIM.FAKE.CHIP is not of type IMAGE");
    156             psFree(fpa);
    157             return NULL;
    158         }
    159 
    160         // we need a chip image if we perform photometry (is it necessary to build it if we don't use it?)
    161         pmFPAfile *forceImage = NULL;
    162         if (input) {
    163             forceImage = pmFPAfileDefineChipMosaic(config, input->fpa, "PPSIM.FORCE.CHIP");
    164             if (!forceImage) {
    165                 psError(PS_ERR_IO, false, _("Unable to generate new file from PPSIM.FORCE.CHIP"));
    166                 psFree(fpa);
    167                 return NULL;
    168             }
    169             if (forceImage->type != PM_FPA_FILE_IMAGE) {
    170                 psError(PS_ERR_IO, true, "PPSIM.FORCE.CHIP is not of type IMAGE");
    171                 psFree(fpa);
    172                 return NULL;
    173             }
    174         }
    175 
    176         // define associated psphot input/output files
    177         if (!ppSimPhotomFiles (config, fakeImage, forceImage)) {
    178             psError(PSPHOT_ERR_CONFIG, false, "Trouble defining the additional input/output files for psphot");
    179             psFree(fpa);
    180             return NULL;
    181         }
    182     } else {
    183         // have we supplied a psf model?  this happens in ppSimPhotomFiles if we request a photometry
    184         // analysis.  however, even if we do not, a psf model may be used to generate the fake
    185         // sources.
    186         if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
    187             // tie the psf file to the chipMosaic
    188             pmFPAfileBindFromArgs(&status, output, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
    189             if (!status) {
    190                 psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
    191                 psFree(fpa);
    192                 return NULL;
    193             }
    194         }
     137        psError(PS_ERR_UNKNOWN, false, "in-line photometry in ppSim had been deprecated");
     138        return NULL;
     139    }
     140
     141    // have we supplied a psf model?  this happens in ppSimPhotomFiles if we request a photometry
     142    // analysis.  however, even if we do not, a psf model may be used to generate the fake
     143    // sources.
     144    if (psMetadataLookupPtr(NULL, config->arguments, "PSPHOT.PSF")) {
     145        // tie the psf file to the chipMosaic
     146        pmFPAfileBindFromArgs(&status, output, config, "PSPHOT.PSF.LOAD", "PSPHOT.PSF");
     147        if (!status) {
     148            psError(PS_ERR_UNKNOWN, false, "Failed to find/build PSPHOT.PSF.LOAD");
     149            psFree(fpa);
     150            return NULL;
     151        }
    195152    }
    196153
     
    257214                }
    258215            }
     216            // XXX this is a hack, but I don't have a better way at the moment: assumes a single cell per chip
     217            if (nx) {
     218                psMetadataAddS32(chip->concepts, PS_LIST_TAIL, "CHIP.XSIZE", PS_META_REPLACE, "", nx);
     219            } else {
     220                nx = psMetadataLookupF32(&status, cell->concepts, "CELL.XSIZE");
     221                psMetadataAddS32(chip->concepts, PS_LIST_TAIL, "CHIP.XSIZE", PS_META_REPLACE, "", nx);
     222            }
     223            if (ny) {
     224                psMetadataAddS32(chip->concepts, PS_LIST_TAIL, "CHIP.YSIZE", PS_META_REPLACE, "", ny);
     225            } else {
     226                ny = psMetadataLookupF32(&status, cell->concepts, "CELL.YSIZE");
     227                psMetadataAddS32(chip->concepts, PS_LIST_TAIL, "CHIP.YSIZE", PS_META_REPLACE, "", ny);
     228            }
    259229        }
    260230    }
  • branches/czw_branch/20101203/ppSim/src/ppSimLoop.c

    r28125 r30631  
    4242    if (type == PPSIM_TYPE_OBJECT) {
    4343        // Load forced-photometry positions (these are placed on fpa->analysis for use in ppSimPhotomReadout)
    44         if (!ppSimLoadSpots (fpa, config)) ESCAPE (PS_ERR_UNKNOWN, "failed to load forced-photometry spots");
     44        // if (!ppSimLoadSpots (fpa, config)) ESCAPE (PS_ERR_UNKNOWN, "failed to load forced-photometry spots");
    4545
    4646        // Load catalogue stars
     
    5858
    5959    pmFPAview *view = pmFPAviewAlloc(0);// View for iterating over FPA
    60 
    61     // XXX if we include the psphot analysis, we will need to activate the correct pmFPAfiles
    62     // at the correct times.  ppSim operates on PPSIM.OUTPUT and PPSIM.SOURCES
    6360
    6461    // load any needed files (eg, input image, PSF)
     
    209206
    210207        // we perform photometry on the readouts of this chip in the output
    211         if (!ppSimPhotom (config, view)) ESCAPE (PS_ERR_UNKNOWN, "problem performing photometry");
     208        // if (!ppSimPhotom (config, view)) ESCAPE (PS_ERR_UNKNOWN, "problem performing photometry");
    212209
    213210        if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
  • branches/czw_branch/20101203/ppSim/src/ppSimMakeGalaxies.c

    r29011 r30631  
    2828    float galaxyThetaMax  = psMetadataLookupF32(&mdok, recipe, "GALAXY.THETA.MAX"); // Density of fakes
    2929    float galaxyThetaMin  = psMetadataLookupF32(&mdok, recipe, "GALAXY.THETA.MIN"); // Density of fakes
     30    galaxyThetaMax *= PS_RAD_DEG;
     31    galaxyThetaMin *= PS_RAD_DEG;
    3032
    3133    float galaxyIndexMin  = psMetadataLookupF32(&mdok, recipe, "GALAXY.INDEX.MIN"); // Density of fakes
     
    9597
    9698        int i = 0;
     99        float refNorm = 1.0;
     100        float ourNorm = 1.0;
    97101
    98102        for (long iy = 0.5*galaxyGridDY; iy < ySize; iy += galaxyGridDY) {
     
    104108                galaxy->y    = iy;
    105109
    106                 galaxy->peak = 1000;
     110                // galaxy->peak = 1000;
     111                galaxy->peak = bright / 100.0;
     112                // fprintf (stderr, "Io: %f\n", bright);
    107113
    108114                // galaxyIndex from user should be for function of this form: exp(-r^(1/n))
     
    111117                float rndValue;
    112118
    113                 rndValue = galaxyGridRandom ? drand48() : i / num;
     119                rndValue = galaxyGridRandom ? drand48() : i / (float) num;
    114120                float index = (galaxyIndexMin  + rndValue * galaxyIndexSlope);
    115121                galaxy->index = 0.5/index; // factor of 0.5 because the Sersic model creates exp(-z^n), not exp(-r^n)
    116122
    117                 rndValue = galaxyGridRandom ? drand48() : i / num;
     123                rndValue = galaxyGridRandom ? drand48() : i / (float) num;
    118124                float scale = (galaxyRmajorMin + rndValue * galaxyRmajorSlope);
    119125
     
    125131                galaxy->Rmaj  = scale;
    126132
    127                 rndValue = galaxyGridRandom ? drand48() : i / num;
     133                rndValue = galaxyGridRandom ? drand48() : i / (float) num;
    128134                galaxy->Rmin  = (galaxyARatioMin + rndValue * galaxyARatioSlope) * galaxy->Rmaj;
    129135
    130                 rndValue = galaxyGridRandom ? drand48() : i / num;
     136                rndValue = galaxyGridRandom ? drand48() : i / (float) num;
    131137                galaxy->theta = (galaxyThetaMin  + rndValue * galaxyThetaSlope);
    132 
    133                 // galaxy->peak *= Io;
     138               
     139                if (i == 0) {
     140                    refNorm = Io*scale*scale;
     141                }
     142                ourNorm = refNorm / (Io*scale*scale);
     143                galaxy->peak *= ourNorm;
    134144
    135145                if (0) {
    136                     fprintf (stderr, "Rmaj: %f, scale: %f, index: %f, bn: %f, Ro: %f, Io: %f\n", galaxy->Rmaj, scale, index, bn, fR, Io);
     146                    fprintf (stderr, "Rmaj: %f, scale: %f, index: %f, bn: %f, Ro: %f, Io: %f, theta: %f\n", galaxy->Rmaj, scale, index, bn, fR, Io, galaxy->theta);
    137147                }
    138148
  • branches/czw_branch/20101203/ppSim/src/ppSimSetPSF.c

    r26900 r30631  
    2323    float theta    = psMetadataLookupF32(&status, recipe, "PSF.THETA"); // Seeing SIGMA (pixels)
    2424
     25    float seeingMax = psMetadataLookupF32(&status, recipe, "SEEING.MAX"); // Seeing SIGMA (pixels)
     26    bool seeingRamp = psMetadataLookupBool(&status, recipe, "SEEING.RAMP"); // Seeing SIGMA (pixels)
     27
    2528    char *psfModelName = psMetadataLookupStr(&status, recipe, "PSF.MODEL"); // Name of PSF model
    2629    if (psfModelName == NULL) {
     
    3942    int xSize = psMetadataLookupS32(NULL, chip->concepts, "CHIP.XSIZE");
    4043    int ySize = psMetadataLookupS32(NULL, chip->concepts, "CHIP.YSIZE");
    41     xSize = 1000;
    42     ySize = 1000;
    4344
    44     // no spatial variation
    45     options->psfTrendMode = PM_TREND_POLY_ORD;
    46     options->psfTrendNx = 0;
    47     options->psfTrendNy = 0;
    48     options->psfFieldNx = xSize;
    49     options->psfFieldNy = ySize;
     45    if (seeingRamp) {
     46        psEllipseAxes axes;
     47        psEllipsePol pol;
     48        psEllipsePol polMax;
    5049
    51     // generate the psf
    52     psf = pmPSFAlloc (options);
     50        // try spatial variation
     51        options->psfTrendMode = PM_TREND_POLY_ORD;
     52        options->psfTrendNx = 1;
     53        options->psfTrendNy = 1;
     54        options->psfFieldNx = xSize;
     55        options->psfFieldNy = ySize;
    5356
    54     psEllipseAxes axes;
    55     psEllipsePol pol;
     57        // generate the psf
     58        psf = pmPSFAlloc (options);
    5659
    57     // supply the semi-major axis (these are SIGMA values in PIXELS)
    58     axes.major = seeing;
    59     axes.minor = aRatio * seeing;
    60     axes.theta = theta * PS_RAD_DEG;
     60        // supply the semi-major axis (these are SIGMA values in PIXELS)
     61        axes.major = seeing;
     62        axes.minor = aRatio * seeing;
     63        axes.theta = theta * PS_RAD_DEG;
    6164
    62     pol = psEllipseAxesToPol (axes);
     65        pol = psEllipseAxesToPol (axes);
    6366   
    64     param = psf->params->data[PM_PAR_E0];
    65     param->poly->coeff[0][0] = pol.e0;
     67        axes.major = seeingMax;
     68        axes.minor = aRatio * seeingMax;
     69        polMax = psEllipseAxesToPol (axes);
    6670
    67     param = psf->params->data[PM_PAR_E1];
    68     param->poly->coeff[0][0] = pol.e1;
     71        param = psf->params->data[PM_PAR_E0];
     72        param->poly->coeff[0][0] = pol.e0;
     73        param->poly->coeff[1][0] = (polMax.e0 - pol.e0) / xSize;
     74        param->poly->coeff[0][1] = (polMax.e0 - pol.e0) / ySize;
    6975
    70     param = psf->params->data[PM_PAR_E2];
    71     param->poly->coeff[0][0] = pol.e2;
     76        param = psf->params->data[PM_PAR_E1];
     77        param->poly->coeff[0][0] = pol.e1;
     78        param->poly->coeff[1][0] = 0.0;
     79        param->poly->coeff[0][1] = 0.0;
    7280
    73     if (!strcasecmp (psfModelName, "PS_MODEL_QGAUSS")) {
    74         param = psf->params->data[PM_PAR_7];
    75         param->poly->coeff[0][0] = 1.0;
    76     }
     81        param = psf->params->data[PM_PAR_E2];
     82        param->poly->coeff[0][0] = pol.e2;
     83        param->poly->coeff[1][0] = 0.0;
     84        param->poly->coeff[0][1] = 0.0;
    7785
    78     if (!strcasecmp (psfModelName, "PS_MODEL_RGAUSS")) {
    79         param = psf->params->data[PM_PAR_7];
    80         param->poly->coeff[0][0] = 1.0;
     86        if (!strcasecmp (psfModelName, "PS_MODEL_QGAUSS")) {
     87            param = psf->params->data[PM_PAR_7];
     88            param->poly->coeff[0][0] = 1.0;
     89            param->poly->coeff[1][0] = 0.0;
     90            param->poly->coeff[0][1] = 0.0;
     91        }
     92
     93        if (!strcasecmp (psfModelName, "PS_MODEL_RGAUSS")) {
     94            param = psf->params->data[PM_PAR_7];
     95            param->poly->coeff[0][0] = 1.0;
     96            param->poly->coeff[1][0] = 0.0;
     97            param->poly->coeff[0][1] = 0.0;
     98        }
     99
     100    } else {
     101        psEllipseAxes axes;
     102        psEllipsePol pol;
     103
     104        // no spatial variation
     105        options->psfTrendMode = PM_TREND_POLY_ORD;
     106        options->psfTrendNx = 0;
     107        options->psfTrendNy = 0;
     108        options->psfFieldNx = xSize;
     109        options->psfFieldNy = ySize;
     110
     111        // generate the psf
     112        psf = pmPSFAlloc (options);
     113
     114        // supply the semi-major axis (these are SIGMA values in PIXELS)
     115        axes.major = seeing;
     116        axes.minor = aRatio * seeing;
     117        axes.theta = theta * PS_RAD_DEG;
     118
     119        pol = psEllipseAxesToPol (axes);
     120   
     121        param = psf->params->data[PM_PAR_E0];
     122        param->poly->coeff[0][0] = pol.e0;
     123
     124        param = psf->params->data[PM_PAR_E1];
     125        param->poly->coeff[0][0] = pol.e1;
     126
     127        param = psf->params->data[PM_PAR_E2];
     128        param->poly->coeff[0][0] = pol.e2;
     129
     130        if (!strcasecmp (psfModelName, "PS_MODEL_QGAUSS")) {
     131            param = psf->params->data[PM_PAR_7];
     132            param->poly->coeff[0][0] = 1.0;
     133        }
     134
     135        if (!strcasecmp (psfModelName, "PS_MODEL_RGAUSS")) {
     136            param = psf->params->data[PM_PAR_7];
     137            param->poly->coeff[0][0] = 1.0;
     138        }
    81139    }
    82140
  • branches/czw_branch/20101203/ppStack/src/Makefile.am

    r28253 r30631  
    4949        ppStackFinish.c         \
    5050        ppStackTarget.c         \
     51        ppStackUpdateHeader.c   \
     52        ppStackJPEGs.c          \
     53        ppStackStats.c          \
    5154        ppStackErrorCodes.c
    5255
  • branches/czw_branch/20101203/ppStack/src/ppStack.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <psphot.h>
    9 
    101#include "ppStack.h"
    11 #include "ppStackLoop.h"
    12 
    13 #define TIMER_NAME "PPSTACK"            // Name of timer
    142
    153int main(int argc, char *argv[])
    164{
    175    psLibInit(NULL);
    18     psTimerStart(TIMER_NAME);
     6    psTimerStart("PPSTACK");
    197    psTimerStart("PPSTACK_STEPS");
    208
     
    2311    psphotErrorRegister();
    2412
     13    ppStackOptions *options = NULL;                               // Options for stacking
     14
    2515    pmConfig *config = pmConfigRead(&argc, argv, PPSTACK_RECIPE); // Configuration
    26     ppStackOptions *options = NULL;                               // Options for stacking
    2716    if (!config) {
    28         goto die;
     17        ppStackCleanup(config, options);
    2918    }
    3019
     
    3322    if (!pmModelClassInit()) {
    3423        psError(PPSTACK_ERR_PROG, false, "Unable to initialise model classes.");
    35         goto die;
     24        ppStackCleanup(config, options);
    3625    }
    3726
    3827    if (!psphotInit()) {
    3928        psError(PPSTACK_ERR_PROG, false, "Error initialising psphot.");
    40         goto die;
     29        ppStackCleanup(config, options);
    4130    }
    4231
    43     (void)psTraceSetLevel("ppStack", 5);
    44 
    4532    if (!ppStackArgumentsSetup(argc, argv, config)) {
    46         goto die;
     33        ppStackCleanup(config, options);
    4734    }
    4835
    4936    if (!ppStackCamera(config)) {
    50         goto die;
     37        ppStackCleanup(config, options);
    5138    }
    5239
    5340    if (!ppStackArgumentsParse(config)) {
    54         goto die;
     41        ppStackCleanup(config, options);
    5542    }
    5643
    5744    options = ppStackOptionsAlloc();
    5845    if (!ppStackLoop(config, options)) {
    59         goto die;
     46        ppStackCleanup(config, options);
    6047    }
    6148
    62 
    63  die:
    64     // Common code for the death.
    65     {
    66         psExit exitValue = ppStackExitCode(PS_EXIT_SUCCESS); // Exit code
    67 
    68         // Ensure everything closes
    69         if (config) {
    70             ppStackFileActivation(config, PPSTACK_FILES_PREPARE, true);
    71             ppStackFileActivation(config, PPSTACK_FILES_CONVOLVE, true);
    72             ppStackFileActivation(config, PPSTACK_FILES_STACK, true);
    73             ppStackFileActivation(config, PPSTACK_FILES_UNCONV, true);
    74             ppStackFileActivation(config, PPSTACK_FILES_PHOT, true);
    75             if (!ppStackFilesIterateUp(config)) {
    76                 psError(psErrorCodeLast(), false, "Unable to close files.");
    77                 exitValue = ppStackExitCode(exitValue);
    78                 pmFPAfileFreeSetStrict(false);
    79             }
    80         }
    81 
    82         // Write out summary statistics
    83         if (options && options->stats) {
    84 
    85             psMetadataAddS32(options->stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE,
    86                              "Bad data quality flag", options->quality);
    87             psMetadataAddF32(options->stats, PS_LIST_TAIL, "TIME_STACK", 0,
    88                              "Total time", psTimerClear("PPSTACK_TOTAL"));
    89 
    90             const char *statsMDC = psMetadataConfigFormat(options->stats);
    91             if (!statsMDC || strlen(statsMDC) == 0) {
    92                 psError(PS_ERR_IO, false, "Unable to get statistics MDC file.");
    93                 return false;
    94             }
    95             if (fprintf(options->statsFile, "%s", statsMDC) != strlen(statsMDC)) {
    96                 psError(PS_ERR_IO, false, "Unable to write statistics MDC file.");
    97                 return false;
    98             }
    99             psFree(statsMDC);
    100             if (fclose(options->statsFile) == EOF) {
    101                 psError(PS_ERR_IO, false, "Unable to close statistics MDC file.");
    102                 return false;
    103             }
    104             options->statsFile = NULL;
    105             pmConfigRunFilenameAddWrite(config, "STATS",
    106                                         psMetadataLookupStr(NULL, config->arguments, "STATS"));
    107         }
    108         psFree(options);
    109 
    110         // Dump configuration
    111         bool mdok;                                                                    // Status of MD lookup
    112         psString dump = psMetadataLookupStr(&mdok, config->arguments, "DUMP_CONFIG"); // File for config
    113         if (dump && !pmConfigDump(config, dump)) {
    114             psError(psErrorCodeLast(), false, "Unable to dump configuration.");
    115             exitValue = ppStackExitCode(exitValue);
    116         }
    117 
    118         psTrace("ppStack", 1, "Finished at %f sec\n", psTimerMark(TIMER_NAME));
    119         psTimerStop();
    120 
    121         psFree(config);
    122         pmModelClassCleanup();
    123         pmConfigDone();
    124         psLibFinalize();
    125         pmVisualClose();
    126         pmVisualCleanup ();
    127 
    128         exitValue = ppStackExitCode(exitValue);
    129         exit(exitValue);
    130     }
     49    ppStackCleanup(config, options);
    13150}
    13251
  • branches/czw_branch/20101203/ppStack/src/ppStack.h

    r28253 r30631  
     1#ifdef HAVE_CONFIG_H
     2#include <config.h>
     3#endif
     4
     5#include <stdio.h>
     6#include <pslib.h>
     7#include <psmodules.h>
     8#include <ppStats.h>
     9#include <psphot.h>
     10
    111#ifndef PPSTACK_H
    212#define PPSTACK_H
     
    717#include <pslib.h>
    818#include <psmodules.h>
    9 
    10 #include "ppStackOptions.h"
    11 #include "ppStackErrorCodes.h"
    1219
    1320// Mask values for inputs
     
    2532// List of files
    2633typedef enum {
     34    PPSTACK_FILES_NONE,                 // NOP list
    2735    PPSTACK_FILES_PREPARE,              // Files for preparation
    2836    PPSTACK_FILES_TARGET,               // Files for target generation
     
    3341} ppStackFileList;
    3442
     43#include "ppStackOptions.h"
     44#include "ppStackThread.h"
     45#include "ppStackLoop.h"
     46#include "ppStackErrorCodes.h"
    3547
    3648// Setup command-line arguments
     
    180192    );
    181193
     194bool ppStackWriteVariance(const char *name, // Name of image
     195                          psMetadata *header, // Header
     196                          const psImage *variance, // Variance
     197                          const psImage *covariance, // Variance
     198                          pmConfig *config // Configuration
     199    );
     200
    182201/// Return an appropriate exit code based on the error code
    183202psExit ppStackExitCode(psExit exitValue);
    184203
     204bool ppStackCleanup(pmConfig *config, ppStackOptions *options) PS_ATTR_NORETURN;
     205
    185206#endif
  • branches/czw_branch/20101203/ppStack/src/ppStackArguments.c

    r28617 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <string.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 
    101#include "ppStack.h"
    112
     
    189180    psMetadataAddStr(arguments, PS_LIST_TAIL, "-temp-mask", 0, "Suffix for temporary masks", NULL);
    190181    psMetadataAddStr(arguments, PS_LIST_TAIL, "-temp-variance", 0, "Suffix for temporary variance maps", NULL);
    191     psMetadataAddBool(arguments, PS_LIST_TAIL, "-temp-delete", 0,
    192                       "Delete temporary files on completion?", false);
     182    psMetadataAddBool(arguments, PS_LIST_TAIL, "-temp-delete", 0, "Delete temporary files on completion?", false);
    193183    psMetadataAddS32(arguments, PS_LIST_TAIL, "-threads", 0, "Number of threads to use", 0);
    194184    psMetadataAddBool(arguments, PS_LIST_TAIL, "-visual", 0, "visualisation", false);
    195185
     186    psMetadataAddStr(arguments, PS_LIST_TAIL, "-stack_id",   0, "stack ID",        NULL);
     187    psMetadataAddStr(arguments, PS_LIST_TAIL, "-skycell_id", 0, "skycell ID",      NULL);
     188    psMetadataAddStr(arguments, PS_LIST_TAIL, "-tess_id",    0, "tessellation ID", NULL);
     189
    196190    if (argc == 1) {
    197191        usage(argv[0], arguments, config);
     192    }
     193
     194    // stack-type : used to define the stack for PSPS
     195    if ((argNum = psArgumentGet (argc, argv, "-stack-type"))) {
     196        if (argc <= argNum+1) {
     197            psErrorStackPrint(stderr, "Expected to see an argument for -stack-type");
     198            exit(PS_EXIT_CONFIG_ERROR);
     199        }
     200        psArgumentRemove (argNum, &argc, argv);
     201        if (strcasecmp(argv[argNum], "NIGHTLY") && strcasecmp(argv[argNum], "DEEP") && strcasecmp(argv[argNum], "BEST_IQ")) {
     202            psErrorStackPrint(stderr, "Invalid option for -stack-type %s (must be one of NIGHTLY_STACK, DEEP_STACK, IQ_STACK)", argv[argNum]);
     203            exit(PS_EXIT_CONFIG_ERROR);
     204        }
     205        psMetadataAddStr (arguments, PS_LIST_TAIL, "STACK_TYPE", PS_META_REPLACE, "Stack Type", argv[argNum]);
     206        psArgumentRemove (argNum, &argc, argv);
     207    } else {
     208        psMetadataAddStr (arguments, PS_LIST_TAIL, "STACK_TYPE", PS_META_REPLACE, "Stack Type", "DEEP_STACK");
    198209    }
    199210
  • branches/czw_branch/20101203/ppStack/src/ppStackCamera.c

    r27989 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <string.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 #include <psphot.h>
    10 
    111#include "ppStack.h"
    122
  • branches/czw_branch/20101203/ppStack/src/ppStackCleanup.c

    r27427 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
     1#include "ppStack.h"
    42
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <ppStats.h>
    9 
    10 #include "ppStack.h"
    11 #include "ppStackLoop.h"
    12 
    13 
    14 bool ppStackCleanup(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config)
     3// ppStackCleanupFiles
     4bool ppStackCleanupFiles(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config, ppStackFileList stackFiles, ppStackFileList photFiles, bool closeJPEGs)
    155{
    166    psAssert(stack, "Require stack");
     
    188    psAssert(config, "Require configuration");
    199
    20     psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
    21     psAssert(recipe, "We've thrown an error on this before.");
    22 
    23 #if 0
    24     // Ensure masked regions really look masked
    25     {
    26         psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits for bad
    27         psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
    28         if (!pmReadoutMaskApply(options->outRO, maskBad)) {
    29             psWarning("Unable to apply mask");
    30         }
    31     }
    32 #endif
    33 
    34     // Generate binned JPEGs
    35     {
    36         int bin1 = psMetadataLookupS32(NULL, recipe, "BIN1"); // First binning level
    37         int bin2 = psMetadataLookupS32(NULL, recipe, "BIN2"); // Second binning level
    38 
    39         // Target cells
    40         pmFPAview *view = pmFPAviewAlloc(0); // View to cells of interest
    41         view->chip = view->cell = 0;
    42         pmCell *cell1 = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT.JPEG1");
    43         pmCell *cell2 = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT.JPEG2");
    44         psImageMaskType maskValue = pmConfigMaskGet("BLANK", config); // Bits to mask
    45         psFree(view);
    46 
    47         pmReadout *ro1 = pmReadoutAlloc(cell1), *ro2 = pmReadoutAlloc(cell2); // Binned readouts
    48         if (!pmReadoutRebin(ro1, options->outRO, maskValue, bin1, bin1) ||
    49             !pmReadoutRebin(ro2, ro1, 0, bin2, bin2)) {
    50             psError(PPSTACK_ERR_DATA, false, "Unable to bin output.");
    51             psFree(ro1);
    52             psFree(ro2);
    53             return false;
    54         }
    55         psFree(ro1);
    56         psFree(ro2);
    57     }
    58 
    59     // Statistics on output
    60     if (options->stats) {
    61         psTrace("ppStack", 1, "Gathering statistics on stacked image....\n");
    62         psString maskBadStr = psMetadataLookupStr(NULL, recipe, "MASK.BAD"); // Name of bits for bad
    63         psImageMaskType maskBad = pmConfigMaskGet(maskBadStr, config); // Bits to mask for bad pixels
    64 
    65         pmFPAview *view = pmFPAviewAlloc(0); // View to readout
    66         view->chip = view->cell = view->readout = 0;
    67 
    68         ppStatsFPA(options->stats, options->outRO->parent->parent->parent, view, maskBad, config);
    69 
    70         psFree(view);
    71     }
    72 
    7310    if (!ppStackFilesIterateUp(config)) {
    7411        psError(psErrorCodeLast(), false, "Unable to close files.");
    7512        return false;
    7613    }
    77     ppStackFileActivation(config, PPSTACK_FILES_STACK, false);
    78     ppStackFileActivation(config, PPSTACK_FILES_PHOT, false);
     14    ppStackFileActivation(config, stackFiles, false);
     15    ppStackFileActivation(config, photFiles, false);
    7916
    8017    // Ensure files are freed
    81     {
    82         options->outRO->data_exists = false;
    83         options->outRO->parent->data_exists = false;
    84         options->outRO->parent->parent->data_exists = false;
    85         psFree(options->outRO);
    86         options->outRO = NULL;
     18    options->outRO->data_exists = false;
     19    options->outRO->parent->data_exists = false;
     20    options->outRO->parent->parent->data_exists = false;
     21    psFree(options->outRO);
     22    options->outRO = NULL;
    8723
    88         options->expRO->data_exists = false;
    89         options->expRO->parent->data_exists = false;
    90         options->expRO->parent->parent->data_exists = false;
    91         psFree(options->expRO);
    92         options->expRO = NULL;
     24    options->expRO->data_exists = false;
     25    options->expRO->parent->data_exists = false;
     26    options->expRO->parent->parent->data_exists = false;
     27    psFree(options->expRO);
     28    options->expRO = NULL;
    9329
     30    for (int i = 0; i < options->num; i++) {
     31        pmCellFreeData(options->cells->data[i]);
     32    }
     33
     34    if (closeJPEGs) {
     35        // XXX move these close / free operations to the jpeg creation function
    9436        pmFPAview *view = pmFPAviewAlloc(0);// Pointer into FPA hierarchy
    9537        view->chip = view->cell = 0;        // pmFPAviewFreeData doesn't want to deal with readouts
     
    11860    return true;
    11961}
     62
     63bool ppStackCleanup (pmConfig *config, ppStackOptions *options) {
     64
     65    psExit exitValue = ppStackExitCode(PS_EXIT_SUCCESS); // Exit code
     66
     67    // Ensure everything closes
     68    if (config) {
     69        ppStackFileActivation(config, PPSTACK_FILES_PREPARE, true);
     70        ppStackFileActivation(config, PPSTACK_FILES_CONVOLVE, true);
     71        ppStackFileActivation(config, PPSTACK_FILES_STACK, true);
     72        ppStackFileActivation(config, PPSTACK_FILES_UNCONV, true);
     73        ppStackFileActivation(config, PPSTACK_FILES_PHOT, true);
     74        if (!ppStackFilesIterateUp(config)) {
     75            psError(psErrorCodeLast(), false, "Unable to close files.");
     76            exitValue = ppStackExitCode(exitValue);
     77            pmFPAfileFreeSetStrict(false);
     78        }
     79    }
     80
     81    // Write out summary statistics
     82    if (options && options->stats) {
     83
     84        psMetadataAddS32(options->stats, PS_LIST_TAIL, "QUALITY", PS_META_REPLACE, "Bad data quality flag", options->quality);
     85        psMetadataAddF32(options->stats, PS_LIST_TAIL, "TIME_STACK", 0, "Total time", psTimerClear("PPSTACK_TOTAL"));
     86
     87        const char *statsMDC = psMetadataConfigFormat(options->stats);
     88        if (!statsMDC || strlen(statsMDC) == 0) {
     89            psError(PS_ERR_IO, false, "Unable to get statistics MDC file.");
     90            exitValue = ppStackExitCode(exitValue);
     91            exit(exitValue);
     92        }
     93        if (fprintf(options->statsFile, "%s", statsMDC) != strlen(statsMDC)) {
     94            psError(PS_ERR_IO, false, "Unable to write statistics MDC file.");
     95            exitValue = ppStackExitCode(exitValue);
     96            exit(exitValue);
     97        }
     98        psFree(statsMDC);
     99        if (fclose(options->statsFile) == EOF) {
     100            psError(PS_ERR_IO, false, "Unable to close statistics MDC file.");
     101            exitValue = ppStackExitCode(exitValue);
     102            exit(exitValue);
     103        }
     104        options->statsFile = NULL;
     105        pmConfigRunFilenameAddWrite(config, "STATS", psMetadataLookupStr(NULL, config->arguments, "STATS"));
     106    }
     107    psFree(options);
     108
     109    // Dump configuration
     110    bool mdok;                                                                    // Status of MD lookup
     111    psString dump = psMetadataLookupStr(&mdok, config->arguments, "DUMP_CONFIG"); // File for config
     112    if (dump && !pmConfigDump(config, dump)) {
     113        psError(psErrorCodeLast(), false, "Unable to dump configuration.");
     114        exitValue = ppStackExitCode(exitValue);
     115    }
     116
     117    psTrace("ppStack", 1, "Finished at %f sec\n", psTimerMark("PPSTACK"));
     118    psTimerStop();
     119
     120    psFree(config);
     121    pmModelClassCleanup();
     122    pmConfigDone();
     123    psLibFinalize();
     124    pmVisualClose();
     125    pmVisualCleanup ();
     126
     127    exitValue = ppStackExitCode(exitValue);
     128    exit(exitValue);
     129}
  • branches/czw_branch/20101203/ppStack/src/ppStackCombineFinal.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    11 
    12 #define WCS_TOLERANCE 0.001             // Tolerance for WCS
    132
    143//#define TESTING                         // Enable test output
     
    10796    }
    10897
    109     // Propagate WCS
    110     bool wcsDone = false;           // Have we done the WCS?
    111     for (int i = 0; i < options->num && !wcsDone; i++) {
    112         if (options->inputMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
    113             continue;
    114         }
    115 
    116         ppStackThread *thread = stack->threads->data[0]; // Representative stack
    117         pmReadout *inRO = thread->readouts->data[i]; // Template readout
    118         if (inRO && !wcsDone) {
    119             // Copy astrometry over
    120             wcsDone = true;
    121             pmHDU *inHDU = pmHDUFromCell(inRO->parent); // Template HDU
    122             pmHDU *outHDU = pmHDUFromCell(outRO->parent); // Output HDU
    123             pmChip *outChip = outRO->parent->parent; // Output chip
    124             pmFPA *outFPA = outChip->parent; // Output FPA
    125             if (!outHDU || !inHDU) {
    126                 psWarning("Unable to find HDU at FPA level to copy astrometry.");
    127             } else {
    128                 if (!pmAstromReadWCS(outFPA, outChip, inHDU->header, 1.0)) {
    129                     psErrorClear();
    130                     psWarning("Unable to read WCS astrometry from input FPA.");
    131                     wcsDone = false;
    132                 } else {
    133                     if (!outHDU->header) {
    134                         outHDU->header = psMetadataAlloc();
    135                     }
    136                     if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
    137                         psErrorClear();
    138                         psWarning("Unable to write WCS astrometry to output FPA.");
    139                         wcsDone = false;
    140                     }
    141                 }
    142             }
    143         }
    144     }
    145 
    146     // Put version information into the header
    147     pmHDU *hdu = pmHDUFromCell(outRO->parent);
    148     if (!hdu) {
    149         psError(PPSTACK_ERR_PROG, false, "Unable to find HDU for output.");
    150         return false;
    151     }
    152     if (!hdu->header) {
    153         hdu->header = psMetadataAlloc();
    154     }
    155     ppStackVersionHeader(hdu->header);
    156 
    157 
    15898#ifdef TESTING
    15999    static int pass = 0;                // Pass through
  • branches/czw_branch/20101203/ppStack/src/ppStackCombineInitial.c

    r28405 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    112
    123//#define TESTING                         // Enable test output
  • branches/czw_branch/20101203/ppStack/src/ppStackCombinePrepare.c

    r27427 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    112
    123bool ppStackCombinePrepare(const char *outName, const char *expName,
  • branches/czw_branch/20101203/ppStack/src/ppStackConvolve.c

    r30586 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    112
    123//#define TESTING
    13 
    144
    155// Update the value of a concept
     
    2010    item->data.F32 = VALUE; \
    2111}
    22 
    23 
    2412
    2513bool ppStackConvolve(ppStackOptions *options, pmConfig *config)
     
    6957        ppStackFileActivationSingle(config, PPSTACK_FILES_CONVOLVE, true, i);
    7058        if (options->convolve) {
    71             // XXX PPSTACK.CONV.KERNEL not defined unless convolve
    72             // pmFPAfileActivate(config->files, true, "PPSTACK.CONV.KERNEL");
     59            // PPSTACK.CONV.KERNEL not defined unless convolve
    7360            pmFPAfileActivateSingle(config->files, true, "PPSTACK.CONV.KERNEL", i); // Activated file
    7461        }
     
    191178        }
    192179        psFree(maskHeader);
    193         if (!ppStackWriteImage(options->convVariances->data[i], hdu->header, readout->variance, config)) {
     180        if (!ppStackWriteVariance(options->convVariances->data[i], hdu->header, readout->variance, readout->covariance->image, config)) {
    194181            psError(PPSTACK_ERR_IO, false, "Unable to write convolved variance %d", i);
    195182            psFree(fpaList);
  • branches/czw_branch/20101203/ppStack/src/ppStackErrorCodes.c.in

    r27004 r30631  
     1#include "ppStack.h"
     2
    13/** @file ppStackErrorCodes.c.in
    24 *
     
    1012 *  Copyright 2009 Institute for Astronomy, University of Hawaii
    1113 */
    12 
    13 #include "pslib.h"
    14 #include "ppStackErrorCodes.h"
    1514
    1615/*
  • branches/czw_branch/20101203/ppStack/src/ppStackFiles.c

    r28253 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <unistd.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 
    101#include "ppStack.h"
    112
    12 
    133// Here follows lists of files for activation/deactivation at various stages.  Each must be NULL-terminated.
     4
     5/// NOP list
     6static char *filesNOP[] = { NULL };
    147
    158/// Files required in preparation for convolution
     
    4134{
    4235    switch (list) {
     36      case PPSTACK_FILES_NONE:     return filesNOP;
    4337      case PPSTACK_FILES_PREPARE:  return filesPrepare;
    4438      case PPSTACK_FILES_TARGET:   return filesTarget;
     
    206200    return true;
    207201}
     202
     203// Write an image to a FITS file
     204bool ppStackWriteVariance(const char *name, // Name of image
     205                          psMetadata *header, // Header
     206                          const psImage *variance, // Variance
     207                          const psImage *covariance, // Variance
     208                          pmConfig *config // Configuration
     209    )
     210{
     211    assert(name);
     212    assert(variance);
     213
     214    psString resolved = pmConfigConvertFilename(name, config, true, true); // Resolved file name
     215    psFits *fits = psFitsOpen(resolved, "w");
     216    if (!fits) {
     217        psError(PPSTACK_ERR_IO, false, "Unable to open FITS file %s to write image.", resolved);
     218        psFree(resolved);
     219        return false;
     220    }
     221    if (!psFitsWriteImage(fits, header, variance, 0, NULL)) {
     222        psError(PPSTACK_ERR_IO, false, "Unable to write FITS image %s.", resolved);
     223        psFitsClose(fits);
     224        psFree(resolved);
     225        return false;
     226    }
     227    if (covariance) {
     228        psMetadata *tmphead = psMetadataAlloc();
     229        psMetadataAddS32(tmphead, PS_LIST_TAIL, "COVARIANCE.CENTRE.X", PS_META_REPLACE, "center", (int)(covariance->numCols / 2));
     230        psMetadataAddS32(tmphead, PS_LIST_TAIL, "COVARIANCE.CENTRE.Y", PS_META_REPLACE, "center", (int)(covariance->numRows / 2));
     231        if (!psFitsWriteImage(fits, tmphead, covariance, 0, "COVAR_SkyChip_SkyCell")) {
     232            psError(PPSTACK_ERR_IO, false, "Unable to write FITS image %s.", resolved);
     233            psFitsClose(fits);
     234            psFree(resolved);
     235            return false;
     236        }
     237    }
     238    if (!psFitsClose(fits)) {
     239        psError(PPSTACK_ERR_IO, false, "Unable to close FITS image %s.", resolved);
     240        psFree(resolved);
     241        return false;
     242    }
     243    psFree(resolved);
     244    return true;
     245}
  • branches/czw_branch/20101203/ppStack/src/ppStackFinish.c

    r27906 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
     1#include "ppStack.h"
    42
    5 #include <stdio.h>
    6 #include <unistd.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 #include <psphot.h>
    10 
    11 #include "ppStack.h"
    12 #include "ppStackLoop.h"
    13 
     3// this function unlinks the temporary files (if desired)
    144bool ppStackFinish(ppStackOptions *options, pmConfig *config)
    155{
     
    3222            }
    3323
    34             psString imageResolved = pmConfigConvertFilename(options->convImages->data[i],
    35                                                              config, false, false);
    36             psString maskResolved = pmConfigConvertFilename(options->convMasks->data[i],
    37                                                             config, false, false);
    38             psString varianceResolved = pmConfigConvertFilename(options->convVariances->data[i],
    39                                                                 config, false, false);
     24            // XXX careful about repeatative resolution of nebulous names (though these are probably not neb names)
     25            psString imageResolved = pmConfigConvertFilename(options->convImages->data[i], config, false, false);
     26            psString maskResolved = pmConfigConvertFilename(options->convMasks->data[i], config, false, false);
     27            psString varianceResolved = pmConfigConvertFilename(options->convVariances->data[i], config, false, false);
    4028            if (unlink(imageResolved) == -1 || unlink(maskResolved) == -1 ||
    4129                unlink(varianceResolved) == -1) {
     
    4735        }
    4836    }
    49 
    50 
    5137    return true;
    5238}
     
    6046
    6147    psErrorCode errorCode = psErrorCodeLast(); // Error code
    62     if (errorCode != PS_ERR_NONE) {
    63         psErrorStackPrint(stderr, "Unable to perform stack.");
    64         pmFPAfileFreeSetStrict(false);
    65         switch (errorCode) {
    66           case PPSTACK_ERR_UNKNOWN:
    67           case PS_ERR_UNKNOWN:
    68             psLogMsg("ppStack", PS_LOG_WARN, "Unknown error code: %x", errorCode);
    69             exitValue = PS_EXIT_UNKNOWN_ERROR;
    70             break;
    71           case PS_ERR_IO:
    72           case PS_ERR_DB_CLIENT:
    73           case PS_ERR_DB_SERVER:
    74           case PS_ERR_BAD_FITS:
    75           case PS_ERR_OS_CALL_FAILED:
    76           case PM_ERR_SYS:
    77           case PPSTACK_ERR_IO:
    78             psLogMsg("ppStack", PS_LOG_WARN, "I/O error code: %x", errorCode);
    79             exitValue = PS_EXIT_SYS_ERROR;
    80             break;
    81           case PS_ERR_BAD_PARAMETER_VALUE:
    82           case PS_ERR_BAD_PARAMETER_TYPE:
    83           case PS_ERR_BAD_PARAMETER_NULL:
    84           case PS_ERR_BAD_PARAMETER_SIZE:
    85           case PPSTACK_ERR_ARGUMENTS:
    86           case PPSTACK_ERR_CONFIG:
    87             psLogMsg("ppStack", PS_LOG_WARN, "Configuration error code: %x", errorCode);
    88             exitValue = PS_EXIT_CONFIG_ERROR;
    89             break;
    90           case PPSTACK_ERR_PSF:
    91           case PSPHOT_ERR_PSF:
    92           case PM_ERR_STAMPS:
    93           case PM_ERR_SMALL_AREA:
    94           case PPSTACK_ERR_REJECTED:
    95           case PPSTACK_ERR_DATA:
    96             psLogMsg("ppStack", PS_LOG_WARN, "Data error code: %x", errorCode);
    97             exitValue = PS_EXIT_DATA_ERROR;
    98             break;
    99           case PS_ERR_UNEXPECTED_NULL:
    100           case PS_ERR_PROGRAMMING:
    101           case PPSTACK_ERR_NOT_IMPLEMENTED:
    102           case PPSTACK_ERR_PROG:
    103             psLogMsg("ppStack", PS_LOG_WARN, "Programming error code: %x", errorCode);
    104             exitValue = PS_EXIT_PROG_ERROR;
    105             break;
    106           default:
    107             // It's a programming error if we're not dealing with the error correctly
    108             psLogMsg("ppStack", PS_LOG_WARN, "Unrecognised error code: %x", errorCode);
    109             exitValue = PS_EXIT_PROG_ERROR;
    110             break;
    111         }
     48    if (errorCode == PS_ERR_NONE) {
     49        return exitValue;
     50    }
     51
     52    psErrorStackPrint(stderr, "Unable to perform stack.");
     53    pmFPAfileFreeSetStrict(false);
     54    switch (errorCode) {
     55      case PPSTACK_ERR_UNKNOWN:
     56      case PS_ERR_UNKNOWN:
     57        psLogMsg("ppStack", PS_LOG_WARN, "Unknown error code: %x", errorCode);
     58        exitValue = PS_EXIT_UNKNOWN_ERROR;
     59        break;
     60      case PS_ERR_IO:
     61      case PS_ERR_DB_CLIENT:
     62      case PS_ERR_DB_SERVER:
     63      case PS_ERR_BAD_FITS:
     64      case PS_ERR_OS_CALL_FAILED:
     65      case PM_ERR_SYS:
     66      case PPSTACK_ERR_IO:
     67        psLogMsg("ppStack", PS_LOG_WARN, "I/O error code: %x", errorCode);
     68        exitValue = PS_EXIT_SYS_ERROR;
     69        break;
     70      case PS_ERR_BAD_PARAMETER_VALUE:
     71      case PS_ERR_BAD_PARAMETER_TYPE:
     72      case PS_ERR_BAD_PARAMETER_NULL:
     73      case PS_ERR_BAD_PARAMETER_SIZE:
     74      case PPSTACK_ERR_ARGUMENTS:
     75      case PPSTACK_ERR_CONFIG:
     76        psLogMsg("ppStack", PS_LOG_WARN, "Configuration error code: %x", errorCode);
     77        exitValue = PS_EXIT_CONFIG_ERROR;
     78        break;
     79      case PPSTACK_ERR_PSF:
     80      case PSPHOT_ERR_PSF:
     81      case PM_ERR_STAMPS:
     82      case PM_ERR_SMALL_AREA:
     83      case PPSTACK_ERR_REJECTED:
     84      case PPSTACK_ERR_DATA:
     85        psLogMsg("ppStack", PS_LOG_WARN, "Data error code: %x", errorCode);
     86        exitValue = PS_EXIT_DATA_ERROR;
     87        break;
     88      case PS_ERR_UNEXPECTED_NULL:
     89      case PS_ERR_PROGRAMMING:
     90      case PPSTACK_ERR_NOT_IMPLEMENTED:
     91      case PPSTACK_ERR_PROG:
     92        psLogMsg("ppStack", PS_LOG_WARN, "Programming error code: %x", errorCode);
     93        exitValue = PS_EXIT_PROG_ERROR;
     94        break;
     95      default:
     96        // It's a programming error if we're not dealing with the error correctly
     97        psLogMsg("ppStack", PS_LOG_WARN, "Unrecognised error code: %x", errorCode);
     98        exitValue = PS_EXIT_PROG_ERROR;
     99        break;
    112100    }
    113101    return exitValue;
  • branches/czw_branch/20101203/ppStack/src/ppStackLoop.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
     2
     3// static functions are defined below
     4static int stackSummary(const ppStackOptions *options, const char *place);
     5
     6bool ppStackLoop(pmConfig *config, ppStackOptions *options)
     7{
     8    assert(config);
     9
     10    psTimerStart("PPSTACK_TOTAL");
     11    psTimerStart("PPSTACK_STEPS");
     12
     13    // Setup
     14    psTrace("ppStack", 1, "Setup....\n");
     15    if (!ppStackSetup(options, config)) {
     16        psError(psErrorCodeLast(), false, "Unable to setup.");
     17        return false;
     18    }
     19    psLogMsg("ppStack", PS_LOG_INFO, "Stage 0: Setup: %f sec", psTimerClear("PPSTACK_STEPS"));
     20    ppStackMemDump("setup");
     21
     22    // Preparation for stacking
     23    psTrace("ppStack", 1, "Preparation for stacking: merging sources, determining target PSF....\n");
     24
     25    if (!ppStackPrepare(options, config)) {
     26        psError(psErrorCodeLast(), false, "Unable to prepare for stacking.");
     27        return false;
     28    }
     29    psLogMsg("ppStack", PS_LOG_INFO, "Stage 1: Load Sources and Generate Target PSF: %f sec", psTimerClear("PPSTACK_STEPS"));
     30    ppStackMemDump("prepare");
     31    if (options->quality) return true; // Can't do anything else
     32
     33    // Convolve inputs
     34    psTrace("ppStack", 1, "Convolving inputs to target PSF....\n");
     35    if (!ppStackConvolve(options, config)) {
     36        psError(psErrorCodeLast(), false, "Unable to convolve images.");
     37        return false;
     38    }
     39    psLogMsg("ppStack", PS_LOG_INFO, "Stage 2: Generate Convolutions and Save: %f sec", psTimerClear("PPSTACK_STEPS"));
     40    ppStackMemDump("convolve");
     41    if (options->quality) return true; // Can't do anything else
     42
     43    // Ensure sufficient inputs
     44    {
     45        int numGood = stackSummary(options, "initial combination");
     46        psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
     47        bool safe = psMetadataLookupBool(NULL, recipe, "SAFE"); // Be safe when combining
     48        if (safe && numGood <= 1) {
     49            options->quality = PPSTACK_ERR_REJECTED;
     50            psErrorStackPrint(stderr, "Insufficient inputs for combination with safety on");
     51            psErrorClear();
     52            psWarning("Insufficient inputs for combination with safety on");
     53            return true;
     54        }
     55    }
     56
     57    // Start threading
     58    ppStackThreadInit();
     59    ppStackThreadData *stack = ppStackThreadDataSetup(options, config, true);
     60    if (!stack) {
     61        psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     62        return false;
     63    }
     64
     65    // Prepare for combination
     66    if (!ppStackCombinePrepare("PPSTACK.OUTPUT", "PPSTACK.OUTPUT.EXP", PPSTACK_FILES_STACK, stack, options, config)) {
     67        psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
     68        psFree(stack);
     69        return false;
     70    }
     71
     72    // Initial combination
     73    psTrace("ppStack", 1, "Initial stack of convolved images....\n");
     74    if (!ppStackCombineInitial(stack, options, config)) {
     75        psError(psErrorCodeLast(), false, "Unable to perform initial combination.");
     76        psFree(stack);
     77        return false;
     78    }
     79    ppStackMemDump("initial");
     80    psLogMsg("ppStack", PS_LOG_INFO, "Stage 3: Make Initial Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     81
     82    // Done with stack inputs for now
     83    // XXX is this where we are leaking??
     84    for (int i = 0; i < options->num; i++) {
     85        pmCellFreeData(options->cells->data[i]);
     86    }
     87    psFree(stack);
     88
     89    // Pixel rejection
     90    psTrace("ppStack", 1, "Reject pixels....\n");
     91    if (!ppStackReject(options, config)) {
     92        psError(psErrorCodeLast(), false, "Unable to reject pixels.");
     93        psFree(stack);
     94        return false;
     95    }
     96    psLogMsg("ppStack", PS_LOG_INFO, "Stage 4: Pixel Rejection: %f sec", psTimerClear("PPSTACK_STEPS"));
     97
     98    // Check inputs
     99    {
     100        int numGood = stackSummary(options, "final combination");
     101        if (numGood <= 0) {
     102            options->quality = PPSTACK_ERR_REJECTED;
     103            psErrorStackPrint(stderr, "Insufficient inputs survived rejection stage");
     104            psErrorClear();
     105            psWarning("Insufficient inputs survived rejection stage");
     106            return true;
     107        }
     108    }
     109
     110    stack = ppStackThreadDataSetup(options, config, true);
     111    if (!stack) {
     112        psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     113        return false;
     114    }
     115
     116    // Final combination
     117    psTrace("ppStack", 2, "Final stack of convolved images....\n");
     118    if (!ppStackCombineFinal(stack, options->convCovars, options, config, false, false, true)) {
     119        psError(psErrorCodeLast(), false, "Unable to perform final combination.");
     120        psFree(stack);
     121        return false;
     122    }
     123    psLogMsg("ppStack", PS_LOG_INFO, "Stage 5: Final Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     124    ppStackMemDump("final");
     125
     126    // Photometry
     127    psTrace("ppStack", 1, "Photometering stacked image....\n");
     128    if (!ppStackPhotometry(options, config)) {
     129        psError(psErrorCodeLast(), false, "Unable to perform photometry.");
     130        return false;
     131    }
     132    psLogMsg("ppStack", PS_LOG_INFO, "Stage 6: Photometry Analysis: %f sec", psTimerClear("PPSTACK_STEPS"));
     133    ppStackMemDump("photometry");
     134
     135    // Update Header
     136    if (!ppStackUpdateHeader(stack, options, config)) {
     137        psError(psErrorCodeLast(), false, "Unable to update header.");
     138        psFree(stack);
     139        return false;
     140    }
     141    // Create JPEGS
     142    if (!ppStackJPEGs(stack, options, config)) {
     143        psError(psErrorCodeLast(), false, "Unable to make jpegs.");
     144        psFree(stack);
     145        return false;
     146    }
     147    // Assemble Stats
     148    if (!ppStackStats(stack, options, config)) {
     149        psError(psErrorCodeLast(), false, "Unable to assemble statistics.");
     150        psFree(stack);
     151        return false;
     152    }
     153
     154   // Clean up
     155    psTrace("ppStack", 2, "Cleaning up after combination....\n");
     156    if (!ppStackCleanupFiles(stack, options, config, PPSTACK_FILES_STACK, PPSTACK_FILES_PHOT, true)) {
     157        psError(psErrorCodeLast(), false, "Unable to clean up.");
     158        psFree(stack);
     159        return false;
     160    }
     161    psFree(stack);
     162    psLogMsg("ppStack", PS_LOG_INFO, "Stage 7: Cleanup, WCS & JPEGS: %f sec", psTimerClear("PPSTACK_STEPS"));
     163    ppStackMemDump("cleanup");
     164
     165    // Unconvolved stack --- it's cheap to calculate, compared to everything else!
     166    // XXX unconvolved stack is currently using the convolved mask!  oops!
     167    if (options->convolve) {
     168        // Start threading
     169        ppStackThreadData *stack = ppStackThreadDataSetup(options, config, false);
     170        if (!stack) {
     171            psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
     172            return false;
     173        }
     174
     175        // Prepare for combination
     176        if (!ppStackCombinePrepare("PPSTACK.UNCONV", "PPSTACK.UNCONV.EXP", PPSTACK_FILES_UNCONV,
     177                                   stack, options, config)) {
     178            psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
     179            psFree(stack);
     180            return false;
     181        }
     182
     183        // generate the unconvolved stack
     184        psTrace("ppStack", 2, "Stack of unconvolved images....\n");
     185        if (!ppStackCombineFinal(stack, options->origCovars, options, config,
     186                                 false, true, false)) {
     187            psError(psErrorCodeLast(), false, "Unable to perform unconvolved combination.");
     188            psFree(stack);
     189            return false;
     190        }
     191        psLogMsg("ppStack", PS_LOG_INFO, "Stage 8: Unconvolved Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
     192        ppStackMemDump("unconv");
     193
     194        // Clean up unconvolved stack
     195        psTrace("ppStack", 2, "Cleaning up after unconvolved stack....\n");
     196        if (!ppStackCleanupFiles(stack, options, config, PPSTACK_FILES_UNCONV, PPSTACK_FILES_NONE, false)) {
     197            psError(psErrorCodeLast(), false, "Unable to clean up.");
     198            psFree(stack);
     199            return false;
     200        }
     201        psFree(stack);
     202    }
     203    psFree(options->cells); options->cells = NULL;
     204
     205    // Finish up
     206    psTrace("ppStack", 1, "Finishing up....\n");
     207    if (!ppStackFinish(options, config)) {
     208        psError(psErrorCodeLast(), false, "Unable to finish up.");
     209        return false;
     210    }
     211    ppStackMemDump("finish");
     212
     213    return true;
     214}
    11215
    12216/// Print a summary of the inputs, and return the number of good inputs
    13 static int stackSummary(const ppStackOptions *options, // Stack options, with input mask
    14                         const char *place              // Place in code
    15     )
     217static int stackSummary(const ppStackOptions *options, const char *place)
    16218{
    17219    int numGood = 0;                // Number of good inputs
     
    47249    return numGood;
    48250}
    49 
    50 
    51 
    52 bool ppStackLoop(pmConfig *config, ppStackOptions *options)
    53 {
    54     assert(config);
    55 
    56     psTimerStart("PPSTACK_TOTAL");
    57     psTimerStart("PPSTACK_STEPS");
    58 
    59     // Setup
    60     psTrace("ppStack", 1, "Setup....\n");
    61     if (!ppStackSetup(options, config)) {
    62         psError(psErrorCodeLast(), false, "Unable to setup.");
    63         return false;
    64     }
    65     psLogMsg("ppStack", PS_LOG_INFO, "Stage 0: Setup: %f sec", psTimerClear("PPSTACK_STEPS"));
    66     ppStackMemDump("setup");
    67 
    68 
    69     // Preparation for stacking
    70     psTrace("ppStack", 1, "Preparation for stacking: merging sources, determining target PSF....\n");
    71     if (!ppStackPrepare(options, config)) {
    72         psError(psErrorCodeLast(), false, "Unable to prepare for stacking.");
    73         return false;
    74     }
    75     psLogMsg("ppStack", PS_LOG_INFO, "Stage 1: Load Sources and Generate Target PSF: %f sec",
    76              psTimerClear("PPSTACK_STEPS"));
    77     ppStackMemDump("prepare");
    78     if (options->quality) {
    79         // Can't do anything else
    80         return true;
    81     }
    82 
    83     // Convolve inputs
    84     psTrace("ppStack", 1, "Convolving inputs to target PSF....\n");
    85     if (!ppStackConvolve(options, config)) {
    86         psError(psErrorCodeLast(), false, "Unable to convolve images.");
    87         return false;
    88     }
    89     psLogMsg("ppStack", PS_LOG_INFO, "Stage 2: Generate Convolutions and Save: %f sec",
    90              psTimerClear("PPSTACK_STEPS"));
    91     ppStackMemDump("convolve");
    92 
    93     if (options->quality) {
    94         // Can't do anything else
    95         return true;
    96     }
    97 
    98     // Ensure sufficient inputs
    99     {
    100         int numGood = stackSummary(options, "initial combination");
    101         psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSTACK_RECIPE); // ppStack recipe
    102         bool safe = psMetadataLookupBool(NULL, recipe, "SAFE"); // Be safe when combining
    103         if (safe && numGood <= 1) {
    104             options->quality = PPSTACK_ERR_REJECTED;
    105             psErrorStackPrint(stderr, "Insufficient inputs for combination with safety on");
    106             psErrorClear();
    107             psWarning("Insufficient inputs for combination with safety on");
    108             return true;
    109         }
    110     }
    111 
    112     // Start threading
    113     ppStackThreadInit();
    114     ppStackThreadData *stack = ppStackThreadDataSetup(options, config, true);
    115     if (!stack) {
    116         psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    117         return false;
    118     }
    119 
    120     // Prepare for combination
    121     if (!ppStackCombinePrepare("PPSTACK.OUTPUT", "PPSTACK.OUTPUT.EXP", PPSTACK_FILES_STACK,
    122                                stack, options, config)) {
    123         psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
    124         psFree(stack);
    125         return false;
    126     }
    127 
    128     // Initial combination
    129     psTrace("ppStack", 1, "Initial stack of convolved images....\n");
    130     if (!ppStackCombineInitial(stack, options, config)) {
    131         psError(psErrorCodeLast(), false, "Unable to perform initial combination.");
    132         psFree(stack);
    133         return false;
    134     }
    135     ppStackMemDump("initial");
    136     psLogMsg("ppStack", PS_LOG_INFO, "Stage 3: Make Initial Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    137 
    138     // Done with stack inputs for now
    139     for (int i = 0; i < options->num; i++) {
    140         pmCellFreeData(options->cells->data[i]);
    141     }
    142     psFree(stack);
    143 
    144     // Pixel rejection
    145     psTrace("ppStack", 1, "Reject pixels....\n");
    146     if (!ppStackReject(options, config)) {
    147         psError(psErrorCodeLast(), false, "Unable to reject pixels.");
    148         psFree(stack);
    149         return false;
    150     }
    151     psLogMsg("ppStack", PS_LOG_INFO, "Stage 4: Pixel Rejection: %f sec", psTimerClear("PPSTACK_STEPS"));
    152 
    153     // Check inputs
    154     {
    155         int numGood = stackSummary(options, "final combination");
    156         if (numGood <= 0) {
    157             options->quality = PPSTACK_ERR_REJECTED;
    158             psErrorStackPrint(stderr, "Insufficient inputs survived rejection stage");
    159             psErrorClear();
    160             psWarning("Insufficient inputs survived rejection stage");
    161             return true;
    162         }
    163     }
    164 
    165     stack = ppStackThreadDataSetup(options, config, true);
    166     if (!stack) {
    167         psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    168         return false;
    169     }
    170 
    171     // Final combination
    172     psTrace("ppStack", 2, "Final stack of convolved images....\n");
    173     if (!ppStackCombineFinal(stack, options->convCovars, options, config, false, false, true)) {
    174         psError(psErrorCodeLast(), false, "Unable to perform final combination.");
    175         psFree(stack);
    176         return false;
    177     }
    178     psLogMsg("ppStack", PS_LOG_INFO, "Stage 5: Final Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    179     ppStackMemDump("final");
    180 
    181     // Photometry
    182     psTrace("ppStack", 1, "Photometering stacked image....\n");
    183     if (!ppStackPhotometry(options, config)) {
    184         psError(psErrorCodeLast(), false, "Unable to perform photometry.");
    185         return false;
    186     }
    187     psLogMsg("ppStack", PS_LOG_INFO, "Stage 6: Photometry Analysis: %f sec", psTimerClear("PPSTACK_STEPS"));
    188     ppStackMemDump("photometry");
    189 
    190     // Clean up
    191     psTrace("ppStack", 2, "Cleaning up after combination....\n");
    192     if (!ppStackCleanup(stack, options, config)) {
    193         psError(psErrorCodeLast(), false, "Unable to clean up.");
    194         psFree(stack);
    195         return false;
    196     }
    197     for (int i = 0; i < options->num; i++) {
    198         pmCellFreeData(options->cells->data[i]);
    199     }
    200     psFree(stack);
    201     psLogMsg("ppStack", PS_LOG_INFO, "Stage 7: Cleanup, WCS & JPEGS: %f sec", psTimerClear("PPSTACK_STEPS"));
    202     ppStackMemDump("cleanup");
    203 
    204 #if 1
    205     // Unconvolved stack --- it's cheap to calculate, compared to everything else!
    206     // XXX unconvolved stack is currently using the convolved mask!  oops!
    207     if (options->convolve) {
    208         // Start threading
    209         ppStackThreadData *stack = ppStackThreadDataSetup(options, config, false);
    210         if (!stack) {
    211             psError(psErrorCodeLast(), false, "Unable to initialise stack threads.");
    212             return false;
    213         }
    214 
    215         // Prepare for combination
    216         if (!ppStackCombinePrepare("PPSTACK.UNCONV", "PPSTACK.UNCONV.EXP", PPSTACK_FILES_UNCONV,
    217                                    stack, options, config)) {
    218             psError(psErrorCodeLast(), false, "Unable to prepare for combination.");
    219             psFree(stack);
    220             return false;
    221         }
    222 
    223         psTrace("ppStack", 2, "Stack of unconvolved images....\n");
    224         if (!ppStackCombineFinal(stack, options->origCovars, options, config,
    225                                  false, true, false)) {
    226             psError(psErrorCodeLast(), false, "Unable to perform unconvolved combination.");
    227             psFree(stack);
    228             return false;
    229         }
    230         psLogMsg("ppStack", PS_LOG_INFO, "Stage 8: Unconvolved Stack: %f sec", psTimerClear("PPSTACK_STEPS"));
    231         ppStackMemDump("unconv");
    232 
    233         if (!ppStackFilesIterateUp(config)) {
    234             psError(psErrorCodeLast(), false, "Unable to close files.");
    235             psFree(stack);
    236             return false;
    237         }
    238         ppStackFileActivation(config, PPSTACK_FILES_UNCONV, false);
    239         options->outRO->data_exists = false;
    240         options->outRO->parent->data_exists = false;
    241         options->outRO->parent->parent->data_exists = false;
    242         psFree(options->outRO);
    243         options->outRO = NULL;
    244         options->expRO->data_exists = false;
    245         options->expRO->parent->data_exists = false;
    246         options->expRO->parent->parent->data_exists = false;
    247         psFree(options->expRO);
    248         options->expRO = NULL;
    249 
    250         for (int i = 0; i < options->num; i++) {
    251             pmCellFreeData(options->cells->data[i]);
    252         }
    253         psFree(stack);
    254     }
    255     psFree(options->cells); options->cells = NULL;
    256 #endif
    257 
    258     // Finish up
    259     psTrace("ppStack", 1, "Finishing up....\n");
    260     if (!ppStackFinish(options, config)) {
    261         psError(psErrorCodeLast(), false, "Unable to finish up.");
    262         return false;
    263     }
    264     ppStackMemDump("finish");
    265 
    266     return true;
    267 }
  • branches/czw_branch/20101203/ppStack/src/ppStackLoop.h

    r27427 r30631  
    11#ifndef PPSTACK_LOOP_H
    22#define PPSTACK_LOOP_H
    3 
    4 #include <pslib.h>
    5 #include <psmodules.h>
    6 
    7 #include "ppStackOptions.h"
    8 #include "ppStackThread.h"
    9 #include "ppStack.h"
    10 
    113
    124// Loop over the inputs, doing the combination
     
    7163
    7264// Cleanup following combination
    73 bool ppStackCleanup(
     65bool ppStackCleanupFiles(
    7466    ppStackThreadData *stack,           // Stack
    7567    ppStackOptions *options,            // Options
    76     pmConfig *config                    // Configuration
    77     );
     68    pmConfig *config,                   // Configuration
     69    ppStackFileList stackFiles,         // cleanup these stack files
     70    ppStackFileList photFiles,          // cleanup these phot files (PHOT or NOP)
     71    bool closeJPEGs                     // close the jpeg files?
     72);
    7873
    7974// Photometry
     
    8984    );
    9085
     86
     87bool ppStackUpdateHeader(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config);
     88bool ppStackJPEGs(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config);
     89bool ppStackStats(ppStackThreadData *stack, ppStackOptions *options, pmConfig *config);
     90
    9191#endif
  • branches/czw_branch/20101203/ppStack/src/ppStackMatch.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <psphot.h>
    9 
    101#include "ppStack.h"
    112
     
    325316                // Scale the input parameters
    326317                psVector *widthsCopy = psVectorCopy(NULL, widths, PS_TYPE_F32); // Copy of kernel widths
    327                 if (scale && !pmSubtractionParamsScale(&size, &footprint, widthsCopy, scaleRef, scaleMin, scaleMax)) {
    328                     psError(psErrorCodeLast(), false, "Unable to scale kernel parameters");
    329                     psFree(fake);
    330                     psFree(optWidths);
    331                     psFree(conv);
    332                     psFree(widthsCopy);
    333                     if (threads > 0) {
    334                         pmSubtractionThreadsFinalize();
    335                     }
    336                     return false;
    337                 }
     318
     319                pmSubtractionParamScaleOptions(scale, scaleRef, scaleMin, scaleMax);
     320
     321                // XXX EAM : the kernel scaling process has changed: the scale is now set
     322                // inside pmSubtractionMatch after the normalization window is measured
     323
     324                // if (scale && !pmSubtractionParamsScale(&size, &footprint, widthsCopy, scaleRef, scaleMin, scaleMax)) {
     325                //     psError(psErrorCodeLast(), false, "Unable to scale kernel parameters");
     326                //     psFree(fake);
     327                //     psFree(optWidths);
     328                //     psFree(conv);
     329                //     psFree(widthsCopy);
     330                //     if (threads > 0) {
     331                //         pmSubtractionThreadsFinalize();
     332                //     }
     333                //     return false;
     334                // }
    338335
    339336                if (!pmSubtractionMatch(NULL, conv, fake, readout, footprint, stride, regionSize, spacing,
  • branches/czw_branch/20101203/ppStack/src/ppStackOptions.c

    r27427 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <pslib.h>
    6 #include "ppStackLoop.h"
     1#include "ppStack.h"
    72
    83static void stackOptionsFree(ppStackOptions *options)
     
    3631    psFree(options->inspect);
    3732    psFree(options->rejected);
     33
    3834    return;
    3935}
  • branches/czw_branch/20101203/ppStack/src/ppStackOptions.h

    r27427 r30631  
    11#ifndef PPSTACK_OPTIONS_H
    22#define PPSTACK_OPTIONS_H
    3 
    4 #include <pslib.h>
    5 #include <psmodules.h>
    63
    74/// Options for stacking process
  • branches/czw_branch/20101203/ppStack/src/ppStackPSF.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    102
  • branches/czw_branch/20101203/ppStack/src/ppStackPhotometry.c

    r30118 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <psphot.h>
    9 
    101#include "ppStack.h"
    11 #include "ppStackLoop.h"
    122
    133bool ppStackPhotometry(ppStackOptions *options, pmConfig *config)
  • branches/czw_branch/20101203/ppStack/src/ppStackPrepare.c

    r27446 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    112
    123#define PHOT_SOURCE_MASK (PM_SOURCE_MODE_FAIL | PM_SOURCE_MODE_SATSTAR | PM_SOURCE_MODE_BLEND | \
  • branches/czw_branch/20101203/ppStack/src/ppStackReadout.c

    r29552 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <psphot.h>
    9 
    10 #include "ppStackThread.h"
    111#include "ppStack.h"
    122
  • branches/czw_branch/20101203/ppStack/src/ppStackReject.c

    r28405 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 
    91#include "ppStack.h"
    10 #include "ppStackLoop.h"
    112
    123// #define TESTING
  • branches/czw_branch/20101203/ppStack/src/ppStackSetup.c

    r27158 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <libgen.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 
    101#include "ppStack.h"
    11 #include "ppStackLoop.h"
    122
    133#define BUFFER 16                       // Buffer for name array
    14 
    154
    165bool ppStackSetup(ppStackOptions *options, pmConfig *config)
  • branches/czw_branch/20101203/ppStack/src/ppStackSources.c

    r28144 r30631  
    1 #include <stdio.h>
    2 #include <math.h>
    3 #include <string.h>
    4 #include <pslib.h>
    5 #include <psmodules.h>
    6 
    71#include "ppStack.h"
    82
  • branches/czw_branch/20101203/ppStack/src/ppStackTarget.c

    r28302 r30631  
    1 #include <stdio.h>
    2 #include <pslib.h>
    3 #include <psmodules.h>
    4 
    51#include "ppStack.h"
    62
     
    3733        return psMemIncrRefCounter(sources);
    3834    }
     35    exclusion = 2;
    3936
    4037    int num = sources->n;               // Number of sources
  • branches/czw_branch/20101203/ppStack/src/ppStackThread.c

    r27427 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <unistd.h>
    7 #include <pslib.h>
    8 #include <psmodules.h>
    9 
    101#include "ppStack.h"
    11 #include "ppStackOptions.h"
    12 #include "ppStackThread.h"
    13 
    142
    153#define THREAD_WAIT 10000               // Microseconds to wait if thread is not available
    16 
    174
    185static void stackThreadFree(ppStackThread *thread)
  • branches/czw_branch/20101203/ppStack/src/ppStackThread.h

    r26076 r30631  
    11#ifndef PPSTACK_THREAD_H
    22#define PPSTACK_THREAD_H
    3 
    4 #include <pslib.h>
    5 #include <psmodules.h>
    6 
    7 #include "ppStackOptions.h"
    83
    94// Thread for stacking chunks
  • branches/czw_branch/20101203/ppStack/src/ppStackVersion.c

    r28043 r30631  
    1 #ifdef HAVE_CONFIG_H
    2 #include <config.h>
    3 #endif
    4 
    5 #include <stdio.h>
    6 #include <pslib.h>
    7 #include <psmodules.h>
    8 #include <ppStats.h>
    9 #include <psphot.h>
    10 
    111#include "ppStack.h"
    122#include "ppStackVersionDefinitions.h"
  • branches/czw_branch/20101203/ppSub/src/ppSub.c

    r29551 r30631  
    112112        psLibFinalize();
    113113
     114        fprintf (stderr, "found %d leaks at %s\n", psMemCheckLeaks (0, NULL, stdout, false), "ppSub");
    114115        exitValue = ppSubExitCode(exitValue);
    115116        exit(exitValue);
  • branches/czw_branch/20101203/ppSub/src/ppSubFiles.c

    r27094 r30631  
    125125    // FPA
    126126    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     127        psFree (view);
    127128        return false;
    128129    }
     
    131132    view->chip = 0;
    132133    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     134        psFree (view);
    133135        return false;
    134136    }
     
    137139    view->cell = 0;
    138140    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
     141        psFree (view);
    139142        return false;
    140143    }
     
    143146    view->readout = 0;
    144147    if (!pmFPAfileIOChecks(config, view, PM_FPA_BEFORE)) {
    145         return false;
    146     }
    147 
    148     ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
    149 
     148        psFree (view);
     149        return false;
     150    }
     151
     152    ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
     153
     154    psFree (view);
    150155    return true;
    151156}
     
    162167    // Readout
    163168    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     169        psFree (view);
    164170        return false;
    165171    }
     
    168174    view->readout = -1;
    169175    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     176        psFree (view);
    170177        return false;
    171178    }
     
    174181    view->cell = -1;
    175182    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     183        psFree (view);
    176184        return false;
    177185    }
     
    180188    view->chip = -1;
    181189    if (!pmFPAfileIOChecks(config, view, PM_FPA_AFTER)) {
     190        psFree (view);
    182191        return false;
    183192    }
     
    222231    ppSubFilesActivate(config, PPSUB_FILES_ALL, false);
    223232
     233    psFree (view);
    224234    return true;
    225235}
  • branches/czw_branch/20101203/ppSub/src/ppSubInputDetections.c

    r30118 r30631  
    118118        if (!psMetadataCopy(sourcesHDU->header, imageHDU->header)) {
    119119            psError(PPSUB_ERR_PROG, false, "Unable to copy header");
     120            psFree(view);
    120121            return false;
    121122        }
     
    130131    if (!psphotCopyResults (foundDetections, sourcesFile, photFile, view)) {
    131132        psError(PPSUB_ERR_PROG, false, "Unable to copy psphot outputs");
     133        psFree(view);
    132134        return false;
    133135    }
    134136    // if no sources were found here, we report that back and let them handle it
    135137
     138    psFree(view);
    136139    return true;
    137140}
  • branches/czw_branch/20101203/ppSub/src/ppSubLoop.c

    r30118 r30631  
    2626  pmReadout *out = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
    2727  psphotSaveImage (NULL, out->image, name);
     28  psFree(view);
    2829  return true;
    2930}
     
    111112        goto ESCAPE;
    112113    }
     114
    113115    if (data->quality) {
    114116        // Can't do anything at all
     
    257259        psError(psErrorCodeLast(), false, "Unable to generate mask statistics");
    258260        success = false;
     261        psFree(view);
    259262        goto ESCAPE;
    260263      }
     264      psFree(view);
    261265    }
    262266    // dumpout(config, "diff.4.fits");
  • branches/czw_branch/20101203/ppSub/src/ppSubMakePSF.c

    r30118 r30631  
    7979    if (!detections || !detections->allSources) {
    8080        psError(PPSUB_ERR_CONFIG, true, "No sources from which to determine PSF.");
     81        psFree(view);
    8182        return false;
    8283    }
     
    8687    // use flags to toss totally bogus entries?
    8788    psArray *goodSources = ppSubSelectPSFSources (sources);
     89
    8890    if (!psphotReadoutFindPSF(config, view, "PSPHOT.INPUT", goodSources)) {
    8991        // This is likely a data quality issue
     
    126128        outputRO = pmReadoutAlloc(outputCell);
    127129        outputRO->image = psMemIncrRefCounter(inputRO->image);
     130        psFree(outputRO); // I have a copy on the outputCell
    128131    }
    129132
  • branches/czw_branch/20101203/ppSub/src/ppSubMatchPSFs.c

    r30118 r30631  
    7878        return true;
    7979    }
    80 
    8180    psFree(view);
    8281
     
    127126    if (!inSourceRO || !refSourceRO) {
    128127        psError(PPSUB_ERR_DATA, false, "Unable to scale kernel, since no sources were provided.");
     128        psFree(view);
    129129        return false;
    130130    }
     
    164164
    165165    // is auto-scaling needed?
    166     if (!psMetadataLookupBool(NULL, recipe, "SCALE")) {
    167         // No scaling requested
    168         psFree(view);
    169         return true;
    170     }
    171 
     166    bool scale = psMetadataLookupBool(NULL, recipe, "SCALE");
    172167    float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling
    173168    float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling
    174169    float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling
    175     if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) {
     170
     171    if (scale && (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax))) {
    176172        psError(PPSUB_ERR_ARGUMENTS, false,
    177                 "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
     173                "auto-scale selected but scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.",
    178174                scaleRef, scaleMin, scaleMax);
    179175        return false;
    180176    }
    181177
    182     if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths, scaleRef, scaleMin, scaleMax)) {
    183         psError(PPSUB_ERR_DATA, false, "Unable to scale parameters.");
    184         return false;
    185     }
     178    pmSubtractionParamScaleOptions(scale, scaleRef, scaleMin, scaleMax);
     179
     180    // if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths)) {
     181    //     psError(PPSUB_ERR_DATA, false, "Unable to scale parameters.");
     182    //     return false;
     183    // }
    186184
    187185    return true;
     
    221219        pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.INPUT.CONV"); // Cell for convolved input
    222220        inConv = pmReadoutAlloc(cell);
     221        psFree(inConv);
    223222    }
    224223    pmReadout *refConv = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.CONV"); // Reference convolved
     
    226225        pmCell *cell = pmFPAfileThisCell(config->files, view, "PPSUB.REF.CONV"); // Cell for convolved ref.
    227226        refConv = pmReadoutAlloc(cell);
     227        psFree(refConv);
    228228    }
    229229
     
    380380
    381381    if (inRO->covariance) {
    382         psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
     382        psKernel *truncated = psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC);
     383        psFree(inRO->covariance);
     384        inRO->covariance = truncated;
    383385    }
    384386    if (refRO->covariance) {
    385         psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
     387        psKernel *truncated = psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC);
     388        psFree(refRO->covariance);
     389        refRO->covariance = truncated;
    386390    }
    387391
     
    494498
    495499    if (inConv->covariance) {
    496         psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
     500        psKernel *truncated = psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC);
     501        psFree(inConv->covariance);
     502        inConv->covariance = truncated;
    497503    }
    498504    if (refConv->covariance) {
    499         psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
     505        psKernel *truncated = psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC);
     506        psFree(refConv->covariance);
     507        refConv->covariance = truncated;
    500508    }
    501509
  • branches/czw_branch/20101203/ppSub/src/ppSubReadoutForcedPhot.c

    r30118 r30631  
    6161        // XXX remove the pixels from photFile?
    6262        // XXX other cleanup operations?
     63        psFree(view);
    6364        return true;
    6465    }
     
    8990    if (!psphotCopyResults (&foundDetections, outputFile, photFile, view)) {
    9091        psError(PPSUB_ERR_PROG, false, "Unable to copy psphot outputs");
     92        psFree(view);
    9193        return false;
    9294    }
     
    9799    psMetadataAddF32(data->stats, PS_LIST_TAIL, "TIME_PHOT", PS_META_REPLACE, "Time to do photometry", elapsed);
    98100
     101    psFree(view);
    99102    return true;
    100103}
  • branches/czw_branch/20101203/ppSub/src/ppSubReadoutInverse.c

    r27789 r30631  
    3636    if (!pmAstromWriteWCS(invHDU->header, outFPA, outChip, WCS_TOLERANCE)) {
    3737        psError(psErrorCodeLast(), false, "Unable to write WCS astrometry to PPSUB.INVERSE.");
     38        psFree(view);
    3839        return false;
    3940    }
     
    4142    if (!pmAstromReadWCS(invFPA, invChip, invHDU->header, 1.0)) {
    4243        psError(psErrorCodeLast(), false, "Unable to read WCS astrometry.");
     44        psFree(view);
    4345        return false;
    4446    }
    4547
     48    psFree(view);
    4649    return true;
    4750}
  • branches/czw_branch/20101203/ppSub/src/ppSubReadoutJpeg.c

    r26982 r30631  
    139139    }
    140140
     141    psFree(view);
    141142    return true;
    142143}
  • branches/czw_branch/20101203/ppSub/src/ppSubReadoutPhotometry.c

    r30118 r30631  
    109109        if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, "PSPHOT.DETECTIONS")) {
    110110            psError(PPSUB_ERR_PROG, false, "Unable to copy PSPHOT.DETECTIONS");
     111            psFree(view);
    111112            return false;
    112113        }
    113114        if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, "PSPHOT.HEADER")) {
    114115            psError(PPSUB_ERR_PROG, false, "Unable to copy PSPHOT.HEADER");
     116            psFree(view);
    115117            return false;
    116118        }
    117119        if (!psMetadataCopySingle(inRO->analysis, photRO->analysis, PM_DETEFF_ANALYSIS)) {
    118120            psError(PPSUB_ERR_PROG, false, "Unable to copy Detection Efficiency");
     121            psFree(view);
    119122            return false;
    120123        }
     
    128131    }
    129132
     133    psFree(view);
    130134    return true;
    131135}
    132136
    133137#ifdef TESTING
    134     // Record data about sources: not everything gets into the output CMF files
     138// Record data about sources: not everything gets into the output CMF files
    135139    {
    136140        pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout with the sources
  • branches/czw_branch/20101203/ppSub/src/ppSubReadoutSubtract.c

    r29003 r30631  
    3131
    3232    bool reverse = psMetadataLookupBool(&mdok, config->arguments, "REVERSE"); // Reverse sense of subtraction?
     33    bool addPair = psMetadataLookupBool(&mdok, recipe, "ADD.NOT.SUBTRACT"); // add instead of subtracting
    3334
    3435    pmFPAview *view = ppSubViewReadout(); // View to readout
     
    4748    // Do the actual subtraction
    4849    pmReadout *outRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
    49     outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "-", subtrahend->image);
     50
     51    if (addPair) {
     52        outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "+", subtrahend->image);
     53    } else {
     54        outRO->image = (psImage*)psBinaryOp(outRO->image, minuend->image, "-", subtrahend->image);
     55    }
    5056    outRO->mask = (psImage*)psBinaryOp(outRO->mask, minuend->mask, "|", subtrahend->mask);
    5157    outRO->variance = (psImage*)psBinaryOp(outRO->variance, minuend->variance, "+", subtrahend->variance);
     
    7076    psFree(rng);
    7177
     78    // XXX EAM : I suspect that the weighted averaging is giving the wrong answer for
     79    // single-direction PSF matching (and maybe DUAL as well).  I am testing the difference by
     80    // generating A+B images instead of A-B images and then checking the significance of the
     81    // sources in the image
     82
    7283    // Combine the covariances
    7384    // These are weighted by the appropriate mean variance.  This is probably not perfectly correct, but it
     
    7990    covarWeights->data.F32[0] = minuendVar;
    8091    covarWeights->data.F32[1] = subtrahendVar;
     92# if (0)   
    8193    outRO->covariance = psImageCovarianceAverageWeighted(covars, covarWeights);
     94# else
     95    outRO->covariance = psImageCovarianceAverage(covars);
     96# endif
    8297    psFree(covars);
    8398    psFree(covarWeights);
     
    109124    pmChip *outChip = pmFPAfileThisChip(config->files, view, "PPSUB.OUTPUT"); // Output chip
    110125    psFree(view);
     126
    111127    if (!outHDU || !inHDU) {
    112128        psError(PPSUB_ERR_PROG, true, "Unable to find HDU at FPA level to copy astrometry.");
  • branches/czw_branch/20101203/ppSub/src/ppSubSetMasks.c

    r29551 r30631  
    8080    if (!pmReadoutMaskInvalid(inRO, maskValue, satValue)) {
    8181        psError(PPSUB_ERR_DATA, false, "Unable to mask non-finite pixels in input.");
     82        psFree(view);
    8283        return false;
    8384    }
    8485    if (!pmReadoutMaskInvalid(refRO, maskValue, satValue)) {
    8586        psError(PPSUB_ERR_DATA, false, "Unable to mask non-finite pixels in reference.");
     87        psFree(view);
    8688        return false;
    8789    }
     
    9496    if (interpMode == PS_INTERPOLATE_NONE) {
    9597        psError(PPSUB_ERR_CONFIG, false, "Unknown interpolation mode: %s", interpModeStr);
     98        psFree(view);
    9699        return false;
    97100    }
     
    104107    if (!pmReadoutInterpolateBadPixels(inRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
    105108        psError(PPSUB_ERR_DATA, false, "Unable to interpolate bad pixels for input image.");
     109        psFree(view);
    106110        return false;
    107111    }
    108112    if (!pmReadoutInterpolateBadPixels(refRO, maskVal, interpMode, poorFrac, maskPoor, maskBad)) {
    109113        psError(PPSUB_ERR_DATA, false, "Unable to interpolate bad pixels for reference image.");
     114        psFree(view);
    110115        return false;
    111116    }
     
    113118#endif
    114119
     120    psFree(view);
    115121    return true;
    116122}
  • branches/czw_branch/20101203/ppSub/src/ppSubThreshold.c

    r26982 r30631  
    9898    if (!in) {
    9999        psError(PPSUB_ERR_UNKNOWN, false, "Unable to find readout.");
     100        psFree(view);
    100101        return false;
    101102    }
     
    104105    if (!ref) {
    105106        psError(PPSUB_ERR_UNKNOWN, false, "Unable to find readout.");
     107        psFree(view);
    106108        return false;
    107109    }
     
    115117        if (!lowThreshold(in, thresh, maskVal, maskThresh, region, "input convolved image")) {
    116118            psError(psErrorCodeLast(), false, "Unable to threshold input image.");
     119            psFree(view);
    117120            return false;
    118121        }
    119122        if (!lowThreshold(ref, thresh, maskVal, maskThresh, region, "reference convolved image")) {
    120123            psError(psErrorCodeLast(), false, "Unable to threshold input image.");
     124            psFree(view);
    121125            return false;
    122126        }
  • branches/czw_branch/20101203/ppSub/src/ppSubVarianceRescale.c

    r28121 r30631  
    5757        // Nothing to renormalise
    5858        psWarning("Renormalisation of the variance requested, but no variance provided.");
     59        psFree(view);
    5960        return true;
    6061    }
     
    6970    }
    7071
     72    psFree(view);
    7173    return true;
    7274}
  • branches/czw_branch/20101203/ppTranslate/src/ppMopsMerge.c

    r30587 r30631  
    165165    if (num == 0) {
    166166        //All detections were NULL?!
    167         psTrace("ppMops.merge", 3, "All %d detections were NULL\n", detections->n);
     167        psTrace("ppMops.merge", 3, "All %ld detections were NULL\n", detections->n);
    168168        return NULL;
    169169    }
  • branches/czw_branch/20101203/psLib/src/imageops

    • Property svn:mergeinfo deleted
  • branches/czw_branch/20101203/psLib/src/imageops/psImageConvolve.c

    r28405 r30631  
    6767}
    6868
    69 psKernel *psKernelAlloc(int xMin, int xMax, int yMin, int yMax)
     69psKernel *p_psKernelAlloc(const char *file,
     70                          unsigned int lineno,
     71                          const char *func,
     72                          int xMin, int xMax, int yMin, int yMax)
    7073{
    7174    // Check the inputs to make sure max > min; if not, switch.
     
    9194    int numCols = xMax - xMin + 1;      // Number of columns for kernel image
    9295
    93     psKernel *kernel = psAlloc(sizeof(psKernel)); // The kernel, to be returned
     96    psKernel *kernel = p_psAlloc(file, lineno, func, sizeof(psKernel)); // The kernel, to be returned
    9497    psMemSetDeallocator(kernel,(psFreeFunc)kernelFree);
    9598
     
    272275    float threshold = sumKernel * (1.0 - frac); // Threshold for truncation
    273276
     277    int truncateRadius = maxSize;       // Truncation radius
     278    bool truncate = false;
     279
    274280    // Find truncation size
    275     int truncate = 0;                     // Truncation radius
    276     for (int radius = 1; truncate == 0 && radius < maxSize; radius++) {
     281    for (int radius = 1; !truncate && (radius < maxSize); radius++) {
    277282        int uMin = PS_MAX(-radius, xMin);
    278283        int uMax = PS_MIN(radius, xMax);
     
    290295            }
    291296        }
     297        // This is the truncation radius
    292298        if (sum > threshold) {
    293             // This is the truncation radius
    294             truncate = radius;
    295         }
    296     }
    297     if (truncate == maxSize) {
    298         // No truncation possible
     299            truncate = true;
     300            truncateRadius = radius;
     301        }
     302    }
     303
     304    // Do nothing if no truncation is possible
     305    if (!truncate) {
    299306        return true;
    300307    }
     
    302309    // Truncate the kernel
    303310    {
    304         int uMin = PS_MAX(-truncate, xMin);
    305         int uMax = PS_MIN(truncate, xMax);
    306         int vMin = PS_MAX(-truncate, yMin);
    307         int vMax = PS_MIN(truncate, yMax);
    308         int r2 = PS_SQR(truncate);
     311        int uMin = PS_MAX(-truncateRadius, xMin);
     312        int uMax = PS_MIN(truncateRadius, xMax);
     313        int vMin = PS_MAX(-truncateRadius, yMin);
     314        int vMax = PS_MIN(truncateRadius, yMax);
     315        int r2 = PS_SQR(truncateRadius);
    309316        for (int v = vMin; v <= vMax; v++) {
    310317            int v2 = PS_SQR(v);
     
    317324        }
    318325    }
    319     kernel->xMin = PS_MAX(-truncate, kernel->xMin);
    320     kernel->xMax = PS_MIN(truncate, kernel->xMax);
    321     kernel->yMin = PS_MAX(-truncate, kernel->yMin);
    322     kernel->yMax = PS_MIN(truncate, kernel->yMax);
     326    kernel->xMin = PS_MAX(-truncateRadius, kernel->xMin);
     327    kernel->xMax = PS_MIN(truncateRadius, kernel->xMax);
     328    kernel->yMin = PS_MAX(-truncateRadius, kernel->yMin);
     329    kernel->yMax = PS_MIN(truncateRadius, kernel->yMax);
    323330
    324331    return true;
  • branches/czw_branch/20101203/psLib/src/imageops/psImageConvolve.h

    r26892 r30631  
    8484/// @return psKernel*          A new kernel object
    8585///
     86#ifdef DOXYGEN
    8687psKernel *psKernelAlloc(
    8788    int xMin,                          ///< Most negative x index
     
    8990    int yMin,                          ///< Most negative y index
    9091    int yMax                           ///< Most positive y index
     92);
     93#else // ifdef DOXYGEN
     94psKernel *p_psKernelAlloc(
     95    const char *file,                   ///< File of caller
     96    unsigned int lineno,                ///< Line number of caller
     97    const char *func,                   ///< Function name of caller
     98    int xMin,                          ///< Most negative x index
     99    int xMax,                          ///< Most positive x index
     100    int yMin,                          ///< Most negative y index
     101    int yMax                           ///< Most positive y index
    91102) PS_ATTR_MALLOC;
     103#define psKernelAlloc(xMin, xMax, yMin, yMax)                           \
     104    p_psKernelAlloc(__FILE__, __LINE__, __func__, (xMin), (xMax), (yMin), (yMax))
     105#endif // ifdef DOXYGEN
    92106
    93107/// Allocate a convolution kernel from a provided image
  • branches/czw_branch/20101203/psLib/src/imageops/psImageCovariance.c

    r28667 r30631  
    3030    return covar;
    3131}
     32
     33/**
     34
     35 * changes from 28666
     36 ** add scale to imageCovarianceCalculate (& multiply at return)
     37 ** add scale to imageCovarianceCalculateThread
     38
     39 ** accumulate scale (sum kernel^2) in psImageCovarianceCalculate and pass to imageCovarianceCalculate
     40 ** accumulate scale (sum kernel^2) in psImageCovarianceCalculateFactor and pass to imageCovarianceCalculate
     41
     42 **/
    3243
    3344/// Calculation of covariance matrix element when convolving
  • branches/czw_branch/20101203/psLib/src/sys/psLogMsg.c

    r28404 r30631  
    291291        head_ptr += strlen(head_ptr);
    292292    }
    293     // Hostname should be 20 characters.
     293
     294    // Hostname should be 10 characters.
    294295    if (logHost) {
    295296        if (head_ptr > head) {
    296297            *head_ptr++ = '|';
    297298        }
    298         maxLength -= snprintf(head_ptr, maxLength, "%-20s", hostname);
     299        maxLength -= snprintf(head_ptr, maxLength, " %s ", hostname);
    299300        head_ptr += strlen(head_ptr);
    300301    }
     
    310311            *head_ptr++ = '|';
    311312        }
    312         maxLength -= snprintf(head_ptr, maxLength, "%s", name);
     313        maxLength -= snprintf(head_ptr, maxLength, "%s|\n", name);
    313314
    314315        head_ptr += strlen(head_ptr);
    315     }
    316 
    317     if (head_ptr > head) {
    318         *head_ptr++ = '\n';
    319     } else if (!logMsg) {                  // no output desired
     316    } else {
     317      if (head_ptr > head) {
     318        *head_ptr++ = '|';
     319      }
     320    }
     321
     322    // rather than putting in a return for the message, let's only put in the return if we asked for the function name
     323    if ((head_ptr == head) && !logMsg) { // no output desired
    320324        return;
    321325    }
     
    332336        char *line = strtok_r(msg, "\n", &msgPtr);
    333337        while (line) {
    334             if(write(logFD, "    ", 4)) {;} // ignore return value
     338            if(write(logFD, "          ", PS_MIN(2*level, 10))) {;} // ignore return value
    335339            if(write(logFD, line, strlen(line))) {;} // ignore return value
    336340            if(write(logFD, "\n", 1)) {;} // ignore return value
  • branches/czw_branch/20101203/psLib/src/sys/psMemory.c

    r28401 r30631  
    626626            nleak++;
    627627
    628             if (fd != NULL) {
     628            // only print a max of 500 leaks (make this an argument)
     629            if ((nleak < 500) && (fd != NULL)) {
    629630                if (nleak == 1) {
    630631                    fprintf(fd, "# func at (file:line)  ID: X  Ref: X\n");
  • branches/czw_branch/20101203/psLib/test/imageops/Makefile.am

    r30118 r30631  
    2626        tap_psImageMapFit \
    2727        tap_psImageMapFit2 \
    28         tap_psImageMaskOps
     28        tap_psImageMaskOps \
     29        tap_psImageCovariance
    2930
    3031#       tap_psImageShiftKernel
  • branches/czw_branch/20101203/psLib/test/math

    • Property svn:mergeinfo deleted
  • branches/czw_branch/20101203/psModules/src/extras/pmVisual.c

    r29545 r30631  
    1616#include <pslib.h>
    1717
    18 #include "pmHDU.h"
    19 #include "pmFPA.h"
    20 #include "pmFPAfile.h"
    21 #include "pmAstrometryObjects.h"
    22 #include "pmSubtractionStamps.h"
    23 
    24 #include "pmTrend2D.h"
    25 #include "pmResiduals.h"
    26 #include "pmGrowthCurve.h"
    27 #include "pmSpan.h"
    28 #include "pmFootprintSpans.h"
    29 #include "pmFootprint.h"
    30 #include "pmPeaks.h"
    31 #include "pmMoments.h"
    32 #include "pmModelFuncs.h"
    33 #include "pmModel.h"
    34 #include "pmSourceMasks.h"
    35 #include "pmSourceExtendedPars.h"
    36 #include "pmSourceDiffStats.h"
    37 #include "pmSource.h"
    38 #include "pmSourceFitModel.h"
    39 #include "pmPSF.h"
    40 #include "pmPSFtry.h"
    41 
    42 #include "pmFPAExtent.h"
    43 
    44 #include "pmAstrometryVisual.h"
    45 #include "pmSubtractionVisual.h"
    46 #include "pmStackVisual.h"
    47 #include "pmSourceVisual.h"
     18bool pmSubtractionVisualClose(void);
     19bool pmAstromVisualClose(void);
     20bool pmSubtractionVisualClose(void);
     21bool pmStackVisualClose(void);
     22bool pmSourceVisualClose(void);
     23
     24// #include "pmHDU.h"
     25// #include "pmFPA.h"
     26// #include "pmFPAfile.h"
     27// #include "pmAstrometryObjects.h"
     28// #include "pmSubtractionStamps.h"
     29// #include "pmTrend2D.h"
     30// #include "pmResiduals.h"
     31// #include "pmGrowthCurve.h"
     32// #include "pmSpan.h"
     33// #include "pmFootprintSpans.h"
     34// #include "pmFootprint.h"
     35// #include "pmPeaks.h"
     36// #include "pmMoments.h"
     37// #include "pmModelFuncs.h"
     38// #include "pmModel.h"
     39// #include "pmSourceMasks.h"
     40// #include "pmSourceExtendedPars.h"
     41// #include "pmSourceDiffStats.h"
     42// #include "pmSource.h"
     43// #include "pmSourceFitModel.h"
     44// #include "pmPSF.h"
     45// #include "pmPSFtry.h"
     46// #include "pmFPAExtent.h"
     47// #include "pmAstrometryVisual.h"
     48// #include "pmSubtractionVisual.h"
     49// #include "pmStackVisual.h"
     50// #include "pmSourceVisual.h"
    4851
    4952# if (HAVE_KAPA)
     
    306309}
    307310
     311bool pmVisualRangeImage (int kapaFD, psImage *inImage, const char *name, int channel, float min, float max) {
     312
     313    KiiImage image;
     314    KapaImageData data;
     315    Coords coords;
     316
     317    strcpy (coords.ctype, "RA---TAN");
     318
     319    image.data2d = inImage->data.F32;
     320    image.Nx = inImage->numCols;
     321    image.Ny = inImage->numRows;
     322
     323    strcpy (data.name, name);
     324    strcpy (data.file, name);
     325    data.zero = min;
     326    data.range = max - min;
     327    data.logflux = 0;
     328
     329    KiiSetChannel (kapaFD, channel);
     330    KiiNewPicture2D (kapaFD, &image, &data, &coords);
     331
     332    return true;
     333}
    308334
    309335psImage* pmVisualImageToFloat(psImage *image) {
  • branches/czw_branch/20101203/psModules/src/extras/pmVisual.h

    r23242 r30631  
    6464                        const char *name, int channel, bool clip);
    6565
     66bool pmVisualRangeImage (int kapaFD, psImage *inImage, const char *name, int channel, float min, float max);
    6667
    6768/** Calculate statistics on an image.
  • branches/czw_branch/20101203/psModules/src/imcombine/Makefile.am

    r26893 r30631  
    3030        pmStackReject.h         \
    3131        pmSubtraction.h         \
     32        pmSubtractionTypes.h            \
    3233        pmSubtractionAnalysis.h \
    3334        pmSubtractionEquation.h \
  • branches/czw_branch/20101203/psModules/src/imcombine/pmReadoutCombine.c

    r24907 r30631  
    3333    params->blank = 0;
    3434    params->nKeep = 0;
    35     params->fracHigh = 0.0;
     35    params->fracLow = 0.0;
    3636    params->fracHigh = 0.0;
    3737    params->iter = 1;
  • branches/czw_branch/20101203/psModules/src/imcombine/pmStackReject.c

    r28405 r30631  
    77#include <pslib.h>
    88
     9#include "pmFPA.h"
     10#include "pmSubtractionTypes.h"
    911#include "pmSubtraction.h"
    1012#include "pmSubtractionThreads.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtraction.c

    r29777 r30631  
    2020#include "pmHDU.h"                      // Required for pmFPA.h
    2121#include "pmFPA.h"
     22#include "pmSubtractionTypes.h"
    2223#include "pmSubtractionStamps.h"
    2324#include "pmSubtractionEquation.h"
    2425#include "pmSubtractionVisual.h"
    2526#include "pmSubtractionThreads.h"
     27
     28bool psFitsWriteImageSimple (char *filename, psImage *image, psMetadata *header);
    2629
    2730#include "pmSubtraction.h"
     
    773776
    774777    if (convolutions) {
    775         // Already done
    776778        return convolutions;
    777779    }
     
    787789}
    788790
     791
     792bool pmSubtractionConvolveStampThread(psThreadJob *job)
     793{
     794    PS_ASSERT_THREAD_JOB_NON_NULL(job, false);
     795
     796    pmSubtractionStamp *stamp = job->args->data[0]; // List of stamps
     797    pmSubtractionKernels *kernels = job->args->data[1]; // Kernels
     798    int footprint = PS_SCALAR_VALUE(job->args->data[2], S32); // Stamp index
     799
     800    return pmSubtractionConvolveStamp(stamp, kernels, footprint);
     801}
    789802
    790803bool pmSubtractionConvolveStamp (pmSubtractionStamp *stamp, pmSubtractionKernels *kernels, int footprint)
     
    818831    }
    819832
     833#ifdef TESTING
     834    for (int j = 0; j < kernels->num; j++) {
     835        if (stamp->convolutions1) {
     836            psString convName = NULL;
     837            psStringAppend(&convName, "conv1_%03d_%03d.fits", index, j);
     838            psFits *fits = psFitsOpen(convName, "w");
     839            psFree(convName);
     840            psKernel *conv = stamp->convolutions1->data[j];
     841            psFitsWriteImage(fits, NULL, conv->image, 0, NULL);
     842            psFitsClose(fits);
     843        }
     844
     845        if (stamp->convolutions2) {
     846            psString convName = NULL;
     847            psStringAppend(&convName, "conv2_%03d_%03d.fits", index, j);
     848            psFits *fits = psFitsOpen(convName, "w");
     849            psFree(convName);
     850            psKernel *conv = stamp->convolutions2->data[j];
     851            psFitsWriteImage(fits, NULL, conv->image, 0, NULL);
     852            psFitsClose(fits);
     853        }
     854    }
     855#endif
     856
    820857    return true;
    821858}
    822859
     860bool pmSubtractionConvolveStamps(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels)
     861{
     862    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     863    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, false);
     864
     865    psTimerStart("pmSubtractionConvolveStamps");
     866
     867    int footprint = stamps->footprint;  // Half-size of stamps
     868
     869    // We iterate over each stamp and generate the convolution if needed.  We do NOT need the
     870    // convolution if (a) it has already been calculated or (b) the stamp is not available for
     871    // use (available = USED or CALCULATE)
     872   
     873    for (int i = 0; i < stamps->num; i++) {
     874        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
     875
     876        bool keep = false;
     877        keep |= (stamp->status == PM_SUBTRACTION_STAMP_USED);
     878        keep |= (stamp->status == PM_SUBTRACTION_STAMP_CALCULATE);
     879        if (!keep) continue;
     880
     881        bool haveConvolutions = false;
     882        if (kernels->mode == PM_SUBTRACTION_MODE_1) {
     883            haveConvolutions = (stamp->convolutions1 != NULL);
     884        }
     885        if (kernels->mode == PM_SUBTRACTION_MODE_2) {
     886            haveConvolutions = (stamp->convolutions2 != NULL);
     887        }
     888        if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) {
     889            haveConvolutions = (stamp->convolutions1 != NULL) && (stamp->convolutions2 != NULL);
     890        }
     891        if (haveConvolutions) {
     892            continue;
     893        }
     894
     895        if (pmSubtractionThreaded()) {
     896            psThreadJob *job = psThreadJobAlloc("PSMODULES_SUBTRACTION_CONVOLVE_STAMP");
     897            psArrayAdd(job->args, 1, stamp);
     898            psArrayAdd(job->args, 1, kernels);
     899            PS_ARRAY_ADD_SCALAR(job->args, footprint, PS_TYPE_S32);
     900            if (!psThreadJobAddPending(job)) {
     901                return false;
     902            }
     903        } else {
     904            pmSubtractionConvolveStamp(stamp, kernels, footprint);
     905        }
     906    }
     907    if (!psThreadPoolWait(true)) {
     908        psError(psErrorCodeLast(), false, "Error waiting for threads.");
     909        return false;
     910    }
     911    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Convolve stamps: %f sec", psTimerClear("pmSubtractionConvolveStamps"));
     912    return true;
     913}
    823914
    824915int pmSubtractionRejectStamps(pmSubtractionKernels *kernels, pmSubtractionStampList *stamps,
    825                               const psVector *deviations, psImage *subMask, float sigmaRej)
     916                              pmSubtractionQuality *match, psImage *subMask, float sigmaRej)
    826917{
    827918    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, false);
    828919    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, -1);
    829     PS_ASSERT_VECTOR_NON_NULL(deviations, -1);
    830     PS_ASSERT_VECTOR_TYPE(deviations, PS_TYPE_F32, -1);
    831920    PS_ASSERT_IMAGE_NON_EMPTY(subMask, -1);
    832921    PS_ASSERT_IMAGE_TYPE(subMask, PS_TYPE_IMAGE_MASK, -1);
    833922
    834     // I used to measure the rms deviation about zero, and use that as the sigma against which to clip, but
    835     // the distribution is actually something like a chi^2 or Student's t, both of which become Gaussian-like
    836     // with large N.  Therefore, let's just treat this as a Gaussian distribution.
     923    // Comment from PAP (r18287): I used to measure the rms deviation about zero, and use that as the
     924    // sigma against which to clip, but the distribution is actually something like a chi^2 or
     925    // Student's t, both of which become Gaussian-like with large N.  Therefore, let's just
     926    // treat this as a Gaussian distribution.
     927
     928    // Comment from EAM (r29777): The residual distribution is only chisq-like if the model is
     929    // a good fit to the data.  In the (likely) case that there is a systematic difference
     930    // between the model and the data, the squared-residual distribution grows quadratically
     931    // with increasing flux: the systematic residual flux is a constant factor times the source
     932    // flux; the squared-residual is then of the form (k0 + k1*flux)^2, where k0 comes from the
     933    // Gaussian distributed residual and k1*flux is the systematic residual error.
     934
     935    // By rejecting sources with the largest squared-residuals, the rejection biases against
     936    // the brighter sources; in severe cases, this pushes the measurement to the weakest
     937    // sources with the most noise.  To account for this, let's fit a 2nd order polynomial to
     938    // the distribution of flux vs squared-residual, subtract that fit, and reject sources
     939    // which are significantly deviant from that distribution.
    837940
    838941    kernels->mean = NAN;
     
    840943    kernels->numStamps = -1;
    841944
    842     int numStamps = 0;                  // Number of used stamps
    843     psVector *mask = psVectorAlloc(stamps->num, PS_TYPE_VECTOR_MASK); // Mask, for statistics
    844     psVectorInit(mask, 0);
    845     for (int i = 0; i < stamps->num; i++) {
    846         pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
    847         if (stamp->status != PM_SUBTRACTION_STAMP_USED) {
    848             mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = 0xff;
    849             continue;
    850         }
    851         numStamps++;
    852     }
    853     psTrace("psModules.imcombine", 1, "Number of good stamps: %d\n", numStamps);
    854 
    855     if (numStamps == 0) {
    856         psError(PM_ERR_STAMPS, true, "No good stamps found.");
    857         psFree(mask);
    858         return -1;
    859     }
    860 
    861     psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV |
    862                                   PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_QUARTILE); // Statistics for deviatns
    863     if (!psVectorStats(stats, deviations, NULL, mask, 0xff)) {
    864         psError(PM_ERR_DATA, false, "Unable to measure statistics for deviations.");
     945    psTrace("psModules.imcombine", 1, "Number of good stamps: %d\n", match->nGood);
     946
     947    // the chisq & flux vectors are calculated by pmSubtractionCalculateChisqAndMoments
     948
     949    // use 3hi/3lo sigma clipping on the chisq fit
     950    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV);
     951    stats->clipSigma = 5.0;
     952    stats->clipIter = 2;
     953    psPolynomial1D *model = psPolynomial1DAlloc (PS_POLYNOMIAL_ORD, 2);
     954
     955    bool result = psVectorClipFitPolynomial1D(model, stats, match->stampMask, 0xff, match->chisq, NULL, match->fluxes);
     956    if (!result) {
     957        psError(PM_ERR_DATA, false, "Unable to measure statistics for deviations.");
     958        psFree(model);
    865959        psFree(stats);
    866         psFree(mask);
    867         return -1;
    868     }
    869     psFree(mask);
    870 
    871     // XXX raise an error?
     960        return -1;
     961    }
    872962    if (isnan(stats->sampleMean)) {
     963        psError(PM_ERR_DATA, false, "Unable to measure statistics for deviations.");
     964        psFree(model);
    873965        psFree(stats);
    874966        return -1;
    875967    }
    876968
    877     double mean, rms;                 // Mean and RMS of deviations
    878     if (numStamps < MIN_SAMPLE_STATS) {
    879         mean = stats->sampleMean;
    880         rms = stats->sampleStdev;
    881     } else {
    882         mean = stats->sampleMedian;
    883         rms = 0.74 * (stats->sampleUQ - stats->sampleLQ);
    884     }
    885     psFree(stats);
    886 
    887     psTrace("psModules.imcombine", 1, "Mean: %f\n", mean);
    888     psTrace("psModules.imcombine", 1, "RMS deviation: %f\n", rms);
    889 
    890     kernels->mean = mean;
    891     kernels->rms = rms;
    892     kernels->numStamps = numStamps;
    893 
    894     psLogMsg("psModules.imcombine", PS_LOG_INFO, "Mean deviation from %d stamps: %lf +/- %lf",
    895              numStamps, mean, rms);
    896 
    897     if (!isfinite(sigmaRej) || sigmaRej <= 0.0) {
    898         // User just wanted to calculate and record the deviation for posterity
    899         return 0;
    900     }
    901 
    902     float limit = sigmaRej * rms; // Limit on maximum deviation
    903     psTrace("psModules.imcombine", 1, "Deviation limit: %f\n", limit);
    904 
     969    kernels->mean = stats->sampleMean;
     970    kernels->rms = stats->sampleStdev;
     971    kernels->numStamps = stats->clippedNvalues;
     972
     973    psLogMsg ("pmPSFtry", 4, "chisq vs flux model: %e + %e flux + %e flux^2\n", model->coeff[0], model->coeff[1], model->coeff[2]);
     974    psLogMsg ("pmPSFtry", 4, "chisq vs flux resid: %f +/- %f\n", stats->sampleMean, stats->sampleStdev);
     975    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Mean deviation from %d stamps: %lf +/- %lf",  kernels->numStamps, kernels->mean, kernels->rms);
    905976
    906977    psString ds9name = NULL;            // Filename for ds9 region file
     
    914985    int numRejected = 0;                // Number of stamps rejected
    915986    int numGood = 0;                    // Number of good stamps
    916     double newMean = 0.0;               // New mean
    917987    psString log = NULL;                // Log message
    918     psStringAppend(&log, "Rejecting stamps, mean = %f, threshold = %f\n", mean, limit);
     988
     989    // save DS9 region files for the stamps and mark for rejection and replacement
    919990    for (int i = 0; i < stamps->num; i++) {
    920991        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
    921         if (stamp->status == PM_SUBTRACTION_STAMP_USED) {
     992        if (stamp->status  != PM_SUBTRACTION_STAMP_USED) { continue; }
     993        if (match->stampMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
    922994            // Should we reject stars with low deviation?  Well, if this is really a Gaussian-like
    923995            // distribution and they're low, then we have the right to ask why.  Isn't it suspicious that
     
    926998            // subtract well, in which case very few (if any) stars will be legitimately rejected for being
    927999            // low.
    928             if (fabsf(deviations->data.F32[i] - mean) > limit) {
    929                 // Mask out the stamp in the image so you it's not found again
    930                 psTrace("psModules.imcombine", 3, "Rejecting stamp %d (%d,%d)\n", i,
    931                         (int)(stamp->x - 0.5), (int)(stamp->y - 0.5));
    932                 psStringAppend(&log, "Stamp %d (%d,%d): %f\n", i,
    933                                (int)(stamp->x - 0.5), (int)(stamp->y - 0.5),
    934                                fabsf(deviations->data.F32[i] - mean));
    935                 numRejected++;
    936                 for (int y = stamp->y - footprint; y <= stamp->y + footprint; y++) {
    937                     for (int x = stamp->x - footprint; x <= stamp->x + footprint; x++) {
    938                         subMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= PM_SUBTRACTION_MASK_REJ;
    939                     }
    940                 }
    941                 pmSubtractionStampPrint(ds9, stamp->x, stamp->y, stamps->footprint, "red");
    942 
    943                 // Set stamp for replacement
    944                 stamp->x = 0;
    945                 stamp->y = 0;
    946                 stamp->xNorm = NAN;
    947                 stamp->yNorm = NAN;
    948                 stamp->status = PM_SUBTRACTION_STAMP_REJECTED;
    949                 // Recalculate convolutions
    950                 psFree(stamp->convolutions1);
    951                 psFree(stamp->convolutions2);
    952                 stamp->convolutions1 = stamp->convolutions2 = NULL;
    953                 psFree(stamp->image1);
    954                 psFree(stamp->image2);
    955                 psFree(stamp->weight);
    956                 stamp->image1 = stamp->image2 = stamp->weight = NULL;
    957                 psFree(stamp->matrix);
    958                 stamp->matrix = NULL;
    959                 psFree(stamp->vector);
    960                 stamp->vector = NULL;
    961             } else {
    962                 numGood++;
    963                 newMean += deviations->data.F32[i];
    964                 pmSubtractionStampPrint(ds9, stamp->x, stamp->y, stamps->footprint, "green");
    965             }
    966         }
    967     }
    968     newMean /= numGood;
     1000            psTrace("psModules.imcombine", 3, "Rejecting stamp %d (%d,%d)\n", i,
     1001                    (int)(stamp->x - 0.5), (int)(stamp->y - 0.5));
     1002            psStringAppend(&log, "Stamp %d (%d,%d): %f : %f : %f\n",
     1003                           i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5),
     1004                           match->chisq->data.F32[i], match->fluxes->data.F32[i], match->chisq->data.F32[i] - psPolynomial1DEval(model, match->fluxes->data.F32[i]));
     1005            numRejected++;
     1006            for (int y = stamp->y - footprint; y <= stamp->y + footprint; y++) {
     1007                for (int x = stamp->x - footprint; x <= stamp->x + footprint; x++) {
     1008                    subMask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= PM_SUBTRACTION_MASK_REJ;
     1009                }
     1010            }
     1011            pmSubtractionStampPrint(ds9, stamp->x, stamp->y, stamps->footprint, "red");
     1012
     1013            // Set stamp for replacement
     1014            stamp->x = 0;
     1015            stamp->y = 0;
     1016            stamp->xNorm = NAN;
     1017            stamp->yNorm = NAN;
     1018            stamp->status = PM_SUBTRACTION_STAMP_REJECTED;
     1019            // Recalculate convolutions
     1020            psFree(stamp->convolutions1);
     1021            psFree(stamp->convolutions2);
     1022            stamp->convolutions1 = stamp->convolutions2 = NULL;
     1023            psFree(stamp->image1);
     1024            psFree(stamp->image2);
     1025            psFree(stamp->weight);
     1026            stamp->image1 = stamp->image2 = stamp->weight = NULL;
     1027            psFree(stamp->matrix);
     1028            stamp->matrix = NULL;
     1029            psFree(stamp->vector);
     1030            stamp->vector = NULL;
     1031        } else {
     1032            numGood++;
     1033            pmSubtractionStampPrint(ds9, stamp->x, stamp->y, stamps->footprint, "green");
     1034        }
     1035    }
    9691036
    9701037    if (numRejected == 0) {
     
    9781045    }
    9791046
     1047    psFree(model);
     1048    psFree(stats);
     1049
    9801050    if (numRejected > 0) {
    981         psLogMsg("psModules.imcombine", PS_LOG_INFO,
    982                  "%d good stamps; %d rejected.\nMean deviation: %lf --> %lf\n",
    983                  numGood, numRejected, mean, newMean);
     1051        psLogMsg("psModules.imcombine", PS_LOG_INFO, "%d good stamps; %d rejected.\n", numGood, numRejected);
    9841052    } else {
    985         psLogMsg("psModules.imcombine", PS_LOG_INFO,
    986                  "%d good stamps; 0 rejected.\nMean deviation: %lf\n",
    987                  numGood, mean);
     1053        psLogMsg("psModules.imcombine", PS_LOG_INFO, "%d good stamps; 0 rejected.\n", numGood);
    9881054    }
    9891055
     
    13731439    psFree(kernelErr2);
    13741440
     1441    static int nOut1 = 0;
     1442    static int nOut2 = 0;
     1443
    13751444    // Calculate covariances
    13761445    // This can be fairly involved, so we only do it for a small number of instances
     
    13861455                psKernelTruncate(kernel, covarFrac);
    13871456                covars->data[i] = psImageCovarianceCalculate(kernel, ro1->covariance);
     1457                if (0) {
     1458                    char name[128];
     1459                    snprintf (name, 128, "covar.sample1.%03d.fits", nOut1);
     1460                    psKernel *cov = covars->data[i];
     1461                    psFitsWriteImageSimple (name, cov->image, NULL);
     1462
     1463                    snprintf (name, 128, "incovar.sample1.%03d.fits", nOut1);
     1464                    psFitsWriteImageSimple (name, ro1->covariance->image, NULL);
     1465
     1466                    snprintf (name, 128, "conv.sample1.%03d.fits", nOut1);
     1467                    psFitsWriteImageSimple (name, kernel->image, NULL);
     1468
     1469                    fprintf (stderr, "incov: %d,%d; kern: %d,%d, outcov: %d,%d\n",
     1470                             ro1->covariance->image->numCols, ro1->covariance->image->numRows,
     1471                             kernel->image->numCols, kernel->image->numRows,
     1472                             cov->image->numCols, cov->image->numRows);
     1473
     1474                    nOut1 ++;
     1475                }
    13881476                psFree(kernel);
    13891477            }
     
    14071495                psKernelTruncate(kernel, covarFrac);
    14081496                covars->data[i] = psImageCovarianceCalculate(kernel, ro2->covariance);
     1497                if (0) {
     1498                    char name[128];
     1499                    snprintf (name, 128, "covar.sample2.%03d.fits", nOut2);
     1500                    psKernel *cov = covars->data[i];
     1501                    psFitsWriteImageSimple (name, cov->image, NULL);
     1502
     1503                    snprintf (name, 128, "incovar.sample2.%03d.fits", nOut2);
     1504                    psFitsWriteImageSimple (name, ro2->covariance->image, NULL);
     1505
     1506                    snprintf (name, 128, "conv.sample2.%03d.fits", nOut2);
     1507                    psFitsWriteImageSimple (name, kernel->image, NULL);
     1508
     1509                    fprintf (stderr, "incov: %d,%d; kern: %d,%d, outcov: %d,%d\n",
     1510                             ro2->covariance->image->numCols, ro2->covariance->image->numRows,
     1511                             kernel->image->numCols, kernel->image->numRows,
     1512                             cov->image->numCols, cov->image->numRows);
     1513
     1514                    nOut2 ++;
     1515                }
    14091516                psFree(kernel);
    14101517            }
     
    14221529    psImageCovarianceSetThreads(oldThreads);
    14231530
    1424     // Copy anything that wasn't convolved
     1531    // Copy anything that wasn't convolved (they may have been allocated though, so free them)
    14251532    switch (kernels->mode) {
    14261533      case PM_SUBTRACTION_MODE_1:
    14271534        if (out2) {
     1535            psFree(out2->image);
     1536            psFree(out2->variance);
     1537            psFree(out2->mask);
     1538            psFree(out2->covariance);
    14281539            out2->image = psMemIncrRefCounter(ro2->image);
    14291540            out2->variance = psMemIncrRefCounter(ro2->variance);
     
    14341545      case PM_SUBTRACTION_MODE_2:
    14351546        if (out1) {
     1547            psFree(out1->image);
     1548            psFree(out1->variance);
     1549            psFree(out1->mask);
     1550            psFree(out1->covariance);
    14361551            out1->image = psMemIncrRefCounter(ro1->image);
    14371552            out1->variance = psMemIncrRefCounter(ro1->variance);
     
    14791594  return true;
    14801595}
     1596
     1597static void pmSubtractionQualityFree(pmSubtractionQuality *quality) {
     1598
     1599    psFree (quality->fluxes);
     1600    psFree (quality->chisq);
     1601    psFree (quality->moments);
     1602    psFree (quality->stampMask);
     1603}   
     1604
     1605pmSubtractionQuality *pmSubtractionQualityAlloc() {
     1606
     1607    pmSubtractionQuality *quality = psAlloc(sizeof(pmSubtractionQuality)); // Stamp list to return
     1608    psMemSetDeallocator(quality, (psFreeFunc)pmSubtractionQualityFree);
     1609
     1610    quality->fluxes = NULL;
     1611    quality->chisq = NULL;
     1612    quality->moments = NULL;
     1613    quality->stampMask = NULL;
     1614
     1615    quality->score = NAN;
     1616    quality->mode = PM_SUBTRACTION_MODE_ERR;
     1617    quality->spatialOrder = -1;
     1618    quality->nGood = 0;
     1619   
     1620    return quality;
     1621}
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtraction.h

    r29601 r30631  
    1414#define PM_SUBTRACTION_H
    1515
    16 #include <pslib.h>
    17 
    18 #include <pmHDU.h>
    19 #include <pmFPA.h>
    20 #include <pmSubtractionKernels.h>
    21 #include <pmSubtractionStamps.h>
     16// #include <pslib.h>
     17// #include <pmHDU.h>
     18// #include <pmFPA.h>
     19// #include <pmSubtractionKernels.h>
     20// #include <pmSubtractionStamps.h>
    2221
    2322// if we use the original ppSub implementation, we subtract a central delta-function for all
     
    3029/// @addtogroup imcombine Image Combinations
    3130/// @{
    32 
    33 /// Mask values for the subtraction mask
    34 typedef enum {
    35     PM_SUBTRACTION_MASK_CLEAR          = 0x00, // No masking
    36     PM_SUBTRACTION_MASK_BAD_1          = 0x01, // Image 1 is bad
    37     PM_SUBTRACTION_MASK_BAD_2          = 0x02, // Image 2 is bad
    38     PM_SUBTRACTION_MASK_CONVOLVE_1     = 0x04, // If image 1 is convolved, would be poor or bad
    39     PM_SUBTRACTION_MASK_CONVOLVE_2     = 0x08, // If image 2 is convolved, would be poor or bad
    40     PM_SUBTRACTION_MASK_CONVOLVE_BAD_1 = 0x10, // If image 1 is convolved, would be bad
    41     PM_SUBTRACTION_MASK_CONVOLVE_BAD_2 = 0x20, // If image 2 is convolved, would be bad
    42     PM_SUBTRACTION_MASK_BORDER         = 0x40, // Image border
    43     PM_SUBTRACTION_MASK_REJ            = 0x80, // Previously tried as a stamp, and rejected
    44 } pmSubtractionMasks;
    45 
    4631
    4732/// Number of terms in a polynomial
     
    7055    );
    7156
     57bool pmSubtractionConvolveStamps(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels);
     58
     59bool pmSubtractionConvolveStampThread(psThreadJob *job);
     60
    7261/// Reject stamps
    7362int pmSubtractionRejectStamps(pmSubtractionKernels *kernels, ///< Kernel parameters to update
    7463                              pmSubtractionStampList *stamps, ///< Stamps
    75                               const psVector *deviations, ///< Deviations for each stamp
     64                              pmSubtractionQuality *match, ///< data on the subtraction quality
    7665                              psImage *subMask, ///< Subtraction mask
    7766                              float sigmaRej ///< Number of RMS deviations above zero at which to reject
     
    167156bool pmSubtractionSetFWHMs(float fwhm1, float fwhm2);
    168157
     158pmSubtractionQuality *pmSubtractionQualityAlloc();
     159
    169160/// @}
    170161#endif
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionAnalysis.c

    r29595 r30631  
    77
    88#include "pmFPA.h"
     9#include "pmSubtractionTypes.h"
    910#include "pmSubtraction.h"
    1011#include "pmSubtractionKernels.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionDeconvolve.c

    r26893 r30631  
    1010
    1111#include "pmFPA.h"
     12#include "pmSubtractionTypes.h"
    1213#include "pmSubtractionKernels.h"
    1314#include "pmSubtractionDeconvolve.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionEquation.c

    r29598 r30631  
    88
    99#include "pmErrorCodes.h"
     10#include "pmVisual.h"
     11#include "pmFPA.h"
     12#include "pmSubtractionTypes.h"
    1013#include "pmSubtraction.h"
    1114#include "pmSubtractionKernels.h"
     
    1619#include "pmSubtractionVisual.h"
    1720
    18 //#define TESTING                         // TESTING output for debugging; may not work with threads!
    19 
    20 //#define USE_WEIGHT                      // Include weight (1/variance) in equation?
    21 
    22 // XXX TEST:
    23 //# define USE_WINDOW                      // window to avoid neighbor contamination
     21//# define TESTING                         // TESTING output for debugging; may not work with threads!
     22# define USE_WEIGHT                      // Include weight (1/variance) in equation?
     23# define USE_WINDOW                      // window to avoid neighbor contamination
     24
     25/* I believe we want to apply the WEIGHT to the chisq portions of the calculation (but not the WINDOW),
     26 * and the WINDOW to the moments portiosn of the calculations (but not the WEIGHT)
     27 *
     28 */
    2429
    2530# define PENALTY false
    2631# define MOMENTS (!PENALTY)
    27 # define MOMENTS_PENALTY_SCALE 2e-5 // XXX this value is not completely arbitrary, but I don't understand why it needs to be this value...
     32# define MOMENTS_PENALTY_SCALE 20 // up-weight the moments somewhat
    2833
    2934//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    107112                        cc *= weight->kernel[y][x];
    108113                    }
    109                     if (window) {
     114                    // XXX NOTE: do NOT apply the window to the chisq portions of the calculation
     115                    if (false && window) {
    110116                        cc *= window->kernel[y][x];
    111117                    }
     
    138144                    rc *= wtVal;
    139145                }
    140                 if (window) {
     146                // XXX NOTE: do NOT apply the window to the chisq portions of the calculation
     147                if (false && window) {
    141148                    float winVal = window->kernel[y][x];
    142149                    ic *= winVal;
     
    173180}
    174181
     182# define RENORM_BY_FLUX 0
    175183
    176184// Calculate the least-squares matrix and vector for dual convolution
     
    268276            for (int y = - footprint; y <= footprint; y++) {
    269277                for (int x = - footprint; x <= footprint; x++) {
     278
     279                    // XXX NOTE: clipping low S/N pixels does not seem to work very well
     280                    if (false && weight) {
     281                        float i1 = image1->kernel[y][x];
     282                        float i2 = image2->kernel[y][x];
     283                        float sn = (i1 + i2) / sqrt (weight->kernel[y][x]);
     284                        if (sn < 0.5) continue;
     285                    }
     286
    270287                    double aa = iConv1->kernel[y][x] * jConv1->kernel[y][x] * PS_SQR(normValue);
    271288                    double bb = iConv2->kernel[y][x] * jConv2->kernel[y][x];
    272289                    double ab = iConv1->kernel[y][x] * jConv2->kernel[y][x] * normValue;
    273                     if (weight) {
    274                         float wtVal = weight->kernel[y][x];
    275                         aa *= wtVal;
    276                         bb *= wtVal;
    277                         ab *= wtVal;
    278                     }
    279                     if (window) {
    280                         float wtVal = window->kernel[y][x];
    281                         aa *= wtVal;
    282                         bb *= wtVal;
    283                         ab *= wtVal;
    284                     }
    285                     sumAA += aa;
    286                     sumBB += bb;
    287                     sumAB += ab;
     290
     291                    float wtVal = (weight) ? weight->kernel[y][x] : 1.0;
     292                    sumAA += wtVal*aa;
     293                    sumBB += wtVal*bb;
     294                    sumAB += wtVal*ab;
    288295
    289296                    if (MOMENTS) {
    290                         MxxAA += x*x*aa;
    291                         MyyAA += y*y*aa;
    292                         MxxBB += x*x*bb;
    293                         MyyBB += y*y*bb;
     297                        float winVal = (window) ? window->kernel[y][x] : 1.0;
     298                        MxxAA += winVal*x*x*aa;
     299                        MyyAA += winVal*y*y*aa;
     300                        MxxBB += winVal*x*x*bb;
     301                        MyyBB += winVal*y*y*bb;
    294302                    }
    295303                }
    296304            }
    297305
     306            // XXX does normSquare1,2 mess up the relative scaling?
     307            // XXX no: normSquare1,2 is the sum of the flux^2 for the source
    298308            if (MOMENTS) {
    299309                MxxAA /= stamp->normSquare1 * PS_SQR(normValue);
     
    305315            // XXX this makes the Chisq portion independent of the normalization and star flux
    306316            // but may be mis-scaling between stars of different fluxes
     317# if (RENORM_BY_FLUX)       
    307318            sumAA /= PS_SQR(stamp->normI2);
    308319            sumAB /= PS_SQR(stamp->normI2);
    309320            sumBB /= PS_SQR(stamp->normI2);
     321# endif
    310322
    311323            // fprintf (stderr, "i,j : %d %d : M(xx,yy)(AA,BB) : %f %f %f %f\n", i, j, MxxAA, MyyAA, MxxBB, MyyBB);
     
    328340                        matrix->data.F64[iIndex][jIndex] += kernels->penalty * MxxAA * MOMENTS_PENALTY_SCALE;
    329341                        matrix->data.F64[iIndex][jIndex] += kernels->penalty * MyyAA * MOMENTS_PENALTY_SCALE;
    330 
    331342                        matrix->data.F64[jIndex][iIndex] += kernels->penalty * MxxAA * MOMENTS_PENALTY_SCALE;
    332343                        matrix->data.F64[jIndex][iIndex] += kernels->penalty * MyyAA * MOMENTS_PENALTY_SCALE;
     
    334345                        matrix->data.F64[iIndex + numParams][jIndex + numParams] += kernels->penalty * MxxBB * MOMENTS_PENALTY_SCALE;
    335346                        matrix->data.F64[iIndex + numParams][jIndex + numParams] += kernels->penalty * MyyBB * MOMENTS_PENALTY_SCALE;
    336 
    337347                        matrix->data.F64[jIndex + numParams][iIndex + numParams] += kernels->penalty * MxxBB * MOMENTS_PENALTY_SCALE;
    338348                        matrix->data.F64[jIndex + numParams][iIndex + numParams] += kernels->penalty * MyyBB * MOMENTS_PENALTY_SCALE;
     
    354364                        ab *= weight->kernel[y][x];
    355365                    }
    356                     if (window) {
     366                    // XXX NOTE: do NOT apply the window to the chisq portions of the calculation
     367                    if (false && window) {
    357368                        ab *= window->kernel[y][x];
    358369                    }
     
    363374            // XXX this makes the Chisq portion independent of the normalization and star flux
    364375            // but may be mis-scaling between stars of different fluxes
     376# if (RENORM_BY_FLUX)
    365377            sumAB /= PS_SQR(stamp->normI2);
     378# endif
    366379
    367380            // Spatial variation of kernel coeffs
     
    391404                float i2 = image2->kernel[y][x];
    392405
     406                // XXX NOTE: clipping low S/N pixels does not seem to work very well
     407                if (false && weight) {
     408                    float sn = (i1 + i2) / sqrt (weight->kernel[y][x]);
     409                    if (sn < 0.5) continue;
     410                }
     411
    393412                double ai2 = a * i2 * normValue;
    394413                double bi2 = b * i2;
     
    396415                double bi1 = b * i1 * normValue;
    397416
    398                 if (weight) {
    399                     float wtVal = weight->kernel[y][x];
    400                     ai2 *= wtVal;
    401                     bi2 *= wtVal;
    402                     ai1 *= wtVal;
    403                     bi1 *= wtVal;
    404                 }
    405                 if (window) {
    406                     float wtVal = window->kernel[y][x];
    407                     ai2 *= wtVal;
    408                     bi2 *= wtVal;
    409                     ai1 *= wtVal;
    410                     bi1 *= wtVal;
    411                 }
    412                 sumAI2 += ai2;
    413                 sumBI2 += bi2;
    414                 sumAI1 += ai1;
    415                 sumBI1 += bi1;
     417                float wtVal = (weight) ? weight->kernel[y][x] : 1.0;
     418                sumAI2 += wtVal*ai2;
     419                sumBI2 += wtVal*bi2;
     420                sumAI1 += wtVal*ai1;
     421                sumBI1 += wtVal*bi1;
    416422
    417423                if (MOMENTS) {
    418                     MxxAI1 += x*x*ai1;
    419                     MyyAI1 += y*y*ai1;
    420                     MxxBI2 += x*x*bi2;
    421                     MyyBI2 += y*y*bi2;
     424                    float winVal = (window) ? window->kernel[y][x] : 1.0;
     425                    MxxAI1 += winVal*x*x*ai1;
     426                    MyyAI1 += winVal*y*y*ai1;
     427                    MxxBI2 += winVal*x*x*bi2;
     428                    MyyBI2 += winVal*y*y*bi2;
    422429                }
    423430            }
     
    435442        // XXX this makes the Chisq portion independent of the normalization and star flux
    436443        // but may be mis-scaling between stars of different fluxes
     444# if (RENORM_BY_FLUX)
    437445        sumAI1 /= PS_SQR(stamp->normI2);
    438446        sumBI1 /= PS_SQR(stamp->normI2);
    439447        sumAI2 /= PS_SQR(stamp->normI2);
    440448        sumBI2 /= PS_SQR(stamp->normI2);
     449# endif
    441450
    442451        // Spatial variation
     
    788797    stamp->normSquare2 = normSquare2;
    789798
    790     psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "normValue: %f %f %f  (%f %f)\n", normI1, normI2, stamp->norm, normSquare1, normSquare2);
     799    // psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "normValue: %f %f %f  (%f %f)\n", normI1, normI2, stamp->norm, normSquare1, normSquare2);
    791800
    792801    return true;
     
    800809    pmSubtractionKernels *kernels = job->args->data[1]; // Kernels
    801810    int index = PS_SCALAR_VALUE(job->args->data[2], S32); // Stamp index
    802     pmSubtractionEquationCalculationMode mode  = PS_SCALAR_VALUE(job->args->data[3], S32); // calculation model
    803 
    804     return pmSubtractionCalculateEquationStamp(stamps, kernels, index, mode);
    805 }
    806 
    807 bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels,
    808                                          int index, const pmSubtractionEquationCalculationMode mode)
     811
     812    return pmSubtractionCalculateEquationStamp(stamps, kernels, index);
     813}
     814
     815bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels, int index)
    809816{
    810817    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     
    833840    psAssert(stamp->status == PM_SUBTRACTION_STAMP_CALCULATE, "We only operate on stamps with this state.");
    834841
    835     // Generate convolutions: these are generated once and saved
    836     if (!pmSubtractionConvolveStamp(stamp, kernels, footprint)) {
    837         psError(psErrorCodeLast(), false, "Unable to convolve stamp %d.", index);
    838         return NULL;
    839     }
    840 
    841 #ifdef TESTING
    842     for (int j = 0; j < numKernels; j++) {
    843         if (stamp->convolutions1) {
    844             psString convName = NULL;
    845             psStringAppend(&convName, "conv1_%03d_%03d.fits", index, j);
    846             psFits *fits = psFitsOpen(convName, "w");
    847             psFree(convName);
    848             psKernel *conv = stamp->convolutions1->data[j];
    849             psFitsWriteImage(fits, NULL, conv->image, 0, NULL);
    850             psFitsClose(fits);
    851         }
    852 
    853         if (stamp->convolutions2) {
    854             psString convName = NULL;
    855             psStringAppend(&convName, "conv2_%03d_%03d.fits", index, j);
    856             psFits *fits = psFitsOpen(convName, "w");
    857             psFree(convName);
    858             psKernel *conv = stamp->convolutions2->data[j];
    859             psFitsWriteImage(fits, NULL, conv->image, 0, NULL);
    860             psFitsClose(fits);
    861         }
    862     }
    863 #endif
    864 
    865     // XXX visualize the set of convolved stamps
    866 
    867     psImage *polyValues = p_pmSubtractionPolynomial(NULL, spatialOrder,
    868                                                     stamp->xNorm, stamp->yNorm); // Polynomial terms
    869 
    870     bool new = stamp->vector ? false : true; // Is this a new run?
     842    psImage *polyValues = p_pmSubtractionPolynomial(NULL, spatialOrder, stamp->xNorm, stamp->yNorm); // Polynomial terms
     843
     844    // Is this a new run? Have we allocated the correct sized vector/matrix?
     845    bool new = stamp->vector ? false : true;
     846    if (!new && (stamp->vector->n != numParams)) {
     847        psFree (stamp->vector);
     848        psFree (stamp->matrix);
     849        new = true;
     850    }
     851
    871852    if (new) {
    872853        stamp->matrix = psImageAlloc(numParams, numParams, PS_TYPE_F64);
     
    941922}
    942923
    943 bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels,
    944                                     const pmSubtractionEquationCalculationMode mode)
     924bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels)
    945925{
    946926    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     
    953933    for (int i = 0; i < stamps->num; i++) {
    954934        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
     935
    955936        if (stamp->status != PM_SUBTRACTION_STAMP_CALCULATE) {
    956937            continue;
     
    969950            psArrayAdd(job->args, 1, (pmSubtractionKernels*)kernels); // Casting away const to put on array
    970951            PS_ARRAY_ADD_SCALAR(job->args, i, PS_TYPE_S32);
    971             PS_ARRAY_ADD_SCALAR(job->args, mode, PS_TYPE_S32);
    972952            if (!psThreadJobAddPending(job)) {
    973953                return false;
    974954            }
    975955        } else {
    976             pmSubtractionCalculateEquationStamp(stamps, kernels, i, mode);
     956            pmSubtractionCalculateEquationStamp(stamps, kernels, i);
    977957        }
    978958    }
     
    983963    }
    984964
    985     pmSubtractionVisualPlotLeastSquares(stamps);
    986965    pmSubtractionVisualShowKernels((pmSubtractionKernels  *)kernels);
    987966    pmSubtractionVisualShowBasis(stamps);
    988 
    989     psLogMsg("psModules.imcombine", PS_LOG_INFO, "Calculate equation: %f sec",
    990              psTimerClear("pmSubtractionCalculateEquation"));
    991 
     967    pmSubtractionVisualPlotLeastSquares(stamps);
     968
     969    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Calculate equation: %f sec", psTimerClear("pmSubtractionCalculateEquation"));
    992970
    993971    return true;
     
    998976bool psFitsWriteImageSimple (char *filename, psImage *image, psMetadata *header);
    999977
    1000 psImage *p_pmSubSolve_wUt (psVector *w, psImage *U);
    1001 psImage *p_pmSubSolve_VwUt (psImage *V, psImage *wUt);
    1002 
    1003 bool p_pmSubSolve_SetWeights (psVector *wApply, psVector *w, psVector *wMask);
    1004 
    1005 bool p_pmSubSolve_UtB (psVector **UtB, psImage *U, psVector *B);
    1006 bool p_pmSubSolve_wUtB (psVector **wUtB, psVector *w, psVector *UtB);
    1007 bool p_pmSubSolve_VwUtB (psVector **VwUtB, psImage *V, psVector *wUtB);
    1008 
    1009 bool p_pmSubSolve_Ax (psVector **B, psImage *A, psVector *x);
    1010 bool p_pmSubSolve_VdV (double *value, psVector *x, psVector *y);
    1011 bool p_pmSubSolve_y2 (double *y2, pmSubtractionKernels *kernels, const pmSubtractionStampList *stamps);
    1012 
    1013 psImage *p_pmSubSolve_Xvar (psImage *V, psVector *w);
    1014 
    1015 double p_pmSubSolve_ChiSquare (pmSubtractionKernels *kernels, const pmSubtractionStampList *stamps);
    1016 
    1017 bool pmSubtractionSolveEquation(pmSubtractionKernels *kernels,
    1018                                 const pmSubtractionStampList *stamps,
    1019                                 const pmSubtractionEquationCalculationMode mode)
     978bool pmSubtractionSolveEquation(pmSubtractionKernels *kernels, const pmSubtractionStampList *stamps)
    1020979{
    1021980    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, false);
    1022981    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
     982
     983    psTimerStart("pmSubtractionSolveEquation");
    1023984
    1024985    // Check inputs
     
    10421003        }
    10431004
     1005        if (stamp->vector->n != numParams) {
     1006            fprintf (stderr, "mismatch length\n");
     1007        }
    10441008        PS_ASSERT_VECTOR_NON_NULL(stamp->vector, false);
    10451009        PS_ASSERT_VECTOR_SIZE(stamp->vector, (long)numParams, false);
     
    10801044        }
    10811045
     1046        pmSubtractionVisualPlotLeastSquaresResid(stamps, sumMatrix, numStamps);
     1047
    10821048#if 0
    10831049        psImage *save = psImageCopy(NULL, sumMatrix, PS_TYPE_F32);
     
    10871053#endif
    10881054
     1055        // XXX TEST : print the matrix & vector
     1056        if (0) {
     1057            for (int iy = 0; iy < sumMatrix->numRows; iy++) {
     1058                for (int ix = 0; ix < sumMatrix->numCols; ix++) {
     1059                    fprintf (stderr, "%e  ", sumMatrix->data.F64[iy][ix]);
     1060                }
     1061                fprintf (stderr, " : %e\n", sumVector->data.F64[iy]);
     1062            }
     1063        }
     1064
     1065        psImage *invMatrix = NULL;
    10891066        psVector *solution = NULL;                       // Solution to equation!
    10901067        solution = psVectorAlloc(numParams, PS_TYPE_F64);
     
    10941071        // solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN);
    10951072        // SINGLE solution
    1096         if (1) {
    1097             solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN);
    1098         } else {
    1099             psVector *PERM = NULL;
    1100             psImage *LU = psMatrixLUDecomposition(NULL, &PERM, sumMatrix);
    1101             solution = psMatrixLUSolution(solution, LU, sumVector, PERM);
    1102             psFree (LU);
    1103             psFree (PERM);
    1104         }
     1073# if (1)
     1074        solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, 1e-10);
     1075        invMatrix = psMatrixInvert(NULL, sumMatrix, NULL);
     1076# endif
     1077# if (0)
     1078        psMatrixLUSolve(sumMatrixLU, sumVector);
     1079        solution = psMemIncrRefCounter(sumVector);
     1080        invMatrix = psMemIncrRefCounter(sumMatrix);
     1081# endif
     1082# if (0)
     1083        psMatrixGJSolve(sumMatrix, sumVector);
     1084        invMatrix = psMemIncrRefCounter(sumMatrix);
     1085        solution = psMemIncrRefCounter(sumVector);
     1086# endif
    11051087
    11061088# if (0)
    11071089        for (int i = 0; i < solution->n; i++) {
    1108             psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "Single solution %d: %lf\n", i, solution->data.F64[i]);
     1090            psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "Single solution %d: %lf +/- %lf\n", i, solution->data.F64[i], sqrt(fabs(invMatrix->data.F64[i][i])));
    11091091        }
    11101092# endif
    11111093
    1112         if (!kernels->solution1) {
    1113             kernels->solution1 = psVectorAlloc(sumVector->n + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
    1114             psVectorInit(kernels->solution1, 0.0);
    1115         }
     1094        // ensure we have a solution vector of the right size
     1095        kernels->solution1    = psVectorRecycle(kernels->solution1,    sumVector->n + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1096        kernels->solution1err = psVectorRecycle(kernels->solution1err, sumVector->n + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1097        psVectorInit(kernels->solution1, 0.0);
     1098        psVectorInit(kernels->solution1err, 0.0);
    11161099
    11171100        int numKernels = kernels->num;
    11181101        int spatialOrder = kernels->spatialOrder;       // Order of spatial variation
    11191102        int numPoly = PM_SUBTRACTION_POLYTERMS(spatialOrder); // Number of polynomial terms
     1103
    11201104        for (int i = 0; i < numKernels * numPoly; i++) {
    11211105            kernels->solution1->data.F64[i] = solution->data.F64[i];
     1106            kernels->solution1err->data.F64[i] = sqrt(invMatrix->data.F64[i][i]);
    11221107        }
    11231108
     
    11311116        psFree(sumVector);
    11321117        psFree(sumMatrix);
     1118        psFree(invMatrix);
    11331119
    11341120    } else {
     
    11601146        }
    11611147
     1148        pmSubtractionVisualPlotLeastSquaresResid(stamps, sumMatrix, numStamps);
     1149
    11621150#if 0
    11631151        psImage *save = psImageCopy(NULL, sumMatrix, PS_TYPE_F32);
     
    11711159        }
    11721160
     1161        // XXX TEST : print the matrix & vector
     1162        if (0) {
     1163            for (int iy = 0; iy < sumMatrix->numRows; iy++) {
     1164                for (int ix = 0; ix < sumMatrix->numCols; ix++) {
     1165                    fprintf (stderr, "%e  ", sumMatrix->data.F64[iy][ix]);
     1166                }
     1167                fprintf (stderr, " : %e\n", sumVector->data.F64[iy]);
     1168            }
     1169        }
     1170
     1171        psImage *invMatrix = NULL;
    11731172        psVector *solution = NULL;                       // Solution to equation!
    11741173        solution = psVectorAlloc(numParams, PS_TYPE_F64);
     
    11761175
    11771176        // DUAL solution
    1178         solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, NAN);
     1177# if (1)
     1178        solution = psMatrixSolveSVD(solution, sumMatrix, sumVector, 1e-10);
     1179        invMatrix = psMatrixInvert(NULL, sumMatrix, NULL);
     1180# endif
     1181# if (0)
     1182        psMatrixLUSolve(sumMatrix, sumVector);
     1183        solution = psMemIncrRefCounter(sumVector);
     1184        invMatrix = psMemIncrRefCounter(sumMatrix);
     1185# endif
    11791186
    11801187#if (0)
    11811188        for (int i = 0; i < solution->n; i++) {
    1182             fprintf(stderr, "Dual solution %d: %lf\n", i, solution->data.F64[i]);
     1189            fprintf(stderr, "Dual solution %d: %lf +/- %lf\n", i, solution->data.F64[i], sqrt(invMatrix->data.F64[i][i]));
    11831190        }
    11841191#endif
    11851192
    1186         if (!kernels->solution1) {
    1187             kernels->solution1 = psVectorAlloc(numSolution1 + 2, PS_TYPE_F64);
    1188             psVectorInit (kernels->solution1, 0.0);
    1189         }
    1190         if (!kernels->solution2) {
    1191             kernels->solution2 = psVectorAlloc(numSolution2, PS_TYPE_F64);
    1192             psVectorInit (kernels->solution2, 0.0);
    1193         }
     1193        // XXX TEST: manually set the coeffs to a desired solution
     1194        // solution->data.F64[0] = +1.826;
     1195        // solution->data.F64[1] = -0.115;
     1196        // solution->data.F64[2] =  0.0;
     1197        // solution->data.F64[3] =  0.0;
     1198
     1199        // ensure we have solution vectors of the right size
     1200        kernels->solution1    = psVectorRecycle(kernels->solution1,    numSolution1 + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1201        kernels->solution1err = psVectorRecycle(kernels->solution1err, numSolution1 + 2, PS_TYPE_F64); // 1 for norm, 1 for bg
     1202        kernels->solution2    = psVectorRecycle(kernels->solution2,    numSolution2,     PS_TYPE_F64); // 1 for norm, 1 for bg
     1203        kernels->solution2err = psVectorRecycle(kernels->solution2err, numSolution2,     PS_TYPE_F64); // 1 for norm, 1 for bg
     1204
     1205        psVectorInit(kernels->solution1, 0.0);
     1206        psVectorInit(kernels->solution1err, 0.0);
     1207        psVectorInit(kernels->solution2, 0.0);
     1208        psVectorInit(kernels->solution2err, 0.0);
    11941209
    11951210        // for DUAL convolution analysis, we apply the normalization to I1 as follows:
     
    12051220            kernels->solution1->data.F64[i] = solution->data.F64[i] * stamps->normValue;
    12061221            kernels->solution2->data.F64[i] = solution->data.F64[i + numSolution1];
     1222
     1223            kernels->solution1err->data.F64[i] = sqrt(invMatrix->data.F64[i][i]) * stamps->normValue;
     1224            int i2 = i + numSolution1;
     1225            kernels->solution2err->data.F64[i] = sqrt(invMatrix->data.F64[i2][i2]);
    12071226        }
    12081227
     
    12131232        kernels->solution1->data.F64[bgIndex] = 0.0;
    12141233
     1234        psFree(solution);
     1235        psFree(sumVector);
    12151236        psFree(sumMatrix);
    1216         psFree(sumVector);
    1217         psFree(solution);
     1237        psFree(invMatrix);
    12181238    }
    12191239
     
    12241244    if (psTraceGetLevel("psModules.imcombine") >= 7) {
    12251245        for (int i = 0; i < kernels->solution1->n; i++) {
    1226             psTrace("psModules.imcombine", 7, "Solution 1 %d: %f\n", i, kernels->solution1->data.F64[i]);
     1246            psTrace("psModules.imcombine", 7, "Solution 1 %d: %f +/- %f\n", i, kernels->solution1->data.F64[i], kernels->solution1err->data.F64[i]);
    12271247        }
    12281248        if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) {
    12291249            for (int i = 0; i < kernels->solution2->n; i++) {
    1230                 psTrace("psModules.imcombine", 7, "Solution 2 %d: %f\n", i, kernels->solution2->data.F64[i]);
     1250                psTrace("psModules.imcombine", 7, "Solution 2 %d: %f +/- %f\n", i, kernels->solution2->data.F64[i], kernels->solution2err->data.F64[i]);
    12311251            }
    12321252        }
    12331253     }
     1254
     1255    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Solve equation: %f sec", psTimerClear("pmSubtractionSolveEquation"));
    12341256
    12351257    // pmSubtractionVisualPlotLeastSquares((pmSubtractionStampList *) stamps); //casting away const
     
    12831305}
    12841306
    1285 psVector *pmSubtractionCalculateDeviations(pmSubtractionStampList *stamps,
    1286                                            pmSubtractionKernels *kernels)
     1307// given the convolved image(s) and the residual image, calculate the second moment(s) and the chisq
     1308bool pmSubtractionChisqStats(psVector *fluxesVector, psVector *chisqDVector, psVector *chisqRVector, psVector *momentVector, psVector *stampMask, psKernel *convolved1, psKernel *convolved2, psKernel *difference, psKernel *residual, psKernel *weight, psKernel *window) {
     1309
     1310# ifndef USE_WEIGHT
     1311    psAssert(weight == NULL, "impossible!");
     1312# endif
     1313# ifndef USE_WINDOW
     1314    psAssert(window == NULL, "impossible!");
     1315# endif
     1316
     1317    int npix = 0;
     1318    float chisqR = 0;
     1319    float chisqD = 0;
     1320
     1321    // get the chisq
     1322    for (int y = residual->yMin; y <= residual->yMax; y++) {
     1323        for (int x = residual->xMin; x <= residual->xMax; x++) {
     1324            float valueR = PS_SQR(residual->kernel[y][x]);
     1325            if (weight) {
     1326                valueR *= weight->kernel[y][x];
     1327            }
     1328            // XXX NOTE: do NOT apply the window to the chisq portions of the calculation (that would bias the chisq)
     1329            chisqR += valueR;
     1330
     1331            float valueD = PS_SQR(difference->kernel[y][x]);
     1332            if (weight) {
     1333                valueD *= weight->kernel[y][x];
     1334            }
     1335            chisqD += valueD;
     1336            npix ++;
     1337        }
     1338    }
     1339    psVectorAppend(chisqRVector, chisqR / npix);
     1340    psVectorAppend(chisqDVector, chisqD / npix);
     1341
     1342    float value1 = 0;
     1343    float value2 = 0;
     1344    float flux2 = 0;
     1345    float fluxX = 0;
     1346    float fluxY = 0;
     1347    float fluxX2 = 0;
     1348    float fluxY2 = 0;
     1349
     1350    float fluxC1 = 0;
     1351    float fluxC2 = 0;
     1352
     1353    float moment = 0;
     1354
     1355    // get the moments from convolved1
     1356    if (convolved1) {
     1357        for (int y = residual->yMin; y <= residual->yMax; y++) {
     1358            for (int x = residual->xMin; x <= residual->xMax; x++) {
     1359                value1  = convolved1->kernel[y][x];
     1360                value2  = PS_SQR(value1);
     1361
     1362                if (window) {
     1363                    value1 *= window->kernel[y][x];
     1364                    value2 *= window->kernel[y][x];
     1365                }
     1366
     1367                fluxC1 += value1;
     1368                flux2  += value2;
     1369                fluxX  += x * value2;
     1370                fluxY  += y * value2;
     1371                // fluxX2 += PS_SQR(x) * value2;
     1372                // fluxY2 += PS_SQR(y) * value2;
     1373                fluxX2 += PS_SQR(x) * value1;
     1374                fluxY2 += PS_SQR(y) * value1;
     1375            }
     1376        }
     1377        // float Mx = fluxX / flux2;
     1378        // float My = fluxY / flux2;
     1379        // float Mxx = fluxX2 / flux2;
     1380        // float Myy = fluxY2 / flux2;
     1381        float Mxx = fluxX2 / fluxC1;
     1382        float Myy = fluxY2 / fluxC1;
     1383
     1384        // fprintf (stderr, "conv1, flux2: %f, Mx: %f, My: %f, Mxx: %f, Myy: %f, chisq: %f, npix: %d\n", flux2, Mx, My, Mxx, Myy, chisq, npix);
     1385        moment += Mxx + Myy;
     1386    }
     1387
     1388    // get the moments from convolved1
     1389    if (convolved2) {
     1390        for (int y = residual->yMin; y <= residual->yMax; y++) {
     1391            for (int x = residual->xMin; x <= residual->xMax; x++) {
     1392                value1  = convolved2->kernel[y][x];
     1393                value2  = PS_SQR(value1);
     1394
     1395                // XXX NOTE: do NOT apply the weight to the moments calculation
     1396                if (false && weight) {
     1397                    value2 *= weight->kernel[y][x];
     1398                }
     1399                if (window) {
     1400                    value1 *= window->kernel[y][x];
     1401                    value2 *= window->kernel[y][x];
     1402                }
     1403
     1404                fluxC2 += value1;
     1405                flux2  += value2;
     1406                fluxX  += x * value2;
     1407                fluxY  += y * value2;
     1408                // fluxX2 += PS_SQR(x) * value2;
     1409                // fluxY2 += PS_SQR(y) * value2;
     1410                fluxX2 += PS_SQR(x) * value1;
     1411                fluxY2 += PS_SQR(y) * value1;
     1412            }
     1413        }
     1414        // float Mx = fluxX / flux2;
     1415        // float My = fluxY / flux2;
     1416        // float Mxx = fluxX2 / flux2;
     1417        // float Myy = fluxY2 / flux2;
     1418        float Mxx = fluxX2 / fluxC2;
     1419        float Myy = fluxY2 / fluxC2;
     1420
     1421        // fprintf (stderr, "conv2, flux2: %f, Mx: %f, My: %f, Mxx: %f, Myy: %f, chisq: %f, npix: %d\n", flux2, Mx, My, Mxx, Myy, chisq, npix);
     1422        moment += Mxx + Myy;
     1423    }
     1424
     1425    float flux = fluxC1 + fluxC2;
     1426   
     1427    if (convolved1 && convolved2) {
     1428        moment *= 0.5;
     1429        flux *= 0.5;
     1430    }
     1431    psVectorAppend(momentVector, moment);
     1432    psVectorAppend(fluxesVector, flux);
     1433
     1434    // check that the last appended values are ok:
     1435    int Nelem = fluxesVector->n - 1;
     1436    bool valid = true;
     1437    valid &= isfinite(chisqRVector->data.F32[Nelem]);
     1438    valid &= isfinite(fluxesVector->data.F32[Nelem]);
     1439    valid &= isfinite(momentVector->data.F32[Nelem]);
     1440    if (valid) {
     1441      psVectorAppend(stampMask, 0);
     1442    } else {
     1443      psVectorAppend(stampMask, 0x02);
     1444    }
     1445    return true;
     1446}
     1447
     1448bool pmSubtractionCalculateChisqAndMoments(pmSubtractionQuality **bestMatch,
     1449                                           pmSubtractionStampList *stamps,
     1450                                           pmSubtractionKernels *kernels)
    12871451{
    12881452    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, NULL);
    12891453    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, NULL);
    12901454    PM_ASSERT_SUBTRACTION_KERNELS_SOLUTION(kernels, NULL);
     1455
     1456    psTimerStart("pmSubtractionCalculateChisqAndMoments");
     1457
     1458    // XXX need to save these somewhere
     1459    psVector *fluxes = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1460    psVector *chisqD = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1461    psVector *chisqR = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1462    psVector *moments = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     1463    psVector *stampMask = psVectorAllocEmpty(stamps->num, PS_TYPE_VECTOR_MASK);
     1464
     1465    int footprint = stamps->footprint; // Half-size of stamps
     1466    int numKernels = kernels->num;      // Number of kernels
     1467
     1468    psImage *polyValues = NULL;         // Polynomial values
     1469
     1470    // storage for the image (convolved2 is not used in SINGLE mode)
     1471    psKernel *residual = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1472    psKernel *difference = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1473    psKernel *convolved1 = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1474    psKernel *convolved2 = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
     1475
     1476    int nGood = 0;
     1477    for (int i = 0; i < stamps->num; i++) {
     1478        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // The stamp of interest
     1479        if (stamp->status != PM_SUBTRACTION_STAMP_USED) {
     1480            // mark this stamp as unused (note that we have to append NANs to the other vectors to keep the lengths in sync)
     1481            psVectorAppend(moments, NAN);
     1482            psVectorAppend(fluxes, NAN);
     1483            psVectorAppend(chisqD, NAN);
     1484            psVectorAppend(chisqR, NAN);
     1485            psVectorAppend(stampMask, 0x01);
     1486            continue;
     1487        }
     1488        nGood ++;
     1489
     1490        // Calculate coefficients of the kernel basis functions
     1491        polyValues = p_pmSubtractionPolynomial(polyValues, kernels->spatialOrder, stamp->xNorm, stamp->yNorm);
     1492        double norm = p_pmSubtractionSolutionNorm(kernels); // Normalisation
     1493        double background = p_pmSubtractionSolutionBackground(kernels, polyValues);// Difference in background
     1494
     1495        // Calculate residuals
     1496        psImageInit(residual->image, 0.0);
     1497        psImageInit(difference->image, 0.0);
     1498
     1499        psKernel *weight = NULL;
     1500        psKernel *window = NULL;
     1501   
     1502#ifdef USE_WEIGHT
     1503    weight = stamp->weight;
     1504#endif
     1505#ifdef USE_WINDOW
     1506    window = stamps->window;
     1507#endif
     1508
     1509        if (kernels->mode != PM_SUBTRACTION_MODE_DUAL) {
     1510
     1511            // the single-direction psf match code attempts to find the kernel such that:
     1512            // source * kernel = target.  we need to assign 'source' and 'target' correctly
     1513            // depending on which of image1 or image2 we asked to be convolved.
     1514
     1515            psKernel *target;           // Target postage stamp (convolve source to match the target)
     1516            psKernel *source;           // Source postage stamp (convolve source to match the target)
     1517            psArray *convolutions;      // Convolution postage stamps for each kernel basis function
     1518
     1519            // init the accumulation image
     1520            psImageInit(convolved1->image, 0.0);
     1521
     1522            switch (kernels->mode) {
     1523              case PM_SUBTRACTION_MODE_1:
     1524                target = stamp->image2;
     1525                source = stamp->image1;
     1526                convolutions = stamp->convolutions1;
     1527                break;
     1528              case PM_SUBTRACTION_MODE_2:
     1529                target = stamp->image1;
     1530                source = stamp->image2;
     1531                convolutions = stamp->convolutions2;
     1532                break;
     1533              default:
     1534                psAbort("Unsupported subtraction mode: %x", kernels->mode);
     1535            }
     1536
     1537            // generate the convolved source image (sum over kernels)
     1538            for (int j = 0; j < numKernels; j++) {
     1539                psKernel *convolution = convolutions->data[j]; // Convolution
     1540                double coefficient = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient
     1541                for (int y = - footprint; y <= footprint; y++) {
     1542                    for (int x = - footprint; x <= footprint; x++) {
     1543                        convolved1->kernel[y][x] += convolution->kernel[y][x] * coefficient;
     1544                    }
     1545                }
     1546            }
     1547
     1548            // Generate the difference, residual, and convolved source images.  Note the we
     1549            // accumulate the convolution of (A-B), so we need to replace it to generate the
     1550            // images of the convolved source image.
     1551            for (int y = - footprint; y <= footprint; y++) {
     1552                for (int x = - footprint; x <= footprint; x++) {
     1553                    difference->kernel[y][x] = target->kernel[y][x] - source->kernel[y][x] * norm - background;
     1554                    residual->kernel[y][x] = difference->kernel[y][x] - convolved1->kernel[y][x];
     1555                    convolved1->kernel[y][x] += source->kernel[y][x] * norm;
     1556                }
     1557            }
     1558
     1559            // XXX if we want to have a weight and window, we'll need to pass through to here
     1560            pmSubtractionChisqStats(fluxes, chisqD, chisqR, moments, stampMask, convolved1, NULL, difference, residual, weight, window);
     1561
     1562        } else {
     1563
     1564            // Dual convolution
     1565            psArray *convolutions1 = stamp->convolutions1; // Convolutions of the first image
     1566            psArray *convolutions2 = stamp->convolutions2; // Convolutions of the second image
     1567            psKernel *image1 = stamp->image1; // The first image
     1568            psKernel *image2 = stamp->image2; // The second image
     1569
     1570            // init the accumulation images
     1571            psImageInit(convolved1->image, 0.0);
     1572            psImageInit(convolved2->image, 0.0);
     1573
     1574            for (int j = 0; j < numKernels; j++) {
     1575                psKernel *conv1 = convolutions1->data[j]; // Convolution of first image
     1576                psKernel *conv2 = convolutions2->data[j]; // Convolution of second image
     1577                double coeff1 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient 1
     1578                double coeff2 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, true); // Coefficient 2
     1579
     1580                for (int y = - footprint; y <= footprint; y++) {
     1581                    for (int x = - footprint; x <= footprint; x++) {
     1582                        // NOTE sign for coeff2
     1583                        convolved1->kernel[y][x] += +conv1->kernel[y][x] * coeff1;
     1584                        convolved2->kernel[y][x] += -conv2->kernel[y][x] * coeff2;
     1585                    }
     1586                }
     1587            }
     1588
     1589            // Generate the difference, residual, and convolved source images.  Note the we
     1590            // accumulate the convolutions of (A-B), so we need to replace (A or B) to generate
     1591            // the images of the convolved source images.
     1592            for (int y = - footprint; y <= footprint; y++) {
     1593                for (int x = - footprint; x <= footprint; x++) {
     1594                    difference->kernel[y][x] = image2->kernel[y][x] - image1->kernel[y][x] * norm - background;
     1595                    residual->kernel[y][x] = difference->kernel[y][x] + convolved2->kernel[y][x] - convolved1->kernel[y][x];
     1596                    convolved1->kernel[y][x] += image1->kernel[y][x] * norm;
     1597                    convolved2->kernel[y][x] += image2->kernel[y][x];
     1598                }
     1599            }
     1600
     1601            if (0) {
     1602                psFitsWriteImageSimple("conv1.fits", convolved1->image, NULL);
     1603                psFitsWriteImageSimple("conv2.fits", convolved2->image, NULL);
     1604                psFitsWriteImageSimple("resid.fits", residual->image,   NULL);
     1605                pmVisualAskUser(NULL);
     1606            }
     1607
     1608            pmSubtractionChisqStats(fluxes, chisqD, chisqR, moments, stampMask, convolved1, convolved2, difference, residual, weight, window);
     1609        }
     1610    }
     1611
     1612    // find the mean chisq and mean moment
     1613    psStats *stats = psStatsAlloc(PS_STAT_SAMPLE_MEAN);
     1614    psVectorStats (stats, chisqD, NULL, stampMask, 0xff);
     1615    float chisqDValue = stats->sampleMean;
     1616
     1617    psStatsInit(stats);
     1618    psVectorStats (stats, chisqR, NULL, stampMask, 0xff);
     1619    float chisqRValue = stats->sampleMean;
     1620
     1621    psStatsInit(stats);
     1622    psVectorStats (stats, moments, NULL, stampMask, 0xff);
     1623    float momentValue = stats->sampleMean;
     1624
     1625    double sumKernel1 = 0.0, sumKernel2 = 0.0; // Sum of the kernel
     1626
     1627    // calculate the variance contribution from this smoothing kernel
     1628    psKernel *modelKernel = pmSubtractionKernel(kernels, 0.0, 0.0, false);
     1629    for (int y = modelKernel->yMin; y <= modelKernel->yMax; y++) {
     1630        for (int x = modelKernel->xMin; x <= modelKernel->xMax; x++) {
     1631            if (!isfinite(modelKernel->kernel[y][x])) {
     1632                psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Non-finite covariance matrix element in kernel at %d,%d", x, y);
     1633                return NULL;
     1634            }
     1635            sumKernel1 += PS_SQR(modelKernel->kernel[y][x]);
     1636        }
     1637    }
     1638    psFree (modelKernel);
     1639
     1640    if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) {
     1641        psKernel *modelKernel = pmSubtractionKernel(kernels, 0.0, 0.0, true);
     1642        for (int y = modelKernel->yMin; y <= modelKernel->yMax; y++) {
     1643            for (int x = modelKernel->xMin; x <= modelKernel->xMax; x++) {
     1644                if (!isfinite(modelKernel->kernel[y][x])) {
     1645                    psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Non-finite covariance matrix element in kernel at %d,%d", x, y);
     1646                    return NULL;
     1647                }
     1648                sumKernel2 += PS_SQR(modelKernel->kernel[y][x]);
     1649            }
     1650        }
     1651        psFree (modelKernel);
     1652    } else {
     1653        sumKernel2 = 1.0;
     1654    }
     1655
     1656    // if we modify the chisq value by the (sumKernel1 + sumKernel2), we account for the
     1657    // smoothing coming from larger kernels adding additional spatial fit terms should be
     1658    // penalized by increasing the score somewhat.  the 0.01 value is not well-chosen.
     1659    float orderFactor = 0.01 * kernels->spatialOrder;
     1660    float score = 2.0 * chisqRValue / (sumKernel1 + sumKernel2) + orderFactor;
     1661    psLogMsg("psModules.imcombine", PS_LOG_INFO, "chisq: %6.3f, chisqD: %6.3f, moment: %6.3f, sumKernel_1: %6.3f, sumKernel_2, score: %6.3f: %6.3f\n", chisqRValue, chisqDValue, momentValue, sumKernel1, sumKernel2, score);
     1662
     1663    // save this result if it is the first or the best (skip if bestMatch is NULL)
     1664    if (bestMatch) {
     1665        pmSubtractionQuality *match = *bestMatch;
     1666        bool keep = false;
     1667        if (match == NULL) {
     1668            *bestMatch = match = pmSubtractionQualityAlloc();
     1669            keep = true;
     1670        } else {
     1671            if (score < match->score) {
     1672                psFree(match->fluxes);
     1673                psFree(match->chisq);
     1674                psFree(match->moments);
     1675                psFree(match->stampMask);
     1676                keep = true;
     1677            }
     1678        }
     1679        if (keep) {
     1680            psLogMsg("psModules.imcombine", PS_LOG_INFO, "keeping order: %d, mode: %d, score: %f\n", kernels->spatialOrder, kernels->mode, score);
     1681            match->score        = score;
     1682            match->spatialOrder = kernels->spatialOrder;
     1683            match->mode         = kernels->mode;
     1684            match->nGood        = nGood;
     1685            match->fluxes       = psMemIncrRefCounter(fluxes);
     1686            match->chisq        = psMemIncrRefCounter(chisqR);
     1687            match->moments      = psMemIncrRefCounter(moments);
     1688            match->stampMask    = psMemIncrRefCounter(stampMask);
     1689        }           
     1690    }
     1691
     1692    pmSubtractionVisualPlotChisqAndMoments(fluxes, chisqR, moments);
     1693
     1694    psFree(stats);
     1695    psFree(chisqR);
     1696    psFree(chisqD);
     1697    psFree(fluxes);
     1698    psFree(moments);
     1699    psFree(stampMask);
     1700
     1701    psFree(residual);
     1702    psFree(difference);
     1703    psFree(convolved1);
     1704    psFree(convolved2);
     1705    psFree(polyValues);
     1706
     1707    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Calculate Chisq and Moments: %f sec", psTimerClear("pmSubtractionCalculateChisqAndMoments"));
     1708
     1709    return true;
     1710}
     1711
     1712// XXX for now, let's not use this, and let's instead just use values from pmSubtractionCalculateChisqAndMoments
     1713psVector *pmSubtractionCalculateDeviations(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels)
     1714{
     1715    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, NULL);
     1716    PM_ASSERT_SUBTRACTION_KERNELS_NON_NULL(kernels, NULL);
     1717    PM_ASSERT_SUBTRACTION_KERNELS_SOLUTION(kernels, NULL);
     1718
     1719    psTimerStart("pmSubtractionCalculateDeviations");
    12911720
    12921721    psVector *deviations = psVectorAlloc(stamps->num, PS_TYPE_F32); // Mean deviation for stamps
     
    12981727    psImage *polyValues = NULL;         // Polynomial values
    12991728    psKernel *residual = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
    1300 
    1301     // set up holding images for the visualization
    1302     pmSubtractionVisualShowFitInit (stamps);
    13031729
    13041730    psVector *fResSigma = psVectorAllocEmpty(stamps->num, PS_TYPE_F32);
     
    14011827            }
    14021828
    1403             // XXX visualize the target, source, convolution and residual
    1404             pmSubtractionVisualShowFitAddStamp (target, source, residual, background, norm, i);
    1405 
    14061829            for (int y = - footprint; y <= footprint; y++) {
    14071830                for (int x = - footprint; x <= footprint; x++) {
     
    14381861            }
    14391862
    1440             // XXX visualize the target, source, convolution and residual
    1441             pmSubtractionVisualShowFitAddStamp (image2, image1, residual, background, norm, i);
    1442 
    14431863            for (int y = - footprint; y <= footprint; y++) {
    14441864                for (int x = - footprint; x <= footprint; x++) {
     
    14551875        }
    14561876
     1877        double flux = 0.0;
    14571878        double deviation = 0.0;         // Sum of differences
    14581879        for (int y = - footprint; y <= footprint; y++) {
     
    14601881                double dev = PS_SQR(residual->kernel[y][x]) * weight->kernel[y][x];
    14611882                deviation += dev;
    1462 #ifdef TESTING
    1463                 residual->kernel[y][x] = dev;
    1464 #endif
     1883                flux += stamp->image1->kernel[y][x] + stamp->image2->kernel[y][x];
    14651884            }
    14661885        }
    14671886        deviations->data.F32[i] = devNorm * deviation;
    1468         psTrace("psModules.imcombine", 5, "Deviation for stamp %d (%d,%d): %f\n",
    1469                 i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5), deviations->data.F32[i]);
     1887        psTrace("psModules.imcombine", 5, "Deviation and Flux for stamp %d (%d,%d): %f %f\n",
     1888                i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5), deviations->data.F32[i], flux);
    14701889        psStringAppend(&log, "Stamp %d (%d,%d): %f\n",
    14711890                       i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5), deviations->data.F32[i]);
    14721891        if (!isfinite(deviations->data.F32[i])) {
    14731892            stamp->status = PM_SUBTRACTION_STAMP_REJECTED;
    1474             psTrace("psModules.imcombine", 5,
    1475                     "Rejecting stamp %d (%d,%d) because of non-finite deviation\n",
    1476                     i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5));
     1893            psTrace("psModules.imcombine", 5, "Rejecting stamp %d (%d,%d) because of non-finite deviation\n", i, (int)(stamp->x - 0.5), (int)(stamp->y - 0.5));
    14771894            continue;
    14781895        }
    1479 
    1480 #ifdef TESTING
    1481         {
    1482             psString filename = NULL;
    1483             psStringAppend(&filename, "resid_%03d.fits", i);
    1484             psFits *fits = psFitsOpen(filename, "w");
    1485             psFree(filename);
    1486             psFitsWriteImage(fits, NULL, residual->image, 0, NULL);
    1487             psFitsClose(fits);
    1488         }
    1489         if (stamp->image1) {
    1490             psString filename = NULL;
    1491             psStringAppend(&filename, "stamp_image1_%03d.fits", i);
    1492             psFits *fits = psFitsOpen(filename, "w");
    1493             psFree(filename);
    1494             psFitsWriteImage(fits, NULL, stamp->image1->image, 0, NULL);
    1495             psFitsClose(fits);
    1496         }
    1497         if (stamp->image2) {
    1498             psString filename = NULL;
    1499             psStringAppend(&filename, "stamp_image2_%03d.fits", i);
    1500             psFits *fits = psFitsOpen(filename, "w");
    1501             psFree(filename);
    1502             psFitsWriteImage(fits, NULL, stamp->image2->image, 0, NULL);
    1503             psFitsClose(fits);
    1504         }
    1505         if (stamp->weight) {
    1506             psString filename = NULL;
    1507             psStringAppend(&filename, "stamp_weight_%03d.fits", i);
    1508             psFits *fits = psFitsOpen(filename, "w");
    1509             psFree(filename);
    1510             psFitsWriteImage(fits, NULL, stamp->weight->image, 0, NULL);
    1511             psFitsClose(fits);
    1512         }
    1513 #endif
    1514 
    15151896    }
    15161897
    15171898    psFree(keepStamps);
    15181899
    1519     psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "%s", log);
     1900    psLogMsg("psModules.imcombine", PS_LOG_MINUTIA, "%s", log);
    15201901    psFree(log);
    15211902
     
    15271908        psLogMsg("psModules.imcombine", PS_LOG_INFO, "normalization: %f, background: %f", norm, background);
    15281909
    1529         pmSubtractionVisualShowFit(norm);
    1530         pmSubtractionVisualPlotFit(kernels);
    1531 
    15321910        psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
    15331911        psVectorStats (stats, fResSigma, NULL, NULL, 0);
     
    15601938    psFree(polyValues);
    15611939
     1940    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Calculate Deviations: %f sec", psTimerClear("pmSubtractionCalculateDeviations"));
     1941
    15621942    return deviations;
    1563 }
    1564 
    1565 // we are supplied U, not Ut; w represents a diagonal matrix (also, we apply 1/w instead of w)
    1566 psImage *p_pmSubSolve_wUt (psVector *w, psImage *U) {
    1567 
    1568     psAssert (w->n == U->numCols, "w and U dimensions do not match");
    1569 
    1570     // wUt has dimensions transposed relative to Ut.
    1571     psImage *wUt = psImageAlloc (U->numRows, U->numCols, PS_TYPE_F64);
    1572     psImageInit (wUt, 0.0);
    1573 
    1574     for (int i = 0; i < wUt->numCols; i++) {
    1575         for (int j = 0; j < wUt->numRows; j++) {
    1576             if (!isfinite(w->data.F64[j])) continue;
    1577             if (w->data.F64[j] == 0.0) continue;
    1578             wUt->data.F64[j][i] = U->data.F64[i][j] / w->data.F64[j];
    1579         }
    1580     }
    1581     return wUt;
    1582 }
    1583 
    1584 // XXX this is just standard matrix multiplication: use psMatrixMultiply?
    1585 psImage *p_pmSubSolve_VwUt (psImage *V, psImage *wUt) {
    1586 
    1587     psAssert (V->numCols == wUt->numRows, "matrix dimensions do not match");
    1588 
    1589     psImage *Ainv = psImageAlloc (wUt->numCols, V->numRows, PS_TYPE_F64);
    1590 
    1591     for (int i = 0; i < Ainv->numCols; i++) {
    1592         for (int j = 0; j < Ainv->numRows; j++) {
    1593             double sum = 0.0;
    1594             for (int k = 0; k < V->numCols; k++) {
    1595                 sum += V->data.F64[j][k] * wUt->data.F64[k][i];
    1596             }
    1597             Ainv->data.F64[j][i] = sum;
    1598         }
    1599     }
    1600     return Ainv;
    1601 }
    1602 
    1603 // we are supplied U, not Ut
    1604 bool p_pmSubSolve_UtB (psVector **UtB, psImage *U, psVector *B) {
    1605 
    1606     psAssert (U->numRows == B->n, "U and B dimensions do not match");
    1607 
    1608     UtB[0] = psVectorRecycle (UtB[0], U->numCols, PS_TYPE_F64);
    1609 
    1610     for (int i = 0; i < U->numCols; i++) {
    1611         double sum = 0.0;
    1612         for (int j = 0; j < U->numRows; j++) {
    1613             sum += B->data.F64[j] * U->data.F64[j][i];
    1614         }
    1615         UtB[0]->data.F64[i] = sum;
    1616     }
    1617     return true;
    1618 }
    1619 
    1620 // w is diagonal
    1621 bool p_pmSubSolve_wUtB (psVector **wUtB, psVector *w, psVector *UtB) {
    1622 
    1623     psAssert (w->n == UtB->n, "w and UtB dimensions do not match");
    1624 
    1625     // wUt has dimensions transposed relative to Ut.
    1626     wUtB[0] = psVectorRecycle (wUtB[0], w->n, PS_TYPE_F64);
    1627     psVectorInit (wUtB[0], 0.0);
    1628 
    1629     for (int i = 0; i < w->n; i++) {
    1630         if (!isfinite(w->data.F64[i])) continue;
    1631         if (w->data.F64[i] == 0.0) continue;
    1632         wUtB[0]->data.F64[i] = UtB->data.F64[i] / w->data.F64[i];
    1633     }
    1634     return true;
    1635 }
    1636 
    1637 // this is basically matrix * vector
    1638 bool p_pmSubSolve_VwUtB (psVector **VwUtB, psImage *V, psVector *wUtB) {
    1639 
    1640     psAssert (V->numCols == wUtB->n, "V and wUtB dimensions do not match");
    1641 
    1642     VwUtB[0] = psVectorRecycle (*VwUtB, V->numRows, PS_TYPE_F64);
    1643 
    1644     for (int j = 0; j < V->numRows; j++) {
    1645         double sum = 0.0;
    1646         for (int i = 0; i < V->numCols; i++) {
    1647             sum += V->data.F64[j][i] * wUtB->data.F64[i];
    1648         }
    1649         VwUtB[0]->data.F64[j] = sum;
    1650     }
    1651     return true;
    1652 }
    1653 
    1654 // this is basically matrix * vector
    1655 bool p_pmSubSolve_Ax (psVector **B, psImage *A, psVector *x) {
    1656 
    1657     psAssert (A->numCols == x->n, "A and x dimensions do not match");
    1658 
    1659     B[0] = psVectorRecycle (*B, A->numRows, PS_TYPE_F64);
    1660 
    1661     for (int j = 0; j < A->numRows; j++) {
    1662         double sum = 0.0;
    1663         for (int i = 0; i < A->numCols; i++) {
    1664             sum += A->data.F64[j][i] * x->data.F64[i];
    1665         }
    1666         B[0]->data.F64[j] = sum;
    1667     }
    1668     return true;
    1669 }
    1670 
    1671 // this is basically Vector * vector
    1672 bool p_pmSubSolve_VdV (double *value, psVector *x, psVector *y) {
    1673 
    1674     psAssert (x->n == y->n, "x and y dimensions do not match");
    1675 
    1676     double sum = 0.0;
    1677     for (int i = 0; i < x->n; i++) {
    1678         sum += x->data.F64[i] * y->data.F64[i];
    1679     }
    1680     *value = sum;
    1681     return true;
    1682 }
    1683 
    1684 bool p_pmSubSolve_y2 (double *y2, pmSubtractionKernels *kernels, const pmSubtractionStampList *stamps) {
    1685 
    1686     int footprint = stamps->footprint; // Half-size of stamps
    1687 
    1688     double sum = 0.0;
    1689     for (int i = 0; i < stamps->num; i++) {
    1690 
    1691         pmSubtractionStamp *stamp = stamps->stamps->data[i];
    1692         if (stamp->status != PM_SUBTRACTION_STAMP_USED) continue;
    1693 
    1694         psKernel *weight = NULL;
    1695         psKernel *window = NULL;
    1696         psKernel *input = NULL;
    1697 
    1698 #ifdef USE_WEIGHT
    1699         weight = stamp->weight;
    1700 #endif
    1701 #ifdef USE_WINDOW
    1702         window = stamps->window;
    1703 #endif
    1704 
    1705         switch (kernels->mode) {
    1706             // MODE_1 : convolve image 1 to match image 2 (and vice versa)
    1707           case PM_SUBTRACTION_MODE_1:
    1708             input = stamp->image2;
    1709             break;
    1710           case PM_SUBTRACTION_MODE_2:
    1711             input = stamp->image1;
    1712             break;
    1713           default:
    1714             psAbort ("programming error");
    1715         }
    1716 
    1717         for (int y = - footprint; y <= footprint; y++) {
    1718             for (int x = - footprint; x <= footprint; x++) {
    1719                 double in = input->kernel[y][x];
    1720                 double value = in*in;
    1721                 if (weight) {
    1722                     float wtVal = weight->kernel[y][x];
    1723                     value *= wtVal;
    1724                 }
    1725                 if (window) {
    1726                     float  winVal = window->kernel[y][x];
    1727                     value *= winVal;
    1728                 }
    1729                 sum += value;
    1730             }
    1731         }
    1732     }
    1733     *y2 = sum;
    1734     return true;
    1735 }
    1736 
    1737 double p_pmSubSolve_ChiSquare (pmSubtractionKernels *kernels, const pmSubtractionStampList *stamps) {
    1738 
    1739     int footprint = stamps->footprint; // Half-size of stamps
    1740     int numKernels = kernels->num;      // Number of kernels
    1741 
    1742     double sum = 0.0;
    1743 
    1744     psKernel *residual = psKernelAlloc(-footprint, footprint, -footprint, footprint); // Residual image
    1745     psImageInit(residual->image, 0.0);
    1746 
    1747     psImage *polyValues = NULL;         // Polynomial values
    1748 
    1749     for (int i = 0; i < stamps->num; i++) {
    1750 
    1751         pmSubtractionStamp *stamp = stamps->stamps->data[i];
    1752         if (stamp->status != PM_SUBTRACTION_STAMP_USED) continue;
    1753 
    1754         psKernel *weight = NULL;
    1755         psKernel *window = NULL;
    1756         psKernel *target = NULL;
    1757         psKernel *source = NULL;
    1758 
    1759         psArray *convolutions = NULL;
    1760 
    1761 #ifdef USE_WEIGHT
    1762         weight = stamp->weight;
    1763 #endif
    1764 #ifdef USE_WINDOW
    1765         window = stamps->window;
    1766 #endif
    1767 
    1768         switch (kernels->mode) {
    1769             // MODE_1 : convolve image 1 to match image 2 (and vice versa)
    1770           case PM_SUBTRACTION_MODE_1:
    1771             target = stamp->image2;
    1772             source = stamp->image1;
    1773             convolutions = stamp->convolutions1;
    1774             break;
    1775           case PM_SUBTRACTION_MODE_2:
    1776             target = stamp->image1;
    1777             source = stamp->image2;
    1778             convolutions = stamp->convolutions2;
    1779             break;
    1780           default:
    1781             psAbort ("programming error");
    1782         }
    1783 
    1784         // Calculate coefficients of the kernel basis functions
    1785         polyValues = p_pmSubtractionPolynomial(polyValues, kernels->spatialOrder, stamp->xNorm, stamp->yNorm);
    1786         double norm = p_pmSubtractionSolutionNorm(kernels); // Normalisation
    1787         double background = p_pmSubtractionSolutionBackground(kernels, polyValues);// Difference in background
    1788 
    1789         psImageInit(residual->image, 0.0);
    1790         for (int j = 0; j < numKernels; j++) {
    1791             psKernel *convolution = convolutions->data[j]; // Convolution
    1792             double coefficient = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient
    1793             for (int y = - footprint; y <= footprint; y++) {
    1794                 for (int x = - footprint; x <= footprint; x++) {
    1795                     residual->kernel[y][x] -= convolution->kernel[y][x] * coefficient;
    1796                 }
    1797             }
    1798         }
    1799 
    1800         for (int y = - footprint; y <= footprint; y++) {
    1801             for (int x = - footprint; x <= footprint; x++) {
    1802                 double resid = target->kernel[y][x] - background - source->kernel[y][x] * norm + residual->kernel[y][x];
    1803                 double value = PS_SQR(resid);
    1804                 if (weight) {
    1805                     float wtVal = weight->kernel[y][x];
    1806                     value *= wtVal;
    1807                 }
    1808                 if (window) {
    1809                     float  winVal = window->kernel[y][x];
    1810                     value *= winVal;
    1811                 }
    1812                 sum += value;
    1813             }
    1814         }
    1815     }
    1816     psFree (polyValues);
    1817     psFree (residual);
    1818 
    1819     return sum;
    1820 }
    1821 
    1822 bool p_pmSubSolve_SetWeights (psVector *wApply, psVector *w, psVector *wMask) {
    1823 
    1824     for (int i = 0; i < w->n; i++) {
    1825         wApply->data.F64[i] = wMask->data.U8[i] ? 0.0 : w->data.F64[i];
    1826     }
    1827     return true;
    1828 }
    1829 
    1830 // we are supplied V and w; w represents a diagonal matrix (also, we apply 1/w instead of w)
    1831 psImage *p_pmSubSolve_Xvar (psImage *V, psVector *w) {
    1832 
    1833     psAssert (w->n == V->numCols, "w and U dimensions do not match");
    1834 
    1835     psImage *Vn = psImageAlloc (V->numCols, V->numRows, PS_TYPE_F64);
    1836     psImageInit (Vn, 0.0);
    1837 
    1838     // generate Vn = V * w^{-1}
    1839     for (int j = 0; j < Vn->numRows; j++) {
    1840         for (int i = 0; i < Vn->numCols; i++) {
    1841             if (!isfinite(w->data.F64[i])) continue;
    1842             if (w->data.F64[i] == 0.0) continue;
    1843             Vn->data.F64[j][i] = V->data.F64[j][i] / w->data.F64[i];
    1844         }
    1845     }
    1846 
    1847     psImage *Xvar = psImageAlloc (V->numCols, V->numRows, PS_TYPE_F64);
    1848     psImageInit (Xvar, 0.0);
    1849 
    1850     // generate Xvar = Vn * Vn^T
    1851     for (int j = 0; j < Vn->numRows; j++) {
    1852         for (int i = 0; i < Vn->numCols; i++) {
    1853             double sum = 0.0;
    1854             for (int k = 0; k < Vn->numCols; k++) {
    1855                 sum += Vn->data.F64[k][i]*Vn->data.F64[k][j];
    1856             }
    1857             Xvar->data.F64[j][i] = sum;
    1858         }
    1859     }
    1860     return Xvar;
    18611943}
    18621944
     
    18641946// of the elements of an image A(x,y) = A->data.F64[y][x] = A_x,y, a matrix
    18651947// multiplication is: A_k,j * B_i,k = C_i,j
    1866 
    18671948
    18681949bool psFitsWriteImageSimple (char *filename, psImage *image, psMetadata *header) {
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionEquation.h

    r29543 r30631  
    44#include "pmSubtractionStamps.h"
    55#include "pmSubtractionKernels.h"
    6 
    7 typedef enum {
    8     PM_SUBTRACTION_EQUATION_NONE    = 0x00,
    9     PM_SUBTRACTION_EQUATION_NORM    = 0x01,
    10     PM_SUBTRACTION_EQUATION_BG      = 0x02,
    11     PM_SUBTRACTION_EQUATION_KERNELS = 0x04,
    12     PM_SUBTRACTION_EQUATION_ALL     = 0x07, // value should be NORM | BG | KERNELS
    13 } pmSubtractionEquationCalculationMode;
     6#include "pmSubtraction.h"
    147
    158/// Execute a thread job to calculate the least-squares equation for a stamp
     
    2013bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, ///< Stamps
    2114                                         pmSubtractionKernels *kernels, ///< Kernel parameters
    22                                          int index, ///< Index of stamp
    23                                          const pmSubtractionEquationCalculationMode mode
     15                                         int index ///< Index of stamp
    2416    );
    2517
    2618/// Calculate the least-squares equation to match the image quality
    2719bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, ///< Stamps
    28                                     pmSubtractionKernels *kernels, ///< Kernel parameters
    29                                     const pmSubtractionEquationCalculationMode mode
     20                                    pmSubtractionKernels *kernels ///< Kernel parameters
    3021    );
    3122
    3223/// Solve the least-squares equation to match the image quality
    3324bool pmSubtractionSolveEquation(pmSubtractionKernels *kernels, ///< Kernel parameters
    34                                 const pmSubtractionStampList *stamps, ///< Stamps
    35                                 const pmSubtractionEquationCalculationMode mode
     25                                const pmSubtractionStampList *stamps ///< Stamps
    3626    );
    3727
     
    9282bool pmSubtractionCalculateMomentsKernel(double *Mxx, double *Myy, psKernel *image, int footprint, int window);
    9383
     84bool pmSubtractionChisqStats(psVector *fluxesVector, psVector *chisqDVector, psVector *chisqRVector, psVector *momentVector, psVector *stampMask, psKernel *convolved1, psKernel *convolved2, psKernel *difference, psKernel *residual, psKernel *weight, psKernel *window);
     85
     86bool pmSubtractionCalculateChisqAndMoments(pmSubtractionQuality **bestMatch, pmSubtractionStampList *stamps, pmSubtractionKernels *kernels);
    9487#endif
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionEquation.v0.c

    r29543 r30631  
    99#include "pmErrorCodes.h"
    1010#include "pmSubtraction.h"
     11#include "pmSubtractionTypes.h"
    1112#include "pmSubtractionKernels.h"
    1213#include "pmSubtractionStamps.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionHermitian.c

    r26893 r30631  
    88#include <pslib.h>
    99
     10#include "pmSubtractionTypes.h"
    1011#include "pmSubtractionHermitian.h"
    1112
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionIO.c

    r27321 r30631  
    1212#include "pmConceptsRead.h"
    1313
     14#include "pmSubtractionTypes.h"
    1415#include "pmSubtraction.h"
    1516#include "pmSubtractionKernels.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionKernels.c

    r29597 r30631  
    88#include <pslib.h>
    99
     10#include "pmFPA.h"
     11#include "pmSubtractionTypes.h"
    1012#include "pmSubtraction.h"
    1113#include "pmSubtractionKernels.h"
     
    2022{
    2123    psFree(kernels->description);
     24    psFree(kernels->fwhms);
     25    psFree(kernels->orders);
    2226    psFree(kernels->u);
    2327    psFree(kernels->v);
     
    3034    psFree(kernels->solution1);
    3135    psFree(kernels->solution2);
     36    psFree(kernels->solution1err);
     37    psFree(kernels->solution2err);
    3238    psFree(kernels->sampleStamps);
    3339}
     
    419425
    420426    int num = 0;                        // Number of basis functions
    421     psString params = NULL;             // List of parameters
    422427    for (int i = 0; i < numGaussians; i++) {
    423428        int gaussOrder = orders->data.S32[i]; // Polynomial order to apply to Gaussian
    424         psStringAppend(&params, "(%.1f,%d)", fwhms->data.F32[i], orders->data.S32[i]);
    425429        num += (gaussOrder + 1) * (gaussOrder + 2) / 2;
    426430    }
    427431
    428     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_ISIS, size,
    429                                                               spatialOrder, penalty, bounds, mode); // Kernels
    430     psStringAppend(&kernels->description, "ISIS(%d,%s,%d,%.2e)", size, params, spatialOrder, penalty);
    431 
    432     psLogMsg("psModules.imcombine", PS_LOG_INFO, "ISIS kernel: %s,%d --> %d elements",
    433              params, spatialOrder, num);
    434     psFree(params);
     432    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_ISIS, size, fwhms, orders, spatialOrder, penalty, bounds, mode); // Kernels
     433    pmSubtractionKernelsMakeDescription(kernels);
     434    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    435435
    436436# if (!CENTRAL_DELTA && !ZERO_KERNEL_ZERO_FLUX)
     
    503503
    504504    int num = 0;                        // Number of basis functions
    505     psString params = NULL;             // List of parameters
    506505    for (int i = 0; i < numGaussians; i++) {
    507506        int gaussOrder = orders->data.S32[i]; // Polynomial order to apply to Gaussian
    508         psStringAppend(&params, "(%.1f,%d)", fwhms->data.F32[i], orders->data.S32[i]);
    509507        num += (gaussOrder + 1) * (gaussOrder + 2) / 2;
    510508        num += (11 - gaussOrder - 1);   // include all higher order radial terms
    511509    }
    512510
    513     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_ISIS_RADIAL, size,
    514                                                               spatialOrder, penalty, bounds, mode); // Kernels
    515     psStringAppend(&kernels->description, "ISIS_RADIAL(%d,%s,%d,%.2e)", size, params, spatialOrder, penalty);
    516 
    517     psLogMsg("psModules.imcombine", PS_LOG_INFO, "ISIS_RADIAL kernel: %s,%d --> %d elements", params, spatialOrder, num);
    518     psFree(params);
     511    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_ISIS_RADIAL, size, fwhms, orders, spatialOrder, penalty, bounds, mode); // Kernels
     512    pmSubtractionKernelsMakeDescription(kernels);
     513    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    519514
    520515    // Set the kernel parameters
     
    569564
    570565    int num = 0;                        // Number of basis functions
    571     psString params = NULL;             // List of parameters
    572566    for (int i = 0; i < numGaussians; i++) {
    573567        int gaussOrder = orders->data.S32[i]; // Polynomial order to apply to Gaussian
    574         psStringAppend(&params, "(%.1f,%d)", fwhms->data.F32[i], orders->data.S32[i]);
    575568        num += (gaussOrder + 1) * (gaussOrder + 2) / 2;
    576569    }
    577570
    578     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_HERM, size,
    579                                                               spatialOrder, penalty, bounds, mode); // Kernels
    580     psStringAppend(&kernels->description, "HERM(%d,%s,%d,%.2e)", size, params, spatialOrder, penalty);
    581 
    582     psLogMsg("psModules.imcombine", PS_LOG_INFO, "HERM kernel: %s,%d --> %d elements",
    583              params, spatialOrder, num);
    584     psFree(params);
     571    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_HERM, size, fwhms, orders, spatialOrder, penalty, bounds, mode); // Kernels
     572    pmSubtractionKernelsMakeDescription(kernels);
     573    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    585574
    586575    // Set the kernel parameters
     
    627616
    628617    int num = 0;                        // Number of basis functions
    629     psString params = NULL;             // List of parameters
    630618    for (int i = 0; i < numGaussians; i++) {
    631619        int gaussOrder = orders->data.S32[i]; // Polynomial order to apply to Gaussian
    632         psStringAppend(&params, "(%.1f,%d)", fwhms->data.F32[i], orders->data.S32[i]);
    633620        num += PS_SQR(gaussOrder + 1);
    634621    }
    635622
    636     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_DECONV_HERM, size,
    637                                                               spatialOrder, penalty, bounds, mode); // Kernels
    638     psStringAppend(&kernels->description, "DECONV_HERM(%d,%s,%d,%.2e)", size, params, spatialOrder, penalty);
    639 
    640     psLogMsg("psModules.imcombine", PS_LOG_INFO, "DECONVOLVED HERM kernel: %s,%d --> %d elements", params, spatialOrder, num);
    641     psFree(params);
     623    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_DECONV_HERM, size, fwhms, orders, spatialOrder, penalty, bounds, mode); // Kernels
     624    pmSubtractionKernelsMakeDescription(kernels);
     625    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    642626
    643627    // XXXXX hard-wired reference sigma for now of 1.7 pix (== 4.0 pix fwhm == 1.0 arcsec in simtest)
     
    713697
    714698pmSubtractionKernels *pmSubtractionKernelsAlloc(int numBasisFunctions, pmSubtractionKernelsType type,
    715                                                 int size, int spatialOrder, float penalty, psRegion bounds,
     699                                                int size, psVector *fwhms, psVector *orders, int spatialOrder, float penalty, psRegion bounds,
    716700                                                pmSubtractionMode mode)
    717701{
     
    722706    kernels->description = NULL;
    723707    kernels->num = numBasisFunctions;
     708    kernels->fwhms = psMemIncrRefCounter(fwhms);
     709    kernels->orders = psMemIncrRefCounter(orders);
    724710    kernels->u = psVectorAlloc(numBasisFunctions, PS_TYPE_S32);
    725711    kernels->v = psVectorAlloc(numBasisFunctions, PS_TYPE_S32);
     
    740726    kernels->size = size;
    741727    kernels->inner = 0;
     728    kernels->binning = 0;
     729    kernels->ringsOrder = 0;
    742730    kernels->spatialOrder = spatialOrder;
    743731    kernels->bgOrder = 0;
     
    745733    kernels->solution1 = NULL;
    746734    kernels->solution2 = NULL;
     735    kernels->solution1err = NULL;
     736    kernels->solution2err = NULL;
    747737    kernels->mean = NAN;
    748738    kernels->rms = NAN;
     
    823813    int num = PS_SQR(2 * size + 1) - 1; // Number of basis functions
    824814
    825     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(0, PM_SUBTRACTION_KERNEL_POIS, size,
    826                                                               spatialOrder, penalty, bounds, mode); // Kernels
    827     psStringAppend(&kernels->description, "POIS(%d,%d,%.2e)", size, spatialOrder, penalty);
    828     psLogMsg("psModules.imcombine", PS_LOG_INFO, "POIS kernel: %d,%d --> %d elements",
    829              size, spatialOrder, num);
     815    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(0, PM_SUBTRACTION_KERNEL_POIS, size, NULL, NULL, spatialOrder, penalty, bounds, mode); // Kernels
     816    pmSubtractionKernelsMakeDescription(kernels);
     817    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    830818
    831819    if (!p_pmSubtractionKernelsAddGrid(kernels, 0, size)) {
     
    871859    psTrace("psModules.imcombine", 3, "Number of basis functions: %d\n", num);
    872860
    873     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_SPAM, size,
    874                                                               spatialOrder, penalty, bounds, mode); // Kernels
     861    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_SPAM, size, NULL, NULL, spatialOrder, penalty, bounds, mode); // Kernels
    875862    kernels->inner = inner;
    876     psStringAppend(&kernels->description, "SPAM(%d,%d,%d,%d,%.2e)", size, inner, binning, spatialOrder,
    877                    penalty);
    878 
    879     psLogMsg("psModules.imcombine", PS_LOG_INFO, "SPAM kernel: %d,%d,%d,%d --> %d elements",
    880              size, inner, binning, spatialOrder, num);
     863    kernels->binning = binning;
     864    pmSubtractionKernelsMakeDescription(kernels);
     865    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    881866
    882867    kernels->uStop = psVectorAlloc(num, PS_TYPE_S32);
     
    970955    psTrace("psModules.imcombine", 3, "Number of basis functions: %d\n", num);
    971956
    972     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_FRIES, size,
    973                                                               spatialOrder, penalty, bounds, mode); // Kernels
     957    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_FRIES, size, NULL, NULL, spatialOrder, penalty, bounds, mode); // Kernels
    974958    kernels->inner = inner;
    975     psStringAppend(&kernels->description, "FRIES(%d,%d,%d,%.2e)", size, inner, spatialOrder, penalty);
    976 
    977     psLogMsg("psModules.imcombine", PS_LOG_INFO, "FRIES kernel: %d,%d,%d --> %d elements",
    978              size, inner, spatialOrder, num);
     959    pmSubtractionKernelsMakeDescription(kernels);
     960    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    979961
    980962    kernels->uStop = psVectorAlloc(num, PS_TYPE_S32);
     
    10531035    PS_ASSERT_INT_LESS_THAN(inner, size, NULL);
    10541036
    1055     pmSubtractionKernels *kernels = p_pmSubtractionKernelsRawISIS(size, spatialOrder, fwhms, orders,
    1056                                                                   penalty, bounds, mode); // Kernels
     1037    pmSubtractionKernels *kernels = p_pmSubtractionKernelsRawISIS(size, spatialOrder, fwhms, orders, penalty, bounds, mode); // Kernels
    10571038    kernels->type = PM_SUBTRACTION_KERNEL_GUNK;
    1058     psStringPrepend(&kernels->description, "GUNK=");
    1059     psStringAppend(&kernels->description, "+POIS(%d,%d)", inner, spatialOrder);
     1039    kernels->inner = inner;
     1040    pmSubtractionKernelsMakeDescription(kernels);
     1041    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, (int) kernels->num);
    10601042
    10611043    int numISIS = kernels->num;         // Number of ISIS kernels
     
    11001082    int num = numRings * numPoly; // Total number of basis functions
    11011083
    1102     pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_RINGS, size,
    1103                                                               spatialOrder, penalty, bounds, mode); // Kernels
     1084    pmSubtractionKernels *kernels = pmSubtractionKernelsAlloc(num, PM_SUBTRACTION_KERNEL_RINGS, size, NULL, NULL, spatialOrder, penalty, bounds, mode); // Kernels
    11041085    kernels->inner = inner;
    1105     psStringAppend(&kernels->description, "RINGS(%d,%d,%d,%d,%.2e)", size, inner, ringsOrder, spatialOrder,
    1106                    penalty);
    1107 
    1108     psLogMsg("psModules.imcombine", PS_LOG_INFO, "RINGS kernel: %d,%d,%d,%d --> %d elements",
    1109              size, inner, ringsOrder, spatialOrder, num);
     1086    kernels->ringsOrder = ringsOrder;
     1087    pmSubtractionKernelsMakeDescription(kernels);
     1088    psLogMsg("psModules.imcombine", PS_LOG_INFO, "kernel: %s --> %d elements", kernels->description, num);
    11101089
    11111090    // Set the Gaussian kernel parameters
     
    14051384}
    14061385
     1386bool pmSubtractionKernelsMakeDescription(pmSubtractionKernels *kernels) {
     1387
     1388    // free if it exists
     1389    psFree (kernels->description);
     1390
     1391    // generate the description parameter string
     1392    psString params = NULL;
     1393    if (kernels->fwhms) {
     1394        for (int i = 0; i < kernels->fwhms->n; i++) {
     1395            psStringAppend(&params, "(%.1f,%d)", kernels->fwhms->data.F32[i], kernels->orders->data.S32[i]);
     1396        }
     1397    }
     1398
     1399    switch (kernels->type) {
     1400      case PM_SUBTRACTION_KERNEL_ISIS:
     1401        psStringAppend (&kernels->description, "ISIS(%d,%s,%d,%.2e)", kernels->size, params, kernels->spatialOrder, kernels->penalty);
     1402        break;
     1403
     1404      case PM_SUBTRACTION_KERNEL_ISIS_RADIAL:
     1405        psStringAppend(&kernels->description, "ISIS_RADIAL(%d,%s,%d,%.2e)", kernels->size, params, kernels->spatialOrder, kernels->penalty);
     1406        break;
     1407
     1408      case PM_SUBTRACTION_KERNEL_HERM:
     1409        psStringAppend(&kernels->description, "HERM(%d,%s,%d,%.2e)", kernels->size, params, kernels->spatialOrder, kernels->penalty);
     1410        break;
     1411
     1412      case PM_SUBTRACTION_KERNEL_DECONV_HERM:
     1413        psStringAppend(&kernels->description, "DECONV_HERM(%d,%s,%d,%.2e)", kernels->size, params, kernels->spatialOrder, kernels->penalty);
     1414        break;
     1415
     1416      case PM_SUBTRACTION_KERNEL_POIS:
     1417        psStringAppend(&kernels->description, "POIS(%d,%d,%.2e)", kernels->size, kernels->spatialOrder, kernels->penalty);
     1418        break;
     1419
     1420      case PM_SUBTRACTION_KERNEL_SPAM:
     1421        psStringAppend(&kernels->description, "SPAM(%d,%d,%d,%d,%.2e)", kernels->size, kernels->inner, kernels->binning, kernels->spatialOrder, kernels->penalty);
     1422        break;
     1423
     1424      case PM_SUBTRACTION_KERNEL_FRIES:
     1425        psStringAppend(&kernels->description, "FRIES(%d,%d,%d,%.2e)", kernels->size, kernels->inner, kernels->spatialOrder, kernels->penalty);
     1426        break;
     1427
     1428        // Grid United with Normal Kernel [description: GUNK=ISIS(...)+POIS(...)]
     1429      case PM_SUBTRACTION_KERNEL_GUNK:
     1430        psStringAppend(&kernels->description, "GUNK=ISIS(%d,%s,%d,%.2e)", kernels->size, params, kernels->spatialOrder, kernels->penalty);
     1431        psStringAppend(&kernels->description, "+POIS(%d,%d)", kernels->inner, kernels->spatialOrder);
     1432        break;
     1433       
     1434      case PM_SUBTRACTION_KERNEL_RINGS:
     1435        psStringAppend(&kernels->description, "RINGS(%d,%d,%d,%d,%.2e)", kernels->size, kernels->inner, kernels->ringsOrder, kernels->spatialOrder, kernels->penalty);
     1436        break;
     1437
     1438      default:
     1439        psAbort("unknown kernel");
     1440    }
     1441    psFree (params);
     1442    return true;
     1443}
     1444
    14071445pmSubtractionKernels *pmSubtractionKernelsCopy(const pmSubtractionKernels *in)
    14081446{
     
    14351473    out->solution1 = in->solution1 ? psVectorCopy(NULL, in->solution1, PS_TYPE_F64) : NULL;
    14361474    out->solution2 = in->solution2 ? psVectorCopy(NULL, in->solution2, PS_TYPE_F64) : NULL;
     1475    out->solution1err = in->solution1err ? psVectorCopy(NULL, in->solution1err, PS_TYPE_F64) : NULL;
     1476    out->solution2err = in->solution2err ? psVectorCopy(NULL, in->solution2err, PS_TYPE_F64) : NULL;
    14371477    out->sampleStamps = psMemIncrRefCounter(in->sampleStamps);
    14381478
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionKernels.h

    r29601 r30631  
    22#define PM_SUBTRACTION_KERNELS_H
    33
    4 #include <string.h>
    5 #include <pslib.h>
    6 
    7 /// Type of subtraction kernel
    8 typedef enum {
    9     PM_SUBTRACTION_KERNEL_NONE,         ///< Nothing --- an error
    10     PM_SUBTRACTION_KERNEL_POIS,         ///< Pan-STARRS Optimal Image Subtraction --- delta functions
    11     PM_SUBTRACTION_KERNEL_ISIS,         ///< Traditional kernel --- gaussians modified by polynomials
    12     PM_SUBTRACTION_KERNEL_ISIS_RADIAL,  ///< ISIS + higher-order radial Hermitians
    13     PM_SUBTRACTION_KERNEL_HERM,         ///< Hermitian polynomial kernels
    14     PM_SUBTRACTION_KERNEL_DECONV_HERM,  ///< Deconvolved Hermitian polynomial kernels
    15     PM_SUBTRACTION_KERNEL_SPAM,         ///< Summed Pixels for Advanced Matching --- summed delta functions
    16     PM_SUBTRACTION_KERNEL_FRIES,        ///< Fibonacci Radius Increases Excellence of Subtraction
    17     PM_SUBTRACTION_KERNEL_GUNK,         ///< Grid United with Normal Kernel --- POIS and ISIS hybrid
    18     PM_SUBTRACTION_KERNEL_RINGS,        ///< Rings Instead of the Normal Gaussian Subtraction
    19 } pmSubtractionKernelsType;
    20 
    21 /// Modes --- specifies which image to convolve
    22 typedef enum {
    23     PM_SUBTRACTION_MODE_ERR,            // Error in the mode
    24     PM_SUBTRACTION_MODE_1,              // Convolve image 1
    25     PM_SUBTRACTION_MODE_2,              // Convolve image 2
    26     PM_SUBTRACTION_MODE_UNSURE,         // Not sure yet which image to convolve so try to satisfy both
    27     PM_SUBTRACTION_MODE_DUAL,           // Dual convolution
    28 } pmSubtractionMode;
    29 
    30 /// Kernels specification
    31 typedef struct {
    32     pmSubtractionKernelsType type;      ///< Type of kernels --- allowing the use of multiple kernels
    33     psString description;               ///< Description of the kernel parameters
    34     int xMin, xMax, yMin, yMax;         ///< Bounds of image (for normalisation)
    35     long num;                           ///< Number of kernel components (not including the spatial ones)
    36     psVector *u, *v;                    ///< Offset (for POIS) or polynomial order (for ISIS, HERM or DECONV_HERM)
    37     psVector *widths;                   ///< Gaussian FWHMs (ISIS, HERM or DECONV_HERM)
    38     psVector *uStop, *vStop;            ///< Width of kernel element (SPAM,FRIES only)
    39     psArray *preCalc;                   ///< Array of images containing pre-calculated kernel (for ISIS, HERM or DECONV_HERM)
    40     float penalty;                      ///< Penalty for wideness
    41     psVector *penalties1;               ///< Penalty for each kernel component
    42     psVector *penalties2;               ///< Penalty for each kernel component
    43     bool havePenalties;                 ///< flag to test if we have already calculated the penalties or not.
    44     int size;                           ///< The half-size of the kernel
    45     int inner;                          ///< The size of an inner region
    46     int spatialOrder;                   ///< The spatial order of the kernels
    47     int bgOrder;                        ///< The order for the background fitting
    48     pmSubtractionMode mode;             ///< Mode for subtraction
    49     psVector *solution1, *solution2;    ///< Solution for the PSF matching
    50     // Quality information
    51     float mean, rms;                    ///< Mean and RMS of chi^2 from stamps
    52     int numStamps;                      ///< Number of good stamps
    53     float fResSigmaMean;                ///< mean fractional stdev of residuals
    54     float fResSigmaStdev;               ///< stdev of fractional stdev of residuals
    55     float fResOuterMean;                ///< mean fractional positive swing in residuals
    56     float fResOuterStdev;               ///< stdev of fractional positive swing in residuals
    57     float fResTotalMean;                ///< mean fractional negative swing in residuals
    58     float fResTotalStdev;               ///< stdev of fractional negative swing in residuals
    59     psArray *sampleStamps;              ///< array of brightest set of stamps for output visualizations
    60 } pmSubtractionKernels;
    61 
    62 // pmSubtractionKernels->preCalc is an array of pmSubtractionKernelPreCalc structures
    63 typedef struct {
    64     psVector *uCoords;                  // used by RINGS
    65     psVector *vCoords;                  // used by RINGS
    66     psVector *poly;                     // used by RINGS
    67 
    68     psVector *xKernel;                  // used by ISIS, HERM, DECONV_HERM
    69     psVector *yKernel;                  // used by ISIS, HERM, DECONV_HERM
    70     psKernel *kernel;                   // used by ISIS, HERM, DECONV_HERM
    71 } pmSubtractionKernelPreCalc;
     4// #include <string.h>
     5// #include <pslib.h>
    726
    737// Assertion to check pmSubtractionKernels
     
    16296                                                pmSubtractionKernelsType type, ///< Kernel type
    16397                                                int size, ///< Half-size of kernel
     98                                                psVector *fwhms, ///< requested kernel basis function
     99                                                psVector *orders,
    164100                                                int spatialOrder, ///< Order of spatial variations
    165101                                                float penalty, ///< Penalty for wideness
     
    303239    );
    304240
     241bool pmSubtractionKernelsMakeDescription(pmSubtractionKernels *kernels);
     242
     243
    305244/// Copy kernels
    306245///
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionMask.c

    r27402 r30631  
    77
    88#include "pmErrorCodes.h"
     9#include "pmFPA.h"
     10#include "pmSubtractionTypes.h"
    911#include "pmSubtraction.h"
    1012#include "pmSubtractionKernels.h"
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionMatch.c

    r29596 r30631  
    1111#include "pmFPA.h"
    1212#include "pmHDUUtils.h"
     13#include "pmSubtractionTypes.h"
     14#include "pmSubtraction.h"
    1315#include "pmSubtractionParams.h"
    1416#include "pmSubtractionKernels.h"
    1517#include "pmSubtractionStamps.h"
    1618#include "pmSubtractionEquation.h"
    17 #include "pmSubtraction.h"
    1819#include "pmSubtractionAnalysis.h"
    1920#include "pmSubtractionMask.h"
     
    2728
    2829static bool useFFT = true;              // Do convolutions using FFT
    29 
    30 # define SUBMODE PM_SUBTRACTION_EQUATION_ALL
    3130
    3231//#define TESTING
     
    5958}
    6059
    61 
    62 static bool subtractionGetStamps(pmSubtractionStampList **stamps, // Stamps to read
    63                                  const pmReadout *ro1, // Readout 1
    64                                  const pmReadout *ro2, // Readout 2
    65                                  const psImage *subMask, // Mask for subtraction, or NULL
    66                                  psImage *variance,  // Variance map
    67                                  const psRegion *region, // Region of interest
    68                                  float thresh1,  // Threshold for stamp finding on readout 1
    69                                  float thresh2,  // Threshold for stamp finding on readout 2
    70                                  float stampSpacing, // Spacing between stamps
    71                                  float normFrac,     // Fraction of flux in window for normalisation window
    72                                  float sysError,     // Relative systematic error in images
    73                                  float skyError,     // Relative systematic error in images
    74                                  int size,         // Kernel half-size
    75                                  int footprint,     // Convolution footprint for stamps
    76                                  pmSubtractionMode mode // Mode for subtraction
    77     )
    78 {
    79     PS_ASSERT_PTR_NON_NULL(stamps, false);
    80     PM_ASSERT_READOUT_NON_NULL(ro1, false);
    81     PM_ASSERT_READOUT_NON_NULL(ro2, false);
    82     PS_ASSERT_IMAGE_NON_NULL(subMask, false);
    83     PS_ASSERT_IMAGE_NON_NULL(variance, false);
    84     PS_ASSERT_PTR_NON_NULL(region, false);
    85 
    86     psTrace("psModules.imcombine", 3, "Finding stamps...\n");
    87 
    88     psImage *image1 = ro1 ? ro1->image : NULL, *image2 = ro2 ? ro2->image : NULL; // Images of interest
    89 
    90     *stamps = pmSubtractionStampsFind(*stamps, image1, image2, subMask, region, thresh1, thresh2,
    91                                       size, footprint, stampSpacing, normFrac, sysError, skyError, mode);
    92     if (!*stamps) {
    93         psError(psErrorCodeLast(), false, "Unable to find stamps.");
    94         return false;
    95     }
    96 
    97     memCheck("  find stamps");
    98 
    99     psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
    100     if (!pmSubtractionStampsExtract(*stamps, ro1->image, ro2->image, variance, size, *region)) {
    101         psError(psErrorCodeLast(), false, "Unable to extract stamps.");
    102         return false;
    103     }
    104 
    105     memCheck("   extract stamps");
    106     pmSubtractionVisualPlotStamps(*stamps, (pmReadout *) ro1);
    107     return true;
    108 }
    109 
    11060// Check input arguments
    11161static bool subtractionMatchCheck(pmReadout *conv1, pmReadout *conv2, // Convolved images
     
    12373                                  float badFrac,   // Maximum fraction of bad input pixels to accept
    12474                                  pmSubtractionMode subMode // Mode of subtraction
    125                                   )
     75    )
    12676{
    12777    if (subMode != PM_SUBTRACTION_MODE_2) {
     
    473423}
    474424
     425bool pmSubtractionMatchAttempt(pmSubtractionQuality **bestMatch, pmSubtractionKernels *kernels, pmSubtractionStampList *stamps, pmSubtractionMode mode, int spatialOrder, bool final) {
     426
     427    pmSubtractionMode nativeMode = kernels->mode;
     428    pmSubtractionMode nativeOrder = kernels->spatialOrder;
     429
     430    kernels->mode = mode;
     431    kernels->spatialOrder = spatialOrder;
     432
     433    // we always need to recalculate the matrix equation elements...
     434    pmSubtractionStampsResetStatus(stamps);
     435
     436    psTrace("psModules.imcombine", 3, "Convolving stamps as needed...\n");
     437    if (!pmSubtractionConvolveStamps(stamps, kernels)) {
     438        psError(psErrorCodeLast(), false, "Unable to convolve stamps.");
     439        return false;
     440    }
     441
     442    // step 1: generate the elements of the matrix equation Ax = B
     443    psTrace("psModules.imcombine", 3, "Calculating kernel equations...\n");
     444    if (!pmSubtractionCalculateEquation(stamps, kernels)) {
     445        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     446        return false;
     447    }
     448                   
     449    // step 2: solve the matrix equation Ax = B
     450    psTrace("psModules.imcombine", 3, "Solving kernel equations...\n");
     451    if (!pmSubtractionSolveEquation(kernels, stamps)) {
     452        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     453        return false;
     454    }
     455    memCheck("  solve equation");
     456
     457    // calculate the score for this model fit attempt
     458    // XXX store the chisq, flux and moments for stamp rejection
     459    pmSubtractionCalculateChisqAndMoments(bestMatch, stamps, kernels); // Stamp deviations
     460
     461    // display the input and model stamps
     462    pmSubtractionVisualShowFit(stamps, kernels);
     463    pmSubtractionVisualPlotFit(kernels);
     464    pmSubtractionVisualPlotConvKernels(kernels);
     465
     466    // reset the kernel if desired (on final pass, do not reset)
     467    if (!final) {
     468        kernels->mode = nativeMode;
     469        kernels->spatialOrder = nativeOrder;
     470    } else {
     471      pmSubtractionKernelsMakeDescription(kernels);
     472      psLogMsg("psModules.imcombine", PS_LOG_INFO, "final kernel: %s", kernels->description);
     473    }
     474    return true;
     475}
    475476
    476477bool pmSubtractionMatch(pmReadout *conv1, pmReadout *conv2, const pmReadout *ro1, const pmReadout *ro2,
     
    478479                        const psArray *sources, const char *stampsName,
    479480                        pmSubtractionKernelsType type, int size, int spatialOrder,
    480                         const psVector *isisWidths, const psVector *isisOrders,
     481                        psVector *isisWidths, const psVector *isisOrders,
    481482                        int inner, int ringsOrder, int binning, float penalty,
    482483                        bool optimum, const psVector *optFWHMs, int optOrder, float optThreshold,
     
    559560    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS); // Random number generator
    560561
     562    pmSubtractionQuality *bestMatch = NULL;
     563
     564    int N_TEST_MODES;
     565    int N_TEST_ORDER = spatialOrder;
     566
     567    pmSubtractionMode TestModes[3];
     568    switch (subMode) {
     569      case PM_SUBTRACTION_MODE_1:
     570        N_TEST_MODES = 1;
     571        TestModes[0] = PM_SUBTRACTION_MODE_1;
     572        break;
     573      case PM_SUBTRACTION_MODE_2:
     574        N_TEST_MODES = 1;
     575        TestModes[0] = PM_SUBTRACTION_MODE_2;
     576        break;
     577      case PM_SUBTRACTION_MODE_DUAL:
     578        N_TEST_MODES = 3;
     579        TestModes[0] = PM_SUBTRACTION_MODE_1;
     580        TestModes[1] = PM_SUBTRACTION_MODE_2;
     581        TestModes[2] = PM_SUBTRACTION_MODE_DUAL;
     582        break;
     583      default:
     584        psError(psErrorCodeLast(), false, "For now, only modes 1, 2, and DUAL are supported.");
     585        goto MATCH_ERROR;
     586    }
     587   
    561588    memCheck("start");
    562589
     
    628655            regionString = psRegionToString(*region);
    629656            psLogMsg("psModules.imcombine", PS_LOG_DETAIL, "Iso-kernel region: %s out of %d,%d\n",
    630                     regionString, numCols, numRows);
     657                     regionString, numCols, numRows);
    631658
    632659            if (stampsName && strlen(stampsName) > 0) {
     
    640667            }
    641668
    642             // We get the stamps here; we will also attempt to get stamps at the first iteration, but it
    643             // doesn't matter.
    644             if (!subtractionGetStamps(&stamps, ro1, ro2, subMask, variance, region, stampThresh1, stampThresh2,
    645                                       stampSpacing, normFrac, sysError, skyError, size, footprint, subMode)) {
    646                 goto MATCH_ERROR;
    647             }
    648 
    649 
    650             // generate the window function from the set of stamps
    651             if (!pmSubtractionStampsGetWindow(stamps, size)) {
    652                 psError(psErrorCodeLast(), false, "Unable to get stamp window.");
    653                 goto MATCH_ERROR;
    654             }
    655 
    656             // Define kernel basis functions
    657             if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {
    658                 kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder,
    659                                                           optFWHMs, optOrder, stamps, footprint,
    660                                                           optThreshold, penalty, bounds, subMode);
    661                 if (!kernels) {
    662                     psErrorClear();
    663                     psWarning("Unable to derive optimum ISIS kernel --- switching to default.");
    664                 }
    665             }
    666             if (kernels == NULL) {
    667                 // Not an ISIS/GUNK kernel, or the optimum kernel search failed
    668                 kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,
    669                                                        inner, binning, ringsOrder, penalty, bounds, subMode);
    670             }
    671 
    672             memCheck("kernels");
    673 
    674             if (subMode == PM_SUBTRACTION_MODE_UNSURE) {
    675                 pmSubtractionMode newMode = pmSubtractionBestMode(&stamps, &kernels, subMask, rej);
    676                 switch (newMode) {
    677                   case PM_SUBTRACTION_MODE_1:
    678                     psLogMsg("psModules.imcombine", PS_LOG_INFO, "Convolving image 1 to match image 2.");
    679                     break;
    680                   case PM_SUBTRACTION_MODE_2:
    681                     psLogMsg("psModules.imcombine", PS_LOG_INFO, "Convolving image 2 to match image 1.");
    682                     break;
    683                   default:
    684                     psError(psErrorCodeLast(), false, "Unable to determine subtraction order.");
    685                     goto MATCH_ERROR;
    686                 }
    687                 subMode = newMode;
    688             }
    689 
    690             int numRejected = -1;       // Number of rejected stamps in each iteration
    691             for (int k = 0; k < iter && numRejected != 0; k++) {
    692                 psLogMsg("psModules.imcombine", PS_LOG_INFO, "Iteration %d.", k);
    693 
    694                 if (!subtractionGetStamps(&stamps, ro1, ro2, subMask, variance, region,
    695                                           stampThresh1, stampThresh2, stampSpacing, normFrac,
    696                                           sysError, skyError, size, footprint, subMode)) {
    697                     goto MATCH_ERROR;
    698                 }
    699 
    700                 // generate the window function from the set of stamps
    701                 if (!pmSubtractionStampsGetWindow(stamps, size)) {
    702                     psError(psErrorCodeLast(), false, "Unable to get stamps window.");
    703                     goto MATCH_ERROR;
    704                 }
     669            bool tryAgain = true;
     670            while (tryAgain) {
     671                // We get the stamps here; we will also attempt to get stamps at the first iteration, but it
     672                // doesn't matter.
     673                if (!pmSubtractionStampsSelect(&stamps, ro1, ro2, subMask, variance, region, stampThresh1, stampThresh2,
     674                                               stampSpacing, normFrac, sysError, skyError, size, footprint, subMode)) {
     675                    goto MATCH_ERROR;
     676                }
     677
     678                // generate the window function from the set of stamps
     679                if (!pmSubtractionStampsGetWindow(&tryAgain, stamps, size)) {
     680                    // if we failed, it might be due to the desired normWindow being larger than the current footprint.
     681                    // in this case, just adjust the footprint and try again.
     682                    if (tryAgain) {
     683                        // keep the border constant
     684                        int border = footprint - size;
     685                        size = PS_MAX(stamps->normWindow1, stamps->normWindow2) + 2;
     686                        footprint = size + border;
     687
     688                        // we need to reconstruct everything, so just free the stamps here and retry
     689                        psFree(stamps);
     690                    } else {
     691                        // unrecoverable error
     692                        psError(psErrorCodeLast(), false, "Unable to get stamp window.");
     693                        goto MATCH_ERROR;
     694                    }
     695                }
     696            }
     697
     698            // check on the kernel scaling -- if the kron-based radial moments are very different, adjust to match them
     699            {
     700                // float fwhm1;
     701                // float fwhm2;
     702                // pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
     703                // psAssert(isfinite(fwhm1), "fwhm 1 not set");
     704                // psAssert(isfinite(fwhm2), "fwhm 2 not set");
     705
     706                // XXX this is BAD: depends on the relationship below:
     707                // stamps->normWindow1 = 2.75*R1;
     708                // stamps->normWindow2 = 2.75*R2;
     709                float radMoment1 = stamps->normWindow1 / 2.75;
     710                float radMoment2 = stamps->normWindow2 / 2.75;
     711                pmSubtractionParamsScale(NULL, NULL, isisWidths, radMoment1, radMoment2);
     712
     713                // float maxFWHM = PS_MAX(fwhm1, fwhm2);
     714                // float maxRadial = PS_MAX(radMoment1, radMoment2);
     715               
     716                // if (fabs(2.0*(maxFWHM - maxRadial)/(maxFWHM + maxRadial)) > 0.25) {
     717                // if (1) {
     718                //
     719                //     float scale = maxRadial / maxFWHM;
     720                //     psLogMsg ("psModules.imcombine", PS_LOG_INFO, "Kron and FWHM scales are quite different, re-scale by %f to use Kron", scale);
     721                //     
     722                //     for (int i = 0; i < isisWidths->n; i++) {
     723                //      isisWidths->data.F32[i] *= scale;
     724                //     }
     725                // }
     726            }
     727
     728            // Define kernel basis functions
     729            if (optimum && (type == PM_SUBTRACTION_KERNEL_ISIS || type == PM_SUBTRACTION_KERNEL_GUNK)) {
     730                kernels = pmSubtractionKernelsOptimumISIS(type, size, inner, spatialOrder,
     731                                                          optFWHMs, optOrder, stamps, footprint,
     732                                                          optThreshold, penalty, bounds, subMode);
     733                if (!kernels) {
     734                    psErrorClear();
     735                    psWarning("Unable to derive optimum ISIS kernel --- switching to default.");
     736                }
     737            }
     738            if (kernels == NULL) {
     739                // Not an ISIS/GUNK kernel, or the optimum kernel search failed
     740                kernels = pmSubtractionKernelsGenerate(type, size, spatialOrder, isisWidths, isisOrders,
     741                                                       inner, binning, ringsOrder, penalty, bounds, subMode);
     742            }
     743
     744            memCheck("kernels");
     745
     746            if (subMode == PM_SUBTRACTION_MODE_UNSURE) {
     747                pmSubtractionMode newMode = pmSubtractionBestMode(&stamps, &kernels, subMask, rej);
     748                switch (newMode) {
     749                  case PM_SUBTRACTION_MODE_1:
     750                    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Convolving image 1 to match image 2.");
     751                    break;
     752                  case PM_SUBTRACTION_MODE_2:
     753                    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Convolving image 2 to match image 1.");
     754                    break;
     755                  default:
     756                    psError(psErrorCodeLast(), false, "Unable to determine subtraction order.");
     757                    goto MATCH_ERROR;
     758                }
     759                subMode = newMode;
     760            }
     761
     762            int numRejected = -1;       // Number of rejected stamps in each iteration
     763            for (int k = 0; (k < iter) && (numRejected != 0); k++) {
     764                psLogMsg("psModules.imcombine", PS_LOG_INFO, "Iteration %d.", k);
     765
     766                bool tryAgain = true;
     767                while (tryAgain) {
     768                    // We get the stamps here; we will also attempt to get stamps at the first iteration, but it
     769                    // doesn't matter.
     770                    if (!pmSubtractionStampsSelect(&stamps, ro1, ro2, subMask, variance, region, stampThresh1, stampThresh2,
     771                                                   stampSpacing, normFrac, sysError, skyError, size, footprint, subMode)) {
     772                        goto MATCH_ERROR;
     773                    }
     774
     775                    // generate the window function from the set of stamps
     776                    if (!pmSubtractionStampsGetWindow(&tryAgain, stamps, size)) {
     777                        // if we failed, it might be due to the desired normWindow being larger than the current footprint.
     778                        // in this case, just adjust the footprint and try again.
     779                        if (tryAgain) {
     780                            footprint = PS_MAX(stamps->normWindow1, stamps->normWindow2) + 2;
     781
     782                            // we need to reconstruct everything, so just free the stamps here and retry
     783                            psFree(stamps);
     784                        } else {
     785                            // unrecoverable error
     786                            psError(psErrorCodeLast(), false, "Unable to get stamp window.");
     787                            goto MATCH_ERROR;
     788                        }
     789                    }
     790                }
    705791
    706792                // step 0 : calculate the normalizations, pass along to the next steps via stamps->normValue
    707                 psTrace("psModules.imcombine", 3, "Calculating normalization...\n");
    708                 if (!pmSubtractionCalculateNormalization(stamps, kernels->mode)) {
    709                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    710                     goto MATCH_ERROR;
    711                 }
    712 
    713                 // step 0b : calculate the moments, pass along to the next steps via stamps->normValue
    714                 // XXX this step is now skipped -- delete
    715                 psTrace("psModules.imcombine", 3, "Calculating normalization...\n");
    716                 if (!pmSubtractionCalculateMoments(kernels, stamps)) {
    717                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    718                     goto MATCH_ERROR;
    719                 }
    720 
    721                 // step 1: generate the elements of the matrix equation Ax = B
    722                 psTrace("psModules.imcombine", 3, "Calculating kernel equations...\n");
    723                 if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
    724                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    725                     goto MATCH_ERROR;
    726                 }
    727 
    728                 // step 2: solve the matrix equation Ax = B
    729                 psTrace("psModules.imcombine", 3, "Solving kernel equations...\n");
    730                 if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
    731                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    732                     goto MATCH_ERROR;
    733                 }
    734                 memCheck("  solve equation");
    735 
    736                 pmSubtractionVisualPlotConvKernels(kernels);
    737 
    738                 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
    739                 if (!deviations) {
    740                     psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
    741                     goto MATCH_ERROR;
    742                 }
    743                 memCheck("   calculate deviations");
    744 
    745                 psTrace("psModules.imcombine", 3, "Rejecting stamps...\n");
    746                 numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej);
    747                 if (numRejected < 0) {
    748                     psError(psErrorCodeLast(), false, "Unable to reject stamps.");
    749                     psFree(deviations);
    750                     goto MATCH_ERROR;
    751                 }
    752                 psFree(deviations);
    753 
    754                 memCheck("  reject stamps");
    755             }
    756 
    757             // if we hit the max number of iterations and we have rejected stamps, re-solve
    758             if (numRejected > 0) {
    759 
    760                 // step 1: generate the elements of the matrix equation Ax = B
    761                 psTrace("psModules.imcombine", 3, "Calculating equation for normalization...\n");
    762                 if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
    763                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    764                     goto MATCH_ERROR;
    765                 }
    766 
    767                 // step 2: solve the matrix equation Ax = B
    768                 psTrace("psModules.imcombine", 3, "Solving equation for kernels...\n");
    769                 if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
    770                     psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    771                     goto MATCH_ERROR;
    772                 }
    773 
    774                 pmSubtractionVisualPlotConvKernels(kernels);
    775 
    776                 psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
    777                 if (!deviations) {
    778                     psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
    779                     goto MATCH_ERROR;
    780                 }
    781                 pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN);
    782                 psFree(deviations);
    783             }
    784             psFree(stamps);
    785             stamps = NULL;
    786 
    787             memCheck("solution");
    788 
    789             if (!pmSubtractionAnalysis(analysis, header, kernels, region, numCols, numRows)) {
    790                 psError(psErrorCodeLast(), false, "Unable to generate QA data");
    791                 goto MATCH_ERROR;
    792             }
    793             memCheck("diag outputs");
    794 
    795             psTrace("psModules.imcombine", 2, "Convolving...\n");
    796             if (!pmSubtractionConvolve(conv1, conv2, ro1, ro2, subMask, stride, maskBad, maskPoor, poorFrac,
    797                                        kernelError, covarFrac, region, kernels, true, useFFT)) {
    798                 psError(psErrorCodeLast(), false, "Unable to convolve image.");
    799                 goto MATCH_ERROR;
    800             }
    801 
    802             psFree(kernels);
    803             kernels = NULL;
    804         }
     793                psTrace("psModules.imcombine", 3, "Calculating normalization...\n");
     794                if (!pmSubtractionCalculateNormalization(stamps, kernels->mode)) {
     795                    psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     796                    goto MATCH_ERROR;
     797                }
     798
     799                // on each iteration, we start from scratch
     800                psFree(bestMatch);
     801
     802                // choose the spatial order and subtraction direction (1, 2, dual)
     803                // XXX need to make these respect recipe somewhat
     804                for (int order = 0; order <= N_TEST_ORDER; order++) {
     805                    for (int j = 0; j < N_TEST_MODES; j++) {
     806                        if (!pmSubtractionMatchAttempt(&bestMatch, kernels, stamps, TestModes[j], order, false)) {
     807                            goto MATCH_ERROR;
     808                        }
     809                    }
     810                }
     811               
     812                // reject the deviant stamps based on the stats of the best match
     813                psTrace("psModules.imcombine", 3, "Rejecting stamps...\n");
     814                numRejected = pmSubtractionRejectStamps(kernels, stamps, bestMatch, subMask, rej);
     815                if (numRejected < 0) {
     816                    psError(psErrorCodeLast(), false, "Unable to reject stamps.");
     817                    goto MATCH_ERROR;
     818                }
     819                memCheck("  reject stamps");
     820            }
     821
     822            // apply the best fit so we are ready to roll
     823            psLogMsg("psModules.imcombine", PS_LOG_INFO, "applying order: %d, mode: %d\n", bestMatch->spatialOrder, bestMatch->mode);
     824            if (!pmSubtractionMatchAttempt(NULL, kernels, stamps, bestMatch->mode, bestMatch->spatialOrder, true)) {
     825                goto MATCH_ERROR;
     826            }
     827            psFree(stamps);
     828            psFree(bestMatch);
     829            memCheck("solution");
     830
     831            if (!pmSubtractionAnalysis(analysis, header, kernels, region, numCols, numRows)) {
     832                psError(psErrorCodeLast(), false, "Unable to generate QA data");
     833                goto MATCH_ERROR;
     834            }
     835            memCheck("diag outputs");
     836
     837            psTrace("psModules.imcombine", 2, "Convolving...\n");
     838            if (!pmSubtractionConvolve(conv1, conv2, ro1, ro2, subMask, stride, maskBad, maskPoor, poorFrac,
     839                                       kernelError, covarFrac, region, kernels, true, useFFT)) {
     840                psError(psErrorCodeLast(), false, "Unable to convolve image.");
     841                goto MATCH_ERROR;
     842            }
     843
     844            psFree(kernels);
     845            kernels = NULL;
     846        }
    805847    }
    806848    psFree(rng);
     
    816858
    817859    if (conv1 && !pmSubtractionBorder(conv1->image, conv1->variance, conv1->mask, size, maskBad)) {
    818         psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
    819         goto MATCH_ERROR;
     860        psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
     861        goto MATCH_ERROR;
    820862    }
    821863    if (conv2 && !pmSubtractionBorder(conv2->image, conv2->variance, conv2->mask, size, maskBad)) {
    822         psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
    823         goto MATCH_ERROR;
     864        psError(psErrorCodeLast(), false, "Unable to set border of convolved image.");
     865        goto MATCH_ERROR;
    824866    }
    825867
     
    832874#ifdef TESTING
    833875    {
    834         if (subMode == PM_SUBTRACTION_MODE_1 || subMode == PM_SUBTRACTION_MODE_DUAL) {
    835             psFits *fits = psFitsOpen("convolved1.fits", "w");
    836             psFitsWriteImage(fits, NULL, conv1->image, 0, NULL);
    837             psFitsClose(fits);
    838         }
    839 
    840         if (subMode == PM_SUBTRACTION_MODE_2 || subMode == PM_SUBTRACTION_MODE_DUAL) {
    841             psFits *fits = psFitsOpen("convolved2.fits", "w");
    842             psFitsWriteImage(fits, NULL, conv2->image, 0, NULL);
    843             psFitsClose(fits);
    844         }
     876        if (subMode == PM_SUBTRACTION_MODE_1 || subMode == PM_SUBTRACTION_MODE_DUAL) {
     877            psFits *fits = psFitsOpen("convolved1.fits", "w");
     878            psFitsWriteImage(fits, NULL, conv1->image, 0, NULL);
     879            psFitsClose(fits);
     880        }
     881
     882        if (subMode == PM_SUBTRACTION_MODE_2 || subMode == PM_SUBTRACTION_MODE_DUAL) {
     883            psFits *fits = psFitsOpen("convolved2.fits", "w");
     884            psFitsWriteImage(fits, NULL, conv2->image, 0, NULL);
     885            psFitsClose(fits);
     886        }
    845887    }
    846888#endif
     
    858900    psFree(variance);
    859901    psFree(rng);
     902    psFree(bestMatch);
    860903    return false;
    861904}
     
    866909// increment).
    867910static int subtractionOrderWidth(const psKernel *kernel, // Image
    868                                 float bg, // Background in image
    869                                 int size, // Maximum size
    870                                 const psArray *models, // Buffer of models
    871                                 const psVector *modelSums // Buffer of model sums
     911                                float bg, // Background in image
     912                                int size, // Maximum size
     913                                const psArray *models, // Buffer of models
     914                                const psVector *modelSums // Buffer of model sums
    872915    )
    873916{
     
    882925    psVector *chi2 = psVectorAlloc(size, PS_TYPE_F32); // chi^2 as a function of radius
    883926    for (int sigma = 0; sigma < size; sigma++) {
    884         double sumFG = 0.0; // Sum for calculating the normalisation of the Gaussian
    885         psKernel *model = models->data[sigma]; // Model of interest
    886         for (int y = yMin; y <= yMax; y++) {
    887             for (int x = xMin; x <= xMax; x++) {
    888                 sumFG += model->kernel[y][x] * (kernel->kernel[y][x] - bg);
    889             }
    890         }
    891         float norm = sumFG * modelSums->data.F64[sigma]; // Normalisation for Gaussian
    892         double sumDev2 = 0.0;           // Sum of square deviations
    893         for (int y = yMin; y <= yMax; y++) {
    894             for (int x = xMin; x <= xMax; x++) {
    895                 float dev = kernel->kernel[y][x] - bg - norm * model->kernel[y][x]; // Deviation
    896                 sumDev2 += PS_SQR(dev);
    897             }
    898         }
    899         chi2->data.F32[sigma] = sumDev2;
     927        double sumFG = 0.0; // Sum for calculating the normalisation of the Gaussian
     928        psKernel *model = models->data[sigma]; // Model of interest
     929        for (int y = yMin; y <= yMax; y++) {
     930            for (int x = xMin; x <= xMax; x++) {
     931                sumFG += model->kernel[y][x] * (kernel->kernel[y][x] - bg);
     932            }
     933        }
     934        float norm = sumFG * modelSums->data.F64[sigma]; // Normalisation for Gaussian
     935        double sumDev2 = 0.0;           // Sum of square deviations
     936        for (int y = yMin; y <= yMax; y++) {
     937            for (int x = xMin; x <= xMax; x++) {
     938                float dev = kernel->kernel[y][x] - bg - norm * model->kernel[y][x]; // Deviation
     939                sumDev2 += PS_SQR(dev);
     940            }
     941        }
     942        chi2->data.F32[sigma] = sumDev2;
    900943    }
    901944
     
    904947    float bestChi2 = INFINITY;          // Best chi^2
    905948    for (int i = 0; i < size; i++) {
    906         if (chi2->data.F32[i] < bestChi2) {
    907             bestChi2 = chi2->data.F32[i];
    908             bestIndex = i;
    909         }
     949        if (chi2->data.F32[i] < bestChi2) {
     950            bestChi2 = chi2->data.F32[i];
     951            bestIndex = i;
     952        }
    910953    }
    911954    psFree(chi2);
     
    916959
    917960bool pmSubtractionOrderStamp(psVector *ratios, psVector *mask, const pmSubtractionStampList *stamps,
    918                              const psArray *models, const psVector *modelSums,
    919                              int index, float bg1, float bg2)
     961                             const psArray *models, const psVector *modelSums,
     962                             int index, float bg1, float bg2)
    920963{
    921964    PS_ASSERT_VECTOR_NON_NULL(ratios, false);
     
    931974    pmSubtractionStamp *stamp = stamps->stamps->data[index]; // Stamp of interest
    932975    psAssert(stamp->status == PM_SUBTRACTION_STAMP_CALCULATE || stamp->status == PM_SUBTRACTION_STAMP_USED,
    933              "We checked this earlier.");
     976             "We checked this earlier.");
    934977
    935978    // Widths of stars
     
    938981
    939982    if (width1 == 0 || width2 == 0) {
    940         ratios->data.F32[index] = NAN;
    941         mask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0xff;
     983        ratios->data.F32[index] = NAN;
     984        mask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0xff;
    942985    } else {
    943         ratios->data.F32[index] = (float)width1 / (float)width2;
    944         mask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0;
    945         psTrace("psModules.imcombine", 3, "Stamp %d (%.1f,%.1f) widths: %d, %d --> %f\n",
    946                 index, stamp->x, stamp->y, width1, width2, ratios->data.F32[index]);
     986        ratios->data.F32[index] = (float)width1 / (float)width2;
     987        mask->data.PS_TYPE_VECTOR_MASK_DATA[index] = 0;
     988        psTrace("psModules.imcombine", 3, "Stamp %d (%.1f,%.1f) widths: %d, %d --> %f\n",
     989                index, stamp->x, stamp->y, width1, width2, ratios->data.F32[index]);
    947990    }
    948991
     
    9781021    psVector *modelSums = psVectorAlloc(size, PS_TYPE_F64); // Gaussian model sums
    9791022    for (int sigma = 0; sigma < size; sigma++) {
    980         psKernel *model = psKernelAlloc(-size, size, -size, size); // Gaussian model
    981         float invSigma2 = 1.0 / (float)PS_SQR(1 + sigma); // Inverse sigma squared
    982         double sumGG = 0.0;         // Sum of square of Gaussian
    983         for (int y = -size; y <= size; y++) {
    984             int y2 = PS_SQR(y);     // y squared
    985             for (int x = -size; x <= size; x++) {
    986                 float rad2 = PS_SQR(x) + y2; // Radius squared
    987                 float value = expf(-rad2 * invSigma2); // Model value
    988                 model->kernel[y][x] = value;
    989                 sumGG += PS_SQR(value);
    990             }
    991         }
    992         models->data[sigma] = model;
    993         modelSums->data.F64[sigma] = 1.0 / sumGG;
     1023        psKernel *model = psKernelAlloc(-size, size, -size, size); // Gaussian model
     1024        float invSigma2 = 1.0 / (float)PS_SQR(1 + sigma); // Inverse sigma squared
     1025        double sumGG = 0.0;         // Sum of square of Gaussian
     1026        for (int y = -size; y <= size; y++) {
     1027            int y2 = PS_SQR(y);     // y squared
     1028            for (int x = -size; x <= size; x++) {
     1029                float rad2 = PS_SQR(x) + y2; // Radius squared
     1030                float value = expf(-rad2 * invSigma2); // Model value
     1031                model->kernel[y][x] = value;
     1032                sumGG += PS_SQR(value);
     1033            }
     1034        }
     1035        models->data[sigma] = model;
     1036        modelSums->data.F64[sigma] = 1.0 / sumGG;
    9941037    }
    9951038
    9961039    // Fit models to stamps
    9971040    for (int i = 0; i < stamps->num; i++) {
    998         pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
    999         if (stamp->status != PM_SUBTRACTION_STAMP_CALCULATE && stamp->status != PM_SUBTRACTION_STAMP_USED) {
    1000             mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = 0xff;
    1001             continue;
    1002         }
    1003 
    1004         if (pmSubtractionThreaded()) {
    1005             psThreadJob *job = psThreadJobAlloc("PSMODULES_SUBTRACTION_ORDER");
    1006             psArrayAdd(job->args, 1, ratios);
    1007             psArrayAdd(job->args, 1, mask);
    1008             psArrayAdd(job->args, 1, stamps);
    1009             psArrayAdd(job->args, 1, models);
    1010             psArrayAdd(job->args, 1, modelSums);
    1011             PS_ARRAY_ADD_SCALAR(job->args, i, PS_TYPE_S32);
    1012             PS_ARRAY_ADD_SCALAR(job->args, bg1, PS_TYPE_F32);
    1013             PS_ARRAY_ADD_SCALAR(job->args, bg2, PS_TYPE_F32);
    1014             if (!psThreadJobAddPending(job)) {
    1015                 return false;
    1016             }
    1017         } else {
    1018             if (!pmSubtractionOrderStamp(ratios, mask, stamps, models, modelSums, i, bg1, bg2)) {
    1019                 psError(psErrorCodeLast(), false, "Unable to measure PSF width for stamp %d", i);
    1020                 psFree(models);
    1021                 psFree(modelSums);
    1022                 psFree(ratios);
    1023                 psFree(mask);
    1024                 return false;
    1025             }
    1026         }
     1041        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // Stamp of interest
     1042        if (stamp->status != PM_SUBTRACTION_STAMP_CALCULATE && stamp->status != PM_SUBTRACTION_STAMP_USED) {
     1043            mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = 0xff;
     1044            continue;
     1045        }
     1046
     1047        if (pmSubtractionThreaded()) {
     1048            psThreadJob *job = psThreadJobAlloc("PSMODULES_SUBTRACTION_ORDER");
     1049            psArrayAdd(job->args, 1, ratios);
     1050            psArrayAdd(job->args, 1, mask);
     1051            psArrayAdd(job->args, 1, stamps);
     1052            psArrayAdd(job->args, 1, models);
     1053            psArrayAdd(job->args, 1, modelSums);
     1054            PS_ARRAY_ADD_SCALAR(job->args, i, PS_TYPE_S32);
     1055            PS_ARRAY_ADD_SCALAR(job->args, bg1, PS_TYPE_F32);
     1056            PS_ARRAY_ADD_SCALAR(job->args, bg2, PS_TYPE_F32);
     1057            if (!psThreadJobAddPending(job)) {
     1058                return false;
     1059            }
     1060        } else {
     1061            if (!pmSubtractionOrderStamp(ratios, mask, stamps, models, modelSums, i, bg1, bg2)) {
     1062                psError(psErrorCodeLast(), false, "Unable to measure PSF width for stamp %d", i);
     1063                psFree(models);
     1064                psFree(modelSums);
     1065                psFree(ratios);
     1066                psFree(mask);
     1067                return false;
     1068            }
     1069        }
    10271070    }
    10281071
    10291072    if (!psThreadPoolWait(true)) {
    1030         psError(psErrorCodeLast(), false, "Error waiting for threads.");
    1031         psFree(models);
    1032         psFree(modelSums);
    1033         psFree(ratios);
    1034         psFree(mask);
    1035             return false;
     1073        psError(psErrorCodeLast(), false, "Error waiting for threads.");
     1074        psFree(models);
     1075        psFree(modelSums);
     1076        psFree(ratios);
     1077        psFree(mask);
     1078        return false;
    10361079    }
    10371080
     
    10411084    psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN);
    10421085    if (!psVectorStats(stats, ratios, NULL, mask, 0xff)) {
    1043         psError(psErrorCodeLast(), false, "Unable to calculate statistics for moments ratio.");
    1044         psFree(mask);
    1045         psFree(ratios);
    1046         psFree(stats);
    1047         return PM_SUBTRACTION_MODE_ERR;
     1086        psError(psErrorCodeLast(), false, "Unable to calculate statistics for moments ratio.");
     1087        psFree(mask);
     1088        psFree(ratios);
     1089        psFree(stats);
     1090        return PM_SUBTRACTION_MODE_ERR;
    10481091    }
    10491092    psFree(ratios);
     
    10521095    // XXX raise an error here or not?
    10531096    if (isnan(stats->robustMedian)) {
    1054         psFree(stats);
    1055         return PM_SUBTRACTION_MODE_ERR;
     1097        psFree(stats);
     1098        return PM_SUBTRACTION_MODE_ERR;
    10561099    }
    10571100
     
    10661109// Test a subtraction mode by performing a single iteration
    10671110static bool subtractionModeTest(pmSubtractionStampList *stamps, // Stamps to use to find best mode
    1068                                 pmSubtractionKernels *kernels, // Kernel description
    1069                                 const char *description, // Description for trace
    1070                                 psImage *subMask,  // Subtraction mask
    1071                                 float rej               // Rejection threshold
    1072                                 )
     1111                                pmSubtractionKernels *kernels, // Kernel description
     1112                                const char *description, // Description for trace
     1113                                psImage *subMask,  // Subtraction mask
     1114                                float rej               // Rejection threshold
     1115    )
    10731116{
    10741117    assert(stamps);
    10751118    assert(kernels);
    10761119
     1120    psAbort("this function is not working");
     1121# if (0)
     1122    psTrace("psModules.imcombine", 3, "Convolving stamps as needed...\n");
     1123    if (!pmSubtractionConvolveStamps(stamps, kernels)) {
     1124        psError(psErrorCodeLast(), false, "Unable to convolve stamps.");
     1125        return false;
     1126    }
     1127
    10771128    psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
    1078     if (!pmSubtractionCalculateEquation(stamps, kernels, SUBMODE)) {
    1079         psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    1080         return false;
     1129    if (!pmSubtractionCalculateEquation(stamps, kernels)) {
     1130        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     1131        return false;
    10811132    }
    10821133
    10831134    psTrace("psModules.imcombine", 3, "Solving %s normalization equation...\n", description);
    1084     if (!pmSubtractionSolveEquation(kernels, stamps, SUBMODE)) {
    1085         psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    1086         return false;
     1135    if (!pmSubtractionSolveEquation(kernels, stamps)) {
     1136        psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     1137        return false;
    10871138    }
    10881139
     
    10901141    psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
    10911142    if (!deviations) {
    1092         psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
    1093         return false;
    1094     }
    1095 
     1143        psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
     1144        return false;
     1145    }
     1146
     1147    // XXX this needs to be made consistent with the modified 'reject stamps' function
    10961148    psTrace("psModules.imcombine", 3, "Rejecting %s stamps...\n", description);
    10971149    long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, rej);
    10981150    if (numRejected < 0) {
    1099         psError(psErrorCodeLast(), false, "Unable to reject stamps.");
    1100         psFree(deviations);
    1101         return false;
     1151        psError(psErrorCodeLast(), false, "Unable to reject stamps.");
     1152        psFree(deviations);
     1153        return false;
    11021154    }
    11031155    psFree(deviations);
    11041156
    11051157    if (numRejected > 0) {
    1106         // Allow re-fit with reduced stamps set
    1107         psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
    1108         if (!pmSubtractionCalculateEquation(stamps, kernels, PM_SUBTRACTION_EQUATION_ALL)) {
    1109             psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    1110             return false;
    1111         }
    1112 
    1113         psTrace("psModules.imcombine", 3, "Resolving %s equation...\n", description);
    1114         if (!pmSubtractionSolveEquation(kernels, stamps, PM_SUBTRACTION_EQUATION_ALL)) {
    1115             psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
    1116             return false;
    1117         }
    1118         psTrace("psModules.imcombine", 3, "Recalculate %s deviations...\n", description);
    1119 
    1120         psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
    1121         if (!deviations) {
    1122             psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
    1123             return false;
    1124         }
    1125         psTrace("psModules.imcombine", 3, "Measuring %s quality...\n", description);
    1126         long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN);
    1127         if (numRejected < 0) {
    1128             psError(psErrorCodeLast(), false, "Unable to reject stamps.");
    1129             psFree(deviations);
    1130             return false;
    1131         }
    1132         psFree(deviations);
    1133     }
    1134 
     1158        // Allow re-fit with reduced stamps set
     1159        psTrace("psModules.imcombine", 3, "Calculating %s normalization equation...\n", description);
     1160        if (!pmSubtractionCalculateEquation(stamps, kernels)) {
     1161            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     1162            return false;
     1163        }
     1164
     1165        psTrace("psModules.imcombine", 3, "Resolving %s equation...\n", description);
     1166        if (!pmSubtractionSolveEquation(kernels, stamps)) {
     1167            psError(psErrorCodeLast(), false, "Unable to calculate least-squares equation.");
     1168            return false;
     1169        }
     1170        psTrace("psModules.imcombine", 3, "Recalculate %s deviations...\n", description);
     1171
     1172        psVector *deviations = pmSubtractionCalculateDeviations(stamps, kernels); // Stamp deviations
     1173        if (!deviations) {
     1174            psError(psErrorCodeLast(), false, "Unable to calculate deviations.");
     1175            return false;
     1176        }
     1177        psTrace("psModules.imcombine", 3, "Measuring %s quality...\n", description);
     1178        long numRejected = pmSubtractionRejectStamps(kernels, stamps, deviations, subMask, NAN);
     1179        if (numRejected < 0) {
     1180            psError(psErrorCodeLast(), false, "Unable to reject stamps.");
     1181            psFree(deviations);
     1182            return false;
     1183        }
     1184        psFree(deviations);
     1185    }
     1186# endif
    11351187    return true;
    11361188}
     
    11381190
    11391191pmSubtractionMode pmSubtractionBestMode(pmSubtractionStampList **stamps, pmSubtractionKernels **kernels,
    1140                                         const psImage *subMask, float rej)
     1192                                        const psImage *subMask, float rej)
    11411193{
    11421194    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(*stamps, PM_SUBTRACTION_MODE_ERR);
     
    11501202
    11511203    if (!subtractionModeTest(stamps1, kernels1, "convolve 1", subMask1, rej)) {
    1152         psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 1");
    1153         psFree(stamps1);
    1154         psFree(kernels1);
    1155         psFree(subMask1);
    1156         return PM_SUBTRACTION_MODE_ERR;
     1204        psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 1");
     1205        psFree(stamps1);
     1206        psFree(kernels1);
     1207        psFree(subMask1);
     1208        return PM_SUBTRACTION_MODE_ERR;
    11571209    }
    11581210    psFree(subMask1);
     
    11651217
    11661218    if (!subtractionModeTest(stamps2, kernels2, "convolve 2", subMask2, rej)) {
    1167         psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 2");
    1168         psFree(stamps2);
    1169         psFree(kernels2);
    1170         psFree(subMask2);
    1171         psFree(stamps1);
    1172         psFree(kernels1);
    1173         return PM_SUBTRACTION_MODE_ERR;
     1219        psError(psErrorCodeLast(), false, "Unable to test subtraction with convolution of image 2");
     1220        psFree(stamps2);
     1221        psFree(kernels2);
     1222        psFree(subMask2);
     1223        psFree(stamps1);
     1224        psFree(kernels1);
     1225        return PM_SUBTRACTION_MODE_ERR;
    11741226    }
    11751227    psFree(subMask2);
     
    11791231    pmSubtractionKernels *bestKernels = NULL; // Best choice for kernels
    11801232    psLogMsg("psModules.imcombine", PS_LOG_INFO,
    1181              "Image 1: %f +/- %f from %d stamps\nImage 2: %f +/- %f from %d stamps\n",
    1182              kernels1->mean, kernels1->rms, kernels1->numStamps,
    1183              kernels2->mean, kernels2->rms, kernels2->numStamps);
     1233             "Image 1: %f +/- %f from %d stamps\nImage 2: %f +/- %f from %d stamps\n",
     1234             kernels1->mean, kernels1->rms, kernels1->numStamps,
     1235             kernels2->mean, kernels2->rms, kernels2->numStamps);
    11841236
    11851237    if (kernels1->mean < kernels2->mean) {
    1186         bestStamps = stamps1;
    1187         bestKernels = kernels1;
     1238        bestStamps = stamps1;
     1239        bestKernels = kernels1;
    11881240    } else {
    1189         bestStamps = stamps2;
    1190         bestKernels = kernels2;
     1241        bestStamps = stamps2;
     1242        bestKernels = kernels2;
    11911243    }
    11921244
     
    12041256}
    12051257
    1206 
    1207 bool pmSubtractionParamsScale(int *kernelSize, int *stampSize, psVector *widths,
    1208                               float scaleRef, float scaleMin, float scaleMax)
     1258static float scaleRefOption = NAN;
     1259static float scaleMinOption = NAN;
     1260static float scaleMaxOption = NAN;
     1261static bool  scaleOption = false;
     1262
     1263bool pmSubtractionParamScaleOptions(bool scale, float scaleRef, float scaleMin, float scaleMax) {
     1264
     1265    if (scale) {
     1266        PS_ASSERT_FLOAT_LARGER_THAN(scaleRef, 0.0, false);
     1267        PS_ASSERT_FLOAT_LARGER_THAN(scaleMin, 0.0, false);
     1268        PS_ASSERT_FLOAT_LARGER_THAN(scaleMax, 0.0, false);
     1269        PS_ASSERT_FLOAT_LARGER_THAN(scaleMax, scaleMin, false);
     1270    }
     1271
     1272    scaleRefOption = scaleRef;
     1273    scaleMinOption = scaleMin;
     1274    scaleMaxOption = scaleMax;
     1275    scaleOption = scale;
     1276   
     1277    return true;
     1278}
     1279
     1280bool pmSubtractionParamsScale(int *kernelSize, int *stampSize, psVector *widths, float fwhm1, float fwhm2)
    12091281{
    1210     PS_ASSERT_PTR_NON_NULL(kernelSize, false);
    1211     PS_ASSERT_PTR_NON_NULL(stampSize, false);
     1282    // PS_ASSERT_PTR_NON_NULL(kernelSize, false);
     1283    // PS_ASSERT_PTR_NON_NULL(stampSize, false);
    12121284    PS_ASSERT_VECTOR_NON_NULL(widths, false);
    12131285    PS_ASSERT_VECTOR_TYPE(widths, PS_TYPE_F32, false);
    1214     PS_ASSERT_FLOAT_LARGER_THAN(scaleRef, 0.0, false);
    1215     PS_ASSERT_FLOAT_LARGER_THAN(scaleMin, 0.0, false);
    1216     PS_ASSERT_FLOAT_LARGER_THAN(scaleMax, 0.0, false);
    1217     PS_ASSERT_FLOAT_LARGER_THAN(scaleMax, scaleMin, false);
    1218 
    1219     float fwhm1;
    1220     float fwhm2;
    1221 
    1222     pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
    1223     psAssert(isfinite(fwhm1), "fwhm 1 not set");
    1224     psAssert(isfinite(fwhm2), "fwhm 2 not set");
     1286
     1287    if (!scaleOption) return true;
     1288
     1289    // pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
     1290    // psAssert(isfinite(fwhm1), "fwhm 1 not set");
     1291    // psAssert(isfinite(fwhm2), "fwhm 2 not set");
    12251292   
    12261293    // float diff = sqrtf(PS_SQR(PS_MAX(fwhm1, fwhm2)) - PS_SQR(PS_MIN(fwhm1, fwhm2))); // Difference
    1227     float scale = PS_MAX(fwhm1, fwhm2) / scaleRef;      // Scaling factor
    1228 
    1229     if (isfinite(scaleMin) && scale < scaleMin) {
    1230         scale = scaleMin;
    1231     }
    1232     if (isfinite(scaleMax) && scale > scaleMax) {
    1233         scale = scaleMax;
     1294    float scale = PS_MAX(fwhm1, fwhm2) / scaleRefOption;      // Scaling factor
     1295
     1296    if (isfinite(scaleMinOption) && scale < scaleMinOption) {
     1297        scale = scaleMinOption;
     1298    }
     1299    if (isfinite(scaleMaxOption) && scale > scaleMaxOption) {
     1300        scale = scaleMaxOption;
    12341301    }
    12351302
    12361303    for (int i = 0; i < widths->n; i++) {
    1237         widths->data.F32[i] *= scale;
    1238     }
    1239     *kernelSize = *kernelSize * scale + 0.5;
    1240     *stampSize = *stampSize * scale + 0.5;
    1241 
    1242     psLogMsg("psModules.imcombine", PS_LOG_INFO,
    1243              "Scaling kernel parameters by %f: %d %d", scale, *kernelSize, *stampSize);
     1304        widths->data.F32[i] *= scale;
     1305    }
     1306    if (kernelSize) {
     1307        *kernelSize = *kernelSize * scale + 0.5;
     1308    }
     1309    if (stampSize) {
     1310        *stampSize = *stampSize * scale + 0.5;
     1311    }
     1312
     1313    psLogMsg("psModules.imcombine", PS_LOG_INFO, "Scaling kernel parameters by %f", scale);
     1314    if (kernelSize) psLogMsg("psModules.imcombine", PS_LOG_INFO, " modified kernel size %d", *kernelSize);
     1315    if (stampSize) psLogMsg("psModules.imcombine", PS_LOG_INFO, " modified stamp size %d", *stampSize);
    12441316
    12451317    return true;
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionMatch.h

    r29543 r30631  
    88#include <pmSubtractionKernels.h>
    99#include <pmSubtractionStamps.h>
     10#include <pmSubtraction.h>
    1011
    1112/// Match two images
     
    2627                        int size,       ///< Kernel half-size
    2728                        int order,      ///< Spatial polynomial order
    28                         const psVector *widths, ///< ISIS Gaussian widths
     29                        psVector *widths, ///< ISIS Gaussian widths
    2930                        const psVector *orders, ///< ISIS Polynomial orders
    3031                        int inner,      ///< Inner radius for various kernel types
     
    100101
    101102/// Scale subtraction parameters according to the FWHMs of the inputs
    102 bool pmSubtractionParamsScale(
    103     int *kernelSize,                    ///< Half-size of the kernel
    104     int *stampSize,                     ///< Half-size of the stamp (footprint)
    105     psVector *widths,                   ///< ISIS widths
    106     float scaleRef,                     ///< Reference width for scaling
    107     float scaleMin,                     ///< Minimum scaling ratio, or NAN
    108     float scaleMax                      ///< Maximum scaling ratio, or NAN
     103// bool pmSubtractionParamsScale(
     104//     int *kernelSize,                    ///< Half-size of the kernel
     105//     int *stampSize,                     ///< Half-size of the stamp (footprint)
     106//     psVector *widths,                   ///< ISIS widths
     107//     float scaleRef,                     ///< Reference width for scaling
     108//     float scaleMin,                     ///< Minimum scaling ratio, or NAN
     109//     float scaleMax                      ///< Maximum scaling ratio, or NAN
     110//     );
     111
     112bool pmSubtractionParamsScale(int *kernelSize, int *stampSize, psVector *widths, float fwhm1, float fwhm2);
     113
     114bool pmSubtractionParamScaleOptions(bool scale, float scaleRef, float scaleMin, float scaleMax);
     115
     116bool pmSubtractionMatchAttempt(
     117    pmSubtractionQuality **bestMatch,
     118    pmSubtractionKernels *kernels,
     119    pmSubtractionStampList *stamps,
     120    pmSubtractionMode mode,
     121    int spatialOrder,
     122    bool final
    109123    );
    110124
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionParams.c

    r27086 r30631  
    99
    1010#include "pmErrorCodes.h"
     11#include "pmFPA.h"
     12#include "pmSubtractionTypes.h"
     13#include "pmSubtraction.h"
    1114#include "pmSubtractionStamps.h"
    12 #include "pmSubtraction.h"
    1315#include "pmSubtractionParams.h"
    1416
     
    1618
    1719#if 0
     20// XXX this was moved to pmSubtraction.c in r15443 -- delete
    1821// Convolve the reference stamp by the kernel
    1922static psKernel *convolveStamp(const pmSubtractionStamp *stamp, // Stamp to be convolved
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionStamps.c

    r29543 r30631  
    2727#include "pmSource.h"
    2828
     29#include "pmSubtractionTypes.h"
    2930#include "pmSubtraction.h"
    3031#include "pmSubtractionStamps.h"
     32#include "pmSubtractionVisual.h"
    3133
    3234#define STAMP_LIST_BUFFER 20            // Number of stamps to add to list at a time
     
    122124            if ((image1 && image1->data.F32[y][x] < thresh1) ||
    123125                (image2 && image2->data.F32[y][x] < thresh2)) {
     126                // fprintf (stderr, "%f,%f : thresh\n", xRaw, yRaw);
    124127                continue;
    125128            }
     
    366369}
    367370
    368 
    369 pmSubtractionStampList *pmSubtractionStampsFind(pmSubtractionStampList *stamps, const psImage *image1,
    370                                                 const psImage *image2, const psImage *subMask,
    371                                                 const psRegion *region, float thresh1, float thresh2,
    372                                                 int size, int footprint, float spacing, float normFrac,
    373                                                 float sysErr, float skyErr, pmSubtractionMode mode)
     371bool pmSubtractionStampsSelect(pmSubtractionStampList **stamps, // Stamps to read
     372                               const pmReadout *ro1, // Readout 1
     373                               const pmReadout *ro2, // Readout 2
     374                               const psImage *subMask, // Mask for subtraction, or NULL
     375                               psImage *variance,  // Variance map
     376                               const psRegion *region, // Region of interest
     377                               float thresh1,  // Threshold for stamp finding on readout 1
     378                               float thresh2,  // Threshold for stamp finding on readout 2
     379                               float stampSpacing, // Spacing between stamps
     380                               float normFrac,     // Fraction of flux in window for normalisation window
     381                               float sysError,     // Relative systematic error in images
     382                               float skyError,     // Relative systematic error in images
     383                               int size,         // Kernel half-size
     384                               int footprint,     // Convolution footprint for stamps
     385                               pmSubtractionMode mode // Mode for subtraction
     386    )
     387{
     388    PS_ASSERT_PTR_NON_NULL(stamps, false);
     389    PM_ASSERT_READOUT_NON_NULL(ro1, false);
     390    PM_ASSERT_READOUT_NON_NULL(ro2, false);
     391    PS_ASSERT_IMAGE_NON_NULL(subMask, false);
     392    PS_ASSERT_IMAGE_NON_NULL(variance, false);
     393    PS_ASSERT_PTR_NON_NULL(region, false);
     394
     395    psTrace("psModules.imcombine", 3, "Finding stamps...\n");
     396
     397    psImage *image1 = ro1 ? ro1->image : NULL, *image2 = ro2 ? ro2->image : NULL; // Images of interest
     398
     399    *stamps = pmSubtractionStampsFind(*stamps, image1, image2, subMask, region, thresh1, thresh2,
     400                                      size, footprint, stampSpacing, normFrac, sysError, skyError, mode);
     401    if (!*stamps) {
     402        psError(psErrorCodeLast(), false, "Unable to find stamps.");
     403        return false;
     404    }
     405
     406    psTrace("psModules.imcombine", 3, "Extracting stamps...\n");
     407    if (!pmSubtractionStampsExtract(*stamps, ro1->image, ro2->image, variance, size, *region)) {
     408        psError(psErrorCodeLast(), false, "Unable to extract stamps.");
     409        return false;
     410    }
     411
     412    pmSubtractionVisualPlotStamps(*stamps, (pmReadout *) ro1);
     413    return true;
     414}
     415
     416pmSubtractionStampList *pmSubtractionStampsFind(pmSubtractionStampList *stamps,
     417                                                const psImage *image1,
     418                                                const psImage *image2,
     419                                                const psImage *subMask,
     420                                                const psRegion *region,
     421                                                float thresh1,
     422                                                float thresh2,
     423                                                int size,
     424                                                int footprint,
     425                                                float spacing,
     426                                                float normFrac,
     427                                                float sysErr,
     428                                                float skyErr,
     429                                                pmSubtractionMode mode)
    374430{
    375431    if (!image1 && !image2) {
     
    429485        stamps = pmSubtractionStampListAlloc(numCols, numRows, region, footprint, spacing,
    430486                                             normFrac, sysErr, skyErr);
     487    }
     488
     489    // XXX TEST : dump all stars in the stamps here
     490    if (0) {
     491        FILE *f = fopen ("stamp.dat", "w");
     492        for (int i = 0; i < stamps->num; i++) {
     493            psVector *xList = stamps->x->data[i];
     494            psVector *yList = stamps->y->data[i]; // Coordinate lists
     495            psVector *fluxList = stamps->flux->data[i]; // List of stamp fluxes
     496
     497            for (int j = 0; j < xList->n; j++) {
     498                fprintf (f, "%d %d  %f %f  %f\n", i, j, xList->data.F32[j], yList->data.F32[j], fluxList->data.F32[j]);
     499            }
     500        }
     501        fclose (f);
    431502    }
    432503
     
    636707    }
    637708
     709    int nTotal = 0;
     710
    638711    // Sort the list by flux, with the brightest last
    639712    for (int i = 0; i < numStamps; i++) {
     
    662735        stamps->y->data[i] = ySorted;
    663736        stamps->flux->data[i] = fluxSorted;
    664     }
    665 
     737        nTotal += num;
     738    }
     739    // fprintf (stderr, "nTotal %d\n", nTotal);
     740   
    666741    return stamps;
    667742}
    668743
    669 
    670 bool pmSubtractionStampsGetWindow(pmSubtractionStampList *stamps, int kernelSize)
     744// we are essentially using aperture photometry to determine the photometric match between the
     745// images.  we need to choose an appropriate-sized aperture for this analysis.  If it is too
     746// large, the measurement will be noisy (and possibly biased) due to the sky noise.  If it is
     747// too small, or inconsistent, the measurement will be biased.  We use Kron-mag like aperture
     748// scaled by the first radial moment.
     749bool pmSubtractionStampsGetWindow(bool *tryAgain, pmSubtractionStampList *stamps, int kernelSize)
    671750{
    672751    PM_ASSERT_SUBTRACTION_STAMP_LIST_NON_NULL(stamps, false);
    673752    PS_ASSERT_INT_NONNEGATIVE(kernelSize, false);
    674753
     754    // if we succeed, or fail with an unrecoverable error, do not try again
     755    if (tryAgain) {
     756        *tryAgain = false;
     757    }
     758
    675759    int size = stamps->footprint; // Size of postage stamps
    676760
     761    // window for moments calculations downstream
     762    psFree (stamps->window);
     763    stamps->window = psKernelAlloc(-size, size, -size, size);
     764    psImageInit(stamps->window->image, 0.0);
     765
     766    // window1 and window2 are mean stamp images used here to measure the
     767    // first radial moment, and thus the normalization window
    677768    psFree (stamps->window1);
    678769    stamps->window1 = psKernelAlloc(-size, size, -size, size);
     
    683774    psImageInit(stamps->window2->image, 0.0);
    684775
    685     // Generate a weighting window based on the fwhms (20% larger than the largest)
    686     {
    687         float fwhm1, fwhm2;
    688 
    689         // XXX this is annoyingly hack-ish
    690         pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
    691 
    692         float sigma = 1.5 * PS_MAX(fwhm1, fwhm2) / 2.35;
    693 
    694         psFree (stamps->window);
    695         stamps->window = psKernelAlloc(-size, size, -size, size);
    696         psImageInit(stamps->window->image, 0.0);
    697 
    698         for (int y = -size; y <= size; y++) {
    699             for (int x = -size; x <= size; x++) {
    700                 stamps->window->kernel[y][x] = exp(-0.5*(x*x + y*y)/(sigma*sigma));
    701             }
    702         }
     776    // Generate an initial weighting window based on the fwhms (50% larger than the largest)
     777    float fwhm1, fwhm2;
     778
     779    // XXX this is annoyingly hack-ish
     780    pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
     781   
     782    float sigma = 1.5 * PS_MAX(fwhm1, fwhm2) / 2.35;
     783   
     784    for (int y = -size; y <= size; y++) {
     785        for (int x = -size; x <= size; x++) {
     786            stamps->window->kernel[y][x] = exp(-0.5*(x*x + y*y)/(sigma*sigma));
     787        }
    703788    }
    704789
     
    790875    psTrace("psModules.imcombine", 3, "Window total (2): %f, threshold: %f\n", sum2, (1.0 - stamps->normFrac) * sum2);
    791876
    792 # if (1)
    793     // this block attempts to calculate the radius based on the first radial moment
     877    // attempt to calculate the normalization window based on the first radial moment
    794878    double Sr1 = 0.0;
    795879    double Sr2 = 0.0;
     
    809893    float R2 = Sr2 / Sf2;
    810894
    811     stamps->normWindow1 = 2.0*R1;
    812     stamps->normWindow2 = 2.0*R2;
    813     psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "Kron Radii: %f for 1, %f for 2\n", stamps->normWindow1, stamps->normWindow2);
    814 
    815 # else
    816     // XXX : this block attempts to calculate the radius by looking at the curve of growth (or something vaguely equivalent).
    817     // It did not do very well (though a true curve-of-growth analysis might be better...)
    818     bool done1 = false;
    819     bool done2 = false;
    820     double prior1 = 0.0;
    821     double prior2 = 0.0;
    822     double delta1o = 1.0;
    823     double delta2o = 1.0;
    824     for (int radius = 1; radius <= size && !(done1 && done2); radius++) {
    825         double within1 = 0.0;
    826         double within2 = 0.0;
    827         for (int y = -radius; y <= radius; y++) {
    828             for (int x = -radius; x <= radius; x++) {
    829                 if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) {
    830                     within1 += stamps->window1->kernel[y][x];
    831                 }
    832                 if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) {
    833                     within2 += stamps->window2->kernel[y][x];
    834                 }
    835             }
    836         }
    837         double delta1 = (within1 - prior1) / within1;
    838         if (!done1 && (fabs(delta1) < stamps->normFrac)) {
    839             // interpolate to the radius at which delta2 is normFrac:
    840             stamps->normWindow1 = radius - (stamps->normFrac - delta1) / (delta1o - delta1);
    841             psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "choosing %f (%d) for 1 (%f : %f)\n", stamps->normWindow1, radius, within1, delta1);
    842             done1 = true;
    843         }
    844         double delta2 = (within2 - prior2) / within2;
    845         if (!done2 && (fabs(delta2) < stamps->normFrac)) {
    846             // interpolate to the radius at which delta2 is normFrac:
    847             stamps->normWindow2 = radius - (stamps->normFrac - delta2) / (delta2o - delta2);
    848             psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "choosing %f (%d) for 2 (%f : %f)\n", stamps->normWindow2, radius, within2, delta2);
    849             done2 = true;
    850         }
    851         psTrace("psModules.imcombine", 5, "Radius %d: %f (%f) and %f (%f)\n", radius, within1, delta1, within2, delta2);
    852 
    853         prior1 = within1;
    854         prior2 = within2;
    855         delta1o = delta1;
    856         delta2o = delta2;
    857 
    858         // if (!done1 && (within1 > (1.0 - stamps->normFrac) * sum1)) {
    859         //     stamps->normWindow1 = radius;
    860         //     done1 = true;
    861         // }
    862         // if (!done2 && (within2 > (1.0 - stamps->normFrac) * sum2)) {
    863         //     stamps->normWindow2 = radius;
    864         //     done2 = true;
    865         // }
    866 
    867     }
    868 # endif
    869 
    870     psTrace("psModules.imcombine", 3, "Normalisation window radii set to %f and %f\n", stamps->normWindow1, stamps->normWindow2);
    871     if (stamps->normWindow1 == 0 || stamps->normWindow1 >= size) {
     895    // Compare the Kron Radii (R1 & R2) to above to the FWHMs : if they are too discrepant, we will need to rescale
     896    psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "Kron Radii vs FWHMs 1: fwhm: %f, kron %f\n", fwhm1, R1);
     897    psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "Kron Radii vs FWHMs 2: fwhm: %f, kron %f\n", fwhm2, R2);
     898
     899    // XXX CAREFUL : in pmSubtractionMatch.c:703, we rely on this factor of 2.75..
     900    stamps->normWindow1 = 2.75*R1;
     901    stamps->normWindow2 = 2.75*R2;
     902    psLogMsg ("psModules.imcombine", PS_LOG_DETAIL, "Windows from Kron Radii: %f for 1, %f for 2\n", stamps->normWindow1, stamps->normWindow2);
     903
     904    // if the calculated normWindows are too large, we will fall off the stamps.  In this case, we need to try again.
     905    if ((stamps->normWindow1 > size) || (stamps->normWindow2 > size)) {
     906        if (tryAgain) {
     907            *tryAgain = true;
     908        }
     909        psFree (stats);
     910        psFree (flux1);
     911        psFree (flux2);
     912        psFree (norm1);
     913        psFree (norm2);
     914        return false;
     915    }
     916
     917    // this is an unrecoverable error : something really bogus in the data
     918    if (stamps->normWindow1 == 0) {
    872919        psError(PM_ERR_STAMPS, true, "Unable to determine normalisation window size (1).");
     920        psFree (stats);
     921        psFree (flux1);
     922        psFree (flux2);
     923        psFree (norm1);
     924        psFree (norm2);
    873925        return false;
    874926    }
    875     if (stamps->normWindow2 == 0 || stamps->normWindow2 >= size) {
     927    if (stamps->normWindow2 == 0) {
    876928        psError(PM_ERR_STAMPS, true, "Unable to determine normalisation window size (2).");
     929        psFree (stats);
     930        psFree (flux1);
     931        psFree (flux2);
     932        psFree (norm1);
     933        psFree (norm2);
    877934        return false;
     935    }
     936
     937    // Generate a weighting window based on the kron radii
     938    float radius = 2.0 * PS_MAX(R1, R2);
     939    psImageInit(stamps->window->image, 0.0);
     940
     941    // we use a top-hat window for the moments analysis
     942    for (int y = -size; y <= size; y++) {
     943        for (int x = -size; x <= size; x++) {
     944            if (hypot(x,y) > radius) continue;
     945            stamps->window->kernel[y][x] = 1.0;
     946        }
    878947    }
    879948
     
    890959        }
    891960    }
    892 
    893 #if 0
    894     {
    895         psFits *fits = NULL;
    896         fits = psFitsOpen ("window1.norm.fits", "w");
    897         psFitsWriteImage (fits, NULL, stamps->window1->image, 0, NULL);
    898         psFitsClose (fits);
    899         fits = psFitsOpen ("window2.norm.fits", "w");
    900         psFitsWriteImage (fits, NULL, stamps->window2->image, 0, NULL);
    901         psFitsClose (fits);
    902     }
    903 #endif
    904961
    905962    psFree (stats);
     
    11281185            continue;
    11291186        }
     1187       
     1188        // XXX this is somewhat arbitrary...
     1189        if (source->errMag > 0.05) continue;
     1190        if (fabs(source->psfMag - source->apMag) > 0.5) continue;
     1191
    11301192        if (source->modelPSF) {
    11311193            x->data.F32[numOut] = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     
    11351197            y->data.F32[numOut] = source->peak->yf;
    11361198        }
    1137         // fprintf (stderr, "stamp: %5.1f %5.1f\n", x->data.F32[numOut], y->data.F32[numOut]);
    11381199        numOut++;
    11391200    }
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionStamps.h

    r29543 r30631  
    11#ifndef PM_SUBTRACTION_STAMPS_H
    22#define PM_SUBTRACTION_STAMPS_H
    3 
    4 #include <pslib.h>
    5 
    6 #include "pmSubtractionKernels.h"
    7 
    8 /// Status of stamp
    9 typedef enum {
    10     PM_SUBTRACTION_STAMP_INIT,          ///< Initial state
    11     PM_SUBTRACTION_STAMP_FOUND,         ///< Found a suitable source for this stamp
    12     PM_SUBTRACTION_STAMP_CALCULATE,     ///< Calculate matrix and vector values for this stamp
    13     PM_SUBTRACTION_STAMP_USED,          ///< Use this stamp
    14     PM_SUBTRACTION_STAMP_REJECTED,      ///< This stamp has been rejected
    15     PM_SUBTRACTION_STAMP_NONE           ///< No stamp in this region
    16 } pmSubtractionStampStatus;
    17 
    18 /// A list of stamps
    19 typedef struct {
    20     long num;                           ///< Number of stamps
    21     psArray *stamps;                    ///< The stamps
    22     psArray *regions;                   ///< Regions for each stamp
    23     psArray *x, *y;                     ///< Coordinates for possible stamps (or NULL)
    24     psArray *flux;                      ///< Fluxes for possible stamps (or NULL)
    25     int footprint;                      ///< Half-size of stamps
    26     float normFrac;                     ///< Fraction of flux in window for normalisation window
    27     float normValue;                    ///< calculated normalization
    28     float normValue2;                   ///< calculated normalization
    29     psKernel *window1;                  ///< window function generated from ensemble of stamps (input 1)
    30     psKernel *window2;                  ///< window function generated from ensemble of stamps (input 2)
    31     psKernel *window;                   ///< weighting window function (sigma = 1.1 * MAX(fwhm))
    32     float normWindow1;                  ///< Size of window for measuring normalisation
    33     float normWindow2;                  ///< Size of window for measuring normalisation
    34     float sysErr;                       ///< Systematic error
    35     float skyErr;                       ///< increase effective readnoise
    36 } pmSubtractionStampList;
    373
    384/// Allocate a list of stamps
     
    7440    );
    7541
    76 
    77 /// A stamp for image subtraction
    78 typedef struct {
    79     float x, y;                         ///< Position
    80     float flux;                         ///< Flux
    81     float xNorm, yNorm;                 ///< Normalised position
    82     psKernel *image1;                   ///< Reference image postage stamp
    83     psKernel *image2;                   ///< Input image postage stamp
    84     psKernel *weight;                   ///< Weight image (1/variance) postage stamp, or NULL
    85     psArray *convolutions1;             ///< Convolutions of image 1 for each kernel component, or NULL
    86     psArray *convolutions2;             ///< Convolutions of image 2 for each kernel component, or NULL
    87     psImage *matrix;                    ///< Least-squares matrix, or NULL
    88     psVector *vector;                   ///< Least-squares vector, or NULL
    89     double norm;                        ///< Normalisation difference
    90     double normI1;                       ///< Sum(flux) for image 1
    91     double normI2;                       ///< Sum(flux) for image 2
    92     double normSquare1;                 ///< Sum(flux^2) for image 1 (used for penalty)
    93     double normSquare2;                 ///< Sum(flux^2) for image 2 (used for penalty)
    94     pmSubtractionStampStatus status;    ///< Status of stamp
    95     psVector *MxxI1;                    ///< second moments of convolution images
    96     psVector *MyyI1;                    ///< second moments of convolution images
    97     psVector *MxxI2;                    ///< second moments of convolution images
    98     psVector *MyyI2;                    ///< second moments of convolution images
    99     double MxxI1raw;
    100     double MyyI1raw;
    101     double MxxI2raw;
    102     double MyyI2raw;
    103 } pmSubtractionStamp;
    104 
    10542/// Allocate a stamp
    10643pmSubtractionStamp *pmSubtractionStampAlloc(void);
     44
     45// find and extract the stamps
     46bool pmSubtractionStampsSelect(pmSubtractionStampList **stamps, // Stamps to read
     47                               const pmReadout *ro1, // Readout 1
     48                               const pmReadout *ro2, // Readout 2
     49                               const psImage *subMask, // Mask for subtraction, or NULL
     50                               psImage *variance,  // Variance map
     51                               const psRegion *region, // Region of interest
     52                               float thresh1,  // Threshold for stamp finding on readout 1
     53                               float thresh2,  // Threshold for stamp finding on readout 2
     54                               float stampSpacing, // Spacing between stamps
     55                               float normFrac,     // Fraction of flux in window for normalisation window
     56                               float sysError,     // Relative systematic error in images
     57                               float skyError,     // Relative systematic error in images
     58                               int size,         // Kernel half-size
     59                               int footprint,     // Convolution footprint for stamps
     60                               pmSubtractionMode mode // Mode for subtraction
     61    );
     62
    10763
    10864/// Find stamps on an image
     
    172128/// Calculate the window and normalisation window from the stamps
    173129bool pmSubtractionStampsGetWindow(
     130    bool *tryAgain,                     ///< re-try with new stamp size?
    174131    pmSubtractionStampList *stamps,     ///< List of stamps
    175132    int kernelSize                      ///< Half-size of kernel
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionThreads.c

    r27365 r30631  
    66#include <pslib.h>
    77
     8#include "pmSubtractionTypes.h"
    89#include "pmSubtractionMatch.h"
    910#include "pmSubtractionEquation.h"
     
    3334
    3435    {
    35         psThreadTask *task = psThreadTaskAlloc("PSMODULES_SUBTRACTION_CALCULATE_EQUATION", 4);
     36        psThreadTask *task = psThreadTaskAlloc("PSMODULES_SUBTRACTION_CALCULATE_EQUATION", 3);
    3637        task->function = &pmSubtractionCalculateEquationThread;
     38        psThreadTaskAdd(task);
     39        psFree(task);
     40    }
     41
     42    {
     43        psThreadTask *task = psThreadTaskAlloc("PSMODULES_SUBTRACTION_CONVOLVE_STAMP", 3);
     44        task->function = &pmSubtractionConvolveStampThread;
    3745        psThreadTaskAdd(task);
    3846        psFree(task);
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionVisual.c

    r29600 r30631  
    1616
    1717#include "pmKapaPlots.h"
     18#include "pmFPA.h"
     19#include "pmSubtractionTypes.h"
    1820#include "pmSubtraction.h"
    1921#include "pmSubtractionStamps.h"
    2022#include "pmSubtractionEquation.h"
    2123#include "pmSubtractionKernels.h"
     24#include "pmSubtractionVisual.h"
    2225
    2326#include "pmVisual.h"
     
    210213        psImageOverlaySection(canvas, im, x0, y0, "=");
    211214        stampNum++;
     215
     216        // renormalize the section
     217        float maxValue = 0;
     218        for (int iy = 0; iy < im->numRows; iy++) {
     219            for (int ix = 0; ix < im->numCols; ix++) {
     220                maxValue = PS_MAX(maxValue, im->data.F64[iy][ix]);
     221            }
     222        }
     223        if (maxValue == 0.0) continue;
     224        for (int iy = 0; iy < im->numRows; iy++) {
     225            for (int ix = 0; ix < im->numCols; ix++) {
     226                canvas->data.F64[y0 + iy][x0 + ix] /= maxValue;
     227            }
     228        }
    212229    }
    213230
     
    231248}
    232249
     250/** Plot the least-squares matrix of each stamp */
     251bool pmSubtractionVisualPlotLeastSquaresResid (const pmSubtractionStampList *stamps, psImage *matrixIn, int nUsed) {
     252
     253    if (!pmVisualTestLevel("ppsub.chisq", 1)) return true;
     254
     255    if (!plotLeastSquares) return true;
     256
     257    if (!pmVisualInitWindow (&kapa1, "PPSub:Images")) {
     258        return false;
     259    }
     260
     261    psImage *matrixNorm = psImageCopy(NULL, matrixIn, PS_TYPE_F64);
     262    {
     263        // renormalize the matrix
     264        float maxValue = 0;
     265        for (int iy = 0; iy < matrixNorm->numRows; iy++) {
     266            for (int ix = 0; ix < matrixNorm->numCols; ix++) {
     267                maxValue = PS_MAX(maxValue, matrixNorm->data.F64[iy][ix]);
     268            }
     269        }
     270        for (int iy = 0; iy < matrixNorm->numRows; iy++) {
     271            for (int ix = 0; ix < matrixNorm->numCols; ix++) {
     272                matrixNorm->data.F64[iy][ix] /= maxValue;
     273            }
     274        }
     275    }
     276
     277    // Find the stamp size
     278    int imageMax = -1;
     279    int numStamps = 0;
     280    psElemType type = PS_TYPE_F64;
     281
     282    for(int i = 0; i < stamps->num; i++) {
     283        pmSubtractionStamp *stamp = stamps->stamps->data[i];
     284        if (stamp == NULL) continue;
     285
     286        psImage *im = stamp->matrix;
     287        if (im == NULL) continue;
     288
     289        imageMax = PS_MAX(imageMax, im->numCols);
     290        imageMax = PS_MAX(imageMax, im->numRows);
     291        numStamps++;
     292        type = im->type.type;
     293    }
     294    if (imageMax == -1) return false;
     295
     296    int border = 15;
     297    imageMax += border;
     298    int tileRowCount = (int) ceil(sqrt(numStamps));
     299    int canvasX = tileRowCount * (imageMax);
     300    int canvasY = tileRowCount * (imageMax);
     301    psImage *canvas = psImageAlloc (canvasX, canvasY, type);
     302    psImageInit (canvas, NAN);
     303
     304    // overlay the images
     305    int stampNum = 0;
     306    int stampListNum = 0;
     307    while (stampNum < numStamps) {
     308        int x0 = (imageMax) * (stampNum % tileRowCount);
     309        int y0 = (imageMax) * (stampNum / tileRowCount);
     310
     311        pmSubtractionStamp *stamp = stamps->stamps->data[stampListNum++];
     312        if (stamp == NULL) continue;
     313
     314        psImage *im = stamp->matrix;
     315        if (im == NULL) continue;
     316
     317        stampNum++;
     318
     319        if (stamp->status != PM_SUBTRACTION_STAMP_USED) continue;
     320
     321        // renormalize the section
     322        float maxValue = 0;
     323        for (int iy = 0; iy < im->numRows; iy++) {
     324            for (int ix = 0; ix < im->numCols; ix++) {
     325                maxValue = PS_MAX(maxValue, im->data.F64[iy][ix]);
     326            }
     327        }
     328        if (maxValue == 0.0) continue;
     329        for (int iy = 0; iy < im->numRows; iy++) {
     330            for (int ix = 0; ix < im->numCols; ix++) {
     331                canvas->data.F64[y0 + iy][x0 + ix] = (im->data.F64[iy][ix] / maxValue - matrixNorm->data.F64[iy][ix]) / matrixNorm->data.F64[iy][ix];
     332            }
     333        }
     334    }
     335
     336    psImage *canvas32 = pmVisualImageToFloat(canvas);
     337    pmVisualRangeImage(kapa2, canvas32, "Least_Squares", 0, -100.0, 100.0);
     338
     339    if (0) {
     340        static int count = 0;
     341        char filename[64];
     342        sprintf (filename, "chisq.%02d.fits", count);
     343        count ++;
     344        psFits *fits = psFitsOpen (filename, "w");
     345        psFitsWriteImage (fits, NULL, canvas32, 0, NULL);
     346        psFitsClose (fits);
     347    }
     348
     349    pmVisualAskUser(&plotLeastSquares);
     350    psFree(canvas);
     351    psFree(canvas32);
     352    return true;
     353}
     354
    233355bool pmSubtractionVisualShowSubtraction(psImage *image, psImage *ref, psImage *sub) {
    234356
     
    304426    }
    305427
    306     // XXX clear the overlay(s) (red at least!)
     428    // clear the overlay (red at least!)
     429    KiiEraseOverlay (kapa2, "red");
    307430
    308431    // get the kernel sizes
     
    337460    int nKernels = 0;
    338461
     462    // paste in the kernel images, scaled by sum2
    339463    if (maxStamp->convolutions1) {
    340464        // output image is a grid of NXsub by NYsub sub-images
     
    359483            int yPix = ySub * (2*footprint + 1 + 3) + footprint;
    360484           
    361             double sum = 0.0;
    362485            double sum2 = 0.0;
    363486            for (int y = -footprint; y <= footprint; y++) {
    364487                for (int x = -footprint; x <= footprint; x++) {
    365                     output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x];
    366                     sum += kernel->kernel[y][x];
    367488                    sum2 += PS_SQR(kernel->kernel[y][x]);
    368489                }
    369490            }
    370             // fprintf (stderr, "kernel %d, sum %f, sum2: %e\n", i, sum, sum2);
     491            float scale = sqrt(sum2) / PS_SQR(2*footprint + 1);
     492            for (int y = -footprint; y <= footprint; y++) {
     493                for (int x = -footprint; x <= footprint; x++) {
     494                    output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x] / scale;
     495                }
     496            }
    371497        }               
    372498        pmVisualScaleImage(kapa2, output, "Image", 0, true);
     
    401527            int yPix = ySub * (2*footprint + 1 + 3) + footprint;
    402528           
    403             double sum = 0.0;
    404529            double sum2 = 0.0;
    405530            for (int y = -footprint; y <= footprint; y++) {
    406531                for (int x = -footprint; x <= footprint; x++) {
    407                     output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x];
    408                     sum += kernel->kernel[y][x];
    409532                    sum2 += PS_SQR(kernel->kernel[y][x]);
    410533                }
    411534            }
    412             // fprintf (stderr, "kernel %d, sum %f, sum2: %e\n", i, sum, sum2);
     535            float scale = sqrt(sum2) / PS_SQR(2*footprint + 1);
     536            for (int y = -footprint; y <= footprint; y++) {
     537                for (int x = -footprint; x <= footprint; x++) {
     538                    output->data.F32[y + yPix][x + xPix] = kernel->kernel[y][x] / scale;
     539                }
     540            }
    413541        }               
    414542        pmVisualScaleImage(kapa2, output, "Image", 1, true);
     
    468596    KiiLoadOverlay (kapa2, overlay, Noverlay, "red");
    469597    FREE (overlay);
    470     return true;
    471 }
    472 
    473 static int footprint = 0;
    474 static int NX = 0;
    475 static int NY = 0;
    476 static psImage *sourceImage      = NULL;
    477 static psImage *targetImage      = NULL;
    478 static psImage *residualImage    = NULL;
    479 static psImage *fresidualImage   = NULL;
    480 static psImage *differenceImage  = NULL;
    481 static psImage *convolutionImage = NULL;
    482 
    483 bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps) {
    484 
    485     if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
    486 
    487     // generate 4 storage images large enough to hold the N stamps:
    488 
    489     footprint = stamps->footprint;
    490 
    491     float NXf = sqrt(stamps->num);
    492     NX = (int) NXf == NXf ? NXf : NXf + 1.0;
    493    
    494     float NYf = stamps->num / NX;
    495     NY = (int) NYf == NY ? NYf : NYf + 1.0;
    496 
    497     int NXpix = (2*footprint + 1) * NX;
    498     NXpix += (NX > 1) ? 3 * NX : 0;
    499 
    500     int NYpix = (2*footprint + 1) * NY;
    501     NYpix += (NY > 1) ? 3 * NY : 0;
    502 
    503     sourceImage      = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    504     targetImage      = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    505     residualImage    = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    506     fresidualImage   = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    507     differenceImage  = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    508     convolutionImage = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
    509    
    510     psImageInit (sourceImage,      0.0);
    511     psImageInit (targetImage,      0.0);
    512     psImageInit (residualImage,    0.0);
    513     psImageInit (fresidualImage,   0.0);
    514     psImageInit (differenceImage,  0.0);
    515     psImageInit (convolutionImage, 0.0);
    516 
    517     return true;
    518 }
    519 
    520 bool pmSubtractionVisualShowFitAddStamp(psKernel *target, psKernel *source, psKernel *convolution, double background, double norm, int index) {
    521 
    522     if (!pmVisualTestLevel("ppsub.stamp", 1)) return true;
    523 
    524     double sum;
    525 
    526     int NXoff = index % NX;
    527     int NYoff = index / NX;
    528 
    529     int NXpix = NXoff * (2*footprint + 1 + 3) + footprint;
    530     int NYpix = NYoff * (2*footprint + 1 + 3) + footprint;
    531 
    532     // insert the (target) kernel into the (target) image:
    533     sum = 0.0;
    534     for (int y = -footprint; y <= footprint; y++) {
    535         for (int x = -footprint; x <= footprint; x++) {
    536             targetImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x];
    537             sum += targetImage->data.F32[y + NYpix][x + NXpix];
    538         }
    539     }
    540     targetImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
    541 
    542     // insert the (source) kernel into the (source) image:
    543     sum = 0.0;
    544     for (int y = -footprint; y <= footprint; y++) {
    545         for (int x = -footprint; x <= footprint; x++) {
    546             sourceImage->data.F32[y + NYpix][x + NXpix] = source->kernel[y][x];
    547             sum += sourceImage->data.F32[y + NYpix][x + NXpix];
    548         }
    549     }
    550     sourceImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
    551 
    552     // insert the (convolution) kernel into the (convolution) image:
    553     sum = 0.0;
    554     for (int y = -footprint; y <= footprint; y++) {
    555         for (int x = -footprint; x <= footprint; x++) {
    556             convolutionImage->data.F32[y + NYpix][x + NXpix] = convolution->kernel[y][x];
    557             sum += convolutionImage->data.F32[y + NYpix][x + NXpix];
    558         }
    559     }
    560     convolutionImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
    561    
    562     // insert the (difference) kernel into the (difference) image:
    563     sum = 0.0;
    564     for (int y = -footprint; y <= footprint; y++) {
    565         for (int x = -footprint; x <= footprint; x++) {
    566             differenceImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x] - background - source->kernel[y][x] * norm;
    567             sum += differenceImage->data.F32[y + NYpix][x + NXpix];
    568         }
    569     }
    570     differenceImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
    571 
    572     // insert the (residual) kernel into the (residual) image:
    573     sum = 0.0;
    574     for (int y = -footprint; y <= footprint; y++) {
    575         for (int x = -footprint; x <= footprint; x++) {
    576             residualImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x] - background - source->kernel[y][x] * norm - convolution->kernel[y][x];
    577             sum += residualImage->data.F32[y + NYpix][x + NXpix];
    578         }
    579     }
    580     residualImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
    581 
    582     // insert the (fresidual) kernel into the (fresidual) image:
    583     for (int y = -footprint; y <= footprint; y++) {
    584         for (int x = -footprint; x <= footprint; x++) {
    585             fresidualImage->data.F32[y + NYpix][x + NXpix] = residualImage->data.F32[y + NYpix][x + NXpix] / sqrt(PS_MAX(target->kernel[y][x], 100.0));
    586         }
    587     }
    588598    return true;
    589599}
     
    611621}
    612622
    613 bool pmSubtractionVisualShowFit(double norm) {
    614 
    615     // for testing, dump the residual image and exit
    616     if (0) {
    617         psMetadata *header = psMetadataAlloc();
    618         psMetadataAddF32 (header, PS_LIST_TAIL, "NORM", 0, "Normalization", norm);
    619         psFits *fits = psFitsOpen("resid.fits", "w");
    620         psFitsWriteImage(fits, header, residualImage, 0, NULL);
    621         psFitsClose(fits);
    622         // exit (0);
    623     }
     623static int footprint = 0;
     624static int NX = 0;
     625static int NY = 0;
     626static psImage *sourceImage      = NULL;
     627static psImage *targetImage      = NULL;
     628static psImage *residualImage    = NULL;
     629static psImage *fresidualImage   = NULL;
     630static psImage *differenceImage  = NULL;
     631static psImage *convolutionImage = NULL;
     632
     633bool pmSubtractionVisualShowFit(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels) {
    624634
    625635    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
     
    627637    if (!pmVisualInitWindow(&kapa1, "ppSub:Images")) return false;
    628638    if (!pmVisualInitWindow(&kapa2, "ppSub:Misc")) return false;
     639
     640    // set up holding images for the visualization
     641    pmSubtractionVisualShowFitInit (stamps);
     642
     643    int numKernels = kernels->num;      // Number of kernels
     644
     645    psImage *polyValues = NULL;         // Polynomial values
     646    psKernel *residual = psKernelAlloc(-stamps->footprint, stamps->footprint, -stamps->footprint, stamps->footprint); // Residual image
     647
     648    double norm = p_pmSubtractionSolutionNorm(kernels); // Normalisation
     649
     650    for (int i = 0; i < stamps->num; i++) {
     651        pmSubtractionStamp *stamp = stamps->stamps->data[i]; // The stamp of interest
     652        if (stamp->status != PM_SUBTRACTION_STAMP_USED) { continue; }
     653
     654        // Calculate coefficients of the kernel basis functions
     655        polyValues = p_pmSubtractionPolynomial(polyValues, kernels->spatialOrder, stamp->xNorm, stamp->yNorm);
     656        double background = p_pmSubtractionSolutionBackground(kernels, polyValues); // Difference in background
     657
     658        psImageInit(residual->image, 0.0);
     659
     660        if (kernels->mode != PM_SUBTRACTION_MODE_DUAL) {
     661            psKernel *target;           // Target postage stamp
     662            psKernel *source;           // Source postage stamp
     663            psArray *convolutions;      // Convolution postage stamps for each kernel basis function
     664            switch (kernels->mode) {
     665              case PM_SUBTRACTION_MODE_1:
     666                target = stamp->image2;
     667                source = stamp->image1;
     668                convolutions = stamp->convolutions1;
     669                break;
     670              case PM_SUBTRACTION_MODE_2:
     671                target = stamp->image1;
     672                source = stamp->image2;
     673                convolutions = stamp->convolutions2;
     674                break;
     675              default:
     676                psAbort("Unsupported subtraction mode: %x", kernels->mode);
     677            }
     678
     679            for (int j = 0; j < numKernels; j++) {
     680                psKernel *convolution = convolutions->data[j]; // Convolution
     681                double coefficient = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient
     682                for (int y = - footprint; y <= footprint; y++) {
     683                    for (int x = - footprint; x <= footprint; x++) {
     684                        residual->kernel[y][x] += convolution->kernel[y][x] * coefficient;
     685                    }
     686                }
     687            }
     688            // visualize the target, source, convolution and residual
     689            pmSubtractionVisualShowFitAddStamp (target, source, residual, background, norm, i);
     690        } else {
     691            // Dual convolution
     692            psArray *convolutions1 = stamp->convolutions1; // Convolutions of the first image
     693            psArray *convolutions2 = stamp->convolutions2; // Convolutions of the second image
     694            psKernel *image1 = stamp->image1; // The first image
     695            psKernel *image2 = stamp->image2; // The second image
     696
     697            for (int j = 0; j < numKernels; j++) {
     698                psKernel *conv1 = convolutions1->data[j]; // Convolution of first image
     699                psKernel *conv2 = convolutions2->data[j]; // Convolution of second image
     700                double coeff1 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, false); // Coefficient 1
     701                double coeff2 = p_pmSubtractionSolutionCoeff(kernels, polyValues, j, true); // Coefficient 2
     702
     703                for (int y = - footprint; y <= footprint; y++) {
     704                    for (int x = - footprint; x <= footprint; x++) {
     705                        residual->kernel[y][x] += conv2->kernel[y][x] * coeff2 + conv1->kernel[y][x] * coeff1;
     706                    }
     707                }
     708            }
     709            // visualize the target, source, convolution and residual
     710            pmSubtractionVisualShowFitAddStamp (image2, image1, residual, background, norm, i);
     711        }
     712    }
     713    pmSubtractionVisualShowFitImage(norm);
     714
     715    return true;
     716}
     717
     718// generate 4 storage images large enough to hold the stamps:
     719bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps) {
     720
     721    footprint = stamps->footprint;
     722
     723    float NXf = sqrt(stamps->num);
     724    NX = (int) NXf == NXf ? NXf : NXf + 1.0;
     725   
     726    float NYf = stamps->num / NX;
     727    NY = (int) NYf == NY ? NYf : NYf + 1.0;
     728
     729    int NXpix = (2*footprint + 1) * NX;
     730    NXpix += (NX > 1) ? 3 * NX : 0;
     731
     732    int NYpix = (2*footprint + 1) * NY;
     733    NYpix += (NY > 1) ? 3 * NY : 0;
     734
     735    sourceImage      = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     736    targetImage      = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     737    residualImage    = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     738    fresidualImage   = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     739    differenceImage  = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     740    convolutionImage = psImageAlloc (NXpix, NYpix, PS_TYPE_F32);
     741   
     742    psImageInit (sourceImage,      0.0);
     743    psImageInit (targetImage,      0.0);
     744    psImageInit (residualImage,    0.0);
     745    psImageInit (fresidualImage,   0.0);
     746    psImageInit (differenceImage,  0.0);
     747    psImageInit (convolutionImage, 0.0);
     748
     749    return true;
     750}
     751
     752bool pmSubtractionVisualShowFitAddStamp(psKernel *target, psKernel *source, psKernel *convolution, double background, double norm, int index) {
     753
     754    double sum;
     755
     756    int NXoff = index % NX;
     757    int NYoff = index / NX;
     758
     759    int NXpix = NXoff * (2*footprint + 1 + 3) + footprint;
     760    int NYpix = NYoff * (2*footprint + 1 + 3) + footprint;
     761
     762    // insert the (target) kernel into the (target) image:
     763    sum = 0.0;
     764    for (int y = -footprint; y <= footprint; y++) {
     765        for (int x = -footprint; x <= footprint; x++) {
     766            targetImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x];
     767            sum += targetImage->data.F32[y + NYpix][x + NXpix];
     768        }
     769    }
     770    targetImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
     771
     772    // insert the (source) kernel into the (source) image:
     773    sum = 0.0;
     774    for (int y = -footprint; y <= footprint; y++) {
     775        for (int x = -footprint; x <= footprint; x++) {
     776            sourceImage->data.F32[y + NYpix][x + NXpix] = source->kernel[y][x];
     777            sum += sourceImage->data.F32[y + NYpix][x + NXpix];
     778        }
     779    }
     780    sourceImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
     781
     782    // insert the (convolution) kernel into the (convolution) image:
     783    sum = 0.0;
     784    for (int y = -footprint; y <= footprint; y++) {
     785        for (int x = -footprint; x <= footprint; x++) {
     786            convolutionImage->data.F32[y + NYpix][x + NXpix] = convolution->kernel[y][x];
     787            sum += convolutionImage->data.F32[y + NYpix][x + NXpix];
     788        }
     789    }
     790    convolutionImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
     791   
     792    // insert the (difference) kernel into the (difference) image:
     793    sum = 0.0;
     794    for (int y = -footprint; y <= footprint; y++) {
     795        for (int x = -footprint; x <= footprint; x++) {
     796            differenceImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x] - background - source->kernel[y][x] * norm;
     797            sum += differenceImage->data.F32[y + NYpix][x + NXpix];
     798        }
     799    }
     800    differenceImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
     801
     802    // insert the (residual) kernel into the (residual) image:
     803    sum = 0.0;
     804    for (int y = -footprint; y <= footprint; y++) {
     805        for (int x = -footprint; x <= footprint; x++) {
     806            residualImage->data.F32[y + NYpix][x + NXpix] = target->kernel[y][x] - background - source->kernel[y][x] * norm - convolution->kernel[y][x];
     807            sum += residualImage->data.F32[y + NYpix][x + NXpix];
     808        }
     809    }
     810    residualImage->data.F32[footprint + 1 + NYpix][NXpix] = sum;
     811
     812    // insert the (fresidual) kernel into the (fresidual) image:
     813    for (int y = -footprint; y <= footprint; y++) {
     814        for (int x = -footprint; x <= footprint; x++) {
     815            fresidualImage->data.F32[y + NYpix][x + NXpix] = residualImage->data.F32[y + NYpix][x + NXpix] / sqrt(PS_MAX(target->kernel[y][x], 100.0));
     816        }
     817    }
     818    return true;
     819}
     820
     821bool pmSubtractionVisualShowFitImage(double norm) {
    629822
    630823    KiiEraseOverlay (kapa1, "red");
     
    673866    psVector *x = psVectorAllocEmpty (kernels->num, PS_TYPE_F32);
    674867    psVector *y = psVectorAllocEmpty (kernels->num, PS_TYPE_F32);
     868    psVector *dy = psVectorAllocEmpty (kernels->num, PS_TYPE_F32);
    675869
    676870    graphdata.xmin = -1.0;
     
    685879        x->data.F32[i] = i;
    686880        y->data.F32[i] = p_pmSubtractionSolutionCoeff(kernels, polyValues, i, false);
     881        dy->data.F32[i] = kernels->solution1err->data.F64[i];
    687882        graphdata.ymin = PS_MIN(graphdata.ymin, y->data.F32[i]);
    688883        graphdata.ymax = PS_MAX(graphdata.ymax, y->data.F32[i]);
    689884    }
    690     x->n = y->n = kernels->num;
     885    x->n = y->n = dy->n = kernels->num;
    691886
    692887    float range;
     
    709904    graphdata.size = 0.5;
    710905    graphdata.style = 2;
     906    graphdata.etype |= 0x01;
    711907
    712908    KapaPrepPlot   (kapa3, x->n, &graphdata);
    713909    KapaPlotVector (kapa3, x->n, x->data.F32, "x");
    714910    KapaPlotVector (kapa3, x->n, y->data.F32, "y");
     911    KapaPlotVector (kapa3, x->n, dy->data.F32, "dym");
     912    KapaPlotVector (kapa3, x->n, dy->data.F32, "dyp");
    715913
    716914    psFree (x);
    717915    psFree (y);
     916    psFree (dy);
    718917    psFree (polyValues);
     918
     919    pmVisualAskUser(NULL);
     920    return true;
     921}
     922
     923// plot log(flux) vs log(chisq), log(flux) vs log(moments), log(chisq) vs log(moments)
     924bool pmSubtractionVisualPlotChisqAndMoments(psVector *fluxes, psVector *chisq, psVector *moments) {
     925
     926    Graphdata graphdata;
     927    KapaSection section;
     928
     929    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
     930
     931    if (!pmVisualInitWindow(&kapa3, "ppSub:plots")) return false;
     932
     933    KapaClearSections (kapa3);
     934    KapaInitGraph (&graphdata);
     935    KiiResize(kapa3, 1500, 500);
     936
     937    psVector *lchi = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     938    psVector *lflx = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     939    psVector *lMxx = psVectorAlloc (fluxes->n, PS_TYPE_F32);
     940
     941    // construct the plot vectors
     942    for (int i = 0; i < fluxes->n; i++) {
     943        lchi->data.F32[i] = log10(chisq->data.F32[i]);
     944        lflx->data.F32[i] = log10(fluxes->data.F32[i]);
     945        lMxx->data.F32[i] = log10(moments->data.F32[i]);
     946    }
     947
     948    section.bg = KapaColorByName ("none"); // XXX probably should be 'none'
     949
     950    // section 1: lflux vs lchi
     951    section.dx = 0.33;
     952    section.dy = 1.00;
     953    section.x  = 0.00;
     954    section.y  = 0.00;
     955    section.name = psStringCopy ("flux.v.chi");
     956    KapaSetSection (kapa3, &section);
     957    psFree (section.name);
     958
     959    graphdata.color = KapaColorByName ("black");
     960    pmVisualScaleGraphdata(&graphdata, lflx, lchi, false);
     961    KapaSetLimits (kapa3, &graphdata);
     962
     963    KapaSetFont (kapa3, "helvetica", 14);
     964    KapaBox (kapa3, &graphdata);
     965    KapaSendLabel (kapa3, "log(flux)", KAPA_LABEL_XM);
     966    KapaSendLabel (kapa3, "log(chisq)", KAPA_LABEL_YM);
     967
     968    graphdata.color = KapaColorByName ("black");
     969    graphdata.ptype = 2;
     970    graphdata.size = 0.5;
     971    graphdata.style = 2;
     972
     973    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     974    KapaPlotVector (kapa3, lflx->n, lflx->data.F32, "x");
     975    KapaPlotVector (kapa3, lflx->n, lchi->data.F32, "y");
     976
     977    // section 2: lflux vs lMxx
     978    section.dx = 0.33;
     979    section.dy = 1.00;
     980    section.x  = 0.33;
     981    section.y  = 0.00;
     982    section.name = psStringCopy ("flux.v.mom");
     983    KapaSetSection (kapa3, &section);
     984    psFree (section.name);
     985
     986    graphdata.color = KapaColorByName ("black");
     987    pmVisualScaleGraphdata(&graphdata, lflx, lMxx, false);
     988    KapaSetLimits (kapa3, &graphdata);
     989
     990    KapaSetFont (kapa3, "helvetica", 14);
     991    KapaBox (kapa3, &graphdata);
     992    KapaSendLabel (kapa3, "log(flux)", KAPA_LABEL_XM);
     993    KapaSendLabel (kapa3, "log(moments)", KAPA_LABEL_YM);
     994
     995    graphdata.color = KapaColorByName ("black");
     996    graphdata.ptype = 2;
     997    graphdata.size = 0.5;
     998    graphdata.style = 2;
     999
     1000    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     1001    KapaPlotVector (kapa3, lflx->n, lflx->data.F32, "x");
     1002    KapaPlotVector (kapa3, lflx->n, lMxx->data.F32, "y");
     1003
     1004    // section 1: lflux vs lchi
     1005    section.dx = 0.33;
     1006    section.dy = 1.00;
     1007    section.x  = 0.66;
     1008    section.y  = 0.00;
     1009    section.name = psStringCopy ("chi.v.mom");
     1010    KapaSetSection (kapa3, &section);
     1011    psFree (section.name);
     1012
     1013    graphdata.color = KapaColorByName ("black");
     1014    pmVisualScaleGraphdata(&graphdata, lchi, lMxx, false);
     1015    KapaSetLimits (kapa3, &graphdata);
     1016
     1017    KapaSetFont (kapa3, "helvetica", 14);
     1018    KapaBox (kapa3, &graphdata);
     1019    KapaSendLabel (kapa3, "log(chisq)", KAPA_LABEL_XM);
     1020    KapaSendLabel (kapa3, "log(moments)", KAPA_LABEL_YM);
     1021
     1022    graphdata.color = KapaColorByName ("black");
     1023    graphdata.ptype = 2;
     1024    graphdata.size = 0.5;
     1025    graphdata.style = 2;
     1026
     1027    KapaPrepPlot   (kapa3, lflx->n, &graphdata);
     1028    KapaPlotVector (kapa3, lflx->n, lchi->data.F32, "x");
     1029    KapaPlotVector (kapa3, lflx->n, lMxx->data.F32, "y");
    7191030
    7201031    pmVisualAskUser(NULL);
  • branches/czw_branch/20101203/psModules/src/imcombine/pmSubtractionVisual.h

    r29601 r30631  
    66bool pmSubtractionVisualPlotStamps(pmSubtractionStampList *stamps, pmReadout *ro);
    77bool pmSubtractionVisualPlotLeastSquares(pmSubtractionStampList *stamps);
     8bool pmSubtractionVisualPlotLeastSquaresResid (const pmSubtractionStampList *stamps, psImage *matrixIn, int nUsed);
    89bool pmSubtractionVisualShowSubtraction(psImage *image, psImage *ref, psImage *sub);
     10
     11bool pmSubtractionVisualShowFit(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels);
    912bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps);
    1013bool pmSubtractionVisualShowFitAddStamp(psKernel *target, psKernel *source, psKernel *convolution, double background, double norm, int index);
    11 bool pmSubtractionVisualShowFit(double norm);
     14bool pmSubtractionVisualShowFitImage(double norm);
     15
    1216bool pmSubtractionVisualPlotFit(const pmSubtractionKernels *kernels);
    1317bool pmSubtractionVisualShowKernels(pmSubtractionKernels *kernels);
    1418bool pmSubtractionVisualShowBasis(pmSubtractionStampList *stamps);
     19bool pmSubtractionVisualPlotChisqAndMoments(psVector *fluxes, psVector *chisq, psVector *moments);
    1520
    1621#endif
  • branches/czw_branch/20101203/psModules/src/objects

    • Property svn:mergeinfo deleted
  • branches/czw_branch/20101203/psModules/src/objects/pmFootprint.c

    r29004 r30631  
    255255}
    256256
     257// create a new footprint with the same span set as the input footprint
     258pmFootprint *pmFootprintCopyData(pmFootprint *inFoot, psImage *image) {
     259
     260    pmFootprint *outFoot = pmFootprintAlloc(inFoot->nspans, image);
     261    for (int i = 0; i < inFoot->nspans; i++) {
     262        pmSpan *span = inFoot->spans->data[i];
     263        pmFootprintAddSpan(outFoot, span->y, span->x0, span->x1);
     264    }
     265    return outFoot;
     266}
     267
    257268/************************************************************************************************************/
  • branches/czw_branch/20101203/psModules/src/objects/pmFootprint.h

    r29004 r30631  
    2727
    2828bool pmFootprintAllocEmptySpans (pmFootprint *footprint, int nSpans);
     29
     30pmFootprint *pmFootprintCopyData(pmFootprint *inFoot, psImage *image);
    2931
    3032pmFootprint *pmFootprintNormalize(pmFootprint *fp);
  • branches/czw_branch/20101203/psModules/src/objects/pmFootprintAssignPeaks.c

    r29004 r30631  
    2424/*
    2525 * Given a psArray of pmFootprints and another of pmPeaks, assign the peaks to the
    26  * footprints in which that fall; if they _don't_ fall in a footprint, add a suitable
     26 * footprints in which they fall; if they _don't_ fall in a footprint, add a suitable
    2727 * one to the list.
    2828 */
     
    8383
    8484        // XXX are we allowed to have peak-less footprints??
    85         if (!fp->peaks->n) continue;
     85        if (fp->peaks->n == 0) continue;
     86        if (fp->peaks->n == 1) continue;
    8687
    8788        fp->peaks = psArraySort(fp->peaks, pmPeakSortBySN);
    8889
     90        // XXX check for an assert on duplicates (I don't think they can happen, but
     91        // let's double check for now)
     92
     93        for (int j = 1; j < fp->peaks->n; j++) {
     94            psAssert (fp->peaks->data[j] != fp->peaks->data[j-1], "duplicate peak!");
     95        }
     96        continue;
     97
     98        // XXX WHY am I culling duplicates?  how can there be duplicates?
    8999        // XXX EAM : the algorithm below should be much faster than using psArrayRemove if
    90100        // the number of peaks in the footprint is large, or if there are no duplicates.
     
    93103
    94104        // track the number of good peaks in the footprint
    95         int nGood = 1;
     105        int lastGood = 0;
    96106
    97107        // check for duplicates
     
    100110        // (if sorted list has A, B, A, B ...)
    101111        for (int j = 1; j < fp->peaks->n; j++) {
    102             if (fp->peaks->data[j] == fp->peaks->data[nGood]) {
     112            if (fp->peaks->data[j] == fp->peaks->data[lastGood]) {
    103113                // everything on the array has its own mem reference; free and drop this one
    104114                psFree (fp->peaks->data[j]);
    105115                fp->peaks->data[j] = NULL;
    106116            } else {
    107                 nGood ++;
     117                lastGood ++;
    108118            }
    109119        }
     120        int nGood = lastGood + 1;
    110121
    111122        // no deleted peaks, go to next footprint
  • branches/czw_branch/20101203/psModules/src/objects/pmFootprintCullPeaks.c

    r29004 r30631  
    2828  * Examine the peaks in a pmFootprint, and throw away the ones that are not sufficiently
    2929  * isolated.  More precisely, for each peak find the highest coll that you'd have to traverse
    30   * to reach a still higher peak --- and if that coll's more than nsigma DN below your
     30  * to reach a still higher peak --- and if that coll's more (less?) than nsigma DN below your
    3131  * starting point, discard the peak.
    3232  */
     
    9999        float threshold = flux - nsigma_delta*stdev_pad;
    100100
    101         if (isnan(threshold) || threshold < min_threshold) {
     101        if (isnan(threshold)) {
    102102            // min_threshold is assumed to be below the detection threshold,
    103103            // so all the peaks are pmFootprint, and this isn't the brightest
     
    109109            threshold = subImg->data.F32[y][x] - 10*FLT_EPSILON;
    110110        }
     111
     112        if (threshold < min_threshold) {
     113            threshold = min_threshold;
     114        }
    111115
    112116        // init peakFootprint here?
  • branches/czw_branch/20101203/psModules/src/objects/pmModel.c

    r29004 r30631  
    7070    tmp->fitRadius = 0;
    7171    tmp->flags = PM_MODEL_STATUS_NONE;
    72     tmp->residuals = NULL;              // XXX should the model own this memory?
     72    tmp->residuals = NULL;              // do not free: the model does not own this memory
     73    tmp->isPCM = false;
    7374
    7475    psS32 Nparams = pmModelClassParameterCount(type);
     
    108109pmModel *pmModelCopy (pmModel *model)
    109110{
    110     PS_ASSERT_PTR_NON_NULL(model, NULL);
    111 
     111    if (model == NULL) {
     112        return NULL;
     113    }
    112114    pmModel *new = pmModelAlloc (model->type);
    113115
  • branches/czw_branch/20101203/psModules/src/objects/pmModel.h

    r29004 r30631  
    4343    float fitRadius;                    ///< fit radius actually used
    4444    pmResiduals *residuals;             ///< normalized PSF residuals
     45    bool isPCM;                         ///< is this model fitted with PSF-convolution?
    4546
    4647    // functions for this model which depend on the model class
  • branches/czw_branch/20101203/psModules/src/objects/pmPCMdata.h

    r29004 r30631  
    8383    pmPCMdata *pcm);
    8484
     85psKernel *pmPCMkernelFromPSF (pmSource *source, int nPix);
     86
    8587bool pmSourceModelGuessPCM (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, psImageMaskType markVal);
    8688
  • branches/czw_branch/20101203/psModules/src/objects/pmPSFtry.c

    r29004 r30631  
    8484
    8585    for (int i = 0; i < sources->n; i++) {
    86         test->sources->data[i] = pmSourceCopy (sources->data[i]);
     86        pmSource *sourceOld = sources->data[i];
     87        pmSource *sourceNew = pmSourceCopy (sourceOld);
     88
     89        // save a reference so we can get back to the original
     90        // this is specifically used in psphotChooosePSF to unflag the candidate PSF sources
     91        // which were not actually used to generate a PSF model
     92        sourceNew->parent = sourceOld;
     93
     94        test->sources->data[i] = sourceNew;
    8795    }
    8896
  • branches/czw_branch/20101203/psModules/src/objects/pmPSFtryFitEXT.c

    r29004 r30631  
    6565        if (!source->moments) {
    6666            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_EXT_FAIL;
     67            psTrace ("psModules.objects", 4, "masking %d (%d,%d) : no moments\n", i, source->peak->x, source->peak->y);
    6768            continue;
    6869        }
    6970        if (!source->moments->nPixels) {
     71            psTrace ("psModules.objects", 4, "masking %d (%d,%d) : no pixels\n", i, source->peak->x, source->peak->y);
    7072            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_EXT_FAIL;
    7173            continue;
     
    7476        source->modelEXT = pmSourceModelGuess (source, options->type);
    7577        if (source->modelEXT == NULL) {
     78            psTrace ("psModules.objects", 4, "masking %d (%d,%d) : failed to generate model guess\n", i, source->peak->x, source->peak->y);
    7679            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_EXT_FAIL;
    77             psTrace ("psModules.objects", 4, "masking %d (%d,%d) : failed to generate model guess\n", i, source->peak->x, source->peak->y);
    7880            continue;
    7981        }
     
    9193        // exclude the poor fits
    9294        if (!status) {
     95            psTrace ("psModules.objects", 4, "masking %d (%d,%d) : status is poor\n", i, source->peak->x, source->peak->y);
    9396            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_EXT_FAIL;
    94             psTrace ("psModules.objects", 4, "masking %d (%d,%d) : status is poor\n", i, source->peak->x, source->peak->y);
    9597            continue;
    9698        }
  • branches/czw_branch/20101203/psModules/src/objects/pmPSFtryFitPSF.c

    r29004 r30631  
    8181        if (source->modelPSF == NULL) {
    8282            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_MODEL;
     83            psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : bad PSF fit\n", i, source->peak->x, source->peak->y);
    8384            continue;
    8485        }
  • branches/czw_branch/20101203/psModules/src/objects/pmPSFtryModel.c

    r30118 r30631  
    5252// mask values indicate the reason the source was rejected:
    5353
     54// XXX some test code (delete eventually)
     55// bool countMaskedSources(pmPSFtry *try, char *msg) {
     56//
     57//     int N1 = 0;
     58//     int N2 = 0;
     59//     for (int i = 0; i < try->sources->n; i++) {
     60//         pmSource *source = try->sources->data[i];
     61//      // fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
     62//      if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
     63//          N1 ++;
     64//         }
     65//      if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
     66//          N2 ++;
     67//         }
     68//     }
     69//     fprintf (stderr, "%s : masked: %d or %d of %ld\n", msg, N1, N2, try->sources->n);
     70//     return true;
     71// }
     72
    5473// generate a pmPSFtry with a copy of the test PSF sources
    5574pmPSFtry *pmPSFtryModel (const psArray *sources, const char *modelName, pmPSFOptions *options, psImageMaskType maskVal, psImageMaskType markVal)
     
    203222            chisq->data.F32[i] = 0.0;
    204223            mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = 0xff;
     224            psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : no PSF model\n", i, source->peak->x, source->peak->y);
    205225        } else {
    206226            flux->data.F32[i] = source->modelPSF->params->data.F32[PM_PAR_I0];
  • branches/czw_branch/20101203/psModules/src/objects/pmSource.c

    r30118 r30631  
    6161    psFree(tmp->moments);
    6262    psFree(tmp->diffStats);
    63     psFree(tmp->radial);
     63    psFree(tmp->radialAper);
    6464    psFree(tmp->blends);
    6565    psTrace("psModules.objects", 10, "---- end ----\n");
     
    126126    source->extpars = NULL;
    127127    source->diffStats = NULL;
    128     source->radial = NULL;
     128    source->radialAper = NULL;
     129    source->parent = NULL;
    129130
    130131    source->region = psRegionSet(NAN, NAN, NAN, NAN);
     
    159160affecting the original.  This Copy can be used to allow multiple fit attempts on the same
    160161object.  The pixels, variance, and mask arrays all point to the same original subarrays.  The
    161 peak and moments point at the original values.
     162peak and moments point at the original values.  The models, blends, and XXX are NOT copied
    162163*****************************************************************************/
    163164pmSource *pmSourceCopy(pmSource *in)
     
    167168    }
    168169    pmSource *source = pmSourceAlloc ();
    169 
    170     // keep the original ID so we can find map back to the original
    171     P_PM_SOURCE_SET_ID(source, in->id);
    172170
    173171    // peak has the same values as the original
     
    203201}
    204202
    205 /******************************************************************************
    206 pmSourceCopyData(): this creates a new, duplicate source with the same parameters as the
    207 original (but is actually a new source at the same location)
    208 *****************************************************************************/
    209 pmSource *pmSourceCopyData(pmSource *in)
    210 {
    211     if (in == NULL) {
    212         return(NULL);
    213     }
    214     // this copy is used to allow multiple fit attempts on the
    215     // same object.  the pixels, variance, and mask arrays all point to
    216     // the same original subarrays.  the peak and moments point at
    217     // the original values.
    218     pmSource *source = pmSourceAlloc ();
    219 
    220     // this is actually the same peak as the original, is this the correct way to handle this?
    221     if (in->peak != NULL) {
    222         source->peak = pmPeakAlloc (in->peak->x, in->peak->y, in->peak->value, in->peak->type);
    223         source->peak->xf = in->peak->xf;
    224         source->peak->yf = in->peak->yf;
    225         source->peak->flux = in->peak->flux;
    226         source->peak->SN = in->peak->SN;
    227     }
    228 
    229     // copy the values in the moments structure
    230     if (in->moments != NULL) {
    231         source->moments  =  pmMomentsAlloc();
    232         *source->moments = *in->moments;
    233     }
    234 
    235     // These images are all views to the parent.
    236     // We want a new view, but pointing at the same pixels.
    237     source->pixels   = psImageCopyView(NULL, in->pixels);
    238     source->variance   = psImageCopyView(NULL, in->variance);
    239     source->maskView = in->maskView ? psImageCopyView(NULL, in->maskView) : NULL;
    240 
    241     // the maskObj is a unique mask array; create a new mask image
    242     source->maskObj = in->maskObj ? psImageCopy (NULL, in->maskObj, PS_TYPE_IMAGE_MASK) : NULL;
    243 
    244     source->type = in->type;
    245     source->mode = in->mode;
    246     source->imageID = in->imageID;
    247 
    248     return(source);
    249 }
    250 
    251203// x,y are defined in the parent image coords of readout->image
    252204bool pmSourceDefinePixels(pmSource *mySource,
     
    300252    newRegion = psRegionForImage (readout->image, newRegion);
    301253
     254    // re-define if required by region or absence of pixels
    302255    extend = false;
    303256    extend |= (int)(newRegion.x0) < (int)(mySource->region.x0);
     
    335288    }
    336289    return extend;
     290}
     291
     292bool pmSourceRedefinePixelsByRegion(pmSource *mySource,
     293                                    const pmReadout *readout,
     294                                    psRegion newRegion)
     295{
     296    PS_ASSERT_PTR_NON_NULL(mySource, false);
     297    PS_ASSERT_PTR_NON_NULL(readout, false);
     298    PS_ASSERT_PTR_NON_NULL(readout->image, false);
     299
     300    // re-create the subimage
     301    psFree (mySource->pixels);
     302    psFree (mySource->variance);
     303    psFree (mySource->maskView);
     304       
     305    mySource->pixels   = psImageSubset(readout->image,    newRegion);
     306    mySource->variance = psImageSubset(readout->variance, newRegion);
     307    mySource->maskView = psImageSubset(readout->mask,     newRegion);
     308    mySource->region   = newRegion;
     309
     310    // re-copy the main mask pixels.  NOTE: the user will need to reset the object mask
     311    // pixels (eg, with psImageKeepCircle)
     312    mySource->maskObj = psImageCopy (mySource->maskObj, mySource->maskView, PS_TYPE_IMAGE_MASK);
     313
     314    // drop the old modelFlux pixels and force the user to re-create
     315    psFree (mySource->modelFlux);
     316    mySource->modelFlux = NULL;
     317
     318    // drop the old psfImage pixels and force the user to re-create
     319    psFree (mySource->psfImage);
     320    mySource->psfImage = NULL;
     321
     322    return true;
    337323}
    338324
     
    675661            if ((source->moments->SN > PSF_SN_LIM) && (radius < PSF_CLUMP_NSIGMA)) {
    676662                source->type = PM_SOURCE_TYPE_STAR;
    677                 source->mode |= PM_SOURCE_MODE_PSFSTAR;
     663                source->tmpFlags |= PM_SOURCE_TMPF_CANDIDATE_PSFSTAR;
    678664                Npsf ++;
    679665                continue;
     
    934920}
    935921
    936 // construct a realization of the source model
    937 // XXX this function should optionally save an existing psf image from modelFlux
     922// construct a realization of the PSF model at the location of this source
    938923bool pmSourceCachePSF (pmSource *source, psImageMaskType maskVal) {
    939924    PS_ASSERT_PTR_NON_NULL(source, false);
     
    10201005    }
    10211006
    1022     if (!addNoise) {
    1023         if (add) {
    1024             status = pmModelAddWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
    1025             source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
    1026         } else {
    1027             status = pmModelSubWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
    1028             source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
    1029         }
     1007    if (add) {
     1008        status = pmModelAddWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
     1009        source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     1010    } else {
     1011        status = pmModelSubWithOffset (target, source->maskObj, model, PM_MODEL_OP_FULL, maskVal, dx, dy);
     1012        source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
    10301013    }
    10311014
  • branches/czw_branch/20101203/psModules/src/objects/pmSource.h

    r29546 r30631  
    3535    PM_SOURCE_TMPF_SIZE_CR_CANDIDATE = 0x0008,
    3636    PM_SOURCE_TMPF_MOMENTS_MEASURED  = 0x0010,
     37    PM_SOURCE_TMPF_CANDIDATE_PSFSTAR = 0x0020,
    3738} pmSourceTmpF;
    3839
     
    4243 *  simplest measurement of a source is the location and flux of the peak pixel
    4344 *  associated with the source:
     45 *
     46 * a pmSource is the information about a (possible) blob of flux in a specific image.  A source
     47 * may represent an insignificant or undetected source.  There may be multiple representations
     48 * of an image (eg, alternate smoothed copies); sources on alternate images may have a pointer
     49 * to the version on the primary image (source->parent).  A set of sources on different, but
     50 * related images (eg, multiple exposures or different filters) which (may) represent the same
     51 * astronomical object are grouped together with the pmPhotObj type (set pmPhotObj.h).
     52 *
     53 * A single source may be fitted with multiple models (not at the same time!).  The PSF model
     54 * fit is a fit of the position (optionally) and the flux to the PSF model at the location of
     55 * the source.  Alternate model fits are extended source models. The best model fit is used to
     56 * subtract the object from the image.
    4457 *
    4558 *  XXX do I have to re-organize this (again!) to allow an arbitrary set of extended model fits??
     
    5467    pmPeak  *peak;                      ///< Description of peak pixel.
    5568    psImage *pixels;                    ///< Rectangular region including object pixels.
    56     psImage *variance;                    ///< Image variance.
     69    psImage *variance;                  ///< Image variance.
    5770    psImage *maskObj;                   ///< unique mask for this object which marks included pixels associated with objects.
    5871    psImage *maskView;                  ///< view into global image mask for this object region
    5972    psImage *modelFlux;                 ///< cached copy of the best model for this source
    60     psImage *psfImage;                   ///< cached copy of the psf model for this source
     73    psImage *psfImage;                  ///< cached copy of the psf model for this source
    6174    pmMoments *moments;                 ///< Basic moments measured for the object.
    6275    pmModel *modelPSF;                  ///< PSF Model fit (parameters and type)
     
    89102    pmSourceExtendedPars *extpars;      ///< extended source parameters
    90103    pmSourceDiffStats *diffStats;       ///< extra parameters for difference detections
    91     pmSourceRadialApertures *radial;    ///< radial flux in circular apertures
     104    psArray *radialAper;                ///< radial flux in circular apertures
     105    pmSource *parent;                   ///< reference to the master source from which this is derived
    92106    int imageID;
    93107};
     
    162176);
    163177
     178bool pmSourceRedefinePixelsByRegion (
     179    pmSource *mySource,   ///< source to be re-defined
     180    const pmReadout *readout,   ///< base the source on this readout
     181    psRegion newRegion ///< region for source pixel definition
     182);
     183
    164184/** pmSourcePSFClump()
    165185 *
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceExtendedPars.c

    r28013 r30631  
    1717#endif
    1818
    19 // #include <stdio.h>
    20 // #include <math.h>
    21 // #include <string.h>
    2219#include <pslib.h>
    23 // #include "pmHDU.h"
    24 // #include "pmFPA.h"
    25 // #include "pmFPAMaskWeight.h"
    26 // #include "pmSpan.h"
    27 // #include "pmFootprint.h"
    28 // #include "pmPeaks.h"
    29 // #include "pmMoments.h"
    30 // #include "pmResiduals.h"
    31 // #include "pmGrowthCurve.h"
    32 // #include "pmTrend2D.h"
    33 // #include "pmPSF.h"
    34 // #include "pmModel.h"
    35 // #include "pmSource.h"
    3620#include "pmSourceExtendedPars.h"
    3721
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceFitPCM.c

    r29004 r30631  
    8989    if (!fitStatus) pcm->modelConv->flags |= PM_MODEL_STATUS_NONCONVERGE;
    9090
     91    // once we have fitted a model, we need to record that this model is a PCM model:
     92    pcm->modelConv->isPCM = true;
     93
    9194    // models can go insane: reject these
    9295    bool onPic = true;
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO.c

    r29546 r30631  
    5959
    6060// lookup the EXTNAME values used for table data and image header segments
    61 static bool sourceExtensions(psString *headname, // Extension name for header
    62                              psString *dataname, // Extension name for data
    63                              psString *deteffname, // Extension name for detection efficiency
    64                              psString *xsrcname, // Extension name for extended sources
    65                              psString *xfitname, // Extension name for extended fits
    66                              const pmFPAfile *file, // File of interest
    67                              const pmFPAview *view // View to level of interest
    68                              )
     61bool pmSourceIOextnames(psString *headname,    // Extension name for image header
     62                        psString *dataname,    // Extension name for PSF table data
     63                        psString *deteffname,  // Extension name for detection efficiency
     64                        psString *xsrcname,    // Extension name for extended non-parametric measurements
     65                        psString *xfitname,    // Extension name for extended fitted measurements
     66                        psString *xradname,    // Extension name for radial apertures
     67                        const pmFPAfile *file, // File of interest
     68                        const pmFPAview *view  // View to level of interest
     69    )
    6970{
    7071    bool status;                        // Status of MD lookup
     
    8788    }
    8889
    89     // EXTNAME for table data
     90    // EXTNAME for PSF table data
    9091    if (dataname) {
    9192        const char *rule = psMetadataLookupStr(&status, menu, "CMF.DATA");
     
    107108    }
    108109
    109     // EXTNAME for extended source data table
     110    // EXTNAME for extended source non-parametric measurements
    110111    if (xsrcname) {
    111112        const char *rule = psMetadataLookupStr(&status, menu, "CMF.XSRC");
     
    117118    }
    118119
     120    // EXTNAME for extended source fitted measurements
    119121    if (xfitname) {
    120         // EXTNAME for extended source data table
    121122        const char *rule = psMetadataLookupStr(&status, menu, "CMF.XFIT");
    122123        if (!rule) {
     
    125126        }
    126127        *xfitname = pmFPAfileNameFromRule (rule, file, view);
     128    }
     129
     130    // EXTNAME for radial apertures
     131    if (xradname) {
     132        const char *rule = psMetadataLookupStr(&status, menu, "CMF.XRAD");
     133        if (!rule) {
     134            psError(PS_ERR_UNKNOWN, true, "missing entry for CMF.XRAD in EXTNAME.RULES in camera.config");
     135            return false;
     136        }
     137        *xradname = pmFPAfileNameFromRule (rule, file, view);
    127138    }
    128139
     
    344355            status &= pmSourcesWrite_##TYPE##_XFIT (file->fits, readout, sources, file->header, xfitname); \
    345356        }                                                               \
     357        if (xradname) {                                                 \
     358            status &= pmSourcesWrite_##TYPE##_XRAD (file->fits, readout, sources, file->header, xradname, recipe); \
     359        }                                                               \
    346360    }
    347361
     
    449463        bool XSRC_OUTPUT = psMetadataLookupBool(&status, recipe, "EXTENDED_SOURCE_ANALYSIS");
    450464        bool XFIT_OUTPUT = psMetadataLookupBool(&status, recipe, "EXTENDED_SOURCE_FITS");
     465        bool XRAD_OUTPUT = psMetadataLookupBool(&status, recipe, "RADIAL_APERTURES");
    451466
    452467        // define the EXTNAME values for the different data segments:
     
    456471        psString xsrcname = NULL;
    457472        psString xfitname = NULL;
    458         if (!sourceExtensions(&headname, &dataname, &deteffname,
    459                               XSRC_OUTPUT ? &xsrcname : NULL,
    460                               XFIT_OUTPUT ? &xfitname : NULL,
    461                               file, view)) {
     473        psString xradname = NULL;
     474        if (!pmSourceIOextnames(&headname, &dataname, &deteffname,
     475                                XSRC_OUTPUT ? &xsrcname : NULL,
     476                                XFIT_OUTPUT ? &xfitname : NULL,
     477                                XRAD_OUTPUT ? &xradname : NULL,
     478                                file, view)) {
    462479            return false;
    463480        }
     
    539556                psMetadataAddStr (outhead, PS_LIST_TAIL, "XFITNAME", PS_META_REPLACE, "name of XFIT table extension", xfitname);
    540557            }
    541    
     558            if (xradname) {
     559                psMetadataAddStr (outhead, PS_LIST_TAIL, "XRADNAME", PS_META_REPLACE, "name of XRAD table extension", xradname);
     560            }
    542561
    543562            // these are case-sensitive since the EXTYPE is case-sensitive
     
    563582        }
    564583
    565 
    566584        // write out the detection efficiency TABLE segments
    567585        if (deteffname) {
     
    583601        psFree (xsrcname);
    584602        psFree (xfitname);
     603        psFree (xradname);
    585604        psFree (deteffname);
    586605
     
    593612        psFree (xsrcname);
    594613        psFree (xfitname);
     614        psFree (xradname);
    595615        psFree (deteffname);
    596616        return false;
     
    939959        psString dataname = NULL;
    940960        psString deteffname = NULL;
    941         if (!sourceExtensions(&headname, &dataname, &deteffname, NULL, NULL, file, view)) {
     961        if (!pmSourceIOextnames(&headname, &dataname, &deteffname, NULL, NULL, NULL, file, view)) {
    942962            return false;
    943963        }
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO.h

    r29546 r30631  
    3030bool pmSourcesWrite_SMPDATA_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    3131bool pmSourcesWrite_SMPDATA_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     32bool pmSourcesWrite_SMPDATA_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    3233
    3334bool pmSourcesWrite_PS1_DEV_0(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    3435bool pmSourcesWrite_PS1_DEV_0_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    3536bool pmSourcesWrite_PS1_DEV_0_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     37bool pmSourcesWrite_PS1_DEV_0_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    3638
    3739bool pmSourcesWrite_PS1_DEV_1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    3840bool pmSourcesWrite_PS1_DEV_1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    3941bool pmSourcesWrite_PS1_DEV_1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     42bool pmSourcesWrite_PS1_DEV_1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    4043
    4144bool pmSourcesWrite_PS1_CAL_0(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    4245bool pmSourcesWrite_PS1_CAL_0_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    4346bool pmSourcesWrite_PS1_CAL_0_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     47bool pmSourcesWrite_PS1_CAL_0_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    4448
    4549bool pmSourcesWrite_CMF_PS1_V1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    4650bool pmSourcesWrite_CMF_PS1_V1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    4751bool pmSourcesWrite_CMF_PS1_V1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     52bool pmSourcesWrite_CMF_PS1_V1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    4853
    4954bool pmSourcesWrite_CMF_PS1_V2(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    5055bool pmSourcesWrite_CMF_PS1_V2_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    5156bool pmSourcesWrite_CMF_PS1_V2_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     57bool pmSourcesWrite_CMF_PS1_V2_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    5258
    5359bool pmSourcesWrite_CMF_PS1_V3(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    5460bool pmSourcesWrite_CMF_PS1_V3_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    5561bool pmSourcesWrite_CMF_PS1_V3_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     62bool pmSourcesWrite_CMF_PS1_V3_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    5663
    5764bool pmSourcesWrite_CMF_PS1_SV1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    5865bool pmSourcesWrite_CMF_PS1_SV1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    5966bool pmSourcesWrite_CMF_PS1_SV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     67bool pmSourcesWrite_CMF_PS1_SV1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    6068
    6169bool pmSourcesWrite_CMF_PS1_DV1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    6270bool pmSourcesWrite_CMF_PS1_DV1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    6371bool pmSourcesWrite_CMF_PS1_DV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     72bool pmSourcesWrite_CMF_PS1_DV1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    6473
    6574bool pmSourcesWrite_CMF_PS1_DV2(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    6675bool pmSourcesWrite_CMF_PS1_DV2_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    6776bool pmSourcesWrite_CMF_PS1_DV2_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     77bool pmSourcesWrite_CMF_PS1_DV2_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    6878
    6979psArray *pmSourcesReadCMP (char *filename, psMetadata *header);
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_DV1.c

    r29004 r30631  
    676676    return true;
    677677}
     678
     679bool pmSourcesWrite_CMF_PS1_DV1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     680{
     681    return true;
     682}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_DV2.c

    r29620 r30631  
    737737    return true;
    738738}
     739
     740bool pmSourcesWrite_CMF_PS1_DV2_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     741{
     742    return true;
     743}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_SV1.c

    r29019 r30631  
    2020
    2121#include "pmConfig.h"
     22#include "pmErrorCodes.h"
    2223#include "pmDetrendDB.h"
    2324
     
    7172    psF32 errMag, chisq, apRadius;
    7273    psS32 nPix, nDOF;
    73     char keyword1[80], keyword2[80];
    7474
    7575    pmChip *chip = readout->parent->parent;
     
    103103    table = psArrayAllocEmpty (sources->n);
    104104
     105# if (0)
    105106    // we use this just to define the output vectors (which must be present for all objects)
    106107    bool status = false;
     
    118119      psMetadataAddF32 (imageHeader, PS_LIST_TAIL, keyword2, PS_META_REPLACE, "min radius for SB profile", radMax->data.F32[i]);
    119120    }
    120 
     121# endif
    121122
    122123    // we write out PSF-fits for all sources, regardless of quality.  the source flags tell us the state
     
    258259        psMetadataAdd (row, PS_LIST_TAIL, "FLAGS2",           PS_DATA_U32, "psphot analysis flags",                     source->mode2);
    259260
     261# if (0)
     262        // XXX if we have raw radial apertures, write them out here
    260263        psVector *radFlux    = psVectorAlloc(radMax->n, PS_TYPE_F32);
    261264        psVector *radFluxErr = psVectorAlloc(radMax->n, PS_TYPE_F32);
     
    284287        psFree (radFluxErr);
    285288        psFree (radFill);
     289# endif
    286290
    287291        // XXX not sure how to get this : need to load Nimages with weight?
     
    781785    return true;
    782786}
     787
     788// **** write out the radial flux values for the sources for a given matched-PSF image
     789// **** how do we distinguish the matched-PSF images from the non-matched version
     790bool pmSourcesWrite_CMF_PS1_SV1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     791{
     792
     793    psArray *table;
     794    psMetadata *row;
     795    psF32 xPos, yPos;
     796    char keyword1[80], keyword2[80];
     797
     798    // create a header to hold the output data
     799    psMetadata *outhead = psMetadataAlloc ();
     800
     801    // write the links to the image header
     802    psMetadataAddStr (outhead, PS_LIST_TAIL, "EXTNAME", PS_META_REPLACE, "radial flux table extension", extname);
     803
     804    // we use this just to define the output vectors (which must be present for all objects)
     805    bool status = false;
     806    psVector *radMin = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.LOWER");
     807    psVector *radMax = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.UPPER");
     808    psAssert (radMax, "this must have been defined and tested earlier!");
     809    psAssert (radMax->n, "this must have been defined and tested earlier!");
     810    psAssert (radMin->n == radMax->n, "inconsistent annular bins");
     811
     812    // write the radial profile apertures to header
     813    for (int i = 0; i < radMax->n; i++) {
     814      sprintf (keyword1, "RMIN_%02d", i);
     815      sprintf (keyword2, "RMAX_%02d", i);
     816      psMetadataAddF32 (imageHeader, PS_LIST_TAIL, keyword1, PS_META_REPLACE, "min radius for SB profile", radMin->data.F32[i]);
     817      psMetadataAddF32 (imageHeader, PS_LIST_TAIL, keyword2, PS_META_REPLACE, "min radius for SB profile", radMax->data.F32[i]);
     818    }
     819
     820    psVector *fwhmValues = psMetadataLookupVector(&status, readout->analysis, "STACK.PSF.FWHM.VALUES");
     821    if (!fwhmValues) {
     822        psError (PM_ERR_CONFIG, true, "convolved or measured FWHM is not defined for this readout");
     823        return false;
     824    }
     825
     826    // let's write these out in S/N order
     827    sources = psArraySort (sources, pmSourceSortBySN);
     828
     829    table = psArrayAllocEmpty (sources->n);
     830
     831    // we write out all sources, regardless of quality.  the source flags tell us the state
     832    for (int i = 0; i < sources->n; i++) {
     833
     834        pmSource *source = sources->data[i];
     835
     836        // skip sources without radial aper measurements (or insufficient)
     837        if (source->radialAper == NULL) continue;
     838        psAssert (source->radialAper->n == fwhmValues->n, "inconsistent radial aperture set");
     839
     840        for (int entry = 0; entry < fwhmValues->n; entry++) {
     841
     842            // choose the convolved EXT model, if available, otherwise the simple one
     843            pmSourceRadialApertures *radialAper = source->radialAper->data[entry];
     844            assert (radialAper);
     845
     846            bool useMoments = true;
     847            useMoments = (useMoments && source->moments);          // can't if there are no moments
     848            useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
     849            useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
     850
     851            if (useMoments) {
     852                xPos = source->moments->Mx;
     853                yPos = source->moments->My;
     854            } else {
     855                xPos = source->peak->xf;
     856                yPos = source->peak->yf;
     857            }
     858
     859            row = psMetadataAlloc ();
     860
     861            // XXX we are not writing out the mode (flags) or the type (psf, ext, etc)
     862            psMetadataAddU32 (row, PS_LIST_TAIL, "IPP_IDET",         0, "IPP detection identifier index",             source->seq);
     863            psMetadataAddF32 (row, PS_LIST_TAIL, "X_APER",           0, "Center of aperture measurements",            xPos);
     864            psMetadataAddF32 (row, PS_LIST_TAIL, "Y_APER",           0, "Center of aperture measurements",            yPos);
     865            psMetadataAddF32 (row, PS_LIST_TAIL, "PSF_FWHM",         0, "FWHM of matched PSF",                        fwhmValues->data.F32[entry]);
     866
     867            // XXX if we have raw radial apertures, write them out here
     868            psVector *radFlux    = psVectorAlloc(radMax->n, PS_TYPE_F32);
     869            psVector *radFluxErr = psVectorAlloc(radMax->n, PS_TYPE_F32);
     870            psVector *radFill    = psVectorAlloc(radMax->n, PS_TYPE_F32);
     871            psVectorInit (radFlux,    NAN);
     872            psVectorInit (radFluxErr, NAN);
     873            psVectorInit (radFill,    NAN);
     874            if (!radialAper->flux) goto write_annuli;
     875            if (!radialAper->fill) goto write_annuli;
     876            psAssert (radialAper->flux->n <= radFlux->n, "inconsistent vector lengths");
     877            psAssert (radialAper->fill->n <= radFlux->n, "inconsistent vector lengths");
     878
     879            // copy the data from fluxVal (which is not guaranteed to be the full length) to radFlux
     880            for (int j = 0; j < radialAper->flux->n; j++) {
     881                radFlux->data.F32[j]    = radialAper->flux->data.F32[j];
     882                radFluxErr->data.F32[j] = radialAper->fluxErr->data.F32[j];
     883                radFill->data.F32[j]    = radialAper->fill->data.F32[j];
     884            }
     885
     886        write_annuli:
     887            psMetadataAdd (row, PS_LIST_TAIL, "APER_FLUX",     PS_DATA_VECTOR, "flux within annuli",    radFlux);
     888            psMetadataAdd (row, PS_LIST_TAIL, "APER_FLUX_ERR", PS_DATA_VECTOR, "flux error in annuli",  radFluxErr);
     889            psMetadataAdd (row, PS_LIST_TAIL, "APER_FILL",     PS_DATA_VECTOR, "fill factor of annuli", radFill);
     890            psFree (radFlux);
     891            psFree (radFluxErr);
     892            psFree (radFill);
     893
     894            psArrayAdd (table, 100, row);
     895            psFree (row);
     896        }
     897    }
     898
     899    if (table->n == 0) {
     900        if (!psFitsWriteBlank (fits, outhead, extname)) {
     901            psError(psErrorCodeLast(), false, "Unable to write empty sources file.");
     902            psFree(outhead);
     903            psFree(table);
     904            return false;
     905        }
     906        psFree (outhead);
     907        psFree (table);
     908        return true;
     909    }
     910
     911    psTrace ("pmFPAfile", 5, "writing ext data %s\n", extname);
     912    if (!psFitsWriteTable (fits, outhead, table, extname)) {
     913        psError(psErrorCodeLast(), false, "writing ext data %s\n", extname);
     914        psFree (outhead);
     915        psFree(table);
     916        return false;
     917    }
     918    psFree (outhead);
     919    psFree (table);
     920    return true;
     921}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c

    r29004 r30631  
    721721    return false;
    722722}
     723
     724bool pmSourcesWrite_CMF_PS1_V1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     725{
     726    return true;
     727}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_V2.c

    r29004 r30631  
    724724    return true;
    725725}
     726
     727bool pmSourcesWrite_CMF_PS1_V2_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     728{
     729    return true;
     730}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_CMF_PS1_V3.c

    r29620 r30631  
    762762    return true;
    763763}
     764
     765bool pmSourcesWrite_CMF_PS1_V3_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     766{
     767    return true;
     768}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_PS1_CAL_0.c

    r29004 r30631  
    702702    return true;
    703703}
     704
     705bool pmSourcesWrite_PS1_CAL_0_XRAD (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     706{
     707    return true;
     708}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_PS1_DEV_0.c

    r29004 r30631  
    248248    return true;
    249249}
     250
     251bool pmSourcesWrite_PS1_DEV_0_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     252{
     253    return true;
     254}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_PS1_DEV_1.c

    r29004 r30631  
    588588    return true;
    589589}
     590
     591bool pmSourcesWrite_PS1_DEV_1_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     592{
     593    return true;
     594}
  • branches/czw_branch/20101203/psModules/src/objects/pmSourceIO_SMPDATA.c

    r29004 r30631  
    222222    return true;
    223223}
     224
     225bool pmSourcesWrite_SMPDATA_XRAD(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     226{
     227    return true;
     228}
  • branches/czw_branch/20101203/psModules/src/psmodules.h

    r29388 r30631  
    9898// the following headers are from psModule:imcombine
    9999#include <pmStack.h>
     100#include <pmSubtractionTypes.h>
    100101#include <pmStackReject.h>
    101102#include <pmSubtraction.h>
  • branches/czw_branch/20101203/psconfig/psbuild

    r29559 r30631  
    1313$operations = 0;
    1414$start = "";
     15$start_after = "";
    1516$stop = "";
    1617$verbose = 0;
     
    8586    }
    8687    if ($ARGV[0] eq "-only") {
     88        if ($start || $stop || $start_after) { die "do not mix -only (-start, -stop) or (-after, -stop)\n"; }
    8789        $start = $ARGV[1];
    8890        $stop = $ARGV[1];
     
    9092    }
    9193    if ($ARGV[0] eq "-start") {
     94        if ($start || $start_after) { die "do not mix -only (-start, -stop) or (-after, -stop)\n"; }
    9295        $start = $ARGV[1];
    9396        shift; shift; next;
    9497    }
     98    if ($ARGV[0] eq "-after") {
     99        if ($start || $start_after) { die "do not mix -only (-start, -stop) or (-after, -stop)\n"; }
     100        $start_after = $ARGV[1];
     101        $start = "INVALID_VALUE"; # need to have a value to skip earlier entries
     102        shift; shift; next;
     103    }
    95104    if ($ARGV[0] eq "-stop") {
     105        if ($stop) { die "do not mix -only (-start, -stop) or (-after, -stop)\n"; }
    96106        $stop = $ARGV[1];
    97107        shift; shift; next;
     
    258268    $stop_now = 0;
    259269    for ($i = 0; !$stop_now && ($i < @module); $i++) {
    260         if (($stop ne "") && ($stop eq $module[$i])) { $stop_now = 1; }
    261         if (($start ne "") && ($start ne $module[$i])) { next; }
     270        if ($stop && ($stop eq $module[$i])) { $stop_now = 1; }
     271        if ($start_after && ($start_after eq $module[$i])) { $start = $module[$i+1]; next; }
     272        if ($start && ($start ne $module[$i])) { next; }
    262273        $start = "";
    263274
     
    348359        chdir $homedir;
    349360    }
     361    if ($start) { if ($start_after) { $start = $start_after; } &failure($start, "unknown -start or -after module $start\n"); }
     362    if ($stop && !$stop_now) { &failure($start, "unknown -stop module $stop\n"); }
     363
    350364    print "\033]0; ** psbuild: finished ** \007";
    351365    exit 0;
  • branches/czw_branch/20101203/psphot

  • branches/czw_branch/20101203/psphot/doc/stack.txt

    r28013 r30631  
     1
     220101221
     3
     4  psphotStackReadout is now correctly subtracting the PSF models from
     5  the images as it measures the radial aperture fluxes.
     6
     7  Some issues:
     8
     9  * the source properties get buggered up by the radial aperture flux
     10    analysis (we need to re-determine the psf, re-create the source
     11    pixels, and re-fit the models (linearly) to subtract them
     12    correctly).
     13
     14  * the standard analysis sequence is not doing a second pass
     15
     16  * the choice of the best model is ambiguous
     17
     18  * the radial aperture (and source addition / subtraction) is only
     19    using the PSF model
     20
     21  * make sure psphotEfficiency actually subtracts the inserted fake
     22    sources
     23
     24  * XXX how does the PSF-convolved model work with changing psf models?
     25
     2620101207
     27
     28  header fields for PSPS:
     29
     30  * stack needs to count # of inputs
     31  * propagate the input header to the output image (DONE)
     32  * save PSF parameters in header
     33  * stack type is only known to the launcher
     34
     35  * what are we doing for the stack calibrations??
     36
     37skycellID    : code
     38surveyID     : code
     39filterID     : code
     40stackMetaID  : stack_id
     41photoCalID   : photcode -> number
     42magSat       : FSATUR       saturation magnitude level ** not correctly set
     43completMag   : FLIMIT       95% completion level in mag ** not correctly set
     44stackTypeID  : STK_TYPE     stack type identifier ** deep stack, nightly stack, best IQ stack?
     45refImageID   :              identifier of image used as reference for analysis
     46subtrImageID : N/A (stack)  identifier of image subtracted to generate difference image
     47analVer      :              analysis version index  ** index for tess_id + skycell_id + filter?
     48nP2Images    : NINPUTS      number of P2 images contributing to this cell ** missing from input stack
     49astroScat    :              astrometric scatter for chip ** measure scatter on stack creation?
     50photoScat    :              photometric scatter for chip ** internal scatter? 
     51nAstroRef    :              number of astrometric reference sources ** (connected to above)
     52nPhoRef      :              number of photometric reference sources ** same
     53psfFwhm      :              PSF full width at half maximum ** 0.5(FWHM_MAJ + FWHM_MIN)
     54psfmodelID   : * PSFMODEL   PSF model identifier ** save the PSF model name in the header
     55psfWidMajor  : * FWHM_MAJ     PSF parameters
     56psfWidMinor  : * FWHM_MIN     PSF parameters
     57psfTheta     : * ANGLE        PSF parameters
     58psfExtra1    : * PSF_EXT1   PSF parameters ** (at field center?)
     59psfExtra2    : * PSF_EXT2   PSF parameters ** (not set for all models)
     60photoZero    :              local derived photometric zero point
     61photoColor   :              local derived photometric color term
     62ctype1       : * CTYPE1             name of astrometric projection in RA ** propagate from input stacks
     63ctype2       : * CTYPE2             name of astrometric projection in DEC
     64crval1       : * CRVAL1             RA corresponding to reference pixel
     65crval2       : * CRVAL2             DEC corresponding to reference pixel
     66crpix1       : * CRPIX1             reference pixel value for RA
     67crpix2       : * CRPIX2             reference pixel value for DEC
     68cdelt1       : * CDELT1             scale factor for RA
     69cdelt2       : * CDELT2             scale factor for DEC
     70pc001001     : * PC001001           elements of rotation/Dcale matrix
     71pc001002     : * PC001002           elements of rotation/Dcale matrix
     72pc002001     : * PC002001           elements of rotation/Dcale matrix
     73pc002002     : * PC002002           elements of rotation/Dcale matrix
     74polyOrder    : * NPLYTERM           polynomial order of astrometry fit ** default to 1
     75pca1x3y0     : * PCA1X3Y0     polynomial coefficients for the astrometric fit
     76pca1x2y1     : * PCA1X2Y1     polynomial coefficients for the astrometric fit
     77pca1x1y2     : * PCA1X1Y2     polynomial coefficients for the astrometric fit
     78pca1x0y3     : * PCA1X0Y3     polynomial coefficients for the astrometric fit
     79pca1x2y0     : * PCA1X2Y0     polynomial coefficients for the astrometric fit
     80pca1x1y1     : * PCA1X1Y1     polynomial coefficients for the astrometric fit
     81pca1x0y2     : * PCA1X0Y2     polynomial coefficients for the astrometric fit
     82pca2x3y0     : * PCA2X3Y0     polynomial coefficients for the astrometric fit
     83pca2x2y1     : * PCA2X2Y1     polynomial coefficients for the astrometric fit
     84pca2x1y2     : * PCA2X1Y2     polynomial coefficients for the astrometric fit
     85pca2x0y3     : * PCA2X0Y3     polynomial coefficients for the astrometric fit
     86pca2x2y0     : * PCA2X2Y0     polynomial coefficients for the astrometric fit
     87pca2x1y1     : * PCA2X1Y1     polynomial coefficients for the astrometric fit
     88pca2x0y2     : * PCA2X0Y2     polynomial coefficients for the astrometric fit
     89calibModNum  :              calibration modification number
     90dataRelease  :              Data release
    191
    29220100506:
  • branches/czw_branch/20101203/psphot/src/Makefile.am

    r30118 r30631  
    9898        psphotStackMatchPSFsUtils.c   \
    9999        psphotStackMatchPSFsPrepare.c \
     100        psphotStackMatchPSFsNext.c    \
    100101        psphotStackOptions.c          \
    101102        psphotStackObjects.c          \
  • branches/czw_branch/20101203/psphot/src/psphot.h

    r30118 r30631  
    7575bool            psphotImageQualityReadout(pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
    7676
    77 bool            psphotChoosePSF (pmConfig *config, const pmFPAview *view, const char *filerule);
    78 bool            psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     77bool            psphotChoosePSF (pmConfig *config, const pmFPAview *view, const char *filerule, bool newSources);
     78bool            psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool newSources);
    7979
    8080bool            psphotGuessModels (pmConfig *config, const pmFPAview *view, const char *filerule);
     
    260260bool            psphotVisualShowSourceSize (pmReadout *readout, psArray *sources);
    261261bool            psphotVisualPlotSourceSizeAlt (psMetadata *recipe, psMetadata *analysis, psArray *sources);
    262 bool            psphotVisualShowResidualImage (pmReadout *readout);
     262bool            psphotVisualShowResidualImage (pmReadout *readout, bool reshow);
     263bool            psphotVisualShowObjectRegions (pmReadout *readout, psMetadata *recipe, psArray *sources);
    263264bool            psphotVisualPlotApResid (psArray *sources, float mean, float error, bool useApMag);
    264265bool            psphotVisualPlotChisq (psArray *sources);
     
    312313
    313314int psphotFileruleCount(const pmConfig *config, const char *filerule);
     315bool psphotFileruleCountSet(const pmConfig *config, const char *filerule, int num);
    314316
    315317bool psphotAddKnownSources (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *inSources);
     
    403405bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
    404406bool psphotStackMatchPSFsPrepare (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index);
     407bool psphotStackMatchPSFsNext (bool *smoothAgain, pmConfig *config, const pmFPAview *view, const char *filerule, int lastSize);
     408bool psphotStackMatchPSFsNextReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, psVector *fwhmValues, int lastSize);
    405409
    406410// psphotStackMatchPSFsUtils
     
    426430bool psphotRadialApertures (pmConfig *config, const pmFPAview *view, const char *filerule);
    427431bool psphotRadialAperturesReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
    428 bool psphotRadialApertureSource (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *radMax);
     432bool psphotRadialApertureSource (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *radMax, int entry);
    429433
    430434bool psphotExtendedSourceAnalysisByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule);
    431 bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule);
     435bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule, int nMatchedPSF);
    432436
    433437bool psphotStackObjectsUnifyPosition (psArray *objects);
     
    440444bool psphotCleanInputs (pmConfig *config, const pmFPAview *view, const char *filerule);
    441445
     446bool psphotResetModels (pmConfig *config, const pmFPAview *view, const char *filerule);
     447bool psphotResetModelsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     448
     449bool psphotRedefinePixels (pmConfig *config, const pmFPAview *view, const char *filerule);
     450bool psphotRedefinePixelsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     451
     452bool psphotSourceChildren (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc);
     453bool psphotSourceChildrenReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index);
     454psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc);
     455
    442456#endif
  • branches/czw_branch/20101203/psphot/src/psphotApResid.c

    • Property svn:mergeinfo deleted
  • branches/czw_branch/20101203/psphot/src/psphotBlendFit.c

    r30118 r30631  
    187187    psLogMsg ("psphot.psphotBlendFit", PS_LOG_INFO, "fit models: %f sec for %d objects (%d psf, %d ext, %d failed, %ld skipped)\n", psTimerMark ("psphot.fit.nonlinear"), Nfit, Npsf, Next, Nfail, sources->n - Nfit);
    188188
    189     psphotVisualShowResidualImage (readout);
     189    psphotVisualShowResidualImage (readout, false);
     190    psphotVisualShowObjectRegions (readout, recipe, sources);
    190191    psphotVisualShowFlags (sources);
    191192
  • branches/czw_branch/20101203/psphot/src/psphotCheckStarDistribution.c

    r19909 r30631  
    3434        pmSource *source = stars->data[i];
    3535        if (source->peak == NULL) continue;
    36         if (!(source->mode & PM_SOURCE_MODE_PSFSTAR)) continue;
     36        if (!(source->tmpFlags & PM_SOURCE_TMPF_CANDIDATE_PSFSTAR)) continue;
    3737
    3838        int binX = psImageBinningGetRuffX (binning, source->peak->xf);
     
    8585        if (source->peak == NULL) continue;
    8686        if (source->moments == NULL) continue;
    87         if (source->mode & PM_SOURCE_MODE_PSFSTAR) continue;
     87        if (source->tmpFlags & PM_SOURCE_TMPF_CANDIDATE_PSFSTAR) continue;
    8888        if (source->mode & PM_SOURCE_MODE_SATSTAR) continue;
    8989        if (source->type != PM_SOURCE_TYPE_STAR) continue;
     
    9797        if (y > Ye) continue;
    9898
    99         source->mode |= PM_SOURCE_MODE_PSFSTAR;
     99        source->tmpFlags |= PM_SOURCE_TMPF_CANDIDATE_PSFSTAR;
    100100        psArrayAdd (stars, 200, source);
    101101
  • branches/czw_branch/20101203/psphot/src/psphotChoosePSF.c

    r30118 r30631  
    22
    33// generate a PSF model for inputs without PSF models already loaded
    4 bool psphotChoosePSF (pmConfig *config, const pmFPAview *view, const char *filerule)
     4bool psphotChoosePSF (pmConfig *config, const pmFPAview *view, const char *filerule, bool newSources)
    55{
    66    bool status = true;
     
    1919    for (int i = 0; i < num; i++) {
    2020        if (i == chisqNum) continue; // skip chisq image
    21         if (!psphotChoosePSFReadout (config, view, filerule, i, recipe)) {
     21        if (!psphotChoosePSFReadout (config, view, filerule, i, recipe, newSources)) {
    2222            psError (PSPHOT_ERR_CONFIG, false, "failed to choose a psf model for %s entry %d", filerule, i);
    2323            return false;
     
    2828
    2929// try PSF models and select best option
    30 bool psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     30bool psphotChoosePSFReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe, bool newSources) {
    3131
    3232    bool status;
     
    5050    psAssert (detections, "missing detections?");
    5151
    52     psArray *sources = detections->newSources;
     52    psArray *sources = newSources ? detections->newSources : detections->allSources;
    5353    psAssert (sources, "missing sources?");
    5454
     
    166166    for (int i = 0; i < sources->n; i++) {
    167167        pmSource *source = sources->data[i];
    168         if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
    169             // keep NSTARS PSF stars, unmark the rest
     168        if (source->tmpFlags & PM_SOURCE_TMPF_CANDIDATE_PSFSTAR) {
     169            // keep NSTARS PSF stars
    170170            if (stars->n < NSTARS) {
    171171                psArrayAdd (stars, 200, source);
    172             } else {
    173                 source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    174172            }
    175173        }
     
    214212        psFree(options);
    215213
    216         // unset the PSFSTAR flags (none are used):
    217         for (int i = 0; i < sources->n; i++) {
    218             pmSource *source = sources->data[i];
    219             source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    220         }
     214        // no sources are used as PSF stars
    221215
    222216        // XXX set sxx, etc from FWHM in recipe
     
    292286        psLogMsg ("psphot.pspsf", PS_LOG_INFO, "Using guess PSF model");
    293287
    294         // unset the PSFSTAR flags (none are used):
    295         for (int i = 0; i < sources->n; i++) {
    296             pmSource *source = sources->data[i];
    297             source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    298         }
     288        // no sources are used as PSF stars
    299289
    300290        // XXX set sxx, etc from FWHM in recipe
     
    322312    pmPSFtry *try = models->data[bestN];
    323313
    324     // unset the PSFSTAR flag for stars not used for PSF model
    325     // XXX a more efficient way of achieving this would be to record a pair of arrays
    326     // of the source index and the source id for the psf stars.  but that would require we do
    327     // not re-sort the source list in the meanwhile
    328     int nDrop = 0;
     314    // set the PSFSTAR flag for stars used for the PSF model
     315    int nKeep = 0;
    329316    for (int i = 0; i < try->sources->n; i++) {
    330317        pmSource *source = try->sources->data[i];
    331         if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
    332             // need to find this source in the original list (these are copies, not pointers)
    333             for (int j = 0; j < sources->n; j++) {
    334                 pmSource *realSource = sources->data[j];
    335                 if (realSource->id != source->id) continue;
    336                 realSource->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    337                 source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    338                 nDrop ++;
    339                 break;
    340             }
    341         }
    342     }
    343     // fprintf (stderr, "drop %d stars as PSF stars\n", nDrop);
    344 
    345     // XXX is this working?
    346     // int N1 = 0;
    347     // for (int i = 0; i < try->sources->n; i++) {
    348     //     pmSource *source = try->sources->data[i];
    349     //  fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
    350     //  if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
    351     //      N1 ++;
    352     //     }
    353     // }
    354     // int N2 = 0;
    355     // for (int i = 0; i < sources->n; i++) {
    356     //     pmSource *source = sources->data[i];
    357     //  fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
    358     //  if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
    359     //      N2 ++;
    360     //     }
    361     // }
    362     // fprintf (stderr, "N1: %d, N2: %d\n", N1, N2);
     318        if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) continue;
     319        source->mode |= PM_SOURCE_MODE_PSFSTAR;
     320
     321        // this source was used: find the parent and set its PSFSTAR flag
     322        pmSource *realSource = source->parent;
     323        psAssert (realSource, "pmPSFtryAlloc should have set the parent pointers");
     324        realSource->mode |= PM_SOURCE_MODE_PSFSTAR;
     325        nKeep ++;
     326    }
     327    psLogMsg ("psphot.pspsf", PS_LOG_DETAIL, "used %d of %ld candidate PSF objects\n", nKeep, try->sources->n);
    363328
    364329    // build a PSF residual image
     330    // we need to use the 'try' set because they have the associated models defined
    365331    if (!psphotMakeResiduals (try->sources, recipe, try->psf, maskVal)) {
    366332        psError(PSPHOT_ERR_PSF, false, "Unable to construct residual table for PSF");
     
    386352    }
    387353
    388     // XXX test dump of psf star data and psf-subtracted image
     354    // test dump of psf star data and psf-subtracted image
    389355    if (psTraceGetLevel("psphot.psfstars") > 5) {
    390356        psphotDumpPSFStars (readout, try, options->fitRadius, maskVal, markVal);
     
    413379        return false;
    414380    }
    415     psFree (psf); // XXX double-check
    416 
    417     // move into psphotChoosePSF
     381    psFree (psf);
     382
    418383    psphotVisualShowPSFModel (readout, psf);
    419384
     
    436401    psVector *fwhmMajor = psVectorAllocEmpty (100, PS_DATA_F32);
    437402    psVector *fwhmMinor = psVectorAllocEmpty (100, PS_DATA_F32);
     403    psVector *psfExtra1 = psVectorAllocEmpty (100, PS_DATA_F32);
     404    psVector *psfExtra2 = psVectorAllocEmpty (100, PS_DATA_F32);
    438405
    439406    for (float ix = -0.4; ix <= +0.4; ix += 0.1) {
     
    459426            shape.sxy = modelPSF->params->data.F32[PM_PAR_SXY];
    460427            axes = psEllipseShapeToAxes (shape, 20.0);
    461             psFree (modelPSF);
    462428
    463429            float FWHM_MINOR = FWHM_MAJOR * (axes.minor / axes.major);
    464430            if (!isfinite(FWHM_MAJOR) || !isfinite(FWHM_MINOR)) {
    465431                fprintf (stderr, "!");
     432                psFree (modelPSF);
    466433                continue;
    467434            }
    468435            psVectorAppend (fwhmMajor, FWHM_MAJOR);
    469436            psVectorAppend (fwhmMinor, FWHM_MINOR);
     437
     438            if (modelPSF->params->n >= 7) {
     439              psVectorAppend (psfExtra1, modelPSF->params->data.F32[7]);
     440            }
     441            if (modelPSF->params->n >= 8) {
     442              psVectorAppend (psfExtra2, modelPSF->params->data.F32[8]);
     443            }
     444            psFree (modelPSF);
    470445        }
    471446    }
     
    474449    if (!psVectorStats (stats, fwhmMajor, NULL, NULL, 0)) {
    475450        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for FWHM MAJOR");
    476         return false;
     451        goto escape;
    477452    }
    478453
     
    486461    if (!psVectorStats (stats, fwhmMinor, NULL, NULL, 0)) {
    487462        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for FWHM MINOR");
    488         return false;
     463        goto escape;
    489464    }
    490465    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FWHM_MIN",   PS_META_REPLACE, "PSF FWHM Minor axis (mean)", stats->sampleMean);
     
    494469
    495470    float fwhmMin = stats->sampleMean;  // FWHM on minor axis
     471
    496472    if (readout->parent) {
     473
     474        // we now have 2 possible measurements of the seeing : the PSF model based version and
     475        // the Moments based value we need to define a definitive "CHIP.SEEING" value.  This
     476        // value is used by the PSF-matching programs (ppSub and ppStack).  The moments-based
     477        // value is probably more representative of the value needed by those tools (it is also
     478        // more stable?)
     479        bool status = false;
     480        float fwhmMajorMoments = psMetadataLookupF32(&status, readout->analysis, "IQ_FW1");
     481        float fwhmMinorMoments = psMetadataLookupF32(&status, readout->analysis, "IQ_FW1");
     482
    497483        pmChip *chip = readout->parent->parent; // Parent chip
    498484        psAssert(chip, "Cell should be attached to a chip.");
    499485        psMetadataItem *item = psMetadataLookup(chip->concepts, "CHIP.SEEING"); // Item with chip
    500         item->data.F32 = 0.5 * (fwhmMaj + fwhmMin);
     486        item->data.F32 = 0.5 * (fwhmMajorMoments + fwhmMinorMoments);
     487
     488        psLogMsg ("psphot", PS_LOG_DETAIL, "fwhm (psf): %f,%f (moments): %f,%f", fwhmMaj, fwhmMin, fwhmMajorMoments, fwhmMinorMoments);
     489    }
     490
     491    if (psfExtra1->n) {
     492      if (!psVectorStats (stats, psfExtra1, NULL, NULL, 0)) {
     493        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for PSF EXTRA 1");
     494        goto escape;
     495      }
     496      psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "PSF_EXT1", PS_META_REPLACE, "PSF extra param 1", stats->sampleMean);
     497    }
     498
     499    if (psfExtra2->n) {
     500      if (!psVectorStats (stats, psfExtra2, NULL, NULL, 0)) {
     501        psError(PS_ERR_UNKNOWN, false, "failure to measure stats for PSF EXTRA 2");
     502        goto escape;
     503      }
     504      psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "PSF_EXT2", PS_META_REPLACE, "PSF extra param 2", stats->sampleMean);
    501505    }
    502506
    503507    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           axes.theta);
    504508    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", psf->nPSFstars);
    505     psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", true);
     509
     510    char *psfModelName = pmModelClassGetName(psf->type);
     511    psMetadataAddStr(readout->analysis,  PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "PSF Model Name", psfModelName);
     512    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSF_OK",   PS_META_REPLACE, "Valid PSF Model?", true);
     513
     514    int nParams = pmModelClassParameterCount(psf->type);
     515    psMetadataAddS32(readout->analysis, PS_LIST_TAIL, "PSF_NPAR",   PS_META_REPLACE, "Number of PSF parameters", nParams);
    506516
    507517    psFree (fwhmMajor);
    508518    psFree (fwhmMinor);
     519    psFree (psfExtra1);
     520    psFree (psfExtra2);
    509521    psFree (stats);
    510522    return true;
     523
     524escape:
     525    psFree (fwhmMajor);
     526    psFree (fwhmMinor);
     527    psFree (psfExtra1);
     528    psFree (psfExtra2);
     529    psFree (stats);
     530    return false;
    511531}
    512532
     
    568588    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_LQ",   PS_META_REPLACE, "PSF FWHM Minor axis (lower quartile)", 0);
    569589    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "FW_MN_UQ",   PS_META_REPLACE, "PSF FWHM Minor axis (upper quartile)", 0);
    570     psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",    PS_META_REPLACE, "PSF angle",           FWHM_T);
    571     psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR", PS_META_REPLACE, "Number of stars used to make PSF", 0);
    572     psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSFMODEL", PS_META_REPLACE, "Valid PSF Model?", false);
     590    psMetadataAddF32 (readout->analysis, PS_LIST_TAIL, "ANGLE",      PS_META_REPLACE, "PSF angle",           FWHM_T);
     591    psMetadataAddS32 (readout->analysis, PS_LIST_TAIL, "NPSFSTAR",   PS_META_REPLACE, "Number of stars used to make PSF", 0);
     592    psMetadataAddStr(readout->analysis,  PS_LIST_TAIL, "PSFMODEL",   PS_META_REPLACE, "PSF Model Name", "NONE");
     593    psMetadataAddBool(readout->analysis, PS_LIST_TAIL, "PSF_OK",     PS_META_REPLACE, "Valid PSF Model?", false);
    573594
    574595    return true;
  • branches/czw_branch/20101203/psphot/src/psphotExtendedSourceAnalysis.c

    r30118 r30631  
    173173    psLogMsg ("psphot", PS_LOG_INFO, "  %d kron\n", Nkron);
    174174
    175     psphotVisualShowResidualImage (readout);
     175    psphotVisualShowResidualImage (readout, false);
    176176
    177177    if (doPetrosian) {
  • branches/czw_branch/20101203/psphot/src/psphotExtendedSourceAnalysisByObject.c

    r30118 r30631  
    5353        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    5454        psAssert (readout, "missing readout?");
     55
     56        psLogMsg("psphot", PS_LOG_INFO, "petrosians for image %d", i);
     57        psphotVisualShowImage(readout);
    5558
    5659        readouts->data[i] = psMemIncrRefCounter(readout);
  • branches/czw_branch/20101203/psphot/src/psphotExtendedSourceFits.c

    r30118 r30631  
    1818    int num = psphotFileruleCount(config, filerule);
    1919
     20    // skip the chisq image (optionally?)
     21    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     22    if (!status) chisqNum = -1;
     23
    2024    // loop over the available readouts
    2125    for (int i = 0; i < num; i++) {
     26        if (i == chisqNum) continue; // skip chisq image
    2227        if (!psphotExtendedSourceFitsReadout (config, view, filerule, i, recipe)) {
    2328            psError (PSPHOT_ERR_CONFIG, false, "failed on to fit extended sources for %s entry %d", filerule, i);
     
    3742    int Nplain = 0;
    3843    int NplainPass = 0;
     44    int Nfaint = 0;
    3945
    4046    psTimerStart ("psphot.extended");
     
    4652    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    4753    psAssert (readout, "missing readout?");
     54
     55    psLogMsg("psphot", PS_LOG_INFO, "extended source fits for image %d", index);
     56    psphotVisualShowImage(readout);
    4857
    4958    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     
    167176            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nplain
    168177            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for NplainPass
     178            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfain
    169179
    170180            if (false && !psThreadJobAddPending(job)) {
     
    189199                scalar = job->args->data[11];
    190200                NplainPass += scalar->data.S32;
     201                scalar = job->args->data[12];
     202                Nfaint += scalar->data.S32;
    191203                psFree(job);
    192204            }
     
    217229                scalar = job->args->data[11];
    218230                NplainPass += scalar->data.S32;
     231                scalar = job->args->data[12];
     232                Nfaint += scalar->data.S32;
    219233            }
    220234            psFree(job);
     
    227241    psLogMsg ("psphot", PS_LOG_INFO, "  %d convolved models (%d passed)\n", Nconvolve, NconvolvePass);
    228242    psLogMsg ("psphot", PS_LOG_INFO, "  %d plain models (%d passed)\n", Nplain, NplainPass);
     243    psLogMsg ("psphot", PS_LOG_INFO, "  %d too faint to fit\n", Nfaint);
    229244    return true;
    230245}
     
    238253    int NconvolvePass = 0;
    239254    int Nplain = 0;
     255    int Nfaint = 0;
    240256    int NplainPass = 0;
    241257    bool savePics = false;
     
    326342
    327343        // array to store the pointers to the model flux images while the models are being fitted
    328         psArray *modelFluxes = psArrayAllocEmpty (4);
     344        psArray *modelFluxes = psArrayAllocEmpty (models->list->n);
    329345
    330346        // allocate the array to store the model fits
    331347        if (source->modelFits == NULL) {
    332             source->modelFits = psArrayAllocEmpty (4);
     348            source->modelFits = psArrayAllocEmpty (models->list->n);
    333349        }
    334350
     
    350366          // limit selection to some SN limit
    351367          assert (source->peak); // how can a source not have a peak?
    352           if (source->peak->SN < SNlim) continue;
     368          if (source->peak->SN < SNlim) {
     369              Nfaint ++;
     370              continue;
     371          }
    353372
    354373          // check on the model type
     
    497516    scalar->data.S32 = NplainPass;
    498517
     518    scalar = job->args->data[12];
     519    scalar->data.S32 = Nfaint;
     520
    499521    return true;
    500522}
  • branches/czw_branch/20101203/psphot/src/psphotFindDetections.c

    r30118 r30631  
    8686    psImage *significance = psphotSignificanceImage (readout, recipe, maskVal);
    8787
    88     // display the significance image
    89     psphotVisualShowSignificance (significance, -1.0, PS_SQR(3.0*NSIGMA_PEAK));
    90 
    9188    // display the log significance image
    9289    psphotVisualShowLogSignificance (significance, 0.0, 4.5);
     90
     91    // display the significance image
     92    psphotVisualShowSignificance (significance, 0.98*threshold, 1.02*threshold);
    9393
    9494    // detect the peaks in the significance image
     
    108108    }
    109109
     110    // XXX do a second (or third?) pass with rebinning (to detected more extended sources)
     111
    110112    psFree (significance);
    111113
  • branches/czw_branch/20101203/psphot/src/psphotFindFootprints.c

    r27673 r30631  
    2121    psArray *footprints = pmFootprintsFind (significance, threshold, npixMin);
    2222
    23     pmFootprintsAssignPeaks(footprints, detections->peaks);
    24     // XXX handle the error conditions here
     23    if (pmFootprintsAssignPeaks(footprints, detections->peaks) != PS_ERR_NONE) {
     24        psAbort ("inconsistent peaks and footprints");
     25    }
    2526
    2627    // footprints now owns the peaks; after culling (below), we will rebuild the peaks array
  • branches/czw_branch/20101203/psphot/src/psphotFitSourcesLinear.c

    r30118 r30631  
    4646            return false;
    4747        }
     48
     49        psphotVisualShowResidualImage (readout, (num > 0));
     50        psphotVisualShowPeaks (detections);
     51        psphotVisualShowObjectRegions (readout, recipe, sources);
    4852    }
    4953    return true;
     
    151155        if (x > AnalysisRegion.x1) continue;
    152156        if (y > AnalysisRegion.y1) continue;
     157
     158        // check the integral of the model : is it large enough?
     159        float modelSum = 0.0;
     160        for (int iy = 0; iy < source->modelFlux->numRows; iy++) {
     161            for (int ix = 0; ix < source->modelFlux->numCols; ix++) {
     162                modelSum += source->modelFlux->data.F32[iy][ix];
     163            }
     164        }
     165        if (modelSum < 0.5) continue; // skip sources with no model constraint (somewhat arbitrary limit)
     166        // if (modelSum < 0.01) continue; // skip sources with no model constraint (somewhat arbitrary limit)
     167        if (modelSum < 0.8) {
     168            fprintf (stderr, "low-sig model @ %f, %f (%f sum, %f peak)\n",
     169                     source->peak->xf, source->peak->yf, modelSum, source->peak->flux);
     170        }
     171
     172        pmModel *model = pmSourceGetModel (NULL, source);
     173
     174        psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
     175        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, model->fitRadius, "OR", markVal);
    153176
    154177        source->mode |= PM_SOURCE_MODE_LINEAR_FIT;
     
    270293        model->params->data.F32[PM_PAR_I0] = norm->data.F32[i];
    271294        model->dparams->data.F32[PM_PAR_I0] = errors->data.F32[i];
    272         // XXX is the value of 'errors' modified by the sky fit?
    273295
    274296        // subtract object
     
    297319    psLogMsg ("psphot.ensemble", PS_LOG_INFO, "measure ensemble of PSFs: %f sec\n", psTimerMark ("psphot.linear"));
    298320
    299     psphotVisualShowResidualImage (readout);
    300321    psphotVisualPlotChisq (sources);
    301322    // psphotVisualShowFlags (sources);
  • branches/czw_branch/20101203/psphot/src/psphotFitSourcesLinearStack.c

    r29548 r30631  
    176176
    177177    psLogMsg ("psphot.ensemble", PS_LOG_INFO, "measure ensemble of PSFs: %f sec\n", psTimerMark ("psphot.linear"));
    178 
    179178    return true;
    180179}
  • branches/czw_branch/20101203/psphot/src/psphotImageQuality.c

    r30118 r30631  
    7979        // select by PSFSTAR mode?
    8080        // ??
    81         if (source->type != PM_SOURCE_TYPE_STAR || !(source->mode & PM_SOURCE_MODE_PSFSTAR)) {
     81        if (source->type != PM_SOURCE_TYPE_STAR || !(source->tmpFlags & PM_SOURCE_TMPF_CANDIDATE_PSFSTAR)) {
    8282            psTrace("psphot", 10, "Ignoring source for image quality because not a good star");
    8383            continue;
     
    133133    if (num == 0) {
    134134        psLogMsg ("psphot", PS_LOG_INFO, "no valid sources for image quality, skipping");
     135        psFree(FWHM_MAJOR);
     136        psFree(FWHM_MINOR);
     137        psFree(M2);
     138        psFree(M2c);
     139        psFree(M2s);
     140        psFree(M3);
     141        psFree(M4);
     142
    135143        return true;
    136144    }
  • branches/czw_branch/20101203/psphot/src/psphotMakeFluxScale.c

    • Property svn:mergeinfo deleted
  • branches/czw_branch/20101203/psphot/src/psphotMakePSFReadout.c

    r30118 r30631  
    6060    // Use bright stellar objects to measure PSF. If we do not have enough stars to generate
    6161    // the PSF, build one from the SEEING guess and model class
    62     if (!psphotChoosePSF (config, view, filerule)) {
     62    if (!psphotChoosePSF (config, view, filerule, true)) {
    6363        psLogMsg ("psphot", 3, "failure to construct a psf model");
    6464        return psphotReadoutCleanup (config, view, filerule);
  • branches/czw_branch/20101203/psphot/src/psphotMakeResiduals.c

    r27532 r30631  
    136136        psFree (interp);
    137137    }
     138    xSize = PS_MIN(xSize, 2*radiusMax+3);
     139    ySize = PS_MIN(ySize, 2*radiusMax+3);
     140
    138141    pmResiduals *resid = pmResidualsAlloc (xSize, ySize, xBin, yBin);
    139142    psImageInit (resid->mask, 0);
     
    316319    psFree (B);
    317320
    318     psLogMsg ("psphot.pspsf", PS_LOG_MINUTIA, "generate residuals for %ld objects: %f sec\n", input->n, psTimerMark ("psphot.residuals"));
     321    psLogMsg ("psphot.pspsf", PS_LOG_DETAIL, "generate residuals for %ld objects: %f sec\n", input->n, psTimerMark ("psphot.residuals"));
    319322
    320323    psFree (xC);
  • branches/czw_branch/20101203/psphot/src/psphotMergeSources.c

    r30118 r30631  
    513513    }
    514514
    515     return true;
    516 }
    517 
     515    // loop over the sources, redefine their pixels to point at the new filerule image,
     516    // copy the source data, and add a reference back to the original source
     517   
     518
     519    return true;
     520}
     521
     522// create source children from ruleSrc for ruleOut
     523bool psphotSourceChildren (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 (!psphotSourceChildrenReadout (config, view, ruleOut, ruleSrc, i)) {
     537            psError (PSPHOT_ERR_CONFIG, false, "failed to copy sources from %s to %s entry %d", ruleSrc, ruleOut, i);
     538            return false;
     539        }
     540    }
     541    return true;
     542}
     543
     544// create source children from ruleSrc for ruleOut for this entry.  XXX currently, this is only
     545// used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be called
     546// repeatedly)
     547bool psphotSourceChildrenReadout (pmConfig *config, const pmFPAview *view, const char *ruleOut, const char *ruleSrc, int index) {
     548
     549    bool status;
     550
     551    // find the currently selected readout
     552    pmFPAfile *fileSrc = pmFPAfileSelectSingle(config->files, ruleSrc, index); // File of interest
     553    psAssert (fileSrc, "missing file?");
     554
     555    pmReadout *readoutSrc = pmFPAviewThisReadout(view, fileSrc->fpa);
     556    psAssert (readoutSrc, "missing readout?");
     557
     558    pmDetections *detectionsSrc = psMetadataLookupPtr (&status, readoutSrc->analysis, "PSPHOT.DETECTIONS");
     559    psAssert (detectionsSrc, "missing detections?");
     560
     561    psArray *sourcesSrc = detectionsSrc->allSources;
     562    psAssert (sourcesSrc, "missing sources?");
     563
     564    // find the currently selected readout
     565    pmFPAfile *fileOut = pmFPAfileSelectSingle(config->files, ruleOut, index); // File of interest
     566    psAssert (fileOut, "missing file?");
     567
     568    pmReadout *readoutOut = pmFPAviewThisReadout(view, fileOut->fpa);
     569    psAssert (readoutOut, "missing readout?");
     570
     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
     582    }
     583
     584    // loop over the sources, redefine their pixels to point at the new filerule image,
     585    // copy the source data, and add a reference back to the original source
     586   
     587    // copy the sources from sourceSrcs to the new detection structure
     588    for (int i = 0; i < sourcesSrc->n; i++) {
     589      pmSource *sourceSrc = sourcesSrc->data[i];
     590
     591      pmSource *sourceOut = pmSourceCopy(sourceSrc);
     592      sourceOut->parent = sourceSrc;
     593     
     594      // keep the original source flags
     595      sourceOut->type     = sourceSrc->type;
     596      sourceOut->mode     = sourceSrc->mode;
     597      sourceOut->mode2    = sourceSrc->mode2;
     598      sourceOut->tmpFlags = sourceSrc->tmpFlags;
     599
     600      // does this copy all model data? (NO)
     601      sourceOut->modelPSF = pmModelCopy(sourceSrc->modelPSF);
     602      sourceOut->modelEXT = pmModelCopy(sourceSrc->modelEXT);
     603
     604      if (sourceSrc->modelFits) {
     605          sourceOut->modelFits = psArrayAlloc(sourceSrc->modelFits->n);
     606          for (int j = 0; j < sourceSrc->modelFits->n; j++) {
     607              sourceOut->modelFits->data[j] = pmModelCopy(sourceSrc->modelFits->data[j]);
     608          }
     609      }
     610
     611      // drop the references to the original image pixels:
     612      pmSourceFreePixels (sourceOut);
     613
     614      // allocate image, weight, mask for the new image for each peak
     615      pmSourceRedefinePixels (sourceOut, readoutOut, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
     616
     617      // child sources have not been subtracted in this image, but this flag may be raised if
     618      // they were subtracted in the parent's image
     619      sourceOut->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     620
     621      psArrayAdd (detectionsOut->allSources, 100, sourceOut);
     622      psFree (sourceOut);
     623    }
     624    psLogMsg ("psphot", 3, "%ld known sources supplied", detectionsOut->allSources->n);
     625
     626
     627    return true;
     628}
     629
     630// create source children associated with 'filerule' from the objectsSrc.  XXX currently, this
     631// is only used by psphotStackReadout (sources go on allSources so that psphotChoosePSF can be
     632// called repeatedly)
     633psArray *psphotSourceChildrenByObject (pmConfig *config, const pmFPAview *view, const char *filerule, psArray *objectsSrc) {
     634
     635    bool status;
     636
     637    int nImages = psphotFileruleCount(config, filerule);
     638
     639    // generate look-up arrays for detections and readouts
     640    psArray *detArrays = psArrayAlloc(nImages);
     641    psArray *readouts = psArrayAlloc(nImages);
     642
     643    for (int i = 0; i < nImages; i++) {
     644
     645        // find the currently selected readout
     646        pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
     647        psAssert (file, "missing file?");
     648
     649        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     650        psAssert (readout, "missing readout?");
     651
     652        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     653        if (!detections) {
     654            detections = pmDetectionsAlloc();
     655            detections->allSources = psArrayAllocEmpty (100);
     656            detections->peaks = psArrayAllocEmpty (100);
     657            // save detections on the readout->analysis
     658            if (!psMetadataAddPtr (readout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detections)) {
     659                psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout");
     660                return NULL;
     661            }
     662            psFree(detections); // a copy remains on the analysis metadata
     663            psAssert (detections, "missing detections?");
     664        }
     665        detArrays->data[i] = psMemIncrRefCounter(detections);
     666        readouts->data[i] = psMemIncrRefCounter(readout);
     667    }
     668
     669    psArray *objectsOut = psArrayAlloc(objectsSrc->n);
     670
     671    // copy all sources for each object
     672    for (int k = 0; k < objectsSrc->n; k++) {
     673
     674        pmPhotObj *objectSrc = objectsSrc->data[k];
     675        if (!objectSrc) continue;
     676        if (!objectSrc->sources) continue;
     677
     678        pmPhotObj *objectOut = pmPhotObjAlloc();
     679        objectsOut->data[k] = objectOut;
     680
     681        objectOut->SN = objectSrc->SN;
     682        objectOut->x  = objectSrc->x;
     683        objectOut->y  = objectSrc->y;
     684       
     685        objectOut->sources = psArrayAlloc(objectSrc->sources->n);
     686
     687        // copy the sources from sourceSrcs to the new detection structure
     688        // loop over the sources, redefine their pixels to point at the new filerule image,
     689        // copy the source data, and add a reference back to the original source
     690        for (int i = 0; i < objectSrc->sources->n; i++) {
     691
     692            pmSource *sourceSrc = objectSrc->sources->data[i];
     693
     694            pmSource *sourceOut = pmSourceCopy(sourceSrc);
     695            objectOut->sources->data[i] = sourceOut;
     696
     697            sourceOut->parent = sourceSrc;
     698
     699            // keep the original source flags
     700            sourceOut->type     = sourceSrc->type;
     701            sourceOut->mode     = sourceSrc->mode;
     702            sourceOut->mode2    = sourceSrc->mode2;
     703            sourceOut->tmpFlags = sourceSrc->tmpFlags;
     704
     705            // does this copy all model data? (NO)
     706            sourceOut->modelPSF = pmModelCopy(sourceSrc->modelPSF);
     707            sourceOut->modelEXT = pmModelCopy(sourceSrc->modelEXT);
     708
     709            if (sourceSrc->modelFits) {
     710                sourceOut->modelFits = psArrayAlloc(sourceSrc->modelFits->n);
     711                for (int j = 0; j < sourceSrc->modelFits->n; j++) {
     712                    sourceOut->modelFits->data[j] = pmModelCopy(sourceSrc->modelFits->data[j]);
     713                }
     714            }
     715
     716            // drop the references to the original image pixels:
     717            pmSourceFreePixels (sourceOut);
     718
     719            // set the output readotu
     720            int index = sourceOut->imageID;
     721            if (index >= readouts->n) continue; // skip the sources generated by the chisq image
     722            pmReadout *readout = readouts->data[index];
     723
     724            // allocate image, weight, mask for the new image for each peak
     725            pmSourceRedefinePixels (sourceOut, readout, sourceOut->peak->x, sourceOut->peak->y, sourceOut->modelPSF->fitRadius);
     726
     727            // child sources have not been subtracted in this image, but this flag may be raised if
     728            // they were subtracted in the parent's image
     729            sourceOut->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     730
     731            // set the output detections:
     732            pmDetections *detectionsOut = detArrays->data[index];
     733            psArrayAdd (detectionsOut->allSources, 100, sourceOut);
     734            psArrayAdd (detectionsOut->peaks, 100, sourceOut->peak);
     735        }
     736    }
     737
     738    for (int i = 0; i < nImages; i++) {
     739        pmDetections *detections = detArrays->data[i];
     740        psLogMsg ("psphot", 3, "%ld source children for image %d", detections->allSources->n, i);
     741    }
     742
     743    psFree (detArrays);
     744    psFree (readouts);
     745
     746    return objectsOut;
     747}
     748
  • branches/czw_branch/20101203/psphot/src/psphotOutput.c

    r30587 r30631  
    1515    psFree (name);
    1616    return num;
     17}
     18
     19// convert filerule to filerule.NUM and look up in the config->arguments metadata
     20bool psphotFileruleCountSet(const pmConfig *config, const char *filerule, int num) {
     21
     22    psString name = NULL;
     23    psStringAppend(&name, "%s.NUM", filerule);
     24
     25    bool status = psMetadataAddS32(config->arguments, PS_LIST_TAIL, name, PS_META_REPLACE, "", num);
     26    psFree (name);
     27
     28    return status;
    1729}
    1830
     
    261273    psMetadataItemSupplement (&status, header, analysis, "ANGLE");
    262274
     275    psMetadataItemSupplement (&status, header, analysis, "PSFMODEL");
     276    psMetadataItemSupplement (&status, header, analysis, "PSF_OK");
     277
    263278    // Image Quality measurements
    264279    psMetadataItemSupplement (&status, header, analysis, "IQ_NSTAR");
  • branches/czw_branch/20101203/psphot/src/psphotPetrosianAnalysis.c

    r25755 r30631  
    6464    }
    6565
    66     psphotVisualShowResidualImage (readout);
     66    psphotVisualShowResidualImage (readout, false);
    6767    return true;
    6868}
  • branches/czw_branch/20101203/psphot/src/psphotPetrosianProfile.c

    r25755 r30631  
    6868    // XXX this will only work in the psphot context, not the psphotPetrosianStudy...
    6969    // XXX add the petrosian to the pmSource structure...
    70     // psphotVisualShowResidualImage (readout);
    7170    psphotVisualShowPetrosian (source, petrosian);
    7271
  • branches/czw_branch/20101203/psphot/src/psphotRadialApertures.c

    r30118 r30631  
    102102        if (source->peak->x > AnalysisRegion.x1) continue;
    103103        if (source->peak->y > AnalysisRegion.y1) continue;
     104
     105        // allocate pmSourceExtendedParameters, if not already defined
     106        if (!source->radialAper) {
     107            source->radialAper = psArrayAlloc(1);
     108        }
    104109
    105110        // replace object in image
     
    116121        pmSourceRedefinePixels (source, readout, source->peak->xf, source->peak->yf, 1.5*radius);
    117122
    118         if (!psphotRadialApertureSource (source, recipe, skynoise, maskVal, radMax)) {
     123        if (!psphotRadialApertureSource (source, recipe, skynoise, maskVal, radMax, 0)) {
    119124            psTrace ("psphot", 5, "failed to extract radial profile for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    120125        } else {
     
    130135}
    131136
    132 bool psphotRadialApertureSource (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *radMax) {
    133 
    134     // allocate pmSourceExtendedParameters, if not already defined
    135     if (!source->radial) {
    136         source->radial = pmSourceRadialAperturesAlloc ();
     137bool psphotRadialApertureSource (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *aperRadii, int entry) {
     138
     139    // if we are a child source, save the results to the parent source radial aperture array
     140    psArray *radialAperSet = source->radialAper;
     141    if (source->parent) {
     142        radialAperSet = source->parent->radialAper;
     143    }
     144    psAssert(radialAperSet, "this should be defined before calling");
     145    psAssert(radialAperSet->data[entry] == NULL, "why is this already defined?");
     146
     147    pmSourceRadialApertures *radialAper = pmSourceRadialAperturesAlloc ();
     148    radialAperSet->data[entry] = radialAper;
     149
     150    // storage for the derived pixel values
     151    psVector *pixRadius2 = psVectorAllocEmpty(100, PS_TYPE_F32);
     152    psVector *pixFlux    = psVectorAllocEmpty(100, PS_TYPE_F32);
     153    psVector *pixVar     = psVectorAllocEmpty(100, PS_TYPE_F32);
     154
     155    // outer-most radius for initial truncation
     156    float Rmax  = aperRadii->data.F32[aperRadii->n - 1];
     157    float Rmax2 = PS_SQR(Rmax);
     158
     159    // store the R^2 values for the apertures
     160    psVector *aperRadii2 = psVectorAlloc(aperRadii->n, PS_TYPE_F32);
     161    for (int i = 0; i < aperRadii->n; i++) {
     162        aperRadii2->data.F32[i] = PS_SQR(aperRadii->data.F32[i]);
     163    }
     164
     165    // center of the apertures
     166    float xCM = source->moments->Mx - 0.5 - source->pixels->col0; // coord of peak in subimage
     167    float yCM = source->moments->My - 0.5 - source->pixels->row0; // coord of peak in subimage
     168
     169    // one pass through the pixels to select the valid pixels and calculate R^2
     170    for (int iy = 0; iy < source->pixels->numRows; iy++) {
     171
     172        float yDiff = iy - yCM;
     173        if (fabs(yDiff) > Rmax) continue;
     174
     175        float *vPix = source->pixels->data.F32[iy];
     176        float *vWgt = source->variance->data.F32[iy];
     177        psImageMaskType  *vMsk = (source->maskObj == NULL) ? NULL : source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy];
     178
     179        for (int ix = 0; ix < source->pixels->numCols; ix++, vPix++, vWgt++) {
     180
     181            if (vMsk) {
     182                if (*vMsk & maskVal) {
     183                    vMsk++;
     184                    continue;
     185                }
     186                vMsk++;
     187            }
     188            if (isnan(*vPix)) continue;
     189
     190            float xDiff = ix - xCM;
     191            if (fabs(xDiff) > Rmax) continue;
     192
     193            // radius is just a function of (xDiff, yDiff)
     194            float r2  = PS_SQR(xDiff) + PS_SQR(yDiff);
     195            if (r2 > Rmax2) continue;
     196
     197            psVectorAppend(pixRadius2, r2);
     198            psVectorAppend(pixFlux, *vPix);
     199            psVectorAppend(pixVar, *vWgt);
     200        }
     201    }
     202
     203    psVector *flux    = psVectorAlloc(aperRadii->n, PS_TYPE_F32); // surface brightness of radial bin
     204    psVector *fluxErr = psVectorAlloc(aperRadii->n, PS_TYPE_F32); // surface brightness of radial bin
     205    psVector *fill    = psVectorAlloc(aperRadii->n, PS_TYPE_F32); // surface brightness of radial bin
     206
     207    psVectorInit (flux,    0.0);
     208    psVectorInit (fluxErr, 0.0);
     209    psVectorInit (fill,    0.0);
     210
     211    float *rPix2 = pixRadius2->data.F32;
     212    for (int i = 0; i < pixRadius2->n; i++, rPix2++) {
     213
     214        float *aRad2 = aperRadii2->data.F32;
     215        for (int j = 0; (*rPix2 < *aRad2) && (j < aperRadii2->n); j++, aRad2++) {
     216            flux->data.F32[j]    += pixFlux->data.F32[i];
     217            fluxErr->data.F32[j] += pixVar->data.F32[i];
     218            fill->data.F32[j]    += 1.0;
     219        }
     220    }
     221
     222    for (int i = 0; i < flux->n; i++) {
     223        // calculate the total flux for bin 'nOut'
     224        float Area = M_PI*aperRadii2->data.F32[i];
     225        fluxErr->data.F32[i] = sqrt(fluxErr->data.F32[i]);
     226        fill->data.F32[i] /= Area;
     227        psTrace ("psphot", 5, "radial bins: %3d  %5.1f : %8.1f +/- %7.2f : %4.2f %6.1f\n",
     228                 i, aperRadii->data.F32[i], flux->data.F32[i], fluxErr->data.F32[i], fill->data.F32[i], Area);
    137229    }
    138230   
    139     psVector *radius  = psVectorAllocEmpty(100, PS_TYPE_F32);
     231    radialAper->flux = flux;
     232    radialAper->fluxErr = fluxErr;
     233    radialAper->fill = fill;
     234
     235    psFree (aperRadii2);
     236    psFree (pixRadius2);
     237    psFree (pixFlux);
     238    psFree (pixVar);
     239
     240    return true;
     241}
     242
     243static int nCalls = 0;
     244static int nPass = 0;
     245static int nPix = 0;
     246
     247bool psphotRadialApertureSource_With_Sort (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal, const psVector *radMax, int entry) {
     248
     249    psAssert(source->radialAper->data[entry] == NULL, "why is this already defined?");
     250
     251    pmSourceRadialApertures *radialAper = pmSourceRadialAperturesAlloc ();
     252    source->radialAper->data[entry] = radialAper;
     253
     254    psVector *pixRadius  = psVectorAllocEmpty(100, PS_TYPE_F32);
    140255    psVector *pixFlux = psVectorAllocEmpty(100, PS_TYPE_F32);
    141256    psVector *pixVar  = psVectorAllocEmpty(100, PS_TYPE_F32);
     
    144259        for (int ix = 0; ix < source->pixels->numCols; ix++) {
    145260
    146             // 0.5 PIX: get radius as a function of pixel coord
     261            // 0.5 PIX: get pixRadius as a function of pixel coord
    147262            float x = ix + 0.5 - source->peak->xf + source->pixels->col0;
    148263            float y = iy + 0.5 - source->peak->yf + source->pixels->row0;
     
    150265            float r = hypot(x, y);
    151266
    152             psVectorAppend(radius, r);
     267            psVectorAppend(pixRadius, r);
    153268            psVectorAppend(pixFlux, source->pixels->data.F32[iy][ix]);
    154269            psVectorAppend(pixVar, source->variance->data.F32[iy][ix]);
    155         }
    156     }
    157     psphotRadialAperturesSortFlux(radius, pixFlux, pixVar);
     270            nPix ++;
     271            // if (nPix % 10000 == 0) {fprintf (stderr, "?");}
     272        }
     273    }
     274    psphotRadialAperturesSortFlux(pixRadius, pixFlux, pixVar);
    158275
    159276    psVector *flux    = psVectorAllocEmpty(radMax->n, PS_TYPE_F32); // surface brightness of radial bin
     
    174291
    175292    // XXX assume (or enforce) that the bins are contiguous and non-overlapping (Rmax[i] = Rmin[i+1])
    176     for (int i = 0; !done && (i < radius->n); i++) {
    177         if (radius->data.F32[i] > Rmax) {
     293    for (int i = 0; !done && (i < pixRadius->n); i++) {
     294        if (pixRadius->data.F32[i] > Rmax) {
    178295            // calculate the total flux for bin 'nOut'
    179296            float Area = M_PI*PS_SQR(Rmax);
     
    185302                     nOut, radMax->data.F32[nOut], flux->data.F32[nOut], fluxErr->data.F32[nOut], fill->data.F32[nOut], Area);
    186303
     304            nPass ++;
     305            // if (nPass % 1000 == 0) {fprintf (stderr, "!");}
     306
    187307            nOut ++;
    188308            if (nOut >= radMax->n) break;
     
    195315    flux->n = fluxErr->n = fill->n = nOut;
    196316   
    197     psFree(source->radial->flux);
    198     psFree(source->radial->fluxErr);
    199     psFree(source->radial->fill);
    200 
    201     source->radial->flux = flux;
    202     source->radial->fluxErr = fluxErr;
    203     source->radial->fill = fill;
    204 
    205     psFree (radius);
     317    radialAper->flux = flux;
     318    radialAper->fluxErr = fluxErr;
     319    radialAper->fill = fill;
     320
     321    psFree (pixRadius);
    206322    psFree (pixFlux);
    207323    psFree (pixVar);
    208324
     325    nCalls ++;
     326    // if (nCalls % 100 == 0) {fprintf (stderr, "*");}
    209327    return true;
    210328}
  • branches/czw_branch/20101203/psphot/src/psphotRadialAperturesByObject.c

    r30118 r30631  
    33// aperture-like measurements for extended sources
    44// flux in simple, circular apertures
    5 bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule) {
     5
     6// **** it looks like this function will re-point the source pixels at the specified FILERULE
     7// **** I need to distinguish PSF-matched images from raw
     8// **** save (somewhere) the PSF-matched PSF values
     9
     10// this function measures the radial aperture fluxes for the set of readouts.  this function
     11// may be called multiple times (presumably with different matched PSF sizes).  we must have
     12// already added an entry to the readout->analysis identifying the FWHM of this version.
     13
     14bool psphotRadialAperturesByObject (pmConfig *config, psArray *objects, const pmFPAview *view, const char *filerule, int nMatchedPSF) {
    615
    716    bool status;
     
    2837    psAssert (radMax, "annular bins (RADIAL.ANNULAR.BINS.UPPER) are not defined in the recipe");
    2938    psAssert (radMax->n, "no valid annular bins (RADIAL.ANNULAR.BINS.UPPER) are define");
     39    float outerRadius = radMax->data.F32[radMax->n - 1];
    3040
    3141    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     
    3949    float SN_LIM = psMetadataLookupF32 (&status, recipe, "RADIAL_APERTURES_SN_LIM");
    4050
     51    // how many target PSFs do we want?
     52    int nPSFsizes = 0;
     53    {
     54        psMetadataLookupF32 (&status, recipe, "PSPHOT.STACK.TARGET.PSF.FWHM");
     55        if (status) {
     56            nPSFsizes = 1;
     57        } else {
     58            psVector *fwhmValues = psMetadataLookupVector(&status, recipe, "PSPHOT.STACK.TARGET.PSF.FWHM"); // Magnitude offsets
     59            psAssert (status, "missing psphot recipe value PSPHOT.STACK.TARGET.PSF.FWHM");
     60            nPSFsizes = fwhmValues->n;
     61        }
     62    }
     63   
    4164    // source analysis is done in S/N order (brightest first)
    4265    objects = psArraySort (objects, pmPhotObjSortBySN);
     
    5275        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    5376        psAssert (readout, "missing readout?");
     77
     78        psVector *fwhmValues = psMetadataLookupVector(&status, readout->analysis, "STACK.PSF.FWHM.VALUES");
     79        if (!fwhmValues) {
     80            psError (PSPHOT_ERR_CONFIG, true, "convolved or measured FWHM is not defined for this readout");
     81            return false;
     82        }
     83        if (fwhmValues->n != nMatchedPSF + 1) {
     84            psError (PSPHOT_ERR_CONFIG, true, "convolved or measured FWHM sequence is inconsistent this readout");
     85            return false;
     86        }
     87        psLogMsg ("psphot", PS_LOG_DETAIL, "PSF FWHM of %s : %f pixels\n", file->name, fwhmValues->data.F32[nMatchedPSF]);
    5488
    5589        readouts->data[i] = psMemIncrRefCounter(readout);
     
    77111            if (source->peak->SN < SN_LIM) continue;
    78112
     113            int index = source->imageID;
     114            if (index >= readouts->n) continue; // skip the sources generated by the chisq image
     115            pmReadout *readout = readouts->data[index];
     116
     117            // psLogMsg("psphot", PS_LOG_INFO, "radial apertures for %d", index);
     118            // psphotVisualShowImage(readout);
     119
     120            // allocate pmSourceExtendedParameters, if not already defined
     121            if (source->parent) {
     122                if (!source->parent->radialAper) {
     123                    source->parent->radialAper = psArrayAlloc(nPSFsizes);
     124                }
     125            } else {
     126                if (!source->radialAper) {
     127                    source->radialAper = psArrayAlloc(nPSFsizes);
     128                }
     129            }
     130
    79131            // replace object in image
    80132            if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) {
    81133                pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    82134            }
     135
     136            // we need to change the view for the radial aperture analysis, but we want to recover exactly
     137            // the original view; the following elements get destroyed by pmSourceRedefinePixels so save them:
     138            psImage *oldMaskObj   = psMemIncrRefCounter(source->maskObj);
     139            psImage *oldModelFlux = psMemIncrRefCounter(source->modelFlux);
     140            psImage *oldPSFimage  = psMemIncrRefCounter(source->psfImage);
     141            psRegion oldRegion    = source->region;
     142
    83143            Nradial ++;
    84144
    85             int index = source->imageID;
    86             pmReadout *readout = readouts->data[index];
     145            // psLogMsg("psphot", PS_LOG_INFO, "radial apertures for %d", index);
     146            // psphotVisualShowImage(readout);
    87147
    88148            // force source image to be a bit larger...
    89             float radius = source->peak->xf - source->pixels->col0;
    90             radius = PS_MAX (radius, source->peak->yf - source->pixels->row0);
    91             radius = PS_MAX (radius, source->pixels->numRows - source->peak->yf + source->pixels->row0);
    92             radius = PS_MAX (radius, source->pixels->numCols - source->peak->xf + source->pixels->col0);
    93             pmSourceRedefinePixels (source, readout, source->peak->xf, source->peak->yf, 1.5*radius);
     149            // float radius = source->peak->xf - source->pixels->col0;
     150            // radius = PS_MAX (radius, source->peak->yf - source->pixels->row0);
     151            // radius = PS_MAX (radius, source->pixels->numRows - source->peak->yf + source->pixels->row0);
     152            // radius = PS_MAX (radius, source->pixels->numCols - source->peak->xf + source->pixels->col0);
     153            pmSourceRedefinePixels (source, readout, source->peak->xf, source->peak->yf, outerRadius + 2);
    94154
    95             if (!psphotRadialApertureSource (source, recipe, skynoise, maskVal, radMax)) {
     155            if (!psphotRadialApertureSource (source, recipe, skynoise, maskVal, radMax, nMatchedPSF)) {
    96156                psTrace ("psphot", 5, "failed to extract radial profile for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    97157            } else {
     
    99159            }
    100160
     161            pmSourceRedefinePixelsByRegion (source, readout, oldRegion);
     162            psFree(source->maskObj);   source->maskObj   = oldMaskObj;
     163            psFree(source->modelFlux); source->modelFlux = oldModelFlux;
     164            psFree(source->psfImage);  source->psfImage  = oldPSFimage;
     165
    101166            // re-subtract the object, leave local sky
    102167            pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     168
     169            // psLogMsg("psphot", PS_LOG_INFO, "radial apertures for %d", index);
     170            // psphotVisualShowImage(readout);
    103171        }
    104172    }
  • branches/czw_branch/20101203/psphot/src/psphotReadout.c

    r30118 r30631  
    112112    // use bright stellar objects to measure PSF if we were supplied a PSF for any input file,
    113113    // this step is skipped
    114     if (!psphotChoosePSF (config, view, filerule)) { // pass 1
     114    if (!psphotChoosePSF (config, view, filerule, true)) { // pass 1
    115115        psLogMsg ("psphot", 3, "failure to construct a psf model");
    116116        return psphotReadoutCleanup (config, view, filerule);
  • branches/czw_branch/20101203/psphot/src/psphotReadoutFindPSF.c

    r30118 r30631  
    4949    }
    5050
    51     if (!psphotChoosePSF(config, view, filerule)) {
     51    if (!psphotChoosePSF(config, view, filerule, true)) {
    5252        psError(PSPHOT_ERR_PSF, false, "Failed to construct a psf model");
    5353        return psphotReadoutCleanup (config, view, filerule);
  • branches/czw_branch/20101203/psphot/src/psphotReadoutKnownSources.c

    r30118 r30631  
    4343    }
    4444
    45     if (!psphotChoosePSF (config, view, filerule)) {
     45    if (!psphotChoosePSF (config, view, filerule, true)) {
    4646        psError(PSPHOT_ERR_PSF, false, "Failed to construct a psf model");
    4747        return psphotReadoutCleanup (config, view, filerule);
  • branches/czw_branch/20101203/psphot/src/psphotReplaceUnfit.c

    r30118 r30631  
    7575      pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    7676    }
     77
     78    psphotVisualShowImage(readout);
    7779    psLogMsg ("psphot.replace", PS_LOG_INFO, "replaced models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
    7880    return true;
     
    101103    return true;
    102104}
     105
     106// modify the sources to point at the corresponding pixels for the given filerule
     107bool psphotRedefinePixels (pmConfig *config, const pmFPAview *view, const char *filerule)
     108{
     109    bool status = true;
     110
     111    // select the appropriate recipe information
     112    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     113    psAssert (recipe, "missing recipe?");
     114
     115    int num = psphotFileruleCount(config, filerule);
     116
     117    // loop over the available readouts
     118    for (int i = 0; i < num; i++) {
     119        if (!psphotRedefinePixelsReadout (config, view, filerule, i, recipe)) {
     120            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
     121            return false;
     122        }
     123    }
     124    return true;
     125}
     126
     127bool psphotRedefinePixelsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     128
     129    bool status;
     130    pmSource *source;
     131
     132    psTimerStart ("psphot.replace");
     133
     134    // find the currently selected readout
     135    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
     136    psAssert (file, "missing file?");
     137
     138    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     139    psAssert (readout, "missing readout?");
     140
     141    // XXX the sources have already been copied (merge into here?)
     142    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     143    psAssert (detections, "missing detections?");
     144
     145    psArray *sources = detections->allSources;
     146    psAssert (sources, "missing sources?");
     147
     148    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     149    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     150    psAssert (maskVal, "missing mask value?");
     151
     152    for (int i = 0; i < sources->n; i++) {
     153      source = sources->data[i];
     154
     155      // sources have not yet been subtracted in this image (but this flag may be raised)
     156      source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     157
     158      float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     159      float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     160      float radius = source->modelPSF->fitRadius;
     161
     162      // force a redefine to this image
     163      pmSourceFreePixels(source);
     164      pmSourceRedefinePixels (source, readout, Xo, Yo, radius);
     165    }
     166    return true;
     167}
     168
     169// for now, let's store the detections on the readout->analysis for each readout
     170bool psphotResetModels (pmConfig *config, const pmFPAview *view, const char *filerule)
     171{
     172    bool status = true;
     173
     174    // select the appropriate recipe information
     175    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     176    psAssert (recipe, "missing recipe?");
     177
     178    int num = psphotFileruleCount(config, filerule);
     179
     180    // loop over the available readouts
     181    for (int i = 0; i < num; i++) {
     182        if (!psphotResetModelsReadout (config, view, filerule, i, recipe)) {
     183            psError (PSPHOT_ERR_CONFIG, false, "failed to replace all sources for %s entry %d", filerule, i);
     184            return false;
     185        }
     186    }
     187    return true;
     188}
     189
     190bool psphotResetModelsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     191
     192    bool status;
     193    pmSource *source;
     194
     195    psTimerStart ("psphot.replace");
     196
     197    // find the currently selected readout
     198    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
     199    psAssert (file, "missing file?");
     200
     201    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     202    psAssert (readout, "missing readout?");
     203
     204    // XXX the sources have already been copied (merge into here?)
     205    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     206    psAssert (detections, "missing detections?");
     207
     208    psArray *sources = detections->allSources;
     209    psAssert (sources, "missing sources?");
     210
     211    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     212    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     213    psAssert (maskVal, "missing mask value?");
     214
     215    // what fraction of the PSF is used? (radius in pixels : 2 -> 5x5 box)
     216    int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
     217    assert (status);
     218
     219    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
     220    psAssert (psf, "missing psf?");
     221
     222    for (int i = 0; i < sources->n; i++) {
     223      source = sources->data[i];
     224
     225      // *** we need to cache the 'best' model, and we have 3 cases:
     226      // 1) model is the psf model --> generate from the new psf
     227      // 2) model is an unconvolved extended model --> just cache the copy (not perfect)
     228      // 3) model is a convolved extended model --> re-generate
     229
     230      // use the 'best' model to cache the model (PSF or EXT : EXT may point at one of modelFits
     231      bool isPSF = false;
     232      pmModel *model = pmSourceGetModel(&isPSF, source);
     233      float radius = model->fitRadius; // save for future use below
     234
     235      // regenerate the PSF if the model is a PSF, or if we need the PSF for a PCM
     236      if (isPSF || model->isPCM) {
     237          // the guess central intensity comes from the peak:
     238          float Io = source->peak->flux;
     239          float Xo = source->modelPSF->params->data.F32[PM_PAR_XPOS];
     240          float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS];
     241
     242          // generate a model for this object with Io = 1.0
     243          pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
     244          if (modelPSF == NULL) {
     245              psWarning ("Failed to determine PSF model for source at (%f,%f), skipping", Xo, Yo);
     246              continue;
     247          }
     248
     249          // set the source PSF model
     250          psFree (source->modelPSF);
     251          source->modelPSF = modelPSF;
     252          source->modelPSF->fitRadius = radius;
     253      }
     254
     255      if (model->isPCM) {
     256          psAssert(false, "this section is not complete");
     257
     258          pmSourceCachePSF (source, maskVal);
     259
     260          psKernel *psfKernel = pmPCMkernelFromPSF(source, psfSize);
     261          if (!psfKernel) {
     262              psWarning ("no psf kernel");
     263          }
     264
     265          // generate an image of the right size
     266          psImage *rawModelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     267          psImageInit (rawModelFlux, 0.0);
     268         
     269          // insert the model image normalized to 1.0
     270          pmModelAdd (rawModelFlux, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM, maskVal);
     271
     272          psImageConvolveFFT (source->modelFlux, rawModelFlux, NULL, 0, psfKernel);
     273         
     274          psFree (psfKernel);
     275          psFree (rawModelFlux);
     276      }
     277
     278      pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
     279    }
     280
     281    psLogMsg ("psphot.replace", PS_LOG_INFO, "subtracted models for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.replace"));
     282    return true;
     283}
     284
  • branches/czw_branch/20101203/psphot/src/psphotSetThreads.c

    r29004 r30631  
    3535    psFree(task);
    3636
    37     task = psThreadTaskAlloc("PSPHOT_EXTENDED_FIT", 12);
     37    task = psThreadTaskAlloc("PSPHOT_EXTENDED_FIT", 13);
    3838    task->function = &psphotExtendedSourceFits_Threaded;
    3939    psThreadTaskAdd(task);
  • branches/czw_branch/20101203/psphot/src/psphotSourceFits.c

    r29548 r30631  
    365365
    366366    // copy most data from the primary source (modelEXT, blends stay NULL)
    367     pmSource *newSrc = pmSourceCopyData (source);
     367    pmSource *newSrc = pmSourceCopy (source);
    368368    newSrc->modelPSF = psMemIncrRefCounter (DBL->data[1]);
    369369
     
    385385            psLogMsg ("psphot", 1, "PAR %d : %f +/- %f\n", i, source->modelPSF->params->data.F32[i], source->modelPSF->dparams->data.F32[i]);
    386386        }
    387         psphotVisualShowResidualImage (readout);
     387        psphotVisualShowResidualImage (readout, false);
    388388    }
    389389# endif
  • branches/czw_branch/20101203/psphot/src/psphotSourceMatch.c

    r30118 r30631  
    187187        pmPhotObj *obj = objects->data[i];
    188188
     189        // we will find the input source with the max number of spans and reproduce that footprint
     190        int nSpansMax = 0;
     191        int iSpansMax = -1;
     192
    189193        // mark the images for which sources have been found
    190194        psVectorInit (found, 0);
     
    196200            psAssert (index < found->n, "invalid index");
    197201
     202            if (src->peak && src->peak->footprint && src->peak->footprint->nspans > nSpansMax) {
     203                nSpansMax = src->peak->footprint->nspans;
     204                iSpansMax = j;
     205            }
     206
    198207            found->data.U8[index] = 1;
     208        }
     209
     210        // we make a copy of the largest footprint; this will be used for all new sources associated with this object
     211        pmFootprint *footprint = NULL;
     212        if (iSpansMax != -1) { // copy the footprint info
     213            pmSource *src = obj->sources->data[iSpansMax];
     214            psAssert(src->peak, "source does not exist?");
     215            psAssert(src->peak->footprint, "footprint does not exist");
     216            psAssert(src->peak->footprint->nspans == nSpansMax, "wrong footprint?");
     217           
     218            // we only care about the spans, do not worry about the image of this footprint
     219            footprint = pmFootprintCopyData(src->peak->footprint, NULL);
    199220        }
    200221
     
    219240            peak->dy = NAN;
    220241           
    221             // XXX assign to a footprint?
    222 
     242            // assign to a footprint on this readout->image
     243            peak->footprint = pmFootprintCopyData(footprint, readout->image);
     244
     245            // the peak does not claim ownership of the footprint (it does not free it). save a copy of this
     246            // footprint on detections->footprints so we can free it later
     247            psArrayAdd(detections->footprints, 100, peak->footprint);
     248            psFree (peak->footprint);
     249           
    223250            // create a new source
    224251            pmSource *source = pmSourceAlloc();
     
    227254
    228255            // add the peak
    229             source->peak = psMemIncrRefCounter(peak);
     256            source->peak = peak;
    230257
    231258            // allocate space for moments
     
    239266            psArrayAdd (detections->newSources, 100, source);
    240267            psFree (source);
    241             psFree (peak);
    242         }
     268        }
     269        psFree (footprint);
    243270    }
    244271
  • branches/czw_branch/20101203/psphot/src/psphotSourceSize.c

    r30118 r30631  
    499499
    500500        // clear the mask bit and set the circular mask pixels
    501         psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(options->markVal));
    502         psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", options->markVal);
    503 
     501        // psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(options->markVal));
     502        // psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", options->markVal);
    504503        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    505504
    506505        // clear the mask bit
    507         psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(options->markVal));
     506        // psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(options->markVal));
    508507
    509508        // re-subtract the object, leave local sky
  • branches/czw_branch/20101203/psphot/src/psphotStackArguments.c

    r28013 r30631  
    4141    if ((N = psArgumentGet (argc, argv, "-break"))) {
    4242        if (argc <= N+1) {
    43           psErrorStackPrint(stderr, "Expected to see 1 more argument; saw %d", argc - 1);
     43          psErrorStackPrint(stderr, "Expected to see an argument for -break");
    4444          exit(PS_EXIT_CONFIG_ERROR);
    4545        }
  • branches/czw_branch/20101203/psphot/src/psphotStackChisqImage.c

    r30118 r30631  
    66
    77// XXX supply filename or keep PSPHOT.INPUT fixed?
    8 bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *ruleDet, const char *ruleCnv)
     8bool psphotStackChisqImage (pmConfig *config, const pmFPAview *view, const char *ruleDet, const char *ruleSrc)
    99{
    1010    psTimerStart ("psphot.chisq.image");
     
    2727
    2828    psMetadataAddS32(config->arguments, PS_LIST_TAIL, "PSPHOT.CHISQ.NUM", PS_META_REPLACE, "", num);
     29
     30    // we need to increment the counter for ruleDet and ruleSrc:
    2931    num++;
    30     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "", num);
    3132
    3233    // save the resulting image in the 'detection' set
     
    3536        return false;
    3637    }
     38    psphotFileruleCountSet(config, ruleDet, num);
    3739
    38     // save the resulting image in the 'convolved' set
    39     if (!psMetadataAddPtr(config->files, PS_LIST_TAIL, ruleCnv, PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "", chisqFile)) {
    40         psError(PM_ERR_CONFIG, false, "could not add chisqFPA to config files");
    41         return false;
     40    // also save the resulting image in the 'source' set (analysis set)
     41    if (strcmp(ruleDet, ruleSrc)) {
     42        if (!psMetadataAddPtr(config->files, PS_LIST_TAIL, ruleSrc, PS_DATA_UNKNOWN | PS_META_DUPLICATE_OK, "", chisqFile)) {
     43            psError(PM_ERR_CONFIG, false, "could not add chisqFPA to config files");
     44            return false;
     45        }
     46        psphotFileruleCountSet(config, ruleSrc, num);
    4247    }
    4348
     
    119124    psAssert (status, "programming error: must define PSPHOT.CHISQ.NUM");
    120125
    121     int inputNum = psphotFileruleCount(config, "PSPHOT.INPUT");
     126    int inputNum = psphotFileruleCount(config, filerule);
    122127
    123128    pmFPAfileRemoveSingle (config->files, filerule, chisqNum);
    124129
    125130    inputNum --;
    126     psMetadataAddS32(config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "", inputNum);
     131    psphotFileruleCountSet(config, filerule, inputNum);
    127132
    128133    return true;
  • branches/czw_branch/20101203/psphot/src/psphotStackImageLoop.c

    r30118 r30631  
    101101*/
    102102
    103 # define UPDATE_HEADER 0
     103# define UPDATE_HEADER 1
    104104
    105105bool GetAstrometryFPA (pmConfig *config, pmFPAview *view) {
     
    174174        pmChip *outChip = pmFPAviewThisChip(view, output->fpa); ///< Chip in the output
    175175
    176 # if (UPDATE_HEADER)
    177176        pmHDU *outHDU = pmFPAviewThisHDU (view, output->fpa);
    178177        if (!outHDU) {
    179             pmFPAAddSourceFromView(output->fpa, "name", view, output->format);
     178            pmFPAAddSourceFromView(output->fpa, view, output->format);
    180179            outHDU = pmFPAviewThisHDU (view, output->fpa);
    181180            psAssert (outHDU, "failed to make HDU");
    182181        }
    183 # endif
     182        if (!outHDU->header) {
     183          outHDU->header = psMetadataAlloc();
     184        }
    184185
    185186        if (bilevelAstrometry) {
     
    188189                continue;
    189190            }
    190 # if (UPDATE_HEADER)
    191191            if (!pmAstromWriteBilevelChip(outHDU->header, outChip, WCS_NONLIN_TOL)) {
    192192                psWarning("Unable to generate WCS header.");
    193193                continue;
    194194            }
    195 # endif
    196195        } else {
    197196            // we use a default FPA pixel scale of 1.0
     
    200199                continue;
    201200            }
    202 # if (UPDATE_HEADER)
    203             if (UPDATE_HEADER && !pmAstromWriteWCS(outHDU->header, output->fpa, outChip, WCS_NONLIN_TOL)) {
     201            if (!pmAstromWriteWCS(outHDU->header, output->fpa, outChip, WCS_NONLIN_TOL)) {
    204202                psWarning("Unable to generate WCS header.");
    205203                continue;
    206204            }
    207 # endif
    208205        }
    209206    }
     
    225222        psAssert (output, "missing file?");
    226223
    227 # if (UPDATE_HEADER)
    228224        pmHDU *PHU = pmFPAviewThisPHU(view, output->fpa);
    229225        if (!PHU) {
    230             pmFPAAddSourceFromView(output->fpa, "name", view, output->format);
     226            pmFPAAddSourceFromView(output->fpa, view, output->format);
    231227            PHU = pmFPAviewThisPHU (view, output->fpa);
    232228            psAssert (PHU, "failed to make PHU");
    233229        }
     230        if (!PHU->header) {
     231          PHU->header = psMetadataAlloc();
     232        }
    234233
    235234        if (!pmAstromWriteBilevelMosaic(PHU->header, output->fpa, WCS_NONLIN_TOL)) {
    236235            psWarning("Unable to generate WCS header.");
    237236        }
    238 # endif
    239237    }
    240238
  • branches/czw_branch/20101203/psphot/src/psphotStackMatchPSFs.c

    r30118 r30631  
    6565bool psphotStackMatchPSFsReadout (pmConfig *config, const pmFPAview *view, psphotStackOptions *options, int index) {
    6666
     67    psImageMaskType maskValue;
     68    psImageMaskType markValue;
     69
     70    // get the PSPHOT.MASK value from the config
     71    if (!pmConfigMaskSetBits (&maskValue, &markValue, config)) {
     72        psError (PS_ERR_UNKNOWN, true, "Unable to define the mask bit values");
     73        return false;
     74    }
     75
    6776    pmFPAfile *fileSrc = psphotStackGetConvolveSource(config, options, index);
    6877    if (!fileSrc) {
     
    8493
    8594    // set NAN pixels to 'SAT'
    86     // XXX replace this is pmReadoutMaskInvalid?
    87     psImageMaskType maskVal = pmConfigMaskGet("SAT", config);
    88     if (!pmReadoutMaskNonfinite(readoutSrc, maskVal)) {
     95    psImageMaskType maskSat = pmConfigMaskGet("SAT", config);
     96    if (!pmReadoutMaskInvalid(readoutSrc, maskValue, maskSat)) {
    8997        psError(psErrorCodeLast(), false, "Unable to mask non-finite pixels in readout.");
    9098        return false;
     
    95103        matchKernel(config, readoutOut, readoutSrc, options, index);
    96104        saveMatchData(readoutOut, options, index);
    97         // renormKernel(readoutCnv, options, index);
    98     } else {
    99         // only match the flux (NO! not for multi-filter, at least!)
    100         // XXX do not generate readoutCnv in this case?
    101         // float norm = powf(10.0, -0.4 * options->norm->data.F32[index]); // Normalisation
    102         // psBinaryOp(readoutRaw->image, readoutRaw->image, "*", psScalarAlloc(norm, PS_TYPE_F32));
    103         // psBinaryOp(readoutRaw->variance, readoutRaw->variance, "*", psScalarAlloc(PS_SQR(norm), PS_TYPE_F32));
    104105    }
    105 
    106106    rescaleData(readoutOut, config, options, index);
    107107
    108     // dumpImage(readoutOut, readoutSrc, index, "convolved");
     108    // save the output fwhm values in the readout->analysis.  we may have / will have multiple output PSF sizes,
     109    // so we save this in a vector.  if the vector is not yet defined, create it
     110    bool mdok = false;
     111    psVector *fwhmValues = psMetadataLookupVector(&mdok, readoutOut->analysis, "STACK.PSF.FWHM.VALUES");
     112    if (!fwhmValues) {
     113        fwhmValues = psVectorAllocEmpty(10, PS_TYPE_F32);
     114        psMetadataAddVector(readoutOut->analysis, PS_LIST_TAIL, "STACK.PSF.FWHM.VALUES", PS_META_REPLACE, "PSF sizes", fwhmValues);
     115        psFree(fwhmValues); // drops the extra copy
     116    }
     117    psVectorAppend(fwhmValues, options->targetSeeing);
    109118
    110119    return true;
    111120}
    112 
    113 
    114 # if (0)
    115 // Read previously produced kernel
    116 if (psMetadataLookupBool(NULL, config->arguments, "PPSTACK.DEBUG.STACK")) {
    117     loadKernel(config, readoutCnv, options, index);
    118 } else {
    119     matchKernel(config, readoutCnv, readoutRaw, options, index);
    120 }
    121 # endif
  • branches/czw_branch/20101203/psphot/src/psphotStackMatchPSFsUtils.c

    r29548 r30631  
    365365        pmSubtractionSetFWHMs(options->inputSeeing->data.F32[index], options->targetSeeing);
    366366
    367         if (scale && !pmSubtractionParamsScale(&size, &footprint, widthsCopy, scaleRef, scaleMin, scaleMax)) {
    368             psError(psErrorCodeLast(), false, "Unable to scale kernel parameters");
    369             goto escape;
    370         }
     367        pmSubtractionParamScaleOptions(scale, scaleRef, scaleMin, scaleMax);
     368
     369        // if (scale && !pmSubtractionParamsScale(&size, &footprint, widthsCopy, scaleRef, scaleMin, scaleMax)) {
     370        //     psError(psErrorCodeLast(), false, "Unable to scale kernel parameters");
     371        //     goto escape;
     372        // }
    371373
    372374        if (!pmSubtractionMatch(NULL, readoutOut, fake, readoutSrc, footprint, stride, regionSize, spacing, threshold, stampSources, stampsName, type, size, order, widthsCopy, orders, inner, ringsOrder, binning, penalty, optimum, optWidths, optOrder, optThresh, iter, rej, normFrac, sysError, skyErr, kernelError, covarFrac, maskVal, maskBad, maskPoor, poorFrac, badFrac, PM_SUBTRACTION_MODE_2)) {
     
    383385        goto escape;
    384386    }
     387
     388    // save the PSF on the new readout->analysis:
     389    // if (!psMetadataAddPtr (readoutOut->analysis, PS_LIST_TAIL, "PSPHOT.PSF", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot psf model", options->psf)) {
     390    //     psError (PSPHOT_ERR_UNKNOWN, false, "problem saving sources on readout");
     391    //     return false;
     392    // }
    385393
    386394    // dumpImage(readoutOut, readoutSrc, index, "conv");
  • branches/czw_branch/20101203/psphot/src/psphotStackPSF.c

    r28013 r30631  
    1212    bool autoPSF = psMetadataLookupBool (&mdok, psphotRecipe, "PSPHOT.STACK.TARGET.PSF.AUTO");
    1313
     14    // Get the recipe values
     15    psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, "PPSTACK"); // ppStack recipe
     16    psAssert(recipe, "We've thrown an error on this before.");
     17       
     18    char *psfModel = psMetadataLookupStr(NULL, recipe, "PSF.MODEL"); // Model for PSF
     19
    1420    if (autoPSF) {
    15         // Get the recipe values
    16         psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, "PPSTACK"); // ppStack recipe
    17         psAssert(recipe, "We've thrown an error on this before.");
    18 
    1921        int psfInstances = psMetadataLookupS32(NULL, recipe, "PSF.INSTANCES"); // Number of instances for PSF
    2022        float psfRadius = psMetadataLookupF32(NULL, recipe, "PSF.RADIUS"); // Radius for PSF
    21         const char *psfModel = psMetadataLookupStr(NULL, recipe, "PSF.MODEL"); // Model for PSF
    2223        int psfOrder = psMetadataLookupS32(NULL, recipe, "PSF.ORDER"); // Spatial order for PSF
    2324
     
    4950
    5051        float targetFWHM = psMetadataLookupF32 (&mdok, psphotRecipe, "PSPHOT.STACK.TARGET.PSF.FWHM");
    51         psAssert (isfinite(targetFWHM), "missing psphot recipe value PSPHOT.STACK.TARGET.PSF.FWHM");
     52        if (!mdok) {
     53            psVector *fwhmValues = psMetadataLookupVector(&mdok, psphotRecipe, "PSPHOT.STACK.TARGET.PSF.FWHM"); // Magnitude offsets
     54            psAssert (mdok, "missing psphot recipe value PSPHOT.STACK.TARGET.PSF.FWHM");
     55            targetFWHM = fwhmValues->data.F32[0];
     56        }
    5257
    5358        float Sxx = sqrt(2.0)*targetFWHM / 2.35;
    5459
    5560        // XXX probably should make the model type (and par 7) optional from recipe
    56         psf = pmPSFBuildSimple("PS_MODEL_PS1_V1", Sxx, Sxx, 0.0, 1.0);
     61        psf = pmPSFBuildSimple(psfModel, Sxx, Sxx, 0.0, 1.0);
    5762        if (!psf) {
    5863            psError(PSPHOT_ERR_PSF, false, "Unable to build dummy PSF.");
  • branches/czw_branch/20101203/psphot/src/psphotStackParseCamera.c

    r29548 r30631  
    1515    }
    1616
     17    int nRaw = 0;
     18    int nCnv = 0;
    1719    int nInputs = inputs->list->n;
    1820    for (int i = 0; i < nInputs; i++) {
     
    5759                }
    5860            }
     61            nRaw ++;
    5962        }
    6063
     
    8891                }
    8992            }
     93            nCnv ++;
    9094        }
    9195
     
    9599        }
    96100
     101        // XXX what if they do not match in length
     102        if (nCnv && nRaw) {
     103            if (nCnv != nRaw) {
     104                psError (PSPHOT_ERR_CONFIG, true, "if both RAW and CNV images are supplied, the number must match");
     105                return false;
     106            }
     107        }
     108
    97109        psString sources = psMetadataLookupStr(&status, input, "SOURCES"); // Name of mask
    98         // pmFPAfile *srcInputFile = rawInputFile ? rawInputFile : cnvInputFile;
    99110        if (sources && strlen(sources) > 0) {
    100111            if (!defineFile(config, NULL, "PSPHOT.STACK.SOURCES", sources, PM_FPA_FILE_CMF)) {
     
    107118        // XXX output of these files should be optional
    108119        {
     120            // pmFPAfile *srcInputFile = rawInputFile ? rawInputFile : cnvInputFile;
    109121            pmFPAfile *outputImage = pmFPAfileDefineOutput(config, NULL, "PSPHOT.STACK.OUTPUT.IMAGE");
    110122            if (!outputImage) {
     
    150162    }
    151163    psMetadataRemoveKey(config->arguments, "FILENAMES");
     164    psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.STACK.INPUT.RAW.NUM", PS_META_REPLACE, "number of inputs", nRaw);
     165    psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.STACK.INPUT.CNV.NUM", PS_META_REPLACE, "number of inputs", nCnv);
     166    psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.STACK.OUTPUT.IMAGE.NUM", PS_META_REPLACE, "number of inputs", nInputs);
    152167    psMetadataAddS32 (config->arguments, PS_LIST_TAIL, "PSPHOT.INPUT.NUM", PS_META_REPLACE, "number of inputs", nInputs);
    153168
  • branches/czw_branch/20101203/psphot/src/psphotStackReadout.c

    r30118 r30631  
    11# include "psphotInternal.h"
    22
     3// we have 3 possible real filesets:
    34# define STACK_RAW "PSPHOT.STACK.INPUT.RAW"
    4 # define STACK_OUT "PSPHOT.STACK.OUTPUT.IMAGE"
     5# define STACK_CNV "PSPHOT.STACK.INPUT.CNV"
     6# define STACK_OUT "PSPHOT.STACK.OUTPUT.IMAGE"  /* the psf-matched image */
     7
     8// we have 3 files on which we operate:
     9// DET (detection image)       : nominally RAW (optionally CNV?)
     10// SRC (source analysis image) : nominally CNV (optionally RAW)
     11// OUT (psf-matched images)    : always OUT
     12
     13bool psphotStackVisualFilerule(pmConfig *config, const pmFPAview *view, const char *filerule) {
     14
     15    bool status = false;
     16
     17    int num = psphotFileruleCount(config, filerule);
     18
     19    // select the appropriate recipe information
     20    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSPHOT_RECIPE);
     21
     22    // loop over the available readouts
     23    for (int i = 0; i < num; i++) {
     24
     25        // find the currently selected readout
     26        pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, i); // File of interest
     27        psAssert (file, "missing file?");
     28
     29        pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     30        psAssert (readout, "missing readout?");
     31
     32        pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     33        psAssert (detections, "missing detections?");
     34
     35        psArray *sources = detections->allSources;
     36        psAssert (sources, "missing sources?");
     37
     38        psphotVisualShowResidualImage (readout, true);
     39        psphotVisualShowObjectRegions (readout, recipe, sources);
     40    }
     41    return true;
     42}
    543
    644bool psphotStackReadout (pmConfig *config, const pmFPAview *view) {
     
    2058    PS_ASSERT_PTR_NON_NULL (breakPt, false);
    2159
     60    // XXX or do I set OUT to be a pmFPAfile pointing at the input of interest?
     61    bool useRaw = psMetadataLookupBool (NULL, recipe, "PSPHOT.STACK.USE.RAW");
     62    char *STACK_SRC = useRaw ? STACK_RAW : STACK_CNV;
     63    char *STACK_DET = STACK_RAW; // XXX optionally allow this to be CNV?
     64
    2265    // we have 3 relevant files: RAW, CNV, OUT
    2366
    2467    // set the photcode for each image
    25     if (!psphotAddPhotcode (config, view, STACK_OUT)) {
     68    if (!psphotAddPhotcode (config, view, STACK_SRC)) {
    2669        psError (PSPHOT_ERR_CONFIG, false, "trouble defining the photcode");
    2770        return false;
     
    3073    // Generate the mask and weight images
    3174    // XXX this should be done before we perform the convolutions
    32     if (!psphotSetMaskAndVariance (config, view, STACK_RAW)) {
    33         return psphotReadoutCleanup (config, view, STACK_OUT);
     75    if (!psphotSetMaskAndVariance (config, view, STACK_DET)) {
     76        return psphotReadoutCleanup (config, view, STACK_SRC);
    3477    }
    3578    if (!strcasecmp (breakPt, "NOTHING")) {
    36         return psphotReadoutCleanup (config, view, STACK_OUT);
     79        return psphotReadoutCleanup (config, view, STACK_SRC);
    3780    }
    3881
     
    4083    // XXX I think this is not defined correctly for an array of images.
    4184    // XXX probably need to subtract the model (same model?) for both RAW and OUT
    42     if (!psphotModelBackground (config, view, STACK_RAW)) {
    43         return psphotReadoutCleanup (config, view, STACK_OUT);
    44     }
    45     if (!psphotSubtractBackground (config, view, STACK_RAW)) {
    46         return psphotReadoutCleanup (config, view, STACK_OUT);
     85    if (!psphotModelBackground (config, view, STACK_DET)) {
     86        return psphotReadoutCleanup (config, view, STACK_SRC);
     87    }
     88    if (!psphotSubtractBackground (config, view, STACK_DET)) {
     89        return psphotReadoutCleanup (config, view, STACK_SRC);
    4790    }
    4891    if (!strcasecmp (breakPt, "BACKMDL")) {
    49         return psphotReadoutCleanup (config, view, STACK_OUT);
    50     }
    51 
    52     // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are determined and saved on
    53     // readout->analysis XXX this function currently only works with a single PSPHOT.INPUT
    54     if (!psphotLoadPSF (config, view, STACK_RAW)) {
    55         psError (PSPHOT_ERR_UNKNOWN, false, "error loading psf model");
    56         return psphotReadoutCleanup (config, view, STACK_OUT);
    57     }
    58 
    59     if (!psphotStackChisqImage(config, view, STACK_RAW, STACK_OUT)) {
     92        return psphotReadoutCleanup (config, view, STACK_SRC);
     93    }
     94
     95    if (!psphotStackChisqImage(config, view, STACK_DET, STACK_SRC)) {
    6096        psError (PSPHOT_ERR_UNKNOWN, false, "failure to generate chisq image");
    61         return psphotReadoutCleanup (config, view, STACK_OUT);
     97        return psphotReadoutCleanup (config, view, STACK_SRC);
    6298    }
    6399    if (!strcasecmp (breakPt, "CHISQ")) {
    64         return psphotReadoutCleanup (config, view, STACK_OUT);
     100        return psphotReadoutCleanup (config, view, STACK_SRC);
    65101    }
    66102
    67103    // find the detections (by peak and/or footprint) in the image.
    68104    // This finds the detections on Chisq image as well as the individuals
    69     if (!psphotFindDetections (config, view, STACK_RAW, true)) { // pass 1
     105    if (!psphotFindDetections (config, view, STACK_DET, true)) { // pass 1
    70106        // this only happens if we had an error in psphotFindDetections
    71107        psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    72         return psphotReadoutCleanup (config, view, STACK_OUT);
    73     }
    74 
    75     // copy the detections from RAW to OUT
    76     if (!psphotCopySources (config, view, STACK_OUT, STACK_RAW)) {
    77         psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
    78         return psphotReadoutCleanup (config, view, STACK_OUT);
     108        return psphotReadoutCleanup (config, view, STACK_SRC);
     109    }
     110
     111    // copy the detections from DET to SRC
     112    if (strcmp(STACK_SRC, STACK_DET)) {
     113        if (!psphotCopySources (config, view, STACK_SRC, STACK_DET)) {
     114            psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
     115            return psphotReadoutCleanup (config, view, STACK_SRC);
     116        }
    79117    }
    80118
    81119    // construct sources and measure basic stats (saved on detections->newSources)
    82     // only run this on detections from the input images, not chisq image
    83     if (!psphotSourceStats (config, view, STACK_OUT, true)) { // pass 1
     120    if (!psphotSourceStats (config, view, STACK_SRC, true)) { // pass 1
    84121        psError(PSPHOT_ERR_UNKNOWN, false, "failure to generate sources");
    85         return psphotReadoutCleanup (config, view, STACK_OUT);
     122        return psphotReadoutCleanup (config, view, STACK_SRC);
     123    }
     124
     125    if (!strcasecmp (breakPt, "TEST1")) {
     126        return psphotReadoutCleanup (config, view, STACK_SRC);
    86127    }
    87128
    88129    // generate the objects (object unify the sources from the different images)
    89     psArray *objects = psphotMatchSources (config, view, STACK_OUT);
     130    // XXX this could just match the detections for the chisq image, and not bother measuring the
     131    // source stats in that case...
     132    psArray *objects = psphotMatchSources (config, view, STACK_SRC);
     133
     134    if (!strcasecmp (breakPt, "TEST2")) {
     135        psFree(objects);
     136        return psphotReadoutCleanup (config, view, STACK_SRC);
     137    }
    90138
    91139    // construct sources for the newly-generated sources (from other images)
    92     if (!psphotSourceStats (config, view, STACK_OUT, false)) { // pass 1
     140    if (!psphotSourceStats (config, view, STACK_SRC, false)) { // pass 1
     141        psFree(objects);
    93142        psError(PSPHOT_ERR_UNKNOWN, false, "failure to generate sources");
    94         return psphotReadoutCleanup (config, view, STACK_OUT);
     143        return psphotReadoutCleanup (config, view, STACK_SRC);
    95144    }
    96145
     
    98147    // if (!psphotDeblendSatstars (config, view)) {
    99148    //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on satstar deblend analysis");
    100     //     return psphotReadoutCleanup (config, view, STACK_OUT);
     149    //     return psphotReadoutCleanup (config, view, STACK_SRC);
    101150    // }
    102151
     
    104153    // if (!psphotBasicDeblend (config, view)) {
    105154    //     psError (PSPHOT_ERR_UNKNOWN, false, "failed on deblend analysis");
    106     //     return psphotReadoutCleanup (config, view, STACK_OUT);
     155    //     return psphotReadoutCleanup (config, view, STACK_SRC);
    107156    // }
    108157
    109158    // classify sources based on moments, brightness
    110159    // only run this on detections from the input images, not chisq image
    111     if (!psphotRoughClass (config, view, STACK_OUT)) {
     160    if (!psphotRoughClass (config, view, STACK_SRC)) {
     161        psFree(objects);
    112162        psError (PSPHOT_ERR_UNKNOWN, false, "failed to determine rough classifications");
    113         return psphotReadoutCleanup (config, view, STACK_OUT);
     163        return psphotReadoutCleanup (config, view, STACK_SRC);
    114164    }
    115165    // if we were not supplied a PSF model, determine the IQ stats here (detections->newSources)
    116166    // only run this on detections from the input images, not chisq image
    117     if (!psphotImageQuality (config, view, STACK_OUT)) { // pass 1
     167    if (!psphotImageQuality (config, view, STACK_SRC)) { // pass 1
     168        psFree(objects);
    118169        psError (PSPHOT_ERR_UNKNOWN, false, "failed to measure image quality");
    119         return psphotReadoutCleanup (config, view, STACK_OUT);
     170        return psphotReadoutCleanup (config, view, STACK_SRC);
    120171    }
    121172    if (!strcasecmp (breakPt, "MOMENTS")) {
    122         return psphotReadoutCleanup (config, view, STACK_OUT);
     173        psFree(objects);
     174        return psphotReadoutCleanup (config, view, STACK_SRC);
    123175    }
    124176
    125177    // use bright stellar objects to measure PSF if we were supplied a PSF for any input file,
    126178    // this step is skipped
    127     if (!psphotChoosePSF (config, view, STACK_OUT)) { // pass 1
     179    if (!psphotChoosePSF (config, view, STACK_SRC, true)) { // pass 1
     180        psFree(objects);
    128181        psLogMsg ("psphot", 3, "failure to construct a psf model");
    129         return psphotReadoutCleanup (config, view, STACK_OUT);
     182        return psphotReadoutCleanup (config, view, STACK_SRC);
    130183    }
    131184    if (!strcasecmp (breakPt, "PSFMODEL")) {
    132         return psphotReadoutCleanup (config, view, STACK_OUT);
     185        psFree(objects);
     186        return psphotReadoutCleanup (config, view, STACK_SRC);
    133187    }
    134188
    135189    // construct an initial model for each object, set the radius to fitRadius, set circular fit mask
    136     psphotGuessModels (config, view, STACK_OUT);
     190    psphotGuessModels (config, view, STACK_SRC);
    137191
    138192    // merge the newly selected sources into the existing list
    139193    // NOTE: merge OLD and NEW
    140     psphotMergeSources (config, view, STACK_OUT);
     194    psphotMergeSources (config, view, STACK_SRC);
    141195
    142196    // linear PSF fit to source peaks, subtract the models from the image (in PSF mask)
    143197    psphotFitSourcesLinearStack (config, objects, FALSE);
     198    psphotStackVisualFilerule(config, view, STACK_SRC);
    144199
    145200    // identify CRs and extended sources
    146     psphotSourceSize (config, view, STACK_OUT, TRUE);
     201    psphotSourceSize (config, view, STACK_SRC, TRUE);
     202
     203    // XXX do we want to do a preliminary (unconvolved) model fit here, and then
     204    // do a second detection pass? (like standard psphot)
    147205
    148206    // measure aperture photometry corrections
    149     if (!psphotApResid (config, view, STACK_OUT)) {
     207    if (!psphotApResid (config, view, STACK_SRC)) {
    150208        psFree (objects);
    151209        psLogMsg ("psphot", 3, "failed on psphotApResid");
    152         return psphotReadoutCleanup (config, view, STACK_OUT);
     210        return psphotReadoutCleanup (config, view, STACK_SRC);
    153211    }
    154212
    155213    psphotStackObjectsUnifyPosition (objects);
    156214
    157     // measure circular, radial apertures (objects sorted by S/N)
    158     psphotRadialAperturesByObject (config, objects, view, STACK_OUT);
    159 
    160215    // measure elliptical apertures, petrosians (objects sorted by S/N)
    161     psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_OUT); // pass 1 (detections->allSources)
     216    psphotExtendedSourceAnalysisByObject (config, objects, view, STACK_SRC); // pass 1 (detections->allSources)
    162217
    163218    // measure non-linear extended source models (exponential, deVaucouleur, Sersic) (sources sorted by S/N)
    164     psphotExtendedSourceFits (config, view, STACK_OUT); // pass 1 (detections->allSources)
     219    psphotExtendedSourceFits (config, view, STACK_SRC); // pass 1 (detections->allSources)
    165220
    166221    // calculate source magnitudes
    167     psphotMagnitudes(config, view, STACK_OUT);
     222    psphotMagnitudes(config, view, STACK_SRC);
     223
     224    // create source children for the OUT filerule (for radial aperture photometry)
     225    psArray *objectsRadial = psphotSourceChildrenByObject (config, view, STACK_OUT, objects);
     226    if (!objectsRadial) {
     227        psFree(objects);
     228        psError (PSPHOT_ERR_UNKNOWN, false, "failure in peak analysis");
     229        return psphotReadoutCleanup (config, view, STACK_SRC);
     230    }
     231
     232    bool smoothAgain = true;
     233    for (int nMatchedPSF = 0; smoothAgain; nMatchedPSF++) {
     234
     235        // re-measure the PSF for the smoothed image (using entries in 'allSources')
     236        psphotChoosePSF (config, view, STACK_OUT, false);
     237
     238        // this is necessary to update the models based on the new PSF
     239        psphotResetModels (config, view, STACK_OUT);
     240
     241        // this is necessary to get the right normalization for the new models
     242        psphotFitSourcesLinear (config, view, STACK_OUT, false);
     243
     244        // measure circular, radial apertures (objects sorted by S/N)
     245        psphotRadialAperturesByObject (config, objectsRadial, view, STACK_OUT, nMatchedPSF);
     246
     247        // replace the flux in the image so it is returned to its original state
     248        psphotReplaceAllSources (config, view, STACK_OUT);
     249
     250        // smooth to the next FWHM, or set 'smoothAgain' to false if no more
     251        psphotStackMatchPSFsNext(&smoothAgain, config, view, STACK_OUT, nMatchedPSF);
     252    }
    168253
    169254    if (0 && !psphotEfficiency(config, view, STACK_OUT)) {
     
    176261
    177262    // replace background in residual image
    178     psphotSkyReplace (config, view, STACK_RAW);
     263    psphotSkyReplace (config, view, STACK_DET);
    179264
    180265    // drop the references to the image pixels held by each source
    181     psphotSourceFreePixels (config, view, STACK_OUT);
    182 
    183     // remove chisq image from config->file:PSPHOT.INPUT (why?)
    184     psphotStackRemoveChisqFromInputs(config, STACK_RAW);
     266    // psphotSourceFreePixels (config, view, STACK_OUT);
     267    psphotSourceFreePixels (config, view, STACK_SRC);
     268
     269    // remove chisq image from config->file:PSPHOT.INPUT
     270    psphotStackRemoveChisqFromInputs(config, STACK_DET);
     271    if (strcmp(STACK_SRC, STACK_DET)) {
     272        psphotStackRemoveChisqFromInputs(config, STACK_SRC);
     273    }
    185274
    186275    psFree (objects);
     276    psFree (objectsRadial);
    187277
    188278    // create the exported-metadata and free local data
    189     return psphotReadoutCleanup (config, view, STACK_OUT);
     279    return psphotReadoutCleanup (config, view, STACK_SRC);
    190280}
    191281
     282/* here is the process:
     283
     284 * we have three(*) images:
     285 * RAW : unconvolved image stack
     286 * CNV : input convolved image
     287
     288 * OUT : psf-matched output image (there may be more than one of
     289 * these.  we will generate the first matched image by selecting the
     290 * target PSF and doing a full psf-maching process (as used by ppStack
     291 * and ppSub).  But, additional target output files should use a
     292 * simple gaussian convolution kernel determind from therms of the
     293 * current and the target).
     294
     295 * the output should be / could be one of the matched images, but not
     296 * all.  should we ensure the first gets written out, and ot save the
     297 * others (or only optionally).
     298
     299 * by default, we probably only sve the cmf ffile outputs.
     300
     301 * load the RAW image (unconvolved stacks)
     302 * add photcode to the output headers / readout->analysis
     303 * generate mask and variance image (this is probably never needed in
     304   practice: we always load an input mask & var.
     305 * generate & subtract a model background for ?? (RAW? CNV? OUT? all?)
     306 * load a PSF (probably not yet working)
     307
     308 * generate the CHISQ image from the RAW input images (why save on OUT?)
     309
     310 * find detections on RAW
     311
     312 * copy detections to OUT
     313
     314 * generate source stats (moments) for OUT
     315
     316 * match sources across inputs (on OUT?)
     317
     318 * generate source stats for the new constructions
     319
     320 * rough class (star, galaxy, cosmic, etc)
     321
     322 * Image quality
     323
     324 * generate PSF
     325
     326 * guess models
     327
     328 * merge sources (new -> old)
     329
     330 * linear fit to the psf
     331
     332 * find ApResid
     333
     334 * assign common positions
     335
     336 * radial apertures (** this should be on the PSF-matched images
     337
     338 * extended analysis (elliptical profile & petrosian)
     339
     340 * extended fits (sersic, etc)
     341
     342 * psphot magnitudes
     343
     344
     345 ******
     346
     347 the above is all wrong:  first, we should be doing the full
     348 morphology analysis (ExtendedAnalysis & ExtendedFits) on the CNV or
     349 RAW image (as desired optionally), etc.
     350
     351 In the discussion below, 'BST' (best) means optionally RAW or CNV
     352
     353 * detection : RAW & CHISQ (of RAW)
     354 * moments : used by psf analysis & classification (BST)
     355 * rough class : uses moments, not pixels
     356 * image quality : uses moments as well
     357 * generate PSF : (BST)
     358 * guess models (BST)
     359 * linear fit (BST)
     360 * find ApResid (BST) -- uses sources not pixels
     361 * extended analysis (BST)
     362 * extended fits (BST)
     363 * detection efficiency (BST)
     364 
     365 * somehow need to copy the sources so they point at the pixels on the
     366 * OUT image
     367
     368 * foreach target PSF
     369   * radial aperture
     370   * convolve to next target PSF
     371
     372   * somehow need to organize the output file to have the values from
     373   * the different PSFs in separate tables (with header info to
     374   * specify the size of that PSF)
     375
     376   */
  • branches/czw_branch/20101203/psphot/src/psphotVisual.c

    r29548 r30631  
    9393    strcpy (coords.ctype, "RA---TAN");
    9494
    95     psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
    96     psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
    97     if (!psImageBackground(stats, NULL, inImage, NULL, 0, rng)) {
    98         fprintf (stderr, "failed to get background values\n");
    99         return false;
    100     }
    101 
    10295    image.Nx = inImage->numCols;
    10396    image.Ny = inImage->numRows;
     
    125118    free (image.data2d);
    126119
     120    return true;
     121}
     122
     123bool psphotVisualShowObjectRegions (pmReadout *readout, psMetadata *recipe, psArray *sources) {
     124
     125    KiiImage image;
     126    KapaImageData data;
     127    Coords coords;
     128
     129    bool status = false;
     130
     131    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     132    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     133    assert (maskVal);
     134
     135    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); // Mask value for bad pixels
     136    assert (maskVal);
     137
     138    maskVal |= markVal;
     139
     140    if (!pmVisualTestLevel("psphot.image.objects", 2)) return true;
     141
     142    int kapa = psphotKapaChannel (1);
     143    if (kapa == -1) return false;
     144
     145    strcpy (coords.ctype, "RA---TAN");
     146
     147    psImage *inImage = readout->image;
     148    psImage *inMask = readout->mask;
     149    image.Nx = inImage->numCols;
     150    image.Ny = inImage->numRows;
     151   
     152    psStats *stats = psStatsAlloc (PS_STAT_ROBUST_MEDIAN | PS_STAT_ROBUST_STDEV);
     153    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);
     154    if (!psImageBackground(stats, NULL, inImage, inMask, 0xffff, rng)) {
     155        fprintf (stderr, "failed to get background values\n");
     156        return false;
     157    }
     158
     159    ALLOCATE (image.data2d, float *, image.Ny);
     160    for (int iy = 0; iy < image.Ny; iy++) {
     161        ALLOCATE (image.data2d[iy], float, image.Nx);
     162        for (int ix = 0; ix < image.Nx; ix++) {
     163            image.data2d[iy][ix] = 0;
     164        }
     165    }
     166
     167    // loop over sources and set unmasked pixels to 0
     168    for (int i = 0; i < sources->n; i++) {
     169
     170        pmSource *source = sources->data[i];
     171        if (source == NULL) continue;
     172
     173        psImage *mask = source->maskObj;
     174        if (mask == NULL) continue;
     175
     176        for (int iy = 0; iy < mask->numRows; iy++) {
     177            int jy = iy + mask->row0;
     178            if (jy < 0) continue;
     179            if (jy >= inImage->numRows) continue;
     180            for (int ix = 0; ix < mask->numCols; ix++) {
     181                int jx = ix + mask->col0;
     182                if (jx < 0) continue;
     183                if (jx >= inImage->numCols) continue;
     184
     185                if (mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskVal) continue;
     186                image.data2d[jy][jx] = 1;
     187            }
     188        }
     189    }
     190
     191    for (int iy = 0; iy < image.Ny; iy++) {
     192        for (int ix = 0; ix < image.Nx; ix++) {
     193            image.data2d[iy][ix] = (image.data2d[iy][ix] == 0.0) ? NAN : inImage->data.F32[iy][ix];
     194        }
     195    }
     196
     197    strcpy (data.name, "maskObj");
     198    strcpy (data.file, "maskObj");
     199    // data.zero = 0.0;
     200    // data.range = 1.0;
     201    data.zero = stats->robustMedian - stats->robustStdev;
     202    data.range = 5*stats->robustStdev;
     203    data.logflux = 0;
     204
     205    KiiSetChannel (kapa, 2);
     206    KiiNewPicture2D (kapa, &image, &data, &coords);
     207
     208    for (int iy = 0; iy < image.Ny; iy++) {
     209        free (image.data2d[iy]);
     210    }
     211    free (image.data2d);
     212
    127213    psFree (stats);
    128214    psFree (rng);
    129 
     215   
     216    pmVisualAskUser(NULL);
    130217    return true;
    131218}
     
    24012488}
    24022489
    2403 bool psphotVisualShowResidualImage (pmReadout *readout) {
     2490// option to redo variance since in some cases we may have displayed a different image in the meanwhile
     2491bool psphotVisualShowResidualImage (pmReadout *readout, bool reshow) {
    24042492
    24052493    if (!pmVisualTestLevel("psphot.image.resid", 2)) return true;
     
    24082496    if (myKapa == -1) return false;
    24092497
    2410     psphotVisualScaleImage (myKapa, readout->image, readout->mask, "resid", 1);
     2498    if (reshow) {
     2499        psphotVisualShowMask (myKapa, readout->mask, "mask", 2);
     2500        psphotVisualScaleImage (myKapa, readout->variance, readout->mask, "variance", 1);
     2501    }
     2502    psphotVisualScaleImage (myKapa, readout->image, readout->mask, "resid", 0);
    24112503
    24122504    pmVisualAskUser(NULL);
  • branches/czw_branch/20101203/pstamp/scripts/pstamp_checkdependent.pl

    r30587 r30631  
    153153
    154154        # assume the fault is transient.
     155        # fault the dependent to give the fault a chance to correct itself
    155156        my_die("Component faulted on update dep_id: $dep_id", $PS_EXIT_SYS_ERROR);
    156157    }
     
    410411
    411412        if ($warp->{data_state} ne 'full') {
    412             my $warp_status = check_states_warp($warp, 0, $rlabel, $need_magic);
     413            my $warp_status = check_states_warp($warp, $rlabel, $need_magic);
    413414            if ($warp_status eq $PSTAMP_GONE) {
    414415                faultComponent('diff', $diff_id, $skycell_id, $PSTAMP_GONE);
     
    426427        if ($warp1->{data_state} ne 'full') {
    427428            $warps_ready = 0;
    428             $warp_status = check_states_warp($warp1, 0, $rlabel, $need_magic);
     429            $warp_status = check_states_warp($warp1, $rlabel, $need_magic);
    429430            if ($warp_status) {
    430431                if ($warp_status eq $PSTAMP_GONE) {
     
    440441        if ($warp2->{data_state} ne 'full') {
    441442            $warps_ready = 0;
    442             $warp_status = check_states_warp($warp2, 0, $rlabel, $need_magic);
     443            $warp_status = check_states_warp($warp2, $rlabel, $need_magic);
    443444            if ($warp_status eq $PSTAMP_GONE) {
    444445                faultComponent('diff', $diff_id, $skycell_id, $PSTAMP_GONE);
     
    493494
    494495        if ($warp->{data_state} ne 'full') {
    495             my $warp_status = check_states_warp($warp, 0, $rlabel, $need_magic);
     496            my $warp_status = check_states_warp($warp, $rlabel, $need_magic);
    496497            if ($warp_status eq $PSTAMP_GONE) {
    497498                faultComponent('diff', $diff_id, $skycell_id, $PSTAMP_GONE);
Note: See TracChangeset for help on using the changeset viewer.