Ejemplo n.º 1
0
def ctskymap(inobs,
             outmap,
             xref=221.0,
             yref=46.0,
             proj='CAR',
             coordsys='CEL',
             binsz=0.02,
             nxpix=200,
             nypix=200,
             emin=0.1,
             emax=100.0,
             bkgsubtract='NONE'):
    smap = ctools.ctskymap()
    smap['inobs'] = inobs
    smap['xref'] = xref
    smap['yref'] = yref
    smap['proj'] = proj
    smap['coordsys'] = coordsys
    smap['binsz'] = binsz
    smap['nxpix'] = nxpix
    smap['nypix'] = nypix
    smap['emin'] = emin
    smap['emax'] = emax
    smap['bkgsubtract'] = bkgsubtract
    smap['outmap'] = outmap
    smap.execute()
    print("Generated " + outmap)
Ejemplo n.º 2
0
    def _test_python_irf(self):
        """
        Test ctskymap from Python for IRF background method
        """
        # Test IRF background subtraction
        skymap = ctools.ctskymap()
        skymap['inobs'] = self._events
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 20
        skymap['nypix'] = 20
        skymap['binsz'] = 0.2
        skymap['coordsys'] = 'GAL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 184.5575
        skymap['yref'] = -5.7844
        skymap['bkgsubtract'] = 'IRF'
        skymap['caldb'] = self._caldb
        skymap['irf'] = self._irf
        skymap['outmap'] = 'ctskymap_irf_py1.fits'
        skymap['logfile'] = 'ctskymap_irf_py1.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_irf_py1.fits')
        self._check_result_file('ctskymap_irf_py1.fits[BACKGROUND]')
        self._check_result_file('ctskymap_irf_py1.fits[SIGNIFICANCE]')

        # Return
        return
Ejemplo n.º 3
0
    def ctskymap(self,log=False,debug=False, **kwargs):
        '''
        Create ctskymap instance with given parameters
        Parameters
        ---------
        log  : save or not the log file
        debug  : debug mode or not. This will print a lot of information
        '''
        self.info("Running ctskymap to make map")


        self.skymap = ct.ctskymap()
        
        self._fill_app( self.skymap,log=log,debug=debug, **kwargs)

        self.skymap["xref"] = self.config['target']["ra"]
        self.skymap["yref"] = self.config['target']["dec"]

        if self.verbose:
            print self.skymap

        self.skymap.run()

        self.skymap.save()
        self.info("Saved sky map to {0:s}".format(self.skymap["outmap"]))

        del self.skymap
Ejemplo n.º 4
0
    def _test_python(self):
        """
        Test ctskymap from Python
        """
        # Set-up ctskymap
        skymap = ctools.ctskymap()
        skymap['inobs'] = self._events
        skymap['outmap'] = 'ctskymap_py1.fits'
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 200
        skymap['nypix'] = 200
        skymap['binsz'] = 0.02
        skymap['coordsys'] = 'CEL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 83.63
        skymap['yref'] = 22.01
        skymap['logfile'] = 'ctskymap_py1.log'
        skymap['chatter'] = 2

        # Run ctskymap tool
        skymap.logFileOpen()  # Make sure we get a log file
        skymap.run()
        skymap.save()

        # Check result file
        self._check_result_file('ctskymap_py1.fits')

        # Return
        return
def generate_skymap(name, lon, lat, emin=0.030, emax=199.0):
    """
    Generate sky map

    Parameters
    ----------
    name : string
        Source name
    lon : float
        Galactic longitude of binary (deg)
    lat : float
        Galactic latitude of binary (deg)
    emin : float, optional
        Minimum energy (TeV)
    emax : float, optional
        Maximum energy (TeV)
    """
    # Dump header
    print('Generate sky map')

    # Enter working directory
    cwd = enter_wd(name)

    # Continue only if skymap does not exist
    if not os.path.isfile('skymap.fits'):

        # Setup task parameters
        skymap = ctools.ctskymap()
        skymap['inobs'] = 'gps_obs_simulated.xml'
        skymap['emin'] = emin
        skymap['emax'] = emax
        skymap['nxpix'] = 200
        skymap['nypix'] = 200
        skymap['binsz'] = 0.02
        skymap['coordsys'] = 'GAL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = lon
        skymap['yref'] = lat
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.02
        skymap['inradius'] = 0.60
        skymap['outradius'] = 0.80
        skymap['iterations'] = 3
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = 'NONE'
        skymap['outmap'] = 'skymap.fits'
        skymap['logfile'] = 'ctskymap.log'
        skymap.logFileOpen()

        # Generate sky map
        skymap.execute()

    # Exit working directory
    exit_wd(cwd)

    # Return
    return
Ejemplo n.º 6
0
def run_ctskymap(outfile='ctskymap.fits.gz'):
    tool = ctools.ctskymap()
    tool['inobs'] = 'hess_events_023523.fits.gz'
    tool['outmap'] = outfile
    tool['emin'] = 0.1
    tool['emax'] = 100
    set_spatial_pars(tool)
    print('Writing {}'.format(outfile))
    tool.execute()
Ejemplo n.º 7
0
def run_ctskymap(outfile='ctskymap.fits.gz'):
    tool = ctools.ctskymap()
    tool['inobs'] = 'hess_events_023523.fits.gz'
    tool['outmap'] = outfile
    tool['emin'] = 0.1
    tool['emax'] = 100
    set_spatial_pars(tool)
    print('Writing {}'.format(outfile))
    tool.execute()
Ejemplo n.º 8
0
    def _test_python_exclusion_map(self):
        """
        Test exclusion_map methods
        """
        # Setup ctskymap
        skymap = ctools.ctskymap()

        # Set exclusion map
        map = gammalib.GSkyRegionMap(self._exclusion_map)

        # Test iterative RING background subtraction with an exclusion map
        # set via the ctskymap::exclusion_map() method
        skymap = ctools.ctskymap()
        skymap['inmap'] = self._skymap_ring
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 2
        skymap['threshold'] = 5.0
        skymap['outmap'] = 'ctskymap_ring_py7.fits'
        skymap['logfile'] = 'ctskymap_ring_py7.log'
        skymap['chatter'] = 2
        skymap.exclusion_map(map)

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py7.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py7.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py7.fits[SIGNIFICANCE]', 10, 10)

        # Recover exclusion map
        map2 = skymap.exclusion_map()

        # Check exclusion map
        self._check_map(map2.map(), nx=10, ny=10)

        # Return
        return
