Example #1
0
    def load_lightcurve(self, task):
        """
		Load lightcurve from task ID or full task dictionary.

		Parameters:
			task (integer or dict):

		Returns:
			:class:`lightkurve.TessLightCurve`: Lightcurve for the star in question.

		Raises:
			ValueError: On invalid file format.

		.. codeauthor:: Rasmus Handberg <*****@*****.**>
		"""

        logger = logging.getLogger(__name__)

        # Find the relevant information in the TODO-list:
        if not isinstance(task, dict) or task.get("lightcurve") is None:
            if isinstance(task, dict):
                priority = int(task['priority'])
            else:
                priority = int(task)

            self.cursor.execute(
                "SELECT * FROM todolist INNER JOIN diagnostics ON todolist.priority=diagnostics.priority WHERE todolist.priority=? LIMIT 1;",
                (priority, ))
            task = self.cursor.fetchone()
            if task is None:
                raise ValueError(
                    "Priority could not be found in the TODO list")
            task = dict(task)

        # Get the path of the FITS file:
        fname = os.path.join(self.input_folder, task.get('lightcurve'))
        logger.debug('Loading lightcurve: %s', fname)

        # Load lightcurve file and create a TessLightCurve object:
        if fname.endswith(('.fits.gz', '.fits')):
            with fits.open(fname, mode='readonly', memmap=True) as hdu:
                # Filter out invalid parts of the input lightcurve:
                hdu = _filter_fits_hdu(hdu)

                # Quality flags from the pixels:
                pixel_quality = np.asarray(
                    hdu['LIGHTCURVE'].data['PIXEL_QUALITY'], dtype='int32')

                # Corrections applied to timestamps:
                timecorr = hdu['LIGHTCURVE'].data['TIMECORR']

                # Create the QUALITY column and fill it with flags of bad data points:
                quality = np.zeros_like(hdu['LIGHTCURVE'].data['TIME'],
                                        dtype='int32')
                bad_data = ~np.isfinite(hdu['LIGHTCURVE'].data['FLUX_RAW'])
                bad_data |= (pixel_quality & TESSQualityFlags.DEFAULT_BITMASK
                             != 0)
                quality[bad_data] |= CorrectorQualityFlags.FlaggedBadData

                # Create lightkurve object:
                lc = TessLightCurve(
                    time=hdu['LIGHTCURVE'].data['TIME'],
                    flux=hdu['LIGHTCURVE'].data['FLUX_RAW'],
                    flux_err=hdu['LIGHTCURVE'].data['FLUX_RAW_ERR'],
                    centroid_col=hdu['LIGHTCURVE'].data['MOM_CENTR1'],
                    centroid_row=hdu['LIGHTCURVE'].data['MOM_CENTR2'],
                    quality=quality,
                    cadenceno=np.asarray(hdu['LIGHTCURVE'].data['CADENCENO'],
                                         dtype='int32'),
                    time_format='btjd',
                    time_scale='tdb',
                    targetid=hdu[0].header.get('TICID'),
                    label=hdu[0].header.get('OBJECT'),
                    camera=hdu[0].header.get('CAMERA'),
                    ccd=hdu[0].header.get('CCD'),
                    sector=hdu[0].header.get('SECTOR'),
                    ra=hdu[0].header.get('RA_OBJ'),
                    dec=hdu[0].header.get('DEC_OBJ'),
                    quality_bitmask=CorrectorQualityFlags.DEFAULT_BITMASK,
                    meta={'data_rel': hdu[0].header.get('DATA_REL')})

                # Apply manual exclude flag:
                manexcl = manual_exclude(lc)
                lc.quality[manexcl] |= CorrectorQualityFlags.ManualExclude

        elif fname.endswith(('.noisy', '.sysnoise')):  # pragma: no cover
            data = np.loadtxt(fname)

            # Quality flags from the pixels:
            pixel_quality = np.asarray(data[:, 3], dtype='int32')

            # Corrections applied to timestamps:
            timecorr = np.zeros(data.shape[0], dtype='float32')

            # Change the Manual Exclude flag, since the simulated data
            # and the real TESS quality flags differ in the definition:
            indx = (pixel_quality & 256 != 0)
            pixel_quality[indx] -= 256
            pixel_quality[indx] |= TESSQualityFlags.ManualExclude

            # Create the QUALITY column and fill it with flags of bad data points:
            quality = np.zeros(data.shape[0], dtype='int32')
            bad_data = ~np.isfinite(data[:, 1])
            bad_data |= (pixel_quality & TESSQualityFlags.DEFAULT_BITMASK != 0)
            quality[bad_data] |= CorrectorQualityFlags.FlaggedBadData

            # Create lightkurve object:
            lc = TessLightCurve(
                time=data[:, 0],
                flux=data[:, 1],
                flux_err=data[:, 2],
                quality=quality,
                cadenceno=np.arange(1, data.shape[0] + 1, dtype='int32'),
                time_format='jd',
                time_scale='tdb',
                targetid=task['starid'],
                label="Star%d" % task['starid'],
                camera=task['camera'],
                ccd=task['ccd'],
                sector=2,
                #ra=0,
                #dec=0,
                quality_bitmask=CorrectorQualityFlags.DEFAULT_BITMASK,
                meta={})

        else:
            raise ValueError("Invalid file format")

        # Add additional attributes to lightcurve object:
        lc.pixel_quality = pixel_quality
        lc.timecorr = timecorr

        # Modify the "extra_columns" tuple of the lightkurve object:
        # This is used internally in lightkurve to keep track of the columns in the
        # object, and make sure they are propergated.
        lc.extra_columns = tuple(
            list(lc.extra_columns) + ['timecorr', 'pixel_quality'])

        # Keep the original task in the metadata:
        lc.meta['task'] = task
        lc.meta['additional_headers'] = fits.Header()

        if logger.isEnabledFor(logging.DEBUG):
            with contextlib.redirect_stdout(LoggerWriter(
                    logger, logging.DEBUG)):
                lc.show_properties()

        return lc
