Commit 9902fbdc5971c51c93d5e2ba24bb266aaa6f44ca

Authored by Jürgen Knödlseder
1 parent 13edcb49

Avoid intermediate storing of counts and model cubes.

scripts/csresmap.py
@@ -44,8 +44,9 @@ class csresmap(gammalib.GApplication): @@ -44,8 +44,9 @@ class csresmap(gammalib.GApplication):
44 self.version = "0.1.0" 44 self.version = "0.1.0"
45 45
46 # Initialise some members 46 # Initialise some members
47 - self.obs = None  
48 - self.algorithm = "SUB" 47 + self.obs = None
  48 + self.algorithm = "SUB"
  49 + self.resmap = None
49 50
50 # Initialise some members 51 # Initialise some members
51 if isinstance(argv[0],gammalib.GObservations): 52 if isinstance(argv[0],gammalib.GObservations):
@@ -144,8 +145,8 @@ class csresmap(gammalib.GApplication): @@ -144,8 +145,8 @@ class csresmap(gammalib.GApplication):
144 observation = gammalib.GCTAObservation() 145 observation = gammalib.GCTAObservation()
145 observation.load(obsfile) 146 observation.load(obsfile)
146 147
147 - self.m_irf = self["irf"].string()  
148 - self.m_caldb = self["caldb"].string() 148 + self.m_irf = self["irf"].string()
  149 + self.m_caldb = self["caldb"].string()
149 150
150 caldb = gammalib.GCaldb() 151 caldb = gammalib.GCaldb()
151 if os.path.isdir(self.m_caldb): 152 if os.path.isdir(self.m_caldb):
@@ -204,6 +205,9 @@ class csresmap(gammalib.GApplication): @@ -204,6 +205,9 @@ class csresmap(gammalib.GApplication):
204 """ 205 """
205 # Run the script 206 # Run the script
206 self.run() 207 self.run()
  208 +
  209 + # Save residual map
  210 + self.resmap.save(self.m_outfile, self.m_clobber)
207 211
208 # Return 212 # Return
209 return 213 return
@@ -238,6 +242,7 @@ class csresmap(gammalib.GApplication): @@ -238,6 +242,7 @@ class csresmap(gammalib.GApplication):
238 242
239 # Create countsmap 243 # Create countsmap
240 bin = ctools.ctbin(self.obs) 244 bin = ctools.ctbin(self.obs)
  245 + bin["debug"].boolean(True)
241 bin["nxpix"].integer(self.m_nxpix) 246 bin["nxpix"].integer(self.m_nxpix)
242 bin["nypix"].integer(self.m_nypix) 247 bin["nypix"].integer(self.m_nypix)
243 bin["proj"].string(self.m_proj) 248 bin["proj"].string(self.m_proj)
@@ -250,69 +255,56 @@ class csresmap(gammalib.GApplication): @@ -250,69 +255,56 @@ class csresmap(gammalib.GApplication):
250 bin["emax"].real(self.m_emax) 255 bin["emax"].real(self.m_emax)
251 bin["binsz"].real(self.m_binsz) 256 bin["binsz"].real(self.m_binsz)
252 bin["clobber"].boolean(self.m_clobber) 257 bin["clobber"].boolean(self.m_clobber)
253 - if not self.m_binfile == "NONE":  
254 - bin["outfile"].filename(self.m_binfile)  
255 - else:  
256 - os,filename = tempfile.mkstemp()  
257 - bin["outfile"].filename(filename)  
258 - bin.execute() 258 + bin.run()
  259 +
  260 + # Store counts map as residual map. Note that we need a
  261 + # special construct here to avoid memory leaks. This seems
  262 + # to be a SWIG feature as SWIG creates a new object when
  263 + # calling bin.cube()
  264 + #residualmap = bin.cube().map()
  265 + counts_cube = bin.cube()
  266 + self.resmap = counts_cube.map().copy()
  267 + self.resmap.stack_maps()
259 268
260 # Write header 269 # Write header
261 if self.logTerse(): 270 if self.logTerse():
262 self.log("\n") 271 self.log("\n")
263 self.log.header1("Generate model map") 272 self.log.header1("Generate model map")
264 273
265 - # create model map 274 + # Create model map
266 model = ctools.ctmodel(self.obs) 275 model = ctools.ctmodel(self.obs)
267 - model["infile"].filename(bin["outfile"].filename()) 276 + model["debug"].boolean(True)
  277 + model.cube(bin.cube())