def ctamap( obs , emin , emax , \
    coordsys , ra , dec , rad , \
    caldb , irf , name , plotfile ) :
    skymap = ctools.ctskymap() 
    skymap[ 'inobs' ] = obs
    skymap[ 'emin' ] = emin
    skymap[ 'emax' ] = emax
    skymap[ 'nxpix' ] = 100
    skymap[ 'nypix' ] = 100
    skymap[ 'binsz' ] = 0.02
    skymap[ 'proj' ] = 'CAR'
    skymap[ 'coordsys' ] = coordsys
    skymap[ 'xref' ] = ra
    skymap[ 'yref' ] = dec
    skymap[ 'bkgsubtract' ] = 'IRF'
    skymap[ 'caldb' ] = caldb
    skymap[ 'irf' ] = irf
    skymap[ 'outmap' ] = name

    skymap.execute()

    skymap.skymap().smooth( 'GAUSSIAN', 0.02 )

    ax = plt.subplot()
    plt.imshow( skymap.skymap().array() , \
        origin='lower' , \
        extent=[ ra + rad , \
            ra - rad , \
            dec - rad , \
            dec + rad ] , \
        norm=SymLogNorm( 1 , base=10 ) )
    xlabel = '' ; ylabel = '' ;
    if coordsys == 'GAL' :
        xlabel = 'Longitude (deg)'
        ylabel = 'Latitude (deg)'
    elif coordsys == 'CEL' :
        xlabel = 'R.A. (deg)'
        ylabel = 'DEC (deg)'
    else :
        print( 'Unknown coordinate System. \
            I will assume Celestial Coordinate System' )
        xlabel = 'R.A. (deg)'
        ylabel = 'DEC (deg)'
    ax.set_xlabel( xlabel )
    ax.set_ylabel( ylabel )
    cbar = plt.colorbar()
    cbar.set_label( 'Counts' )

    if len( plotfile ) > 0 :
        plt.savefig( plotfile )
    else :
        plt.show()
    plt.close()
Ejemplo n.º 10
0
def generate_skymap(emin=0.670, emax=30.0):
    """
    Generate sky map

    Parameters
    ----------
    emin : float, optional
        Minimum energy (TeV)
    emax : float, optional
        Maximum energy (TeV)
    """
    # Set filenames
    inobs = 'obs_crab_selected.xml'
    outmap = 'crab_skymap.fits'
    logfile = 'crab_skymap.log'

    # Continue only if inmodel exists
    if os.path.isfile(inobs) and not os.path.isfile(outmap):

        # Setup task parameters
        skymap = ctools.ctskymap()
        skymap['inobs'] = inobs
        skymap['emin'] = emin
        skymap['emax'] = emax
        skymap['nxpix'] = 200
        skymap['nypix'] = 200
        skymap['binsz'] = 0.02
        skymap['coordsys'] = 'CEL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 83.63
        skymap['yref'] = 22.01
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 3
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = 'NONE'
        skymap['outmap'] = outmap
        skymap['logfile'] = logfile
        skymap.logFileOpen()

        # Generate sky map
        skymap.execute()

    # Return
    return
Ejemplo n.º 11
0
 def ctskymap(self, fits):
     skymap = ctools.ctskymap()
     skymap['inobs'] = 'selected_events.fits'
     skymap['emin'] = 0.3
     skymap['emax'] = 150.0
     skymap['nxpix'] = 40
     skymap['nypix'] = 40
     skymap['binsz'] = 0.02
     skymap['proj'] = 'TAN'
     skymap['coordsys'] = 'CEL'
     skymap['xref'] = 87
     skymap['yref'] = 22.01
     skymap['bkgsubtract'] = 'IRF'
     skymap['caldb'] = 'prod2'
     skymap['irf'] = 'South_0.5h'
     skymap['outmap'] = fits
     skymap.run()
     skymap.save()
Ejemplo n.º 12
0
    def test_functional(self):
        """
        Test ctskymap functionnality.
        """
        # Set-up ctskymap
        skymap = ctools.ctskymap()
        skymap["inobs"]    = self.events_name
        skymap["outmap"]   = "skymap.fits"
        skymap["emin"]     = 0.1
        skymap["emax"]     = 100
        skymap["nxpix"]    = 200
        skymap["nypix"]    = 200
        skymap["binsz"]    = 0.02
        skymap["coordsys"] = "CEL"
        skymap["proj"]     = "CAR"
        skymap["xref"]     = 83.63
        skymap["yref"]     = 22.01

        # Run tool
        self.test_try("Run ctskymap")
        try:
            skymap.run()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in ctskymap.")

        # Save counts cube
        self.test_try("Save sky map")
        try:
            skymap.save()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in saving sky map.")

        # Return
        return
Ejemplo n.º 13
0
    def test_functional(self):
        """
        Test ctskymap functionnality.
        """
        # Set-up ctskymap
        skymap = ctools.ctskymap()
        skymap["inobs"].filename(self.events_name)
        skymap["outmap"].filename("skymap.fits")
        skymap["emin"].real(0.1)
        skymap["emax"].real(100.0)
        skymap["nxpix"].integer(200)
        skymap["nypix"].integer(200)
        skymap["binsz"].real(0.02)
        skymap["coordsys"].string("CEL")
        skymap["proj"].string("CAR")
        skymap["xref"].real(83.63)
        skymap["yref"].real(22.01)
        
        # Run tool
        self.test_try("Run ctskymap")
        try:
            skymap.run()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in ctskymap.")

        # Save counts cube
        self.test_try("Save sky map")
        try:
            skymap.save()
            self.test_try_success()
        except:
            self.test_try_failure("Exception occured in saving sky map.")

        # Return
        return
Ejemplo n.º 14
0
    def _test_python_ring(self):
        """
        Test ctskymap from Python for RING background method
        """
        # Test basic RING background subtraction
        skymap = ctools.ctskymap()
        skymap['inobs'] = self._events
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 10
        skymap['nypix'] = 10
        skymap['binsz'] = 0.2
        skymap['coordsys'] = 'CEL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 83.63
        skymap['yref'] = 22.01
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 0
        skymap['inexclusion'] = 'NONE'
        skymap['caldb'] = self._caldb
        skymap['irf'] = self._irf
        skymap['outmap'] = 'ctskymap_ring_py1.fits'
        skymap['logfile'] = 'ctskymap_ring_py1.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py1.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py1.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py1.fits[SIGNIFICANCE]', 10, 10)

        # Test iterative RING background subtraction
        skymap = ctools.ctskymap()
        skymap['inobs'] = self._events
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 10
        skymap['nypix'] = 10
        skymap['binsz'] = 0.2
        skymap['coordsys'] = 'CEL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 83.63
        skymap['yref'] = 22.01
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 2
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = 'NONE'
        skymap['caldb'] = self._caldb
        skymap['irf'] = self._irf
        skymap['outmap'] = 'ctskymap_ring_py2.fits'
        skymap['logfile'] = 'ctskymap_ring_py2.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py2.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py2.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py2.fits[SIGNIFICANCE]', 10, 10)

        # Test iterative RING background subtraction, starting from a sky map
        skymap = ctools.ctskymap()
        skymap['inmap'] = self._skymap_ring
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 2
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = 'NONE'
        skymap['outmap'] = 'ctskymap_ring_py3.fits'
        skymap['logfile'] = 'ctskymap_ring_py3.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py3.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py3.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py3.fits[SIGNIFICANCE]', 10, 10)

        # Test iterative RING background subtraction with an exclusion region
        skymap = ctools.ctskymap()
        skymap['inmap'] = self._skymap_ring
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 2
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = self._exclusion_region
        skymap['outmap'] = 'ctskymap_ring_py4.fits'
        skymap['logfile'] = 'ctskymap_ring_py4.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py4.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py4.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py4.fits[SIGNIFICANCE]', 10, 10)

        # Test iterative RING background subtraction with an exclusion map
        skymap = ctools.ctskymap()
        skymap['inmap'] = self._skymap_ring
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 2
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = self._exclusion_map
        skymap['outmap'] = 'ctskymap_ring_py5.fits'
        skymap['logfile'] = 'ctskymap_ring_py5.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py5.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py5.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py5.fits[SIGNIFICANCE]', 10, 10)

        # Test RING background subtraction with direct significance computation
        skymap = ctools.ctskymap()
        skymap['inmap'] = self._skymap_ring
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius'] = 0.1
        skymap['inradius'] = 0.6
        skymap['outradius'] = 0.8
        skymap['iterations'] = 0
        skymap['threshold'] = 5.0
        skymap['inexclusion'] = 'NONE'
        skymap['usefft'] = False  # Direct computation, no FFT
        skymap['outmap'] = 'ctskymap_ring_py6.fits'
        skymap['logfile'] = 'ctskymap_ring_py6.log'
        skymap['chatter'] = 2

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_ring_py6.fits', 10, 10)
        self._check_result_file('ctskymap_ring_py6.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_ring_py6.fits[SIGNIFICANCE]', 10, 10)

        # Return
        return
