csmodelsois.py
8.62 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#! /usr/bin/env python
# ==========================================================================
# Allows generation of DiffuseMapCube model from subset of sources in a
# model definition XML file
#
# Copyright (C) 2017 Josh Cardenzana
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# ==========================================================================
import sys
import gammalib
import ctools
#=============#
# csmodelsois #
#=============#
class csmodelsois(ctools.cscript):
"""
Generates a DiffuseMapCube model from a subset of sources in a model
definition XML file. All background models will be ignored!
"""
# Constructor
def __init__(self, *argv):
"""
Constructor.
Parameters
----------
argv : list of parameters
Raises
------
TypeError
An invalid number of command line arguments was provided.
"""
# Set name and version
self._name = 'csmodelsois'
self._version = ctools.__version__
# Initialize parameters
self._init_members()
# Initialise application by calling the appropriate class constructor
self._init_cscript(argv)
# Return
return
def _init_members(self):
"""
Initialize the parameters for this object
"""
self.cubegen = ctools.ctmapcube() # Implements ctmapcube for generate the cube
self.m_models = gammalib.GModels() # Stores the full list of input models
self.cubemodels = gammalib.GModels() # Stores the list of models to be written to file
return
def _set_cube_parameters(self):
"""
Fill in the parameters associated with this object
"""
# Get the cube centre coordinates
self.cubegen['coordsys'].string( self['coordsys'].string() )
if self['coordsys'].string() == 'CEL':
# Center in celestial coordinates
self.cubegen['xref'].real( self['ra'].real() )
self.cubegen['yref'].real( self['dec'].real() )
else:
# Otherwise assume center in galactic coordinates
self.cubegen['xref'].real( self['glon'].real() )
self.cubegen['yref'].real( self['glat'].real() )
# Get spatial binning parameters
self.cubegen['binsz'].real( self['binsz'].real() )
self.cubegen['nxpix'].integer( self['nxpix'].integer() )
self.cubegen['nypix'].integer( self['nypix'].integer() )
self.cubegen['proj'].string( self['proj'].string() )
# Get the energy binning parameters
self.cubegen['ebinalg'].string( self['ebinalg'].string() )
if self['ebinalg'].string() == "FILE":
# Read energy binning from a file
self.cubegen['ebinfile'].filename( self['ebinfile'].filename() )
else:
# Create the energy binning information
self.cubegen['emin'].real( self['emin'].real() )
self.cubegen['emax'].real( self['emax'].real() )
self.cubegen['enumbins'].integer( self['enumbins'].integer() )
# Get remaining hidden parameters
self.cubegen['ptsrcsig'].real( self['ptsrcsig'].real() )
# Read optionally output cube filenames
if self._read_ahead():
self.cubegen['outcube'].filename( self['outcube'].filename() )
self['soilist'].string()
self['outmodel'].filename()
# Write parameters into logger
self._log_parameters(gammalib.TERSE);
return
def _gen_cubemodel(self):
"""
Generates a binned model from the model file using ctmapcube
returns: updated model filename
"""
# Get a list of sources we DONT want to put into the cube
sources = self['soilist'].string().split(',')
# Load the model into a model container
if (self.m_models.size() == 0):
self.m_models = gammalib.GModels(self['inmodel'].filename())
self.cubemodels = self.m_models.copy()
# Loop through models and pull out all models not in the list
models2remove = []
for model in self.m_models:
# Convert the model to a GModelSky
mod = model.classname()
# Check the model
if mod != "GModelSky":
# Model is most likely a background model, so continue
self.cubemodels.remove(model.name())
elif model.name() in sources:
# Model is a source of interest, so remove it from list of models
# to be used in generating the cube
self.cubemodels.remove(model.name())
# Log the number of models to be put into the cube
self._log_value(self['chatter'].integer(), 'Cube models', self.cubemodels.size())
return
def mapcube(self):
"""
Return the mapcube generated by the underlying 'ctmapcube' object
"""
return self.cubegen.mapcube()
def cubemodelname(self):
"""
Return the name to be given to the cube model in the output XML file
"""
return "FixedSourcesCube"
def models(self, models:gammalib.GModels):
"""
Set the models for analysis
"""
self.m_models = models
return
def run(self):
"""
Implements the actual bulk of the script's tasks
"""
# Switch screen logging on in debug mode
if self._logDebug():
self._log.cout(True)
# Get parameters
self._set_cube_parameters()
# Set the models to put into the cube
self._gen_cubemodel()
# Only run if there still models to be filled into the cube
if not self.cubemodels.is_empty():
self._log_string(gammalib.NORMAL, 'Generting model cube')
# Fill the cube models into the ctmapcube object
self.cubegen.models( self.cubemodels )
# Run the map generating method
self.cubegen.run()
else:
self._log_string(gammalib.NORMAL, 'List of models is empty, nothing will be done')
return
def save(self):
"""
Save the cube and optionally the updated XML model file
"""
#cube = gammalib.GModelSpatialDiffuseCube(self.cubegen.mapcube())
# Save the generated cube if the cube and filename are not empty
if ((not self.cubegen.mapcube().cube().is_empty()) and
self._is_valid_filename(self['outcube'].filename())):
self.cubegen.mapcube().save( self['outcube'].filename(), self['clobber'].boolean() )
# If requested, save the updated list of models
if self._is_valid_filename( self['outmodel'].filename() ):
# Generate a list of models that will be output to file
outmodels = gammalib.GModels(self.m_models)
# Remove all models used in the generated cube
for model in self.cubemodels:
outmodels.remove(model.name())
# Generate the actual cube model
spat = gammalib.GModelSpatialDiffuseCube( self['outcube'].filename() )
spec = gammalib.GModelSpectralConst()
newmodel = gammalib.GModelSky(spat, spec)
# Give the model a default name
newmodel.name( self.cubemodelname() )
# Now append the model to the list of output models
outmodels.append(newmodel)
# Save the model list
outmodels.save( self['outmodel'].filename() )
return
def execute(self):
"""
Execute the script
"""
# Open logfile
self.logFileOpen()
# Read ahead output parameters
self._read_ahead(True)
# Run the script
self.run()
self.save()
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Create instance of application
app = csmodelsois(sys.argv)
# Execute application
app.execute()