{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Generate synchrotron amplitude map\n" ], "id": "d8f246b766ea" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import healpy as hp\n", "from pathlib import Path" ], "id": "f7335ca1a069" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "datadir=Path(\"data/\")\n", "output_dir = Path(\"production-data/synch\")\n", "output_dir_raw = output_dir / \"raw\"" ], "id": "a76e33bd29bc" }, { "cell_type": "code", "execution_count": null, "metadata": { "tags": [ "parameters" ] }, "outputs": [], "source": [ "output_nside = 2048" ], "id": "13c78ea58101" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output_lmax = int(min(2.5*output_nside, 8192*2))" ], "id": "6221c18f993b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "comp = \"synch\"\n", "sub = \"template\"" ], "id": "3de296d6054c" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Large scales" ], "id": "673fd732e2fa" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "largescale_filename = [f\"{comp}_largescale_{sub}_logpoltens_alm_lmax128_2023.02.24.fits.gz\"]" ], "id": "928aae16381e" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "assert len(largescale_filename) == 1" ], "id": "e1a6b95ab9f9" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "largescale_filename = largescale_filename[0]" ], "id": "dfc8351eba0b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "largescale_filename" ], "id": "4aad9493b58a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "alm_log_pol_tens_large_scale = hp.read_alm(largescale_filename, hdu=(1,2,3))" ], "id": "cd2adbac6877" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "map_log_pol_tens_large_scale = hp.alm2map(alm_log_pol_tens_large_scale.astype(np.complex128), nside=output_nside)" ], "id": "9f1ca3364f9a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del alm_log_pol_tens_large_scale" ], "id": "d5fdcf423c40" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Small scales modulation" ], "id": "0cb54dd56a8d" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "modulate_alm = { k:hp.read_alm(output_dir_raw/f\"synch_{k}_modulation_alms_lmax64_2023.02.24.fits.gz\").astype(np.complex128) for k in [\"temperature\",\"polarization\"] }" ], "id": "910f656b3be7" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Small scales" ], "id": "15019f8fd423" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "cl_small_scale = hp.read_cl(output_dir_raw / \"synch_small_scales_cl_lmax16384_2023.02.24.fits.gz\")" ], "id": "3cfb90cc3dae" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "synalm_lmax = 8192*2 # for reproducibility\n", "# synalm_lmax = 512\n", "\n", "np.random.seed(555)\n", "\n", "alm_log_pol_tens_small_scale = hp.synalm(\n", " list(cl_small_scale),\n", " lmax=synalm_lmax,\n", " new=True,\n", ")\n", "\n", "alm_log_pol_tens_small_scale = [hp.almxfl(each, np.ones(output_lmax+1)) for each in alm_log_pol_tens_small_scale]\n", "map_log_pol_tens_small_scale = hp.alm2map(alm_log_pol_tens_small_scale, nside=output_nside)\n", "del alm_log_pol_tens_small_scale\n", "map_log_pol_tens_small_scale[0] *= hp.alm2map(modulate_alm[\"temperature\"], output_nside)\n", "map_log_pol_tens_small_scale[1:] *= hp.alm2map(modulate_alm[\"polarization\"], output_nside)\n", "assert np.isnan(map_log_pol_tens_small_scale).sum() == 0" ], "id": "abd0558e95f5" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Combine scales\n", "\n", "* Combine small and large scale maps\n", "* Transform from logpoltens to IQU\n", "* Write output map" ], "id": "c45da79b4008" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "map_log_pol_tens = map_log_pol_tens_large_scale + map_log_pol_tens_small_scale" ], "id": "b7738e8634ec" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "del map_log_pol_tens_large_scale, map_log_pol_tens_small_scale" ], "id": "0aae26121905" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from pysm3.utils import log_pol_tens_to_map, add_metadata" ], "id": "0b2054429ff0" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output_map = log_pol_tens_to_map(map_log_pol_tens)" ], "id": "bc50e9b72481" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Galactic plane fix" ], "id": "f677d0f26003" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "galplane_fix = hp.read_map(output_dir_raw / \"synch_galplane.fits.gz\", (0, 1, 2, 3))" ], "id": "7dbdde1c5738" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "output_map *= hp.ud_grade(galplane_fix[3], output_nside)\n", "output_map += hp.ud_grade(galplane_fix[:3] * (1 - galplane_fix[3]), output_nside)" ], "id": "ffc73476bf3a" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "if output_nside < 4096:\n", " hp.mollview((output_map - log_pol_tens_to_map(map_log_pol_tens))[0])\n", " hp.mollview(output_map[0])" ], "id": "944f675e1a45" }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Write outputs" ], "id": "f1cd4c15fbf9" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from datetime import date\n", "today = date.today()\n", "version = today.strftime(\"%Y.%m.%d\")\n", "version" ], "id": "547b23bd7f59" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "hp.write_map(output_dir / f\"synch_template_nside{output_nside}_{version}.fits\", output_map, dtype=np.float32, overwrite=True)" ], "id": "860aa4c7aa3b" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "add_metadata([output_dir / f\"synch_template_nside{output_nside}_{version}.fits\"], coord=\"G\", unit=\"uK_RJ\", ref_freq=\"23 GHz\")" ], "id": "e1bca5588bf0" }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [], "id": "a33736913fdf" } ], "metadata": { "kernelspec": { "display_name": "pycmb", "language": "python", "name": "pycmb" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 4 }