test.py

Knödlseder Jürgen, 10/25/2014 01:40 AM

Download (6.94 KB)

 
1
#! /usr/bin/env python
2

    
3
import ctools
4
import gammalib
5
from math import *
6
import os
7
import glob
8
import sys
9
import time
10
import numpy
11

    
12
# Energy range for the simulation
13
sim_emin = 0.1
14
sim_emax = 100.
15

    
16

    
17
# =========================== #
18
# Setup Galactic plane survey #
19
# =========================== #
20
def survey_gplane2(lrange=6, lstep=2, brange=2, bstep=2, duration=36000):
21
    """
22
    Creates a single observation survey for test purposes.
23

24
    Keywords:
25
     lrange   - Longitude range (deg)
26
     lstep    - Longitude step size (deg)
27
     brange   - Latitude range (deg)
28
     bstep    - Latitude step size (deg)
29
     duration - Observation duration (integer seconds)
30
    """
31
    # Allocate observation container
32
    obs = gammalib.GObservations()
33

    
34
    # Loop over longitudes
35
    run_counter=0
36
    for l in numpy.linspace(-lrange,lrange,2*lrange/lstep+1):
37
        for b in numpy.linspace(-brange,brange,2*brange/bstep+1):
38
            # Set pointing
39
            pntdir = gammalib.GSkyDir()
40
            pntdir.lb_deg(l, b)
41
            run = ctools.obsutils.set_obs(pntdir, duration=duration, irf="irf_file.fits", caldb="$CALDB/data/cta/e/bcf/IFAE20120510_50h", emin=sim_emin, emax=sim_emax)
42
            run.id("run_counter_%i" %run_counter)
43
            run_counter+=1
44
            obs.append(run) 
45

    
46
    # Return observation container
47
    return obs
48

    
49

    
50
#==========================#
51
# Main routine entry point #
52
#==========================#
53
if __name__ == '__main__':
54

    
55
    # Set parameters
56
    outpath         = "TEST_out/"
57
    extension       = "490h"
58
    xml_model_name  = "obs_models_%s.xml" %(extension)
59
    xml_obs_name    = "%sobs_stacked%s.xml" %(outpath,extension)
60
    xml_smodel_name = "%sobs_models_%s.xml" %(outpath,extension)
61
    events_name     = "%sevents%s.xml" %(outpath,extension)
62
    cntmap_name     = "%scntmap%s.fits" %(outpath,extension)
63
    expcube_name    = "%sexpcube%s.fits" %(outpath,extension)
64
    psfcube_name    = "%spsfcube%s.fits" %(outpath,extension)
65
    bkgcube_name    = "%sbkgcube%s.fits" %(outpath,extension)
66
    likeout_model   = "%scube_results%s.xml" %(outpath,extension)
67
    model_name      = "%smodmap%s.fits" %(outpath,extension)
68
    nxpix           = 100
69
    nypix           = 100
70
    binsz           = 0.06
71

    
72
    # Setup observation survey
73
    obs = survey_gplane2(lrange=2.5, lstep=1, brange=3.5, bstep=1, duration=100)
74
    obs.models(gammalib.GModels(xml_model_name))
75

    
76
    # Run ctobssim (simulates event lists)
77
    print("Simulate events")
78
    sim = ctools.ctobssim(obs)
79
    sim.logFileOpen()
80
    sim["outfile"].filename(events_name)
81
    sim["prefix"].string("%ssim_events_%s" %(outpath,extension))
82
    sim.execute()
83
    print("Done")
84

    
85
    # Run ctbin (builds stacked event cube)
86
    print("\nBin events")
87
    bin = ctools.ctbin()
88
    bin.logFileOpen()
89
    bin["evfile"].filename(events_name)
90
    bin["outfile"].filename(cntmap_name)
91
    bin["ebinalg"].string("LOG")
92
    bin["emin"].real(0.1)
93
    bin["emax"].real(100.0)
94
    bin["enumbins"].integer(20)
95
    bin["usepnt"].boolean(False)
96
    bin["nxpix"].integer(nxpix)
97
    bin["nypix"].integer(nypix)
98
    bin["binsz"].real(binsz)
99
    bin["coordsys"].string("GAL")
100
    bin["proj"].string("CAR")
101
    bin["xref"].real(0.0)
102
    bin["yref"].real(0.0)
103
    bin.execute()
104
    print("Done")
105

    
106
    # Run ctexpcube (builds exposure cube)
107
    print("\nBuild exposure cube")
108
    expcube = ctools.ctexpcube(sim.obs())
109
    expcube.logFileOpen()