Ejemplo n.º 15
0
    def _test_python(self):
        """
        Test ctskymap from Python
        """
        # Allocate ctskymap
        skymap = ctools.ctskymap()

        # Check that empty ctskymap tool holds a map that has no pixels
        self._check_map(skymap.skymap(), nx=0, ny=0)

        # Check that saving does not nothing
        skymap['outmap'] = 'ctskymap_py0.fits'
        skymap['logfile'] = 'ctskymap_py0.log'
        skymap.logFileOpen()
        skymap.save()
        self.test_assert(not os.path.isfile('ctskymap_py0.fits'),
                         'Check that no sky map has been created')

        # Check that clearing does not lead to an exception or segfault
        skymap.clear()

        # Now set ctskymap parameters
        skymap['inobs'] = self._events
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 20
        skymap['nypix'] = 20
        skymap['binsz'] = 0.2
        skymap['coordsys'] = 'CEL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 83.63
        skymap['yref'] = 22.01
        skymap['bkgsubtract'] = 'NONE'
        skymap['outmap'] = 'ctskymap_py1.fits'
        skymap['logfile'] = 'ctskymap_py1.log'
        skymap['chatter'] = 2

        # Run ctskymap tool
        skymap.logFileOpen()  # Make sure we get a log file
        skymap.run()
        skymap.save()

        # Check result file
        self._check_result_file('ctskymap_py1.fits')

        # Copy ctskymap tool
        cpy_skymap = skymap.copy()

        # Check sky map of ctskymap copy
        self._check_map(cpy_skymap.skymap())

        # Execute copy of ctskymap tool again, now with a higher chatter
        # level than before
        cpy_skymap['usepnt'] = True
        cpy_skymap['emin'] = 0.2
        cpy_skymap['emax'] = 150.0
        cpy_skymap['coordsys'] = 'GAL'
        cpy_skymap['proj'] = 'CAR'
        cpy_skymap['outmap'] = 'ctskymap_py2.fits'
        cpy_skymap['logfile'] = 'ctskymap_py2.log'
        cpy_skymap['publish'] = True
        cpy_skymap['chatter'] = 3
        cpy_skymap.logFileOpen()  # Needed to get a new log file
        cpy_skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py2.fits')

        # Now clear copy of ctskymap tool
        cpy_skymap.clear()

        # Check that cleared ctskymap tool holds a map that has no pixels
        self._check_map(cpy_skymap.skymap(), nx=0, ny=0)

        # Get mixed observation container
        obs = self._obs_mixed()

        # Allocate ctskymap tool from observation container
        skymap = ctools.ctskymap(obs)
        skymap['emin'] = 0.1
        skymap['emax'] = 100
        skymap['nxpix'] = 20
        skymap['nypix'] = 20
        skymap['binsz'] = 0.2
        skymap['coordsys'] = 'GAL'
        skymap['proj'] = 'CAR'
        skymap['xref'] = 184.5575
        skymap['yref'] = -5.7844
        skymap['bkgsubtract'] = 'NONE'
        skymap['outmap'] = 'ctskymap_py3.fits'
        skymap['logfile'] = 'ctskymap_py3.log'
        skymap['chatter'] = 4

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py3.fits')

        # Publish with name
        skymap.publish('My sky map')

        # Return
        return