Example #2
0
    def load_lightcurve(self, task, ver='RAW'):
        """
		Load lightcurve from task ID or full task dictionary.

		Parameters:
			task (integer or dict):

		Returns:
			``lightkurve.TessLightCurve``: Lightcurve for the star in question.

		Raises:
			ValueError: On invalid file format.

		.. codeauthor:: Rasmus Handberg <*****@*****.**>
		"""

        logger = logging.getLogger(__name__)

        # Find the relevant information in the TODO-list:
        if not isinstance(task, dict) or task.get("lightcurve") is None:
            self.cursor.execute(
                "SELECT * FROM todolist INNER JOIN diagnostics ON todolist.priority=diagnostics.priority WHERE todolist.priority=? LIMIT 1;",
                (task, ))
            task = self.cursor.fetchone()
            if task is None:
                raise ValueError(
                    "Priority could not be found in the TODO list")
            task = dict(task)

        # Get the path of the FITS file:
        fname = os.path.join(self.input_folder, task.get('lightcurve'))
        logger.debug('Loading lightcurve: %s', fname)

        if fname.endswith('.fits') or fname.endswith('.fits.gz'):
            with fits.open(fname, mode='readonly', memmap=True) as hdu:
                # Quality flags from the pixels:
                pixel_quality = np.asarray(
                    hdu['LIGHTCURVE'].data['PIXEL_QUALITY'], dtype='int32')

                # Create the QUALITY column and fill it with flags of bad data points:
                quality = np.zeros_like(hdu['LIGHTCURVE'].data['TIME'],
                                        dtype='int32')

                if ver == 'RAW':
                    LC = hdu['LIGHTCURVE'].data['FLUX_RAW']
                    LC_ERR = hdu['LIGHTCURVE'].data['FLUX_RAW_ERR'],
                elif ver == 'CORR':
                    LC = hdu['LIGHTCURVE'].data['FLUX_CORR']
                    LC_ERR = hdu['LIGHTCURVE'].data['FLUX_CORR_ERR'],

                bad_data = ~np.isfinite(LC)

                bad_data |= (pixel_quality & TESSQualityFlags.DEFAULT_BITMASK
                             != 0)
                quality[bad_data] |= CorrectorQualityFlags.FlaggedBadData

                # Create lightkurve object:
                lc = TessLightCurve(
                    time=hdu['LIGHTCURVE'].data['TIME'],
                    flux=LC,
                    flux_err=LC_ERR,
                    centroid_col=hdu['LIGHTCURVE'].data['MOM_CENTR1'],
                    centroid_row=hdu['LIGHTCURVE'].data['MOM_CENTR2'],
                    quality=quality,
                    cadenceno=np.asarray(hdu['LIGHTCURVE'].data['CADENCENO'],
                                         dtype='int32'),
                    time_format='btjd',
                    time_scale='tdb',
                    targetid=hdu[0].header.get('TICID'),
                    label=hdu[0].header.get('OBJECT'),
                    camera=hdu[0].header.get('CAMERA'),
                    ccd=hdu[0].header.get('CCD'),
                    sector=hdu[0].header.get('SECTOR'),
                    ra=hdu[0].header.get('RA_OBJ'),
                    dec=hdu[0].header.get('DEC_OBJ'),
                    quality_bitmask=CorrectorQualityFlags.DEFAULT_BITMASK,
                    meta={})

                # Apply manual exclude flag:
                manexcl = manual_exclude(lc)
                lc.quality[manexcl] |= CorrectorQualityFlags.ManualExclude

        else:
            raise ValueError("Invalid file format")

        # Add additional attributes to lightcurve object:
        lc.pixel_quality = pixel_quality

        # Keep the original task in the metadata:
        lc.meta['task'] = task
        lc.meta['additional_headers'] = fits.Header()

        if logger.isEnabledFor(logging.DEBUG):
            lc.show_properties()

        return lc