110
    expcube["cntmap"].filename(cntmap_name)
111
    expcube["outfile"].filename(expcube_name)
112
    #expcube["caldb"].string(caldb)    # Not needed, response in XML file
113
    #expcube["irf"].string(irf)        # Not needed, response in XML file
114
    expcube.execute()
115
    print("Done")
116

    
117
    # Run ctpsfcube (builds PSF cube)
118
    print("\nBuild PSF cube")
119
    psfcube = ctools.ctpsfcube(sim.obs())
120
    psfcube.logFileOpen()
121
    psfcube["cntmap"].filename("NONE")
122
    psfcube["outfile"].filename(psfcube_name)
123
    #psfcube["caldb"].string(caldb)    # Not needed, response in XML file
124
    #psfcube["irf"].string(irf)        # Not needed, response in XML file
125
    psfcube["ebinalg"].string("LOG")
126
    psfcube["emin"].real(0.1)
127
    psfcube["emax"].real(100.0)
128
    psfcube["enumbins"].integer(20)
129
    psfcube["nxpix"].integer(nxpix)
130
    psfcube["nypix"].integer(nypix)
131
    psfcube["binsz"].real(binsz)
132
    psfcube["coordsys"].string("GAL")
133
    psfcube["proj"].string("CAR")
134
    psfcube["xref"].real(0.)
135
    psfcube["yref"].real(0.)
136
    psfcube["amax"].real(0.3)
137
    psfcube["anumbins"].integer(10)
138
    psfcube.execute()
139
    print("Done")
140

    
141
    # Run ctbkgcube (builds background cube)
142
    print("\nBuild background cube")
143
    bkgcube = ctools.ctbkgcube(sim.obs())
144
    bkgcube.logFileOpen()
145
    bkgcube["infile"].filename(events_name)
146
    bkgcube["cntmap"].filename(cntmap_name)
147
    bkgcube["bkgmdl"].filename(xml_model_name)
148
    bkgcube["outfile"].filename(bkgcube_name)
149
    bkgcube.execute()
150
    print("Done")
151

    
152
    # Create XML file for stacked analysis. We need this as so far there is
153
    # no way to directly feed the stacked response files to ctlike and
154
    # ctmodel. This should be changed in the future, but for the moment
155
    # that's how to do the job ...
156
    obs      = gammalib.GObservations()
157
    cta      = gammalib.GCTAObservation()
158
    exposure = gammalib.GCTAExposure(expcube_name)
159
    psf      = gammalib.GCTAMeanPsf(psfcube_name)
160
    cta.load(cntmap_name)
161
    cta.response(exposure, psf)
162
    obs.append(cta)
163
    obs.save(xml_obs_name)
164

    
165
    # Create model file for stacked analysis. To do this I remove the
166
    # original background model from the model container and add a diffuse
167
    # cube background with a constant fitting parameter.
168
    models = gammalib.GModels(xml_model_name)
169
    models.remove("Background")
170
    bgd_spatial  = gammalib.GModelSpatialDiffuseCube(bkgcube_name)
171
    bgd_spectral = gammalib.GModelSpectralConst(1.0)
172
    bgd          = gammalib.GCTAModelCubeBackground(bgd_spatial, bgd_spectral)
173
    models.append(bgd)
174
    models.save(xml_smodel_name)
175

    
176
    # Run ctlike
177
    print("\nMaximum likelihood fitting")
178
    like = ctools.ctlike()
179
    like.logFileOpen()
180
    like["infile"].filename(xml_obs_name)
181
    #like["irf"].string(irf)        # Not needed, response in XML file
182
    #like["caldb"].string(caldb)    # Not needed, response in XML file
183
    like["srcmdl"].filename(xml_smodel_name)
184
    #like["tscalc"].boolean(False)  # This parameter has been removed from ctlike
185
    like["debug"].boolean(True)
186
    like["outmdl"].filename(likeout_model)
187
    like.execute()
188
    print("Done")
189
 
190
    # Make model map
191
    print("\nMake model map")
192
    model = ctools.ctmodel()
193
    model.logFileOpen()
194
    model["infile"].filename(cntmap_name)
195
    #model["obsfile"].filename(events_name) # Use the unstacked response
196
    model["obsfile"].filename(xml_obs_name) # Use the stacked response (faster!)
197
    model["outfile"].filename(model_name)
198
    #model["caldb"].string(caldb)           # Not needed, response in XML file
199
    #model["irf"].string(irf)               # Not needed, response in XML file
200
    model["srcmdl"].filename(likeout_model)
201
    model.execute()
202
    print("Done")