From 3f80a50de51f2b057818f35250d9f25bc52bc247 Mon Sep 17 00:00:00 2001 From: Josh Dillon Date: Fri, 19 Jul 2024 20:35:33 -0700 Subject: [PATCH] separately smooth the relative phase between the polarizations --- notebooks/calibration_smoothing.ipynb | 88 ++++++++++++++++++++++----- 1 file changed, 74 insertions(+), 14 deletions(-) diff --git a/notebooks/calibration_smoothing.ipynb b/notebooks/calibration_smoothing.ipynb index 9b40d4a..416ac44 100644 --- a/notebooks/calibration_smoothing.ipynb +++ b/notebooks/calibration_smoothing.ipynb @@ -7,7 +7,7 @@ "source": [ "# Calibration Smoothing\n", "\n", - "**by Josh Dillon**, last updated July 15, 2023\n", + "**by Josh Dillon**, last updated July 19, 2023\n", "\n", "This notebook runs calibration smoothing to the gains coming out of [file_calibration](https://github.com/HERA-Team/hera_notebook_templates/blob/master/notebooks/file_calibration.ipynb) notebook. It removes any flags founds on by that notebook and replaces them with flags generated from [full_day_rfi](https://github.com/HERA-Team/hera_notebook_templates/blob/master/notebooks/full_day_rfi.ipynb) and [full_day_antenna_flagging](https://github.com/HERA-Team/hera_notebook_templates/blob/master/notebooks/full_day_antenna_flagging.ipynb). It also plots the results for a couple of antennas.\n", "\n", @@ -92,7 +92,7 @@ "id": "c6ecd16f", "metadata": {}, "source": [ - "## Load files" + "## Load files and select reference antenna(s)" ] }, { @@ -135,36 +135,66 @@ { "cell_type": "code", "execution_count": null, - "id": "ee831559", + "id": "0976d9b5-b08c-4899-a866-8badc6d9173d", "metadata": {}, "outputs": [], "source": [ - "cs = smooth_cal.CalibrationSmoother(cal_files, flag_file_list=(ant_flag_files + rfi_flag_files), ignore_calflags=True,\n", - " pick_refant=True, propagate_refant_flags=True, load_chisq=True, load_cspa=True, per_pol_refant=PER_POL_REFANT)\n", + "cs = smooth_cal.CalibrationSmoother(cal_files, flag_file_list=(ant_flag_files + rfi_flag_files),\n", + " ignore_calflags=True, pick_refant=False, load_chisq=True, load_cspa=True)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "4c2af8ac-efb8-4489-80f6-d0f30d8b62a8", + "metadata": {}, + "outputs": [], + "source": [ + "cs.refant = smooth_cal.pick_reference_antenna(cs.gain_grids, cs.flag_grids, cs.freqs, per_pol=True)\n", + "for pol in cs.refant:\n", + " print(f'Reference antenna {cs.refant[pol][0]} selected for smoothing {pol} gains.')\n", + "\n", + "if not PER_POL_REFANT:\n", + " # in this case, rephase both pols separately before smoothing, but also smooth the relative polarization calibration phasor\n", + " overall_refant = smooth_cal.pick_reference_antenna({ant: cs.gain_grids[ant] for ant in cs.refant.values()}, \n", + " {ant: cs.flag_grids[ant] for ant in cs.refant.values()}, \n", + " cs.freqs, per_pol=False)\n", + " print(f'Overall reference antenna {overall_refant} selected.')\n", + " other_refant = [ant for ant in cs.refant.values() if ant != overall_refant][0]\n", "\n", - "if PER_POL_REFANT:\n", - " for pol in cs.refant:\n", - " print(f'Reference antenna {cs.refant[pol][0]} selected for {pol}.')\n", - "else:\n", - " print(f'Reference antenna {cs.refant[0]} selected for both polarizations.')" + " relative_pol_phasor = cs.gain_grids[overall_refant] * cs.gain_grids[other_refant].conj() # TODO: is this conjugation right?\n", + " relative_pol_phasor /= np.abs(relative_pol_phasor)" ] }, { "cell_type": "code", "execution_count": null, - "id": "482760b0", + "id": "9f459577-0ba2-4c55-a767-df9d10c7226d", "metadata": {}, "outputs": [], "source": [ "# duplicate a small number of abscal gains for plotting\n", "antnums = set([ant[0] for ant in cs.ants])\n", "flags_per_antnum = [np.sum(cs.flag_grids[ant, 'Jnn']) + np.sum(cs.flag_grids[ant, 'Jee']) for ant in antnums]\n", - "\n", - "refant_nums = [ant[0] for ant in (cs.refant.values() if PER_POL_REFANT else [cs.refant])]\n", + "refant_nums = [ant[0] for ant in cs.refant.values()]\n", "candidate_ants = [ant for ant, nflags in zip(antnums, flags_per_antnum) if (ant not in refant_nums) and (nflags <= np.percentile(flags_per_antnum, 25))\n", " and not np.all(cs.flag_grids[ant, 'Jee']) and not np.all(cs.flag_grids[ant, 'Jnn'])]\n", "ants_to_plot = [func(candidate_ants) for func in (np.min, np.max)]\n", - "abscal_gains = {(ant, pol): np.array(cs.gain_grids[(ant, pol)]) for ant in ants_to_plot for pol in ['Jee', 'Jnn']}" + "abscal_gains = {}\n", + "for pol in ['Jee', 'Jnn']:\n", + " for antnum in ants_to_plot:\n", + " refant_here = (cs.refant[pol] if PER_POL_REFANT else overall_refant)\n", + " abscal_gains[antnum, pol] = cs.gain_grids[(antnum, pol)] * np.abs(cs.gain_grids[refant_here]) / cs.gain_grids[refant_here]" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "ff18e5a9-23e5-41f5-ab0b-35f09ab67404", + "metadata": {}, + "outputs": [], + "source": [ + "cs.rephase_to_refant(propagate_refant_flags=True)" ] }, { @@ -175,6 +205,19 @@ "## Perform smoothing" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "178ce9a5-570e-4cbc-a7d0-978da71033a6", + "metadata": {}, + "outputs": [], + "source": [ + "if not PER_POL_REFANT:\n", + " # treat the relative_pol_phasor as if it were antenna -1\n", + " cs.gain_grids[(-1, other_refant[1])] = relative_pol_phasor\n", + " cs.flag_grids[(-1, other_refant[1])] = cs.flag_grids[overall_refant] | cs.flag_grids[other_refant]" + ] + }, { "cell_type": "code", "execution_count": null, @@ -186,6 +229,23 @@ " method='DPSS', fit_method='lu_solve', fix_phase_flips=True, flag_phase_flip_ints=True)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "10ff0705-f3fd-4570-b47a-e96e360d183c", + "metadata": {}, + "outputs": [], + "source": [ + "if not PER_POL_REFANT:\n", + " # put back in the smoothed phasor, ensuring the amplitude is 1 and that data are flagged anywhere either polarization's refant is flagged\n", + " smoothed_relative_pol_phasor = cs.gain_grids[(-1, other_refant[-1])] / np.abs(cs.gain_grids[(-1, other_refant[-1])])\n", + " for ant in cs.gain_grids:\n", + " if ant[0] >= 0 and ant[1] == other_refant[1]:\n", + " cs.gain_grids[ant] /= smoothed_relative_pol_phasor\n", + " cs.flag_grids[ant] |= (cs.flag_grids[(-1, other_refant[1])])\n", + " cs.refant = overall_refant" + ] + }, { "cell_type": "markdown", "id": "c40160e2",