def __init__(self, *argv): """ Constructor. Parameters ---------- argv : list of str List of IRAF command line parameter strings of the form ``parameter=3``. Raises ------ TypeError An invalid number of command line arguments was provided. """ # Set name and version self._name = 'csworkflow' self._version = '1.0.0' # Set members self._workflow = gammalib.GXml() self._actors = [] # Initialise application by calling the appropriate class constructor self._init_cscript(argv) # Return return
def report_one_test_result(file): """ Report one test result Parameters ---------- file : str Test result file """ # Open XML file xml = gammalib.GXml(file) # Determine number of testsuites num = int(xml.element('testsuites').elements('testsuite')) # Loop over all test suites for i in range(num): # Get test suite suite = xml.element('testsuites > testsuite[%d]' % i) # Report test suite result report_one_test_suite(suite) # Return return
def _write_new_xmlevent_from_obsid(self, xmlin, xmlout, obsID): """ Read the xml file gathering the event list, remove the event files that are not selected, and write a new xml file. Parameters ---------- - xmlin (str): input xml file - xmlout (str): output xml file - obsID (list): list of str Outputs ------- - The new xml file is writen """ xml = gammalib.GXml(xmlin) obslist = xml.element('observation_list') obsid_in = [] for i in range(len(obslist))[::-1]: if obslist[i].attribute('id') not in obsID: obslist.remove(i) else: obsid_in.append(obslist[i].attribute('id')) for i in range(len(obsID)): if obsID[i] not in obsid_in: print('WARNING: Event file with obsID ' + obsID[i] + ' does not exist. It is ignored.') xml.save(xmlout)
def _correct_eventfile_names(self, xmlfile, prefix='Events'): """ Change the event filename and associated xml file by naming them using the obsid Parameters ---------- - xmlfile (str): the xml file name """ xml = gammalib.GXml(xmlfile) obslist = xml.element('observation_list') for i in range(len(obslist)): obs = obslist[i] obsid = obs.attribute('id') # make sure the EventList key exist killnum = None for j in range(len(obs)): if obs[j].attribute('name') == 'EventList': killnum = j # In case there is one EventList, move the file and rename the xml if killnum is not None: file_in = obs[killnum].attribute('file') file_out = os.path.dirname( file_in) + '/' + prefix + obsid + '.fits' os.rename(file_in, file_out) obs.remove(killnum) obs.append('parameter name="EventList" file="' + file_out + '"') xml.save(xmlfile)
def _get_parameters(self): # Load workflow XML file xmlfile = self['inflow'].filename() self._workflow = gammalib.GXml(xmlfile.url()) # Write input parameters into logger self._log_parameters(gammalib.TERSE) # Return return
def create_xml(): """ Illustrates the creation of an XML document. This function creates a simple XML document that illustrates how an XML document can be constructed with GammaLib. In this example, the document creation is done linearly, in the order in which the nodes are written into the document. The append() method is used to append nodes to the XML document root and to child nodes. You will see two flavours of the append() method: - one that takes a text string as argument, which serves to define the name and the attribtes of the child element - one that takes a child node as argument, which serves to append either a text leaf using the GXmlText constructor, or other markup nodes, such as processing instructions (GXmlPI) or comments (GXmlComment) """ # Allocate XML document xml = gammalib.GXml() # Append first child element to XML document root. The first child # element contains a single child which is an empty-element tag. level1 = xml.append('element type="Measurement"') level2 = level1.append('parameter name="Flux" value="1.0"') # Append second child element to XML document root. The second child # element contains a single child named list, which contains two # childs that each contain a text leaf. level1 = xml.append('element') level2 = level1.append('list') level3 = level2.append('string') level4 = level3.append(gammalib.GXmlText("This is a text")) level3 = level2.append('integer') level4 = level3.append(gammalib.GXmlText("17")) # Append third child element to XML document root. The third child # element contains a single Processing Instruction. level1 = xml.append('processing-instruction') level2 = level1.append( gammalib.GXmlPI('<?xml-stylesheet type="text/xsl"?>')) # Append forth child element to XML document root. The forth child # element contains two comments. The first comment is a single-line # comment while the second comment runs over multiple lines. level1 = xml.append('comment') level2 = level1.append(gammalib.GXmlComment('This is a comment')) level2 = level1.append(gammalib.GXmlComment('This is a\ntwo line comment')) # Return XML document return xml
def __init__(self, *argv): """ Constructor """ # Initialise application by calling the base class constructor self._init_cscript(self.__class__.__name__, ctools.__version__, argv) # Initialise some members self._obs = gammalib.GObservations() self._ebounds = gammalib.GEbounds() self._datapath = os.getenv('VHEFITS', '') self._prodname = '' self._xml = gammalib.GXml() self._models = gammalib.GModels() self._runlist = [] self._runlistfile = gammalib.GFilename() self._bkgpars = 0 self._master_indx = '' self._use_bkg_scale = False self._ev_hiera = [''] self._aeff_hiera = [''] self._psf_hiera = [''] self._bkg_hiera = [''] self._edisp_hiera = [''] self._bkg_mod_hiera = [''] self._bkg_gauss_norm = 1.0 self._bkg_gauss_index = 0.0 self._bkg_gauss_sigma = 1.0 self._bkg_aeff_index = 0.0 self._bkg_aeff_norm = 1.0 self._bkg_range_factor = 1.0 self._hdu_index = '' self._obs_index = '' self._subdir = '' self._debug = False # Initialise empty observation definition XML file self._xml.append( gammalib.GXmlElement('observation_list ' 'title="observation list"')) # Append an observation list to XML instance self._xml.append( gammalib.GXmlElement('observation_list title="observation list"')) self._xml_obslist = self._xml.element('observation_list', 0) # Return return
def __init__(self, *argv): """ Constructor. """ # Set name and version self._name = 'csiactobs' self._version = '1.1.0' # Initialise some members self._ebounds = gammalib.GEbounds() self._datapath = os.getenv('VHEFITS', '') self._inmodels = gammalib.GModels() self._prodname = '' self._xml = gammalib.GXml() self._models = gammalib.GModels() self._runlist = [] self._runlistfile = gammalib.GFilename() self._bkgpars = 0 self._outmodel = gammalib.GFilename() self._outobs = gammalib.GFilename() self._master_indx = '' self._use_bkg_scale = False self._ev_hiera = [''] self._aeff_hiera = [''] self._psf_hiera = [''] self._bkg_hiera = [''] self._edisp_hiera = [''] self._bkg_mod_hiera = [''] self._bkg_gauss_norm = 1.0 self._bkg_gauss_index = 0.0 self._bkg_gauss_sigma = 1.0 self._bkg_aeff_index = 0.0 self._bkg_aeff_norm = 1.0 self._bkg_range_factor = 1.0 self._hdu_index = '' self._obs_index = '' self._subdir = '' self._debug = False # Initialise application by calling the appropriate class # constructor. self._init_cscript(argv) # Return return
def create_html(): """ Illustrates how GammaLib can be used to create HTML documents. The HTML document is composed of a main element with name 'html'. This elements has two childs: a 'head' child for the header and a 'body' child for the page information. The header contains metadata in the 'meta' element and a page title defined by the 'title' element. The body contains arbitrary text. In this example there are two lines of text with different font formatting and a GammaLib icon that is placed in the top-right corner of the page. """ # Allocate XML document xml = gammalib.GXml() # Creates HTML base html = xml.append('html') # Writes header header = html.append('head') meta = header.append('meta content="text/html; charset=ISO-8859-1" ' 'http-equiv="content-type"') title = header.append('title') text = title.append(gammalib.GXmlText('GammaLib')) # Writes body body = html.append('body') image = body.append('img style="float: right;" alt="GammaLib" ' 'src="http://a.fsdn.com/allura/p/gammalib/icon"') big = image.append('big') bigger = big.append('big') span = bigger.append('span style="font-family: Arial; font-weight: bold;"') text = span.append(gammalib.GXmlText('Gammalib')) text = image.append(gammalib.GXmlText('<br>')) span = image.append('span style="font-family: Arial;"') text = span.append(gammalib.GXmlText('A versatile toolbox for the scientific analysis ' 'of astronomical gamma-ray data')) # Return XML document return xml
def _rm_source_xml(self, xmlin, xmlout, source): """ Read a model xml file, remove a given source, and write a new model file. Parameters ---------- - xmlin (str): input xml file - xmlout (str): output xml file - source (str): name of the source to remove Outputs ------- - The new xml file is writen """ xml = gammalib.GXml(xmlin) srclist = xml.element('source_library') for i in range(len(srclist))[::-1]: if srclist[i].attribute('name') == source: srclist.remove(i) xml.save(xmlout)
def __init__(self, *argv): """ Constructor. Parameters ---------- argv : list of str List of IRAF command line parameter strings of the form ``parameter=3``. 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) # Set members self._workflow = gammalib.GXml() self._actors = [] # Return return
def run_pipeline(obs, emin=0.1, emax=100.0, enumbins=20, nxpix=200, nypix=200, binsz=0.02, coordsys='CEL', proj='CAR', model='data/crab.xml', caldb='prod2', irf='South_0.5h', debug=False): """ Simulation and binned analysis pipeline Parameters ---------- obs : `~gammalib.GObservations` Observation container emin : float, optional Minimum energy (TeV) emax : float, optional Maximum energy (TeV) enumbins : int, optional Number of energy bins nxpix : int, optional Number of pixels in X axis nypix : int, optional Number of pixels in Y axis binsz : float, optional Pixel size (deg) coordsys : str, optional Coordinate system proj : str, optional Coordinate projection model : str, optional Model definition XML file caldb : str, optional Calibration database path irf : str, optional Instrument response function debug : bool, optional Debug function """ # Simulate events sim = ctools.ctobssim(obs) sim['debug'] = debug sim['outevents'] = 'obs.xml' sim.execute() # Bin events by looping over all observations in the container sim_obs = gammalib.GObservations('obs.xml') obs = gammalib.GObservations() for run in sim_obs: # Get event filename and set counts cube filename eventfile = run.eventfile().url() cubefile = 'cube_'+eventfile # Bin events for that observation bin = ctools.ctbin() bin['inobs'] = eventfile bin['outcube'] = cubefile bin['ebinalg'] = 'LOG' bin['emin'] = emin bin['emax'] = emax bin['enumbins'] = enumbins bin['nxpix'] = nxpix bin['nypix'] = nypix bin['binsz'] = binsz bin['coordsys'] = coordsys bin['usepnt'] = True bin['proj'] = proj bin.execute() # Set observation ID bin.obs()[0].id(cubefile) bin.obs()[0].eventfile(cubefile) # Append result to observations obs.extend(bin.obs()) # Save XML file xml = gammalib.GXml() obs.write(xml) xml.save('obs_cube.xml') # Perform maximum likelihood fitting like = ctools.ctlike() like['inobs'] = 'obs_cube.xml' like['inmodel'] = model like['outmodel'] = 'fit_results.xml' like['expcube'] = 'NONE' like['psfcube'] = 'NONE' like['bkgcube'] = 'NONE' like['caldb'] = caldb like['irf'] = irf like['debug'] = True # Switch this always on for results in console like.execute() # Return return
def collect_data(event_class): """ Collect data and write observation container Parameters ---------- event_class : dict Event class """ # Dump header print('Generate observation definition file for "%s" event class' % (event_class['name'])) # Get current working directory cwd = os.getcwd() # Set filenames inxml = 'gps_obs_%s_obsdef.xml' % (event_class['name']) outxml = 'gps_obs_%s.xml' % (event_class['name']) reldirname = '../data/%s' % (event_class['name']) absdirname = '$CTADATA/data/%s' % (event_class['name']) # Step into data directory os.chdir('gps/obs') # Create empty XML file xmlout = gammalib.GXml() list = xmlout.append('observation_list title="observation list"') # Load XML file xml = gammalib.GXml(inxml) # Get observation list obslist = xml.element('observation_list', 0) # Get number of observations nobs = obslist.elements('observation') # Loop over all observations for iobs in range(nobs): # Get current observation obs = obslist.element('observation', iobs) # Get observation ID id = obs.attribute('id') # Set filename filename = '%s/gps_%s_%s.fits.gz' % (reldirname, event_class['name'], id) # Search for a corresponding event file files = glob.glob(os.path.expandvars(filename)) # If we found one file then get the relative filename and append a # "EventList" parameter to the XML file if len(files) == 1: # Get event filename eventfile = '%s/%s' % (absdirname, os.path.basename(files[0])) # Append XML element obs.append('parameter name="EventList" file="%s"' % eventfile) # Copy element to output file list.append(obs) # ... otherwise notify that an event file is missing else: print('No event file for observation %s' % id) # Save XML file xmlout.save(outxml) # Step out of working directory os.chdir(cwd) # Return return
if not os.path.exists( PATH_OBS): #If the observation path doesn't exist, create it. os.makedirs(PATH_OBS) #Set Filenames wisely: suf = "" input_model = PATH_MODEL + 'test_ctools/sources/LMC_' + Component + suf + '.xml' cntcube = PATH_OBS + "cntcube_" + 'LMC_' + Component + suf + '.fits' modcube = PATH_OBS + "modcube_" + 'LMC_' + Component + suf + '.fits' outfile = PATH_OBS + 'observations_' + 'LMC_' + Component + '.xml' #List of Observations file that will be produced ('.xml') file = open(outfile, 'w') Obs_list = gammalib.GObservations() xml = gammalib.GXml(outfile) rndseed = random.randint(1, 300000) number = 1 for ra in ra_list: dec = dec_list[number - 1] # SIMULATE EACH OBSERVATION filename = 'events_' + 'LMC_' + Component + '_' + str( duration / 3600) + 'h_' + '0' + str(number) + '.fits' eventfile = gammalib.GFilename(filename) sim = ctools.ctobssim() sim["seed"] = number + rndseed sim["inmodel"] = input_model sim["outevents"] = filename
def collect_data(name, lc=True): """ Collect data and write observation container Parameters ---------- name : string Source name lc : bool, optional Use light curve """ # Dump header print('Generate observation definition file') # Enter working directory cwd = enter_wd(name) # Set filenames if lc: suffix = '' else: suffix = '_const' outxml = 'gps_obs_simulated%s.xml' % (suffix) # Continue only if XML file does not exist if not os.path.isfile(outxml): # Create empty XML file xmlout = gammalib.GXml() list = xmlout.append('observation_list title="observation list"') # Load XML file xml = gammalib.GXml('gps_obs_selected.xml') # Get observation list obslist = xml.element('observation_list', 0) # Get number of observations nobs = obslist.elements('observation') # Loop over all observations for iobs in range(nobs): # Get current observation obs = obslist.element('observation', iobs) # Get observation ID id = obs.attribute('id') # Search for a corresponding event file files = glob.glob( os.path.expandvars('data/*%s%s.fits.gz' % (id, suffix))) # If we found one file then get the relative filename and append a # "EventList" parameter to the XML file if len(files) == 1: # Get event filename eventfile = 'data/' + os.path.basename(files[0]) # Append XML element obs.append('parameter name="EventList" file="%s"' % eventfile) # Copy element to output file list.append(obs) # ... otherwise notify that an event file is missing else: print('No event file for observation %s' % id) # Save XML file xmlout.save(outxml) # Exit working directory exit_wd(cwd) # Return return
def get_pointings(filename): """ Extract pointings from XML file Parameters ---------- filename : str File name of observation definition XML file Returns ------- pnt : list of dict Pointings """ # Initialise pointings pnt = [] # Open XML file xml = gammalib.GXml(filename) # Get observation list obs = xml.element('observation_list') # Get number of observations nobs = obs.elements('observation') # Loop over observations for i in range(nobs): # Get observation run = obs.element('observation', i) # Get pointing parameter npars = run.elements('parameter') ra = None dec = None south = False evfile = None for k in range(npars): par = run.element('parameter', k) if par.attribute('name') == 'Pointing': ra = float(par.attribute('ra')) dec = float(par.attribute('dec')) elif par.attribute('name') == 'Calibration': if 'South' in par.attribute('response'): south = True else: south = False elif par.attribute('name') == 'EventList': evfile = par.attribute('file') # If no pointing was found then load observation if ra == None and evfile != None: cta = gammalib.GCTAObservation(gammalib.GFilename(evfile)) ra = cta.pointing().dir().ra_deg() dec = cta.pointing().dir().dec_deg() # Add valid pointing if ra != None: p = gammalib.GSkyDir() p.radec_deg(ra, dec) entry = { 'l': p.l_deg(), 'b': p.b_deg(), 'ra': ra, 'dec': dec, 'south': south } pnt.append(entry) # Return pointings return pnt
def sim_select_like(sim_yaml, jobs_in, model_xml, background_fits, counter): """ :param sim_yaml: :param jobs_in: :param model_xml: :param background_fits: :param counter: :return: """ #print("----------------------------") print(model_xml.split('/')[-1], counter) config_in = yaml.safe_load(open(jobs_in)) ctools_pipe_path = create_path(config_in['exe']['software_path']) sim_in = yaml.safe_load(open(sim_yaml)) ctobss_params = sim_in['ctobssim'] # find proper IRF name irf = IRFPicker(sim_in, ctools_pipe_path) name_irf = irf.irf_pick() if irf.prod_version == "3b" and irf.prod_number == 0: caldb = "prod3b" else: caldb = f'prod{irf.prod_number}-v{irf.prod_version}' # loading background (this is a way of doing it without saving any file) # output.save("name.xml") to save the file obs_def = gammalib.GObservations() background_id = f"{str(int(counter) + 1).zfill(6)}" output = gammalib.GXml() level0 = gammalib.GXmlElement('observation_list title="observation library"') level1 = gammalib.GXmlElement(f'observation name="name_source" id="{background_id}" instrument="CTA"') level2 = gammalib.GXmlElement(f'parameter name="EventList" file="{background_fits}"') level1.append(level2) level0.append(level1) output.append(level0) obs_def.read(output) seed = int(counter)*10 # do the simulation sim = ctools.ctobssim() sim['inmodel'] = model_xml sim['caldb'] = caldb sim['irf'] = name_irf sim['ra'] = 0 sim['dec'] = 0 sim['rad'] = ctobss_params['radius'] sim['tmin'] = u.Quantity(ctobss_params['time']['t_min']).to_value(u.s) sim['tmax'] = u.Quantity(ctobss_params['time']['t_max']).to_value(u.s) sim['emin'] = u.Quantity(ctobss_params['energy']['e_min']).to_value(u.TeV) sim['emax'] = u.Quantity(ctobss_params['energy']['e_max']).to_value(u.TeV) sim['seed'] = seed sim.run() obs_def.append(sim.obs()[0]) # for obs in obs_def: # print(obs.id(), obs.nobserved()) select = ctools.ctselect(obs_def) select['rad'] = 3 select['tmin'] = 0 select['tmax'] = 50 select['emin'] = 0.05 select['emax'] = 1.0 select.run()
#if ver != '': print(url.string()) # Return return # ======================== # # Main routine entry point # # ======================== # if __name__ == '__main__': nomefile = str(sys.argv[1]) #f = open(nomefile, 'r') # Allocate XML document xml = gammalib.GXml() # main branch sourcelibrary = xml.append('source_library title="source library"') # if argv has verbose attribute, set verbose on global ver if len(sys.argv) == 3: ver = 'yes' else: ver = '' # read lines