arcwind 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
arcwind/cli.py ADDED
@@ -0,0 +1,144 @@
1
+ """
2
+ Command-line interface: ``arcwind <command> ...``.
3
+
4
+ Commands
5
+ --------
6
+ ``init`` write a template configuration (``arcwind init mysource.toml``)
7
+ ``options`` list every option with its default (``arcwind options [section]``)
8
+ ``convert`` convert an old ``*_config.py`` to TOML
9
+ ``center`` fit the disk center to a continuum image
10
+ ``check`` noise and disk-rotation checks
11
+ ``fit`` one global fit (``--law``, ``--radial``, ``--guide``, ``--nboot`` ...)
12
+ ``run`` the full standard analysis
13
+ ``mock`` write a synthetic cube and a matching configuration
14
+ (``--preset elias20`` or ``--preset hh30``)
15
+ """
16
+ from __future__ import annotations
17
+
18
+ import argparse
19
+ import sys
20
+
21
+ __all__ = ["main"]
22
+
23
+
24
+ def _cmd_init(a):
25
+ import os
26
+ with open(os.path.join(os.path.dirname(__file__), "data", "template.toml")) as fh:
27
+ txt = fh.read()
28
+ with open(a.path, "w") as fh:
29
+ fh.write(txt)
30
+ print(f"wrote {a.path}: fill in the [source] block, then `arcwind check {a.path}`")
31
+
32
+
33
+ def _cmd_options(a):
34
+ from .config import WindConfig
35
+ print(WindConfig.describe(a.section))
36
+
37
+
38
+ def _cmd_convert(a):
39
+ from .config import WindConfig
40
+ cfg = WindConfig.from_legacy(a.legacy)
41
+ out = a.out or a.legacy.rsplit(".", 1)[0] + ".toml"
42
+ cfg.save(out)
43
+ print(f"wrote {out}")
44
+
45
+
46
+ def _cmd_center(a):
47
+ from .continuum import fit_disk_center
48
+ c = fit_disk_center(a.continuum, box_arcsec=a.box)
49
+ print(c.summary())
50
+ print("\n# for the [source] block of the configuration:")
51
+ print(c.toml())
52
+
53
+
54
+ def _cmd_check(a):
55
+ from .analysis import WindAnalysis
56
+ wa = WindAnalysis(a.config)
57
+ print(wa.check_noise().summary())
58
+ print(wa.check_disk_rotation().summary())
59
+
60
+
61
+ def _cmd_fit(a):
62
+ from .analysis import WindAnalysis
63
+ wa = WindAnalysis(a.config)
64
+ guide = wa.load_result(a.guide) if a.guide else None
65
+ start = wa.load_result(a.start_from) if a.start_from else None
66
+ fixed = dict(kv.split("=") for kv in (a.fix or []))
67
+ res = wa.fit(a.law, radial=a.radial, rotation=a.rotation, guide=guide, start_from=start,
68
+ iterations=a.iterations, n_boot=a.nboot, label=a.label,
69
+ fixed={k: float(v) for k, v in fixed.items()})
70
+ print(res.summary())
71
+ if not a.no_plots:
72
+ wa.plot_cuts(res)
73
+ wa.plot_profiles(res)
74
+
75
+
76
+ def _cmd_run(a):
77
+ from .analysis import WindAnalysis
78
+ WindAnalysis(a.config).run(n_boot=a.nboot, figures=not a.no_plots)
79
+
80
+
81
+ def _cmd_mock(a):
82
+ from .mock import MockWind, make_mock_cube
83
+ m = MockWind.hh30_like() if a.preset == "hh30" else MockWind.elias20_like()
84
+ make_mock_cube(a.fits, m, n_wind=a.n_wind, n_disk=a.n_disk)
85
+ cfg = m.config(a.fits)
86
+ cfg.output.dir = "mock_output"
87
+ cfg.save(a.config)
88
+ print(f"wrote {a.fits} and {a.config}; truth: {m.truth()}")
89
+
90
+
91
+ def main(argv=None):
92
+ ap = argparse.ArgumentParser(prog="arcwind", description=__doc__,
93
+ formatter_class=argparse.RawDescriptionHelpFormatter)
94
+ sub = ap.add_subparsers(dest="cmd", required=True)
95
+ p = sub.add_parser("init", help="write a template configuration")
96
+ p.add_argument("path", nargs="?", default="arcwind.toml")
97
+ p.set_defaults(func=_cmd_init)
98
+ p = sub.add_parser("options", help="list all options")
99
+ p.add_argument("section", nargs="?", default=None)
100
+ p.set_defaults(func=_cmd_options)
101
+ p = sub.add_parser("convert", help="convert an old *_config.py")
102
+ p.add_argument("legacy")
103
+ p.add_argument("-o", "--out", default=None)
104
+ p.set_defaults(func=_cmd_convert)
105
+ p = sub.add_parser("center", help="fit the disk center to a continuum image")
106
+ p.add_argument("continuum")
107
+ p.add_argument("--box", type=float, default=None, help="half-size of the fitted region (arcsec)")
108
+ p.set_defaults(func=_cmd_center)
109
+ p = sub.add_parser("check", help="noise and disk-rotation checks")
110
+ p.add_argument("config")
111
+ p.set_defaults(func=_cmd_check)
112
+ p = sub.add_parser("fit", help="one global fit")
113
+ p.add_argument("config")
114
+ p.add_argument("--law", default=None, help="velocity law (default: [fit] velocity_law)")
115
+ p.add_argument("--radial", default=None, choices=("cone", "proportional", "independent"))
116
+ p.add_argument("--rotation", default=None, choices=("constant", "power-law"))
117
+ p.add_argument("--guide", default=None, help="label or path of a result to guide the traces")
118
+ p.add_argument("--start-from", default=None, help="label or path of a result to start from")
119
+ p.add_argument("--iterations", type=int, default=None)
120
+ p.add_argument("--nboot", type=int, default=None)
121
+ p.add_argument("--fix", nargs="*", help="name=value pairs to hold fixed")
122
+ p.add_argument("--label", default=None)
123
+ p.add_argument("--no-plots", action="store_true")
124
+ p.set_defaults(func=_cmd_fit)
125
+ p = sub.add_parser("run", help="the full standard analysis")
126
+ p.add_argument("config")
127
+ p.add_argument("--nboot", type=int, default=None)
128
+ p.add_argument("--no-plots", action="store_true")
129
+ p.set_defaults(func=_cmd_run)
130
+ p = sub.add_parser("mock", help="write a synthetic cube + configuration")
131
+ p.add_argument("--preset", default="elias20", choices=("elias20", "hh30"),
132
+ help="elias20: inclined, one lobe; hh30: nearly edge-on, both lobes")
133
+ p.add_argument("--fits", default="mock_wind_cube.fits")
134
+ p.add_argument("--config", default="mock.toml")
135
+ p.add_argument("--n-wind", type=int, default=1_500_000)
136
+ p.add_argument("--n-disk", type=int, default=2_000_000)
137
+ p.set_defaults(func=_cmd_mock)
138
+ a = ap.parse_args(argv)
139
+ a.func(a)
140
+
141
+
142
+
143
+ if __name__ == "__main__":
144
+ sys.exit(main())