IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 18, 2006, 8:44:12 AM (20 years ago)
Author:
eugene
Message:

select refstars by magnitude, fix various memory leaks, better error tests, fix luminosity function test

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psastro/src/psastroLuminosityFunction.c

    r9587 r9627  
    1515  psMemSetDeallocator(func, (psFreeFunc) pmLumFuncFree);
    1616
    17   func->mMin = 0;
    18   func->mMax = 0;
    19   func->slope = 0;
    20   func->offset = 0;
     17  func->mMin = mMin;
     18  func->mMax = mMax;
     19  func->slope = slope;
     20  func->offset = offset;
    2121
    2222  return func;
    2323}
    2424
    25 pmLumFunc *psastroRefstarSubset (psArray *stars) {
     25pmLumFunc *psastroLuminosityFunction (psArray *stars) {
    2626
    2727  // determine the max and min magnitude for the array of stars
    2828  pmAstromObj *star = stars->data[0];
    29   mMin = star->Mag;
    30   mMax = star->Mag;
     29  double mMin = star->Mag;
     30  double mMax = star->Mag;
    3131  for (int i = 0; i < stars->n; i++) {
    3232    star = stars->data[i];
     
    3535  }
    3636
    37   int nBin = 1 + (nMax - nMin) / dMag;
     37  int nBin = 1 + (mMax - mMin) / dMag;
    3838  if (nBin <= 1) {
    3939    psError(PSASTRO_ERR_DATA, true, "magnitude range of 0.0\n");
     
    4545  // bin[i] = mMin + i*dMag : mMin + (i+1)*dMag
    4646  psVector *nMags = psVectorAlloc (nBin, PS_TYPE_F32);
     47  nMags->n = nBin;
     48  psVectorInit (nMags, 0);
     49
    4750  for (int i = 0; i < stars->n; i++) {
    4851    star = stars->data[i];
    4952    int bin = (star->Mag - mMin) / dMag;
    5053    nMags->data.F32[bin] += 1.0;
     54  }
     55
     56  // find the peak and position
     57  int iPeak = 0;
     58  int nPeak = 0;
     59  for (int i = 0; i < nMags->n; i++) {
     60    if (nMags->data.F32[i] < nPeak) continue;
     61    iPeak = i;
     62    nPeak = nMags->data.F32[i];
    5163  }
    5264
     
    5668  // create 2 vectors represnting the dlogN/dlogS line
    5769  // exclude bins with no stars
     70  // exclude points after the peak with N < 0.8*peak
    5871  int n = 0;
    59   for (int i = 0; i < stars->n; i++) {
     72  for (int i = 0; i < nMags->n; i++) {
    6073    if (nMags->data.F32[i] < 1) continue;
     74    if ((i > iPeak) && (nMags->data.F32[i] < 0.8*nPeak)) continue;
    6175    lnMag->data.F32[n] = log10 (nMags->data.F32[i]);
    6276    Mag->data.F32[n] = mMin + (i + 0.5)*dMag;
    6377    n++;
    6478  }
     79  lnMag->n = n;
     80  Mag->n = n;
     81  psLogMsg ("psastro", 4, "fitting %d points to luminosity function\n", n);
    6582
    6683  psVector *mask = psVectorAlloc (nBin, PS_TYPE_MASK);
     84  mask->n = Mag->n;
     85  psVectorInit (mask, 0);
     86
    6787  psPolynomial1D *line = psPolynomial1DAlloc (PS_POLYNOMIAL_ORD, 1);
    6888  psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV);
     
    7090  stats->clipIter = 3;
    7191
    72   line = psVectorClipFitPolynomial1D(line, stats, mask, 1, lnMag, NULL, Mag);
     92  line = psVectorClipFitPolynomial1D(line, stats, mask, 0xff, lnMag, NULL, Mag);
    7393
    7494  // find min and max unmasked magnitudes
    75   mMinValid = NAN;
    76   mMaxValid = NAN;
     95  double mMinValid = NAN;
     96  double mMaxValid = NAN;
    7797  for (int i = 0; i < Mag->n; i++) {
    7898    if (mask->data.U8[i]) continue;
     
    89109
    90110  pmLumFunc *lumFunc = pmLumFuncAlloc (mMinValid, mMaxValid, line->coeff[0], line->coeff[1]);
     111
     112  psFree (lnMag);
     113  psFree (nMags);
     114  psFree (Mag);
     115  psFree (mask);
     116  psFree (line);
     117  psFree (stats);
     118
    91119  return (lumFunc);
    92120}
Note: See TracChangeset for help on using the changeset viewer.