better handling of data rotation, add information about reduction in header
This commit is contained in:
@@ -17,7 +17,7 @@ from lib.utils import princ_angle, sci_not
|
||||
from matplotlib.colors import LogNorm
|
||||
|
||||
|
||||
def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=False, interactive=False):
|
||||
def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=False, interactive=False, **kwargs):
|
||||
# Reduction parameters
|
||||
# Deconvolution
|
||||
deconvolve = False
|
||||
@@ -36,13 +36,12 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
|
||||
# Background estimation
|
||||
error_sub_type = "freedman-diaconis" # sqrt, sturges, rice, scott, freedman-diaconis (default) or shape (example (51, 51))
|
||||
subtract_error = 1.0
|
||||
subtract_error = 0.7
|
||||
display_bkg = False
|
||||
|
||||
# Data binning
|
||||
rebin = True
|
||||
pxsize = 2
|
||||
px_scale = "px" # pixel, arcsec or full
|
||||
pxsize = 0.1
|
||||
pxscale = "arcsec" # pixel, arcsec or full
|
||||
rebin_operation = "sum" # sum or average
|
||||
|
||||
# Alignement
|
||||
@@ -55,23 +54,45 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
|
||||
# Smoothing
|
||||
smoothing_function = "combine" # gaussian_after, weighted_gaussian_after, gaussian, weighted_gaussian or combine
|
||||
smoothing_FWHM = 1.5 # If None, no smoothing is done
|
||||
smoothing_scale = "px" # pixel or arcsec
|
||||
smoothing_FWHM = 0.2 # If None, no smoothing is done
|
||||
smoothing_scale = "arcsec" # pixel or arcsec
|
||||
|
||||
# Rotation
|
||||
rotate_data = False # rotation to North convention can give erroneous results
|
||||
rotate_stokes = True
|
||||
rotate_North = True
|
||||
|
||||
# Polarization map output
|
||||
SNRp_cut = 3.0 # P measurments with SNR>3
|
||||
SNRi_cut = 3.0 # I measurments with SNR>30, which implies an uncertainty in P of 4.7%.
|
||||
flux_lim = None # lowest and highest flux displayed on plot, defaults to bkg and maximum in cut if None
|
||||
vec_scale = 3
|
||||
SNRi_cut = 1.0 # I measurments with SNR>30, which implies an uncertainty in P of 4.7%.
|
||||
flux_lim = 1e-19, 3e-17 # lowest and highest flux displayed on plot, defaults to bkg and maximum in cut if None
|
||||
scale_vec = 5
|
||||
step_vec = 1 # plot all vectors in the array. if step_vec = 2, then every other vector will be plotted if step_vec = 0 then all vectors are displayed at full length
|
||||
|
||||
# Pipeline start
|
||||
# Step 0:
|
||||
# Get parameters from kwargs
|
||||
for key, value in [
|
||||
["error_sub_type", error_sub_type],
|
||||
["subtract_error", subtract_error],
|
||||
["pxsize", pxsize],
|
||||
["pxscale", pxscale],
|
||||
["smoothing_function", smoothing_function],
|
||||
["smoothing_FWHM", smoothing_FWHM],
|
||||
["smoothing_scale", smoothing_scale],
|
||||
["SNRp_cut", SNRp_cut],
|
||||
["SNRi_cut", SNRi_cut],
|
||||
["flux_lim", flux_lim],
|
||||
["scale_vec", scale_vec],
|
||||
["step_vec", step_vec],
|
||||
]:
|
||||
try:
|
||||
value = kwargs[key]
|
||||
except KeyError:
|
||||
pass
|
||||
rebin = True if pxsize is not None else False
|
||||
|
||||
# Step 1:
|
||||
# Get data from fits files and translate to flux in erg/cm²/s/Angstrom.
|
||||
outfiles = []
|
||||
if infiles is not None:
|
||||
prod = np.array([["/".join(filepath.split("/")[:-1]), filepath.split("/")[-1]] for filepath in infiles], dtype=str)
|
||||
obs_dir = "/".join(infiles[0].split("/")[:-1])
|
||||
@@ -85,7 +106,7 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
target, products = retrieve_products(target, proposal_id, output_dir=output_dir)
|
||||
prod = products.pop()
|
||||
for prods in products:
|
||||
main(target=target, infiles=["/".join(pr) for pr in prods], output_dir=output_dir, crop=crop, interactive=interactive)
|
||||
outfiles.append(main(target=target, infiles=["/".join(pr) for pr in prods], output_dir=output_dir, crop=crop, interactive=interactive)[0])
|
||||
data_folder = prod[0][0]
|
||||
try:
|
||||
plots_folder = data_folder.replace("data", "plots")
|
||||
@@ -99,8 +120,8 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
figname = "_".join([target, "FOC"])
|
||||
figtype = ""
|
||||
if rebin:
|
||||
if px_scale not in ["full"]:
|
||||
figtype = "".join(["b", "{0:.2f}".format(pxsize), px_scale]) # additionnal informations
|
||||
if pxscale not in ["full"]:
|
||||
figtype = "".join(["b", "{0:.2f}".format(pxsize), pxscale]) # additionnal informations
|
||||
else:
|
||||
figtype = "full"
|
||||
if smoothing_FWHM is not None:
|
||||
@@ -137,9 +158,34 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
return_background=True,
|
||||
)
|
||||
|
||||
# Rotate data to have same orientation
|
||||
rotate_data = np.unique([float(head["ORIENTAT"]) for head in headers]).size != 1
|
||||
if rotate_data:
|
||||
ang = np.mean([head["ORIENTAT"] for head in headers])
|
||||
for head in headers:
|
||||
head["ORIENTAT"] -= ang
|
||||
data_array, error_array, data_mask, headers = proj_red.rotate_data(data_array, error_array, data_mask, headers)
|
||||
if display_data:
|
||||
proj_plots.plot_obs(
|
||||
data_array,
|
||||
headers,
|
||||
savename="_".join([figname, "rotate_data"]),
|
||||
plots_folder=plots_folder,
|
||||
norm=LogNorm(
|
||||
vmin=data_array[data_array > 0.0].min() * headers[0]["photflam"], vmax=data_array[data_array > 0.0].max() * headers[0]["photflam"]
|
||||
),
|
||||
)
|
||||
|
||||
# Align and rescale images with oversampling.
|
||||
data_array, error_array, headers, data_mask, shifts, error_shifts = proj_red.align_data(
|
||||
data_array, headers, error_array=error_array, background=background, upsample_factor=10, ref_center=align_center, return_shifts=True
|
||||
data_array,
|
||||
headers,
|
||||
error_array=error_array,
|
||||
data_mask=data_mask,
|
||||
background=background,
|
||||
upsample_factor=10,
|
||||
ref_center=align_center,
|
||||
return_shifts=True,
|
||||
)
|
||||
|
||||
if display_align:
|
||||
@@ -155,17 +201,11 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
# Rebin data to desired pixel size.
|
||||
if rebin:
|
||||
data_array, error_array, headers, Dxy, data_mask = proj_red.rebin_array(
|
||||
data_array, error_array, headers, pxsize=pxsize, scale=px_scale, operation=rebin_operation, data_mask=data_mask
|
||||
data_array, error_array, headers, pxsize=pxsize, scale=pxscale, operation=rebin_operation, data_mask=data_mask
|
||||
)
|
||||
|
||||
# Rotate data to have North up
|
||||
if rotate_data:
|
||||
data_mask = np.ones(data_array.shape[1:]).astype(bool)
|
||||
alpha = headers[0]["orientat"]
|
||||
data_array, error_array, data_mask, headers = proj_red.rotate_data(data_array, error_array, data_mask, headers, -alpha)
|
||||
|
||||
# Plot array for checking output
|
||||
if display_data and px_scale.lower() not in ["full", "integrate"]:
|
||||
if display_data and pxscale.lower() not in ["full", "integrate"]:
|
||||
proj_plots.plot_obs(
|
||||
data_array,
|
||||
headers,
|
||||
@@ -202,7 +242,7 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
|
||||
# Step 3:
|
||||
# Rotate images to have North up
|
||||
if rotate_stokes:
|
||||
if rotate_North:
|
||||
I_stokes, Q_stokes, U_stokes, Stokes_cov, data_mask, headers = proj_red.rotate_Stokes(
|
||||
I_stokes, Q_stokes, U_stokes, Stokes_cov, data_mask, headers, SNRi_cut=None
|
||||
)
|
||||
@@ -215,7 +255,7 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
# Step 4:
|
||||
# Save image to FITS.
|
||||
figname = "_".join([figname, figtype]) if figtype != "" else figname
|
||||
Stokes_test = proj_fits.save_Stokes(
|
||||
Stokes_hdul = proj_fits.save_Stokes(
|
||||
I_stokes,
|
||||
Q_stokes,
|
||||
U_stokes,
|
||||
@@ -238,25 +278,25 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
# crop to desired region of interest (roi)
|
||||
if crop:
|
||||
figname += "_crop"
|
||||
stokescrop = proj_plots.crop_Stokes(deepcopy(Stokes_test), norm=LogNorm())
|
||||
stokescrop = proj_plots.crop_Stokes(deepcopy(Stokes_hdul), norm=LogNorm())
|
||||
stokescrop.crop()
|
||||
stokescrop.write_to("/".join([data_folder, figname + ".fits"]))
|
||||
Stokes_test, headers = stokescrop.hdul_crop, [dataset.header for dataset in stokescrop.hdul_crop]
|
||||
Stokes_hdul, headers = stokescrop.hdul_crop, [dataset.header for dataset in stokescrop.hdul_crop]
|
||||
|
||||
data_mask = Stokes_test["data_mask"].data.astype(bool)
|
||||
data_mask = Stokes_hdul["data_mask"].data.astype(bool)
|
||||
print(
|
||||
"F_int({0:.0f} Angs) = ({1} ± {2})e{3} ergs.cm^-2.s^-1.Angs^-1".format(
|
||||
headers[0]["photplam"],
|
||||
*sci_not(
|
||||
Stokes_test[0].data[data_mask].sum() * headers[0]["photflam"],
|
||||
np.sqrt(Stokes_test[3].data[0, 0][data_mask].sum()) * headers[0]["photflam"],
|
||||
Stokes_hdul[0].data[data_mask].sum() * headers[0]["photflam"],
|
||||
np.sqrt(Stokes_hdul[3].data[0, 0][data_mask].sum()) * headers[0]["photflam"],
|
||||
2,
|
||||
out=int,
|
||||
),
|
||||
)
|
||||
)
|
||||
print("P_int = {0:.1f} ± {1:.1f} %".format(headers[0]["p_int"] * 100.0, np.ceil(headers[0]["p_int_err"] * 1000.0) / 10.0))
|
||||
print("PA_int = {0:.1f} ± {1:.1f} °".format(princ_angle(headers[0]["pa_int"]), princ_angle(np.ceil(headers[0]["pa_int_err"] * 10.0) / 10.0)))
|
||||
print("P_int = {0:.1f} ± {1:.1f} %".format(headers[0]["p_int"] * 100.0, np.ceil(headers[0]["sP_int"] * 1000.0) / 10.0))
|
||||
print("PA_int = {0:.1f} ± {1:.1f} °".format(princ_angle(headers[0]["pa_int"]), princ_angle(np.ceil(headers[0]["sPA_int"] * 10.0) / 10.0)))
|
||||
# Background values
|
||||
print(
|
||||
"F_bkg({0:.0f} Angs) = ({1} ± {2})e{3} ergs.cm^-2.s^-1.Angs^-1".format(
|
||||
@@ -266,122 +306,124 @@ def main(target=None, proposal_id=None, infiles=None, output_dir="./data", crop=
|
||||
print("P_bkg = {0:.1f} ± {1:.1f} %".format(debiased_P_bkg[0, 0] * 100.0, np.ceil(s_P_bkg[0, 0] * 1000.0) / 10.0))
|
||||
print("PA_bkg = {0:.1f} ± {1:.1f} °".format(princ_angle(PA_bkg[0, 0]), princ_angle(np.ceil(s_PA_bkg[0, 0] * 10.0) / 10.0)))
|
||||
# Plot polarization map (Background is either total Flux, Polarization degree or Polarization degree error).
|
||||
if px_scale.lower() not in ["full", "integrate"] and not interactive:
|
||||
if pxscale.lower() not in ["full", "integrate"] and not interactive:
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname]),
|
||||
plots_folder=plots_folder,
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "I"]),
|
||||
plots_folder=plots_folder,
|
||||
display="Intensity",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "P_flux"]),
|
||||
plots_folder=plots_folder,
|
||||
display="Pol_Flux",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "P"]),
|
||||
plots_folder=plots_folder,
|
||||
display="Pol_deg",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "PA"]),
|
||||
plots_folder=plots_folder,
|
||||
display="Pol_ang",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "I_err"]),
|
||||
plots_folder=plots_folder,
|
||||
display="I_err",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "P_err"]),
|
||||
plots_folder=plots_folder,
|
||||
display="Pol_deg_err",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "SNRi"]),
|
||||
plots_folder=plots_folder,
|
||||
display="SNRi",
|
||||
)
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test),
|
||||
deepcopy(Stokes_hdul),
|
||||
data_mask,
|
||||
SNRp_cut=SNRp_cut,
|
||||
SNRi_cut=SNRi_cut,
|
||||
flux_lim=flux_lim,
|
||||
step_vec=step_vec,
|
||||
vec_scale=vec_scale,
|
||||
scale_vec=scale_vec,
|
||||
savename="_".join([figname, "SNRp"]),
|
||||
plots_folder=plots_folder,
|
||||
display="SNRp",
|
||||
)
|
||||
elif not interactive:
|
||||
proj_plots.polarization_map(
|
||||
deepcopy(Stokes_test), data_mask, SNRp_cut=SNRp_cut, SNRi_cut=SNRi_cut, savename=figname, plots_folder=plots_folder, display="integrate"
|
||||
deepcopy(Stokes_hdul), data_mask, SNRp_cut=SNRp_cut, SNRi_cut=SNRi_cut, savename=figname, plots_folder=plots_folder, display="integrate"
|
||||
)
|
||||
elif px_scale.lower() not in ["full", "integrate"]:
|
||||
proj_plots.pol_map(Stokes_test, SNRp_cut=SNRp_cut, SNRi_cut=SNRi_cut, flux_lim=flux_lim)
|
||||
elif pxscale.lower() not in ["full", "integrate"]:
|
||||
proj_plots.pol_map(Stokes_hdul, SNRp_cut=SNRp_cut, SNRi_cut=SNRi_cut, flux_lim=flux_lim)
|
||||
|
||||
return 0
|
||||
outfiles.append(Stokes_hdul[0].header["FILENAME"])
|
||||
|
||||
return outfiles
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
@@ -400,4 +442,4 @@ if __name__ == "__main__":
|
||||
exitcode = main(
|
||||
target=args.target, proposal_id=args.proposal_id, infiles=args.files, output_dir=args.output_dir, crop=args.crop, interactive=args.interactive
|
||||
)
|
||||
print("Finished with ExitCode: ", exitcode)
|
||||
print("Written to: ", exitcode)
|
||||
|
||||
Reference in New Issue
Block a user