IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 40408


Ignore:
Timestamp:
Apr 26, 2018, 12:37:03 PM (8 years ago)
Author:
eugene
Message:

clean up memory leaks for avperiodogram and related; finish avperiodomatch

Location:
trunk/Ohana/src/opihi
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Ohana/src/opihi/dvo/Makefile

    r40398 r40408  
    2727$(SRC)/mySequence.$(ARCH).o             \
    2828$(SRC)/photcode_ops.$(ARCH).o           \
     29$(SRC)/PeriodogramOps.$(ARCH).o         \
    2930$(SRC)/find_matches.$(ARCH).o           
    3031
     
    4647$(SRC)/avmatch.$(ARCH).o                \
    4748$(SRC)/avperiodogram.$(ARCH).o          \
     49$(SRC)/avperiodomatch.$(ARCH).o         \
    4850$(SRC)/badimages.$(ARCH).o              \
    4951$(SRC)/catdir.$(ARCH).o                 \
  • trunk/Ohana/src/opihi/dvo/avperiodogram.c

    r40402 r40408  
    11# include "dvoshell.h"
    2 # define MIN_VAR 1e-8
    3 # define NPERIODS 50
    4 
    5 /* basic outline of the code:
    6 
    7  * parse optional arguments
    8  * -parallel : launch parallel jobs [below is called for non-parallel or for dvo_client
    9 
    10  * parse optional -where-measure clause
    11  *
    12 
    13  */
    14 
    15 typedef struct {
    16   int Nperiods;
    17   opihi_flt *period;
    18   opihi_flt *power;
    19 } PeriodogramResult;
    20 
    21 void PeriodogramResultFree (PeriodogramResult *result);
    22 void PeriodogramResultSave (PeriodogramResult *result, char *extname, FILE *foutput, Average *average);
    23 PeriodogramResult *periodogram_fm_raw (opihi_flt *time, opihi_flt *flux, opihi_flt *dflux, int Npts);
    24 
    25 float minP = 0.2;
    26 float maxP = 200.0;
    272
    283int avperiodogram (int argc, char **argv) {
     
    5732    PARALLEL = TRUE;
    5833  }
     34
     35  /* load photcode information (this needs to come before the SelectRegions command below to define the catdir */
     36  if (!InitPhotcodes ()) goto escape;
     37  int Nsecfilt = GetPhotcodeNsecfilt ();
    5938
    6039  // parse skyregion options.  NOTE: this is stripped off in parallel operation and always
     
    7049
    7150  // **** launch parallel / remote jobs ****
    72   // this does all the work of re-packaging the command, calling it on the remote machines, then loading in the results
     51  // the result files are left behind for the user to access (no automatic re-merge)
    7352  if (PARALLEL && !HOST_ID) {
    74     int status = HostTableParallelOps (skylist, argc, argv, NULL, TRUE, 0, VERBOSE);
     53    int status = HostTableParallelOps (skylist, argc, argv, NULL, FALSE, 0, VERBOSE);
    7554    SkyListFree (skylist);
    7655    FreeSkyRegionSelection (selection);
     
    8261  if ((N = get_argument (argc, argv, "-min-period"))) {
    8362    remove_argument (N, &argc, argv);
    84     minP = atof (argv[N]);
     63    PeriodogramSetOptions (atof (argv[N]), NAN);
    8564    remove_argument (N, &argc, argv);
    8665  }
     
    8867  if ((N = get_argument (argc, argv, "-max-period"))) {
    8968    remove_argument (N, &argc, argv);
    90     maxP = atof (argv[N]);
     69    PeriodogramSetOptions (NAN, atof (argv[N]));
    9170    remove_argument (N, &argc, argv);
    9271  }
     
    179158  dvo_catalog_init (&catalog, TRUE);
    180159
    181   /* load photcode information */
    182   if (!InitPhotcodes ()) goto escape;
    183   int Nsecfilt = GetPhotcodeNsecfilt ();
    184 
    185160  // init locally static variables (time refs)
    186161  dbExtractAveragesInit ();
     162  dbExtractMeasuresInit(HOST_ID);
    187163
    188164  // examine line for 'where' or 'match to'.  'match to' is forbidden
     
    207183
    208184  // parse stack elements into fields and scalars as needed
     185  Nfields = 0;
     186  ALLOCATE (fields, dbField, 1);
    209187  if (!dbCheckStack (stack, Nstack, DVO_TABLE_AVERAGE, &fields, &Nfields)) goto escape;
    210188
     
    288266        // The first 3 values[] / fields[] will be time, mag, dmag
    289267        for (off_t n = 0; n < Nmfields; n++) {
    290           mvalues[n] = dbExtractMeasures (average, secfilt, measure, NULL, NULL, &mfields[n]);
     268          mvalues[n] = dbExtractMeasures (average, secfilt, &measure[k], NULL, NULL, &mfields[n]);
    291269        }
    292270        if (!dbBooleanCond (mstack, Nmstack, mvalues)) continue;
     271
     272        // do not allow any NAN or Inf values to polute the result
     273        if (!isfinite(mvalues[0].Flt)) continue;
     274        if (!isfinite(mvalues[1].Flt)) continue;
     275        if (!isfinite(mvalues[2].Flt)) continue;
    293276
    294277        time[Npts] = mvalues[0].Flt;
     
    299282      }
    300283
    301       PeriodogramResult *result = periodogram_fm_raw (time, mag, dmag, Npts);
     284      PeriodogramResult *result = PeriodogramRawFloatingMean (time, mag, dmag, Npts);
     285      free (time);
     286      free (mag);
     287      free (dmag);
     288
     289      if (!result) continue;
     290
     291      if (VERBOSE2) gprint (GP_ERR, "periodogram for %d, %d\n", (int) average->catID, (int) average->objID);
    302292
    303293      // XXX if we have 1e6 objects, we will have output files with sizes of ~80GB
     
    308298      Nobject ++;
    309299    }
    310 
    311     fclose (foutput);
    312     fflush (foutput);
    313 
    314300    dvo_catalog_free (&catalog);
    315301  }
    316302  ClearInterrupt (old_sigaction);
    317303
    318   if (values) free (values);
     304  fclose (foutput);
     305  fflush (foutput);
     306
     307  // free measure stack stuff
     308  FREE (mvalues);
     309  dbFreeFields(mfields, Nmfields);
     310  dbFreeStack(mstack, Nmstack);
     311  FREE (mstack);
     312
     313  FREE (values);
    319314  dbFreeFields (fields, Nfields);
    320315  dbFreeStack (stack, Nstack);
    321   if (stack) free (stack);
     316  FREE (stack);
     317
    322318  SkyListFree (skylist);
    323319  FreeSkyRegionSelection (selection);
     320
     321  free (output);
    324322  return (TRUE);
    325323
     
    341339  return (FALSE);
    342340}
    343 
    344 // low-level periodogram-fm function
    345 PeriodogramResult *periodogram_fm_raw (opihi_flt *time, opihi_flt *flux, opihi_flt *dflux, int Npts) {
    346 
    347   /* find the max baseline, sum the inverse variances */
    348   double minT = time[0];
    349   double maxT = time[0];
    350   opihi_flt Weight = 0;
    351 
    352   opihi_flt *tv =  time;
    353   opihi_flt *df = dflux;
    354   opihi_flt *fv =  flux;
    355 
    356   for (int i = 0; i < Npts; i++, tv++, df++) {
    357     minT = MIN (minT, *tv);
    358     maxT = MAX (maxT, *tv);
    359     // skip points with 0.0 error? or add minimum variance?
    360     Weight += 1.0 / (SQ(*df) + MIN_VAR); // MIN_VAR : added in quadrature to avoid Inf (XXX make this a user parameter?)
    361   }
    362 
    363   double WeightInv = 1.0 / Weight;
    364   double dTime = maxT - minT;
    365   if (dTime == 0) {
    366     gprint (GP_ERR, "ERROR: time range is zero\n");
    367     return NULL;
    368   }
    369 
    370   int Np = 0;
    371   int NP = NPERIODS;
    372   ALLOCATE_PTR (period, double, NP);
    373   ALLOCATE_PTR (power, double, NP);
    374 
    375   // for testing, we are going to write out the terms explicitly
    376   // for optimization, some of the trig functions can be calculated more efficiently
    377   // by storing cos,sin(w t) and using some recurrence rules
    378 
    379   // maxP = minP * dP^n [n = 0 -- Nperiods-1]
    380   // log(maxP) = log(minP) + (Nperiods - 1) * log (dP)
    381   // log(dP) = (log(maxP) - log(minP)) / (Nperiods - 1)
    382   // double dP = LINEAR ? (maxP - minP)/(NPERIODS - 1) : pow(10.0, ((log10(maxP) - log10(minP))/(NPERIODS - 1)));
    383 
    384   double P = minP;
    385   for (Np = 0; (Np < NPERIODS) && (P <= maxP); Np++) {
    386     double w = 2*M_PI/P;
    387    
    388     /* find the period offset tau  */
    389     tv = time;
    390     df = dflux;
    391 
    392     double cs = 0.0, sn = 0.0, sn2 = 0.0, cs2 = 0.0;
    393 
    394     for (int i = 0; i < Npts; i++, tv++, df++) {
    395       opihi_flt wt = WeightInv / (SQ(*df) + MIN_VAR);
    396       cs  += wt*cos (*tv*w);
    397       sn  += wt*sin (*tv*w);
    398       cs2 += wt*cos (*tv*w*2);
    399       sn2 += wt*sin (*tv*w*2);
    400     }
    401     double ytan = sn2 - 2*cs*sn;
    402     double xtan = cs2 - (SQ(cs) - SQ(sn));
    403     double tau = 0.5*atan2 (ytan, xtan) / w;
    404      
    405     /* find the power at this period */
    406     tv = time;
    407     df = dflux;
    408     fv =  flux;
    409 
    410     // YY = YY_s - Y_s*Y_s
    411     // CC = CC_s - C_s*C_s
    412     // SS = SS_s - S_s*S_s
    413     // YC = YC_s - Y_s*C_s
    414     // YS = YS_s - Y_s*S_s
    415    
    416     double YY_s = 0.0, CC_s = 0.0, SS_s = 0.0, YC_s = 0.0, YS_s = 0.0;
    417     double Y_s = 0.0, C_s = 0.0, S_s = 0.0;
    418     for (int i = 0; i < Npts; i++, tv++, fv++, df++) {
    419       opihi_flt wt = WeightInv / (SQ(*df) + MIN_VAR);
    420       cs = cos (w*(*tv-tau));
    421       sn = sin (w*(*tv-tau));
    422 
    423       double wtf = *fv*wt;
    424       Y_s += wtf;
    425       C_s += wt*cs;
    426       S_s += wt*sn;
    427 
    428       YY_s += wtf*(*fv);
    429       YC_s += wtf*cs;
    430       YS_s += wtf*sn;
    431 
    432       CC_s += wt*cs*cs;
    433       SS_s += wt*sn*sn;
    434     }
    435     double YY = YY_s - SQ(Y_s);
    436     double YC = YC_s - Y_s*C_s;
    437     double YS = YS_s - Y_s*S_s;
    438     double CC = CC_s - SQ(C_s);
    439     double SS = SS_s - SQ(S_s);
    440 
    441     double Pa = SQ(YC) / CC;
    442     double Pb = SQ(YS) / SS;
    443     double Po = (Pa + Pb) / YY;
    444 
    445     power[Np] = Po;
    446     period[Np] = P;
    447     if (Np >= NP) {
    448       NP += 100;
    449       REALLOCATE (power, opihi_flt, NP);
    450       REALLOCATE (period, opihi_flt, NP);
    451     }
    452 
    453     float dP = 0.4 * SQ(P) / dTime;
    454     P += dP;
    455   }
    456 
    457   ALLOCATE_PTR (result, PeriodogramResult, 1);
    458   result->Nperiods = Np;
    459   result->period = period;
    460   result->power = power;
    461  
    462   return result;
    463 }
    464 
    465 void PeriodogramResultFree (PeriodogramResult *result) {
    466   if (!result) return;
    467 
    468   FREE (result->period);
    469   FREE (result->power);
    470   free (result);
    471   return;
    472 }
    473 
    474 // write the period and power vectors to an extension in this output file
    475 void PeriodogramResultSave (PeriodogramResult *result, char *extname, FILE *foutput, Average *average) {
    476 
    477   // generate a binary table
    478   Header outheader;
    479   FTable outtable;
    480   gfits_init_header (&outheader);
    481   gfits_init_table  (&outtable);
    482   outtable.header = &outheader;
    483 
    484   gfits_create_table_header (&outheader, "BINTABLE", extname);
    485 
    486   // add some metadata derived from average:
    487   gfits_modify (&outheader, "RA_OBJ",  "%lf", 1, average->R);
    488   gfits_modify (&outheader, "DEC_OBJ", "%lf", 1, average->D);
    489   gfits_modify (&outheader, "OBJ_ID",  "%d", 1, average->objID);
    490   gfits_modify (&outheader, "CAT_ID",  "%d", 1, average->catID);
    491 
    492   gfits_define_bintable_column (&outheader, "D", "PERIOD", NULL, NULL, 1.0, 0.0);
    493   gfits_define_bintable_column (&outheader, "D", "POWER", NULL, NULL, 1.0, 0.0);
    494 
    495   gfits_create_table (&outheader, &outtable);
    496 
    497   gfits_set_bintable_column (&outheader, &outtable, "PERIOD", result->period, result->Nperiods);
    498   gfits_set_bintable_column (&outheader, &outtable, "POWER",  result->power,  result->Nperiods);
    499 
    500   // write the actual table data
    501   gfits_fwrite_Theader (foutput, &outheader);
    502   gfits_fwrite_table   (foutput, &outtable);
    503    
    504   gfits_free_header (&outheader);
    505   gfits_free_table  (&outtable);
    506 }
  • trunk/Ohana/src/opihi/dvo/dvo_host_utils.c

    r40291 r40408  
    212212        continue;
    213213      }
    214       free (table->hosts[i].results);
    215       table->hosts[i].results = NULL;
     214      // free (table->hosts[i].results);
     215      // table->hosts[i].results = NULL;
    216216      set_int_variable (name, 1); // result file has been read
    217217
     
    225225        FreeVectorArray (invec, Ninvec);
    226226      }
     227    } else {
     228      // free (table->hosts[i].results);
     229      // table->hosts[i].results = NULL;
    227230    }
    228231  }
     
    242245  free (vec);
    243246
    244   free (table);
     247  FreeHostTable (table);
    245248  return TRUE;
    246249}
  • trunk/Ohana/src/opihi/dvo/init.c

    r40398 r40408  
    44int avmatch         PROTO((int, char **));
    55int avperiodogram   PROTO((int, char **));
     6int avperiodomatch  PROTO((int, char **));
    67int badimages       PROTO((int, char **));
    78int calextract      PROTO((int, char **));
     
    6869  {1, "avextract",   avextract,    "extract average data values"},
    6970  {1, "avmatch",     avmatch,      "extract average data values matched to RA,DEC points"},
    70   {1, "avperiodogram", avperiodogram, "perform periodogram on objects based on restrictions"},
     71  {1, "avperiodogram",  avperiodogram, "perform periodogram on objects based on restrictions"},
     72  {1, "avperiodomatch", avperiodomatch, "perform periodogram on objects based on ra,dec list"},
    7173  {1, "badimages",   badimages,    "look for images with anomalous astrometry"},
    7274//  {1, "calextract",  calextract,   "extract photometry calibration"},
  • trunk/Ohana/src/opihi/dvo/region_list.c

    r33662 r40408  
    181181  SkyList *skylist;
    182182
     183  // the list of regions comes directly from a file
     184  if (selection->list != NULL) {
     185    skylist = SkyListLoadFile (selection->list);
     186    return (skylist);
     187  }
     188
     189  // all other options require sky to be set
     190  if (!sky) {
     191    gprint (GP_ERR, "CATDIR not set\n");
     192    return NULL;
     193  }
     194
    183195  /* determine region-file names */
    184196  if (selection->name != NULL) {
     
    186198    return (skylist);
    187199  }
    188 
    189   if (selection->list != NULL) {
    190     skylist = SkyListLoadFile (selection->list);
    191     return (skylist);
    192   }
    193200
    194201  if (selection->useDisplay) {
  • trunk/Ohana/src/opihi/include/dvoshell.h

    r39569 r40408  
    4141char *HOSTDIR;
    4242char *RESULT_FILE;
     43
     44typedef struct {
     45  int Nmeasure;
     46  int Nperiods;
     47  opihi_flt *period;
     48  opihi_flt *power;
     49  double P50;
     50  double R50;
     51  double R90;
     52} PeriodogramResult;
     53
     54/*** Periodogram functions (avperiodogram and avperiodomatch) ***/
     55void PeriodogramResultFree (PeriodogramResult *result);
     56void PeriodogramResultSave (PeriodogramResult *result, char *extname, FILE *foutput, Average *average);
     57void PeriodogramSetOptions (float minimumPeriod, float maximumPeriod);
     58PeriodogramResult *PeriodogramRawFloatingMean (opihi_flt *time, opihi_flt *flux, opihi_flt *dflux, int Npts);
    4359
    4460/*** dvo prototypes ***/
Note: See TracChangeset for help on using the changeset viewer.