IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 29060


Ignore:
Timestamp:
Aug 26, 2010, 9:18:39 AM (16 years ago)
Author:
Serge CHASTEL
Message:

Merging trunk in branch

Location:
branches/sc_branches/trunkTest
Files:
1 deleted
289 edited
42 copied

Legend:

Unmodified
Added
Removed
  • branches/sc_branches/trunkTest

  • branches/sc_branches/trunkTest/Ohana

    • Property svn:mergeinfo deleted
  • branches/sc_branches/trunkTest/Ohana/src/addstar/include/addstar.h

    r27435 r29060  
    209209Stars     *ReadStarsSDSS          PROTO((FILE *f, char *name, Header *header, Header *in_theader, Image *images, off_t *nimages, unsigned int *nstars));
    210210int        ReadImageHeader        PROTO((Header *header, Image *image, int photcode));
    211 Stars     *FilterStars            PROTO((Stars *instars, Image *image, unsigned int imageID));
     211Stars     *FilterStars            PROTO((Stars *instars, Image *image, unsigned int imageID, const AddstarClientOptions *options));
    212212Stars     *MergeStars             PROTO((Stars *stars, unsigned int *Nstars, Stars *instars, unsigned int Ninstars));
    213213double     scat_subpix            PROTO((double x, double y));
  • branches/sc_branches/trunkTest/Ohana/src/addstar/src/ConfigInit.c

    r28214 r29060  
    212212  }
    213213
     214  /* get detection filtering mask */
     215  if(!ScanConfig (config, "DETECTIONFILTER", "%d", 0, &options.detectionFilter)) {
     216      options.detectionFilter = 0;
     217      fprintf (stderr, "Could not find 'DETECTIONFILTER' in config, using '%x'\n", options.detectionFilter);
     218  }
     219
    214220  free (config);
    215221  free (file);
  • branches/sc_branches/trunkTest/Ohana/src/addstar/src/FilterStars.c

    r28214 r29060  
    1010// the imageID supplied here is the sequence **within this set**
    1111// this value is updated based on the image table later
    12 Stars *FilterStars (Stars *instars, Image *image, unsigned int imageID) {
     12Stars *FilterStars (Stars *instars, Image *image, unsigned int imageID, const AddstarClientOptions *options) {
    1313
    1414  int j, N;
     
    2828  ALLOCATE (stars, Stars, image[0].nstar);
    2929  for (N = j = 0; j < image[0].nstar; j++) {
     30
     31    if (instars[j].measure.photFlags & options->detectionFilter) continue;
    3032
    3133    /* allow for some dynamic filtering of star list */
     
    100102    N ++;
    101103  }
     104
     105  // DEBUG printf("N stars orig = %d after filter = %d\n", image[0].nstar, N);
     106 
    102107  image[0].nstar = N;
    103108  REALLOCATE (stars, Stars, image[0].nstar);
  • branches/sc_branches/trunkTest/Ohana/src/addstar/src/LoadData.c

    r27435 r29060  
    8080         
    8181    inStars = ReadStarsFITS (f, headers[Nhead], headers[Ndata], &images[0][Nvalid].nstar);
    82     inStars = FilterStars (inStars, &images[0][Nvalid], Nvalid);
     82    if (!inStars) {
     83      // XXX need to free the data here or in ReadStarsFITS on error
     84      continue;
     85    }
     86    inStars = FilterStars (inStars, &images[0][Nvalid], Nvalid, options);
    8387    *stars = MergeStars (*stars, Nstars, inStars, images[0][Nvalid].nstar);
    8488    Nvalid++;
  • branches/sc_branches/trunkTest/Ohana/src/addstar/src/ReadStarsFITS.c

    r27435 r29060  
    4949  if (stars == NULL) {
    5050    fprintf (stderr, "ERROR: invalid table type %s\n", type);
    51     exit (1);
     51    return (NULL);
    5252  }
    5353  // Nstars is not necessarily == *nstars (The former is the number of detections, the
     
    6969  swapped = FALSE;
    7070  smpdata = gfits_table_get_SMPData (table, &Nstars, &swapped);
     71  if (!smpdata) {
     72    fprintf (stderr, "skipping inconsistent entry\n");
     73    return (NULL);
     74  }
    7175  ZeroPt = GetZeroPoint();
    7276
     
    108112
    109113  ps1data = gfits_table_get_PS1_DEV_0 (table, &Nstars, NULL);
     114  if (!ps1data) {
     115    fprintf (stderr, "skipping inconsistent entry\n");
     116    return (NULL);
     117  }
    110118  ZeroPt = GetZeroPoint();
    111119
     
    153161
    154162  ps1data = gfits_table_get_PS1_DEV_1 (table, &Nstars, NULL);
     163  if (!ps1data) {
     164    fprintf (stderr, "skipping inconsistent entry\n");
     165    return (NULL);
     166  }
    155167  ZeroPt = GetZeroPoint();
    156168
     
    212224
    213225  ps1data = gfits_table_get_CMF_PS1_V1 (table, &Nstars, NULL);
     226  if (!ps1data) {
     227    fprintf (stderr, "skipping inconsistent entry\n");
     228    return (NULL);
     229  }
    214230  ZeroPt = GetZeroPoint();
    215231
     
    284300
    285301  ps1data = gfits_table_get_CMF_PS1_V1_Alt (table, &Nstars, NULL);
     302  if (!ps1data) {
     303    fprintf (stderr, "skipping inconsistent entry\n");
     304    return (NULL);
     305  }
    286306  ZeroPt = GetZeroPoint();
    287307
     
    348368
    349369  ps1data = gfits_table_get_CMF_PS1_V2 (table, &Nstars, NULL);
     370  if (!ps1data) {
     371    fprintf (stderr, "skipping inconsistent entry\n");
     372    return (NULL);
     373  }
    350374  ZeroPt = GetZeroPoint();
    351375
  • branches/sc_branches/trunkTest/Ohana/src/addstar/src/find_matches_refstars.c

    r28241 r29060  
    218218    }
    219219
    220     N = N1[i];
     220    N = i;
    221221    if (stars[N][0].found > -1) continue;
    222222    if (!IN_REGION (stars[N][0].average.R, stars[N][0].average.D)) continue;
     
    286286
    287287    for (j = 0; j < NREFSTAR_GROUP; j++) {
    288       N = N1[i + j];
     288      N = i + j;
    289289
    290290      catalog[0].measure[Nmeas]          = stars[N][0].measure;
  • branches/sc_branches/trunkTest/Ohana/src/addstar/test/dvomerge.dvo

    r28368 r29060  
    9595    subset T = t if (id == imageID[$i])
    9696    set dT = T - time[$i]
    97     vstat -q dT
     97    vstat dT
    9898    tapOK {abs($MEAN)  < 0.00001} "time for measure ID $i (MEAN)"
    9999    tapOK {abs($SIGMA) < 0.00001} "time for measure ID $i (SIGMA)"
  • branches/sc_branches/trunkTest/Ohana/src/delstar/src/delete_imagename.c

    r28241 r29060  
    1919
    2020  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     21  if (!image) {
     22    fprintf (stderr, "ERROR: failed to read images\n");
     23    exit (2);
     24  }
    2125
    2226  /* find image in db by name */
  • branches/sc_branches/trunkTest/Ohana/src/delstar/src/delete_times.c

    r27435 r29060  
    2626
    2727  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     28  if (!image) {
     29    fprintf (stderr, "ERROR: failed to read images\n");
     30    exit (2);
     31  }
    2832
    2933  /* find images for time range, delete each image */
  • branches/sc_branches/trunkTest/Ohana/src/delstar/src/find_image_db.c

    r27435 r29060  
    1717
    1818  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     19  if (!image) {
     20    fprintf (stderr, "ERROR: failed to read images\n");
     21    exit (2);
     22  }
    1923
    2024  Nlist = 0;
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/Makefile

    r27583 r29060  
    2020dvomerge     : $(BIN)/dvoconvert.$(ARCH)
    2121
    22 all: dvomerge dvoconvert
     22all: dvomerge dvoconvert dvosecfilt
    2323
    2424DVOMERGE = \
     
    5858$(BIN)/dvoconvert.$(ARCH) : $(DVOCONVERT)
    5959
    60 INSTALL = dvomerge dvoconvert
     60DVOSECFILT = \
     61$(SRC)/dvosecfilt.$(ARCH).o \
     62$(SRC)/SetSignals.$(ARCH).o \
     63$(SRC)/ConfigInit.$(ARCH).o \
     64$(SRC)/Shutdown.$(ARCH).o \
     65$(SRC)/help.$(ARCH).o \
     66$(SRC)/args.$(ARCH).o
     67
     68$(DVOSECFILT)  : $(INC)/dvomerge.h
     69
     70$(BIN)/dvosecfilt.$(ARCH) : $(DVOSECFILT)
     71
     72INSTALL = dvomerge dvoconvert dvosecfilt
    6173
    6274# dependancy rules for binary code #########################
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/include/dvomerge.h

    r28855 r29060  
    5555int        dvoconvert_args        PROTO((int *argc, char **argv));
    5656
     57void       dvosecfilt_usage       PROTO((void));
     58void       dvosecfilt_help        PROTO((int argc, char **argv));
     59int        dvosecfilt_args        PROTO((int *argc, char **argv));
     60
    5761int        SkyTablePopulatedRange PROTO((off_t *ns, off_t *ne, SkyTable *sky, off_t Nstart));
     62int        SkyListPopulatedRange  PROTO((off_t *ns, off_t *ne, SkyList *sky, off_t Nstart));
    5863
    5964SkyList   *SkyTablePopulatedList  PROTO((SkyTable *sky));
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/SkyTablePopulatedRange.c

    r27435 r29060  
    1212    Ns = sky[0].regions[Nstart].childS;
    1313    Ne = sky[0].regions[Nstart].childE;
     14    if (Ns == 0) {
     15      fprintf (stderr, "no populated tables at an appropriate depth\n");
     16      exit (1);
     17    }
     18    Nstart = Ns;
     19  }
     20
     21  *ns = Ns;
     22  *ne = Ne;
     23  return (TRUE);
     24}
     25
     26int SkyListPopulatedRange (off_t *ns, off_t *ne, SkyList *sky, off_t Nstart) {
     27
     28  off_t Ns, Ne;
     29
     30  // given the starting sky region, find the populated range at or below this region
     31
     32  Ns = Nstart;
     33  Ne = Nstart + 1;
     34  while (!sky[0].regions[Nstart][0].table) {
     35    Ns = sky[0].regions[Nstart][0].childS;
     36    Ne = sky[0].regions[Nstart][0].childE;
    1437    if (Ns == 0) {
    1538      fprintf (stderr, "no populated tables at an appropriate depth\n");
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/args.c

    r28855 r29060  
    1515    remove_argument (N, argc, argv);
    1616    ALTERNATE_PHOTCODE_FILE = strdup(argv[N]);
     17    remove_argument (N, argc, argv);
     18  }
     19
     20  /*** provide additional data ***/
     21  /* restrict to a portion of the sky? */
     22  UserPatch.Rmin = 0;
     23  UserPatch.Rmax= 360;
     24  UserPatch.Dmin = -90;
     25  UserPatch.Dmax = +90;
     26  if ((N = get_argument (*argc, argv, "-region"))) {
     27    remove_argument (N, argc, argv);
     28    UserPatch.Rmin = atof (argv[N]);
     29    remove_argument (N, argc, argv);
     30    UserPatch.Rmax = atof (argv[N]);
     31    remove_argument (N, argc, argv);
     32    UserPatch.Dmin = atof (argv[N]);
     33    remove_argument (N, argc, argv);
     34    UserPatch.Dmax = atof (argv[N]);
    1735    remove_argument (N, argc, argv);
    1836  }
     
    3755  return TRUE;
    3856}
     57
     58/*** check for command line options ***/
     59int dvosecfilt_args (int *argc, char **argv) {
     60 
     61  int N;
     62
     63  /* extra error messages */
     64  VERBOSE = FALSE;
     65  if ((N = get_argument (*argc, argv, "-v"))) {
     66    VERBOSE = TRUE;
     67    remove_argument (N, argc, argv);
     68  }
     69
     70  if (*argc != 3) dvosecfilt_usage();
     71  return TRUE;
     72}
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/dvo_image_merge_dbs.c

    r28354 r29060  
    1313 
    1414  images = gfits_table_get_Image (&in[0].ftable, &Nimages, &in[0].swapped);
     15  if (!images) {
     16    fprintf (stderr, "ERROR: failed to read images\n");
     17    exit (2);
     18  }
     19
    1520  ALLOCATE (IDmap->old, off_t, Nimages);
    1621  ALLOCATE (IDmap->new, off_t, Nimages);
     
    5964
    6065  off_t Nlo, Nhi, N;
     66
     67  if (!IDmap->Nmap) return 0;
    6168
    6269  // find the a close entry below desired ID
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/dvoconvert.c

    r28855 r29060  
    148148  // convert the raw image table to Image type (byteswap if needed)
    149149  images = gfits_table_get_Image (&inDB.ftable, &Nimages, &inDB.swapped);
     150  if (!images) {
     151    fprintf (stderr, "ERROR: failed to read images\n");
     152    exit (2);
     153  }
    150154
    151155  // update additional metadata
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/dvomergeUpdate.c

    r28857 r29060  
    66  off_t i, j, Ns, Ne;
    77  SkyTable *outsky, *insky;
    8   SkyList *inlist;
     8  SkyList *outlist, *inlist;
    99  Catalog incatalog, outcatalog;
    1010  char filename[256], *input, *output;
     
    7070  SkyTableSetFilenames (insky, input, "cpt");
    7171
     72  // XXX apply this...generate the subset matching the user-selected region
     73  inlist = SkyListByPatch (insky, -1, &UserPatch);
     74
    7275  // generate an output table populated at the desired depth
    7376  outsky = SkyTableLoadOptimal (output, NULL, GSCFILE, TRUE, SKY_DEPTH, VERBOSE);
     
    7982  // loop over the populatable output tables; check for data in input in the corresponding regions
    8083
    81   SkyTablePopulatedRange (&Ns, &Ne, insky, 0);
    82   depth = insky[0].regions[Ns].depth;
     84  SkyListPopulatedRange (&Ns, &Ne, inlist, 0);
     85  depth = inlist[0].regions[Ns][0].depth;
    8386 
    84   // loop over the populatable output regions
    85   for (i = 0; i < outsky[0].Nregions; i++) {
    86     if (!outsky[0].regions[i].table) continue;
    87     if (VERBOSE) fprintf (stderr, "output: %s\n", outsky[0].regions[i].name);
     87  // loop over the populated input regions
     88  for (i = 0; i < inlist[0].Nregions; i++) {
     89    if (!inlist[0].regions[i][0].table) continue;
     90    if (VERBOSE) fprintf (stderr, "input: %s\n", inlist[0].regions[i][0].name);
    8891
    89     SetPhotcodeTable(outputPhotcodes);
     92    SetPhotcodeTable(inputPhotcodes);
    9093    // load / create output catalog (if catalog does not exist, it will be created)
    91     LoadCatalog (&outcatalog, &outsky[0].regions[i], outsky[0].filename[i], "w");
     94    LoadCatalog (&incatalog, &inlist[0].regions[i][0], inlist[0].filename[i], "r");
     95    // skip empty input catalogs
     96    if (!incatalog.Naves_disk) {
     97        dvo_catalog_unlock (&incatalog);
     98        dvo_catalog_free (&incatalog);
     99        continue;
     100    }
    92101
    93102    // combine only tables at equal or larger depth
     
    97106    // compare to a slightly reduced footprint
    98107    float dPos = 2.0/3600.0;
    99     inlist = SkyListByBounds (insky, depth, outsky[0].regions[i].Rmin + dPos, outsky[0].regions[i].Rmax - dPos, outsky[0].regions[i].Dmin + dPos, outsky[0].regions[i].Dmax - dPos);
    100     for (j = 0; j < inlist[0].Nregions; j++) {
    101       if (VERBOSE) fprintf (stderr, "input : %s\n", inlist[0].regions[j][0].name);
     108    outlist = SkyListByBounds (outsky, depth, inlist[0].regions[i][0].Rmin + dPos, inlist[0].regions[i][0].Rmax - dPos, inlist[0].regions[i][0].Dmin + dPos, inlist[0].regions[i][0].Dmax - dPos);
     109    for (j = 0; j < outlist[0].Nregions; j++) {
     110      if (VERBOSE) fprintf (stderr, "output : %s\n", outlist[0].regions[j][0].name);
    102111
    103112      // load input catalog
    104       SetPhotcodeTable(inputPhotcodes);
    105       LoadCatalog (&incatalog, inlist[0].regions[j], inlist[0].filename[j], "r");
     113      SetPhotcodeTable(outputPhotcodes);
     114      LoadCatalog (&outcatalog, outlist[0].regions[j], outlist[0].filename[j], "w");
    106115
    107       // skip empty input catalogs
    108       if (!incatalog.Naves_disk) {
    109         dvo_catalog_unlock (&incatalog);
    110         dvo_catalog_free (&incatalog);
    111         continue;
    112       }
    113116      dvo_update_image_IDs (&IDmap, &incatalog);
    114       merge_catalogs_old (&outsky[0].regions[i], &outcatalog, &incatalog, RADIUS, secfiltMap);
    115       dvo_catalog_unlock (&incatalog);
    116       dvo_catalog_free (&incatalog);
     117      merge_catalogs_old (&outsky[0].regions[j], &outcatalog, &incatalog, RADIUS, secfiltMap);
    117118
    118       fprintf (stderr, "merged %s into %s\n", outsky[0].regions[i].name, inlist[0].regions[j][0].name);
     119      outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
     120
     121      // if we receive a signal which would cause us to exit, wait until the full catalog is written
     122      SetProtect (TRUE);
     123      dvo_catalog_save (&outcatalog, VERBOSE);
     124      SetProtect (FALSE);
     125
     126      dvo_catalog_unlock (&outcatalog);
     127      dvo_catalog_free (&outcatalog);
     128
     129      fprintf (stderr, "merged %s into %s\n", inlist[0].regions[i][0].name, outlist[0].regions[j][0].name);
    119130    }
    120     SkyListFree (inlist);
     131    SkyListFree (outlist);
    121132
    122     outcatalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
    123     dvo_catalog_save (&outcatalog, VERBOSE);
    124     dvo_catalog_unlock (&outcatalog);
    125     dvo_catalog_free (&outcatalog);
     133    dvo_catalog_unlock (&incatalog);
     134    dvo_catalog_free (&incatalog);
    126135  }
    127136
     
    153162  // load the image table
    154163  if (inDB.dbstate == LCK_EMPTY) {
    155     Shutdown ("can't find input image catalog %s", inDB.filename);
     164    // Shutdown ("can't find input image catalog %s", inDB.filename);
     165    IDmap->Nmap = 0;
     166    return TRUE;
    156167  }
    157168  if (!dvo_image_load (&inDB, VERBOSE, TRUE)) {
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/dvoverify.c

    r28268 r29060  
    154154  // convert the raw image table to Image type (byteswap if needed)
    155155  images = gfits_table_get_Image (&inDB.ftable, &Nimages, &inDB.swapped);
     156  if (!images) {
     157    fprintf (stderr, "ERROR: failed to read images\n");
     158    exit (2);
     159  }
    156160
    157161  // update additional metadata
  • branches/sc_branches/trunkTest/Ohana/src/dvomerge/src/help.c

    r27583 r29060  
    44  fprintf (stderr, "USAGE: dvomerge (input1) and (input2) to (output)\n");
    55  fprintf (stderr, "   OR: dvomerge (input) into (output)\n");
     6  fprintf (stderr, "   [-region Rmin Rmax Dmin Dmax]\n");
    67  exit (2);
    78}
     
    1011
    1112  fprintf (stderr, "USAGE: dvoconvert (input) to (output)\n");
     13
     14  exit (2);
     15}
     16
     17void dvosecfilt_usage(void) {
     18
     19  fprintf (stderr, "USAGE: dvosecfilt (input) -photcodes (photcodes.txt)\n");
    1220
    1321  exit (2);
     
    5765}
    5866
     67void dvosecfilt_help (int argc, char **argv) {
     68
     69  /* check for help request */
     70  if (get_argument (argc, argv, "-help")) goto show_help;
     71  if (get_argument (argc, argv, "-h"))    goto show_help;
     72  return;
     73
     74show_help:
     75
     76  fprintf (stderr, "USAGE\n");
     77  fprintf (stderr, "  dvosecfilt (input) (Nsecfilt)\n\n");
     78
     79  fprintf (stderr, "  change number of secfilt entries in photcode table (updates secfilt tables only)\n");
     80 
     81  fprintf (stderr, "  optional flags:\n");
     82  fprintf (stderr, "  -v                          : verbose mode\n");
     83  fprintf (stderr, "  -help                       : this list\n");
     84  fprintf (stderr, "  -h                          : this list\n\n");
     85  exit (2);
     86}
     87
  • branches/sc_branches/trunkTest/Ohana/src/gastro2/src/rfits.c

    r27435 r29060  
    1515
    1616  stars = gfits_table_get_SMPData (&table, &Nstars, NULL);
     17  if (!stars) {
     18    fprintf (stderr, "ERROR: failed to read stars\n");
     19    exit (2);
     20  }
    1721
    1822  ALLOCATE (stardata, StarData, Nstars);
  • branches/sc_branches/trunkTest/Ohana/src/getstar/src/dvoImageExtract.c

    r27435 r29060  
    3232  // convert database table to internal structure
    3333  images = gfits_table_get_Image (&db.ftable, &Nimages, &db.swapped);
     34  if (!images) {
     35    fprintf (stderr, "ERROR: failed to read images\n");
     36    exit (2);
     37  }
    3438 
    3539  matches = SelectImages (argv[1], images, Nimages, &Nmatches);
  • branches/sc_branches/trunkTest/Ohana/src/getstar/src/dvoImageOverlaps.c

    r27435 r29060  
    3535  // convert database table to internal structure
    3636  dbImages = gfits_table_get_Image (&db.ftable, &NdbImages, &db.swapped);
     37  if (!dbImages) {
     38    fprintf (stderr, "ERROR: failed to read images\n");
     39    exit (2);
     40  }
    3741 
    3842  // for (i = 1; i < 2; i++) {
  • branches/sc_branches/trunkTest/Ohana/src/getstar/src/dvoImagesAtCoords.c

    r27435 r29060  
    5555    // convert database table to internal structure
    5656    dbImages = gfits_table_get_Image (&db.ftable, &NdbImages, &db.swapped);
     57    if (!dbImages) {
     58      fprintf (stderr, "ERROR: failed to read images\n");
     59      exit (2);
     60    }
    5761  }
    5862 
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/detsearch.c

    r27435 r29060  
    3636  if (!output.Modify && !output.Delete && !output.Altpath) gfits_db_close (&db);
    3737  detrend = gfits_table_get_DetReg (&db.ftable, &Ndetrend, &db.swapped);
     38  if (!detrend) {
     39    fprintf (stderr, "ERROR: failed to read detrend info\n");
     40    exit (2);
     41  }
    3842 
    3943  match = MatchCriteria (detrend, Ndetrend, &Nmatch);          /* match basic criteria */
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/imphotsearch.c

    r27435 r29060  
    3333
    3434  image = gfits_table_get_Image (&db.ftable, &Nimage, &db.swapped);
     35  if (!image) {
     36    fprintf (stderr, "ERROR: failed to read images\n");
     37    exit (2);
     38  }
    3539
    3640  match = subset (image, Nimage, &Nmatch);
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/imregtable.c

    r27435 r29060  
    3434
    3535  image = gfits_table_get_RegImage (&ftable, &Nimage, NULL);
     36  if (!image) {
     37    fprintf (stderr, "ERROR: failed to read images\n");
     38    exit (2);
     39  }
    3640
    3741  /* load database table */
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/imsearch.c

    r27435 r29060  
    3030
    3131  image = gfits_table_get_RegImage (&db.ftable, &Nimage, &db.swapped);
     32  if (!image) {
     33    fprintf (stderr, "ERROR: failed to read images\n");
     34    exit (2);
     35  }
    3236 
    3337  match = match_criteria (image, Nimage, &Nmatch);
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/imstatreg.c

    r27611 r29060  
    6767    }
    6868    subset = gfits_table_get_RegImage (&temp_db.ftable, &Nsubset, &temp_db.swapped);
     69    if (!subset) {
     70      fprintf (stderr, "ERROR: failed to read images\n");
     71      exit (2);
     72    }
    6973    fprintf (stderr, "temporary database read\n");
    7074
     
    9397    }
    9498    image = gfits_table_get_RegImage (&image_db.ftable, &Nimage, &image_db.swapped);
     99    if (!image) {
     100      fprintf (stderr, "ERROR: failed to read images\n");
     101    exit (2);
     102    }
    95103    fprintf (stderr, "main database read\n");
    96104
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/photsearch.c

    r27435 r29060  
    4242  if (output.convert) {
    4343    photpars_old = gfits_table_get_PhotParsOld (&db.ftable, &Nphotpars, &db.swapped);
     44    if (!photpars_old) {
     45      fprintf (stderr, "ERROR: failed to read photometry parameters\n");
     46      exit (2);
     47    }
    4448    photpars = PhotParsOld_to_PhotPars (photpars_old, Nphotpars);
    4549  } else {
    4650    photpars = gfits_table_get_PhotPars (&db.ftable, &Nphotpars, &db.swapped);
     51    if (!photpars) {
     52      fprintf (stderr, "ERROR: failed to read photometry parameters\n");
     53      exit (2);
     54    }
    4755  }
    4856
  • branches/sc_branches/trunkTest/Ohana/src/imregister/src/spsearch.c

    r27435 r29060  
    3030
    3131  spectrum = gfits_table_get_Spectrum (&db.ftable, &Nspectrum, &db.swapped);
     32  if (!spectrum) {
     33    fprintf (stderr, "ERROR: failed to read spectrum\n");
     34    exit (2);
     35  }
    3236 
    3337  match = match_criteria (spectrum, Nspectrum, &Nmatch);
  • branches/sc_branches/trunkTest/Ohana/src/kapa2/src/DrawFrame.c

    r27796 r29060  
    134134    *minor = 0.1 * power;
    135135    if (axis[0].areticks == 1) {
    136       *major = 1.0 * power;
    137       *minor = 0.2 * power;
     136      *major = 0.25 * power;
     137      *minor = 0.05 * power;
    138138    }     
    139139  }
     
    142142    *minor = 0.2 * power;
    143143    if (axis[0].areticks == 1) {
    144       *major = 2.0 * power;
    145       *minor = 0.5 * power;
     144      *major = 0.5 * power;
     145      *minor = 0.1 * power;
    146146    }     
    147147  }
     
    150150    *minor = 0.5 * power;
    151151    if (axis[0].areticks == 1) {
    152       *major = 2.0 * power;
    153       *minor = 0.5 * power;
     152      *major = 1.0 * power;
     153      *minor = 0.2 * power;
    154154    }     
    155155  }
     
    158158    *minor = 0.5 * power;
    159159    if (axis[0].areticks == 1) {
    160       *major = 2.5 * power;
     160      *major = 1.0 * power;
    161161      *minor = 0.5 * power;
    162162    }     
     
    166166    *minor = 0.5 * power;
    167167    if (axis[0].areticks == 1) {
    168       *major = 5.0 * power;
    169       *minor = 1.0 * power;
     168      *major = 2.0 * power;
     169      *minor = 0.5 * power;
    170170    }     
    171171  }
     
    215215  //   }
    216216  // }
     217
     218  // XXX need to fix first and last value for printing label: currently prints first label
     219  // too early & fails to do last label if too close
    217220
    218221  // loop to find the ticks
  • branches/sc_branches/trunkTest/Ohana/src/libautocode/def/AddstarClientOptions.d

    r16810 r29060  
    22EXTNAME OPTIONS
    33TYPE    BINTABLE
    4 SIZE    80
     4SIZE    84
    55
    66FIELD     Nsigma,           NSIGMA,               double,         match radius in terms of astrometric error
     
    2222FIELD     timeref,          TIMEREF,              e_time,         time/date of input data (REFLIST only?)
    2323FIELD     imageID,          IMAGE_ID,             unsigned int,   reference to image
     24FIELD     detectionFilter,  DETECTIONFILTER,      unsigned int,   filter mask for detections from smf file
  • branches/sc_branches/trunkTest/Ohana/src/libautocode/def/autocode.c

    r28277 r29060  
    6666  if (Ncols != $SIZE) {
    6767    fprintf (stderr, "ERROR: mis-match in table size: width is %d but should be %d bytes\n", Ncols, $SIZE);
    68     exit (1);
     68    return NULL;
    6969  }
    7070
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/include/dvo.h

    r28855 r29060  
    8585# define PHOT_MAG 0x06  /* generic magnitude; never stored */
    8686
    87 /* Image.code values.  these are codes to note bad images */
    88 # define ID_IMAGE_NEW   0x0000  /* no nrphot attempted */
    89 # define ID_IMAGE_NOCAL 0x0001  /* used within nrphot to mean "don't apply fit" */
    90 # define ID_IMAGE_POOR  0x0002  /* relphot says image is bad */
    91 # define ID_IMAGE_SKIP  0x0004  /* external information image is bad */
    92 # define ID_IMAGE_FEW   0x0008  /* currently too few measurements for good value */
     87/* Image.code values -- these values are 32 bit (as of PS1_V1) */
     88typedef enum {
     89  ID_IMAGE_NEW          = 0x00000000,  /* no calibrations yet attempted */
     90  ID_IMAGE_PHOTOM_NOCAL = 0x00000001,  /* user-set value used within relphot: ignore */
     91  ID_IMAGE_PHOTOM_POOR  = 0x00000002,  /* relphot says image is bad (dMcal > limit) */
     92  ID_IMAGE_PHOTOM_SKIP  = 0x00000004,  /* user-set value: assert that this image has bad photometry */
     93  ID_IMAGE_PHOTOM_FEW   = 0x00000008,  /* currently too few measurements for photometry */
     94  ID_IMAGE_ASTROM_NOCAL = 0x00000010,  /* user-set value used within relastro: ignore */
     95  ID_IMAGE_ASTROM_POOR  = 0x00000020,  /* relastro says image is bad (dR,dD > limit) */
     96  ID_IMAGE_ASTROM_FAIL  = 0x00000040,  /* relastro fit diverged, fit not applied */
     97  ID_IMAGE_ASTROM_SKIP  = 0x00000080,  /* user-set value: assert that this image has bad astrometry */
     98  ID_IMAGE_ASTROM_FEW   = 0x00000100,  /* currently too few measurements for astrometry */
     99} DVOImageFlags;
    93100
    94101/* Measure.flags values -- these values are 32 bit (as of PS1_V1) */
    95102typedef enum {
    96   ID_MEAS_NOCAL        = 0x0001,  // detection ignored for this analysis (photcode, time range) -- internal only
    97   ID_MEAS_POOR_PHOTOM  = 0x0002,  // detection is photometry outlier                                             
    98   ID_MEAS_SKIP_PHOTOM  = 0x0004,  // detection was ignored for photometry measurement                             
    99   ID_MEAS_AREA         = 0x0008,  // detection near image edge                                               
    100   ID_MEAS_POOR_ASTROM  = 0x0010,  // detection is astrometry outlier                                             
    101   ID_MEAS_SKIP_ASTROM  = 0x0020,  // detection was ignored for astrometry measurement                             
    102   ID_MEAS_USED_OBJ     = 0x0040,  // detection was used during opdate objects                               
    103   ID_MEAS_USED_CHIP    = 0x0080,  // detection was used during update chips                                         
    104   ID_MEAS_BLEND_MEAS   = 0x0100,  // detection is within radius of multiple objects                                 
    105   ID_MEAS_BLEND_OBJ    = 0x0200,  // multiple detections within radius of object                                     
    106   ID_MEAS_UNDEF_3      = 0x0400,  // unused                                                                         
    107   ID_MEAS_UNDEF_4      = 0x0800,  // unused                                                                         
    108   ID_MEAS_BLEND_MEAS_X = 0x1000,  // detection is within radius of multiple objects across catalogs                 
    109   ID_MEAS_ARTIFACT     = 0x2000,  // detection is thought to be non-astronomical                                     
    110   ID_MEAS_UNDEF_5      = 0x4000,  // unused                                                                         
    111   ID_MEAS_UNDEF_6      = 0x8000,  // unused                                                                         
     103  ID_MEAS_NOCAL        = 0x00000001,  // detection ignored for this analysis (photcode, time range) -- internal only
     104  ID_MEAS_POOR_PHOTOM  = 0x00000002,  // detection is photometry outlier                                                 
     105  ID_MEAS_SKIP_PHOTOM  = 0x00000004,  // detection was ignored for photometry measurement                                 
     106  ID_MEAS_AREA         = 0x00000008,  // detection near image edge                                                   
     107  ID_MEAS_POOR_ASTROM  = 0x00000010,  // detection is astrometry outlier                                                 
     108  ID_MEAS_SKIP_ASTROM  = 0x00000020,  // detection was ignored for astrometry measurement                                 
     109  ID_MEAS_USED_OBJ     = 0x00000040,  // detection was used during opdate objects                                   
     110  ID_MEAS_USED_CHIP    = 0x00000080,  // detection was used during update chips                                     
     111  ID_MEAS_BLEND_MEAS   = 0x00000100,  // detection is within radius of multiple objects                             
     112  ID_MEAS_BLEND_OBJ    = 0x00000200,  // multiple detections within radius of object                                 
     113  ID_MEAS_UNDEF_3      = 0x00000400,  // unused                                                                     
     114  ID_MEAS_UNDEF_4      = 0x00000800,  // unused                                                                     
     115  ID_MEAS_BLEND_MEAS_X = 0x00001000,  // detection is within radius of multiple objects across catalogs             
     116  ID_MEAS_ARTIFACT     = 0x00002000,  // detection is thought to be non-astronomical                                 
     117  ID_MEAS_UNDEF_5      = 0x00004000,  // unused                                                                     
     118  ID_MEAS_UNDEF_6      = 0x00008000,  // unused                                                                     
    112119} DVOMeasureFlags;
    113120
     
    147154  ID_STAR_USE_PM  = 0x00100000, // proper motion used (not AVE or PAR)
    148155  ID_STAR_USE_PAR = 0x00200000, // parallax used (not AVE or PM)
     156  ID_OBJ_EXT      = 0x01000000, // extended in our data (eg, PS)
     157  ID_OBJ_EXT_ALT  = 0x02000000, // extended in external data (eg, 2MASS)
     158  ID_OBJ_GOOD     = 0x04000000, // good-quality measurement in our data (eg, PS)
     159  ID_OBJ_GOOD_ALT = 0x08000000, // good-quality measurement in external data (eg, 2MASS)
    149160} DVOAverageFlags;
    150161
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/LoadPhotcodesFITS.c

    r27580 r29060  
    4343  if (!strcmp (extname, "DVO_PHOTCODE")) {
    4444    PhotCode_Elixir *photcode_elixir = gfits_table_get_PhotCode_Elixir (&db.ftable, &Ncode, &db.swapped);
     45    if (!photcode_elixir) {
     46      fprintf (stderr, "ERROR: failed to read photcodes\n");
     47      exit (2);
     48    }
    4549    photcode = PhotCode_Elixir_To_Internal (photcode_elixir, Ncode);
    4650    free (photcode_elixir);
     
    4953  if (!strcmp (extname, "DVO_PHOTCODE_ELIXIR")) {
    5054    PhotCode_Elixir *photcode_elixir = gfits_table_get_PhotCode_Elixir (&db.ftable, &Ncode, &db.swapped);
     55    if (!photcode_elixir) {
     56      fprintf (stderr, "ERROR: failed to read photcodes\n");
     57      exit (2);
     58    }
    5159    photcode = PhotCode_Elixir_To_Internal (photcode_elixir, Ncode);
    5260    free (photcode_elixir);
     
    5563  if (!strcmp (extname, "DVO_PHOTCODE_PS1_DEV_1")) {
    5664    PhotCode_PS1_DEV_1 *photcode_ps1_dev_1 = gfits_table_get_PhotCode_PS1_DEV_1 (&db.ftable, &Ncode, &db.swapped);
     65    if (!photcode_ps1_dev_1) {
     66      fprintf (stderr, "ERROR: failed to read photcodes\n");
     67      exit (2);
     68    }
    5769    photcode = PhotCode_PS1_DEV_1_To_Internal (photcode_ps1_dev_1, Ncode);
    5870    free (photcode_ps1_dev_1);
     
    6173  if (!strcmp (extname, "DVO_PHOTCODE_PS1_DEV_2")) {
    6274    PhotCode_PS1_DEV_2 *photcode_ps1_dev_2 = gfits_table_get_PhotCode_PS1_DEV_2 (&db.ftable, &Ncode, &db.swapped);
     75    if (!photcode_ps1_dev_2) {
     76      fprintf (stderr, "ERROR: failed to read photcodes\n");
     77      exit (2);
     78    }
    6379    photcode = PhotCode_PS1_DEV_2_To_Internal (photcode_ps1_dev_2, Ncode);
    6480    free (photcode_ps1_dev_2);
     
    6783  if (!strcmp (extname, "DVO_PHOTCODE_PS1_DEV_3")) {
    6884    PhotCode_PS1_DEV_3 *photcode_ps1_dev_3 = gfits_table_get_PhotCode_PS1_DEV_3 (&db.ftable, &Ncode, &db.swapped);
     85    if (!photcode_ps1_dev_3) {
     86      fprintf (stderr, "ERROR: failed to read photcodes\n");
     87      exit (2);
     88    }
    6989    photcode = PhotCode_PS1_DEV_3_To_Internal (photcode_ps1_dev_3, Ncode);
    7090    free (photcode_ps1_dev_3);
     
    7393  if (!strcmp (extname, "DVO_PHOTCODE_PS1_V1")) {
    7494    PhotCode_PS1_V1 *photcode_ps1_v1 = gfits_table_get_PhotCode_PS1_V1 (&db.ftable, &Ncode, &db.swapped);
     95    if (!photcode_ps1_v1) {
     96      fprintf (stderr, "ERROR: failed to read photcodes\n");
     97      exit (2);
     98    }
    7599    photcode = PhotCode_PS1_V1_To_Internal (photcode_ps1_v1, Ncode);
    76100    free (photcode_ps1_v1);
     
    79103  if (!strcmp (extname, "DVO_PHOTCODE_PS1_V2")) {
    80104    PhotCode_PS1_V2 *photcode_ps1_v2 = gfits_table_get_PhotCode_PS1_V2 (&db.ftable, &Ncode, &db.swapped);
     105    if (!photcode_ps1_v2) {
     106      fprintf (stderr, "ERROR: failed to read photcodes\n");
     107      exit (2);
     108    }
    81109    photcode = PhotCode_PS1_V2_To_Internal (photcode_ps1_v2, Ncode);
    82110    free (photcode_ps1_v2);
     
    85113  if (!strcmp (extname, "DVO_PHOTCODE_PS1_REF")) {
    86114    PhotCode_PS1_REF *photcode_ps1_ref = gfits_table_get_PhotCode_PS1_REF (&db.ftable, &Ncode, &db.swapped);
     115    if (!photcode_ps1_ref) {
     116      fprintf (stderr, "ERROR: failed to read photcodes\n");
     117      exit (2);
     118    }
    87119    photcode = PhotCode_PS1_REF_To_Internal (photcode_ps1_ref, Ncode);
    88120    free (photcode_ps1_ref);
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/cmf-ps1-v1-alt.c

    r28241 r29060  
    1414  if (ftable[0].header[0].Naxis[0] != 136) {
    1515    fprintf (stderr, "ERROR: wrong format for CMF_PS1_V1_Alt: "OFF_T_FMT" vs %d\n",  ftable[0].header[0].Naxis[0], 136);
    16     exit (2);
     16    return (NULL);
    1717  }
    1818
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/coordops.c

    r28837 r29060  
    2020int XY_to_LM (double *L, double *M, double x, double y, Coords *coords) {
    2121
     22  OhanaProjection proj;
     23  OhanaProjectionMode mode;
    2224  double X, Y, X2, XY, Y2, X3, Y3;
     25
     26  proj = GetProjection (coords[0].ctype);
     27  mode = GetProjectionMode (proj);
    2328
    2429  /** convert pixel coordinates to cartesian system **/
     
    4550  }
    4651
     52  if (mode == PROJ_MODE_CARTESIAN) {
     53    *L += coords[0].crval1;
     54    *M += coords[0].crval2;
     55  }
     56
    4757  return (TRUE);
    4858}
     
    6474  /** Locally Cartesian Projections **/
    6575  if (mode == PROJ_MODE_CARTESIAN) {
    66     *ra  = L + coords[0].crval1;
    67     *dec = M + coords[0].crval2;
    68 
    6976    /* mosaic astrometry : WRP is chip astrometry; apply mosaic (DIS) term */
    7077    if (proj == PROJ_WRP) {
    7178      if (!gotMosaic) return (FALSE);
    72       XY_to_RD (ra, dec, *ra, *dec, &mosaic);
     79      XY_to_RD (ra, dec, L, M, &mosaic);
    7380    }
    7481    return (TRUE);
     
    183190
    184191  double phi, theta;
    185   double Lo, Mo;
    186192  double sphi, cphi, stht, ctht;
    187193  double salp, calp, sdel, cdel, sdp, cdp;
     
    201207    if (proj == PROJ_WRP) {
    202208      if (!gotMosaic) return (FALSE);
    203       RD_to_XY (&Lo, &Mo, ra, dec, &mosaic);
    204       *L = (Lo - coords[0].crval1);
    205       *M = (Mo - coords[0].crval2);
     209      RD_to_XY (L, M, ra, dec, &mosaic);
    206210      return (TRUE);
    207211    }
    208     *L = (ra  - coords[0].crval1);
    209     *M = (dec - coords[0].crval2);
     212    *L = ra;
     213    *M = dec;
    210214    return (TRUE);
    211215  }
     
    310314  double dX, dY, Lo, Mo, dL, dM;
    311315  double dLdX, dLdY, dMdX, dMdY, Do;
     316  OhanaProjection proj;
     317  OhanaProjectionMode mode;
     318
     319  proj = GetProjection (coords[0].ctype);
     320  mode = GetProjectionMode (proj);
    312321
    313322  *x = 0;
    314323  *y = 0;
     324
     325  if (mode == PROJ_MODE_CARTESIAN) {
     326    L -= coords[0].crval1;
     327    M -= coords[0].crval2;
     328  }
    315329
    316330  /* start with linear solution for X,Y */
     
    422436}
    423437
     438enum {COORD_TYPE_NONE, COORD_TYPE_PC, COORD_TYPE_ROT, COORD_TYPE_CD, COORD_TYPE_LIN};
     439
    424440int GetCoords (Coords *coords, Header *header) {
    425441 
     
    428444  double equinox;
    429445  char *ctype;
     446  int mode;
    430447 
    431448  rotate = 0.0;
     
    437454  strcpy (coords[0].ctype, "NONE");
    438455 
    439   status = FALSE;
    440   if (gfits_scan (header, "CTYPE2", "%s", 1, coords[0].ctype)) {
    441     status  = gfits_scan (header, "CRVAL1", "%lf", 1, &coords[0].crval1);
    442     status &= gfits_scan (header, "CRPIX1", "%f", 1, &coords[0].crpix1);
    443     status &= gfits_scan (header, "CRVAL2", "%lf", 1, &coords[0].crval2); 
    444     status &= gfits_scan (header, "CRPIX2", "%f", 1, &coords[0].crpix2);
    445 
    446     if (gfits_scan (header, "CDELT1", "%f", 1, &coords[0].cdelt1)) {
    447       status &= gfits_scan (header, "CDELT2", "%f", 1, &coords[0].cdelt2);
    448       if (gfits_scan (header, "CROTA2", "%lf", 1, &rotate)) {
    449         Lambda = coords[0].cdelt2 / coords[0].cdelt1;
    450         coords[0].pc1_1 =  cos(rotate*RAD_DEG);
    451         coords[0].pc1_2 = -sin(rotate*RAD_DEG) * Lambda;
    452         coords[0].pc2_1 =  sin(rotate*RAD_DEG) / Lambda;
    453         coords[0].pc2_2 =  cos(rotate*RAD_DEG);
    454       }
    455       if (gfits_scan (header, "PC001001", "%f", 1, &coords[0].pc1_1)) {
    456         status &= gfits_scan (header, "PC001002", "%f", 1, &coords[0].pc1_2);
    457         status &= gfits_scan (header, "PC002001", "%f", 1, &coords[0].pc2_1);
    458         status &= gfits_scan (header, "PC002002", "%f", 1, &coords[0].pc2_2);
    459       }
     456  mode = COORD_TYPE_NONE;
     457  {   
     458    int haveCTYPE, haveCDELT, haveCROTA, haveCDij, havePCij, haveRAo;
     459    float tmp;
     460    char stmp[80];
     461   
     462    // there are a few different representations for scale and rotation.  choose an appropriate
     463    // set: (CDELTi + CROTAi), (CDELTi + PCij), (CDij),
     464
     465    haveCTYPE = gfits_scan (header, "CTYPE2",   "%s", 1, stmp);
     466    haveCDELT = gfits_scan (header, "CDELT1",   "%f", 1, &tmp);
     467    haveCROTA = gfits_scan (header, "CROTA1",   "%f", 1, &tmp);
     468    haveCDij  = gfits_scan (header, "CD1_1",    "%f", 1, &tmp);
     469    havePCij  = gfits_scan (header, "PC001001", "%f", 1, &tmp);
     470    haveRAo   = gfits_scan (header, "RA_O",     "%f", 1, &tmp);
     471   
     472    if (haveCTYPE && havePCij  && haveCDELT) { mode = COORD_TYPE_PC;   goto gotit; }
     473    if (haveCTYPE && haveCROTA && haveCDELT) { mode = COORD_TYPE_ROT;  goto gotit; }
     474    if (haveCTYPE && haveCDij)               { mode = COORD_TYPE_CD;   goto gotit; }
     475    if (haveRAo)                             { mode = COORD_TYPE_LIN;  goto gotit; }
     476    // fprintf (stderr, "no valid WCS keywords\n");
     477    return (FALSE);
     478  }
     479 
     480gotit:
     481
     482  status = TRUE;
     483  switch (mode) {
     484    case COORD_TYPE_PC:
     485      status &= gfits_scan (header, "CTYPE2",   "%s",  1, coords[0].ctype);
     486      status &= gfits_scan (header, "CRVAL1",   "%lf", 1, &coords[0].crval1);
     487      status &= gfits_scan (header, "CRPIX1",   "%f",  1, &coords[0].crpix1);
     488      status &= gfits_scan (header, "CRVAL2",   "%lf", 1, &coords[0].crval2); 
     489      status &= gfits_scan (header, "CRPIX2",   "%f",  1, &coords[0].crpix2);
     490
     491      status &= gfits_scan (header, "CDELT1",   "%f",  1, &coords[0].cdelt1);
     492      status &= gfits_scan (header, "CDELT2",   "%f",  1, &coords[0].cdelt2);
     493      status &= gfits_scan (header, "PC001001", "%f",  1, &coords[0].pc1_1);
     494      status &= gfits_scan (header, "PC001002", "%f",  1, &coords[0].pc1_2);
     495      status &= gfits_scan (header, "PC002001", "%f",  1, &coords[0].pc2_1);
     496      status &= gfits_scan (header, "PC002002", "%f",  1, &coords[0].pc2_2);
    460497
    461498      /* set NPLYTERM based on header.  if NPLYTERM is missing, it should have a
     
    491528          break;
    492529      }
    493     } else {
    494       if (gfits_scan (header, "CD1_1", "%f", 1, &coords[0].pc1_1)) {
    495         status &= gfits_scan (header, "CD1_2", "%f", 1, &coords[0].pc1_2);
    496         status &= gfits_scan (header, "CD2_1", "%f", 1, &coords[0].pc2_1);
    497         status &= gfits_scan (header, "CD2_2", "%f", 1, &coords[0].pc2_2);
    498         /* renormalize */
    499         scale = hypot (coords[0].pc1_1, coords[0].pc1_2);
    500         coords[0].cdelt1 = coords[0].cdelt2 = scale;
    501         coords[0].pc1_1 /= scale;
    502         coords[0].pc1_2 /= scale;
    503         coords[0].pc2_1 /= scale;
    504         coords[0].pc2_2 /= scale;
    505       } else {
    506         status = FALSE;
    507       }
    508     }
    509   } else {
    510     /* some of my thesis data uses this simple linear model - convert on read? */
    511     if (gfits_scan (header, "RA_O", "%lf", 1, &coords[0].crval1)) {
    512       status  = gfits_scan (header, "RA_X", "%f", 1, &coords[0].pc1_1);
     530      break;
     531
     532    case COORD_TYPE_ROT:
     533      status &= gfits_scan (header, "CTYPE2", "%s",  1, coords[0].ctype);
     534      status &= gfits_scan (header, "CRVAL1", "%lf", 1, &coords[0].crval1);
     535      status &= gfits_scan (header, "CRPIX1", "%f",  1, &coords[0].crpix1);
     536      status &= gfits_scan (header, "CRVAL2", "%lf", 1, &coords[0].crval2); 
     537      status &= gfits_scan (header, "CRPIX2", "%f",  1, &coords[0].crpix2);
     538
     539      status &= gfits_scan (header, "CDELT1", "%f", 1, &coords[0].cdelt1);
     540      status &= gfits_scan (header, "CDELT2", "%f", 1, &coords[0].cdelt2);
     541
     542      status &= gfits_scan (header, "CROTA2", "%lf", 1, &rotate);
     543      Lambda = coords[0].cdelt2 / coords[0].cdelt1;
     544      coords[0].pc1_1 =  cos(rotate*RAD_DEG);
     545      coords[0].pc1_2 = -sin(rotate*RAD_DEG) * Lambda;
     546      coords[0].pc2_1 =  sin(rotate*RAD_DEG) / Lambda;
     547      coords[0].pc2_2 =  cos(rotate*RAD_DEG);
     548      break;
     549
     550    case COORD_TYPE_CD:
     551      status &= gfits_scan (header, "CTYPE2", "%s",  1, coords[0].ctype);
     552      status &= gfits_scan (header, "CRVAL1", "%lf", 1, &coords[0].crval1);
     553      status &= gfits_scan (header, "CRPIX1", "%f",  1, &coords[0].crpix1);
     554      status &= gfits_scan (header, "CRVAL2", "%lf", 1, &coords[0].crval2); 
     555      status &= gfits_scan (header, "CRPIX2", "%f",  1, &coords[0].crpix2);
     556
     557      status &= gfits_scan (header, "CD1_1", "%f", 1, &coords[0].pc1_1);
     558      status &= gfits_scan (header, "CD1_2", "%f", 1, &coords[0].pc1_2);
     559      status &= gfits_scan (header, "CD2_1", "%f", 1, &coords[0].pc2_1);
     560      status &= gfits_scan (header, "CD2_2", "%f", 1, &coords[0].pc2_2);
     561      /* renormalize */
     562      scale = hypot (coords[0].pc1_1, coords[0].pc1_2);
     563      coords[0].cdelt1 = coords[0].cdelt2 = scale;
     564      coords[0].pc1_1 /= scale;
     565      coords[0].pc1_2 /= scale;
     566      coords[0].pc2_1 /= scale;
     567      coords[0].pc2_2 /= scale;
     568      break;
     569
     570    case COORD_TYPE_LIN:
     571      /* some of my thesis data uses this simple linear model - convert on read? */
     572      status &= gfits_scan (header, "RA_O", "%lf", 1, &coords[0].crval1);
     573      status &= gfits_scan (header, "RA_X", "%f", 1, &coords[0].pc1_1);
    513574      status &= gfits_scan (header, "RA_Y", "%f", 1, &coords[0].pc1_2);
    514575      status &= gfits_scan (header, "DEC_O", "%lf", 1, &coords[0].crval2); 
     
    518579      coords[0].cdelt1 = coords[0].cdelt2 = 1.0;
    519580      strcpy (coords[0].ctype, "GENE");
    520     }
    521   }
     581      break;
     582  }
     583
    522584  if (status) {
    523585    if (!gfits_scan (header, "EQUINOX", "%lf", 1, &equinox)) {
     
    530592    }
    531593  }
     594
    532595  if (!status) {
    533     fprintf (stderr, "error getting all elements for coordinate mode %s\n", coords[0].ctype);
     596    // fprintf (stderr, "error getting all elements for coordinate mode %s\n", coords[0].ctype);
    534597    coords[0].crval1 = coords[0].crpix1 = coords[0].cdelt1 = 0.0;
    535598    coords[0].crval2 = coords[0].crpix2 = coords[0].cdelt2 = 0.0;
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/dvo_catalog_mef.c

    r28241 r29060  
    125125    /* no conversions currently defined */
    126126    catalog[0].missing = gfits_table_get_Missing (&ftable, &catalog[0].Nmissing, NULL);
     127    if (!catalog[0].missing) {
     128      fprintf (stderr, "ERROR: failed to read missing\n");
     129      exit (2);
     130    }
    127131    if (Nmissing != catalog[0].Nmiss_disk) {
    128132      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmissing,  catalog[0].Nmiss_disk);
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/dvo_catalog_split.c

    r28241 r29060  
    285285    /* no conversions currently defined : this just does the byte swap */
    286286    catalog[0].missing = gfits_table_get_Missing (&ftable, &Nmissing, NULL);
     287    if (!catalog[0].missing) {
     288      fprintf (stderr, "ERROR: failed to read missing\n");
     289      exit (2);
     290    }
    287291    if (Nmissing != catalog[0].Nmiss_disk) {
    288292      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmissing,  catalog[0].Nmiss_disk);
     
    473477    /* no conversions currently defined : this just does the byte swap */
    474478    catalog[0].missing = gfits_table_get_Missing (&ftable, &Nmissing, NULL);
     479    if (!catalog[0].missing) {
     480      fprintf (stderr, "ERROR: failed to read missing\n");
     481      exit (2);
     482    }
    475483    if (Nmissing != Nrows) {
    476484      fprintf (stderr, "Warning: mismatch between Nmissing in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmissing,  Nrows);
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/dvo_convert.c

    r28241 r29060  
    3232    Average_##TYPE *tmpAverage; \
    3333    tmpAverage = gfits_table_get_Average_##TYPE (ftable, Naverage, NULL); \
     34    if (!tmpAverage) { \
     35      fprintf (stderr, "ERROR: failed to read averages\n"); \
     36      exit (2); \
     37    } \
    3438    average = Average_##TYPE##_ToInternal (tmpAverage, *Naverage, primary); \
    3539    free (tmpAverage); \
     
    3943  if (!strcmp (extname, "DVO_AVERAGE")) {
    4044    average = gfits_table_get_Average (ftable, Naverage, NULL);
     45    if (!average) {
     46      fprintf (stderr, "ERROR: failed to read averages\n");
     47      exit (2);
     48    }
    4149    *format = DVO_FORMAT_INTERNAL;
    4250    return (average);
     
    111119    Measure_##TYPE *tmpMeasure; \
    112120    tmpMeasure = gfits_table_get_Measure_##TYPE (ftable, Nmeasure, NULL); \
     121    if (!tmpMeasure) { \
     122      fprintf (stderr, "ERROR: failed to read measures\n"); \
     123      exit (2); \
     124    } \
    113125    measure = Measure_##TYPE##_ToInternal (tmpMeasure, *Nmeasure); \
    114126    free (tmpMeasure); \
     
    118130  if (!strcmp (extname, "DVO_MEASURE")) {
    119131    measure = gfits_table_get_Measure (ftable, Nmeasure, NULL);
     132    if (!measure) {
     133      fprintf (stderr, "ERROR: failed to read measures\n");
     134      exit (2);
     135    }
    120136    *format = DVO_FORMAT_INTERNAL;
    121137    return (measure);
     
    190206    SecFilt_##TYPE *tmpSecFilt; \
    191207    tmpSecFilt = gfits_table_get_SecFilt_##TYPE (ftable, Nsecfilt, NULL); \
     208    if (!tmpSecFilt) { \
     209      fprintf (stderr, "ERROR: failed to read secfilts\n"); \
     210      exit (2); \
     211    } \
    192212    secfilt = SecFilt_##TYPE##_ToInternal (tmpSecFilt, *Nsecfilt); \
    193213    free (tmpSecFilt); \
     
    197217  if (!strcmp (extname, "DVO_SECFILT")) {
    198218    secfilt = gfits_table_get_SecFilt (ftable, Nsecfilt, NULL);
     219    if (!secfilt) {
     220      fprintf (stderr, "ERROR: failed to read secfilts\n");
     221      exit (2);
     222    }
    199223    *format = DVO_FORMAT_INTERNAL;
    200224    return (secfilt);
     
    263287    Image_Elixir *tmpimage;
    264288    tmpimage = gfits_table_get_Image_Elixir (ftable, &Nimage, NULL);
     289    if (!tmpimage) {
     290      fprintf (stderr, "ERROR: failed to read images\n");
     291      exit (2);
     292    }
    265293    ftable[0].buffer = (char *) Image_Elixir_ToInternal (tmpimage, Nimage);
    266294    free (tmpimage);
     
    284312    *format = DVO_FORMAT_##FORMAT; \
    285313    tmpimage = gfits_table_get_Image_##TYPE (ftable, &Nimage, NULL); \
     314    if (!tmpimage) { \
     315      fprintf (stderr, "ERROR: failed to read images\n"); \
     316      exit (2); \
     317    } \
    286318    ftable[0].buffer = (char *) Image_##TYPE##_ToInternal (tmpimage, Nimage); \
    287319    free (tmpimage); \
     
    296328    Image *image;
    297329    image = gfits_table_get_Image (ftable, &Nimage, NULL);
     330    if (!image) {
     331      fprintf (stderr, "ERROR: failed to read images\n");
     332      exit (2);
     333    }
    298334    *format = DVO_FORMAT_INTERNAL;
    299335    return (TRUE);
  • branches/sc_branches/trunkTest/Ohana/src/libdvo/src/skyregion_io.c

    r27435 r29060  
    4040  ALLOCATE (skytable, SkyTable, 1);
    4141  skytable[0].regions = gfits_table_get_SkyRegion (&ftable, &skytable[0].Nregions, NULL);
     42  if (!skytable[0].regions) {
     43    fprintf (stderr, "ERROR: failed to read sky regions\n");
     44    exit (2);
     45  }
     46
    4247  ALLOCATE (skytable[0].filename, char *, skytable[0].Nregions);
    4348  for (i = 0; i < skytable[0].Nregions; i++) {
  • branches/sc_branches/trunkTest/Ohana/src/markstar/src/find_images.c

    r7080 r29060  
    2626
    2727  timage = gfits_table_get_Image (&db[0].ftable, &Ntimage, &db[0].swapped);
     28  if (!timage) {
     29    fprintf (stderr, "ERROR: failed to read images\n");
     30    exit (2);
     31  }
    2832
    2933  /* set up buffers for images, temporary storage */
  • branches/sc_branches/trunkTest/Ohana/src/mosastro/src/rfits.c

    r27435 r29060  
    3232  off_t Nstars;
    3333  stars = gfits_table_get_SMPData (&table, &Nstars, NULL);
     34  if (!stars) {
     35    fprintf (stderr, "ERROR: failed to read stars\n");
     36    exit (2);
     37  }
     38
    3439  mychip[0].Nstars = Nstars;
    3540  gfits_scan (table.header, "NAXIS1", "%d", 1, &Nx);
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.astro/cgrid.c

    r26258 r29060  
    153153  SetVector (&Yvec, OPIHI_FLT, NELEMENTS);
    154154 
     155  { // sanity check
     156    float Nelem;
     157    Nelem = 180.0 / minorRA;
     158    if (!isfinite(Nelem) || (fabs(Nelem) > 10000)) { fprintf (stderr, "absurd cgrid spacing %f\n", minorRA); return FALSE; }
     159    Nelem = 180.0 / minorDEC;
     160    if (!isfinite(Nelem) || (fabs(Nelem) > 10000)) { fprintf (stderr, "absurd cgrid spacing %f\n", minorDEC); return FALSE; }
     161  }
     162
    155163  /***  do consecutive RA lines, first increasing **/
    156164  OnPic = TRUE;
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.data/Makefile

    r27817 r29060  
    1919$(SRC)/init.$(ARCH).o          \
    2020$(SRC)/accum.$(ARCH).o          \
     21$(SRC)/applyfit1d.$(ARCH).o     \
    2122$(SRC)/applyfit2d.$(ARCH).o     \
    22 $(SRC)/applyfit1d.$(ARCH).o     \
     23$(SRC)/applyfit3d.$(ARCH).o     \
    2324$(SRC)/box.$(ARCH).o            \
    2425$(SRC)/book.$(ARCH).o           \
     
    4647$(SRC)/fit1d.$(ARCH).o          \
    4748$(SRC)/fit2d.$(ARCH).o          \
     49$(SRC)/fit3d.$(ARCH).o          \
    4850$(SRC)/gaussj.$(ARCH).o \
    4951$(SRC)/gaussdeviate.$(ARCH).o   \
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.data/applyfit2d.c

    r20936 r29060  
    1010
    1111  if (argc != 4) {
    12     gprint (GP_ERR, "USAGE: applyfit x y z\n");
     12    gprint (GP_ERR, "USAGE: applyfit2d x y z\n");
    1313    return (FALSE);
    1414  }
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.data/fit2d.c

    r27817 r29060  
    168168    }
    169169
     170    /** test print **/
     171    if (VERBOSE) {
     172      for (i = 0; i < wterm; i++) {
     173        for (j = 0; j < wterm; j++) {
     174          gprint (GP_ERR, "%g  ", c[i][j]);
     175        }
     176        gprint (GP_ERR, "\n");
     177      }
     178      gprint (GP_ERR, "-----\n");
     179    }
     180
    170181    dgaussjordan (c, b, wterm, 1);
    171182
     
    176187          gprint (GP_ERR, "x^%d y^%d: %g\n", nx, ny, b[i][0]);
    177188        }
     189      }
     190    }
     191
     192    /** test print **/
     193    if (VERBOSE) {
     194      for (i = 0; i < wterm; i++) {
     195        for (j = 0; j < wterm; j++) {
     196          gprint (GP_ERR, "%g  ", c[i][j]);
     197        }
     198        gprint (GP_ERR, "\n");
    178199      }
    179200    }
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.data/init.c

    r27817 r29060  
    55int applyfit1d       PROTO((int, char **));
    66int applyfit2d       PROTO((int, char **));
     7int applyfit3d       PROTO((int, char **));
    78int box              PROTO((int, char **));
    89int book_command     PROTO((int, char **));
     
    3435int fit1d            PROTO((int, char **));
    3536int fit2d            PROTO((int, char **));
     37int fit3d            PROTO((int, char **));
    3638int gaussjordan      PROTO((int, char **));
    3739int gaussdeviate     PROTO((int, char **));
     
    138140  {1, "applyfit1d",   applyfit1d,       "apply 1-d fit to new vector"},
    139141  {1, "applyfit2d",   applyfit2d,       "apply 2-d fit to new vector"},
     142  {1, "applyfit3d",   applyfit3d,       "apply 3-d fit to new vector"},
    140143  {1, "book",         book_command,     "commands to manipulate book/page/word data"},
    141144  {1, "box",          box,              "draw a box on the plot"},
     
    168171  {1, "fit1d",        fit1d,            "fit polynomial to vector pair"},
    169172  {1, "fit2d",        fit2d,            "fit 2-d polynomial to vector triplet"},
     173  {1, "fit3d",        fit3d,            "fit 3-d polynomial to vector quad"},
    170174  {1, "gaussdev",     gaussdeviate,     "generate a gaussian deviate vector"},
    171175  {1, "gaussint",     gaussintegral,    "return the integrated gaussian vector"},
  • branches/sc_branches/trunkTest/Ohana/src/opihi/cmd.data/vgauss.c

    r20936 r29060  
    4242  if ((ovec = SelectVector (argv[4], ANYVECTOR, TRUE)) == NULL) return (FALSE);
    4343
    44   REQUIRE_VECTOR_FLT (xvec, FALSE);
    45   REQUIRE_VECTOR_FLT (yvec, FALSE);
    46   REQUIRE_VECTOR_FLT (svec, FALSE);
     44  CastVector (xvec, OPIHI_FLT);
     45  CastVector (yvec, OPIHI_FLT);
     46  CastVector (svec, OPIHI_FLT);
    4747
    4848  Npts = xvec[0].Nelements;
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/ImageSelection.c

    r28955 r29060  
    3838/* free loaded images */
    3939void FreeImageSelection () {
    40   if (image != NULL) free (image);
     40  if (image != NULL) FreeImages(image);
    4141  if (subset != NULL) free (subset);
    4242  image = NULL;
     
    4747Image *MatchImage (unsigned int time, short int source, unsigned int imageID) {
    4848
    49   int m;
     49  int m = -1;
    5050
    5151  if ((imageID != 0) && (imageID < Nimage)) {
    52     m = (int) imageID - 1;
    53   } else {
     52    // imageID is in range for the array of images. If the table is still in order and
     53    // no images have been deleted the index of the image we are looking for will be imageID - 1
     54    // If this is the case, we have it. Otherwise we'll have to go search for it below
     55    int guess = (int) imageID - 1;
     56    if (image[guess].imageID == imageID) {
     57        m = guess;
     58    }
     59  }
     60  if (m == -1) {
    5461    m = match_image_subset (image, subset, Nsubset, time, source);
    5562  }
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/LoadImages.c

    r27435 r29060  
    11# include "dvoshell.h"
     2
     3// XXX: Maybe make this a command line option
     4int dvoUseImageCache = 1;
     5
     6static char *lastFilename = NULL;
     7static time_t lastModified = 0;
     8static Image *imageCache = NULL;
     9static off_t cacheNimage = 0;
     10
     11static time_t getLastModified(char *filename);
    212
    313Image *LoadImages (off_t *Nimage) {
     
    1222  catdir = GetCATDIR ();
    1323  sprintf (filename, "%s/Images.dat", catdir);
     24
     25  if (lastFilename) {
     26    if (dvoUseImageCache && !strcmp(lastFilename, filename)) {
     27      // Make sure the file hasn't changed since we loaded it
     28      if (getLastModified(filename) == lastModified) {
     29        *Nimage = cacheNimage;
     30        return  imageCache;
     31      }
     32    }
     33    free(lastFilename);
     34    lastFilename = NULL;
     35    free(imageCache);
     36    imageCache = NULL;
     37    cacheNimage = 0;
     38    lastModified = 0;
     39  }
     40
    1441
    1542  gfits_db_init (&db);
     
    3865
    3966  image = gfits_table_get_Image (&db.ftable, Nimage, &db.swapped);
     67  if (!image) {
     68    fprintf (stderr, "ERROR: failed to read images\n");
     69    return (NULL);
     70  }
     71  if (dvoUseImageCache && image) {
     72    cacheNimage = *Nimage;
     73    imageCache = image;
     74    lastFilename = strdup(filename);
     75    lastModified = getLastModified(filename);
     76  }
    4077  return (image);
    4178}
     79
     80static time_t getLastModified(char *filename) {
     81  struct stat statbuf;
     82  if (!stat(filename, &statbuf)) {
     83    return statbuf.st_mtime;
     84  } else {
     85    return 0;
     86  }
     87}
     88
     89void FreeImages(Image *images) {
     90  if (!dvoUseImageCache && (images != NULL)) {
     91    free(images);
     92  } else {
     93    // defer free until next LoadImages with a different or modified Images.dat
     94  }
     95}
     96
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/cmpReadFile.c

    r27435 r29060  
    2020
    2121  smpdata = gfits_table_get_SMPData (&table, &Nstars, NULL);
     22  if (!smpdata) {
     23    fprintf (stderr, "ERROR: failed to read stars\n");
     24    exit (2);
     25  }
    2226
    2327  ALLOCATE (stars, CMPstars, Nstars);
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/gimages.c

    r28872 r29060  
    247247  set_int_variable ("IMAGEphotcode:n", Nfound);
    248248
    249   free (image);
     249  FreeImages (image);
    250250  free (subset);
    251251
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/images.c

    r27530 r29060  
    319319  free (Xvec.elements.Flt);
    320320  free (Yvec.elements.Flt);
    321   free (image);
     321  FreeImages (image);
    322322  return (TRUE);
    323323
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/imdense.c

    r27435 r29060  
    7070  free (Xvec.elements.Flt);
    7171  free (Yvec.elements.Flt);
    72   free (image);
     72  FreeImages (image);
    7373  return (TRUE);
    7474
  • branches/sc_branches/trunkTest/Ohana/src/opihi/dvo/imextract.c

    r27587 r29060  
    137137
    138138  // free (subset);
    139   free (image);
     139  FreeImages (image);
    140140
    141141  if (vec) free (vec);
  • branches/sc_branches/trunkTest/Ohana/src/opihi/include/dvoshell.h

    r28955 r29060  
    302302int           InitPhotcodes         PROTO((void));
    303303Image        *LoadImages            PROTO((off_t *Nimage));
     304void          FreeImages            PROTO((Image *images));
    304305Image        *MatchImage            PROTO((unsigned int time, short int source, unsigned int imageID));
    305306Coords       *MatchMosaic           PROTO((unsigned int time, short int source));
  • branches/sc_branches/trunkTest/Ohana/src/photdbc/src/copy_images.c

    r28331 r29060  
    4646   
    4747  image = gfits_table_get_Image (&in.ftable, &Nimage, &in.swapped);
     48  if (!image) {
     49      fprintf (stderr, "ERROR: failed to read images\n");
     50      exit (2);
     51  }
    4852  dvo_image_addrows (&out, image, Nimage);
    4953
  • branches/sc_branches/trunkTest/Ohana/src/photdbc/src/find_images.c

    r12332 r29060  
    2828
    2929  timage = gfits_table_get_Image (&db[0].ftable, &Ntimage, &db[0].swapped);
     30  if (!timage) {
     31      fprintf (stderr, "ERROR: failed to read images\n");
     32      exit (2);
     33  }
    3034
    3135  nimage = 0;
     
    97101      if (found) {
    98102        image[nimage] = timage[i];
    99         if (image[nimage].code == ID_IMAGE_NOCAL) {
    100           image[nimage].code &= ~ID_IMAGE_NOCAL;
     103        if (image[nimage].code == ID_IMAGE_PHOTOM_NOCAL) {
     104          image[nimage].code &= ~ID_IMAGE_PHOTOM_NOCAL;
    101105        }           
    102106        line_number[nimage] = i;
  • branches/sc_branches/trunkTest/Ohana/src/photdbc/src/flag_measures.c

    r12332 r29060  
    1616
    1717  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     18  if (!image) {
     19      fprintf (stderr, "ERROR: failed to read images\n");
     20      exit (2);
     21  }
    1822
    1923  PhotNsec = GetPhotcodeNsecfilt ();
  • branches/sc_branches/trunkTest/Ohana/src/relastro/Makefile

    r28184 r29060  
    5858$(SRC)/CoordOps.$(ARCH).o            \
    5959$(SRC)/FixProblemImages.$(ARCH).o    \
     60$(SRC)/StarMaps.$(ARCH).o    \
    6061$(SRC)/high_speed_catalogs.$(ARCH).o  \
    6162$(SRC)/high_speed_objects.$(ARCH).o  \
  • branches/sc_branches/trunkTest/Ohana/src/relastro/doc/notes.txt

    r27435 r29060  
     1
     22010.07.15
     3
     4  Some issues:
     5
     6  * when I apply the fitted image parameters (UpdateObjectOffsets), I
     7    currently check for good/bad fits and warn if the image is bad.
     8    But the correction requires multiple re-loads of the catalog
     9    tables.
     10
     11  * I would like to check for bad fits by defining a grid of test
     12    points and seeing how much they move.  However, for some chips,
     13    the region of valid data is much smaller than the regino with any
     14    data. 
     15
     16  * I would like to define a grid (say, Nx * Ny with Nx >> polynomial
     17    order) and only use those grid regions in testing the solution
     18
     19  * This means knowing which detections belong on a specific image.
     20    I'm not sure if there is a good way to do this.  at the moment, it
     21    is easy to do detection -> image, but not so easy to do
     22    image->detection.
     23
     24  * That might argue for generating the grid for each image, then
     25    running through all detections and, as detections are examined,
     26    populate the grid elements for their own image.
    127
    2282010.03.24
  • branches/sc_branches/trunkTest/Ohana/src/relastro/include/relastro.h

    r28184 r29060  
    33# include <kapa.h>
    44# include <signal.h>
     5# include <assert.h>
    56
    67typedef enum {
     
    7677  int    Nmeas;
    7778} StatType;
     79
     80# define MARKTIME(MSG,...) { \
     81  float dtime; \
     82  gettimeofday (&stop, (void *) NULL); \
     83  dtime = DTIME (stop, start); \
     84  fprintf (stderr, MSG, __VA_ARGS__); }
    7885
    7986/* global variables set in parameter file */
     
    145152int DoUpdateChips;
    146153int DoUpdateMosaics;
     154
     155// StarMap parameters:
     156int NX_MAP;
     157int NY_MAP;
     158double DPOS_MAX;
     159double ADDSTAR_RADIUS;
    147160
    148161/*** relphot prototypes ***/
     
    207220void create_image_db (FITS_DB *db);
    208221void save_catalogs (Catalog *catalog, int Ncatalog);
     222
     223int reload_images (FITS_DB *db);
    209224
    210225int           main                PROTO((int argc, char **argv));
     
    267282double **poly2d_copy (double **poly, int Nx, int Ny);
    268283double poly2d_eval (double **poly, int Nx, int Ny, double x, double y);
    269 int fit_apply_coords (CoordFit *fit, Coords *coords);
     284int fit_apply_coords (CoordFit *fit, Coords *coords, int keepRef);
    270285int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo);
    271286CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo);
    272 int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
     287int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image);
    273288void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
    274289void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
     
    293308StarData *getImageRef (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode);
    294309StarData *getImageRaw (Catalog *catalog, int Ncatalog, off_t im, off_t *Nstars, CoordMode mode);
     310off_t getImageByID (off_t ID);
    295311
    296312Mosaic *getmosaics (off_t *N);
     
    331347int high_speed_objects (SkyRegion *region, Catalog *catalog);
    332348int MeasMatchesPhotcode(Measure *measure, PhotCode **photcodeSet, int Nset);
     349
     350int initStarMaps ();
     351int updateStarMaps(Catalog *catalog);
     352int createStarMapPoints();
     353int checkStarMap(int N);
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/ConfigInit.c

    r25757 r29060  
    3232  GetConfig (config, "PM_TOOFEW",              "%d",  0, &PM_TOOFEW);
    3333  GetConfig (config, "POS_TOOFEW",             "%d",  0, &POS_TOOFEW);
     34
     35  GetConfig (config, "RELASTRO_MAP_NX",        "%d",  0, &NX_MAP);
     36  GetConfig (config, "RELASTRO_MAP_NY",        "%d",  0, &NY_MAP);
     37  GetConfig (config, "RELASTRO_DPOS_MAX",      "%lf", 0, &DPOS_MAX);
     38  GetConfig (config, "ADDSTAR_RADIUS",         "%lf", 0, &ADDSTAR_RADIUS);
    3439
    3540  GetConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/FitChip.c

    r27581 r29060  
    33
    44// XXX make these user parameters
    5 # define FIT_CHIP_MAX_ERROR 0.05
    65# define FIT_CHIP_NITER     3
    76# define FIT_CHIP_NSIGMA    3.0
    8 
    9 
    10 // XXX we should test if the fit is sufficiently constrained across the chip, or if the
    11 // new positions deviate too much from the old positions
    12 
    13 // XXX add visualization tools: per chip residual plots
    147
    158// XXX save the fit[0].Npts value in the image table?
     
    1710// XXX save measurements of the fit quality (scatter, chisq) in the image table
    1811
    19 int FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
     12int FitChip (StarData *raw, StarData *ref, int Nmatch, Image *image) {
    2013
    2114  int i, Nscatter, Niter, skip;
     
    2922    for (i = Nscatter = 0; i < Nmatch; i++) {
    3023      if (raw[i].mask) continue;
    31       if (isnan(raw[i].dMag) || raw[i].dMag > FIT_CHIP_MAX_ERROR) continue;
     24      if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) continue;
    3225
    3326      dL = raw[i].L - ref[i].L;
     
    3932    }
    4033
    41     // for a 2D Gaussian, 40% of the points are within 1 sigma; dRmax is ~ 3 sigma
    42     dsort (values, Nscatter);
    43     dRmax = FIT_CHIP_NSIGMA*values[(int)(0.40*Nscatter)];
    44     relastroVisualPlotScatter(values, dRmax, Nscatter);
    45     relastroVisualPlotRawRef(raw, ref, dRmax, Nmatch);
     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    }
    4644
    4745    // fit the requested order polynomial
    4846    if (CHIPORDER > 0) {
    49       coords[0].Npolyterms = CHIPORDER;
     47      image[0].coords.Npolyterms = CHIPORDER;
    5048    }
    51     fit = fit_init (coords[0].Npolyterms);
     49    fit = fit_init (image[0].coords.Npolyterms);
    5250
    5351    // generate the fit matches
     
    5654        continue;
    5755      }
    58       if (isnan(raw[i].dMag) || raw[i].dMag > FIT_CHIP_MAX_ERROR) {
     56      if (isnan(raw[i].dMag) || raw[i].dMag > SIGMA_LIM) {
    5957        continue;
    6058      }
     
    6462      dM = raw[i].M - ref[i].M;
    6563      dR = hypot (dL, dM);
    66       if (dR > dRmax) continue;
     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;
    6768
    6869      fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, raw[i].dPos);
     
    7172    // check if the fit has enough data points for the polynomial order
    7273    skip = FALSE;
    73     switch (coords[0].Npolyterms) {
     74    switch (image[0].coords.Npolyterms) {
    7475      case 0:
    7576      case 1:
     
    8384        break;
    8485      default:
    85         fprintf (stderr, "invalid chip order %d\n", coords[0].Npolyterms);
     86        fprintf (stderr, "invalid chip order %d\n", image[0].coords.Npolyterms);
    8687        skip = TRUE;
    8788    }
    8889    if (skip) {
    89       fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, coords[0].Npolyterms);
     90      if (VERBOSE) fprintf (stderr, "insufficient measurements (%d) for requested order (%d)\n", fit[0].Npts, image[0].coords.Npolyterms);
    9091      fit_free (fit);
    9192      free (values);
     93      image[0].flags |= ID_IMAGE_ASTROM_FEW;
    9294      return FALSE;
    9395    }
     
    98100    if (!fit_eval (fit)) {
    99101      fprintf (stderr, "failed to fit new model\n");
     102      image[0].flags |= ID_IMAGE_ASTROM_FAIL;
    100103      return FALSE;
    101104    }
    102105
    103     if (!fit_apply_coords (fit, coords)) {
     106    if (!fit_apply_coords (fit, &image[0].coords, FALSE)) {
    104107      fprintf (stderr, "failed to fit new model\n");
     108      image[0].flags |= ID_IMAGE_ASTROM_FAIL;
    105109      return FALSE;
    106110    }
     
    109113
    110114    for (i = 0; i < Nmatch; i++) {
    111       XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, coords);
     115      XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[0].coords);
    112116    }
    113117
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/FitMosaic.c

    r16810 r29060  
    2626  }
    2727  fit_eval (fit);
    28   fit_apply_coords (fit, coords);
     28  fit_apply_coords (fit, coords, TRUE);
    2929  fit_free (fit);
    3030
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/FitSimple.c

    r27435 r29060  
    2626  }
    2727  fit_eval (fit);
    28   fit_apply_coords (fit, coords);
     28  fit_apply_coords (fit, coords, TRUE);
    2929  fit_free (fit);
    3030
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/ImageOps.c

    r28241 r29060  
    319319}
    320320
     321void dump_measures(Average *average, Measure *measure) {
     322
     323  off_t j, off;
     324
     325  for (j = 0; j < average[0].Nmeasure; j++) {
     326    off = average[0].measureOffset + j;
     327    fprintf (stderr, "%f, %f\n", measure[off].dR, measure[off].dD);
     328  }
     329  return;
     330}
     331
    321332// return StarData values for detections in the specified image, converting coordinates from the
    322333// chip positions: X,Y -> L,M -> P,Q -> R,D
     
    325336  off_t i, m, c, n, nPos;
    326337  double X, Y, L, M, P, Q, R, D, dR, dD;
    327   double dPos;
     338  double dPos, DPOS_MAX_ASEC;
    328339
    329340  Mosaic *mosaic;
     
    341352  }
    342353  imcoords = &image[im].coords;
     354
     355  if (moscoords) {
     356    DPOS_MAX_ASEC = 3600.0*DPOS_MAX*hypot(moscoords[0].cdelt1, moscoords[0].cdelt2);
     357  } else {
     358    DPOS_MAX_ASEC = 3600.0*DPOS_MAX*hypot(imcoords[0].cdelt1, imcoords[0].cdelt2);
     359  }
    343360
    344361  // accumulate the rms position offsets.  if this value, or any specific entry, is too
     
    372389
    373390    // complain if the new location is far from the average location
    374     if (fabs(dR) > 2.0) {
    375       fprintf (stderr, "!");
    376       setBadCoords (im); // report a failure for this image
    377       return;
    378     }
    379     if (fabs(dD) > 2.0) {
    380       fprintf (stderr, "*");
    381       setBadCoords (im); // report a failure for this image
    382       return;
     391    // NOTE: This should never happen, or our StarMap tests are not working
     392    if (fabs(dR) > 1.5*ADDSTAR_RADIUS) {
     393      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);
     394      dump_measures (&catalog[c].average[n], catalog[c].measure);
     395      // abort ();
     396    }
     397    if (fabs(dD) > 1.5*ADDSTAR_RADIUS) {
     398      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);
     399      dump_measures (&catalog[c].average[n], catalog[c].measure);
     400      // abort ();
    383401    }
    384402
    385403    // complain if the new location is far from the old location
    386     if (fabs(catalog[c].measure[m].dR - dR) > 2.0) {
    387       fprintf (stderr, "@");
    388       setBadCoords (im); // report a failure for this image
    389       return;
    390     }
    391     if (fabs(catalog[c].measure[m].dD - dD) > 2.0) {
    392       fprintf (stderr, "#");
    393       setBadCoords (im); // report a failure for this image
    394       return;
     404    if (fabs(catalog[c].measure[m].dR - dR) > DPOS_MAX_ASEC) {
     405      fprintf (stderr, "measurement is far from original location (R): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
     406      dump_measures (&catalog[c].average[n], catalog[c].measure);
     407      // abort();
     408    }
     409    if (fabs(catalog[c].measure[m].dD - dD) > DPOS_MAX_ASEC) {
     410      fprintf (stderr, "measurement is far from original location (D): %f %f (%f %f)\n", catalog[c].average[n].R, catalog[c].average[n].D, dR, dD);
     411      dump_measures (&catalog[c].average[n], catalog[c].measure);
     412      // abort();
    395413    }
    396414
     
    414432
    415433  saveOffsets (dPos, nPos, im);
     434
     435  // XXX this function should probably update the average positions as well.
    416436
    417437  return;
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/UpdateChips.c

    r28241 r29060  
    3131    // FitChip does iterative, clipped fitting
    3232    // fprintf (stderr, "image "OFF_T_FMT" : Nstars: "OFF_T_FMT"\n",  i,  Nraw);
    33     if (!FitChip (raw, ref, Nraw, &image[i].coords)) {
    34       fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     33    if (!FitChip (raw, ref, Nraw, &image[i])) {
     34      if (VERBOSE) fprintf (stderr, "reject fit for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
    3535      oldCoords = getCoords (i);
    3636      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     37      free (raw);
     38      free (ref);
     39      continue;
     40    }
     41
     42    if (!checkStarMap (i)) {
     43      if (VERBOSE) fprintf (stderr, "fit diverges too much for image %s ("OFF_T_FMT") : Nstars: "OFF_T_FMT"\n", image[i].name,  i,  Nraw);
     44      oldCoords = getCoords (i);
     45      memcpy (&image[i].coords, oldCoords, sizeof(Coords));
     46      image[i].flags |= ID_IMAGE_ASTROM_POOR;
    3747    }
    3848
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/UpdateMeasures.c

    r27435 r29060  
    55  off_t i, Nimage;
    66  Image *image;
     7
     8  int badImage =
     9    ID_IMAGE_ASTROM_NOCAL |
     10    ID_IMAGE_ASTROM_POOR |
     11    ID_IMAGE_ASTROM_FAIL |
     12    ID_IMAGE_ASTROM_SKIP |
     13    ID_IMAGE_ASTROM_FEW;
    714
    815  image = getimages (&Nimage);
     
    1320    if (!strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
    1421
     22    // skip images that have failed solutions (divergent or otherwise)
     23    if (image[i].flags & badImage) continue;
     24
    1525    /* convert measure coordinates to raw entries */
    1626    fixImageRaw (catalog, Ncatalog, i);
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/bcatalog.c

    r28241 r29060  
    4545      offset = catalog[0].average[i].measureOffset + j;
    4646     
    47       //filter objects based on user supplied criteria
     47      // filter objects based on user supplied criteria
    4848      if (!MeasFilterTest(&catalog[0].measure[offset])) {
    4949        catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
     
    5151      }
    5252
    53       //filter out outliers
     53      // filter out outliers
    5454      if (FlagOutlier && (catalog[0].measure[offset].dbFlags & ID_MEAS_POOR_ASTROM)) {
    5555        catalog[0].measure[offset].dbFlags &= ~ID_MEAS_USED_CHIP;
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/fitpoly.c

    r27581 r29060  
    110110  if (fit[0].Npts == 0) {
    111111    fprintf (stderr, "warning: no valid pts\n");
     112    return (FALSE);
    112113  }
    113114
     
    140141  }
    141142
    142   for (i = 0; i < fit[0].Nelems; i++) {
     143  for (i = 0; FALSE && (i < fit[0].Nelems); i++) {
    143144    ix = i % fit[0].Nterms;
    144145    iy = i / fit[0].Nterms;
    145     // fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n",
    146     // ix, iy, vector[i][0], ix, iy, vector[i][1]);
     146    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n",
     147    ix, iy, vector[i][0], ix, iy, vector[i][1]);
    147148  }     
    148149 
     
    151152  }
    152153
    153   for (i = 0; i < fit[0].Nelems; i++) {
     154  for (i = 0; FALSE && i < fit[0].Nelems; i++) {
    154155    ix = i % fit[0].Nterms;
    155156    iy = i / fit[0].Nterms;
    156     // fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n",
    157     // ix, iy, vector[i][0], ix, iy, vector[i][1]);
     157    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n",
     158    ix, iy, vector[i][0], ix, iy, vector[i][1]);
    158159  }     
    159160
     
    274275/* this should only apply to the polynomial, not the projection terms */
    275276/* compare with psastro supporting code */
    276 int fit_apply_coords (CoordFit *fit, Coords *coords) {
     277int fit_apply_coords (CoordFit *fit, Coords *coords, int keepRef) {
    277278
    278279  double Xo, Yo, R1, R2;
    279280  CoordFit *modfit;
    280281
    281   /* I have L,M = fit(X,Y). set corresponding terms for coords */
    282 
    283   // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
    284   // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
    285 
    286   if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
    287     fprintf (stderr, "failed to modify model\n");
    288     return (FALSE);
    289   }
    290   coords[0].crpix1 = Xo;
    291   coords[0].crpix2 = Yo;
    292  
    293   // resulting fit should have zero constant terms
    294   modfit = CoordsSetCenter (fit, Xo, Yo);
    295 
    296   /* we do not modify crval1,2: these are kept at the default values */
     282  if (keepRef) {
     283    // adjust crpix1,2 as needed:
     284    // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
     285    // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
     286
     287    if (!CoordsGetCenter (fit, 0.001, &Xo, &Yo)) {
     288      fprintf (stderr, "failed to modify model\n");
     289      return (FALSE);
     290    }
     291    coords[0].crpix1 = Xo;
     292    coords[0].crpix2 = Yo;
     293    /* we do not modify crval1,2: these are kept at the default values */
     294 
     295    // resulting fit should have zero constant terms
     296    modfit = CoordsSetCenter (fit, Xo, Yo);
     297  } else {
     298    // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
     299    // P = cd1*x, Q = cd2*y
     300    // L = pc1_1*P + pc1_2*Q + ...
     301
     302    /* modify crval1,2: these are kept at the default values */
     303
     304    coords[0].crpix1 = 0.0;
     305    coords[0].crpix2 = 0.0;
     306    modfit = fit;
     307  }
    297308
    298309  // set cdelt1, cdelt2
     
    331342    coords[0].polyterms[6][1] = modfit[0].yfit[0][3]*R2*R2*R2;
    332343  }
    333 
    334   fit_free (modfit);
     344 
     345  if (keepRef) {
     346    fit_free (modfit);
     347  } else {
     348    fit_apply (fit, &coords[0].crval1, &coords[0].crval2, coords[0].crpix1, coords[0].crpix2);
     349  }
    335350  /* keep the order and type from initial values */
    336351 
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/initialize.c

    r28184 r29060  
    100100  initstats (STATMODE);
    101101
    102   // IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
     102  // IMAGE_BAD = ID_IMAGE_ASTROM_POOR | ID_IMAGE_ASTROM_FEW | ID_IMAGE_ASTROM_SKIP;
    103103  // STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    104104  // MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_ASTROM | ID_MEAS_SKIP_ASTROM | ID_MEAS_AREA;
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/load_catalogs.c

    r24308 r29060  
    99
    1010  ALLOCATE (catalog, Catalog, skylist[0].Nregions);
     11
     12  initStarMaps();
    1113
    1214  // load data from each region file, only use bright stars
     
    3436      FlagOutliers(&tcatalog);
    3537    }
     38
     39    updateStarMaps (&tcatalog);
    3640
    3741    // select only the brighter stars
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/load_images.c

    r27581 r29060  
    3333  // convert database table to internal structure
    3434  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     35  if (!image) {
     36      fprintf (stderr, "ERROR: failed to read images\n");
     37      exit (2);
     38  }
     39
    3540  MARKTIME("  convert image table: %f sec\n", dtime);
    3641
     
    5257  return (skylist);
    5358}
     59
     60int reload_images (FITS_DB *db) {
     61
     62  Image     *image;
     63  off_t     Nimage, Nx, i;
     64  VTable    *vtable;
     65
     66  image = getimages (&Nimage);
     67
     68  vtable = &db[0].vtable;
     69
     70  gfits_scan (vtable[0].header, "NAXIS1", OFF_T_FMT, 1,  &Nx);
     71  for (i = 0; i < Nimage; i++) {
     72    memcpy (vtable[0].buffer[i], &image[i], Nx);
     73  }
     74  return (TRUE);
     75}
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/mkpolyterm.c

    r27581 r29060  
    6666      Xo -= beta[0][0];
    6767      Yo -= beta[1][0];
     68
    6869      dPos = hypot(beta[0][0], beta[1][0]);
    6970      if (i == 0) {
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/relastro.c

    r28184 r29060  
    11# include "relastro.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__); }
    82
    93int main (int argc, char **argv) {
     
    4943
    5044  /* load catalog data from region files : subselect high-quality measurements */
     45  // XXX pass in the image table
     46  // XXX who carries the image grid?
    5147  catalog = load_catalogs (skylist, &Ncatalog, TRUE);
    5248  MARKTIME("load catalog data: %f sec\n", dtime);
     
    6359    // plot_mosaic_fields (catalog);
    6460  }
     61
     62  // set test points based on the starmap
     63  createStarMapPoints();
    6564
    6665  /* major modes */
     
    9897  freeImageBins (1);
    9998
     99  reload_images (&db);
     100   
    100101  // iterate over catalogs to make detection coordinates consistant
    101102  UpdateObjectOffsets (skylist);
    102103
    103104  // iterate over catalogs to make detection coordinates consistant
    104   FixProblemImages (skylist);
     105  // FixProblemImages (skylist);
    105106
    106107  // save the updated image parameters
  • branches/sc_branches/trunkTest/Ohana/src/relastro/src/select_images.c

    r28241 r29060  
    2828  double *RmaxSky;
    2929  off_t *index;
     30
     31  int badImage =
     32    ID_IMAGE_ASTROM_POOR |
     33    ID_IMAGE_ASTROM_FAIL |
     34    ID_IMAGE_ASTROM_FEW;
    3035
    3136  if (skylist[0].Nregions < 1) {
     
    171176      image[nimage] = timage[i];
    172177      /* always allow 'few' images to succeed, if possible */
    173       if (image[nimage].flags & ID_IMAGE_FEW) {
    174         image[nimage].flags &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
     178      if (image[nimage].flags & ID_IMAGE_ASTROM_FEW) {
     179        image[nimage].flags &= ~ID_IMAGE_ASTROM_FEW;
    175180      }
    176181      if (RESET) {
     
    178183        assignMcal (&image[nimage], (double *) NULL, -1);
    179184        image[nimage].dMcal = NAN;
    180         image[nimage].flags &= ~ID_IMAGE_POOR;
     185        image[nimage].flags &= ~badImage;
    181186      }
    182187      line_number[nimage] = i;
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/ImageOps.c

    r28241 r29060  
    323323    /* on PoorImages run, skip good images */
    324324    if (PoorImages) {
    325       bad = image[i].flags & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
     325      bad = image[i].flags & (ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP);
    326326      if (!bad) continue;
    327327    }     
     
    369369      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
    370370      if (mark) {
    371         image[i].flags |= ID_IMAGE_FEW;
     371        image[i].flags |= ID_IMAGE_PHOTOM_FEW;
    372372        Nfew ++;
    373373      } else {
    374         image[i].flags &= ~ID_IMAGE_FEW;
     374        image[i].flags &= ~ID_IMAGE_PHOTOM_FEW;
    375375      }     
    376376    }
     
    387387
    388388  if (PoorImages) {
    389     IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
     389    IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
    390390    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    391391    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
     
    429429  for (i = 0; i < Nimage; i++) {
    430430    mark = FALSE;
    431     image[i].flags &= ~ID_IMAGE_POOR;
     431    image[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
    432432    mark = (image[i].dMcal > MaxScatter) || (fabs(image[i].Mcal - MedOffset) > MaxOffset);
    433433    if (mark) {
    434434      Nmark ++;
    435       image[i].flags |= ID_IMAGE_POOR;
     435      image[i].flags |= ID_IMAGE_PHOTOM_POOR;
    436436    } else {
    437       image[i].flags &= ~ID_IMAGE_POOR;
     437      image[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
    438438    }
    439439  }
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/MosaicOps.c

    r28241 r29060  
    189189      image[im].dMcal = mosaic[i].dMcal;
    190190      image[im].Xm = mosaic[i].Xm;
    191       image[im].flags |= (mosaic[i].flags & ID_IMAGE_FEW);
    192       image[im].flags |= (mosaic[i].flags & ID_IMAGE_POOR);
     191      image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_FEW);
     192      image[im].flags |= (mosaic[i].flags & ID_IMAGE_PHOTOM_POOR);
    193193    }
    194194  }     
     
    351351    /* on PoorImages run, skip good images */
    352352    if (PoorImages) {
    353       bad = mosaic[i].flags & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
     353      bad = mosaic[i].flags & (ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP);
    354354      if (!bad) continue;
    355355    }     
     
    401401      if (mark) {
    402402        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]);
    403         mosaic[i].flags |= ID_IMAGE_FEW;
     403        mosaic[i].flags |= ID_IMAGE_PHOTOM_FEW;
    404404        Nfew ++;
    405405      } else {
    406         mosaic[i].flags &= ~ID_IMAGE_FEW;
     406        mosaic[i].flags &= ~ID_IMAGE_PHOTOM_FEW;
    407407      }
    408408    }
     
    421421
    422422  if (PoorImages) {
    423     IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
     423    IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
    424424    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    425425    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
     
    593593    if (mark) {
    594594      Nmark ++;
    595       mosaic[i].flags |= ID_IMAGE_POOR;
     595      mosaic[i].flags |= ID_IMAGE_PHOTOM_POOR;
    596596    } else {
    597       mosaic[i].flags &= ~ID_IMAGE_POOR;
     597      mosaic[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
    598598    }
    599599  }
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/StarOps.c

    r28241 r29060  
    188188  StatType stats;
    189189  PhotCode *code;
     190  DVOAverageFlags flagBits;
     191
     192  flagBits = ID_OBJ_EXT | ID_OBJ_EXT_ALT | ID_OBJ_GOOD | ID_OBJ_GOOD_ALT;
    190193
    191194  // pre-allocate a list for stats purposes
     
    281284        }
    282285
     286        // we attempt to set a few flags here; reset those bits before trying:
     287        catalog[i].average[j].flags &= ~flagBits;
     288
    283289        if (nEXT && (nEXT > nPSF)) {
    284           catalog[i].average[j].flags |= 0x00010000;
     290          catalog[i].average[j].flags |= ID_OBJ_EXT;
    285291        }
    286292        if (goodPS1) {
    287           catalog[i].average[j].flags |= 0x00020000;
     293          catalog[i].average[j].flags |= ID_OBJ_GOOD;
    288294        }
    289295        if (Galaxy2MASS) {
    290           catalog[i].average[j].flags |= 0x00040000;
     296          catalog[i].average[j].flags |= ID_OBJ_EXT_ALT;
    291297        }
    292298        if (good2MASS) {
    293           catalog[i].average[j].flags |= 0x00080000;
     299          catalog[i].average[j].flags |= ID_OBJ_GOOD_ALT;
    294300        }
    295301      }
     
    302308}
    303309
    304 /* set measure.Mcal for all measures except ID_MEAS_NOCAL and ID_IMAGE_NOCAL */
     310/* set measure.Mcal for all measures except ID_MEAS_NOCAL and ID_IMAGE_PHOTOM_NOCAL */
    305311int setMcalOutput (Catalog *catalog, int Ncatalog) {
    306312
     
    310316
    311317  MEAS_BAD = ID_MEAS_NOCAL;
    312   IMAGE_BAD = ID_IMAGE_NOCAL;
     318  IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL;
    313319
    314320  for (i = 0; i < Ncatalog; i++) {
     
    470476      /* apply to list of all relevant measurements, including IMAGE_POOR & IMAGE_FEW */
    471477      image_bad = IMAGE_BAD;
    472       IMAGE_BAD = ID_IMAGE_NOCAL;
     478      IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL;
    473479      m = catalog[i].average[j].measureOffset;
    474480      N = 0;
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/initialize.c

    r17242 r29060  
    2727  initstats (STATMODE);
    2828
    29   IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
     29  IMAGE_BAD = ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_SKIP;
    3030  STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    3131  MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM | ID_MEAS_AREA;
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/load_images.c

    r28241 r29060  
    2525  // convert database table to internal structure
    2626  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     27  if (!image) {
     28      fprintf (stderr, "ERROR: failed to read images\n");
     29      exit (2);
     30  }
    2731
    2832  // select the images which overlap the selected sky regions
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/select_images.c

    r28241 r29060  
    157157      image[nimage] = timage[i];
    158158      /* always allow 'few' images to succeed, if possible */
    159       if (image[nimage].flags & ID_IMAGE_FEW) {
    160         image[nimage].flags &= ~(ID_IMAGE_FEW | ID_IMAGE_POOR);
     159      if (image[nimage].flags & ID_IMAGE_PHOTOM_FEW) {
     160        image[nimage].flags &= ~(ID_IMAGE_PHOTOM_FEW | ID_IMAGE_PHOTOM_POOR);
    161161      }
    162162      if (RESET) {
    163163        assignMcal (&image[nimage], (double *) NULL, -1);
    164164        image[nimage].dMcal = NAN;
    165         image[nimage].flags &= ~ID_IMAGE_POOR;
     165        image[nimage].flags &= ~ID_IMAGE_PHOTOM_POOR;
    166166      }
    167167      line_number[nimage] = i;
  • branches/sc_branches/trunkTest/Ohana/src/relphot/src/setMrelFinal.c

    r28241 r29060  
    7575
    7676  /* allow measures from images marked POOR and FEW */
    77   if (pass >= 3) IMAGE_BAD = ID_IMAGE_NOCAL;
     77  if (pass >= 3) IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL;
    7878 
    7979  /* allow measures marked as outliers (POOR) and off image region (AREA) */
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/find_image_sgroups.c

    r27435 r29060  
    1313  imlink = *Imlink;
    1414  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     15  if (!image) {
     16      fprintf (stderr, "ERROR: failed to read images\n");
     17      exit (2);
     18  }
    1519
    1620  coords.crpix1 = coords.crpix2 = 0.0;
     
    2832  for (i = 0; i < Nimage; i++) {
    2933    if (imlink[i].sgroup != NULL) continue;
    30     if (image[i].flags & ID_IMAGE_NOCAL) continue;
     34    if (image[i].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
    3135
    3236    /* new sgroup, set ref coords */
     
    5357
    5458    for (j = 0; j < Nimage; j++) {
    55       if (image[j].flags & ID_IMAGE_NOCAL) continue;
     59      if (image[j].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
    5660      if (imlink[j].sgroup != NULL) continue;
    5761
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/find_image_tgroups.c

    r27435 r29060  
    1212
    1313  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     14  if (!image) {
     15      fprintf (stderr, "ERROR: failed to read images\n");
     16      exit (2);
     17  }
     18
    1419  ALLOCATE (imlink, ImageLink, Nimage);
    1520
     
    6469      if (image[j].tzero < tmin[i]) continue;
    6570      if (image[j].tzero > tmax[i]) continue;
    66       if (image[j].flags & ID_IMAGE_NOCAL) continue;
     71      if (image[j].flags & ID_IMAGE_PHOTOM_NOCAL) continue;
    6772     
    6873      group[i].image[Nentry] = &image[j];
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/initialize.c

    r27790 r29060  
    1616  }
    1717
    18   IMAGE_BAD = ID_IMAGE_NOCAL | ID_IMAGE_POOR | ID_IMAGE_SKIP | ID_IMAGE_FEW;
     18  IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL | ID_IMAGE_PHOTOM_POOR | ID_IMAGE_PHOTOM_SKIP | ID_IMAGE_PHOTOM_FEW;
    1919
    2020  initstats (STATMODE);
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/load_images.c

    r28241 r29060  
    2222  /* use a vtable to keep the images to be calibrated */
    2323  image = gfits_table_get_Image (&db[0].ftable, Nimage, &db[0].swapped);
     24  if (!image) {
     25      fprintf (stderr, "ERROR: failed to read images\n");
     26      exit (2);
     27  }
    2428
    2529  fprintf (stderr, "loaded "OFF_T_FMT" images\n", *Nimage);
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/match_zpts_to_images.c

    r28717 r29060  
    7171    image[Ni].Mcal = SCALE*code[0].C - zpts[Nz].zpt;
    7272    image[Ni].dMcal = zpts[Nz].zpt_err;
    73     image[Ni].flags &= ~ID_IMAGE_NOCAL; // clear the NOCAL flag
     73    image[Ni].flags &= ~ID_IMAGE_PHOTOM_NOCAL; // clear the NOCAL flag
    7474    zpts[Nz].found = TRUE;
    7575    NImatch ++;
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/subset_images.c

    r27435 r29060  
    99  /* use a vtable to keep the images to be calibrated */
    1010  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     11  if (!image) {
     12      fprintf (stderr, "ERROR: failed to read images\n");
     13      exit (2);
     14  }
    1115
    1216  Nkeep = 0;
     
    1620  for (i = 0; i < Nimage; i++) {
    1721     
    18     image[i].flags |= ID_IMAGE_NOCAL;
     22    image[i].flags |= ID_IMAGE_PHOTOM_NOCAL;
    1923
    2024    /* select images by photcode */
     
    2731      if (image[i].tzero > TSTOP) continue;
    2832    }
    29     image[i].flags &= ~ID_IMAGE_NOCAL;
     33    image[i].flags &= ~ID_IMAGE_PHOTOM_NOCAL;
    3034    keep[Nkeep] = i;
    3135    Nkeep ++;
  • branches/sc_branches/trunkTest/Ohana/src/uniphot/src/update.c

    r27435 r29060  
    1414
    1515  image = gfits_table_get_Image (&db[0].ftable, &Nimage, &db[0].swapped);
     16  if (!image) {
     17      fprintf (stderr, "ERROR: failed to read images\n");
     18      exit (2);
     19  }
    1620
    1721  /* clear the NOCAL flags */
    1822  for (i = 0; i < Nimage; i++) {
    19     image[i].flags &= ~ID_IMAGE_NOCAL;
     23    image[i].flags &= ~ID_IMAGE_PHOTOM_NOCAL;
    2024  }
    2125
  • branches/sc_branches/trunkTest/PS-IPP-PStamp/lib/PS/IPP/PStamp/Job.pm

    r28562 r29060  
    11301130                    }
    11311131                    $cam_path_base = $run->{cam_path_base};
    1132                     $astrom_file = $run->{astrom_file};
     1132                    $astrom_file = $run->{astrom};
    11331133                }
    11341134                my $astrom_file_resolved = $ipprc->file_resolve($astrom_file);
  • branches/sc_branches/trunkTest/ippMonitor/Makefile.in

    r28724 r29060  
    6161$(DESTWWW)/cleanTmpDirectory.php \
    6262$(DESTWWW)/warpProcessedExp_Images.php \
    63 $(DESTWWW)/warpProcessedExp.php
     63$(DESTWWW)/warpProcessedExp.php \
     64$(DESTWWW)/diskUsage.php
    6465
    6566DEFSRC = \
     
    103104$(DESTWWW)/chipProcessedImfile.php \
    104105$(DESTWWW)/chipProcessedImfile_failure.php \
     106$(DESTWWW)/failedChipProcessedImfile.php \
    105107$(DESTWWW)/camSummary.php \
    106108$(DESTWWW)/camStageExp.php \
     
    109111$(DESTWWW)/camProcessedExp_Images.php \
    110112$(DESTWWW)/camProcessedExp_failure.php \
     113$(DESTWWW)/failedCamProcessedExp.php \
    111114$(DESTWWW)/camIQstats.php \
    112115$(DESTWWW)/camProcessedImfile.php \
     
    118121$(DESTWWW)/fakeProcessedImfile.php \
    119122$(DESTWWW)/fakeProcessedImfile_failure.php \
     123$(DESTWWW)/failedFakeProcessedImfile.php \
    120124$(DESTWWW)/pzPendingExp.php \
    121125$(DESTWWW)/pzPendingImfile.php \
     
    142146$(DESTWWW)/warpProcessedSkyfiles.php \
    143147$(DESTWWW)/warpFailedSkyfiles.php \
     148$(DESTWWW)/failedWarpSkyfiles.php \
    144149$(DESTWWW)/diffSummary.php \
    145150$(DESTWWW)/diffSummary_Images.php \
     
    148153$(DESTWWW)/diffProcessedSkyfile.php \
    149154$(DESTWWW)/diffProcessedSkyfile_Images.php \
    150 $(DESTWWW)/diffFailedSkyfile.php \
     155$(DESTWWW)/failedDiffSkyfile.php \
    151156$(DESTWWW)/stackRun.php \
    152157$(DESTWWW)/stackSummary.php \
     
    156161$(DESTWWW)/stackProcessedSkyfile_Images.php \
    157162$(DESTWWW)/stackFailedSkyfile.php \
     163$(DESTWWW)/failedStackSkyfile.php \
    158164$(DESTWWW)/staticskyRun.php \
    159165$(DESTWWW)/staticskySummary.php \
     
    168174$(DESTWWW)/magicProcessedMasks_Images.php \
    169175$(DESTWWW)/magicFailedTrees.php \
     176$(DESTWWW)/failedMagicTrees.php \
    170177$(DESTWWW)/magicFailedNodes.php \
     178$(DESTWWW)/failedMagicNodes.php \
    171179$(DESTWWW)/magicFailedMasks.php \
     180$(DESTWWW)/failedMagicMasks.php \
    172181$(DESTWWW)/distSummary.php \
    173182$(DESTWWW)/distTarget.php \
  • branches/sc_branches/trunkTest/ippMonitor/raw/czartool_labels.php

    r28861 r29060  
    1010//$db = dbconnect($ID['proj']);
    1111$czardb = dbconnect("czardb"); // HACK to connect to czarDb
     12$lastUpdateTime = getLastUpdateTime($czardb);
     13
    1214$gpc1db = dbconnect($ID['proj']);
    1315
     
    2224else {$myMenu = "ipp.imfiles.dat";}
    2325
    24 menu($myMenu, 'Czartool', 'ipp.css', $ID['link'], $ID['proj']);
     26menu($myMenu, 'Czartool on '.$lastUpdateTime, 'ipp.css', $ID['link'], $ID['proj']);
    2527
    2628$pass = $ID['pass'];
     
    3436$selectedRevertMode = $_GET[revertmode];
    3537
    36 if ($selectedLabel == "") $selectedLabel = "all_labels";
    37 if ($selectedStage == "") $selectedStage = "all_stages";
     38if ($selectedLabel == "") { $selectedLabel = "all_stdscience_labels"; }
     39if ($selectedStage == "") { $selectedStage = "all_stages"; }
     40
     41echo "<p  align=\"center\"> Current status of the IPP as of $lastUpdateTime (faults are shown in parentheses). <br>Documentation can be found <a href=\"http://svn.pan-starrs.ifa.hawaii.edu/trac/ipp/wiki/Processing\">here</a> and cluster load monitored <a href=\"http://ganglia.pan-starrs.ifa.hawaii.edu/?r=hour&s=descending&c=IPP%2520Production\">here</a></p>";
    3842
    3943// deal with reverts: turn on or off if requested and pass current revert state for this stage onto labels table later
     
    5256$distLabels = getLabels($czardb, "distribution");
    5357$pubLabels = getLabels($czardb, "publishing");
     58$updateLabels = getLabels($czardb, "update");
    5459
    5560if ($debug) {
     
    6166
    6267$states=array("full","new","drop","wait");
    63 $stages=array("chip","cam","fake","warp","stack","diff","magic","magicDS","dist");
     68$stages=array("burntool", "chip","cam","fake","warp","stack","diff","magic","magicDS","dist");
    6469$servers=array(
    6570        "addstar",
     
    8287echo "<input type=\"hidden\" name=\"menu\" value=\"$menu\">\n";
    8388echo "</form>\n";
    84 $lastUpdateTime = getLastUpdateTime($czardb);
    85 echo "<p  align=\"center\"> Current status of IPP (any faults are shown in parentheses). NOTE: This data is good as of: $lastUpdateTime </p>";
    8689
    8790echo "<table>\n";
     
    9295
    9396    echo "<td> \n";
    94       createLabelsTable($pass, $proj, $czardb, $stdsLabels, $distLabels, $pubLabels, $stages, $states, "new", $selectedLabel, $selectedStage);
     97      createLabelsTable($pass, $proj, $czardb, "stdscience", $stdsLabels, $distLabels, $pubLabels, $stages, $states, "new", $selectedLabel, $selectedStage);
     98    echo "</td>\n";
     99  echo "</tr>\n";
     100
     101  echo "<tr>\n";
     102    echo "<td>\n";
     103      echo "<img src=\"czartool_getplot.php?type=h&label=$selectedLabel&stage=$selectedStage\"><br>";
     104    echo "</td>\n";
     105
     106    echo "<td> \n";
     107      createLabelsTable($pass, $proj, $czardb, "update", $updateLabels, $distLabels, $pubLabels, $stages, $states, "new", $selectedLabel, $selectedStage);
    95108    echo "</td>\n";
    96109  echo "</tr>\n";
     
    100113      echo "<tr valign=top>\n";
    101114        echo "<td> \n";
    102           echo "<img src=\"czartool_getplot.php?type=h&label=$selectedLabel&stage=$selectedStage\"><br>";
     115          echo "<img src=\"czartool_getplot.php?type=s\"><br>";
    103116        echo "</td>\n";
    104117        echo "<td>\n";
     
    116129      echo "<tr valign=top>\n";
    117130        echo "<td> \n";
    118           echo "<img src=\"czartool_getplot.php?type=s\"><br>";
    119131        echo "</td>\n";
    120132        echo "<td>\n";
     
    211223#
    212224###########################################################################
    213 function createLabelsTable($pass, $proj, $db, $stdsLabels, $distLabels, $pubLabels, $stages, $states, $selectedState, $selectedLabel, $selectedStage) {
     225function createLabelsTable($pass, $proj, $db, $server, $labels, $distLabels, $pubLabels, $stages, $states, $selectedState, $selectedLabel, $selectedStage) {
    214226
    215227    // set up table columns
     
    218230    echo "<tr><td></td>\n";
    219231
     232    echo "<p  align=\"center\"> Current labels for $server server (D=distributing, P=publishing)</p>";
     233
    220234    write_header_cell($class, "");
    221235    write_header_cell($class, "");
     
    223237    foreach ($stages as &$stage) {
    224238
     239        if ($stage == "burntool") continue;
    225240        $reverting = getRevertStatus($db, $stage);
    226241        $link = "czartool_labels.php?pass=" . $pass . "&proj=" . $proj . "&label=" . $selectedLabel . "&stage=" . $selectedStage . "&revertstage=" . $stage . "&revertmode=";
     
    234249    echo "</tr>\n";
    235250    echo "<tr><td></td>\n";
     251    write_header_cell($class, " ");
    236252    write_header_cell($class, "Label (in order of priority)");
    237     write_header_cell($class, "Distributing?");
    238     write_header_cell($class, "Publishing?");
    239253    foreach ($stages as &$stage) {
    240254       
     
    254268
    255269    // write rows
    256     foreach ($stdsLabels as &$stdsLabel) {
     270    foreach ($labels as &$thisLabel) {
    257271
    258272        $distributing = false;
     
    260274        foreach ($distLabels as &$distLabel) {
    261275
    262             if ($stdsLabel == $distLabel) { $distributing = true; break;}
     276            if ($thisLabel == $distLabel) { $distributing = true; break;}
    263277        }
    264278        foreach ($pubLabels as &$pubLabel) {
    265279
    266             if ($stdsLabel == $pubLabel) { $publishing = true; break;}
     280            if ($thisLabel == $pubLabel) { $publishing = true; break;}
    267281        }
    268282
    269283        // create link to label summary page for each label
    270         if ($stdsLabel == $selectedLabel) $link = "";
    271         else $link = "czartool_labels.php?pass=" . $pass . "&proj=" . $proj . "&label=" . $stdsLabel . "&stage=" . $selectedStage;
     284        if ($thisLabel == $selectedLabel) $link = "";
     285        else $link = "czartool_labels.php?pass=" . $pass . "&proj=" . $proj . "&label=" . $thisLabel . "&stage=" . $selectedStage;
    272286
    273287        echo "<tr><td></td>\n";
    274         write_table_cell($class, '%s', $link, $stdsLabel);
    275         write_table_cell($class, '%s', "", $distributing ? "yes" : "NO");
    276         write_table_cell($class, '%s', "", $publishing ? "yes" : "NO");
     288        $distPub = " ";
     289        if ($distributing) $distPub = "D";
     290        if ($publishing) $distPub = $distPub . "P";
     291        write_table_cell($class, '%s', "", $distPub);
     292
     293        write_table_cell($class, '%s', $link, $thisLabel);
    277294
    278295        $str = "";
    279296        $anyFaults = false;
    280297
    281         $link = "chipProcessedImfile_failure.php?pass=" . $pass . "&proj=" . $proj . "&chipRun.label=" . $stdsLabel . "&chipRun.state=new";
    282         getStateAndFaults($db, $stdsLabel, $selectedState, "chip", $str, $anyFaults);
     298        $link = $defaultlink;
     299        getStateAndFaults($db, $thisLabel, $selectedState, "burntool", $str, $anyFaults);
    283300        write_table_cell($class, '%s', $anyFaults ? $link : "", $str);
    284301
    285         $link = "camProcessedExp_failure.php?pass=" . $pass . "&proj=" . $proj . "&camRun.label=" . $stdsLabel . "&camRun.state=new";
    286         getStateAndFaults($db, $stdsLabel, $selectedState, "cam", $str, $anyFaults);
     302        $link = "failedChipProcessedImfile.php?pass=" . $pass . "&proj=" . $proj . "&chipRun.label=" . $thisLabel . "&chipRun.state=new";
     303        getStateAndFaults($db, $thisLabel, $selectedState, "chip", $str, $anyFaults);
     304        write_table_cell($class, '%s', $anyFaults ? $link : "", $str);
     305
     306        $link = "failedCamProcessedExp.php?pass=" . $pass . "&proj=" . $proj . "&camRun.label=" . $thisLabel . "&camRun.state=new";
     307        getStateAndFaults($db, $thisLabel, $selectedState, "cam", $str, $anyFaults);
    287308        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    288309
     310        $link =  "failedFakeProcessedImfile.php?pass=" . $pass . "&proj=" . $proj . "&fakeRun.label=" . $thisLabel . "&fakeRun.state=new";
     311        getStateAndFaults($db, $thisLabel, $selectedState, "fake", $str, $anyFaults);
     312        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
     313
     314        $link = "failedWarpSkyfiles.php?pass=" . $pass . "&proj=" . $proj . "&warpRun.label=" . $thisLabel . "&warpRun.state=new";
     315        getStateAndFaults($db, $thisLabel, $selectedState, "warp", $str, $anyFaults);
     316        write_table_cell($class, '%s',  $anyFaults ? $link : "",  $str);
     317
     318        $link = "failedStackSkyfile.php?pass=" . $pass . "&proj=" . $proj . "&stackRun.label=" . $thisLabel . "&stackRun.state=new";
     319        getStateAndFaults($db, $thisLabel, $selectedState, "stack", $str, $anyFaults);
     320        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
     321
     322        $link = "failedDiffSkyfile.php?pass=" . $pass . "&proj=" . $proj . "&diffRun.label=" . $thisLabel . "&diffRun.state=new";
     323        getStateAndFaults($db, $thisLabel, $selectedState, "diff", $str, $anyFaults);
     324        write_table_cell($class, '%s',  $anyFaults ? $link : "",  $str);
     325
    289326        $link = $defaultlink;
    290         getStateAndFaults($db, $stdsLabel, $selectedState, "fake", $str, $anyFaults);
     327        getStateAndFaults($db, $thisLabel, $selectedState, "magic", $str, $anyFaults);
    291328        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    292329
    293         $link = "warpFailedSkyfiles.php?pass=" . $pass . "&proj=" . $proj . "&warpRun.label=" . $stdsLabel . "&warpRun.state=new";
    294         getStateAndFaults($db, $stdsLabel, $selectedState, "warp", $str, $anyFaults);
    295         write_table_cell($class, '%s',  $anyFaults ? $link : "",  $str);
    296 
    297         $link = "stackFailedSkyfile.php?pass=" . $pass . "&proj=" . $proj . "&stackRun.label=" . $stdsLabel . "&stackRun.state=new";
    298         getStateAndFaults($db, $stdsLabel, $selectedState, "stack", $str, $anyFaults);
     330        $link = $defaultlink;
     331        getStateAndFaults($db, $thisLabel, $selectedState, "magicDS", $str, $anyFaults);
    299332        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    300333
    301         $link = "diffFailedSkyfile.php?pass=" . $pass . "&proj=" . $proj . "&diffRun.label=" . $stdsLabel . "&diffRun.state=new";
    302         getStateAndFaults($db, $stdsLabel, $selectedState, "diff", $str, $anyFaults);
    303         write_table_cell($class, '%s',  $anyFaults ? $link : "",  $str);
    304 
    305334        $link = $defaultlink;
    306         getStateAndFaults($db, $stdsLabel, $selectedState, "magic", $str, $anyFaults);
     335        getStateAndFaults($db, $thisLabel, $selectedState, "dist", $str, $anyFaults);
    307336        write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    308337
    309         $link = $defaultlink;
    310         getStateAndFaults($db, $stdsLabel, $selectedState, "magicDS", $str, $anyFaults);
    311         write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    312 
    313         $link = $defaultlink;
    314         getStateAndFaults($db, $stdsLabel, $selectedState, "dist", $str, $anyFaults);
    315         write_table_cell($class, '%s',  $anyFaults ? $link : "", $str);
    316 
    317338        echo "</tr>\n";
    318339    }
    319340
    320     if ($selectedLabel == "all_labels") $link = "";
    321     else  $link = "czartool_labels.php?pass=" . $pass . "&proj=" . $proj . "&label=all_labels&stage=".$selectedStage;
     341    if ($selectedLabel == "all_".$server."_labels") $link = "";
     342    else  $link = "czartool_labels.php?pass=" . $pass . "&proj=" . $proj . "&label=all_".$server."_labels&stage=".$selectedStage;
    322343
    323344    echo "<tr><td></td>\n";
    324     write_table_cell($class, '%s', $link, "All labels");
     345    write_table_cell($class, '%s', "", " ");
     346    write_table_cell($class, '%s', $link, "All $server labels");
    325347
    326348    echo "</tr>\n";
  • branches/sc_branches/trunkTest/ippMonitor/raw/ipp.imfiles.dat

    r28608 r29060  
    2525
    2626menutop   | menutop      | plain   | &nbsp;                       |
     27menutop   | menutop      | link    | New failed chip              | failedChipProcessedImfile.php
     28menutop   | menutop      | link    | New failed cam               | failedCamProcessedExp.php
     29menutop   | menutop      | link    | New failed fake              | failedFakeProcessedImfile.php
     30menutop   | menutop      | link    | New failed warp              | failedWarpSkyfiles.php
     31menutop   | menutop      | link    | New failed stack             | failedStackSkyfile.php
     32menutop   | menutop      | link    | New failed diff              | failedDiffSkyfile.php
     33menutop   | menutop      | link    | New failed magic tree        | failedMagicTrees.php
     34menutop   | menutop      | link    | New failed magic node        | failedMagicNodes.php
     35menutop   | menutop      | link    | New failed magic mask        | failedMagicMasks.php
     36
     37menutop   | menutop      | plain   | &nbsp;                       |
    2738menulink  | menuselect   | link    | Tables columns               | columns_in_db.php
    2839menulink  | menuselect   | link    | Clean /tmp directory         | cleanTmpDirectory.php
     40menutop   | menutop      | link    | disk usage                   | diskUsage.php
  • branches/sc_branches/trunkTest/ippMonitor/scripts/generate

    r28654 r29060  
    648648    foreach $word (@words) {
    649649        $fword = &parse_fieldname ($word);
    650         # print "field: $word -> $fword\n";
     650#        print "field: $word -> $fword\n";
    651651        push @outwords, $fword;
    652652        if ($word =~ m|^\$|) {
     
    660660    $opf{$name} = $outline;
    661661
    662     # print "ops: $name : $ops{$name}\n";
    663     # print "opf: $name : $opf{$name}\n";
     662#    print "ops: $name : $ops{$name}\n";
     663#    print "opf: $name : $opf{$name}\n";
    664664
    665665    return 1;
  • branches/sc_branches/trunkTest/ippScripts/scripts/addstar_run.pl

    r28298 r29060  
    147147           
    148148        }
     149        else {
     150
     151            $minidvodb_path = $dvodbReal;
     152        }
     153
    149154unless ($no_op) {
    150155            print $dvodbReal;
  • branches/sc_branches/trunkTest/ippScripts/scripts/automate_stacks.pl

    r28916 r29060  
    4646my ( $check_stacks, $queue_stacks, $check_sweetspot, $queue_sweetspot, $check_diffs, $queue_diffs, $clean_old);
    4747my ( $check_detrends, $queue_detrends, $check_dqstats, $queue_dqstats);
     48my ( $confirm_stacks, $check_confirm_stacks );
    4849
    4950GetOptions(
     
    7071    'check_stacks'         => \$check_stacks,
    7172    'queue_stacks'         => \$queue_stacks,
     73    'confirm_stacks'       => \$confirm_stacks,
     74    'check_confirm_stacks' => \$check_confirm_stacks,
    7275    'check_sweetspot'      => \$check_sweetspot,
    7376    'queue_sweetspot'      => \$queue_sweetspot,
     
    120123    defined $queue_chips or defined $queue_stacks or $queue_sweetspot or $queue_detrends or $queue_dqstats or
    121124    defined $check_chips or defined $check_stacks or $check_sweetspot or $check_detrends or $check_dqstats or
    122     defined $test_mode or defined $clean_old or defined $check_mode;
     125    defined $test_mode or defined $clean_old or defined $check_mode or
     126    defined $confirm_stacks;
    123127
    124128# Configurable parameters from our config file.
     
    132136my %stackable_list = ();
    133137my %reduction_class = ();
     138my %macro_formats = ();
    134139my @unrecoverable_quality = ();
    135140my @detrend_list = ();
     
    181186    elsif (${ $entry }{name} eq 'UNRECOVERABLE_QUALITY') {
    182187        push @unrecoverable_quality, ${ $entry }{value};
     188    }
     189    elsif (${ $entry }{name} eq 'MACRO_DEFINITIONS') {
     190        my @macro_data = @{ ${ $entry }{value} };
     191        my $this_mode = '';
     192        foreach my $mentry (@macro_data) {
     193            if (${ $mentry }{name} eq 'PROC_MODE') {
     194                $this_mode = ${ $mentry }{value};
     195            }
     196            elsif (${ $mentry }{name} eq 'MACRO') {
     197                $macro_formats{$this_mode} = ${ $mentry }{value};
     198            }
     199        }
    183200    }
    184201    elsif (${ $entry }{name} eq 'TARGETS') {
     
    306323        print "CLEAN: $z $clean_commands{$z} $clean_retention{$z}\n";
    307324    }
     325    foreach $z (keys (%macro_formats)) {
     326        print "MACROS: $z $macro_formats{$z}\n";
     327    }
    308328   
    309329}
     
    385405        $metadata_out{nsState} = 'STACKING_POSSIBLE_ERROR';
    386406    }
     407    return_metadata($date);
     408    exit(0);
     409}
     410
     411if (defined($check_confirm_stacks) || defined($test_mode) || defined($check_mode)) {
     412    $metadata_out{nsState} = 'CONFIRM_STACKING';
     413    &confirm_stacks($date,"pretend");
     414    return_metadata($date);
     415    unless (defined($test_mode) || defined($check_mode)) { exit(0); }
     416}
     417if (defined($confirm_stacks)) {
     418    $metadata_out{nsState} = 'CONFIRM_STACKING';
     419    &confirm_stacks($date);
    387420    return_metadata($date);
    388421    exit(0);
     
    582615    $metadata_out{btN} = $N - 1;
    583616    $metadata_out{btNCounter} = 0;
    584 
     617    add_to_macro_list('define_burntool',1,$date);
    585618    return(0);
    586619}
     
    589622# Chips
    590623################################################################################
     624
    591625
    592626sub construct_chiptool_cmd {
     
    680714    }
    681715    $metadata_out{nsState} = 'TOWARP';
     716
    682717    return(0);
    683718}
     
    696731        if ($Nexposures == 0) {
    697732            print STDERR "execute_chips: Target $target on $date had no exposures.\n";
    698             next;
     733#           next;
    699734        }
    700735        if ($Nalready != 0) {
    701736            print STDERR "execute_chips: Not queueing $target on $date due to already existing exposures.\n";
    702             next;
     737#            next;
    703738        }
    704739        if ($Nimfiles != $Nburntooled) {
     
    712747            chip_queue($date,$target);
    713748        }
     749        if (defined($pretend)) {
     750            add_to_macro_list('check_chips',$stackable_list{$target},$date,$target);
     751        }
     752        else {
     753            add_to_macro_list('queue_chips',$stackable_list{$target},$date,$target);
     754        }
     755                   
    714756    }
    715757    if ($exposures == 0) {
     
    717759    }
    718760}
     761
    719762
    720763#
     
    742785    my $date = shift;
    743786   
    744 #     my $db = init_gpc_db();
    745 #     my $trunc_date = $date; $trunc_date =~ s/-//g;
    746 
    747 #     my $where = " label LIKE '%.nightlyscience' AND data_group' ";
    748 #     my $chip_sth = "SELECT * from chipRun WHERE (state = 'full' OR state = 'new' OR state = 'cleaned') AND $where ";
    749 #     my $cam_sth = "SELECT * from camRun WHERE state = 'full' AND $where ";
    750 
    751 #     my $chip_ref = $db->selectall_arrayref( $chip_sth );
    752 #     my $cam_ref = $db->selectall_arrayref( $cam_sth );
    753    
    754787    my $command = construct_dqstats_cmd($date) . ' -pretend ';
    755788    my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     
    762795    my @input_exposures = split /\n/, (join '', @$stdout_buf);
    763796
    764     return($#input_exposures + 1,1,1); # $#{ $chip_ref } + 1, $#{ $cam_ref } + 1);
     797    return($#input_exposures + 1,1,1);
    765798}
    766799 
     
    791824        unless(defined($pretend)) {
    792825            dqstats_queue($date);
     826        }
     827        if (defined($pretend)) {
     828            add_to_macro_list('check_dqstats',1,$date);
     829        }
     830        else {
     831            add_to_macro_list('queue_dqstats',1,$date);
    793832        }
    794833    }
     
    887926            detrend_queue($date,$target);
    888927        }
     928        if (defined($pretend)) {
     929            add_to_macro_list('check_detrends',1,$date,$target,"dettool");
     930        }
     931        else {
     932            add_to_macro_list('queue_detrends',1,$date,$target,"dettool");
     933        }
    889934    }
    890935    if ($exposures == 0) {
     
    10231068        $metadata_out{nsState} = 'SS_DONE';
    10241069        sweetspot_queue($date);
     1070    }
     1071    if (defined($pretend)) {
     1072        add_to_macro_list('check_sweetspot',1,$date);
     1073    }
     1074    else {
     1075        add_to_macro_list('queue_sweetspot',1,$date);
    10251076    }
    10261077}
     
    11731224                    stack_queue($date,$target,$filter);
    11741225                }
     1226                if (defined($pretend)) {
     1227                    add_to_macro_list('check_stacks',$stackable_list{$target},$date,$target,$filter);
     1228                }
     1229                else {
     1230                    add_to_macro_list('queue_stacks',$stackable_list{$target},$date,$target,$filter);
     1231                }
    11751232            }
    11761233        }
     
    11811238
    11821239}
     1240
     1241sub post_stack_queue {
     1242    my $date = shift;
     1243    my $target = shift;
     1244
     1245    my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction) = get_tool_parameters($date,$target);
     1246    # check warp stage == chip stage
     1247    my $db = init_gpc_db();
     1248
     1249    my $trunc_date = $date; $trunc_date =~ s/-//g;
     1250
     1251    my $where = " label = '$label' AND data_group = '$data_group' ";
     1252
     1253    my $stack_full_sth = "SELECT * from stackRun where $where ";
     1254    my $stack_done_sth = "SELECT * from stackRun LEFT JOIN stackSumSkyfile USING(stack_id) WHERE $where ";
     1255    $stack_done_sth .= " AND ((state = 'full') OR (state = 'new' && fault != 0)) ";
     1256
     1257    my $stack_full_ref = $db->selectall_arrayref( $stack_full_sth );
     1258    my $stack_done_ref = $db->selectall_arrayref( $stack_done_sth );
     1259
     1260    return($#{ $stack_full_ref } + 1, $#{ $stack_done_ref } + 1);
     1261}
     1262
     1263sub confirm_stacks {
     1264    my $date = shift;
     1265    my $pretend = shift;
     1266   
     1267    foreach my $target (@target_list) {
     1268        if ($stackable_list{$target} == 1) {
     1269            my ($Nstacks, $Nattempted) = post_stack_queue($date,$target);
     1270            if ($Nstacks != $Nattempted) {
     1271                if ($debug == 1) {
     1272                    print STDERR "confirm_stacks: Target $target on $date is not done stacking. $Nstacks $Nattempted\n"
     1273                }
     1274                if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
     1275                    $metadata_out{nsState} = 'STACKING';
     1276                }
     1277                   
     1278                next;
     1279            }
     1280            if ($metadata_out{nsState} eq 'CONFIRM_STACKING') {
     1281                if (defined($pretend)) {
     1282                    add_to_macro_list('check_confirm_stacks',$stackable_list{$target},$date,$target);
     1283                }
     1284                else {
     1285                    add_to_macro_list('confirm_stacks',$stackable_list{$target},$date,$target);
     1286                }
     1287            }
     1288        }
     1289    }
     1290}           
    11831291
    11841292#
     
    12331341
    12341342    foreach my $mode (@mode_list) {
    1235 #       if ((exists($clean_alternate{$mode})) && ($clean_alternate{$mode} eq 'A')) {
    1236 #           my ($cleaning_date,$command) = construct_cleantool_args($date,"",$mode);
    1237 #           if ($cleaning_date eq 'no clean') {
    1238 #               next;
    1239 #           }
    1240 #           print STDERR "$command\n";
    1241 #           if (!(defined($pretend) || $debug == 1)) {
    1242 # #           print STDERR "BEAR IS DRIVING!?\n";
    1243 #               my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1244 #                   run ( command => $command, verbose => $verbose );
    1245 #               unless ($success) {
    1246 #                   $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1247 #                   &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1248 #               }
    1249 #           }
    1250 #       }
    1251 #       else {
    1252             foreach my $target (@target_list) {
    1253                 if (exists($noclean_list{$target})) {
    1254                     next;
     1343        foreach my $target (@target_list) {
     1344            if (exists($noclean_list{$target})) {
     1345                next;
     1346            }
     1347            my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
     1348            if ($cleaning_date eq 'no clean') {
     1349                next;
     1350            }           
     1351            print STDERR "$command\n";
     1352            if (!(defined($pretend) || $debug == 1)) {
     1353#           print STDERR "BEAR IS DRIVING!?\n";
     1354                my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
     1355                    run ( command => $command, verbose => $verbose );
     1356                unless ($success) {
     1357                    $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
     1358                    &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    12551359                }
    1256                 my ($cleaning_date,$command) = construct_cleantool_args($date,$target,$mode);
    1257                 if ($cleaning_date eq 'no clean') {
    1258                     next;
    1259                 }               
    1260                 print STDERR "$command\n";
    1261                 if (!(defined($pretend) || $debug == 1)) {
    1262 #           print STDERR "BEAR IS DRIVING!?\n";
    1263                     my ( $success, $error_code, $full_buf, $stdout_buf, $stderr_buf ) =
    1264                         run ( command => $command, verbose => $verbose );
    1265                     unless ($success) {
    1266                         $error_code = (($error_code >> 8) or $PS_EXIT_PROG_ERROR);
    1267                         &my_die("Unable to perform cleantool ($command): $error_code", 0,0,$date, $PS_EXIT_SYS_ERROR);
    1268                     }
    1269                 }
    1270             }
    1271 #       }
     1360                add_to_macro_list('clean_old',1,$date,$target,$mode);
     1361            }
     1362        }
    12721363    }
    12731364    return(0);
    12741365}
    1275 
    1276 
    1277 
    12781366
    12791367#
     
    13241412    my $workdir = 'neb://@HOST@.0/' . $dbname . "/detverify.nt/${workdir_date}/${lc_type}${internal_filter}";
    13251413    return($label,$workdir,$det_filter,$exp_type,$det_type,$ref_det_id,$ref_iter,$maxN);
     1414}
     1415
     1416sub add_to_macro_list {
     1417    my $proc_mode = shift;
     1418    my $do_or_do_not = shift;
     1419    my $date = shift;
     1420    my $target = shift;
     1421    my $mode = shift;
     1422
     1423    if ((defined($macro_formats{$proc_mode}))&&($do_or_do_not)) {
     1424        unless (defined($metadata_out{N_MACROS})) {
     1425            $metadata_out{N_MACROS} = 0;
     1426        }
     1427        my $N = $metadata_out{N_MACROS};
     1428        $metadata_out{"ns${N}Macro"} = $macro_formats{$proc_mode};
     1429        print STDERR "WORKING ON A MACRO: ns${N}Macro $proc_mode $macro_formats{$proc_mode}\n";
     1430        if (defined($date)&&(defined($target))) {
     1431
     1432            my ($label,$workdir,$obs_mode,$object,$comment,$tess_id,$dist_group,$data_group,$reduction)
     1433                = get_tool_parameters($date,$target);
     1434            $metadata_out{"ns${N}Macro"} =~ s/\@LABEL\@/$label/;
     1435            $metadata_out{"ns${N}Macro"} =~ s/\@WORKDIR\@/$workdir/;
     1436            $metadata_out{"ns${N}Macro"} =~ s/\@OBS_MODE\@/$obs_mode/;
     1437            $metadata_out{"ns${N}Macro"} =~ s/\@OBJECT\@/$object/;
     1438            $metadata_out{"ns${N}Macro"} =~ s/\@COMMENT\@/$comment/;
     1439            $metadata_out{"ns${N}Macro"} =~ s/\@TESS_ID\@/$tess_id/;
     1440            $metadata_out{"ns${N}Macro"} =~ s/\@DIST_GROUP\@/$dist_group/;
     1441            $metadata_out{"ns${N}Macro"} =~ s/\@DATA_GROUP\@/$data_group/;
     1442            $metadata_out{"ns${N}Macro"} =~ s/\@REDUCTION\@/$reduction/;
     1443        }
     1444        if (defined($mode)) {
     1445            $metadata_out{"ns${N}Macro"} =~ s/\@EXTRA\@/$mode/;
     1446        }
     1447        if (defined($date)) {
     1448            $metadata_out{"ns${N}Macro"} =~ s/\@DATE\@/$date/;
     1449        }
     1450        $metadata_out{N_MACROS} ++;
     1451    }
    13261452}
    13271453
  • branches/sc_branches/trunkTest/ippScripts/scripts/dist_advancerun.pl

    r28735 r29060  
    6767my $stacktool   = can_run('stacktool') or (warn "Can't find stacktool" and $missing_tools = 1);
    6868my $bgtool = can_run('bgtool') or (warn "Can't find bgtool" and $missing_tools = 1);
     69my $staticskytool = can_run('staticskytool') or (warn "Can't find staticskytool" and $missing_tools = 1);
    6970if ($missing_tools) {
    7071    &my_die("Can't find required tools.", $dist_id, $PS_EXIT_CONFIG_ERROR);
     
    108109    $list_mode = "-sumskyfile";
    109110    $component_key = "skycell_id";
     111} elsif ($stage eq "sky") {
     112    $tool_cmd = "$staticskytool -sky_id";
     113    $list_mode = "-result";
     114    $component_key = "";
    110115} elsif ($stage eq "diff") {
    111116    $tool_cmd = "$difftool -diff_id";
  • branches/sc_branches/trunkTest/ippScripts/scripts/dist_bundle.pl

    r28942 r29060  
    5353                      'PPSTACK.OUTPUT.MASK' => 'mask',
    5454                      'PPSTACK.OUTPUT.VARIANCE' => 'variance' );
     55my %sky_cleaned   = ( 'PSPHOT.STACK.OUTPUT.IMAGE' => 'image',
     56                      'PSPHOT.STACK.OUTPUT.MASK' => 'mask',
     57                      'PSPHOT.STACK.OUTPUT.VARIANCE' => 'variance' );
    5558
    5659
     
    420423    } elsif ($stage eq "stack") {
    421424        $type = $stack_cleaned{$rule};
     425    } elsif ($stage eq "sky") {
     426        $type = $sky_cleaned{$rule};
    422427    } else {
    423428        &my_die("$stage is not a valid stage", $component, $PS_EXIT_CONFIG_ERROR);
     
    500505    } elsif ($stage eq "stack") {
    501506        $config_file_rule = "PPSTACK.CONFIG";
     507    } elsif ($stage eq "sky") {
     508        $config_file_rule = "PSPHOT.STACK.CONFIG";
    502509    } else {
    503510        &my_die("$stage is not a valid stage", $component, $PS_EXIT_CONFIG_ERROR);
  • branches/sc_branches/trunkTest/ippScripts/scripts/dist_make_fileset.pl

    r28536 r29060  
    223223    my $dbname = shift;
    224224
    225     if (($stage eq 'stack') or ($stage eq 'diff') or ($stage eq 'SSdiff')) {
     225    if (($stage eq 'stack') or ($stage eq 'diff') or ($stage eq 'SSdiff') or $stage eq 'sky') {
    226226        return "";
    227227    }
  • branches/sc_branches/trunkTest/ippScripts/scripts/staticsky.pl

    r28154 r29060  
    9999my $nInputs = @$files;
    100100
     101my $configuration = $ipprc->filename("PSPHOT.STACK.CONFIG", $outroot);
     102
    101103foreach my $file (@$files) {
    102104    print $listFile "INPUT   METADATA\n";
     
    161163    $command .= " -threads $threads" if defined $threads;
    162164    $command .= " -recipe PSPHOT $recipe_psphot";
    163     # $command .= " -dumpconfig $configuration";
     165    $command .= " -dumpconfig $configuration";
    164166    # $command .= " -tracedest $traceDest -log $logDest";
    165167    # $command .= " -dbname $dbname" if defined $dbname;
  • branches/sc_branches/trunkTest/ippTasks/nightly_stacks.pro

    r28887 r29060  
    8888    active true
    8989  end
     90  task ns.stacks.confirm
     91    active true
     92  end
    9093end
    9194
     
    155158$ns_stackPAGE = 0
    156159$ns_RstackPAGE = 0
     160$ns_CstackPAGE = 0
    157161
    158162book init nsData
     
    431435    end
    432436
     437    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     438    for i 0 $ns_Nmacros
     439        sprintf macroName "ns%dMacro" $i
     440        book getword nsData $options:0 $macroName -var macroCmd
     441        $macroCmd
     442    end
     443
    433444    if ($VERBOSE > 2)
    434445        book listbook nsData
     
    486497    ipptool2book stdout nsData -uniq -key date
    487498
     499    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     500    for i 0 $ns_Nmacros
     501        sprintf macroName "ns%dMacro" $i
     502        book getword nsData $options:0 $macroName -var macroCmd
     503        $macroCmd
     504    end
     505
    488506    if ($VERBOSE > 2)
    489507        book listbook nsData
     
    541559    ipptool2book stdout nsData -uniq -key date
    542560
     561    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     562    for i 0 $ns_Nmacros
     563        sprintf macroName "ns%dMacro" $i
     564        book getword nsData $options:0 $macroName -var macroCmd
     565        $macroCmd
     566    end
     567
    543568    if ($VERBOSE > 2)
    544569        book listbook nsData
     
    562587# Check to see if the warps are finished and so we can make stacks.
    563588#
     589
    564590task              ns.stacks.run
    565591  host            local
     
    595621# We've queued up stacking. We're done with this date, so don't reload the page.
    596622    ipptool2book stdout nsData -uniq -key date
     623
     624    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     625    for i 0 $ns_Nmacros
     626        sprintf macroName "ns%dMacro" $i
     627        book getword nsData $options:0 $macroName -var macroCmd
     628        $macroCmd
     629    end
     630
     631    if ($VERBOSE > 2)
     632        book listbook nsData
     633    end
     634  end
     635
     636  # locked list
     637  task.exit    default
     638    showcommand failure
     639  end
     640  task.exit    crash
     641    showcommand crash
     642  end
     643  #operation times out?
     644  task.exit    timeout
     645    showcommand timeout
     646  end
     647end
     648
     649#
     650# Confirm that all the stacks that can be built have been built, or at least attempted.
     651#
     652task              ns.stacks.confirm
     653  host            local
     654  periods         -poll $LOADPOLL
     655  periods         -exec $LOADEXEC
     656  periods         -timeout 120
     657  npending        1
     658
     659  task.exec
     660     stdout NULL
     661     stderr $LOGDIR/ns.stacks.log
     662
     663     book getpage nsData $ns_CstackPAGE -var date
     664     book getword nsData $date nsState -var ns_STATE
     665     book npages nsData -var Npages
     666
     667     if ($VERBOSE > 5)
     668        echo "ns.stacks.confirm: " $ns_CstackPAGE $date $ns_STATE $Npages
     669     end
     670
     671     $ns_CstackPAGE ++
     672     if ($ns_CstackPAGE >= $Npages) set ns_CstackPAGE = 0
     673     option $date
     674
     675     if ("$ns_STATE" != "STACKING") break
     676     $run = automate_stacks.pl --confirm_stacks --date $date
     677     command $run
     678   end
     679  # success
     680  task.exit   0
     681#    convert 'stdout' to book format
     682    book delpage nsData $options:0
     683    ipptool2book stdout nsData -uniq -key date
     684
     685    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     686    for i 0 $ns_Nmacros
     687        sprintf macroName "ns%dMacro" $i
     688        book getword nsData $options:0 $macroName -var macroCmd
     689        $macroCmd
     690    end
     691
    597692    if ($VERBOSE > 2)
    598693        book listbook nsData
     
    650745    ipptool2book stdout nsBurntool -uniq -key date
    651746    book setword nsData $options:0 nsState "QUEUEBURNING"
     747
     748    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     749    for i 0 $ns_Nmacros
     750        sprintf macroName "ns%dMacro" $i
     751        book getword nsData $options:0 $macroName -var macroCmd
     752        $macroCmd
     753    end
    652754
    653755    if ($VERBOSE > 2)
     
    766868    # Set the job state for success.
    767869    book setword nsBurntool $options:0 $options:2 FINISHED
     870
     871    book getword nsData $options:0 nsNmacros -var ns_Nmacros
     872    for i 0 $ns_Nmacros
     873        sprintf macroName "ns%dMacro" $i
     874        book getword nsData $options:0 $macroName -var macroCmd
     875        $macroCmd
     876    end
     877
    768878    if ($VERBOSE > 2)
    769879        book listbook nsData
  • branches/sc_branches/trunkTest/ippToPsps/perl/checkOdmStatus.pl

    r28954 r29060  
    3232}
    3333if (!defined $singleBatch) {
    34     print "* OPTIONAL: a single batch                  -b                   (default = none)\n";
     34    print "* OPTIONAL: a single batch                  -b <batchNum>        (default = none)\n";
    3535}
    3636if (!defined $fromTime) {
    3737    $fromTime = "2010-01-01";
    38     print "* OPTIONAL: from time                       -f                   (default = $fromTime)\n";
     38    print "* OPTIONAL: from time                       -f <dateTime>        (default = $fromTime)\n";
    3939}
    4040if (!defined $toTime) {
    4141    $toTime = "2099-12-31";
    42     print "* OPTIONAL: to time                         -t                   (default = $toTime)\n";
     42    print "* OPTIONAL: to time                         -t <dateTime>        (default = $toTime)\n";
    4343}
    4444if (!defined $verbose) {
     
    110110
    111111    my $response = $ua->post($odmUrl,
    112             [batchNameFilter => $batchFilter,
     112            [batchNameFilter => "%".$batchFilter,
    113113            statusFilter => $statusFilter,
    114114            fromFilter => "2010-01-01",
  • branches/sc_branches/trunkTest/ippToPsps/src/ippToPspsBatchDetection.c

    r28701 r29060  
    326326            fits_write_col(this->fitsOut, TLONG, IMAGEMETA_NDETECT, 1, 1, 1, &numDetectionsOut, &status);
    327327
    328             // detections
    329             ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "Detection", false);
    330             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_OBJID, 1, 1, nDet, objID, &status);
    331             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_DETECTID, 1, 1, nDet, detectID, &status);
    332             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IPPOBJID, 1, 1, nDet, ippObjID, &status);
    333             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IPPDETECTID, 1, 1, nDet, ippDetectID, &status);
    334             fits_write_col(this->fitsOut, TBYTE, DETECTION_FILTERID, 1, 1, nDet, filterIDs, &status);
    335             fits_write_col(this->fitsOut, TBYTE, DETECTION_SURVEYID, 1, 1, nDet, surveyIDs, &status);
    336             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IMAGEID, 1, 1, nDet, imageID, &status);
    337             fits_write_col(this->fitsOut, TDOUBLE, DETECTION_OBSTIME, 1, 1, nDet, obsTimes, &status);
    338             fits_write_col(this->fitsOut, TFLOAT, DETECTION_INSTFLUX, 1, 1, nDet, instFlux, &status);
    339             fits_write_col(this->fitsOut, TFLOAT, DETECTION_INSTFLUXERR, 1, 1, nDet, instFluxErr, &status);
    340             fits_write_col(this->fitsOut, TFLOAT, DETECTION_PEAKADU, 1, 1, nDet, peakFlux, &status);
    341             fits_write_col(this->fitsOut, TSTRING, DETECTION_ASSOCDATE, 1, 1, nDet, assocDate, &status);
    342             fits_write_col(this->fitsOut, TLONGLONG, DETECTION_INFOFLAG, 1, 1, nDet, flags, &status);
    343             if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
    344 
    345             // skinny object
    346             ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "SkinnyObject", false);
    347             fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_OBJID, 1, 1, nDet, objID, &status);
    348             fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_IPPOBJID, 1, 1, nDet, ippObjID, &status);
    349             fits_write_col(this->fitsOut, TBYTE, SKINNYOBJECT_SURVEYID, 1, 1, nDet, surveyIDs, &status);
    350             if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
    351 
    352             // object calibration color
    353             ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "ObjectCalColor", false);
    354             fits_write_col(this->fitsOut, TLONGLONG, OBJECTCALCOLOR_OBJID, 1, 1, nDet, objID, &status);
    355             fits_write_col(this->fitsOut, TLONGLONG, OBJECTCALCOLOR_IPPOBJID, 1, 1, nDet, ippObjID, &status);
    356             fits_write_col(this->fitsOut, TBYTE, OBJECTCALCOLOR_FILTERID, 1, 1, nDet, filterIDs, &status);
    357             if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
     328            // avoid writing out an empty table of detections
     329            if (numDetectionsOut > 0) {
     330
     331                // detections
     332                ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "Detection", false);
     333                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_OBJID, 1, 1, nDet, objID, &status);
     334                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_DETECTID, 1, 1, nDet, detectID, &status);
     335                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IPPOBJID, 1, 1, nDet, ippObjID, &status);
     336                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IPPDETECTID, 1, 1, nDet, ippDetectID, &status);
     337                fits_write_col(this->fitsOut, TBYTE, DETECTION_FILTERID, 1, 1, nDet, filterIDs, &status);
     338                fits_write_col(this->fitsOut, TBYTE, DETECTION_SURVEYID, 1, 1, nDet, surveyIDs, &status);
     339                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_IMAGEID, 1, 1, nDet, imageID, &status);
     340                fits_write_col(this->fitsOut, TDOUBLE, DETECTION_OBSTIME, 1, 1, nDet, obsTimes, &status);
     341                fits_write_col(this->fitsOut, TFLOAT, DETECTION_INSTFLUX, 1, 1, nDet, instFlux, &status);
     342                fits_write_col(this->fitsOut, TFLOAT, DETECTION_INSTFLUXERR, 1, 1, nDet, instFluxErr, &status);
     343                fits_write_col(this->fitsOut, TFLOAT, DETECTION_PEAKADU, 1, 1, nDet, peakFlux, &status);
     344                fits_write_col(this->fitsOut, TSTRING, DETECTION_ASSOCDATE, 1, 1, nDet, assocDate, &status);
     345                fits_write_col(this->fitsOut, TLONGLONG, DETECTION_INFOFLAG, 1, 1, nDet, flags, &status);
     346                if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
     347
     348                // skinny object
     349                ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "SkinnyObject", false);
     350                fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_OBJID, 1, 1, nDet, objID, &status);
     351                fits_write_col(this->fitsOut, TLONGLONG, SKINNYOBJECT_IPPOBJID, 1, 1, nDet, ippObjID, &status);
     352                fits_write_col(this->fitsOut, TBYTE, SKINNYOBJECT_SURVEYID, 1, 1, nDet, surveyIDs, &status);
     353                if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
     354
     355                // object calibration color
     356                ippToPspsConfig_writeTable(this->config, fitsIn, this->fitsOut, nDet, "ObjectCalColor", false);
     357                fits_write_col(this->fitsOut, TLONGLONG, OBJECTCALCOLOR_OBJID, 1, 1, nDet, objID, &status);
     358                fits_write_col(this->fitsOut, TLONGLONG, OBJECTCALCOLOR_IPPOBJID, 1, 1, nDet, ippObjID, &status);
     359                fits_write_col(this->fitsOut, TBYTE, OBJECTCALCOLOR_FILTERID, 1, 1, nDet, filterIDs, &status);
     360                if (numOfDuplicates||numInvalidFlux) fits_delete_rowlist(this->fitsOut, removeList, numOfDuplicates+numInvalidFlux, &status);
     361            }
    358362
    359363            psLogMsg("ippToPsps", PS_LOG_INFO,
  • branches/sc_branches/trunkTest/ippTools/share/Makefile.am

    r28941 r29060  
    153153        disttool_definebyquery_fake.sql \
    154154        disttool_definebyquery_raw.sql \
     155        disttool_definebyquery_sky.sql \
    155156        disttool_definebyquery_stack.sql \
    156157        disttool_definebyquery_warp.sql \
     
    166167        disttool_pending_fake.sql \
    167168        disttool_pending_raw.sql \
     169        disttool_pending_sky.sql \
    168170        disttool_pending_stack.sql \
    169171        disttool_pending_warp.sql \
     
    328330        staticskytool_definebyquery_select.sql \
    329331        staticskytool_definebyquery_inputs.sql \
     332        staticskytool_export_run.sql \
     333        staticskytool_export_input.sql \
     334        staticskytool_export_result.sql \
    330335        staticskytool_inputs.sql \
    331336        staticskytool_todo.sql \
  • branches/sc_branches/trunkTest/ippTools/share/disttool_toadvance.sql

    r28733 r29060  
    240240        AND distComponent.fault = 0
    241241UNION
     242-- staticsky stage
     243-- NOTE this assumes that there is only one component per staticskyRun
     244-- (one skycell)
     245SELECT
     246    distRun.dist_id,
     247    stage,
     248    stage_id,
     249    outroot,
     250    label,
     251    clean
     252    FROM distRun
     253    JOIN staticskyResult on stage_id = sky_id
     254    LEFT JOIN distComponent
     255        ON distRun.dist_id = distComponent.dist_id
     256    WHERE
     257        distRun.state = 'new'
     258        AND distRun.fault = 0
     259        AND distRun.stage = 'sky'
     260        AND distComponent.component IS NOT NULL
     261        AND distComponent.fault = 0
     262UNION
    242263-- SSdiff stage
    243264SELECT
  • branches/sc_branches/trunkTest/ippTools/share/staticskytool_result.sql

    r28091 r29060  
    1 SELECT
     1SELECT DISTINCT
    22    staticskyResult.*,
    33    staticskyRun.state,
    4     staticskyRun.tess_id,
    5     staticskyRun.skycell_id,
    6     staticskyRun.filter,
    74    staticskyRun.workdir,
    85    staticskyRun.label,
     6    stackRun.tess_id,
     7    stackRun.skycell_id,
     8    stackRun.filter
    99FROM staticskyRun
    10 JOIN staticskyResult
    11 USING(stack_id)
     10JOIN staticskyResult USING(sky_id)
     11JOIN staticskyInput USING(sky_id)
     12JOIN stackRun using(stack_id)
  • branches/sc_branches/trunkTest/ippTools/src/disttool.c

    r28938 r29060  
    329329        // stack stage doesn't require magic
    330330        no_magic = true;
     331    } else if (!strcmp(stage, "sky")) {
     332        magicRunType = "staticskyRun";
     333        query = pxDataGet("disttool_definebyquery_sky.sql");
     334        if (!query) {
     335            psError(PXTOOLS_ERR_SYS, false, "failed to retreive SQL statement");
     336            psFree(where);
     337            return false;
     338        }
     339
     340        if (label) {
     341            psStringAppend(&query, " AND (staticskyRun.label = '%s')", label);
     342        }
     343        if (dist_group) {
     344            psStringAppend(&query, " AND (sticskyRun.dist_group = '%s')", dist_group);
     345        }
     346        // (static)sky stage doesn't require magic
     347        no_magic = true;
    331348    } else if (!strcmp(stage, "SSdiff")) {
    332349      magicRunType = "diffRun";
  • branches/sc_branches/trunkTest/ippTools/src/staticskytool.c

    r28154 r29060  
    727727bool exportrunMode(pxConfig *config)
    728728{
    729 # if (0)
    730729  typedef struct ExportTable {
    731730    char tableName[80];
     
    737736  PS_ASSERT_PTR_NON_NULL(config, NULL);
    738737
    739   PXOPT_LOOKUP_S64(det_id, config->args, "-stack_id", true,  false);
     738  PXOPT_LOOKUP_S64(det_id, config->args, "-sky_id", true,  false);
    740739  PXOPT_LOOKUP_STR(outfile, config->args, "-outfile", true,  false);
    741740  PXOPT_LOOKUP_BOOL(clean, config->args, "-clean", false);
     
    753752  }
    754753  psMetadata *where = psMetadataAlloc();
    755   PXOPT_COPY_S64(config->args, where, "-stack_id", "stack_id", "==");
     754  PXOPT_COPY_S64(config->args, where, "-sky_id", "sky_id", "==");
    756755
    757756  ExportTable tables [] = {
    758     {"stackRun", "staticskytool_export_run.sql"},
    759     {"stackInputSkyfile", "staticskytool_export_input_skyfile.sql"},
    760     {"stackSumSkyfile", "staticskytool_export_sum_skyfile.sql"},
     757    {"staticskyRun", "staticskytool_export_run.sql"},
     758    {"staticskyInput", "staticskytool_export_input.sql"},
     759    {"staticskyResult", "staticskytool_export_result.sql"},
    761760  };
    762761
     
    800799
    801800    if (clean) {
    802         if (!strcmp(tables[i].tableName, "stackRun")) {
    803             if (!pxSetStateCleaned("stackRun", "state", output)) {
     801        if (!strcmp(tables[i].tableName, "staticskyRun")) {
     802            if (!pxSetStateCleaned("staticskyRun", "state", output)) {
    804803                psFree(output);
    805804                psError(PS_ERR_UNKNOWN, false, "pxSetStateClean failed for table %s",  tables[i].tableName);
     
    821820    fclose (f);
    822821
    823 # endif
    824822    return true;
    825823}
  • branches/sc_branches/trunkTest/ippconfig/gpc1/ppMerge.config

    r28596 r29060  
    6060# END
    6161
     62# DARK.ORDINATES        METADATA
     63#       TEMP    METADATA
     64#               ORDER   S32     1
     65#               RULE    STR     CHIP.TEMP
     66#       END
     67#       TEMP2   METADATA
     68#               ORDER   S32     1
     69#               RULE    STR     CHIP.TEMP * CHIP.TEMP
     70#       END
     71# END
     72
    6273# Ordinates for fitting dark current as function of darktime and fpa temp:
    6374# Counts = C0 + (CT0 + CT1*Temp)*exptime
  • branches/sc_branches/trunkTest/ippconfig/recipes

    • Property svn:mergeinfo deleted
  • branches/sc_branches/trunkTest/ippconfig/recipes/filerules-split.mdc

    r28619 r29060  
    209209PSPHOT.STACK.OUTPUT.VARIANCE OUTPUT {OUTPUT}.{FILE.ID}.wt.fits        VARIANCE        NONE       FPA        TRUE      NONE
    210210PSPHOT.STACK.OUTPUT          OUTPUT {OUTPUT}.{FILE.ID}.cmf            CMF             NONE       FPA        TRUE      NONE
     211PSPHOT.STACK.CONFIG          OUTPUT {OUTPUT}.psphotStack.mdc          TEXT            NONE       FPA        TRUE      NONE
    211212                                                     
    212213SOURCE.PLOT.RAW.MOMENTS      OUTPUT {OUTPUT}.{CHIP.NAME}.mnt.png      KAPA            NONE       CHIP       TRUE      NONE
  • branches/sc_branches/trunkTest/ippconfig/recipes/fitstypes.mdc

    r27417 r29060  
    107107        BSCALE          F32     0.1     # Disk is stored as 1/10th sec
    108108        BZERO           F32     3276.8  # Store unsigned zero as zero
     109        # switch to NONE to work around ticket 1411
     110        #COMPRESSION    STR     NONE
    109111        COMPRESSION     STR     GZIP
    110112        TILE.X          S32     0
  • branches/sc_branches/trunkTest/ippconfig/recipes/nightly_science.config

    r28914 r29060  
    3737FILTERS STR z.00000
    3838FILTERS STR y.00000
     39
     40UNRECOVERABLE_QUALITY MULTI
     41UNRECOVERABLE_QUALITY S16 4007
     42
     43MACRO_DEFINITIONS MULTI
     44#MACRO_DEFINITIONS METADATA
     45#  PROC_MODE STR check_chips
     46#  MACRO     STR echo survey.del.destreak @LABEL@
     47#END
     48MACRO_DEFINITIONS METADATA
     49  PROC_MODE STR queue_chips
     50  MACRO     STR survey.del.destreak @LABEL@
     51END
     52MACRO_DEFINITIONS METADATA
     53  PROC_MODE STR confirm_stacks
     54  MACRO     STR survey.add.destreak @LABEL@ /data/ipp053.0/gpc1_destreak
     55END
     56#MACRO_DEFINITIONS METADATA
     57#  PROC_MODE STR check_confirm_stacks
     58#  MACRO     STR echo survey.add.destreak @LABEL@ /data/ipp053.0/gpc1_destreak
     59#END
     60# MACRO_DEFINITIONS METADATA
     61#   PROC_MODE STR clean_old
     62#   MACRO     STR
     63# END
     64# MACRO_DEFINITIONS METADATA
     65#   PROC_MODE STR check_registration
     66#   MACRO     STR
     67# END
     68# MACRO_DEFINITIONS METADATA
     69#   PROC_MODE STR define_burntool
     70#   MACRO     STR
     71# END
     72# MACRO_DEFINITIONS METADATA
     73#   PROC_MODE STR queue_burntool
     74#   MACRO     STR
     75# END
     76# MACRO_DEFINITIONS METADATA
     77#   PROC_MODE STR check_stacks
     78#   MACRO     STR
     79# END
     80# MACRO_DEFINITIONS METADATA
     81#   PROC_MODE STR queue_stacks
     82#   MACRO     STR
     83# END
     84# MACRO_DEFINITIONS METADATA
     85#   PROC_MODE STR check_sweetspot
     86#   MACRO     STR
     87# END
     88# MACRO_DEFINITIONS METADATA
     89#   PROC_MODE STR queue_sweetspot
     90#   MACRO     STR
     91# END
     92# MACRO_DEFINITIONS METADATA
     93#   PROC_MODE STR check_dqstats
     94#   MACRO     STR
     95# END
     96# MACRO_DEFINITIONS METADATA
     97#   PROC_MODE STR queue_dqstats
     98#   MACRO     STR
     99# END
     100# MACRO_DEFINITIONS METADATA
     101#   PROC_MODE STR check_detrends
     102#   MACRO     STR
     103# END
     104# MACRO_DEFINITIONS METADATA
     105#   PROC_MODE STR queue_detrends
     106#   MACRO     STR
     107# END
    39108
    40109TARGETS MULTI
  • branches/sc_branches/trunkTest/ippconfig/recipes/ppSim.config

    r28125 r29060  
    7070
    7171GALAXY.LUM        F32   -0.8            # Stellar luminosity function slope
    72 GALAXY.MAG        F32   11.0            # Brightest magnitude for fake stars
    73 GALAXY.DENSITY    F32   6.0             # Galaxy density (per square degree) at the brightest magnitude
    74 
    75 GALAXY.RMAJOR.MIN F32   2.0             # Minimum semi-major axis for galaxy models
     72GALAXY.MAG        F32   15.0            # Brightest magnitude for fake stars
     73GALAXY.DENSITY    F32   5.0             # Galaxy density (per square degree) at the brightest magnitude
     74
     75GALAXY.RMAJOR.MIN F32   5.0             # Minimum semi-major axis for galaxy models
    7676GALAXY.RMAJOR.MAX F32   10.0            # Maximum semi-major axis for galaxy models
    7777
    7878GALAXY.ARATIO.MIN F32   1.0             # Minimum axis ratio for galaxy models
    79 GALAXY.ARATIO.MAX F32   0.2             # Maximum axis ratio for galaxy models
     79GALAXY.ARATIO.MAX F32   0.5             # Maximum axis ratio for galaxy models
    8080
    8181GALAXY.INDEX.MIN F32    0.25            # Minimum Sersic index for galaxy models
    82 GALAXY.INDEX.MAX F32    0.500           # Maximum Sersic index for galaxy models
     82GALAXY.INDEX.MAX F32    1.000           # Maximum Sersic index for galaxy models
    8383
    8484GALAXY.THETA.MIN F32    0.0             # Minimum position angle for galaxy models
    8585GALAXY.THETA.MAX F32    3.14            # Maximum position angle for galaxy models
    8686
    87 GALAXY.GRID      BOOL   FALSE           # Generate a (regular) grid of galaxy models?
    88 GALAXY.GRID.DX   S32    200             # Spacing between galaxies in grid in x
    89 GALAXY.GRID.DY   S32    200             # Spacing between galaxies in grid in y
     87GALAXY.GRID        BOOL   FALSE         # Generate a (regular) grid of galaxy models?
     88GALAXY.GRID.DX     S32    100             # Spacing between galaxies in grid in x
     89GALAXY.GRID.DY     S32    200             # Spacing between galaxies in grid in y
     90GALAXY.GRID.RANDOM BOOL   FALSE         # randomize shape parameters in grid (or sequence?)
    9091
    9192GALAXY.MODEL     STR    PS_MODEL_GAUSS
  • branches/sc_branches/trunkTest/ippconfig/recipes/ppSub.config

    r28043 r29060  
    1515SYS.ERR         F32     0.1             # Relative systematic error in images
    1616SKY.ERR         F32     0.0             # Relative systematic error in images
    17 NORM.FRAC       F32     0.03            # Fraction of window for normalisation window
     17NORM.FRAC       F32     0.01            # Fraction of window for normalisation window
    1818COVAR.FRAC      F32     0.01            # Fraction of kernel to truncate before calculating covariance
    1919
     
    2828MASKSTAT.ADVISORY   U32 0x080
    2929
     30INPUT.MATCH.RADIUS F32  20.0
     31INPUT.MATCH.MIN.SN F32  10.0
    3032
    3133LOW.THRESHOLD   F32     4.0             # Mask pixels below this threshold (std dev)
     
    5759INNER           S32     5               # Inner half-size for SPAM and FRIES kernels
    5860RINGS.ORDER     S32     2               # Polynomial order for RINGS kernels
    59 PENALTY         F32     1.0             # Penalty for wideness
     61PENALTY         F32     16.0            # Penalty for wideness
    6062BIN1            S32     4               # Binning factor for first level
    6163BIN2            S32     4               # Binning factor for second level
  • branches/sc_branches/trunkTest/ippconfig/recipes/psphot.config

    r28422 r29060  
    9090
    9191PSF_FIT_ITER                        S32   50              # Maximum number of fitting iterations for PSF
    92 PSF_FIT_TOL                         F32   0.01            # Fit tolerance for PSF
     92PSF_FIT_MIN_TOL                     F32   0.01            # Fit tolerance for PSF
     93PSF_FIT_MAX_TOL                     F32   2.00            # Fit tolerance for PSF
     94
     95PSF_FIT_MIN_VALID_FLUX              F32  -100000000.0     # minimum allow flux for fitted source
     96PSF_FIT_MAX_VALID_FLUX              F32  +100000000.0     # maximum allow flux for fitted source
     97
     98# the following is used to require a minimum quality of fit before
     99# stopping based on the tolerance.  be careful of using this in
     100# conjunction with non-poisson errors (POISSON.ERRORS.LMM) or with
     101# pooly determined errors
     102PSF_FIT_MAX_CHISQ                   F32  20.00            # require chisq / DOF to reach this level before giving up
    93103
    94104# PSF model parameters : choose the PSF model
     
    113123PSF.FAKE.ALLOW                      BOOL  FALSE           # Allow use of fake PSFs when having trouble?
    114124
     125PSF_FIT_RADIUS                      F32   15.0            # XXX this is now autoscaled
    115126PSF_REF_RADIUS                      F32   25.0            # aperture magnitudes are scaled via
    116127                                                          # curve-of-growth to this radius
     
    138149 
    139150# this model is used for approximate subtraction in the main object analysis step
    140 EXT_MODEL                           STR   PS_MODEL_QGAUSS
     151EXT_MODEL                           STR   PS_MODEL_SERSIC
    141152EXT_FIT_NSIGMA                      F32   1               # significance for pixel included in fit
    142153EXT_FIT_PADDING                     F32   5               # extra annulus to use for fit
     
    145156
    146157EXT_FIT_ITER                        S32   20              # Maximum number of fitting iterations for EXT
    147 EXT_FIT_TOL                         F32   0.01            # Fit tolerance for EXT
     158EXT_FIT_MIN_TOL                     F32   0.01            # Fit tolerance for EXT
     159EXT_FIT_MAX_TOL                     F32   2.00            # Fit tolerance for EXT
     160
     161# the following is used to require a minimum quality of fit before
     162# stopping based on the tolerance.  be careful of using this in
     163# conjunction with non-poisson errors (POISSON.ERRORS.LMM) or with
     164# pooly determined errors
     165EXT_FIT_MAX_CHISQ                   F32  20.00            # require chisq / DOF to reach this level before giving up
    148166
    149167# extended source aperture-like measurements
     
    170188 #PGAUSS_PSF  EXTENDED_SOURCE_MODEL  PS_MODEL_PGAUSS   20.0    TRUE
    171189 #QGAUSS_PSF  EXTENDED_SOURCE_MODEL  PS_MODEL_QGAUSS   20.0    TRUE
    172  SERSIC_PSF  EXTENDED_SOURCE_MODEL  PS_MODEL_SERSIC   20.0    TRUE
     190 EXP_PCM  EXTENDED_SOURCE_MODEL  PS_MODEL_EXP      20.0    TRUE
     191 DEV_PCM  EXTENDED_SOURCE_MODEL  PS_MODEL_DEV      20.0    TRUE
     192 SER_PCM  EXTENDED_SOURCE_MODEL  PS_MODEL_SERSIC   20.0    TRUE
    173193END
    174194
     
    224244PCM_BOX_SIZE                        S32   8
    225245
    226 NOISE.FACTOR                        F32   5.0
     246NOISE.FACTOR                        F32  10.0
    227247NOISE.SIZE                          F32   2.0
    228248
     
    307327# Extended source fit parameters
    308328STACKPHOT                             METADATA
     329  EXTENDED_SOURCE_FITS                BOOL  TRUE  # perform any of the aperture-like measurements?
    309330  EXTENDED_SOURCE_ANALYSIS            BOOL  TRUE  # perform any of the aperture-like measurements?
    310331  EXTENDED_SOURCE_SN_LIM              F32   20.0
    311332  EXTENDED_SOURCE_PETROSIAN           BOOL  TRUE
    312   EXTENDED_SOURCE_ISOPHOTAL           BOOL  FALSE
    313333  EXTENDED_SOURCE_ANNULI              BOOL  TRUE
    314   EXTENDED_SOURCE_KRON                BOOL  FALSE
    315334  PSPHOT.STACK.MATCH.PSF.SOURCE       STR   AUTO # which inputs to convolve? (RAW, CNV, AUTO)
    316335  PSPHOT.STACK.TARGET.PSF.AUTO        BOOL  F    # automatically determine target PSF size?
     
    319338  RADIAL_APERTURES_SN_LIM             F32   0.0  # S/N limit for radial aperture calculation
    320339  OUTPUT.FORMAT                       STR   PS1_SV1
     340
     341  EXT_FIT_MAX_RADIUS                  F32   50.0
     342  PSF_MODEL                           STR   PS_MODEL_GAUSS
     343  EXT_MODEL                           STR   PS_MODEL_SERSIC
    321344END
    322345
     
    343366  DIFF_STATS                        BOOL TRUE          # Calculate diff-specific stats
    344367  OUTPUT.FORMAT                     STR  PS1_DV1       # Format of output file
    345 
    346368END
    347369
  • branches/sc_branches/trunkTest/ippconfig/simtest/format.config

    r27231 r29060  
    6969        CELL.X0         S32     0
    7070        CELL.Y0         S32     0
    71         CELL.XSIZE      S32     1024
    72         CELL.YSIZE      S32     1024
     71        CELL.XSIZE      S32     1000
     72        CELL.YSIZE      S32     1000
    7373        CELL.XWINDOW    S32     0
    7474        CELL.YWINDOW    S32     0
  • branches/sc_branches/trunkTest/ppSim/src/ppSimArguments.c

    r24807 r29060  
    5555    psMetadataAddStr(arguments,  PS_LIST_TAIL, "-galmodel", 0, "Type of Galaxy model", NULL);
    5656    psMetadataAddS32(arguments,  PS_LIST_TAIL, "-bin", 0, "Binning in x and y", 1);
     57    psMetadataAddS32(arguments,  PS_LIST_TAIL, "-nx", 0, "cell x-size in pixels", 0);
     58    psMetadataAddS32(arguments,  PS_LIST_TAIL, "-ny", 0, "cell y-size in pixels", 0);
    5759    psMetadataAddBool(arguments, PS_LIST_TAIL, "+photom", 0, "Perform photometry on fake sources", false);
    5860
     
    197199    ppSimArgToRecipeS32(&status,  options, "BINNING",       arguments, "-bin");
    198200
     201    ppSimArgToRecipeS32(&status,  config->arguments, "NX.CELL", arguments, "-nx");
     202    ppSimArgToRecipeS32(&status,  config->arguments, "NY.CELL", arguments, "-ny");
     203
    199204    if (type == PPSIM_TYPE_OBJECT) {
    200205        // Load values required for adding stars
  • branches/sc_branches/trunkTest/ppSim/src/ppSimCreate.c

    r27989 r29060  
    2626    }
    2727
     28    assert (config->camera);
     29
     30    // int nx = psMetadataLookupS32 (&status, config->arguments, "NX.CELL");
     31    // if (nx) {
     32    //   psMetadata *defaults = psMetadataLookupPtr(&status, config->camera, "DEFAULTS");
     33    //   psMetadataLookupS32 (&status, defaults, "CELL.XSIZE");
     34    //   psAssert (status, "CELL.XSIZE should be in DEFAULTS");
     35    //   psMetadataAddF32(defaults, PS_LIST_TAIL, "CELL.XSIZE", PS_META_REPLACE, "", nx);
     36    // }
     37    // int ny = psMetadataLookupS32 (&status, config->arguments, "NY.CELL");
     38    // if (ny) {
     39    //   psMetadata *defaults = psMetadataLookupPtr(&status, config->camera, "DEFAULTS");
     40    //   psMetadataLookupS32 (&status, defaults, "CELL.YSIZE");
     41    //   psAssert (status, "CELL.YSIZE should be in DEFAULTS");
     42    //   psMetadataAddF32(defaults, PS_LIST_TAIL, "CELL.YSIZE", PS_META_REPLACE, "", ny);
     43    // }
     44
    2845    // generate the fpa structure used by the output camera (determined from INPUT or specified)
    29     assert (config->camera);
    3046    fpa = pmFPAConstruct(config->camera, config->cameraName); // FPA to contain the observation
    3147    if (!fpa) {
  • branches/sc_branches/trunkTest/ppSim/src/ppSimInsertGalaxies.c

    r27657 r29060  
    11# include "ppSim.h"
    22static char *defaultModel = "PS_MODEL_SERSIC";
     3
     4float imageSum (psImage *image) {
     5    float sum = 0.0;
     6    for (int iy = 0; iy < image->numRows; iy++) {
     7        for (int ix = 0; ix < image->numCols; ix++) {
     8            sum += image->data.F32[iy][ix];
     9        }
     10    }
     11    return sum;
     12}
    313
    414bool ppSimInsertGalaxies (pmReadout *readout, psImage *expCorr, psArray *galaxies, pmConfig *config) {
     
    8494    psArray *sources = sources = detections->allSources;
    8595
     96    // output filename
     97    char outname[1024];
     98    char *outroot = psMetadataLookupStr(&mdok, config->arguments, "OUTPUT");
     99    sprintf (outname, "%s.dat", outroot);
     100
     101    FILE *outfile = fopen (outname, "a");
     102
    86103    // add sources to the readout image & weight
    87104    for (long i = 0; i < galaxies->n; i++) {
     
    131148
    132149        // XXX let the flux limit be a user-defined number of sky sigmas (not just 1.0)
    133         float radius = model->modelRadius (model->params, roughNoise);
     150        float radius = model->modelRadius (model->params, 0.1*roughNoise);
    134151        radius = PS_MAX (radius, 1.0);
    135152        // XXX the exp(-r^0.25) models can go way out if allowed...
    136         radius = PS_MIN (radius, 150.0);
     153        radius = PS_MIN (radius, 300.0);
    137154
    138155        // construct a source, with model flux pixels set, based on the model
    139156        pmSource *source = pmSourceFromModel (model, readout, radius, PM_SOURCE_TYPE_EXTENDED);
     157
     158        galaxy->flux = model->modelFlux (model->params);
    140159
    141160        // XXX set the mag & err values (should this be done in pmSourceFromModel?)
     
    145164        source->errMag = sqrt(Area*PS_SQR(roughNoise) + galaxy->flux) / galaxy->flux;   
    146165       
    147         // XXX add the sources to a source array
     166        // insert the source flux in the image
     167        float sum1 = imageSum(source->pixels);
     168        pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
     169        float sum2 = imageSum(source->pixels);
     170        float flux = sum2 - sum1;
    148171
    149 # if (0)       
    150         if (CONVOLVED_FIT) {
     172        // fprintf (stderr, "flux: %f, sum1: %f, sum2: %f, flux2: %f, dflux: %f\n", galaxy->flux, sum1, sum2, sum2 - sum1, galaxy->flux - sum2 + sum1);
    151173
    152             // select the PSF (XXX : move out of loop)
    153             pmPSF *psfModel = psMetadataLookupPtr (&mdok, chip->analysis, "PSPHOT.PSF");
    154             assert (psf);
    155            
    156             // supply the psf to the source (normalized?)
    157             source->modelPSF = pmModelFromPSFforXY (psfModel, xChip, yChip, 1.0);
     174        float par8 = (model->params->n == 8) ? model->params->data.F32[7] : 0.0;
     175        fprintf (outfile, "%8.3f %8.3f %10.2f  %2d  %7.3f %5.3f  %5.3f %5.3f %5.3f %5.3f\n", galaxy->x, galaxy->y, flux, 1, source->psfMag, source->errMag, axes.major, axes.minor, axes.theta, par8);
    158176
    159             // instantiate the psf flux
    160             pmSourceCachePSF (source, maskVal);
    161 
    162             // convert the cached cached psf model for this source to a psKernel
    163             // XXX for the moment, hard-wire the kernel to be 5x5 (2 pix radius)
    164             // XXX for the moment, hard-wire the kernel to be 9x9 (4 pix radius)
    165             psKernel *psf = psphotKernelFromPSF (source, psfSize);
    166 
    167             // instantiate the source model flux
    168             // XXX this may need to handle the offset?
    169             pmSourceCacheModel (source, maskVal);
    170 
    171             // make a storage buffer for the output image
    172             psImage *buffer = psImageCopy (source->modelFlux, PS_TYPE_F32);
    173 
    174             // convolve the psf image with the model image
    175             psImageConvolveDirect (buffer, source->modelFlux, psf);
    176 
    177             // save the result back in the source
    178             psFree (source->modelFlux);
    179             source->modelFlux = buffer;
    180         }
    181 # endif
    182 
    183         // insert the source flux in the image
    184         pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
    185177        psArrayAdd (sources, 100,source);
    186178    }
     179    fclose (outfile);
    187180
    188181    // XXX many leaks in here, i think
     
    191184    return true;
    192185}
    193 
  • branches/sc_branches/trunkTest/ppSim/src/ppSimInsertStars.c

    r28127 r29060  
    105105        float flux = star->flux * expCorr->data.F32[(int)yCell][(int)xCell];
    106106
    107         // if psfConvolve is TRUE, we will (elsewhere) convolve the image we a PSF
    108         // in this case, simply place delta functions in the image
    109         if (psfConvolve) {
    110             readout->image->data.F32[(int)(yCell)][(int)(xCell)] += flux;
    111             continue;
    112         }
    113 
    114107        // instantiate a model for the PSF at this location, set desired flux
    115108        pmModel *model = pmModelFromPSFforXY (psf, xChip, yChip, 1.0);
     
    125118        // XXX i should be applying the gain and the correct effective area
    126119        psEllipseAxes axes = pmPSF_ModelToAxes (model->params->data.F32, 20.0);
    127         psF64 Area = 2.0 * M_PI * axes.major * axes.minor;
     120        float Area = 2.0 * M_PI * axes.major * axes.minor;
    128121
    129         // this value is the pure (input) flux, and is saved in the output source cmf files
    130122        source->psfMag = -2.5*log10(star->flux);
    131123        source->errMag = sqrt(Area*PS_SQR(roughNoise) + flux) / flux;
     124       
     125        // set the expected model errors
     126        model->dparams->data.F32[PM_PAR_I0] = source->errMag * model->params->data.F32[PM_PAR_I0];
    132127
    133         fprintf (outfile, "%8.3f %8.3f  %10.2f  %7.3f %5.3f\n", star->x, star->y, star->flux, source->psfMag, source->errMag);
     128        float par8 = (model->params->n == 8) ? model->params->data.F32[7] : 0.0;
     129        fprintf (outfile, "%8.3f %8.3f %10.2f  %2d  %7.3f %5.3f  %5.3f %5.3f %5.3f %5.3f\n", star->x, star->y, star->flux, 0, source->psfMag, source->errMag, axes.major, axes.minor, axes.theta, par8);
    134130
    135         // insert the source flux in the image
    136         pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
    137 
    138         // insert the source flux in the noise image
    139         pmSourceAddWithOffset (source, PM_MODEL_OP_FULL | PM_MODEL_OP_NOISE, 0xff, dX, dY);
    140 
    141         // Blow away the image parts of the source, which makes the memory explode
    142         RESET(source->pixels);
    143         RESET(source->variance);
    144         RESET(source->maskObj);
    145         RESET(source->maskView);
    146         RESET(source->modelFlux);
    147         RESET(source->psfImage);
    148         RESET(source->blends);
     131        // if psfConvolve is TRUE, we will (elsewhere) convolve the image we a PSF
     132        // in this case, simply place delta functions in the image
     133        if (psfConvolve) {
     134            readout->image->data.F32[(int)(yCell)][(int)(xCell)] += flux;
     135        } else {
     136            // insert the source flux in the image
     137            pmSourceAddWithOffset (source, PM_MODEL_OP_FULL, 0xff, dX, dY);
     138           
     139            // insert the source flux in the noise image
     140            pmSourceAddWithOffset (source, PM_MODEL_OP_FULL | PM_MODEL_OP_NOISE, 0xff, dX, dY);
     141           
     142            // Blow away the image parts of the source, which makes the memory explode
     143            RESET(source->pixels);
     144            RESET(source->variance);
     145            RESET(source->maskObj);
     146            RESET(source->maskView);
     147            RESET(source->modelFlux);
     148            RESET(source->psfImage);
     149            RESET(source->blends);
     150        }
    149151
    150152        // add the sources to the source array
  • branches/sc_branches/trunkTest/ppSim/src/ppSimMakeGalaxies.c

    r18011 r29060  
    1818    int galaxyGridDX      = psMetadataLookupS32(&mdok, recipe, "GALAXY.GRID.DX"); // Density of fakes
    1919    int galaxyGridDY      = psMetadataLookupS32(&mdok, recipe, "GALAXY.GRID.DY"); // Density of fakes
     20    bool galaxyGridRandom = psMetadataLookupBool(&mdok, recipe, "GALAXY.GRID.RANDOM"); // Density of fakes
    2021   
    2122    float galaxyRmajorMax = psMetadataLookupF32(&mdok, recipe, "GALAXY.RMAJOR.MAX"); // Density of fakes
     
    4344
    4445    if (galaxyDensity <= 0) return true;
     46
     47    if (galaxyGridRandom) {
     48        long A, B;
     49        A = time(NULL);
     50        for (B = 0; A == time(NULL); B++);
     51        srand48(B);
     52    }
    4553
    4654    // Size of FPA
     
    8189        psLogMsg("ppSim", PS_LOG_INFO, "Adding grid of %ld galaxies\n", num);
    8290
    83         float galaxyIndexSlope = (galaxyIndexMax - galaxyIndexMin) / num;
    84         float galaxyThetaSlope = (galaxyThetaMax - galaxyThetaMin) / num;
    85         float galaxyARatioSlope = (galaxyARatioMax - galaxyARatioMin) / num;
    86         float galaxyRmajorSlope = (galaxyRmajorMax - galaxyRmajorMin) / num;
     91        float galaxyIndexSlope = (galaxyIndexMax - galaxyIndexMin);
     92        float galaxyThetaSlope = (galaxyThetaMax - galaxyThetaMin);
     93        float galaxyARatioSlope = (galaxyARatioMax - galaxyARatioMin);
     94        float galaxyRmajorSlope = (galaxyRmajorMax - galaxyRmajorMin);
    8795
    8896        int i = 0;
     
    96104                galaxy->y    = iy;
    97105
    98                 galaxy->peak = 20000;
     106                galaxy->peak = 1000;
    99107
    100                 galaxy->index = (galaxyIndexMin  + i * galaxyIndexSlope);
    101                 galaxy->Rmaj  = (galaxyRmajorMin + i * galaxyRmajorSlope);
    102                 galaxy->Rmin  = (galaxyARatioMin + i * galaxyARatioSlope) * galaxy->Rmaj;
    103                 galaxy->theta = (galaxyThetaMin  + i * galaxyThetaSlope);
     108                // galaxyIndex from user should be for function of this form: exp(-r^(1/n))
     109                // galaxy->index = (1/2n)
     110
     111                float rndValue;
     112
     113                rndValue = galaxyGridRandom ? drand48() : i / num;
     114                float index = (galaxyIndexMin  + rndValue * galaxyIndexSlope);
     115                galaxy->index = 0.5/index; // factor of 0.5 because the Sersic model creates exp(-z^n), not exp(-r^n)
     116
     117                rndValue = galaxyGridRandom ? drand48() : i / num;
     118                float scale = (galaxyRmajorMin + rndValue * galaxyRmajorSlope);
     119
     120                // for a sersic model,
     121                float bn = 1.9992*index - 0.3271;
     122                float fR = 1.0 / (sqrt(2.0) * pow (bn, index));
     123                float Io = exp(bn);
     124
     125                galaxy->Rmaj  = scale;
     126
     127                rndValue = galaxyGridRandom ? drand48() : i / num;
     128                galaxy->Rmin  = (galaxyARatioMin + rndValue * galaxyARatioSlope) * galaxy->Rmaj;
     129
     130                rndValue = galaxyGridRandom ? drand48() : i / num;
     131                galaxy->theta = (galaxyThetaMin  + rndValue * galaxyThetaSlope);
     132
     133                // galaxy->peak *= Io;
     134
     135                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);
     137                }
    104138
    105139                psArrayAdd (galaxies, 100, galaxy);
     
    124158            galaxy->y    = psRandomUniform(rng) * ySize; // y position
    125159
    126             galaxy->flux = pow ((double)((i + 1) / norm), (double) (1.0 / galaxyLum));
     160            float index = (sqrt(psRandomUniform(rng)) * galaxyIndexSlope  + galaxyIndexMin);
     161            galaxy->index = 0.5/index; // factor of 0.5 because the Sersic model creates exp(-z^n), not exp(-r^n)
    127162
    128             galaxy->index = (psRandomUniform(rng) * galaxyIndexSlope  + galaxyIndexMin);
     163            float scale = (psRandomUniform(rng) * galaxyRmajorSlope + galaxyRmajorMin);
     164
     165            // for a sersic model,
     166            float bn = 1.9992*index - 0.3271;
     167            float fR = 1.0 / (sqrt(2.0) * pow (bn, index));
     168            float Io = exp(bn);
     169
     170            galaxy->Rmaj  = scale;
     171
     172            galaxy->Rmin  = (PS_SQR(psRandomUniform(rng)) * galaxyARatioSlope + galaxyARatioMin) * galaxy->Rmaj;
     173
    129174            galaxy->theta = (psRandomUniform(rng) * galaxyThetaSlope  + galaxyThetaMin);
    130             galaxy->Rmaj  = (psRandomUniform(rng) * galaxyRmajorSlope + galaxyRmajorMin);
    131             galaxy->Rmin  = (psRandomUniform(rng) * galaxyARatioSlope + galaxyARatioMin) * galaxy->Rmaj;
    132175
     176            // the flux is only approximately correct (scaling of the peak is wrong)
     177            // elsewhere (ppSimInsertGalaxies.c) we calculate the true galaxy flux
    133178            galaxy->flux = expf((logf(i + 1) - logf(norm)) / galaxyLum); // Peak flux
    134179            galaxy->peak = galaxy->flux / (2.0*M_PI*PS_SQR(seeing));
    135             // galaxy->peak = 5000;
     180
     181            if (0) {
     182              fprintf (stderr, "Rmaj: %f, scale: %f, index: %f, bn: %f, Ro: %f, Io: %f\n", galaxy->Rmaj, scale, index, bn, fR, Io);
     183            }
    136184
    137185            psArrayAdd (galaxies, 100, galaxy);
  • branches/sc_branches/trunkTest/ppSim/src/ppSimPhotomReadout.c

    r27657 r29060  
    6969    psArray *fakeSources = psArrayAlloc (injectedSources->n);
    7070    for (int i = 0; i < injectedSources->n; i++) {
    71       fakeSources->data[i] = pmSourceCopy (injectedSources->data[i]);
     71      fakeSources->data[i] = pmSourceCopyData (injectedSources->data[i]);
    7272    }
    7373
  • branches/sc_branches/trunkTest/ppSim/src/ppSimPhotomReadoutFake.c

    r27657 r29060  
    4040    psArray *fakeSources = psArrayAlloc (injectedSources->n);
    4141    for (int i = 0; i < injectedSources->n; i++) {
    42       fakeSources->data[i] = pmSourceCopy (injectedSources->data[i]);
     42      fakeSources->data[i] = pmSourceCopyData (injectedSources->data[i]);
    4343    }
    4444
     
    4747    psArray *realSources = psArrayAlloc (realMeasuredSources->n);
    4848    for (int i = 0; i < realMeasuredSources->n; i++) {
    49         realSources->data[i] = pmSourceCopy (realMeasuredSources->data[i]);
     49        realSources->data[i] = pmSourceCopyData (realMeasuredSources->data[i]);
    5050    }
    5151
  • branches/sc_branches/trunkTest/ppSim/src/ppSimPhotomReadoutForce.c

    r27657 r29060  
    4646    psArray *realSources = psArrayAlloc (realMeasuredSources->n);
    4747    for (int i = 0; i < realMeasuredSources->n; i++) {
    48         realSources->data[i] = pmSourceCopy (realMeasuredSources->data[i]);
     48        realSources->data[i] = pmSourceCopyData (realMeasuredSources->data[i]);
    4949    }
    5050
  • branches/sc_branches/trunkTest/ppSim/src/ppSimSmoothReadout.c

    r28125 r29060  
    77    // XXX use these defaults?
    88    // float minGauss = 0.1;
    9     float nSigma = 3.0;
     9    float nSigma = 4.0;
    1010    float sigma = psMetadataLookupF32(&status, recipe, "SEEING"); // Seeing SIGMA (pixels)
    1111
     
    2828// 4*pi*SIGMA_SMTH^2, but for measurements based on apertures comparable to or larger than the
    2929// smoothing kernel, the effective per-pixel variance is maintained.
     30
     31
     32// XXX this only allows for Gaussian PSFs.  To extend this, we need to convolve with an image
     33// of the PSF model
  • branches/sc_branches/trunkTest/ppSub/src/Makefile.am

    r28043 r29060  
    3939        ppSubDefineOutput.c             \
    4040        ppSubExtras.c                   \
     41        ppSubFlagNeighbors.c            \
    4142        ppSubMakePSF.c                  \
    4243        ppSubMatchPSFs.c                \
  • branches/sc_branches/trunkTest/ppSub/src/ppSub.h

    r28121 r29060  
    175175    );
    176176
     177bool ppSubFlagNeighbors(pmConfig *config, pmFPAview *view, psArray *sources, bool matchRef);
     178bool ppSubMatchSources (psArray *objects, psArray *sources, float RADIUS, float MIN_SN);
     179bool ppSubSetSourceImageIDs (psArray *sources, int imageID);
     180
    177181///@}
    178182#endif
  • branches/sc_branches/trunkTest/ppSub/src/ppSubBackground.c

    r26982 r29060  
    6767    for (int y = 0; y < numRows; y++) {
    6868        for (int x = 0; x < numCols; x++) {
     69            // special case 1: NAN the masked pixels
    6970            if (mask && mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskBad) {
    70                 image->data.F32[y][x] = 0.0;
    71             } else {
    72                 float value = psImageUnbinPixel(x + 0.5, y + 0.5, modelImage, binning); // Background value
    73                 if (!isfinite(value)) {
    74                     image->data.F32[y][x] = NAN;
    75                     mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskBad;
    76                 } else {
    77                     image->data.F32[y][x] -= value;
    78                 }
    79             }
     71                image->data.F32[y][x] = NAN;
     72                continue;
     73            }
     74            // special case 1: NAN & mask pixels without a valid background model
     75            float value = psImageUnbinPixel(x + 0.5, y + 0.5, modelImage, binning); // Background value
     76            if (!isfinite(value)) {
     77              image->data.F32[y][x] = NAN;
     78              mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] |= maskBad;
     79              continue;
     80            }
     81            image->data.F32[y][x] -= value;
    8082        }
    8183    }
  • branches/sc_branches/trunkTest/ppSub/src/ppSubLoop.c

    r28121 r29060  
    1717#include <pslib.h>
    1818#include <psmodules.h>
     19#include <psphot.h>
    1920
    2021#include "ppSub.h"
     22
     23bool dumpout(pmConfig *config, char *name)
     24{
     25  pmFPAview *view = ppSubViewReadout(); // View to readout
     26  pmReadout *out = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT");
     27  psphotSaveImage (NULL, out->image, name);
     28  return true;
     29}
     30
    2131
    2232bool ppSubLoop(ppSubData *data)
     
    105115        return false;
    106116    }
     117    // dumpout(config, "diff.1.fits");
    107118
    108119    // Close convolved files
     
    111122        return false;
    112123    }
     124    // dumpout(config, "diff.2a.fits");
    113125
    114126    // Higher order background subtraction using psphot
     
    117129        return false;
    118130    }
     131    // dumpout(config, "diff.2b.fits");
    119132
    120133    // Perform Variance correction (rescale within a modest range)
     
    123136        return false;
    124137    }
     138    // dumpout(config, "diff.2c.fits");
    125139
    126140    if (data->quality) {
     
    138152        return false;
    139153    }
     154    // dumpout(config, "diff.3.fits");
    140155
    141156    if (!ppSubFilesIterateUp(config, PPSUB_FILES_PHOT_SUB)) {
     
    157172      }
    158173    }
     174    // dumpout(config, "diff.4.fits");
    159175   
    160176    // generate the binned image used to write the jpeg
     
    204220    } else {
    205221        // Close subtraction files
     222        // dumpout(config, "diff.5.fits");
    206223        if (!ppSubFilesIterateUp(config, PPSUB_FILES_SUB)) {
    207224            psError(PPSUB_ERR_IO, false, "Unable to close subtraction files.");
  • branches/sc_branches/trunkTest/ppSub/src/ppSubReadoutPhotometry.c

    r26982 r29060  
    9797    psAssert (sources, "missing sources?");
    9898
     99    // a likely source of false positives is poor subtractions.  this results in
     100    // detections in the wings (or cores) of bright(er) stars found in both images.
     101    // flag detections based on their distance from the bright(er) input sources.
     102    bool matchRef = !strcasecmp(name, "PPSUB.INVERSE");
     103    ppSubFlagNeighbors (config, view, sources, matchRef);
     104
    99105    if (data->stats) {
    100106        bool mdok;
  • branches/sc_branches/trunkTest/ppSub/src/ppSubReadoutSubtract.c

    r28006 r29060  
    5151    outRO->variance = (psImage*)psBinaryOp(outRO->variance, minuend->variance, "+", subtrahend->variance);
    5252
     53    // NAN the masked pixels in the diff image (pixels masked in A are not yet NAN'ed in B)
     54    psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config) | pmConfigMaskGet("BLANK", config); // Bits to mask in inputs
     55    for (int iy = 0; iy < outRO->image->numRows; iy++) {
     56        for (int ix = 0; ix < outRO->image->numCols; ix++) {
     57            if ((outRO->mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskVal) == 0) continue;
     58            outRO->image->data.F32[iy][ix] = NAN;
     59        }
     60    }
     61
    5362    // Measure the variance scales
    5463    psStats *varStats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics for variance images
    5564    psRandom *rng = psRandomAlloc(PS_RANDOM_TAUS);           // Random number generator
    56     psImageMaskType maskVal = pmConfigMaskGet("MASK.VALUE", config) |
    57         pmConfigMaskGet("BLANK", config); // Bits to mask in inputs
    5865    psImageBackground(varStats, NULL, minuend->variance, minuend->mask, maskVal, rng);
    5966    float minuendVar = varStats->robustMedian; // Mean variance for minuend
  • branches/sc_branches/trunkTest/psLib/src/fft/psImageFFT.c

    r21183 r29060  
    2323#include <pthread.h>
    2424
     25#include "psAbort.h"
    2526#include "psAssert.h"
    2627#include "psError.h"
     
    532533    return out;
    533534}
     535
     536void psKernelFFTFree(psKernelFFT *kernel) {
     537
     538    if (!kernel->fft) return;
     539   
     540    bool threaded = psThreadPoolSize(); // Are we running threaded?
     541
     542    FFTW_LOCK;
     543    fftwf_free(kernel->fft);
     544    FFTW_UNLOCK;
     545
     546    return;
     547}
     548
     549psKernelFFT *psKernelFFTAlloc(const psKernel *input) {
     550
     551    psKernelFFT *kernel = psAlloc(sizeof(psKernelFFT));
     552    psMemSetDeallocator(kernel, (psFreeFunc)psKernelFFTFree);
     553
     554    kernel->fft = NULL;
     555
     556    kernel->xMin = input->xMin;
     557    kernel->xMax = input->xMax;
     558    kernel->yMin = input->yMin;
     559    kernel->yMax = input->yMax;
     560
     561    kernel->paddedCols = 0;
     562    kernel->paddedRows = 0;
     563
     564    return kernel;
     565}
     566
     567// generate the FFTed kernel image appropriate to be used for convolution of the given input image
     568psKernelFFT *psImageConvolveKernelInit(const psImage *in, const psKernel *kernel)
     569{
     570    PS_ASSERT_IMAGE_NON_NULL(in, NULL);
     571    PS_ASSERT_IMAGE_TYPE(in, PS_TYPE_F32, NULL);
     572    PS_ASSERT_KERNEL_NON_NULL(kernel, NULL);
     573
     574    bool threaded = psThreadPoolSize(); // Are we running threaded?
     575
     576    int numCols = in->numCols, numRows = in->numRows; // Size of image
     577    int xMin = kernel->xMin, xMax = kernel->xMax, yMin = kernel->yMin, yMax = kernel->yMax; // Kernel sizes
     578
     579    // Need to pad the kernel (and later the input image) to protect from wrap-around effects
     580    if (xMax - xMin > numCols || yMax - yMin > numRows) {
     581        // Cannot pad the image if the kernel is larger.
     582        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     583                _("Kernel cannot extend further than input image size (%dx%d vs %dx%d)."),
     584                xMax, yMax, numCols, numRows);
     585        return NULL;
     586    }
     587
     588    int paddedCols = numCols + PS_MAX(-xMin, xMax); // Number of columns in padded image
     589    int paddedRows = numRows + PS_MAX(-yMin, yMax); // Number of rows in padded image
     590
     591#if CONVOLVE_FFT_BINARY_SIZE
     592    // Make the size an integer power of two
     593    {
     594        int twoCols, twoRows;           // Size that is a factor of two
     595        for (twoCols = 1; twoCols <= paddedCols && twoCols < INT_MAX - 1; twoCols <<= 1); // No action
     596        for (twoRows = 1; twoRows <= paddedRows && twoRows < INT_MAX - 1; twoRows <<= 1); // No action
     597        if (paddedCols > twoCols || paddedRows > twoRows) {
     598            psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Unable to scale size (%dx%d) up to factor of two",
     599                    paddedCols, paddedRows);
     600            return NULL;
     601        }
     602        paddedCols = twoCols;
     603        paddedRows = twoRows;
     604    }
     605#endif
     606
     607    int numPadded = paddedCols * paddedRows; // Number of pixels in padded image
     608
     609    // Create data array containing the padded kernel
     610    FFTW_LOCK;
     611    psF32 *data = fftwf_malloc(numPadded * PSELEMTYPE_SIZEOF(PS_TYPE_F32)); // Data for FFTW
     612    FFTW_UNLOCK;
     613
     614    // Generate the padded kernel image.  We could generate the padded kernel image using
     615    // memcpy, but by going pixel by pixel we can apply the normalisation that corrects for the
     616    // FFT renormalisation.  By applying it to the kernel here, we save applying it to the
     617    // output image(s).
     618
     619    // copy kernel into data array
     620    psF32 *dataPtr = data;              // Pointer into FFTW data
     621    float norm = 1.0 / (float)(paddedRows * paddedCols); // Normalisation to correct for FFT
     622
     623    int xNegMin = PS_MIN(-1, xMin), xNegMax = PS_MIN(-1, xMax); // Min and max for x when negative
     624    int xPosMin = PS_MAX(0, xMin), xPosMax = PS_MAX(0, xMax); // Min and max for x when positive
     625    int yNegMin = PS_MIN(-1, yMin), yNegMax = PS_MIN(-1, yMax); // Min and max for x when negative
     626    int yPosMin = PS_MAX(0, yMin), yPosMax = PS_MAX(0, yMax); // Min and max for x when positive
     627    int blankCols = xNegMin + paddedCols - xPosMax - 1; // Number of columns between kernel extrema
     628    int blankRows = (yNegMin + paddedRows - yPosMax - 1) * paddedCols; // Rows between kernel extrema
     629    size_t blankColBytes = blankCols * PSELEMTYPE_SIZEOF(PS_TYPE_F32); // Number of bytes in blankCols
     630    for (int y = yPosMin; y <= yPosMax; y++) {
     631        // y is positive
     632        for (int x = xPosMin; x <= xPosMax; x++, dataPtr++) {
     633            // x is positive
     634            *dataPtr = kernel->kernel[y][x] * norm;
     635        }
     636        // Columns between kernel extrema
     637        memset(dataPtr, 0, blankColBytes);
     638        dataPtr += blankCols;
     639        for (int x = xNegMin; x <= xNegMax; x++, dataPtr++) {
     640            // x is negative
     641            *dataPtr = kernel->kernel[y][x] * norm;
     642        }
     643    }
     644    // Rows between kernel extrema
     645    memset(dataPtr, 0, blankRows * PSELEMTYPE_SIZEOF(PS_TYPE_F32));
     646    dataPtr += blankRows;
     647    for (int y = yNegMin; y <= yNegMax; y++) {
     648        // y is negative
     649        for (int x = xPosMin; x <= xPosMax; x++, dataPtr++) {
     650            // x is positive
     651            *dataPtr = kernel->kernel[y][x] * norm;
     652        }
     653        // Columns between kernel extrema
     654        memset(dataPtr, 0, blankColBytes);
     655        dataPtr += blankCols;
     656        for (int x = xNegMin; x <= xNegMax; x++, dataPtr++) {
     657            // x is negative
     658            *dataPtr = kernel->kernel[y][x] * norm;
     659        }
     660    }
     661
     662#if 0
     663    {
     664        // Use this for inspecting the result of copying the kernel
     665        psImage *test = psImageAlloc(paddedCols, paddedRows, PS_TYPE_F32);
     666        psFree(test->p_rawDataBuffer);
     667        test->p_rawDataBuffer = data;
     668        test->data.V[0] = test->p_rawDataBuffer;
     669        for (int y = 1; y < paddedRows; y++) {
     670            test->data.V[y] = (psPtr)((int8_t *)test->data.V[y - 1] + paddedCols * PSELEMTYPE_SIZEOF(PS_TYPE_F32));
     671        }
     672        // View image here or write to disk
     673        test->p_rawDataBuffer = NULL;
     674        psFree(test);
     675    }
     676#endif
     677
     678    // Do the forward FFT
     679    // Note that the FFT images have different size from the input
     680    FFTW_LOCK;
     681    fftwf_complex *fft = fftwf_malloc((paddedCols/2 + 1) * paddedRows * sizeof(fftwf_complex)); // FFT
     682    FFTW_UNLOCK;
     683
     684    FFTW_LOCK;
     685    fftwf_plan plan = fftwf_plan_dft_r2c_2d(paddedRows, paddedCols, data, fft, FFTW_PLAN_RIGOR);
     686    FFTW_UNLOCK;
     687
     688    fftwf_execute(plan);
     689
     690    FFTW_LOCK;
     691    fftwf_destroy_plan(plan);
     692    fftwf_free(data);
     693    FFTW_UNLOCK;
     694
     695    psKernelFFT *output = psKernelFFTAlloc(kernel);
     696    output->fft = fft;
     697    output->paddedCols = paddedCols;
     698    output->paddedRows = paddedRows;
     699
     700    return output;
     701}
     702
     703psImage *psImageConvolveKernel(psImage *out, const psImage *in, const psImage *mask, psImageMaskType maskVal, const psKernelFFT *kernel)
     704{
     705    PS_ASSERT_IMAGE_NON_NULL(in, NULL);
     706    PS_ASSERT_IMAGE_TYPE(in, PS_TYPE_F32, NULL);
     707    PS_ASSERT_PTR_NON_NULL(kernel, NULL);
     708    PS_ASSERT_PTR_NON_NULL(kernel->fft, NULL);
     709    if (mask) {
     710        PS_ASSERT_IMAGE_NON_NULL(mask, NULL);
     711        PS_ASSERT_IMAGE_TYPE(mask, PS_TYPE_IMAGE_MASK, NULL);
     712        PS_ASSERT_IMAGES_SIZE_EQUAL(mask, in, NULL);
     713    }
     714
     715    bool threaded = psThreadPoolSize(); // Are we running threaded?
     716
     717    int numCols = in->numCols, numRows = in->numRows; // Size of image
     718    int xMin = kernel->xMin, xMax = kernel->xMax, yMin = kernel->yMin, yMax = kernel->yMax; // Kernel sizes
     719
     720    // Need to pad the input image to protect from wrap-around effects
     721    if (xMax - xMin > numCols || yMax - yMin > numRows) {
     722        // Cannot pad the image if the kernel is larger.
     723        psError(PS_ERR_BAD_PARAMETER_SIZE, true, _("Kernel cannot extend further than input image size (%dx%d vs %dx%d)."), xMax, yMax, numCols, numRows);
     724        return NULL;
     725    }
     726
     727    int paddedCols = numCols + PS_MAX(-xMin, xMax); // Number of columns in padded image
     728    int paddedRows = numRows + PS_MAX(-yMin, yMax); // Number of rows in padded image
     729
     730#if CONVOLVE_FFT_BINARY_SIZE
     731    // Make the size an integer power of two
     732    {
     733        int twoCols, twoRows;           // Size that is a factor of two
     734        for (twoCols = 1; twoCols <= paddedCols && twoCols < INT_MAX - 1; twoCols <<= 1); // No action
     735        for (twoRows = 1; twoRows <= paddedRows && twoRows < INT_MAX - 1; twoRows <<= 1); // No action
     736        if (paddedCols > twoCols || paddedRows > twoRows) {
     737            psError(PS_ERR_BAD_PARAMETER_SIZE, true, "Unable to scale size (%dx%d) up to factor of two",
     738                    paddedCols, paddedRows);
     739            return NULL;
     740        }
     741        paddedCols = twoCols;
     742        paddedRows = twoRows;
     743    }
     744#endif
     745
     746    if (paddedCols != kernel->paddedCols) {
     747        psAbort(_("Image is inconsistent with allocated kernel"));
     748    }   
     749    if (paddedRows != kernel->paddedRows) {
     750        psAbort(_("Image is inconsistent with allocated kernel"));
     751    }   
     752
     753    int numPadded = paddedCols * paddedRows; // Number of pixels in padded image
     754
     755    // Create data array containing the padded image
     756    FFTW_LOCK;
     757    psF32 *data = fftwf_malloc(numPadded * PSELEMTYPE_SIZEOF(PS_TYPE_F32)); // Data for FFTW
     758    FFTW_UNLOCK;
     759    psF32 *dataPtr = data;              // Pointer into FFTW data
     760    psF32 **imageData = in->data.F32;   // Pointer into image data
     761
     762    // Copy image into data array
     763    size_t goodBytes = numCols * PSELEMTYPE_SIZEOF(PS_TYPE_F32); // Number of bytes per image row
     764    size_t padBytes = (paddedCols - numCols) * PSELEMTYPE_SIZEOF(PS_TYPE_F32); // Number of bytes to pad
     765    for (int y = 0; y < numRows; y++, dataPtr += paddedCols, imageData++) {
     766        memcpy(dataPtr, *imageData, goodBytes);
     767        memset(dataPtr + numCols, 0, padBytes);
     768    }
     769    memset(dataPtr, 0, (paddedRows - numRows) * paddedCols * PSELEMTYPE_SIZEOF(PS_TYPE_F32));
     770
     771#if 0
     772    {
     773        // Use this for inspecting the result of copying the image
     774        psImage *test = psImageAlloc(paddedCols, paddedRows, PS_TYPE_F32);
     775        psFree(test->p_rawDataBuffer);
     776        test->p_rawDataBuffer = data;
     777        test->data.V[0] = test->p_rawDataBuffer;
     778        for (int y = 1; y < paddedRows; y++) {
     779            test->data.V[y] = (psPtr)((int8_t *)test->data.V[y - 1] +
     780                                      paddedCols * PSELEMTYPE_SIZEOF(PS_TYPE_F32));
     781        }
     782        // View image here
     783        test->p_rawDataBuffer = NULL;
     784        psFree(test);
     785    }
     786#endif
     787
     788    // Mask bad pixels (which may be NANs), lest they infect everything
     789    if (mask && maskVal) {
     790        for (int y = 0; y < numRows; y++) {
     791            for (int x = 0; x < numCols; x++) {
     792                if (mask->data.PS_TYPE_IMAGE_MASK_DATA[y][x] & maskVal) {
     793                    data[x + paddedCols * y] = 0;
     794                }
     795            }
     796        }
     797    }
     798
     799    // Do the forward FFT
     800    // Note that the FFT images have different size from the input
     801    FFTW_LOCK;
     802    fftwf_complex *fft = fftwf_malloc((paddedCols/2 + 1) * paddedRows * sizeof(fftwf_complex)); // FFT
     803    FFTW_UNLOCK;
     804
     805    int fftCols = paddedCols/2 + 1, fftRows = paddedRows; // Size of FFT images
     806    int fftPixels = fftCols * fftRows;  // Number of pixels in FFT image
     807
     808    FFTW_LOCK;
     809    fftwf_plan forward = fftwf_plan_dft_r2c_2d(paddedRows, paddedCols, data, fft, FFTW_PLAN_RIGOR);
     810    FFTW_UNLOCK;
     811
     812    fftwf_execute(forward);
     813
     814    FFTW_LOCK;
     815    fftwf_destroy_plan(forward);
     816    FFTW_UNLOCK;
     817
     818    fftwf_complex *kernelFFT = kernel->fft;
     819
     820    // Multiply the two transforms
     821    for (int i = 0; i < fftPixels; i++) {
     822        // (a + bi) * (c + di) = (ac - bd) + (bc + ad)i
     823#if !defined(FFTW_NO_Complex) && defined(_Complex_I) && defined(complex) && defined(I)
     824        // C99 complex support
     825        fft[i] *= kernelFFT[i];
     826#else
     827        // FFTW's backup complex support
     828        float imageReal = fft[i][0], imageImag = fft[i][1];
     829        float kernelReal = kernelFFT[i][0], kernelImag = kernelFFT[i][1];
     830        fft[i][0] = imageReal * kernelReal - imageImag * kernelImag;
     831        fft[i][1] = imageImag * kernelReal + imageReal * kernelImag;
     832#endif
     833    }
     834
     835    // Do the backward FFT
     836    FFTW_LOCK;
     837    fftwf_plan backward = fftwf_plan_dft_c2r_2d(paddedRows, paddedCols, fft, data, FFTW_PLAN_RIGOR);
     838    FFTW_UNLOCK;
     839
     840    fftwf_execute(backward);
     841
     842    FFTW_LOCK;
     843    fftwf_destroy_plan(backward);
     844    fftwf_free(fft);
     845    FFTW_UNLOCK;
     846
     847    // Copy into the target, without the padding
     848    out = psImageRecycle(out, numCols, numRows, PS_TYPE_F32);
     849    psF32 **outData = out->data.F32;    // Pointer into output
     850    dataPtr = data;                     // Reset to start
     851    for (int y = 0; y < numRows; y++, outData++, dataPtr += paddedCols) {
     852        memcpy(*outData, dataPtr, goodBytes);
     853    }
     854
     855    FFTW_LOCK;
     856    fftwf_free(data);
     857    FFTW_UNLOCK;
     858
     859    return out;
     860}
  • branches/sc_branches/trunkTest/psLib/src/fft/psImageFFT.h

    r21183 r29060  
    1515#include "psImage.h"
    1616#include "psImageConvolve.h"
     17
     18typedef struct {
     19    void *fft;
     20    int xMin;                          ///< Most negative x index
     21    int yMin;                          ///< Most negative y index
     22    int xMax;                          ///< Most positive x index
     23    int yMax;                          ///< Most positive y index
     24    int paddedCols;
     25    int paddedRows;
     26} psKernelFFT;
    1727
    1828/// @addtogroup MathOps Mathematical Operations
     
    7181    const psImage *mask,                ///< Corresponding mask
    7282    psImageMaskType maskVal,            ///< Value to mask
    73     const psKernel *kernel              ///< kernel to colvolve with
     83    const psKernel *kernel              ///< kernel to convolve with
     84    );
     85
     86/// Allocate an the psKernelFFT structure
     87psKernelFFT *psKernelFFTAlloc(
     88    const psKernel *input               ///< kernel to convolve with
    7489);
     90
     91/// Generate an FFT'ed kernel suitable for convolution with the given image
     92psKernelFFT *psImageConvolveKernelInit(
     93    const psImage *in,                  ///< representative image to convolve
     94    const psKernel *kernel              ///< kernel to convolve with
     95    );
     96
     97/// Convolve the given image with the pre-FFT'ed kernel
     98psImage *psImageConvolveKernel(
     99    psImage *out,                       ///< Output image, or NULL
     100    const psImage *in,                  ///< Image to convolve
     101    const psImage *mask,                ///< Corresponding mask
     102    psImageMaskType maskVal,            ///< Value to mask
     103    const psKernelFFT *kernel           ///< kernel to convolve with
     104    );
    75105
    76106/// @}
  • branches/sc_branches/trunkTest/psLib/src/jpeg/psImageJpeg.c

    r27145 r29060  
    77#include <string.h>
    88
     9#include <kapa.h>
    910#include "psMemory.h"
    1011#include "psImage.h"
     
    134135    return psImageJpegColormapSet (map, "greyscale");
    135136}
     137
     138// XXX need to fix library references for this (psLib does not depend on libkapa)
     139# if (0)
     140// XXX Add colormap bar with scale (min -> max)
     141// XXX Add option to plot the source overlay (pass in bDrawBuffer populated with points?)
     142// XXX need to update bDraw APIs to pass in/out structure and avoid the local static
     143bool psImageJpegNew(const psImageJpegColormap *map, const psImage *image, const char *filename,
     144                 float min, float max)
     145{
     146    PS_ASSERT_PTR_NON_NULL(map, false);
     147    PS_ASSERT_IMAGE_NON_NULL(image, false);
     148    PS_ASSERT_IMAGE_TYPE(image, PS_TYPE_F32, false);
     149    PS_ASSERT_VECTOR_NON_NULL(map->red, false);
     150    PS_ASSERT_VECTOR_NON_NULL(map->green, false);
     151    PS_ASSERT_VECTOR_NON_NULL(map->blue, false);
     152    PS_ASSERT_PTR_NON_NULL(filename, false);
     153    PS_ASSERT_INT_POSITIVE(strlen(filename), false);
     154    PS_ASSERT_FLOAT_REAL(min, false);
     155    PS_ASSERT_FLOAT_REAL(max, false);
     156
     157    float zero, scale;
     158    struct jpeg_compress_struct cinfo;
     159    struct jpeg_error_mgr jerr;
     160
     161    long pixel;
     162    JSAMPLE *jpegLine;   // Points to data for current line
     163    JSAMPROW jpegLineList[1];  // pointer to JSAMPLE row[s]
     164    JSAMPLE *jpegImage;
     165    JSAMPLE *outPix;
     166
     167    /* JPEG init calls */
     168    cinfo.err = jpeg_std_error (&jerr);
     169    jpeg_create_compress (&cinfo);
     170
     171    /* open file, prep for jpeg */
     172    FILE *f = fopen(filename, "w");
     173    if (!f) {
     174        psError(PS_ERR_IO, true, "failed to open %s for output\n", filename);
     175        return false;
     176    }
     177    jpeg_stdio_dest(&cinfo, f);
     178
     179    /* set up color jpeg buffers */
     180    int quality = 75;
     181    cinfo.image_width = image->numCols; // image width and height, in pixels
     182    cinfo.image_height = image->numRows;
     183    cinfo.input_components = 3;
     184    cinfo.in_color_space = JCS_RGB;
     185    jpeg_set_defaults (&cinfo);
     186    jpeg_set_quality (&cinfo, quality, true); // limit to baseline-JPEG values
     187    jpeg_start_compress (&cinfo, true);
     188
     189    psU8 *Rpix = map->red->data.U8;
     190    psU8 *Gpix = map->green->data.U8;
     191    psU8 *Bpix = map->blue->data.U8;
     192
     193    if (max == min) {
     194        zero = min - 0.1;
     195        scale = 256.0/0.2;
     196    } else {
     197        zero = min;
     198        scale = 256.0/(max - min);
     199    }
     200
     201    int dx = image->numCols;
     202    int dy = image->numRows;
     203
     204    // output image buffer and line buffer
     205    jpegLine = psAlloc (3*dx*sizeof(JSAMPLE));
     206    jpegImage = psAlloc (3*dx*dy*sizeof(JSAMPLE));
     207
     208    // first copy the image data into the output buffer
     209    for (int j = 0; j < dy; j++) {
     210        psF32 *row = image->data.F32[j];
     211
     212        outPix = jpegLine;
     213        for (int i = 0; i < dx; i++, outPix += 3) {
     214            if (isfinite(row[i])) {
     215                pixel = PS_JPEG_SCALEVALUE(row[i],zero,scale);
     216                outPix[0] = Rpix[pixel];
     217                outPix[1] = Gpix[pixel];
     218                outPix[2] = Bpix[pixel];
     219            } else {
     220                // XXX NAN value should be set per-color map
     221                outPix[0] = 0x00;
     222                outPix[1] = 0xff;
     223                outPix[2] = 0x00;
     224            }
     225        }
     226        memcpy (&jpegImage[j*3*dx], jpegLine, 3*dx);
     227    }
     228
     229    bDrawBuffer *bdbuf = bDrawBufferCreate(dx, dy);
     230    bDrawSetBuffer(bdbuf);
     231    bDrawColor red = KapaColorByName("red");
     232    bDrawSetStyle (red, 1, 0);
     233    bDrawCircle(40.0, 20.0, 3.0);
     234
     235    {
     236        int Npalette;
     237        png_color *palette = KapaPNGPalette (&Npalette);
     238        bDrawColor white = KapaColorByName ("white");
     239        for (int j = 0; j < dy; j++) {
     240            for (int i = 0; i < dx; i++) {
     241                bDrawColor color = bdbuf[0].pixels[j][i];
     242                if (color == white) continue;
     243                jpegImage[j*3*dx + 3*i + 0] = palette[color].red;
     244                jpegImage[j*3*dx + 3*i + 1] = palette[color].green;
     245                jpegImage[j*3*dx + 3*i + 2] = palette[color].blue;
     246            }
     247        }
     248    }
     249    bDrawBufferFree (bdbuf);
     250
     251    // write out the image buffer
     252    for (int j = 0; j < image->numRows; j++) {
     253        jpegLineList[0] = &jpegImage[j*3*dx];
     254        if (jpeg_write_scanlines(&cinfo, jpegLineList, 1) == 0) {
     255            psError(PS_ERR_IO, true, "Unable to write line %d to JPEG", j);
     256            psFree(jpegLine);
     257            psFree(jpegImage);
     258            fclose(f);
     259            return false;
     260        }
     261    }
     262
     263    jpeg_finish_compress(&cinfo);
     264    if (fclose(f) == EOF) {
     265        psError(PS_ERR_IO, true, "Failed to close %s", filename);
     266        psFree(jpegLine);
     267        psFree(jpegImage);
     268        return false;
     269    }
     270    jpeg_destroy_compress(&cinfo);
     271
     272    psFree(jpegLine);
     273    psFree(jpegImage);
     274    return true;
     275}
     276# endif
    136277
    137278bool psImageJpeg(const psImageJpegColormap *map, const psImage *image, const char *filename,
  • branches/sc_branches/trunkTest/psLib/src/math/psMinimizeLMM.c

    r26951 r29060  
    441441    }
    442442
     443    // number of degrees of freedom for this fit
     444    int nDOF = dy->n - params->n;
     445
    443446    // calculate initial alpha and beta, set chisq (min->value)
    444447    min->value = psMinLM_SetABX(alpha, beta, params, paramMask, x, y, dy, func);
     
    456459    }
    457460
    458     // iterate until the tolerance is reached, or give up
    459     while ((min->iter < min->maxIter) && ((min->lastDelta > min->tol) || !isfinite(min->lastDelta))) {
     461    // iterate until: (a) nIter = min->iter or (b) (chisq / ndof) < maxChisq and deltaChisq < minTol (but don't stop unless Chisq is finite)
     462    bool done = (min->iter >= min->maxIter);
     463    while (!done) {
    460464        psTrace("psLib.math", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
    461         psTrace("psLib.math", 5, "Last delta is %f.  Min->tol is %f.\n", min->lastDelta, min->tol);
     465        psTrace("psLib.math", 5, "Last delta is %f.  stop if < %f, accept if < %f\n", min->lastDelta, min->minTol, min->maxTol);
    462466
    463467        // set a new guess for Alpha, Beta, Params
    464468        if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, checkLimits, lambda, &dLinear)) {
    465469            min->iter ++;
     470            if (min->iter >=  min->maxIter) break;
    466471            lambda *= 10.0;
    467472            continue;
     
    482487        if (isnan(Chisq)) {
    483488            min->iter ++;
     489            if (min->iter >= min->maxIter) break;
    484490            lambda *= 10.0;
    485491            continue;
     
    492498        psF32 rho = (min->value - Chisq) / dLinear;
    493499
    494         psTrace("psLib.math", 5, "last chisq: %f, new chisq %f, delta: %f, dLinear: %f, rho: %f, lambda: %f\n", min->value,
    495                 Chisq, min->lastDelta, dLinear, rho, lambda);
    496 
    497         psTrace("psLib.math.dLinear", 5, "last chisq: %f, new chisq %f, delta: %f, dLinear: %f, rho: %f, lambda: %f\n", min->value,
    498                 Chisq, min->lastDelta, dLinear, rho, lambda);
     500        psTrace("psLib.math", 5, "last chisq: %f, new chisq %f, delta: %f, dLinear: %f, rho: %f, lambda: %f, nDOF: %d\n", min->value, Chisq, min->lastDelta, dLinear, rho, lambda, nDOF);
     501
     502        psTrace("psLib.math.dLinear", 5, "last chisq: %f, new chisq %f, delta: %f, dLinear: %f, rho: %f, lambda: %f\n", min->value, Chisq, min->lastDelta, dLinear, rho, lambda);
    499503
    500504        // dump some useful info if trace is defined
     
    506510        /* rho is positive if the new chisq is smaller; allow for some insignificant change (slight negative rho) */
    507511        if (rho >= -1e-6) {
    508             min->lastDelta = (min->value - Chisq) / (dy->n - params->n);
     512            min->lastDelta = (min->value - Chisq) / nDOF;
    509513            min->value = Chisq;
    510514            alpha  = psImageCopy(alpha, Alpha, PS_TYPE_F32);
     
    516520        }
    517521        min->iter++;
     522
     523        done = (min->iter >= min->maxIter);
     524       
     525        // check for convergence:
     526        float chisqDOF = Chisq / nDOF;
     527        if (!isfinite(min->maxChisqDOF) || ((chisqDOF < min->maxChisqDOF) && isfinite(min->lastDelta))) {
     528            done |= (min->lastDelta < min->minTol);
     529        }
    518530    }
    519531    psTrace("psLib.math", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);
     
    549561        psFree(dy);
    550562    }
    551     if (min->iter == min->maxIter) {
    552         psTrace("psLib.math", 3, "---- end (false) ----\n");
    553         return(false);
    554     }
    555     psTrace("psLib.math", 3, "---- end (true) ----\n");
    556     return(true);
     563
     564    // if the last improvement was at least as good as maxTol, accept the fit:
     565    if (min->lastDelta <= min->maxTol) {
     566        psTrace("psLib.math", 6, "---- end (true) ----\n");
     567        return(true);
     568    }
     569    psTrace("psLib.math", 6, "---- end (false) ----\n");
     570    return(false);
    557571}
    558572
     
    588602}
    589603
    590 psMinimization *psMinimizationAlloc(int maxIter,
    591                                     float tol)
     604psMinimization *psMinimizationAlloc(int maxIter, float minTol, float maxTol)
    592605{
    593606    PS_ASSERT_INT_NONNEGATIVE(maxIter, NULL);
     
    595608    psMinimization *min = psAlloc(sizeof(psMinimization));
    596609    psMemSetDeallocator(min, (psFreeFunc)minimizationFree);
     610
    597611    P_PSMINIMIZATION_SET_MAXITER(min,maxIter);
    598     P_PSMINIMIZATION_SET_TOL(min,tol);
     612    P_PSMINIMIZATION_SET_MIN_TOL(min,minTol);
     613    P_PSMINIMIZATION_SET_MAX_TOL(min,maxTol);
     614
    599615    min->value = 0.0;
    600616    min->iter = 0;
    601617    min->lastDelta = NAN;
     618    min->maxChisqDOF = NAN;
    602619
    603620    return(min);
  • branches/sc_branches/trunkTest/psLib/src/math/psMinimizeLMM.h

    r23486 r29060  
    3333#define PS_MAX_LMM_ITERATIONS 100
    3434#define PS_MAX_MINIMIZE_ITERATIONS 100
    35 #define P_PSMINIMIZATION_SET_MAXITER(m,val) *(int*)&m->maxIter = val
    36         #define P_PSMINIMIZATION_SET_TOL(m,val) *(float*)&m->tol = val
     35#define P_PSMINIMIZATION_SET_MAXITER(m,val) { *(int*)&m->maxIter  = val; }
     36#define P_PSMINIMIZATION_SET_MIN_TOL(m,val) { *(float*)&m->minTol = val; }
     37#define P_PSMINIMIZATION_SET_MAX_TOL(m,val) { *(float*)&m->maxTol = val; }
    3738
    3839                typedef enum {
     
    7576typedef struct
    7677{
    77     const int maxIter;                 ///< Convergence limit
    78     const float tol;                   ///< Error Tolerance
     78    const int maxIter;                  ///< Convergence limit
     79    const float minTol;                 ///< Convergence Tolerance (stop if we reach this value)
     80    const float maxTol;                 ///< Max Tolerance (accept fit if last improvement was this good)
    7981    float value;                       ///< Value of function at minimum
    8082    int iter;                          ///< Number of iterations to date
    8183    float lastDelta;                   ///< The last difference for the fit
     84    float maxChisqDOF;                 ///< for Chisq minimization, require that we reach here before checking tolerance
    8285}
    8386psMinimization;
     
    102105psMinimization *psMinimizationAlloc(
    103106    int maxIter,                       ///< Number of minimization iterations to perform.
    104     float tol                          ///< Requested error tolerance
     107    float minTol,                      ///< stop if tolerance is less than this
     108    float maxTol                       ///< accept fit if tolerance is less than this
    105109) PS_ATTR_MALLOC;
    106110
  • branches/sc_branches/trunkTest/psLib/src/math/psMinimizePowell.c

    r28635 r29060  
    457457
    458458        mul = bracket->data.F32[1];
    459         if ((fabs(a-b) < min->tol) && (fabs(b-c) < min->tol)) {
     459        if ((fabs(a-b) < min->minTol) && (fabs(b-c) < min->minTol)) {
    460460            PS_VECTOR_ADD_MULTIPLE(params, paramMask, line, params, mul);
    461461            min->value = func(params, coords);
     
    524524    psTrace("psLib.math", 4, "---- psMinimizePowell() begin ----\n");
    525525    psTrace("psLib.math", 6, "min->maxIter is %d\n", min->maxIter);
    526     psTrace("psLib.math", 6, "min->tol is %f\n", min->tol);
     526    psTrace("psLib.math", 6, "min->minTol is %f\n", min->minTol);
    527527
    528528    if (paramMask == NULL) {
     
    573573        for (i=0;i<numDims;i++) {
    574574            if (myParamMask->data.PS_TYPE_VECTOR_MASK_DATA[i] == 0) {
     575
    575576                P_PSMINIMIZATION_SET_MAXITER((&dummyMin),PS_MINIMIZE_POWELL_LINEMIN_MAX_ITERATIONS);
    576                 *(float*)&dummyMin.tol = PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE;
     577                P_PSMINIMIZATION_SET_MIN_TOL((&dummyMin),PS_MINIMIZE_POWELL_LINEMIN_ERROR_TOLERANCE);
     578
    577579                mul = LineMin(&dummyMin, Q, ((psVector *) v->data[i]),
    578580                              myParamMask, coords, func);
     
    677679
    678680        // 8: Go to step 3 until the change is less than some tolerance.
    679         if (fabs(baseFuncVal - currFuncVal) <= min->tol) {
     681        if (fabs(baseFuncVal - currFuncVal) <= min->minTol) {
    680682            psFree(v);
    681683            psFree(pQP);
  • branches/sc_branches/trunkTest/psLib/src/math/psStats.c

    r27334 r29060  
    12111211
    12121212        // Fit a Gaussian to the data.
    1213         psMinimization *minimizer = psMinimizationAlloc(100, 0.01); // The minimizer information
     1213        psMinimization *minimizer = psMinimizationAlloc(100, 0.01, 1.0); // The minimizer information
    12141214        psVector *params = psVectorAlloc(2, PS_TYPE_F32); // Parameters for the Gaussian
    12151215        // Initial guess for the mean (index 0) and var (index 1).
  • branches/sc_branches/trunkTest/psLib/test/math/Makefile.am

    r24321 r29060  
    4949        tap_psMinimizePowell \
    5050        tap_psSpline1D \
    51         tap_psPolynomialMD
     51        tap_psPolynomialMD \
     52        tap_psPolynomialMD_sampleDark
    5253
    5354#       tap_psRandom
  • branches/sc_branches/trunkTest/psModules

    • Property svn:mergeinfo deleted
  • branches/sc_branches/trunkTest/psModules/src/camera/pmFPAfileIO.c

    r28340 r29060  
    2424#include "pmFPAfileFitsIO.h"
    2525#include "pmFPAfileFringeIO.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
    2630#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
    2732#include "pmFootprint.h"
    2833#include "pmPeaks.h"
    2934#include "pmMoments.h"
    30 #include "pmResiduals.h"
    31 #include "pmGrowthCurve.h"
    32 #include "pmTrend2D.h"
     35#include "pmModelFuncs.h"
     36#include "pmModel.h"
     37#include "pmSourceMasks.h"
     38#include "pmSourceExtendedPars.h"
     39#include "pmSourceDiffStats.h"
     40#include "pmSource.h"
     41#include "pmSourceFitModel.h"
    3342#include "pmPSF.h"
    34 #include "pmModel.h"
    35 #include "pmSource.h"
     43#include "pmPSFtry.h"
     44
    3645#include "pmSourceIO.h"
    37 #include "pmResiduals.h"
    3846#include "pmPSF_IO.h"
     47
    3948#include "pmAstrometryModel.h"
    4049#include "pmAstrometryRefstars.h"
  • branches/sc_branches/trunkTest/psModules/src/camera/pmReadoutFake.c

    r28405 r29060  
    1010#include "pmFPA.h"
    1111
    12 #include "pmMoments.h"
     12
     13#include "pmTrend2D.h"
    1314#include "pmResiduals.h"
    1415#include "pmGrowthCurve.h"
    15 #include "pmTrend2D.h"
    16 #include "pmPSF.h"
    17 #include "pmModel.h"
    18 #include "pmModelClass.h"
    1916#include "pmSpan.h"
     17#include "pmFootprintSpans.h"
    2018#include "pmFootprint.h"
    2119#include "pmPeaks.h"
     20#include "pmMoments.h"
     21#include "pmModelFuncs.h"
     22#include "pmModel.h"
     23#include "pmModelUtils.h"
     24#include "pmModelClass.h"
     25#include "pmSourceMasks.h"
     26#include "pmSourceExtendedPars.h"
     27#include "pmSourceDiffStats.h"
    2228#include "pmSource.h"
    23 #include "pmSourceUtils.h"
    24 #include "pmModelUtils.h"
     29#include "pmSourceFitModel.h"
     30#include "pmPSF.h"
     31#include "pmPSFtry.h"
     32
    2533#include "pmSourceGroups.h"
    26 
    2734#include "pmReadoutFake.h"
    2835
  • branches/sc_branches/trunkTest/psModules/src/camera/pmReadoutFake.h

    r26450 r29060  
    22#define PM_READOUT_FAKE_H
    33
    4 #include <pslib.h>
    5 #include <pmHDU.h>
    6 #include <pmFPA.h>
    7 
    8 #include <pmMoments.h>
    9 #include <pmResiduals.h>
    10 #include <pmGrowthCurve.h>
    11 #include <pmTrend2D.h>
    12 #include <pmPSF.h>
    13 #include <pmSourceMasks.h>
     4// #include <pslib.h>
     5// #include <pmHDU.h>
     6// #include <pmFPA.h>
     7//
     8// #include <pmMoments.h>
     9// #include <pmResiduals.h>
     10// #include <pmGrowthCurve.h>
     11// #include <pmTrend2D.h>
     12// #include <pmPSF.h>
     13// #include <pmSourceMasks.h>
    1414
    1515/// Set threading
  • branches/sc_branches/trunkTest/psModules/src/config/pmConfig.c

    r28287 r29060  
    3232
    3333#include "pmConfig.h"
     34#include "pmVisualUtils.h"
    3435
    3536#ifdef HAVE_NEBCLIENT
     
    638639        psArgumentVerbosity(argc, argv);
    639640        // XXX: substitute the string for the default log level for "2".
     641    }
     642
     643    // Set the visualization levels
     644    // argument format is: -visual (facil) (level)
     645    while ((argNum = psArgumentGet(*argc, argv, "-visual"))) {
     646        if ( (*argc < argNum + 3) ) {
     647            psError(PS_ERR_IO, true, "-visual switch specified without facility and level.");
     648            return NULL;
     649        }
     650        psArgumentRemove(argNum, argc, argv);
     651        pmVisualSetLevel(argv[argNum], atoi(argv[argNum+1]));
     652        psArgumentRemove(argNum, argc, argv);
     653        psArgumentRemove(argNum, argc, argv);
     654    }
     655    if ((argNum = psArgumentGet(*argc, argv, "-visual-all"))) {
     656        pmVisualSetLevel(".", 10);
     657        psArgumentRemove(argNum, argc, argv);
     658    }
     659    if ((argNum = psArgumentGet(*argc, argv, "-visual-levels"))) {
     660        pmVisualPrintLevels(stdout);
     661        psArgumentRemove(argNum, argc, argv);
    640662    }
    641663
  • branches/sc_branches/trunkTest/psModules/src/detrend/pmMaskStats.c

    r28100 r29060  
    77
    88#include <pslib.h>
    9 #include <psmodules.h>
    109
    1110#include "pmHDU.h"
     
    1615#include "pmFPAAstrometry.h"
    1716
     17#include "pmMaskStats.h"
    1818
    1919#define ESCAPE { \
  • branches/sc_branches/trunkTest/psModules/src/detrend/pmShutterCorrection.c

    r28405 r29060  
    302302    PS_ASSERT_PTR_NON_NULL(guess, NULL);
    303303
    304     psMinimization *minInfo = psMinimizationAlloc(15, 0.1); // Minimization information
     304    psMinimization *minInfo = psMinimizationAlloc(15, 0.1, 1.0); // Minimization information
    305305
    306306    psVector *params = psVectorAlloc (3, PS_TYPE_F32); // Fitting parameters
  • branches/sc_branches/trunkTest/psModules/src/extras/Makefile.am

    r27750 r29060  
    99        pmKapaPlots.c \
    1010        pmVisual.c \
     11        pmVisualUtils.c \
    1112        ippStages.c
    1213
     
    1718        pmKapaPlots.h \
    1819        pmVisual.h \
     20        pmVisualUtils.h \
    1921        ippDiffMode.h \
    2022        ippStages.h
  • branches/sc_branches/trunkTest/psModules/src/extras/pmVisual.c

    r28129 r29060  
    2121#include "pmAstrometryObjects.h"
    2222#include "pmSubtractionStamps.h"
     23
    2324#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"
    2439#include "pmPSF.h"
    2540#include "pmPSFtry.h"
    26 #include "pmSource.h"
     41
    2742#include "pmFPAExtent.h"
    28 
    29 # if (HAVE_KAPA)
    30 # include <kapa.h>
    31 # include "pmVisual.h"
    32 # include "pmKapaPlots.h"
    33 # define KAPAX 700
    34 # define KAPAY 700
    3543
    3644#include "pmAstrometryVisual.h"
     
    3846#include "pmStackVisual.h"
    3947#include "pmSourceVisual.h"
     48
     49# if (HAVE_KAPA)
     50# include <kapa.h>
     51#include "pmVisual.h"
     52#include "pmKapaPlots.h"
     53
     54# define KAPAX 700
     55# define KAPAY 700
    4056
    4157//#define TESTING
     
    102118
    103119    /* Wait up to 1.0 second for a response, then continue */
    104     timeout.tv_sec = 2;
     120    timeout.tv_sec = 10;
    105121    timeout.tv_usec = 0;
    106122
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmPSFEnvelope.c

    r28332 r29060  
    1010#include "pmHDU.h"
    1111#include "pmFPA.h"
    12 #include "pmReadoutFake.h"
    13 
    14 #include "pmMoments.h"
     12
     13#include "pmTrend2D.h"
    1514#include "pmResiduals.h"
    1615#include "pmGrowthCurve.h"
    17 #include "pmTrend2D.h"
    18 #include "pmPSF.h"
    19 #include "pmModel.h"
    20 #include "pmModelClass.h"
    21 #include "pmModelUtils.h"
    2216#include "pmSpan.h"
     17#include "pmFootprintSpans.h"
    2318#include "pmFootprint.h"
    2419#include "pmPeaks.h"
     20#include "pmMoments.h"
     21#include "pmModelFuncs.h"
     22#include "pmModel.h"
     23#include "pmModelUtils.h"
     24#include "pmModelClass.h"
     25#include "pmSourceMasks.h"
     26#include "pmSourceExtendedPars.h"
     27#include "pmSourceDiffStats.h"
    2528#include "pmSource.h"
    26 #include "pmSourceUtils.h"
    2729#include "pmSourceFitModel.h"
     30#include "pmPSF.h"
    2831#include "pmPSFtry.h"
    2932
    30 
     33#include "pmReadoutFake.h"
    3134#include "pmPSFEnvelope.h"
    3235#include "pmStackVisual.h"
     
    410413    options->chiFluxTrend = false;      // All sources have similar flux, so fitting a trend often fails
    411414
    412     pmSourceFitModelInit(SOURCE_FIT_ITERATIONS, 0.01, VARIANCE_VAL, true);
     415    // options which modify the behavior of the model fitting
     416    options->fitOptions                = pmSourceFitOptionsAlloc();
     417    options->fitOptions->nIter         = SOURCE_FIT_ITERATIONS;
     418    options->fitOptions->minTol        = 0.01;
     419    options->fitOptions->maxTol        = 1.00;
     420    options->fitOptions->poissonErrors = true;
     421    options->fitOptions->weight        = VARIANCE_VAL;
     422    options->fitOptions->mode          = PM_SOURCE_FIT_PSF;
     423
    413424    pmModelClassSetLimits(PM_MODEL_LIMITS_STRICT); // Important for getting a good stack target PSF
    414425
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtraction.c

    r28667 r29060  
    3333#define USE_KERNEL_ERR                  // Use kernel error image?
    3434#define NUM_COVAR_POS 5                 // Number of positions for covariance calculation
     35
     36// XXX we need to pass these fwhm values elsewhere.  These should go on one of the structure, but
     37// things are too confusing to do that now.  just save them here.
     38static float FWHM1 = NAN;
     39static float FWHM2 = NAN;
    3540
    3641//////////////////////////////////////////////////////////////////////////////////////////////////////////////
     
    752757
    753758
    754 bool pmSubtractionConvolveStamp(pmSubtractionStamp *stamp, const pmSubtractionKernels *kernels, int footprint)
     759bool pmSubtractionConvolveStamp (pmSubtractionStamp *stamp, pmSubtractionKernels *kernels, int footprint)
    755760{
    756761    PS_ASSERT_PTR_NON_NULL(stamp, false);
     
    774779        stamp->convolutions1 = convolveStamp(stamp->convolutions1, stamp->image1, kernels, footprint);
    775780        stamp->convolutions2 = convolveStamp(stamp->convolutions2, stamp->image2, kernels, footprint);
     781        if (!pmSubtractionKernelPenaltiesStamp(stamp, kernels)) {
     782            psAbort("failure in penalties");
     783        }
    776784        break;
    777785      default:
     
    12151223    bool threaded = pmSubtractionThreaded(); // Running threaded?
    12161224
     1225    // XXX This is no longer used
    12171226    psImage *convMask = NULL;           // Convolved mask image (common to inputs 1 and 2)
    12181227    if (subMask) {
     
    14131422    return true;
    14141423}
     1424
     1425bool pmSubtractionGetFWHMs(float *fwhm1, float *fwhm2) {
     1426
     1427  *fwhm1 = FWHM1;
     1428  *fwhm2 = FWHM2;
     1429  return true;
     1430}
     1431
     1432bool pmSubtractionSetFWHMs(float fwhm1, float fwhm2) {
     1433
     1434  FWHM1 = fwhm1;
     1435  FWHM2 = fwhm2;
     1436  return true;
     1437}
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtraction.h

    r26893 r29060  
    5959/// Convolve the reference stamp with the kernel components
    6060bool pmSubtractionConvolveStamp(pmSubtractionStamp *stamp, ///< Stamp to convolve
    61                                 const pmSubtractionKernels *kernels, ///< Kernel parameters
     61                                pmSubtractionKernels *kernels, ///< Kernel parameters
    6262                                int footprint ///< Half-size of region over which to calculate equation
    6363    );
     
    157157    );
    158158
     159bool pmSubtractionGetFWHMs(float *fwhm1, float *fwhm2);
     160bool pmSubtractionSetFWHMs(float fwhm1, float fwhm2);
     161
    159162/// @}
    160163#endif
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionEquation.c

    r28405 r29060  
    3838                                  const psImage *polyValues, // Spatial polynomial values
    3939                                  int footprint, // (Half-)Size of stamp
    40                                   int normWindow, // Window (half-)size for normalisation measurement
     40                                  int normWindow1, // Window (half-)size for normalisation measurement
     41                                  int normWindow2, // Window (half-)size for normalisation measurement
    4142                                  const pmSubtractionEquationCalculationMode mode
    4243                                  )
     
    184185            double one = 1.0;
    185186
    186             if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow)) {
     187            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow1)) {
    187188                normI1 += ref;
     189            }
     190            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow2)) {
    188191                normI2 += in;
    189192            }
     
    214217
    215218    *norm = normI2 / normI1;
     219
     220    fprintf (stderr, "normValue: %f %f %f\n", normI1, normI2, *norm);
    216221
    217222    if (mode & PM_SUBTRACTION_EQUATION_NORM) {
     
    262267                                      const psImage *polyValues, // Spatial polynomial values
    263268                                      int footprint, // (Half-)Size of stamp
    264                                       int normWindow, // Window (half-)size for normalisation measurement
     269                                      int normWindow1, // Window (half-)size for normalisation measurement
     270                                      int normWindow2, // Window (half-)size for normalisation measurement
    265271                                      const pmSubtractionEquationCalculationMode mode
    266272                                      )
     
    492498            double i1i2 = i1 * i2;
    493499
    494             if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow)) {
     500            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow1)) {
    495501                normI1 += i1;
     502            }
     503            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(normWindow2)) {
    496504                normI2 += i2;
    497505            }
     
    522530
    523531    *norm = normI2 / normI1;
     532    fprintf (stderr, "normValue: %f %f %f\n", normI1, normI2, *norm);
    524533
    525534    if (mode & PM_SUBTRACTION_EQUATION_NORM) {
     
    559568// Add in penalty term to least-squares vector
    560569bool calculatePenalty(psImage *matrix,                     // Matrix to which to add in penalty term
    561                              psVector *vector,                    // Vector to which to add in penalty term
    562                              const pmSubtractionKernels *kernels, // Kernel parameters
    563                              float norm                           // Normalisation
    564     )
     570                      psVector *vector,                    // Vector to which to add in penalty term
     571                      const pmSubtractionKernels *kernels, // Kernel parameters
     572                      float norm                           // Normalisation
     573  )
    565574{
    566575    if (kernels->penalty == 0.0) {
     
    568577    }
    569578
    570     psVector *penalties = kernels->penalties; // Penalties for each kernel component
     579    psVector *penalties1 = kernels->penalties1; // Penalties for each kernel component (input)
     580    psVector *penalties2 = kernels->penalties2; // Penalties for each kernel component (ref)
     581
    571582    int spatialOrder = kernels->spatialOrder; // Order of spatial variations
    572583    int numKernels = kernels->num; // Number of kernel components
     
    588599            for (int xOrder = 0; xOrder <= spatialOrder - yOrder; xOrder++, index += numKernels) {
    589600                // Contribution to chi^2: a_i^2 P_i
    590                 psAssert(isfinite(penalties->data.F32[i]), "Invalid penalty");
    591                 matrix->data.F64[index][index] += norm * penalties->data.F32[i];
     601                psAssert(isfinite(penalties1->data.F32[i]), "Invalid penalty");
     602                fprintf (stderr, "penalty: %f + %f (%f * %f)\n", matrix->data.F64[index][index], norm * penalties1->data.F32[i], norm, penalties1->data.F32[i]);
     603                matrix->data.F64[index][index] += norm * penalties1->data.F32[i];
    592604                if (kernels->mode == PM_SUBTRACTION_MODE_DUAL) {
    593                     matrix->data.F64[index + numParams + 2][index + numParams + 2] += norm * penalties->data.F32[i];
     605                    fprintf (stderr, "penalty: (x^%d y^%d fwhm %f) : %f + %f (%f * %f)\n", kernels->u->data.S32[index], kernels->v->data.S32[index], kernels->widths->data.F32[index],
     606                             matrix->data.F64[index + numParams + 2][index + numParams + 2], norm * penalties2->data.F32[i], norm, penalties2->data.F32[i]);
     607                    matrix->data.F64[index + numParams + 2][index + numParams + 2] += norm * penalties2->data.F32[i];                       
    594608                    // matrix[i][i] is ~ (k_i * I_1)(k_i * I_1)
    595609                    // penalties scale with second moments
     
    682696
    683697    pmSubtractionStampList *stamps = job->args->data[0]; // List of stamps
    684     const pmSubtractionKernels *kernels = job->args->data[1]; // Kernels
     698    pmSubtractionKernels *kernels = job->args->data[1]; // Kernels
    685699    int index = PS_SCALAR_VALUE(job->args->data[2], S32); // Stamp index
    686700    pmSubtractionEquationCalculationMode mode  = PS_SCALAR_VALUE(job->args->data[3], S32); // calculation model
     
    689703}
    690704
    691 bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels,
     705bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels,
    692706                                         int index, const pmSubtractionEquationCalculationMode mode)
    693707{
     
    778792        status = calculateMatrixVector(stamp->matrix, stamp->vector, &stamp->norm, stamp->image2, stamp->image1,
    779793                                       weight, window, stamp->convolutions1, kernels,
    780                                        polyValues, footprint, stamps->normWindow, mode);
     794                                       polyValues, footprint, stamps->normWindow1, stamps->normWindow2, mode);
    781795        break;
    782796      case PM_SUBTRACTION_MODE_2:
    783797        status = calculateMatrixVector(stamp->matrix, stamp->vector, &stamp->norm, stamp->image1, stamp->image2,
    784798                                       weight, window, stamp->convolutions2, kernels,
    785                                        polyValues, footprint, stamps->normWindow, mode);
     799                                       polyValues, footprint, stamps->normWindow2, stamps->normWindow1, mode);
    786800        break;
    787801      case PM_SUBTRACTION_MODE_DUAL:
     
    789803                                           stamp->image1, stamp->image2,
    790804                                           weight, window, stamp->convolutions1, stamp->convolutions2,
    791                                            kernels, polyValues, footprint, stamps->normWindow, mode);
     805                                           kernels, polyValues, footprint, stamps->normWindow1, stamps->normWindow2, mode);
    792806        break;
    793807      default:
     
    830844}
    831845
    832 bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, const pmSubtractionKernels *kernels,
     846bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, pmSubtractionKernels *kernels,
    833847                                    const pmSubtractionEquationCalculationMode mode)
    834848{
     
    9961010            }
    9971011
     1012            // double normValue = 1.0;
    9981013            double normValue = stats->robustMedian;
    9991014            // double bgValue = 0.0;
     
    10231038        }
    10241039# endif
     1040
     1041#if (1)
     1042        for (int i = 0; i < solution->n; i++) {
     1043            fprintf(stderr, "Single solution %d: %lf\n", i, solution->data.F64[i]);
     1044        }
     1045#endif
    10251046
    10261047        if (!kernels->solution1) {
     
    10961117
    10971118        int normIndex = PM_SUBTRACTION_INDEX_NORM(kernels); // Index for normalisation
    1098         calculatePenalty(sumMatrix, sumVector, kernels, sumMatrix->data.F64[normIndex][normIndex] / 1000.0);
     1119        calculatePenalty(sumMatrix, sumVector, kernels, sumMatrix->data.F64[normIndex][normIndex] / 100.0);
    10991120#endif
    11001121
     
    11771198
    11781199
    1179 #ifdef TESTING
     1200#if (1)
    11801201        for (int i = 0; i < solution->n; i++) {
    11811202            fprintf(stderr, "Dual solution %d: %lf\n", i, solution->data.F64[i]);
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionEquation.h

    r26893 r29060  
    1919/// Calculate the least-squares equation to match the image quality for a single stamp
    2020bool pmSubtractionCalculateEquationStamp(pmSubtractionStampList *stamps, ///< Stamps
    21                                          const pmSubtractionKernels *kernels, ///< Kernel parameters
     21                                         pmSubtractionKernels *kernels, ///< Kernel parameters
    2222                                         int index, ///< Index of stamp
    2323                                         const pmSubtractionEquationCalculationMode mode
     
    2626/// Calculate the least-squares equation to match the image quality
    2727bool pmSubtractionCalculateEquation(pmSubtractionStampList *stamps, ///< Stamps
    28                                     const pmSubtractionKernels *kernels, ///< Kernel parameters
     28                                    pmSubtractionKernels *kernels, ///< Kernel parameters
    2929                                    const pmSubtractionEquationCalculationMode mode
    3030    );
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionKernels.c

    r27335 r29060  
    2626    psFree(kernels->vStop);
    2727    psFree(kernels->preCalc);
    28     psFree(kernels->penalties);
     28    psFree(kernels->penalties1);
     29    psFree(kernels->penalties2);
    2930    psFree(kernels->solution1);
    3031    psFree(kernels->solution2);
     
    140141    kernels->v = psVectorRealloc(kernels->v, start + numNew);
    141142    kernels->preCalc = psArrayRealloc(kernels->preCalc, start + numNew);
    142     kernels->penalties = psVectorRealloc(kernels->penalties, start + numNew);
     143
     144    kernels->penalties1 = psVectorRealloc(kernels->penalties1, start + numNew);
     145    kernels->penalties2 = psVectorRealloc(kernels->penalties2, start + numNew);
     146
    143147    kernels->inner = start;
    144148    kernels->num += numNew;
     
    156160            kernels->v->data.S32[index] = v;
    157161            kernels->preCalc->data[index] = NULL;
    158             kernels->penalties->data.F32[index] = kernels->penalty * PS_SQR(PS_SQR(u) + PS_SQR(v));
    159             psAssert (isfinite(kernels->penalties->data.F32[index]), "invalid penalty");
     162
     163            // XXX this needs to be changed to use the *convolved* second moment
     164            kernels->penalties1->data.F32[index] = kernels->penalty * PS_SQR(PS_SQR(u) + PS_SQR(v));
     165            psAssert (isfinite(kernels->penalties1->data.F32[index]), "invalid penalty");
     166
     167            kernels->penalties2->data.F32[index] = kernels->penalty * PS_SQR(PS_SQR(u) + PS_SQR(v));
     168            psAssert (isfinite(kernels->penalties2->data.F32[index]), "invalid penalty");
     169
    160170            psTrace("psModules.imcombine", 7, "Kernel %d: %d %d\n", index, u, v);
    161171        }
     
    166176    kernels->v->n = start + numNew;
    167177    kernels->preCalc->n = start + numNew;
    168     kernels->penalties->n = start + numNew;
     178
     179    kernels->penalties1->n = start + numNew;
     180    kernels->penalties2->n = start + numNew;
    169181
    170182    return true;
    171183}
    172184
    173 bool pmSubtractionKernelPreCalcNormalize(pmSubtractionKernels *kernels, pmSubtractionKernelPreCalc *preCalc,
    174                                          int index, int size, int uOrder, int vOrder, float fwhm,
    175                                          bool AlardLuptonStyle, bool forceZeroNull)
     185static bool pmSubtractionKernelPreCalcNormalize(pmSubtractionKernels *kernels, pmSubtractionKernelPreCalc *preCalc,
     186                                                int index, int uOrder, int vOrder, float fwhm,
     187                                                bool AlardLuptonStyle, bool forceZeroNull)
    176188{
    177189    // we have 4 cases here:
     
    182194
    183195    // Calculate moments
    184     double penalty = 0.0;                   // Moment, for penalty
    185196    double sum = 0.0, sum2 = 0.0;           // Sum of kernel component
    186197    float min = INFINITY, max = -INFINITY;  // Minimum and maximum kernel value
    187     for (int v = -size; v <= size; v++) {
    188         for (int u = -size; u <= size; u++) {
     198
     199    for (int v = preCalc->kernel->yMin; v <= preCalc->kernel->yMax; v++) {
     200        for (int u = preCalc->kernel->xMin; u <= preCalc->kernel->xMax; u++) {
    189201            double value = preCalc->kernel->kernel[v][u];
    190202            double value2 = PS_SQR(value);
    191203            sum += value;
    192204            sum2 += value2;
    193             penalty += value2 * PS_SQR((PS_SQR(u) + PS_SQR(v)));
    194205            min = PS_MIN(value, min);
    195206            max = PS_MAX(value, max);
     
    198209
    199210#if 0
    200     fprintf(stderr, "%d raw: %lf, null: %f, min: %lf, max: %lf, moment: %lf\n", index, sum, preCalc->kernel->kernel[0][0], min, max, penalty);
     211    fprintf(stderr, "%d raw: %lf, null: %f, min: %lf, max: %lf\n", index, sum, preCalc->kernel->kernel[0][0], min, max);
    201212#endif
    202213
     
    207218        if (uOrder % 2 == 0 && vOrder % 2 == 0) {
    208219            // Even functions: normalise to unit sum and subtract null pixel so that sum is zero
    209             scale2D = 1.0 / fabs(sum);
     220            // Re-normalize
     221            // scale2D  = 1.0 / fabs(sum);
     222            scale2D  = 1.0 / sqrt(sum2);
    210223            zeroNull = true;
    211224        } else {
     
    239252
    240253    psBinaryOp(preCalc->kernel->image, preCalc->kernel->image, "*", psScalarAlloc(scale2D, PS_TYPE_F32));
    241     penalty *= 1.0 / sum2;
    242254
    243255    if (zeroNull) {
    244         preCalc->kernel->kernel[0][0] -= 1.0;
    245     }
    246 
    247 #if 0
     256        // preCalc->kernel->kernel[0][0] -= 1.0;
     257        preCalc->kernel->kernel[0][0] -= sum / sqrt (sum2);
     258    }
     259
     260#if 1
    248261    {
    249         double sum = 0.0;   // Sum of kernel component
     262        double Sum = 0.0;   // Sum of kernel component
     263        double Sum2 = 0.0;   // Sum of kernel component
    250264        float min = INFINITY, max = -INFINITY;  // Minimum and maximum kernel value
    251         for (int v = -size; v <= size; v++) {
    252             for (int u = -size; u <= size; u++) {
    253                 sum += preCalc->kernel->kernel[v][u];
     265        for (int v = preCalc->kernel->yMin; v <= preCalc->kernel->yMax; v++) {
     266            for (int u = preCalc->kernel->xMin; u <= preCalc->kernel->xMax; u++) {
     267                double value = preCalc->kernel->kernel[v][u];
     268                Sum += value;
     269                Sum2 += PS_SQR(value);
    254270                min = PS_MIN(preCalc->kernel->kernel[v][u], min);
    255271                max = PS_MAX(preCalc->kernel->kernel[v][u], max);
    256272            }
    257273        }
    258         fprintf(stderr, "%d mod: %lf, null: %f, min: %lf, max: %lf, scale: %f\n", index, sum, preCalc->kernel->kernel[0][0], min, max, scale2D);
     274        fprintf(stderr, "%d sum: %lf, sum2: %lf, null: %f, min: %lf, max: %lf, scale: %f\n", index, Sum, Sum2, preCalc->kernel->kernel[0][0], min, max, scale2D);
    259275    }
    260276#endif
     
    267283    }
    268284    kernels->preCalc->data[index] = preCalc;
    269     kernels->penalties->data.F32[index] = kernels->penalty * penalty;
    270     psAssert (isfinite(kernels->penalties->data.F32[index]), "invalid penalty");
    271     psTrace("psModules.imcombine", 7, "Kernel %d: %f %d %d %f\n", index, fwhm, uOrder, vOrder, penalty);
     285    psTrace("psModules.imcombine", 7, "Kernel %d: %f %d %d\n", index, fwhm, uOrder, vOrder);
    272286
    273287    return true;
     
    321335
    322336                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_ISIS, uOrder, vOrder, size, sigma); // structure to hold precalculated values
    323                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true, false);
    324                 // pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], false, false);
     337                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, uOrder, vOrder, fwhms->data.F32[i], true, false);
     338                // pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, uOrder, vOrder, fwhms->data.F32[i], false, false);
    325339            }
    326340        }
     
    379393            for (int vOrder = 0; vOrder <= orders->data.S32[i] - uOrder; vOrder++, index++) {
    380394                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_ISIS, uOrder, vOrder, size, sigma); // structure to hold precalculated values
    381                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true, false);
     395                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, uOrder, vOrder, fwhms->data.F32[i], true, false);
    382396            }
    383397        }
     
    385399            // XXX modify size for hermitians to account for sqrt(2) in Hermitian definition (relative to ISIS Gaussian)
    386400            pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_ISIS_RADIAL, order, order, size, sigma / sqrt(2.0)); // structure to hold precalculated values
    387             pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, order, order, fwhms->data.F32[i], true, true);
     401            pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, order, order, fwhms->data.F32[i], true, true);
    388402        }
    389403    }
     
    437451            for (int vOrder = 0; vOrder <= orders->data.S32[i] - uOrder; vOrder++, index++) {
    438452                pmSubtractionKernelPreCalc *preCalc = pmSubtractionKernelPreCalcAlloc(PM_SUBTRACTION_KERNEL_HERM, uOrder, vOrder, size, sigma); // structure to hold precalculated values
    439                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true, false);
     453                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, uOrder, vOrder, fwhms->data.F32[i], true, false);
    440454            }
    441455        }
     
    506520
    507521                // XXX do we use Alard-Lupton normalization (last param true) or not?
    508                 pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, size, uOrder, vOrder, fwhms->data.F32[i], true, false);
     522                pmSubtractionKernelPreCalcNormalize (kernels, preCalc, index, uOrder, vOrder, fwhms->data.F32[i], true, false);
    509523
    510524                // XXXX test demo that deconvolved kernel is valid
     
    572586    kernels->preCalc = psArrayAlloc(numBasisFunctions);
    573587    kernels->penalty = penalty;
    574     kernels->penalties = psVectorAlloc(numBasisFunctions, PS_TYPE_F32);
     588    kernels->penalties1 = psVectorAlloc(numBasisFunctions, PS_TYPE_F32);
     589    psVectorInit(kernels->penalties1, NAN);
     590    kernels->penalties2 = psVectorAlloc(numBasisFunctions, PS_TYPE_F32);
     591    psVectorInit(kernels->penalties2, NAN);
     592    kernels->havePenalties = false;
    575593    kernels->size = size;
    576594    kernels->inner = 0;
     
    771789
    772790    psWarning("Kernel penalty for dual-convolution is not configured for SPAM kernels.");
    773     psVectorInit(kernels->penalties, 0.0);
     791    psVectorInit(kernels->penalties1, 0.0);
     792    psVectorInit(kernels->penalties2, 0.0);
    774793
    775794    return kernels;
     
    866885
    867886    psWarning("Kernel penalty for dual-convolution is not configured for FRIES kernels.");
    868     psVectorInit(kernels->penalties, 0.0);
     887    psVectorInit(kernels->penalties1, 0.0);
     888    psVectorInit(kernels->penalties2, 0.0);
    869889
    870890    return kernels;
     
    10401060                kernels->u->data.S32[index] = uOrder;
    10411061                kernels->v->data.S32[index] = vOrder;
    1042                 kernels->penalties->data.F32[index] = kernels->penalty * fabsf(moment);
    1043                 if (!isfinite(kernels->penalties->data.F32[index])) {
     1062
     1063                // XXX convert to use the convolved 2nd moment
     1064                kernels->penalties1->data.F32[index] = kernels->penalty * fabsf(moment);
     1065                if (!isfinite(kernels->penalties1->data.F32[index])) {
     1066                    psAbort ("invalid penalty");
     1067                }
     1068                kernels->penalties2->data.F32[index] = kernels->penalty * fabsf(moment);
     1069                if (!isfinite(kernels->penalties2->data.F32[index])) {
    10441070                    psAbort ("invalid penalty");
    10451071                }
     
    12471273    out->preCalc = psMemIncrRefCounter(in->preCalc);
    12481274    out->penalty = in->penalty;
    1249     out->penalties = psMemIncrRefCounter(in->penalties);
     1275    out->penalties1 = psMemIncrRefCounter(in->penalties1);
     1276    out->penalties2 = psMemIncrRefCounter(in->penalties2);
    12501277    out->uStop = psMemIncrRefCounter(in->uStop);
    12511278    out->vStop = psMemIncrRefCounter(in->vStop);
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionKernels.h

    r26893 r29060  
    3939    psArray *preCalc;                   ///< Array of images containing pre-calculated kernel (for ISIS, HERM or DECONV_HERM)
    4040    float penalty;                      ///< Penalty for wideness
    41     psVector *penalties;                ///< Penalty for each kernel component
     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.
    4244    int size;                           ///< The half-size of the kernel
    4345    int inner;                          ///< The size of an inner region
     
    308310    );
    309311
    310 
    311312#endif
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionMatch.c

    r28405 r29060  
    13041304    float scale = PS_MAX(fwhm1, fwhm2) / scaleRef;      // Scaling factor
    13051305
     1306    // XXX save these values in a static for later use
     1307    pmSubtractionSetFWHMs(fwhm1, fwhm2);
     1308
    13061309    if (isfinite(scaleMin) && scale < scaleMin) {
    13071310        scale = scaleMin;
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionMatch.h

    r26893 r29060  
    110110    );
    111111
    112 
    113112#endif
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionStamps.c

    r27402 r29060  
    77#include <pslib.h>
    88
     9#include "pmErrorCodes.h"
     10#include "pmHDU.h"
     11#include "pmFPA.h"
     12
    913// All these includes required to get stamps out of an array of pmSources
    10 #include "pmMoments.h"
     14#include "pmTrend2D.h"
     15#include "pmResiduals.h"
     16#include "pmGrowthCurve.h"
    1117#include "pmSpan.h"
     18#include "pmFootprintSpans.h"
    1219#include "pmFootprint.h"
    1320#include "pmPeaks.h"
    14 #include "pmResiduals.h"
    15 #include "pmHDU.h"
    16 #include "pmFPA.h"
    17 #include "pmGrowthCurve.h"
    18 #include "pmTrend2D.h"
    19 #include "pmPSF.h"
     21#include "pmMoments.h"
     22#include "pmModelFuncs.h"
    2023#include "pmModel.h"
     24#include "pmSourceMasks.h"
     25#include "pmSourceExtendedPars.h"
     26#include "pmSourceDiffStats.h"
    2127#include "pmSource.h"
    22 #include "pmErrorCodes.h"
    2328
    2429#include "pmSubtraction.h"
     
    4651    psFree(list->y);
    4752    psFree(list->flux);
    48     psFree(list->window);
     53    psFree(list->window1);
     54    psFree(list->window2);
    4955}
    5056
     
    225231    list->y = NULL;
    226232    list->flux = NULL;
    227     list->window = NULL;
    228233    list->normFrac = normFrac;
    229     list->normWindow = 0;
     234    list->window1 = NULL;
     235    list->window2 = NULL;
     236    list->normWindow1 = 0;
     237    list->normWindow2 = 0;
    230238    list->footprint = footprint;
    231239    list->sysErr = sysErr;
     
    248256    out->y = NULL;
    249257    out->flux = NULL;
    250     out->window = psMemIncrRefCounter(in->window);
     258    out->window1 = psMemIncrRefCounter(in->window1);
     259    out->window2 = psMemIncrRefCounter(in->window2);
    251260    out->footprint = in->footprint;
    252     out->normWindow = in->normWindow;
     261    out->normWindow1 = in->normWindow1;
     262    out->normWindow2 = in->normWindow2;
    253263
    254264    for (int i = 0; i < num; i++) {
     
    638648    int size = stamps->footprint; // Size of postage stamps
    639649
    640     psFree (stamps->window);
    641     stamps->window = psKernelAlloc(-size, size, -size, size);
    642     psImageInit(stamps->window->image, 0.0);
     650    psFree (stamps->window1);
     651    stamps->window1 = psKernelAlloc(-size, size, -size, size);
     652    psImageInit(stamps->window1->image, 0.0);
     653
     654    psFree (stamps->window2);
     655    stamps->window2 = psKernelAlloc(-size, size, -size, size);
     656    psImageInit(stamps->window2->image, 0.0);
    643657
    644658    // generate normalizations for each stamp
     
    669683
    670684    // generate the window pixels
    671     double sum = 0.0;                   // Sum inside the window
    672     float maxValue = 0.0;               // Maximum value, for normalisation
     685    double sum1 = 0.0;                   // Sum inside the window
     686    double sum2 = 0.0;                   // Sum inside the window
     687    float maxValue1 = 0.0;               // Maximum value, for normalisation
     688    float maxValue2 = 0.0;               // Maximum value, for normalisation
    673689    for (int y = -size; y <= size; y++) {
    674690        for (int x = -size; x <= size; x++) {
     
    691707            }
    692708            float f1 = stats->robustMedian;
     709
    693710            psStatsInit (stats);
    694711            if (!psVectorStats (stats, flux2, NULL, NULL, 0)) {
     
    697714            float f2 = stats->robustMedian;
    698715
    699             stamps->window->kernel[y][x] = f1 + f2;
     716            stamps->window1->kernel[y][x] = f1;
    700717            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(size)) {
    701                 sum += stamps->window->kernel[y][x];
    702             }
    703             maxValue = PS_MAX(maxValue, stamps->window->kernel[y][x]);
    704         }
    705     }
    706 
    707     psTrace("psModules.imcombine", 3, "Window total: %f, threshold: %f\n",
    708             sum, (1.0 - stamps->normFrac) * sum);
    709     bool done = false;
    710     for (int radius = 1; radius <= size && !done; radius++) {
    711         double within = 0.0;
     718                sum1 += stamps->window1->kernel[y][x];
     719            }
     720            maxValue1 = PS_MAX(maxValue1, stamps->window1->kernel[y][x]);
     721
     722            stamps->window2->kernel[y][x] = f2;
     723            if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(size)) {
     724                sum2 += stamps->window2->kernel[y][x];
     725            }
     726            maxValue2 = PS_MAX(maxValue2, stamps->window2->kernel[y][x]);
     727        }
     728    }
     729
     730    psTrace("psModules.imcombine", 3, "Window total (1): %f, threshold: %f\n", sum1, (1.0 - stamps->normFrac) * sum1);
     731    psTrace("psModules.imcombine", 3, "Window total (2): %f, threshold: %f\n", sum2, (1.0 - stamps->normFrac) * sum2);
     732
     733# if (0)
     734    // this block attempts to calculate the radius based on the first radial moment
     735    bool done1 = false;
     736    bool done2 = false;
     737    double prior1 = 0.0;
     738    double prior2 = 0.0;
     739    for (int y = -size; y <= size; y++) {
     740        for (int x = -size; x <= size; x++) {
     741            float r = hypot(x, y);
     742            Sr1 += r * stamps->window1->kernel[y][x];
     743            Sr2 += r * stamps->window2->kernel[y][x];
     744            Sf1 += stamps->window1->kernel[y][x];
     745            Sf2 += stamps->window2->kernel[y][x];
     746        }
     747    }
     748   
     749    float R1 = Sr1 / Sf1;
     750    float R2 = Sr2 / Sf2;
     751
     752    stamps->normWindow1 = 2.5*R1;
     753    stamps->normWindow1 = 2.5*R2;
     754# else
     755    // XXX : this block attempts to calculate the radius by looking at the curve of growth (or something vaguely equivalent).
     756    // It did not do very well (though a true curve-of-growth analysis might be better...)
     757    bool done1 = false;
     758    bool done2 = false;
     759    double prior1 = 0.0;
     760    double prior2 = 0.0;
     761    double delta1o = 1.0;
     762    double delta2o = 1.0;
     763    for (int radius = 1; radius <= size && !(done1 && done2); radius++) {
     764        double within1 = 0.0;
     765        double within2 = 0.0;
    712766        for (int y = -radius; y <= radius; y++) {
    713767            for (int x = -radius; x <= radius; x++) {
    714768                if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) {
    715                     within += stamps->window->kernel[y][x];
     769                    within1 += stamps->window1->kernel[y][x];
    716770                }
    717             }
    718         }
    719         psTrace("psModules.imcombine", 5, "Radius %d: %f\n", radius, within);
    720         if (within > (1.0 - stamps->normFrac) * sum) {
    721             stamps->normWindow = radius;
    722             done = true;
    723         }
    724     }
    725 
    726     psTrace("psModules.imcombine", 3, "Normalisation window radius set to %d\n", stamps->normWindow);
    727     if (stamps->normWindow == 0 || stamps->normWindow >= size) {
    728         psError(PM_ERR_STAMPS, true, "Unable to determine normalisation window size.");
     771                if (PS_SQR(x) + PS_SQR(y) <= PS_SQR(radius)) {
     772                    within2 += stamps->window2->kernel[y][x];
     773                }
     774            }
     775        }
     776        double delta1 = (within1 - prior1) / within1;
     777        if (!done1 && (fabs(delta1) < stamps->normFrac)) {
     778            // interpolate to the radius at which delta2 is normFrac:
     779            stamps->normWindow1 = radius - (stamps->normFrac - delta1) / (delta1o - delta1);
     780            fprintf (stderr, "choosing %f (%d) for 1 (%f : %f)\n", stamps->normWindow1, radius, within1, delta1);
     781            done1 = true;
     782        }
     783        double delta2 = (within2 - prior2) / within2;
     784        if (!done2 && (fabs(delta2) < stamps->normFrac)) {
     785            // interpolate to the radius at which delta2 is normFrac:
     786            stamps->normWindow2 = radius - (stamps->normFrac - delta2) / (delta2o - delta2);
     787            fprintf (stderr, "choosing %f (%d) for 2 (%f : %f)\n", stamps->normWindow2, radius, within2, delta2);
     788            done2 = true;
     789        }
     790        psTrace("psModules.imcombine", 5, "Radius %d: %f (%f) and %f (%f)\n", radius, within1, delta1, within2, delta2);
     791
     792        prior1 = within1;
     793        prior2 = within2;
     794        delta1o = delta1;
     795        delta2o = delta2;
     796
     797        // if (!done1 && (within1 > (1.0 - stamps->normFrac) * sum1)) {
     798        //     stamps->normWindow1 = radius;
     799        //     done1 = true;
     800        // }
     801        // if (!done2 && (within2 > (1.0 - stamps->normFrac) * sum2)) {
     802        //     stamps->normWindow2 = radius;
     803        //     done2 = true;
     804        // }
     805
     806    }
     807# endif
     808
     809    psTrace("psModules.imcombine", 3, "Normalisation window radii set to %f and %f\n", stamps->normWindow1, stamps->normWindow2);
     810    if (stamps->normWindow1 == 0 || stamps->normWindow1 >= size) {
     811        psError(PM_ERR_STAMPS, true, "Unable to determine normalisation window size (1).");
     812        return false;
     813    }
     814    if (stamps->normWindow2 == 0 || stamps->normWindow2 >= size) {
     815        psError(PM_ERR_STAMPS, true, "Unable to determine normalisation window size (2).");
    729816        return false;
    730817    }
     
    733820    for (int y = -size; y <= size; y++) {
    734821        for (int x = -size; x <= size; x++) {
    735             stamps->window->kernel[y][x] /= maxValue;
     822            stamps->window1->kernel[y][x] /= maxValue1;
     823        }
     824    }
     825    // re-normalize so chisquare values are sensible
     826    for (int y = -size; y <= size; y++) {
     827        for (int x = -size; x <= size; x++) {
     828            stamps->window2->kernel[y][x] /= maxValue2;
    736829        }
    737830    }
     
    739832#if 0
    740833    {
    741         psFits *fits = psFitsOpen ("window.fits", "w");
    742         psFitsWriteImage (fits, NULL, stamps->window->image, 0, NULL);
     834        psFits *fits = NULL;
     835        fits = psFitsOpen ("window1.fits", "w");
     836        psFitsWriteImage (fits, NULL, stamps->window1->image, 0, NULL);
     837        psFitsClose (fits);
     838        fits = psFitsOpen ("window2.fits", "w");
     839        psFitsWriteImage (fits, NULL, stamps->window2->image, 0, NULL);
    743840        psFitsClose (fits);
    744841    }
     
    747844    psFree (stats);
    748845    psFree (flux1);
    749     psFree(flux2);
     846    psFree (flux2);
    750847    psFree (norm1);
    751848    psFree (norm2);
    752849    return true;
     850}
     851
     852static pthread_mutex_t getPenaltiesMutex = PTHREAD_MUTEX_INITIALIZER;
     853
     854// kernels->penalty is an overall scaling factor (user-supplied)
     855bool pmSubtractionKernelPenaltiesStamp(pmSubtractionStamp *stamp, pmSubtractionKernels *kernels)
     856{
     857    // we only need the penalties if we are doing dual convolution
     858    if (kernels->mode != PM_SUBTRACTION_MODE_DUAL) return true;
     859
     860    // we only calculate the penalties once.
     861    if (kernels->havePenalties) return true;
     862
     863    // in a threaded context, only one thread can calculate the penalties.  attempt to grab a
     864    // mutex before continuing
     865    pthread_mutex_lock(&getPenaltiesMutex);
     866
     867    // did someone else already get the mutex and do this?
     868    if (kernels->havePenalties) {
     869        pthread_mutex_unlock(&getPenaltiesMutex);
     870        return true;
     871    }
     872
     873    for (int i = 0; i < kernels->num; i++) {
     874        pmSubtractionKernelPenalties(stamp, kernels, i);
     875    }
     876
     877    kernels->havePenalties = true;
     878    pthread_mutex_unlock(&getPenaltiesMutex);
     879    return true;
     880}
     881
     882# define EMPIRICAL 0
     883
     884// kernels->penalty is an overall scaling factor (user-supplied)
     885bool pmSubtractionKernelPenalties(pmSubtractionStamp *stamp, pmSubtractionKernels *kernels, int index)
     886{
     887    float penalty1, penalty2;
     888    float fwhm1, fwhm2;
     889
     890    // XXX this is annoyingly hack-ish
     891    pmSubtractionGetFWHMs(&fwhm1, &fwhm2);
     892
     893    bool zeroNull = false;
     894    int uOrder = kernels->u->data.S32[index];
     895    int vOrder = kernels->v->data.S32[index];
     896    if (uOrder % 2 == 0 && vOrder % 2 == 0) zeroNull = true;
     897
     898    if (EMPIRICAL) {
     899        psKernel *convolution1 = stamp->convolutions1->data[index];
     900        penalty1 = pmSubtractionKernelPenaltySingle(convolution1, zeroNull);
     901
     902        psKernel *convolution2 = stamp->convolutions2->data[index];
     903        penalty2 = pmSubtractionKernelPenaltySingle(convolution2, zeroNull);
     904    } else {
     905        pmSubtractionKernelPreCalc *kernel = kernels->preCalc->data[index];
     906        float M2 = pmSubtractionKernelPenaltySingle(kernel->kernel, zeroNull);
     907
     908        penalty1 = M2 + PS_SQR(fwhm1 / 2.35); // rescale the unconvolved second-moment by the image second moment
     909        penalty2 = M2 + PS_SQR(fwhm2 / 2.35); // rescale the unconvolved second-moment by the image second moment
     910    }
     911    kernels->penalties1->data.F32[index] = kernels->penalty * penalty1;
     912    psAssert (isfinite(kernels->penalties1->data.F32[index]), "invalid penalty");
     913
     914    kernels->penalties2->data.F32[index] = kernels->penalty * penalty2;
     915    psAssert (isfinite(kernels->penalties2->data.F32[index]), "invalid penalty");
     916
     917    fprintf(stderr, "penalty1: %f, penalty2: %f\n", penalty1, penalty2);
     918
     919    return true;
     920}
     921
     922float pmSubtractionKernelPenaltySingle(psKernel *kernel, bool zeroNull)
     923{
     924    // Calculate moments
     925    double penalty = 0.0;                   // Moment, for penalty
     926    double sum = 0.0, sum2 = 0.0;           // Sum of kernel component
     927    float min = INFINITY, max = -INFINITY;  // Minimum and maximum kernel value
     928    for (int v = kernel->yMin; v <= kernel->yMax; v++) {
     929        for (int u = kernel->xMin; u <= kernel->xMax; u++) {
     930            double value = kernel->kernel[v][u];
     931            if (false && zeroNull && (u == 0) && (v == 0)) {
     932                value += 1.0;
     933            }
     934            double value2 = PS_SQR(value);
     935            sum += value;
     936            sum2 += value2;
     937            penalty += value2 * PS_SQR((PS_SQR(u) + PS_SQR(v)));
     938            min = PS_MIN(value, min);
     939            max = PS_MAX(value, max);
     940        }
     941    }
     942    penalty *= 1.0 / sum2;
     943
     944    if (1) {
     945        fprintf(stderr, "min: %lf, max: %lf, moment: %lf, flux^2: %lf\n", min, max, penalty, sum2);
     946        // psTrace("psModules.imcombine", 7, "Kernel %d: %f %d %d %f\n", index, fwhm, uOrder, vOrder, penalty);
     947    }
     948
     949    return penalty;
    753950}
    754951
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionStamps.h

    r26893 r29060  
    2424    psArray *flux;                      ///< Fluxes for possible stamps (or NULL)
    2525    int footprint;                      ///< Half-size of stamps
    26     psKernel *window;                   ///< window function generated from ensemble of stamps
    2726    float normFrac;                     ///< Fraction of flux in window for normalisation window
    28     int normWindow;                     ///< Size of window for measuring normalisation
     27    psKernel *window1;                  ///< window function generated from ensemble of stamps (input 1)
     28    psKernel *window2;                  ///< window function generated from ensemble of stamps (input 2)
     29    float normWindow1;                    ///< Size of window for measuring normalisation
     30    float normWindow2;                    ///< Size of window for measuring normalisation
    2931    float sysErr;                       ///< Systematic error
    3032    float skyErr;                       ///< increase effective readnoise
     
    195197bool pmSubtractionStampsResetStatus (pmSubtractionStampList *stamps);
    196198
     199
     200bool pmSubtractionKernelPenaltiesStamp(pmSubtractionStamp *stamp, pmSubtractionKernels *kernels);
     201bool pmSubtractionKernelPenalties(pmSubtractionStamp *stamp, pmSubtractionKernels *kernels, int index);
     202float pmSubtractionKernelPenaltySingle(psKernel *kernel, bool zeroNull);
     203
    197204#endif
  • branches/sc_branches/trunkTest/psModules/src/imcombine/pmSubtractionVisual.c

    r26893 r29060  
    2121
    2222#include "pmVisual.h"
     23#include "pmVisualUtils.h"
    2324
    2425#include "pmHDU.h"
     
    6162 *    @return true for success */
    6263bool pmSubtractionVisualPlotConvKernels(psImage *convKernels) {
    63     if (!pmVisualIsVisual() || !plotConvKernels) return true;
     64
     65    if (!pmVisualTestLevel("ppsub.kernels", 1)) return true;
     66
     67    if (!plotConvKernels) return true;
     68
    6469    if (!pmVisualInitWindow(&kapa1, "ppSub:Images")) {
    6570        return false;
     
    7580    @return true for success */
    7681bool pmSubtractionVisualPlotStamps(pmSubtractionStampList *stamps, pmReadout *ro) {
    77     if (!pmVisualIsVisual() || !plotStamps) return true;
     82
     83    if (!pmVisualTestLevel("ppsub.stamps", 1)) return true;
     84
     85    if (!plotStamps) return true;
     86
    7887    if (!pmVisualInitWindow (&kapa1, "ppSub:Images")) {
    7988        return false;
     
    145154bool pmSubtractionVisualPlotLeastSquares (pmSubtractionStampList *stamps, bool dual) {
    146155
    147     if (!pmVisualIsVisual() || !plotLeastSquares) return true;
     156    if (!pmVisualTestLevel("ppsub.chisq", 1)) return true;
     157
     158    if (!plotLeastSquares) return true;
     159
    148160    if (!pmVisualInitWindow (&kapa1, "PPSub:Images")) {
    149161        return false;
     
    204216
    205217bool pmSubtractionVisualShowSubtraction(psImage *image, psImage *ref, psImage *sub) {
    206     if (!pmVisualIsVisual() || !plotImage) return true;
     218
     219    if (!pmVisualTestLevel("ppsub.images.sub", 1)) return true;
     220
     221    if (!plotImage) return true;
     222
    207223    if (!pmVisualInitWindow (&kapa1, "PPSub:Images")) {
    208224        return false;
     
    218234bool pmSubtractionVisualShowKernels(pmSubtractionKernels *kernels) {
    219235
    220     if (!pmVisualIsVisual()) return true;
     236    if (!pmVisualTestLevel("ppsub.kernels.final", 1)) return true;
     237
    221238    if (!pmVisualInitWindow (&kapa1, "PPSub:Images")) {
    222239        return false;
     
    264281bool pmSubtractionVisualShowBasis(pmSubtractionStampList *stamps) {
    265282
    266     if (!pmVisualIsVisual()) return true;
     283    if (!pmVisualTestLevel("ppsub.basis", 1)) return true;
     284
    267285    if (!pmVisualInitWindow (&kapa2, "ppSub:StampMasterImage")) {
    268286        return false;
     
    425443bool pmSubtractionVisualShowFitInit(pmSubtractionStampList *stamps) {
    426444
    427     if (!pmVisualIsVisual()) return true;
     445    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
    428446
    429447    // generate 4 storage images large enough to hold the N stamps:
     
    462480bool pmSubtractionVisualShowFitAddStamp(psKernel *target, psKernel *source, psKernel *convolution, double background, double norm, int index) {
    463481
    464     if (!pmVisualIsVisual()) return true;
     482    if (!pmVisualTestLevel("ppsub.stamp", 1)) return true;
    465483
    466484    double sum;
     
    543561    }
    544562
    545     if (!pmVisualIsVisual()) return true;
     563    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
     564
    546565    if (!pmVisualInitWindow(&kapa1, "ppSub:Images")) return false;
    547566    if (!pmVisualInitWindow(&kapa2, "ppSub:Misc")) return false;
     
    605624    Graphdata graphdata;
    606625
    607     if (!pmVisualIsVisual()) return true;
     626    if (!pmVisualTestLevel("ppsub.fit", 1)) return true;
     627
    608628    if (!pmVisualInitWindow(&kapa3, "ppSub:plots")) return false;
    609629
  • branches/sc_branches/trunkTest/psModules/src/objects/Makefile.am

    r28013 r29060  
    4343        pmSourceIO_CMF_PS1_V1.c \
    4444        pmSourceIO_CMF_PS1_V2.c \
     45        pmSourceIO_CMF_PS1_V3.c \
    4546        pmSourceIO_CMF_PS1_SV1.c \
    4647        pmSourceIO_CMF_PS1_DV1.c \
     48        pmSourceIO_CMF_PS1_DV2.c \
    4749        pmSourceIO_MatchedRefs.c \
    4850        pmSourcePlots.c \
     
    6062        pmPSFtryFitPSF.c \
    6163        pmPSFtryMetric.c \
     64        pmPCMdata.c \
     65        pmPCM_MinimizeChisq.c \
     66        pmSourceFitPCM.c \
    6267        pmTrend2D.c \
    6368        pmGrowthCurveGenerate.c \
     
    7176        models/pmModel_QGAUSS.c \
    7277        models/pmModel_RGAUSS.c \
    73         models/pmModel_SERSIC.c
     78        models/pmModel_SERSIC.c \
     79        models/pmModel_EXP.c \
     80        models/pmModel_DEV.c
    7481
    7582pkginclude_HEADERS = \
     
    8087        pmPeaks.h \
    8188        pmMoments.h \
     89        pmModelFuncs.h \
    8290        pmModel.h \
    8391        pmModelClass.h \
     
    101109        pmPSF_IO.h \
    102110        pmPSFtry.h \
     111        pmPCMdata.h \
    103112        pmTrend2D.h \
    104113        pmGrowthCurve.h \
     
    111120        models/pmModel_QGAUSS.h \
    112121        models/pmModel_RGAUSS.h \
    113         models/pmModel_SERSIC.h
     122        models/pmModel_SERSIC.h \
     123        models/pmModel_EXP.h \
     124        models/pmModel_DEV.h
    114125
    115126CLEANFILES = *~
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_GAUSS.c

    r26916 r29060  
    2121#include <stdio.h>
    2222#include <pslib.h>
    23 
     23#include "pmHDU.h"
     24#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
     29#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
     31#include "pmFootprint.h"
     32#include "pmPeaks.h"
    2433#include "pmMoments.h"
    25 #include "pmPeaks.h"
     34#include "pmModelFuncs.h"
     35#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    2641#include "pmSource.h"
    27 #include "pmModel.h"
     42#include "pmSourceFitModel.h"
     43#include "pmPSF.h"
     44#include "pmPSFtry.h"
     45#include "pmDetections.h"
     46
    2847#include "pmModel_GAUSS.h"
    2948
     49# define PM_MODEL_NPARAM          7
    3050# define PM_MODEL_FUNC            pmModelFunc_GAUSS
    3151# define PM_MODEL_FLUX            pmModelFlux_GAUSS
     
    83103        dPAR[PM_PAR_XPOS] = q*(2*px/PAR[PM_PAR_SXX] + Y*PAR[PM_PAR_SXY]);
    84104        dPAR[PM_PAR_YPOS] = q*(2*py/PAR[PM_PAR_SYY] + X*PAR[PM_PAR_SXY]);
     105
    85106        // the extra factor of 2 below is needed to avoid excessive swings
    86107        dPAR[PM_PAR_SXX]  = +4.0*q*px*px/PAR[PM_PAR_SXX];
     
    102123        return true;
    103124    }
    104     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     125    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    105126
    106127    // we need to calculate the limits for SXY specially
     
    347368// this test is invalid if the parameters are derived
    348369// from the PSF model
     370// XXX how is this used?  it prevents forced photometry from ever being 'successful'
    349371bool PM_MODEL_FIT_STATUS (pmModel *model)
    350372{
     
    394416    return;
    395417}
    396 
    397 # undef PM_MODEL_FUNC
    398 # undef PM_MODEL_FLUX
    399 # undef PM_MODEL_GUESS
    400 # undef PM_MODEL_LIMITS
    401 # undef PM_MODEL_RADIUS
    402 # undef PM_MODEL_FROM_PSF
    403 # undef PM_MODEL_PARAMS_FROM_PSF
    404 # undef PM_MODEL_FIT_STATUS
    405 # undef PM_MODEL_SET_LIMITS
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_PGAUSS.c

    r27565 r29060  
    2121#include <stdio.h>
    2222#include <pslib.h>
    23 
     23#include "pmHDU.h"
     24#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
     29#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
     31#include "pmFootprint.h"
     32#include "pmPeaks.h"
    2433#include "pmMoments.h"
    25 #include "pmPeaks.h"
     34#include "pmModelFuncs.h"
     35#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    2641#include "pmSource.h"
    27 #include "pmModel.h"
     42#include "pmSourceFitModel.h"
     43#include "pmPSF.h"
     44#include "pmPSFtry.h"
     45#include "pmDetections.h"
     46
    2847#include "pmModel_PGAUSS.h"
    2948
     49# define PM_MODEL_NPARAM          7
    3050# define PM_MODEL_FUNC            pmModelFunc_PGAUSS
    3151# define PM_MODEL_FLUX            pmModelFlux_PGAUSS
     
    103123        return true;
    104124    }
    105     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     125    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    106126
    107127    // we need to calculate the limits for SXY specially
     
    448468    return;
    449469}
    450 
    451 # undef PM_MODEL_FUNC
    452 # undef PM_MODEL_FLUX
    453 # undef PM_MODEL_GUESS
    454 # undef PM_MODEL_LIMITS
    455 # undef PM_MODEL_RADIUS
    456 # undef PM_MODEL_FROM_PSF
    457 # undef PM_MODEL_PARAMS_FROM_PSF
    458 # undef PM_MODEL_FIT_STATUS
    459 # undef PM_MODEL_SET_LIMITS
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_PS1_V1.c

    r27565 r29060  
    2222#include <stdio.h>
    2323#include <pslib.h>
    24 
     24#include "pmHDU.h"
     25#include "pmFPA.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
     30#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
     32#include "pmFootprint.h"
     33#include "pmPeaks.h"
    2534#include "pmMoments.h"
    26 #include "pmPeaks.h"
     35#include "pmModelFuncs.h"
     36#include "pmModel.h"
     37#include "pmModelUtils.h"
     38#include "pmModelClass.h"
     39#include "pmSourceMasks.h"
     40#include "pmSourceExtendedPars.h"
     41#include "pmSourceDiffStats.h"
    2742#include "pmSource.h"
    28 #include "pmModel.h"
     43#include "pmSourceFitModel.h"
     44#include "pmPSF.h"
     45#include "pmPSFtry.h"
     46#include "pmDetections.h"
     47
    2948#include "pmModel_PS1_V1.h"
    3049
     50# define PM_MODEL_NPARAM          8
    3151# define PM_MODEL_FUNC            pmModelFunc_PS1_V1
    3252# define PM_MODEL_FLUX            pmModelFlux_PS1_V1
     
    122142        return true;
    123143    }
    124     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     144    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    125145
    126146    // we need to calculate the limits for SXY specially
     
    222242    PAR[PM_PAR_SYY]  = PS_MAX(0.5, M_SQRT2*shape.sy);
    223243    PAR[PM_PAR_SXY]  = shape.sxy;
    224     PAR[PM_PAR_7]    = 1.0;
     244    PAR[PM_PAR_7]    = 0.5;
    225245
    226246    return(true);
     
    468488    return;
    469489}
    470 
    471 # undef PM_MODEL_FUNC
    472 # undef PM_MODEL_FLUX
    473 # undef PM_MODEL_GUESS
    474 # undef PM_MODEL_LIMITS
    475 # undef PM_MODEL_RADIUS
    476 # undef PM_MODEL_FROM_PSF
    477 # undef PM_MODEL_PARAMS_FROM_PSF
    478 # undef PM_MODEL_FIT_STATUS
    479 # undef PM_MODEL_SET_LIMITS
    480 # undef ALPHA
    481 # undef ALPHA_M
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_QGAUSS.c

    r27565 r29060  
    2222#include <stdio.h>
    2323#include <pslib.h>
    24 
     24#include "pmHDU.h"
     25#include "pmFPA.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
     30#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
     32#include "pmFootprint.h"
     33#include "pmPeaks.h"
    2534#include "pmMoments.h"
    26 #include "pmPeaks.h"
     35#include "pmModelFuncs.h"
     36#include "pmModel.h"
     37#include "pmModelUtils.h"
     38#include "pmModelClass.h"
     39#include "pmSourceMasks.h"
     40#include "pmSourceExtendedPars.h"
     41#include "pmSourceDiffStats.h"
    2742#include "pmSource.h"
    28 #include "pmModel.h"
     43#include "pmSourceFitModel.h"
     44#include "pmPSF.h"
     45#include "pmPSFtry.h"
     46#include "pmDetections.h"
     47
    2948#include "pmModel_QGAUSS.h"
    3049
     50# define PM_MODEL_NPARAM          8
    3151# define PM_MODEL_FUNC            pmModelFunc_QGAUSS
    3252# define PM_MODEL_FLUX            pmModelFlux_QGAUSS
     
    123143        return true;
    124144    }
    125     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     145    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    126146
    127147    // we need to calculate the limits for SXY specially
     
    469489    return;
    470490}
    471 
    472 # undef PM_MODEL_FUNC
    473 # undef PM_MODEL_FLUX
    474 # undef PM_MODEL_GUESS
    475 # undef PM_MODEL_LIMITS
    476 # undef PM_MODEL_RADIUS
    477 # undef PM_MODEL_FROM_PSF
    478 # undef PM_MODEL_PARAMS_FROM_PSF
    479 # undef PM_MODEL_FIT_STATUS
    480 # undef PM_MODEL_SET_LIMITS
    481 # undef ALPHA
    482 # undef ALPHA_M
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_RGAUSS.c

    r27565 r29060  
    2222#include <stdio.h>
    2323#include <pslib.h>
    24 
     24#include "pmHDU.h"
     25#include "pmFPA.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
     30#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
     32#include "pmFootprint.h"
     33#include "pmPeaks.h"
    2534#include "pmMoments.h"
    26 #include "pmPeaks.h"
     35#include "pmModelFuncs.h"
     36#include "pmModel.h"
     37#include "pmModelUtils.h"
     38#include "pmModelClass.h"
     39#include "pmSourceMasks.h"
     40#include "pmSourceExtendedPars.h"
     41#include "pmSourceDiffStats.h"
    2742#include "pmSource.h"
    28 #include "pmModel.h"
     43#include "pmSourceFitModel.h"
     44#include "pmPSF.h"
     45#include "pmPSFtry.h"
     46#include "pmDetections.h"
     47
    2948#include "pmModel_RGAUSS.h"
    3049
     50# define PM_MODEL_NPARAM          8
    3151# define PM_MODEL_FUNC            pmModelFunc_RGAUSS
    3252# define PM_MODEL_FLUX            pmModelFlux_RGAUSS
     
    112132        return true;
    113133    }
    114     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     134    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    115135
    116136    // we need to calculate the limits for SXY specially
     
    209229    PAR[PM_PAR_XPOS] = peak->xf;
    210230    PAR[PM_PAR_YPOS] = peak->yf;
    211     PAR[PM_PAR_SXX]  = PS_MAX(0.5, M_SQRT2*shape.sx);
    212     PAR[PM_PAR_SYY]  = PS_MAX(0.5, M_SQRT2*shape.sy);
     231    PAR[PM_PAR_SXX]  = PS_MAX(0.5, shape.sx);
     232    PAR[PM_PAR_SYY]  = PS_MAX(0.5, shape.sy);
    213233    PAR[PM_PAR_SXY]  = shape.sxy;
    214     PAR[PM_PAR_7]    = 2.25;
     234    PAR[PM_PAR_7]    = 1.5;
    215235
    216236    return(true);
     
    463483    return;
    464484}
    465 
    466 # undef PM_MODEL_FUNC
    467 # undef PM_MODEL_FLUX
    468 # undef PM_MODEL_GUESS
    469 # undef PM_MODEL_LIMITS
    470 # undef PM_MODEL_RADIUS
    471 # undef PM_MODEL_FROM_PSF
    472 # undef PM_MODEL_PARAMS_FROM_PSF
    473 # undef PM_MODEL_FIT_STATUS
    474 # undef PM_MODEL_SET_LIMITS
  • branches/sc_branches/trunkTest/psModules/src/objects/models/pmModel_SERSIC.c

    r26916 r29060  
    1818   * PM_PAR_7   7   - normalized sersic parameter
    1919
    20    note that a standard sersic model uses exp(-K*(z^(1/n) - 1).  the additional elements (K,
     20   note that a standard sersic model uses exp(-K*(z^(1/2n) - 1).  the additional elements (K,
    2121   the -1 offset) are absorbed in this model by the normalization, the exponent, and the
    2222   radial scale.  We fit the elements in this form, then re-normalize them on output.
     
    2525#include <stdio.h>
    2626#include <pslib.h>
    27 
     27#include "pmHDU.h"
     28#include "pmFPA.h"
     29
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
     33#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
     35#include "pmFootprint.h"
     36#include "pmPeaks.h"
    2837#include "pmMoments.h"
    29 #include "pmPeaks.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
    3045#include "pmSource.h"
    31 #include "pmModel.h"
     46#include "pmSourceFitModel.h"
     47#include "pmPSF.h"
     48#include "pmPSFtry.h"
     49#include "pmDetections.h"
     50
    3251#include "pmModel_SERSIC.h"
    3352
     53# define PM_MODEL_NPARAM          8
    3454# define PM_MODEL_FUNC            pmModelFunc_SERSIC
    3555# define PM_MODEL_FLUX            pmModelFlux_SERSIC
     
    4767
    4868// Lax parameter limits
    49 static float paramsMinLax[] = { -1.0e3, 1.0e-2, -100, -100, 0.05, 0.05, -1.0, 0.05 };
     69static float paramsMinLax[] = { -1.0e3, 1.0e-2, -100, -100, 0.001, 0.001, -1.0, 0.05 };
    5070static float paramsMaxLax[] = { 1.0e5, 1.0e8, 1.0e4, 1.0e4, 100, 100, 1.0, 4.0 };
    5171
     
    84104    assert (z >= 0);
    85105
    86     psF32 f2 = pow(z,PAR[PM_PAR_7]);
    87     psF32 f1 = exp(-f2);
     106    float index = 0.5 / PAR[PM_PAR_7];
     107    float bn = 1.9992*index - 0.3271;
     108    float Io = exp(bn);
     109
     110    psF32 f2 = bn*pow(z,PAR[PM_PAR_7]);
     111    psF32 f1 = Io*exp(-f2);
    88112    psF32 z0 = PAR[PM_PAR_I0]*f1;
    89113    psF32 f0 = PAR[PM_PAR_SKY] + z0;
     
    98122
    99123        // gradient is infinite for z = 0; saturate at z = 0.01
    100         psF32 z1 = (z < 0.01) ? z0*PAR[PM_PAR_7]*pow(0.01,PAR[PM_PAR_7] - 1.0) : z0*PAR[PM_PAR_7]*pow(z,PAR[PM_PAR_7] - 1.0);
     124        psF32 z1 = (z < 0.01) ? z0*bn*PAR[PM_PAR_7]*pow(0.01,PAR[PM_PAR_7] - 1.0) : z0*bn*PAR[PM_PAR_7]*pow(z,PAR[PM_PAR_7] - 1.0);
    101125
    102126        dPAR[PM_PAR_SKY]  = +1.0;
    103127        dPAR[PM_PAR_I0]   = +f1;
    104         dPAR[PM_PAR_7]    = (z == 0.0) ? 0.0 : -z0*f2*log(z);
     128        dPAR[PM_PAR_7]    = (z < 0.01) ? -z0*pow(0.01,PAR[PM_PAR_7])*log(0.01) : -z0*f2*log(z);
     129        dPAR[PM_PAR_7]   *= 3.0;
    105130
    106131        assert (isfinite(z1));
     
    109134        dPAR[PM_PAR_XPOS] = +1.0*z1*(2.0*px/PAR[PM_PAR_SXX] + Y*PAR[PM_PAR_SXY]);
    110135        dPAR[PM_PAR_YPOS] = +1.0*z1*(2.0*py/PAR[PM_PAR_SYY] + X*PAR[PM_PAR_SXY]);
    111         dPAR[PM_PAR_SXX]  = +2.0*z1*px*px/PAR[PM_PAR_SXX];
     136        dPAR[PM_PAR_SXX]  = +2.0*z1*px*px/PAR[PM_PAR_SXX]; // XXX : increase drag?
    112137        dPAR[PM_PAR_SYY]  = +2.0*z1*py*py/PAR[PM_PAR_SYY];
    113         dPAR[PM_PAR_SXY]  = -1.0*z1*X*Y;
    114138        dPAR[PM_PAR_SXY]  = -1.0*z1*X*Y;
    115139    }
     
    127151        return true;
    128152    }
    129     psAssert(nParam >= 0 && nParam <= PM_PAR_7, "Parameter index is out of bounds");
     153    psAssert(nParam >= 0 && nParam < PM_MODEL_NPARAM, "Parameter index is out of bounds");
    130154
    131155    // we need to calculate the limits for SXY specially
     
    201225    psF32     *PAR  = model->params->data.F32;
    202226
     227    // the other parameters depend on the guess for PAR_7
     228    if (!isfinite(PAR[PM_PAR_7])) {
     229        PAR[PM_PAR_7]    = 0.25;
     230    }   
     231    float index = 0.5 / PAR[PM_PAR_7];
     232
     233    // the scale-length is a function of the moments and the index:
     234    // Rmajor_guess = Rmajor_moments * Scale * Zero
     235    float Scale = 0.70 + 0.053 * PAR[PM_PAR_7];
     236    float Zero  = 1.16 - 0.615 * PAR[PM_PAR_7];
     237
    203238    psEllipseMoments emoments;
    204239    emoments.x2 = moments->Mxx;
     
    213248    if (!isfinite(axes.theta)) return false;
    214249
     250    // set a lower limit to avoid absurd solutions..
     251    float Rmajor = PS_MAX(1.0, Scale * axes.major + Zero);
     252    float Rminor = Rmajor * (axes.minor / axes.major);
     253
     254    // fprintf (stderr, "guess index: %f : %f, %f -> %f, %f\n", index, axes.major, axes.minor, Rmajor, Rminor);
     255
     256    axes.major = Rmajor;
     257    axes.minor = Rminor;
    215258    psEllipseShape shape = psEllipseAxesToShape (axes);
    216259
     
    219262    if (!isfinite(shape.sxy)) return false;
    220263
     264    float bn = 1.9992*index - 0.3271;
     265    // float fR = 1.0 / (sqrt(2.0) * pow (bn, index));
     266    float Io = exp(0.5*bn);
     267
     268    // XXX do we need this factor of sqrt(2)?
     269    // float Sxx = PS_MAX(0.5, M_SQRT2*shape.sx);
     270    // float Syy = PS_MAX(0.5, M_SQRT2*shape.sy);
     271
     272    float Sxx = PS_MAX(0.5, shape.sx);
     273    float Syy = PS_MAX(0.5, shape.sy);
     274
    221275    PAR[PM_PAR_SKY]  = 0.0;
    222     PAR[PM_PAR_I0]   = peak->flux;
     276    PAR[PM_PAR_I0]   = peak->flux / Io;
    223277    PAR[PM_PAR_XPOS] = peak->xf;
    224278    PAR[PM_PAR_YPOS] = peak->yf;
    225     PAR[PM_PAR_SXX]  = PS_MAX(0.5, M_SQRT2*shape.sx);
    226     PAR[PM_PAR_SYY]  = PS_MAX(0.5, M_SQRT2*shape.sy);
     279    PAR[PM_PAR_SXX]  = Sxx;
     280    PAR[PM_PAR_SYY]  = Syy;
    227281    PAR[PM_PAR_SXY]  = shape.sxy;
    228     PAR[PM_PAR_7]    = 0.5;
    229282
    230283    return(true);
     
    254307    float f1, f2;
    255308    for (z = DZ; z < 50; z += DZ) {
    256         f1 = 1.0 / (1 + PAR[PM_PAR_7]*z + pow(z, 2.25));
     309        // f1 = 1.0 / (1 + PAR[PM_PAR_7]*z + pow(z, 2.25));
     310        f1 = exp(-pow(z,PAR[PM_PAR_7]));
    257311        z += DZ;
    258         f2 = 1.0 / (1 + PAR[PM_PAR_7]*z + pow(z, 2.25));
     312        f2 = exp(-pow(z,PAR[PM_PAR_7]));
    259313        norm += f0 + 4*f1 + f2;
    260314        f0 = f2;
     
    287341
    288342    psEllipseAxes axes = psEllipseShapeToAxes (shape, 20.0);
    289     psF64 sigma = axes.major;
    290 
    291     psF64 limit = flux / PAR[PM_PAR_I0];
    292 
    293     psF64 z = pow (-log(limit), (1.0 / PAR[PM_PAR_7]));
    294     psAssert (isfinite(z), "fix this code: z should not be nan for %f", PAR[PM_PAR_7]);
    295 
    296     psF64 radius = sigma * sqrt (2.0 * z);
    297     psAssert (isfinite(radius), "fix this code: radius should not be nan for %f, %f", PAR[PM_PAR_7], sigma);
    298 
    299     if (isnan(radius))
    300         psAbort("error in code: radius is NaN");
    301 
     343
     344    // f = Io exp(-z^n) -> z^n = ln(Io/f)
     345    psF64 zn = log(PAR[PM_PAR_I0] / flux);
     346    psF64 radius = axes.major * sqrt (2.0) * pow(zn, 0.5 / PAR[PM_PAR_7]);
     347
     348    // fprintf (stderr, "sersic model %f %f, n %f, radius: %f, zn: %f, f/Io: %f, major: %f\n", PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], PAR[PM_PAR_7], radius, zn, flux/PAR[PM_PAR_I0], axes.major);
     349
     350    psAssert (isfinite(radius), "fix this code: z should not be nan for %f", PAR[PM_PAR_7]);
    302351    return (radius);
    303352}
     
    448497    return;
    449498}
    450 
    451 # undef PM_MODEL_FUNC
    452 # undef PM_MODEL_FLUX
    453 # undef PM_MODEL_GUESS
    454 # undef PM_MODEL_LIMITS
    455 # undef PM_MODEL_RADIUS
    456 # undef PM_MODEL_FROM_PSF
    457 # undef PM_MODEL_PARAMS_FROM_PSF
    458 # undef PM_MODEL_FIT_STATUS
    459 # undef PM_MODEL_SET_LIMITS
  • branches/sc_branches/trunkTest/psModules/src/objects/pmDetEff.c

    r25477 r29060  
    33#endif
    44
     5#include <string.h>
    56#include <pslib.h>
     7#include "pmHDU.h"
     8#include "pmFPA.h"
    69
     10#include "pmTrend2D.h"
     11#include "pmResiduals.h"
     12#include "pmGrowthCurve.h"
     13#include "pmSpan.h"
     14#include "pmFootprintSpans.h"
     15#include "pmFootprint.h"
     16#include "pmPeaks.h"
     17#include "pmMoments.h"
     18#include "pmModelFuncs.h"
     19#include "pmModel.h"
     20#include "pmModelUtils.h"
     21#include "pmModelClass.h"
     22#include "pmSourceMasks.h"
     23#include "pmSourceExtendedPars.h"
     24#include "pmSourceDiffStats.h"
     25#include "pmSource.h"
     26#include "pmSourceFitModel.h"
     27#include "pmPSF.h"
     28#include "pmPSFtry.h"
     29#include "pmDetections.h"
    730#include "pmDetEff.h"
    8 
    931
    1032static void detEffFree(pmDetEff *de)
  • branches/sc_branches/trunkTest/psModules/src/objects/pmDetEff.h

    r25383 r29060  
    11#ifndef PM_DET_EFF_H
    22#define PM_DET_EFF_H
    3 
    4 #include <pslib.h>
    5 #include <string.h>
    6 
    7 #include "pmFPA.h"
    83
    94#define PM_DETEFF_ANALYSIS "DETEFF"     // Location of detection efficiency on pmReadout.analysis
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprint.c

    r27672 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    2223#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprint.h

    r27672 r29060  
    1010#ifndef PM_FOOTPRINT_H
    1111#define PM_FOOTPRINT_H
    12 
    13 #include <pslib.h>
    14 #include "pmSpan.h"
    15 #include "pmFootprintSpans.h"
    1612
    1713typedef struct {
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintArrayGrow.c

    r21183 r29060  
    1818#include <pslib.h>
    1919#include "pmSpan.h"
     20#include "pmFootprintSpans.h"
    2021#include "pmFootprint.h"
    2122#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintArraysMerge.c

    r24274 r29060  
    1818#include <pslib.h>
    1919#include "pmSpan.h"
     20#include "pmFootprintSpans.h"
    2021#include "pmFootprint.h"
    2122#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintAssignPeaks.c

    r20937 r29060  
    1818#include <pslib.h>
    1919#include "pmSpan.h"
     20#include "pmFootprintSpans.h"
    2021#include "pmFootprint.h"
    2122#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintCullPeaks.c

    r27672 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    22 #include "pmFootprintSpans.h"
    2323#include "pmPeaks.h"
    2424
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintFind.c

    r20937 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    2223#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintFindAtPoint.c

    r27672 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    2223#include "pmPeaks.h"
    23 #include "pmFootprintSpans.h"
    2424
    2525/************************************************************************************************************/
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintIDs.c

    r27672 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    2223#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintSpans.c

    r27672 r29060  
    1919#include <pslib.h>
    2020#include "pmSpan.h"
     21#include "pmFootprintSpans.h"
    2122#include "pmFootprint.h"
    2223#include "pmPeaks.h"
    23 #include "pmFootprintSpans.h"
    2424
    2525static void pmStartSpanFree(pmStartSpan *sspan) {
  • branches/sc_branches/trunkTest/psModules/src/objects/pmFootprintSpans.h

    r27672 r29060  
    1010#ifndef PM_FOOTPRINT_SPANS_H
    1111#define PM_FOOTPRINT_SPANS_H
    12 
    13 #include <pslib.h>
    14 #include "pmSpan.h"
    1512
    1613/* We define two helper structures used in building the pmFootprints:
  • branches/sc_branches/trunkTest/psModules/src/objects/pmGrowthCurve.c

    r20937 r29060  
    2020#include "pmHDU.h"
    2121#include "pmFPA.h"
     22
     23#include "pmTrend2D.h"
     24#include "pmResiduals.h"
     25#include "pmGrowthCurve.h"
    2226#include "pmSpan.h"
     27#include "pmFootprintSpans.h"
    2328#include "pmFootprint.h"
    2429#include "pmPeaks.h"
    2530#include "pmMoments.h"
    26 #include "pmResiduals.h"
    27 #include "pmGrowthCurve.h"
    28 #include "pmTrend2D.h"
     31#include "pmModelFuncs.h"
     32#include "pmModel.h"
     33#include "pmModelUtils.h"
     34#include "pmModelClass.h"
     35#include "pmSourceMasks.h"
     36#include "pmSourceExtendedPars.h"
     37#include "pmSourceDiffStats.h"
     38#include "pmSource.h"
     39#include "pmSourceFitModel.h"
    2940#include "pmPSF.h"
    30 #include "pmModel.h"
    31 #include "pmSource.h"
     41#include "pmPSFtry.h"
     42#include "pmDetections.h"
     43
    3244#include "psVectorBracket.h"
    3345
  • branches/sc_branches/trunkTest/psModules/src/objects/pmGrowthCurveGenerate.c

    r27531 r29060  
    2424#include "pmHDU.h"
    2525#include "pmFPA.h"
     26#include "pmFPAMaskWeight.h"
     27#include "psVectorBracket.h"
     28#include "pmErrorCodes.h"
     29
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    2633#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    2735#include "pmFootprint.h"
    2836#include "pmPeaks.h"
    2937#include "pmMoments.h"
    30 #include "pmResiduals.h"
    31 #include "pmGrowthCurve.h"
    32 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3347#include "pmPSF.h"
    34 #include "pmModel.h"
    35 #include "pmSource.h"
    36 #include "pmModelClass.h"
    37 #include "pmModelUtils.h"
     48#include "pmPSFtry.h"
     49#include "pmDetections.h"
     50
    3851#include "pmSourcePhotometry.h"
    39 #include "pmFPAMaskWeight.h"
    40 #include "psVectorBracket.h"
    41 #include "pmErrorCodes.h"
    4252
    4353pmGrowthCurve *pmGrowthCurveForPosition (psImage *image, pmPSF *psf, bool ignore, psImageMaskType maskVal, psImageMaskType markVal, float xc, float yc);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmModel.c

    r25754 r29060  
    2323#include "pmHDU.h"
    2424#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
    2527#include "pmResiduals.h"
    2628#include "pmGrowthCurve.h"
    27 #include "pmTrend2D.h"
    28 #include "pmPSF.h"
     29#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
     31#include "pmFootprint.h"
     32#include "pmPeaks.h"
     33#include "pmMoments.h"
     34#include "pmModelFuncs.h"
    2935#include "pmModel.h"
    3036#include "pmModelClass.h"
     
    7581
    7682    for (psS32 i = 0; i < tmp->params->n; i++) {
    77         tmp->params->data.F32[i] = 0.0;
    78         tmp->dparams->data.F32[i] = 0.0;
     83        tmp->params->data.F32[i] = NAN;
     84        tmp->dparams->data.F32[i] = NAN;
    7985    }
    8086
  • branches/sc_branches/trunkTest/psModules/src/objects/pmModel.h

    r26916 r29060  
    1414# define PM_MODEL_H
    1515
    16 #include <pslib.h>
    17 #include "pmPSF.h"
    18 
    1916/// @addtogroup Objects Object Detection / Analysis Functions
    2017/// @{
     
    2219/* pointers for the functions types below are supplied to each pmModel, and can be used by
    2320   the programmer without needing to know the model class */
    24 
    25 typedef enum {
    26     PM_MODEL_STATUS_NONE         = 0x00, ///< model fit not yet attempted, no other info
    27     PM_MODEL_STATUS_FITTED       = 0x01, ///< model fit completed
    28     PM_MODEL_STATUS_NONCONVERGE  = 0x02, ///< model fit did not converge
    29     PM_MODEL_STATUS_OFFIMAGE     = 0x04, ///< model fit drove out of range
    30     PM_MODEL_STATUS_BADARGS      = 0x08, ///< model fit called with invalid args
    31     PM_MODEL_STATUS_LIMITS       = 0x10  ///< model parameters hit limits
    32 } pmModelStatus;
    33 
    34 typedef enum {
    35     PM_MODEL_OP_NONE    = 0x00,
    36     PM_MODEL_OP_FUNC    = 0x01,
    37     PM_MODEL_OP_RES0    = 0x02,
    38     PM_MODEL_OP_RES1    = 0x04,
    39     PM_MODEL_OP_FULL    = 0x07,
    40     PM_MODEL_OP_SKY     = 0x08,
    41     PM_MODEL_OP_CENTER  = 0x10,
    42     PM_MODEL_OP_NORM    = 0x20,
    43     PM_MODEL_OP_NOISE   = 0x40,
    44 } pmModelOpMode;
    45 
    46 /// Parameter limit types
    47 typedef enum {
    48     PM_MODEL_LIMITS_NONE,               ///< Apply no limits: suitable for debugging
    49     PM_MODEL_LIMITS_IGNORE,             ///< Ignore all limits: fit can go to town
    50     PM_MODEL_LIMITS_LAX,                ///< Lax limits: attempting to reproduce even bad data
    51     PM_MODEL_LIMITS_MODERATE,           ///< Moderate limits: cope with mildly bad data
    52     PM_MODEL_LIMITS_STRICT,             ///< Strict limits: insist on good quality data
    53 } pmModelLimitsType;
    54 
    55 typedef struct pmModel pmModel;
    56 typedef struct pmSource pmSource;
    57 
    58 //  This function is the model chi-square minimization function for this model.
    59 typedef psMinimizeLMChi2Func pmModelFunc;
    60 
    61 //  This function sets the parameter limits for this model.
    62 typedef psMinimizeLMLimitFunc pmModelLimits;
    63 
    64 // This function returns the integrated flux for the given model parameters.
    65 typedef psF64 (*pmModelFlux)(const psVector *params);
    66 
    67 // This function returns the radius at which the given model and parameters
    68 // achieves the given flux.
    69 typedef psF64 (*pmModelRadius)(const psVector *params, double flux);
    70 
    71 //  This function provides the model guess parameters based on the details of
    72 //  the given source.
    73 typedef bool (*pmModelGuessFunc)(pmModel *model, pmSource *source);
    74 
    75 //  This function constructs the PSF model for the given source based on the
    76 //  supplied psf and the EXT model for the object.
    77 typedef bool (*pmModelFromPSFFunc)(pmModel *modelPSF, pmModel *modelEXT, const pmPSF *psf);
    78 
    79 //  This function sets the model parameters based on the PSF for a given coordinate and central
    80 //  intensity
    81 typedef bool (*pmModelParamsFromPSF)(pmModel *model, const pmPSF *psf, float Xo, float Yo, float Io);
    82 
    83 //  This function returns the success / failure status of the given model fit
    84 typedef bool (*pmModelFitStatusFunc)(pmModel *model);
    85 
    86 //  This function sets the parameter limits for the given model
    87 typedef bool (*pmModelSetLimitsFunc)(pmModelLimitsType type);
    8821
    8922/** pmModel data structure
     
    12255    pmModelSetLimitsFunc modelSetLimits;
    12356};
    124 
    125 /** Symbolic names for the elements of [d]params
    126  * Note: these are #defines not enums as a given element of [d]params
    127  * may/will correspond to different parameters in different contexts
    128  */
    129 #define PM_PAR_SKY 0   ///< Sky
    130 #define PM_PAR_I0 1   ///< Central intensity
    131 #define PM_PAR_XPOS 2   ///< X centre of object
    132 #define PM_PAR_YPOS 3   ///< Y centre of object
    133 #define PM_PAR_SXX 4   ///< shape X^2 moment
    134 #define PM_PAR_SYY 5   ///< shape Y^2 moment
    135 #define PM_PAR_SXY 6   ///< shape XY moment
    136 #define PM_PAR_7 7   ///< ??? Unknown parameter
    137 #define PM_PAR_8 8   ///< ??? Unknown parameter
    13857
    13958/** pmModelAlloc()
  • branches/sc_branches/trunkTest/psModules/src/objects/pmModelClass.c

    r25754 r29060  
    2323#include "pmHDU.h"
    2424#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
    2529#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
    2631#include "pmFootprint.h"
    2732#include "pmPeaks.h"
    2833#include "pmMoments.h"
    29 #include "pmResiduals.h"
    30 #include "pmGrowthCurve.h"
    31 #include "pmTrend2D.h"
    32 #include "pmPSF.h"
     34#include "pmModelFuncs.h"
    3335#include "pmModel.h"
    34 #include "pmSource.h"
     36#include "pmModelUtils.h"
    3537#include "pmModelClass.h"
     38
    3639#include "pmErrorCodes.h"
    3740
     
    4649# include "models/pmModel_RGAUSS.h"
    4750# include "models/pmModel_SERSIC.h"
     51# include "models/pmModel_EXP.h"
     52# include "models/pmModel_DEV.h"
    4853
    4954static pmModelClass defaultModels[] = {
     
    5358    {"PS_MODEL_PS1_V1",       8, (pmModelFunc)pmModelFunc_PS1_V1,  (pmModelFlux)pmModelFlux_PS1_V1,  (pmModelRadius)pmModelRadius_PS1_V1,  (pmModelLimits)pmModelLimits_PS1_V1,  (pmModelGuessFunc)pmModelGuess_PS1_V1, (pmModelFromPSFFunc)pmModelFromPSF_PS1_V1, (pmModelParamsFromPSF)pmModelParamsFromPSF_PS1_V1, (pmModelFitStatusFunc)pmModelFitStatus_PS1_V1, (pmModelSetLimitsFunc)pmModelSetLimits_PS1_V1 },
    5459    {"PS_MODEL_RGAUSS",       8, (pmModelFunc)pmModelFunc_RGAUSS,  (pmModelFlux)pmModelFlux_RGAUSS,  (pmModelRadius)pmModelRadius_RGAUSS,  (pmModelLimits)pmModelLimits_RGAUSS,  (pmModelGuessFunc)pmModelGuess_RGAUSS, (pmModelFromPSFFunc)pmModelFromPSF_RGAUSS, (pmModelParamsFromPSF)pmModelParamsFromPSF_RGAUSS, (pmModelFitStatusFunc)pmModelFitStatus_RGAUSS, (pmModelSetLimitsFunc)pmModelSetLimits_RGAUSS },
    55     {"PS_MODEL_SERSIC",       8, (pmModelFunc)pmModelFunc_SERSIC,  (pmModelFlux)pmModelFlux_SERSIC,  (pmModelRadius)pmModelRadius_SERSIC,  (pmModelLimits)pmModelLimits_SERSIC,  (pmModelGuessFunc)pmModelGuess_SERSIC, (pmModelFromPSFFunc)pmModelFromPSF_SERSIC, (pmModelParamsFromPSF)pmModelParamsFromPSF_SERSIC, (pmModelFitStatusFunc)pmModelFitStatus_SERSIC, (pmModelSetLimitsFunc)pmModelSetLimits_SERSIC }
     60    {"PS_MODEL_SERSIC",       8, (pmModelFunc)pmModelFunc_SERSIC,  (pmModelFlux)pmModelFlux_SERSIC,  (pmModelRadius)pmModelRadius_SERSIC,  (pmModelLimits)pmModelLimits_SERSIC,  (pmModelGuessFunc)pmModelGuess_SERSIC, (pmModelFromPSFFunc)pmModelFromPSF_SERSIC, (pmModelParamsFromPSF)pmModelParamsFromPSF_SERSIC, (pmModelFitStatusFunc)pmModelFitStatus_SERSIC, (pmModelSetLimitsFunc)pmModelSetLimits_SERSIC },
     61    {"PS_MODEL_EXP",          7, (pmModelFunc)pmModelFunc_EXP,     (pmModelFlux)pmModelFlux_EXP,     (pmModelRadius)pmModelRadius_EXP,     (pmModelLimits)pmModelLimits_EXP,     (pmModelGuessFunc)pmModelGuess_EXP,    (pmModelFromPSFFunc)pmModelFromPSF_EXP,    (pmModelParamsFromPSF)pmModelParamsFromPSF_EXP,    (pmModelFitStatusFunc)pmModelFitStatus_EXP,    (pmModelSetLimitsFunc)pmModelSetLimits_EXP },
     62    {"PS_MODEL_DEV",          7, (pmModelFunc)pmModelFunc_DEV,     (pmModelFlux)pmModelFlux_DEV,     (pmModelRadius)pmModelRadius_DEV,     (pmModelLimits)pmModelLimits_DEV,     (pmModelGuessFunc)pmModelGuess_DEV,    (pmModelFromPSFFunc)pmModelFromPSF_DEV,    (pmModelParamsFromPSF)pmModelParamsFromPSF_DEV,    (pmModelFitStatusFunc)pmModelFitStatus_DEV,    (pmModelSetLimitsFunc)pmModelSetLimits_DEV },
    5663};
    5764
  • branches/sc_branches/trunkTest/psModules/src/objects/pmModelClass.h

    r25738 r29060  
    2828# ifndef PM_MODEL_CLASS_H
    2929# define PM_MODEL_CLASS_H
    30 
    31 #include <pmModel.h>
    3230
    3331/// @addtogroup Objects Object Detection / Analysis Functions
  • branches/sc_branches/trunkTest/psModules/src/objects/pmModelUtils.c

    r25754 r29060  
    2222#include "pmHDU.h"
    2323#include "pmFPA.h"
     24
     25#include "pmTrend2D.h"
    2426#include "pmResiduals.h"
    2527#include "pmGrowthCurve.h"
    26 #include "pmTrend2D.h"
     28#include "pmSpan.h"
     29#include "pmFootprintSpans.h"
     30#include "pmFootprint.h"
     31#include "pmPeaks.h"
     32#include "pmMoments.h"
     33#include "pmModelFuncs.h"
     34#include "pmModel.h"
     35#include "pmModelUtils.h"
     36#include "pmModelClass.h"
     37#include "pmSourceMasks.h"
     38#include "pmSourceExtendedPars.h"
     39#include "pmSourceDiffStats.h"
     40#include "pmSource.h"
     41#include "pmSourceFitModel.h"
    2742#include "pmPSF.h"
    28 #include "pmModel.h"
     43
    2944#include "pmErrorCodes.h"
    30 #include "pmModelUtils.h"
    3145
    3246/*****************************************************************************
  • branches/sc_branches/trunkTest/psModules/src/objects/pmMoments.c

    r24498 r29060  
    2929    psTrace("psModules.objects", 10, "---- %s() begin ----\n", __func__);
    3030    pmMoments *tmp = (pmMoments *) psAlloc(sizeof(pmMoments));
     31
     32    tmp->Mrf = 0.0;
     33    tmp->Mrh = 0.0;
     34    tmp->KronFlux = 0.0;
     35    tmp->KronFluxErr = 0.0;
     36
     37    tmp->KronFinner = 0.0;
     38    tmp->KronFouter = 0.0;
     39
    3140    tmp->Mx = 0.0;
    3241    tmp->My = 0.0;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmMoments.h

    r23487 r29060  
    2424typedef struct
    2525{
     26    float Mrf;    ///< radial first moment
     27    float Mrh;    ///< radial half moment
     28
    2629    float Mx;     ///< X-coord of centroid.
    2730    float My;     ///< Y-coord of centroid.
     
    4750    float SN;     ///< approx signal-to-noise
    4851    int nPixels;  ///< Number of pixels used.
     52
     53    float KronFlux;    ///< Kron flux (flux in 2.5*Mrf)
     54    float KronFluxErr; ///< Kron flux error
     55
     56    float KronFinner;    ///< Kron flux (flux in 2.5*Mrf)
     57    float KronFouter;    ///< Kron flux (flux in 2.5*Mrf)
     58
    4959}
    5060pmMoments;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSF.c

    r26893 r29060  
    2525#include "pmHDU.h"
    2626#include "pmFPA.h"
     27#include "pmFPAMaskWeight.h"
     28#include "psVectorBracket.h"
     29
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    2733#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    2835#include "pmFootprint.h"
    2936#include "pmPeaks.h"
    3037#include "pmMoments.h"
    31 #include "pmResiduals.h"
    32 #include "pmGrowthCurve.h"
    33 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3447#include "pmPSF.h"
    35 #include "pmModel.h"
    36 #include "pmSource.h"
    37 #include "pmModelClass.h"
    38 #include "pmModelUtils.h"
    39 #include "pmSourcePhotometry.h"
    40 #include "pmFPAMaskWeight.h"
    41 #include "psVectorBracket.h"
     48#include "pmPSFtry.h"
     49#include "pmDetections.h"
     50
    4251#include "pmErrorCodes.h"
    4352
     
    5463
    5564    psFree (options->stats);
     65    psFree (options->fitOptions);
    5666    return;
    5767}
     
    6575
    6676    options->stats         = NULL;
     77    options->fitOptions    = NULL; // XXX this has to be set before calling pmPSF fit functions
    6778
    6879    options->psfTrendMode  = PM_TREND_NONE;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSF.h

    r26893 r29060  
    1414# define PM_PSF_H
    1515
    16 #include <pslib.h>
    17 #include "pmTrend2D.h"
    18 #include "pmGrowthCurve.h"
    19 #include "pmResiduals.h"
    20 #include "pmFPA.h"
    21 
    2216/// @addtogroup Objects Object Detection / Analysis Functions
    2317/// @{
    24 
    25 // type of model carried by the pmModel structure
    26 typedef int pmModelType;
    2718
    2819/** pmPSF data structure
     
    3829 *
    3930 */
    40 typedef struct {
     31struct pmPSF {
    4132    pmModelType type;                   ///< PSF Model in use
    4233    psArray *params;                    ///< Model parameters (psPolynomial2D)
     
    6455    pmGrowthCurve *growth;              ///< apMag vs Radius
    6556    pmResiduals *residuals;             ///< normalized residual image (no spatial variation)
    66 } pmPSF;
     57};
    6758
    6859typedef struct {
     
    8273    float         apRadius;
    8374    bool          chiFluxTrend;         // Fit a trend in Chi2 as a function of flux?
     75    pmSourceFitOptions *fitOptions;
    8476} pmPSFOptions;
    8577
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSF_IO.c

    r28449 r29060  
    3838#include "pmFPAfileFitsIO.h"
    3939
     40#include "pmTrend2D.h"
     41#include "pmResiduals.h"
     42#include "pmGrowthCurve.h"
    4043#include "pmSpan.h"
     44#include "pmFootprintSpans.h"
    4145#include "pmFootprint.h"
    4246#include "pmPeaks.h"
    4347#include "pmMoments.h"
    44 #include "pmGrowthCurve.h"
    45 #include "pmResiduals.h"
    46 #include "pmTrend2D.h"
     48#include "pmModelFuncs.h"
     49#include "pmModel.h"
     50#include "pmModelUtils.h"
     51#include "pmModelClass.h"
     52#include "pmSourceMasks.h"
     53#include "pmSourceExtendedPars.h"
     54#include "pmSourceDiffStats.h"
     55#include "pmSource.h"
     56#include "pmSourceFitModel.h"
    4757#include "pmPSF.h"
    48 #include "pmModel.h"
     58#include "pmPSFtry.h"
     59#include "pmDetections.h"
     60
    4961#include "pmPSF_IO.h"
    50 #include "pmSource.h"
    51 #include "pmModelClass.h"
    52 #include "pmModelUtils.h"
    5362#include "pmSourceIO.h"
    5463
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtry.c

    r25754 r29060  
    1919#include "pmFPA.h"
    2020#include "pmFPAMaskWeight.h"
     21
     22#include "pmTrend2D.h"
     23#include "pmResiduals.h"
     24#include "pmGrowthCurve.h"
    2125#include "pmSpan.h"
     26#include "pmFootprintSpans.h"
    2227#include "pmFootprint.h"
    2328#include "pmPeaks.h"
    2429#include "pmMoments.h"
    25 #include "pmResiduals.h"
    26 #include "pmGrowthCurve.h"
    27 #include "pmTrend2D.h"
     30#include "pmModelFuncs.h"
     31#include "pmModel.h"
     32#include "pmModelUtils.h"
     33#include "pmModelClass.h"
     34#include "pmSourceMasks.h"
     35#include "pmSourceExtendedPars.h"
     36#include "pmSourceDiffStats.h"
     37#include "pmSource.h"
     38#include "pmSourceFitModel.h"
    2839#include "pmPSF.h"
    29 #include "pmModel.h"
    30 #include "pmSource.h"
    31 #include "pmSourceUtils.h"
    3240#include "pmPSFtry.h"
    33 #include "pmModelClass.h"
    34 #include "pmModelUtils.h"
    35 #include "pmSourceFitModel.h"
     41#include "pmDetections.h"
     42
    3643#include "pmSourcePhotometry.h"
    3744#include "pmSourceVisual.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtryFitEXT.c

    r25754 r29060  
    1919#include "pmFPA.h"
    2020#include "pmFPAMaskWeight.h"
     21
     22#include "pmTrend2D.h"
     23#include "pmResiduals.h"
     24#include "pmGrowthCurve.h"
    2125#include "pmSpan.h"
     26#include "pmFootprintSpans.h"
    2227#include "pmFootprint.h"
    2328#include "pmPeaks.h"
    2429#include "pmMoments.h"
    25 #include "pmResiduals.h"
    26 #include "pmGrowthCurve.h"
    27 #include "pmTrend2D.h"
    28 #include "pmPSF.h"
     30#include "pmModelFuncs.h"
    2931#include "pmModel.h"
     32#include "pmModelUtils.h"
     33#include "pmModelClass.h"
     34#include "pmSourceMasks.h"
     35#include "pmSourceExtendedPars.h"
     36#include "pmSourceDiffStats.h"
    3037#include "pmSource.h"
    3138#include "pmSourceUtils.h"
     39#include "pmSourceFitModel.h"
     40#include "pmPSF.h"
    3241#include "pmPSFtry.h"
    33 #include "pmModelClass.h"
    34 #include "pmModelUtils.h"
    35 #include "pmSourceFitModel.h"
     42#include "pmDetections.h"
     43
    3644#include "pmSourcePhotometry.h"
    3745#include "pmSourceVisual.h"
     
    4452
    4553    psTimerStart ("psf.fit");
     54
     55    // in this segment, we are fitting the full PSF model class (shape unconstrained)
     56    options->fitOptions->mode = PM_SOURCE_FIT_EXT;
    4657
    4758    // maskVal is used to test for rejected pixels, and must include markVal
     
    7384
    7485        // fit model as EXT, not PSF
    75         status = pmSourceFitModel (source, source->modelEXT, PM_SOURCE_FIT_EXT, maskVal);
     86        status = pmSourceFitModel (source, source->modelEXT, options->fitOptions, maskVal);
    7687
    7788        // clear object mask to define valid pixels
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtryFitPSF.c

    r26027 r29060  
    1717#include "pmFPA.h"
    1818#include "pmFPAMaskWeight.h"
     19
     20#include "pmTrend2D.h"
     21#include "pmResiduals.h"
     22#include "pmGrowthCurve.h"
    1923#include "pmSpan.h"
     24#include "pmFootprintSpans.h"
    2025#include "pmFootprint.h"
    2126#include "pmPeaks.h"
    2227#include "pmMoments.h"
    23 #include "pmResiduals.h"
    24 #include "pmGrowthCurve.h"
    25 #include "pmTrend2D.h"
     28#include "pmModelFuncs.h"
     29#include "pmModel.h"
     30#include "pmModelUtils.h"
     31#include "pmModelClass.h"
     32#include "pmSourceMasks.h"
     33#include "pmSourceExtendedPars.h"
     34#include "pmSourceDiffStats.h"
     35#include "pmSource.h"
     36#include "pmSourceFitModel.h"
    2637#include "pmPSF.h"
    27 #include "pmModel.h"
    28 #include "pmSource.h"
    29 #include "pmSourceUtils.h"
    3038#include "pmPSFtry.h"
    31 #include "pmModelClass.h"
    32 #include "pmModelUtils.h"
    33 #include "pmSourceFitModel.h"
     39#include "pmDetections.h"
     40
    3441#include "pmSourcePhotometry.h"
    3542#include "pmSourceVisual.h"
     
    4249
    4350    psTimerStart ("psf.fit");
     51
     52    // in this segment, we are fitting the fitted PSF model class (shape constrained)
     53    options->fitOptions->mode = PM_SOURCE_FIT_PSF;
    4454
    4555    // maskVal is used to test for rejected pixels, and must include markVal
     
    8191
    8292        // fit the PSF model to the source
    83         status = pmSourceFitModel (source, source->modelPSF, PM_SOURCE_FIT_NORM, maskVal);
     93        status = pmSourceFitModel (source, source->modelPSF, options->fitOptions, maskVal);
    8494
    8595        // skip poor fits
     
    98108
    99109        // This function calculates the psf and aperture magnitudes
    100         status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal); // raw PSF mag, AP mag
     110        status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal, markVal); // raw PSF mag, AP mag
    101111        if (!status || isnan(source->apMag)) {
    102112            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtryMakePSF.c

    r25754 r29060  
    1818#include "pmFPA.h"
    1919#include "pmFPAMaskWeight.h"
     20
     21#include "pmTrend2D.h"
     22#include "pmResiduals.h"
     23#include "pmGrowthCurve.h"
    2024#include "pmSpan.h"
     25#include "pmFootprintSpans.h"
    2126#include "pmFootprint.h"
    2227#include "pmPeaks.h"
    2328#include "pmMoments.h"
    24 #include "pmResiduals.h"
    25 #include "pmGrowthCurve.h"
    26 #include "pmTrend2D.h"
     29#include "pmModelFuncs.h"
     30#include "pmModel.h"
     31#include "pmModelUtils.h"
     32#include "pmModelClass.h"
     33#include "pmSourceMasks.h"
     34#include "pmSourceExtendedPars.h"
     35#include "pmSourceDiffStats.h"
     36#include "pmSource.h"
     37#include "pmSourceFitModel.h"
    2738#include "pmPSF.h"
    28 #include "pmModel.h"
    29 #include "pmSource.h"
    30 #include "pmSourceUtils.h"
    3139#include "pmPSFtry.h"
    32 #include "pmModelClass.h"
    33 #include "pmModelUtils.h"
    34 #include "pmSourceFitModel.h"
     40#include "pmDetections.h"
     41
    3542#include "pmSourcePhotometry.h"
    3643#include "pmSourceVisual.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtryMetric.c

    r26260 r29060  
    1818#include "pmFPA.h"
    1919#include "pmFPAMaskWeight.h"
     20
     21#include "pmTrend2D.h"
     22#include "pmResiduals.h"
     23#include "pmGrowthCurve.h"
    2024#include "pmSpan.h"
     25#include "pmFootprintSpans.h"
    2126#include "pmFootprint.h"
    2227#include "pmPeaks.h"
    2328#include "pmMoments.h"
    24 #include "pmResiduals.h"
    25 #include "pmGrowthCurve.h"
    26 #include "pmTrend2D.h"
     29#include "pmModelFuncs.h"
     30#include "pmModel.h"
     31#include "pmModelUtils.h"
     32#include "pmModelClass.h"
     33#include "pmSourceMasks.h"
     34#include "pmSourceExtendedPars.h"
     35#include "pmSourceDiffStats.h"
     36#include "pmSource.h"
     37#include "pmSourceFitModel.h"
    2738#include "pmPSF.h"
    28 #include "pmModel.h"
    29 #include "pmSource.h"
    30 #include "pmSourceUtils.h"
    3139#include "pmPSFtry.h"
    32 #include "pmModelClass.h"
    33 #include "pmModelUtils.h"
    34 #include "pmSourceFitModel.h"
     40#include "pmDetections.h"
     41
    3542#include "pmSourcePhotometry.h"
    3643#include "pmSourceVisual.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPSFtryModel.c

    r27818 r29060  
    1919#include "pmFPA.h"
    2020#include "pmFPAMaskWeight.h"
     21
     22#include "pmTrend2D.h"
     23#include "pmResiduals.h"
     24#include "pmGrowthCurve.h"
    2125#include "pmSpan.h"
     26#include "pmFootprintSpans.h"
    2227#include "pmFootprint.h"
    2328#include "pmPeaks.h"
    2429#include "pmMoments.h"
    25 #include "pmResiduals.h"
    26 #include "pmGrowthCurve.h"
    27 #include "pmTrend2D.h"
     30#include "pmModelFuncs.h"
     31#include "pmModel.h"
     32#include "pmModelUtils.h"
     33#include "pmModelClass.h"
     34#include "pmSourceMasks.h"
     35#include "pmSourceExtendedPars.h"
     36#include "pmSourceDiffStats.h"
     37#include "pmSource.h"
     38#include "pmSourceFitModel.h"
    2839#include "pmPSF.h"
    29 #include "pmModel.h"
    30 #include "pmSource.h"
    31 #include "pmSourceUtils.h"
    3240#include "pmPSFtry.h"
    33 #include "pmModelClass.h"
    34 #include "pmModelUtils.h"
    35 #include "pmSourceFitModel.h"
    36 #include "pmSourcePhotometry.h"
     41#include "pmDetections.h"
     42
    3743#include "pmSourceVisual.h"
    3844
     
    4955pmPSFtry *pmPSFtryModel (const psArray *sources, const char *modelName, pmPSFOptions *options, psImageMaskType maskVal, psImageMaskType markVal)
    5056{
     57    assert (options->fitOptions);
     58
    5159    // validate the requested model name
    5260    options->type = pmModelClassGetType (modelName);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPeaks.c

    r26893 r29060  
    2222#include <pslib.h>
    2323#include "pmSpan.h"
     24#include "pmFootprintSpans.h"
    2425#include "pmFootprint.h"
    2526#include "pmPeaks.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPeaks.h

    r27657 r29060  
    1717# ifndef PM_PEAKS_H
    1818# define PM_PEAKS_H
    19 
    20 #include <pslib.h>
    21 #include "pmFootprint.h"
    2219
    2320/// @addtogroup Objects Object Detection / Analysis Functions
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPhotObj.c

    r28013 r29060  
    1616#include <string.h>
    1717#include <pslib.h>
     18
     19#include "pmHDU.h"
     20#include "pmFPA.h"
     21
     22#include "pmTrend2D.h"
     23#include "pmResiduals.h"
     24#include "pmGrowthCurve.h"
     25#include "pmSpan.h"
     26#include "pmFootprintSpans.h"
     27#include "pmFootprint.h"
     28#include "pmPeaks.h"
     29#include "pmMoments.h"
     30#include "pmModelFuncs.h"
     31#include "pmModel.h"
     32#include "pmModelUtils.h"
     33#include "pmModelClass.h"
     34#include "pmSourceMasks.h"
     35#include "pmSourceExtendedPars.h"
     36#include "pmSourceDiffStats.h"
     37#include "pmSource.h"
     38
    1839#include "pmPhotObj.h"
    19 #include "pmSource.h"
     40
    2041
    2142static void pmPhotObjFree (pmPhotObj *tmp)
     
    82103    return (0);
    83104}
     105
     106// sort by X (ascending)
     107int pmPhotObjSortByX (const void **a, const void **b)
     108{
     109    pmPhotObj *objA = *(pmPhotObj **)a;
     110    pmPhotObj *objB = *(pmPhotObj **)b;
     111
     112    psF32 fA = objA->x;
     113    psF32 fB = objB->x;
     114
     115    psF32 diff = fA - fB;
     116    if (diff > FLT_EPSILON) return (+1);
     117    if (diff < FLT_EPSILON) return (-1);
     118    return (0);
     119}
  • branches/sc_branches/trunkTest/psModules/src/objects/pmPhotObj.h

    r28013 r29060  
    1010# ifndef PM_PHOT_OBJ_H
    1111# define PM_PHOT_OBJ_H
    12 
    13 #include <pslib.h>
    14 #include "pmPeaks.h"
    15 #include "pmModel.h"
    16 #include "pmMoments.h"
    1712
    1813/// @addtogroup Objects Object Detection / Analysis Functions
     
    4742
    4843int pmPhotObjSortBySN (const void **a, const void **b);
     44int pmPhotObjSortByX (const void **a, const void **b);
    4945
    5046/// @}
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSource.c

    r28013 r29060  
    2323#include "pmFPA.h"
    2424#include "pmFPAMaskWeight.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
    2529#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
    2631#include "pmFootprint.h"
    2732#include "pmPeaks.h"
    2833#include "pmMoments.h"
    29 #include "pmResiduals.h"
    30 #include "pmGrowthCurve.h"
    31 #include "pmTrend2D.h"
    32 #include "pmPSF.h"
     34#include "pmModelFuncs.h"
    3335#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    3441#include "pmSource.h"
    3542
     
    98105    static int id = 1;
    99106    pmSource *source = (pmSource *) psAlloc(sizeof(pmSource));
    100     *(int *)&source->id = id++;
     107    P_PM_SOURCE_SET_ID(source, id++);
     108
    101109    source->seq = -1;
    102110    source->peak = NULL;
     
    114122    source->type = PM_SOURCE_TYPE_UNKNOWN;
    115123    source->mode = PM_SOURCE_MODE_DEFAULT;
     124    source->mode2 = PM_SOURCE_MODE_DEFAULT;
    116125    source->tmpFlags = 0;
    117126    source->extpars = NULL;
     
    131140    source->sky    = NAN;
    132141    source->skyErr = NAN;
    133     source->pixWeight = NAN;
     142    source->pixWeightNotBad = NAN;
     143    source->pixWeightNotPoor = NAN;
    134144
    135145    source->psfChisq = NAN;
     
    142152
    143153/******************************************************************************
    144 pmSourceCopy(): copy the pmSource structure and contents
    145 XXX : are we OK with incrementing the ID?
     154pmSourceCopy(): copy the pmSource, yielding a copy of the source that can be used without
     155affecting the original.  This Copy can be used to allow multiple fit attempts on the same
     156object.  The pixels, variance, and mask arrays all point to the same original subarrays.  The
     157peak and moments point at the original values.
    146158*****************************************************************************/
    147159pmSource *pmSourceCopy(pmSource *in)
     160{
     161    if (in == NULL) {
     162        return(NULL);
     163    }
     164    pmSource *source = pmSourceAlloc ();
     165
     166    // keep the original ID so we can find map back to the original
     167    P_PM_SOURCE_SET_ID(source, in->id);
     168
     169    // peak has the same values as the original
     170    if (in->peak != NULL) {
     171        source->peak = pmPeakAlloc (in->peak->x, in->peak->y, in->peak->value, in->peak->type);
     172        source->peak->xf = in->peak->xf;
     173        source->peak->yf = in->peak->yf;
     174        source->peak->flux = in->peak->flux;
     175        source->peak->SN = in->peak->SN;
     176    }
     177
     178    // copy the values in the moments structure
     179    if (in->moments != NULL) {
     180        source->moments  =  pmMomentsAlloc();
     181        *source->moments = *in->moments;
     182    }
     183
     184    // These images are all views to the parent.  We want a new view, but pointing at the same
     185    // pixels.  Modifying these pixels (ie, subtracting the model) will affect the pixels seen
     186    // by all copies.
     187    source->pixels   = psImageCopyView(NULL, in->pixels);
     188    source->variance   = psImageCopyView(NULL, in->variance);
     189    source->maskView = in->maskView ? psImageCopyView(NULL, in->maskView) : NULL;
     190
     191    // the maskObj is a unique mask array; create a new mask image
     192    source->maskObj = in->maskObj ? psImageCopy (NULL, in->maskObj, PS_TYPE_IMAGE_MASK) : NULL;
     193
     194    source->type = in->type;
     195    source->mode = in->mode;
     196    source->imageID = in->imageID;
     197
     198    return(source);
     199}
     200
     201/******************************************************************************
     202pmSourceCopyData(): this creates a new, duplicate source with the same parameters as the
     203original (but is actually a new source at the same location)
     204*****************************************************************************/
     205pmSource *pmSourceCopyData(pmSource *in)
    148206{
    149207    if (in == NULL) {
     
    482540        }
    483541        psfClump.X  = stats->clippedMean;
    484         psfClump.dX = stats->clippedStdev;
     542        psfClump.dX = hypot(stats->clippedStdev, PSF_CLUMP_GRID_SCALE);
    485543
    486544        if (!psVectorStats (stats, tmpSy, NULL, NULL, 0)) {
     
    489547        }
    490548        psfClump.Y  = stats->clippedMean;
    491         psfClump.dY = stats->clippedStdev;
     549        psfClump.dY = hypot(stats->clippedStdev, PSF_CLUMP_GRID_SCALE);
    492550
    493551        psTrace ("psModules.objects", 2, "clump  X,  Y: %f, %f\n", psfClump.X, psfClump.Y);
     
    910968    bool addNoise = mode & PM_MODEL_OP_NOISE;
    911969
    912     if (source->modelFlux) {
     970    // require the use of pmModelAddWithOffset if we are adding noise (because the model size and norm are rescaled)
     971    if (!addNoise && source->modelFlux) {
    913972        // add in the pixels from the modelFlux image
    914973        int dX = source->modelFlux->col0 - source->pixels->col0;
     
    931990
    932991        psF32 **target = source->pixels->data.F32;
    933         if (addNoise) {
    934             // when adding noise, we assume the shape and Io have been modified
    935             target = source->variance->data.F32;
    936         }
    937992
    938993        for (int iy = 0; iy < source->modelFlux->numRows; iy++) {
     
    9491004            }
    9501005        }
    951         if (!addNoise) {
    952             if (add) {
    953                 source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
    954             } else {
    955                 source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
    956             }
     1006        if (add) {
     1007            source->tmpFlags &= ~PM_SOURCE_TMPF_SUBTRACTED;
     1008        } else {
     1009            source->tmpFlags |= PM_SOURCE_TMPF_SUBTRACTED;
    9571010        }
    9581011        return true;
     
    9731026        }
    9741027    }
     1028
     1029    return true;
     1030}
     1031
     1032// should we call pmSourceCacheModel if it does not exist?
     1033bool pmSourceNoiseOp (pmSource *source, pmModelOpMode mode, float FACTOR, float SIZE, bool add, psImageMaskType maskVal, int dx, int dy)
     1034{
     1035    assert (mode & PM_MODEL_OP_NOISE);
     1036    PS_ASSERT_PTR_NON_NULL(source, false);
     1037    PS_ASSERT_PTR_NON_NULL(source->peak, false);
     1038
     1039    if (add) {
     1040        psTrace ("psphot", 3, "adding noise to object at %f,%f\n", source->peak->xf, source->peak->yf);
     1041    } else {
     1042        psTrace ("psphot", 3, "removing noise from object at %f,%f\n", source->peak->xf, source->peak->yf);
     1043    }
     1044
     1045    pmSourceNoiseOpModel (source->modelPSF, source, mode, FACTOR, SIZE, add, maskVal, dx, dy);
     1046
     1047    if (source->modelEXT) {
     1048        pmSourceNoiseOpModel (source->modelEXT, source, mode, FACTOR, SIZE, add, maskVal, dx, dy);
     1049    }
     1050
     1051    return true;
     1052}
     1053
     1054bool pmSourceNoiseOpModel (pmModel *model, pmSource *source, pmModelOpMode mode, float FACTOR, float SIZE, bool add, psImageMaskType maskVal, int dx, int dy)
     1055{
     1056    bool status;
     1057    psEllipseShape oldshape;
     1058    psEllipseShape newshape;
     1059    psEllipseAxes axes;
     1060
     1061    if (add) {
     1062        psTrace ("psphot", 4, "adding noise for object at %f,%f\n", model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
     1063    } else {
     1064        psTrace ("psphot", 4, "remove noise for object at %f,%f\n", model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
     1065    }
     1066
     1067    psF32 *PAR = model->params->data.F32;
     1068
     1069    // save original values
     1070    float oldI0  = PAR[PM_PAR_I0];
     1071    oldshape.sx  = PAR[PM_PAR_SXX];
     1072    oldshape.sy  = PAR[PM_PAR_SYY];
     1073    oldshape.sxy = PAR[PM_PAR_SXY];
     1074
     1075    // XXX can this be done more intelligently?
     1076    if (oldI0 == 0.0) return false;
     1077    if (!isfinite(oldI0)) return false;
     1078
     1079    // increase size and height of source
     1080    axes = psEllipseShapeToAxes (oldshape, 20.0);
     1081    axes.major *= SIZE;
     1082    axes.minor *= SIZE;
     1083    newshape = psEllipseAxesToShape (axes);
     1084    PAR[PM_PAR_I0]  = FACTOR*oldI0;
     1085    PAR[PM_PAR_SXX] = newshape.sx;
     1086    PAR[PM_PAR_SYY] = newshape.sy;
     1087    PAR[PM_PAR_SXY] = newshape.sxy;
     1088
     1089    psImage *target = source->variance;
     1090
     1091    if (add) {
     1092        status = pmModelAddWithOffset (target, source->maskObj, model, mode, maskVal, dx, dy);
     1093    } else {
     1094        status = pmModelSubWithOffset (target, source->maskObj, model, mode, maskVal, dx, dy);
     1095    }
     1096
     1097    // restore original values
     1098    PAR[PM_PAR_I0]  = oldI0;
     1099    PAR[PM_PAR_SXX] = oldshape.sx;
     1100    PAR[PM_PAR_SYY] = oldshape.sy;
     1101    PAR[PM_PAR_SXY] = oldshape.sxy;
    9751102
    9761103    return true;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSource.h

    r28013 r29060  
    1111# define PM_SOURCE_H
    1212
    13 #include <pslib.h>
    14 #include "pmPeaks.h"
    15 #include "pmModel.h"
    16 #include "pmMoments.h"
    17 #include "pmSourceExtendedPars.h"
    18 #include "pmSourceDiffStats.h"
    19 
    2013/// @addtogroup Objects Object Detection / Analysis Functions
    2114/// @{
    22 
    23 #include <pmSourceMasks.h>
    2415
    2516/** pmSourceType enumeration
     
    7465    pmSourceType type;                  ///< Best identification of object.
    7566    pmSourceMode mode;                  ///< analysis flags set for object.
     67    pmSourceMode2 mode2;                ///< analysis flags set for object.
    7668    pmSourceTmpF tmpFlags;              ///< internal-only flags
    7769    psArray *blends;                    ///< collection of sources thought to be confused with object
     
    8274    float errMag;                       ///< error in psfMag OR extMag (depending on type)
    8375    float apMag;                        ///< apMag corresponding to psfMag or extMag (depending on type)
    84     float pixWeight;                    ///< model-weighted coverage of valid pixels
     76    float apMagRaw;                     ///< raw mag in given aperture
     77    float apRadius;                     ///< radius for aperture magnitude
     78
     79    float pixWeightNotBad;              ///< PSF-weighted coverage of unmasked (not BAD) pixels
     80    float pixWeightNotPoor;             ///< PSF-weighted coverage of unmasked (not POOR) pixels
     81
    8582    float psfChisq;                     ///< probability of PSF
    8683    float crNsigma;                     ///< Nsigma deviation from PSF to CR
    8784    float extNsigma;                    ///< Nsigma deviation from PSF to EXT
    8885    float sky, skyErr;                  ///< The sky and its error at the center of the object
    89     float apRadius;
    9086    psRegion region;                    ///< area on image covered by selected pixels
    9187    pmSourceExtendedPars *extpars;      ///< extended source parameters
     
    113109pmPSFClump;
    114110
     111// private macro to set the source ID (a const)
     112#define P_PM_SOURCE_SET_ID(S,V) { *(int *)&(S)->id = (V); }
    115113
    116114/** pmSourceAlloc()
     
    126124
    127125pmSource  *pmSourceCopy(pmSource *source);
     126pmSource *pmSourceCopyData(pmSource *in);
    128127
    129128// free just the pixels for a source, keeping derived data
     
    242241bool pmSourceSubWithOffset (pmSource *source, pmModelOpMode mode, psImageMaskType maskVal, int dx, int dy);
    243242
     243bool pmSourceNoiseOpModel (pmModel *model, pmSource *source, pmModelOpMode mode, float FACTOR, float SIZE, bool add, psImageMaskType maskVal, int dx, int dy);
     244bool pmSourceNoiseOp (pmSource *source, pmModelOpMode mode, float FACTOR, float SIZE, bool add, psImageMaskType maskVal, int dx, int dy);
     245
    244246bool pmSourceOp (pmSource *source, pmModelOpMode mode, bool add, psImageMaskType maskVal, int dx, int dy);
    245247bool pmSourceCacheModel (pmSource *source, psImageMaskType maskVal);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceContour.c

    r24887 r29060  
    2323#include "pmHDU.h"
    2424#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
    2529#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
    2631#include "pmFootprint.h"
    2732#include "pmPeaks.h"
    2833#include "pmMoments.h"
    29 #include "pmResiduals.h"
    30 #include "pmGrowthCurve.h"
    31 #include "pmTrend2D.h"
    32 #include "pmPSF.h"
     34#include "pmModelFuncs.h"
    3335#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    3441#include "pmSource.h"
     42
    3543#include "pmSourceContour.h"
    3644
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceDiffStats.c

    r27531 r29060  
    2929    diffStats->nRatioAll = NAN;
    3030    diffStats->nGood = 0;
     31
     32    diffStats->SNp = NAN;
     33    diffStats->SNm = NAN;
     34    diffStats->Rp = NAN;
     35    diffStats->Rm = NAN;
    3136}
    3237
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceDiffStats.h

    r27531 r29060  
    2929    float nRatioAll;                    // = nGood / (nGood + nMask + nBad)
    3030    int   nGood;                        // nGood as defined above
     31    float SNp;                          // S/N of matched source in positive image
     32    float SNm;                          // S/N of matched source in negative image
     33    float Rp;                           // radius of matched source in positive image
     34    float Rm;                           // radius of matched source in negative image
    3135} pmSourceDiffStats;
    3236
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceFitModel.c

    r26070 r29060  
    2323#include "pmHDU.h"
    2424#include "pmFPA.h"
     25
     26#include "pmTrend2D.h"
     27#include "pmResiduals.h"
     28#include "pmGrowthCurve.h"
    2529#include "pmSpan.h"
     30#include "pmFootprintSpans.h"
    2631#include "pmFootprint.h"
    2732#include "pmPeaks.h"
    2833#include "pmMoments.h"
    29 #include "pmGrowthCurve.h"
    30 #include "pmResiduals.h"
    31 #include "pmTrend2D.h"
    32 #include "pmPSF.h"
     34#include "pmModelFuncs.h"
    3335#include "pmModel.h"
     36#include "pmModelUtils.h"
     37#include "pmModelClass.h"
     38#include "pmSourceMasks.h"
     39#include "pmSourceExtendedPars.h"
     40#include "pmSourceDiffStats.h"
    3441#include "pmSource.h"
    35 #include "pmModelClass.h"
    3642#include "pmSourceFitModel.h"
    3743
    38 // save as static values so they may be set externally
    39 static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
    40 static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
    41 static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0;
    42 static bool  PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true;
    43 
    44 bool pmSourceFitModelInit (float nIter, float tol, float weight, bool poissonErrors)
     44void pmSourceFitOptionsFree(pmSourceFitOptions *opt)
    4545{
    46 
    47     PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = nIter;
    48     PM_SOURCE_FIT_MODEL_TOLERANCE = tol;
    49     PM_SOURCE_FIT_MODEL_WEIGHT = weight;
    50     PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = poissonErrors;
    51 
    52     return true;
     46    return;
     47}
     48
     49pmSourceFitOptions *pmSourceFitOptionsAlloc(void) {
     50
     51    pmSourceFitOptions *opt = (pmSourceFitOptions *) psAlloc(sizeof(pmSourceFitOptions));
     52    psMemSetDeallocator(opt, (psFreeFunc) pmSourceFitOptionsFree);
     53
     54    opt->mode = PM_SOURCE_FIT_PSF;
     55    opt->nIter  = 15;
     56    opt->minTol = 0.01;
     57    opt->maxTol = 1.00;
     58    opt->weight = 1.00;
     59    opt->maxChisqDOF = NAN;
     60    opt->poissonErrors = true;
     61
     62    return opt;
    5363}
    5464
    5565bool pmSourceFitModel (pmSource *source,
    5666                       pmModel *model,
    57                        pmSourceFitMode mode,
     67                       pmSourceFitOptions *options,
    5868                       psImageMaskType maskVal)
    5969{
     
    7686    psVector *yErr = psVectorAllocEmpty(nPix, PS_TYPE_F32);
    7787
     88    // XXX for a test, skip the central pixel in the sersic fit
     89    bool skipCenter = false && (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
     90    float Xo = model->params->data.F32[PM_PAR_XPOS];
     91    float Yo = model->params->data.F32[PM_PAR_YPOS];
     92
    7893    // fill in the coordinate and value entries
    7994    nPix = 0;
     
    95110            // skip nan values in image
    96111            if (!isfinite(source->variance->data.F32[i][j])) {
    97               fprintf (stderr, "impossible! %x vs %x\n", source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[i][j], maskVal);
    98               continue;
    99             }
    100 
    101             psVector *coord = psVectorAlloc(2, PS_TYPE_F32);
     112                fprintf (stderr, "impossible! %x vs %x\n", source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[i][j], maskVal);
     113                continue;
     114            }
    102115
    103116            // Convert i/j to image space:
    104117            // 0.5 PIX: the coordinate values must be in pixel coords, not index           
    105             coord->data.F32[0] = (psF32) (j + 0.5 + source->pixels->col0);
    106             coord->data.F32[1] = (psF32) (i + 0.5 + source->pixels->row0);
     118            float Xv = (psF32) (j + 0.5 + source->pixels->col0);
     119            float Yv = (psF32) (i + 0.5 + source->pixels->row0);
     120
     121            // XXX possible skip of center pixel:
     122            if (skipCenter) {
     123                float r = hypot(Xv - Xo, Yv - Yo);
     124                if (r < 0.75) {
     125                    continue;
     126                }
     127            }
     128
     129            psVector *coord = psVectorAlloc(2, PS_TYPE_F32);
     130            coord->data.F32[0] = Xv;
     131            coord->data.F32[1] = Yv;
    107132            x->data[nPix] = (psPtr *) coord;
    108133            y->data.F32[nPix] = source->pixels->data.F32[i][j];
     
    111136            // as variance to avoid the bias from systematic errors here we would just use the
    112137            // source sky variance
    113             if (PM_SOURCE_FIT_MODEL_PIX_WEIGHTS) {
     138            if (options->poissonErrors) {
    114139                yErr->data.F32[nPix] = 1.0 / source->variance->data.F32[i][j];
    115140            } else {
    116                 yErr->data.F32[nPix] = 1.0 / PM_SOURCE_FIT_MODEL_WEIGHT;
     141                yErr->data.F32[nPix] = 1.0 / options->weight;
    117142            }
    118143            nPix++;
     
    133158    // set parameter mask based on fitting mode
    134159    int nParams = 0;
    135     switch (mode) {
    136     case PM_SOURCE_FIT_NORM:
     160    switch (options->mode) {
     161      case PM_SOURCE_FIT_NORM:
    137162        // NORM-only model fits only source normalization (Io)
    138163        nParams = 1;
     
    140165        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
    141166        break;
    142     case PM_SOURCE_FIT_PSF:
     167      case PM_SOURCE_FIT_PSF:
    143168        // PSF model only fits x,y,Io
    144169        nParams = 3;
     
    148173        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_YPOS] = 0;
    149174        break;
    150     case PM_SOURCE_FIT_EXT:
     175      case PM_SOURCE_FIT_EXT:
    151176        // EXT model fits all params (except sky)
    152177        nParams = params->n - 1;
     
    154179        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
    155180        break;
    156     default:
    157         psAbort("invalid fitting mode");
     181      case PM_SOURCE_FIT_INDEX:
     182        // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params
     183        psVectorInit (constraint->paramMask, 1);
     184        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
     185        if (params->n == 7) {
     186            nParams = 1;
     187        } else {
     188            nParams = 2;
     189            constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 0;
     190        }
     191        break;
     192      case PM_SOURCE_FIT_NO_INDEX:
     193        // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params
     194        psVectorInit (constraint->paramMask, 0);
     195        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
     196        if (params->n == 7) {
     197            nParams = params->n - 1;
     198        } else {
     199            nParams = params->n - 2;
     200            constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 1;
     201        }
     202        break;
     203      default:
     204        psAbort("invalid fitting mode");
    158205    }
    159206    // force the floating parameters to fall within the contraint ranges
    160207    for (int i = 0; i < params->n; i++) {
    161         model->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
    162         model->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
     208        model->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
     209        model->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
    163210    }
    164211
     
    173220    }
    174221
    175     psMinimization *myMin = psMinimizationAlloc (PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_TOLERANCE);
     222    psMinimization *myMin = psMinimizationAlloc (options->nIter, options->minTol, options->maxTol);
    176223
    177224    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
     
    194241    model->flags |= PM_MODEL_STATUS_FITTED;
    195242    if (!fitStatus) model->flags |= PM_MODEL_STATUS_NONCONVERGE;
     243    if (myMin->lastDelta > myMin->minTol) model->flags |= PM_MODEL_STATUS_WEAK_FIT;
    196244
    197245    // get the Gauss-Newton distance for fixed model parameters
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceFitModel.h

    r21183 r29060  
    1919    PM_SOURCE_FIT_EXT,
    2020    PM_SOURCE_FIT_PSF_AND_SKY,
    21     PM_SOURCE_FIT_EXT_AND_SKY
     21    PM_SOURCE_FIT_EXT_AND_SKY,
     22    PM_SOURCE_FIT_INDEX,
     23    PM_SOURCE_FIT_NO_INDEX,
    2224} pmSourceFitMode;
    2325
    24 bool pmSourceFitModelInit(
    25     float nIter,   ///< max number of allowed iterations
    26     float tol,      ///< convergence criterion
    27     float weight,      ///< use this weight for constant-weight fits
    28     bool poissonErrors   // use poisson errors for fits?
    29 );
     26typedef struct {
     27    pmSourceFitMode mode;               ///< optionally fit all or a subset of parameters
     28    float nIter;                        ///< max number of allowed iterations
     29    float minTol;                       ///< convergence criterion
     30    float maxTol;                       ///< convergence criterion
     31    float maxChisqDOF;                  ///< convergence criterion
     32    float weight;                       ///< use this weight for constant-weight fits
     33    bool poissonErrors;                 ///< use poisson errors for fits?
     34} pmSourceFitOptions;
     35
     36// the pmSourceFitOptions structure is used to control details of the fitting process
     37pmSourceFitOptions *pmSourceFitOptionsAlloc(void);
     38
     39// bool pmSourceFitModelInit(
     40//     pmSourceFitMode mode,            ///< what parameter set should be fitted?
     41//     float nIter,                     ///< max number of allowed iterations
     42//     float tol,                               ///< convergence criterion
     43//     float weight,                    ///< use this weight for constant-weight fits
     44//     bool poissonErrors                       ///< use poisson errors for fits?
     45// );
    3046
    3147/** pmSourceFitModel()
     
    3854 */
    3955bool pmSourceFitModel(
    40     pmSource *source,   ///< The input pmSource
    41     pmModel *model,   ///< model to be fitted
    42     pmSourceFitMode mode,  ///< define parameters to be fitted
     56    pmSource *source,                   ///< The input pmSource
     57    pmModel *model,                     ///< model to be fitted
     58    pmSourceFitOptions *options,        ///< define parameters to be fitted
    4359    psImageMaskType maskVal             ///< Value to mask
    4460);
    4561
    46 
    47 // initialize data for a group of object models
    48 bool pmSourceFitSetInit (pmModelType type);
    49 
    50 // clear data for a group of object models
    51 void pmSourceFitSetClear (void);
    52 
    53 // function used to set limits for a group of models
    54 bool pmSourceFitSet_CheckLimits (psMinConstraintMode mode, int nParam, float *params, float *betas);
    55 
    56 // function used to fit a group of object models
    57 psF32 pmSourceFitSet_Function(psVector *deriv,
    58                               const psVector *params,
    59                               const psVector *x);
    60 
    61 /** pmSourceFitSet()
    62  *
    63  * Fit the requested model to the specified source. The starting guess for the model is given
    64  * by the input source.model parameter values. The pixels of interest are specified by the
    65  * source.pixels and source.mask entries. This function calls psMinimizeLMChi2() on the image
    66  * data. The function returns TRUE on success or FALSE on failure.
    67  *
    68  */
    69 bool pmSourceFitSet(
    70     pmSource *source,   ///< The input pmSource
    71     psArray *modelSet,   ///< model to be fitted
    72     pmSourceFitMode mode,  ///< define parameters to be fitted
    73     psImageMaskType maskVal             ///< Vale to mask
    74 
    75 );
     62// // initialize data for a group of object models
     63// bool pmSourceFitSetInit (pmModelType type);
     64//
     65// // clear data for a group of object models
     66// void pmSourceFitSetClear (void);
     67//
     68// // function used to set limits for a group of models
     69// bool pmSourceFitSet_CheckLimits (psMinConstraintMode mode, int nParam, float *params, float *betas);
     70//
     71// // function used to fit a group of object models
     72// psF32 pmSourceFitSet_Function(psVector *deriv,
     73//                               const psVector *params,
     74//                               const psVector *x);
     75//
     76// /** pmSourceFitSet()
     77//  *
     78//  * Fit the requested model to the specified source. The starting guess for the model is given
     79//  * by the input source.model parameter values. The pixels of interest are specified by the
     80//  * source.pixels and source.mask entries. This function calls psMinimizeLMChi2() on the image
     81//  * data. The function returns TRUE on success or FALSE on failure.
     82//  *
     83//  */
     84// bool pmSourceFitSet(
     85//     pmSource *source,   ///< The input pmSource
     86//     psArray *modelSet,   ///< model to be fitted
     87//     pmSourceFitMode mode,  ///< define parameters to be fitted
     88//     psImageMaskType maskVal          ///< Vale to mask
     89//
     90// );
    7691
    7792/// @}
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceFitSet.c

    r27903 r29060  
    2222#include "pmHDU.h"
    2323#include "pmFPA.h"
     24
     25#include "pmTrend2D.h"
     26#include "pmResiduals.h"
     27#include "pmGrowthCurve.h"
    2428#include "pmSpan.h"
     29#include "pmFootprintSpans.h"
    2530#include "pmFootprint.h"
    2631#include "pmPeaks.h"
    2732#include "pmMoments.h"
    28 #include "pmGrowthCurve.h"
    29 #include "pmResiduals.h"
    30 #include "pmTrend2D.h"
    31 #include "pmPSF.h"
     33#include "pmModelFuncs.h"
    3234#include "pmModel.h"
     35#include "pmModelUtils.h"
     36#include "pmModelClass.h"
     37#include "pmSourceMasks.h"
     38#include "pmSourceExtendedPars.h"
     39#include "pmSourceDiffStats.h"
    3340#include "pmSource.h"
    34 #include "pmModelClass.h"
     41
    3542#include "pmSourceFitModel.h"
    3643#include "pmSourceFitSet.h"
    3744
    3845// save as static values so they may be set externally
    39 static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
    40 static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
    41 static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0;
    42 static bool  PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true;
     46// static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
     47// static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
     48// static psF32 PM_SOURCE_FIT_MODEL_WEIGHT = 1.0;
     49// static bool  PM_SOURCE_FIT_MODEL_PIX_WEIGHTS = true;
    4350
    4451/********************* Source Model Set Functions ***************************/
     
    429436bool pmSourceFitSet (pmSource *source,
    430437                     psArray *modelSet,
    431                      pmSourceFitMode mode,
     438                     pmSourceFitOptions *options,
    432439                     psImageMaskType maskVal)
    433440{
     
    478485            // as variance to avoid the bias from systematic errors here we would just use the
    479486            // source sky variance
    480             if (PM_SOURCE_FIT_MODEL_PIX_WEIGHTS) {
    481                 yErr->data.F32[nPix] = 1.0 / source->variance->data.F32[i][j];
    482             } else {
    483                 yErr->data.F32[nPix] = 1.0 / PM_SOURCE_FIT_MODEL_WEIGHT;
    484             }
    485             nPix++;
    486         }
     487            if (options->poissonErrors) {
     488                yErr->data.F32[nPix] = 1.0 / source->variance->data.F32[i][j];
     489            } else {
     490                yErr->data.F32[nPix] = 1.0 / options->weight;
     491            }
     492            nPix++;
     493        }
    487494    }
    488495    x->n = nPix;
     
    490497    yErr->n = nPix;
    491498
    492     // create the FitSet for this thread and set the initial parameter guesses
     499// create the FitSet for this thread and set the initial parameter guesses
    493500    pmSourceFitSetData *thisSet = pmSourceFitSetDataSet(modelSet);
    494501
    495     // define param and deriv vectors for complete set of parameters
     502// define param and deriv vectors for complete set of parameters
    496503    psVector *params = psVectorAlloc (thisSet->nParamSet, PS_TYPE_F32);
    497504
    498     // set the param and deriv vectors based on the curent values
     505// set the param and deriv vectors based on the curent values
    499506    pmSourceFitSetJoin (NULL, params, thisSet);
    500507
    501     // create the minimization constraints
     508// create the minimization constraints
    502509    psMinConstraint *constraint = psMinConstraintAlloc();
    503510    constraint->paramMask = psVectorAlloc (thisSet->nParamSet, PS_TYPE_VECTOR_MASK);
    504511    constraint->checkLimits = pmSourceFitSetCheckLimits;
    505512
    506     pmSourceFitSetMasks (constraint, thisSet, mode);
    507 
    508     // force the floating parameters to fall within the contraint ranges
     513    pmSourceFitSetMasks (constraint, thisSet, options->mode);
     514
     515// force the floating parameters to fall within the contraint ranges
    509516    for (int i = 0; i < params->n; i++) {
    510         pmSourceFitSetCheckLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
    511         pmSourceFitSetCheckLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
     517        pmSourceFitSetCheckLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
     518        pmSourceFitSetCheckLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
    512519    }
    513520
    514521    if (psTraceGetLevel("psModules.objects") >= 5) {
    515         for (int i = 0; i < params->n; i++) {
    516             fprintf (stderr, "%d %f %d\n", i, params->data.F32[i], constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i]);
    517         }
     522        for (int i = 0; i < params->n; i++) {
     523            fprintf (stderr, "%d %f %d\n", i, params->data.F32[i], constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i]);
     524        }
    518525    }
    519526
    520527    if (nPix <  thisSet->nParamSet + 1) {
    521         psTrace (__func__, 4, "insufficient valid pixels\n");
    522         psTrace("psModules.objects", 10, "---- %s() end : fail pixels ----\n", __func__);
    523         for (int i = 0; i < modelSet->n; i++) {
    524             pmModel *model = modelSet->data[i];
    525             model->flags |= PM_MODEL_STATUS_BADARGS;
    526         }
    527         psFree (x);
    528         psFree (y);
    529         psFree (yErr);
    530         psFree (params);
    531         psFree(constraint);
    532         pmSourceFitSetDataClear(); // frees thisSet and removes if from the array of fitSets
    533         return(false);
    534     }
    535 
    536     psMinimization *myMin = psMinimizationAlloc (PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_TOLERANCE);
     528        psTrace (__func__, 4, "insufficient valid pixels\n");
     529        psTrace("psModules.objects", 10, "---- %s() end : fail pixels ----\n", __func__);
     530        for (int i = 0; i < modelSet->n; i++) {
     531            pmModel *model = modelSet->data[i];
     532            model->flags |= PM_MODEL_STATUS_BADARGS;
     533        }
     534        psFree (x);
     535        psFree (y);
     536        psFree (yErr);
     537        psFree (params);
     538        psFree(constraint);
     539        pmSourceFitSetDataClear(); // frees thisSet and removes if from the array of fitSets
     540        return(false);
     541    }
     542
     543    psMinimization *myMin = psMinimizationAlloc (options->nIter, options->minTol, options->maxTol);
    537544
    538545    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
     
    540547    fitStatus = psMinimizeLMChi2(myMin, covar, params, constraint, x, y, yErr, pmSourceFitSetFunction);
    541548    if (!fitStatus) {
    542         psTrace("psModules.objects", 4, "Failed to fit model (%ld components)\n", modelSet->n);
    543     }
    544 
    545     // parameter errors from the covariance matrix
     549        psTrace("psModules.objects", 4, "Failed to fit model (%ld components)\n", modelSet->n);
     550    }
     551
     552// parameter errors from the covariance matrix
    546553    psVector *dparams = psVectorAlloc (thisSet->nParamSet, PS_TYPE_F32);
    547554    for (int i = 0; i < dparams->n; i++) {
    548         if ((constraint->paramMask != NULL) && constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
    549             continue;
    550         dparams->data.F32[i] = sqrt(covar->data.F32[i][i]);
    551     }
    552 
    553     // get the Gauss-Newton distance for fixed model parameters
     555        if ((constraint->paramMask != NULL) && constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
     556            continue;
     557        dparams->data.F32[i] = sqrt(covar->data.F32[i][i]);
     558    }
     559
     560// get the Gauss-Newton distance for fixed model parameters
    554561    if (constraint->paramMask != NULL) {
    555         psVector *delta = psVectorAlloc (params->n, PS_TYPE_F32);
    556         psVector *altmask = psVectorAlloc (params->n, PS_TYPE_VECTOR_MASK);
    557         altmask->data.PS_TYPE_VECTOR_MASK_DATA[0] = 1;
    558         for (int i = 1; i < dparams->n; i++) {
    559             altmask->data.PS_TYPE_VECTOR_MASK_DATA[i] = (constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) ? 0 : 1;
    560         }
    561         psMinimizeGaussNewtonDelta(delta, params, altmask, x, y, yErr, pmSourceFitSetFunction);
    562 
    563         for (int i = 0; i < dparams->n; i++) {
    564             if (!constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
    565                 continue;
    566             // note that delta is the value *subtracted* from the parameter
    567             // to get the new guess.  for dparams to represent the direction
    568             // of motion, we need to take -delta
    569             dparams->data.F32[i] = -delta->data.F32[i];
    570         }
    571         psFree (delta);
    572         psFree (altmask);
     562        psVector *delta = psVectorAlloc (params->n, PS_TYPE_F32);
     563        psVector *altmask = psVectorAlloc (params->n, PS_TYPE_VECTOR_MASK);
     564        altmask->data.PS_TYPE_VECTOR_MASK_DATA[0] = 1;
     565        for (int i = 1; i < dparams->n; i++) {
     566            altmask->data.PS_TYPE_VECTOR_MASK_DATA[i] = (constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i]) ? 0 : 1;
     567        }
     568        psMinimizeGaussNewtonDelta(delta, params, altmask, x, y, yErr, pmSourceFitSetFunction);
     569
     570        for (int i = 0; i < dparams->n; i++) {
     571            if (!constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
     572                continue;
     573            // note that delta is the value *subtracted* from the parameter
     574            // to get the new guess.  for dparams to represent the direction
     575            // of motion, we need to take -delta
     576            dparams->data.F32[i] = -delta->data.F32[i];
     577        }
     578        psFree (delta);
     579        psFree (altmask);
    573580    }
    574581
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceFitSet.h

    r23487 r29060  
    5656    pmSource *source,                   ///< The input pmSource
    5757    psArray *modelSet,                  ///< model to be fitted
    58     pmSourceFitMode mode,               ///< define parameters to be fitted
     58    pmSourceFitOptions *options,        ///< define options for fitting process
    5959    psImageMaskType maskVal             ///< Vale to mask
    6060
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceGroups.c

    r26450 r29060  
    66#include <pslib.h>
    77
     8#include "pmHDU.h"
    89#include "pmFPA.h"
     10
     11#include "pmTrend2D.h"
     12#include "pmResiduals.h"
     13#include "pmGrowthCurve.h"
     14#include "pmSpan.h"
     15#include "pmFootprintSpans.h"
     16#include "pmFootprint.h"
     17#include "pmPeaks.h"
     18#include "pmMoments.h"
     19#include "pmModelFuncs.h"
     20#include "pmModel.h"
     21#include "pmModelUtils.h"
     22#include "pmModelClass.h"
     23#include "pmSourceMasks.h"
     24#include "pmSourceExtendedPars.h"
     25#include "pmSourceDiffStats.h"
    926#include "pmSource.h"
     27#include "pmSourceFitModel.h"
     28#include "pmPSF.h"
     29#include "pmPSFtry.h"
     30#include "pmDetections.h"
     31
    1032#include "pmSourceGroups.h"
    1133
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceGroups.h

    r26182 r29060  
    11#ifndef PM_SOURCE_GROUPS_H
    22#define PM_SOURCE_GROUPS_H
    3 
    4 #include <pslib.h>
    5 
    6 #include "pmFPA.h"
    73
    84/// Groups of sources
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO.c

    r28013 r29060  
    3131#include "pmConceptsRead.h"
    3232
     33#include "pmTrend2D.h"
     34#include "pmResiduals.h"
     35#include "pmGrowthCurve.h"
    3336#include "pmSpan.h"
     37#include "pmFootprintSpans.h"
    3438#include "pmFootprint.h"
    3539#include "pmPeaks.h"
    3640#include "pmMoments.h"
    37 #include "pmGrowthCurve.h"
    38 #include "pmResiduals.h"
    39 #include "pmTrend2D.h"
     41#include "pmModelFuncs.h"
     42#include "pmModel.h"
     43#include "pmModelUtils.h"
     44#include "pmModelClass.h"
     45#include "pmSourceMasks.h"
     46#include "pmSourceExtendedPars.h"
     47#include "pmSourceDiffStats.h"
     48#include "pmSource.h"
     49#include "pmSourceFitModel.h"
    4050#include "pmPSF.h"
    41 #include "pmModel.h"
     51#include "pmPSFtry.h"
     52
    4253#include "pmDetections.h"
    43 #include "pmSource.h"
    44 #include "pmModelClass.h"
    4554#include "pmDetEff.h"
    4655#include "pmSourceIO.h"
     
    326335}
    327336
     337# define PM_SOURCES_WRITE(NAME,TYPE)                                    \
     338    if (!strcmp (exttype, NAME)) {                                      \
     339        status &= pmSourcesWrite_##TYPE(file->fits, readout, sources, file->header, outhead, dataname, recipe); \
     340        if (xsrcname) {                                                 \
     341            status &= pmSourcesWrite_##TYPE##_XSRC(file->fits, readout, sources, file->header, xsrcname, recipe); \
     342        }                                                               \
     343        if (xfitname) {                                                 \
     344            status &= pmSourcesWrite_##TYPE##_XFIT (file->fits, readout, sources, file->header, xfitname); \
     345        }                                                               \
     346    }
     347
    328348// write out all readout-level Objects files for this cell
    329349bool pmReadoutWriteObjects (pmReadout *readout, const pmFPAview *view, pmFPAfile *file, const pmConfig *config)
     
    360380    }
    361381
     382    // the older types (RAW, OBJ, SX, CMP) are for backwards compatibility -- deprecate eventually?
    362383    switch (file->type) {
    363384      case PM_FPA_FILE_RAW:
     
    518539                psMetadataAddStr (outhead, PS_LIST_TAIL, "XFITNAME", PS_META_REPLACE, "name of XFIT table extension", xfitname);
    519540            }
    520 
    521             // XXX these are case-sensitive since the EXTYPE is case-sensitive
     541   
     542
     543            // these are case-sensitive since the EXTYPE is case-sensitive
    522544            status = true;
    523             if (!strcmp (exttype, "SMPDATA")) {
    524                 status &= pmSourcesWrite_SMPDATA (file->fits, sources, file->header, outhead, dataname);
    525             }
    526             if (!strcmp (exttype, "PS1_DEV_0")) {
    527                 status &= pmSourcesWrite_PS1_DEV_0 (file->fits, sources, file->header, outhead, dataname);
    528             }
    529             if (!strcmp (exttype, "PS1_DEV_1")) {
    530                 status &= pmSourcesWrite_PS1_DEV_1 (file->fits, sources, file->header, outhead, dataname);
    531             }
    532             if (!strcmp (exttype, "PS1_CAL_0")) {
    533                 status &= pmSourcesWrite_PS1_CAL_0 (file->fits, readout, sources, file->header, outhead, dataname);
    534             }
    535             if (!strcmp (exttype, "PS1_V1")) {
    536                 status &= pmSourcesWrite_CMF_PS1_V1 (file->fits, readout, sources, file->header, outhead, dataname);
    537             }
    538             if (!strcmp (exttype, "PS1_V2")) {
    539                 status &= pmSourcesWrite_CMF_PS1_V2 (file->fits, readout, sources, file->header, outhead, dataname);
    540             }
    541             if (!strcmp (exttype, "PS1_SV1")) {
    542                 status &= pmSourcesWrite_CMF_PS1_SV1 (file->fits, readout, sources, file->header, outhead, dataname, recipe);
    543             }
    544             if (!strcmp (exttype, "PS1_DV1")) {
    545                 status &= pmSourcesWrite_CMF_PS1_DV1 (file->fits, readout, sources, file->header, outhead, dataname);
    546             }
    547 
    548             if (xsrcname) {
    549                 if (!strcmp (exttype, "PS1_DEV_1")) {
    550                     status &= pmSourcesWrite_PS1_DEV_1_XSRC (file->fits, sources, xsrcname, recipe);
    551                 }
    552                 if (!strcmp (exttype, "PS1_CAL_0")) {
    553                     status &= pmSourcesWrite_PS1_CAL_0_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
    554                 }
    555                 if (!strcmp (exttype, "PS1_V1")) {
    556                     status &= pmSourcesWrite_CMF_PS1_V1_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
    557                 }
    558                 if (!strcmp (exttype, "PS1_V2")) {
    559                     status &= pmSourcesWrite_CMF_PS1_V2_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
    560                 }
    561                 if (!strcmp (exttype, "PS1_SV1")) {
    562                     status &= pmSourcesWrite_CMF_PS1_SV1_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
    563                 }
    564                 if (!strcmp (exttype, "PS1_DV1")) {
    565                     status &= pmSourcesWrite_CMF_PS1_DV1_XSRC (file->fits, readout, sources, file->header, xsrcname, recipe);
    566                 }
    567             }
    568             if (xfitname) {
    569                 if (!strcmp (exttype, "PS1_DEV_1")) {
    570                     status &= pmSourcesWrite_PS1_DEV_1_XFIT (file->fits, sources, xfitname);
    571                 }
    572                 if (!strcmp (exttype, "PS1_CAL_0")) {
    573                     status &= pmSourcesWrite_PS1_CAL_0_XFIT (file->fits, readout, sources, file->header, xfitname);
    574                 }
    575                 if (!strcmp (exttype, "PS1_V1")) {
    576                     status &= pmSourcesWrite_CMF_PS1_V1_XFIT (file->fits, readout, sources, xfitname);
    577                 }
    578                 if (!strcmp (exttype, "PS1_V2")) {
    579                     status &= pmSourcesWrite_CMF_PS1_V2_XFIT (file->fits, readout, sources, xfitname);
    580                 }
    581                 if (!strcmp (exttype, "PS1_SV1")) {
    582                     status &= pmSourcesWrite_CMF_PS1_SV1_XFIT (file->fits, readout, sources, xfitname);
    583                 }
    584                 if (!strcmp (exttype, "PS1_DV1")) {
    585                     status &= pmSourcesWrite_CMF_PS1_DV1_XFIT (file->fits, readout, sources, xfitname);
    586                 }
    587             }
     545            PM_SOURCES_WRITE("SMPDATA",   SMPDATA);
     546            PM_SOURCES_WRITE("PS1_DEV_0", PS1_DEV_0);
     547            PM_SOURCES_WRITE("PS1_DEV_1", PS1_DEV_1);
     548            PM_SOURCES_WRITE("PS1_CAL_0", PS1_CAL_0);
     549            PM_SOURCES_WRITE("PS1_V1",    CMF_PS1_V1);
     550            PM_SOURCES_WRITE("PS1_V2",    CMF_PS1_V2);
     551            PM_SOURCES_WRITE("PS1_V3",    CMF_PS1_V3);
     552            PM_SOURCES_WRITE("PS1_SV1",   CMF_PS1_SV1);
     553            PM_SOURCES_WRITE("PS1_DV1",   CMF_PS1_DV1);
     554            PM_SOURCES_WRITE("PS1_DV2",   CMF_PS1_DV2);
     555
    588556            psFree (outhead);
    589557            psFree (exttype);
     
    10391007                sources = pmSourcesRead_CMF_PS1_DV1 (file->fits, hdu->header);
    10401008            }
     1009            if (!strcmp (exttype, "PS1_DV2")) {
     1010                sources = pmSourcesRead_CMF_PS1_DV2 (file->fits, hdu->header);
     1011            }
    10411012
    10421013            if (!pmReadoutReadDetEff(file->fits, readout, deteffname)) {
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO.h

    r28013 r29060  
    2424bool pmSourcesWriteCMP (psArray *sources, char *filename, psMetadata *header);
    2525
    26 bool pmSourcesWrite_SMPDATA (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    27 bool pmSourcesWrite_PS1_DEV_0 (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
     26bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
    2827
    29 bool pmSourcesWrite_PS1_DEV_1 (psFits *fits, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    30 bool pmSourcesWrite_PS1_DEV_1_XSRC (psFits *fits, psArray *sources, char *extname, psMetadata *recipe);
    31 bool pmSourcesWrite_PS1_DEV_1_XFIT (psFits *fits, psArray *sources, char *extname);
     28// All of these functions need to use the same API, even if not all elements are used in a specific case
     29bool pmSourcesWrite_SMPDATA(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     30bool pmSourcesWrite_SMPDATA_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     31bool pmSourcesWrite_SMPDATA_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    3232
    33 bool pmSourcesWrite_PS1_CAL_0 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    34 bool pmSourcesWrite_PS1_CAL_0_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    35 bool pmSourcesWrite_PS1_CAL_0_XFIT (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     33bool pmSourcesWrite_PS1_DEV_0(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     34bool pmSourcesWrite_PS1_DEV_0_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     35bool pmSourcesWrite_PS1_DEV_0_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    3636
    37 bool pmSourcesWrite_CMF_PS1_V1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    38 bool pmSourcesWrite_CMF_PS1_V1_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    39 bool pmSourcesWrite_CMF_PS1_V1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname);
     37bool pmSourcesWrite_PS1_DEV_1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     38bool pmSourcesWrite_PS1_DEV_1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     39bool pmSourcesWrite_PS1_DEV_1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    4040
    41 bool pmSourcesWrite_CMF_PS1_V2 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    42 bool pmSourcesWrite_CMF_PS1_V2_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    43 bool pmSourcesWrite_CMF_PS1_V2_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname);
     41bool pmSourcesWrite_PS1_CAL_0(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     42bool pmSourcesWrite_PS1_CAL_0_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     43bool pmSourcesWrite_PS1_CAL_0_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    4444
    45 bool pmSourcesWrite_CMF_PS1_SV1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
    46 bool pmSourcesWrite_CMF_PS1_SV1_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    47 bool pmSourcesWrite_CMF_PS1_SV1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname);
     45bool pmSourcesWrite_CMF_PS1_V1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     46bool pmSourcesWrite_CMF_PS1_V1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     47bool pmSourcesWrite_CMF_PS1_V1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    4848
    49 bool pmSourcesWrite_CMF_PS1_DV1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname);
    50 bool pmSourcesWrite_CMF_PS1_DV1_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
    51 bool pmSourcesWrite_CMF_PS1_DV1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname);
     49bool pmSourcesWrite_CMF_PS1_V2(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     50bool pmSourcesWrite_CMF_PS1_V2_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     51bool pmSourcesWrite_CMF_PS1_V2_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    5252
    53 bool pmSource_CMF_WritePHU (const pmFPAview *view, pmFPAfile *file, pmConfig *config);
     53bool pmSourcesWrite_CMF_PS1_V3(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     54bool pmSourcesWrite_CMF_PS1_V3_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     55bool pmSourcesWrite_CMF_PS1_V3_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     56
     57bool pmSourcesWrite_CMF_PS1_SV1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     58bool pmSourcesWrite_CMF_PS1_SV1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     59bool pmSourcesWrite_CMF_PS1_SV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     60
     61bool pmSourcesWrite_CMF_PS1_DV1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     62bool pmSourcesWrite_CMF_PS1_DV1_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     63bool pmSourcesWrite_CMF_PS1_DV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
     64
     65bool pmSourcesWrite_CMF_PS1_DV2(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe);
     66bool pmSourcesWrite_CMF_PS1_DV2_XSRC(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe);
     67bool pmSourcesWrite_CMF_PS1_DV2_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname);
    5468
    5569psArray *pmSourcesReadCMP (char *filename, psMetadata *header);
     
    6377psArray *pmSourcesRead_CMF_PS1_SV1 (psFits *fits, psMetadata *header);
    6478psArray *pmSourcesRead_CMF_PS1_DV1 (psFits *fits, psMetadata *header);
     79psArray *pmSourcesRead_CMF_PS1_DV2 (psFits *fits, psMetadata *header);
    6580
    6681bool pmSourcesWritePSFs (psArray *sources, char *filename);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_CMF_PS1_DV1.c

    r28013 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4756// This version has elements intended for difference images & forced photometry:
    4857// diffStats entries (good for dipoles); flux + flux error (for insignificant detections)
    49 bool pmSourcesWrite_CMF_PS1_DV1 (psFits *fits, pmReadout *readout, psArray *sources,
    50                                 psMetadata *imageHeader, psMetadata *tableHeader, char *extname)
     58bool pmSourcesWrite_CMF_PS1_DV1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    5159{
    5260    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    194202        psMetadataAdd (row, PS_LIST_TAIL, "PSF_MINOR",        PS_DATA_F32, "PSF width (minor axis)",                     axes.minor);
    195203        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    196         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     204        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    197205        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NDOF",         PS_DATA_S32, "degrees of freedom",                         nDOF);
    198206        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NPIX",         PS_DATA_S32, "number of pixels in fit",                    nPix);
     
    324332        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], peakFlux, PM_PEAK_LONE);
    325333        source->peak->flux = peakFlux;
     334        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     335        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
    326336        source->peak->dx   = dPAR[PM_PAR_XPOS];
    327337        source->peak->dy   = dPAR[PM_PAR_YPOS];
    328         source->peak->SN   = sqrt(source->peak->flux); // XXX a proxy: various functions sort by peak S/N
    329 
    330         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     338        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     339          source->peak->SN = 1.0 / source->errMag;
     340        } else {
     341          source->peak->SN = sqrt(source->peak->flux); // an alternate proxy: various functions sort by peak S/N
     342        }
     343
     344        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    331345        source->crNsigma  = psMetadataLookupF32 (&status, row, "CR_NSIGMA");
    332346        source->extNsigma = psMetadataLookupF32 (&status, row, "EXT_NSIGMA");
     
    536550
    537551// XXX this layout is still the same as PS1_DEV_1
    538 bool pmSourcesWrite_CMF_PS1_DV1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname)
     552bool pmSourcesWrite_CMF_PS1_DV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
    539553{
    540554
     
    585599            assert (model);
    586600
     601            // skip models which were not actually fitted
     602            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     603
    587604            PAR = model->params->data.F32;
    588605            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_CMF_PS1_SV1.c

    r28013 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4655
    4756// NOTE: this output function is intended for psphotStack analysis: it includes per-psf radial fluxes
    48 // XXX currently in the 'read' function is NOT consistent with the 'write' function (does not read radial fluxes)
     57// XXX currently, the 'read' function is NOT consistent with the 'write' function (does not read radial fluxes)
    4958
    5059bool pmSourcesWrite_CMF_PS1_SV1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
     
    6271    psF32 errMag, chisq, apRadius;
    6372    psS32 nPix, nDOF;
     73    char keyword1[80], keyword2[80];
    6474
    6575    pmChip *chip = readout->parent->parent;
     
    95105    // we use this just to define the output vectors (which must be present for all objects)
    96106    bool status = false;
     107    psVector *radMin = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.LOWER");
    97108    psVector *radMax = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.UPPER");
    98109    psAssert (radMax, "this must have been defined and tested earlier!");
    99110    psAssert (radMax->n, "this must have been defined and tested earlier!");
     111    psAssert (radMin->n == radMax->n, "inconsistent annular bins");
     112
     113    // write the radial profile apertures to header
     114    for (int i = 0; i < radMax->n; i++) {
     115      sprintf (keyword1, "RMIN_%02d", i);
     116      sprintf (keyword2, "RMAX_%02d", i);
     117      psMetadataAddF32 (imageHeader, PS_LIST_TAIL, keyword1, PS_META_REPLACE, "min radius for SB profile", radMin->data.F32[i]);
     118      psMetadataAddF32 (imageHeader, PS_LIST_TAIL, keyword2, PS_META_REPLACE, "min radius for SB profile", radMax->data.F32[i]);
     119    }
     120
    100121
    101122    // we write out PSF-fits for all sources, regardless of quality.  the source flags tell us the state
     
    177198        psMetadataAdd (row, PS_LIST_TAIL, "PSF_INST_FLUX_SIG",PS_DATA_F32, "Sigma of PSF instrumental flux",             source->psfFluxErr);
    178199        psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG",           PS_DATA_F32, "magnitude in standard aperture",             source->apMag);
     200        psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RAW",       PS_DATA_F32, "magnitude in reported aperture",             source->apMagRaw);
    179201        psMetadataAdd (row, PS_LIST_TAIL, "AP_MAG_RADIUS",    PS_DATA_F32, "radius used for aperture mags",              apRadius);
    180202        psMetadataAdd (row, PS_LIST_TAIL, "PEAK_FLUX_AS_MAG", PS_DATA_F32, "Peak flux expressed as magnitude",           peakMag);
     
    193215        psMetadataAdd (row, PS_LIST_TAIL, "PSF_MINOR",        PS_DATA_F32, "PSF width (minor axis)",                     axes.minor);
    194216        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    195         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     217        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor (bad)",          source->pixWeightNotBad);
     218        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF_PERFECT",   PS_DATA_F32, "PSF coverage/quality factor (poor)",         source->pixWeightNotPoor);
    196219        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NDOF",         PS_DATA_S32, "degrees of freedom",                         nDOF);
    197220        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NPIX",         PS_DATA_S32, "number of pixels in fit",                    nPix);
    198221
    199222        // distinguish moments measure from window vs S/N > XX ??
    200         float mxx = source->moments ? source->moments->Mxx : NAN;
    201         float mxy = source->moments ? source->moments->Mxy : NAN;
    202         float myy = source->moments ? source->moments->Myy : NAN;
    203         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_XX",       PS_DATA_F32, "second moments (X^2)",                      mxx);
    204         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_XY",       PS_DATA_F32, "second moments (X*Y)",                      mxy);
    205         psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_YY",       PS_DATA_F32, "second moments (Y*Y)",                      myy);
    206 
    207         psMetadataAdd (row, PS_LIST_TAIL, "FLAGS",            PS_DATA_U32, "psphot analysis flags",                      source->mode);
     223        float Mxx = source->moments ? source->moments->Mxx : NAN;
     224        float Mxy = source->moments ? source->moments->Mxy : NAN;
     225        float Myy = source->moments ? source->moments->Myy : NAN;
     226
     227        float Mrf  = source->moments ? source->moments->Mrf : NAN;
     228        float Mrh  = source->moments ? source->moments->Mrh : NAN;
     229        float Krf  = source->moments ? source->moments->KronFlux : NAN;
     230        float dKrf = source->moments ? source->moments->KronFluxErr : NAN;
     231
     232        float Kinner = source->moments ? source->moments->KronFinner : NAN;
     233        float Kouter = source->moments ? source->moments->KronFouter : NAN;
     234
     235        float M_c3 = source->moments ? 1.0*source->moments->Mxxx - 3.0*source->moments->Mxyy : NAN;
     236        float M_s3 = source->moments ? 3.0*source->moments->Mxxy - 1.0*source->moments->Myyy : NAN;
     237        float M_c4 = source->moments ? 1.0*source->moments->Mxxxx - 6.0*source->moments->Mxxyy + 1.0*source->moments->Myyyy : NAN;
     238        float M_s4 = source->moments ? 4.0*source->moments->Mxxxy - 4.0*source->moments->Mxyyy : NAN;
     239
     240        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_XX",       PS_DATA_F32, "second moments (X^2)",                      Mxx);
     241        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_XY",       PS_DATA_F32, "second moments (X*Y)",                      Mxy);
     242        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_YY",       PS_DATA_F32, "second moments (Y*Y)",                      Myy);
     243
     244        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M3C",      PS_DATA_F32, "third momemt cos theta",                    M_c3);
     245        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M3S",      PS_DATA_F32, "third momemt sin theta",                    M_s3);
     246        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4C",      PS_DATA_F32, "fourth momemt cos theta",                   M_c4);
     247        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_M4S",      PS_DATA_F32, "fourth momemt sin theta",                   M_s4);
     248
     249        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_R1",       PS_DATA_F32, "first radial moment",                       Mrf);
     250        psMetadataAdd (row, PS_LIST_TAIL, "MOMENTS_RH",       PS_DATA_F32, "half radial moment",                        Mrh);
     251        psMetadataAdd (row, PS_LIST_TAIL, "KRON_FLUX",        PS_DATA_F32, "Kron Flux (in 2.5 R1)",                     Krf);
     252        psMetadataAdd (row, PS_LIST_TAIL, "KRON_FLUX_ERR",    PS_DATA_F32, "Kron Flux Error",                          dKrf);
     253
     254        psMetadataAdd (row, PS_LIST_TAIL, "KRON_FLUX_INNER",  PS_DATA_F32, "Kron Flux (in 2.5 R1)",                     Kinner);
     255        psMetadataAdd (row, PS_LIST_TAIL, "KRON_FLUX_OUTER",  PS_DATA_F32, "Kron Flux (in 2.5 R1)",                     Kouter);
     256
     257        psMetadataAdd (row, PS_LIST_TAIL, "FLAGS",            PS_DATA_U32, "psphot analysis flags",                     source->mode);
     258        psMetadataAdd (row, PS_LIST_TAIL, "FLAGS2",           PS_DATA_U32, "psphot analysis flags",                     source->mode2);
    208259
    209260        psVector *radFlux    = psVectorAlloc(radMax->n, PS_TYPE_F32);
     
    352403        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], peakFlux, PM_PEAK_LONE);
    353404        source->peak->flux = peakFlux;
     405        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     406        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
    354407        source->peak->dx   = dPAR[PM_PAR_XPOS];
    355408        source->peak->dy   = dPAR[PM_PAR_YPOS];
    356         source->peak->SN   = sqrt(source->peak->flux); // XXX a proxy: various functions sort by peak S/N
    357 
    358         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     409        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     410          source->peak->SN = 1.0 / source->errMag;
     411        } else {
     412          source->peak->SN = sqrt(source->peak->flux); // an alternate proxy: various functions sort by peak S/N
     413        }
     414
     415        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
     416        source->pixWeightNotPoor = psMetadataLookupF32 (&status, row, "PSF_QF_PERFECT");
    359417        source->crNsigma  = psMetadataLookupF32 (&status, row, "CR_NSIGMA");
    360418        source->extNsigma = psMetadataLookupF32 (&status, row, "EXT_NSIGMA");
     
    371429        source->moments->Myy = psMetadataLookupF32 (&status, row, "MOMENTS_YY");
    372430
     431        source->moments->Mrf         = psMetadataLookupF32 (&status, row, "MOMENTS_R1");
     432        source->moments->Mrh         = psMetadataLookupF32 (&status, row, "MOMENTS_RH");
     433        source->moments->KronFlux    = psMetadataLookupF32 (&status, row, "KRON_FLUX");
     434        source->moments->KronFluxErr = psMetadataLookupF32 (&status, row, "KRON_FLUX_ERR");
     435
     436        source->moments->KronFinner  = psMetadataLookupF32 (&status, row, "KRON_FLUX_INNER");
     437        source->moments->KronFouter  = psMetadataLookupF32 (&status, row, "KRON_FLUX_OUTER");
     438
     439        // XXX we do not save all of the 3rd and 4th moment parameters. when we load in data,
     440        // we are storing enough information so the output will be consistent with the input
     441        source->moments->Mxxx = +1.0 * psMetadataLookupF32 (&status, row, "MOMENTS_M3C");
     442        source->moments->Mxxy = 0.0;
     443        source->moments->Mxyy = 0.0;
     444        source->moments->Myyy = -1.0 * psMetadataLookupF32 (&status, row, "MOMENTS_M3S");
     445
     446        source->moments->Mxxxx = +1.00 * psMetadataLookupF32 (&status, row, "MOMENTS_M4C");
     447        source->moments->Mxxxy = 0.0;
     448        source->moments->Mxxyy = 0.0;
     449        source->moments->Mxyyy = -0.25 * psMetadataLookupF32 (&status, row, "MOMENTS_M4S");
     450        source->moments->Myyyy = 0.0;
     451
    373452        source->mode = psMetadataLookupU32 (&status, row, "FLAGS");
     453        source->mode2 = psMetadataLookupU32 (&status, row, "FLAGS2");
    374454        assert (status);
    375455
     
    391471    psF32 xErr, yErr;
    392472    int nRow = -1;
     473    char keyword1[80], keyword2[80];
    393474
    394475    // create a header to hold the output data
     
    422503    bool doAnnuli       = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_ANNULI");
    423504    bool doPetrosian    = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_PETROSIAN");
    424     // bool doIsophotal    = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_ISOPHOTAL");
    425     // bool doKron         = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_KRON");
    426505
    427506    psVector *radMin = psMetadataLookupPtr (&status, recipe, "RADIAL.ANNULAR.BINS.LOWER");
     
    429508    psAssert (radMin->n == radMax->n, "inconsistent annular bins");
    430509
    431     // int nRadialBins = 0;
    432     // if (doAnnuli) {
    433     //  // get the max count of radial bins
    434     //  for (int i = 0; i < sources->n; i++) {
    435     //      pmSource *source = sources->data[i];
    436     //      if (!source->extpars) continue;
    437     //      if (!source->extpars->radProfile ) continue;
    438     //         if (!source->extpars->radProfile->binSB) continue;
    439     //      nRadialBins = PS_MAX(nRadialBins, source->extpars->radProfile->binSB->n);
    440     //  }
    441     // }
     510    // write the radial profile apertures to header
     511    for (int i = 0; i < radMax->n; i++) {
     512      sprintf (keyword1, "RMIN_%02d", i);
     513      sprintf (keyword2, "RMAX_%02d", i);
     514      psMetadataAddF32 (outhead, PS_LIST_TAIL, keyword1, PS_META_REPLACE, "min radius for SB profile", radMin->data.F32[i]);
     515      psMetadataAddF32 (outhead, PS_LIST_TAIL, keyword2, PS_META_REPLACE, "min radius for SB profile", radMax->data.F32[i]);
     516    }
    442517
    443518    // we write out all sources, regardless of quality.  the source flags tell us the state
    444519    for (int i = 0; i < sources->n; i++) {
    445         // skip source if it is not a ext sourc
    446         // XXX we have two places that extended source parameters are measured:
    447         // psphotExtendedSources, which measures the aperture-like parameters and (potentially) the psf-convolved extended source models,
    448         // psphotFitEXT, which does the simple extended source model fit (not psf-convolved)
    449         // should we require both?
    450 
    451520        pmSource *source = sources->data[i];
    452521
     
    514583        }
    515584
    516 # if (0)
    517         // Kron measurements
    518         if (doKron) {
    519             pmSourceKronValues *kron = source->extpars->kron;
    520             if (kron) {
    521                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_MAG",        PS_DATA_F32, "Kron Magnitude",       kron->mag);
    522                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_MAG_ERR",    PS_DATA_F32, "Kron Magnitude Error", kron->magErr);
    523                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_RADIUS",     PS_DATA_F32, "Kron Radius",          kron->rad);
    524                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_RADIUS_ERR", PS_DATA_F32, "Kron Radius Error",    kron->radErr);
    525             } else {
    526                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_MAG",        PS_DATA_F32, "Kron Magnitude",       NAN);
    527                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_MAG_ERR",    PS_DATA_F32, "Kron Magnitude Error", NAN);
    528                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_RADIUS",     PS_DATA_F32, "Kron Radius",          NAN);
    529                 psMetadataAdd (row, PS_LIST_TAIL, "KRON_RADIUS_ERR", PS_DATA_F32, "Kron Radius Error",    NAN);
    530             }
    531         }
    532 
    533         // Isophot measurements
    534         // XXX insert header data: isophotal level
    535         if (doIsophotal) {
    536             pmSourceIsophotalValues *isophot = source->extpars->isophot;
    537             if (isophot) {
    538                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_MAG",        PS_DATA_F32, "Isophot Magnitude",       isophot->mag);
    539                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_MAG_ERR",    PS_DATA_F32, "Isophot Magnitude Error", isophot->magErr);
    540                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_RADIUS",     PS_DATA_F32, "Isophot Radius",          isophot->rad);
    541                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_RADIUS_ERR", PS_DATA_F32, "Isophot Radius Error",    isophot->radErr);
    542             } else {
    543                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_MAG",        PS_DATA_F32, "Isophot Magnitude",       NAN);
    544                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_MAG_ERR",    PS_DATA_F32, "Isophot Magnitude Error", NAN);
    545                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_RADIUS",     PS_DATA_F32, "Isophot Radius",          NAN);
    546                 psMetadataAdd (row, PS_LIST_TAIL, "ISOPHOT_RADIUS_ERR", PS_DATA_F32, "Isophot Radius Error",    NAN);
    547             }
    548         }
    549 # endif
    550 
    551585        // Flux Annuli (if we have extended source measurements, we have these.  only optionally save them)
    552586        if (doAnnuli) {
     
    616650
    617651// XXX this layout is still the same as PS1_DEV_1
    618 bool pmSourcesWrite_CMF_PS1_SV1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname)
     652bool pmSourcesWrite_CMF_PS1_SV1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
    619653{
    620654
     
    665699            assert (model);
    666700
     701            // skip models which were not actually fitted
     702            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     703
    667704            PAR = model->params->data.F32;
    668705            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_CMF_PS1_V1.c

    r27818 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4554// followed by a zero-size matrix, followed by the table data
    4655
    47 bool pmSourcesWrite_CMF_PS1_V1 (psFits *fits, pmReadout *readout, psArray *sources,
    48                                 psMetadata *imageHeader, psMetadata *tableHeader, char *extname)
     56bool pmSourcesWrite_CMF_PS1_V1 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    4957{
    5058    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    184192        psMetadataAdd (row, PS_LIST_TAIL, "PSF_MINOR",        PS_DATA_F32, "PSF width (minor axis)",                     axes.minor);
    185193        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    186         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     194        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    187195        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NDOF",         PS_DATA_S32, "degrees of freedom",                         nDOF);
    188196        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NPIX",         PS_DATA_S32, "number of pixels in fit",                    nPix);
     
    310318        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], peakFlux, PM_PEAK_LONE);
    311319        source->peak->flux = peakFlux;
     320        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     321        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
    312322        source->peak->dx   = dPAR[PM_PAR_XPOS];
    313323        source->peak->dy   = dPAR[PM_PAR_YPOS];
    314 
    315         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     324        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     325          source->peak->SN = 1.0 / source->errMag;
     326        } else {
     327          source->peak->SN = sqrt(source->peak->flux); // an alternate proxy: various functions sort by peak S/N
     328        }
     329
     330        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    316331        source->crNsigma  = psMetadataLookupF32 (&status, row, "CR_NSIGMA");
    317332        source->extNsigma = psMetadataLookupF32 (&status, row, "EXT_NSIGMA");
     
    516531
    517532// XXX this layout is still the same as PS1_DEV_1
    518 bool pmSourcesWrite_CMF_PS1_V1_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname)
     533bool pmSourcesWrite_CMF_PS1_V1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
    519534{
    520535
     
    565580            assert (model);
    566581
     582            // skip models which were not actually fitted
     583            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     584
    567585            PAR = model->params->data.F32;
    568586            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_CMF_PS1_V2.c

    r27818 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4554// followed by a zero-size matrix, followed by the table data
    4655
    47 bool pmSourcesWrite_CMF_PS1_V2 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname)
     56bool pmSourcesWrite_CMF_PS1_V2 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    4857{
    4958    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    182191        psMetadataAdd (row, PS_LIST_TAIL, "PSF_MINOR",        PS_DATA_F32, "PSF width (minor axis)",                     axes.minor);
    183192        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    184         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     193        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    185194        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NDOF",         PS_DATA_S32, "degrees of freedom",                         nDOF);
    186195        psMetadataAdd (row, PS_LIST_TAIL, "PSF_NPIX",         PS_DATA_S32, "number of pixels in fit",                    nPix);
     
    314323        source->peak = pmPeakAlloc(PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], peakFlux, PM_PEAK_LONE);
    315324        source->peak->flux = peakFlux;
     325        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     326        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
    316327        source->peak->dx   = dPAR[PM_PAR_XPOS];
    317328        source->peak->dy   = dPAR[PM_PAR_YPOS];
    318         source->peak->SN   = sqrt(source->peak->flux); // XXX a proxy: various functions sort by peak S/N
    319 
    320         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     329        if (isfinite (source->errMag) && (source->errMag > 0.0)) {
     330          source->peak->SN = 1.0 / source->errMag;
     331        } else {
     332          source->peak->SN = sqrt(source->peak->flux); // an alternate proxy: various functions sort by peak S/N
     333        }
     334
     335        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    321336        source->crNsigma  = psMetadataLookupF32 (&status, row, "CR_NSIGMA");
    322337        source->extNsigma = psMetadataLookupF32 (&status, row, "EXT_NSIGMA");
     
    578593
    579594// XXX this layout is still the same as PS1_DEV_1
    580 bool pmSourcesWrite_CMF_PS1_V2_XFIT (psFits *fits, pmReadout *readout, psArray *sources, char *extname)
     595bool pmSourcesWrite_CMF_PS1_V2_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
    581596{
    582597
     
    627642            assert (model);
    628643
     644            // skip models which were not actually fitted
     645            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     646
    629647            PAR = model->params->data.F32;
    630648            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_CMP.c

    r20937 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_MatchedRefs.c

    r27177 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
     51
    4252#include "pmAstrometryObjects.h"
    4353#include "pmAstrometryWCS.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_OBJ.c

    r20937 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_PS1_CAL_0.c

    r25979 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4958// XXX how do I generate the source tables which I need to send to PSPS?
    5059
    51 bool pmSourcesWrite_PS1_CAL_0 (psFits *fits, pmReadout *readout, psArray *sources,
    52                                psMetadata *imageHeader, psMetadata *tableHeader, char *extname)
     60bool pmSourcesWrite_PS1_CAL_0 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    5361{
    5462    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    180188        psMetadataAdd (row, PS_LIST_TAIL, "PSF_WIDTH_Y",      PS_DATA_F32, "PSF width in y coordinate",                  axes.minor);
    181189        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    182         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     190        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    183191
    184192        // XXX not sure how to get this : need to load Nimages with weight?
     
    289297        source->peak->dx   = dPAR[PM_PAR_XPOS];
    290298        source->peak->dy   = dPAR[PM_PAR_YPOS];
    291 
    292         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     299        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     300        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
     301
     302        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    293303
    294304        // note that some older versions used PSF_PROBABILITY: this was not well defined.
     
    597607            assert (model);
    598608
     609            // skip models which were not actually fitted
     610            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     611
    599612            PAR = model->params->data.F32;
    600613            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_PS1_DEV_0.c

    r25979 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4958// XXX how do I generate the source tables which I need to send to PSPS?
    5059// XXX: input parameter imageHeader is never used.
    51 bool pmSourcesWrite_PS1_DEV_0 (psFits *fits, psArray *sources, psMetadata *imageHeader,
    52                                psMetadata *tableHeader, char *extname)
     60bool pmSourcesWrite_PS1_DEV_0 (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    5361{
    5462    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    117125        psMetadataAdd (row, PS_LIST_TAIL, "PSF_WIDTH_Y",      PS_DATA_F32, "PSF width in y coordinate",                  axes.minor);
    118126        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    119         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     127        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    120128        // XXX not sure how to get this : need to load Nimages with weight
    121129        psMetadataAdd (row, PS_LIST_TAIL, "N_FRAMES",         PS_DATA_U16, "Number of frames overlapping source center", nImageOverlap);
     
    215223        source->peak->dx   = dPAR[PM_PAR_XPOS];
    216224        source->peak->dy   = dPAR[PM_PAR_YPOS];
    217 
    218         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     225        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     226        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
     227
     228        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    219229
    220230        // XXX other values saved but not loaded?
     
    228238    return (sources);
    229239}
     240
     241bool pmSourcesWrite_PS1_DEV_0_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     242{
     243    return true;
     244}
     245
     246bool pmSourcesWrite_PS1_DEV_0_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
     247{
     248    return true;
     249}
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_PS1_DEV_1.c

    r25979 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4756// this output format is valid for psphot analysis of an image, and does not include calibrated
    4857// values derived in the DVO database.
    49 // XXX how do I generate the source tables which I need to send to PSPS?
    50 
    51 bool pmSourcesWrite_PS1_DEV_1 (psFits *fits, psArray *sources,
    52                                psMetadata *imageHeader, psMetadata *tableHeader, char *extname)
     58
     59bool pmSourcesWrite_PS1_DEV_1(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    5360{
    5461    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    142149        psMetadataAdd (row, PS_LIST_TAIL, "PSF_WIDTH_Y",      PS_DATA_F32, "PSF width in y coordinate",                  axes.minor);
    143150        psMetadataAdd (row, PS_LIST_TAIL, "PSF_THETA",        PS_DATA_F32, "PSF orientation angle",                      axes.theta);
    144         psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeight);
     151        psMetadataAdd (row, PS_LIST_TAIL, "PSF_QF",           PS_DATA_F32, "PSF coverage/quality factor",                source->pixWeightNotBad);
    145152
    146153        // XXX not sure how to get this : need to load Nimages with weight?
     
    259266        source->peak->dx   = dPAR[PM_PAR_XPOS];
    260267        source->peak->dy   = dPAR[PM_PAR_YPOS];
    261 
    262         source->pixWeight = psMetadataLookupF32 (&status, row, "PSF_QF");
     268        source->peak->xf   = PAR[PM_PAR_XPOS]; // more accurate position
     269        source->peak->yf   = PAR[PM_PAR_YPOS]; // more accurate position
     270
     271        source->pixWeightNotBad = psMetadataLookupF32 (&status, row, "PSF_QF");
    263272
    264273        // note that some older versions used PSF_PROBABILITY: this was not well defined.
     
    281290}
    282291
    283 bool pmSourcesWrite_PS1_DEV_1_XSRC (psFits *fits, psArray *sources, char *extname, psMetadata *recipe)
     292bool pmSourcesWrite_PS1_DEV_1_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
    284293{
    285294
     
    453462}
    454463
    455 bool pmSourcesWrite_PS1_DEV_1_XFIT (psFits *fits, psArray *sources, char *extname)
     464bool pmSourcesWrite_PS1_DEV_1_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
    456465{
    457466
     
    502511            assert (model);
    503512
     513            // skip models which were not actually fitted
     514            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
     515
    504516            PAR = model->params->data.F32;
    505517            dPAR = model->dparams->data.F32;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_RAW.c

    r25754 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
    41 #include "pmSourcePhotometry.h"
     48#include "pmPSFtry.h"
     49
    4250#include "pmSourceIO.h"
    4351
     
    120128                 source[0].peak->SN,
    121129                 source[0].apRadius,
    122                  source[0].pixWeight,
     130                 source[0].pixWeightNotBad,
    123131                 model[0].nDOF,
    124132                 model[0].nIter);
     
    182190                 source[0].peak->SN,
    183191                 source[0].apRadius,
    184                  source[0].pixWeight,
     192                 source[0].pixWeightNotBad,
    185193                 model[0].nDOF,
    186194                 model[0].nIter);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_SMPDATA.c

    r25979 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmResiduals.h"
    35 #include "pmGrowthCurve.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
     
    4554// followed by a zero-size matrix, followed by the table data
    4655// XXX: input parameter imageHeader is never used
    47 bool pmSourcesWrite_SMPDATA (psFits *fits, psArray *sources, psMetadata *imageHeader,
    48                              psMetadata *tableHeader, char *extname)
     56bool pmSourcesWrite_SMPDATA (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, psMetadata *tableHeader, char *extname, psMetadata *recipe)
    4957{
    5058    PS_ASSERT_PTR_NON_NULL(fits, false);
     
    106114        psMetadataAdd (row, PS_LIST_TAIL, "THETA",   PS_DATA_F32, "", axes.theta);
    107115        psMetadataAdd (row, PS_LIST_TAIL, "DOPHOT",  PS_DATA_U8,  "", type);
    108         psMetadataAdd (row, PS_LIST_TAIL, "WEIGHT",  PS_DATA_U8,  "", PS_MIN (255, PS_MAX(0, 255*source->pixWeight)));
     116        psMetadataAdd (row, PS_LIST_TAIL, "WEIGHT",  PS_DATA_U8,  "", PS_MIN (255, PS_MAX(0, 255*source->pixWeightNotBad)));
    109117        psMetadataAdd (row, PS_LIST_TAIL, "DUMMY",   PS_DATA_U16, "", 0);
    110118
     
    189197        source->apMag  = psMetadataLookupF32 (&status, row, "MAG_AP")  - ZERO_POINT;
    190198
    191         source->pixWeight = psMetadataLookupU8 (&status, row, "WEIGHT")/255.0;
     199        source->pixWeightNotBad = psMetadataLookupU8 (&status, row, "WEIGHT")/255.0;
    192200        int dophot = psMetadataLookupU8 (&status, row, "DOPHOT");
    193201        pmSourceSetDophotType (source, dophot);
     
    204212    return (sources);
    205213}
     214
     215bool pmSourcesWrite_SMPDATA_XSRC (psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname, psMetadata *recipe)
     216{
     217    return true;
     218}
     219
     220bool pmSourcesWrite_SMPDATA_XFIT(psFits *fits, pmReadout *readout, psArray *sources, psMetadata *imageHeader, char *extname)
     221{
     222    return true;
     223}
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceIO_SX.c

    r20937 r29060  
    2828#include "pmFPAfile.h"
    2929
     30#include "pmTrend2D.h"
     31#include "pmResiduals.h"
     32#include "pmGrowthCurve.h"
    3033#include "pmSpan.h"
     34#include "pmFootprintSpans.h"
    3135#include "pmFootprint.h"
    3236#include "pmPeaks.h"
    3337#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     38#include "pmModelFuncs.h"
     39#include "pmModel.h"
     40#include "pmModelUtils.h"
     41#include "pmModelClass.h"
     42#include "pmSourceMasks.h"
     43#include "pmSourceExtendedPars.h"
     44#include "pmSourceDiffStats.h"
     45#include "pmSource.h"
     46#include "pmSourceFitModel.h"
    3747#include "pmPSF.h"
    38 #include "pmModel.h"
    39 #include "pmSource.h"
    40 #include "pmModelClass.h"
     48#include "pmPSFtry.h"
     49
    4150#include "pmSourceIO.h"
    4251
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceMasks.h

    r24694 r29060  
    3939} pmSourceMode;
    4040
     41// Bit flags to distinguish analysis results
     42// When adding to or subtracting from this list, please also modify pmSourceMaskHeader
     43typedef enum {
     44    PM_SOURCE_MODE2_DEFAULT          = 0x00000000, ///< Initial value: resets all bits
     45    PM_SOURCE_MODE2_DIFF_WITH_SINGLE = 0x00000001, ///< diff source matched to a single positive detection
     46    PM_SOURCE_MODE2_DIFF_WITH_DOUBLE = 0x00000002, ///< diff source matched to positive detections in both images
     47} pmSourceMode2;
    4148
    4249/// Populate header with mask values
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceMatch.c

    r28518 r29060  
    66#include <string.h>
    77#include <pslib.h>
    8 
     8#include "pmHDU.h"
     9#include "pmFPA.h"
     10
     11#include "pmTrend2D.h"
     12#include "pmResiduals.h"
     13#include "pmGrowthCurve.h"
     14#include "pmSpan.h"
     15#include "pmFootprintSpans.h"
     16#include "pmFootprint.h"
     17#include "pmPeaks.h"
     18#include "pmMoments.h"
     19#include "pmModelFuncs.h"
     20#include "pmModel.h"
     21#include "pmModelUtils.h"
     22#include "pmModelClass.h"
     23#include "pmSourceMasks.h"
     24#include "pmSourceExtendedPars.h"
     25#include "pmSourceDiffStats.h"
    926#include "pmSource.h"
     27#include "pmSourceFitModel.h"
     28#include "pmPSF.h"
     29#include "pmPSFtry.h"
     30#include "pmDetections.h"
     31
    1032#include "pmErrorCodes.h"
    1133
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceMatch.h

    r26076 r29060  
    11#ifndef PM_SOURCE_MATCH_H
    22#define PM_SOURCE_MATCH_H
    3 
    4 #include <pslib.h>
    53
    64/// Mask values for matched sources
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceMoments.c

    r27531 r29060  
    2222#include <strings.h>
    2323#include <pslib.h>
     24
    2425#include "pmHDU.h"
    2526#include "pmFPA.h"
    26 #include "pmFPAMaskWeight.h"
     27
     28#include "pmTrend2D.h"
     29#include "pmResiduals.h"
     30#include "pmGrowthCurve.h"
    2731#include "pmSpan.h"
     32#include "pmFootprintSpans.h"
    2833#include "pmFootprint.h"
    2934#include "pmPeaks.h"
    3035#include "pmMoments.h"
    31 #include "pmResiduals.h"
    32 #include "pmGrowthCurve.h"
    33 #include "pmTrend2D.h"
    34 #include "pmPSF.h"
     36#include "pmModelFuncs.h"
    3537#include "pmModel.h"
     38#include "pmModelUtils.h"
     39#include "pmModelClass.h"
     40#include "pmSourceMasks.h"
     41#include "pmSourceExtendedPars.h"
     42#include "pmSourceDiffStats.h"
    3643#include "pmSource.h"
    3744
     
    5461# define VALID_RADIUS(X,Y,RAD2) (((RAD2) >= (PS_SQR(X) + PS_SQR(Y))) ? 1 : 0)
    5562
     63static bool beVerbose = false;
     64void pmSourceMomentsSetVerbose(bool state){ beVerbose = state; }
     65
    5666bool pmSourceMoments(pmSource *source, psF32 radius, psF32 sigma, psF32 minSN, psImageMaskType maskVal)
    5767{
     
    6171    PS_ASSERT_FLOAT_LARGER_THAN(radius, 0.0, false);
    6272
    63     // use sky from moments if defined, 0.0 otherwise
     73    // use sky from moments if defined, 0.0 otherwise
     74
     75    // XXX this value comes from the sky model at the source center, and tends to over-estimate
     76    // the sky in the vicinity of bright sources.  we are better off assuming the model worked
     77    // well:
    6478    psF32 sky = 0.0;
    6579    if (source->moments == NULL) {
    66         source->moments = pmMomentsAlloc();
    67     } else {
    68         sky = source->moments->Sky;
    69     }
     80      source->moments = pmMomentsAlloc();
     81    }
     82    // XXX if (source->moments == NULL) {
     83    // XXX     source->moments = pmMomentsAlloc();
     84    // XXX } else {
     85    // XXX     sky = source->moments->Sky;
     86    // XXX }
    7087
    7188    // First Pass: calculate the first moments (these are subtracted from the coordinates below)
     
    131148            psF32 wDiff = *vWgt;
    132149
    133             // skip pixels below specified significance level.  this is allowed, but should be
    134             // avoided -- the over-weights the wings of bright stars compared to those of faint
    135             // stars.
    136             if (PS_SQR(pDiff) < minSN2*wDiff) continue;
    137             // if (pDiff < 0) continue; // XXX : MWV says I should include < 0.0 valued points...
     150            // skip pixels below specified significance level.  for a PSFs, this
     151            // over-weights the wings of bright stars compared to those of faint stars.
     152            // for the estimator used for extended source analysis (where the window
     153            // function is allowed to be arbitrarily large), we need to clip to avoid
     154            // negative second moments.
     155            if (PS_SQR(pDiff) < minSN2*wDiff) continue; //
     156            if ((minSN > 0.0) && (pDiff < 0)) continue; //
    138157
    139158            // Apply a Gaussian window function.  Be careful with the window function.  S/N
     
    197216    // Xn  = SUM (x - xc)^n * (z - sky)
    198217
     218    psF32 RF = 0.0;
     219    psF32 RH = 0.0;
     220    psF32 RS = 0.0;
    199221    psF32 XX = 0.0;
    200222    psF32 XY = 0.0;
     
    244266            if (r > radius) continue;
    245267
    246             psF32 pDiff = *vPix - sky;
     268            psF32 fDiff = *vPix - sky;
     269            psF32 pDiff = fDiff;
    247270            psF32 wDiff = *vWgt;
    248271
     
    257280            if (sigma > 0.0) {
    258281                // XXX a lot of extra flops; can we do pre-calculate?
    259                 psF32 z  = (PS_SQR(xDiff) + PS_SQR(yDiff))*rsigma2;
     282                // XXX we were re-calculating r2 (maybe the compiler caught this?)
     283                // psF32 z  = (PS_SQR(xDiff) + PS_SQR(yDiff))*rsigma2;
     284                psF32 z  = r2 * rsigma2;
    260285                assert (z >= 0.0);
    261286                psF32 weight  = exp(-z);
     
    266291
    267292            Sum += pDiff;
     293
     294# if (1)
     295# if (0)
     296            if (fDiff < 0) continue;
     297# endif
     298            psF32 rf = r * fDiff;
     299            psF32 rh = sqrt(r) * fDiff;
     300            psF32 rs = fDiff;
     301# else
     302            psF32 rf = r * pDiff;
     303            psF32 rh = sqrt(r) * pDiff;
     304            psF32 rs = pDiff;
     305# endif
    268306
    269307            psF32 x = xDiff * pDiff;
     
    284322            psF32 xyyy = xDiff * yyy / r2;
    285323            psF32 yyyy = yDiff * yyy / r2;
     324
     325            RF  += rf;
     326            RH  += rh;
     327            RS  += rs;
    286328
    287329            XX  += xx;
     
    302344    }
    303345
     346    source->moments->Mrf = RF/RS;
     347    source->moments->Mrh = RH/RS;
     348
    304349    source->moments->Mxx = XX/Sum;
    305350    source->moments->Mxy = XY/Sum;
     
    317362    source->moments->Myyyy = YYYY/Sum;
    318363
    319     // if (source->moments->Mxx < 0) {
    320     //  fprintf (stderr, "error: neg second moment??\n");
    321     // }
    322     // if (source->moments->Myy < 0) {
    323     //  fprintf (stderr, "error: neg second moment??\n");
    324     // }
    325 
    326     psTrace ("psModules.objects", 4, "Mxx: %f  Mxy: %f  Myy: %f  Mxxx: %f  Mxxy: %f  Mxyy: %f  Myyy: %f  Mxxxx: %f  Mxxxy: %f  Mxxyy: %f  Mxyyy: %f  Mxyyy: %f\n",
     364    // Calculate the Kron magnitude (make this block optional?)
     365    // float radKron = 2.5*source->moments->Mrf;
     366    float radKinner = 1.0*source->moments->Mrf;
     367    float radKron   = 2.5*source->moments->Mrf;
     368    float radKouter = 4.0*source->moments->Mrf;
     369
     370    int nKronPix = 0;
     371    Sum = Var = 0.0;
     372    float SumInner = 0.0;
     373    float SumOuter = 0.0;
     374
     375    for (psS32 row = 0; row < source->pixels->numRows ; row++) {
     376
     377        psF32 yDiff = row - yCM;
     378        if (fabs(yDiff) > radKron) continue;
     379
     380        psF32 *vPix = source->pixels->data.F32[row];
     381        psF32 *vWgt = source->variance->data.F32[row];
     382        psImageMaskType  *vMsk = (source->maskObj == NULL) ? NULL : source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[row];
     383
     384        for (psS32 col = 0; col < source->pixels->numCols ; col++, vPix++, vWgt++) {
     385            if (vMsk) {
     386                if (*vMsk & maskVal) {
     387                    vMsk++;
     388                    continue;
     389                }
     390                vMsk++;
     391            }
     392            if (isnan(*vPix)) continue;
     393
     394            psF32 xDiff = col - xCM;
     395            if (fabs(xDiff) > radKron) continue;
     396
     397            // radKron is just a function of (xDiff, yDiff)
     398            psF32 r2  = PS_SQR(xDiff) + PS_SQR(yDiff);
     399            psF32 r  = sqrt(r2);
     400
     401            psF32 pDiff = *vPix - sky;
     402            psF32 wDiff = *vWgt;
     403
     404            // skip pixels below specified significance level.  this is allowed, but should be
     405            // avoided -- the over-weights the wings of bright stars compared to those of faint
     406            // stars.
     407            if (PS_SQR(pDiff) < minSN2*wDiff) continue;
     408
     409            if (r < radKron) {
     410                Sum += pDiff;
     411                Var += wDiff;
     412                nKronPix ++;
     413                // if (beVerbose) fprintf (stderr, "mome: %d %d  %f  %f  %f\n", col, row, sky, *vPix, Sum);
     414            }
     415
     416            if ((r > radKinner) && (r < radKron)) {
     417                SumInner += pDiff;
     418            }
     419            if ((r > radKron)  && (r < radKouter)) {
     420                SumOuter += pDiff;
     421            }
     422        }
     423    }
     424    source->moments->KronFlux = Sum;
     425    source->moments->KronFinner = SumInner;
     426    source->moments->KronFouter = SumOuter;
     427    source->moments->KronFluxErr = sqrt(Var);
     428
     429    psTrace ("psModules.objects", 4, "Mrf: %f  KronFlux: %f  Mxx: %f  Mxy: %f  Myy: %f  Mxxx: %f  Mxxy: %f  Mxyy: %f  Myyy: %f  Mxxxx: %f  Mxxxy: %f  Mxxyy: %f  Mxyyy: %f  Mxyyy: %f\n",
     430             source->moments->Mrf,   source->moments->KronFlux,
    327431             source->moments->Mxx,   source->moments->Mxy,   source->moments->Myy,
    328432             source->moments->Mxxx,  source->moments->Mxxy,  source->moments->Mxyy,  source->moments->Myyy,
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourcePhotometry.c

    r28517 r29060  
    2222#include "pmFPA.h"
    2323#include "pmFPAMaskWeight.h"
     24
     25#include "pmTrend2D.h"
     26#include "pmResiduals.h"
     27#include "pmGrowthCurve.h"
    2428#include "pmSpan.h"
     29#include "pmFootprintSpans.h"
    2530#include "pmFootprint.h"
    2631#include "pmPeaks.h"
    2732#include "pmMoments.h"
    28 #include "pmGrowthCurve.h"
    29 #include "pmResiduals.h"
    30 #include "pmTrend2D.h"
     33#include "pmModelFuncs.h"
     34#include "pmModel.h"
     35#include "pmModelUtils.h"
     36#include "pmModelClass.h"
     37#include "pmSourceMasks.h"
     38#include "pmSourceExtendedPars.h"
     39#include "pmSourceDiffStats.h"
     40#include "pmSource.h"
     41#include "pmSourceFitModel.h"
    3142#include "pmPSF.h"
    32 #include "pmModel.h"
    33 #include "pmSource.h"
    34 #include "pmModelClass.h"
     43#include "pmPSFtry.h"
     44
    3545#include "pmSourcePhotometry.h"
    3646
     
    6676
    6777// XXX masked region should be (optionally) elliptical
    68 bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal)
     78bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal, psImageMaskType markVal)
    6979{
    7080    PS_ASSERT_PTR_NON_NULL(source, false);
     
    122132        for (int i = 0; i < source->modelFits->n; i++) {
    123133            pmModel *model = source->modelFits->data[i];
     134            if (model->flags & PM_MODEL_STATUS_BADARGS) continue;
    124135            status = pmSourcePhotometryModel (&model->mag, NULL, model);
    125136            if (model == source->modelEXT) foundEXT = true;
     
    145156    // measure the contribution of included pixels
    146157    if (mode & PM_SOURCE_PHOT_WEIGHT) {
    147         pmSourcePixelWeight (&source->pixWeight, model, source->maskObj, maskVal);
     158        pmSourcePixelWeight (&source->pixWeightNotBad, &source->pixWeightNotPoor, model, source->maskObj, maskVal, markVal);
    148159    }
    149160
    150161    // measure the contribution of included pixels
    151162    if (mode & PM_SOURCE_PHOT_DIFFSTATS) {
    152         pmSourceMeasureDiffStats (source, maskVal);
     163        pmSourceMeasureDiffStats (source, maskVal, markVal);
    153164    }
    154165
     
    191202
    192203    // measure object aperture photometry
    193     status = pmSourcePhotometryAper  (&source->apMag, model, flux, mask, maskVal);
     204    status = pmSourcePhotometryAper  (&source->apMagRaw, model, flux, mask, maskVal);
    194205    if (!status) {
    195206        psTrace ("psModules.objects", 3, "fail mag : bad Ap Mag");
     
    199210    // if the aper mag is NAN, the flux < 0.  this can happen for sources near the
    200211    // detection limits (esp near bright neighbors)
     212    source->apMag = source->apMagRaw;
    201213    if (isfinite (source->apMag) && isPSF && psf) {
    202214        if (psf->growth && (mode & PM_SOURCE_PHOT_GROWTH)) {
    203             source->apMag += pmGrowthCurveCorrect (psf->growth, source->apRadius);
     215            source->apMag = source->apMagRaw + pmGrowthCurveCorrect (psf->growth, source->apRadius);
    204216        }
    205217        if (mode & PM_SOURCE_PHOT_APCORR) {
    206218            // XXX this should be removed -- we no longer fit for the 'sky bias'
     219            // XXX is this happening???
    207220            rflux   = pow (10.0, 0.4*source->psfMag);
     221            psAssert (psf->skyBias == 0.0, "sky bias not 0");
     222            psAssert (psf->skySat == 0.0, "sky sat not 0");
    208223            source->apMag -= PS_SQR(source->apRadius)*rflux * psf->skyBias + psf->skySat / rflux;
    209224        }
     
    257272    PS_ASSERT_PTR_NON_NULL(image, false);
    258273    PS_ASSERT_PTR_NON_NULL(mask, false);
    259     PS_ASSERT_PTR_NON_NULL(model, false);
     274
     275    if (DO_SKY) {
     276        PS_ASSERT_PTR_NON_NULL(model, false);
     277    }
    260278
    261279    float apSum = 0;
     
    271289    psF32 **imData = image->data.F32;
    272290    psImageMaskType **mkData = mask->data.PS_TYPE_IMAGE_MASK_DATA;
     291    int nAperPix = 0;
    273292
    274293    // measure apMag
    275     for (int ix = 0; ix < image->numCols; ix++) {
    276         for (int iy = 0; iy < image->numRows; iy++) {
     294    for (int iy = 0; iy < image->numRows; iy++) {
     295        for (int ix = 0; ix < image->numCols; ix++) {
    277296            if (mkData[iy][ix] & maskVal)
    278297                continue;
    279298            apSum += imData[iy][ix] - sky;
     299            nAperPix ++;
     300            // fprintf (stderr, "aper: %d %d  %f  %f  %f\n", ix, iy, sky, imData[iy][ix], apSum);
    280301        }
    281302    }
     
    290311
    291312// return source aperture magnitude
    292 bool pmSourcePixelWeight (float *pixWeight, pmModel *model, psImage *mask, psImageMaskType maskVal)
    293 {
    294     PS_ASSERT_PTR_NON_NULL(pixWeight, false);
     313bool pmSourcePixelWeight (float *pixWeightNotBad, float *pixWeightNotPoor, pmModel *model, psImage *mask, psImageMaskType maskVal, psImageMaskType markVal)
     314{
     315    PS_ASSERT_PTR_NON_NULL(pixWeightNotBad, false);
     316    PS_ASSERT_PTR_NON_NULL(pixWeightNotPoor, false);
    295317    PS_ASSERT_PTR_NON_NULL(mask, false);
    296318    PS_ASSERT_PTR_NON_NULL(model, false);
    297319
    298320    float modelSum = 0;
    299     float validSum = 0;
     321    float notBadSum = 0;
     322    float notPoorSum = 0;
    300323    float sky = 0;
    301324    float value;
     
    305328    int dY, DY, NY;
    306329
    307     *pixWeight = 0.0;
     330    *pixWeightNotBad = 0.0;
     331    *pixWeightNotPoor = 0.0;
    308332
    309333    // we only care about the value of the object model, not the local sky
     
    345369
    346370            // for the full model, add all points
    347             value = model->modelFunc (NULL, params, coord) - sky;
     371            value = fabs(model->modelFunc (NULL, params, coord) - sky);
    348372            modelSum += value;
    349373
    350374            // include count only the unmasked pixels within the image area
    351             if (mx < 0)
    352                 continue;
    353             if (my < 0)
    354                 continue;
    355             if (mx >= NX)
    356                 continue;
    357             if (my >= NY)
    358                 continue;
    359             if (mask->data.PS_TYPE_IMAGE_MASK_DATA[my][mx] & maskVal)
    360                 continue;
    361 
    362             validSum += value;
     375            if (mx < 0) continue;
     376            if (my < 0) continue;
     377            if (mx >= NX) continue;
     378            if (my >= NY) continue;
     379
     380            if (!(mask->data.PS_TYPE_IMAGE_MASK_DATA[my][mx] & maskVal)) {
     381                notBadSum += value;
     382            }
     383            if (!(mask->data.PS_TYPE_IMAGE_MASK_DATA[my][mx] & ~markVal)) {
     384                notPoorSum += value;
     385            }
    363386        }
    364387    }
    365388    psFree (coord);
    366389
    367     if (validSum <= 0)
    368         return false;
    369 
    370     *pixWeight = validSum / modelSum;
     390    *pixWeightNotBad  = notBadSum  / modelSum;
     391    *pixWeightNotPoor = notPoorSum / modelSum;
     392
    371393    return (true);
    372394}
     
    374396# define FLUX_LIMIT 3.0
    375397
    376 // return source aperture magnitude
    377 bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal)
     398// measure stats that may be using in difference images for distinguishing real sources from bad residuals
     399bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal, psImageMaskType markVal)
    378400{
    379401    PS_ASSERT_PTR_NON_NULL(source, false);
     
    399421    for (int iy = 0; iy < flux->numRows; iy++) {
    400422        for (int ix = 0; ix < flux->numCols; ix++) {
     423            // only count up the stats in the unmarked region (ie, the aperture)
     424            if (mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & markVal) {
     425                continue;
     426            }
    401427            if (mask->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix] & maskVal) {
    402428                nMask ++;
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourcePhotometry.h

    r28426 r29060  
    5252
    5353bool pmSourceMagnitudesInit (psMetadata *config);
    54 bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal);
    55 bool pmSourcePixelWeight (float *pixWeight, pmModel *model, psImage *mask, psImageMaskType maskVal);
     54bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal, psImageMaskType markVal);
     55
     56bool pmSourcePixelWeight (float *pixWeightNotBad, float *pixWeightNotPoor, pmModel *model, psImage *mask, psImageMaskType maskVal, psImageMaskType markVal);
     57
    5658bool pmSourceChisq (pmModel *model, psImage *image, psImage *mask, psImage *weight, psImageMaskType maskVal, const float covarFactor);
    5759
    58 bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal);
     60bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal, psImageMaskType markVal);
    5961
    6062double pmSourceDataDotModel (const pmSource *Mi, const pmSource *Mj, const bool unweighted_sum, const float covarFactor, psImageMaskType maskVal);
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourcePlotApResid.c

    r26893 r29060  
    2525#include "pmFPAview.h"
    2626#include "pmFPAfile.h"
     27
     28#include "pmTrend2D.h"
     29#include "pmResiduals.h"
     30#include "pmGrowthCurve.h"
    2731#include "pmSpan.h"
     32#include "pmFootprintSpans.h"
    2833#include "pmFootprint.h"
    2934#include "pmPeaks.h"
    3035#include "pmMoments.h"
    31 #include "pmResiduals.h"
    32 #include "pmGrowthCurve.h"
    33 #include "pmTrend2D.h"
     36#include "pmModelFuncs.h"
     37#include "pmModel.h"
     38#include "pmModelUtils.h"
     39#include "pmModelClass.h"
     40#include "pmSourceMasks.h"
     41#include "pmSourceExtendedPars.h"
     42#include "pmSourceDiffStats.h"
     43#include "pmSource.h"
     44#include "pmSourceFitModel.h"
    3445#include "pmPSF.h"
    35 #include "pmModel.h"
    3646#include "pmDetections.h"
    37 #include "pmSource.h"
     47
    3848#include "pmSourcePlots.h"
    3949#include "pmKapaPlots.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourcePlotMoments.c

    r26893 r29060  
    2828#include "pmFPAview.h"
    2929#include "pmFPAfile.h"
     30
     31#include "pmTrend2D.h"
     32#include "pmResiduals.h"
     33#include "pmGrowthCurve.h"
    3034#include "pmSpan.h"
     35#include "pmFootprintSpans.h"
    3136#include "pmFootprint.h"
    3237#include "pmPeaks.h"
    3338#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     39#include "pmModelFuncs.h"
     40#include "pmModel.h"
     41#include "pmModelUtils.h"
     42#include "pmModelClass.h"
     43#include "pmSourceMasks.h"
     44#include "pmSourceExtendedPars.h"
     45#include "pmSourceDiffStats.h"
     46#include "pmSource.h"
     47#include "pmSourceFitModel.h"
    3748#include "pmPSF.h"
    38 #include "pmModel.h"
    3949#include "pmDetections.h"
    40 #include "pmSource.h"
    4150#include "pmSourcePlots.h"
    4251#include "pmKapaPlots.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourcePlotPSFModel.c

    r26893 r29060  
    2828#include "pmFPAview.h"
    2929#include "pmFPAfile.h"
     30
     31
     32#include "pmTrend2D.h"
     33#include "pmResiduals.h"
     34#include "pmGrowthCurve.h"
    3035#include "pmSpan.h"
     36#include "pmFootprintSpans.h"
    3137#include "pmFootprint.h"
    3238#include "pmPeaks.h"
    3339#include "pmMoments.h"
    34 #include "pmGrowthCurve.h"
    35 #include "pmResiduals.h"
    36 #include "pmTrend2D.h"
     40#include "pmModelFuncs.h"
     41#include "pmModel.h"
     42#include "pmModelUtils.h"
     43#include "pmModelClass.h"
     44#include "pmSourceMasks.h"
     45#include "pmSourceExtendedPars.h"
     46#include "pmSourceDiffStats.h"
     47#include "pmSource.h"
     48#include "pmSourceFitModel.h"
    3749#include "pmPSF.h"
    38 #include "pmModel.h"
    3950#include "pmDetections.h"
    40 #include "pmSource.h"
    4151#include "pmSourcePlots.h"
    4252#include "pmKapaPlots.h"
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceSky.c

    r23187 r29060  
    2121#include <string.h>
    2222#include <pslib.h>
     23
    2324#include "pmHDU.h"
    2425#include "pmFPA.h"
    25 #include "pmFPAMaskWeight.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
    2630#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
    2732#include "pmFootprint.h"
    2833#include "pmPeaks.h"
    2934#include "pmMoments.h"
    30 #include "pmResiduals.h"
    31 #include "pmGrowthCurve.h"
    32 #include "pmTrend2D.h"
    33 #include "pmPSF.h"
     35#include "pmModelFuncs.h"
    3436#include "pmModel.h"
     37#include "pmModelUtils.h"
     38#include "pmModelClass.h"
     39#include "pmSourceMasks.h"
     40#include "pmSourceExtendedPars.h"
     41#include "pmSourceDiffStats.h"
    3542#include "pmSource.h"
     43
    3644#include "pmSourceSky.h"
    3745
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceUtils.c

    r24578 r29060  
    2121#include <string.h>
    2222#include <pslib.h>
     23
    2324#include "pmHDU.h"
    2425#include "pmFPA.h"
    25 #include "pmFPAMaskWeight.h"
     26
     27#include "pmTrend2D.h"
     28#include "pmResiduals.h"
     29#include "pmGrowthCurve.h"
    2630#include "pmSpan.h"
     31#include "pmFootprintSpans.h"
    2732#include "pmFootprint.h"
    2833#include "pmPeaks.h"
    2934#include "pmMoments.h"
    30 #include "pmResiduals.h"
    31 #include "pmGrowthCurve.h"
    32 #include "pmTrend2D.h"
    33 #include "pmPSF.h"
     35#include "pmModelFuncs.h"
    3436#include "pmModel.h"
     37#include "pmModelUtils.h"
     38#include "pmModelClass.h"
     39#include "pmSourceMasks.h"
     40#include "pmSourceExtendedPars.h"
     41#include "pmSourceDiffStats.h"
    3542#include "pmSource.h"
     43
    3644#include "pmSourceUtils.h"
    3745
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSourceVisual.c

    r26260 r29060  
    44
    55#include <pslib.h>
     6#include "pmHDU.h"
     7#include "pmFPA.h"
     8
    69#include "pmTrend2D.h"
     10#include "pmResiduals.h"
     11#include "pmGrowthCurve.h"
     12#include "pmSpan.h"
     13#include "pmFootprintSpans.h"
     14#include "pmFootprint.h"
     15#include "pmPeaks.h"
     16#include "pmMoments.h"
     17#include "pmModelFuncs.h"
     18#include "pmModel.h"
     19#include "pmModelUtils.h"
     20#include "pmModelClass.h"
     21#include "pmSourceMasks.h"
     22#include "pmSourceExtendedPars.h"
     23#include "pmSourceDiffStats.h"
     24#include "pmSource.h"
     25#include "pmSourceFitModel.h"
    726#include "pmPSF.h"
    827#include "pmPSFtry.h"
    9 #include "pmSource.h"
     28#include "pmDetections.h"
     29
    1030#include "pmSourceVisual.h"
    1131
     
    1333#include <kapa.h>
    1434#include "pmVisual.h"
     35#include "pmVisualUtils.h"
    1536
    1637// functions used to visualize the analysis as it goes
     
    3455    Graphdata graphdata;
    3556
    36     if (!pmVisualIsVisual()) return true;
     57    if (!pmVisualTestLevel("psphot.psf.metric", 2)) return true;
    3758
    3859    if (kapa1 == -1) {
     
    118139    Graphdata graphdata;
    119140
    120     if (!pmVisualIsVisual()) return true;
     141    if (!pmVisualTestLevel("psphot.psf.subpix", 3)) return true;
    121142
    122143    if (kapa1 == -1) {
     
    280301bool pmSourceVisualShowModelFits (pmPSF *psf, psArray *sources, psImageMaskType maskVal) {
    281302
    282     if (!pmVisualIsVisual()) return true;
     303    if (!pmVisualTestLevel("psphot.psf.fits", 2)) return true;
    283304
    284305    if (kapa2 == -1) {
     
    360381bool pmSourceVisualShowModelFit (pmSource *source) {
    361382
    362     if (!pmVisualIsVisual()) return true;
     383    if (!pmVisualTestLevel("psphot.psf.fitresid", 2)) return true;
     384
    363385    if (!source->pixels) return false;
    364386    if (!source->modelFlux) return false;
     
    404426    Graphdata graphdata;
    405427
    406     if (!pmVisualIsVisual() || !plotPSF) return true;
     428    if (!plotPSF) return true;
     429    if (!pmVisualTestLevel("psphot.psf.resid", 2)) return true;
    407430
    408431    if (kapa1 == -1) {
  • branches/sc_branches/trunkTest/psModules/src/objects/pmSpan.h

    r20945 r29060  
    1010# ifndef PM_SPAN_H
    1111# define PM_SPAN_H
    12 
    13 #include <pslib.h>
    1412
    1513/// @addtogroup Objects Object Detection / Analysis Functions
  • branches/sc_branches/trunkTest/psModules/src/objects/pmTrend2D.h

    r25754 r29060  
    1212# ifndef PM_TREND_2D_H
    1313# define PM_TREND_2D_H
    14 
    15 #include <pslib.h>
    1614
    1715/// @addtogroup Objects Object Detection / Analysis Functions
  • branches/sc_branches/trunkTest/psModules/src/psmodules.h

    r28043 r29060  
    1010#include <pmKapaPlots.h>
    1111#include <pmVisual.h>
     12#include <pmVisualUtils.h>
    1213#include <ippStages.h>
    1314#include <ippDiffMode.h>
     
    113114
    114115// the following headers are from psModule:objects
     116#include <pmTrend2D.h>
     117#include <pmResiduals.h>
     118#include <pmGrowthCurve.h>
     119
    115120#include <pmSpan.h>
    116121#include <pmFootprintSpans.h>
     
    119124#include <pmDetections.h>
    120125#include <pmMoments.h>
     126
     127#include <pmModelFuncs.h>
     128#include <pmModel.h>
     129
     130#include <pmSourceMasks.h>
    121131#include <pmSourceExtendedPars.h>
    122132#include <pmSourceDiffStats.h>
    123 #include <pmResiduals.h>
    124 #include <pmGrowthCurve.h>
    125 #include <pmTrend2D.h>
     133#include <pmSource.h>
     134#include <pmSourceFitModel.h>
    126135#include <pmPSF.h>
    127 #include <pmModel.h>
    128 #include <pmSourceMasks.h>
    129 #include <pmSource.h>
     136#include <pmPSFtry.h>
    130137#include <pmPhotObj.h>
    131138#include <pmSourceUtils.h>
    132139#include <pmSourceIO.h>
    133140#include <pmSourceSky.h>
    134 #include <pmSourceFitModel.h>
    135141#include <pmSourceFitSet.h>
    136142#include <pmSourceContour.h>
    137143#include <pmSourcePlots.h>
    138144#include <pmPSF_IO.h>
    139 #include <pmPSFtry.h>
    140145#include <pmModelClass.h>
    141146#include <pmModelUtils.h>
     
    144149#include <pmSourceMatch.h>
    145150#include <pmDetEff.h>
     151#include <pmPCMdata.h>
    146152
    147153// The following headers are from random locations, here because they cross bounds
  • branches/sc_branches/trunkTest/psastro/src/psastroModelFitBoresite.c

    r23989 r29060  
    8080    params->data.F32[PAR_T0] = 0.0;
    8181
    82     psMinimization *myMin = psMinimizationAlloc (25, 0.001);
     82    psMinimization *myMin = psMinimizationAlloc (25, 0.001, 1.0);
    8383    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
    8484   
  • branches/sc_branches/trunkTest/psphot

    • Property svn:mergeinfo deleted
  • branches/sc_branches/trunkTest/psphot/src/Makefile.am

    r28013 r29060  
    163163        psphotOutput.c                 \
    164164        psphotFakeSources.c            \
    165         psphotModelWithPSF.c           \
    166165        psphotExtendedSourceAnalysis.c \
    167166        psphotExtendedSourceAnalysisByObject.c \
    168167        psphotExtendedSourceFits.c     \
    169168        psphotKernelFromPSF.c          \
    170         psphotPSFConvModel.c           \
    171169        psphotFitSet.c                 \
    172170        psphotSourceFreePixels.c       \
  • branches/sc_branches/trunkTest/psphot/src/psphot.h

    r28013 r29060  
    1212
    1313#define PSPHOT_RECIPE_PSF_FAKE_ALLOW "PSF.FAKE.ALLOW" // Name for recipe component permitting fake PSFs
    14 
    15 // pmPCMData : PSF Convolved Model data storage structure
    16 typedef struct {
    17     psImage *model;
    18     psArray *dmodels;
    19     psImage *modelConv;
    20     psArray *dmodelsConv;
    21 } pmPCMData;
    2214
    2315// top-level psphot functions
     
    114106bool            psphotExtendedSourceFits (pmConfig *config, const pmFPAview *view, const char *filerule);
    115107bool            psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe);
     108bool            psphotExtendedSourceFits_Threaded (psThreadJob *job);
    116109
    117110bool            psphotApResid (pmConfig *config, const pmFPAview *view, const char *filerule);
     
    188181
    189182// functions to set the correct source pixels
    190 bool            psphotInitRadiusPSF(const psMetadata *recipe, const psMetadata *analysis, const pmModelType type);
     183bool            psphotInitRadiusPSF (psMetadata *recipe, pmReadout *readout);
     184bool            psphotInitRadiusEXT (psMetadata *recipe, pmReadout *readout);
    191185
    192186bool            psphotCheckRadiusPSF (pmReadout *readout, pmSource *source, pmModel *model, psImageMaskType markVal);
    193187bool            psphotCheckRadiusPSFBlend (pmReadout *readout, pmSource *source, pmModel *model, psImageMaskType markVal, float dR);
    194 bool            psphotInitRadiusEXT (psMetadata *recipe, pmModelType type);
    195 bool            psphotCheckRadiusEXT (pmReadout *readout, pmSource *source, pmModel *model, psImageMaskType markVal);
    196 float           psphotSetRadiusEXT (pmReadout *readout, pmSource *source, psImageMaskType markVal);
     188bool            psphotSetRadiusFootprint (float *radius, pmReadout *readout, pmSource *source, psImageMaskType markVal, float factor);
     189bool            psphotSetRadiusModel (pmModel *model, pmReadout *readout, pmSource *source, psImageMaskType markVal, bool deep);
    197190
    198191bool            psphotDumpMoments (psMetadata *recipe, psArray *sources);
     
    210203//  functions to support the source fitting process
    211204bool            psphotInitLimitsPSF (psMetadata *recipe, pmReadout *readout);
    212 bool            psphotInitLimitsEXT (psMetadata *recipe);
    213 bool            psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal);
    214 bool            psphotFitBlob (pmReadout *readout, pmSource *source, psArray *sources, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal);
    215 bool            psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal);
    216 pmModel        *psphotFitEXT (pmReadout *readout, pmSource *source, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal);
    217 psArray        *psphotFitDBL (pmReadout *readout, pmSource *source, psImageMaskType maskVal, psImageMaskType markVal);
     205bool            psphotInitLimitsEXT (psMetadata *recipe, pmReadout *readout);
     206bool            psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal);
     207bool            psphotFitBlob (pmReadout *readout, pmSource *source, psArray *sources, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal);
     208bool            psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal);
     209pmModel        *psphotFitEXT (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal);
     210psArray        *psphotFitDBL (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal);
    218211
    219212// functions to support simultaneous multi-source fitting
     
    252245bool            psphotVisualShowBackground (pmConfig *config, const pmFPAview *view, pmReadout *readout);
    253246bool            psphotVisualShowSignificance (psImage *image, float min, float max);
     247bool            psphotVisualShowLogSignificance (psImage *image, float min, float max);
    254248bool            psphotVisualShowPeaks (pmDetections *detections);
    255249bool            psphotVisualShowFootprints (pmDetections *detections);
     
    292286bool psphotRadialBins (psMetadata *recipe, pmSource *source, float radiusMax, float skynoise);
    293287
    294 // structures & functions to support psf-convolved model fitting
    295 
    296 // psf-convolved model fitting
    297 bool psphotModelWithPSF_LMM (
    298     psMinimization *min,
    299     psImage *covar,
    300     psVector *params,
    301     psMinConstraint *constraint,
    302     pmSource *source,
    303     const psKernel *psf,
    304     psMinimizeLMChi2Func func);
    305 
    306 psF32 psphotModelWithPSF_SetABX(
    307     psImage  *alpha,
    308     psVector *beta,
    309     const psVector *params,
    310     const psVector *paramMask,
    311     pmPCMData *pcm,
    312     const pmSource *source,
    313     const psKernel *psf,
    314     psMinimizeLMChi2Func func);
    315 
    316 pmPCMData *pmPCMDataAlloc (
    317     const psVector *params,
    318     const psVector *paramMask,
    319     pmSource *source);
    320 
    321 psImage *pmPCMDataSaveImage (pmPCMData *pcm);
    322 
    323288int psphotKapaOpen (void);
    324289bool psphotKapaClose (void);
     
    364329
    365330bool psphotFitSourcesLinearStack (pmConfig *config, psArray *objects, bool final);
    366 int pmPhotObjSortBySN (const void **a, const void **b);
    367 int pmPhotObjSortByX (const void **a, const void **b);
    368331
    369332typedef enum {
     
    462425bool psphotStackObjectsUnifyPosition (psArray *objects);
    463426
     427bool psphotFitSersicIndex (pmModel *model, pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal);
     428
     429bool psphotFitSersicIndexPCM (pmPCMdata *pcm, pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
     430pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
     431
    464432#endif
  • branches/sc_branches/trunkTest/psphot/src/psphotAddNoise.c

    r28013 r29060  
    3434
    3535    bool status = false;
    36     psEllipseShape oldshape;
    37     psEllipseShape newshape;
    38     psEllipseAxes axes;
    3936
    4037    // find the currently selected readout
     
    8683        if (!(source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED)) continue;
    8784
    88         // select appropriate model
    89         pmModel *model = pmSourceGetModel (NULL, source);
    90         if (model == NULL) continue;  // model must be defined
    91 
    92         if (add) {
    93             psTrace ("psphot", 4, "adding noise for object at %f,%f\n", model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
    94         } else {
    95             psTrace ("psphot", 4, "remove noise for object at %f,%f\n", model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]);
    96         }
    97 
    98         psF32 *PAR = model->params->data.F32;
    99 
    100         // save original values
    101         float oldI0  = PAR[PM_PAR_I0];
    102         oldshape.sx  = PAR[PM_PAR_SXX];
    103         oldshape.sy  = PAR[PM_PAR_SYY];
    104         oldshape.sxy = PAR[PM_PAR_SXY];
    105 
    106         // XXX can this be done more intelligently?
    107         if (oldI0 == 0.0) continue;
    108         if (!isfinite(oldI0)) continue;
    109 
    110         // increase size and height of source
    111         axes = psEllipseShapeToAxes (oldshape, 20.0);
    112         axes.major *= SIZE;
    113         axes.minor *= SIZE;
    114         newshape = psEllipseAxesToShape (axes);
    115         PAR[PM_PAR_I0]  = FACTOR*oldI0;
    116         PAR[PM_PAR_SXX] = newshape.sx;
    117         PAR[PM_PAR_SYY] = newshape.sy;
    118         PAR[PM_PAR_SXY] = newshape.sxy;
    119 
    120         // XXX if we use pmSourceOp, the size (and possibly Io) will not be respected
    121         pmSourceOp (source, PM_MODEL_OP_FULL | PM_MODEL_OP_NOISE, add, maskVal, 0, 0);
    122 
    123         // restore original values
    124         PAR[PM_PAR_I0]  = oldI0;
    125         PAR[PM_PAR_SXX] = oldshape.sx;
    126         PAR[PM_PAR_SYY] = oldshape.sy;
    127         PAR[PM_PAR_SXY] = oldshape.sxy;
     85        pmSourceNoiseOp (source, PM_MODEL_OP_FULL | PM_MODEL_OP_NOISE, FACTOR, SIZE, add, maskVal, 0, 0);
    12886    }
    12987    if (add) {
     
    13290        psLogMsg ("psphot.noise", PS_LOG_INFO, "sub noise for %ld objects: %f sec\n", sources->n, psTimerMark ("psphot.noise"));
    13391    }
     92
    13493    return true;
    13594}
  • branches/sc_branches/trunkTest/psphot/src/psphotApResid.c

    r28524 r29060  
    459459        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
    460460
    461         bool status = pmSourceMagnitudes (source, psf, photMode, maskVal);
     461        bool status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    462462
    463463        // clear the mask bit
  • branches/sc_branches/trunkTest/psphot/src/psphotArguments.c

    r25985 r29060  
    176176    }
    177177
    178     // visual : interactive display mode
    179     if ((N = psArgumentGet (argc, argv, "-visual"))) {
    180         psArgumentRemove (N, &argc, argv);
    181         pmVisualSetVisual(true);
    182     }
    183 
    184178    // break : used from recipe throughout psphotReadout
    185179    if ((N = psArgumentGet (argc, argv, "-break"))) {
  • branches/sc_branches/trunkTest/psphot/src/psphotBlendFit.c

    r28405 r29060  
    6565    assert (status && fitIter > 0);
    6666
    67     float fitTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_TOL"); // Fit tolerance
    68     assert (status && isfinite(fitTol) && fitTol > 0);
     67    float fitMinTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MIN_TOL"); // Fit tolerance
     68    if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     69        fitMinTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_TOL"); // Fit tolerance
     70        if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     71            psAbort("PSF_FIT_MIN_TOL (and PSF_FIT_TOL) not defined or positive");
     72        }
     73    }
     74
     75    float fitMaxTol = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MAX_TOL"); // Fit tolerance
     76    if (!status || !isfinite(fitMaxTol) || fitMaxTol <= 0) {
     77        fitMaxTol = 1.0;
     78    }
    6979
    7080    bool poisson = psMetadataLookupBool(&status, recipe, "POISSON.ERRORS.PHOT.LMM"); // Poisson errors?
    7181    assert (status);
    7282
     83    float maxChisqDOF = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MAX_CHISQ"); // Fit tolerance
     84
    7385    float skySig = psMetadataLookupF32(&status, recipe, "SKY_SIG");
    7486    assert (status && isfinite(skySig) && skySig > 0);
    7587
    7688    // Define source fitting parameters for extended source fits
    77     pmSourceFitModelInit(fitIter, fitTol, PS_SQR(skySig), poisson);
     89    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
     90    fitOptions->nIter         = fitIter;
     91    fitOptions->minTol        = fitMinTol;
     92    fitOptions->maxTol        = fitMaxTol;
     93    fitOptions->maxChisqDOF   = maxChisqDOF;
     94    fitOptions->poissonErrors = poisson;
     95    fitOptions->weight        = PS_SQR(skySig);
     96    fitOptions->mode          = PM_SOURCE_FIT_PSF;
    7897
    7998    psphotInitLimitsPSF (recipe, readout);
    80     psphotInitLimitsEXT (recipe);
    81     psphotInitRadiusPSF (recipe, readout->analysis, psf->type);
     99    psphotInitLimitsEXT (recipe, readout);
     100    psphotInitRadiusPSF (recipe, readout);
    82101
    83102    // starts the timer, sets up the array of fitSets
     
    88107    if (!sources->n) {
    89108        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping blend");
     109        psFree (fitOptions);
    90110        return true;
    91111    }
     
    112132            psArrayAdd(job->args, 1, psf);
    113133            psArrayAdd(job->args, 1, newSources); // return for new sources
     134            psArrayAdd(job->args, 1, fitOptions); // default fit options
    114135            psFree (newSources);
    115136
     
    121142            if (!psThreadJobAddPending(job)) {
    122143                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     144                psFree (fitOptions);
    123145                return NULL;
    124146            }
    125 
    126 # if (0)
    127             {
    128                 int nfit = 0;
    129                 int npsf = 0;
    130                 int next = 0;
    131                 int nfail = 0;
    132                 psArray *newSources = psArrayAllocEmpty(16);
    133 
    134                 if (!psphotBlendFit_Unthreaded (&nfit, &npsf, &next, &nfail, readout, recipe, cells->data[j], psf, newSources)) {
    135                     psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    136                     return NULL;
    137                 }
    138                 Nfit += nfit;
    139                 Npsf += npsf;
    140                 Next += next;
    141                 Nfail += nfail;
    142 
    143                 // add these back onto sources
    144                 for (int k = 0; k < newSources->n; k++) {
    145                     psArrayAdd (sources, 16, newSources->data[k]);
    146                 }
    147                 psFree (newSources);
    148             }
    149 # endif
    150147        }
    151148
     
    153150        if (!psThreadPoolWait (false)) {
    154151            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     152            psFree (fitOptions);
    155153            return NULL;
    156154        }
     
    163161            } else {
    164162                psScalar *scalar = NULL;
    165                 scalar = job->args->data[5];
     163                scalar = job->args->data[6];
    166164                Nfit += scalar->data.S32;
    167                 scalar = job->args->data[6];
     165                scalar = job->args->data[7];
    168166                Npsf += scalar->data.S32;
    169                 scalar = job->args->data[7];
     167                scalar = job->args->data[8];
    170168                Next += scalar->data.S32;
    171                 scalar = job->args->data[8];
     169                scalar = job->args->data[9];
    172170                Nfail += scalar->data.S32;
    173171
     
    186184      psphotSaveImage (NULL, readout->image,  "image.v2.fits");
    187185    }
     186    psFree (fitOptions);
    188187
    189188    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);
     
    204203    psScalar *scalar = NULL;
    205204
    206     pmReadout *readout  = job->args->data[0];
    207     psMetadata *recipe  = job->args->data[1];
    208     psArray *sources    = job->args->data[2];
    209     pmPSF *psf          = job->args->data[3];
    210     psArray *newSources = job->args->data[4];
     205    pmReadout *readout             = job->args->data[0];
     206    psMetadata *recipe             = job->args->data[1];
     207    psArray *sources               = job->args->data[2];
     208    pmPSF *psf                     = job->args->data[3];
     209    psArray *newSources            = job->args->data[4];
     210    pmSourceFitOptions *fitOptions = job->args->data[5];
    211211
    212212    // bit-masks to test for good/bad pixels
     
    269269        Nfit ++;
    270270
     271        if (0) {
     272            psF32 Mxx = source->moments->Mxx;
     273            psF32 Myy = source->moments->Myy;
     274            fprintf (stderr, "1: Mxx: %f, Myy: %f\n", Mxx, Myy);
     275        }
     276
    271277        // try fitting PSFs or extended sources depending on source->mode
    272278        // these functions subtract the resulting fitted source
    273279        if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
    274             if (psphotFitBlob (readout, source, newSources, psf, maskVal, markVal)) {
     280            if (psphotFitBlob (readout, source, newSources, psf, fitOptions, maskVal, markVal)) {
    275281                source->type = PM_SOURCE_TYPE_EXTENDED;
    276282                psTrace ("psphot", 5, "source at %7.1f, %7.1f is ext", source->peak->xf, source->peak->yf);
     
    280286            }
    281287        } else {
    282             if (psphotFitBlend (readout, source, psf, maskVal, markVal)) {
     288            if (psphotFitBlend (readout, source, psf, fitOptions, maskVal, markVal)) {
    283289                source->type = PM_SOURCE_TYPE_STAR;
    284290                psTrace ("psphot", 5, "source at %7.1f, %7.1f is psf", source->peak->xf, source->peak->yf);
     
    289295        }
    290296
     297        if (0) {
     298            psF32 Mxx = source->moments->Mxx;
     299            psF32 Myy = source->moments->Myy;
     300            fprintf (stderr, "2: Mxx: %f, Myy: %f\n", Mxx, Myy);
     301        }
     302
    291303        psTrace ("psphot", 5, "source at %7.1f, %7.1f failed", source->peak->xf, source->peak->yf);
    292304        Nfail ++;
     
    298310
    299311    // change the value of a scalar on the array (wrap this and put it in psArray.h)
    300     scalar = job->args->data[5];
     312    scalar = job->args->data[6];
    301313    scalar->data.S32 = Nfit;
    302314
    303     scalar = job->args->data[6];
     315    scalar = job->args->data[7];
    304316    scalar->data.S32 = Npsf;
    305317
    306     scalar = job->args->data[7];
     318    scalar = job->args->data[8];
    307319    scalar->data.S32 = Next;
    308320
    309     scalar = job->args->data[8];
     321    scalar = job->args->data[9];
    310322    scalar->data.S32 = Nfail;
    311323
  • branches/sc_branches/trunkTest/psphot/src/psphotChoosePSF.c

    r28013 r29060  
    7474
    7575    // structure to store user options defining the psf
    76     pmPSFOptions *options = pmPSFOptionsAlloc ();
     76    pmPSFOptions *options = pmPSFOptionsAlloc();
    7777
    7878    // load user options from the recipe. no need to check existence -- they are
     
    138138        return false;
    139139    }
    140     float fitTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_TOL"); // Fit tolerance
    141     if (!status || !isfinite(fitTol) || fitTol <= 0) {
    142         psError(PS_ERR_BAD_PARAMETER_VALUE, true, "PSF_FIT_TOL is not positive");
    143         return false;
    144     }
    145     pmSourceFitModelInit(fitIter, fitTol, PS_SQR(SKY_SIG), options->poissonErrorsPhotLMM);
    146 
     140    float fitMinTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_MIN_TOL"); // Fit tolerance
     141    if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     142        fitMinTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_TOL"); // Fit tolerance
     143        if (!status || !isfinite(fitMinTol) || fitMinTol <= 0) {
     144            psError(PS_ERR_BAD_PARAMETER_VALUE, true, "PSF_FIT_MIN_TOL (and PSF_FIT_TOL) not defined or positive");
     145            return false;
     146        }
     147    }
     148    float fitMaxTol = psMetadataLookupF32 (&status, recipe, "PSF_FIT_MAX_TOL"); // Fit tolerance
     149    if (!status || !isfinite(fitMaxTol) || fitMaxTol <= 0) {
     150        fitMaxTol = 1.0;
     151    }
     152    float maxChisqDOF = psMetadataLookupF32 (&status, recipe, "PSF_FIT_MAX_CHISQ"); // Fit tolerance
     153
     154    // options which modify the behavior of the model fitting
     155    options->fitOptions                = pmSourceFitOptionsAlloc();
     156    options->fitOptions->nIter         = fitIter;
     157    options->fitOptions->minTol        = fitMinTol;
     158    options->fitOptions->maxTol        = fitMaxTol;
     159    options->fitOptions->maxChisqDOF   = maxChisqDOF;
     160    options->fitOptions->poissonErrors = options->poissonErrorsPhotLMM;
     161    options->fitOptions->weight        = PS_SQR(SKY_SIG);
     162    options->fitOptions->mode          = PM_SOURCE_FIT_PSF;
     163   
    147164    psArray *stars = psArrayAllocEmpty (sources->n);
    148165
     
    227244
    228245    // try each model option listed in config
     246    // pmPSFtryModel makes a local copy of the sources -- those points are not the same as those for 'sources'
    229247    for (int i = 0; i < modelNames->n; i++) {
    230248        char *modelName = modelNames->data[i];
     
    304322
    305323    // unset the PSFSTAR flag for stars not used for PSF model
     324    // XXX a more efficient way of achieving this would be to record a pair of arrays
     325    // of the source index and the source id for the psf stars.  but that would require we do
     326    // not re-sort the source list in the meanwhile
     327    int nDrop = 0;
    306328    for (int i = 0; i < try->sources->n; i++) {
    307329        pmSource *source = try->sources->data[i];
    308330        if (try->mask->data.PS_TYPE_VECTOR_MASK_DATA[i]) {
    309             source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
    310         }
    311     }
     331            // need to find this source in the original list (these are copies, not pointers)
     332            for (int j = 0; j < sources->n; j++) {
     333                pmSource *realSource = sources->data[j];
     334                if (realSource->id != source->id) continue;
     335                realSource->mode &= ~PM_SOURCE_MODE_PSFSTAR;
     336                source->mode &= ~PM_SOURCE_MODE_PSFSTAR;
     337                nDrop ++;
     338                break;
     339            }
     340        }
     341    }
     342    // fprintf (stderr, "drop %d stars as PSF stars\n", nDrop);
     343
     344    // XXX is this working?
     345    // int N1 = 0;
     346    // for (int i = 0; i < try->sources->n; i++) {
     347    //     pmSource *source = try->sources->data[i];
     348    //  fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
     349    //  if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
     350    //      N1 ++;
     351    //     }
     352    // }
     353    // int N2 = 0;
     354    // for (int i = 0; i < sources->n; i++) {
     355    //     pmSource *source = sources->data[i];
     356    //  fprintf (stderr, "%llx : %d\n", (long long int) source, (source->mode & PM_SOURCE_MODE_PSFSTAR));
     357    //  if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
     358    //      N2 ++;
     359    //     }
     360    // }
     361    // fprintf (stderr, "N1: %d, N2: %d\n", N1, N2);
    312362
    313363    // build a PSF residual image
  • branches/sc_branches/trunkTest/psphot/src/psphotEllipticalContour.c

    r27819 r29060  
    8282    params->data.F32[PAR_RMIN]    = Rmin;
    8383
    84     psMinimization *myMin = psMinimizationAlloc (25, 0.001);
     84    psMinimization *myMin = psMinimizationAlloc (25, 0.01, 1.00);
    8585    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
    8686   
  • branches/sc_branches/trunkTest/psphot/src/psphotExtendedSourceAnalysis.c

    r28013 r29060  
    121121        if (source->peak->y > AnalysisRegion.y1) continue;
    122122
     123        // fprintf (stderr, "xsrc: %f, %f : %f\n", source->peak->xf, source->peak->yf, source->peak->SN);
     124
    123125        // replace object in image
    124126        if (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED) {
     
    157159        }
    158160
    159 # if (0)
    160         // Isophotal Mags
    161         if (doIsophotal) {
    162             if (!psphotIsophotal (source, recipe, maskVal)) {
    163                 psTrace ("psphot", 5, "failed to measure isophotal mags for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    164             } else {
    165                 psTrace ("psphot", 5, "measured isophotal mags for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    166                 Nisophot ++;
    167                 source->mode |= PM_SOURCE_MODE_EXTENDED_STATS;
    168             }
    169         }
    170         // Kron Mags
    171         if (doKron) {
    172             if (!psphotKron (source, recipe, maskVal)) {
    173                 psTrace ("psphot", 5, "failed to measure kron mags for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    174             } else {
    175                 psTrace ("psphot", 5, "measure kron mags for source at %7.1f, %7.1f", source->moments->Mx, source->moments->My);
    176                 Nkron ++;
    177                 source->mode |= PM_SOURCE_MODE_EXTENDED_STATS;
    178             }
    179         }
    180 # endif
    181 
    182161        // re-subtract the object, leave local sky
    183162        pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     
    201180    }
    202181
     182    // fprintf (stderr, "xsrc : tried %ld objects\n", sources->n);
     183
    203184    return true;
    204185}
  • branches/sc_branches/trunkTest/psphot/src/psphotExtendedSourceFits.c

    r28013 r29060  
    3131// non-linear model fitting for extended sources
    3232bool psphotExtendedSourceFitsReadout (pmConfig *config, const pmFPAview *view, const char *filerule, int index, psMetadata *recipe) {
     33
     34    bool status;
     35    int Next = 0;
     36    int Nconvolve = 0;
     37    int NconvolvePass = 0;
     38    int Nplain = 0;
     39    int NplainPass = 0;
     40
     41    psTimerStart ("psphot.extended");
     42
     43    // find the currently selected readout
     44    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
     45    psAssert (file, "missing file?");
     46
     47    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     48    psAssert (readout, "missing readout?");
     49
     50    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     51    psAssert (detections, "missing detections?");
     52
     53    psArray *sources = detections->allSources;
     54    psAssert (sources, "missing sources?");
     55
     56    if (!sources->n) {
     57        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping source size");
     58        return true;
     59    }
     60
     61    // determine the number of allowed threads
     62    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
     63    if (!status) {
     64        nThreads = 0;
     65    }
     66
     67    // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
     68    psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
     69    assert (maskVal);
     70
     71    psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); // Mask value for bad pixels
     72    assert (markVal);
     73
     74    // maskVal is used to test for rejected pixels, and must include markVal
     75    maskVal |= markVal;
     76
     77    // select the collection of desired models
     78    psMetadata *models = psMetadataLookupMetadata (&status, recipe, "EXTENDED_SOURCE_MODELS");
     79    if (!status) {
     80        psWarning ("extended source model fits requested but model model is missing (EXTENDED_SOURCE_MODELS)\n");
     81        return true;
     82    }
     83    if (models->list->n == 0) {
     84        psWarning ("extended source model fits requested but no models are specified\n");
     85        return true;
     86    }
     87
     88    // validate the model entries
     89    psMetadataIterator *iter = psMetadataIteratorAlloc (models, PS_LIST_HEAD, NULL);
     90    psMetadataItem *item = NULL;
     91    while ((item = psMetadataGetAndIncrement (iter)) != NULL) {
     92
     93      if (item->type != PS_DATA_METADATA) {
     94        psAbort ("Invalid type for EXTENDED_SOURCE_MODEL entry %s, not a metadata folder", item->name);
     95        // XXX we could cull the bad entries or build a validated model folder
     96      }
     97
     98      psMetadata *model = (psMetadata *) item->data.md;
     99
     100      // check on the model type
     101      char *modelName = psMetadataLookupStr (&status, model, "MODEL");
     102      int modelType = pmModelClassGetType (modelName);
     103      if (modelType < 0) {
     104        psAbort ("Unknown model class for EXTENDED_SOURCE_MODEL entry %s: %s", item->name, modelName);
     105      }
     106      psMetadataAddS32 (model, PS_LIST_TAIL, "MODEL_TYPE", PS_META_REPLACE, "", modelType);
     107
     108      // check on the SNLIM, set a float value
     109      char *SNword = psMetadataLookupStr (&status, model, "SNLIM");
     110      if (!status) {
     111        psAbort("SNLIM not defined for extended source model %s\n", item->name);
     112      }
     113      float SNlim = atof (SNword);
     114      psMetadataAddF32 (model, PS_LIST_TAIL, "SNLIM_VALUE", PS_META_REPLACE, "", SNlim);
     115
     116      // check on the PSF-Convolution status
     117      char *convolvedWord = psMetadataLookupStr (&status, model, "PSF_CONVOLVED");
     118      if (!status || (strcasecmp (convolvedWord, "true") && strcasecmp (convolvedWord, "false"))) {
     119        psAbort ("PSF_CONVOLVED entry invalid or missing for EXTENDED_SOURCE_MODEL entry %s", item->name);
     120      }
     121      bool convolved = !strcasecmp (convolvedWord, "true");
     122      psMetadataAddBool (model, PS_LIST_TAIL, "PSF_CONVOLVED_VALUE", PS_META_REPLACE, "", convolved);
     123    }
     124    psFree (iter);
     125
     126    // option to limit analysis to a specific region
     127    char *region = psMetadataLookupStr (&status, recipe, "ANALYSIS_REGION");
     128    psRegion *AnalysisRegion = psRegionAlloc(0,0,0,0);
     129    *AnalysisRegion = psRegionForImage(readout->image, psRegionFromString (region));
     130    if (psRegionIsNaN (*AnalysisRegion)) psAbort("analysis region mis-defined");
     131
     132    // what fraction of the PSF is used? (radius in pixels : 2 -> 5x5 box)
     133    int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
     134    assert (status);
     135
     136    // source analysis is done in S/N order (brightest first)
     137    sources = psArraySort (sources, pmSourceSortBySN);
     138
     139    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
     140    int Cx = 1, Cy = 1;
     141    psphotChooseCellSizes (&Cx, &Cy, readout, nThreads);
     142
     143    psArray *cellGroups = psphotAssignSources (Cx, Cy, sources);
     144
     145    for (int i = 0; i < cellGroups->n; i++) {
     146
     147        psArray *cells = cellGroups->data[i];
     148
     149        for (int j = 0; j < cells->n; j++) {
     150
     151            // allocate a job -- if threads are not defined, this just runs the job
     152            psThreadJob *job = psThreadJobAlloc ("PSPHOT_EXTENDED_FIT");
     153
     154            psArrayAdd(job->args, 1, readout);
     155            psArrayAdd(job->args, 1, cells->data[j]); // sources
     156            psArrayAdd(job->args, 1, models);
     157            psArrayAdd(job->args, 1, AnalysisRegion); // XXX make a pointer
     158
     159            PS_ARRAY_ADD_SCALAR(job->args, psfSize, PS_TYPE_S32);
     160            PS_ARRAY_ADD_SCALAR(job->args, maskVal, PS_TYPE_IMAGE_MASK);
     161            PS_ARRAY_ADD_SCALAR(job->args, markVal, PS_TYPE_IMAGE_MASK);
     162
     163            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Next
     164            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nconvolve
     165            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for NconvolvePass
     166            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nplain
     167            PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for NplainPass
     168
     169            if (!psThreadJobAddPending(job)) {
     170                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     171                psFree(AnalysisRegion);
     172                return false;
     173            }
     174        }
     175
     176        // wait for the threads to finish and manage results
     177        if (!psThreadPoolWait (false)) {
     178            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     179            psFree(AnalysisRegion);
     180            return false;
     181        }
     182
     183        // we have only supplied one type of job, so we can assume the types here
     184        psThreadJob *job = NULL;
     185        while ((job = psThreadJobGetDone()) != NULL) {
     186            if (job->args->n < 1) {
     187                fprintf (stderr, "error with job\n");
     188            } else {
     189                psScalar *scalar = NULL;
     190                scalar = job->args->data[7];
     191                Next += scalar->data.S32;
     192                scalar = job->args->data[8];
     193                Nconvolve += scalar->data.S32;
     194                scalar = job->args->data[9];
     195                NconvolvePass += scalar->data.S32;
     196                scalar = job->args->data[10];
     197                Nplain += scalar->data.S32;
     198                scalar = job->args->data[11];
     199                NplainPass += scalar->data.S32;
     200            }
     201            psFree(job);
     202        }
     203    }
     204    psFree (cellGroups);
     205    psFree(AnalysisRegion);
     206
     207    psLogMsg ("psphot", PS_LOG_INFO, "extended source analysis: %f sec for %d objects\n", psTimerMark ("psphot.extended"), Next);
     208    psLogMsg ("psphot", PS_LOG_INFO, "  %d convolved models (%d passed)\n", Nconvolve, NconvolvePass);
     209    psLogMsg ("psphot", PS_LOG_INFO, "  %d plain models (%d passed)\n", Nplain, NplainPass);
     210    return true;
     211}
     212
     213// non-linear model fitting for extended sources
     214bool psphotExtendedSourceFits_Threaded (psThreadJob *job) {
    33215
    34216    bool status;
     
    39221    int NplainPass = 0;
    40222    bool savePics = false;
    41 
    42     // find the currently selected readout
    43     pmFPAfile *file = pmFPAfileSelectSingle(config->files, filerule, index); // File of interest
    44     psAssert (file, "missing file?");
    45 
    46     pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
    47     psAssert (readout, "missing readout?");
    48 
    49     pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
    50     psAssert (detections, "missing detections?");
    51 
    52     psArray *sources = detections->allSources;
    53     psAssert (sources, "missing sources?");
    54 
    55     if (!sources->n) {
    56         psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping source size");
    57         return true;
    58     }
    59 
    60     // user-defined masks to test for good/bad pixels (build from recipe list if not yet set)
    61     psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); // Mask value for bad pixels
    62     assert (maskVal);
    63 
    64     psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); // Mask value for bad pixels
    65     assert (markVal);
    66 
    67     // maskVal is used to test for rejected pixels, and must include markVal
    68     maskVal |= markVal;
    69 
    70     // select the collection of desired models
    71     psMetadata *models = psMetadataLookupMetadata (&status, recipe, "EXTENDED_SOURCE_MODELS");
    72     if (!status) {
    73         psWarning ("extended source model fits requested but model model is missing (EXTENDED_SOURCE_MODELS)\n");
    74         return true;
    75     }
    76     if (models->list->n == 0) {
    77         psWarning ("extended source model fits requested but no models are specified\n");
    78         return true;
    79     }
    80 
    81     // validate the model entries
    82     psMetadataIterator *iter = psMetadataIteratorAlloc (models, PS_LIST_HEAD, NULL);
    83     psMetadataItem *item = NULL;
    84     while ((item = psMetadataGetAndIncrement (iter)) != NULL) {
    85 
    86       if (item->type != PS_DATA_METADATA) {
    87         psAbort ("Invalid type for EXTENDED_SOURCE_MODEL entry %s, not a metadata folder", item->name);
    88         // XXX we could cull the bad entries or build a validated model folder
    89       }
    90 
    91       psMetadata *model = (psMetadata *) item->data.md;
    92 
    93       // check on the model type
    94       char *modelName = psMetadataLookupStr (&status, model, "MODEL");
    95       int modelType = pmModelClassGetType (modelName);
    96       if (modelType < 0) {
    97         psAbort ("Unknown model class for EXTENDED_SOURCE_MODEL entry %s: %s", item->name, modelName);
    98       }
    99       psMetadataAddS32 (model, PS_LIST_TAIL, "MODEL_TYPE", PS_META_REPLACE, "", modelType);
    100 
    101       // check on the SNLIM, set a float value
    102       char *SNword = psMetadataLookupStr (&status, model, "SNLIM");
    103       if (!status) {
    104         psAbort("SNLIM not defined for extended source model %s\n", item->name);
    105       }
    106       float SNlim = atof (SNword);
    107       psMetadataAddF32 (model, PS_LIST_TAIL, "SNLIM_VALUE", PS_META_REPLACE, "", SNlim);
    108 
    109       // check on the PSF-Convolution status
    110       char *convolvedWord = psMetadataLookupStr (&status, model, "PSF_CONVOLVED");
    111       if (!status || (strcasecmp (convolvedWord, "true") && strcasecmp (convolvedWord, "false"))) {
    112         psAbort ("PSF_CONVOLVED entry invalid or missing for EXTENDED_SOURCE_MODEL entry %s", item->name);
    113       }
    114       bool convolved = !strcasecmp (convolvedWord, "true");
    115       psMetadataAddBool (model, PS_LIST_TAIL, "PSF_CONVOLVED_VALUE", PS_META_REPLACE, "", convolved);
    116     }
    117     psFree (iter);
    118 
    119     // option to limit analysis to a specific region
    120     char *region = psMetadataLookupStr (&status, recipe, "ANALYSIS_REGION");
    121     psRegion AnalysisRegion = psRegionForImage (readout->image, psRegionFromString (region));
    122     if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined");
    123 
    124     // what fraction of the PSF is used? (radius in pixels : 2 -> 5x5 box)
    125     int psfSize = psMetadataLookupS32 (&status, recipe, "PCM_BOX_SIZE");
    126     assert (status);
    127 
    128     // source analysis is done in S/N order (brightest first)
    129     sources = psArraySort (sources, pmSourceSortBySN);
     223    float radius;
     224    psScalar *scalar = NULL;
     225
     226    // arguments: readout, sources, models, region, psfSize, maskVal, markVal
     227    pmReadout *readout      = job->args->data[0];
     228    psArray *sources        = job->args->data[1];
     229    psMetadata *models      = job->args->data[2];
     230    psRegion *region        = job->args->data[3];
     231    int psfSize             = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     232    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[5],PS_TYPE_IMAGE_MASK_DATA);
     233    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[6],PS_TYPE_IMAGE_MASK_DATA);
     234
     235    // Define source fitting parameters for extended source fits
     236    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
     237    fitOptions->mode          = PM_SOURCE_FIT_EXT;
     238    // XXX for now, use the defaults for the rest:
     239    // fitOptions->nIter         = fitIter;
     240    // fitOptions->tol           = fitTol;
     241    // fitOptions->poissonErrors = poisson;
     242    // fitOptions->weight        = PS_SQR(skySig);
    130243
    131244    // choose the sources of interest
     
    140253
    141254        // XXX this should use peak?
    142         if (source->peak->x < AnalysisRegion.x0) continue;
    143         if (source->peak->y < AnalysisRegion.y0) continue;
    144         if (source->peak->x > AnalysisRegion.x1) continue;
    145         if (source->peak->y > AnalysisRegion.y1) continue;
     255        if (source->peak->x < region->x0) continue;
     256        if (source->peak->y < region->y0) continue;
     257        if (source->peak->x > region->x1) continue;
     258        if (source->peak->y > region->y1) continue;
    146259
    147260        // if model is NULL, we don't have a starting guess
     
    155268        Next ++;
    156269
     270        // set the radius based on the footprint (also sets the mask pixels)
     271        if (!psphotSetRadiusFootprint(&radius, readout, source, markVal, 1.0)) {
     272            fprintf (stderr, "skipping (1) %f, %f\n", source->peak->xf, source->peak->yf);
     273            psFree (fitOptions)
     274            return false;
     275        }
     276
     277        // XXX note that this changes the source moments that are published...
     278        // recalculate the source moments using the larger extended-source moments radius
     279        // at this stage, skip Gaussian windowing, and do not clip pixels by S/N
     280        // this uses the footprint to judge both radius and aperture?
     281        // XXX save the psf-based moments for output
     282        if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) {
     283            fprintf (stderr, "skipping (2) %f, %f\n", source->peak->xf, source->peak->yf);
     284            // subtract the best fit from the object, leave local sky
     285            pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     286            // XXX raise an error of some kind
     287            continue;
     288        }
     289
    157290        // save the modelFlux here in case we need to subtract it (for failure)
    158291        psImage *modelFluxStart = psMemIncrRefCounter (source->modelFlux);
    159 
     292        if (!modelFluxStart) {
     293            pmSourceCacheModel (source, maskVal);
     294            modelFluxStart = psMemIncrRefCounter (source->modelFlux);
     295            if (!modelFluxStart) {
     296                fprintf (stderr, "skipping (3) %f, %f\n", source->peak->xf, source->peak->yf);
     297                // XXX raise an error of some kind?
     298                continue;
     299            }
     300        }
     301       
    160302        if (savePics) {
    161303          psphotSaveImage (NULL, readout->image, "image.xp.fits");
     
    183325          assert (status);
    184326
     327          // fprintf (stderr, "xfit: %f, %f : %f\n", source->peak->xf, source->peak->yf, source->peak->SN);
     328
    185329          // limit selection to some SN limit
    186330          assert (source->peak); // how can a source not have a peak?
     
    201345          pmModel *modelFit = NULL;
    202346          if (convolved) {
    203               modelFit = psphotPSFConvModel (readout, source, modelType, maskVal, markVal, psfSize);
     347              modelFit = psphotFitPCM (readout, source, fitOptions, modelType, maskVal, markVal, psfSize);
    204348              if (!modelFit) {
    205349                  psTrace ("psphot", 5, "failed to fit psf-conv model for object at %f, %f", source->moments->Mx, source->moments->My);
     
    215359              psFree (source->modelFlux);
    216360              source->modelFlux = NULL;
    217               modelFit = psphotFitEXT (readout, source, modelType, maskVal, markVal);
     361              modelFit = psphotFitEXT (readout, source, fitOptions, modelType, maskVal, markVal);
    218362              if (!modelFit) {
    219363                  psTrace ("psphot", 5, "failed to fit plain model for object at %f, %f", source->moments->Mx, source->moments->My);
     
    233377
    234378          // test for fit quality / result
     379          modelFit->fitRadius = radius;
    235380          psArrayAdd (source->modelFits, 4, modelFit);
    236381
     
    311456        }
    312457    }
    313 
    314     psLogMsg ("psphot", PS_LOG_INFO, "extended source analysis: %f sec for %d objects\n", psTimerMark ("psphot"), Next);
    315     psLogMsg ("psphot", PS_LOG_INFO, "  %d convolved models (%d passed)\n", Nconvolve, NconvolvePass);
    316     psLogMsg ("psphot", PS_LOG_INFO, "  %d plain models (%d passed)\n", Nplain, NplainPass);
     458    psFree (fitOptions);
     459
     460    // fprintf (stderr, "xfit : tried %ld objects\n", sources->n);
     461
     462    // change the value of a scalar on the array (wrap this and put it in psArray.h)
     463    scalar = job->args->data[7];
     464    scalar->data.S32 = Next;
     465
     466    scalar = job->args->data[8];
     467    scalar->data.S32 = Nconvolve;
     468
     469    scalar = job->args->data[9];
     470    scalar->data.S32 = NconvolvePass;
     471
     472    scalar = job->args->data[10];
     473    scalar->data.S32 = Nplain;
     474
     475    scalar = job->args->data[11];
     476    scalar->data.S32 = NplainPass;
     477
    317478    return true;
    318479}
  • branches/sc_branches/trunkTest/psphot/src/psphotFindDetections.c

    r28013 r29060  
    9090    psphotVisualShowSignificance (significance, -1.0, PS_SQR(3.0*NSIGMA_PEAK));
    9191
    92     // XXX getting some strange results from significance image
    93     if (0) {
    94         psImage *lsig = (psImage *) psUnaryOp (NULL, significance, "log");
    95         psphotVisualShowSignificance (lsig, 0.0, 4.0);
    96         psFree (lsig);
    97     }   
     92    // display the log significance image
     93    psphotVisualShowLogSignificance (significance, 0.0, 4.5);
    9894
    9995    // detect the peaks in the significance image
  • branches/sc_branches/trunkTest/psphot/src/psphotFitSet.c

    r21183 r29060  
    2424    }
    2525
     26    // Define source fitting parameters for extended source fits
     27    pmSourceFitOptions *fitOptions = pmSourceFitOptionsAlloc();
     28    fitOptions->mode          = PM_SOURCE_FIT_EXT;
     29    // XXX for now, use the defaults for the rest:
     30    // fitOptions->nIter         = fitIter;
     31    // fitOptions->tol           = fitTol;
     32    // fitOptions->poissonErrors = poisson;
     33    // fitOptions->weight        = PS_SQR(skySig);
     34
    2635    // XXX pmSourceFitSet must cache the modelFlux?
    27     pmSourceFitSet (source, modelSet, mode, maskVal);
     36    pmSourceFitSet (source, modelSet, fitOptions, maskVal);
    2837
    2938    // write out positive object
  • branches/sc_branches/trunkTest/psphot/src/psphotFitSourcesLinear.c

    r28426 r29060  
    100100    if (!status) {
    101101        SKY_FIT_LINEAR = false;
     102    }
     103   
     104    float MIN_VALID_FLUX = psMetadataLookupF32(&status, recipe, "PSF_FIT_MIN_VALID_FLUX");
     105    if (!status) {
     106        MIN_VALID_FLUX = 1e-8;
     107    }
     108    float MAX_VALID_FLUX = psMetadataLookupF32(&status, recipe, "PSF_FIT_MAX_VALID_FLUX");
     109    if (!status) {
     110        MAX_VALID_FLUX = 1e+8;
    102111    }
    103112
     
    228237
    229238    psSparseConstraint constraint;
    230     constraint.paramMin   = 0.0;
    231     constraint.paramMax   = 1e8;
    232     constraint.paramDelta = 1e8;
     239    constraint.paramMin   = MIN_VALID_FLUX;
     240    constraint.paramMax   = MAX_VALID_FLUX;
     241    constraint.paramDelta = 1e7;
    233242
    234243    // solve for normalization terms (need include local sky?)
  • branches/sc_branches/trunkTest/psphot/src/psphotFitSourcesLinearStack.c

    r28426 r29060  
    170170    return true;
    171171}
    172 
    173 // sort by X (ascending)
    174 int pmPhotObjSortByX (const void **a, const void **b)
    175 {
    176     pmPhotObj *objA = *(pmPhotObj **)a;
    177     pmPhotObj *objB = *(pmPhotObj **)b;
    178 
    179     psF32 fA = objA->x;
    180     psF32 fB = objB->x;
    181 
    182     psF32 diff = fA - fB;
    183     if (diff > FLT_EPSILON) return (+1);
    184     if (diff < FLT_EPSILON) return (-1);
    185     return (0);
    186 }
  • branches/sc_branches/trunkTest/psphot/src/psphotGuessModels.c

    r28405 r29060  
    8080
    8181    // setup the PSF fit radius details
    82     psphotInitRadiusPSF (recipe, readout->analysis, psf->type);
     82    psphotInitRadiusPSF (recipe, readout);
    8383
    8484    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
  • branches/sc_branches/trunkTest/psphot/src/psphotLoadSRCTEXT.c

    r25983 r29060  
    8484            source->peak->yf   = PAR[PM_PAR_YPOS]; // but we know the pixel coordinate
    8585
    86             source->pixWeight = 1.0;
     86            source->pixWeightNotBad = 1.0;
     87            source->pixWeightNotPoor = 1.0;
    8788            source->crNsigma  = 0.0;
    8889            source->extNsigma = 0.0;
  • branches/sc_branches/trunkTest/psphot/src/psphotMagnitudes.c

    r28405 r29060  
    124124                return false;
    125125            }
    126 
    127 # if (0)
    128                 int nap = 0;
    129                 if (!psphotMagnitudes_Unthreaded (&nap, cells->data[j], psf, binning, backModel, backStdev, photMode, maskVal)) {
    130                     psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    131                     return false;
    132                 }
    133                 Nap += nap;
    134 # endif
    135126        }
    136127
     
    186177        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
    187178
    188         status = pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     179        status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    189180        if (status && isfinite(source->apMag)) Nap ++;
    190181
     
    268259            psArrayAdd(job->args, 1, cells->data[j]); // sources
    269260            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
     261            PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
    270262
    271263            if (!psThreadJobAddPending(job)) {
     
    304296    psArray *sources                = job->args->data[0];
    305297    psImageMaskType maskVal         = PS_SCALAR_VALUE(job->args->data[1],PS_TYPE_IMAGE_MASK_DATA);
     298    psImageMaskType markVal         = PS_SCALAR_VALUE(job->args->data[2],PS_TYPE_IMAGE_MASK_DATA);
    306299
    307300    for (int i = 0; i < sources->n; i++) {
     
    312305        if (model == NULL) {
    313306          psTrace ("psphot", 3, "fail mag : no valid model");
    314           source->pixWeight = NAN;
     307          source->pixWeightNotBad = NAN;
     308          source->pixWeightNotPoor = NAN;
    315309          continue;
    316310        }
    317311
    318         status = pmSourcePixelWeight (&source->pixWeight, model, source->maskObj, maskVal);
     312        status = pmSourcePixelWeight (&source->pixWeightNotBad, &source->pixWeightNotPoor, model, source->maskObj, maskVal, markVal);
    319313        if (!status) {
    320314          psTrace ("psphot", 3, "fail to measure pixel weight");
    321           source->pixWeight = NAN;
     315          source->pixWeightNotBad = NAN;
     316          source->pixWeightNotPoor = NAN;
    322317          continue;
    323318        }
  • branches/sc_branches/trunkTest/psphot/src/psphotModelWithPSF.c

    r21366 r29060  
    7373
    7474    // iterate until the tolerance is reached, or give up
    75     while ((min->iter < min->maxIter) && ((min->lastDelta > min->tol) || !isfinite(min->lastDelta))) {
     75    while ((min->iter < min->maxIter) && ((min->lastDelta > min->minTol) || !isfinite(min->lastDelta))) {
    7676        psTrace("psphot", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
    77         psTrace("psphot", 5, "Last delta is %f.  Min->tol is %f.\n", min->lastDelta, min->tol);
     77        psTrace("psphot", 5, "Last delta is %f.  Min->minTol is %f.\n", min->lastDelta, min->minTol);
    7878
    7979
     
    166166    psFree(pcm);
    167167
    168     if (min->iter == min->maxIter) {
    169         psTrace("psphot", 3, "---- end (false) ----\n");
    170         return(false);
    171     }
    172 
    173     psTrace("psphot", 3, "---- end (true) ----\n");
    174     return(true);
     168    // if the last improvement was at least as good as maxTol, accept the fit:
     169    if (min->lastDelta <= min->maxTol) {
     170        psTrace("psphot", 6, "---- end (true) ----\n");
     171        return(true);
     172    }
     173    psTrace("psphot", 6, "---- end (false) ----\n");
     174    return(false);
    175175}
    176176
  • branches/sc_branches/trunkTest/psphot/src/psphotPSFConvModel.c

    r26894 r29060  
    44// save as static values so they may be set externally
    55static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
    6 static psF32 PM_SOURCE_FIT_MODEL_TOLERANCE = 0.1;
     6static psF32 PM_SOURCE_FIT_MODEL_MIN_TOL = 0.1;
     7static psF32 PM_SOURCE_FIT_MODEL_MAX_TOL = 2.0;
    78
    89// input source has both modelPSF and modelEXT.  on successful exit, we set the
    910// modelConv to contain the fitted parameters, and the modelFlux to contain the
    1011// convolved model image.
    11 pmModel *psphotPSFConvModel (pmReadout *readout, pmSource *source, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     12
     13// XXX need to generalize this -- number of fitted parameters must be flexible based on the fitOptions
     14
     15pmModel *psphotPSFConvModel (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
    1216   
    1317    // maskVal is used to test for rejected pixels, and must include markVal
     
    9094
    9195    // set up the minimization process
    92     psMinimization *myMin = psMinimizationAlloc (PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_TOLERANCE);
     96    psMinimization *myMin = psMinimizationAlloc (PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_MIN_TOL, PM_SOURCE_FIT_MODEL_MAX_TOL);
    9397
    9498    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
  • branches/sc_branches/trunkTest/psphot/src/psphotPetrosianVisual.c

    r27819 r29060  
    11# include "psphotInternal.h"
    2 # define FORCE_VISUAL 0
    32
    43// this function displays representative images as the psphot analysis progresses:
     
    5453    Graphdata graphdata;
    5554
    56     // return true;
    57     if (!FORCE_VISUAL && !pmVisualIsVisual()) return true;
     55    if (!pmVisualTestLevel("psphot.petro.byangle", 2)) return true;
    5856
    5957    if (kapa2 == -1) {
     
    10199    Graphdata graphdata;
    102100
    103     // return true;
    104     if (!FORCE_VISUAL && !pmVisualIsVisual()) return true;
     101    if (!pmVisualTestLevel("psphot.petro.radii", 2)) return true;
    105102
    106103    if (kapa == -1) {
     
    173170    KapaSection section;
    174171
    175     if (!FORCE_VISUAL && !pmVisualIsVisual()) return true;
     172    if (!pmVisualTestLevel("psphot.petro.stats", 2)) return true;
    176173
    177174    if (kapa2 == -1) {
     
    311308    Graphdata graphdata;
    312309
    313     if (!FORCE_VISUAL && !pmVisualIsVisual()) return true;
     310    if (!pmVisualTestLevel("psphot.petro.ellipse", 2)) return true;
    314311
    315312    if (kapa == -1) {
  • branches/sc_branches/trunkTest/psphot/src/psphotRadiusChecks.c

    r28418 r29060  
    88                                        // and a per-object radius is calculated)
    99
    10 bool psphotInitRadiusPSF(const psMetadata *recipe, const psMetadata *analysis, const pmModelType type) {
     10bool psphotInitRadiusPSF(psMetadata *recipe, pmReadout *readout) {
    1111
    1212    bool status = true;
     
    1515    PSF_FIT_PADDING = psMetadataLookupF32(&status, recipe, "PSF_FIT_PADDING");
    1616
    17     PSF_FIT_RADIUS =  psMetadataLookupF32(&status, analysis, "PSF_FIT_RADIUS");
     17    PSF_FIT_RADIUS =  psMetadataLookupF32(&status, readout->analysis, "PSF_FIT_RADIUS");
    1818    if (!status) {
    1919        PSF_FIT_RADIUS = psMetadataLookupF32(&status, recipe, "PSF_FIT_RADIUS");
    2020    }
    2121
    22     PSF_APERTURE =  psMetadataLookupF32(&status, analysis, "PSF_APERTURE");
     22    PSF_APERTURE =  psMetadataLookupF32(&status, readout->analysis, "PSF_APERTURE");
    2323    if (!status) {
    2424        PSF_APERTURE =  psMetadataLookupF32(&status, recipe, "PSF_APERTURE");
     
    2828
    2929    if (PSF_FIT_RADIUS == 0.0) {
    30         float gaussSigma = psMetadataLookupF32(&status, analysis, "MOMENTS_GAUSS_SIGMA");
     30        float gaussSigma = psMetadataLookupF32(&status, readout->analysis, "MOMENTS_GAUSS_SIGMA");
    3131        if (!status) {
    3232            gaussSigma = psMetadataLookupF32(&status, recipe, "MOMENTS_GAUSS_SIGMA");
     
    3737
    3838    if (PSF_APERTURE == 0.0) {
    39         float gaussSigma = psMetadataLookupF32(&status, analysis, "MOMENTS_GAUSS_SIGMA");
     39        float gaussSigma = psMetadataLookupF32(&status, readout->analysis, "MOMENTS_GAUSS_SIGMA");
    4040        if (!status) {
    4141            gaussSigma = psMetadataLookupF32(&status, recipe, "MOMENTS_GAUSS_SIGMA");
     
    122122}
    123123
     124static float EXT_FIT_SKY_SIG;
    124125static float EXT_FIT_NSIGMA;
    125126static float EXT_FIT_PADDING;
    126127static float EXT_FIT_MAX_RADIUS;
    127128
    128 bool psphotInitRadiusEXT (psMetadata *recipe, pmModelType type) {
     129bool psphotInitRadiusEXT (psMetadata *recipe, pmReadout *readout) {
    129130
    130131    bool status;
     
    134135    EXT_FIT_MAX_RADIUS = psMetadataLookupF32 (&status, recipe, "EXT_FIT_MAX_RADIUS");
    135136
     137    float skyMean  = psMetadataLookupF32 (&status, readout->analysis, "SKY_MEAN");
     138    float skyStdev = psMetadataLookupF32 (&status, readout->analysis, "SKY_STDEV");
     139
     140    fprintf (stderr, "sky: %f +/- %f\n", skyMean, skyStdev);
     141
     142    EXT_FIT_SKY_SIG = skyStdev;
     143
    136144    return true;
    137145}
    138146
    139147// call this function whenever you (re)-define the EXT model
    140 float psphotSetRadiusEXT (pmReadout *readout, pmSource *source, psImageMaskType markVal) {
     148bool psphotSetRadiusFootprint (float *radius, pmReadout *readout, pmSource *source, psImageMaskType markVal, float factor) {
    141149
    142150    psAssert (source, "source not defined??");
     
    146154
    147155    // set the radius based on the footprint:
    148     if (!peak->footprint) goto escape;
     156    if (!peak->footprint) return false;
    149157    pmFootprint *footprint = peak->footprint;
    150     if (!footprint->spans) goto escape;
    151     if (footprint->spans->n < 1) goto escape;
     158    if (!footprint->spans) return false;
     159    if (footprint->spans->n < 1) return false;
    152160
    153161    // find the max radius
    154     float radius = 0.0;
     162    float rawRadius = 0.0;
    155163    for (int j = 0; j < footprint->spans->n; j++) {
    156164        pmSpan *span = footprint->spans->data[j];
     
    160168        float dX1 = span->x1 - peak->xf;
    161169
    162         radius = PS_MAX (radius, hypot(dY, dX0));
    163         radius = PS_MAX (radius, hypot(dY, dX1));
    164     }
    165 
    166     radius += EXT_FIT_PADDING;
    167     if (isnan(radius)) psAbort("error in radius");
    168 
    169     radius = PS_MIN (radius, EXT_FIT_MAX_RADIUS);
     170        rawRadius = PS_MAX (rawRadius, hypot(dY, dX0));
     171        rawRadius = PS_MAX (rawRadius, hypot(dY, dX1));
     172    }
     173    if (isnan(rawRadius)) return false;
     174    rawRadius = PS_MIN (factor*rawRadius + EXT_FIT_PADDING, EXT_FIT_MAX_RADIUS);
    170175
    171176    // redefine the pixels if needed
    172     pmSourceRedefinePixels (source, readout, peak->xf, peak->yf, radius);
    173 
    174     // set the mask to flag the excluded pixels
    175     psImageKeepCircle (source->maskObj, peak->xf, peak->yf, radius, "OR", markVal);
    176     return radius;
    177 
    178 escape:
    179     return NAN;
    180     // bool result = psphotCheckRadiusEXT (readout, source, model, markVal);
    181     // return result;
     177    pmSourceRedefinePixels (source, readout, peak->xf, peak->yf, rawRadius);
     178
     179    // set the mask to flag the excluded pixels
     180    psImageKeepCircle (source->maskObj, peak->xf, peak->yf, rawRadius, "OR", markVal);
     181
     182    *radius = rawRadius;
     183    return true;
    182184}
    183185
    184186// alternative EXT radius based on model guess (for use without footprints)
    185 bool psphotCheckRadiusEXT (pmReadout *readout, pmSource *source, pmModel *model, psImageMaskType markVal) {
    186 
    187     psAbort ("do not use this function");
     187bool psphotSetRadiusModel (pmModel *model, pmReadout *readout, pmSource *source, psImageMaskType markVal, bool deep) {
    188188
    189189    psF32 *PAR = model->params->data.F32;
     
    193193
    194194    // set the fit radius based on the object flux limit and the model
    195     float rawRadius = model->modelRadius (model->params, EXT_FIT_NSIGMA*moments->dSky);
    196 
    197     model->fitRadius = rawRadius + EXT_FIT_PADDING;
    198     if (isnan(model->fitRadius)) psAbort("error in radius");
     195    float flux = deep ? EXT_FIT_NSIGMA*EXT_FIT_SKY_SIG : 0.1 * model->params->data.F32[PM_PAR_I0];
     196
     197    float rawRadius = model->modelRadius (model->params, flux);
     198    if (isnan(rawRadius)) return false;
     199
     200    rawRadius = PS_MIN (rawRadius + EXT_FIT_PADDING, EXT_FIT_MAX_RADIUS);
     201    model->fitRadius = rawRadius;
    199202
    200203    // redefine the pixels if needed
    201     bool status = pmSourceRedefinePixels (source, readout, PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], model->fitRadius);
     204    pmSourceRedefinePixels (source, readout, PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], model->fitRadius);
    202205
    203206    // set the mask to flag the excluded pixels
    204207    psImageKeepCircle (source->maskObj, PAR[PM_PAR_XPOS], PAR[PM_PAR_YPOS], model->fitRadius, "OR", markVal);
    205     return status;
    206 }
     208    return true;
     209}
  • branches/sc_branches/trunkTest/psphot/src/psphotReadout.c

    r28398 r29060  
    5353    }
    5454
    55     // load the psf model, if suppled.  FWHM_X,FWHM_Y,etc are determined and saved on
    56     // readout->analysis XXX this function currently only works with a single PSPHOT.INPUT
     55    // load the psf model, if suppled.  FWHM_MAJ,FWHM_MIN,etc are determined and saved on
     56    // readout->analysis. XXX Note: this function currently only works with a single
     57    // PSPHOT.INPUT
    5758    if (!psphotLoadPSF (config, view)) { // ??? need to supply 2 ?
    5859        psError (PSPHOT_ERR_UNKNOWN, false, "error loading psf model");
  • branches/sc_branches/trunkTest/psphot/src/psphotSetThreads.c

    r26894 r29060  
    1515    psFree(task);
    1616
    17     task = psThreadTaskAlloc("PSPHOT_PSF_WEIGHTS", 2);
     17    task = psThreadTaskAlloc("PSPHOT_PSF_WEIGHTS", 3);
    1818    task->function = &psphotPSFWeights_Threaded;
    1919    psThreadTaskAdd(task);
     
    3030    psFree(task);
    3131
    32     task = psThreadTaskAlloc("PSPHOT_BLEND_FIT", 9);
     32    task = psThreadTaskAlloc("PSPHOT_BLEND_FIT", 10);
    3333    task->function = &psphotBlendFit_Threaded;
     34    psThreadTaskAdd(task);
     35    psFree(task);
     36
     37    task = psThreadTaskAlloc("PSPHOT_EXTENDED_FIT", 12);
     38    task->function = &psphotExtendedSourceFits_Threaded;
    3439    psThreadTaskAdd(task);
    3540    psFree(task);
  • branches/sc_branches/trunkTest/psphot/src/psphotSignificanceImage.c

    r28667 r29060  
    9797                smooth_im->data.F32[j][i] = 0.0;
    9898            } else {
    99                 smooth_im->data.F32[j][i] = factor * PS_SQR(value) / smooth_wt->data.F32[j][i];
     99                float v2 = value + PS_SQR(value/1000.0);
     100                smooth_im->data.F32[j][i] = factor * PS_SQR(v2) / smooth_wt->data.F32[j][i];
    100101            }
    101102        }
  • branches/sc_branches/trunkTest/psphot/src/psphotSourceFits.c

    r26894 r29060  
    22
    33// given a source with an existing modelPSF, attempt a full PSF fit, subtract if successful
    4 // XXX this function does not call pmSourceFitModelInit : fix this?
    54
    65static int NfitPSF = 0;
     
    87static int NfitDBL = 0;
    98static int NfitEXT = 0;
     9static int NfitPCM = 0;
    1010
    1111bool psphotFitInit (int nThreads) {
     
    1818
    1919    psLogMsg ("psphot.pspsf", PS_LOG_INFO, "fitted %5d psf, %5d blend, %5d ext, %5d dbl : %6.2f sec\n",
    20              NfitPSF, NfitBlend, NfitEXT, NfitDBL, psTimerMark ("psphot.fits"));
    21     return true;
    22 }
    23 
    24 bool psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal) {
     20              NfitPSF, NfitBlend, NfitEXT, NfitDBL, psTimerMark ("psphot.fits"));
     21    return true;
     22}
     23
     24bool psphotFitBlend (pmReadout *readout, pmSource *source, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
    2525
    2626    float x, y, dR;
     27
     28    pmSourceFitOptions options = *fitOptions;
    2729
    2830    // maskVal is used to test for rejected pixels, and must include markVal
     
    3133    // if this source is not a possible blend, just fit as PSF
    3234    if ((source->blends == NULL) || (source->mode & PM_SOURCE_MODE_SATSTAR)) {
    33         bool status = psphotFitPSF (readout, source, psf, maskVal, markVal);
     35        bool status = psphotFitPSF (readout, source, psf, fitOptions, maskVal, markVal);
    3436        return status;
    3537    }
     
    9193
    9294    // fit PSF model
    93     pmSourceFitSet (source, modelSet, PM_SOURCE_FIT_PSF, maskVal);
     95    options.mode = PM_SOURCE_FIT_PSF;
     96    pmSourceFitSet (source, modelSet, &options, maskVal);
    9497
    9598    // clear the circular mask
     
    154157}
    155158
    156 bool psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal) {
     159bool psphotFitPSF (pmReadout *readout, pmSource *source, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
    157160
    158161    double chiTrend;
     162    pmSourceFitOptions options = *fitOptions;
    159163
    160164    // maskVal is used to test for rejected pixels, and must include markVal
     
    171175
    172176    // fit PSF model (set/unset the pixel mask)
    173     pmSourceFitModel (source, PSF, PM_SOURCE_FIT_PSF, maskVal);
     177    options.mode = PM_SOURCE_FIT_PSF;
     178    pmSourceFitModel (source, PSF, &options, maskVal);
    174179
    175180    if (!isfinite(PSF->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
     
    199204}
    200205
     206// save a local, static copy of the EXT model type so we don't have to lookup for each object
    201207static pmModelType modelTypeEXT;
    202208
    203 bool psphotInitLimitsEXT (psMetadata *recipe) {
     209bool psphotInitLimitsEXT (psMetadata *recipe, pmReadout *readout) {
    204210
    205211    bool status;
     
    208214    char *modelNameEXT = psMetadataLookupStr (&status, recipe, "EXT_MODEL");
    209215    modelTypeEXT = pmModelClassGetType (modelNameEXT);
    210     psphotInitRadiusEXT (recipe, modelTypeEXT);
    211 
    212     return true;
    213 }
    214 
    215 bool psphotFitBlob (pmReadout *readout, pmSource *source, psArray *newSources, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal) {
    216 
     216
     217    psphotInitRadiusEXT (recipe, readout);
     218
     219    return true;
     220}
     221
     222bool psphotFitBlob (pmReadout *readout, pmSource *source, psArray *newSources, pmPSF *psf, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
     223
     224    float radius;
    217225    bool okEXT, okDBL;
    218226    float chiEXT, chiDBL;
     
    222230    pmModel *EXT = NULL;
    223231    psArray *DBL = NULL;
     232    pmMoments psfMoments;
    224233
    225234    // skip the source if we don't think it is extended
     235    // XXX are these robust, or do we have better info from the source-size analysis??
    226236    if (source->type == PM_SOURCE_TYPE_UNKNOWN) return false;
    227237    if (source->type == PM_SOURCE_TYPE_DEFECT) return false;
     
    229239
    230240    // set the radius based on the footprint (also sets the mask pixels)
    231     float radius = psphotSetRadiusEXT (readout, source, markVal);
     241    if (!psphotSetRadiusFootprint(&radius, readout, source, markVal, 1.0)) return false;
    232242
    233243    // XXX note that this changes the source moments that are published...
     244    // XXX all published moments should use the same measurement
    234245    // recalculate the source moments using the larger extended-source moments radius
    235246    // at this stage, skip Gaussian windowing, and do not clip pixels by S/N
    236247    // this uses the footprint to judge both radius and aperture?
    237     if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) return false;
     248    // XXX save the psf-based moments for output
     249    psfMoments = *source->moments;
     250    if (!pmSourceMoments (source, radius, 0.0, 0.5, maskVal)) {
     251      *source->moments = psfMoments;
     252      return false;
     253    }
    238254
    239255    psTrace ("psphot", 5, "trying blob...\n");
     
    244260    // this temporary source is used as a place-holder by the psphotEval functions below
    245261    tmpSrc = pmSourceAlloc ();
    246 
    247     // XXX need to handle failures better here
    248     EXT = psphotFitEXT (readout, source, modelTypeEXT, maskVal, markVal);
    249     if (!EXT) goto escape;
    250     if (!isfinite(EXT->params->data.F32[PM_PAR_I0])) goto escape;
    251 
    252     okEXT = psphotEvalEXT (tmpSrc, EXT);
    253     chiEXT = EXT ? EXT->chisq / EXT->nDOF : NAN;
    254 
    255     // DBL will always be defined, but DBL->data[n] might not
    256     DBL = psphotFitDBL (readout, source, maskVal, markVal);
    257     if (!DBL) goto escape;
    258     if (!DBL->n) goto escape;
    259 
    260     okDBL  = psphotEvalDBL (tmpSrc, DBL->data[0]);
    261     okDBL &= psphotEvalDBL (tmpSrc, DBL->data[1]);
    262     // XXX should I keep / save the flags set in the eval functions?
     262    {
     263        // DBL will always be defined, but DBL->data[n] might not
     264        DBL = psphotFitDBL (readout, source, fitOptions, maskVal, markVal);
     265        if (!DBL) goto escape;
     266        if (!DBL->n) goto escape;
     267
     268        okDBL  = psphotEvalDBL (tmpSrc, DBL->data[0]);
     269        okDBL &= psphotEvalDBL (tmpSrc, DBL->data[1]);
     270        // XXX should I keep / save the flags set in the eval functions?
     271
     272        // correct first model chisqs for flux trend
     273        chiDBL = NAN;
     274        ONE = DBL->data[0];
     275        if (ONE) {
     276            if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
     277            chiTrend = psPolynomial1DEval (psf->ChiTrend, ONE->params->data.F32[1]);
     278            ONE->chisqNorm = ONE->chisq / chiTrend;
     279            chiDBL = ONE->chisq / ONE->nDOF; // save chisq for double-star/galaxy comparison
     280            ONE->fitRadius = radius;
     281        }
     282
     283        // correct second model chisqs for flux trend
     284        ONE = DBL->data[1];
     285        if (ONE) {
     286            if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
     287            chiTrend = psPolynomial1DEval (psf->ChiTrend, ONE->params->data.F32[1]);
     288            ONE->chisqNorm = ONE->chisq / chiTrend;
     289            ONE->fitRadius = radius;
     290        }
     291    }
     292
     293    {
     294        // XXX need to handle failures better here
     295        EXT = psphotFitEXT (readout, source, fitOptions, modelTypeEXT, maskVal, markVal);
     296        if (!EXT) goto escape;
     297        if (!isfinite(EXT->params->data.F32[PM_PAR_I0])) goto escape;
     298
     299        okEXT = psphotEvalEXT (tmpSrc, EXT);
     300        chiEXT = EXT ? EXT->chisq / EXT->nDOF : NAN;
     301    }
    263302
    264303    // clear the circular mask
    265304    psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
    266 
    267     // correct first model chisqs for flux trend
    268     chiDBL = NAN;
    269     ONE = DBL->data[0];
    270     if (ONE) {
    271         if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
    272       chiTrend = psPolynomial1DEval (psf->ChiTrend, ONE->params->data.F32[1]);
    273       ONE->chisqNorm = ONE->chisq / chiTrend;
    274       chiDBL = ONE->chisq / ONE->nDOF; // save chisq for double-star/galaxy comparison
    275     }
    276 
    277     // correct second model chisqs for flux trend
    278     ONE = DBL->data[1];
    279     if (ONE) {
    280         if (!isfinite(ONE->params->data.F32[PM_PAR_I0])) psAbort("nan in fit");
    281       chiTrend = psPolynomial1DEval (psf->ChiTrend, ONE->params->data.F32[1]);
    282       ONE->chisqNorm = ONE->chisq / chiTrend;
    283     }
    284305
    285306    psFree (tmpSrc);
     
    298319
    299320    // both models failed; reject them both
    300     // XXX -- change type flags to psf in this case and keep original moments?
     321    // XXX -- change type flags to psf in this case, and make sure we subtract it?
     322    // reset the psf moments
     323    *source->moments = psfMoments;
     324
    301325    psFree (EXT);
    302326    psFree (DBL);
     
    308332
    309333    // save new model
     334    // XXX save the correct radius...
    310335    source->modelEXT = EXT;
    311     source->modelEXT->fitRadius = radius;
    312336    source->type = PM_SOURCE_TYPE_EXTENDED;
    313337    source->mode |= PM_SOURCE_MODE_EXTMODEL;
     
    327351# endif
    328352
     353    // reset the psf moments
     354    *source->moments = psfMoments;
    329355    return true;
    330356
     
    337363    source->modelPSF = psMemIncrRefCounter (DBL->data[0]);
    338364    source->mode     |= PM_SOURCE_MODE_PAIR;
    339     source->modelPSF->fitRadius = radius;
    340365
    341366    // copy most data from the primary source (modelEXT, blends stay NULL)
    342     pmSource *newSrc = pmSourceCopy (source);
     367    pmSource *newSrc = pmSourceCopyData (source);
    343368    newSrc->modelPSF = psMemIncrRefCounter (DBL->data[1]);
    344     newSrc->modelPSF->fitRadius = radius;
    345369
    346370    // build cached models and subtract
     
    365389# endif
    366390
     391    // reset the (original) psf moments
     392    *source->moments = psfMoments;
     393    *newSrc->moments = psfMoments;
     394
    367395    psArrayAdd (newSources, 100, newSrc);
    368396    psFree (newSrc);
     
    371399
    372400escape:
     401    // reset the psf moments
     402    *source->moments = psfMoments;
    373403    psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
    374404    psFree (tmpSrc);
     
    379409
    380410// fit a double PSF source to an extended blob
    381 psArray *psphotFitDBL (pmReadout *readout, pmSource *source, psImageMaskType maskVal, psImageMaskType markVal) {
     411psArray *psphotFitDBL (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
    382412
    383413    float dx, dy;
     
    387417    psEllipseMoments moments;
    388418    psArray *modelSet;
     419
     420    pmSourceFitOptions options = *fitOptions;
    389421
    390422    NfitDBL ++;
     
    403435    axes = psEllipseMomentsToAxes (moments, 20.0);
    404436
     437    if (isnan(axes.major)) return NULL;
     438    if (isnan(axes.minor)) return NULL;
     439    if (isnan(axes.theta)) return NULL;
     440
    405441    // XXX this is really arbitrary: 4 pixel separation?
    406442    dx = 2 * cos (axes.theta);
     
    426462
    427463    // fit PSF model (set/unset the pixel mask)
    428     pmSourceFitSet (source, modelSet, PM_SOURCE_FIT_PSF, maskVal);
     464    options.mode = PM_SOURCE_FIT_PSF;
     465    pmSourceFitSet (source, modelSet, &options, maskVal);
    429466    return (modelSet);
    430467}
    431468
    432 pmModel *psphotFitEXT (pmReadout *readout, pmSource *source, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
     469pmModel *psphotFitEXT (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
     470
     471    if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
     472        psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
     473    }
     474
     475    pmSourceFitOptions options = *fitOptions;
    433476
    434477    NfitEXT ++;
     
    438481
    439482    // use the source moments, etc to guess basic model parameters
    440     pmModel *EXT = pmSourceModelGuess (source, modelType);
    441     if (!EXT) {
     483    pmModel *model = pmSourceModelGuess (source, modelType);
     484    if (!model) {
    442485        psTrace ("psphot", 5, "failed to generate a model for source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
    443486        return NULL;
    444487    }
    445488
     489    // for sersic models, use a grid search to choose an index, then float the params there
     490    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     491        // for the test fits, use a somewhat smaller radius
     492        psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
     493        psphotFitSersicIndex (model, readout, source, fitOptions, maskVal, markVal);
     494    }
     495
     496    if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
     497        psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 1.0);
     498    }
     499
     500    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     501        options.mode = PM_SOURCE_FIT_NO_INDEX;
     502    } else {
     503        options.mode = PM_SOURCE_FIT_EXT;
     504    }
     505
     506    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
     507    pmSourceFitModel (source, model, &options, maskVal);
     508    // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     509
     510    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
     511    return (model);
     512}
     513
     514pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     515
    446516    if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
    447517        psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
    448518    }
    449519
     520    pmSourceFitOptions options = *fitOptions;
     521
     522    NfitPCM ++;
     523
     524    // maskVal is used to test for rejected pixels, and must include markVal
     525    maskVal |= markVal;
     526
     527    // allocate the model
     528    pmModel *model = pmModelAlloc(modelType);
     529    if (!model) {
     530        return NULL;
     531    }
     532
     533    pmPCMdata *pcm = pmPCMinit (source, &options, model, maskVal, psfSize);
     534    if (!pcm) {
     535        psTrace ("psphot", 5, "failed to generate a model for source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
     536        model->flags |= PM_MODEL_STATUS_BADARGS; // XXX this is probably already set in pmPCMinit
     537        return model;
     538    }
     539
     540    // use the source moments, etc to guess basic model parameters
     541    if (!pmSourceModelGuessPCM (pcm, source, maskVal, markVal)) {
     542        psFree (pcm);
     543        model->flags |= PM_MODEL_STATUS_BADARGS;
     544        return model;
     545    }
     546
     547    // for sersic models, use a grid search to choose an index, then float the params there
     548    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     549        // for the test fits, use a somewhat smaller radius
     550        psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
     551
     552        if (!psphotFitSersicIndexPCM (pcm, readout, source, fitOptions, maskVal, markVal, psfSize)) {
     553            model->flags |= PM_MODEL_STATUS_BADARGS;
     554            return model;
     555        }
     556    }
     557
     558    if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
     559        psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 1.0);
     560    }
     561
     562    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     563        options.mode = PM_SOURCE_FIT_NO_INDEX;
     564    } else {
     565        options.mode = PM_SOURCE_FIT_EXT;
     566    }
     567    // update the pcm elements if we have changed the circumstance (options.mode or source->pixels)
     568    pmPCMupdate(pcm, source, &options, model);
     569
     570    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
     571    pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
     572    // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     573
     574    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
     575    psFree (pcm);
     576
     577    return model;
     578}
     579
     580// note that these should be 1/2n of the standard sersic index
     581float indexGuess[] = {0.5, 0.33, 0.25, 0.167, 0.125, 0.083};
     582# define N_INDEX_GUESS 6
     583
     584// A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
     585// for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
     586bool psphotFitSersicIndex (pmModel *model, pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
     587
     588    assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
     589
     590    pmSourceFitOptions options = *fitOptions;
     591   
    450592    // fit EXT (not PSF) model (set/unset the pixel mask)
    451     pmSourceFitModel (source, EXT, PM_SOURCE_FIT_EXT, maskVal);
    452     return (EXT);
    453 }
     593    options.mode = PM_SOURCE_FIT_NO_INDEX;
     594    options.nIter = 4;
     595
     596    int iMin = -1;
     597    float xMin = NAN;
     598    float chiSquare[N_INDEX_GUESS];
     599
     600    for (int i = 0; i < N_INDEX_GUESS; i++) {
     601        model->params->data.F32[PM_PAR_7] = indexGuess[i];
     602
     603        if (!model->modelGuess(model, source)) {
     604            model->flags |= PM_MODEL_STATUS_BADARGS;
     605            return false;
     606        }
     607
     608        // each time we change the model guess, we need to adjust the radius
     609        // XXX this did not work : we do not need such a large radius -- just uses moments-based radius
     610        if (false && !psphotSetRadiusModel (model, readout, source, markVal, false)) {
     611            psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
     612        }
     613       
     614        pmSourceFitModel (source, model, &options, maskVal);
     615        // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     616
     617        chiSquare[i] = model->chisqNorm;
     618        if (i == 0) {
     619            xMin = chiSquare[i];
     620            iMin = i;
     621        } else {
     622            if (chiSquare[i] < xMin) {
     623                xMin = chiSquare[i];
     624                iMin = i;
     625            }
     626        }
     627    }
     628    assert (iMin >= 0);
     629
     630    model->flags = PM_MODEL_STATUS_NONE; // do not attempt to handle failures here, let the next iteration deal with it
     631    model->params->data.F32[PM_PAR_7] = indexGuess[iMin];
     632    model->modelGuess(model, source);
     633
     634    // each time we change the model guess, we need to adjust the radius
     635    // if (!psphotSetRadiusModel (model, readout, source, markVal, true)) {
     636    //  psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal);
     637    // }
     638
     639    return true;
     640}
     641
     642// A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
     643// for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
     644bool psphotFitSersicIndexPCM (pmPCMdata *pcm, pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     645
     646    pmModel *model = pcm->modelConv;
     647
     648    assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
     649
     650    pmSourceFitOptions options = *fitOptions;
     651   
     652    // fit EXT (not PSF) model (set/unset the pixel mask)
     653    options.mode = PM_SOURCE_FIT_NO_INDEX;
     654    options.nIter = 4;
     655
     656    // update the pcm elements if we have changed the circumstance (here, options.mode)
     657    pmPCMupdate(pcm, source, &options, model);
     658
     659    int iMin = -1;
     660    float xMin = NAN;
     661    float chiSquare[N_INDEX_GUESS];
     662
     663    for (int i = 0; i < N_INDEX_GUESS; i++) {
     664        model->params->data.F32[PM_PAR_7] = indexGuess[i];
     665       
     666        if (!model->modelGuess(model, source)) {
     667            model->flags |= PM_MODEL_STATUS_BADARGS;
     668            return false;
     669        }
     670
     671        // each time we change the model guess, we need to adjust the radius
     672        // XXX this did not work : we do not need such a large radius -- just uses moments-based radius
     673        if (false && !psphotSetRadiusModel (model, readout, source, markVal, false)) {
     674            psphotSetRadiusFootprint(&model->fitRadius, readout, source, markVal, 0.5);
     675        }
     676       
     677# if (0)
     678        pmSourceFitModel (source, model, &options, maskVal);
     679# else
     680        pmSourceModelGuessPCM(pcm, source, maskVal, markVal);
     681        pmSourceFitPCM (pcm, source, &options, maskVal, markVal, psfSize);
     682# endif
     683        // fprintf (stderr, "chisq: %f, nIter: %d, radius: %f, npix: %d\n", model->chisqNorm, model->nIter, model->fitRadius, model->nPix);
     684
     685        chiSquare[i] = model->chisq;
     686        if (i == 0) {
     687            xMin = chiSquare[i];
     688            iMin = i;
     689        } else {
     690            if (chiSquare[i] < xMin) {
     691                xMin = chiSquare[i];
     692                iMin = i;
     693            }
     694        }
     695    }
     696    assert (iMin >= 0);
     697   
     698    model->flags = PM_MODEL_STATUS_NONE; // do not attempt to handle failures here, let the next iteration deal with it
     699    model->params->data.F32[PM_PAR_7] = indexGuess[iMin];
     700
     701    pmSourceModelGuessPCM(pcm, source, maskVal, markVal);
     702
     703    return true;
     704}
  • branches/sc_branches/trunkTest/psphot/src/psphotSourceSize.c

    r28013 r29060  
    11# include "psphotInternal.h"
    22# include <gsl/gsl_sf_gamma.h>
     3
     4# define KRON 1
    35
    46typedef struct {
     
    168170    psVector *ApErr = psVectorAllocEmpty (100, PS_TYPE_F32);
    169171
     172    psImageMaskType markVal = options->markVal;
    170173    psImageMaskType maskVal = options->maskVal | options->markVal;
    171174
     
    189192
    190193        // XXX can we test if psfMag is set and calculate only if needed?
    191         pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     194        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    192195
    193196        // clear the mask bit
     
    197200        pmSourceSub (source, PM_MODEL_OP_FULL, options->maskVal);
    198201
     202        // XXX test: switch to kron flux
     203# if (KRON)
     204        float apMag = -2.5*log10(source->moments->KronFlux);
     205# else
    199206        float apMag = -2.5*log10(source->moments->Sum);
     207# endif
    200208        float dMag = source->psfMag - apMag;
    201209
     
    288296    pmSourcePhotometryMode photMode = PM_SOURCE_PHOT_WEIGHT;
    289297
     298    psImageMaskType markVal = options->markVal;
    290299    psImageMaskType maskVal = options->maskVal | options->markVal;
    291300
     
    335344
    336345        // XXX can we test if psfMag is set and calculate only if needed?
    337         pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     346        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    338347
    339348        // clear the mask bit
     
    343352        pmSourceSub (source, PM_MODEL_OP_FULL, options->maskVal);
    344353
     354# if (KRON)
     355        float apMag = -2.5*log10(source->moments->KronFlux);
     356# else
    345357        float apMag = -2.5*log10(source->moments->Sum);
     358# endif
    346359        float dMag = source->psfMag - apMag;
    347360
    348361        // set nSigma to include both systematic and poisson error terms
    349362        // XXX the 'poisson error' contribution for size is probably wrong...
    350         float nSigmaMAG = (dMag - options->ApResid) / hypot(source->errMag, options->ApSysErr);
     363        // XXX add in a hard floor on the Ap Sys Err (to be a bit generous)
     364        float nSigmaMAG = (dMag - options->ApResid) / hypot(source->errMag, hypot(options->ApSysErr, 0.025));
    351365        float nSigmaMXX = (Mxx - psfClump->X) / hypot(psfClump->dX, psfClump->X*psfClump->X*source->errMag);
    352366        float nSigmaMYY = (Myy - psfClump->Y) / hypot(psfClump->dY, psfClump->Y*psfClump->Y*source->errMag);
    353367
    354         // partially-masked sources are more likely to be mis-measured PSFs
     368        // fprintf (stderr, "%f %f : Mxx: %f, Myy: %f, dx: %f, dy: %f, psfMag: %f, apMag: %f, dMag: %f, errMag: %f, nSigmaMag: %f, nSigmaMxx: %f, nSigmaMyy: %f\n",
     369        // source->peak->xf, source->peak->yf, Mxx, Myy, source->peak->xf - source->moments->Mx, source->peak->yf - source->moments->My,
     370        // source->psfMag, apMag, dMag, source->errMag, nSigmaMAG, nSigmaMXX, nSigmaMYY);
     371
     372        // XXX double check on ths stuff!! partially-masked sources are more likely to be mis-measured PSFs
    355373        float sizeBias = 1.0;
    356         if (source->pixWeight < 0.9) {
     374        if (source->pixWeightNotBad < 0.9) {
     375            sizeBias = 3.0;
     376        }
     377        if (source->pixWeightNotPoor < 0.9) {
    357378            sizeBias = 3.0;
    358379        }
     
    389410        if (isCR) {
    390411            psTrace("psphotSourceClassRegion.CR",4,"CLASS: %g %g %f\t%g %g  %g %g  %g %g\t%g %g\t%g CR\t%g %g\n",
    391                     source->peak->xf,source->peak->yf,source->pixWeight,Mxx,Myy,psfClump->X,psfClump->Y,psfClump->dX,psfClump->dY,apMag,dMag,nSigmaMAG,
     412                    source->peak->xf,source->peak->yf,source->pixWeightNotBad,Mxx,Myy,psfClump->X,psfClump->Y,psfClump->dX,psfClump->dY,apMag,dMag,nSigmaMAG,
    392413                    options->nSigmaApResid,sizeBias*options->nSigmaMoments);
    393414            source->mode |= PM_SOURCE_MODE_DEFECT;
  • branches/sc_branches/trunkTest/psphot/src/psphotSourceStats.c

    r28419 r29060  
    11# include "psphotInternal.h"
     2void pmSourceMomentsSetVerbose(bool state);
    23
    34// convert detections to sources and measure their basic properties (moments, local sky, sky
     
    369370    maskVal |= markVal;
    370371
     372    // XXX test : pmSourceMomentsSetVerbose(true);
     373
    371374    // threaded measurement of the sources moments
    372375    int Nfail = 0;
     
    408411        // measure basic source moments (no S/N clipping on input pixels)
    409412        status = pmSourceMoments (source, RADIUS, SIGMA, 0.0, maskVal);
     413        // XXX moments / aperture test:
     414        if (0) {
     415            // clear the mask bit and set the circular mask pixels
     416            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
     417            psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, 4.0*source->moments->Mrf, "OR", markVal);
     418           
     419            float apMag = NAN;
     420            pmSourcePhotometryAper (&apMag, NULL, source->pixels, source->maskObj, maskVal);
     421            fprintf (stderr, "apMag: %f, kronMag: %f\n", apMag, -2.5*log10(source->moments->KronFlux));
     422
     423            // clear the mask bit
     424            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal));
     425        }           
    410426        if (status) {
    411427            Nmoments ++;
     
    502518        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DX", PS_META_REPLACE, "psf clump center", psfClump.dX);
    503519        psMetadataAddF32 (regionMD, PS_LIST_TAIL, "PSF.CLUMP.DY", PS_META_REPLACE, "psf clump center", psfClump.dY);
    504         psphotVisualPlotMoments (recipe, analysis, sources);
     520        if (pmVisualTestLevel("psphot.moments.full", 2)) {
     521            psphotVisualPlotMoments (recipe, analysis, sources);
     522        }
    505523#endif
    506524
  • branches/sc_branches/trunkTest/psphot/src/psphotStack.c

    r27547 r29060  
    2424        exit (psphotGetExitStatus());
    2525    }
     26    // Dump configuration
     27    bool mdok;
     28    psString dump = psMetadataLookupStr(&mdok, config->arguments, "DUMP_CONFIG"); // File for config
     29    if (dump && !pmConfigDump(config, dump)) {
     30        psError(psErrorCodeLast(), false, "Unable to dump configuration.");
     31        exit (psphotGetExitStatus());
     32    }
     33
     34
    2635
    2736    psLogMsg ("psphot", 3, "complete psphot run: %f sec\n", psTimerMark ("complete"));
  • branches/sc_branches/trunkTest/psphot/src/psphotStackMatchPSFsUtils.c

    r28013 r29060  
    346346    if (!fake) goto escape;
    347347
    348     dumpImage(fake, readoutSrc, index, "fake");
    349     dumpImage(readoutSrc,  readoutSrc, index, "real");
     348    // dumpImage(fake, readoutSrc, index, "fake");
     349    // dumpImage(readoutSrc,  readoutSrc, index, "real");
    350350
    351351    if (threads) pmSubtractionThreadsInit();
     
    380380    }
    381381
    382     dumpImage(readoutOut, readoutSrc, index, "conv");
    383     dumpImageDiff(readoutOut, fake, readoutSrc, index, "diff");
     382    // dumpImage(readoutOut, readoutSrc, index, "conv");
     383    // dumpImageDiff(readoutOut, fake, readoutSrc, index, "diff");
    384384
    385385    psFree(fake);
  • branches/sc_branches/trunkTest/psphot/src/psphotVisual.c

    r28128 r29060  
    6666    int myKapa = psphotKapaChannel (channel);
    6767    if (!(strcasecmp (overlay, "all"))) {
    68       KiiEraseOverlay (myKapa, "red");
    69       KiiEraseOverlay (myKapa, "green");
    70       KiiEraseOverlay (myKapa, "blue");
    71       KiiEraseOverlay (myKapa, "yellow");
    72       return true;
     68        KiiEraseOverlay (myKapa, "red");
     69        KiiEraseOverlay (myKapa, "green");
     70        KiiEraseOverlay (myKapa, "blue");
     71        KiiEraseOverlay (myKapa, "yellow");
     72        return true;
    7373    }
    7474    KiiEraseOverlay (myKapa, overlay);
     
    182182bool psphotVisualShowImage (pmReadout *readout) {
    183183
    184     if (!pmVisualIsVisual()) return true;
     184    if (!pmVisualTestLevel("psphot.image", 1)) return true;
    185185
    186186    int kapa = psphotKapaChannel (1);
     
    199199    pmReadout *backgnd;
    200200
    201     if (!DEBUG && !pmVisualIsVisual()) return true;
     201    if (!pmVisualTestLevel("psphot.image.backgnd", 2)) return true;
    202202
    203203    int kapa = psphotKapaChannel (1);
     
    208208
    209209    if (file->mode == PM_FPA_MODE_INTERNAL) {
    210         backgnd = file->readout;
     210        backgnd = file->readout;
    211211    } else {
    212         backgnd = pmFPAviewThisReadout (view, file->fpa);
     212        backgnd = pmFPAviewThisReadout (view, file->fpa);
    213213    }
    214214
     
    222222bool psphotVisualShowSignificance (psImage *image, float min, float max) {
    223223
    224     if (!DEBUG && !pmVisualIsVisual()) return true;
     224    if (!pmVisualTestLevel("psphot.image.signif", 2)) return true;
    225225
    226226    int kapa = psphotKapaChannel (1);
     
    233233}
    234234
     235bool psphotVisualShowLogSignificance (psImage *image, float min, float max) {
     236
     237    if (!pmVisualTestLevel("psphot.image.logsignif", 3)) return true;
     238
     239    int kapa = psphotKapaChannel (1);
     240    if (kapa == -1) return false;
     241
     242    psImage *lsig = (psImage *) psUnaryOp (NULL, image, "log");
     243    psphotVisualRangeImage (kapa, lsig, "log-signif", 2, min, max);
     244    psFree (lsig);
     245
     246    pmVisualAskUser(NULL);
     247    return true;
     248}
     249
     250// XXX : requires psphotVisualShowImage
    235251bool psphotVisualShowPeaks (pmDetections *detections) {
    236252
     
    238254    KiiOverlay *overlay;
    239255
    240     if (!DEBUG && !pmVisualIsVisual()) return true;
     256    if (!pmVisualTestLevel("psphot.objects.peaks", 1)) return true;
    241257
    242258    int kapa = psphotKapaChannel (1);
     
    252268    for (int i = 0; i < peaks->n; i++) {
    253269
    254         pmPeak *peak = peaks->data[i];
    255         if (peak == NULL) continue;
    256 
    257         overlay[Noverlay].type = KII_OVERLAY_BOX;
    258         overlay[Noverlay].x = peak->xf;
    259         overlay[Noverlay].y = peak->yf;
    260         overlay[Noverlay].dx = 2.0;
    261         overlay[Noverlay].dy = 2.0;
    262         overlay[Noverlay].angle = 0.0;
    263         overlay[Noverlay].text = NULL;
    264         Noverlay ++;
     270        pmPeak *peak = peaks->data[i];
     271        if (peak == NULL) continue;
     272
     273        overlay[Noverlay].type = KII_OVERLAY_BOX;
     274        overlay[Noverlay].x = peak->xf;
     275        overlay[Noverlay].y = peak->yf;
     276        overlay[Noverlay].dx = 2.0;
     277        overlay[Noverlay].dy = 2.0;
     278        overlay[Noverlay].angle = 0.0;
     279        overlay[Noverlay].text = NULL;
     280        Noverlay ++;
    265281    }
    266282
     
    272288}
    273289
     290// XXX : requires psphotVisualShowImage
    274291bool psphotVisualShowFootprints (pmDetections *detections) {
    275292
     
    277294    KiiOverlay *overlay;
    278295
    279     if (!DEBUG && !pmVisualIsVisual()) return true;
     296    if (!pmVisualTestLevel("psphot.objects.footprints", 3)) return true;
    280297
    281298    int kapa = psphotKapaChannel (1);
     
    292309    for (int i = 0; i < footprints->n; i++) {
    293310
    294         pmSpan *span = NULL;
    295 
    296         pmFootprint *footprint = footprints->data[i];
    297         if (footprint == NULL) continue;
    298         if (footprint->spans == NULL) continue;
    299         if (footprint->spans->n < 1) continue;
    300 
    301         // draw the top
    302         // XXX need to allow top (and bottom) to have more than one span
    303         span = footprint->spans->data[0];
    304         overlay[Noverlay].type = KII_OVERLAY_LINE;
    305         overlay[Noverlay].x = span->x0;
    306         overlay[Noverlay].y = span->y;
    307         overlay[Noverlay].dx = span->x1 - span->x0;
    308         overlay[Noverlay].dy = 0;
    309         overlay[Noverlay].angle = 0.0;
    310         overlay[Noverlay].text = NULL;
    311         Noverlay ++;
    312         CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    313 
    314         int ys = span->y;
    315         int x0s = span->x0;
    316         int x1s = span->x1;
    317 
    318         // draw the outer span edges
    319         for (int j = 1; j < footprint->spans->n; j++) {
    320             pmSpan *span1 = footprint->spans->data[j];
    321 
    322             int ye = span1->y;
    323             int x0e = span1->x0;
    324             int x1e = span1->x1;
    325 
    326             // we cannot have two discontinuous spans on the top or bottom, right? (no, probably not right)
    327             // find all of the spans in this row and generate x0e, x01:
    328             for (int k = j + 1; k < footprint->spans->n; k++) {
    329                 pmSpan *span2 = footprint->spans->data[k];
    330                 if (span2->y > span1->y) break;
    331                 x0e = PS_MIN (x0e, span2->x0);
    332                 x1e = PS_MAX (x1e, span2->x1);
    333                 j++;
    334             }
    335 
    336             overlay[Noverlay].type = KII_OVERLAY_LINE;
    337             overlay[Noverlay].x = x0s;
    338             overlay[Noverlay].y = ys;
    339             overlay[Noverlay].dx = x0e - x0s;
    340             overlay[Noverlay].dy = ye - ys;
    341             overlay[Noverlay].angle = 0.0;
    342             overlay[Noverlay].text = NULL;
    343             Noverlay ++;
    344             CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    345 
    346             overlay[Noverlay].type = KII_OVERLAY_LINE;
    347             overlay[Noverlay].x = x1s;
    348             overlay[Noverlay].y = ys;
    349             overlay[Noverlay].dx = x1e - x1s;
    350             overlay[Noverlay].dy = ye - ys;
    351             overlay[Noverlay].angle = 0.0;
    352             overlay[Noverlay].text = NULL;
    353             Noverlay ++;
    354             CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    355 
    356             ys = ye;
    357             x0s = x0e;
    358             x1s = x1e;
    359         }
    360 
    361         // draw the bottom
    362         span = footprint->spans->data[footprint->spans->n - 1];
    363         overlay[Noverlay].type = KII_OVERLAY_LINE;
    364         overlay[Noverlay].x = span->x0;
    365         overlay[Noverlay].y = span->y;
    366         overlay[Noverlay].dx = span->x1 - span->x0;
    367         overlay[Noverlay].dy = 0;
    368         overlay[Noverlay].angle = 0.0;
    369         overlay[Noverlay].text = NULL;
    370         Noverlay ++;
    371         CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     311        pmSpan *span = NULL;
     312
     313        pmFootprint *footprint = footprints->data[i];
     314        if (footprint == NULL) continue;
     315        if (footprint->spans == NULL) continue;
     316        if (footprint->spans->n < 1) continue;
     317
     318        // draw the top
     319        // XXX need to allow top (and bottom) to have more than one span
     320        span = footprint->spans->data[0];
     321        overlay[Noverlay].type = KII_OVERLAY_LINE;
     322        overlay[Noverlay].x = span->x0;
     323        overlay[Noverlay].y = span->y;
     324        overlay[Noverlay].dx = span->x1 - span->x0;
     325        overlay[Noverlay].dy = 0;
     326        overlay[Noverlay].angle = 0.0;
     327        overlay[Noverlay].text = NULL;
     328        Noverlay ++;
     329        CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     330
     331        int ys = span->y;
     332        int x0s = span->x0;
     333        int x1s = span->x1;
     334
     335        // draw the outer span edges
     336        for (int j = 1; j < footprint->spans->n; j++) {
     337            pmSpan *span1 = footprint->spans->data[j];
     338
     339            int ye = span1->y;
     340            int x0e = span1->x0;
     341            int x1e = span1->x1;
     342
     343            // we cannot have two discontinuous spans on the top or bottom, right? (no, probably not right)
     344            // find all of the spans in this row and generate x0e, x01:
     345            for (int k = j + 1; k < footprint->spans->n; k++) {
     346                pmSpan *span2 = footprint->spans->data[k];
     347                if (span2->y > span1->y) break;
     348                x0e = PS_MIN (x0e, span2->x0);
     349                x1e = PS_MAX (x1e, span2->x1);
     350                j++;
     351            }
     352
     353            overlay[Noverlay].type = KII_OVERLAY_LINE;
     354            overlay[Noverlay].x = x0s;
     355            overlay[Noverlay].y = ys;
     356            overlay[Noverlay].dx = x0e - x0s;
     357            overlay[Noverlay].dy = ye - ys;
     358            overlay[Noverlay].angle = 0.0;
     359            overlay[Noverlay].text = NULL;
     360            Noverlay ++;
     361            CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     362
     363            overlay[Noverlay].type = KII_OVERLAY_LINE;
     364            overlay[Noverlay].x = x1s;
     365            overlay[Noverlay].y = ys;
     366            overlay[Noverlay].dx = x1e - x1s;
     367            overlay[Noverlay].dy = ye - ys;
     368            overlay[Noverlay].angle = 0.0;
     369            overlay[Noverlay].text = NULL;
     370            Noverlay ++;
     371            CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     372
     373            ys = ye;
     374            x0s = x0e;
     375            x1s = x1e;
     376        }
     377
     378        // draw the bottom
     379        span = footprint->spans->data[footprint->spans->n - 1];
     380        overlay[Noverlay].type = KII_OVERLAY_LINE;
     381        overlay[Noverlay].x = span->x0;
     382        overlay[Noverlay].y = span->y;
     383        overlay[Noverlay].dx = span->x1 - span->x0;
     384        overlay[Noverlay].dy = 0;
     385        overlay[Noverlay].angle = 0.0;
     386        overlay[Noverlay].text = NULL;
     387        Noverlay ++;
     388        CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    372389    }
    373390
     
    379396}
    380397
     398// XXX : requires psphotVisualShowImage
    381399bool psphotVisualShowMoments (psArray *sources) {
    382400
     
    387405    psEllipseAxes axes;
    388406
    389     if (!DEBUG && !pmVisualIsVisual()) return true;
     407    if (!pmVisualTestLevel("psphot.objects.moments", 2)) return true;
    390408
    391409    int kapa = psphotKapaChannel (1);
     
    401419    for (int i = 0; i < sources->n; i++) {
    402420
    403         pmSource *source = sources->data[i];
    404         if (source == NULL) continue;
    405 
    406         pmMoments *moments = source->moments;
    407         if (moments == NULL) continue;
    408 
    409         overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
    410         overlay[Noverlay].x = moments->Mx;
    411         overlay[Noverlay].y = moments->My;
    412 
    413         emoments.x2 = moments->Mxx;
    414         emoments.xy = moments->Mxy;
    415         emoments.y2 = moments->Myy;
    416 
    417         axes = psEllipseMomentsToAxes (emoments, 20.0);
    418 
    419         overlay[Noverlay].dx = 2.0*axes.major;
    420         overlay[Noverlay].dy = 2.0*axes.minor;
    421 
    422         overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
    423 
    424         overlay[Noverlay].text = NULL;
    425         Noverlay ++;
     421        pmSource *source = sources->data[i];
     422        if (source == NULL) continue;
     423
     424        pmMoments *moments = source->moments;
     425        if (moments == NULL) continue;
     426
     427        overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
     428        overlay[Noverlay].x = moments->Mx;
     429        overlay[Noverlay].y = moments->My;
     430
     431        emoments.x2 = moments->Mxx;
     432        emoments.xy = moments->Mxy;
     433        emoments.y2 = moments->Myy;
     434
     435        axes = psEllipseMomentsToAxes (emoments, 20.0);
     436
     437        overlay[Noverlay].dx = 2.0*axes.major;
     438        overlay[Noverlay].dy = 2.0*axes.minor;
     439
     440        overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
     441
     442        overlay[Noverlay].text = NULL;
     443        Noverlay ++;
    426444    }
    427445
     
    439457    KapaSection section;
    440458
    441     if (!DEBUG && !pmVisualIsVisual()) return true;
     459    if (!pmVisualTestLevel("psphot.moments", 1)) return true;
    442460
    443461    int myKapa = psphotKapaChannel (2);
     
    456474    float Ymin = 1000.0, Ymax = 0.0;
    457475    {
    458         int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
    459         for (int n = 0; n < nRegions; n++) {
    460 
    461             char regionName[64];
    462             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
    463             psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
    464 
    465             float psfX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
    466             float psfY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
    467             float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
    468             float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
    469 
    470             float X0 = psfX - 4.0*psfdX;
    471             float X1 = psfX + 4.0*psfdX;
    472             float Y0 = psfY - 4.0*psfdY;
    473             float Y1 = psfY + 4.0*psfdY;
    474 
    475             if (isfinite(X0)) { Xmin = PS_MIN(Xmin, X0); }
    476             if (isfinite(X1)) { Xmax = PS_MAX(Xmax, X1); }
    477             if (isfinite(Y0)) { Ymin = PS_MIN(Ymin, Y0); }
    478             if (isfinite(Y1)) { Ymax = PS_MAX(Ymax, Y1); }
    479         }
     476        int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
     477        for (int n = 0; n < nRegions; n++) {
     478
     479            char regionName[64];
     480            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
     481            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     482
     483            float psfX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
     484            float psfY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
     485            float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
     486            float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
     487
     488            float X0 = psfX - 4.0*psfdX;
     489            float X1 = psfX + 4.0*psfdX;
     490            float Y0 = psfY - 4.0*psfdY;
     491            float Y1 = psfY + 4.0*psfdY;
     492
     493            if (isfinite(X0)) { Xmin = PS_MIN(Xmin, X0); }
     494            if (isfinite(X1)) { Xmax = PS_MAX(Xmax, X1); }
     495            if (isfinite(Y0)) { Ymin = PS_MIN(Ymin, Y0); }
     496            if (isfinite(Y1)) { Ymax = PS_MAX(Ymax, Y1); }
     497        }
    480498    }
    481499    Xmin = PS_MAX(Xmin, -0.1);
     
    498516    int nF = 0;
    499517    for (int i = 0; i < sources->n; i++) {
    500         pmSource *source = sources->data[i];
    501         if (source->moments == NULL)
    502             continue;
    503 
    504         xFaint->data.F32[nF] = source->moments->Mxx;
    505         yFaint->data.F32[nF] = source->moments->Myy;
    506         mFaint->data.F32[nF] = -2.5*log10(source->moments->Sum);
    507         nF++;
    508 
    509         // XXX make this a user-defined cutoff
    510         if (source->moments->SN < SN_LIM)
    511             continue;
    512 
    513         xBright->data.F32[nB] = source->moments->Mxx;
    514         yBright->data.F32[nB] = source->moments->Myy;
    515         mBright->data.F32[nB] = -2.5*log10(source->moments->Sum);
    516         nB++;
     518        pmSource *source = sources->data[i];
     519        if (source->moments == NULL)
     520            continue;
     521
     522        xFaint->data.F32[nF] = source->moments->Mxx;
     523        yFaint->data.F32[nF] = source->moments->Myy;
     524        mFaint->data.F32[nF] = -2.5*log10(source->moments->Sum);
     525        nF++;
     526
     527        // XXX make this a user-defined cutoff
     528        if (source->moments->SN < SN_LIM)
     529            continue;
     530
     531        xBright->data.F32[nB] = source->moments->Mxx;
     532        yBright->data.F32[nB] = source->moments->Myy;
     533        mBright->data.F32[nB] = -2.5*log10(source->moments->Sum);
     534        nB++;
    517535    }
    518536    xFaint->n = nF;
     
    652670    // draw N circles to outline the clumps
    653671    {
    654         KapaSelectSection (myKapa, "MxxMyy");
    655 
    656         // draw a circle centered on psfX,Y with size of the psf limit
    657         psVector *xLimit  = psVectorAlloc (120, PS_TYPE_F32);
    658         psVector *yLimit  = psVectorAlloc (120, PS_TYPE_F32);
    659 
    660         int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
    661         float PSF_CLUMP_NSIGMA = psMetadataLookupF32 (&status, recipe, "PSF_CLUMP_NSIGMA");
    662 
    663         graphdata.color = KapaColorByName ("blue");
    664         graphdata.style = 0;
    665 
    666         graphdata.xmin = Xmin;
    667         graphdata.ymin = Ymin;
    668         graphdata.xmax = Xmax;
    669         graphdata.ymax = Ymax;
    670         KapaSetLimits (myKapa, &graphdata);
    671 
    672         for (int n = 0; n < nRegions; n++) {
    673 
    674             char regionName[64];
    675             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
    676             psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
    677 
    678             float psfX  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
    679             float psfY  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
    680             float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
    681             float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
    682             float Rx = psfdX * PSF_CLUMP_NSIGMA;
    683             float Ry = psfdY * PSF_CLUMP_NSIGMA;
    684 
    685             for (int i = 0; i < xLimit->n; i++) {
    686                 xLimit->data.F32[i] = Rx*cos(i*2.0*M_PI/120.0) + psfX;
    687                 yLimit->data.F32[i] = Ry*sin(i*2.0*M_PI/120.0) + psfY;
    688             }
    689             KapaPrepPlot (myKapa, xLimit->n, &graphdata);
    690             KapaPlotVector (myKapa, xLimit->n, xLimit->data.F32, "x");
    691             KapaPlotVector (myKapa, yLimit->n, yLimit->data.F32, "y");
    692         }
    693         psFree (xLimit);
    694         psFree (yLimit);
     672        KapaSelectSection (myKapa, "MxxMyy");
     673
     674        // draw a circle centered on psfX,Y with size of the psf limit
     675        psVector *xLimit  = psVectorAlloc (120, PS_TYPE_F32);
     676        psVector *yLimit  = psVectorAlloc (120, PS_TYPE_F32);
     677
     678        int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
     679        float PSF_CLUMP_NSIGMA = psMetadataLookupF32 (&status, recipe, "PSF_CLUMP_NSIGMA");
     680
     681        graphdata.color = KapaColorByName ("blue");
     682        graphdata.style = 0;
     683
     684        graphdata.xmin = Xmin;
     685        graphdata.ymin = Ymin;
     686        graphdata.xmax = Xmax;
     687        graphdata.ymax = Ymax;
     688        KapaSetLimits (myKapa, &graphdata);
     689
     690        for (int n = 0; n < nRegions; n++) {
     691
     692            char regionName[64];
     693            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
     694            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     695
     696            float psfX  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
     697            float psfY  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
     698            float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
     699            float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
     700            float Rx = psfdX * PSF_CLUMP_NSIGMA;
     701            float Ry = psfdY * PSF_CLUMP_NSIGMA;
     702
     703            for (int i = 0; i < xLimit->n; i++) {
     704                xLimit->data.F32[i] = Rx*cos(i*2.0*M_PI/120.0) + psfX;
     705                yLimit->data.F32[i] = Ry*sin(i*2.0*M_PI/120.0) + psfY;
     706            }
     707            KapaPrepPlot (myKapa, xLimit->n, &graphdata);
     708            KapaPlotVector (myKapa, xLimit->n, xLimit->data.F32, "x");
     709            KapaPlotVector (myKapa, yLimit->n, yLimit->data.F32, "y");
     710        }
     711        psFree (xLimit);
     712        psFree (yLimit);
    695713    }
    696714
     
    721739    for (int i = 0; i < sources->n; i++) {
    722740
    723         pmSource *source = sources->data[i];
    724         if (source == NULL) continue;
    725 
    726         if (source->type != type) continue;
    727         if (mode && !(source->mode & mode)) continue;
    728 
    729         pmMoments *moments = source->moments;
    730         if (moments == NULL) continue;
    731 
    732         overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
    733         overlay[Noverlay].x = moments->Mx;
    734         overlay[Noverlay].y = moments->My;
    735 
    736         emoments.x2 = moments->Mxx;
    737         emoments.y2 = moments->Myy;
    738         emoments.xy = moments->Mxy;
    739 
    740         axes = psEllipseMomentsToAxes (emoments, 20.0);
    741 
    742         overlay[Noverlay].dx = 2.0*axes.major;
    743         overlay[Noverlay].dy = 2.0*axes.minor;
    744         overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
    745         overlay[Noverlay].text = NULL;
    746         Noverlay ++;
     741        pmSource *source = sources->data[i];
     742        if (source == NULL) continue;
     743
     744        if (source->type != type) continue;
     745        if (mode && !(source->mode & mode)) continue;
     746
     747        pmMoments *moments = source->moments;
     748        if (moments == NULL) continue;
     749
     750        overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
     751        overlay[Noverlay].x = moments->Mx;
     752        overlay[Noverlay].y = moments->My;
     753
     754        emoments.x2 = moments->Mxx;
     755        emoments.y2 = moments->Myy;
     756        emoments.xy = moments->Mxy;
     757
     758        axes = psEllipseMomentsToAxes (emoments, 20.0);
     759
     760        overlay[Noverlay].dx = 2.0*axes.major;
     761        overlay[Noverlay].dy = 2.0*axes.minor;
     762        overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
     763        overlay[Noverlay].text = NULL;
     764        Noverlay ++;
    747765    }
    748766
     
    753771}
    754772
     773// XXX : requires psphotVisualShowImage
    755774bool psphotVisualShowRoughClass (psArray *sources) {
    756775
    757     if (!DEBUG && !pmVisualIsVisual()) return true;
     776    if (!pmVisualTestLevel("psphot.objects.size", 3)) return true;
    758777
    759778    int myKapa = psphotKapaChannel (1);
     
    776795bool psphotVisualShowPSFModel (pmReadout *readout, pmPSF *psf) {
    777796
    778     if (!DEBUG && !pmVisualIsVisual()) return true;
     797    if (!pmVisualTestLevel("psphot.psf.model", 1)) return true;
    779798
    780799    int myKapa = psphotKapaChannel (3);
     
    797816    // generate a fake model at each of the 3x3 image grid positions
    798817    for (int x = -2; x <= +2; x ++) {
    799         for (int y = -2; y <= +2; y ++) {
    800             // use the center of the center pixel of the image
    801             float xc = (int)((0.5 + 0.225*x)*readout->image->numCols) + readout->image->col0 + 0.5;
    802             float yc = (int)((0.5 + 0.225*y)*readout->image->numRows) + readout->image->row0 + 0.5;
    803 
    804             // assign the x and y coords to the image center
    805             // create an object with center intensity of 1000
    806             modelRef->params->data.F32[PM_PAR_SKY] = 0;
    807             modelRef->params->data.F32[PM_PAR_I0] = 1000;
    808             modelRef->params->data.F32[PM_PAR_XPOS] = xc;
    809             modelRef->params->data.F32[PM_PAR_YPOS] = yc;
    810 
    811             // create modelPSF from this model
    812             pmModel *model = pmModelFromPSF (modelRef, psf);
    813             if (!model) continue;
    814 
    815             // place the reference object in the image center
    816             // no need to mask the source here
    817             // XXX should we measure this for the analytical model only or the full model?
    818             pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
    819             pmModelAddWithOffset (funMosaic, NULL, model, PM_MODEL_OP_FUNC | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
    820             pmModelAddWithOffset (resMosaic, NULL, model, PM_MODEL_OP_RES0 | PM_MODEL_OP_RES1 | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
    821             psFree (model);
    822         }
     818        for (int y = -2; y <= +2; y ++) {
     819            // use the center of the center pixel of the image
     820            float xc = (int)((0.5 + 0.225*x)*readout->image->numCols) + readout->image->col0 + 0.5;
     821            float yc = (int)((0.5 + 0.225*y)*readout->image->numRows) + readout->image->row0 + 0.5;
     822
     823            // assign the x and y coords to the image center
     824            // create an object with center intensity of 1000
     825            modelRef->params->data.F32[PM_PAR_SKY] = 0;
     826            modelRef->params->data.F32[PM_PAR_I0] = 1000;
     827            modelRef->params->data.F32[PM_PAR_XPOS] = xc;
     828            modelRef->params->data.F32[PM_PAR_YPOS] = yc;
     829
     830            // create modelPSF from this model
     831            pmModel *model = pmModelFromPSF (modelRef, psf);
     832            if (!model) continue;
     833
     834            // place the reference object in the image center
     835            // no need to mask the source here
     836            // XXX should we measure this for the analytical model only or the full model?
     837            pmModelAddWithOffset (psfMosaic, NULL, model, PM_MODEL_OP_FULL | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
     838            pmModelAddWithOffset (funMosaic, NULL, model, PM_MODEL_OP_FUNC | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
     839            pmModelAddWithOffset (resMosaic, NULL, model, PM_MODEL_OP_RES0 | PM_MODEL_OP_RES1 | PM_MODEL_OP_CENTER, 0, x*DX, y*DY);
     840            psFree (model);
     841        }
    823842    }
    824843
     
    842861    bool status;
    843862
    844     if (!DEBUG && !pmVisualIsVisual()) return true;
     863    if (!pmVisualTestLevel("psphot.psf.stars", 2)) return true;
    845864
    846865    int myKapa = psphotKapaChannel (3);
     
    871890    for (int i = 0; i < sources->n; i++) {
    872891
    873         pmSource *source = sources->data[i];
    874 
    875         bool keep = false;
    876         keep |= (source->mode & PM_SOURCE_MODE_PSFSTAR);
    877         if (!keep) continue;
    878 
    879         // how does this subimage get placed into the output image?
    880         // DX = source->pixels->numCols
    881         // DY = source->pixels->numRows
    882 
    883         if (dX + DX > NX) {
    884             // too wide for the rest of this row
    885             if (dX == 0) {
    886                 // alone on this row
    887                 NY += DY;
    888                 dX = 0;
    889                 dY = 0;
    890             } else {
    891                 // start the next row
    892                 NY += dY;
    893                 dX = DX;
    894                 dY = DY;
    895             }
    896         } else {
    897             // extend this row
    898             dX += DX;
    899             dY = PS_MAX (dY, DY);
    900         }
     892        pmSource *source = sources->data[i];
     893
     894        bool keep = false;
     895        keep |= (source->mode & PM_SOURCE_MODE_PSFSTAR);
     896        if (!keep) continue;
     897
     898        // how does this subimage get placed into the output image?
     899        // DX = source->pixels->numCols
     900        // DY = source->pixels->numRows
     901
     902        if (dX + DX > NX) {
     903            // too wide for the rest of this row
     904            if (dX == 0) {
     905                // alone on this row
     906                NY += DY;
     907                dX = 0;
     908                dY = 0;
     909            } else {
     910                // start the next row
     911                NY += dY;
     912                dX = DX;
     913                dY = DY;
     914            }
     915        } else {
     916            // extend this row
     917            dX += DX;
     918            dY = PS_MAX (dY, DY);
     919        }
    901920    }
    902921    NY += DY;
     
    918937    for (int i = 0; i < sources->n; i++) {
    919938
    920         pmSource *source = sources->data[i];
    921 
    922         bool keep = false;
    923         if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
    924             nPSF ++;
    925             keep = true;
    926         }
    927         if (!keep) continue;
    928 
    929         if (Xo + DX > NX) {
    930             // too wide for the rest of this row
    931             if (Xo == 0) {
    932                 // place source alone on this row
    933                 bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    934                 if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    935                 psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
    936 
    937                 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    938                 psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
    939 
    940                 if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    941 
    942                 Yo += DY;
    943                 Xo = 0;
    944                 dY = 0;
    945             } else {
    946                 // start the next row
    947                 Yo += dY;
    948                 Xo = 0;
    949 
    950                 bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    951                 if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    952                 psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
    953 
    954                 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    955                 psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
    956 
    957                 if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    958 
    959                 Xo = DX;
    960                 dY = DY;
    961             }
    962         } else {
    963             // extend this row
    964             bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    965             if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    966             psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
    967 
    968             pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    969             psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
    970             if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    971 
    972             Xo += DX;
    973             dY = PS_MAX (dY, DY);
    974         }
     939        pmSource *source = sources->data[i];
     940
     941        bool keep = false;
     942        if (source->mode & PM_SOURCE_MODE_PSFSTAR) {
     943            nPSF ++;
     944            keep = true;
     945        }
     946        if (!keep) continue;
     947
     948        if (Xo + DX > NX) {
     949            // too wide for the rest of this row
     950            if (Xo == 0) {
     951                // place source alone on this row
     952                bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     953                if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     954                psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
     955
     956                pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     957                psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
     958
     959                if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     960
     961                Yo += DY;
     962                Xo = 0;
     963                dY = 0;
     964            } else {
     965                // start the next row
     966                Yo += dY;
     967                Xo = 0;
     968
     969                bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     970                if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     971                psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
     972
     973                pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     974                psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
     975
     976                if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     977
     978                Xo = DX;
     979                dY = DY;
     980            }
     981        } else {
     982            // extend this row
     983            bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     984            if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     985            psphotMosaicSubimage (outpos, source, Xo, Yo, DX, DY, true);
     986
     987            pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     988            psphotMosaicSubimage (outsub, source, Xo, Yo, DX, DY, true);
     989            if (!subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     990
     991            Xo += DX;
     992            dY = PS_MAX (dY, DY);
     993        }
    975994    }
    976995
     
    9941013    bool status;
    9951014
    996     if (!DEBUG && !pmVisualIsVisual()) return true;
     1015    if (!pmVisualTestLevel("psphot.psf.sat", 3)) return true;
    9971016
    9981017    int myKapa = psphotKapaChannel (3);
     
    10231042    for (int i = 0; i < sources->n; i++) {
    10241043
    1025         pmSource *source = sources->data[i];
    1026 
    1027         // only show "real" saturated stars (not defects)
    1028         if (!(source->mode & PM_SOURCE_MODE_SATSTAR)) continue;;
    1029         if (source->mode & PM_SOURCE_MODE_DEFECT) continue;;
    1030 
    1031         // how does this subimage get placed into the output image?
    1032         // DX = source->pixels->numCols
    1033         // DY = source->pixels->numRows
    1034 
    1035         if (dX + DX > NX) {
    1036             // too wide for the rest of this row
    1037             if (dX == 0) {
    1038                 // alone on this row
    1039                 NY += DY;
    1040                 dX = 0;
    1041                 dY = 0;
    1042             } else {
    1043                 // start the next row
    1044                 NY += dY;
    1045                 dX = DX;
    1046                 dY = DY;
    1047             }
    1048         } else {
    1049             // extend this row
    1050             dX += DX;
    1051             dY = PS_MAX (dY, DY);
    1052         }
     1044        pmSource *source = sources->data[i];
     1045
     1046        // only show "real" saturated stars (not defects)
     1047        if (!(source->mode & PM_SOURCE_MODE_SATSTAR)) continue;;
     1048        if (source->mode & PM_SOURCE_MODE_DEFECT) continue;;
     1049
     1050        // how does this subimage get placed into the output image?
     1051        // DX = source->pixels->numCols
     1052        // DY = source->pixels->numRows
     1053
     1054        if (dX + DX > NX) {
     1055            // too wide for the rest of this row
     1056            if (dX == 0) {
     1057                // alone on this row
     1058                NY += DY;
     1059                dX = 0;
     1060                dY = 0;
     1061            } else {
     1062                // start the next row
     1063                NY += dY;
     1064                dX = DX;
     1065                dY = DY;
     1066            }
     1067        } else {
     1068            // extend this row
     1069            dX += DX;
     1070            dY = PS_MAX (dY, DY);
     1071        }
    10531072    }
    10541073    NY += DY;
     
    10681087    for (int i = 0; i < sources->n; i++) {
    10691088
    1070         pmSource *source = sources->data[i];
    1071 
    1072         // only show "real" saturated stars (not defects)
    1073         if (!(source->mode & PM_SOURCE_MODE_SATSTAR)) continue;;
    1074         if (source->mode & PM_SOURCE_MODE_DEFECT) continue;;
    1075         nSAT ++;
    1076 
    1077         if (Xo + DX > NX) {
    1078             // too wide for the rest of this row
    1079             if (Xo == 0) {
    1080                 // place source alone on this row
    1081                 bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    1082                 if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    1083                 psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
    1084                 if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    1085 
    1086                 Yo += DY;
    1087                 Xo = 0;
    1088                 dY = 0;
    1089             } else {
    1090                 // start the next row
    1091                 Yo += dY;
    1092                 Xo = 0;
    1093 
    1094                 bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    1095                 if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    1096                 psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
    1097                 if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    1098 
    1099                 Xo = DX;
    1100                 dY = DY;
    1101             }
    1102         } else {
    1103             // extend this row
    1104             bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
    1105             if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
    1106             psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
    1107             if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
    1108 
    1109             Xo += DX;
    1110             dY = PS_MAX (dY, DY);
    1111         }
     1089        pmSource *source = sources->data[i];
     1090
     1091        // only show "real" saturated stars (not defects)
     1092        if (!(source->mode & PM_SOURCE_MODE_SATSTAR)) continue;;
     1093        if (source->mode & PM_SOURCE_MODE_DEFECT) continue;;
     1094        nSAT ++;
     1095
     1096        if (Xo + DX > NX) {
     1097            // too wide for the rest of this row
     1098            if (Xo == 0) {
     1099                // place source alone on this row
     1100                bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     1101                if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     1102                psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
     1103                if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     1104
     1105                Yo += DY;
     1106                Xo = 0;
     1107                dY = 0;
     1108            } else {
     1109                // start the next row
     1110                Yo += dY;
     1111                Xo = 0;
     1112
     1113                bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     1114                if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     1115                psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
     1116                if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     1117
     1118                Xo = DX;
     1119                dY = DY;
     1120            }
     1121        } else {
     1122            // extend this row
     1123            bool subtracted = (source->tmpFlags & PM_SOURCE_TMPF_SUBTRACTED);
     1124            if (subtracted) pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal);
     1125            psphotMosaicSubimage (outsat, source, Xo, Yo, DX, DY, false);
     1126            if (subtracted) pmSourceSub (source, PM_MODEL_OP_FULL, maskVal);
     1127
     1128            Xo += DX;
     1129            dY = PS_MAX (dY, DY);
     1130        }
    11121131    }
    11131132
     
    11511170    float Yo = source->modelPSF->params->data.F32[PM_PAR_YPOS] - source->pixels->row0;
    11521171    for (int iy = 0; iy < source->pixels->numRows; iy++) {
    1153         for (int ix = 0; ix < source->pixels->numCols; ix++) {
    1154             if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) {
    1155                 rb->data.F32[nb] = hypot (ix + 0.5 - Xo, iy + 0.5 - Yo) ;
    1156                 // rb->data.F32[nb] = hypot (ix - Xo, iy - Yo) ;
    1157                 Rb->data.F32[nb] = log10(rb->data.F32[nb]);
    1158                 fb->data.F32[nb] = log10(source->pixels->data.F32[iy][ix]);
    1159                 nb++;
    1160             } else {
    1161                 rg->data.F32[ng] = hypot (ix + 0.5 - Xo, iy + 0.5 - Yo) ;
    1162                 // rg->data.F32[ng] = hypot (ix - Xo, iy - Yo) ;
    1163                 Rg->data.F32[ng] = log10(rg->data.F32[ng]);
    1164                 fg->data.F32[ng] = log10(source->pixels->data.F32[iy][ix]);
    1165                 ng++;
    1166             }
    1167         }
     1172        for (int ix = 0; ix < source->pixels->numCols; ix++) {
     1173            if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) {
     1174                rb->data.F32[nb] = hypot (ix + 0.5 - Xo, iy + 0.5 - Yo) ;
     1175                // rb->data.F32[nb] = hypot (ix - Xo, iy - Yo) ;
     1176                Rb->data.F32[nb] = log10(rb->data.F32[nb]);
     1177                fb->data.F32[nb] = log10(source->pixels->data.F32[iy][ix]);
     1178                nb++;
     1179            } else {
     1180                rg->data.F32[ng] = hypot (ix + 0.5 - Xo, iy + 0.5 - Yo) ;
     1181                // rg->data.F32[ng] = hypot (ix - Xo, iy - Yo) ;
     1182                Rg->data.F32[ng] = log10(rg->data.F32[ng]);
     1183                fg->data.F32[ng] = log10(source->pixels->data.F32[iy][ix]);
     1184                ng++;
     1185            }
     1186        }
    11681187    }
    11691188
     
    13581377    KapaSection section;  // put the positive profile in one and the residuals in another?
    13591378
    1360     if (!DEBUG && !pmVisualIsVisual()) return true;
     1379    if (!pmVisualTestLevel("psphot.profiles", 3)) return true;
    13611380
    13621381    int myKapa = psphotKapaChannel (2);
     
    13941413    for (int i = 0; i < sources->n; i++) {
    13951414
    1396         pmSource *source = sources->data[i];
    1397         if (!(source->mode & PM_SOURCE_MODE_PSFSTAR)) continue;
    1398 
    1399         psphotVisualPlotRadialProfile (myKapa, source, maskVal);
    1400 
    1401         // pause and wait for user input:
    1402         // continue, save (provide name), ??
    1403         char key[10];
    1404         fprintf (stdout, "[e]rase and continue? [o]verplot and continue? [s]kip rest of stars? : ");
    1405         if (!fgets(key, 8, stdin)) {
    1406             psWarning("Unable to read option");
    1407         }
    1408         if (key[0] == 'e') {
    1409             KapaClearPlots (myKapa);
    1410         }
    1411         if (key[0] == 's') {
    1412             break;
    1413         }
     1415        pmSource *source = sources->data[i];
     1416        if (!(source->mode & PM_SOURCE_MODE_PSFSTAR)) continue;
     1417
     1418        psphotVisualPlotRadialProfile (myKapa, source, maskVal);
     1419
     1420        // pause and wait for user input:
     1421        // continue, save (provide name), ??
     1422        char key[10];
     1423        fprintf (stdout, "[e]rase and continue? [o]verplot and continue? [s]kip rest of stars? : ");
     1424        if (!fgets(key, 8, stdin)) {
     1425            psWarning("Unable to read option");
     1426        }
     1427        if (key[0] == 'e') {
     1428            KapaClearPlots (myKapa);
     1429        }
     1430        if (key[0] == 's') {
     1431            break;
     1432        }
    14141433    }
    14151434
     
    14291448    return true;
    14301449
    1431     if (!DEBUG && !pmVisualIsVisual()) return true;
     1450    if (!pmVisualTestLevel("psphot.objects.flags", 3)) return true;
    14321451
    14331452    int myKapa = psphotKapaChannel (1);
     
    14451464    for (int i = 0; i < sources->n; i++) {
    14461465
    1447         float Xo, Yo, Rmaj, Rmin, cs, sn;
    1448 
    1449         pmSource *source = sources->data[i];
    1450         if (source == NULL) continue;
    1451 
    1452         pmMoments *moments = source->moments;
    1453         if (0) {
    1454             emoments.x2 = moments->Mxx;
    1455             emoments.y2 = moments->Myy;
    1456             emoments.xy = moments->Mxy;
    1457             Xo = moments->Mx;
    1458             Yo = moments->My;
    1459 
    1460             axes = psEllipseMomentsToAxes (emoments, 20.0);
    1461             Rmaj = 2.0*axes.major;
    1462             Rmin = 2.0*axes.minor;
    1463             cs = cos(axes.theta);
    1464             sn = sin(axes.theta);
    1465         } else {
    1466             Rmaj = Rmin = 5.0;
    1467             cs = 1.0;
    1468             sn = 0.0;
    1469             Xo = source->peak->xf;
    1470             Yo = source->peak->yf;
    1471         }
    1472 
    1473         unsigned short int flagMask = 0x01;
    1474         for (int j = 0; j < 8; j++) {
    1475             if (source->mode & flagMask) {
    1476                 overlayE[NoverlayE].type = KII_OVERLAY_LINE;
    1477                 overlayE[NoverlayE].x = Xo;
    1478                 overlayE[NoverlayE].y = Yo;
    1479 
    1480                 float phi = j*M_PI/4.0;
    1481                 overlayE[NoverlayE].dx = +Rmaj*cos(phi)*cs - Rmin*sin(phi)*sn;
    1482                 overlayE[NoverlayE].dy = +Rmaj*cos(phi)*sn + Rmin*sin(phi)*cs;
    1483                 overlayE[NoverlayE].angle = 0;
    1484                 overlayE[NoverlayE].text = NULL;
    1485                 NoverlayE ++;
    1486                 CHECK_REALLOCATE (overlayE, KiiOverlay, NOVERLAYE, NoverlayE, 100);
    1487             }
    1488             flagMask <<= 1;
    1489 
    1490             if (source->mode & flagMask) {
    1491                 overlayO[NoverlayO].type = KII_OVERLAY_LINE;
    1492                 overlayO[NoverlayO].x = Xo + 1;
    1493                 overlayO[NoverlayO].y = Yo;
    1494 
    1495                 float phi = j*M_PI/4.0;
    1496                 overlayO[NoverlayO].dx = +Rmaj*cos(phi)*cs - Rmin*sin(phi)*sn;
    1497                 overlayO[NoverlayO].dy = +Rmaj*cos(phi)*sn + Rmin*sin(phi)*cs;
    1498                 overlayO[NoverlayO].angle = 0;
    1499                 overlayO[NoverlayO].text = NULL;
    1500                 NoverlayO ++;
    1501                 CHECK_REALLOCATE (overlayO, KiiOverlay, NOVERLAYO, NoverlayO, 100);
    1502             }
    1503             flagMask <<= 1;
    1504         }
     1466        float Xo, Yo, Rmaj, Rmin, cs, sn;
     1467
     1468        pmSource *source = sources->data[i];
     1469        if (source == NULL) continue;
     1470
     1471        pmMoments *moments = source->moments;
     1472        if (0) {
     1473            emoments.x2 = moments->Mxx;
     1474            emoments.y2 = moments->Myy;
     1475            emoments.xy = moments->Mxy;
     1476            Xo = moments->Mx;
     1477            Yo = moments->My;
     1478
     1479            axes = psEllipseMomentsToAxes (emoments, 20.0);
     1480            Rmaj = 2.0*axes.major;
     1481            Rmin = 2.0*axes.minor;
     1482            cs = cos(axes.theta);
     1483            sn = sin(axes.theta);
     1484        } else {
     1485            Rmaj = Rmin = 5.0;
     1486            cs = 1.0;
     1487            sn = 0.0;
     1488            Xo = source->peak->xf;
     1489            Yo = source->peak->yf;
     1490        }
     1491
     1492        unsigned short int flagMask = 0x01;
     1493        for (int j = 0; j < 8; j++) {
     1494            if (source->mode & flagMask) {
     1495                overlayE[NoverlayE].type = KII_OVERLAY_LINE;
     1496                overlayE[NoverlayE].x = Xo;
     1497                overlayE[NoverlayE].y = Yo;
     1498
     1499                float phi = j*M_PI/4.0;
     1500                overlayE[NoverlayE].dx = +Rmaj*cos(phi)*cs - Rmin*sin(phi)*sn;
     1501                overlayE[NoverlayE].dy = +Rmaj*cos(phi)*sn + Rmin*sin(phi)*cs;
     1502                overlayE[NoverlayE].angle = 0;
     1503                overlayE[NoverlayE].text = NULL;
     1504                NoverlayE ++;
     1505                CHECK_REALLOCATE (overlayE, KiiOverlay, NOVERLAYE, NoverlayE, 100);
     1506            }
     1507            flagMask <<= 1;
     1508
     1509            if (source->mode & flagMask) {
     1510                overlayO[NoverlayO].type = KII_OVERLAY_LINE;
     1511                overlayO[NoverlayO].x = Xo + 1;
     1512                overlayO[NoverlayO].y = Yo;
     1513
     1514                float phi = j*M_PI/4.0;
     1515                overlayO[NoverlayO].dx = +Rmaj*cos(phi)*cs - Rmin*sin(phi)*sn;
     1516                overlayO[NoverlayO].dy = +Rmaj*cos(phi)*sn + Rmin*sin(phi)*cs;
     1517                overlayO[NoverlayO].angle = 0;
     1518                overlayO[NoverlayO].text = NULL;
     1519                NoverlayO ++;
     1520                CHECK_REALLOCATE (overlayO, KiiOverlay, NOVERLAYO, NoverlayO, 100);
     1521            }
     1522            flagMask <<= 1;
     1523        }
    15051524    }
    15061525
     
    15311550    for (int i = 0; i < sources->n; i++) {
    15321551
    1533         pmSource *source = sources->data[i];
    1534         if (source == NULL) continue;
    1535 
    1536         if (mode) {
    1537             if (keep) {
    1538                 if (!(source->mode & mode)) continue;
    1539             } else {
    1540                 if (source->mode & mode) continue;
    1541             }
    1542         }
    1543 
    1544         pmMoments *moments = source->moments;
    1545         if (moments == NULL) continue;
    1546 
    1547         overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
    1548         overlay[Noverlay].x = moments->Mx;
    1549         overlay[Noverlay].y = moments->My;
    1550 
    1551         emoments.x2 = moments->Mxx;
    1552         emoments.y2 = moments->Myy;
    1553         emoments.xy = moments->Mxy;
    1554 
    1555         axes = psEllipseMomentsToAxes (emoments, 20.0);
    1556 
    1557         overlay[Noverlay].dx = scale*2.0*axes.major;
    1558         overlay[Noverlay].dy = scale*2.0*axes.minor;
    1559         overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
    1560         overlay[Noverlay].text = NULL;
    1561         Noverlay ++;
     1552        pmSource *source = sources->data[i];
     1553        if (source == NULL) continue;
     1554
     1555        if (mode) {
     1556            if (keep) {
     1557                if (!(source->mode & mode)) continue;
     1558            } else {
     1559                if (source->mode & mode) continue;
     1560            }
     1561        }
     1562
     1563        pmMoments *moments = source->moments;
     1564        if (moments == NULL) continue;
     1565
     1566        overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
     1567        overlay[Noverlay].x = moments->Mx;
     1568        overlay[Noverlay].y = moments->My;
     1569
     1570        emoments.x2 = moments->Mxx;
     1571        emoments.y2 = moments->Myy;
     1572        emoments.xy = moments->Mxy;
     1573
     1574        axes = psEllipseMomentsToAxes (emoments, 20.0);
     1575
     1576        overlay[Noverlay].dx = scale*2.0*axes.major;
     1577        overlay[Noverlay].dy = scale*2.0*axes.minor;
     1578        overlay[Noverlay].angle = axes.theta * PS_DEG_RAD;
     1579        overlay[Noverlay].text = NULL;
     1580        Noverlay ++;
    15621581    }
    15631582
     
    15701589bool psphotVisualShowSourceSize (pmReadout *readout, psArray *sources) {
    15711590
    1572     if (!DEBUG && !pmVisualIsVisual()) return true;
     1591    if (!pmVisualTestLevel("psphot.objects.size", 2)) return true;
    15731592
    15741593    int myKapa = psphotKapaChannel (1);
     
    15971616    KapaSection section;
    15981617
    1599     if (!DEBUG && !pmVisualIsVisual()) return true;
     1618    if (!pmVisualTestLevel("psphot.size", 2)) return true;
    16001619
    16011620    int myKapa = psphotKapaChannel (2);
     
    16121631    float Ymin = 1000.0, Ymax = 0.0;
    16131632    {
    1614         int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
    1615         for (int n = 0; n < nRegions; n++) {
    1616 
    1617             char regionName[64];
    1618             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
    1619             psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
    1620 
    1621             float psfX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
    1622             float psfY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
    1623             float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
    1624             float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
    1625 
    1626             float X0 = psfX - 10.0*psfdX;
    1627             float X1 = psfX + 10.0*psfdX;
    1628             float Y0 = psfY - 10.0*psfdY;
    1629             float Y1 = psfY + 10.0*psfdY;
    1630 
    1631             if (isfinite(X0)) { Xmin = PS_MIN(Xmin, X0); }
    1632             if (isfinite(X1)) { Xmax = PS_MAX(Xmax, X1); }
    1633             if (isfinite(Y0)) { Ymin = PS_MIN(Ymin, Y0); }
    1634             if (isfinite(Y1)) { Ymax = PS_MAX(Ymax, Y1); }
    1635         }
     1633        int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
     1634        for (int n = 0; n < nRegions; n++) {
     1635
     1636            char regionName[64];
     1637            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
     1638            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     1639
     1640            float psfX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
     1641            float psfY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
     1642            float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
     1643            float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
     1644
     1645            float X0 = psfX - 10.0*psfdX;
     1646            float X1 = psfX + 10.0*psfdX;
     1647            float Y0 = psfY - 10.0*psfdY;
     1648            float Y1 = psfY + 10.0*psfdY;
     1649
     1650            if (isfinite(X0)) { Xmin = PS_MIN(Xmin, X0); }
     1651            if (isfinite(X1)) { Xmax = PS_MAX(Xmax, X1); }
     1652            if (isfinite(Y0)) { Ymin = PS_MIN(Ymin, Y0); }
     1653            if (isfinite(Y1)) { Ymax = PS_MAX(Ymax, Y1); }
     1654        }
    16361655    }
    16371656    Xmin = PS_MAX(Xmin, -0.1);
     
    16771696    int nCR  = 0;
    16781697    for (int i = 0; i < sources->n; i++) {
    1679         pmSource *source = sources->data[i];
    1680         if (source->moments == NULL) continue;
     1698        pmSource *source = sources->data[i];
     1699        if (source->moments == NULL) continue;
    16811700
    16821701        // only plot the measured sources...
    1683         if (!(source->tmpFlags & PM_SOURCE_TMPF_SIZE_MEASURED)) continue;
    1684 
    1685         if (source->mode & PM_SOURCE_MODE_CR_LIMIT) {
    1686             xCR->data.F32[nCR] = source->moments->Mxx;
    1687             yCR->data.F32[nCR] = source->moments->Myy;
    1688             mCR->data.F32[nCR] = -2.5*log10(source->moments->Sum);
    1689             sCR->data.F32[nCR] = source->extNsigma;
    1690             nCR++;
    1691         }
    1692         if (source->mode & PM_SOURCE_MODE_SATSTAR) {
    1693             xSAT->data.F32[nSAT] = source->moments->Mxx;
    1694             ySAT->data.F32[nSAT] = source->moments->Myy;
    1695             mSAT->data.F32[nSAT] = -2.5*log10(source->moments->Sum);
    1696             sSAT->data.F32[nSAT] = source->extNsigma;
    1697             nSAT++;
    1698         }
    1699         if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
    1700             xEXT->data.F32[nEXT] = source->moments->Mxx;
    1701             yEXT->data.F32[nEXT] = source->moments->Myy;
    1702             mEXT->data.F32[nEXT] = -2.5*log10(source->moments->Sum);
    1703             sEXT->data.F32[nEXT] = source->extNsigma;
    1704             nEXT++;
    1705             continue;
    1706         }
    1707         if (source->mode & PM_SOURCE_MODE_DEFECT) {
    1708             xDEF->data.F32[nDEF] = source->moments->Mxx;
    1709             yDEF->data.F32[nDEF] = source->moments->Myy;
    1710             mDEF->data.F32[nDEF] = -2.5*log10(source->moments->Sum);
    1711             sDEF->data.F32[nDEF] = source->extNsigma;
    1712             nDEF++;
    1713             continue;
    1714         }
    1715         if (source->errMag > 0.1) {
    1716             xLOW->data.F32[nLOW] = source->moments->Mxx;
    1717             yLOW->data.F32[nLOW] = source->moments->Myy;
    1718             mLOW->data.F32[nLOW] = -2.5*log10(source->moments->Sum);
    1719             sLOW->data.F32[nLOW] = source->extNsigma;
    1720             nLOW++;
    1721             continue;
    1722         }
    1723         xPSF->data.F32[nPSF] = source->moments->Mxx;
    1724         yPSF->data.F32[nPSF] = source->moments->Myy;
    1725         mPSF->data.F32[nPSF] = -2.5*log10(source->moments->Sum);
    1726         sPSF->data.F32[nPSF] = source->extNsigma;
    1727         nPSF++;
     1702        if (!(source->tmpFlags & PM_SOURCE_TMPF_SIZE_MEASURED)) continue;
     1703
     1704        if (source->mode & PM_SOURCE_MODE_CR_LIMIT) {
     1705            xCR->data.F32[nCR] = source->moments->Mxx;
     1706            yCR->data.F32[nCR] = source->moments->Myy;
     1707            mCR->data.F32[nCR] = -2.5*log10(source->moments->Sum);
     1708            sCR->data.F32[nCR] = source->extNsigma;
     1709            nCR++;
     1710        }
     1711        if (source->mode & PM_SOURCE_MODE_SATSTAR) {
     1712            xSAT->data.F32[nSAT] = source->moments->Mxx;
     1713            ySAT->data.F32[nSAT] = source->moments->Myy;
     1714            mSAT->data.F32[nSAT] = -2.5*log10(source->moments->Sum);
     1715            sSAT->data.F32[nSAT] = source->extNsigma;
     1716            nSAT++;
     1717        }
     1718        if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) {
     1719            xEXT->data.F32[nEXT] = source->moments->Mxx;
     1720            yEXT->data.F32[nEXT] = source->moments->Myy;
     1721            mEXT->data.F32[nEXT] = -2.5*log10(source->moments->Sum);
     1722            sEXT->data.F32[nEXT] = source->extNsigma;
     1723            nEXT++;
     1724            continue;
     1725        }
     1726        if (source->mode & PM_SOURCE_MODE_DEFECT) {
     1727            xDEF->data.F32[nDEF] = source->moments->Mxx;
     1728            yDEF->data.F32[nDEF] = source->moments->Myy;
     1729            mDEF->data.F32[nDEF] = -2.5*log10(source->moments->Sum);
     1730            sDEF->data.F32[nDEF] = source->extNsigma;
     1731            nDEF++;
     1732            continue;
     1733        }
     1734        if (source->errMag > 0.1) {
     1735            xLOW->data.F32[nLOW] = source->moments->Mxx;
     1736            yLOW->data.F32[nLOW] = source->moments->Myy;
     1737            mLOW->data.F32[nLOW] = -2.5*log10(source->moments->Sum);
     1738            sLOW->data.F32[nLOW] = source->extNsigma;
     1739            nLOW++;
     1740            continue;
     1741        }
     1742        xPSF->data.F32[nPSF] = source->moments->Mxx;
     1743        yPSF->data.F32[nPSF] = source->moments->Myy;
     1744        mPSF->data.F32[nPSF] = -2.5*log10(source->moments->Sum);
     1745        sPSF->data.F32[nPSF] = source->extNsigma;
     1746        nPSF++;
    17281747    }
    17291748
     
    20292048    // draw N circles to outline the clumps
    20302049    {
    2031         KapaSelectSection (myKapa, "MxxMyy");
    2032 
    2033         // draw a circle centered on psfX,Y with size of the psf limit
    2034         psVector *xLimit  = psVectorAlloc (120, PS_TYPE_F32);
    2035         psVector *yLimit  = psVectorAlloc (120, PS_TYPE_F32);
    2036 
    2037         int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
    2038         float PSF_CLUMP_NSIGMA = psMetadataLookupF32 (&status, recipe, "PSF_CLUMP_NSIGMA");
    2039 
    2040         graphdata.color = KapaColorByName ("blue");
    2041         graphdata.style = 0;
    2042 
    2043         graphdata.xmin = Xmin;
    2044         graphdata.ymin = Ymin;
    2045         graphdata.xmax = Xmax;
    2046         graphdata.ymax = Ymax;
    2047         KapaSetLimits (myKapa, &graphdata);
    2048 
    2049         for (int n = 0; n < nRegions; n++) {
    2050 
    2051             char regionName[64];
    2052             snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
    2053             psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
    2054 
    2055             float psfX  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
    2056             float psfY  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
    2057             float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
    2058             float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
    2059             float Rx = psfdX * PSF_CLUMP_NSIGMA;
    2060             float Ry = psfdY * PSF_CLUMP_NSIGMA;
    2061 
    2062             for (int i = 0; i < xLimit->n; i++) {
    2063                 xLimit->data.F32[i] = Rx*cos(i*2.0*M_PI/120.0) + psfX;
    2064                 yLimit->data.F32[i] = Ry*sin(i*2.0*M_PI/120.0) + psfY;
    2065             }
    2066             KapaPrepPlot (myKapa, xLimit->n, &graphdata);
    2067             KapaPlotVector (myKapa, xLimit->n, xLimit->data.F32, "x");
    2068             KapaPlotVector (myKapa, yLimit->n, yLimit->data.F32, "y");
    2069         }
    2070         psFree (xLimit);
    2071         psFree (yLimit);
     2050        KapaSelectSection (myKapa, "MxxMyy");
     2051
     2052        // draw a circle centered on psfX,Y with size of the psf limit
     2053        psVector *xLimit  = psVectorAlloc (120, PS_TYPE_F32);
     2054        psVector *yLimit  = psVectorAlloc (120, PS_TYPE_F32);
     2055
     2056        int nRegions = psMetadataLookupS32 (&status, analysis, "PSF.CLUMP.NREGIONS");
     2057        float PSF_CLUMP_NSIGMA = psMetadataLookupF32 (&status, recipe, "PSF_CLUMP_NSIGMA");
     2058
     2059        graphdata.color = KapaColorByName ("blue");
     2060        graphdata.style = 0;
     2061
     2062        graphdata.xmin = Xmin;
     2063        graphdata.ymin = Ymin;
     2064        graphdata.xmax = Xmax;
     2065        graphdata.ymax = Ymax;
     2066        KapaSetLimits (myKapa, &graphdata);
     2067
     2068        for (int n = 0; n < nRegions; n++) {
     2069
     2070            char regionName[64];
     2071            snprintf (regionName, 64, "PSF.CLUMP.REGION.%03d", n);
     2072            psMetadata *regionMD = psMetadataLookupPtr (&status, analysis, regionName);
     2073
     2074            float psfX  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.X");
     2075            float psfY  = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.Y");
     2076            float psfdX = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DX");
     2077            float psfdY = psMetadataLookupF32 (&status, regionMD, "PSF.CLUMP.DY");
     2078            float Rx = psfdX * PSF_CLUMP_NSIGMA;
     2079            float Ry = psfdY * PSF_CLUMP_NSIGMA;
     2080
     2081            for (int i = 0; i < xLimit->n; i++) {
     2082                xLimit->data.F32[i] = Rx*cos(i*2.0*M_PI/120.0) + psfX;
     2083                yLimit->data.F32[i] = Ry*sin(i*2.0*M_PI/120.0) + psfY;
     2084            }
     2085            KapaPrepPlot (myKapa, xLimit->n, &graphdata);
     2086            KapaPlotVector (myKapa, xLimit->n, xLimit->data.F32, "x");
     2087            KapaPlotVector (myKapa, yLimit->n, yLimit->data.F32, "y");
     2088        }
     2089        psFree (xLimit);
     2090        psFree (yLimit);
    20722091    }
    20732092
     
    21082127bool psphotVisualShowResidualImage (pmReadout *readout) {
    21092128
    2110     if (!DEBUG && !pmVisualIsVisual()) return true;
     2129    if (!pmVisualTestLevel("psphot.image.resid", 2)) return true;
    21112130
    21122131    int myKapa = psphotKapaChannel (1);
     
    21242143    float lineX[2], lineY[2];
    21252144
    2126     if (!DEBUG && !pmVisualIsVisual()) return true;
     2145    if (!pmVisualTestLevel("psphot.apresid", 1)) return true;
    21272146
    21282147    int myKapa = psphotKapaChannel (2);
     
    21442163    int n = 0;
    21452164    for (int i = 0; i < sources->n; i++) {
    2146         pmSource *source = sources->data[i];
    2147         if (!source) continue;
    2148         if (source->type != PM_SOURCE_TYPE_STAR) continue;
    2149         if (!isfinite (source->apMag)) continue;
    2150         if (!isfinite (source->psfMag)) continue;
    2151 
    2152         x->data.F32[n] = source->psfMag;
    2153         y->data.F32[n] = source->apMag - source->psfMag;
    2154         dy->data.F32[n] = source->errMag;
    2155         graphdata.xmin = PS_MIN(graphdata.xmin, x->data.F32[n]);
    2156         graphdata.xmax = PS_MAX(graphdata.xmax, x->data.F32[n]);
    2157         graphdata.ymin = PS_MIN(graphdata.ymin, y->data.F32[n]);
    2158         graphdata.ymax = PS_MAX(graphdata.ymax, y->data.F32[n]);
    2159 
    2160         n++;
     2165        pmSource *source = sources->data[i];
     2166        if (!source) continue;
     2167        if (source->type != PM_SOURCE_TYPE_STAR) continue;
     2168        if (!isfinite (source->apMag)) continue;
     2169        if (!isfinite (source->psfMag)) continue;
     2170
     2171        x->data.F32[n] = source->psfMag;
     2172        y->data.F32[n] = source->apMag - source->psfMag;
     2173        dy->data.F32[n] = source->errMag;
     2174        graphdata.xmin = PS_MIN(graphdata.xmin, x->data.F32[n]);
     2175        graphdata.xmax = PS_MAX(graphdata.xmax, x->data.F32[n]);
     2176        graphdata.ymin = PS_MIN(graphdata.ymin, y->data.F32[n]);
     2177        graphdata.ymax = PS_MAX(graphdata.ymax, y->data.F32[n]);
     2178
     2179        n++;
    21612180    }
    21622181    x->n = y->n = dy->n = n;
     
    22422261    Graphdata graphdata;
    22432262
    2244     if (!DEBUG && !pmVisualIsVisual()) return true;
     2263    if (!pmVisualTestLevel("psphot.chisq", 1)) return true;
    22452264
    22462265    int myKapa = psphotKapaChannel (2);
     
    22632282    int n = 0;
    22642283    for (int i = 0; i < sources->n; i++) {
    2265         pmSource *source = sources->data[i];
    2266         if (!source) continue;
    2267         if (source->type != PM_SOURCE_TYPE_STAR) continue;
    2268         if (!source->moments) continue;
    2269         if (!isfinite(source->moments->Sum)) continue;
    2270         if (!source->modelPSF) continue;
    2271         if (!isfinite(source->modelPSF->chisq)) continue;
    2272 
    2273         x->data.F32[n] = -2.5*log10(source->moments->Sum);
    2274         y->data.F32[n] = source->modelPSF->chisq / source->modelPSF->nDOF;
    2275         graphdata.xmin = PS_MIN(graphdata.xmin, x->data.F32[n]);
    2276         graphdata.xmax = PS_MAX(graphdata.xmax, x->data.F32[n]);
    2277         graphdata.ymin = PS_MIN(graphdata.ymin, y->data.F32[n]);
    2278         graphdata.ymax = PS_MAX(graphdata.ymax, y->data.F32[n]);
    2279 
    2280         fprintf (f, "%d %d %f %f\n", i, n, x->data.F32[n], y->data.F32[n]);
    2281 
    2282         n++;
     2284        pmSource *source = sources->data[i];
     2285        if (!source) continue;
     2286        if (source->type != PM_SOURCE_TYPE_STAR) continue;
     2287        if (!source->moments) continue;
     2288        if (!isfinite(source->moments->Sum)) continue;
     2289        if (!source->modelPSF) continue;
     2290        if (!isfinite(source->modelPSF->chisq)) continue;
     2291
     2292        x->data.F32[n] = -2.5*log10(source->moments->Sum);
     2293        y->data.F32[n] = source->modelPSF->chisq / source->modelPSF->nDOF;
     2294        graphdata.xmin = PS_MIN(graphdata.xmin, x->data.F32[n]);
     2295        graphdata.xmax = PS_MAX(graphdata.xmax, x->data.F32[n]);
     2296        graphdata.ymin = PS_MIN(graphdata.ymin, y->data.F32[n]);
     2297        graphdata.ymax = PS_MAX(graphdata.ymax, y->data.F32[n]);
     2298
     2299        fprintf (f, "%d %d %f %f\n", i, n, x->data.F32[n], y->data.F32[n]);
     2300
     2301        n++;
    22832302    }
    22842303    x->n = y->n = n;
     
    23262345    KiiOverlay *overlay;
    23272346
    2328     if (!DEBUG && !pmVisualIsVisual()) return true;
     2347    if (!pmVisualTestLevel("psphot.objects.petro", 2)) return true;
    23292348
    23302349    int kapa = psphotKapaChannel (1);
     
    23362355
    23372356    for (int i = 0; i < sources->n; i++) {
    2338         pmSource *source = sources->data[i];
    2339 
    2340         if (!source) continue;
    2341         if (!source->extpars) continue;
    2342         if (!source->extpars->petProfile) continue;
    2343 
    2344         float petrosianRadius = source->extpars->petrosianRadius;
     2357        pmSource *source = sources->data[i];
     2358
     2359        if (!source) continue;
     2360        if (!source->extpars) continue;
     2361        if (!source->extpars->petProfile) continue;
     2362
     2363        float petrosianRadius = source->extpars->petrosianRadius;
    23452364        psEllipseAxes *axes = &source->extpars->axes;
    23462365
    2347         overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
    2348         overlay[Noverlay].x = source->peak->xf;
    2349         overlay[Noverlay].y = source->peak->yf;
    2350         overlay[Noverlay].dx = 1.0*petrosianRadius;
    2351         overlay[Noverlay].dy = 1.0*petrosianRadius*axes->minor/axes->major;
    2352         overlay[Noverlay].angle = axes->theta * PS_DEG_RAD;
    2353         overlay[Noverlay].text = NULL;
    2354         Noverlay ++;
    2355         CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    2356 
    2357         // overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
    2358         // overlay[Noverlay].x = source->peak->xf;
    2359         // overlay[Noverlay].y = source->peak->yf;
    2360         // overlay[Noverlay].dx = 2.0*petrosianRadius;
    2361         // overlay[Noverlay].dy = 2.0*petrosianRadius*axes->minor/axes->major;
    2362         // overlay[Noverlay].angle = axes->theta * PS_DEG_RAD;
    2363         // overlay[Noverlay].text = NULL;
    2364         // Noverlay ++;
    2365         // CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     2366        overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
     2367        overlay[Noverlay].x = source->peak->xf;
     2368        overlay[Noverlay].y = source->peak->yf;
     2369        overlay[Noverlay].dx = 1.0*petrosianRadius;
     2370        overlay[Noverlay].dy = 1.0*petrosianRadius*axes->minor/axes->major;
     2371        overlay[Noverlay].angle = axes->theta * PS_DEG_RAD;
     2372        overlay[Noverlay].text = NULL;
     2373        Noverlay ++;
     2374        CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
     2375
     2376        // overlay[Noverlay].type = KII_OVERLAY_CIRCLE;
     2377        // overlay[Noverlay].x = source->peak->xf;
     2378        // overlay[Noverlay].y = source->peak->yf;
     2379        // overlay[Noverlay].dx = 2.0*petrosianRadius;
     2380        // overlay[Noverlay].dy = 2.0*petrosianRadius*axes->minor/axes->major;
     2381        // overlay[Noverlay].angle = axes->theta * PS_DEG_RAD;
     2382        // overlay[Noverlay].text = NULL;
     2383        // Noverlay ++;
     2384        // CHECK_REALLOCATE (overlay, KiiOverlay, NOVERLAY, Noverlay, 100);
    23662385    }
    23672386
  • branches/sc_branches/trunkTest/pstamp/scripts/detectability_respond.pl

    r28902 r29060  
    9898            my $key = shift(@key_values);
    9999            my $val = shift(@key_values);
     100            # if we have wisdom, then we should have updated already.  If not, we'll bomb out later in the code.
     101            if ($key eq 'FAULT') {
     102                $val = 0;
     103            }
    100104            $query{$fpa_id}{$key}[$i] = $val;
    101105        }
     
    148152        # Determine the query style for this fpa_id
    149153        if ($fpa_id =~ /o.*g.*o/) {
    150         $query_style = 'byexp';
    151     }
     154            $query_style = 'byexp';
     155        }
    152156        elsif ($fpa_id =~ /\d+/) {
    153         $query_style = 'byid';
    154     }
     157            $query_style = 'byid';
     158        }
    155159        else {
    156         exit_with_failure(21,"Parse error in request file");
    157     }
     160            exit_with_failure(21,"Parse error in request file");
     161        }
    158162        # Confirm that we only have one stage/filter/mjd
    159163        for (my $i = 0; $i <= $#{ $query{$fpa_id}{STAGE} }; $i++) {
    160         $temp_hash{STAGE}{$query{$fpa_id}{STAGE}[$i]} = 1;
    161         $temp_hash{FILTER}{$query{$fpa_id}{FILTER}[$i]} = 1;
    162         $temp_hash{'MJD-OBS'}{$query{$fpa_id}{'MJD-OBS'}[$i]} = 1;
    163     }
     164            $temp_hash{STAGE}{$query{$fpa_id}{STAGE}[$i]} = 1;
     165            $temp_hash{FILTER}{$query{$fpa_id}{FILTER}[$i]} = 1;
     166            $temp_hash{'MJD-OBS'}{$query{$fpa_id}{'MJD-OBS'}[$i]} = 1;
     167        }
    164168        if (scalar(keys(%{ $temp_hash{STAGE} })) == 1) {
    165169            $stage = (keys(%{ $temp_hash{STAGE} }))[0];
    166170        }
    167171        else {
    168         exit_with_failure(21,"Too many STAGEs specified");
    169     }
     172            exit_with_failure(21,"Too many STAGEs specified");
     173        }
    170174        if (scalar(keys(%{ $temp_hash{FILTER} })) == 1) {
    171         $filter = (keys(%{ $temp_hash{FILTER} }))[0];
    172     }
     175            $filter = (keys(%{ $temp_hash{FILTER} }))[0];
     176        }
    173177        else {
    174         exit_with_failure(21,"Too many FILTERs specified");
    175     }
     178            exit_with_failure(21,"Too many FILTERs specified");
     179        }
    176180        if (scalar(keys(%{ $temp_hash{'MJD-OBS'} })) == 1) {
    177         $mjd = (keys(%{ $temp_hash{'MJD-OBS'} }))[0];
    178     }
     181            $mjd = (keys(%{ $temp_hash{'MJD-OBS'} }))[0];
     182        }
    179183        else {
    180         exit_with_failure(21,"Too many MJD-OBS specified");
    181     }
     184            exit_with_failure(21,"Too many MJD-OBS specified");
     185        }
    182186        # Set common request components
    183187        my $option_mask |= 1;
     
    231235                    }
    232236                    else {
    233                     $query{$fpa_id}{CATALOG}[$valid_index] = $this_image_ref->{cmf};
    234                 }
     237                        $query{$fpa_id}{CATALOG}[$valid_index] = $this_image_ref->{cmf};
     238                    }
    235239                    if (exists($this_image_ref->{class_id})) {
    236240                        $query{$fpa_id}{COMPONENT_ID}[$valid_index] = $this_image_ref->{class_id};
     
    304308            ($query{$fpa_id}{STATE}[$i],$query{$fpa_id}{STAGE}[$i],$query{$fpa_id}{STAGE_ID}[$i],
    305309             $query{$fpa_id}{COMPONENT_ID}[$i],$query{$fpa_id}{NEED_MAGIC}[$i],$query{$fpa_id}{IMAGE_DB}[$i]);
    306         push @{ $processing_request{$fpa_id}{$query{$fpa_id}{IMAGE}[$i]} }, $i;
     310        push @{ $processing_request{$fpa_id}{$query{$fpa_id}{IMAGE}[$i]} }, $i;
    307311    }
    308312}
     
    384388            my ($r_ra,$r_dec,$trash,$r_x,$r_y,$r_chip) = split /\s+/, $line;
    385389            print $targetfile "$r_x $r_y\n";
    386             if (($r_ra == $query{$fpa_id}{RA1_DEG}[$processing_request{$fpa_id}{$image}[$i]])&&
    387                 ($r_dec == $query{$fpa_id}{DEC1_DEG}[$processing_request{$fpa_id}{$image}[$i]])) {
     390            if ((abs($r_ra - $query{$fpa_id}{RA1_DEG}[$processing_request{$fpa_id}{$image}[$i]]) < 1e-8)&&
     391                (abs($r_dec - $query{$fpa_id}{DEC1_DEG}[$processing_request{$fpa_id}{$image}[$i]]) < 1e-8)) {
    388392                $query{$fpa_id}{X_PXL}[$processing_request{$fpa_id}{$image}[$i]] = $r_x;
    389393                $query{$fpa_id}{Y_PXL}[$processing_request{$fpa_id}{$image}[$i]] = $r_y;
     
    392396            else {
    393397                $error_code = $PS_EXIT_PROG_ERROR;
    394                 my_die("Unable to match input RA/DEC with output RA/DEC: ($query{$fpa_id}{RA1_DEG}[$i],$query{$fpa_id}{DEC1_DEG}[$i]) -> ($r_ra,$r_dec) i. $error_code",
     398                my_die("Unable to match input RA/DEC with output RA/DEC: ($query{$fpa_id}{RA1_DEG}[$i],$query{$fpa_id}{DEC1_DEG}[$i]) -> ($r_ra,$r_dec) $error_code",
    395399                   $query{$fpa_id}{QUERY_ID}[$index],$fpa_id,$query{$fpa_id}{'MJD-OBS'}[$index],
    396400                   $query{$fpa_id}{FILTER}[$index],$query{$fpa_id}{OBSCODE}[$index],$query{$fpa_id}{STAGE}[$index],
  • branches/sc_branches/trunkTest/pstamp/scripts/pstamp_checkdependent.pl

    r28929 r29060  
    644644    # if the input file is already magicked no need to queue destreaking for this chipRun
    645645    if ($need_magic and !$input_magicked) {
    646         if ($dsRun_state eq 'cleaned') {
     646        if (!defined($dsRun_state) or ($dsRun_state eq 'NULL')) {
     647            print "No magicDSRun for chipRun $stage_id and magic is required\n";
     648            faultJobs('stop', undef, undef, $PSTAMP_NOT_DESTREAKED);
     649        } elsif ($dsRun_state eq 'cleaned') {
    647650            my $command = "$magicdstool -updaterun -set_state new -stage $stage -stage_id $stage_id";
    648651            $command .= " -set_label $rlabel" if $rlabel;
     
    658661            }
    659662        } elsif ($dsRun_state eq 'failed_revert') {
    660             print "magicDSRun.state = $dsRun_state for chipRun $stage_id is in state failed_revert cannot update";
     663            print "magicDSRun.state = $dsRun_state for chipRun $stage_id is in state failed_revert cannot update\n";
    661664            faultJobs('stop', undef, undef, $PSTAMP_NOT_AVAILABLE);
    662665        } else {
  • branches/sc_branches/trunkTest/pswarp/src/pswarpTransformSources.c

    r27279 r29060  
    102102        new->errMag = source->errMag;
    103103        new->apMag = source->apMag;
    104         new->pixWeight = source->pixWeight;
     104        new->pixWeightNotBad = source->pixWeightNotBad;
     105        new->pixWeightNotPoor = source->pixWeightNotPoor;
    105106        new->psfChisq = source->psfChisq;
    106107        new->crNsigma = source->crNsigma;
  • branches/sc_branches/trunkTest/tools/czarplot.pl

    r28922 r29060  
    3838        );
    3939
    40 print "\n";
     40
     41print "\n*******************************************************************************\n";
     42print "* \n";
     43my $quit = 0;
     44if (@ARGV) {
     45    $quit=1;
     46    print "* UNKNKOWN: option                          @ARGV\n";
     47}
    4148if (!$histogram) {
    4249    print "* OPTIONAL: plot histogram                  -h                          (default=off)\n";}
     
    5057    print "* OPTIONAL: choose a stage                  -s <chip|cam|warp|etc>      (default=none)\n";}
    5158if (!$interval) {
    52     print "* OPTIONAL: choose time interval in past    -i <'1 hour'|'1 day'|etc>   (default=interval since 7am this morning)\n";}
     59    print "* OPTIONAL: choose time interval in past    -i <'1 hour'|'1 day'|etc>   (default=none\n";}
    5360if (!$begin) {
    5461    print "* OPTIONAL: choose a begin time             -b <datetime>               (default=7am this morning)\n";}
     
    5764if (!$path) {
    5865    print "* OPTIONAL: choose an output location       -o <path>                   (default=none)\n";}
     66print "*\n*******************************************************************************\n";
    5967
    60 print "\n";
     68if ($quit) { exit; }
    6169
    6270# default values
     
    7179$czarDb->setDateFormat("%Y%m%d-%H%i%s");
    7280
     81
     82# GENE PLOTS my $czarplot = new czartool::Czarplot($czarDb, "%Y%m%d-%H%M%S", $savingToFile ? "png size 1280,960 font \"/usr/share/fonts/corefonts/arial.ttf\" 12" : "X11", $path, $save_temps);
    7383my $czarplot = new czartool::Czarplot($czarDb, "%Y%m%d-%H%M%S", $savingToFile ? "png font \"/usr/share/fonts/corefonts/arial.ttf\" 8" : "X11", $path, $save_temps);
    74 
    75 my @allStages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
    7684
    7785# sort out times
     
    7987if (!$begin) {
    8088    if ($interval) {$begin = $czarDb->subtractInterval($end, $interval);}
    81     else {$begin =  strftime('%Y-%m-%d 07:00',localtime);}
     89    else {$begin =  strftime('%Y-%m-%d 06:30',localtime);}
    8290}
    8391
  • branches/sc_branches/trunkTest/tools/czartool/CzarDb.pm

    r28921 r29060  
    1 #!/usr/bin/perl i-w
     1#!/usr/bin/perl -w
    22
    33package czartool::CzarDb;
     
    66use strict;
    77
    8 my @stages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist"); # TODO put elsewhere
     8my @stages = ("burntool", "chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist"); # TODO put elsewhere
    99
    1010use base 'czartool::MySQLDb';
     
    491491}
    492492
     493
     494###########################################################################
     495#
     496# Deletes all but one row per interval from all stage tables for all labels between the two dates
     497#
     498###########################################################################
     499sub cleanupDateRange {
     500    my ($self, $startDay, $endDay, $interval) = @_;
     501
     502    my $thisDay = $startDay;
     503    my $quit = 0;
     504    while(!$quit) {
     505
     506        if (!$self->isBefore($thisDay, $endDay)) {
     507       
     508            $quit = 1;
     509        }
     510
     511        print "* Running cleanup for $thisDay with an interval of $interval\n";
     512        $self->cleanupADay($thisDay, $interval);
     513        $thisDay = $self->addInterval($thisDay, "1 DAY");
     514
     515    }
     516}
     517###########################################################################
     518#
     519# Deletes all but one row per interval from all stage tables for all labels between the provided day
     520#
     521###########################################################################
     522sub cleanupADay {
     523    my ($self, $startDay, $interval) = @_;
     524
     525    my $endDay =  $self->addInterval($startDay, "1 DAY");
     526
     527    my $labels = undef;
     528    my $fromTime = $startDay;
     529    my $toTime = undef;
     530    my $totalDeleted = undef;
     531    my $quit = 0;
     532    while(!$quit) {
     533
     534        $toTime = $self->addInterval($fromTime, $interval);
     535        if (!$self->isBefore($toTime, $endDay)) {
     536       
     537            $toTime = $endDay;
     538            $quit = 1;
     539        }
     540   
     541        my $stage = undef;
     542        $totalDeleted = 0;
     543        foreach $stage (@stages) {
     544
     545            if (!$self->getLabelsInThisTimePeriod($stage, $fromTime, $toTime, \$labels)) {next;}
     546
     547            my $label = undef;
     548            my $row = undef;
     549            foreach $row ( @{$labels} ) {
     550                my ($label) = @{$row};
     551
     552                my $query = $self->{_db}->prepare(<<SQL);
     553                SELECT COUNT(*)
     554                    FROM $stage
     555                    WHERE timestamp > '$fromTime'
     556                    AND timestamp <= '$toTime'
     557                    AND label = '$label'
     558SQL
     559
     560                    $query->execute;
     561
     562                my $toDelete = scalar $query->fetchrow_array() - 1;
     563                if ($toDelete < 1) {next;}
     564
     565                $query = $self->{_db}->prepare(<<SQL);
     566                DELETE FROM $stage
     567                    WHERE timestamp > '$fromTime'
     568                    AND timestamp <= '$toTime'
     569                    AND label = '$label' ORDER BY timestamp DESC LIMIT $toDelete
     570SQL
     571                    $query->execute;
     572
     573                $totalDeleted += $toDelete;
     574            }
     575        }
     576        print "   * Deleted $totalDeleted between $fromTime and  $toTime\n";
     577        $fromTime = $toTime;
     578    }
     579}
     580
     581###########################################################################
     582#
     583# Returns an array of labels present during the provided time frame
     584#
     585###########################################################################
     586sub getLabelsInThisTimePeriod {
     587    my ($self, $stage, $fromTime, $toTime, $labels) = @_;
     588
     589    my $query = $self->{_db}->prepare(<<SQL);
     590    SELECT DISTINCT label
     591        FROM $stage
     592        WHERE timestamp > '$fromTime'
     593        AND timestamp <= '$toTime';
     594SQL
     595
     596        if (!$query->execute) {
     597
     598            return 0;
     599        }
     600
     601    ${$labels} = $query->fetchall_arrayref();
     602
     603    return 1;
     604}
     605
    493606###########################################################################
    494607#
     
    500613
    501614    my $currentRevision = -1;
    502     my $latestRevision = 10;
    503 
    504     while ($currentRevision != $latestRevision) {
     615
     616    while (1) {
    505617
    506618        $currentRevision = $self->getRevision();
     
    517629        elsif ($currentRevision == 8) {$self->createRevision_9();}
    518630        elsif ($currentRevision == 9) {$self->createRevision_10();}
     631        elsif ($currentRevision == 10) {$self->createRevision_11();}
     632        else {last;}
    519633    }
    520634}
     
    841955}
    842956
     957#######################################################################################
     958#
     959# Create revision 11 of the database
     960#
     961#######################################################################################
     962sub createRevision_11 {
     963    my ($self) = @_;
     964
     965    print "* Creating revision 11 of '$self->{_dbName}'\n";
     966
     967    # same shape as other stage tables to enable easy update
     968    my $query = $self->{_db}->prepare(<<SQL);
     969    CREATE TABLE burntool (
     970            timestamp TIMESTAMP DEFAULT NOW(),
     971            label VARCHAR(128) DEFAULT "NONE",
     972            pending BIGINT NOT NULL,
     973            processed BIGINT NOT NULL,
     974            faults BIGINT NOT NULL);
     975SQL
     976
     977      $query->execute;
     978        $query = $self->{_db}->prepare(<<SQL);
     979        CREATE INDEX burntoolIndex ON burntool (timestamp, label);
     980SQL
     981
     982      $query->execute;
     983
     984    $self->setRevision(11);
     985}
     986
     987
     988
    8439891;
    844990
  • branches/sc_branches/trunkTest/tools/czartool/Czarplot.pm

    r28923 r29060  
    55use strict;
    66
    7 my @allStages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
     7my @allStages = ("burntool", "chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
    88
    99
     
    7575    if ($numOfPlots == 0 ) {
    7676
    77         print "Warning: No plots could be generated for '$label' during time period '$beginTime', '$endTime'\n";
     77        print "Warning: No plots could be generated for stage '$selectedStage' and label '$label' during time period '$beginTime', '$endTime'\n";
    7878        return;
    7979    }
     
    103103    my ($processed, $pending, $faults);
    104104    my $stage = undef;
     105    my $pendingMinusFaults = undef;
    105106    foreach $stage (@allStages) {
    106107
    107108        $self->{_czarDb}->countProcessedPendingAndFaults($label, $stage, $beginTime, $endTime, \$processed, \$pending, \$faults);
    108         print GNUDAT "$stage $processed, $pending, $faults\n";
     109
     110        $pendingMinusFaults = $pending - $faults;
     111        print GNUDAT "$stage $faults $processed, $pendingMinusFaults\n";
    109112    }
    110113
     
    258261        if ($numOfPlots == 1) {
    259262
    260             print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"Processed\" with lines lt 2,";
    261             print GP "'" . $gnuplotFiles->{$stage} . "' using 1:2 title \"Pending\" with lines lt 4,";
    262             print GP "'" . $gnuplotFiles->{$stage} . "' using 1:3 title \"Faults\" with lines lt 7";
     263            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"Processed\" with lines lt 2 lw 2,";
     264            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:2 title \"Pending\" with lines lt 4 lw 2,";
     265            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:3 title \"Faults\" with lines lt 7 lw 2";
    263266        }
    264267        # when plotting multiple stages, show only processed
    265268        else {
    266             print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines";
     269            print GP "'" . $gnuplotFiles->{$stage} . "' using 1:4 title \"$stage\" with lines lw 2";
    267270        }
    268271        $firstIn = 0;
     
    339342        "set boxwidth;" .
    340343        "set style data histogram;" .
    341         "set style histogram cluster gap 3;" .
     344 "set style histogram rowstacked;" .
    342345        "set style fill solid border -1;" .
    343         "set bmargin 5;" .
    344346        "set ylabel \"Exposures\";" .
    345         "plot '$inputFile' using 2:xtic(1) title \"Processed\" lt 2, '' using 3 title \"Pending\" lt 4, '' using 4 title \"Faults\" lt 7;" .
     347        "set boxwidth 0.75;" .
     348        "plot '$inputFile' using 2:xtic(1) title \"Faults\" lt 1, '' using 3 title \"Processed\" lt 2, '' using 4 title \"Pending\" lt 7;" .
    346349        "\n";
     350
     351
    347352
    348353    close GP;
  • branches/sc_branches/trunkTest/tools/czartool/MySQLDb.pm

    r28771 r29060  
    6464    return $self->{_dbHost};                                                 
    6565}                                                                               
     66
     67###########################################################################
     68#
     69# Adds the provided interval to the provided time
     70#
     71###########################################################################
     72sub addInterval {
     73    my ($self, $time, $interval) = @_;
     74
     75      my $query = $self->{_db}->prepare(<<SQL);
     76          SELECT '$time' + INTERVAL $interval;
     77SQL
     78    $query->execute;
     79
     80return scalar $query->fetchrow_array();
     81}
    6682
    6783###########################################################################
  • branches/sc_branches/trunkTest/tools/roboczar.pl

    r28926 r29060  
    1212use czartool::Nebulous;
    1313use czartool::Czarplot;
     14use czartool::Burntool;
    1415
    1516my $period = 60;
     
    2728my $pantasks = new czartool::Pantasks();
    2829my $czarplot = new czartool::Czarplot($czarDb, "%Y%m%d-%H%M%S", "png font \"/usr/share/fonts/corefonts/arial.ttf\" 8", "/tmp", $save_temps); # TODO hardcoded font path
     30my $burntool = new czartool::Burntool();
     31
    2932$czarDb->setDateFormat("%Y%m%d-%H%i%s");
    3033
    31 my @stages = ("chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
     34my @stages = ("burntool", "chip", "cam", "fake", "warp", "stack", "diff", "magic", "magicDS", "dist");
    3235
    3336
     
    105108
    106109        # sort out times
    107         $begin =  strftime('%Y-%m-%d 07:00',localtime);
     110        $begin =  strftime('%Y-%m-%d 06:30',localtime);
    108111        $end = $czarDb->getNowTimestamp();
    109112
     
    233236            chomp($label);
    234237
    235             $new = $gpc1Db->countExposures($label, $stage, $newState);
    236             $full = $gpc1Db->countExposures($label, $stage, "full");
    237             $faults = $gpc1Db->countFaults($label, $stage, $newState);
    238 
     238            if ($stage eq "burntool") {
     239
     240                if ($labelServer eq "stdscience") {
     241
     242                    $burntool->getPendingAndProcessed($label, \$new, \$full);
     243                    $faults = 0;
     244                }
     245                else { $new = $full = $faults = 0;}
     246            }
     247            else {
     248
     249                $new = $gpc1Db->countExposures($label, $stage, $newState);
     250                $full = $gpc1Db->countExposures($label, $stage, "full");
     251                $faults = $gpc1Db->countFaults($label, $stage, $newState);
     252            }
    239253            #printf("%s  %s, %s, %d, %d\n", $labelServer, $label, $stage, $new, $faults);
    240254            $totalNew += $new;
Note: See TracChangeset for help on using the changeset viewer.