Ejemplo n.º 16
0
def gw_simulation(sim_in, config_in, model_xml, fits_model, counter):
    """

    :param sim_in:
    :param config_in:
    :param model_xml:
    :param fits_model:
    :param counter:
    :return:
    """

    src_name = fits_model.split("/")[-1][:-5]
    run_id, merger_id = src_name.split('_')

    fits_header_0 = fits.open(fits_model)[0].header
    ra_src = fits_header_0['RA']
    dec_src = fits_header_0['DEC']

    coordinate_source = SkyCoord(ra=ra_src * u.deg, dec=dec_src * u.deg, frame="icrs")

    src_yaml = sim_in['source']

    point_path = create_path(src_yaml['pointings_path'])
    opt_point_path = f"{point_path}/optimized_pointings"

    ctools_pipe_path = create_path(config_in['exe']['software_path'])
    ctobss_params = sim_in['ctobssim']

    seed = int(counter)*10

    # # PARAMETERS FROM THE CTOBSSIM
    sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV)
    sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV)

    sim_rad = ctobss_params['radius']
    output_path = create_path(sim_in['output']['path'] + f"/{src_name}/seed-{seed:03}")

    irf_dict = sim_in['IRF']
    site = irf_dict['site']

    detection = sim_in['detection']
    significance_map = detection['skymap_significance']
    srcdetect_ctlike = detection['srcdetect_likelihood']

    save_simulation = ctobss_params['save_simulation']

    try:
        mergers_data = pd.read_csv(
            f"{point_path}/BNS-GW-Time_onAxis5deg.txt",
            sep=" ")
    except FileNotFoundError:
        print("merger data not present. check that the text file with the correct pointings is in the 'pointings' folder!")
        sys.exit()

    filter_mask = (mergers_data["run"] == run_id) & (mergers_data["MergerID"] == f"Merger{merger_id}")
    merger_onset_data = mergers_data[filter_mask]
    time_onset_merger = merger_onset_data['Time'].values[0]

    with open(f"{output_path}/GW-{src_name}_seed-{seed:03}_site-{site}.txt", "w") as f:
        f.write(f"GW_name\tRA_src\tDEC_src\tseed\tpointing_id\tsrc_to_point\tsrc_in_point\tra_point\tdec_point\tradius\ttime_start\ttime_end\tsignificanceskymap\tsigmasrcdetectctlike\n")
        try:
            file_name = f"{opt_point_path}/{run_id}_Merger{merger_id}_GWOptimisation_v3.txt"
            pointing_data = pd.read_csv(
                file_name,
                header=0,
                sep=",")
        except FileNotFoundError:
            print("File not found\n")
            sys.exit()

        RA_data = pointing_data['RA(deg)']
        DEC_data = pointing_data['DEC(deg)']
        times = pointing_data['Observation Time UTC']
        durations = pointing_data['Duration']

        # LOOP OVER POINTINGS
        for index in range(0, len(pointing_data)):
            RA_point = RA_data[index]
            DEC_point = DEC_data[index]
            coordinate_pointing = SkyCoord(
                ra=RA_point * u.degree,
                dec=DEC_point * u.degree,
                frame="icrs"
            )
            src_from_pointing = coordinate_pointing.separation(coordinate_source)

            t_in_point = Time(times[index])

            obs_condition = Observability(site=site)
            obs_condition.set_irf(irf_dict)
            obs_condition.Proposal_obTime = 10
            obs_condition.TimeOffset = 0
            obs_condition.Steps_observability = 10
            condition_check = obs_condition.check(RA=RA_point, DEC=DEC_point, t_start=t_in_point)

            # once the IRF has been chosen, the times are shifted
            # this is a quick and dirty solution to handle the times in ctools...not elegant for sure
            t_in_point = (Time(times[index]) - Time(time_onset_merger)).to(u.s)
            t_end_point = t_in_point + durations[index] * u.s

            if len(condition_check) == 0:
                print(f"Source Not Visible in pointing {index}")
                f.write(
                    f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n")
                continue

            name_irf = condition_check['IRF_name'][0]
            irf = condition_check['IRF'][0]
            # model loading

            if irf.prod_number == "3b" and irf.prod_version == 0:
                caldb = "prod3b"
            else:
                caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

            # simulation
            sim = ctools.ctobssim()
            sim['inmodel'] = model_xml
            sim['caldb'] = caldb
            sim['irf'] = name_irf
            sim['ra'] = RA_point
            sim['dec'] = DEC_point
            sim['rad'] = sim_rad
            sim['tmin'] = t_in_point.value
            sim['tmax'] = t_end_point.value
            sim['emin'] = sim_e_min
            sim['emax'] = sim_e_max
            sim['seed'] = seed

            if save_simulation:
                event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/seed-{seed:03}/")
                sim['outevents'] = f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}_pointingID-{index}.fits"
                sim.execute()
                f.write(
                    f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point}\t{DEC_point}\t{sim_rad}\t{t_in_point.value:.2f}\t{t_end_point.value:.2f}\t -1 \t -1\n"
                )
                continue
            else:
                sim.run()

            obs = sim.obs()

            obs.models(gammalib.GModels())

            # ctskymap

            sigma_onoff = -1
            sqrt_ts_like = -1

            if significance_map:
                pars_skymap = detection['parameters_skymap']
                scale = float(pars_skymap['scale'])
                npix = 2 * int(sim_rad / scale)

                fits_temp_title = f"{output_path}/GW-skymap_point-{index}_{seed}.fits"

                skymap = ctools.ctskymap(obs.copy())
                skymap['proj'] = 'CAR'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = RA_point
                skymap['yref'] = DEC_point
                skymap['binsz'] = scale
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['bkgsubtract'] = 'RING'
                skymap['roiradius'] = pars_skymap['roiradius']
                skymap['inradius'] = pars_skymap['inradius']
                skymap['outradius'] = pars_skymap['outradius']
                skymap['iterations'] = pars_skymap['iterations']
                skymap['threshold'] = pars_skymap['threshold']
                skymap['outmap'] = fits_temp_title
                skymap.execute()

                input_fits = fits.open(fits_temp_title)
                datain = input_fits['SIGNIFICANCE'].data
                datain[np.isnan(datain)] = 0.0
                datain[np.isinf(datain)] = 0.0

                sigma_onoff = np.max(datain)

                if pars_skymap['remove_fits']:
                    os.remove(fits_temp_title)

            if srcdetect_ctlike:
                pars_detect = detection['parameters_detect']
                scale = float(pars_detect['scale'])
                npix = 2 * int(sim_rad / scale)

                skymap = ctools.ctskymap(obs.copy())
                skymap['proj'] = 'TAN'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = RA_point
                skymap['yref'] = DEC_point
                skymap['binsz'] = scale
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['bkgsubtract'] = 'NONE'
                skymap.run()

                # cssrcdetect
                srcdetect = cscripts.cssrcdetect(skymap.skymap().copy())
                srcdetect['srcmodel'] = 'POINT'
                srcdetect['bkgmodel'] = 'NONE'
                srcdetect['corr_kern'] = 'GAUSSIAN'
                srcdetect['threshold'] = pars_detect['threshold']
                srcdetect['corr_rad'] = pars_detect['correlation']
                srcdetect.run()

                models = srcdetect.models()

                # if there's some detection we can do the likelihood.
                # Spectral model is a PL and the spatial model is the one from cssrcdetect
                if len(models) > 0:
                    hotspot = models['Src001']
                    ra_hotspot = hotspot['RA'].value()
                    dec_hotspot = hotspot['DEC'].value()

                    models_ctlike = gammalib.GModels()

                    src_dir = gammalib.GSkyDir()
                    src_dir.radec_deg(ra_hotspot, dec_hotspot)
                    spatial = gammalib.GModelSpatialPointSource(src_dir)

                    spectral = gammalib.GModelSpectralPlaw()
                    spectral['Prefactor'].value(5.5e-16)
                    spectral['Prefactor'].scale(1e-16)
                    spectral['Index'].value(-2.6)
                    spectral['Index'].scale(-1.0)
                    spectral['PivotEnergy'].value(50000)
                    spectral['PivotEnergy'].scale(1e3)

                    model_src = gammalib.GModelSky(spatial, spectral)
                    model_src.name('PL_fit_GW')
                    model_src.tscalc(True)

                    models_ctlike.append(model_src)

                    spectral_back = gammalib.GModelSpectralPlaw()
                    spectral_back['Prefactor'].value(1.0)
                    spectral_back['Prefactor'].scale(1.0)
                    spectral_back['Index'].value(0)
                    spectral_back['PivotEnergy'].value(300000)
                    spectral_back['PivotEnergy'].scale(1e6)

                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTA')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())
                    models_ctlike.append(back_model)

                    xmlmodel_PL_ctlike_std = f"{output_path}/model_PL_ctlike_std_seed-{seed}_pointing-{index}.xml"
                    models_ctlike.save(xmlmodel_PL_ctlike_std)

                    like_pl = ctools.ctlike(obs.copy())
                    like_pl['inmodel'] = xmlmodel_PL_ctlike_std
                    like_pl['caldb'] = caldb
                    like_pl['irf'] = name_irf
                    like_pl.run()

                    ts = -like_pl.obs().models()[0].ts()
                    if ts > 0:
                        sqrt_ts_like = np.sqrt(ts)
                    else:
                        sqrt_ts_like = 0

                    if pars_detect['remove_xml']:
                        os.remove(xmlmodel_PL_ctlike_std)

            f.write(
                f"{src_name}\t{ra_src}\t{dec_src}\t{seed}\t{index}\t{src_from_pointing.value:.2f}\t{src_from_pointing.value < sim_rad}\t{RA_point:.2f}\t{DEC_point:.2f}\t{sim_rad}\t{t_in_point:.2f}\t{t_end_point:.2f}\t{sigma_onoff:.2f}\t{sqrt_ts_like}\n")
