IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 33823


Ignore:
Timestamp:
Apr 19, 2012, 5:27:08 PM (14 years ago)
Author:
eugene
Message:

unify the 3 average magnitude calculation steps

Location:
branches/eam_branches/ipp-20120405/Ohana/src/relphot
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/include/relphot.h

    r33807 r33823  
    277277int           setMmos             PROTO((Catalog *catalog, int Poor, FlatCorrectionTable *flatcorr));
    278278int           setMrel             PROTO((Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr));
    279 void          setMrelFinal        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr));
     279void          setMrelFinal        PROTO((Catalog *catalog, FlatCorrectionTable *flatcorr, int simpleAverage));
    280280int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int pass, FlatCorrectionTable *flatcorr));
    281281int           setMave             PROTO((Catalog *catalog, int Ncatalog));
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/ImageOps.c

    r33651 r33823  
    4141
    4242// relationships between the measure,catalog set and the images:
    43 static IDX_T       **MeasureToImage; // image index from measure,catalog   : MeasureToImage[cat][meas] = ImageIndex
    44 static IDX_T       **ImageToCatalog; // catalog for given measure on image : ImageCatalog[ImageIndex][i] = cat (i : 0 < NonImage[ImageIndex])
    45 static IDX_T       **ImageToMeasure; // measure for given measure on image : ImageMeasure[ImageIndex][i] = cat (i : 0 < NonImage[ImageIndex])
     43static IDX_T       **MeasureToImage = NULL; // image index from measure,catalog   : MeasureToImage[cat][meas] = ImageIndex
     44static IDX_T       **ImageToCatalog = NULL; // catalog for given measure on image : ImageCatalog[ImageIndex][i] = cat (i : 0 < NonImage[ImageIndex])
     45static IDX_T       **ImageToMeasure = NULL; // measure for given measure on image : ImageMeasure[ImageIndex][i] = cat (i : 0 < NonImage[ImageIndex])
    4646
    4747// MeasureToImage was 'bin'
     
    322322  off_t i;
    323323
     324  if (!MeasureToImage) return -1;
     325
    324326  i = MeasureToImage[cat][meas];
    325327  return (i);
     
    354356  off_t i;
    355357  short distance;
     358
     359  if (!MeasureToImage) return -1;
    356360
    357361  i = MeasureToImage[cat][meas];
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/StarOps.c

    r33807 r33823  
    55typedef struct {
    66  int Nfew;
     7  int Ncode;
    78  int Nsys;
    89  int Nbad;
     
    1213  double *list;
    1314  double *dlist;
     15  double *aplist;
     16  double *daplist;
    1417} SetMrelInfo;
    1518
     
    2528} ThreadInfo;
    2629
     30// ** internal functions:
    2731void *setMrel_worker (void *data);
    2832int setMrel_threaded (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr);
    29 int setMrel_catalog (Catalog *catalog, int Nc, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt);
     33int setMrel_catalog (Catalog *catalog, int Nc, int pass, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt);
     34int print_measure_set (Average *average, SecFilt *secfilt, Measure *measure);
    3035
    3136// we want to allocate the stats list,dlist arrays only once (or once per thread).
     
    6570void SetMrelInfoInit (SetMrelInfo *results, int allocLists) {
    6671  results->Nfew  = 0;
     72  results->Ncode  = 0;
    6773  results->Nsys  = 0;
    6874  results->Nbad  = 0;
     
    8389void SetMrelInfoAccum (SetMrelInfo *summary, SetMrelInfo *results) {
    8490  summary->Nfew  += results->Nfew ;
     91  summary->Ncode  += results->Ncode ;
    8592  summary->Nsys  += results->Nsys ;
    8693  summary->Nbad  += results->Nbad ;
     
    110117}
    111118
     119// setMrel and setMrelOutput are extremely similar, but have slightly different implications:
     120// * setMrel uses the internal Tiny structures only
     121// * setMrelOutput skips stars for which there are too few good measurements
     122// * setMrelOutput is meant to be called repeatedly, relaxing the criteria for 'good' on each pass
     123// * setMrelOutput updates 2MASS average flags
     124// * setMrelOutput updates average EXT flags (PS1 and 2MASS)
     125
    112126int setMrel (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
    113127
     
    126140
    127141  for (i = 0; i < Ncatalog; i++) {
    128     setMrel_catalog (catalog, i, flatcorr, &results, Nsecfilt);
     142    // pass == -1 for anything other than the final pass
     143    setMrel_catalog (catalog, i, -1, flatcorr, &results, Nsecfilt);
    129144    SetMrelInfoAccum (&summary, &results);
    130145  }
    131   fprintf (stderr, "%d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
     146  fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
    132147
    133148  SetMrelInfoFree (&results);
     
    135150  return (TRUE);
    136151}
     152
     153int setMrelOutput (Catalog *catalog, int Ncatalog, int pass, FlatCorrectionTable *flatcorr) {
     154
     155  int i;
     156
     157  int Nsecfilt = GetPhotcodeNsecfilt ();
     158
     159  SetMrelInfo summary, results;
     160  SetMrelInfoInit (&summary, FALSE);
     161  SetMrelInfoInit (&results, TRUE); // allocates results->list,dlist
     162  ALLOCATE (results.aplist, double, Nmax);
     163  ALLOCATE (results.daplist, double, Nmax);
     164
     165  for (i = 0; i < Ncatalog; i++) {
     166    setMrel_catalog  (catalog, i, pass, flatcorr, &results, Nsecfilt); // XXX add arguments as needed for options
     167    SetMrelInfoAccum (&summary, &results);
     168  }
     169  fprintf (stderr, "%d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
     170
     171  SetMrelInfoFree (&results);
     172  free (results.aplist);
     173  free (results.daplist);
     174  return (TRUE);
     175}
     176
     177int setMrel_threaded (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
     178
     179  int i;
     180
     181  SetMrelInfo summary;
     182  SetMrelInfoInit (&summary, FALSE);
     183
     184  pthread_attr_t attr;
     185  pthread_attr_init (&attr);
     186  pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
     187 
     188  pthread_t *threads;
     189  ALLOCATE (threads, pthread_t, NTHREADS);
     190
     191  ThreadInfo *threadinfo;
     192  ALLOCATE (threadinfo, ThreadInfo, NTHREADS);
     193
     194  // launch N worker threads
     195  for (i = 0; i < NTHREADS; i++) {
     196    threadinfo[i].entry = i;
     197    threadinfo[i].state = THREAD_RUN;
     198    threadinfo[i].catalog  =  catalog;
     199    threadinfo[i].Ncatalog = Ncatalog;
     200    threadinfo[i].flatcorr = flatcorr;
     201    SetMrelInfoInit (&threadinfo[i].summary, FALSE);
     202    pthread_create (&threads[i], NULL, setMrel_worker, &threadinfo[i]);
     203  }
     204  pthread_attr_destroy (&attr);
     205
     206  // wait until all threads have finished
     207  while (1) {
     208    int allDone = TRUE;
     209    for (i = 0; i < NTHREADS; i++) {
     210      if (threadinfo[i].state == THREAD_RUN) allDone = FALSE;
     211    }
     212    if (allDone) {
     213      break;
     214    }
     215    usleep (500000);
     216  }
     217
     218  // all threads are done, free the threads array and grab the info
     219  free (threads);
     220 
     221  // report stats & summary from the threads
     222  for (i = 0; i < NTHREADS; i++) {
     223    fprintf (stderr, "setMrel thread %d : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n",
     224             i,
     225             threadinfo[i].summary.Ncode,
     226             threadinfo[i].summary.Nfew,
     227             threadinfo[i].summary.Nbad,
     228             threadinfo[i].summary.Ncal,
     229             threadinfo[i].summary.Nmos,
     230             threadinfo[i].summary.Ngrid,
     231             threadinfo[i].summary.Nsys);
     232    SetMrelInfoAccum (&summary, &threadinfo[i].summary);
     233  }
     234  fprintf (stderr, "total : %d stars with no data in photcode, %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Ncode, summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
     235  free (threadinfo);
     236
     237  return TRUE;
     238}
     239
     240void *setMrel_worker (void *data) {
     241
     242  ThreadInfo *threadinfo = data;
     243
     244  int Nsecfilt = GetPhotcodeNsecfilt ();
     245
     246  SetMrelInfo results;
     247  SetMrelInfoInit (&results, TRUE); // allocate list, dlist arrays here
     248
     249  while (1) {
     250
     251    off_t i = getNextCatalogForThread(threadinfo->Ncatalog);
     252    if (i == -1) {
     253      threadinfo->state = THREAD_DONE;
     254      return NULL;
     255    }
     256
     257    Catalog *catalog = threadinfo->catalog;
     258    FlatCorrectionTable *flatcorr = threadinfo->flatcorr;
     259
     260    // pass == -1 for anything other than the final pass
     261    setMrel_catalog (catalog, i, -1, flatcorr, &results, Nsecfilt);
     262    SetMrelInfoAccum (&threadinfo->summary, &results);
     263  }
     264
     265  SetMrelInfoFree (&results);
     266  return NULL;
     267}
     268
     269# define SKIP_THIS_MEAS(REASON) {                               \
     270    catalog[Nc].measureT[m].dbFlags |= ID_MEAS_SKIP_PHOTOM;     \
     271    if (catalog[Nc].measure) {                                  \
     272      catalog[Nc].measure [m].dbFlags |= ID_MEAS_SKIP_PHOTOM;   \
     273    }                                                           \
     274    results->REASON ++;                                         \
     275    continue; }
     276
     277
     278// setMrel_catalog is used in 3 different contexts:
     279
     280// * during the main relphot iterations:
     281// ** operations only apply to measureT / averageT
     282// ** we are applying the analysis to the bright subset catalog
     283// ** we skip any stars found to be bad (STAR_BAD)
     284
     285// * during the final pass
     286// ** operations are applied to all objects
     287// ** we have special tests for PS1 extended data, 2MASS extended & good data, & synthetic photometry
     288
     289// * during the stand-alone '-averages' mode (relphot_objects)
     290// ** no image data is loaded
     291// ** we only use the provided calibration (measure.Mcal)
     292// ** we skip outlier rejection of measurements
    137293
    138294// set the Mrel values for the specified catalog
    139295// NOTE: here 'catalog' is a pointer to a specific catalog, not the root of the array
    140 int setMrel_catalog (Catalog *catalog, int Nc, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt) {
     296int setMrel_catalog (Catalog *catalog, int Nc, int pass, FlatCorrectionTable *flatcorr, SetMrelInfo *results, int Nsecfilt) {
    141297
    142298  off_t j, k, m;
    143299  int N;
    144300  float Msys, Mcal, Mmos, Mgrid;
    145   StatType stats;
    146 
    147   double *list  = results->list;
    148   double *dlist = results->dlist;
     301  StatType stats, apstats;
     302
     303  double *list    = results->list;
     304  double *dlist   = results->dlist;
     305  double *aplist  = results->aplist;
     306  double *daplist = results->daplist;
    149307
    150308  SetMrelInfoInit (results, FALSE); // do not allocate list,dlist arrays
     309
     310  int isSetMrelFinal = (pass >= 0);
    151311
    152312  for (j = 0; j < catalog[Nc].Naverage; j++) {
    153313    // XXX accumulate all secfilt values in a single pass?
    154314
    155     int minUbercalDist = 1000;
     315    // option for a test print
     316    if (FALSE && (catalog[Nc].average[j].objID == 0x46a4) && (catalog[Nc].average[j].catID == 0xf40e)) {
     317      fprintf (stderr, "test obj\n");
     318      print_measure_set (&catalog[Nc].average[j], &catalog[Nc].secfilt[j*Nsecfilt], catalog[Nc].measure);
     319    }
     320
     321    int GoodPS1 = FALSE;
     322    int Good2MASS = FALSE;
     323    int Galaxy2MASS = FALSE;
     324
     325    int NextPS1 = 0;
     326    int NpsfPS1 = 0;
    156327
    157328    int Ns;
     
    163334      /* calculate the average mag in this SEC photcode for a single star */
    164335
     336      /* star/photcodes already calibrated */
     337      if ( isSetMrelFinal && catalog[Nc].found[Nsecfilt*j+Nsec]) continue; 
     338     
    165339      // skip bad stars
    166       if (catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
    167 
     340      if (!isSetMrelFinal && (catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD)) continue;
     341
     342      int Ncode = 0;
     343      int Next = 0;
     344      int haveSynth = FALSE;
     345      int haveUbercal = FALSE;
     346
     347      int minUbercalDist = 1000;
     348   
    168349      N = 0;
    169350      m = catalog[Nc].averageT[j].measureOffset;
     
    174355        if (!code) continue;
    175356        if (code->equiv != thisCode) { continue; }
    176 
    177         if (catalog[Nc].measureT[m].dbFlags & MEAS_BAD) { results->Nbad ++; continue; }
     357        Ncode ++;
     358
     359        if (catalog[Nc].measureT[m].dbFlags & MEAS_BAD) SKIP_THIS_MEAS(Nbad);
    178360
    179361        if (getImageEntry (m, Nc) < 0) {
     
    185367        } else {
    186368          Mcal  = getMcal  (m, Nc, flatcorr, catalog);
    187           if (isnan(Mcal))  { results->Ncal ++; continue; }
     369          if (isnan(Mcal))  SKIP_THIS_MEAS(Ncal);
    188370          Mmos  = getMmos  (m, Nc);
    189           if (isnan(Mmos))  { results->Nmos ++; continue; }
     371          if (isnan(Mmos))  SKIP_THIS_MEAS(Nmos);
    190372          Mgrid = getMgrid (m, Nc);
    191           if (isnan(Mgrid)) { results->Ngrid++; continue; }
     373          if (isnan(Mgrid)) SKIP_THIS_MEAS(Ngrid);
    192374        }
    193375
    194376        Msys = PhotSysTiny (&catalog[Nc].measureT[m], &catalog[Nc].averageT[j], &catalog[Nc].secfilt[j*Nsecfilt]);
    195         if (isnan(Msys)) { results->Nsys++; continue; }
     377        if (isnan(Msys)) SKIP_THIS_MEAS(Nsys);
     378
    196379        list[N] = Msys - Mcal - Mmos - Mgrid;
    197380
    198381        int myUbercalDist = getUbercalDist(m,Nc);
    199382        minUbercalDist = MIN(minUbercalDist, myUbercalDist);
     383
     384        if (isSetMrelFinal) {
     385          float Map = PhotAper (&catalog[Nc].measure[m]);
     386          aplist[N] = Map - Mcal - Mmos - Mgrid;
     387
     388          // count the extended detections
     389          if ((catalog[Nc].measure[m].photcode >= 10000) && (catalog[Nc].measure[m].photcode <= 10500)) {
     390            if (!isnan(catalog[Nc].measure[m].Map)) {
     391              if (catalog[Nc].measure[m].M - catalog[Nc].measure[m].Map > 0.5) {
     392                Next ++;
     393                NextPS1 ++;
     394              } else {
     395                NpsfPS1 ++;
     396              }
     397            }
     398          }
     399          // count extended detections for 2MASS (XXX NOTE hardwired photcodes 2011, 2012, 2013)
     400          if ((catalog[Nc].measure[m].photcode >= 2011) && (catalog[Nc].measure[m].photcode <= 2013)) {
     401            if (catalog[Nc].measure[m].photFlags & 0x00c00000) {
     402              Next ++;
     403              Galaxy2MASS = TRUE;
     404            }
     405            if (pass == 0) {
     406              if (catalog[Nc].measure[m].photFlags & 0x00000007) {
     407                Good2MASS = TRUE;
     408              } else {
     409                // detections without one of these bits should only be used in PASS_1
     410                SKIP_THIS_MEAS(Nbad);
     411                continue;
     412              }
     413            }
     414          }
     415          // ignore SYNTH photocdes until PASS == 4 (where we also accept saturated stars)
     416          if ((catalog[Nc].measure[m].photcode >= 3001) && (catalog[Nc].measure[m].photcode <= 3005)) {
     417            if (pass < 4) {
     418              SKIP_THIS_MEAS(Nbad);
     419              continue;
     420            }
     421            haveSynth = TRUE;
     422          }
     423        }
    200424
    201425        // dlist gives the error, which is used as the weight in WT_MEAN.
     
    225449      }
    226450
    227       // when performing the grid analysis, STAR_TOOFEW will be set to 1;
    228       if (N <= STAR_TOOFEW) { /* too few measurements */
     451      int Nminmeas = isSetMrelFinal ? 1 : STAR_TOOFEW + 1;
     452
     453      // when performing the grid analysis, STAR_TOOFEW should be set to 1;
     454      if (N < Nminmeas) { /* too few measurements */
    229455        // fprintf (f, "%10.6f %10.6f %d %d %d\n", catalog[Nc].averageT[j].R, catalog[Nc].averageT[j].D, catalog[Nc].measureT[catalog[Nc].averageT[j].measureOffset].imageID, N, STAR_TOOFEW);
    230456        catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_STAR_FEW;
    231         results->Nfew ++;
     457        if (Ncode == 0) {
     458          results->Ncode ++;
     459        } else {
     460          results->Nfew ++;
     461        }
     462        continue;
    232463      } else {
    233464        catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags &= ~ID_STAR_FEW;
     
    235466
    236467      liststats (list, dlist, N, &stats);
    237        
     468
    238469      catalog[Nc].secfilt[Nsecfilt*j+Nsec].M  = stats.mean;
    239       catalog[Nc].secfilt[Nsecfilt*j+Nsec].dM = stats.sigma;
     470      catalog[Nc].secfilt[Nsecfilt*j+Nsec].dM = stats.error;
    240471      catalog[Nc].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq + 1e-4) : NAN_S_SHORT;
    241        
    242       catalog[Nc].secfilt[Nsecfilt*j+Nsec].ubercalDist = minUbercalDist;
    243     }
    244   }
    245 
    246   // values which need to be passed out: Nfew, Nbad, Ncal, Nmos, Ngrid, Nsys
     472
     473      // when running -averages, we have no information about the images, so we cannot set this
     474      if (minUbercalDist > -1) {
     475        catalog[Nc].secfilt[Nsecfilt*j+Nsec].ubercalDist = minUbercalDist;
     476      }
     477
     478      if (isSetMrelFinal) {
     479        liststats (aplist, daplist, N, &apstats);
     480        catalog[Nc].found[Nsecfilt*j+Nsec] = TRUE;
     481
     482        catalog[Nc].secfilt[Nsecfilt*j+Nsec].Mstdev = 1000.0*stats.sigma; // Mstdev is in millimags (not enough space for more precision)
     483        catalog[Nc].secfilt[Nsecfilt*j+Nsec].Ncode = Ncode;
     484        catalog[Nc].secfilt[Nsecfilt*j+Nsec].Nused = stats.Nmeas;
     485
     486        catalog[Nc].secfilt[Nsecfilt*j+Nsec].M_80 = 1000 * stats.Upper80;
     487        catalog[Nc].secfilt[Nsecfilt*j+Nsec].M_20 = 1000 * stats.Lower20;
     488
     489        // NOTE: for 2MASS measurements, Next should be 1, as should N
     490        if ((Next > 0) && (Next > 0.5*N)) {
     491          catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_OBJ_EXT;
     492        }
     493
     494        switch (pass) {
     495          case 0:
     496            catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_0;
     497            GoodPS1 = TRUE;
     498            break;
     499          case 1:
     500            catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_1;
     501            GoodPS1 = TRUE;
     502            break;
     503          case 2:
     504            catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_2;
     505            GoodPS1 = TRUE;
     506            break;
     507          case 3:
     508            catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_3;
     509            break;
     510          case 4:
     511            catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_4;
     512            break;
     513        }
     514        if (haveSynth) {
     515          catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_USE_SYNTH;
     516        }       
     517        if (haveUbercal) {
     518          catalog[Nc].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_USE_UBERCAL;
     519        }       
     520      }
     521    }
     522
     523    if (isSetMrelFinal) {
     524      DVOAverageFlags flagBits = ID_OBJ_EXT | ID_OBJ_EXT_ALT | ID_OBJ_GOOD | ID_OBJ_GOOD_ALT;
     525
     526      // we attempt to set a few flags here; reset those bits before trying:
     527      catalog[Nc].average[j].flags &= ~flagBits;
     528
     529      if (NextPS1 && (NextPS1 > NpsfPS1)) {
     530        catalog[Nc].average[j].flags |= ID_OBJ_EXT;
     531      }
     532      if (GoodPS1) {
     533        catalog[Nc].average[j].flags |= ID_OBJ_GOOD;
     534      }
     535      if (Galaxy2MASS) {
     536        catalog[Nc].average[j].flags |= ID_OBJ_EXT_ALT;
     537      }
     538      if (Good2MASS) {
     539        catalog[Nc].average[j].flags |= ID_OBJ_GOOD_ALT;
     540      }
     541    }
     542  }
    247543  return (TRUE);
    248 }
    249 
    250 int setMrel_threaded (Catalog *catalog, int Ncatalog, FlatCorrectionTable *flatcorr) {
    251 
    252   int i;
    253 
    254   SetMrelInfo summary;
    255   SetMrelInfoInit (&summary, FALSE);
    256 
    257   pthread_attr_t attr;
    258   pthread_attr_init (&attr);
    259   pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED);
    260  
    261   pthread_t *threads;
    262   ALLOCATE (threads, pthread_t, NTHREADS);
    263 
    264   ThreadInfo *threadinfo;
    265   ALLOCATE (threadinfo, ThreadInfo, NTHREADS);
    266 
    267   // launch N worker threads
    268   for (i = 0; i < NTHREADS; i++) {
    269     threadinfo[i].entry = i;
    270     threadinfo[i].state = THREAD_RUN;
    271     threadinfo[i].catalog  =  catalog;
    272     threadinfo[i].Ncatalog = Ncatalog;
    273     threadinfo[i].flatcorr = flatcorr;
    274     SetMrelInfoInit (&threadinfo[i].summary, FALSE);
    275     pthread_create (&threads[i], NULL, setMrel_worker, &threadinfo[i]);
    276   }
    277   pthread_attr_destroy (&attr);
    278 
    279   // wait until all threads have finished
    280   while (1) {
    281     int allDone = TRUE;
    282     for (i = 0; i < NTHREADS; i++) {
    283       if (threadinfo[i].state == THREAD_RUN) allDone = FALSE;
    284     }
    285     if (allDone) {
    286       break;
    287     }
    288     usleep (500000);
    289   }
    290 
    291   // all threads are done, free the threads array and grab the info
    292   free (threads);
    293  
    294   // report stats & summary from the threads
    295   for (i = 0; i < NTHREADS; i++) {
    296     fprintf (stderr, "setMrel thread %d : %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n",
    297              i,
    298              threadinfo[i].summary.Nfew,
    299              threadinfo[i].summary.Nbad,
    300              threadinfo[i].summary.Ncal,
    301              threadinfo[i].summary.Nmos,
    302              threadinfo[i].summary.Ngrid,
    303              threadinfo[i].summary.Nsys);
    304     SetMrelInfoAccum (&summary, &threadinfo[i].summary);
    305   }
    306   fprintf (stderr, "total : %d stars marked having too few measurements (Nbad: %d, Ncal: %d, Nmos: %d, Ngrid: %d, Nsys: %d)\n", summary.Nfew, summary.Nbad, summary.Ncal, summary.Nmos, summary.Ngrid, summary.Nsys);
    307   free (threadinfo);
    308 
    309   return TRUE;
    310 }
    311 
    312 void *setMrel_worker (void *data) {
    313 
    314   ThreadInfo *threadinfo = data;
    315 
    316   int Nsecfilt = GetPhotcodeNsecfilt ();
    317 
    318   SetMrelInfo results;
    319   SetMrelInfoInit (&results, TRUE); // allocate list, dlist arrays here
    320 
    321   while (1) {
    322 
    323     off_t i = getNextCatalogForThread(threadinfo->Ncatalog);
    324     if (i == -1) {
    325       threadinfo->state = THREAD_DONE;
    326       return NULL;
    327     }
    328 
    329     Catalog *catalog = threadinfo->catalog;
    330     FlatCorrectionTable *flatcorr = threadinfo->flatcorr;
    331 
    332     setMrel_catalog (catalog, i, flatcorr, &results, Nsecfilt);
    333     SetMrelInfoAccum (&threadinfo->summary, &results);
    334   }
    335 
    336   SetMrelInfoFree (&results);
    337   return NULL;
    338544}
    339545
     
    357563}
    358564
    359 # define MARK_SKIP_MEAS \
    360   catalog[i].measureT[m].dbFlags |= ID_MEAS_SKIP_PHOTOM; \
    361   catalog[i].measure [m].dbFlags |= ID_MEAS_SKIP_PHOTOM;
    362 
    363 // setMrel and setMrelOutput are extremely similar, but have slightly different implications:
    364 // * setMrel uses the internal Tiny structures only
    365 // * setMrelOutput skips stars for which there are too few good measurements
    366 // * setMrelOutput is meant to be called repeatedly, relaxing the criteria for 'good' on each pass
    367 
    368 // setMave is also similar to the above.  but:
    369 // * setMave is called by relphot -update-objects
    370 // * setMave excludes all detections with (PSF_QF < 0.85), setMrelOutput allows these for PASS > 2
    371 // * setMave updates 2MASS average flags
    372 // * setMave updates average EXT flags (PS1 and 2MASS)
    373 int setMrelOutput (Catalog *catalog, int Ncatalog, int pass, FlatCorrectionTable *flatcorr) {
    374 
    375   int i, N;
    376   off_t j, k, m, Nmax;
    377   float Msys, Mcal, Mmos, Mgrid, Map;
    378   double *list, *dlist, *aplist, *daplist;
    379   StatType stats, apstats;
    380   int Nsec;
    381 
    382   int Nsecfilt = GetPhotcodeNsecfilt ();
    383 
    384   /* Nmeasure is now different, need to reallocate */
    385   Nmax = 0;
    386   for (i = 0; i < Ncatalog; i++) {
    387     for (j = 0; j < catalog[i].Naverage; j++) {
    388       Nmax = MAX (Nmax, catalog[i].averageT[j].Nmeasure);
    389     }
    390   }
    391   ALLOCATE (list, double, MAX (1, Nmax));
    392   ALLOCATE (dlist, double, MAX (1, Nmax));
    393 
    394   ALLOCATE (aplist, double, MAX (1, Nmax));
    395   ALLOCATE (daplist, double, MAX (1, Nmax));
    396 
    397   for (i = 0; i < Ncatalog; i++) {
    398     for (j = 0; j < catalog[i].Naverage; j++) {
    399 
    400       if (FALSE && (catalog[i].average[j].objID == 0x46a4) && (catalog[i].average[j].catID == 0xf40e)) {
    401         fprintf (stderr, "test obj\n");
    402         print_measure_set (&catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt], catalog[i].measure);
    403       }
    404 
    405       int minUbercalDist = 1000;
    406 
    407       int Ns;
    408       for (Ns = 0; Ns < Nphotcodes; Ns++) {
    409 
    410         int thisCode = photcodes[Ns][0].code;
    411         Nsec = GetPhotcodeNsec(thisCode);
    412 
    413         /* star/photcodes already calibrated */
    414         if (catalog[i].found[Nsecfilt*j+Nsec]) continue; 
    415 
    416         int Ncode = 0;
    417         int Next = 0;
    418         int haveSynth = FALSE;
    419         int haveUbercal = FALSE;
    420         int isRefPhot = FALSE;
    421 
    422         N = 0;
    423         m = catalog[i].averageT[j].measureOffset;
    424         for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
    425 
    426           // skip measurements that do not match the current photcode
    427           PhotCode *code = GetPhotcodebyCode (catalog[i].measureT[m].photcode);
    428           if (!code) continue;
    429           if (code->equiv != thisCode) { continue; }
    430           Ncode ++;
    431 
    432           if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
    433 
    434           if (getImageEntry (m, i) < 0) {
    435           XXXX : likely problem -- compare with setMrel_catalog;
    436             // these detetions have no image (eg, ref values such as 2MASS)
    437             Mcal = Mmos = Mgrid = 0;
    438             isRefPhot = TRUE;
    439           } else {
    440             Mcal  = getMcal  (m, i, flatcorr, catalog);
    441             if (isnan(Mcal)) {
    442               MARK_SKIP_MEAS;
    443               continue;
    444             }
    445             Mmos  = getMmos  (m, i);
    446             if (isnan(Mmos)) {
    447               MARK_SKIP_MEAS;
    448               continue;
    449             }
    450             Mgrid = getMgrid (m, i);
    451             if (isnan(Mgrid)) {
    452               MARK_SKIP_MEAS;
    453               continue;
    454             }
    455           }
    456 
    457           Msys = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]);
    458           if (isnan(Msys)) {
    459             if (!isRefPhot) {
    460               MARK_SKIP_MEAS;
    461             }
    462             continue;
    463           }
    464           list[N] = Msys - Mcal - Mmos - Mgrid;
    465 
    466           Map = PhotAper (&catalog[i].measure[m]);
    467           aplist[N] = Map - Mcal - Mmos - Mgrid;
    468 
    469           int myUbercalDist = getUbercalDist(m,i);
    470           minUbercalDist = MIN(minUbercalDist, myUbercalDist);
    471 
    472           // count the extended detections
    473           if ((catalog[i].measure[m].photcode >= 10000) && (catalog[i].measure[m].photcode <= 10500)) {
    474             if (!isnan(catalog[i].measure[m].Map)) {
    475               if (catalog[i].measure[m].M - catalog[i].measure[m].Map > 0.5) {
    476                 Next ++;
    477               }
    478             }
    479           }
    480           // count extended detections for 2MASS (XXX NOTE hardwired photcodes 2011, 2012, 2013)
    481           if ((catalog[i].measure[m].photcode >= 2011) && (catalog[i].measure[m].photcode <= 2013)) {
    482             if (catalog[i].measure[m].photFlags & 0x00c00000) {
    483               Next ++;
    484             }
    485             if ((pass == 0) && !(catalog[i].measure[m].photFlags & 0x00000007)) {
    486               // detections without one of these bits should only be used in PASS_1
    487               MARK_SKIP_MEAS;
    488               continue;
    489             }
    490           }
    491           // ignore SYNTH photocdes until PASS == 4 (where we also accept saturated stars)
    492           if ((catalog[i].measure[m].photcode >= 3001) && (catalog[i].measure[m].photcode <= 3005)) {
    493             if (pass < 4) {
    494               MARK_SKIP_MEAS;
    495               continue;
    496             }
    497             haveSynth = TRUE;
    498           }
    499 
    500           // dlist gives the error, which is used as the weight in WT_MEAN.
    501           // we can modify the resulting weight in a few ways:
    502           // 1) MIN_ERROR guarantees a floor
    503           // 2) photomErrSys is added in quadrature as a sytematic error, set per photcode
    504           // 3) UBERCAL measurements can have their weight increased by a big factor to help tie down the averages
    505           // 4) some reference photcode of some kind can be specified as fixed and have a high weight
    506           dlist[N] = MAX (hypot(catalog[i].measureT[m].dM, code->photomErrSys), MIN_ERROR);
    507 
    508           // up-weight the ubercal values (or convergence can take a long time...) (XXX make this optional?)
    509           if (catalog[i].measureT[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
    510             dlist[N] = MAX (0.1*catalog[i].measureT[m].dM, MIN_ERROR);
    511             haveUbercal = TRUE;
    512           }
    513 
    514           // tie down reference photometry if the -refcode (code) option is selected
    515           // eg, -refcode g_SDSS
    516           // this probably makes no sense in the context of multifilter analysis
    517           if (refPhotcode) {
    518             if (code->code == refPhotcode->code) {
    519               // tiny error -> large weight
    520               // dlist[N] = MAX (0.01*catalog[i].measureT[m].dM, MIN_ERROR);
    521               dlist[N] = 0.0001;
    522             }
    523           }
    524           N++;
    525         }
    526         if (N < 1) continue;
    527 
    528         for (m = 0; m < N; m++) {
    529           daplist[m] = dlist[m];
    530         }
    531 
    532         // XXX force WT_MEAN or MEAN here?
    533         liststats (list, dlist, N, &stats);
    534         liststats (aplist, daplist, N, &apstats);
    535         catalog[i].found[Nsecfilt*j+Nsec] = TRUE;
    536         switch (pass) {
    537           case 0:
    538             catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_0;
    539             break;
    540           case 1:
    541             catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_1;
    542             break;
    543           case 2:
    544             catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_2;
    545             break;
    546           case 3:
    547             catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_3;
    548             break;
    549           case 4:
    550             catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_PHOTOM_PASS_4;
    551             break;
    552         }
    553 
    554         if (haveSynth) {
    555           catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_USE_SYNTH;
    556         }       
    557         if (haveUbercal) {
    558           catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_USE_UBERCAL;
    559         }       
    560 
    561         /* use sigma or error in dM for output? */
    562         catalog[i].secfilt[Nsecfilt*j+Nsec].M  = stats.mean;
    563         catalog[i].secfilt[Nsecfilt*j+Nsec].Map = apstats.mean;
    564         catalog[i].secfilt[Nsecfilt*j+Nsec].dM = stats.error;
    565         catalog[i].secfilt[Nsecfilt*j+Nsec].Mstdev = 1000.0*stats.sigma; // Mstdev is in millimags (not enough space for more precision)
    566         catalog[i].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
    567         catalog[i].secfilt[Nsecfilt*j+Nsec].Ncode = Ncode;
    568         catalog[i].secfilt[Nsecfilt*j+Nsec].Nused = stats.Nmeas;
    569 
    570         catalog[i].secfilt[Nsecfilt*j+Nsec].M_80 = 1000 * stats.Upper80;
    571         catalog[i].secfilt[Nsecfilt*j+Nsec].M_20 = 1000 * stats.Lower20;
    572         catalog[i].secfilt[Nsecfilt*j+Nsec].ubercalDist = minUbercalDist;
    573 
    574         if ((Next > 0) && (Next > 0.5*N)) {
    575           catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_SECF_OBJ_EXT;
    576         }
    577       }
    578     }
    579   }
    580 
    581   free (list);
    582   free (dlist);
    583 
    584   free (aplist);
    585   free (daplist);
    586   return (TRUE);
    587 }
     565# if (0)
    588566
    589567/* grab Nsec for named photcode */
     
    776754  return (TRUE);
    777755}
     756# endif
    778757
    779758/* set measure.Mcal for all measures except ID_MEAS_NOCAL and ID_IMAGE_PHOTOM_NOCAL */
     
    800779        if (isnan(Mgrid)) continue;
    801780
    802         // note that measurements for which the image is not selected will not up modified
     781        // note that measurements for which the image is not selected will not be modified
    803782        // (that is a good thing! -- we keep a prior calibration)
    804783
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/initialize.c

    r33651 r33823  
    1212    PhotcodeList = strcreate (argv[1]);
    1313    photcodes = ParsePhotcodeList (PhotcodeList, &Nphotcodes, TRUE); // require SEC photcodes
    14   }
     14  } else {
     15    char tmpline1[256];
     16    int Ns;
     17    Nphotcodes = GetPhotcodeNsecfilt ();
     18    ALLOCATE (photcodes, PhotCode *, Nphotcodes);
     19    ALLOCATE (PhotcodeList, char, 256);
     20    for (Ns = 0; Ns < Nphotcodes; Ns++) {
     21      photcodes[Ns] = GetPhotcodebyNsec (Ns);
     22      if (Ns > 0) {
     23        snprintf (tmpline1, 256, "%s,%s", PhotcodeList, photcodes[Ns][0].name);
     24      } else {
     25        snprintf (tmpline1, 256, "%s", photcodes[Ns][0].name);
     26      }
     27      strcpy (PhotcodeList, tmpline1);
     28    }
     29  }   
     30   
    1531  if (USE_GRID && (Nphotcodes > 1)) {
    1632    fprintf (stderr, "grid correction analysis currently can only operate on a single photcode\n");
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/reload_catalogs.c

    r33651 r33823  
    8686    TIMESTAMP(time4);
    8787
    88     setMrelFinal (&catalog, flatcorr);
     88    initMrel (&catalog, 1);
     89    setMrelFinal (&catalog, flatcorr, FALSE);
    8990    TIMESTAMP(time5);
    9091
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/relphot_objects.c

    r33736 r33823  
    9090    }
    9191
    92     setMave (&catalog, 1);
     92    populate_tiny_values(&catalog, DVO_TV_MEASURE | DVO_TV_AVERAGE);
     93
     94    initMrel (&catalog, 1);
     95    setMrelFinal (&catalog, NULL, TRUE);
    9396
    9497    if (!UPDATE) {
    9598      dvo_catalog_unlock (&catalog);
     99      free_tiny_values(&catalog);
    96100      dvo_catalog_free (&catalog);
    97101      continue;
     
    105109    dvo_catalog_save (&catalog, VERBOSE);
    106110    dvo_catalog_unlock (&catalog);
     111    free_tiny_values(&catalog);
    107112    dvo_catalog_free (&catalog);
    108113  }
  • branches/eam_branches/ipp-20120405/Ohana/src/relphot/src/setMrelFinal.c

    r33807 r33823  
    55// output dbFlags values
    66
    7 void setMrelFinal (Catalog *catalog, FlatCorrectionTable *flatcorr) {
     7void setMrelFinal (Catalog *catalog, FlatCorrectionTable *flatcorr, int simpleAverage) {
    88
    99  off_t i;
     
    9494  }
    9595
    96   clean_measures (catalog, 1, TRUE, flatcorr);    /* mark outliers ID_MEAS_POOR_PHOTOM */
     96  // XXX make this optional? (do not clean for -averages?)
     97  if (!simpleAverage) clean_measures (catalog, 1, TRUE, flatcorr);    /* mark outliers ID_MEAS_POOR_PHOTOM */
    9798  for (i = 0; i < 5; i++) {
    9899    skip_measurements (catalog, i, flatcorr);       /* set ID_MEAS_SKIP for measures to be skipped */
    99100    setMrelOutput  (catalog, 1, i, flatcorr);    /* set Mrel using remaining measures */
    100101  }
    101   setMcalOutput (catalog, 1, flatcorr);
     102  if (!simpleAverage) setMcalOutput (catalog, 1, flatcorr);
    102103
    103104  /* clear ID_STAR_POOR, ID_STAR_FEW values before writing ??? */
     
    258259    }
    259260  }
    260   if (VERBOSE2) fprintf (stderr, "pass %d, Ntot: "OFF_T_FMT", Ntry: "OFF_T_FMT", Nskip: "OFF_T_FMT", Nkeep: "OFF_T_FMT"\n",
     261  if (VERBOSE) fprintf (stderr, "pass %d, Ntot: "OFF_T_FMT", Ntry: "OFF_T_FMT", Nskip: "OFF_T_FMT", Nkeep: "OFF_T_FMT"\n",
    261262                        pass, Ntot, Ntry, Nskip, Nkeep);
    262263}
Note: See TracChangeset for help on using the changeset viewer.