Skip to content

Commit

Permalink
♻ Duplicate + Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Aug 30, 2024
1 parent 4074e50 commit 8e67e6c
Show file tree
Hide file tree
Showing 16 changed files with 1,801 additions and 0 deletions.
1 change: 1 addition & 0 deletions binnew/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.1
11 changes: 11 additions & 0 deletions binnew/FRB20180916B_subwindows_periods.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
burst_rate,burst_rate_neg,burst_rate_pos,period,period_neg,period_pos,source,subinterval,n,tau
0.4034,,,15.91,0.05,0.52,"this work",0.1,5,64
0.337,,,16.30,0.55,0.64,"this work",0.2,10,131
0.442,,,16.32,0.40,0.23,"this work",0.3,19,262
0.413,,,16.24,0.27,0.37,"this work",0.4,24,343
0.4025,,,16.36,0.24,0.21,"this work",0.5,29,458
0.458,,,16.28,0.19,0.18,"this work",0.6,42,555
0.447,,,16.30,0.11,0.10,"this work",0.7,49,655
0.431,,,16.24,0.08,0.17,"this work",0.8,55,733
0.398,,,16.33,0.14,0.06,"this work",0.9,57,847
0.434,,,16.32,0.10,0.07,"this work",1.0,70,929
66 changes: 66 additions & 0 deletions binnew/FRB20180916B_subwindows_periods.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import pandas as pd

import matplotlib.pyplot as plt
plt.rcParams['font.size'] = 15

data = pd.read_csv("./FRB20180916B_subwindows_period.csv")

outdir = '../output/2024-08-04'

Yval = "tau"
Ylabel = r"$\frac{\tau}{n - 1}$ / Period"
group = "this work"
# plt.scatter(
# x=data[data["source"] == group]["period"],
# y=data[data["source"] == group]["burst_rate"],
# )

plt.figure(figsize=(10, 7))
for i, subinterval in enumerate(
data[(data["source"] == group)]["subinterval"].unique()
):
plt.errorbar(
x=data[(data["source"] == group) & (data["subinterval"] == subinterval)][
"period"
],
y=1/(data[(data["source"] == group) & (data["subinterval"] == subinterval)][
"period"
]/(data[(data["source"] == group) & (data["subinterval"] == subinterval)][
Yval
]/(data[(data["source"] == group) & (data["subinterval"] == subinterval)][
"n"
]-1))),
xerr=data[(data["source"] == group) & (data["subinterval"] == subinterval)][
["period_neg", "period_pos"]
].T,
# fmt="o",
fmt=["^", "o", "v", "P", "p", "d", "s", "X", "D", "o"][i],
color=[
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#1f77b4",
"#d62728",
][i],
label=f"{int(100*subinterval)}%",
capsize=0.1,
)

# CHIME/FRB 2020
plt.axvline(16.35, color="green", alpha=0.5)
plt.axvspan(16.35 - 0.15, 16.35 + 0.15, color="green", alpha=0.2, hatch="|")

# Sand et al 2023
plt.axvline(16.34, color="orange", alpha=0.5)
plt.axvspan(16.35 - 0.07, 16.35 + 0.07, color="orange", alpha=0.2, hatch="/")
plt.legend(title="Observation window")
plt.xlabel("Period (d)")
plt.title("FRB20180916B")
plt.ylabel(Ylabel)
# plt.xlim(15.99, 16.7)
plt.savefig(f"{outdir}/FRB20180916B/{Yval}-vs-period.png")
3 changes: 3 additions & 0 deletions binnew/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# bin

Describe your project here.
129 changes: 129 additions & 0 deletions binnew/download_exposures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#! usr/bin/env python

# Author: Murthadza Aznam
# Date: 2024-07-05
# Python Version: 3.12

"""Download exposure data between ranges
"""

import argparse
import datetime
from pathlib import Path
from concurrent.futures import ThreadPoolExecutor
import signal
import threading
from typing import Iterable
import requests

from rich.progress import Progress, TaskID, TextColumn, BarColumn, DownloadColumn, TransferSpeedColumn, TimeElapsedColumn, TimeRemainingColumn

done_event = threading.Event()

