<commit_before># Author <NAME> <EMAIL>
# script to look for near source of given RA and DEC in a catalog
# begun Oct 2009

import sys, getopt, subprocess, xml.dom.minidom, os, time, math
from xml.sax import ContentHandler, make_parser
from math import *
import sys
import pyfits,xml.dom.minidom,numpy

import enrico.environ as env
from enrico.config import get_config


def fluxScale(flux_value):
    return 10**math.floor(math.log10(flux_value)+0.5)

def meanEnergy(emin, emax, index_value):
    x=emax/emin;
    if index_value==-2.0:
        eflux = emax*math.log(x)/(x-1)
    elif index_value==-1.0:
        eflux = emin*(x-1)/math.log(x)
    else:
        eflux = emin*(index_value+1)/(index_value+2)*\
                (x**(index_value+2)-1)/(x**(index_value+1)-1)
    return eflux

def addParameter(el, name, free, value, scale, min, max):
    doc = el.ownerDocument
    param = doc.createElement('parameter')
    param.setAttribute('name',name)
    param.setAttribute('free','%d'%free)
    param.setAttribute('scale','%g'%scale)
    param.setAttribute('value','%g'%value)
    param.setAttribute('max','%g'%max)
    param.setAttribute('min','%g'%min)
    el.appendChild(param)

