<commit_before># pulsar.py

# Class containing pulsar data from timing package [tempo2/PINT].

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

import enterprise
import numpy as np
from ephem import Ecliptic, Equatorial
import os
import json
from enterprise.signals import utils

try:
    import cPickle as pickle
except:
    import pickle

try:
    import libstempo as t2
except ImportError:
    print('Ooh, no libstempo?')
    t2 = None

try:
    import pint
    import pint.toa as toa
    import pint.models.model_builder as mb
    from pint.models import TimingModel
    from pint.residuals import resids
except ImportError:
    print('No PINT? Meh...')
    pint = None

import astropy.units as u

if pint is None and t2 is None:
    err_msg = 'Must have either PINT or libstempo timing package installed'
    raise ImportError(err_msg)


def get_maxobs(timfile):
    """Utility function to return number of lines in tim file.

    :param timfile:
        Full path to tim-file. For tim-files that use INCLUDEs this
        should be the base tim file.

    :returns: Number of lines in tim-file
    """

    maxobs = 0
    with open(timfile) as tfile:
        flines = tfile.readlines()
        lines = [ln for ln in flines if not ln.startswith('C')]
        if any(map(lambda x: 'INCLUDE' in x, lines)):
            for line in filter(lambda x: 'INCLUDE' in x, lines):
                maxobs += get_maxobs(line.split()[-1])
        else:
            maxobs = sum(1 for line in lines if line.rstrip('\n'))
    return maxobs


class BasePulsar(object):
    """Abstract Base Class for Pulsar objects."""

    def _get_pdist(self):
        dfile = enterprise.__path__[0] + '/datafiles/pulsar_distances.json'
        with open(dfile, 'r') as fl:
            pdict = json.load(fl)

        if self.name[0] not in ['J', 'B']:
            if 'J' + self.name in pdict:
                pdist = tuple(pdict.get('J'+self.name))
            elif 'B' + self.name in pdict:
                pdist = tuple(pdict.get('B'+self.name))
        else:
            pdist = tuple(pdict.get(self.name, (1.0, 0.2)))

        if pdist == (1.0, 0.2):
            # TODO: should use logging here
            msg = 'WARNING: Could not find pulsar distance for '
            msg += 'PSR {0}.'.format(self.name)
            msg += ' Setting value to 1 with 20% uncertainty.'
            print(msg)
        return pdist

    def _get_radec_from_ecliptic(self, elong, elat):
        # convert via pyephem
        try:
            ec = Ecliptic(elong, elat)

            # check for B name
            if 'B' in self.name:
                epoch = '1950'
            else:
                epoch = '2000'
            eq = Equatorial(ec, epoch=str(epoch))
            raj = np.double(eq.ra)
            decj = np.double(eq.dec)

        # TODO: should use logging here
        except TypeError:
            msg = 'WARNING: Cannot fine sky location coordinates '
            msg += 'for PSR {0}. '.format(self.name)
            msg += 'Setting values to 0.0'
            print(msg)
            raj = 0.0<commit_msg>used logger for warning messages in pulsar.py (#192)<commit_after># pulsar.py

# Class containing pulsar data from timing package [tempo2/PINT].

from __future__ import (absolute_import, division,
                        print_function, unicode_literals)

import enterprise
import numpy as np
from ephem import Ecliptic, Equatorial
import os
import json
from enterprise.signals import utils
import logging

try:
    import cPickle as pickle
except:
    import pickle

try:
    import libstempo as t2
except ImportError:
    print('Ooh, no libstempo?')
    t2 = None

try:
    import pint
    import pint.toa as toa
    import pint.models.model_builder as mb
    from pint.models import TimingModel
    from pint.residuals import resids
except ImportError:
    print('No PINT? Meh...')
    pint = None

import astropy.units as u

if pint is None and t2 is None:
    err_msg = 'Must have either PINT or libstempo timing package installed'
    raise ImportError(err_msg)

logging.basicConfig(format='%(levelname)s: %(name)s: %(message)s',
                    level=logging.INFO)
logger = logging.getLogger(__name__)


def get_maxobs(timfile):
    """Utility function to return number of lines in tim file.

    :param timfile:
        Full path to tim-file. For tim-files that use INCLUDEs this
        should be the base tim file.

    :returns: Number of lines in tim-file
    """

    maxobs = 0
    with open(timfile) as tfile:
        flines = tfile.readlines()
        lines = [ln for ln in flines if not ln.startswith('C')]
        if any(map(lambda x: 'INCLUDE' in x, lines)):
            for line in filter(lambda x: 'INCLUDE' in x, lines):
                maxobs += get_maxobs(line.split()[-1])
        else:
            maxobs = sum(1 for line in lines if line.rstrip('\n'))
    return maxobs


class BasePulsar(object):
    """Abstract Base Class for Pulsar objects."""

    def _get_pdist(self):
        dfile = enterprise.__path__[0] + '/datafiles/pulsar_distances.json'
        with open(dfile, 'r') as fl:
            pdict = json.load(fl)

        if self.name[0] not in ['J', 'B']:
            if 'J' + self.name in pdict:
                pdist = tuple(pdict.get('J'+self.name))
            elif 'B' + self.name in pdict:
                pdist = tuple(pdict.get('B'+self.name))
        else:
            pdist = tuple(pdict.get(self.name, (1.0, 0.2)))

        if pdist == (1.0, 0.2):
            msg = 'WARNING: Could not find pulsar distance for '
            msg += 'PSR {0}.'.format(self.name)
            msg += ' Setting value to 1 with 20% uncertainty.'
            logger.warning(msg)
        return pdist

    def _get_radec_from_ecliptic(self, elong, elat):
        # convert via pyephem
        try:
            ec = Ecliptic(elong, elat)

            # check for B name
            if 'B' in self.name:
                epoch = '1950'
            else:
                epoch = '2000'
            eq = Equatorial(ec, epoch=str(epoch))
            raj = np.double(eq.ra)
            decj = np.double(eq.dec)

        except TypeError:
            msg = 'WARNING: Cannot fine sky location coordinates '
            msg += 'for PSR {0}. '.format(self.name)
            msg += 'Setting values to 0.0'
            logger.warning(msg)
            raj = 0.0
{'fraction_non_alphanumeric': 0.10067009505999688, 'fraction_numerical': 0.011064360292971793, 'mean_word_length': 2.059103908484271, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, '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': '7108805', 'n_tokens_mistral': 2062, 'n_tokens_neox': 1965, 'n_words': 650}