@@ -224,24 +224,15 @@ end
224224 end
225225end
226226
227- """
228- avg_pds_from_iterable(flux_iterable, dt::Real; kwargs...)
229-
230- Compute averaged power density spectrum from an iterable of flux segments.
231-
232- ## Parameters
233- - `flux_iterable`: Iterator of flux segments
234- - `dt::Real`: Time resolution
235- - `norm::String="frac"`: Normalization type
236- - `use_common_mean::Bool=true`: Use common mean across segments
237- - `silent::Bool=false`: Suppress progress output
238- """
239- function avg_pds_from_iterable (flux_iterable, dt:: Real ;
240- norm:: String = " frac" ,
241- use_common_mean:: Bool = true ,
242- silent:: Bool = false )
243-
244- # Initialize accumulators
227+ function avg_pds_from_iterable (flux_iterable, dt:: Real ; norm:: String = " frac" ,
228+ use_common_mean:: Bool = true ,
229+ silent:: Bool = false )
230+ local_show_progress = show_progress
231+ if silent
232+ local_show_progress = identity
233+ end
234+
235+ # Initialize stuff
245236 cross = unnorm_cross = nothing
246237 n_ave = 0
247238 freq = nothing
@@ -251,9 +242,8 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
251242 fgt0 = nothing
252243 n_bin = nothing
253244
254- for flux in flux_iterable
255- # Validate segment
256- if isnothing (flux) || all (iszero, flux)
245+ for flux in local_show_progress (flux_iterable)
246+ if isnothing (flux) || all (iszero,flux)
257247 continue
258248 end
259249
@@ -262,7 +252,7 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
262252 variance = nothing
263253 if flux isa Tuple
264254 flux, err = flux
265- variance = Statistics. mean (err)^ 2
255+ variance = Statistics. mean (err) ^ 2
266256 end
267257
268258 # Calculate the FFT
@@ -279,43 +269,40 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
279269 sum_of_photons += n_ph
280270
281271 if ! isnothing (variance)
282- common_variance = isnothing (common_variance) ? variance : common_variance + variance
272+ common_variance = sum_if_not_none_or_initialize (common_variance, variance)
283273 end
284274
285275 # In the first loop, define the frequency and the freq. interval > 0
286- # Initialize frequency arrays
287276 if isnothing (cross)
288277 fgt0 = positive_fft_bins (n_bin)
289- freq = FFTW . fftfreq (n_bin, dt)[fgt0]
278+ freq = fftfreq (n_bin, dt)[fgt0]
290279 end
291280
292- keepat! (unnorm_power, fgt0)
281+ # No need for the negative frequencies
282+ keepat! (unnorm_power,fgt0)
293283
294284 # If the user wants to normalize using the mean of the total
295285 # lightcurve, normalize it here
296286 cs_seg = unnorm_power
297- if ! use_common_mean
298- mean_flux = n_ph / n_bin
287+ if ! (use_common_mean)
288+ mean = n_ph / n_bin
289+
299290 cs_seg = normalize_periodograms (
300- unnorm_power,
301- dt,
302- n_bin;
303- mean_flux = mean_flux,
304- n_ph = n_ph,
305- norm = norm,
306- power_type = " real"
291+ unnorm_power, dt, n_bin; mean_flux = mean, n_ph= n_ph,
292+ norm= norm, variance= variance,
307293 )
308294 end
309295
310296 # Accumulate the total sum cross spectrum
311297 cross = sum_if_not_none_or_initialize (cross, cs_seg)
312- unnorm_cross = sum_if_not_none_or_initialize (unnorm_cross, unnorm_power)
298+ unnorm_cross = sum_if_not_none_or_initialize (unnorm_cross,
299+ unnorm_power)
313300
314301 n_ave += 1
315302 end
316303
317- # Validate results
318- if isnothing (cross) || n_ave == 0
304+ # If there were no good intervals, return nothing
305+ if isnothing (cross)
319306 return nothing
320307 end
321308
@@ -337,28 +324,19 @@ function avg_pds_from_iterable(flux_iterable, dt::Real;
337324 # Final normalization (If not done already!)
338325 if use_common_mean
339326 cross = normalize_periodograms (
340- unnorm_cross,
341- dt,
342- n_bin;
343- mean_flux = common_mean,
344- n_ph = n_ph,
345- norm = norm,
346- power_type = " real"
327+ unnorm_cross, dt, n_bin; mean_flux= common_mean, n_ph= n_ph,
328+ norm= norm, variance= common_variance
347329 )
348330 end
349331
350- # Calculate power errors
351- power_errors = cross ./ sqrt (n_ave)
332+ results = DataFrame ()
333+ results[! ," freq" ] = freq
334+ results[! ," power" ] = cross
335+ results[! ," unnorm_power" ] = unnorm_cross
336+ results = (; results , n = n_bin, m = n_ave, dt, norm, df = 1 / (dt * n_bin), nphots = n_ph, setment_size = dt * n_bin, mean = common_mean, variance = common_variance)
337+ results = DataFrame (results. results)
338+ return results
352339
353- # Return AveragedPowerspectrum
354- return AveragedPowerspectrum (
355- freq[2 : end ],
356- cross[2 : end ],
357- power_errors[2 : end ],
358- n_ave,
359- n_bin,
360- dt
361- )
362340end
363341
364342function avg_cs_from_iterables_quick (flux_iterable1 ,flux_iterable2,
@@ -698,24 +676,33 @@ function avg_cs_from_iterables(
698676 return results
699677
700678end
701- function avg_pds_from_events (times:: AbstractVector{<:Real} , gti:: AbstractMatrix{<:Real} ,
702- segment_size:: Real , dt:: Real ; norm:: String = " frac" ,
703- use_common_mean:: Bool = true , silent:: Bool = false ,
704- fluxes= nothing , errors= nothing )
679+
680+ function avg_pds_from_events (times:: AbstractVector{<:Real} , gti:: AbstractMatrix{<:Real} ,
681+ segment_size:: Real , dt:: Real ; norm:: String = " frac" ,
682+ use_common_mean:: Bool = true , silent:: Bool = false ,
683+ fluxes= nothing , errors= nothing )
705684 if isnothing (segment_size)
706685 segment_size = max (gti) - min (gti)
707686 end
708- n_bin = round (Int, segment_size / dt)
687+ n_bin = round (Int,segment_size / dt)
709688 dt = segment_size / n_bin
710689
711690 flux_iterable = get_flux_iterable_from_segments (times, gti, segment_size;
712- n_bin, fluxes= fluxes,
713- errors= errors)
691+ n_bin, fluxes= fluxes,
692+ errors= errors)
693+ cross = avg_pds_from_iterable (flux_iterable, dt, norm= norm,
694+ use_common_mean= use_common_mean,
695+ silent= silent)
696+
697+ if ! isnothing (cross)
698+ cross = (; cross, gti= gti)
699+ cross = DataFrame (cross. cross)
700+ end
701+
702+ return cross
714703
715- return avg_pds_from_iterable (flux_iterable, dt, norm= norm,
716- use_common_mean= use_common_mean,
717- silent= silent)
718704end
705+
719706function avg_cs_from_events (times1:: AbstractVector{<:Real} , times2:: AbstractVector{<:Real} ,
720707 gti:: AbstractMatrix{<:Real} , segment_size:: Real , dt:: Real ;
721708 norm:: String = " frac" , use_common_mean:: Bool = true ,
@@ -771,4 +758,4 @@ function avg_cs_from_events(times1:: AbstractVector{<:Real}, times2:: AbstractVe
771758 end
772759
773760 return results
774- end
761+ end
0 commit comments