Ejemplo n.º 17
0
def grb_simulation(sim_in, config_in, model_xml, fits_header_0, counter):
    """
    Function to handle the GRB simulation.
    :param sim_in: the yaml file for the simulation (unpacked as a dict of dicts)
    :param config_in: the yaml file for the job handling (unpacked as a dict of dicts)
    :param model_xml: the XML model name for the source under analysis
    :param fits_header_0: header for the fits file of the GRB model to use. Used in the visibility calculation
    :param counter: integer number. counts the id of the source realization
    :return: significance obtained with the activated detection methods
    """

    src_name = model_xml.split('/')[-1].split('model_')[1][:-4]
    print(src_name, counter)

    ctools_pipe_path = create_path(config_in['exe']['software_path'])
    ctobss_params = sim_in['ctobssim']

    seed = int(counter)*10

    # PARAMETERS FROM THE CTOBSSIM
    sim_t_min = u.Quantity(ctobss_params['time']['t_min']).to_value(u.s)
    sim_t_max = u.Quantity(ctobss_params['time']['t_max']).to_value(u.s)
    sim_e_min = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV)
    sim_e_max = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV)
    sim_rad = ctobss_params['radius']

    models = sim_in['source']
    source_type = models['type']

    if source_type == "GRB":
        phase_path = "/" + models['phase']
    elif source_type == "GW":
        phase_path = ""

    output_path = create_path(sim_in['output']['path'] + phase_path + '/' + src_name)

    save_simulation = ctobss_params['save_simulation']

    with open(f"{output_path}/GRB-{src_name}_seed-{seed}.txt", "w") as f:
        f.write(f"GRB,seed,time_start,time_end,sigma_lima,sqrt_TS_onoff,sqrt_TS_std\n")
        # VISIBILITY PART
        # choose between AUTO mode (use visibility) and MANUAL mode (manually insert IRF)
        simulation_mode = sim_in['IRF']['mode']

        if simulation_mode == "auto":
            print("using visibility to get IRFs")

            # GRB information from the fits header
            ra = fits_header_0['RA']
            dec = fits_header_0['DEC']
            t0 = Time(fits_header_0['GRBJD'])

            irf_dict = sim_in['IRF']
            site = irf_dict['site']
            obs_condition = Observability(site=site)
            obs_condition.set_irf(irf_dict)

            t_zero_mode = ctobss_params['time']['t_zero'].lower()

            if t_zero_mode == "VIS":
                # check if the source is visible one day after the onset of the source
                print("time starts when source becomes visible")
                obs_condition.Proposal_obTime = 86400
                condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0)

            elif t_zero_mode == "ONSET":
                print("time starts from the onset of the GRB")
                condition_check = obs_condition.check(RA=ra, DEC=dec, t_start=t0, t_min=sim_t_min, t_max=sim_t_max)

            else:
                print(f"Choose some proper mode between 'VIS' and 'ONSET'. {t_zero_mode} is not a valid one.")
                sys.exit()

            # NO IRF in AUTO mode ==> No simulation! == EXIT!
            if len(condition_check) == 0:
                f.write(f"{src_name},{seed}, -1, -1, -1, -1, -1\n")
                sys.exit()

        elif simulation_mode == "manual":
            print("manual picking IRF")

            # find proper IRF name
            irf = IRFPicker(sim_in, ctools_pipe_path)
            name_irf = irf.irf_pick()

            backgrounds_path = create_path(ctobss_params['bckgrnd_path'])
            fits_background_list = glob.glob(
                f"{backgrounds_path}/{irf.prod_number}_{irf.prod_version}_{name_irf}/background*.fits")

            if len(fits_background_list) == 0:
                print(f"No background for IRF {name_irf}")
                sys.exit()

            fits_background_list = sorted(fits_background_list, key=sort_background)
            background_fits = fits_background_list[int(counter) - 1]
            obs_back = gammalib.GCTAObservation(background_fits)

        else:
            print(f"wrong input for IRF - mode. Input is {simulation_mode}. Use 'auto' or 'manual' instead")
            sys.exit()

        if irf.prod_number == "3b" and irf.prod_version == 0:
            caldb = "prod3b"
        else:
            caldb = f'prod{irf.prod_number}-v{irf.prod_version}'

        # source simulation
        sim = ctools.ctobssim()
        sim['inmodel'] = model_xml
        sim['caldb'] = caldb
        sim['irf'] = name_irf
        sim['ra'] = 0.0
        sim['dec'] = 0.0
        sim['rad'] = sim_rad
        sim['tmin'] = sim_t_min
        sim['tmax'] = sim_t_max
        sim['emin'] = sim_e_min
        sim['emax'] = sim_e_max
        sim['seed'] = seed
        sim.run()

        obs = sim.obs()

        # # move the source photons from closer to (RA,DEC)=(0,0), where the background is located
        # for event in obs[0].events():
        #     # ra_evt = event.dir().dir().ra()
        #     dec_evt = event.dir().dir().dec()
        #     ra_evt_deg = event.dir().dir().ra_deg()
        #     dec_evt_deg = event.dir().dir().dec_deg()
        #
        #     ra_corrected = (ra_evt_deg - ra_pointing)*np.cos(dec_evt)
        #     dec_corrected = dec_evt_deg - dec_pointing
        #     event.dir().dir().radec_deg(ra_corrected, dec_corrected)

        # append all background events to GRB ones ==> there's just one observation and not two
        for event in obs_back.events():
            obs[0].events().append(event)

        # ctselect to save data on disk
        if save_simulation:
            event_list_path = create_path(f"{ctobss_params['output_path']}/{src_name}/")
            #obs.save(f"{event_list_path}/event_list_source-{src_name}_seed-{seed:03}.fits")

            select_time = ctools.ctselect(obs)
            select_time['rad'] = sim_rad
            select_time['tmin'] = sim_t_min
            select_time['tmax'] = sim_t_max
            select_time['emin'] = sim_e_min
            select_time['emax'] = sim_e_max
            select_time['outobs'] = f"{event_list_path}/event_list_source-{src_name}_{seed:03}.fits"
            select_time.run()
            sys.exit()

        # delete all 70+ models from the obs def file...not needed any more
        obs.models(gammalib.GModels())

        # CTSELECT
        select_time = sim_in['ctselect']['time_cut']
        slices = int(select_time['t_slices'])

        if slices == 0:
            times = [sim_t_min, sim_t_max]
            times_start = times[:-1]
            times_end = times[1:]
        elif slices > 0:
            time_mode = select_time['mode']
            if time_mode == "log":
                times = np.logspace(np.log10(sim_t_min), np.log10(sim_t_max), slices + 1, endpoint=True)
            elif time_mode == "lin":
                times = np.linspace(sim_t_min, sim_t_max, slices + 1, endpoint=True)
            else:
                print(f"{time_mode} not valid. Use 'log' or 'lin' ")
                sys.exit()

            if select_time['obs_mode'] == "iter":
                times_start = times[:-1]
                times_end = times[1:]
            elif select_time['obs_mode'] == "cumul":
                times_start = np.repeat(times[0], slices)         # this is to use the same array structure for the loop
                times_end = times[1:]
            elif select_time['obs_mode'] == "all":
                begins, ends = np.meshgrid(times[:-1], times[1:])
                mask_times = begins < ends
                times_start = begins[mask_times].ravel()
                times_end = ends[mask_times].ravel()
            else:
                print(f"obs_mode: {select_time['obs_mode']} not supported")
                sys.exit()

        else:
            print(f"value {slices} not supported...check yaml file")
            sys.exit()

        # ------------------------------------
        # ----- TIME LOOP STARTS HERE --------
        # ------------------------------------

        ctlike_mode = sim_in['detection']
        mode_1 = ctlike_mode['counts']
        mode_2 = ctlike_mode['ctlike-onoff']
        mode_3 = ctlike_mode['ctlike-std']

        for t_in, t_end in zip(times_start, times_end):
            sigma_onoff = 0
            sqrt_ts_like_onoff = 0
            sqrt_ts_like_std = 0
            print("-----------------------------")
            print(f"t_in: {t_in:.2f}, t_end: {t_end:.2f}")

            # different ctlikes (onoff or std) need different files.
            # will be appended here and used later on for the final likelihood
            dict_obs_select_time = {}

            # perform time selection for this specific time bin
            select_time = ctools.ctselect(obs)
            select_time['rad'] = sim_rad
            select_time['tmin'] = t_in
            select_time['tmax'] = t_end
            select_time['emin'] = sim_e_min
            select_time['emax'] = sim_e_max
            select_time.run()

            if mode_1:
                fits_temp_title = f"skymap_{seed}_{t_in:.2f}_{t_end:.2f}.fits"
                pars_counts = ctlike_mode['pars_counts']
                scale = float(pars_counts['scale'])
                npix = 2*int(sim_rad/scale)
                skymap = ctools.ctskymap(select_time.obs().copy())
                skymap['emin'] = sim_e_min
                skymap['emax'] = sim_e_max
                skymap['nxpix'] = npix
                skymap['nypix'] = npix
                skymap['binsz'] = scale
                skymap['proj'] = 'TAN'
                skymap['coordsys'] = 'CEL'
                skymap['xref'] = 0
                skymap['yref'] = 0
                skymap['bkgsubtract'] = 'RING'
                skymap['roiradius'] = pars_counts['roiradius']
                skymap['inradius'] = pars_counts['inradius']
                skymap['outradius'] = pars_counts['outradius']
                skymap['iterations'] = pars_counts['iterations']
                skymap['threshold'] = pars_counts['threshold']
                skymap['outmap'] = fits_temp_title
                skymap.execute()

                input_fits = fits.open(fits_temp_title)
                datain = input_fits[2].data
                datain[np.isnan(datain)] = 0.0
                datain[np.isinf(datain)] = 0.0

                sigma_onoff = np.max(datain)
                os.remove(fits_temp_title)

            if mode_3:
                dict_obs_select_time['std'] = select_time.obs().copy()

            if mode_2:
                onoff_time_sel = cscripts.csphagen(select_time.obs().copy())
                onoff_time_sel['inmodel'] = 'NONE'
                onoff_time_sel['ebinalg'] = 'LOG'
                onoff_time_sel['emin'] = sim_e_min
                onoff_time_sel['emax'] = sim_e_max
                onoff_time_sel['enumbins'] = 30
                onoff_time_sel['coordsys'] = 'CEL'
                onoff_time_sel['ra'] = 0.0
                onoff_time_sel['dec'] = 0.5
                onoff_time_sel['rad'] = 0.2
                onoff_time_sel['bkgmethod'] = 'REFLECTED'
                onoff_time_sel['use_model_bkg'] = False
                onoff_time_sel['stack'] = False
                onoff_time_sel.run()

                dict_obs_select_time['onoff'] = onoff_time_sel.obs().copy()

                del onoff_time_sel

                # print(f"sigma ON/OFF: {sigma_onoff:.2f}")

            if mode_2 or mode_3:

                # Low Energy PL fitting
                # to be saved in this dict
                dict_pl_ctlike_out = {}

                e_min_pl_ctlike = 0.030
                e_max_pl_ctlike = 0.080

                # simple ctobssim copy and select for ctlike-std
                select_pl_ctlike = ctools.ctselect(select_time.obs().copy())
                select_pl_ctlike['rad'] = 3
                select_pl_ctlike['tmin'] = t_in
                select_pl_ctlike['tmax'] = t_end
                select_pl_ctlike['emin'] = e_min_pl_ctlike
                select_pl_ctlike['emax'] = e_max_pl_ctlike
                select_pl_ctlike.run()

                # create test source
                src_dir = gammalib.GSkyDir()
                src_dir.radec_deg(0, 0.5)
                spatial = gammalib.GModelSpatialPointSource(src_dir)

                # create and append source spectral model
                spectral = gammalib.GModelSpectralPlaw()
                spectral['Prefactor'].value(5.5e-16)
                spectral['Prefactor'].scale(1e-16)
                spectral['Index'].value(-2.6)
                spectral['Index'].scale(-1.0)
                spectral['PivotEnergy'].value(50000)
                spectral['PivotEnergy'].scale(1e3)
                model_src = gammalib.GModelSky(spatial, spectral)
                model_src.name('PL_fit_temp')
                model_src.tscalc(True)

                spectral_back = gammalib.GModelSpectralPlaw()
                spectral_back['Prefactor'].value(1.0)
                spectral_back['Prefactor'].scale(1.0)
                spectral_back['Index'].value(0)
                spectral_back['PivotEnergy'].value(300000)
                spectral_back['PivotEnergy'].scale(1e6)

                if mode_2:
                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTAOnOff')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())

                    onoff_pl_ctlike_lima = cscripts.csphagen(select_pl_ctlike.obs().copy())
                    onoff_pl_ctlike_lima['inmodel'] = 'NONE'
                    onoff_pl_ctlike_lima['ebinalg'] = 'LOG'
                    onoff_pl_ctlike_lima['emin'] = e_min_pl_ctlike
                    onoff_pl_ctlike_lima['emax'] = e_max_pl_ctlike
                    onoff_pl_ctlike_lima['enumbins'] = 30
                    onoff_pl_ctlike_lima['coordsys'] = 'CEL'
                    onoff_pl_ctlike_lima['ra'] = 0.0
                    onoff_pl_ctlike_lima['dec'] = 0.5
                    onoff_pl_ctlike_lima['rad'] = 0.2
                    onoff_pl_ctlike_lima['bkgmethod'] = 'REFLECTED'
                    onoff_pl_ctlike_lima['use_model_bkg'] = False
                    onoff_pl_ctlike_lima['stack'] = False
                    onoff_pl_ctlike_lima.run()

                    onoff_pl_ctlike_lima.obs().models(gammalib.GModels())
                    onoff_pl_ctlike_lima.obs().models().append(model_src.copy())
                    onoff_pl_ctlike_lima.obs().models().append(back_model.copy())

                    like_pl = ctools.ctlike(onoff_pl_ctlike_lima.obs())
                    like_pl['refit'] = True
                    like_pl.run()
                    dict_pl_ctlike_out['onoff'] = like_pl.obs().copy()
                    del onoff_pl_ctlike_lima
                    del like_pl

                if mode_3:
                    models_ctlike_std = gammalib.GModels()
                    models_ctlike_std.append(model_src.copy())
                    back_model = gammalib.GCTAModelIrfBackground()
                    back_model.instruments('CTA')
                    back_model.name('Background')
                    back_model.spectral(spectral_back.copy())
                    models_ctlike_std.append(back_model)

                    # save models
                    xmlmodel_PL_ctlike_std = 'test_model_PL_ctlike_std.xml'
                    models_ctlike_std.save(xmlmodel_PL_ctlike_std)
                    del models_ctlike_std

                    like_pl = ctools.ctlike(select_pl_ctlike.obs().copy())
                    like_pl['inmodel'] = xmlmodel_PL_ctlike_std
                    like_pl['refit'] = True
                    like_pl.run()
                    dict_pl_ctlike_out['std'] = like_pl.obs().copy()
                    del like_pl

                del spatial
                del spectral
                del model_src
                del select_pl_ctlike

                # EXTENDED CTLIKE
                for key in dict_obs_select_time.keys():
                    likelihood_pl_out = dict_pl_ctlike_out[key]
                    selected_data = dict_obs_select_time[key]

                    pref_out_pl = likelihood_pl_out.models()[0]['Prefactor'].value()
                    index_out_pl = likelihood_pl_out.models()[0]['Index'].value()
                    pivot_out_pl = likelihood_pl_out.models()[0]['PivotEnergy'].value()

                    expplaw = gammalib.GModelSpectralExpPlaw()
                    expplaw['Prefactor'].value(pref_out_pl)
                    expplaw['Index'].value(index_out_pl)
                    expplaw['PivotEnergy'].value(pivot_out_pl)
                    expplaw['CutoffEnergy'].value(80e3)

                    if key == "onoff":
                        selected_data.models()[0].name(src_name)
                        selected_data.models()[0].tscalc(True)
                        selected_data.models()[0].spectral(expplaw.copy())

                        like = ctools.ctlike(selected_data)
                        like['refit'] = True
                        like.run()
                        ts = like.obs().models()[0].ts()
                        if ts > 0:
                            sqrt_ts_like_onoff = np.sqrt(like.obs().models()[0].ts())
                        else:
                            sqrt_ts_like_onoff = 0

                        del like

                    if key == "std":
                        models_fit_ctlike = gammalib.GModels()

                        # create test source
                        src_dir = gammalib.GSkyDir()
                        src_dir.radec_deg(0, 0.5)
                        spatial = gammalib.GModelSpatialPointSource(src_dir)

                        # append spatial and spectral models
                        model_src = gammalib.GModelSky(spatial, expplaw.copy())
                        model_src.name('Source_fit')
                        model_src.tscalc(True)
                        models_fit_ctlike.append(model_src)

                        # create and append background
                        back_model = gammalib.GCTAModelIrfBackground()
                        back_model.instruments('CTA')
                        back_model.name('Background')
                        spectral_back = gammalib.GModelSpectralPlaw()
                        spectral_back['Prefactor'].value(1.0)
                        spectral_back['Prefactor'].scale(1.0)
                        spectral_back['Index'].value(0)
                        spectral_back['PivotEnergy'].value(300000)
                        spectral_back['PivotEnergy'].scale(1e6)
                        back_model.spectral(spectral_back)
                        models_fit_ctlike.append(back_model)

                        # save models
                        input_ctlike_xml = "model_GRB_fit_ctlike_in.xml"
                        models_fit_ctlike.save(input_ctlike_xml)
                        del models_fit_ctlike

                        like = ctools.ctlike(selected_data)
                        like['inmodel'] = input_ctlike_xml
                        like['refit'] = True
                        like.run()
                        ts = like.obs().models()[0].ts()
                        if ts > 0:
                            sqrt_ts_like_std = np.sqrt(like.obs().models()[0].ts())
                        else:
                            sqrt_ts_like_std = 0

                        del like

                    # E_cut_off = like.obs().models()[0]['CutoffEnergy'].value()
                    # E_cut_off_error = like.obs().models()[0]['CutoffEnergy'].error()

                    # print(f"sqrt(TS) {key}: {np.sqrt(ts_like):.2f}")
                    # print(f"E_cut_off {key}: {E_cut_off:.2f} +- {E_cut_off_error:.2f}")
                del dict_pl_ctlike_out

            f.write(f"{src_name},{seed},{t_in:.2f},{t_end:.2f},{sigma_onoff:.2f},{sqrt_ts_like_onoff:.2f},{sqrt_ts_like_std:.2f}\n")
            del dict_obs_select_time
            del select_time
