<commit_before>"""
Convert PWN to XML format.
"""
import numpy as np
import astropy.units as u
from astropy.table import Table
from gammapy.utils.coordinates import galactic as compute_galactic_coordinates

SOURCE_LIBRARY_TEMPLATE = """\
<?xml version="1.0" standalone="no"?>

<source_library title="CTA 1DC simulated supernova remnants">

{xml_sources}

</source_library>
"""

SOURCE_TEMPLATE = """
 <source name="{source_name}" type="ExtendedSource">
{xml_spectral}
{xml_spatial}
 </source>
"""

SPATIAL_TEMPLATE = """\
    <spatialModel type="RadialGaussian">
        <parameter name="GLON" value="{glon:.5f}" unit="deg"/>
        <parameter name="GLAT" value="{glat:.5f}" unit="deg"/>
        <parameter name="Sigma" value="{sigma:.5f}" unit="deg"/>
    </spatialModel>"""


SPECTRUM_TEMPLATE = """\
    <spectrum type="LogParabola">
        <parameter name="Prefactor" value="{norm:.3e}" unit="MeV-1 cm-2 s-1"/>
        <parameter name="Index" value="{index:.3f}" />
        <parameter name="Curvature" value="{curvature:.3f}"/>
        <parameter name="PivotEnergy" value="{energy:.5f}" unit="MeV"/>
      </node>
"""


# def make_table_spectrum_xml(sed_energy, sed_dnde):
#     xml_spectrum_nodes = ''
#     for energy, dnde in zip(sed_energy, sed_dnde):
#         xml_spectrum_nodes += SPECTRUM_NODE_TEMPLATE.format(
#             energy=energy,
#             dnde=dnde,
#         )
#
#     return SPECTRUM_TEMPLATE.format(xml_spectrum_nodes=xml_spectrum_nodes)


def make_pwn_xml(table):
    remove_or_not = 0
    remove_or_not_2 = 0
    remove_or_not_3 = 0
    remove_or_not_4 = 0
    remove_or_not_5 = 0
    xml_sources = ''
    #for row in table[:2]:
    for row in table:
        if (row['spec_norm_crab']>0.1):
            #print(row['spec_norm_crab'])
            continue;


        if (row['spec_norm_crab'] > 0.08):
            if (remove_or_not < 2):
                remove_or_not+=1
                print(remove_or_not, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.08 and row['spec_norm_crab'] > 0.06):
            if (remove_or_not_2 < 2):
                remove_or_not_2+=1
                print(remove_or_not_2, row.index, row['spec_norm_crab'])
                continue;


        if (row['spec_norm_crab'] < 0.06 and row['spec_norm_crab'] > 0.04):
            if (remove_or_not_3 < 2):
                remove_or_not_3 += 1
                print(remove_or_not_3, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.04 and row['spec_norm_crab'] > 0.02):
            if (remove_or_not_4 < 2):
                remove_or_not_4 += 1
                print(remove_or_not_4, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.02 and row['spec_norm_crab'] > 0.01):
            if (remove_or_not_5 < 2):
                remove_or_not_5 += 1
                print(remove_or_not_5, row.index, row['spec_norm_crab'])
                continue;


        xml_spectral = SPECTRUM_TEMPLATE.format(
            norm=row['spec_norm'],
            index=row['spec_alpha'],
            curvature = row['spec_beta'],
            energy = 1e06
            )

        # Arbitrary assumption on width of the  shell
        # TODO: ask Pierre what we should put!
        # Also check if radius is inner or outer!
        #width_fraction = 0
        #radius = u.Quantity(row['size'], 'arcmin').to('deg')<commit_msg>removing bright PWN<commit_after>"""
Convert PWN to XML format.
"""
import numpy as np
import astropy.units as u
from astropy.table import Table
from gammapy.utils.coordinates import galactic as compute_galactic_coordinates

SOURCE_LIBRARY_TEMPLATE = """\
<?xml version="1.0" standalone="no"?>

<source_library title="CTA 1DC simulated supernova remnants">

{xml_sources}

</source_library>
"""

SOURCE_TEMPLATE = """
 <source name="{source_name}" type="ExtendedSource">
{xml_spectral}
{xml_spatial}
 </source>
"""

SPATIAL_TEMPLATE = """\
    <spatialModel type="RadialGaussian">
        <parameter name="GLON" value="{glon:.5f}" unit="deg"/>
        <parameter name="GLAT" value="{glat:.5f}" unit="deg"/>
        <parameter name="Sigma" value="{sigma:.5f}" unit="deg"/>
    </spatialModel>"""


SPECTRUM_TEMPLATE = """\
    <spectrum type="LogParabola">
        <parameter name="Prefactor" value="{norm:.3e}" unit="MeV-1 cm-2 s-1"/>
        <parameter name="Index" value="{index:.3f}" />
        <parameter name="Curvature" value="{curvature:.3f}"/>
        <parameter name="PivotEnergy" value="{energy:.5f}" unit="MeV"/>
      </node>
"""


# def make_table_spectrum_xml(sed_energy, sed_dnde):
#     xml_spectrum_nodes = ''
#     for energy, dnde in zip(sed_energy, sed_dnde):
#         xml_spectrum_nodes += SPECTRUM_NODE_TEMPLATE.format(
#             energy=energy,
#             dnde=dnde,
#         )
#
#     return SPECTRUM_TEMPLATE.format(xml_spectrum_nodes=xml_spectrum_nodes)


def make_pwn_xml(table):
    remove_or_not = 0
    remove_or_not_2 = 0
    remove_or_not_3 = 0
    remove_or_not_4 = 0
    remove_or_not_5 = 0
    xml_sources = ''

    #for row in table[:2]:
    for row in table:
        if (row['spec_norm_crab']>0.1):
            #print(row['spec_norm_crab'])
            continue;


        if (row['spec_norm_crab'] > 0.08):
            if (remove_or_not < 2):
                remove_or_not+=1
                print(remove_or_not, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.08 and row['spec_norm_crab'] > 0.06):
            if (remove_or_not_2 < 2):
                remove_or_not_2+=1
                print(remove_or_not_2, row.index, row['spec_norm_crab'])
                continue;


        if (row['spec_norm_crab'] < 0.06 and row['spec_norm_crab'] > 0.04):
            if (remove_or_not_3 < 2):
                remove_or_not_3 += 1
                print(remove_or_not_3, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.04 and row['spec_norm_crab'] > 0.02):
            if (remove_or_not_4 < 2):
                remove_or_not_4 += 1
                print(remove_or_not_4, row.index, row['spec_norm_crab'])
                continue;

        if (row['spec_norm_crab'] < 0.02 and row['spec_norm_crab'] > 0.01):
            if (remove_or_not_5 < 2):
                remove_or_not_5 += 1
                print(remove_or_not_5, row.index, row['spec_norm_crab'])
                continue;

        xml_spectral = SPECTRUM_TEMPLATE.format(
            norm=row['spec_norm'],
            index=row['spec_alpha'],
            curvature = row['spec_beta'],
            energy = 1e06
            )

        # Arbitrary assumption on width of the  shell
        # TODO: ask Pierre what we should put!
        # Also check if radius is inner or outer!
        #width_fraction = 0
        #radius = u.Quantity(row['size'], 'arcmin').to('deg')
{'fraction_non_alphanumeric': 0.1365473441108545, 'fraction_numerical': 0.022517321016166283, 'mean_word_length': 2.4731829573934836, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 2, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '3600099', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2314, 'n_words': 451}