Changeset 33651 for trunk/Ohana/src/relphot/src/bcatalog.c
- Timestamp:
- Apr 1, 2012, 2:48:41 PM (14 years ago)
- File:
-
- 1 edited
-
trunk/Ohana/src/relphot/src/bcatalog.c (modified) (15 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Ohana/src/relphot/src/bcatalog.c
r31668 r33651 1 1 # include "relphot.h" 2 2 3 extern double drand48(); 4 5 int 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 18 int 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 } 3 int LimitDensityCatalog_ByNmeasure (Catalog *subcatalog, Catalog *oldcatalog); 37 4 38 5 int bcatalog (Catalog *subcatalog, Catalog *catalog) { 39 6 40 7 off_t i, j, offset; 41 int ecode, found, Ns;8 int found, Ns, *Nvalid; 42 9 off_t NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm; 43 10 float mag; 44 int Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew, Ngalaxy, Npsfqf ;11 int Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew, Ngalaxy, Npsfqf, Nnan, Nbad, Npoor; 45 12 46 13 int Nsecfilt = GetPhotcodeNsecfilt (); … … 53 20 ALLOCATE (subcatalog[0].measureT, MeasureTiny, NMEASURE); 54 21 ALLOCATE (subcatalog[0].averageT, AverageTiny, NAVERAGE); 55 ALLOCATE (subcatalog[0].secfilt, SecFilt,NAVERAGE*Nsecfilt);22 ALLOCATE (subcatalog[0].secfilt, SecFilt, NAVERAGE*Nsecfilt); 56 23 Nmeasure = Naverage = 0; 57 24 58 Ncode = Ntime = Ndophot = Nmag = Nsigma = Nimag = Nfew = Npsfqf = Ngalaxy = 0; 25 Ncode = Ntime = Ndophot = Nmag = Nsigma = Nimag = Nfew = Npsfqf = Ngalaxy = Nnan = Nbad = Npoor = 0; 26 27 // copy the following fields to the subcatalog: 28 subcatalog[0].catID = catalog[0].catID; 29 subcatalog[0].filename = catalog[0].filename; 30 31 // array to count measures with photcodes->equiv to each of the secfilts 32 ALLOCATE (Nvalid, int, Nsecfilt); 33 34 // flags used by the photometry analysis (excluding UBERCAL) 35 unsigned int PHOTOM_FLAGS = 36 ID_MEAS_NOCAL | // detection ignored for this analysis (photcode, time range) 37 ID_MEAS_POOR_PHOTOM | // detection is photometry outlier 38 ID_MEAS_SKIP_PHOTOM | // detection was ignored for photometry measurement 39 ID_MEAS_AREA; // detetion was outside acceptable area of device 59 40 60 41 /* exclude stars not in range or with too few measurements */ … … 63 44 64 45 /* start with all stars good */ 65 CopyAverageT iny (&subcatalog[0].averageT[Naverage], &catalog[0].average[i]);46 CopyAverageToTiny (&subcatalog[0].averageT[Naverage], &catalog[0].average[i]); 66 47 subcatalog[0].averageT[Naverage].measureOffset = Nmeasure; 67 48 68 49 for (j = 0; j < Nsecfilt; j++) { 69 50 subcatalog[0].secfilt[Nsecfilt*Naverage+j] = catalog[0].secfilt[Nsecfilt*i+j]; 70 } 71 51 Nvalid[j] = 0; // reset the Nvalid array for this star 52 } 53 54 // reset the calculated average magnitudes (does not affect ubercal-tied measurements or images) 72 55 if (RESET) { 73 56 int Ns; 57 DVOSecfiltFlags secfiltBits = 58 ID_SECF_STAR_FEW | 59 ID_SECF_STAR_POOR | 60 ID_PHOTOM_PASS_0 | 61 ID_PHOTOM_PASS_1 | 62 ID_PHOTOM_PASS_2 | 63 ID_PHOTOM_PASS_3 | 64 ID_PHOTOM_PASS_4 | 65 ID_SECF_USE_SYNTH | 66 ID_SECF_USE_UBERCAL | 67 ID_SECF_OBJ_EXT; 74 68 for (Ns = 0; Ns < Nphotcodes; Ns++) { 75 69 … … 77 71 int Nsec = GetPhotcodeNsec(thisCode); 78 72 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; 73 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].M = NAN; 74 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].Map = NAN; 75 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].dM = NAN; 76 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].Xm = NAN; 77 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].M_20 = NAN_S_SHORT; 78 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].M_80 = NAN_S_SHORT; 79 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].Ncode = 0; 80 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].Nused = 0; 81 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].flags &= ~secfiltBits; 82 subcatalog[0].secfilt[Nsecfilt*Naverage+Nsec].ubercalDist = 1000; 83 83 } 84 84 } … … 92 92 93 93 /* select measurements by photcode */ 94 ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode); 94 PhotCode *code = GetPhotcodebyCode (catalog[0].measure[offset].photcode); 95 if (!code) continue; 95 96 found = FALSE; 97 int Nsec = -1; // Nsec equivalent for the measurement 96 98 for (Ns = 0; !found && (Ns < Nphotcodes); Ns++) { 97 if (ecode == photcodes[Ns][0].code) found = TRUE; 99 if (code->equiv != photcodes[Ns][0].code) continue; 100 found = TRUE; 101 Nsec = GetPhotcodeNsec(code->equiv); 98 102 } 99 103 if (!found) { … … 113 117 // skip garbage measurements 114 118 if (catalog[0].measure[offset].psfQual < 0.85) { Npsfqf ++; continue; } 115 116 // check for galaxies 117 if (!isnan(catalog[0].measure[offset].Map)) { 119 if (isnan(catalog[0].measure[offset].M)) { Nnan ++; continue; } 120 121 if (catalog[0].measure[offset].photFlags & code->photomBadMask) { 122 Nbad++; 123 continue; 124 } 125 if (catalog[0].measure[offset].photFlags & code->photomPoorMask) { Npoor++; continue;} 126 127 // check for galaxies (XXX skip for now) 128 if (FALSE && !isnan(catalog[0].measure[offset].Map)) { 118 129 if (catalog[0].measure[offset].M - catalog[0].measure[offset].Map > 0.15) { 119 130 nEXT ++; … … 137 148 } 138 149 139 CopyMeasureTiny (&subcatalog[0].measureT[Nmeasure], &catalog[0].measure[offset]); 150 // count this measurement as valid for this secfilt entry 151 if (Nsec > -1) { 152 assert (Nsec < Nsecfilt); 153 Nvalid[Nsec] ++; 154 } 155 156 CopyMeasureToTiny (&subcatalog[0].measureT[Nmeasure], &catalog[0].measure[offset]); 140 157 subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_SKIP_PHOTOM; 141 158 subcatalog[0].measureT[Nmeasure].averef = Naverage; 142 159 if (RESET) { 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;160 if (!KEEP_UBERCAL) { 161 subcatalog[0].measureT[Nmeasure].Mcal = 0; 162 subcatalog[0].measureT[Nmeasure].dbFlags &= ~ID_MEAS_PHOTOM_UBERCAL; 163 } 164 subcatalog[0].measureT[Nmeasure].dbFlags &= ~PHOTOM_FLAGS; 148 165 } 149 166 Nmeasure ++; … … 156 173 157 174 // skip object if it is likely to be a galaxy 158 if ( nEXT >= nPSF) {175 if (FALSE && (nEXT >= nPSF)) { 159 176 Nmeasure -= Nm; 160 177 Ngalaxy ++; … … 163 180 164 181 // XXXX test : what checks do I need to make elsewhere to avoid problems here? 165 if (Nm <= STAR_TOOFEW) { /* enough measurements in band? */182 if (Nm <= STAR_TOOFEW) { /* enough measurements total? */ 166 183 Nmeasure -= Nm; 167 184 Nfew ++; 168 185 continue; 169 186 } 187 int anySecfiltGood = FALSE; 188 for (Ns = 0; Ns < Nsecfilt; Ns++) { /* enough measurements in at least one band? */ 189 if (Nvalid[Ns] <= STAR_TOOFEW) continue; 190 anySecfiltGood = TRUE; 191 } 192 if (!anySecfiltGood) { 193 Nmeasure -= Nm; 194 Nfew ++; 195 continue; 196 } 197 170 198 subcatalog[0].averageT[Naverage].Nmeasure = Nm; 171 199 Naverage ++; … … 186 214 // limit the total number of stars in the catalog 187 215 if (MaxDensityUse) { 188 LimitDensityCatalog (subcatalog, catalog);216 LimitDensityCatalog_ByNmeasure (subcatalog, catalog); 189 217 } 190 218 … … 192 220 fprintf (stderr, "using "OFF_T_FMT" stars ("OFF_T_FMT" measures) of "OFF_T_FMT" for catalog %s\n", 193 221 subcatalog[0].Naverage, subcatalog[0].Nmeasure, i, catalog[0].filename); 194 fprintf (stderr, "rejections: %d code, %d time, %d dophot, %d mag, %d sigma, %d imag, %d few, %d psfqf, %d galaxies\n",195 Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew, Npsfqf, N galaxy);222 fprintf (stderr, "rejections: %d code, %d time, %d dophot, %d mag, %d sigma, %d imag, %d few, %d psfqf, %d Nnan, %d galaxies, %d bad, %d poor\n", 223 Ncode, Ntime, Ndophot, Nmag, Nsigma, Nimag, Nfew, Npsfqf, Nnan, Ngalaxy, Nbad, Npoor); 196 224 } 197 225 return (TRUE); 198 226 } 199 227 200 int LimitDensityCatalog (Catalog *subcatalog, Catalog *catalog) { 228 // sort by decreasing Nmeasure (X) 229 void sort_by_Nmeasure (int *X, off_t *Y, off_t N) { 230 231 # define SWAPFUNC(A,B){ int tmpI; off_t tmpT; \ 232 tmpI = X[A]; X[A] = X[B]; X[B] = tmpI; \ 233 tmpT = Y[A]; Y[A] = Y[B]; Y[B] = tmpT; \ 234 } 235 # define COMPARE(A,B)(X[A] > X[B]) 236 237 OHANA_SORT (N, COMPARE, SWAPFUNC); 238 239 # undef SWAPFUNC 240 # undef COMPARE 241 242 } 243 244 int LimitDensityCatalog_ByNmeasure (Catalog *subcatalog, Catalog *oldcatalog) { 201 245 202 246 Catalog tmpcatalog; … … 206 250 int Nsecfilt = GetPhotcodeNsecfilt (); 207 251 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);252 gfits_scan (&oldcatalog[0].header, "RA0", "%lf", 1, &Rmin); 253 gfits_scan (&oldcatalog[0].header, "DEC0", "%lf", 1, &Dmin); 254 gfits_scan (&oldcatalog[0].header, "RA1", "%lf", 1, &Rmax); 255 gfits_scan (&oldcatalog[0].header, "DEC1", "%lf", 1, &Dmax); 212 256 213 257 if (VERBOSE2) fprintf (stderr, "extracting from catalog covering region %f,%f to %f,%f\n", Rmin, Dmin, Rmax, Dmax); … … 229 273 230 274 // we are going to select Nmax entries by generating a random-sorted index list 231 off_t *index, tmp, i, j, ave; 275 int *value; 276 off_t *index, i, j, ave; 232 277 ALLOCATE (index, off_t, Naverage); 278 ALLOCATE (value, int, Naverage); 233 279 for (i = 0; i < Naverage; i++) { 234 280 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 } 281 value[i] = subcatalog[0].averageT[i].Nmeasure; 282 } 283 sort_by_Nmeasure (value, index, Naverage); 242 284 243 285 // count the number of measurements this selection will yield … … 266 308 Nmeasure ++; 267 309 } 310 for (j = 0; j < Nsecfilt; j++) { 311 tmpcatalog.secfilt[i*Nsecfilt + j] = subcatalog[0].secfilt[ave*Nsecfilt + j]; 312 } 313 } 314 315 if (VERBOSE) { 316 char *basename = filebasename (oldcatalog[0].filename); 317 fprintf (stderr, "limited to "OFF_T_FMT" ("OFF_T_FMT" subset, "OFF_T_FMT" total) stars, "OFF_T_FMT" ("OFF_T_FMT" subset, "OFF_T_FMT" total) measures for catalog %s\n", 318 Nmax, subcatalog[0].Naverage, oldcatalog[0].Naverage, Nmeasure, subcatalog[0].Nmeasure, oldcatalog[0].Nmeasure, basename); 319 free (basename); 320 } 321 322 free (index); 323 free (value); 324 free (subcatalog[0].averageT); 325 free (subcatalog[0].measureT); 326 free (subcatalog[0].secfilt); 327 328 subcatalog[0].averageT = tmpcatalog.averageT; 329 subcatalog[0].measureT = tmpcatalog.measureT; 330 subcatalog[0].secfilt = tmpcatalog.secfilt; 331 subcatalog[0].Naverage = Nmax; 332 subcatalog[0].Nmeasure = Nmeasure; 333 subcatalog[0].Nsecfilt = oldcatalog[0].Nsecfilt; 334 subcatalog[0].Nsecf_mem = Naverage * oldcatalog[0].Nsecfilt; 335 336 return (TRUE); 337 } 338 339 int LimitDensityCatalog (Catalog *subcatalog, Catalog *catalog) { 340 341 Catalog tmpcatalog; 342 343 double Rmin, Rmax, Dmin, Dmax; 344 345 int Nsecfilt = GetPhotcodeNsecfilt (); 346 347 gfits_scan (&catalog[0].header, "RA0", "%lf", 1, &Rmin); 348 gfits_scan (&catalog[0].header, "DEC0", "%lf", 1, &Dmin); 349 gfits_scan (&catalog[0].header, "RA1", "%lf", 1, &Rmax); 350 gfits_scan (&catalog[0].header, "DEC1", "%lf", 1, &Dmax); 351 352 if (VERBOSE2) fprintf (stderr, "extracting from catalog covering region %f,%f to %f,%f\n", Rmin, Dmin, Rmax, Dmax); 353 354 float AREA = fabs(Dmax - Dmin) * fabs(Rmax - Rmin) * cos (0.5*RAD_DEG*(Dmax + Dmin)); 355 assert (AREA > 0); 356 357 off_t Nmax = MaxDensityValue * AREA; 358 if (subcatalog[0].Naverage <= Nmax) { 359 if (VERBOSE) { 360 fprintf (stderr, "subcatalog has less than the max density\n"); 361 } 362 return (TRUE); 363 } 364 365 off_t Naverage = subcatalog[0].Naverage; 366 367 // select a random subset of Nmax stars from subcatalog using Fisher-Yates 368 369 // we are going to select Nmax entries by generating a random-sorted index list 370 off_t *index, tmp, i, j, ave; 371 ALLOCATE (index, off_t, Naverage); 372 for (i = 0; i < Naverage; i++) { 373 index[i] = i; 374 } 375 for (i = 0; i < Naverage; i++) { 376 j = (Naverage - i) * drand48() + i; // a number between i and Naverage 377 tmp = index[j]; 378 index[j] = index[i]; 379 index[i] = tmp; 380 } 381 382 // count the number of measurements this selection will yield 383 off_t NMEASURE = 0; 384 for (i = 0; i < Nmax; i++) { 385 ave = index[i]; 386 NMEASURE += subcatalog[0].averageT[ave].Nmeasure; 387 } 388 389 // allocate the output data 390 ALLOCATE (tmpcatalog.averageT, AverageTiny, Nmax); 391 ALLOCATE (tmpcatalog.measureT, MeasureTiny, NMEASURE); 392 ALLOCATE (tmpcatalog.secfilt, SecFilt, Nmax * Nsecfilt); 393 394 off_t Nmeasure = 0; 395 396 // copy the Nmax selected entries from subcatalog to tmpcatalog (adjusting links) 397 for (i = 0; i < Nmax; i++) { 398 ave = index[i]; 399 tmpcatalog.averageT[i] = subcatalog[0].averageT[ave]; 400 tmpcatalog.averageT[i].measureOffset = Nmeasure; 401 for (j = 0; j < tmpcatalog.averageT[i].Nmeasure; j++) { 402 off_t offset = subcatalog[0].averageT[ave].measureOffset + j; 403 tmpcatalog.measureT[Nmeasure] = subcatalog[0].measureT[offset]; 404 tmpcatalog.measureT[Nmeasure].averef = i; 405 Nmeasure ++; 406 } 407 for (j = 0; j < Nsecfilt; j++) { 408 tmpcatalog.secfilt[i*Nsecfilt + j] = subcatalog[0].secfilt[ave*Nsecfilt + j]; 409 } 268 410 } 269 411 … … 287 429 return (TRUE); 288 430 } 289 290 // for the cases where we are not using a subset of the data, we still need to have a copy of these fields291 int populate_tiny_values (Catalog *catalog) {292 293 off_t i;294 295 AverageTiny *averageT;296 Average *average;297 298 MeasureTiny *measureT;299 Measure *measure;300 301 ALLOCATE (catalog[0].measureT, MeasureTiny, catalog[0].Nmeasure);302 ALLOCATE (catalog[0].averageT, AverageTiny, catalog[0].Naverage);303 304 average = catalog[0].average;305 averageT = catalog[0].averageT;306 307 measure = catalog[0].measure;308 measureT = catalog[0].measureT;309 310 for (i = 0; i < catalog[0].Naverage; i++) {311 // CopyAverageTiny (&catalog[0].averageT[i], &catalog[0].average[i]);312 averageT[i].R = average[i].R;313 averageT[i].D = average[i].D;314 averageT[i].flags = average[i].flags;315 averageT[i].Nmeasure = average[i].Nmeasure;316 averageT[i].measureOffset = average[i].measureOffset;317 }318 319 for (i = 0; i < catalog[0].Nmeasure; i++) {320 // CopyMeasureTiny (&catalog[0].measureT[i], &catalog[0].measure[i]);321 measureT[i].dR = measure[i].dR;322 measureT[i].dD = measure[i].dD;323 measureT[i].M = measure[i].M;324 measureT[i].Mcal = measure[i].Mcal;325 measureT[i].dM = measure[i].dM;326 measureT[i].airmass = measure[i].airmass;327 measureT[i].Xccd = measure[i].Xccd;328 measureT[i].Yccd = measure[i].Yccd;329 measureT[i].t = measure[i].t;330 measureT[i].dt = measure[i].dt;331 measureT[i].averef = measure[i].averef;332 measureT[i].imageID = measure[i].imageID;333 measureT[i].dbFlags = measure[i].dbFlags;334 measureT[i].photcode = measure[i].photcode;335 }336 337 return (TRUE);338 }339 340 int free_tiny_values (Catalog *catalog) {341 342 if (catalog[0].averageT) free (catalog[0].averageT);343 if (catalog[0].measureT) free (catalog[0].measureT);344 return (TRUE);345 }346
Note:
See TracChangeset
for help on using the changeset viewer.
