Changeset 15697 for trunk/psModules/src/objects/pmPSFtry.c
- Timestamp:
- Nov 26, 2007, 5:14:57 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/objects/pmPSFtry.c (modified) (17 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/objects/pmPSFtry.c
r15562 r15697 5 5 * @author EAM, IfA 6 6 * 7 * @version $Revision: 1.5 0$ $Name: not supported by cvs2svn $8 * @date $Date: 2007-11- 10 01:09:20$7 * @version $Revision: 1.51 $ $Name: not supported by cvs2svn $ 8 * @date $Date: 2007-11-27 03:14:57 $ 9 9 * 10 10 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 56 56 57 57 // allocate a pmPSFtry based on the desired sources and the model (identified by name) 58 pmPSFtry *pmPSFtryAlloc ( psArray *sources,pmPSFOptions *options)58 pmPSFtry *pmPSFtryAlloc (const psArray *sources, const pmPSFOptions *options) 59 59 { 60 60 pmPSFtry *test = (pmPSFtry *) psAlloc(sizeof(pmPSFtry)); … … 98 98 99 99 // generate a pmPSFtry with a copy of the test PSF sources 100 pmPSFtry *pmPSFtryModel ( psArray *sources,char *modelName, pmPSFOptions *options, psMaskType maskVal, psMaskType mark)100 pmPSFtry *pmPSFtryModel (const psArray *sources, const char *modelName, pmPSFOptions *options, psMaskType maskVal, psMaskType mark) 101 101 { 102 102 bool status; … … 377 377 378 378 if (!pmPSFFitShapeParams (psf, psfTry->sources, x, y, psfTry->mask)) { 379 psFree(x);380 psFree(y);381 psFree(z);382 return false;379 psFree(x); 380 psFree(y); 381 psFree(z); 382 return false; 383 383 } 384 384 … … 406 406 } 407 407 408 psImageBinning *binning = psImageBinningAlloc();409 binning->nXruff = psf->trendNx;410 binning->nYruff = psf->trendNy;411 binning->nXfine = psf->fieldNx;412 binning->nYfine = psf->fieldNy;413 414 if (psf->psfTrendMode == PM_TREND_MAP) {415 psImageBinningSetScale (binning, PS_IMAGE_BINNING_CENTER);416 psImageBinningSetSkipByOffset (binning, psf->fieldXo, psf->fieldYo);417 }418 419 // free existing trend, re-alloc420 psFree (psf->params->data[i]);421 psf->params->data[i] = pmTrend2DNoImageAlloc (psf->psfTrendMode, binning, psf->psfTrendStats);422 psFree (binning);408 psImageBinning *binning = psImageBinningAlloc(); 409 binning->nXruff = psf->trendNx; 410 binning->nYruff = psf->trendNy; 411 binning->nXfine = psf->fieldNx; 412 binning->nYfine = psf->fieldNy; 413 414 if (psf->psfTrendMode == PM_TREND_MAP) { 415 psImageBinningSetScale (binning, PS_IMAGE_BINNING_CENTER); 416 psImageBinningSetSkipByOffset (binning, psf->fieldXo, psf->fieldYo); 417 } 418 419 // free existing trend, re-alloc 420 psFree (psf->params->data[i]); 421 psf->params->data[i] = pmTrend2DNoImageAlloc (psf->psfTrendMode, binning, psf->psfTrendStats); 422 psFree (binning); 423 423 424 424 // fit the collection of measured parameters to the PSF 2D model … … 487 487 pmSource *source = sources->data[i]; 488 488 if (source->modelEXT == NULL) continue; 489 // XXX I am relying on the fact that none of the masked sources490 // have modelEXT set here. perhaps use the value of psfTry->mask instead?489 // XXX I am relying on the fact that none of the masked sources 490 // have modelEXT set here. perhaps use the value of psfTry->mask instead? 491 491 492 492 psEllipsePol pol = pmPSF_ModelToFit (source->modelEXT->params->data.F32); … … 496 496 e2->data.F32[i] = pol.e2; 497 497 498 float flux = source->modelEXT->params->data.F32[PM_PAR_I0];499 mag->data.F32[i] = (flux > 0.0) ? -2.5*log(flux) : -100.0;498 float flux = source->modelEXT->params->data.F32[PM_PAR_I0]; 499 mag->data.F32[i] = (flux > 0.0) ? -2.5*log(flux) : -100.0; 500 500 } 501 501 502 502 if (psf->psfTrendMode == PM_TREND_MAP) { 503 float errorFloor = 0.0;504 float errorTotal = 0.0;505 float errorTotalMin = FLT_MAX;506 int entryMin = -1;507 508 psVector *dz = NULL;509 psVector *mask = psVectorAlloc (sources->n, PS_TYPE_U8);510 511 // check the fit residuals and increase Nx,Ny until the error is minimized512 // pmPSFParamTrend increases the number along the longer of x or y513 for (int i = 1; i < PS_MAX (psf->trendNx, psf->trendNy); i++) {514 515 psVectorInit (mask, 0);516 if (!pmPSFFitShapeParamsMap (psf, i, &errorFloor, &errorTotal, mask, x, y, mag, e0, e1, e2, dz)) {517 break;518 }519 520 // we do not have a good model for the error distributions on e0, e1, e2. 521 // use the bright end scatter from the constant fit to set the scale for the 522 // versions with 2D variation. potentially scale by poisson errors...523 if (i == 1) {524 // if (psf->poissonErrorsParams) do something else..525 dz = psVectorAlloc (sources->n, PS_TYPE_F32);526 for (int i = 0; i < sources->n; i++) {527 dz->data.F32[i] = errorFloor;528 }529 }530 531 // store the resulting errorTotal values and the scales, redo the best532 if (errorTotal < errorTotalMin) {533 errorTotalMin = errorTotal;534 entryMin = i;535 }536 }537 if (entryMin == -1) {538 psError (PS_ERR_UNKNOWN, true, "failed to find image map for shape params");539 return false;540 }541 542 // XXX supply the resulting mask values back to srcMask543 psVectorInit (mask, 0);544 if (!pmPSFFitShapeParamsMap (psf, entryMin, &errorFloor, &errorTotal, mask, x, y, mag, e0, e1, e2, dz)) {545 psAbort ("failed pmPSFFitShapeParamsMap on second pass?");546 }547 548 pmTrend2D *trend = psf->params->data[PM_PAR_E0];549 psf->trendNx = trend->map->map->numCols;550 psf->trendNy = trend->map->map->numRows;551 552 psFree (mask);553 psFree (dz);503 float errorFloor = 0.0; 504 float errorTotal = 0.0; 505 float errorTotalMin = FLT_MAX; 506 int entryMin = -1; 507 508 psVector *dz = NULL; 509 psVector *mask = psVectorAlloc (sources->n, PS_TYPE_U8); 510 511 // check the fit residuals and increase Nx,Ny until the error is minimized 512 // pmPSFParamTrend increases the number along the longer of x or y 513 for (int i = 1; i < PS_MAX (psf->trendNx, psf->trendNy); i++) { 514 515 psVectorInit (mask, 0); 516 if (!pmPSFFitShapeParamsMap (psf, i, &errorFloor, &errorTotal, mask, x, y, mag, e0, e1, e2, dz)) { 517 break; 518 } 519 520 // we do not have a good model for the error distributions on e0, e1, e2. 521 // use the bright end scatter from the constant fit to set the scale for the 522 // versions with 2D variation. potentially scale by poisson errors... 523 if (i == 1) { 524 // if (psf->poissonErrorsParams) do something else.. 525 dz = psVectorAlloc (sources->n, PS_TYPE_F32); 526 for (int i = 0; i < sources->n; i++) { 527 dz->data.F32[i] = errorFloor; 528 } 529 } 530 531 // store the resulting errorTotal values and the scales, redo the best 532 if (errorTotal < errorTotalMin) { 533 errorTotalMin = errorTotal; 534 entryMin = i; 535 } 536 } 537 if (entryMin == -1) { 538 psError (PS_ERR_UNKNOWN, true, "failed to find image map for shape params"); 539 return false; 540 } 541 542 // XXX supply the resulting mask values back to srcMask 543 psVectorInit (mask, 0); 544 if (!pmPSFFitShapeParamsMap (psf, entryMin, &errorFloor, &errorTotal, mask, x, y, mag, e0, e1, e2, dz)) { 545 psAbort ("failed pmPSFFitShapeParamsMap on second pass?"); 546 } 547 548 pmTrend2D *trend = psf->params->data[PM_PAR_E0]; 549 psf->trendNx = trend->map->map->numCols; 550 psf->trendNy = trend->map->map->numRows; 551 552 psFree (mask); 553 psFree (dz); 554 554 } else { 555 555 556 // XXX iterate Nx, Ny based on scatter?557 // XXX we force the x & y order to be the same558 // modify the order to correspond to the actual number of matched stars:559 int order = PS_MAX (psf->trendNx, psf->trendNy);560 if ((sources->n < 15) && (order >= 3)) order = 2;561 if ((sources->n < 11) && (order >= 2)) order = 1;562 if ((sources->n < 8) && (order >= 1)) order = 0;563 if ((sources->n < 3)) {564 psError (PS_ERR_UNKNOWN, true, "failed to fit polynomial to shape params");565 return false;566 }567 psf->trendNx = order;568 psf->trendNy = order;569 570 // we run 'clipIter' cycles clipping in each of x and y, with only one iteration each.571 // This way, the parameters masked by one of the fits will be applied to the others572 for (int i = 0; i < psf->psfTrendStats->clipIter; i++) {573 574 // XXX we are using the same stats structure on each pass: do we need to re-init it?575 bool status = true;576 status &= pmTrend2DFit (psf->params->data[PM_PAR_E0], srcMask, 0xff, x, y, e0, NULL);577 psTrace ("psModules.pmPSFtry", 4, "clipped E0 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e0->n);578 status &= pmTrend2DFit (psf->params->data[PM_PAR_E1], srcMask, 0xff, x, y, e1, NULL);579 psTrace ("psModules.pmPSFtry", 4, "clipped E1 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e1->n);580 status &= pmTrend2DFit (psf->params->data[PM_PAR_E2], srcMask, 0xff, x, y, e2, NULL);581 psTrace ("psModules.pmPSFtry", 4, "clipped E2 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e2->n);582 583 if (!status) {584 psError (PS_ERR_UNKNOWN, true, "failed to fit polynomial to shape params");585 return false;586 }587 }556 // XXX iterate Nx, Ny based on scatter? 557 // XXX we force the x & y order to be the same 558 // modify the order to correspond to the actual number of matched stars: 559 int order = PS_MAX (psf->trendNx, psf->trendNy); 560 if ((sources->n < 15) && (order >= 3)) order = 2; 561 if ((sources->n < 11) && (order >= 2)) order = 1; 562 if ((sources->n < 8) && (order >= 1)) order = 0; 563 if ((sources->n < 3)) { 564 psError (PS_ERR_UNKNOWN, true, "failed to fit polynomial to shape params"); 565 return false; 566 } 567 psf->trendNx = order; 568 psf->trendNy = order; 569 570 // we run 'clipIter' cycles clipping in each of x and y, with only one iteration each. 571 // This way, the parameters masked by one of the fits will be applied to the others 572 for (int i = 0; i < psf->psfTrendStats->clipIter; i++) { 573 574 // XXX we are using the same stats structure on each pass: do we need to re-init it? 575 bool status = true; 576 status &= pmTrend2DFit (psf->params->data[PM_PAR_E0], srcMask, 0xff, x, y, e0, NULL); 577 psTrace ("psModules.pmPSFtry", 4, "clipped E0 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e0->n); 578 status &= pmTrend2DFit (psf->params->data[PM_PAR_E1], srcMask, 0xff, x, y, e1, NULL); 579 psTrace ("psModules.pmPSFtry", 4, "clipped E1 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e1->n); 580 status &= pmTrend2DFit (psf->params->data[PM_PAR_E2], srcMask, 0xff, x, y, e2, NULL); 581 psTrace ("psModules.pmPSFtry", 4, "clipped E2 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e2->n); 582 583 if (!status) { 584 psError (PS_ERR_UNKNOWN, true, "failed to fit polynomial to shape params"); 585 return false; 586 } 587 } 588 588 } 589 589 … … 591 591 if (psTraceGetLevel("psModules.objects") >= 4) { 592 592 FILE *f = fopen ("pol.dat", "w"); 593 fprintf (f, "# x y : e0obs e1obs e2obs : e0fit e1fit e2fit : mask\n");593 fprintf (f, "# x y : e0obs e1obs e2obs : e0fit e1fit e2fit : mask\n"); 594 594 for (int i = 0; i < e0->n; i++) { 595 595 fprintf (f, "%f %f : %f %f %f : %f %f %f : %d\n", 596 596 x->data.F32[i], y->data.F32[i], 597 e0->data.F32[i], e1->data.F32[i], e2->data.F32[i], 597 e0->data.F32[i], e1->data.F32[i], e2->data.F32[i], 598 598 pmTrend2DEval (psf->params->data[PM_PAR_E0], x->data.F32[i], y->data.F32[i]), 599 599 pmTrend2DEval (psf->params->data[PM_PAR_E1], x->data.F32[i], y->data.F32[i]), 600 600 pmTrend2DEval (psf->params->data[PM_PAR_E2], x->data.F32[i], y->data.F32[i]), 601 srcMask->data.U8[i]);601 srcMask->data.U8[i]); 602 602 } 603 603 fclose (f); … … 615 615 616 616 int Nx, Ny; 617 617 618 618 // set the map scale to match the aspect ratio 619 619 if (psf->fieldNx > psf->fieldNy) { 620 Nx = scale;621 Ny = PS_MAX (1, Nx * (psf->fieldNy / psf->fieldNx));620 Nx = scale; 621 Ny = PS_MAX (1, Nx * (psf->fieldNy / psf->fieldNx)); 622 622 } else { 623 Ny = scale;624 Nx = PS_MAX (1, Ny * (psf->fieldNx / psf->fieldNy));623 Ny = scale; 624 Nx = PS_MAX (1, Ny * (psf->fieldNx / psf->fieldNy)); 625 625 } 626 626 627 627 // do we have enough sources for this fine of a grid? 628 628 if (x->n < 3*Nx*Ny) { 629 return false;629 return false; 630 630 } 631 631 … … 658 658 // This way, the parameters masked by one of the fits will be applied to the others 659 659 for (int i = 0; i < nIter; i++) { 660 // XXX we are using the same stats structure on each pass: do we need to re-init it?660 // XXX we are using the same stats structure on each pass: do we need to re-init it? 661 661 pmTrend2DFit (psf->params->data[PM_PAR_E0], mask, 0xff, x, y, e0obs, dz); 662 662 psTrace ("psModules.objects", 5, "clipped E0 : keeping %ld of %ld\n", psf->psfTrendStats->clippedNvalues, e0obs->n); … … 683 683 float mapErrorSum = 0.0; 684 684 float mapError = 0.0; 685 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 686 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 687 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]);688 }685 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 686 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 687 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]); 688 } 689 689 } 690 690 psTrace ("psModules.objects", 4, "E0 error: %f\n", sqrt(mapError)); … … 693 693 trend = psf->params->data[PM_PAR_E1]; 694 694 mapError = 0.0; 695 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 696 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 697 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]);698 }695 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 696 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 697 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]); 698 } 699 699 } 700 700 psTrace ("psModules.objects", 4, "E1 error: %f\n", sqrt(mapError)); … … 703 703 trend = psf->params->data[PM_PAR_E2]; 704 704 mapError = 0.0; 705 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 706 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 707 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]);708 }705 for (int iy = 0; iy < trend->map->error->numRows; iy++) { 706 for (int ix = 0; ix < trend->map->error->numCols; ix++) { 707 mapError += PS_SQR (trend->map->error->data.F32[iy][ix]); 708 } 709 709 } 710 710 psTrace ("psModules.objects", 4, "E2 error: %f\n", sqrt(mapError)); … … 742 742 int nBin = PS_MAX (mag->n / nGroup, 1); 743 743 744 // output vectors for ApResid trend 744 // output vectors for ApResid trend 745 745 psVector *dSo = psVectorAlloc (nBin, PS_TYPE_F32); 746 746 … … 756 756 int n = 0; 757 757 for (int i = 0; i < dSo->n; i++) { 758 int j;759 for (j = 0; (j < nGroup) && (n < mag->n); j++, n++) {760 int N = index->data.U32[n];761 dE0subset->data.F32[j] = e0res->data.F32[N];762 dE1subset->data.F32[j] = e1res->data.F32[N];763 dE2subset->data.F32[j] = e2res->data.F32[N];764 765 mkSubset->data.U8[j] = mask->data.U8[N];766 }767 dE0subset->n = j;768 dE1subset->n = j;769 dE2subset->n = j;770 mkSubset->n = j;771 772 // calculate the root-mean-square of E0, E1, E2773 float dEsquare = 0.0;774 psStatsInit (statsS);775 psVectorStats (statsS, dE0subset, NULL, mkSubset, 0xff);776 dEsquare += PS_SQR(statsS->robustStdev);777 778 psStatsInit (statsS);779 psVectorStats (statsS, dE1subset, NULL, mkSubset, 0xff);780 dEsquare += PS_SQR(statsS->robustStdev);781 782 psStatsInit (statsS);783 psVectorStats (statsS, dE2subset, NULL, mkSubset, 0xff);784 dEsquare += PS_SQR(statsS->robustStdev);785 786 dSo->data.F32[i] = sqrt(dEsquare);758 int j; 759 for (j = 0; (j < nGroup) && (n < mag->n); j++, n++) { 760 int N = index->data.U32[n]; 761 dE0subset->data.F32[j] = e0res->data.F32[N]; 762 dE1subset->data.F32[j] = e1res->data.F32[N]; 763 dE2subset->data.F32[j] = e2res->data.F32[N]; 764 765 mkSubset->data.U8[j] = mask->data.U8[N]; 766 } 767 dE0subset->n = j; 768 dE1subset->n = j; 769 dE2subset->n = j; 770 mkSubset->n = j; 771 772 // calculate the root-mean-square of E0, E1, E2 773 float dEsquare = 0.0; 774 psStatsInit (statsS); 775 psVectorStats (statsS, dE0subset, NULL, mkSubset, 0xff); 776 dEsquare += PS_SQR(statsS->robustStdev); 777 778 psStatsInit (statsS); 779 psVectorStats (statsS, dE1subset, NULL, mkSubset, 0xff); 780 dEsquare += PS_SQR(statsS->robustStdev); 781 782 psStatsInit (statsS); 783 psVectorStats (statsS, dE2subset, NULL, mkSubset, 0xff); 784 dEsquare += PS_SQR(statsS->robustStdev); 785 786 dSo->data.F32[i] = sqrt(dEsquare); 787 787 } 788 788 psFree (dE0subset); … … 790 790 psFree (dE2subset); 791 791 psFree (mkSubset); 792 792 793 793 psStats *stats = psStatsAlloc (PS_STAT_MIN); 794 794 psVectorStats (stats, dSo, NULL, NULL, 0); … … 800 800 801 801 psFree (dSo); 802 802 803 803 psFree (statsS); 804 804
Note:
See TracChangeset
for help on using the changeset viewer.
