csmodelsois.py
8 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
#! /usr/bin/env python
# ==========================================================================
# Puts subset of sources in diffuse model cube
#
# Copyright (C) 2017-2018 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 #
# ================= #
class csmodelsois(ctools.cscript):
"""
Puts subset of sources in diffuse model cube
The csmodelsois class puts a subset of sources in a model definition
XML file into a model cube and generates a new model definition XML file
in which the subset of sources is replaced by a model of type
DiffuseMapCube.
"""
# Constructor
def __init__(self, *argv):
"""
Constructor
Parameters
----------
argv : list of parameters
Raises
------
TypeError
An invalid number of command line arguments was provided.
"""
# Initialise application by calling the base class constructor
self._init_cscript(self.__class__.__name__, ctools.__version__, argv)
# Initialize parameters
self._cubegen = ctools.ctmapcube()
self._models = gammalib.GModels()
self._cubemodels = gammalib.GModels()
# Return
return
# Private methods
def _get_parameters(self):
"""
Get parameters from parfile
"""
# Query input parameters
self['inmodel'].filename()
self['soilist'].string()
# Read input parameters
ptsrcsig = self['ptsrcsig'].real()
# Get the cube centre coordinates
self._cubegen['coordsys'].string(self['coordsys'].string())
if self['coordsys'].string() == 'CEL':
self._cubegen['xref'].real(self['ra'].real())
self._cubegen['yref'].real(self['dec'].real())
else:
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':
self._cubegen['ebinfile'].filename(self['ebinfile'].filename())
else:
self._cubegen['emin'].real(self['emin'].real())
self._cubegen['emax'].real(self['emax'].real())
self._cubegen['enumbins'].integer(self['enumbins'].integer())
# Set point source significance
self._cubegen['ptsrcsig'].real(ptsrcsig)
# Read optionally output cube filenames
if self._read_ahead():
self._cubegen['outcube'].filename(self['outcube'].filename())
self['outmodel'].query()
# Write parameters into logger
self._log_parameters(gammalib.TERSE)
return
def _gen_cubemodel(self):
"""
Generates a binned model from the model file using ctmapcube
"""
# 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._models.size() == 0):
self._models = gammalib.GModels(self['inmodel'].filename())
# Store a copy of the models
self._cubemodels = self._models.copy()
# Loop through models and pull out all models not in the list
for model in self._models:
# If model is not a sky model then continue
if model.classname() != 'GModelSky':
self._cubemodels.remove(model.name())
# ... otherwise, if model is in list of sources the remove it
# from the container
elif model.name() in sources:
self._cubemodels.remove(model.name())
# Log the number of models to be put into the cube
self._log_value(self['chatter'].integer(), 'Numner of cube models',
self._cubemodels.size())
# Return
return
# Public methods
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._get_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():
# Log action
self._log_string(gammalib.NORMAL, 'Generating 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
return
def save(self):
"""
Save the cube and optionally the updated XML model file
"""
# Save the generated cube if the cube and filename are not empty
if ((not self._cubegen.mapcube().cube().is_empty()) and
self['outcube'].is_valid()):
self._cubegen.mapcube().save(self['outcube'].filename(),
self['clobber'].boolean())
# If requested, save the updated list of models
if self['outmodel'].is_valid():
# Generate a list of models that will be output to file
outmodels = gammalib.GModels(self._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
return
def mapcube(self):
"""
Return the mapcube generated by the underlying 'ctmapcube' object
"""
# Return
return self._cubegen.mapcube()
def cubemodelname(self):
"""
Return the name to be given to the cube model in the output XML file
"""
# Return
return 'FixedSourcesCube'
def models(self, models):
"""
Set the models
Parameters
-------
models : `~gammalib.GModels`
Model container
"""
# Set models
self._models = models
# Return
return
# ======================== #
# Main routine entry point #
# ======================== #
if __name__ == '__main__':
# Create instance of application
app = csmodelsois(sys.argv)
# Execute application
app.execute()