Completed
Push — main ( c9b807...564baa )
by Chaitanya
30s queued 14s
created

asgardpy.io.input_dl3.DL3Files.__init__()   A

Complexity

Conditions 2

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 17
nop 3
dl 0
loc 23
rs 9.55
c 0
b 0
f 0
1
"""
2
Basic classes defining Input Config for DL3 files and some functions to
3
retrieve the DL3 files information.
4
5
Currently supporting files following enrico/fermipy for Fermi-LAT data for 3D
6
Dataset and DL3 files that follow GADF v0.3 and can be directly read by Gammapy,
7
for 1D Dataset.
8
"""
9
10
import logging
11
12
from asgardpy.base import BaseConfig, PathType
13
14
__all__ = ["InputFilePatterns", "InputDL3Config", "DL3Files"]
15
16
EXPECTED_DL3_RANGE = ["gadf-dl3", "lat", "lat-aux"]
17
18
19
# Basic Components for the Input Config
20
class InputFilePatterns(BaseConfig):
21
    """
22
    Config section for list of file patterns to use for fetching relevant DL3
23
    files.
24
    """
25
26
    events: str = "*events.fits*"
27
    edisp: str = "*DRM.fits*"
28
    exposure: str = "*BinnedMap.fits*"
29
    xml_model: str = "*out.xml"
30
    psf: str = "*psf.fits*"
31
32
    dl3: str = "dl3*fits"
33
34
    gal_diffuse: str = "gll_iem_v*.fits*"
35
    iso_diffuse: str = "iso_P8R3_SOURCE_V*_*.txt"
36
37
38
class InputDL3Config(BaseConfig):
39
    """
40
    Config section for main information on getting the relevant DL3 files.
41
    """
42
43
    type: str = "type"
44
    input_dir: PathType = PathType("None")
45
    glob_pattern: dict = {}
46
47
48
# Main Classes for I/O
49
class DL3Files:
50
    """
51
    A general class to retrieve information from given DL3 files, along with
52
    other auxiliary files for neighbouring sources, if provided.
53
    """
54
55
    def __init__(self, dir_dict, log=None):
56
        if not log:
57
            self._set_logging()
58
        else:
59
            self.log = log
60
61
        self.dl3_path = dir_dict.input_dir
62
63
        self.dl3_type = dir_dict.type
64
        self._check_dl3_type()
65
66
        self.glob_dict = dir_dict.glob_pattern
67
68
        self.events_files = None
69
        self.edrm_files = None
70
        self.xml_files = None
71
        self.expmap_files = None
72
        self.psf_files = None
73
        self.gal_diff_files = None
74
        self.iso_diff_files = None
75
76
        self.xml_f = None
77
        self.gal_diff_f = None
78
79
    def _set_logging(self):
80
        self.log = logging.getLogger(__name__)
81
        self.log.setLevel(logging.INFO)
82
83
    def _check_dl3_type(self):
84
        if self.dl3_type.lower() not in EXPECTED_DL3_RANGE:
85
            self.log.error("%s is not in the expected range for DL3 files", self.dl3_type)
86
87
    def prepare_lat_files(self, key, file_list):
88
        """
89
        Prepare a list of LAT files following a particular key. If there are no
90
        distinct key types of files, the value is None.
91
        """
92
        # Try to combine LAT and LAT-AUX files
93
        self.list_dl3_files()
94
        file_list = self.select_unique_files(key, file_list)
95
96
        return file_list
97
98
    def list_dl3_files(self):
99
        """
100
        From a given DL3 files path, categorize the different types of DL3
101
        files, to be used for further analysis.
102
103
        The dl3_type of 'gadf-dl3' is used for all GADF v0.3 following DL3
104
        files that can be directly read by Gammapy, for 1D Datasets.
105
        """
106
        if self.dl3_type.lower() in ["lat"]:
107
            self.events_files = sorted(list(self.dl3_path.glob(self.glob_dict["events"])))
108
            self.edrm_files = sorted(list(self.dl3_path.glob(self.glob_dict["edisp"])))
109
            self.xml_files = sorted(list(self.dl3_path.glob(self.glob_dict["xml_model"])))
110
            self.expmap_files = sorted(list(self.dl3_path.glob(self.glob_dict["exposure"])))
111
            self.psf_files = sorted(list(self.dl3_path.glob(self.glob_dict["psf"])))
112
113
        if self.dl3_type.lower() in ["lat-aux"]:
114
            self.gal_diff_files = sorted(list(self.dl3_path.glob(self.glob_dict["gal_diffuse"])))
115
            self.iso_diff_files = sorted(list(self.dl3_path.glob(self.glob_dict["iso_diffuse"])))
116
117
        if self.dl3_type.lower() in ["gadf-dl3"]:
118
            self.events_files = sorted(list(self.dl3_path.glob(self.glob_dict["dl3"])))
119
120
    def select_unique_files(self, key, file_list):
121
        """
122
        Select Unique files from all of the provided LAT files, as per the
123
        given key. If there are no distinct key types of files, the value is None.
124
        """
125
        # Have to make more checks or add conditions on selecting only select
126
        # files instead from the glob-searched lists.
127
        if self.dl3_type.lower() in ["lat"]:
128
            var_list = [
129
                "events_files",
130
                "edrm_files",
131
                "expmap_files",
132
                "psf_files",
133
            ]
134
            file_list["xml_file"] = self.xml_files[0]
135
136
        if self.dl3_type.lower() == "lat-aux":
137
            var_list = []
138
            if key:
139
                if "0" not in key:  # For fermipy files, the diffuse files are already unique
140
                    var_list = [
141
                        "iso_diff_files",
142
                    ]
143
            if isinstance(self.iso_diff_files, list):
144
                self.iso_gal_f = self.iso_diff_files[0]
145
            else:
146
                self.iso_gal_f = self.iso_diff_files
147
            file_list["iso_diff_file"] = self.iso_gal_f
148
149
            if isinstance(self.gal_diff_files, list):
150
                self.diff_gal_f = self.gal_diff_files[0]
151
            else:
152
                self.diff_gal_f = self.gal_diff_files
153
            file_list["gal_diff_file"] = self.diff_gal_f
154
155
        if len(var_list) > 0:
0 ignored issues
show
introduced by
The variable var_list does not seem to be defined in case self.dl3_type.lower() in ListNode on line 127 is False. Are you sure this can never be the case?
Loading history...
156
            for _v in var_list:
157
                if key is not None:
158
                    filtered = [K for K in getattr(self, _v) if key in str(K.name)]
159
                    if len(filtered) == 1:
160
                        self.log.info("Selecting the file with name containing %s", key)
161
                        setattr(self, _v.replace("_files", "_f"), filtered[0])
162
                    else:
163
                        raise ValueError(
164
                            "Variable {%s} does not contain one element after filtering by {%s}",
165
                            getattr(self, _v),
166
                            key,
167
                        )
168
                else:
169
                    self.log.info("No distinct key provided, selecting the first file in the list")
170
                    setattr(self, _v.replace("_files", "_f"), getattr(self, _v)[0])
171
172
                file_list[_v.replace("files", "file")] = getattr(self, _v.replace("_files", "_f"))
173
174
        return file_list
175