Ejemplo n.º 18
0
def skymap(inobs,
           outmap,
           npix,
           reso,
           cra,
           cdec,
           emin=1e-2,
           emax=1e+3,
           caldb=None,
           irf=None,
           bkgsubtract='NONE',
           roiradius=0.1,
           inradius=1.0,
           outradius=2.0,
           iterations=3,
           threshold=3,
           logfile=None,
           silent=False):
    """
    Compute sky map.
    See http://cta.irap.omp.eu/ctools/users/reference_manual/ctskymap.html

    Parameters
    ----------
    - inobs (str): input Event file or xml listing event file
    - outmap (str): full path to fits skymap
    - npix (int): number of pixels
    - reso(float): map resolution in deg
    - cra,cdec (float): map RA,Dec center in deg
    - emin,emax (float) min and max energy considered in TeV
    - caldb (str): calibration database
    - irf (str): instrument response function
    - bkgsubtract (str): 'NONE', 'IRF', or 'RING' method for 
    background subtraction
    - roiradius (float): for each pixel, radius around which on region is considered
    - inradius (float): inner radius for ring off region
    - outradius (float): outer radius for ring off region
    - iterations (int): number of iteration for flagging source regions
    - threshold (float): S/N threshold for flagging source region
    - silent (bool): print information or not

    Outputs
    --------
    - create sky map fits

    """

    smap = ctools.ctskymap()

    smap['inobs'] = inobs
    if caldb is not None: smap['caldb'] = caldb
    if irf is not None: smap['irf'] = irf
    smap['inmap'] = 'NONE'
    smap['outmap'] = outmap
    smap['emin'] = emin
    smap['emax'] = emax
    smap['usepnt'] = False
    smap['nxpix'] = npix
    smap['nypix'] = npix
    smap['binsz'] = reso
    smap['coordsys'] = 'CEL'
    smap['proj'] = 'TAN'
    smap['xref'] = cra
    smap['yref'] = cdec

    smap['bkgsubtract'] = bkgsubtract
    smap['roiradius'] = roiradius
    smap['inradius'] = inradius
    smap['outradius'] = outradius
    smap['iterations'] = iterations
    smap['threshold'] = threshold
    smap['inexclusion'] = 'NONE'
    smap['usefft'] = True
    if logfile is not None: smap['logfile'] = logfile

    if logfile is not None: smap.logFileOpen()
    smap.execute()
    if logfile is not None: smap.logFileClose()

    if not silent:
        print(smap)
        print('')

    return smap