def handle_sigint(signum, frame):
done_event.set()

signal.signal(signal.SIGINT, handle_sigint)

def run_download_from_url_task(
task_id: TaskID, url: str, dest_file: str, progress_manager: Progress
) -> None:
"""Copy data from a url to a local file."""
progress_manager.console.log(f"Requesting {url}")
response = requests.get(url, stream=True)
# This will break if the response doesn't contain content length
progress_manager.update(
task_id, total=int(response.headers.get("Content-length")), visible=True
)
with open(dest_file, "wb") as to_file:
progress_manager.start_task(task_id)
for data in response.iter_content(chunk_size=32768):
to_file.write(data)
progress_manager.update(task_id, advance=len(data))
if done_event.is_set():
return
progress_manager.remove_task(task_id)
progress_manager.console.log(f":inbox_tray: {dest_file} downloaded.")

def manage_download_exposure_data_task(
dates: Iterable[datetime.datetime], progress_manager: Progress, basepath: Path, baseurl: str
):
"""Download multiple files to the given directory."""
# expected_kwargs = run_download_from_url_task.__annotations__
# generated_kwarg = ["task_id", "progress_manager"]
# for keyword in expected_kwargs.keys():
# if keyword not in [*kwargs.keys(), "return"] and keyword not in generated_kwarg:
# raise AttributeError(
# f"Expected {keyword} in function arguments {expected_kwargs.keys()} but only {kwargs.keys()} was given."
# )
with progress_manager:
with ThreadPoolExecutor(max_workers=8) as pool:
for date in dates:
filename = f"exposure_{date.strftime("%Y%m%d")}_{(date + datetime.timedelta(days=1)).strftime("%Y%m%d")}_transit_L_beam_FWHM-600_res_4s_0.86_arcmin.npz"
task_id = progress_manager.add_task(
"download", filename=f"exp_{date.strftime("%Y%m%d")}_L", start=False, visible=False
)
pool.submit(
run_download_from_url_task,
task_id=task_id,
url = f"{baseurl}/{filename}",
dest_file = Path(basepath, filename),
progress_manager=progress_manager,
)
filename = f"exposure_{date.strftime("%Y%m%d")}_{(date + datetime.timedelta(days=1)).strftime("%Y%m%d")}_transit_U_beam_FWHM-600_res_4s_0.86_arcmin.npz"
task_id = progress_manager.add_task(
"download", filename=f"exp_{date.strftime("%Y%m%d")}_U", start=False, visible=False
)
pool.submit(
run_download_from_url_task,
task_id=task_id,
url = f"{baseurl}/{filename}",
dest_file = Path(basepath, filename),
progress_manager=progress_manager,
)

def download(arguments: argparse.Namespace):
basepath = arguments.out
date_begin: datetime.datetime = arguments.begin
date_end: datetime.datetime = arguments.end

length = (date_end - date_begin).days
baseurl = "https://ws.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/files/vault/AstroDataCitationDOI/CISTI.CANFAR/23.0004/data/exposure/daily_exposure_maps"

dates = [date_begin + datetime.timedelta(days=i) for i in range(length+1)]
progress = Progress(
TextColumn("{task.id:>3d}/"),
TextColumn(f"{length*2:>3d} "),
TextColumn(
"[bold blue]{task.fields[filename]}",
justify="right",
),
BarColumn(bar_width=None),
"[progress.percentage]{task.percentage:>3.1f}%",
"•",
DownloadColumn(),
"•",
TransferSpeedColumn(),
"•",
TimeElapsedColumn(),
"/",
TimeRemainingColumn(),
transient=True,
)
manage_download_exposure_data_task(
dates=dates,
progress_manager=progress,
basepath=basepath,
baseurl=baseurl,
)

def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser()
parser.add_argument("--out", type=Path, required=True)
parser.add_argument("--begin", type=datetime.datetime.fromisoformat, required=True)
parser.add_argument("--end", type=datetime.datetime.fromisoformat, required=True)
return parser.parse_args()

if __name__ == "__main__":
arguments = parse_arguments()
download(arguments)
Loading

0 comments on commit 8e67e6c

Please sign in to comment.