from __future__ import division from __future__ import print_function from __future__ import with_statement from fortranfile import FortranRead import physconst import os.path from byte2human import byte2human from time2human import time2human from version2human import version2human from logged import Logged from utils import CachedAttribute, cachedmethod, loader import datetime import numpy as np class LCData(Logged): """Interface to load KEPLER light curve files.""" def __init__(self, filename, zerotime = True, silent = False): """ Constructor; wants file name. """ self.setup_logger(silent) filename = os.path.expanduser(filename) self.filename = os.path.expandvars(filename) self.file = FortranRead(self.filename) self._load() self.file.close() self.close_logger() def _load(self, zerotime = True): """Open file, call load data, time the load, print out diagnostics.""" start_time = datetime.datetime.now() self.logger.info('Loading {} ({})'.format( self.file.filename, byte2human(self.file.filesize))) self.data = [] while not self.file.eof(): record=LCRecord(self.file) self.data.append(record) self.nmodels = len(self.data) if zerotime: self.remove_zerotime() end_time = datetime.datetime.now() load_time = end_time - start_time self.logger_load_info( self.data[ 0].nvers, self.data[ 0].ncyc, self.data[-1].ncyc, self.nmodels, load_time) def remove_zerotime(self): """Detect and remove resets of time.""" zerotime = np.float64(0) time0 = self.data[0].time for i in xrange(1,self.nmodels): if self.data[i].time < time0: zerotime = self.data[i-1].time self.logger.info('@ model = {:8d} zerotime was set to {:12.5g}.'.format( int(self.data[i].ncyc), float(zerotime))) time0 = self.data[i].time self.data[i].time = time0 + zerotime @CachedAttribute def time(self): "time (s)." return np.array([x.time for x in self.data]) @CachedAttribute def xlphot(self): "luminosity (erg/s)." return np.array([x.xlphot for x in self.data]) @CachedAttribute def teff(self): "effective temperature (K)." return np.array([x.teff for x in self.data]) @CachedAttribute def vphot(self): "photospehere velocity (cm/s)." return np.array([x.vphot for x in self.data]) @CachedAttribute def reff(self): "photospehere radius (cm)." fac = physconst.Kepler.a * physconst.Kepler.c * np.pi return np.sqrt(self.xlphot/(self.teff**4 * fac)) @CachedAttribute def radius(self): "radius (cm)." return np.array([x.radius for x in self.data]) @CachedAttribute def xlum(self): "luminosity (erg/s)." return np.array([x.xlum for x in self.data]) loadlc = loader(LCData) class LCRecord(object): def __init__(self, file, data = True): self.file = file self.load(data) def load(self,data=True): f = self.file f.load() self.nvers = f.get_i4() self.ncyc = f.get_i4() self.time = f.get_f8n() if self.nvers >= 10101: self.dt = f.get_f8n() (self.teff, self.radius, self.xlum) = f.get_f8an([3]) if self.nvers >= 10100: self.vphot = f.get_f8n() if self.nvers >= 10102: self.xlphot = f.get_f8n() self.mdata = f.get_i4() if self.mdata > 0: x = f.get_f8an([3,self.mdata]) self.rn = x[0,:].copy() self.un = x[1,:].copy() self.xln = x[2,:].copy() f.assert_eor()