2016-12-06 05:15:03 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from __future__ import unicode_literals
|
|
|
|
import numpy as np
|
|
|
|
import sys
|
2017-01-11 03:46:01 +00:00
|
|
|
import getopt
|
|
|
|
|
|
|
|
sys.path.insert(1,"/usr/local/science/clag/")
|
|
|
|
import clag
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
# For jupyter notebook
|
|
|
|
# %pylab inline
|
|
|
|
|
|
|
|
try:
|
2017-01-11 03:46:01 +00:00
|
|
|
opts,args = getopt.getopt(sys.argv[1:], "")
|
2016-12-06 05:15:03 +00:00
|
|
|
except getopt.GetoptError:
|
|
|
|
print 'analyze_lightcure.py <reference curve> <compared curve>'
|
|
|
|
sys.exit(2)
|
|
|
|
|
|
|
|
## load the first light curve
|
2017-01-11 03:46:01 +00:00
|
|
|
lc1_table = np.loadtxt(args[0],skiprows=1)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
# works if first two entries represent minimum spacing, from example
|
2017-01-11 03:46:01 +00:00
|
|
|
# dt = lc1_table[1,0] - lc1_table[0, 0]
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-10 01:19:49 +00:00
|
|
|
# Time resolution determined from inspection and testing. This script
|
|
|
|
# does not expect evenly spaced data in time.
|
|
|
|
dt = 0.1
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
# _ = plot(lc1_table[:,0], lc1_table[:,1])
|
|
|
|
# _ = plot(lc1_table[:,0], lc1_table[:,3])
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
# Split the light curve into segments #
|
|
|
|
#seg_length = 256
|
|
|
|
#index = np.arange(len(data)).reshape((-1, seg_length))
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
# For now, instead of splitting up the curves, the program will assume
|
|
|
|
# that the data list is shorter than 256 elemements. so,
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
index = np.arange(len(lc1_table)).reshape(-1,len(lc1_table))
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
lc1_time = [lc1_table[i, 0] for i in index]
|
|
|
|
lc1_strength = [lc1_table[i, 1] for i in index]
|
|
|
|
lc1_strength_err = [lc1_table[i, 2] for i in index]
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
#### Get the psd for the first light curve ####
|
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
# These bin values determined summer 2016 for STORM III optical/UV lightcurves
|
2017-01-25 06:19:52 +00:00
|
|
|
# fqL = np.array([0.0049999999, 0.018619375, 0.044733049, 0.069336227, 0.10747115, 0.16658029, 0.25819945, 0.40020915, 0.62032418])
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
# using utilities to set up frequency bins #
|
2017-01-25 06:19:52 +00:00
|
|
|
seg_length = 256;
|
|
|
|
fqL = np.logspace(np.log10(1.1/seg_length),np.log10(.5/dt),7)
|
|
|
|
fqL = np.concatenate(([0.5/seg_length], fqL))
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
nfq = len(fqL) - 1
|
|
|
|
|
|
|
|
|
|
|
|
## initialize the psd class for multiple light curves ##
|
|
|
|
P1 = clag.clag('psd', lc1_time, lc1_strength, lc1_strength_err, dt, fqL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## initial parameters, start with ones
|
|
|
|
inpars = np.ones(nfq)
|
|
|
|
|
|
|
|
## print the loglikelihood for the input values ##
|
2017-01-11 03:46:01 +00:00
|
|
|
P1.logLikelihood(inpars)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Now do the fitting and find the best fit psd values at the given frequency bins ##
|
2017-01-11 06:37:12 +00:00
|
|
|
ref_psd, ref_psd_err = clag.optimize(P1, inpars)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## plot ##
|
|
|
|
fqd = 10**(np.log10( (fqL[:-1]*fqL[1:]) )/2.)
|
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
#loglog(fqd, 0.1*fqd**(-1.5), label='input psd')
|
2017-01-11 06:37:12 +00:00
|
|
|
#errorbar(fqd[1:-1], ref_psd[1:-1], yerr=ref_psd_err[1:-1], fmt='o', ms=10, label='fit')
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
# load second lightcurve
|
|
|
|
|
|
|
|
# This would work if both curves are in same file
|
|
|
|
# lc2_strength = [lc1_table[i, 3] for i in index]
|
|
|
|
# lc2_strength_err = [lc1_table[i, 4] for i in index]
|
|
|
|
|
|
|
|
# But, they aren't, so,
|
|
|
|
|
|
|
|
# Load second light curve
|
|
|
|
lc2_table = np.loadtxt(args[1],skiprows=1)
|
|
|
|
|
|
|
|
index = np.arange(len(lc2_table)).reshape(-1,len(lc2_table))
|
|
|
|
|
|
|
|
lc2_time = [lc2_table[i, 0] for i in index]
|
|
|
|
lc2_strength = [lc2_table[i, 1] for i in index]
|
|
|
|
lc2_strength_err = [lc2_table[i, 2] for i in index]
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## Now do the second light curve
|
|
|
|
|
2017-01-11 03:46:01 +00:00
|
|
|
P2 = clag.clag('psd', lc2_time, lc2_strength, lc2_strength_err, dt, fqL)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 06:37:12 +00:00
|
|
|
echo_psd, echo_psd_err = clag.optimize(P2, inpars)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
### Now the cross spectrum ###
|
|
|
|
### We also give it the calculated psd values as input ###
|
|
|
|
Cx = clag.clag('cxd',
|
|
|
|
[list(i) for i in zip(lc1_time,lc1_time)],
|
2017-01-11 03:46:01 +00:00
|
|
|
[list(i) for i in zip(lc1_strength,lc2_strength)],
|
|
|
|
[list(i) for i in zip(lc1_strength_err,lc2_strength_err)],
|
2017-01-11 06:37:12 +00:00
|
|
|
dt, fqL, ref_psd, echo_psd)
|
2016-12-06 05:15:03 +00:00
|
|
|
|
2017-01-11 06:37:12 +00:00
|
|
|
inpars = np.concatenate( (0.3*(ref_psd*echo_psd)**0.5, ref_psd*0+1.) )
|
2016-12-06 05:15:03 +00:00
|
|
|
p, pe = clag.optimize(Cx, inpars)
|
|
|
|
phi, phie = p[nfq:], pe[nfq:]
|
|
|
|
lag, lage = phi/(2*np.pi*fqd), phie/(2*np.pi*fqd)
|
|
|
|
cx, cxe = p[:nfq], pe[:nfq]
|
|
|
|
|
2017-01-11 06:37:12 +00:00
|
|
|
np.savetxt("freq.out",fqL.reshape((-1,len(fqL))))
|
|
|
|
np.savetxt("ref_psd.out",[ref_psd,ref_psd_err])
|
|
|
|
np.savetxt("echo_psd.out",[echo_psd,echo_psd_err])
|
|
|
|
np.savetxt("crsspctrm.out",[cx,cxe])
|
|
|
|
np.savetxt("timelag.out",[lag,lage])
|
2016-12-06 05:15:03 +00:00
|
|
|
|
|
|
|
|