<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
:py:mod:`usertools.py` - User tools
-----------------------------------

User tools to download, process, and plot the :py:class:`everest` light curves.

'''

from __future__ import division, print_function, absolute_import, unicode_literals
from . import __develop__, __version__
from .config import EVEREST_DAT, EVEREST_SRC, MAST_ROOT, EVEREST_FITS
from .kernels import KernelModels
from .data import Campaign, GetK2Data
from .utils import PlotBounds, MADOutliers, RMS
from .utils import PadWithZeros
from .selector import Selector
from .ccd import CCD
from scipy.ndimage import zoom
from scipy.signal import savgol_filter
import k2plr as kplr
import george
import numpy as np
import matplotlib.pyplot as pl
from matplotlib.widgets import Slider, Button, RadioButtons
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
import glob
import os, subprocess
try:
  import pyfits
except ImportError:
  try:
    import astropy.io.fits as pyfits
  except ImportError:
    raise Exception('Please install the `pyfits` package.')
from tempfile import NamedTemporaryFile
import six
from six.moves import urllib
import shutil

def _EverestVersion():
  '''
  Returns the current :py:mod:`everest` version on MAST.
  
  '''
  
  url = MAST_ROOT + 'version.txt'
  r = urllib.request.Request(url)
  handler = urllib.request.urlopen(r)
  code = handler.getcode()
  if int(code) != 200:
    raise Exception("Error code {0} for URL '{1}'".format(code, url))
  data = handler.read().decode('utf-8')
  if data.endswith('\n'):
    data = data[:-1]
  return data

def _DownloadFITSFile(EPIC, clobber = False):
  '''
  Download a given :py:mod:`everest` FITS file from MAST.
  Returns the full local path to the FITS file.
  
  :param bool clobber: If `True`, download and overwrite existing files. Default `False`
  
  '''
      
  # Get the campaign
  campaign = Campaign(EPIC)  
  
  if not __develop__:
  
    # Get the url
    mast_version = _EverestVersion()
    url = MAST_ROOT + 'c%02d/' % campaign + ('%09d' % EPIC)[:4] + '00000/' + \
          'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, mast_version)
  
    # Get the local file name
    filename = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                            ('%09d' % EPIC)[:4] + '00000',
                            'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, mast_version))
    if not os.path.exists(os.path.dirname(filename)):
      os.makedirs(os.path.dirname(filename))
  
    # Download the data
    r = urllib.request.Request(url)
    handler = urllib.request.urlopen(r)
    code = handler.getcode()
    if int(code) != 200:
      raise Exception("Error code {0} for URL '{1}'".format(code, url))
    data = handler.read()
    
    # Atomically save to disk
    f = NamedTemporaryFile("wb", delete=False)
    f.write(data)
    f.flush()
    os.fsync(f.fileno())
    f.close()
    shutil.move(f.name, filename)
    
  else:
    
    # This section is for pre-publication/development use only!
    if EVEREST_FITS is None:
      raise Exception("The code has not yet been published to MAST. If you have access" + 
                      "to a local or remote directory containing the FITS files, specify its" +
                      "full path in the $EVEREST_FITS environment variable.")
    
    # Get the url
    url = EVEREST_FITS + 'c%02d/' % campaign + ('%09d' % EPIC)[:4] + '00000/' + \
          'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, __version__)
  
    # Get the local file name
    filename = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                            ('%09d' % EPIC)[:4] + '00000',
                            'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, __version__))
    if not os.path.exists(os.path.dirname(filename)):
      os.makedirs(os.path.dirname(filename))
  
    # Download the data
    subprocess.call(['scp', url, filename])
  
  return filename

def _GetFITSFile(EPIC, clobber = False):
  '''
  Returns the path to a given :py:mod:`everest` FITS file.
  In case of multiple versions, returns the file corresponding to the
  latest :py:mod:`everest` version. In case a local copy does not exist,
  downloads the most recent version from MAST.

  :param bool clobber: If `True`, download and overwrite existing files. Default `False`

  '''

  campaign = Campaign(EPIC)
  path = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                     ('%09d' % EPIC)[:4] + '00000',
                      'hlsp_everest_k2_llc_%d-c%02d_kepler_v*.fits' % (EPIC, campaign))
  files = glob.glob(path)<commit_msg>os.path.join<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
:py:mod:`usertools.py` - User tools
-----------------------------------

User tools to download, process, and plot the :py:class:`everest` light curves.

'''