Ejemplo n.º 19
0
    def _test_python(self):
        """
        Test ctskymap from Python
        """
        # Allocate ctskymap
        skymap = ctools.ctskymap()

        # Check that empty ctskymap tool holds a map that has no pixels
        self._check_map(skymap.skymap(), nx=0, ny=0)

        # Check that saving does not nothing
        skymap['outmap']  = 'ctskymap_py0.fits'
        skymap['logfile'] = 'ctskymap_py0.log'
        skymap.logFileOpen()
        skymap.save()
        self.test_assert(not os.path.isfile('ctskymap_py0.fits'),
             'Check that no sky map has been created')

        # Check that clearing does not lead to an exception or segfault
        skymap.clear()

        # Now set ctskymap parameters
        skymap['inobs']       = self._events
        skymap['emin']        = 0.1
        skymap['emax']        = 100
        skymap['nxpix']       = 20
        skymap['nypix']       = 20
        skymap['binsz']       = 0.2
        skymap['coordsys']    = 'CEL'
        skymap['proj']        = 'CAR'
        skymap['xref']        = 83.63
        skymap['yref']        = 22.01
        skymap['bkgsubtract'] = 'NONE'
        skymap['outmap']      = 'ctskymap_py1.fits'
        skymap['logfile']     = 'ctskymap_py1.log'
        skymap['chatter']     = 2

        # Run ctskymap tool
        skymap.logFileOpen()   # Make sure we get a log file
        skymap.run()
        skymap.save()

        # Check result file
        self._check_result_file('ctskymap_py1.fits')

        # Copy ctskymap tool
        cpy_skymap = skymap.copy()

        # Check sky map of ctskymap copy
        self._check_map(cpy_skymap.skymap())

        # Execute copy of ctskymap tool again, now with a higher chatter
        # level than before
        cpy_skymap['usepnt']   = True
        cpy_skymap['emin']     = 0.2
        cpy_skymap['emax']     = 150.0
        cpy_skymap['coordsys'] = 'GAL'
        cpy_skymap['proj']     = 'CAR'
        cpy_skymap['outmap']   = 'ctskymap_py2.fits'
        cpy_skymap['logfile']  = 'ctskymap_py2.log'
        cpy_skymap['publish']  = True
        cpy_skymap['chatter']  = 3
        cpy_skymap.logFileOpen()  # Needed to get a new log file
        cpy_skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py2.fits')

        # Now clear copy of ctskymap tool
        cpy_skymap.clear()

        # Check that cleared ctskymap tool holds a map that has no pixels
        self._check_map(cpy_skymap.skymap(), nx=0, ny=0)

        # Get mixed observation container
        obs = self._obs_mixed()

        # Allocate ctskymap tool from observation container
        skymap = ctools.ctskymap(obs)
        skymap['emin']        = 0.1
        skymap['emax']        = 100
        skymap['nxpix']       = 20
        skymap['nypix']       = 20
        skymap['binsz']       = 0.2
        skymap['coordsys']    = 'GAL'
        skymap['proj']        = 'CAR'
        skymap['xref']        = 184.5575
        skymap['yref']        =  -5.7844
        skymap['bkgsubtract'] = 'NONE'
        skymap['outmap']      = 'ctskymap_py3.fits'
        skymap['logfile']     = 'ctskymap_py3.log'
        skymap['chatter']     = 4

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py3.fits')

        # Publish with name
        skymap.publish('My sky map')

        # ================================
        # TEST IRF BACKGROUND SUBTRACTION
        # ================================
        
        # Allocate ctskymap tool from observation container
        skymap = ctools.ctskymap(obs)
        skymap['emin']        = 0.1
        skymap['emax']        = 100
        skymap['nxpix']       = 20
        skymap['nypix']       = 20
        skymap['binsz']       = 0.2
        skymap['coordsys']    = 'GAL'
        skymap['proj']        = 'CAR'
        skymap['xref']        = 184.5575
        skymap['yref']        =  -5.7844
        skymap['bkgsubtract'] = 'IRF'
        skymap['caldb']       = self._caldb
        skymap['irf']         = self._irf
        skymap['outmap']      = 'ctskymap_py4.fits'
        skymap['logfile']     = 'ctskymap_py4.log'
        skymap['chatter']     = 4

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py4.fits')
        self._check_result_file('ctskymap_py4.fits[BACKGROUND]')
        self._check_result_file('ctskymap_py4.fits[SIGNIFICANCE]')

        # Test RING background subtraction
        skymap = ctools.ctskymap(obs)
        skymap['emin']        = 0.1
        skymap['emax']        = 100
        skymap['nxpix']       = 10  # Make region smaller since the 'RING'
        skymap['nypix']       = 10  # method takes longer to run than 'IRF'
        skymap['binsz']       = 0.2
        skymap['coordsys']    = 'CEL'
        skymap['proj']        = 'CAR'
        skymap['xref']        = 83.63
        skymap['yref']        = 22.01
        skymap['bkgsubtract'] = 'RING'
        skymap['roiradius']   = 0.1
        skymap['inradius']    = 0.6
        skymap['outradius']   = 0.8
        skymap['inexclusion'] = 'NONE'
        skymap['caldb']       = self._caldb
        skymap['irf']         = self._irf
        skymap['outmap']      = 'ctskymap_py5.fits'
        skymap['logfile']     = 'ctskymap_py5.log'
        skymap['chatter']     = 4

        # Execute tool
        skymap.logFileOpen()  # Needed to get a new log file
        skymap.execute()

        # Check result file
        self._check_result_file('ctskymap_py5.fits', 10, 10)
        self._check_result_file('ctskymap_py5.fits[BACKGROUND]', 10, 10)
        self._check_result_file('ctskymap_py5.fits[SIGNIFICANCE]', 10, 10)

        # Return
        return
Ejemplo n.º 20
0
import gammalib
import ctools
import cscripts

debug = True

skymap = ctools.ctskymap()
skymap['inobs']        = 'obs_selected.xml'
skymap['outmap']       = 'skymap_bkgsubtract.fits'
skymap['emin']         = 0.1
skymap['emax']         = 100.0
skymap['nxpix']        = 600
skymap['nypix']        = 600
skymap['binsz']        = 0.02
skymap['coordsys']     = 'GAL'
skymap['proj']         = 'CAR'
skymap['xref']         = 0.0
skymap['yref']         = 0.0
skymap['bkgsubtract']  = 'IRF'
skymap["debug"]        = debug
skymap.execute()

detect = cscripts.cssrcdetect()
detect["inmap"]        = 'skymap_bkgsubtract.fits'
detect["outmodel"]     = 'stacked_models.xml'
detect["outds9file"]   = 'ds9_1.reg'
detect["srcmodel"]     = 'POINT'
detect["bkgmodel"]     = 'CUBE'
detect["threshold"]    = 15.0
detect["debug"]        = debug
detect.execute()