/
fabadaCMD.py
executable file
·126 lines (91 loc) · 2.96 KB
/
fabadaCMD.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/Users/pmsa/opt/anaconda3/bin/python
# -*- coding: utf-8 -*-
"""
FABADA script for denoising fits files from the command line
Sanchez-Alarcon, P.M. and Ascasibar, Y. 2023.
"Fully Adaptive Bayesian Algorithm for Data Analysis. FABADA"
"""
from fabada import fabada
import argparse
from astropy.io import fits
import os
import numpy as np
def make_parser():
"""Create an argument parser"""
parser = argparse.ArgumentParser(description="Runs fabada to denoise image given.")
parser.add_argument(
"filename", type=str, help="Location of input .fits file (string)"
)
parser.add_argument(
"noise", help="Standard deviation estimation of the image (float or string)"
)
parser.add_argument(
"-out",
type=str,
help="Location to save filtered image. Supports .fits filenames (string)",
default=None,
)
parser.add_argument(
"-hdu", type=int, help="HDU of image, default 0 (integer)", default=0
)
parser.add_argument(
"-noise_hdu",
type=int,
help="HDU of noise image, default 0 (integer)",
default=0,
)
parser.add_argument(
"-res", type=bool, help="Save Residuals, default False (boolean)", default=False
)
parser.add_argument(
"-iter",
type=int,
help="Maximum number of iteration of fabada, default 3000 (integer)",
default=3000,
)
parser.add_argument(
"-verbose",
type=bool,
help="Verbose paramater for fabada, default False (boolean)",
default=True,
)
return parser
# Parse arguments
p = make_parser().parse_args()
extension = p.filename.split(".")[-1]
name = os.path.basename(os.path.splitext(p.filename)[0])
path = os.path.dirname(p.filename)
if p.verbose:
print("Starting smoothing with fabada in %s image..." % (name + "." + extension))
# Read fits file
image = fits.open(p.filename)[p.hdu].data
header = fits.open(p.filename)[p.hdu].header
# Read noise
try:
noise = float(p.noise)
except:
noise = str(p.noise)
noise = fits.open(p.noise)[p.noise_hdu].data
nan_mask = np.isnan(image)
if nan_mask.any():
image[nan_mask]=0
noise[nan_mask]=1e-10
# Run fabada
fabada_estimation = fabada(image, noise**2, max_iter=p.iter, verbose=p.verbose)
if nan_mask.any(): fabada_estimation[nan_mask] = np.nan
# Save results
if not p.out:
p.out = name + "_fabada." + extension
if p.verbose:
print("Saving result in " + p.out)
header.append(("COMMENTS", "FABADA smooth estimation", ""), end=True)
hdu_new = fits.PrimaryHDU(fabada_estimation, header=header)
hdu_new.writeto(p.out, overwrite=True)
# Save residuals if want it
if p.res:
residuals = name + "_residuals." + extension
if p.verbose:
print("Saving residuals in " + residuals)
header.append(("COMMENTS", "FABADA smooth residuals", ""), end=True)
hdu_new = fits.PrimaryHDU(image - fabada_estimation, header=header)
hdu_new.writeto(residuals, overwrite=True)