268 model["clobber"].boolean(self.m_clobber) 278 model["clobber"].boolean(self.m_clobber)
269 - if not self.m_modfile == "NONE":  
270 - model["outfile"].filename(self.m_modfile)  
271 - else:  
272 - os,filename = tempfile.mkstemp()  
273 - model["outfile"].filename(filename)  
274 - model.execute() 279 + model.run()
275 280
276 # Get model map into GSkymap object 281 # Get model map into GSkymap object
277 - modelmap = gammalib.GSkymap(model["outfile"].filename()) 282 + modelmap = model.cube().map()
278 modelmap.stack_maps() 283 modelmap.stack_maps()
279 -  
280 - # Initialise resiudalmap from countmap  
281 - residualmap = gammalib.GSkymap(bin["outfile"].filename())  
282 - residualmap.stack_maps()  
283 284
284 - # continue calculations depending on given algorithm 285 + # Continue calculations depending on given algorithm
285 if self.m_algorithm == "SUB": 286 if self.m_algorithm == "SUB":
286 287
287 # Subtract maps 288 # Subtract maps
288 - residualmap -= modelmap 289 + self.resmap -= modelmap
289 290
290 elif self.m_algorithm == "SUBDIV": 291 elif self.m_algorithm == "SUBDIV":
291 292
292 # Subtract and divide by model map 293 # Subtract and divide by model map
293 - residualmap -= modelmap  
294 - residualmap /= modelmap 294 + self.resmap -= modelmap
  295 + self.resmap /= modelmap
295 296
296 elif self.m_algorithm == "SUBDIVSQRT": 297 elif self.m_algorithm == "SUBDIVSQRT":
297 298
298 # subtract and divide by sqrt of model map 299 # subtract and divide by sqrt of model map
299 - residualmap -= modelmap  
300 - residualmap /= modelmap.sqrt() 300 + self.resmap -= modelmap
  301 + self.resmap /= modelmap.sqrt()
301 302
302 else: 303 else:
303 304
304 - # Rais error if algorithm is unkown 305 + # Raise error if algorithm is unkown
305 raise TypeError("Algorithm \""+self.m_algorithm+"\" not known") 306 raise TypeError("Algorithm \""+self.m_algorithm+"\" not known")
306 307
307 - # Save files  
308 - residualmap.save(self.m_outfile,self.m_clobber)  
309 -  
310 - # Remove binned and model files in case they shouldnt be saved  
311 - if self.m_binfile == "NONE":  
312 - os.remove(bin["outfile"].filename())  
313 - if self.m_modfile == "NONE":  
314 - os.remove(model["outfile"].filename())  
315 -  
316 # Return 308 # Return
317 return 309 return
318 310
test/test_cscripts.sh.in
@@ -139,7 +139,7 @@ $ECHO @ECHO_N@ "Test csresmap: " @@ -139,7 +139,7 @@ $ECHO @ECHO_N@ "Test csresmap: "
139 chmod +x @abs_top_srcdir@/scripts/csresmap.py 139 chmod +x @abs_top_srcdir@/scripts/csresmap.py
140 @abs_top_srcdir@/scripts/csresmap.py \ 140 @abs_top_srcdir@/scripts/csresmap.py \
141 infile="data/crab_events.fits" \ 141 infile="data/crab_events.fits" \
142 - outfile="residualmap.fits" \ 142 + outfile="resmap.fits" \
143 srcmdl="data/crab.xml" \ 143 srcmdl="data/crab.xml" \
144 caldb="irf" \ 144 caldb="irf" \
145 irf="cta_dummy_irf" \ 145 irf="cta_dummy_irf" \
@@ -152,11 +152,11 @@ chmod +x @abs_top_srcdir@/scripts/csresmap.py @@ -152,11 +152,11 @@ chmod +x @abs_top_srcdir@/scripts/csresmap.py
152 proj="CAR" \ 152 proj="CAR" \
153 binsz=0.05 > csresmap.log 153 binsz=0.05 > csresmap.log
154 $ECHO @ECHO_N@ "." 154 $ECHO @ECHO_N@ "."
155 -if [ -s "residualmap.fits" ] 155 +if [ -s "resmap.fits" ]
156 then 156 then
157 $ECHO @ECHO_N@ "." 157 $ECHO @ECHO_N@ "."
158 else 158 else
159 - $ECHO " residualmap.fits file is not valid" 159 + $ECHO " resmap.fits file is not valid"
160 exit 1 160 exit 1
161 fi 161 fi
162 $ECHO " ok" 162 $ECHO " ok"