Repository: lightkurve/lightkurve
Issue: Unable to Open Fits Lightcurve Files

I get this error 
`KeyError: "Key 'SAP_QUALITY' does not exist."`
when trying to read in a lightcurve .fits file using the path initialization, i.e. 
`lc = KeplerLightCurveFile('my_lightcurve.fits')`

Changed files:
lightkurve/lightcurvefile.py, lightkurve/tests/test_lightcurve.py, lightkurve/lightcurve.py

diff --git a/lightkurve/lightcurve.py b/lightkurve/lightcurve.py
index 0f8f1dc7..bac5b8dc 100644
--- a/lightkurve/lightcurve.py
+++ b/lightkurve/lightcurve.py
@@ -927,12 +927,12 @@ class LightCurve(object):
                                            **kwargs)
 
     def to_fits(self, path=None, overwrite=False, **extra_data):
-        """Writes the KeplerLightCurve to a fits file.
+        """Writes the LightCurve to a FITS file.
 
         Parameters
         ----------
         path : string, default None
-            File path, if None returns an astropy.io.fits object.
+            File path, if `None` returns an astropy.io.fits.HDUList object.
         overwrite : bool
             Whether or not to overwrite the file
         extra_data : dict
@@ -1012,6 +1012,10 @@ class LightCurve(object):
                     cols.append(fits.Column(name='{}'.format(kw).upper(),
                                             format=typedir[type(extra_data[kw][0])],
                                             array=extra_data[kw]))
+            if 'SAP_QUALITY' not in extra_data:
+                cols.append(fits.Column(name='SAP_QUALITY',
+                                        format='J',
+                                        array=np.zeros(len(self.flux))))
 
             coldefs = fits.ColDefs(cols)
             hdu = fits.BinTableHDU.from_columns(coldefs)
@@ -1023,12 +1027,6 @@ class LightCurve(object):
             return fits.HDUList([_make_primary_hdu(extra_data=extra_data),
                                  _make_lightcurve_extension(extra_data=extra_data)])
 
-        def _header_template(extension):
-            """Returns a template `fits.Header` object for a given extension."""
-            template_fn = os.path.join(PACKAGEDIR, "data",
-                                       "lc-ext{}-header.txt".format(extension))
-            return fits.Header.fromtextfile(template_fn)
-
         hdu = _hdulist(**extra_data)
         if path is not None:
             hdu.writeto(path, overwrite=overwrite, checksum=True)
@@ -1208,12 +1206,12 @@ class KeplerLightCurve(LightCurve):
         return super(KeplerLightCurve, self).to_pandas(columns=columns)
 
     def to_fits(self, path=None, overwrite=False, **extra_data):
-        """Export the KeplerLightCurve as an astropy.io.fits object.
+        """Writes the KeplerLightCurve to a FITS file.
 
         Parameters
         ----------
         path : string, default None
-            File path, if None returns an astropy.io.fits object.
+            File path, if `None` returns an astropy.io.fits.HDUList object.
         overwrite : bool
             Whether or not to overwrite the file
         extra_data : dict
@@ -1238,7 +1236,8 @@ class KeplerLightCurve(LightCurve):
             'RA_OBJ': self.ra,
             'DEC_OBJ': self.dec,
             'EQUINOX': 2000,
-            'DATE-OBS': Time(self.time[0]+2454833., format=('jd')).isot}
+            'DATE-OBS': Time(self.time[0]+2454833., format=('jd')).isot,
+            'SAP_QUALITY': self.quality}
         for kw in kepler_specific_data:
             if ~np.asarray([kw.lower == k.lower() for k in extra_data]).any():
                 extra_data[kw] = kepler_specific_data[kw]
diff --git a/lightkurve/lightcurvefile.py b/lightkurve/lightcurvefile.py
index 2f06744c..ffc5c45e 100644
--- a/lightkurve/lightcurvefile.py
+++ b/lightkurve/lightcurvefile.py
@@ -25,14 +25,19 @@ class LightCurveFile(object):
 
     Parameters
     ----------
-    path : str
+    path : str or `astropy.io.fits.HDUList` object
         Local path or remote url of a lightcurve FITS file.
+        Also accepts a FITS file object already opened using AstroPy.
     kwargs : dict
         Keyword arguments to be passed to astropy.io.fits.open.
     """
     def __init__(self, path, **kwargs):
-        self.path = path
-        self.hdu = pyfits.open(self.path, **kwargs)
+        if isinstance(path, pyfits.HDUList):
+            self.path = None
+            self.hdu = path
+        else:
+            self.path = path
+            self.hdu = pyfits.open(self.path, **kwargs)
 
     def header(self, ext=0):
         """Header of the object at extension `ext`"""
diff --git a/lightkurve/tests/test_lightcurve.py b/lightkurve/tests/test_lightcurve.py
index 347b5205..844f2a41 100644
--- a/lightkurve/tests/test_lightcurve.py
+++ b/lightkurve/tests/test_lightcurve.py
@@ -320,6 +320,7 @@ def test_to_fits():
     """Test the KeplerLightCurve.to_fits() method"""
     lcf = KeplerLightCurveFile(TABBY_Q8)
     hdu = lcf.PDCSAP_FLUX.to_fits()
+    lcf_new = KeplerLightCurveFile(hdu)  # Regression test for #233
     assert type(hdu).__name__ is 'HDUList'
     assert len(hdu) == 2
     assert hdu[0].header['EXTNAME'] == 'PRIMARY'
@@ -329,6 +330,7 @@ def test_to_fits():
     assert hdu[1].header['TTYPE3'] == 'FLUX_ERR'
     assert hdu[1].header['TTYPE4'] == 'CADENCENO'
     hdu = LightCurve([0, 1, 2, 3, 4], [1, 1, 1, 1, 1]).to_fits()
+    lcf_new = KeplerLightCurveFile(hdu)  # Regression test for #233
     assert hdu[0].header['EXTNAME'] == 'PRIMARY'
     assert hdu[1].header['EXTNAME'] == 'LIGHTCURVE'
     assert hdu[1].header['TTYPE1'] == 'TIME'