Commit b5ef86abcef67af122b26039251f95b6920c106a

Authored by Nathan Kelley-Hoskins
1 parent cec6dca8

added atomic loading function

Showing 1 changed file with 90 additions and 0 deletions
pyext/GObservations.i
... ... @@ -179,4 +179,94 @@ typedef GObservations::likelihood likelihood;
179 179 for x in state[1]:
180 180 self.append(x)
181 181 }
  182 +%pythoncode {
  183 +
  184 + def load_atomic_fits( self, fnames, bkg='powerlaw'):
  185 + """Function for loading atomic fits files, that have no outside references.
  186 + Each fits file should have all of the following tables:
  187 + EVENTS
  188 + TELARRAY
  189 + GTI
  190 + POINT SPREAD FUNCTION
  191 + EFFECTIVE AREA
  192 + ENERGY DISPERSION
  193 + BACKGROUND
  194 +
  195 + **Args:**
  196 + fnames : str or list of str, input fits filenames to load into self
  197 +
  198 + **Opts:**
  199 + bkg : str, type of spectral model to use with all backgrounds.
  200 + can be one of:
  201 + constant, Const, GmodelSpectralConst,
  202 + powerlaw, Plaw, GModelSpectralPlaw,
  203 + BrokenPlaw, GModelSpectralBrokenPlaw,
  204 + ExpPlaw, GModelSpectralExpPlaw
  205 + """
  206 +
  207 + import os
  208 +
  209 + if isinstance( fnames, str ) :
  210 + fnames = [ fnames ]
  211 +
  212 + """Loop over our input fits filenames"""
  213 + for ifits, fits in enumerate( fnames ) :
  214 +
  215 + ob = gammalib.GCTAObservation()
  216 +
  217 + """Convert unicode to a regular str"""
  218 + if isinstance(fits, bytes) :
  219 + fits = fits.decode('utf-8')
  220 +
  221 + ob.load( fits )
  222 +
  223 + """We need a unique identifier, so lets just use the
  224 + full filename"""
  225 + ob.id( os.path.abspath( fits ) )
  226 +
  227 + """Load IRFs"""
  228 + rsp = gammalib.GCTAResponseIrf()
  229 + rsp.load_aeff( fits )
  230 + rsp.load_psf( fits )
  231 + rsp.load_edisp( fits )
  232 + rsp.apply_edisp( True )
  233 + rsp.load_background( fits )
  234 + ob.response( rsp )
  235 +
  236 + """Add our GCTAObservation to the GObservations"""
  237 + self.append( ob )
  238 +
  239 + spec = -1
  240 +
  241 + if bkg in ['constant','Const','GModelSpectralConst'] :
  242 + spec = gammalib.GModelSpectralConst()
  243 +
  244 + elif bkg in [ 'powerlaw','Plaw', 'GModelSpectralPlaw'] :
  245 + pivot = gammalib.GEnergy( 1.0, 'TeV' )
  246 + spec = gammalib.GModelSpectralPlaw()
  247 + spec['PivotEnergy'].value( pivot.MeV() )
  248 + spec['PivotEnergy'].fix()
  249 + spec['Prefactor' ].free()
  250 + spec['Prefactor' ].value( 1.35 )
  251 + spec['Index' ].free()
  252 + spec['Index' ].min( -10 )
  253 + spec['Index' ].max( 10 )
  254 + spec['Index' ].value( -0.05 )
  255 +
  256 + elif bkg in ['BrokenPlaw', 'GModelSpectralBrokenPlaw'] :
  257 + spec = gammalib.GModelSpectralBrokenPlaw()
  258 +
  259 + elif bkg in ['ExpPlaw', 'GModelSpectralExpPlaw'] :
  260 + spec = gammalib.GModelSpectralExpPlaw()
  261 +
  262 + else :
  263 + raise ValueError( "For kwarg bkg='%s', '%s' is not a recognizable spectral model." % (bkg,bkg) )
  264 +
  265 + back = gammalib.GCTAModelIrfBackground( spec )
  266 + back.name( '%sbkg_%s' % ( bkg, ob.id() ) )
  267 + back.ids( ob.id() )
  268 + self.models().append( back )
  269 +
  270 +}
  271 +
182 272 };
... ...