IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 31450


Ignore:
Timestamp:
May 5, 2011, 10:43:45 AM (15 years ago)
Author:
eugene
Message:

various improvements to relphot, related support

Location:
trunk/Ohana/src
Files:
30 edited
3 copied

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/dvomerge/src/dvo_image_merge_dbs.c

    r29938 r31450  
    202202    if (newID == 0) {
    203203      fprintf (stderr, "cannot find image ID "OFF_T_FMT"\n",  oldID);
    204       exit (2);
     204      // exit (2);
    205205    }
    206206    catalog[0].measure[i].imageID = newID;
  • trunk/Ohana/src/dvomerge/src/dvoverify.c

    r31160 r31450  
    148148int VerifyTableFile (char *filename) {
    149149
    150   int status;
     150  int status, Next;
    151151  off_t Nbytes;
    152152  Header header;
     
    205205  // scan all extentions
    206206  Nbytes = 0;
     207  Next = -1;
    207208  if (DEBUG) fprintf (stderr, "sizes: ("OFF_T_FMT" vs "OFF_T_FMT")\n", Nbytes, fileStats.st_size);
    208209  while (Nbytes < fileStats.st_size) {
     
    210211    // Check on the PHU
    211212    if (!gfits_fread_header (file, &header)) {
    212       fprintf (stderr, "unable to read PHU header for %s\n", filename);
     213      if (Next == -1) {
     214        fprintf (stderr, "unable to read PHU header for %s\n", filename);
     215      } else {
     216        fprintf (stderr, "unable to read header for %s, extension %d (or file has excess bytes)\n", filename, Next);
     217      }
    213218      fclose(file);
    214219      return (FALSE);
     
    243248      }
    244249    }
     250    Next ++;
    245251  }
    246252  if (DEBUG) fprintf (stderr, "file is good: %s\n", filename);
  • trunk/Ohana/src/libdvo/include/dvo.h

    r31160 r31450  
    2020  DVO_FORMAT_PS1_DEV_2,
    2121  DVO_FORMAT_PS1_DEV_3,
     22  DVO_FORMAT_PS1_REF,
    2223  DVO_FORMAT_PS1_V1,
    2324  DVO_FORMAT_PS1_V2,
    24   DVO_FORMAT_PS1_REF
    2525} DVOTableFormat;
    2626
     
    210210} PhotCodeData;
    211211
     212// a reduced-subset structure for relphot
     213typedef struct {
     214  double         R;
     215  double         D;
     216  unsigned short Nmeasure;
     217  int            measureOffset;
     218  uint32_t       flags;
     219} AverageTiny;
     220
     221// a reduced-subset structure for relphot
     222typedef struct {
     223  float          dR;
     224  float          dD;
     225  float          M;
     226  float          Mcal;
     227  float          dM;
     228  float          airmass;
     229  float          Xccd;
     230  float          Yccd;
     231  float          dt;
     232  int            t;
     233  unsigned int   averef;
     234  unsigned int   imageID;
     235  unsigned int   dbFlags;
     236  unsigned short photcode;
     237} MeasureTiny;
     238
    212239/* a catalog contains this data */
    213240typedef struct Catalog {
     
    226253  off_t Naves_disk, Nmeas_disk, Nmiss_disk, Nsecf_disk; /* current number of each component on disk */
    227254  off_t Naves_off,  Nmeas_off,  Nmiss_off,  Nsecf_off;  /* index of first loaded data value */
     255
     256  // note that we use these for the full-sky relphot analysis
     257  AverageTiny *averageT;
     258  MeasureTiny *measureT;
    228259
    229260  /* the Nsecf_* values above are number of table rows (eg, Naverage*Nsecfilt) */
     
    330361int PhotColor (Average *average, SecFilt *secfilt, Measure *measure, int c1, int c2, double *color);
    331362
     363float PhotInstTiny (MeasureTiny *measure);
     364float PhotCatTiny (MeasureTiny *measure);
     365float PhotAperTiny (MeasureTiny *measure);
     366float PhotSysTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt);
     367float PhotRelTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt);
     368float PhotCalTiny (MeasureTiny *thisone, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code);
     369float PhotAveTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt);
     370float PhotRefTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure);
     371float PhotXmTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt);
     372float PhotdMTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt);
     373
     374float PhotColorForCodeTiny (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code);
     375int PhotColorTiny (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, int c1, int c2, double *color);
     376
     377
    332378PhotCodeData *GetPhotcodeTable (void);
    333379void SetPhotcodeTable (PhotCodeData *);
  • trunk/Ohana/src/libdvo/src/dvo_catalog_split.c

    r29938 r31450  
    448448    catalog[0].measure = FtableToMeasure (&ftable, &Nmeasure, &catalog[0].catformat);
    449449    if (Nmeasure != Nrows) {
    450       fprintf (stderr, "Warning: mismatch between Nmeasure in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmeasure,  Nrows);
     450      // XXX this condition denotes the eof has been reached; not an error or a warning
     451      // fprintf (stderr, "Warning: mismatch between Nmeasure in PHU and Table headers ("OFF_T_FMT" vs "OFF_T_FMT")\n",  Nmeasure,  Nrows);
    451452    }
    452453    gfits_free_header (&header);
  • trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c

    r29938 r31450  
    536536  return (TRUE);
    537537}
     538
     539/******** alternate photometry conversion functions using MeasureTiny and AverageTiny *********/
     540float PhotInstTiny (MeasureTiny *measure) {
     541
     542  int Np;
     543  float M;
     544
     545  Np = photcodes[0].hashcode[measure[0].photcode];
     546  if (Np == -1) return (NAN);
     547
     548  if (photcodes[0].code[Np].type == PHOT_REF) {
     549    M = measure[0].M;
     550    return (M);
     551  }
     552
     553  M = measure[0].M - measure[0].dt - ZERO_POINT;
     554         
     555  return (M);
     556
     557}
     558
     559float PhotCatTiny (MeasureTiny *measure) {
     560
     561  int Np;
     562  float Mcat;
     563  PhotCode *code;
     564
     565  Np = photcodes[0].hashcode[measure[0].photcode];
     566  if (Np == -1) return (NAN);
     567
     568  if (photcodes[0].code[Np].type == PHOT_REF) {
     569    Mcat = measure[0].M;
     570    return (Mcat);
     571  }
     572  code = &photcodes[0].code[Np];
     573  Mcat = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
     574 
     575  return (Mcat);
     576}
     577
     578# if (0)
     579float PhotAperTiny (MeasureTiny *measure) {
     580
     581  int Np;
     582  float Mcat;
     583  PhotCode *code;
     584
     585  Np = photcodes[0].hashcode[measure[0].photcode];
     586  if (Np == -1) return (NAN);
     587
     588  if (photcodes[0].code[Np].type == PHOT_REF) {
     589    Mcat = measure[0].Map;
     590    return (Mcat);
     591  }
     592  code = &photcodes[0].code[Np];
     593  Mcat = measure[0].Map - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
     594 
     595  return (Mcat);
     596}
     597# endif
     598
     599float PhotSysTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt) {
     600
     601  int i, Np;
     602  float Mcat, Mcol, Msys, mc, Mc;
     603  PhotCode *code;
     604
     605  Np = photcodes[0].hashcode[measure[0].photcode];
     606  if (Np == -1) return (NAN);
     607
     608  if (photcodes[0].code[Np].type == PHOT_REF) {
     609    Msys = measure[0].M;
     610    return (Msys);
     611  }
     612  code = &photcodes[0].code[Np];
     613  Mcat = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
     614
     615  /* for DEP, color must be made of PRI/SEC */
     616  mc = PhotColorForCodeTiny (average, secfilt, NULL, code);
     617  if (isnan(mc)) return (Mcat);
     618  mc = mc - SCALE*code[0].dX;
     619
     620  Mc = mc;
     621  Mcol = 0;
     622  for (i = 0; i < code[0].Nc; i++) {
     623    Mcol += code[0].X[i]*Mc;
     624    Mc *= mc;
     625  }
     626  Msys = Mcat + Mcol;
     627  return (Msys);
     628}
     629
     630float PhotRelTiny (MeasureTiny *measure, AverageTiny *average, SecFilt *secfilt) {
     631
     632  int i, Np;
     633  float Mcat, Mcol, Mrel, mc, Mc;
     634  PhotCode *code;
     635
     636  Np = photcodes[0].hashcode[measure[0].photcode];
     637  if (Np == -1) return (NAN);
     638
     639  if (photcodes[0].code[Np].type == PHOT_REF) {
     640    Mcat = measure[0].M;
     641    return (Mcat);
     642  }
     643  code = &photcodes[0].code[Np];
     644  Mrel = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C - measure[0].Mcal;
     645
     646  /* for DEP, color must be made of PRI/SEC */
     647  mc = PhotColorForCodeTiny (average, secfilt, NULL, code);
     648  if (isnan(mc)) return (Mrel);
     649  mc = mc - SCALE*code[0].dX;
     650
     651  Mc = mc;
     652  Mcol = 0;
     653  for (i = 0; i < code[0].Nc; i++) {
     654    Mcol += code[0].X[i]*Mc;
     655    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
     656  }
     657  Mrel += Mcol;
     658  return (Mrel);
     659}
     660
     661/* return calibrated magnitude from measure for given photcode */
     662float PhotCalTiny (MeasureTiny *thisone, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code) {
     663
     664  int i, Np;
     665  float Mcal, Mrel, Mcol, mc, Mc;
     666
     667  if (code == NULL) return NAN;
     668
     669  /* code must be the matching PRI/SEC code for this measurement or an equivalent ALT */
     670  Np = photcodes[0].hashcode[thisone[0].photcode];
     671  if (Np == -1) return (NAN);
     672
     673  if (photcodes[0].code[Np].type == PHOT_REF) {
     674    Mrel = thisone[0].M;
     675    return (Mrel);
     676  }
     677  if (code[0].code != photcodes[0].code[Np].equiv) return (NAN);
     678
     679  Mcal = PhotRelTiny (thisone, average, secfilt) + SCALE*code[0].C;
     680
     681  mc = PhotColorForCodeTiny (average, secfilt, measure, code);
     682  if (isnan(mc)) return (Mcal);
     683  mc = mc - SCALE*code[0].dX;
     684
     685  Mc = mc;
     686  Mcol = 0;
     687  for (i = 0; i < code[0].Nc; i++) {
     688    Mcol += code[0].X[i]*Mc;
     689    Mc *= mc;
     690  }
     691  Mcal += Mcol;
     692  return (Mcal);
     693}
     694
     695/* color term may not use DEP magnitude */
     696float PhotColorForCodeTiny (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, PhotCode *code) {
     697
     698  int i, Ns1, Ns2, Ns;
     699  float m1, m2, mc;
     700  PhotCode *color;
     701
     702  m1 = m2 = NAN;
     703
     704  if (measure == NULL) {
     705    Ns1 = photcodes[0].hashNsec[code[0].c1];
     706    Ns2 = photcodes[0].hashNsec[code[0].c2];
     707 
     708    m1 = (Ns1 == -1) ? NAN : secfilt[Ns1].M;
     709    m2 = (Ns2 == -1) ? NAN : secfilt[Ns2].M;
     710    mc = (isnan(m1) || isnan(m2)) ? NAN : (m1 - m2);
     711    return (mc);
     712  }
     713
     714  /* find magnitude matching first color term */
     715  color = GetPhotcodebyCode (code[0].c1);
     716  if (color == NULL) return (NAN);
     717  if (color[0].type == PHOT_REF) {
     718    for (i = 0; (i < average[0].Nmeasure) && (isnan(m1)); i++) {
     719      if (measure[i].photcode == color[0].code) {
     720        m1 = measure[i].M;
     721      }
     722    }
     723  } else {
     724    Ns = photcodes[0].hashNsec[color[0].code];
     725    m1 = (Ns == -1) ? NAN : secfilt[Ns].M;
     726  }     
     727
     728  /* find magnitude matching second color term */
     729  color = GetPhotcodebyCode (code[0].c2);
     730  if (color == NULL) return (NAN);
     731  if (color[0].type == PHOT_REF) {
     732    for (i = 0; (i < average[0].Nmeasure) && (isnan(m2)); i++) {
     733      if (measure[i].photcode == color[0].code) {
     734        m2 = measure[i].M;
     735      }
     736    }
     737  } else {
     738    Ns = photcodes[0].hashNsec[color[0].code];
     739    m2 = (Ns == -1) ? NAN : secfilt[Ns].M;
     740  }     
     741  mc = (isnan(m1) || isnan(m2)) ? NAN : (m1 - m2);
     742  return (mc);
     743}
     744
     745/* return calibrated magnitude from average/secfilt for given photcode */
     746float PhotRefTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure) {
     747
     748  int i, Ns;
     749  float Mave, Mref, Mcol, mc;
     750  double Mc;
     751
     752  if (code == NULL) return NAN;
     753
     754  Ns = photcodes[0].hashNsec[code[0].code];
     755  Mave = (Ns == -1) ? NAN : secfilt[Ns].M;
     756  Mref = Mave + SCALE*code[0].C;
     757
     758  mc = PhotColorForCodeTiny (average, secfilt, measure, code);
     759  if (isnan(mc)) return (Mref);
     760  mc = mc - SCALE*code[0].dX;
     761
     762  Mc = mc;
     763  Mcol = 0;
     764  for (i = 0; i < code[0].Nc; i++) {
     765    Mcol += code[0].X[i]*Mc;
     766    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
     767  }
     768  Mref += Mcol;
     769  return (Mref);
     770}
     771
     772/***/
     773float PhotAveTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt) {
     774
     775  int Ns;
     776  float Mave;
     777
     778  if (code == NULL) return NAN;
     779
     780  Ns = photcodes[0].hashNsec[code[0].code];
     781  Mave = (Ns == -1) ? NAN : secfilt[Ns].M;
     782  return (Mave);
     783}
     784
     785float PhotdMTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt) {
     786
     787  int Ns;
     788  float dM;
     789
     790  if (code == NULL) return NAN;
     791
     792  Ns = photcodes[0].hashNsec[code[0].code];
     793  dM  = (Ns == -1) ? NAN : secfilt[Ns].dM;
     794  return (dM);
     795}
     796
     797// XXX return NAN or NAN_S_SHORT? (secfilt->Xm is short)
     798float PhotXmTiny (PhotCode *code, AverageTiny *average, SecFilt *secfilt) {
     799
     800  int Ns;
     801  short Mi;
     802  float Xm;
     803
     804  if (code == NULL) return NAN;
     805
     806  Ns = photcodes[0].hashNsec[code[0].code];
     807  Mi = (Ns == -1) ? NAN : secfilt[Ns].Xm;
     808  Xm = (isnan(Mi)) ? -1.0 : pow (10.0, 0.01*Mi);
     809  return (Xm);
     810}
     811
     812/* given a photcode pair c1 & c2, return the color of this star (NaN if not found) */
     813int PhotColorTiny (AverageTiny *average, SecFilt *secfilt, MeasureTiny *measure, int c1, int c2, double *color) {
     814
     815  int i, Ns;
     816  double M1, M2, dM;
     817  PhotCode *code;
     818 
     819  code = GetPhotcodebyCode (c1);
     820  if (code == NULL) return (FALSE);
     821  if (code[0].type == PHOT_REF) {
     822    for (i = 0; i < average[0].Nmeasure; i++) {
     823      if (measure[i].photcode == c1) {
     824        M1 = measure[i].M;
     825        goto filter1;
     826      }
     827    }   
     828    return (FALSE);
     829  } else {
     830    Ns = photcodes[0].hashNsec[code[0].code];
     831    M1 = (Ns == -1) ? NAN : secfilt[Ns].M;
     832  }     
     833
     834filter1:
     835  code = GetPhotcodebyCode (c2);
     836  if (code == NULL) return (FALSE);
     837  if (code[0].type == PHOT_REF) {
     838    for (i = 0; i < average[0].Nmeasure; i++) {
     839      if (measure[i].photcode == c2) {
     840        M2 = measure[i].M;
     841        goto filter2;
     842      }
     843    }   
     844    return (FALSE);
     845  } else {
     846    Ns = photcodes[0].hashNsec[code[0].code];
     847    M2 = (Ns == -1) ? NAN : secfilt[Ns].M;
     848  }     
     849 
     850filter2:
     851
     852  dM = M1 - M2;
     853  *color = dM;
     854 
     855  return (TRUE);
     856}
     857/***********************************************/
    538858
    539859// Create a map between the secfilt values from one photcode table to another
  • trunk/Ohana/src/opihi/cmd.data/Makefile

    r29938 r31450  
    141141$(SRC)/vpop.$(ARCH).o              \
    142142$(SRC)/vroll.$(ARCH).o             \
     143$(SRC)/vshift.$(ARCH).o            \
    143144$(SRC)/vsmooth.$(ARCH).o           \
    144145$(SRC)/vstats.$(ARCH).o            \
  • trunk/Ohana/src/opihi/cmd.data/gridify.c

    r20936 r31450  
    44
    55  int i, Nx, Ny, Xb, Yb, Normalize, N;
    6   float Xmin, Xmax, dX, Ymin, Ymax, dY;
     6  float Xmin, Xmax, dX, Ymin, Ymax, dY, initValue;
    77  float *buf, *val;
    88  int *Nval;
     
    1515    remove_argument (N, &argc, argv);
    1616    Normalize = FALSE;
     17  }
     18
     19  initValue = 0.0;
     20  if ((N = get_argument (argc, argv, "-init-value"))) {
     21    remove_argument (N, &argc, argv);
     22    initValue = atof(argv[N]);
     23    remove_argument (N, &argc, argv);
    1724  }
    1825
     
    6976  buf = (float *) bf[0].matrix.buffer;
    7077  for (i = 0; i < Nx*Ny; i++) {
     78    buf[i] = initValue;
    7179    if (Normalize) {
    7280      if (Nval[i] == 0) {
    73         buf[i] = 0;
    7481        continue;
    7582      }
  • trunk/Ohana/src/opihi/cmd.data/init.c

    r29938 r31450  
    130130int vstats           PROTO((int, char **));
    131131int vroll            PROTO((int, char **));
     132int vshift           PROTO((int, char **));
    132133int vpop             PROTO((int, char **));
    133134int vsmooth          PROTO((int, char **));
     
    274275  {1, "vmaxwell",     vmaxwell,         "fit a Maxwellian to a vector"},
    275276  {1, "vpop",         vpop,             "remove first element of a vector"},
    276   {1, "vroll",        vroll,            "roll vector elements"},
     277  {1, "vroll",        vroll,            "roll vector elements by 1 entry"},
     278  {1, "vshift",       vshift,           "shift vector elements by arbitrary amount"},
    277279  {1, "vsmooth",      vsmooth,          "Gaussian smooth of a vector"},
    278280  {1, "vstats",       vstats,           "statistics on a vector"},
  • trunk/Ohana/src/opihi/dvo/Makefile

    r27594 r31450  
    8080$(SRC)/mextract.$(ARCH).o               \
    8181$(SRC)/mmextract.$(ARCH).o              \
     82$(SRC)/objectcoverage.$(ARCH).o         \
    8283$(SRC)/photcodes.$(ARCH).o              \
    8384$(SRC)/pmeasure.$(ARCH).o               \
  • trunk/Ohana/src/opihi/dvo/avextract.c

    r30612 r31450  
    55  off_t i, j, n, m;
    66  int N, Npts, NPTS, last, next, state, Nfields, Nreturn, Ncstack, Nstack;
    7   int Nsecfilt, mode, VERBOSE;
     7  int Nsecfilt, mode, VERBOSE, needMeasures;
    88  char **cstack, name[1024];
    99  void *Signal;
     
    101101  }
    102102
     103  // check the requested fields : are all average/secfilt entries, or do we need measures?
     104  needMeasures = FALSE;
     105  for (i = 0; !needMeasures && (i < Nfields); i++) {
     106    if (fields[i].magMode == MAG_NONE) continue;
     107    if (fields[i].photcode == NULL) continue; // assert this?
     108    if (fields[i].photcode[0].type == PHOT_REF) needMeasures = TRUE;
     109    if (fields[i].photcode[0].type == PHOT_DEP) needMeasures = TRUE;
     110  }
     111
    103112  // grab data from all selected sky regions
    104113  Signal = signal (SIGINT, handle_interrupt);
     
    107116    /* lock, load, unlock catalog */
    108117    catalog.filename = skylist[0].filename[i];
    109     catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
     118    catalog.catflags = LOAD_AVES | LOAD_SECF;
     119    if (needMeasures) {
     120      catalog.catflags |= LOAD_MEAS;
     121    }
    110122    catalog.Nsecfilt = 0;
    111123
  • trunk/Ohana/src/opihi/dvo/init.c

    r27594 r31450  
    4040int lightcurve      PROTO((int, char **));
    4141int mextract        PROTO((int, char **));
    42 int mmextract        PROTO((int, char **));
     42int mmextract       PROTO((int, char **));
     43int objectcoverage  PROTO((int, char **));
    4344int pcat            PROTO((int, char **));
    4445int photcodes       PROTO((int, char **));
     
    9293  {1, "mextract",    mextract,     "extract measure data values"},
    9394  {1, "mmextract",   mmextract,    "extract joined measurements"},
     95  {1, "objectcoverage", objectcoverage, "plot catalog boundaries"},
    9496  {1, "pcat",        skycat,       "plot catalog boundaries"},
    9597  {1, "photcodes",   photcodes,    "list photometry codes"},
  • trunk/Ohana/src/photdbc/src/make_subcatalog.c

    r30616 r31450  
    7171    }
    7272
     73    // if the input catalog is an old type, generate the catID entries:
     74    if (catalog[0].catformat < DVO_FORMAT_PS1_V1) {
     75      subcatalog[0].average[Naverage].catID = catalog[0].catID;
     76    }
     77
    7378    minMag   = 32;
    7479    minSigma = 32;
     
    127132      subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
    128133      subcatalog[0].measure[Nmeasure].averef = Naverage;
     134
     135      // if the input catalog is an old type, generate the catID entries:
     136      if (catalog[0].catformat < DVO_FORMAT_PS1_V1) {
     137        subcatalog[0].measure[Nmeasure].catID = catalog[0].catID;
     138      }
     139
    129140      Nmeasure ++;
    130141      Nm ++;
  • trunk/Ohana/src/relastro/src/UpdateObjects.c

    r31160 r31450  
    189189      coords.crval1 = R[0];
    190190      coords.crval2 = D[0];
    191       Tmean /= (float) N;
     191
     192      if (FIT_TARGET == TARGET_HIGH_SPEED) {
     193          Tmean = 0.5*(Tmax - Tmin);
     194      } else {
     195          Tmean /= (float) N;
     196      }
    192197     
    193198      XVERB = FALSE && (catalog[i].measure[m].dM < 0.01) && (N == 6) && (mode == FIT_PM_ONLY);
  • trunk/Ohana/src/relphot/include/relphot.h

    r30616 r31450  
    1111  unsigned int start;
    1212  unsigned int stop;
     13  short photcode;
    1314  float Mcal;
    1415  float dMcal;
     
    8586int    RELPHOT_GRID_BINNING;
    8687
    87 PhotCode      *photcode;
    88 int            PhotNsec;
    89 int            PhotSec;
     88int      *photseclist;
     89int      Nphotcodes;
     90PhotCode **photcodes;
     91// int            PhotSec;
     92// int            PhotNsec;
    9093
    9194PhotCode      *refPhotcode;
     95
     96int MaxDensityUse;
     97double MaxDensityValue;
    9298
    9399int AreaSelect;
     
    213219StatType      statsMosaicX        PROTO((Catalog *catalog));
    214220StatType      statsMosaicdM       PROTO((Catalog *catalog));
    215 StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog));
    216 StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog));
    217 StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog));
     221StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog, int Nsec, int seccode));
     222StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
     223StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog, int Nsec));
    218224void          wcatalog            PROTO((Catalog *catalog));
    219225void          wimages             PROTO((void));
     
    223229void relphot_usage (void);
    224230void relphot_help (int argc, char **argv);
     231
     232off_t getImageByID (off_t ID);
     233
     234int rationalize_mosaics ();
     235int LimitDensityCatalog (Catalog *subcatalog, Catalog *catalog);
     236
     237int populate_tiny_values (Catalog *catalog);
     238int free_tiny_values (Catalog *catalog);
  • trunk/Ohana/src/relphot/src/GridOps.c

    r28241 r31450  
    348348  // sums for each star which touches as cell on both bases.
    349349
     350  int Nsecfilt = GetPhotcodeNsecfilt ();
     351  int thisCode = photcodes[0][0].code;
     352  int Nsec = GetPhotcodeNsec(thisCode);
     353
    350354  for (i = 0; i < Ngrid; i++) {
    351355   
     
    362366      mx = mlist[i][j];
    363367      c  = clist[i][j];
    364       n  = catalog[c].measure[mx].averef;
     368      n  = catalog[c].measureT[mx].averef;
    365369     
    366370      // if we have already visited this star, skip the stuff below
     
    369373
    370374      // skip stars marked as BAD
    371       if (catalog[c].average[n].flags & STAR_BAD) {
     375      if (catalog[c].secfilt[n*Nsecfilt+Nsec].flags & STAR_BAD) {
    372376        Nrel ++;
    373377        continue;
     
    389393
    390394        // skip measurements marked as BAD
    391         if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     395        if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
    392396          Nbad ++;
    393397          continue;
     
    410414        // select the color- and airmass-corrected observed magnitude for this star
    411415        // XXX need to be able to turn off the color-correction until initial average mags are found
    412         // Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
    413         Msys = PhotCat (&catalog[c].measure[m]);
     416        Msys = PhotCatTiny (&catalog[c].measureT[m]);
    414417        if (isnan(Msys)) {
    415418          Nsys++;
     
    418421
    419422        // mag-error for this measurement
    420         Merr =  MAX (catalog[c].measure[m].dM, MIN_ERROR);
     423        Merr =  MAX (catalog[c].measureT[m].dM, MIN_ERROR);
    421424
    422425        // Wsys = 1.0 / SQ(Merr);
     
    528531  if (!USE_GRID) return;
    529532
     533  int Nsecfilt = GetPhotcodeNsecfilt ();
     534
    530535  Nmax = Nlist[0];
    531536  for (i = 0; i < Ngrid; i++) {
     
    545550      c = clist[i][j];
    546551     
    547       if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     552      if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
    548553        Nbad ++;
    549554        continue;
     
    565570      }
    566571     
    567       n = catalog[c].measure[m].averef;
    568       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
     572      n = catalog[c].measureT[m].averef;
     573      Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
    569574      if (isnan(Msys)) {
    570575        Nsys++;
     
    572577      }
    573578      list[N] = Msys - Mrel - Mcal - Mmos;
    574       dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
     579      dlist[N] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
    575580      N++;
    576581    }
     
    613618  if (!USE_GRID) return;
    614619
     620  int Nsecfilt = GetPhotcodeNsecfilt ();
     621
    615622  N = 0;
    616623  for (i = 0; i < Ngrid; i++)
     
    630637      c = clist[i][j];
    631638     
    632       if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     639      if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
    633640        Narea ++;
    634641        continue;
     
    641648      if (isnan(Mrel)) continue;
    642649
    643       n = catalog[c].measure[m].averef;
    644       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
     650      n = catalog[c].measureT[m].averef;
     651      Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
    645652
    646653      xlist[N] = Xmeas[c][m];
     
    704711  gfits_create_matrix (&header, &matrix);
    705712  gfits_modify (&header, "NEXTEND", OFF_T_FMT, 1,  Nimage + 3);
    706   gfits_modify (&header, "FILTER", "%s", 1, photcode[0].name);
     713  gfits_modify (&header, "FILTER", "%s", 1, photcodes[0][0].name);  // XXXX note that this expects a single photcode, enforced in initialize.d
    707714  gfits_modify_alt (&header, "COMMENT", "%S", 1, "Mosaic Photometry Grid Analysis");
    708715
     
    727734  theader.bitpix   = -32;
    728735  gfits_create_Theader (&theader, "IMAGE");
    729   gfits_modify (&theader, "FILTER", "%s", 1, photcode[0].name);
     736  gfits_modify (&theader, "FILTER", "%s", 1, photcodes[0][0].name);
    730737  gfits_modify (&theader, "EXTNAME", "%s", 1, "MAG_OFFSET");
    731738  gfits_create_matrix  (&theader, &matrix);
     
    742749  /* save grid Nmeas values */
    743750  gfits_modify (&theader, "EXTNAME", "%s", 1, "NMEAS");
    744   gfits_modify (&theader, "FILTER", "%s", 1, photcode[0].name);
     751  gfits_modify (&theader, "FILTER", "%s", 1, photcodes[0][0].name);
    745752  gfits_create_matrix  (&theader, &matrix);
    746753  for (i = 0; i < gridX; i++) {
     
    756763  /* save grid sigma values */
    757764  gfits_modify (&theader, "EXTNAME", "%s", 1, "SIGMA");
    758   gfits_modify (&theader, "FILTER", "%s", 1, photcode[0].name);
     765  gfits_modify (&theader, "FILTER", "%s", 1, photcodes[0][0].name);
    759766  gfits_create_matrix  (&theader, &matrix);
    760767  for (i = 0; i < gridX; i++) {
     
    776783
    777784    gfits_modify (&theader, "EXTNAME", "%s", 1, camera.ccdname[N]);
    778     gfits_modify (&theader, "FILTER", "%s", 1, photcode[0].name);
     785    gfits_modify (&theader, "FILTER", "%s", 1, photcodes[0][0].name);
    779786    gfits_modify (&theader, "NX", "%d", 1, camera.Nx);
    780787    gfits_modify (&theader, "NY", "%d", 1, camera.Ny);
  • trunk/Ohana/src/relphot/src/ImageOps.c

    r30616 r31450  
    11# include "relphot.h"
    2 
    3 # define USE_IMAGE_ID 1
    42
    53static off_t       **bin;     // link from catalog,measure to image         
     
    1210static off_t        Nimage;   // number of available images
    1311
    14 // if we search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted
    15 // index
    16 
    17 # if USE_IMAGE_ID
     12// to search by image ID, we sort (imageIDs, imageIdx) by imageIDs to get a sorted index
    1813static off_t        *imageIDs; // list of all image IDs
    1914static off_t        *imageIdx; // list of index for image IDs
    20 # else
    21 static unsigned int *start;
    22 static unsigned int *stop;
    23 # endif
    2415
    2516Image *getimages (off_t *N) {
     
    4031  Nimage = N;
    4132
    42 # if USE_IMAGE_ID
    4333  ALLOCATE (imageIDs, off_t, Nimage);
    4434  ALLOCATE (imageIdx, off_t, Nimage);
     
    4939  }
    5040  llsortpair (imageIDs, imageIdx, Nimage);
    51 # else
    52   ALLOCATE (start,   unsigned, Nimage);
    53   ALLOCATE (stop,    unsigned, Nimage);
    54 
    55   for (i = 0; i < Nimage; i++) {
    56     start[i] = image[i].tzero - MAX(0.01*image[i].trate*image[i].NY, 1);
    57     stop[i]  = image[i].tzero + MAX(1.01*image[i].trate*image[i].NY, 1);
    58   }
    59 # endif
    6041}
    6142
     
    6546  // use bisection to find the specified image ID
    6647
    67 # if USE_IMAGE_ID
    6848  off_t Nlo, Nhi, N;
    6949
     
    8262      return (imageIdx[N]);
    8363  }
    84 # endif
    8564
    8665  return (-1);
     
    128107}
    129108
    130 /* select all image equivalent to the current photcode */
     109/* select all image equivalent to the active photcode set */
    131110void findImages (Catalog *catalog, int Ncatalog) {
    132111
    133112  off_t j;
    134   int i, ecode;
    135 
    136   for (i = 0; i < Ncatalog; i++) {
     113  int i, ecode, Ns, found;
     114 
     115  int Nmatch = 0;
     116 for (i = 0; i < Ncatalog; i++) {
    137117    for (j = 0; j < catalog[i].Nmeasure; j++) {
    138       ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
    139       if (photcode[0].code != ecode) continue;
     118      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[j].photcode);
     119      found = FALSE;
     120      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     121        if (ecode == photcodes[Ns][0].code) found = TRUE;
     122      }
     123      if (!found) continue;
    140124      matchImage (catalog, j, i);
    141     }
    142   }
    143 }
    144 
    145 int findCCD (off_t idx, off_t meas, int cat, Measure *measure) {
    146 
    147   int ccdnum;
     125      Nmatch ++;
     126    }
     127  }
     128 // fprintf (stderr, "matched %d detections to images\n", Nmatch);
     129}
     130
     131int findCCD (off_t idx, off_t meas, int cat, MeasureTiny *measure) {
     132
     133  int ccdnum, found, Ns, ecode;
    148134  double X, Y;
    149135  char *pname, *filter, *p, base[256];
     
    151137  /* identify the ccd on the basis of the photcode name */
    152138  pname = GetPhotcodeNamebyCode (image[idx].photcode);
    153   filter = photcode[0].name;
     139
     140  // XXX this seems quite terrible...
     141  ecode = GetPhotcodeEquivCodebyCode (measure[0].photcode);
     142  found = FALSE;
     143  for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     144    if (ecode == photcodes[Ns][0].code) found = TRUE;
     145  }
     146  if (!found) return (FALSE);
     147
     148  filter = photcodes[Ns][0].name;
    154149  sprintf (base, "%s.%s.", MOSAICNAME, filter);
    155150  if (strncmp (pname, base, strlen (base))) return (FALSE);
     
    170165
    171166  // old code to add this measurement to the grid cell for this chip
    172   // ave = measure[0].averef;
    173   // ra  = catalog[cat].average[ave].R - measure[0].dR / 3600.0;
    174   // dec = catalog[cat].average[ave].D - measure[0].dD / 3600.0;
     167  // ave = measureT[0].averef;
     168  // ra  = catalog[cat].averageT[ave].R - measureT[0].dR / 3600.0;
     169  // dec = catalog[cat].averageT[ave].D - measureT[0].dD / 3600.0;
    175170  // RD_to_XY (&X, &Y, ra, dec, &image[i].coords);
    176171
     
    183178}
    184179
    185 # if USE_IMAGE_ID
    186180void matchImage (Catalog *catalog, off_t meas, int cat) {
    187181
    188182  off_t idx, ID;
    189183  int status;
    190   Measure *measure;
     184  MeasureTiny *measure;
    191185 
    192   measure = &catalog[cat].measure[meas];
     186  measure = &catalog[cat].measureT[meas];
    193187
    194188  ID = measure[0].imageID;
     
    226220}
    227221
    228 # else
    229 // this is the time-based match
    230 void matchImage (Catalog *catalog, off_t meas, int cat) {
    231 
    232   off_t i;
    233   int status;
    234   Measure *measure;
    235  
    236   measure = &catalog[cat].measure[meas];
    237   for (i = 0; i < Nimage; i++) {
    238     if (image[0].photcode == -1) continue;
    239     if (measure[0].photcode != image[i].photcode) continue;
    240     if (measure[0].t < start[i]) continue;
    241     if (measure[0].t > stop[i]) continue;
    242    
    243     if (USE_GRID) {
    244       status = findCCD(i, meas, cat);
    245       if (!status) continue;
    246     }
    247 
    248     // index for (catalog, measure) -> image
    249     bin[cat][meas] = i;
    250 
    251     // index for image, Nentry -> catalog
    252     clist[i][Nlist[i]] = cat;
    253 
    254     // index for image, Nentry -> measure
    255     mlist[i][Nlist[i]] = meas;
    256     Nlist[i] ++;
    257 
    258     if (Nlist[i] == NLIST[i]) {
    259       NLIST[i] += 100;
    260       REALLOCATE (clist[i], off_t, NLIST[i]);
    261       REALLOCATE (mlist[i], off_t, NLIST[i]);
    262     }   
    263     return;
    264   }
    265   /*  fprintf (stderr, "can't find source image for this measurement: %d (%d)\n", measure[0].t, measure[0].photcode); */
    266 }
    267 # endif
    268 
    269222off_t getImageEntry (off_t meas, int cat) {
    270223
     
    307260
    308261  if (FREEZE_IMAGES) return;
     262
     263  int Nsecfilt = GetPhotcodeNsecfilt ();
    309264
    310265  if (PoorImages) {
     
    339294      c = clist[i][j];
    340295     
    341       if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     296      if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
    342297          Nbad++;
    343298          continue;
     
    359314      }
    360315     
    361       n = catalog[c].measure[m].averef;
    362       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
     316      n = catalog[c].measureT[m].averef;
     317      Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
    363318      if (isnan(Msys)) {
    364319        Nsys++;
     
    366321      }
    367322      list[N] = Msys - Mrel - Mmos - Mgrid;
    368       dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
    369       if (catalog[c].measure[m].dM < IMFIT_SYS_SIGMA_LIM) {
     323      dlist[N] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
     324      if (catalog[c].measureT[m].dM < IMFIT_SYS_SIGMA_LIM) {
    370325        McalBright += list[N];
    371326        McalBright2 += SQ(list[N]);
     
    632587  return (stats);
    633588}
    634 
  • trunk/Ohana/src/relphot/src/MosaicOps.c

    r30616 r31450  
    99static off_t   **imlist; /* mosaic -> image[] */
    1010static off_t   **bin;    /* catalog, measure -> mosaic */
     11
     12// list of mosaics associated with an image
     13static off_t   *mosimage;
    1114
    1215// list of mosaic associated with each image 
     
    3336  ALLOCATE (NIMLIST, off_t,   NMOSAIC);
    3437
     38  ALLOCATE (mosimage, off_t, Nimage); // mosaic to which image belongs
     39
    3540  /* a 'mosaic' in relphot is (unlike relastro) a virtual concept: there is no
    3641   * entry in the image table that represents this mosaic.  Instead, it is an
     
    4045  /* generate list of unique mosaics */
    4146  for (i = 0; i < Nimage; i++) {
     47    mosimage[i] = -1;
    4248
    4349    /* select valid mosaic images by photcode */
     
    5662      if (start > mosaic[j].stop)  continue;
    5763      found = TRUE;
     64
     65      // add reference from image to mosaic
     66      mosimage[i] = j;
    5867
    5968      /* add image to mosaic image list */
     
    7786    mosaic[Nmosaic].flags  = image[i].flags;
    7887    mosaic[Nmosaic].secz  = image[i].secz;
     88    mosaic[Nmosaic].photcode = GetPhotcodeEquivCodebyCode (image[i].photcode);
    7989
    8090    /* add image to mosaic image list */
     
    8494    imlist[Nmosaic][0] = i;
    8595
     96    // add reference from image to mosaic
     97    mosimage[i] = Nmosaic;
     98   
    8699    Nmosaic ++;
    87100    if (Nmosaic == NMOSAIC) {
     
    250263int findMosaics (Catalog *catalog, int Ncatalog) {
    251264 
    252   int i, ecode;
     265  int i, ecode, found, Ns;
    253266  off_t j;
    254267
    255268  if (!MOSAIC_ZEROPT) return (FALSE);
    256269
     270  int Nmatch = 0;
    257271  for (i = 0; i < Ncatalog; i++) {
    258272    for (j = 0; j < catalog[i].Nmeasure; j++) {
    259273      if (TimeSelect) {
    260         if (catalog[i].measure[j].t < TSTART) continue;
    261         if (catalog[i].measure[j].t > TSTOP) continue;
    262       }
    263       ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
    264       if (photcode[0].code != ecode) continue;
     274        if (catalog[i].measureT[j].t < TSTART) continue;
     275        if (catalog[i].measureT[j].t > TSTOP) continue;
     276      }
     277      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[j].photcode);
     278      found = FALSE;
     279      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     280        if (ecode == photcodes[Ns][0].code) found = TRUE;
     281      }
     282      if (!found) continue;
    265283      matchMosaics (catalog, j, i);
    266     }
    267   }
     284      Nmatch ++;
     285    }
     286  }
     287  // fprintf (stderr, "matched %d detections to mosaics\n", Nmatch);
    268288  return (TRUE);
    269289}
     
    271291void matchMosaics (Catalog *catalog, off_t meas, int cat) {
    272292
    273   int i;
    274 
    275   for (i = 0; i < Nmosaic; i++) {
    276     if (catalog[cat].measure[meas].t < mosaic[i].start) continue;
    277     if (catalog[cat].measure[meas].t > mosaic[i].stop) continue;
     293  off_t idx, ID, mosID;
     294  MeasureTiny *measure;
     295
     296  measure = &catalog[cat].measureT[meas];
     297
     298  ID = measure[0].imageID;
     299  idx = getImageByID (ID);
     300  if (idx == -1) {
     301    if (VERBOSE2) fprintf (stderr, "missed measurement "OFF_T_FMT", %d\n", meas, cat);
     302    return;
     303  }
     304
     305  mosID = mosimage[idx];
     306  if (mosID < 0) {
     307    Image *image = getimage(idx);
     308    fprintf (stderr, "unmatched image %s\n", image[0].name);
     309    return;
     310  }
     311
     312  // test to check we got the right match:
     313  {
     314    Image *image = getimage(idx);
     315    unsigned int imageStart = image[0].tzero - MAX(0.01*image[0].trate*image[0].NY, 1);
     316    if (imageStart != mosaic[mosID].start) {
     317      fprintf (stderr, "error in image to mosaic match\n");
     318      abort();
     319    }
     320  }
     321
     322  bin[cat][meas] = mosID;
     323
     324  clist[mosID][Nlist[mosID]] = cat;
     325  mlist[mosID][Nlist[mosID]] = meas;
     326  Nlist[mosID] ++;
    278327   
    279 # ifdef GRID_V1
    280     if (USE_GRID) {
    281       ave = catalog[cat].measure[meas].averef;
    282       ra  = catalog[cat].average[ave].R - catalog[cat].measure[meas].dR / 3600.0;
    283       dec = catalog[cat].average[ave].D - catalog[cat].measure[meas].dD / 3600.0;
    284 
    285       /* X,Y always positive-definite in range 0,0 - dX, dY */
    286       RD_to_XY (&X, &Y, ra, dec, &mosaic[i].coords);
    287       setGridMeasure (meas, cat, X, Y);
    288     }
    289 # endif
    290 
    291     bin[cat][meas] = i;
    292 
    293     clist[i][Nlist[i]] = cat;
    294     mlist[i][Nlist[i]] = meas;
    295     Nlist[i] ++;
    296    
    297     if (Nlist[i] == NLIST[i]) {
    298       NLIST[i] += 100;
    299       REALLOCATE (clist[i], int,   NLIST[i]);
    300       REALLOCATE (mlist[i], off_t, NLIST[i]);
    301     }   
    302     return;
    303   }
    304   fprintf (stderr, "missed measurement\n");
     328  if (Nlist[mosID] == NLIST[mosID]) {
     329    NLIST[mosID] += 100;
     330    REALLOCATE (clist[mosID], int,   NLIST[mosID]);
     331    REALLOCATE (mlist[mosID], off_t, NLIST[mosID]);
     332  }     
    305333  return;
    306334}
     
    324352
    325353  off_t i, j, m, c, N, Nmax;
    326   int n, mark, bad, Nfew, Nbad, Ncal, Nrel, Ngrid, Nsys;
     354  int n, mark, bad, Nfew, Nbad, Ncal, Nrel, Ngrid, Nsys, Nsecfilt;
    327355  float Msys, Mrel, Mcal, Mgrid;
    328356  double *list, *dlist, *Mlist, *dMlist;
     
    334362
    335363  image = getimages (&N);
     364
     365  Nsecfilt = GetPhotcodeNsecfilt ();
    336366
    337367  if (PoorImages) {
     
    364394      c = clist[i][j];
    365395     
    366       if (catalog[c].measure[m].dbFlags & MEAS_BAD) {
     396      if (catalog[c].measureT[m].dbFlags & MEAS_BAD) {
    367397          Nbad ++;
    368398          continue;
     
    384414      }
    385415     
    386       n = catalog[c].measure[m].averef;
    387       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
     416      n = catalog[c].measureT[m].averef;
     417      Msys = PhotSysTiny (&catalog[c].measureT[m], &catalog[c].averageT[n], &catalog[c].secfilt[n*Nsecfilt]);
    388418      if (isnan(Msys)) {
    389419        Nsys++;
     
    391421      }
    392422      list[N]  = Msys - Mrel - Mcal - Mgrid;
    393       dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
     423      dlist[N] = MAX (catalog[c].measureT[m].dM, MIN_ERROR);
    394424      Mlist[N] = Msys;
    395425      dMlist[N] = list[N];
     
    433463}
    434464 
     465// When we rationalize the images/mosaics, we are driving the negative cloud images back
     466// to 0.0.  At the same time, we make a guess to the effective impact on all other images,
     467// driven by the coupling of common stars.
     468int rationalize_mosaics (Catalog *catalog, int Ncatalog) {
     469
     470  double *imageOffset, **starOffset;
     471  int **starNcount, *seclist;
     472  int **Slist, *NSlist, *NSLIST;
     473  int i, j, k, m, nMos, Ns, found;
     474
     475  off_t Nimage;
     476  Image *image;
     477
     478  // set a test value for now
     479  float CLOUD_TOLERANCE = 0.025;
     480
     481  if (!MOSAIC_ZEROPT) return (FALSE);
     482  if (FREEZE_MOSAICS) return (FALSE);
     483
     484  image = getimages (&Nimage);
     485
     486  ALLOCATE (imageOffset, double, Nmosaic);
     487
     488  ALLOCATE ( Slist, int *, Nmosaic); // array of calibrated star indexes on this mosaic
     489  ALLOCATE (NSlist, int,   Nmosaic); // number of stars on mosaic
     490  ALLOCATE (NSLIST, int,   Nmosaic); // number of Slist entries allocated
     491  memset (Slist, 0, Nmosaic*sizeof(int *));
     492
     493  // find the images / mosaics with negative clouds and save their offset
     494  for (i = 0; i < Nmosaic; i++) {
     495 
     496    NSlist[i] =   0;
     497    NSLIST[i] = 100;
     498    ALLOCATE (Slist[i], int, NSLIST[i]);
     499
     500    imageOffset[i] = 0.0;
     501
     502    if (VERBOSE2 && (fabs(mosaic[i].Mcal) < CLOUD_TOLERANCE)) {
     503      fprintf (stderr, "cloud-free: %s : %f\n", image[imlist[i][0]].name, mosaic[i].Mcal);
     504    }
     505    if (VERBOSE2 && (mosaic[i].Mcal < -CLOUD_TOLERANCE)) {
     506      imageOffset[i] = -mosaic[i].Mcal;
     507      // NOTE the negative sign: down below, we are going to add in the negative of Mcal
     508      // to this image, and the propagated mean values for other images
     509      fprintf (stderr, "anti-clouds: %s : %f\n", image[imlist[i][0]].name, mosaic[i].Mcal);
     510    }
     511    if (VERBOSE2 && (mosaic[i].Mcal > CLOUD_TOLERANCE)) {
     512      fprintf (stderr, "cloudy    : %s : %f\n", image[imlist[i][0]].name, mosaic[i].Mcal);
     513    }
     514  }
     515
     516  int Nsecfilt = GetPhotcodeNsecfilt ();
     517  ALLOCATE (seclist, int, Nphotcodes);
     518  for (Ns = 0; Ns < Nphotcodes; Ns ++) {
     519    int thisCode = photcodes[Ns][0].code;
     520    seclist[Ns] = GetPhotcodeNsec(thisCode);
     521  }
     522
     523  // allocate an array for star offsets
     524  int Nstars = 0;
     525  int NSTARS = 1000;
     526  ALLOCATE (starOffset, double *, NSTARS);
     527  ALLOCATE (starNcount, int *,    NSTARS);
     528  memset (starOffset, 0, NSTARS*sizeof(double *));
     529  memset (starNcount, 0, NSTARS*sizeof(int *));
     530
     531  // find the mean offset for each star
     532  for (i = 0; i < Ncatalog; i++) {
     533    for (j = 0; j < catalog[i].Naverage; j++) {
     534      ALLOCATE (starOffset[Nstars], double, Nphotcodes);
     535      ALLOCATE (starNcount[Nstars], int,    Nphotcodes);
     536      memset (starOffset[Nstars], 0, Nphotcodes*sizeof(double));
     537      memset (starNcount[Nstars], 0, Nphotcodes*sizeof(int));
     538     
     539      m = catalog[i].averageT[j].measureOffset;
     540
     541      // determine the mosaic for each measurement
     542      for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     543
     544        // skip unused measurements
     545        if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
     546
     547        // skip unused measurements
     548        int Nsec;
     549        int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     550        found = FALSE;
     551        for (Ns = 0; !found && (Ns < Nphotcodes); Ns ++) {
     552          if (ecode == photcodes[Ns][0].code) {
     553            found = TRUE;
     554            break;
     555          }
     556        }
     557        if (!found) continue;
     558        Nsec = seclist[Ns];
     559
     560        // bad stars for this secfilt
     561        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     562
     563        // skip REF measurements (not tied to an image)
     564        if (getImageEntry (m, i) < 0) continue;
     565
     566        // find the source of this measurement (skip unassigned measurements)
     567        nMos = bin[i][m];
     568        if (nMos == -1) continue;
     569
     570        if (mosaic[nMos].photcode != ecode) {
     571          fprintf (stderr, "*");
     572        }
     573
     574        assert (Ns >= 0);
     575        assert (Ns < Nphotcodes);
     576
     577        // accumulate the offsets from the negative cloud images (others have 0.0 value)
     578        starOffset[Nstars][Ns] += imageOffset[nMos];
     579        starNcount[Nstars][Ns] ++;
     580       
     581        // record the mosaic->star reference
     582        Slist[nMos][NSlist[nMos]] = Nstars;
     583        NSlist[nMos] ++;
     584        if (NSlist[nMos] == NSLIST[nMos]) {
     585          NSLIST[nMos] += 100;
     586          REALLOCATE (Slist[nMos], int, NSLIST[nMos]);
     587        }         
     588      }
     589      Nstars ++;
     590      if (Nstars == NSTARS) {
     591        NSTARS += 1000;
     592        REALLOCATE (starOffset, double *, NSTARS);
     593        REALLOCATE (starNcount, int *,    NSTARS);
     594        memset (&starOffset[NSTARS-1000], 0, 1000*sizeof(double *));
     595        memset (&starNcount[NSTARS-1000], 0, 1000*sizeof(int *));
     596      }
     597    }
     598  }
     599
     600  // find the mean offset of the images without negative clouds
     601  for (i = 0; i < Nmosaic; i++) {
     602
     603    found = FALSE;
     604    for (Ns = 0; !found && (Ns < Nphotcodes); Ns ++) {
     605      if (mosaic[i].photcode == photcodes[Ns][0].code) {
     606        found = TRUE;
     607        break;
     608      }
     609    }
     610    if (!found) {
     611      fprintf (stderr, "invalid photcode for mosaic?\n");
     612      abort();
     613    }
     614
     615    // a negative cloud image (cloud: Mcal > 0; anti-clouds: Mcal < 0; imageOffset = -Mcal)
     616    if (imageOffset[i] > 0.0) continue;
     617
     618    // we need to actually have cross-references to count
     619    if (NSlist[i] < 2) continue;
     620
     621    float dM = 0.0;
     622    for (j = 0; j < NSlist[i]; j++) {
     623      Nstars = Slist[i][j];
     624      if (starNcount[Nstars][Ns] > 1) {
     625        dM += (starOffset[Nstars][Ns] / starNcount[Nstars][Ns]);
     626      }
     627    }
     628    imageOffset[i] = dM / NSlist[i];
     629    // fprintf (stderr, "adjust image: %s : (%f %d) : %f\n", image[imlist[i][0]].name, dM, NSlist[i], imageOffset[i]);
     630  }
     631
     632  // for (i = 0; i < Nmosaic; i++) {
     633  //   fprintf (stderr, "correction: %s : %f\n", image[imlist[i][0]].name, imageOffset[i]);
     634  // }
     635
     636  // apply all offset values to the mosaics
     637  // find the images / mosaics with negative clouds and save their offset
     638  for (i = 0; i < Nmosaic; i++) {
     639    mosaic[i].Mcal += imageOffset[i];
     640  }
     641
     642  for (i = 0; i < Nstars; i++) {
     643    free (starOffset[i]);
     644    free (starNcount[i]);
     645  }
     646  free (starOffset);
     647  free (starNcount);
     648
     649  for (i = 0; i < Nmosaic; i++){
     650    free (Slist[i]);
     651  }
     652  free (NSlist);
     653  free (NSLIST);
     654  free (imageOffset);
     655  free (seclist);
     656  return (TRUE);
     657}
     658
    435659StatType statsMosaicM (Catalog *catalog) {
    436660
     
    524748    n++;
    525749  }
    526   fprintf (stderr, "Nmosaic: "OFF_T_FMT", n: "OFF_T_FMT"\n",  Nmosaic,  n);
     750  // fprintf (stderr, "Nmosaic: "OFF_T_FMT", n: "OFF_T_FMT"\n",  Nmosaic,  n);
    527751
    528752  liststats (list, dlist, n, &stats);
     
    563787void clean_mosaics () {
    564788
    565   off_t i, N, mark, Nmark;
     789  off_t i, N, mark, Nmark, Nscatter, Noffset;
    566790  double *mlist, *slist, *dlist;
    567791  double MaxOffset, MedOffset, MaxScatter;
     
    592816  fprintf (stderr, "Mrel: %f, dMrel: %f, Max Scatter: %f, Max Offset: %f\n", MedOffset, stats.median, MaxScatter, MaxOffset);
    593817 
    594   Nmark = 0;
     818  Nmark = Nscatter = Noffset = 0;
    595819  for (i = 0; i < Nmosaic; i++) {
    596820    mark = FALSE;
    597     mark = (mosaic[i].dMcal > MaxScatter) || (fabs(mosaic[i].Mcal - MedOffset) > MaxOffset);
     821    if (mosaic[i].dMcal > MaxScatter) {
     822      mark = TRUE;
     823      Nscatter ++;
     824    }
     825    if (fabs(mosaic[i].Mcal - MedOffset) > MaxOffset) {
     826      mark = TRUE;
     827      Noffset ++;
     828    }
    598829    if (mark) {
    599830      Nmark ++;
     
    604835  }
    605836
    606   fprintf (stderr, OFF_T_FMT" mosaics marked poor\n",  Nmark);
     837  fprintf (stderr, OFF_T_FMT" mosaics marked poor ("OFF_T_FMT" scatter, "OFF_T_FMT" offset)\n",  Nmark, Nscatter, Noffset);
    607838  initstats (STATMODE);
    608839  free (mlist);
     
    640871      c = clist[i][j];
    641872     
    642       if (catalog[c].measure[m].dbFlags & (ID_MEAS_AREA | ID_MEAS_NOCAL)) continue;
    643 
    644       ave = catalog[c].measure[m].averef;
    645       xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR / 3600.0;
    646       ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD / 3600.0;
     873      if (catalog[c].measureT[m].dbFlags & (ID_MEAS_AREA | ID_MEAS_NOCAL)) continue;
     874
     875      ave = catalog[c].measureT[m].averef;
     876      xlist[N] = catalog[c].averageT[ave].R - catalog[c].measureT[m].dR / 3600.0;
     877      ylist[N] = catalog[c].averageT[ave].D - catalog[c].measureT[m].dD / 3600.0;
    647878      N++;
    648879    }
  • trunk/Ohana/src/relphot/src/StarOps.c

    r29001 r31450  
    55static double *dlist;
    66
     7// When we rationalize the images/mosaics, we are driving the negative cloud images back
     8// to 0.0.  At the same time, we make a guess to the effective impact on all other images,
     9// driven by the coupling of common stars.  This array carries the impact of those offsets
     10// on each star
     11static double *Moffset;
     12
    713void initMrel (Catalog *catalog, int Ncatalog) {
    814
     
    1218  for (i = 0; i < Ncatalog; i++) {
    1319    for (j = 0; j < catalog[i].Naverage; j++) {
    14       Nmax = MAX (Nmax, catalog[i].average[j].Nmeasure);
    15     }
    16   }
    17 
    18   ALLOCATE (list, double, MAX (1, Nmax));
    19   ALLOCATE (dlist, double, MAX (1, Nmax));
     20      Nmax = MAX (Nmax, catalog[i].averageT[j].Nmeasure);
     21    }
     22  }
     23
     24  ALLOCATE (list,    double, MAX (1, Nmax));
     25  ALLOCATE (dlist,   double, MAX (1, Nmax));
     26  ALLOCATE (Moffset, double, MAX (1, Nmax));
    2027
    2128
    2229float getMrel (Catalog *catalog, off_t meas, int cat) {
    2330
     31  int Nsec, Nsecfilt, photcode;
    2432  int ave;
    2533  float value;
    2634
    27   ave = catalog[cat].measure[meas].averef;
    28   if (catalog[cat].average[ave].flags & STAR_BAD) return (NAN); 
     35  ave = catalog[cat].measureT[meas].averef;
     36  photcode = catalog[cat].measureT[meas].photcode;
     37
     38  int ecode = GetPhotcodeEquivCodebyCode (photcode);
     39  Nsec = GetPhotcodeNsec(ecode);
     40  Nsecfilt = GetPhotcodeNsecfilt ();
     41
     42  // is this star OK?
     43  if (catalog[cat].secfilt[Nsecfilt*ave+Nsec].flags & STAR_BAD) return (NAN); 
    2944 
    30   value = catalog[cat].secfilt[PhotNsec*ave+PhotSec].M;
     45  value = catalog[cat].secfilt[Nsecfilt*ave+Nsec].M;
    3146  return (value);
    3247}
     
    3954  StatType stats;
    4055
     56  int Nsecfilt = GetPhotcodeNsecfilt ();
    4157  Nfew = Nsys = Nbad = Ncal = Nmos = Ngrid = 0;
    4258
     
    4460    for (j = 0; j < catalog[i].Naverage; j++) {
    4561
    46       /* calculate the average value for a single star */
    47       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    48       m = catalog[i].average[j].measureOffset;
    49 
    50       N = 0;
    51       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    52         if (catalog[i].measure[m].dbFlags & MEAS_BAD) {
    53           Nbad ++;
    54           continue;
    55         }
    56         // XXX allow REF stars (no Image Entry) to be included in the calculation this
    57         // should be optionally set, and should allow for REF stars to be downweighted by
    58         // more than their reported errors.  how such info is carried is unclear...
    59         if (getImageEntry (m, i) < 0) {
    60           Mcal = Mmos = Mgrid = 0;
    61         } else {
    62           Mcal  = getMcal  (m, i);
    63           if (isnan(Mcal)) {
    64             Ncal ++;
     62      int Ns;
     63      for (Ns = 0; Ns < Nphotcodes; Ns++) {
     64
     65        int thisCode = photcodes[Ns][0].code;
     66        int Nsec = GetPhotcodeNsec(thisCode);
     67
     68        /* calculate the average value for a single star */
     69
     70        // skip bad stars
     71        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
     72        m = catalog[i].averageT[j].measureOffset;
     73
     74        N = 0;
     75        for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     76          // skip measurements that do not match the current photcode
     77          int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     78          if (ecode != thisCode) { continue; }
     79
     80          if (catalog[i].measureT[m].dbFlags & MEAS_BAD) {
     81            Nbad ++;
    6582            continue;
    6683          }
    67           Mmos  = getMmos  (m, i);
    68           if (isnan(Mmos)) {
    69             Nmos ++;
     84          // XXX allow REF stars (no Image Entry) to be included in the calculation this
     85          // should be optionally set, and should allow for REF stars to be downweighted by
     86          // more than their reported errors.  how such info is carried is unclear...
     87          if (getImageEntry (m, i) < 0) {
     88            Mcal = Mmos = Mgrid = 0;
     89          } else {
     90            Mcal  = getMcal  (m, i);
     91            if (isnan(Mcal)) {
     92              Ncal ++;
     93              continue;
     94            }
     95            Mmos  = getMmos  (m, i);
     96            if (isnan(Mmos)) {
     97              Nmos ++;
     98              continue;
     99            }
     100            Mgrid = getMgrid (m, i);
     101            if (isnan(Mgrid)) {
     102              Ngrid++;
     103              continue;
     104            }
     105          }
     106
     107          Msys = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]);
     108          if (isnan(Msys)) {
     109            Nsys++;
    70110            continue;
    71111          }
    72           Mgrid = getMgrid (m, i);
    73           if (isnan(Mgrid)) {
    74             Ngrid++;
    75             continue;
     112          list[N] = Msys - Mcal - Mmos - Mgrid;
     113          dlist[N] = MAX (catalog[i].measureT[m].dM, MIN_ERROR);
     114
     115          // tie down reference photometry if the -refcode (code) option is selected
     116          if (refPhotcode) {
     117            if (GetPhotcodeEquivCodebyCode(catalog[i].measureT[m].photcode) == refPhotcode[0].equiv) {
     118              // increase the weight by a factor of 100:
     119              dlist[N] = 0.01*catalog[i].measureT[m].dM;
     120            }
    76121          }
    77         }
    78 
    79         Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
    80         if (isnan(Msys)) {
    81           Nsys++;
    82           continue;
    83         }
    84         list[N] = Msys - Mcal - Mmos - Mgrid;
    85         dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
    86 
    87         if (refPhotcode) {
    88           if (GetPhotcodeEquivCodebyCode(catalog[i].measure[m].photcode) == refPhotcode[0].equiv) {
    89             // increase the weight by a factor of 100:
    90             dlist[N] = 0.01*catalog[i].measure[m].dM;
    91           }
    92         }
    93         N++;
    94       }
    95 
    96       // when performing the grid analysis, STAR_TOOFEW will be set to 1;
    97 
    98       if (N <= STAR_TOOFEW) { /* too few measurements */
    99         catalog[i].average[j].flags |= ID_STAR_FEW;
    100         Nfew ++;
    101       } else {
    102         catalog[i].average[j].flags &= ~ID_STAR_FEW;
    103       }
    104 
    105       liststats (list, dlist, N, &stats);
    106 
    107       catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
    108       catalog[i].secfilt[PhotNsec*j+PhotSec].dM = stats.sigma;
    109       catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
     122          N++;
     123        }
     124
     125        // when performing the grid analysis, STAR_TOOFEW will be set to 1;
     126        if (N <= STAR_TOOFEW) { /* too few measurements */
     127          catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_STAR_FEW;
     128          Nfew ++;
     129        } else {
     130          catalog[i].secfilt[Nsecfilt*j+Nsec].flags &= ~ID_STAR_FEW;
     131        }       
     132
     133        liststats (list, dlist, N, &stats);
     134
     135        catalog[i].secfilt[Nsecfilt*j+Nsec].M  = stats.mean;
     136        catalog[i].secfilt[Nsecfilt*j+Nsec].dM = stats.sigma;
     137        catalog[i].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
     138      }
    110139    }
    111140  }
     
    122151  double *list, *dlist;
    123152  StatType stats;
     153  int Nsec, Nsecfilt, ecode;
     154
     155  Nsecfilt = GetPhotcodeNsecfilt ();
    124156
    125157  /* Nmeasure is now different, need to reallocate */
     
    127159  for (i = 0; i < Ncatalog; i++) {
    128160    for (j = 0; j < catalog[i].Naverage; j++) {
    129       Nmax = MAX (Nmax, catalog[i].average[j].Nmeasure);
     161      Nmax = MAX (Nmax, catalog[i].averageT[j].Nmeasure);
    130162    }
    131163  }
     
    135167  for (i = 0; i < Ncatalog; i++) {
    136168    for (j = 0; j < catalog[i].Naverage; j++) {
    137 
    138169      /* skip stars already calibrated */
    139170      if (catalog[i].found[j]) continue; 
    140171
    141       N = 0;
    142       m = catalog[i].average[j].measureOffset;
    143       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    144         if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue;
    145         // XXX allow REF stars (no Image Entry) to be included in the calculation this
    146         // should be optionally set, and should allow for REF stars to be downweighted by
    147         // more than their reported errors.  how such info is carried is unclear...
    148         if (getImageEntry (m, i) < 0) {
    149           Mcal = Mmos = Mgrid = 0;
    150         } else {
    151           Mcal  = getMcal  (m, i);
    152           if (isnan(Mcal)) continue;
    153           Mmos  = getMmos  (m, i);
    154           if (isnan(Mmos)) continue;
    155           Mgrid = getMgrid (m, i);
    156           if (isnan(Mgrid)) continue;
    157         }
    158 
    159         Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
    160         list[N] = Msys - Mcal - Mmos - Mgrid;
    161         dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
    162         N++;
    163       }
    164       if (N < 1) continue;
    165 
    166       liststats (list, dlist, N, &stats);
    167       if (mark) catalog[i].found[j] = TRUE;
    168 
    169       /* use sigma or error in dM for output? */
    170       catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
    171       catalog[i].secfilt[PhotNsec*j+PhotSec].dM = MAX (stats.error, stats.sigma);
    172       catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
     172      int Ns;
     173      for (Ns = 0; Ns < Nphotcodes; Ns++) {
     174        int thisCode = photcodes[Ns][0].code;
     175        Nsec = GetPhotcodeNsec(thisCode);
     176
     177        N = 0;
     178        m = catalog[i].averageT[j].measureOffset;
     179        for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     180          // skip measurements that do not match the current photcode
     181          ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     182          if (ecode != thisCode) { continue; }
     183
     184          if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
     185
     186          // XXX allow REF stars (no Image Entry) to be included in the calculation this
     187          // should be optionally set, and should allow for REF stars to be downweighted by
     188          // more than their reported errors.  how such info is carried is unclear...
     189          if (getImageEntry (m, i) < 0) {
     190            Mcal = Mmos = Mgrid = 0;
     191          } else {
     192            Mcal  = getMcal  (m, i);
     193            if (isnan(Mcal)) continue;
     194            Mmos  = getMmos  (m, i);
     195            if (isnan(Mmos)) continue;
     196            Mgrid = getMgrid (m, i);
     197            if (isnan(Mgrid)) continue;
     198          }
     199
     200          Msys = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]);
     201          list[N] = Msys - Mcal - Mmos - Mgrid;
     202          dlist[N] = MAX (catalog[i].measureT[m].dM, MIN_ERROR);
     203          N++;
     204        }
     205        if (N < 1) continue;
     206
     207        liststats (list, dlist, N, &stats);
     208        if (mark) catalog[i].found[j] = TRUE;
     209
     210        /* use sigma or error in dM for output? */
     211        catalog[i].secfilt[Nsecfilt*j+Nsec].M  = stats.mean;
     212        catalog[i].secfilt[Nsecfilt*j+Nsec].dM = MAX (stats.error, stats.sigma);
     213        catalog[i].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
     214      }
    173215    }
    174216  }
     
    179221}
    180222
    181 // for each average object, set the average mags based on existing equiv photometry
     223// For each average object, set the average mags based on existing equiv photometry.
     224// NOTE: this function operates on the real Measure & Average structures, not the
     225// MeasureTiny & AverageTiny structures
    182226int setMave (Catalog *catalog, int Ncatalog) {
    183227
     
    202246  ALLOCATE (dlist, double, MAX (1, Nmax));
    203247
    204   Nsecfilt = catalog[0].Nsecfilt;
     248  Nsecfilt = GetPhotcodeNsecfilt ();
    205249
    206250# define PSFQUALSTATS 1
     
    220264          if (GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode) != Nc) continue;
    221265
    222           Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
     266          Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*Nsecfilt]);
    223267          if (isnan(Msys)) continue;
    224268
     
    321365    for (j = 0; j < catalog[i].Naverage; j++) {
    322366
    323       m = catalog[i].average[j].measureOffset;
    324       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    325         if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue;
     367      m = catalog[i].averageT[j].measureOffset;
     368      for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     369        if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
    326370        Mcal  = getMcal  (m, i);
    327371        if (isnan(Mcal)) continue;
     
    330374        Mgrid = getMgrid (m, i);
    331375        if (isnan(Mgrid)) continue;
     376
     377        // set the output calibration
    332378        catalog[i].measure[m].Mcal = Mcal + Mmos + Mgrid;
    333379      }
     
    339385void clean_stars (Catalog *catalog, int Ncatalog) {
    340386
    341   int i, j, Ndel, Nave, Ntot, mark;
     387  int i, j, Ndel, Nave, Ntot, mark, Ns;
    342388  float dM, Xm;
    343389  double Chisq, MaxScatter, MaxChisq;
     
    354400  ALLOCATE (slist, double, Ntot);
    355401  ALLOCATE (dlist, double, Ntot);
    356   for (i = Ntot = 0; i < Ncatalog; i++) {
    357     for (j = 0; j < catalog[i].Naverage; j++) {
    358       if (catalog[i].average[j].flags & STAR_BAD) continue;
    359       Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
    360       if (Xm == -1) continue;
    361       Chisq = pow (10.0, 0.01*Xm);
    362       xlist[Ntot] = Chisq;
    363       slist[Ntot] = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
    364       dlist[Ntot] = 1;
    365       Ntot ++;
    366     }
    367   }
     402
     403  int Nsecfilt = GetPhotcodeNsecfilt ();
     404
     405  // eliminate bad stars using the stats for a single secfilt at a time
     406  // XXX DEP replace average.flags with secfilt flags
     407  for (Ns = 0; Ns < Nphotcodes; Ns ++) {
     408   
     409    int thisCode = photcodes[Ns][0].code;
     410    int Nsec = GetPhotcodeNsec(thisCode);
     411
     412    for (i = Ntot = 0; i < Ncatalog; i++) {
     413      for (j = 0; j < catalog[i].Naverage; j++) {
     414        if ( catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD ) continue;
     415        Xm = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
     416        if (Xm == -1) continue;
     417        Chisq = pow (10.0, 0.01*Xm);
     418        xlist[Ntot] = Chisq;
     419        slist[Ntot] = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     420        dlist[Ntot] = 1;
     421        Ntot ++;
     422      }
     423    }
    368424 
    369   initstats ("MEAN");
    370   liststats (xlist, dlist, Ntot, &stats);
    371   MaxChisq = MAX (STAR_CHISQ, 2*stats.median);
    372   liststats (slist, dlist, Ntot, &stats);
    373   MaxScatter = MAX (STAR_SCATTER, 2*stats.median);
    374   fprintf (stderr, "Max Scatter: %f, Max Chisq: %f\n", MaxScatter, MaxChisq);
    375 
    376   Ndel = Nave = 0;
    377   for (i = 0; i < Ncatalog; i++) {
    378     for (j = 0; j < catalog[i].Naverage; j++) {
    379       dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
    380       Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
    381       Chisq = pow (10.0, 0.01*Xm);
    382       mark = (dM > MaxScatter) || (Xm == NAN_S_SHORT) || (Chisq > MaxChisq);
    383       if (mark) {
    384         catalog[i].average[j].flags |= ID_STAR_POOR;
    385         Ndel ++;
    386       } else {
    387         catalog[i].average[j].flags &= ~ID_STAR_POOR;
    388       }
    389       Nave ++;
    390     }
    391   }
    392   fprintf (stderr, "%d stars marked variable, %d total\n", Ndel, Nave);
    393   initstats (STATMODE);
     425    initstats ("MEAN");
     426    liststats (xlist, dlist, Ntot, &stats);
     427    MaxChisq = MAX (STAR_CHISQ, 2*stats.median);
     428    liststats (slist, dlist, Ntot, &stats);
     429    MaxScatter = MAX (STAR_SCATTER, 2*stats.median);
     430    fprintf (stderr, "Max Scatter: %f, Max Chisq: %f\n", MaxScatter, MaxChisq);
     431
     432    Ndel = Nave = 0;
     433    for (i = 0; i < Ncatalog; i++) {
     434      for (j = 0; j < catalog[i].Naverage; j++) {
     435        dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     436        Xm = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
     437        Chisq = pow (10.0, 0.01*Xm);
     438        mark = (dM > MaxScatter) || (Xm == NAN_S_SHORT) || (Chisq > MaxChisq);
     439        if (mark) {
     440          catalog[i].secfilt[Nsecfilt*j+Nsec].flags |= ID_STAR_POOR;
     441          Ndel ++;
     442        } else {
     443          catalog[i].secfilt[Nsecfilt*j+Nsec].flags &= ~ID_STAR_POOR;
     444        }
     445        Nave ++;
     446      }
     447    }
     448    fprintf (stderr, "%d stars marked variable, %d total\n", Ndel, Nave);
     449    initstats (STATMODE);
     450  }
    394451  free (xlist);
    395452  free (slist);
     
    409466  int Ncal, Nmos, Ngrid, Nfew;
    410467
     468  int Nsecfilt = GetPhotcodeNsecfilt ();
     469
    411470  if (VERBOSE) fprintf (stderr, "marking poor measures\n");
    412471  /* Nmeasure is now different, need to reallocate */
     
    414473  for (i = 0; i < Ncatalog; i++) {
    415474    for (j = 0; j < catalog[i].Naverage; j++) {
    416       Nmax = MAX (Nmax, catalog[i].average[j].Nmeasure);
     475      Nmax = MAX (Nmax, catalog[i].averageT[j].Nmeasure);
    417476    }
    418477  }
     
    430489    for (j = 0; j < catalog[i].Naverage; j++) {
    431490
    432       /* skip bad stars to prevent them from becoming good (on inner sample) */
    433       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    434 
    435       /* on final processing, skip stars already measured */
    436       if (final && catalog[i].found[j]) continue; 
    437 
    438       /* accumulate list of valid measurements */
    439       m = catalog[i].average[j].measureOffset;
    440       N = 0;
    441       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    442         /* if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue; */
    443         Mcal  = getMcal  (m, i);
    444         if (isnan(Mcal)) { Ncal ++; continue; }
    445         Mmos  = getMmos  (m, i);
    446         if (isnan(Mmos)) { Nmos ++; continue; }
    447         Mgrid = getMgrid (m, i);
    448         if (isnan(Mgrid)) { Ngrid ++; continue; }
    449 
    450         Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
    451         list[N] = Msys - Mcal - Mmos - Mgrid;
    452         dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
    453         N++;
    454       }
    455       if (N <= TOOFEW) { Nfew ++; continue; }
    456 
    457       /* 3-sigma clip based on stats of inner 50% */
    458 
    459       // calculated mean of inner 50%
    460       initstats ("INNER_MEAN");
    461       liststats (list, dlist, N, &stats);
    462       stats.sigma = MAX (MIN_ERROR, stats.sigma); /* if measurements agree too well, sigma -> 0.0 */
    463 
    464       // ignore entries > 3sigma from inner mean
    465       for (k = m = 0; k < N; k++) {
    466         if (fabs (list[k] - stats.median) < NSIGMA_CLIP*stats.sigma) {
    467           list[m] = list[k];
    468           m++;
    469         }
    470       }
    471       // recalculate the mean & sigma of the accepted measurements
    472       initstats ("MEAN");
    473       liststats (list, dlist, m, &stats);
    474       stats.sigma = MAX (MIN_ERROR, stats.sigma);
    475 
    476       /* apply to list of all relevant measurements, including IMAGE_POOR & IMAGE_FEW */
    477       image_bad = IMAGE_BAD;
    478       IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL;
    479       m = catalog[i].average[j].measureOffset;
    480       N = 0;
    481       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    482         /* if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue; */
    483         Mcal  = getMcal  (m, i);
    484         if (isnan(Mcal)) continue;
    485         Mmos  = getMmos  (m, i);
    486         if (isnan(Mmos)) continue;
    487         Mgrid = getMgrid (m, i);
    488         if (isnan(Mgrid)) continue;
    489 
    490         Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
    491         list[N] = Msys - Mcal - Mmos - Mgrid;
    492         dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
    493         ilist[N] = m;
    494         N++;
    495         Nave ++;
    496       }
    497       if (N < TOOFEW) continue;
    498 
    499       /* mark bad measures (> 3 sigma deviant) */
    500       for (k = 0; k < N; k++) {
    501         if (fabs (list[k] - stats.median) > NSIGMA_REJECT*stats.sigma) {
    502           catalog[i].measure[ilist[k]].dbFlags |= ID_MEAS_POOR_PHOTOM;
    503           Ndel ++;
    504         }
    505       }
    506       IMAGE_BAD = image_bad;
     491      int Ns;
     492      for (Ns = 0; Ns < Nphotcodes; Ns++) {
     493       
     494        /* on final processing, skip stars already measured */
     495        if (final && catalog[i].found[j]) continue; 
     496
     497        int thisCode = photcodes[Ns][0].code;
     498        int Nsec = GetPhotcodeNsec(thisCode);
     499       
     500        /* skip bad stars to prevent them from becoming good (on inner sample) */
     501        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     502
     503        /* accumulate list of valid measurements */
     504        m = catalog[i].averageT[j].measureOffset;
     505        N = 0;
     506        for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     507          // skip measurements that do not match the current photcode
     508          int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     509          if (ecode != thisCode) { continue; }
     510
     511          /* if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue; */
     512          Mcal  = getMcal  (m, i);
     513          if (isnan(Mcal)) { Ncal ++; continue; }
     514          Mmos  = getMmos  (m, i);
     515          if (isnan(Mmos)) { Nmos ++; continue; }
     516          Mgrid = getMgrid (m, i);
     517          if (isnan(Mgrid)) { Ngrid ++; continue; }
     518
     519          Msys = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]);
     520          list[N] = Msys - Mcal - Mmos - Mgrid;
     521          dlist[N] = MAX (catalog[i].measureT[m].dM, MIN_ERROR);
     522          N++;
     523        }
     524        if (N <= TOOFEW) { Nfew ++; continue; }
     525
     526        /* 3-sigma clip based on stats of inner 50% */
     527
     528        // calculated mean of inner 50%
     529        initstats ("INNER_MEAN");
     530        liststats (list, dlist, N, &stats);
     531        stats.sigma = MAX (MIN_ERROR, stats.sigma); /* if measurements agree too well, sigma -> 0.0 */
     532
     533        // ignore entries > 3sigma from inner mean
     534        for (k = m = 0; k < N; k++) {
     535          if (fabs (list[k] - stats.median) < NSIGMA_CLIP*stats.sigma) {
     536            list[m] = list[k];
     537            m++;
     538          }
     539        }
     540        // recalculate the mean & sigma of the accepted measurements
     541        initstats ("MEAN");
     542        liststats (list, dlist, m, &stats);
     543        stats.sigma = MAX (MIN_ERROR, stats.sigma);
     544
     545        /* apply to list of all relevant measurements, including IMAGE_POOR & IMAGE_FEW */
     546        image_bad = IMAGE_BAD;
     547        IMAGE_BAD = ID_IMAGE_PHOTOM_NOCAL;
     548        m = catalog[i].averageT[j].measureOffset;
     549        N = 0;
     550        for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     551          // skip measurements that do not match the current photcode
     552          int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     553          if (ecode != thisCode) { continue; }
     554
     555          /* if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue; */
     556          Mcal  = getMcal  (m, i);
     557          if (isnan(Mcal)) continue;
     558          Mmos  = getMmos  (m, i);
     559          if (isnan(Mmos)) continue;
     560          Mgrid = getMgrid (m, i);
     561          if (isnan(Mgrid)) continue;
     562
     563          Msys = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]);
     564          list[N] = Msys - Mcal - Mmos - Mgrid;
     565          dlist[N] = MAX (catalog[i].measureT[m].dM, MIN_ERROR);
     566          ilist[N] = m;
     567          N++;
     568          Nave ++;
     569        }
     570        if (N < TOOFEW) continue;
     571
     572        /* mark bad measures (> 3 sigma deviant) */
     573        for (k = 0; k < N; k++) {
     574          if (fabs (list[k] - stats.median) > NSIGMA_REJECT*stats.sigma) {
     575            catalog[i].measureT[ilist[k]].dbFlags |= ID_MEAS_POOR_PHOTOM;
     576            if (final) {
     577              // for the final pass, we have a duplicate set of values in measure and measureT
     578              catalog[i].measure[ilist[k]].dbFlags |= ID_MEAS_POOR_PHOTOM;
     579            }
     580            Ndel ++;
     581          }
     582        }
     583        IMAGE_BAD = image_bad;
     584      }
    507585    }
    508586  }
     
    513591}
    514592
    515 StatType statsStarN (Catalog *catalog, int Ncatalog) {
     593StatType statsStarN (Catalog *catalog, int Ncatalog, int Nsec, int seccode) {
    516594
    517595  off_t j, k, m, Ntot;
     
    520598  float Mcal, Mmos, Mgrid;
    521599  StatType stats;
     600  // int N1, N2, N3, N4, N0;
     601  // N1 = N2 = N3 = N4 = N0 = 0;
     602
     603  int Nsecfilt = GetPhotcodeNsecfilt ();
    522604
    523605  Ntot = 0;
     
    534616
    535617      /* calculate the average value for a single star */
    536       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    537       m = catalog[i].average[j].measureOffset;
     618      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) { continue;  }
     619      m = catalog[i].averageT[j].measureOffset;
    538620
    539621      N = 0;
    540       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
     622      for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
     623        int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     624        if (ecode != seccode) { continue;}
    541625        Mcal = getMcal  (m, i);
    542         if (isnan(Mcal)) continue;
     626        if (isnan(Mcal)) { continue;}
    543627        Mmos = getMmos  (m, i);
    544         if (isnan(Mmos)) continue;
     628        if (isnan(Mmos)) { continue; }
    545629        Mgrid = getMgrid (m, i);
    546         if (isnan(Mgrid)) continue;
     630        if (isnan(Mgrid)) { continue;}
    547631        N++;
    548632      }
     
    554638  }
    555639
     640  // fprintf (stderr, "N1: %d, N2: %d, N3: %d, N4: %d, N0: %d\n", N1, N2, N3, N4, N0);
    556641  liststats (list, dlist, n, &stats);
    557642  free (list);
     
    560645}
    561646
    562 StatType statsStarX (Catalog *catalog, int Ncatalog) {
     647// stats for a single secfilt at a time
     648StatType statsStarX (Catalog *catalog, int Ncatalog, int Nsec) {
    563649
    564650  off_t j, Ntot;
     
    567653  StatType stats;
    568654
     655  int Nsecfilt = GetPhotcodeNsecfilt ();
     656
    569657  Ntot = 0;
    570658  for (i = 0; i < Ncatalog; i++) {
     
    580668
    581669      /* calculate the average value for a single star */
    582       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    583 
    584       Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
     670      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     671       
     672      Xm = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
    585673      if (Xm == NAN_S_SHORT) continue;
    586674      list[n] = pow (10.0, 0.01*Xm);
     
    596684}
    597685
    598 StatType statsStarS (Catalog *catalog, int Ncatalog) {
     686StatType statsStarS (Catalog *catalog, int Ncatalog, int Nsec) {
    599687
    600688  int i, n;
     
    604692  StatType stats;
    605693
     694  int Nsecfilt = GetPhotcodeNsecfilt ();
     695
    606696  Ntot = 0;
    607697  for (i = 0; i < Ncatalog; i++) {
     
    617707
    618708      /* calculate the average value for a single star */
    619       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    620 
    621       dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
     709      if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     710
     711      dM = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
    622712      list[n] = dM;
    623713      dlist[n] = 1;
     
    644734  ALLOCATE (Mlist, double, NBIN);
    645735
    646   for (i = 0; i < NBIN; i++) xlist[i] = 0.00025*i;
    647   bzero (Mlist, NBIN*sizeof(double));
    648   for (i = 0; i < Ncatalog; i++) {
    649     for (j = 0; j < catalog[i].Naverage; j++) {
    650       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    651       dMrel = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
    652       bin = dMrel / 0.00025;
    653       bin = MAX (0, MIN (NBIN-1, bin));
    654       Mlist[bin] += 1.0;
    655     }
    656   }
    657 
    658   plot_defaults (&graphdata);
    659   graphdata.style = 1;
    660   plot_list (&graphdata, xlist, Mlist, NBIN, "dMrel hist", "%s.dMhist.png", OUTROOT);
    661 
     736  int Nsecfilt = GetPhotcodeNsecfilt ();
     737
     738  int Ns;
     739  for (Ns = 0; Ns < Nphotcodes; Ns++) {
     740
     741    int thisCode = photcodes[Ns][0].code;
     742    int Nsec = GetPhotcodeNsec(thisCode);
     743
     744    for (i = 0; i < NBIN; i++) xlist[i] = 0.00025*i;
     745    bzero (Mlist, NBIN*sizeof(double));
     746    for (i = 0; i < Ncatalog; i++) {
     747      for (j = 0; j < catalog[i].Naverage; j++) {
     748        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     749        dMrel = catalog[i].secfilt[Nsecfilt*j+Nsec].dM;
     750        bin = dMrel / 0.00025;
     751        bin = MAX (0, MIN (NBIN-1, bin));
     752        Mlist[bin] += 1.0;
     753      }
     754    }
     755
     756    plot_defaults (&graphdata);
     757    graphdata.style = 1;
     758    plot_list (&graphdata, xlist, Mlist, NBIN, "dMrel hist", "%s.dMhist.png", OUTROOT);
     759  }
    662760  free (xlist);
    663761  free (Mlist);
     
    671769  Graphdata graphdata;
    672770
     771  int Nsecfilt = GetPhotcodeNsecfilt ();
     772
    673773  Ntotal = 0;
    674774  for (i = 0; i < Ncatalog; i++) Ntotal += catalog[i].Naverage;
     
    677777  ALLOCATE (ylist, double, Ntotal);
    678778
    679   N = 0;
    680   for (i = 0; i < Ncatalog; i++) {
    681     for (j = 0; j < catalog[i].Naverage; j++) {
    682       if (catalog[i].average[j].flags & STAR_BAD) continue;
    683       xlist[N] = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
    684       value    = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
    685       if (value == NAN_S_SHORT) continue;
    686       ylist[N] = 0.01*value;
    687       N++;
    688     }
    689   }
    690 
    691   plot_defaults (&graphdata);
    692   graphdata.ymin = -3.0;
    693   plot_list (&graphdata, xlist, ylist, N, "chisq", "%s.chisq.png", OUTROOT);
     779  int Ns;
     780  for (Ns = 0; Ns < Nphotcodes; Ns++) {
     781
     782    int thisCode = photcodes[Ns][0].code;
     783    int Nsec = GetPhotcodeNsec(thisCode);
     784
     785    N = 0;
     786    for (i = 0; i < Ncatalog; i++) {
     787      for (j = 0; j < catalog[i].Naverage; j++) {
     788        if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue;
     789        xlist[N] = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
     790        value    = catalog[i].secfilt[Nsecfilt*j+Nsec].Xm;
     791        if (value == NAN_S_SHORT) continue;
     792        ylist[N] = 0.01*value;
     793        N++;
     794      }
     795    }
     796
     797    plot_defaults (&graphdata);
     798    graphdata.ymin = -3.0;
     799    plot_list (&graphdata, xlist, ylist, N, "chisq", "%s.chisq.png", OUTROOT);
     800  }
     801
    694802  free (xlist);
    695803  free (ylist);
     
    716824  for (i = 0; i < Ncatalog; i++) {
    717825    for (j = 0; j < catalog[i].Naverage; j++) {
    718       xlist[N] = catalog[i].average[j].R;
    719       ylist[N] = catalog[i].average[j].D;
     826      xlist[N] = catalog[i].averageT[j].R;
     827      ylist[N] = catalog[i].averageT[j].D;
    720828      N++;
    721829    }
  • trunk/Ohana/src/relphot/src/args.c

    r31160 r31450  
    128128  }
    129129
     130  MaxDensityUse = FALSE;
     131  if ((N = get_argument (argc, argv, "-max-density"))) {
     132    remove_argument (N, &argc, argv);
     133    MaxDensityValue = atof(argv[N]);
     134    remove_argument (N, &argc, argv);
     135    MaxDensityUse = TRUE;
     136  }
     137
    130138  SHOW_PARAMS = FALSE;
    131139  if ((N = get_argument (argc, argv, "-params"))) {
  • trunk/Ohana/src/relphot/src/bcatalog.c

    r31160 r31450  
    11# include "relphot.h"
     2
     3extern double drand48();
     4
     5int CopyAverageTiny (AverageTiny *averageT, Average *average) {
     6
     7  averageT[0].R     = average[0].R;
     8  averageT[0].D     = average[0].D;
     9  averageT[0].flags = average[0].flags;
     10  averageT[0].Nmeasure      = average[0].Nmeasure;
     11  averageT[0].measureOffset = average[0].measureOffset;
     12
     13  // make Nmeasure & measureOffset optional?
     14
     15  return (TRUE);
     16}
     17
     18int CopyMeasureTiny (MeasureTiny *measureT, Measure *measure) {
     19
     20  measureT[0].dR       = measure[0].dR;
     21  measureT[0].dD       = measure[0].dD;
     22  measureT[0].M        = measure[0].M;
     23  measureT[0].Mcal     = measure[0].Mcal;
     24  measureT[0].dM       = measure[0].dM;
     25  measureT[0].airmass  = measure[0].airmass;
     26  measureT[0].Xccd     = measure[0].Xccd;
     27  measureT[0].Yccd     = measure[0].Yccd;
     28  measureT[0].t        = measure[0].t;
     29  measureT[0].dt       = measure[0].dt;
     30  measureT[0].averef   = measure[0].averef;
     31  measureT[0].imageID  = measure[0].imageID;
     32  measureT[0].dbFlags  = measure[0].dbFlags;
     33  measureT[0].photcode = measure[0].photcode;
     34
     35  return (TRUE);
     36}
    237
    338int bcatalog (Catalog *subcatalog, Catalog *catalog) {
    439 
    540  off_t i, j, offset;
    6   int ecode;
     41  int ecode, found, Ns;
    742  off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm;
    843  float mag;
    944  int Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew, Ngalaxy, Npsfqf;
    1045
    11   // XXX PhotNsec as a global is a bad idea; either get it from catalog
    12   // or get it from:
    13   // Nsecfilt = GetPhotcodeNsecfilt ();
    14   // assert (catalog[0].Nsecfilt == Nsecfilt);
     46  int Nsecfilt = GetPhotcodeNsecfilt ();
     47  assert (Nsecfilt == catalog[0].Nsecfilt);
    1548
    1649  /* we are moving only the subset of measurements from catalog[0] to subcatalog[0] */
     
    1851  NAVERAGE = 50;
    1952  NMEASURE = 1000;
    20   ALLOCATE (subcatalog[0].average, Average, NAVERAGE);
    21   ALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*PhotNsec);
    22   ALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
     53  ALLOCATE (subcatalog[0].measureT, MeasureTiny, NMEASURE);
     54  ALLOCATE (subcatalog[0].averageT, AverageTiny, NAVERAGE);
     55  ALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt);
    2356  Nmeasure = Naverage = 0;
    2457
     
    3063
    3164    /* start with all stars good */
    32     subcatalog[0].average[Naverage] = catalog[0].average[i];
    33     subcatalog[0].average[Naverage].measureOffset = Nmeasure;
    34     for (j = 0; j < PhotNsec; j++) {
    35       subcatalog[0].secfilt[PhotNsec*Naverage+j] = catalog[0].secfilt[PhotNsec*i+j];
     65    CopyAverageTiny (&subcatalog[0].averageT[Naverage], &catalog[0].average[i]);
     66    subcatalog[0].averageT[Naverage].measureOffset = Nmeasure;
     67
     68    for (j = 0; j < Nsecfilt; j++) {
     69      subcatalog[0].secfilt[Nsecfilt*Naverage+j] = catalog[0].secfilt[Nsecfilt*i+j];
    3670    }
    3771
    3872    if (RESET) {
    39       subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].M  = NAN;
    40       subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].dM = NAN;
    41       subcatalog[0].average[Naverage].flags &= ~ID_STAR_FEW;
    42       subcatalog[0].average[Naverage].flags &= ~ID_STAR_POOR;
     73      int Ns;
     74      for (Ns = 0; Ns < Nphotcodes; Ns++) {
     75
     76        int thisCode = photcodes[Ns][0].code;
     77        int Nsec = GetPhotcodeNsec(thisCode);
     78
     79        subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].M  = NAN;
     80        subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].dM = NAN;
     81        subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].flags &= ~ID_STAR_FEW;
     82        subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].flags &= ~ID_STAR_POOR;
     83      }
    4384    }
    4485
     
    5293      /* select measurements by photcode */
    5394      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode);
    54       if (ecode != photcode[0].code) { Ncode ++; continue; }
     95      found = FALSE;
     96      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     97        if (ecode == photcodes[Ns][0].code) found = TRUE;
     98      }
     99      if (!found) {
     100        Ncode ++;
     101        continue;
     102      }
    55103
    56104      /* select measurements by time */
     
    61109
    62110      /* select measurements by quality */
    63       // XXX ignore this criterion for REF measurements?
    64       // XXX chnage this to select by bitflags
    65111      if (DophotSelect && ((catalog[0].measure[offset].photFlags >> 16) != DophotValue)) { Ndophot ++; continue; }
    66112
     
    70116      // check for galaxies
    71117      if (!isnan(catalog[0].measure[offset].Map)) {
    72           if (catalog[0].measure[offset].M - catalog[0].measure[offset].Map > 0.15) {
    73               nEXT ++;
    74           } else {
    75               nPSF ++;
    76           }
     118        if (catalog[0].measure[offset].M - catalog[0].measure[offset].Map > 0.15) {
     119          nEXT ++;
     120        } else {
     121          nPSF ++;
     122        }
    77123      }
    78124
     
    91137      }
    92138
    93       subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
    94       subcatalog[0].measure[Nmeasure]        = catalog[0].measure[offset];
    95       subcatalog[0].measure[Nmeasure].averef = Naverage;
     139      CopyMeasureTiny (&subcatalog[0].measureT[Nmeasure], &catalog[0].measure[offset]);
     140      subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
     141      subcatalog[0].measureT[Nmeasure].averef = Naverage;
    96142      if (RESET) {
    97         subcatalog[0].measure[Nmeasure].Mcal = 0;
    98         subcatalog[0].measure[Nmeasure].dbFlags &= 0xff00;
    99         subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_POOR_PHOTOM;
    100         subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_AREA;
    101         subcatalog[0].measure[Nmeasure].dbFlags &= ~ID_MEAS_NOCAL;
     143        subcatalog[0].measureT[Nmeasure].Mcal = 0;
     144        subcatalog[0].measureT[Nmeasure].dbFlags &= 0xff00;
     145        subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_POOR_PHOTOM;
     146        subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_AREA;
     147        subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_NOCAL;
    102148      }
    103149      Nmeasure ++;
     
    105151      if (Nmeasure == NMEASURE) {
    106152        NMEASURE += 1000;
    107         REALLOCATE (subcatalog[0].measure, Measure, NMEASURE);
     153        REALLOCATE (subcatalog[0].measureT, MeasureTiny, NMEASURE);
    108154      }
    109155    }
     
    122168      continue;
    123169    }
    124     subcatalog[0].average[Naverage].Nmeasure = Nm;
     170    subcatalog[0].averageT[Naverage].Nmeasure = Nm;
    125171    Naverage ++;
    126172    if (Naverage == NAVERAGE) {
    127173      NAVERAGE += 50;
    128       REALLOCATE (subcatalog[0].average, Average, NAVERAGE);
    129       REALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*PhotNsec);
    130     }
    131   }
    132   REALLOCATE (subcatalog[0].average, Average, MAX (Naverage, 1));
    133   REALLOCATE (subcatalog[0].measure, Measure, MAX (Nmeasure, 1));
    134   REALLOCATE (subcatalog[0].secfilt, SecFilt, PhotNsec*MAX (Naverage, 1));
     174      REALLOCATE (subcatalog[0].averageT, AverageTiny, NAVERAGE);
     175      REALLOCATE (subcatalog[0].secfilt,  SecFilt,     NAVERAGE*Nsecfilt);
     176    }
     177  }
     178  REALLOCATE (subcatalog[0].averageT, AverageTiny, MAX (Naverage, 1));
     179  REALLOCATE (subcatalog[0].measureT, MeasureTiny, MAX (Nmeasure, 1));
     180  REALLOCATE (subcatalog[0].secfilt, SecFilt, Nsecfilt*MAX (Naverage, 1));
    135181  subcatalog[0].Naverage = Naverage;
    136182  subcatalog[0].Nmeasure = Nmeasure;
    137183  subcatalog[0].Nsecfilt = catalog[0].Nsecfilt;
    138184  subcatalog[0].Nsecf_mem = Naverage * catalog[0].Nsecfilt;
    139   assert (PhotNsec == catalog[0].Nsecfilt);
     185
     186  // limit the total number of stars in the catalog
     187  if (MaxDensityUse) {
     188    LimitDensityCatalog (subcatalog, catalog);
     189  }
    140190
    141191  if (VERBOSE) {
     
    147197  return (TRUE);
    148198}
     199
     200int LimitDensityCatalog (Catalog *subcatalog, Catalog *catalog) {
     201
     202  Catalog tmpcatalog;
     203
     204  double Rmin, Rmax, Dmin, Dmax;
     205
     206  int Nsecfilt = GetPhotcodeNsecfilt ();
     207
     208  gfits_scan (&catalog[0].header, "RA0",  "%lf", 1, &Rmin);
     209  gfits_scan (&catalog[0].header, "DEC0", "%lf", 1, &Dmin);
     210  gfits_scan (&catalog[0].header, "RA1",  "%lf", 1, &Rmax);
     211  gfits_scan (&catalog[0].header, "DEC1", "%lf", 1, &Dmax);
     212
     213  if (VERBOSE2) fprintf (stderr, "extracting from catalog covering region %f,%f to %f,%f\n", Rmin, Dmin, Rmax, Dmax);
     214
     215  float AREA = fabs(Dmax - Dmin) * fabs(Rmax - Rmin) * cos (0.5*RAD_DEG*(Dmax + Dmin));
     216  assert (AREA > 0);
     217
     218  off_t Nmax = MaxDensityValue * AREA;
     219  if (subcatalog[0].Naverage <= Nmax) {
     220    if (VERBOSE) {
     221      fprintf (stderr, "subcatalog has less than the max density\n");
     222    }
     223    return (TRUE);
     224  }
     225
     226  off_t Naverage = subcatalog[0].Naverage;
     227
     228  // select a random subset of Nmax stars from subcatalog using Fisher-Yates
     229
     230  // we are going to select Nmax entries by generating a random-sorted index list
     231  off_t *index, tmp, i, j, ave;
     232  ALLOCATE (index, off_t, Naverage);
     233  for (i = 0; i < Naverage; i++) {
     234    index[i] = i;
     235  }
     236  for (i = 0; i < Naverage; i++) {
     237    j = (Naverage - i) * drand48() + i; // a number between i and Naverage
     238    tmp = index[j];
     239    index[j] = index[i];
     240    index[i] = tmp;
     241  }
     242
     243  // count the number of measurements this selection will yield
     244  off_t NMEASURE = 0;
     245  for (i = 0; i < Nmax; i++) {
     246    ave = index[i];
     247    NMEASURE += subcatalog[0].averageT[ave].Nmeasure;
     248  }
     249
     250  // allocate the output data
     251  ALLOCATE (tmpcatalog.averageT, AverageTiny, Nmax);
     252  ALLOCATE (tmpcatalog.measureT, MeasureTiny, NMEASURE);
     253  ALLOCATE (tmpcatalog.secfilt,  SecFilt, Nmax * Nsecfilt);
     254
     255  off_t Nmeasure = 0;
     256
     257  // copy the Nmax selected entries from subcatalog to tmpcatalog (adjusting links)
     258  for (i = 0; i < Nmax; i++) {
     259    ave = index[i];
     260    tmpcatalog.averageT[i] = subcatalog[0].averageT[ave];
     261    tmpcatalog.averageT[i].measureOffset = Nmeasure;
     262    for (j = 0; j < tmpcatalog.averageT[i].Nmeasure; j++) {
     263      off_t offset = subcatalog[0].averageT[ave].measureOffset + j;
     264      tmpcatalog.measureT[Nmeasure] = subcatalog[0].measureT[offset];
     265      tmpcatalog.measureT[Nmeasure].averef = i;
     266      Nmeasure ++;
     267    }
     268  }
     269
     270  if (VERBOSE) {
     271    fprintf (stderr, "limited to "OFF_T_FMT" of "OFF_T_FMT" stars ("OFF_T_FMT" of "OFF_T_FMT" measures) for catalog %s\n",
     272             Nmax, subcatalog[0].Naverage, Nmeasure, subcatalog[0].Nmeasure,  catalog[0].filename);
     273  }
     274
     275  free (subcatalog[0].averageT);
     276  free (subcatalog[0].measureT);
     277  free (subcatalog[0].secfilt);
     278
     279  subcatalog[0].averageT = tmpcatalog.averageT;
     280  subcatalog[0].measureT = tmpcatalog.measureT;
     281  subcatalog[0].secfilt = tmpcatalog.secfilt;
     282  subcatalog[0].Naverage = Nmax;
     283  subcatalog[0].Nmeasure = Nmeasure;
     284  subcatalog[0].Nsecfilt = catalog[0].Nsecfilt;
     285  subcatalog[0].Nsecf_mem = Naverage * catalog[0].Nsecfilt;
     286
     287  return (TRUE);
     288}
     289
     290// for the cases where we are not using a subset of the data, we still need to have a copy of these fields
     291int populate_tiny_values (Catalog *catalog) {
     292
     293  off_t i;
     294
     295  ALLOCATE (catalog[0].measureT, MeasureTiny, catalog[0].Nmeasure);
     296  ALLOCATE (catalog[0].averageT, AverageTiny, catalog[0].Naverage);
     297
     298  for (i = 0; i < catalog[0].Naverage; i++) {
     299    CopyAverageTiny (&catalog[0].averageT[i], &catalog[0].average[i]);
     300  }
     301
     302  for (i = 0; i < catalog[0].Nmeasure; i++) {
     303    CopyMeasureTiny (&catalog[0].measureT[i], &catalog[0].measure[i]);
     304  }
     305
     306  return (TRUE);
     307}
     308
     309int free_tiny_values (Catalog *catalog) {
     310
     311  free (catalog[0].averageT);
     312  free (catalog[0].measureT);
     313  return (TRUE);
     314}
     315
  • trunk/Ohana/src/relphot/src/global_stats.c

    r5143 r31450  
    77  initstats ("MEAN");
    88
    9   stN = statsStarN (catalog, Ncatalog);
    10   stX = statsStarX (catalog, Ncatalog);
    11   stS = statsStarS (catalog, Ncatalog);
     9  fprintf (stderr, "\n");
     10  fprintf (stderr, "STATS            median     mean    sigma      min      max   Nmeas\n");
    1211
     12  int Ns;
     13  for (Ns = 0; Ns < Nphotcodes; Ns++) {
     14
     15    int thisCode = photcodes[Ns][0].code;
     16    int Nsec = GetPhotcodeNsec(thisCode);
     17    int seccode = photcodes[Ns][0].code;
     18
     19    stN = statsStarN (catalog, Ncatalog, Nsec, seccode);
     20    stX = statsStarX (catalog, Ncatalog, Nsec);
     21    stS = statsStarS (catalog, Ncatalog, Nsec);
     22 
     23    fprintf (stderr, "   --- stats for %s ---\n", photcodes[Ns][0].name);
     24    fprintf (stderr, "meas / star:    %7.0f  %7.1f  %7.1f  %7.0f  %7.0f  %6d\n",   stN.median, stN.mean, stN.sigma, stN.min, stN.max, stN.Nmeas);
     25    fprintf (stderr, "dMrel star:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n", stS.median, stS.mean, stS.sigma, stS.min, stS.max, stS.Nmeas);
     26    fprintf (stderr, "chisq star:     %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   stX.median, stX.mean, stX.sigma, stX.min, stX.max, stX.Nmeas);
     27  }
     28 
    1329  imN = statsImageN (catalog);
    1430  imX = statsImageX (catalog);
    1531  imM = statsImageM (catalog);
    1632  imD = statsImagedM (catalog);
    17 
     33 
    1834  msN = statsMosaicN (catalog);
    1935  msM = statsMosaicM (catalog);
    2036  msD = statsMosaicdM (catalog);
    2137  msX = statsMosaicX (catalog);
    22 
    23   fprintf (stderr, "STATS            median     mean    sigma      min      max   Nmeas\n");
     38 
    2439  fprintf (stderr, "meas / image:   %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   imN.median, imN.mean, imN.sigma, imN.min, imN.max, imN.Nmeas);
    25   fprintf (stderr, "meas / mosaic:  %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   msN.median, msN.mean, msN.sigma, msN.min, msN.max, msN.Nmeas);
    26   fprintf (stderr, "meas / star:    %7.0f  %7.1f  %7.1f  %7.0f  %7.0f  %6d\n",   stN.median, stN.mean, stN.sigma, stN.min, stN.max, stN.Nmeas);
    27 
    28   fprintf (stderr, "chisq image:    %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   imX.median, imX.mean, imX.sigma, imX.min, imX.max, imX.Nmeas);
    29   fprintf (stderr, "chisq mosaic:   %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   msX.median, msX.mean, msX.sigma, msX.min, msX.max, msX.Nmeas);
    30   fprintf (stderr, "chisq star:     %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   stX.median, stX.mean, stX.sigma, stX.min, stX.max, stX.Nmeas);
    31 
    3240  fprintf (stderr, "Mcal image:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imM.median, imM.mean, imM.sigma, imM.min, imM.max, imM.Nmeas);
    3341  fprintf (stderr, "dMcal image:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   imD.median, imD.mean, imD.sigma, imD.min, imD.max, imD.Nmeas);
     42  fprintf (stderr, "chisq image:    %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   imX.median, imX.mean, imX.sigma, imX.min, imX.max, imX.Nmeas);
    3443
     44  fprintf (stderr, "meas / mosaic:  %7.0f  %7.0f  %7.0f  %7.0f  %7.0f  %6d\n",   msN.median, msN.mean, msN.sigma, msN.min, msN.max, msN.Nmeas);
    3545  fprintf (stderr, "Mcal mosaic:    %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msM.median, msM.mean, msM.sigma, msM.min, msM.max, msM.Nmeas);
    3646  fprintf (stderr, "dMcal mosaic:   %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n",   msD.median, msD.mean, msD.sigma, msD.min, msD.max, msD.Nmeas);
    37 
    38   fprintf (stderr, "dMrel star:     %7.4f  %7.4f  %7.4f  %7.4f  %7.4f  %6d\n\n", stS.median, stS.mean, stS.sigma, stS.min, stS.max, stS.Nmeas);
     47  fprintf (stderr, "chisq mosaic:   %7.1f  %7.1f  %7.1f  %7.1f  %7.1f  %6d\n",   msX.median, msX.mean, msX.sigma, msX.min, msX.max, msX.Nmeas);
     48 
    3949
    4050  initstats (STATMODE);
  • trunk/Ohana/src/relphot/src/initialize.c

    r30616 r31450  
    1313    N = UserPatchSelect ? 1 : 2;
    1414
     15# if (0)
     16    // XXX DEP
    1517    if ((photcode = GetPhotcodebyName (argv[N])) == NULL) {
    1618      fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", argv[N]);
     
    2123      exit (1);
    2224    }
     25    // PhotSec is used to select the single average photcode being processed
    2326    PhotSec = GetPhotcodeNsec (photcode[0].code);
     27# endif
     28
     29    Nphotcodes = 0;
     30    photcodes = NULL;
     31    int NPHOTCODES = 10;
     32    ALLOCATE (photcodes, PhotCode *, NPHOTCODES);
     33
     34    /* parse the comma-separated list of photcodesKeep */
     35    char *myList = strcreate(argv[N]);
     36    char *list = myList;
     37    char *codename = NULL;
     38    char *ptr = NULL;
     39    while ((codename = strtok_r (list, ",", &ptr)) != NULL) {
     40      list = NULL; // pass NULL on successive strtok_r calls
     41      fprintf (stderr, "PHOTCODE LIST: %s\n", myList);
     42      fprintf (stderr, "codename: %s\n", codename);
     43      if ((photcodes[Nphotcodes] = GetPhotcodebyName (codename)) == NULL) {
     44        fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
     45        exit (1);
     46      }
     47      if (photcodes[Nphotcodes][0].type != PHOT_SEC) {
     48          fprintf (stderr, "photcode %s is not an filter type (SEC)\n", codename);
     49          exit (1);
     50      }
     51      Nphotcodes ++;
     52      CHECK_REALLOCATE (photcodes, PhotCode *, NPHOTCODES, Nphotcodes, 10);
     53    }
    2454  }
    25 
    26   PhotNsec = GetPhotcodeNsecfilt ();
     55  // XXX DEP PhotNsec = GetPhotcodeNsecfilt ();
     56  if (USE_GRID && (Nphotcodes > 1)) {
     57    fprintf (stderr, "grid correction analysis currently can only operate on a single photcode\n");
     58    exit (1);
     59  }
    2760
    2861  initstats (STATMODE);
     
    5588    exit (0);
    5689  }
     90
     91  // init the random seed
     92  long A, B;
     93  A = time(NULL);
     94  for (B = 0; A == time(NULL); B++);
     95  srand48(B);
    5796}
    58 
  • trunk/Ohana/src/relphot/src/load_images.c

    r31160 r31450  
    4444  // select the images which overlap the selected sky regions
    4545  subset = select_images (skylist, image, Nimage, &LineNumber, &Nsubset);
    46   MARKTIME("selected images: %f sec\n", dtime);
     46  MARKTIME("selected %d overlapping images: %f sec\n", (int) Nsubset, dtime);
    4747
    4848  // generate db->vtable from db->ftable based on the selection
  • trunk/Ohana/src/relphot/src/plot_scatter.c

    r27435 r31450  
    1818  ALLOCATE (ilist, double, Ntot);
    1919
    20   N = 0;
    21   for (i = 0; i < Ncatalog; i++) {
    22     for (j = 0; j < catalog[i].Naverage; j++) {
     20  int Nsecfilt = GetPhotcodeNsecfilt ();
    2321
    24       /* calculate the average value for a single star */
    25       if (catalog[i].average[j].flags & STAR_BAD) continue; 
    26       m = catalog[i].average[j].measureOffset;
     22  int Ns;
     23  for (Ns = 0; Ns < Nphotcodes; Ns++) {
    2724
    28       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
    29         if (catalog[i].measure[m].dbFlags & MEAS_BAD) continue;
    30         Mcal = getMcal  (m, i);
    31         if (isnan(Mcal)) continue;
    32         Mmos = getMmos  (m, i);
    33         if (isnan(Mmos)) continue;
    34         Mgrid = getMgrid (m, i);
    35         if (isnan(Mgrid)) continue;
     25    int thisCode = photcodes[Ns][0].code;
     26    int Nsec = GetPhotcodeNsec(thisCode);
    3627
    37         Mrel = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
    38         xlist[N] = Mrel;
    39         ylist[N] = PhotSys  (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]) - Mcal - Mmos - Mgrid - Mrel;
    40         ilist[N] = PhotInst (&catalog[i].measure[m]);
    41         N++;
    42       }
     28    N = 0;
     29    for (i = 0; i < Ncatalog; i++) {
     30        for (j = 0; j < catalog[i].Naverage; j++) {
     31
     32            /* calculate the average value for a single star */
     33            if (catalog[i].secfilt[Nsecfilt*j+Nsec].flags & STAR_BAD) continue; 
     34            m = catalog[i].average[j].measureOffset;
     35
     36            for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
     37                // skip measurements that do not match the current photcode
     38                int ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     39                if (ecode != thisCode) { continue; }
     40
     41                if (catalog[i].measureT[m].dbFlags & MEAS_BAD) continue;
     42                Mcal = getMcal  (m, i);
     43                if (isnan(Mcal)) continue;
     44                Mmos = getMmos  (m, i);
     45                if (isnan(Mmos)) continue;
     46                Mgrid = getMgrid (m, i);
     47                if (isnan(Mgrid)) continue;
     48
     49                Mrel = catalog[i].secfilt[Nsecfilt*j+Nsec].M;
     50                xlist[N] = Mrel;
     51                ylist[N] = PhotSysTiny (&catalog[i].measureT[m], &catalog[i].averageT[j], &catalog[i].secfilt[j*Nsecfilt]) - Mcal - Mmos - Mgrid - Mrel;
     52                ilist[N] = PhotInstTiny (&catalog[i].measureT[m]);
     53                N++;
     54            }
     55        }
    4356    }
     57
     58    plot_defaults (&graphdata);
     59    graphdata.xmin = PlotMmin;
     60    graphdata.xmax = PlotMmax;
     61    graphdata.ymin = PlotdMmin;
     62    graphdata.ymax = PlotdMmax;
     63    plot_list (&graphdata, xlist, ylist, N, "mag vs dmag", "%s.Mag.png", OUTROOT);
     64
     65    plot_defaults (&graphdata);
     66    graphdata.ymin = PlotdMmin;
     67    graphdata.ymax = PlotdMmax;
     68    plot_list (&graphdata, ilist, ylist, N, "imag vs dmag", "%s.iMag.png", OUTROOT);
    4469  }
    45 
    46   plot_defaults (&graphdata);
    47   graphdata.xmin = PlotMmin;
    48   graphdata.xmax = PlotMmax;
    49   graphdata.ymin = PlotdMmin;
    50   graphdata.ymax = PlotdMmax;
    51   plot_list (&graphdata, xlist, ylist, N, "mag vs dmag", "%s.Mag.png", OUTROOT);
    52 
    53   plot_defaults (&graphdata);
    54   graphdata.ymin = PlotdMmin;
    55   graphdata.ymax = PlotdMmax;
    56   plot_list (&graphdata, ilist, ylist, N, "imag vs dmag", "%s.iMag.png", OUTROOT);
    5770  free (xlist);
    5871  free (ylist);
  • trunk/Ohana/src/relphot/src/reload_catalogs.c

    r15743 r31450  
    11# include "relphot.h"
     2
     3# define TIMESTAMP(TIME) \
     4    gettimeofday (&stop, (void *) NULL);        \
     5    dtime = DTIME (stop, start);                \
     6    TIME += dtime;                              \
     7    gettimeofday (&start, (void *) NULL);
    28
    39void reload_catalogs (SkyList *skylist) {
     
    814  Catalog catalog;
    915
     16  struct timeval start, stop;
     17  double dtime = 0.0;
     18  double time1 = 0.0;
     19  double time2 = 0.0;
     20  double time3 = 0.0;
     21  double time4 = 0.0;
     22  double time5 = 0.0;
     23  double time6 = 0.0;
     24  double time7 = 0.0;
     25
    1026  if (VERBOSE) fprintf (stderr, "re-loading catalog data\n");
    1127
    1228  /* load data from each region file */
    1329  for (i = 0; i < skylist[0].Nregions; i++) {
     30    gettimeofday (&start, (void *) NULL);
    1431    catalog.filename = skylist[0].filename[i];
    1532
     
    2037      continue;
    2138    }
     39    TIMESTAMP(time1);
    2240
    2341    catalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
     
    3654        continue;
    3755    }
     56    TIMESTAMP(time2);
     57
     58    populate_tiny_values(&catalog);
    3859
    3960    initImageBins  (&catalog, 1);
    4061    initMosaicBins (&catalog, 1);
    41     initGridBins   (&catalog, 1);
     62    initGridBins   (&catalog, 1);
     63    TIMESTAMP(time3);
    4264
    43     findImages (&catalog, 1);
    44     findMosaics (&catalog, 1);
     65    findImages (&catalog, 1);  // FX
     66    findMosaics (&catalog, 1); //
     67    TIMESTAMP(time4);
    4568
    4669    setMrelFinal (&catalog);
     70    TIMESTAMP(time5);
     71
    4772    dvo_catalog_save (&catalog, VERBOSE);
    4873    dvo_catalog_unlock (&catalog);
     74
     75    free_tiny_values(&catalog);
    4976    dvo_catalog_free (&catalog);
     77    TIMESTAMP(time6);
    5078
    5179    freeImageBins (1);
    5280    freeMosaicBins (1);
    5381    freeGridBins (1);
     82    TIMESTAMP(time7);
    5483  }
     84
     85  fprintf (stderr, "time1 %f : find catalog\n", time1);
     86  fprintf (stderr, "time2 %f : load catalog\n", time2);
     87  fprintf (stderr, "time3 %f : init imbins\n",  time3);
     88  fprintf (stderr, "time4 %f : find images\n",  time4);
     89  fprintf (stderr, "time5 %f : set Mrel\n",     time5);
     90  fprintf (stderr, "time6 %f : save catalog\n", time6);
     91  fprintf (stderr, "time7 %f : free catalog\n", time7);
    5592}
  • trunk/Ohana/src/relphot/src/relphot.c

    r30616 r31450  
    4747    if (!dvo_image_load (&db, VERBOSE, FALSE)) Shutdown ("can't read image catalog %s", db.filename);
    4848  }
    49   MARKTIME("load image data: %f sec\n", dtime);
     49  MARKTIME("-- load image data: %f sec\n", dtime);
    5050
    5151  /* load regions and images based on specified sky patch */
     
    5454  // XXX this is fairly lame: argv[1] is photcode if UserPatchSelect is true
    5555  skylist = load_images (&db, argv[1], &UserPatch, UserPatchSelect);
    56   MARKTIME("load images: %f sec\n", dtime);
     56  MARKTIME("-- load images: %f sec\n", dtime);
    5757
    5858  /* unlock, if we can (else, unlocked below) */
     
    6161  /* load catalog data from region files */
    6262  catalog = load_catalogs (skylist, &Ncatalog);
    63   MARKTIME("load catalog data: %f sec\n", dtime);
     63  MARKTIME("-- load catalog data: %f sec\n", dtime);
    6464 
    6565  /* add in a loop over the catalogs calling dvo_catalog_chipcoords */
     
    6767  /* match measurements with images, mosaics */
    6868  initImageBins  (catalog, Ncatalog);
    69   MARKTIME("make image bins: %f sec\n", dtime);
     69  MARKTIME("-- make image bins: %f sec\n", dtime);
    7070
    7171  initMosaicBins (catalog, Ncatalog);
     
    7474
    7575  findImages (catalog, Ncatalog);
    76   MARKTIME("set up image indexes: %f sec\n", dtime);
     76  MARKTIME("-- set up image indexes: %f sec\n", dtime);
    7777
    7878  findMosaics (catalog, Ncatalog);  /* also sets Grid values */
    79   MARKTIME("set up mosaic indexes: %f sec\n", dtime);
     79  MARKTIME("-- set up mosaic indexes: %f sec\n", dtime);
    8080
    8181  SAVEPLOT = FALSE;
    8282
    8383  setExclusions (catalog, Ncatalog);
     84
     85  global_stats (catalog, Ncatalog);
    8486
    8587  if (PLOTSTUFF) {
     
    9193  if (USE_GRID) {
    9294      int star_toofew;
    93 
    94 # if (USE_DIRECT)
    95       // until we finish the grid analysis, do not reject stars out-of-hand based on ID_STAR_FEW
    96       // XXX this is kind of poor: need to have a better distinctions about STAR_BAD in setMrel vs getMrel
    97       star_toofew = STAR_TOOFEW;
    98       STAR_TOOFEW = 0;
    99       STAR_BAD  = ID_STAR_POOR;
    100 
    101       showGridCount ();
    102       setMgridDirect (catalog, Ncatalog);
    103 
    104       STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    105       STAR_TOOFEW = star_toofew;
    106 
    107       dump_grid ();
    108       exit (0);
    109 
    110 # else
    11195
    11296      // until we finish the grid analysis, do not reject stars out-of-hand based on ID_STAR_FEW
     
    122106      STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
    123107      STAR_TOOFEW = star_toofew;
    124 # endif
    125108  }
    126109
     
    140123      plot_chisq (catalog, Ncatalog);
    141124    }
    142     if ((i == 1) || (i == 5) || (i ==  9) || (i == 13)) clean_measures (catalog, Ncatalog, FALSE);
    143     if ((i == 2) || (i == 6) || (i == 10) || (i == 14)) clean_stars (catalog, Ncatalog);
    144     if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_mosaics ();
    145     if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
     125    if (i % 6 == 1) rationalize_mosaics (catalog, Ncatalog);
     126    // if (i % 6 == 1) rationalize_images ();
     127    if (i % 6 == 2) clean_measures (catalog, Ncatalog, FALSE);
     128    if (i % 6 == 3) clean_stars (catalog, Ncatalog);
     129    if (i % 6 == 5) clean_mosaics ();
     130    if (i % 6 == 5) clean_images ();
     131
     132    // if ((i == 1) || (i == 5) || (i ==  9) || (i == 13)) clean_measures (catalog, Ncatalog, FALSE);
     133    // if ((i == 2) || (i == 6) || (i == 10) || (i == 14)) clean_stars (catalog, Ncatalog);
     134    // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_mosaics ();
     135    // if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
    146136    global_stats (catalog, Ncatalog);
     137    MARKTIME("-- finished loop %d: %f sec\n", i, dtime);
    147138  }
    148139
    149   SAVEPLOT = TRUE;
    150   plot_scatter (catalog, Ncatalog);
    151   plot_grid (catalog);
    152   plot_mosaics ();
    153   plot_images ();
    154   plot_stars (catalog, Ncatalog);
    155   plot_chisq (catalog, Ncatalog);
    156 
     140  if (PLOTSTUFF) {
     141    plot_scatter (catalog, Ncatalog);
     142    plot_grid (catalog);
     143    plot_mosaics ();
     144    plot_images ();
     145    plot_stars (catalog, Ncatalog);
     146    plot_chisq (catalog, Ncatalog);
     147  }
     148 
    157149  if (USE_GRID) dump_grid ();
    158150  if (!UPDATE) exit (0);
     
    161153  setMcal  (catalog, TRUE);
    162154  setMmos  (catalog, TRUE);
     155  MARKTIME("-- finalize Mcal values: %f sec\n", dtime);
    163156
    164157  /* at this point, we have correct cal coeffs in the image/mosaic structures */
    165   for (i = 0; i < Ncatalog; i++) dvo_catalog_free (&catalog[i]);
     158  for (i = 0; i < Ncatalog; i++) {
     159    free_tiny_values (&catalog[i]);
     160    dvo_catalog_free (&catalog[i]);
     161  }
    166162  freeImageBins (Ncatalog);
    167163  freeMosaicBins (Ncatalog);
     
    170166  /* load catalog data from region files, update Mrel include all data */
    171167  reload_catalogs (skylist);
     168  MARKTIME("-- updated all catalogs: %f sec\n", dtime);
     169
    172170  setMcalFinal ();
    173 
    174171  reload_images (&db);
    175172 
  • trunk/Ohana/src/relphot/src/relphot_objects.c

    r28241 r31450  
    33int relphot_objects () {
    44
    5   off_t i, j, k, m;
     5  off_t i, j, k;
    66  int Nsecfilt;
    77
     
    3838    }
    3939
    40     // XXX consider what gets reset (only PHOTOM flags)
     40    // reset
    4141    if (RESET) {
    4242      Nsecfilt = catalog.Nsecfilt;
     
    5151          catalog.secfilt[j*Nsecfilt + k].Ncode = 0;
    5252          catalog.secfilt[j*Nsecfilt + k].Nused = 0;
    53         }
    54         m = catalog.average[j].measureOffset;
    55         for (k = 0; k < catalog.average[j].Nmeasure; k++) {
    56           catalog.measure[m+k].dbFlags = 0;
    57           catalog.measure[m+k].Mcal = 0;
     53          // XXX reset the photometry flags for secfilt entries?
    5854        }
    5955      }
  • trunk/Ohana/src/relphot/src/select_images.c

    r31160 r31450  
    6464  DmaxSkyRegion = -90.0;
    6565
     66  // FILE *ftest = fopen ("relphot.dump.dat", "w");
     67
    6668  /* compare with each region file */
    6769  for (i = 0; i < skylist[0].Nregions; i++) {
     
    117119    /* exclude images by photcode */
    118120    ecode = GetPhotcodeEquivCodebyCode (timage[i].photcode);
    119     if (ecode != photcode[0].code) continue;
     121    found = FALSE;
     122    int Ns;
     123    for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     124        if (ecode == photcodes[Ns][0].code) found = TRUE;
     125    }
     126    if (!found) continue;
    120127
    121128    /* exclude images by time */
     
    130137      continue;
    131138    }
     139
     140    // XXX temporary test : record center coords for each accepted and rejected chip/mosaic
     141    // double RAo, DECo;
    132142
    133143    /* define image corners - note the DIS images (mosaic phu) are special */
     
    138148      Xi[3] = -0.5*timage[i].NX; Yi[3] = +0.5*timage[i].NY;
    139149      Xi[4] = -0.5*timage[i].NX; Yi[4] = -0.5*timage[i].NY;
     150      // XY_to_RD(&RAo, &DECo, 0.0, 0.0, &timage[i].coords);
    140151    } else {
    141152      Xi[0] = 0;            Yi[0] = 0;
     
    144155      Xi[3] = 0;            Yi[3] = timage[i].NY;
    145156      Xi[4] = 0;            Yi[4] = 0;
     157      // XY_to_RD(&RAo, &DECo, 0.5*timage[i].NX, 0.5*timage[i].NY, &timage[i].coords);
    146158    }
    147159    found = FALSE;
    148160
    149161    /* transform corners to ra,dec -- costs ~3sec for 3M images (pikake) */
    150     double RminImage = 360.0;
    151     double RmaxImage =   0.0;
     162    double RminImage = RmidSkyRegion + 180.0;
     163    double RmaxImage = RmidSkyRegion - 180.0;
    152164    double DminImage = +90.0;
    153165    double DmaxImage = -90.0;
     
    218230
    219231  found_it:
     232    // XXX We claim this is a good image: write to a test file
     233    // fprintf (ftest, "%s : %lf %lf  : %f %f %d %x\n", timage[i].name, RAo, DECo, timage[i].Mcal, timage[i].dMcal, timage[i].nstar, timage[i].flags);
     234
    220235    image[nimage] = timage[i];
    221236    /* always allow 'few' images to succeed, if possible */
     
    239254  }
    240255  MARKTIME("finish image selection: %f sec\n", dtime);
     256
     257  // fclose (ftest);
    241258
    242259  if (VERBOSE) fprintf (stderr, "found "OFF_T_FMT" images\n", nimage);
  • trunk/Ohana/src/relphot/src/setExclusions.c

    r28241 r31450  
    11# include "relphot.h"
     2
     3// this function sets the NOCAL and AREA dbFlags bits for the MeasureTiny elements these
     4// are used elsewhere (StarOps.c, ImageOps.c, MosaicOps.c, GridOps.c, etc) to skip bad
     5// measurements.  The only exception is 'setMave' which is called by 'relphot_objects',
     6// and uses the bits read from disk as the test
    27
    38int setExclusions (Catalog *catalog, int Ncatalog) {
    49
    510  off_t i, j, k, m, Narea, Nnocal, Ngood;
    6   int ecode;
     11  int ecode, found, Ns;
    712  Coords *coords;
    813  double r, d, x, y;
     
    1116  for (i = 0; i < Ncatalog; i++) {
    1217    for (j = 0; j < catalog[i].Naverage; j++) {
    13       m = catalog[i].average[j].measureOffset;
    14       for (k = 0; k < catalog[i].average[j].Nmeasure; k++, m++) {
     18      m = catalog[i].averageT[j].measureOffset;
     19      for (k = 0; k < catalog[i].averageT[j].Nmeasure; k++, m++) {
    1520
    1621        /* select measurements by photcode */
    17         ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
    18         if (ecode != photcode[0].code) goto mark_nocal;
     22        ecode = GetPhotcodeEquivCodebyCode (catalog[i].measureT[m].photcode);
     23        found = FALSE;
     24        for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     25            if (ecode == photcodes[Ns][0].code) found = TRUE;
     26        }
     27        if (!found) goto mark_nocal;
    1928       
    2029        /* select measurements by time */
    2130        if (TimeSelect) {
    22           if (catalog[i].measure[m].t < TSTART) goto mark_nocal;
    23           if (catalog[i].measure[m].t > TSTOP) goto mark_nocal;
     31          if (catalog[i].measureT[m].t < TSTART) goto mark_nocal;
     32          if (catalog[i].measureT[m].t > TSTOP) goto mark_nocal;
    2433        }
    2534
    2635        /* select measurements by mag limit */
    2736        if (AreaSelect) {
    28           r = catalog[i].average[j].R + catalog[i].measure[m].dR / 3600.0;
    29           d = catalog[i].average[j].D + catalog[i].measure[m].dD / 3600.0;
     37          r = catalog[i].averageT[j].R + catalog[i].measureT[m].dR / 3600.0;
     38          d = catalog[i].averageT[j].D + catalog[i].measureT[m].dD / 3600.0;
    3039          if ((coords = getCoords (m, i)) == NULL) goto markbad;
    3140          RD_to_XY (&x, &y, r, d, coords);
     
    3948
    4049      markbad:
    41         catalog[i].measure[m].dbFlags |= ID_MEAS_AREA;
     50        catalog[i].measureT[m].dbFlags |= ID_MEAS_AREA;
    4251        Narea ++;
    4352        continue;
    4453       
    4554      mark_nocal:
    46         catalog[i].measure[m].dbFlags |= ID_MEAS_NOCAL;
     55        catalog[i].measureT[m].dbFlags |= ID_MEAS_NOCAL;
    4756        Nnocal ++;
    4857        continue;
  • trunk/Ohana/src/relphot/src/setMrelFinal.c

    r29001 r31450  
    11# include "relphot.h"
     2
     3// we've just reloaded the data from disk; we now need to apply the Image/Mosaic/Grid
     4// calibrations determined by the rest of the program.  We also need to set the final
     5// output dbFlags values
    26
    37void setMrelFinal (Catalog *catalog) {
     
    913  if (RESET) {
    1014
    11     for (i = 0; i < catalog[0].Naverage; i++) {
    12       catalog[0].secfilt[PhotNsec*i+PhotSec].M  = NAN;
    13       catalog[0].secfilt[PhotNsec*i+PhotSec].dM = NAN;
    14       catalog[0].secfilt[PhotNsec*i+PhotSec].Xm = NAN_S_SHORT;
     15    int Nsecfilt = GetPhotcodeNsecfilt ();
    1516
    16       m = catalog[0].average[i].measureOffset;
    17       for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
     17    int Ns;
     18    for (Ns = 0; Ns < Nphotcodes; Ns++) {
    1819       
    19         /* select measurements by photcode */
    20         ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
    21         if (ecode != photcode[0].code) continue;
     20      int thisCode = photcodes[Ns][0].code;
     21      int Nsec = GetPhotcodeNsec(thisCode);
     22
     23      for (i = 0; i < catalog[0].Naverage; i++) {
     24        catalog[0].secfilt[Nsecfilt*i+Nsec].M  = NAN;
     25        catalog[0].secfilt[Nsecfilt*i+Nsec].dM = NAN;
     26        catalog[0].secfilt[Nsecfilt*i+Nsec].Xm = NAN_S_SHORT;
     27
     28        m = catalog[0].average[i].measureOffset;
     29        for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
    2230       
    23         /* select measurements by time */
    24         if (TimeSelect) {
    25           if (catalog[0].measure[m].t < TSTART) continue;
    26           if (catalog[0].measure[m].t > TSTOP) continue;
     31          // skip measurements that do not match the current photcode
     32          ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
     33          if (ecode != thisCode) { continue; }
     34
     35          /* select measurements by time */
     36          if (TimeSelect) {
     37            if (catalog[0].measure[m].t < TSTART) continue;
     38            if (catalog[0].measure[m].t > TSTOP) continue;
     39          }
     40       
     41          catalog[0].measure[m].Mcal = 0;
     42          catalog[0].measure[m].dbFlags &= 0xff00;
     43          catalog[0].measure[m].dbFlags &= ~ID_MEAS_POOR_PHOTOM;
     44          catalog[0].measure[m].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
     45          catalog[0].measure[m].dbFlags &= ~ID_MEAS_AREA;
     46          catalog[0].measure[m].dbFlags &= ~ID_MEAS_NOCAL;
    2747        }
    28        
    29         catalog[0].measure[m].Mcal = 0;
    30         catalog[0].measure[m].dbFlags &= 0xff00;
    31         catalog[0].measure[m].dbFlags &= ~ID_MEAS_POOR_PHOTOM;
    32         catalog[0].measure[m].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
    33         catalog[0].measure[m].dbFlags &= ~ID_MEAS_AREA;
    34         catalog[0].measure[m].dbFlags &= ~ID_MEAS_NOCAL;
    3548      }
    3649    }
    3750  }
    3851
     52  // this sets flags in the measureT element, not the measure element
    3953  setExclusions (catalog, 1);  /* mark by area */
    4054
     
    5367  setMcalOutput (catalog, 1);
    5468
    55   /* clear ID_STAR_POOR, ID_STAR_FEW, ID_MEAS_NOCAL values before writing ??? */
     69  int Nsecfilt = GetPhotcodeNsecfilt ();
     70
     71  /* clear ID_STAR_POOR, ID_STAR_FEW values before writing ??? */
     72  /* ID_MEAS_NOCAL is an internal bit, so it should be cleared */
    5673  for (i = 0; i < catalog[0].Naverage; i++) {
    5774    catalog[0].average[i].flags &= ~ID_STAR_FEW;
    5875    catalog[0].average[i].flags &= ~ID_STAR_POOR;
     76    for (j = 0; j < Nsecfilt; j++) {
     77        catalog[0].secfilt[i*Nsecfilt+j].flags &= ~ID_STAR_FEW;
     78        catalog[0].secfilt[i*Nsecfilt+j].flags &= ~ID_STAR_POOR;
     79    }
    5980    m = catalog[0].average[i].measureOffset;
    6081    for (j = 0; j < catalog[0].average[i].Nmeasure; j++, m++) {
     
    6889
    6990  off_t i, k, m;
    70   int ecode;
     91  int ecode, found, Ns;
    7192  off_t Ntot, Ntry, Nkeep, Nskip;
    7293  float mag;
     
    90111
    91112      /* clear SKIP for all measures at first */
    92       catalog[0].measure[m].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
     113      catalog[0].measureT[m].dbFlags &= ~ID_MEAS_SKIP_PHOTOM;
    93114
    94115      /** never use these measurements (wrong photcode, bad time range) */
    95116      /* skipped via NOCAL, don't mark as skipped */
    96117      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
    97       if (ecode != photcode[0].code) continue;
     118      found = FALSE;
     119      for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) {
     120        if (ecode == photcodes[Ns][0].code) found = TRUE;
     121      }
     122      if (!found) continue;
    98123
    99124      /* skip measurements by time range */
     
    134159
    135160    skip:
    136       catalog[0].measure[m].dbFlags |= ID_MEAS_SKIP_PHOTOM;
     161      catalog[0].measure [m].dbFlags |= ID_MEAS_SKIP_PHOTOM;
     162      catalog[0].measureT[m].dbFlags |= ID_MEAS_SKIP_PHOTOM;
    137163      Nskip ++;
    138164    }
Note: See TracChangeset for help on using the changeset viewer.