plotting.py 2.14 KB
Newer Older
1
2
3
4
5
6
7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
Created by schaffer at 9/9/19

"""
8
import argparse
9
import warnings
10
from os.path import basename
11
12
13
from warnings import catch_warnings

import matplotlib as mpl
14
15
from tqdm import tqdm

Carl Schaffer's avatar
Carl Schaffer committed
16
mpl.use("Agg")
17
import matplotlib.pyplot as plt  # noqa: E402
18
from ..generic.fits import FitsFile  # noqa: E402
19
from sunpy.map import Map  # noqa: E402
20
from ..util.locplot import make_loc_plot  # noqa: E402
21
22
23
24
import numpy as np  # noqa: E402


def loc_preview(fits_file, fn_out):
Carl Schaffer's avatar
Carl Schaffer committed
25
    """Generate a preview showing the center of the observation on a up to date image of the soalr disk."""
26
    m = Map(fits_file)
Carl Schaffer's avatar
Carl Schaffer committed
27
28
29
    x, y = (m.top_right_coord.Tx + m.bottom_left_coord.Tx) / 2, (
        m.top_right_coord.Ty + m.bottom_left_coord.Ty
    ) / 2
30
31
32
    time = FitsFile(fits_file).obs_time
    with catch_warnings():
        warnings.simplefilter("ignore")
33
        fig, ax = make_loc_plot((x.value, y.value), time)
34
        # plt.tight_layout()
Carl Schaffer's avatar
Carl Schaffer committed
35
        fig.savefig(fn_out, bbox_inches="tight", pad_inches=-0.01, dpi=200)
36
37
38
39
        return fn_out


def map_preview(fits_file, fn_out):
Carl Schaffer's avatar
Carl Schaffer committed
40
    """Generate a preview of the reconstructed data."""
41
    m = Map(fits_file)
Carl Schaffer's avatar
Carl Schaffer committed
42
43
    m.plot_settings["vmin"] = np.percentile(m.data, 0.1)
    m.plot_settings["vmax"] = np.percentile(m.data, 99.9)
44
45
46
47
48
49

    _ = m.plot()
    plt.gcf().savefig(fn_out, bbox_inches="tight", pad_inches=0.1, dpi=200)
    return fn_out


50
def run_bbi_preview(cmd_line_args):
Carl Schaffer's avatar
Carl Schaffer committed
51
    mpl.use("Agg")
Carl Schaffer's avatar
Carl Schaffer committed
52
    from ..interface.commandline_interface import add_arg_infiles
Carl Schaffer's avatar
Carl Schaffer committed
53

54
    parser = argparse.ArgumentParser()
Carl Schaffer's avatar
Carl Schaffer committed
55
    add_arg_infiles(parser)
56
    args = parser.parse_args(cmd_line_args)
Carl Schaffer's avatar
Carl Schaffer committed
57
    in_files = args.files
58

Carl Schaffer's avatar
Carl Schaffer committed
59
    progress = tqdm(in_files)
60
61
62
63
64
65
66
67
    for infile in progress:
        progress.set_description(basename(infile))
        fn_out_reco = infile.replace("reco_bb.fits", "reco_preview.png")
        fn_out_loc = fn_out_reco.replace("reco_preview.png", "location_preview.png")
        fn_out_alpha = fn_out_reco.replace("reco_preview.png", "alpha_preview.png")

        map_preview(infile, fn_out_reco)
        loc_preview(infile, fn_out_loc)
Carl Schaffer's avatar
Carl Schaffer committed
68
        plt.close("all")
69
70

    # folder = dirname(infiles[0])
Carl Schaffer's avatar
Carl Schaffer committed
71
    # bbi_gen_gif(folder)