ctools 2.1.0.dev
Loading...
Searching...
No Matches
csiactdata.py
Go to the documentation of this file.
1#!/usr/bin/env python
2# ==========================================================================
3# Inspect an IACT data storage
4#
5# Copyright (C) 2015-2022 Michael Mayer
6#
7# This program is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program. If not, see <http://www.gnu.org/licenses/>.
19#
20# ==========================================================================
21import sys
22import os
23import json
24import gammalib
25import ctools
26
27
28# ================ #
29# csiactdata class #
30# ================ #
31class csiactdata(ctools.cscript):
32 """
33 Inspect an IACT data storage
34
35 This script inspects the available FITS data storage on the user machine
36 and writes information about the available FITS configurations into a log
37 file or prints it on the screen. These information can be used as input
38 for 'csiactobs'.
39 """
40
41 # Constructor
42 def __init__(self, *argv):
43 """
44 Constructor.
45 """
46 # Initialise application by calling the base class constructor
47 self._init_cscript(self.__class__.__name__, ctools.__version__, argv)
48
49 # Set name
50 self._datapath = os.getenv('VHEFITS','')
51 self._prodnames = []
52 self._master_indx = ''
53 self._master_file = ''
54
55 # Return
56 return
57
58
59 # Private methods
60 def _get_parameters(self):
61 """
62 Get parameters from parfile and setup the observation
63 """
64 # Query datapath. If the parameter is not NONE then use it, otherwise
65 # use the datapath from the VHEFITS environment variable
66 datapath = self['datapath'].string()
67 if gammalib.toupper(datapath) != 'NONE':
68 self._datapath = datapath
69 else:
70 self._datapath = os.getenv('VHEFITS','')
71
72 # Expand environment
73 self._datapath = gammalib.expand_env(self._datapath)
74
75 # Get filename of master index file
76 self._master_indx = self['master_indx'].string()
77 self._master_file = os.path.join(self._datapath, self._master_indx)
78
79 # Check for presence of master index file
80 if not os.path.isfile(self._master_file):
81 raise RuntimeError('Master index file "'+self._master_file+
82 '" not found. Use hidden parameter '+
83 '"master_indx" to specifiy a different '+
84 'filename.')
85
86 # Write input parameters into logger
87 self._log_parameters(gammalib.TERSE)
88
89 # Return
90 return
91
92 # Log configuration
93 def _log_configuration(self, config):
94 """
95 Log configuration
96
97 Parameters
98 ----------
99 config : dict
100 Dictionary containing data configuration
101 """
102 # Log configuration information
103 self._log_header3(gammalib.TERSE, str(config['name']))
104 self._log_value(gammalib.TERSE, 'Name', config['name'])
105 self._log_value(gammalib.TERSE, 'Observation index', config['obsindx'])
106 self._log_value(gammalib.TERSE, 'HDU index', config['hduindx'])
107
108 # Print additional information
109 for key in config:
110
111 # Skip keys that were treated above
112 if key in ['name','hduindx','obsindx']:
113 continue
114 self._log_value(gammalib.TERSE, key, config[key])
115
116 # Return
117 return
118
119
120 # Public methods
121 def process(self):
122 """
123 Process the script
124 """
125 # Get parameters
126 self._get_parameters()
127
128 # Write header into logger
129 if self._logTerse():
130 self._log('\n')
131 self._log.header1('Data storage entries')
132 self._log.parformat('Master index file')
133 self._log(self._master_file)
134 self._log('\n')
135
136 # Open and load JSON file
137 json_data = open(self._master_file).read()
138 data = json.loads(json_data)
139 configs = data['datasets']
140
141 # Initialise array for available names
142 self._prodnames = []
143
144 # Write header into logger
145 self._log('\n')
146 self._log.header2('Available data configs')
147
148 # Loop over configs and log available configs
149 for config in configs:
150
151 # Create hdu and obs index files
152 hdu = os.path.join(self._datapath, config['hduindx'])
153 obs = os.path.join(self._datapath, config['obsindx'])
154 filename_hdu = gammalib.GFilename(str(hdu)+'[HDU_INDEX]')
155 filename_obs = gammalib.GFilename(str(obs)+'[OBS_INDEX]')
156
157 # If index files are available then log configuration
158 # information ...
159 if (filename_hdu.is_fits() and filename_obs.is_fits()):
160
161 # Append to available names
162 self._prodnames.append(str(config['name']))
163
164 # Log information
165 self._log_configuration(config)
166
167 # ... otherwise log that the configuration is not available
168 else:
169 if self._logTerse():
170 self._log.header3(str(config['name']))
171 self._log(' Not available\n')
172
173 # Return
174 return
175
176 def names(self):
177 """
178 Return available FITS production names
179
180 Returns
181 -------
182 names : list of str
183 List of available FITS production names.
184 """
185 # Return
186 return self._prodnames
187
188
189# ======================== #
190# Main routine entry point #
191# ======================== #
192if __name__ == '__main__':
193
194 # Create instance of application
195 app = csiactdata(sys.argv)
196
197 # Execute application
198 app.execute()