def addDiffusePL(lib, file, free=1, value=1.0, scale=1.0, max=10.0, min=1.0,
               name = 'EG_v02'):
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('file',file)
    spec.setAttribute('type','FileFunction')
    addParameter(spec, 'Normalization', free, 1, 1, 0.001, 1000)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','ConstantValue')
    addParameter(spatial,'Value',0,1,1.0,0.0,10.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addGalprop(lib, file, free=1, value=1.0, scale=1.0, max=10.0, min=.010,
               name = 'GAL_v02'):
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','ConstantValue')
    addParameter(spec, 'Value', free, value, scale, min, max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('file',file)
    spatial.setAttribute('type','MapCubeFunction')
    addParameter(spatial, 'Normalization', 0, 1, 1, 0.001, 1000)
    src.appendChild(spatial)
    lib.appendChild(src)


def addDiffusePL2(lib, file, name ,emin=200, emax=3e5,
                   flux_free=1, flux_value=1.6e-8, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):

    elim_min = 30;
    elim_max = 300000;
    if emin<elim_min:
        elim_min = emin
    if emax>elim_max:
        elim_max = emax
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale
 
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')   
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw2')
    addParameter(spec,'Integral',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'LowerLimit',0,emin,1.0,elim_min,elim_max)
    addParameter(spec,'UpperLimit',0,emax,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('file',file)
    spatial.setAttribute('type','SpatialMap')
    addParameter(spatial, 'Prefactor', 0, 1, 1, 0.001, 1000)
    src.appendChild(spatial)
    lib.appendChild(src)

#def addPSPowerLaw1(lib, name, ra, dec, emin=200, emax=100000, eflux=0,
#                   flux_free=1, flux_value=1e-9, flux_scale=0,
#                   flux_max=1000.0, flux_min=1e-5,
#                   index_free=1, index_value=-2.0,
#                   index_min=-5.0, index_max=-0.5):
def addPSPowerLaw1(lib, name, ra, dec, eflux=0,
                   flux_free=1, flux_value=1e-9, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):
    elim_min = 30;
    elim_max = 300000;
#    if emin<elim_min:
#        elim_min = emin
#    if emax>elim_max:
#        elim_max = emax 
#    if eflux==0:
#        eflux =2e5# meanEnergy(emin,emax,index_value)
#        flux_value *= (eflux/100.0)**index_value
#    else :
#	flux_value *= 0.1*(eflux/100.0)**index_value
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale        
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw')
    addParameter(spec,'Prefactor',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'Scale',0,eflux,1.0,elim_min,elim_max)
#    addParameter(spec,'Scale',0,2e5,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','SkyDirFunction')
    addParameter(spatial,'RA',0,ra,1.0,-360.0,360.0)
    addParameter(spatial,'DEC',0,dec,1.0,-90.0,90.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addPSPowerLaw2(lib, name, ra, dec, emin=200, emax=3e5,
                   flux_free=1, flux_value=1.6e-8, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):
    elim_min = 30;
    elim_max = 300000;
    if emin<elim_min:
        elim_min = emin
    if emax>elim_max:
        elim_max = emax
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw2')
    addParameter(spec,'Integral',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'LowerLimit',0,emin,1.0,elim_min,elim_max)
    addParameter(spec,'UpperLimit',0,emax,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','SkyDirFunction')
    addParameter(spatial,'RA',0,ra,1.0,-360.0,360.0)
    addParameter(spatial,'DEC',0,dec,1.0,-90.0,90.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addPSLogparabola(lib, name, ra, dec,  enorm=300,
                   norm_free=1, norm_value=1e-9, norm_scale=0,
                   norm_max=1000.0, norm_min=1e-5,
                   alpha_free=1, alpha_value=1.0,
                   alpha_min=.5, alpha_max=5.,
                   beta_free=1, beta_value=1.0,
                   beta_min=0.5, beta_max=5.0):
    elim_min = 30;
    elim_max = 300000;

    if enorm==0:
        enorm =2e5# meanEnergy(emin,emax,index_value)
        norm_value *= (enorm/100.0)**alpha_value
    if norm_scale == 0:
        norm_scale=fluxScale(norm_value)
    norm_value /= norm_scale        
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','LogParabola')
    addParameter(spec,'norm',
                 norm_free,norm_value,norm_scale,norm_min,norm_max)
    addParameter(spec,'alpha',alpha_free,alpha_value,1.0,alpha_min,alpha_max)
    addParameter(spec,'Eb',0,enorm,1.0,elim_min,elim_max)
    addParameter(spec,'beta',beta_free,beta_value,1.0,beta_min,beta_max)
#    addParameter(spec,'Scale',0,2e5,1.0,elim_min,elim_max)<commit_msg>fix the limits of the beta parameter fir the logparabola model<commit_after># Author <NAME> <EMAIL>
# script to look for near source of given RA and DEC in a catalog
# begun Oct 2009

import sys, getopt, subprocess, xml.dom.minidom, os, time, math
from xml.sax import ContentHandler, make_parser
from math import *
import sys
import pyfits,xml.dom.minidom,numpy

import enrico.environ as env
from enrico.config import get_config


def fluxScale(flux_value):
    return 10**math.floor(math.log10(flux_value)+0.5)

def meanEnergy(emin, emax, index_value):
    x=emax/emin;
    if index_value==-2.0:
        eflux = emax*math.log(x)/(x-1)
    elif index_value==-1.0:
        eflux = emin*(x-1)/math.log(x)
    else:
        eflux = emin*(index_value+1)/(index_value+2)*\
                (x**(index_value+2)-1)/(x**(index_value+1)-1)
    return eflux

def addParameter(el, name, free, value, scale, min, max):
    doc = el.ownerDocument
    param = doc.createElement('parameter')
    param.setAttribute('name',name)
    param.setAttribute('free','%d'%free)
    param.setAttribute('scale','%g'%scale)
    param.setAttribute('value','%g'%value)
    param.setAttribute('max','%g'%max)
    param.setAttribute('min','%g'%min)
    el.appendChild(param)

def addDiffusePL(lib, file, free=1, value=1.0, scale=1.0, max=10.0, min=1.0,
               name = 'EG_v02'):
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('file',file)
    spec.setAttribute('type','FileFunction')
    addParameter(spec, 'Normalization', free, 1, 1, 0.001, 1000)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','ConstantValue')
    addParameter(spatial,'Value',0,1,1.0,0.0,10.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addGalprop(lib, file, free=1, value=1.0, scale=1.0, max=10.0, min=.010,
               name = 'GAL_v02'):
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','ConstantValue')
    addParameter(spec, 'Value', free, value, scale, min, max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('file',file)
    spatial.setAttribute('type','MapCubeFunction')
    addParameter(spatial, 'Normalization', 0, 1, 1, 0.001, 1000)
    src.appendChild(spatial)
    lib.appendChild(src)


def addDiffusePL2(lib, file, name ,emin=200, emax=3e5,
                   flux_free=1, flux_value=1.6e-8, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):

    elim_min = 30;
    elim_max = 300000;
    if emin<elim_min:
        elim_min = emin
    if emax>elim_max:
        elim_max = emax
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale
 
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','DiffuseSource')   
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw2')
    addParameter(spec,'Integral',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'LowerLimit',0,emin,1.0,elim_min,elim_max)
    addParameter(spec,'UpperLimit',0,emax,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('file',file)
    spatial.setAttribute('type','SpatialMap')
    addParameter(spatial, 'Prefactor', 0, 1, 1, 0.001, 1000)
    src.appendChild(spatial)
    lib.appendChild(src)

#def addPSPowerLaw1(lib, name, ra, dec, emin=200, emax=100000, eflux=0,
#                   flux_free=1, flux_value=1e-9, flux_scale=0,
#                   flux_max=1000.0, flux_min=1e-5,
#                   index_free=1, index_value=-2.0,
#                   index_min=-5.0, index_max=-0.5):
def addPSPowerLaw1(lib, name, ra, dec, eflux=0,
                   flux_free=1, flux_value=1e-9, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):
    elim_min = 30;
    elim_max = 300000;
#    if emin<elim_min:
#        elim_min = emin
#    if emax>elim_max:
#        elim_max = emax 
#    if eflux==0:
#        eflux =2e5# meanEnergy(emin,emax,index_value)
#        flux_value *= (eflux/100.0)**index_value
#    else :
#	flux_value *= 0.1*(eflux/100.0)**index_value
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale        
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw')
    addParameter(spec,'Prefactor',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'Scale',0,eflux,1.0,elim_min,elim_max)
#    addParameter(spec,'Scale',0,2e5,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','SkyDirFunction')
    addParameter(spatial,'RA',0,ra,1.0,-360.0,360.0)
    addParameter(spatial,'DEC',0,dec,1.0,-90.0,90.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addPSPowerLaw2(lib, name, ra, dec, emin=200, emax=3e5,
                   flux_free=1, flux_value=1.6e-8, flux_scale=0,
                   flux_max=1000.0, flux_min=1e-5,
                   index_free=1, index_value=-2.0,
                   index_min=-5.0, index_max=-0.5):
    elim_min = 30;
    elim_max = 300000;
    if emin<elim_min:
        elim_min = emin
    if emax>elim_max:
        elim_max = emax
    if flux_scale == 0:
        flux_scale=fluxScale(flux_value)
    flux_value /= flux_scale
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','PowerLaw2')
    addParameter(spec,'Integral',
                 flux_free,flux_value,flux_scale,flux_min,flux_max)
    addParameter(spec,'Index',index_free,index_value,1.0,index_min,index_max)
    addParameter(spec,'LowerLimit',0,emin,1.0,elim_min,elim_max)
    addParameter(spec,'UpperLimit',0,emax,1.0,elim_min,elim_max)
    src.appendChild(spec)
    spatial = doc.createElement('spatialModel')
    spatial.setAttribute('type','SkyDirFunction')
    addParameter(spatial,'RA',0,ra,1.0,-360.0,360.0)
    addParameter(spatial,'DEC',0,dec,1.0,-90.0,90.0)
    src.appendChild(spatial)
    lib.appendChild(src)

def addPSLogparabola(lib, name, ra, dec,  enorm=300,
                   norm_free=1, norm_value=1e-9, norm_scale=0,
                   norm_max=1000.0, norm_min=1e-5,
                   alpha_free=1, alpha_value=1.0,
                   alpha_min=.5, alpha_max=5.,
                   beta_free=1, beta_value=1.0,
                   beta_min=0.05, beta_max=5.0):
    elim_min = 30;
    elim_max = 300000;

    if enorm==0:
        enorm =2e5# meanEnergy(emin,emax,index_value)
        norm_value *= (enorm/100.0)**alpha_value
    if norm_scale == 0:
        norm_scale=fluxScale(norm_value)
    norm_value /= norm_scale        
    doc = lib.ownerDocument
    src = doc.createElement('source')
    src.setAttribute('name',name)
    src.setAttribute('type','PointSource')
    spec = doc.createElement('spectrum')
    spec.setAttribute('type','LogParabola')
    addParameter(spec,'norm',
                 norm_free,norm_value,norm_scale,norm_min,norm_max)
    addParameter(spec,'alpha',alpha_free,alpha_value,1.0,alpha_min,alpha_max)
    addParameter(spec,'Eb',0,enorm,1.0,elim_min,elim_max)
    addParameter(spec,'beta',beta_free,beta_value,1.0,beta_min,beta_max)
#    addParameter(spec,'Scale',0,2e5,1.0,elim_min,elim_max)
{'fraction_non_alphanumeric': 0.1442272672089353, 'fraction_numerical': 0.04243049654000243, 'mean_word_length': 3.3230123327210705, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '214972', 'n_tokens_mistral': 6223, 'n_tokens_neox': 5860, 'n_words': 839}