@@ -664,15 +664,20 @@ def spherical_average(uvp_in, kbins, bin_widths, blpair_groups=None, time_avg=Fa
664664 if isinstance (bin_widths , (float , int )):
665665 bin_widths = np .ones_like (kbins ) * bin_widths
666666
667+ # copy input
668+ uvp = copy .deepcopy (uvp_in )
669+
670+ # transform kgrid to little_h units
671+ if not little_h :
672+ kbins = kbins / uvp .cosmo .h
673+ bin_widths = bin_widths / uvp .cosmo .h
674+
667675 # ensure bins don't overlap
668676 assert len (kbins ) == len (bin_widths )
669677 kbin_left = kbins - bin_widths / 2
670678 kbin_right = kbins + bin_widths / 2
671679 assert np .all (kbin_left [1 :] >= kbin_right [:- 1 ] - 1e-6 ), "kbins must not overlap"
672680
673- # copy input
674- uvp = copy .deepcopy (uvp_in )
675-
676681 # perform time and cylindrical averaging upfront if requested
677682 if not uvp .exact_windows and (blpair_groups is not None or time_avg ):
678683 uvp .average_spectra (blpair_groups = blpair_groups , time_avg = time_avg ,
@@ -695,11 +700,6 @@ def spherical_average(uvp_in, kbins, bin_widths, blpair_groups=None, time_avg=Fa
695700 if store_window :
696701 window_function_array = odict ()
697702
698- # transform kgrid to little_h units
699- if not little_h :
700- kbins = kbins / uvp .cosmo .h
701- bin_widths = bin_widths / uvp .cosmo .h
702-
703703 # iterate over spectral windows
704704 spw_ranges = uvp .get_spw_ranges ()
705705 for spw in uvp .spw_array :
@@ -887,7 +887,7 @@ def spherical_average(uvp_in, kbins, bin_widths, blpair_groups=None, time_avg=Fa
887887 time_avg = time_avg ,
888888 error_weights = error_weights ,
889889 spw_array = spw ,
890- little_h = little_h ,
890+ little_h = True ,
891891 verbose = True )[spw ]
892892
893893 # handle data arrays
@@ -943,9 +943,9 @@ def spherical_average(uvp_in, kbins, bin_widths, blpair_groups=None, time_avg=Fa
943943 return uvp
944944
945945def spherical_wf_from_uvp (uvp_in , kbins , bin_widths ,
946- blpair_groups = None , blpair_lens = None , blpair_weights = None ,
947- error_weights = None , time_avg = False , spw_array = None ,
948- little_h = True , verbose = False ):
946+ blpair_groups = None , blpair_lens = None , blpair_weights = None ,
947+ error_weights = None , time_avg = False , spw_array = None ,
948+ little_h = True , verbose = False ):
949949
950950 """
951951 Obtains exact spherical window functions from an UVPspec object,
@@ -984,8 +984,9 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
984984 Spectral window indices.
985985
986986 little_h : bool, optional
987- If True, kgrid is in h Mpc^-1 units, otherwise just Mpc^-1 units.
988- If False, user must ensure adopted h is consistent with uvp_in.cosmo
987+ If True, kbins is in h Mpc^-1 units, otherwise just Mpc^-1 units.
988+ The code ensures adopted h is consistent with uvp_in.cosmo. If not,
989+ it modifies the unit of kbins.
989990
990991 verbose : bool, optional
991992 If True, print progress, warnings and debugging info to stdout.
@@ -1004,6 +1005,21 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
10041005 if isinstance (bin_widths , (float , int )):
10051006 bin_widths = np .ones_like (kbins ) * bin_widths
10061007
1008+ # if window functions have been computed without little h
1009+ # it is not possible to re adjust so kbins need to be in Mpc-1
1010+ # and reciprocally
1011+ if little_h != ('h^-3' in uvp_in .norm_units ):
1012+ warnings .warn ('Changed little_h units to make kbins consistent ' \
1013+ 'with uvp.window_function_array. Might be inconsistent ' \
1014+ 'with the power spectrum units.' )
1015+ if little_h :
1016+ kbins *= uvp_in .cosmo .h
1017+ bin_widths *= uvp_in .cosmo .h
1018+ else :
1019+ kbins /= uvp_in .cosmo .h
1020+ bin_widths /= uvp_in .cosmo .h
1021+ little_h = 'h^-3' in uvp_in .norm_units
1022+
10071023 # ensure bins don't overlap
10081024 assert len (kbins ) == len (bin_widths )
10091025 kbin_left = kbins - bin_widths / 2
@@ -1062,11 +1078,6 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
10621078 time_avg = time_avg ,
10631079 inplace = True )
10641080
1065- # transform kgrid to little_h units
1066- if not little_h :
1067- kbins = kbins / uvp .cosmo .h
1068- bin_widths = bin_widths / uvp .cosmo .h
1069-
10701081 # initialize blank arrays and dicts
10711082 window_function_array = odict ()
10721083
@@ -1078,7 +1089,7 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
10781089 # construct array giving the k probed by each baseline-tau pair
10791090 kperps = uvp .cosmo .bl_to_kperp (uvp .cosmo .f2z (avg_nu ), little_h = little_h ) * blpair_lens
10801091 kparas = uvp .cosmo .tau_to_kpara (uvp .cosmo .f2z (avg_nu ), little_h = little_h ) * uvp .get_dlys (spw )
1081- kmags = np .sqrt (kperps [:, None ]** 2 + kparas ** 2 )
1092+ kmags = np .sqrt (kperps [:, None ]** 2 + kparas ** 2 )
10821093
10831094 # setup arrays
10841095 window_function_array [spw ] = np .zeros ((uvp .Ntimes , Nk , Nk , uvp .Npols ), dtype = np .float64 )
@@ -1092,17 +1103,17 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
10921103 kpara_bins = uvp .window_function_kpara [spw ][:, ip ]
10931104 ktot = np .sqrt (kperp_bins [:, None ]** 2 + kpara_bins ** 2 )
10941105
1095- cyl_wf = uvp .window_function_array [spw ][:, :, :, : , ip ]
1106+ cyl_wf = uvp .window_function_array [spw ][... , ip ]
10961107 # separate baseline-time axis to iterate over times
1097- cyl_wf = cyl_wf .reshape ((uvp .Ntimes , uvp .Nblpairs , * cyl_wf .shape [1 :] ))
1108+ cyl_wf = cyl_wf .reshape ((uvp .Ntimes , uvp .Nblpairs , * cyl_wf .shape [1 :]))
10981109
10991110 # take average for each time
11001111 for it in range (uvp .Ntimes ):
11011112 wf_spherical = np .zeros ((Nk , Nk ))
11021113 for m1 in range (Nk ):
11031114 mask1 = (kbin_left [m1 ] <= kmags ) & (kmags < kbin_right [m1 ])
11041115 if np .any (mask1 ):
1105- wf_temp = np .sum (cyl_wf [it , :, :, :, : ]* mask1 [:, :, None , None ].astype (int ), axis = (0 , 1 ))/ np .sum (mask1 )
1116+ wf_temp = np .sum (cyl_wf [it , ... ]* mask1 [:, :, None , None ].astype (int ), axis = (0 , 1 ))/ np .sum (mask1 )
11061117 if np .sum (wf_temp ) > 0. :
11071118 for m2 in range (Nk ):
11081119 mask2 = (kbin_left [m2 ] <= ktot ) & (ktot < kbin_right [m2 ])
@@ -1113,7 +1124,7 @@ def spherical_wf_from_uvp(uvp_in, kbins, bin_widths,
11131124 where = np .sum (wf_spherical [m1 ,:]) != 0 )
11141125 spw_window_function .append (wf_spherical )
11151126
1116- window_function_array [spw ][:, :, : , ip ] = np .copy (spw_window_function )
1127+ window_function_array [spw ][... , ip ] = np .copy (spw_window_function )
11171128
11181129 return window_function_array
11191130
0 commit comments