from __future__ import division, print_function, absolute_import, unicode_literals
from . import __develop__, __version__
from .config import EVEREST_DAT, EVEREST_SRC, MAST_ROOT, EVEREST_FITS
from .kernels import KernelModels
from .data import Campaign, GetK2Data
from .utils import PlotBounds, MADOutliers, RMS
from .utils import PadWithZeros
from .selector import Selector
from .ccd import CCD
from scipy.ndimage import zoom
from scipy.signal import savgol_filter
import k2plr as kplr
import george
import numpy as np
import matplotlib.pyplot as pl
from matplotlib.widgets import Slider, Button, RadioButtons
import matplotlib.ticker as ticker
from mpl_toolkits.axes_grid1 import make_axes_locatable
import glob
import os, subprocess
try:
  import pyfits
except ImportError:
  try:
    import astropy.io.fits as pyfits
  except ImportError:
    raise Exception('Please install the `pyfits` package.')
from tempfile import NamedTemporaryFile
import six
from six.moves import urllib
import shutil

def _EverestVersion():
  '''
  Returns the current :py:mod:`everest` version on MAST.
  
  '''
  
  url = MAST_ROOT + 'version.txt'
  r = urllib.request.Request(url)
  handler = urllib.request.urlopen(r)
  code = handler.getcode()
  if int(code) != 200:
    raise Exception("Error code {0} for URL '{1}'".format(code, url))
  data = handler.read().decode('utf-8')
  if data.endswith('\n'):
    data = data[:-1]
  return data

def _DownloadFITSFile(EPIC, clobber = False):
  '''
  Download a given :py:mod:`everest` FITS file from MAST.
  Returns the full local path to the FITS file.
  
  :param bool clobber: If `True`, download and overwrite existing files. Default `False`
  
  '''
      
  # Get the campaign
  campaign = Campaign(EPIC)  
  
  if not __develop__:
  
    # Get the url
    mast_version = _EverestVersion()
    url = MAST_ROOT + 'c%02d/' % campaign + ('%09d' % EPIC)[:4] + '00000/' + \
          'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, mast_version)
  
    # Get the local file name
    filename = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                            ('%09d' % EPIC)[:4] + '00000',
                            'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, mast_version))
    if not os.path.exists(os.path.dirname(filename)):
      os.makedirs(os.path.dirname(filename))
  
    # Download the data
    r = urllib.request.Request(url)
    handler = urllib.request.urlopen(r)
    code = handler.getcode()
    if int(code) != 200:
      raise Exception("Error code {0} for URL '{1}'".format(code, url))
    data = handler.read()
    
    # Atomically save to disk
    f = NamedTemporaryFile("wb", delete=False)
    f.write(data)
    f.flush()
    os.fsync(f.fileno())
    f.close()
    shutil.move(f.name, filename)
    
  else:
    
    # This section is for pre-publication/development use only!
    if EVEREST_FITS is None:
      raise Exception("The code has not yet been published to MAST. If you have access " + 
                      "to a local or remote directory containing the FITS files, specify its " +
                      "full path in the $EVEREST_FITS environment variable.")
    
    # Get the url
    url = os.path.join(EVEREST_FITS, 'c%02d' % campaign, ('%09d' % EPIC)[:4] + '00000',
          'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, __version__))
  
    # Get the local file name
    filename = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                            ('%09d' % EPIC)[:4] + '00000',
                            'hlsp_everest_k2_llc_%d-c%02d_kepler_v%s.fits' % (EPIC, campaign, __version__))
    if not os.path.exists(os.path.dirname(filename)):
      os.makedirs(os.path.dirname(filename))
  
    # Download the data
    subprocess.call(['scp', url, filename])
  
  return filename

def _GetFITSFile(EPIC, clobber = False):
  '''
  Returns the path to a given :py:mod:`everest` FITS file.
  In case of multiple versions, returns the file corresponding to the
  latest :py:mod:`everest` version. In case a local copy does not exist,
  downloads the most recent version from MAST.

  :param bool clobber: If `True`, download and overwrite existing files. Default `False`

  '''

  campaign = Campaign(EPIC)
  path = os.path.join(EVEREST_DAT, 'fits', 'c%02d' % campaign, 
                     ('%09d' % EPIC)[:4] + '00000',
                      'hlsp_everest_k2_llc_%d-c%02d_kepler_v*.fits' % (EPIC, campaign))
  files = glob.glob(path)
{'fraction_non_alphanumeric': 0.12341370558375635, 'fraction_numerical': 0.01713197969543147, 'mean_word_length': 3.0311167945439044, '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': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1416725', 'n_tokens_mistral': 3348, 'n_tokens_neox': 3200, 'n_words': 881}