c c########################################################################## c Transit search with robust TFA+FOUR filtering for stellar variability c and outlier/transit preservation c########################################################################## c c DATE: - September 07, 2020 c c INPUT: - tran_k2_v0.par :: Input parameter file c - filename.lis :: List of stars to be analyzed with complete paths c to their time series c - ref.pos :: Reference position/magnitude data file of all c stars available c - template.lis :: List of templates & paths to their time series c c OUTPUT: - snr.dat :: Result of the analysis on the stars analyzed c [this is an "appended" file] c - spec/*.sp :: Frequency spectrum of the star analyzed c - flc/*.lc :: Filtered [non-reconstructed] LC of the star analyzed c c - sp.dat :: Frequency spectrum of the star analyzed c - four_ord.dat :: Diagnostic parameters for the optimum Fourier order c - test_par.dat :: Test signal parameters ( produced only if itest > 0 ) c [this is an "appended" file] c - ts_4.dat :: Time series output [after TFA+FOUR filtering] c - outlier.dat :: Outlying points as given by the routine "outlier1" c - pre_bls.dat :: Data AFTER AR edge correction for Gibbs phenomenon c (this is the time series used by the BLS search) c - sp##.dat :: BLS spectr. after the ##-th prewhitening [no reconstruction] c - lc##.dat :: LC after the ##-th prewhitening [no reconstruction] c - pa##.dat :: Folding params for the ##-th prewhitening [no reconstruction] c - rec_tfa.dat :: LC after the SINGLE transit reconstruction step c - multi_bls.dat :: BLS parameters after the FULL prewhitening cycle c - multi_par.dat :: Parameters of the significant components derived by the c iterative reconstruction c - tr#.lc :: Individual/reconstructed transit components and the c corresponding fits with folding phase values c - tr_all.lc :: Reconstructed transit LC and the fit with all components c - tr#_fit.lc :: Densely sampled transit fit to the #-th component c c c PLOTS: - cand_4_plot.png:: 4-panel diagnostic plot for the main transit component of c a given candidate c - mult_plot.png :: BLS spectra and zoomed/folded LCs for each transit c component of a given candidate c - plots/*.png :: The above files with star IDs c c c NOTES: - This is the "cleaned-up" version of the code "tran_k2" that was used c to produce all results presented in the paper on this code and the c survey of K2/C05 (submitted on 2020.06.23). c c - Not all output files are generated after each run. For example, if c isurvey=1, then the only output file produced is *snr.dat* c [and, if itest > 0, then the test signal parameters] c c - The same type of data product might appear in different files at c various stages of filtering. This may be useful in checking the c consistency of these data products. c c - Plots are ALWAYS generated, except if isurvey=0. They are saved under c target-specific names only if ispl=1 c c - Plots are generated by using GNUPLOT. Files required: c * fold_k2_rr.f c * cand_4_plot.sh c * mult_plot.sh c * mult_#.sh - with #=1,2,3,4,5 c c - Maximum number of time series data points = 30333 c Maximum number of frequency spectrum values = 155555 c c - There is a seemingly harmless error message at the end of each run c that we could not cure: c "The following floating-point exceptions are signalling: c IEEE_DIVIDE_BY_ZERO" c c================================================================================= c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fname character*9 sname,ref_id(30333) character*3 camp character*70 command c dimension t(30333),x(30333),we(30333),x_rec(30333) dimension xf(30333),y(30333),xw(30333),u(30333),v(30333) dimension told(30333),xold(30333),xsyn(30333),x_int(30333) dimension ref_mag(30333),ref_ra(30333),ref_de(30333) dimension xft(30333),fourts(30333),tfats(30333) dimension xf1(30333),xf2(30333),xp1(30333),xp2(30333) dimension tp1(30333),tp2(30333),xfa(30333),z(30333),xtr(30333) dimension trsyn(30333),x_1(30333),tfa_r(30333),four_r(30333) dimension ft(6,30333),xt(6,30333) dimension fw(100),dw(100),snrw(100),fr(100) dimension td(100),qt(100),ri(100),tc(100) dimension p(155555) dimension intr(30333) c common /tt/ ttemp(30333),ntemp common /rc/ m3_rc c c================================================================================= c c... WIRED-IN PARAMETERS c tc0 = 57000.0d0 ntw = 6 pfac = 1.05d0 ext = 0.05d0 mar = 900 iar = 1 npol = 5 iew = 0 iwa = 0 ndsp = 6 nb_sp = 2000 iso = 1 isel = 1 asig2 = 3.0d0 snr_min = 6.0d0 ising = 0 dsp_min = 10.0d0 ksm = 1 c c tc0 :: Transit epoch for the test signal c ntw :: Number of prewhitening cycles for the multiple transit search c pfac :: The fundametal period of the Fourier component is c pfac*(t_max-t_min), to avoid (or decrease) the Gibbs c phenomenon at the edges. c ext :: AR prediction is performed on both sides for N*ext c data points at the edges c mar :: Order of the AR process c iar :: 0 - do not use AR process to decrease Gibbs oscillations c 1 - use AR process to decrease Gibbs oscillations c npol :: Order of the polynomial for filtering out trends from the c AR predictions c iew :: 1 - use equal weights in "one_rec" c 0 - use weights in "one_rec" as given in that routine, but c do not employ "exact zero weight" windows. c 2 - as iew=0, but DO EMPLOY "exact zero weight" windows. c iwa :: 0 - do not employ exact ZERO weights c 1 - employ exact ZERO weights [when appropriate - see "slide_rms"] c NOTE: This parameter is used ONLY in the basic signal processing c ndsp :: Order of the polynomial fit to the BLS spectrum c nb_sp :: Number of frequency bins for the economically sampled frequency c spectra c iso :: 0/1 - no/yes single outlier correction c isel :: 0 - Take always the type of edge correction given by "iar" c iar=0 -- we have FOUR edge fit c iar=1 -- we have AR edge fit c 1 - Compare the FOUR and AR approximations and take the one c with the smallest residual robust scatter. c iar=0 -- we have FOUR edge fit c iar=1 -- we have either FOUR or AR edge fit, depending c on the size of the residual robust scatter c asig2 :: Sigma clipping parameter. Employed ONLY on the reconstructed, c multi-component time series (once all the components are found. c snr_min:: Minimum spectrum SNR to declare a transit component detected c in the multi transit reconstruction routine "multi_rec". c ising :: 1 - Perform single transit test c 0 - Skip single transit test c dsp_min:: Minimum DSP to declare a single transit event as such. c ksm :: 1- Employ reconstruction in the multi-tranist parameter c determination c 0- Do NOT employ reconstruction in the multi-tranist parameter c determination [this option DOES not work yet] c c================================================================================= c c... READ IN BASIC PARAMETERS c call parin(isurvey,jsm,itemp,nstar,fmin1,fmax1,nf1, &iwhite,nb,qmi,qma,itest,tfreq0,tfreq1,dep0,dep1,t14_0, &t14_1,pht0,pht1,frin0,frin1,amootv,phootv,frootv,iflare, &igen,std,fdep,iseed,issp,isflc,ispl,iplot,asig,ic_flare, &ibg,ixy,m_four,m_opt) c c.................................... Set certain parameters iseed0= iseed iseed = 2000*iseed+1 totdf = fmax1-fmin1 fmin11= fmin1 nbb = 2000 qtr0 = t14_0*tfreq0 qtr1 = t14_1*tfreq1 dep00 = dep0 dep10 = dep1 rfac = 0 c c.................................... Change some parameters if(iplot.eq.0) ispl=0 if(isurvey.eq.0) go to 11 issp=0 isflc=0 iwhite=0 11 continue c c... Read in *ref.pos* c call read_ref(n_ref,ref_id,ref_ra,ref_de,ref_mag) c c... Create directories "spec", "flc" and "plots" if: c a) they do not exisist c b) Any of these parameters is greater than zero: c isflc, issp, ispl c command='if [ ! -d spec ]; then mkdir spec; fi' if(issp.gt.0) call SYSTEM(command) command='if [ ! -d flc ]; then mkdir flc; fi' if(isflc.gt.0) call SYSTEM(command) command='if [ ! -d plots ]; then mkdir plots; fi' if(ispl.gt.0) call SYSTEM(command) c open(14,file='snr.dat',access='append') if(itest.gt.0) open(12,file='test_par.dat',access='append') write(14,2) 2 format('# SNAME N TOT AVMAG SIG_FINAL', &' F0 DEPTH SNR SPD DSP QTRAN TCEN', &' NEV NDIT SIG_FOUR SIG_TFA SNRT FRET', &' RING ITOC F03 SNR03 F35 SNR35 M_FOUR') c c c************************************** c START CYCLE OF ANALYSIS * c************************************** c do 1 nseq=1,nstar c write(*,6) nseq,nstar 6 format('*************************** CYCLE ',2(1x,i6)) c call cpu_time(t_start) c if(itest.eq.0) go to 15 if(nstar.eq.1) go to 15 rfac=2.0d0*arand(iseed)-1.0d0 dep0=dep00*(1.0d0+rfac*fdep) dep1=dep10*(1.0d0+rfac*fdep) 15 continue c c... Read in the first file item of 'filename.lis' call file_in(fname,sname,nfile,camp) c c... Match object with its average magnitude call match_obj(sname,n_ref,ref_id,ref_mag,avmag) c c... Read in data (time series, etc.) and perform TFA+FOUR filtering call datain_tfa_four(isurvey,nseq,itest,iseed,m_four, &itemp,fname,sname,n,t,x,nvold,told,xold, &intemp,avmag,frin0,frin1,pht1,tc0,frootv,amootv, &phootv,qtr0,qtr1,pht0,igen,std,xsyn,trsyn, &x_int,camp,we,iflare,ibg,ixy,sig,pfac,ext, &iwa,fourts,tfats,tfreq0,dep0,tfreq1,dep1,m_opt) c c... Brief summary: c - {n, t(i),x_int(i)} :: ORIGINAL (interpolated to {ttemp}) time series c - {tfats} :: TFA vector c - {fourts} :: FOURIER vector c - {x} :: Filtered time series: {x_int}-{tfats}-{fourts} c if(intemp.eq.1) write(*,151) sname 151 format(/,'>>>>>>>>> Target ',a10, &' was a member of the template set!',/) c do 9 i=1,n xfa(i)=fourts(i) xf(i)=fourts(i) 9 continue npr=0 if(iar.eq.0) go to 150 c c%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% c AUTOREGRESSIVE EDGE CORRECTION FOR THE GIBBS OSCILLATIONS c%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% c c... Generate noisy truncated signal from the Fourier fit c ... We use UNIFORM noise distribution [to avoid large c perturbations in the synthetic Fourier signal c [ this is why we have that sqrt(3) factor ]. c npr=idint(dfloat(n)*ext) sigf=dsqrt(3.0d0)*sig do 8 i=1,n z(i)=x_int(i)-tfats(i) y(i)=xf(i)+sigf*(2.0d0*arand(iseed)-1.0d0) 8 continue c c.................................................... Cut edges for AR modeling call cut_ts(npr,n,t,y,n_ar,u,v) c c.................................................... AR prediction at the edges call LR_pred(mar,npr,n_ar,u,v,xf1,xf2,xp1,xp2, &tp1,tp2,xw,sig) c c.................................................... Full - AR+FOUR+AR - time series {xfa} call ar_four_ar(npr,n,t,xf,tp1,xp1,tp2,xp2,xfa) c c.................................................... Robust fit of low-order polynomials c to the AR extrapolations at the edges call end_pol(npol,npr,n,t,z,xfa) c c.................................................... Compare FOUR and AR and select the better one if(isel.eq.1) call ar_vs_four(npr,n,z,xf,xfa,s_xf1,s_xfa1, &s_xf2,s_xfa2,iflag1,iflag2) c c%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% c 150 continue c c The time series below, {t(i),x(i)}, is corrected for c systematics {tfats} and stellar variability {xfa}. c c HOWEVER: it is still *incomplete/biased*, if there are c other signals, i.e., transits. Correction for these c additional components will be done by the c reconstruction routines, e.g., by *one_rec*. c----------------------------------------------------------------- do 10 i=1,n x(i) = x_int(i)-xfa(i)-tfats(i) 10 continue c c........................................ Omitting single outliers if(iso.eq.1) call outlier_1g(isurvey,1,n,t,x,nout) c c........................................ Flare correction if(ic_flare.eq.1) call kill_flare(1,n,x,nflare) c c........................................ Sigma clipping, zero-average, save PRE-BLS call sigma_clipping(1,asig,n,x,nclip) c c........................................ Save PRE-BLS data and set {x_rec}={x} if(isurvey.ne.1) call save_pre_bls(isflc,sname,n,t,x,xfa, &tfats,we,x_rec) c c........................................ Check efficacy of systematics and c stellar variability removal call check_sys_four(n,t,x,f03,snr03,f35,snr35) c c........................................ Compute RMS of the FOUR and TFA c corrections call four_vs_tfa(n,tfats,xfa,sig_four,sig_tfa) c c........................................ Save the AR correction if(isurvey.eq.0) call ts_ar_dat(npr,n,t,x_int,xf,xfa,x) c c................................. Compute total time span and frequency range call minmax(n,t,tmin,tmax) tot = tmax-tmin fmin0 = 1.001d0/tot fmin1 = fmin11 if(fmin1.lt.fmin0) fmin1=fmin0 fmax1 = fmin1+totdf df1 = (fmax1-fmin1)/dfloat(nf1-1) c c................................. Search for single transit event nsing=0 if(ising.eq.1) call single_tran(n,t,x,tmin,tmax,dsp_min, &tce_1,dep_1,qtr_1,qin_1,x_1,nev_1,ndit_1,itoc_1,dsp_1,nsing) c if(nsing.eq.0) go to 50 write(*,52) dep_1,qtr_1,qin_1,tce_1,nev_1,ndit_1,itoc_1,dsp_1 52 format('Single transit parameters:',/, &'Depth = ',f13.6,/, &'Q_tran = ',f13.6,/, &'Q_ingr = ',f13.6,/, &'T_cen = ',f13.5,/, &'N_event= ',i13,/, &'N_intr = ',i13,/, &'ITOC = ',i13,/, &'DSP = ',f13.1) 50 continue c c................................. Standard BLS search call eebls(n,t,x,u,v,nf1,fmin1,df1,nb,qmi,qma, &p,bper,bpow,depth,qtran,in1,in2) c c................................. Filter out low-frequency trend from the frequency spectrum call sppolfit_w(ndsp,nf1,p) c c................................. Save frequency spectrum call save_sp(nb_sp,nf1,fmin1,df1,p) if(issp.eq.1) call save_spec(sname,nb_sp,nf1,fmin1,df1,p) c c................................. Compute frequency at the highest peak and SNR call spsnr1(nf1,p,fmin1,df1,frmax0,pmax0,sde0,spd0) c if(nsing.eq.0) go to 54 p0=tmax-tmin f0=1.0d0/p0 frmax0=f0 go to 51 54 continue c c................................. Compute ACCURATE period "p0", check 1st (sub)harmonic p10=0.5d0/frmax0 pac=p10 dev=1.0d30 do 30 j=1,3 p1=p10*(2**(j-1)) call ac_per(p1,df1,n,t,x,nbb,qmi,qma,pac1,sig) if(sig.gt.dev) go to 30 dev=sig pac=pac1 30 continue p0=pac f0=1.0d0/p0 c 51 continue c c................................. Light curve reconstruction and transit parameter determination nout=0 nflare=0 nclip=0 call one_rec(isurvey,iew,jsm,p0,n,t,x_int,x,nbb,qmi,qma, &m_four,iso,intr,nintr,depth,ring,qtran,tcen,xft,x_rec,we, &sig,nout,tfa_r,four_r) c c................................. Flare correction if(ic_flare.eq.1) call kill_flare_r(2,n,x_rec,xft,nflare) c c... NOTE: We do not have relative sigma clipping in respect of c {xft} - sigma_clipping_r(2,asig2,n,x_rec,xft,nclip) -, c because this would clip also possible other components. c c................................. Compute DSP, SNRT, FRET call dsp_ok(n,t,x_rec,p0,tcen,qtran,dsp,snrt,fret) c c................................. Compute unbiased estimate of the standard deviation of c the residuals around the best-fitting TRANSIT model do 550 i=1,n y(i)=x_rec(i)-xft(i) 550 continue call astat(y,n,aver,sigma) n_red=n-4-m3_rc-nout-nflare-nclip sig_final=sigma*dsqrt(dfloat(n)/dfloat(n_red)) c c................................. Save filtered/reconstructed time series call save_rec(n,t,x_rec,xft,xsyn,trsyn, &tfa_r,four_r,qtran) c if(isurvey.eq.1) go to 14 c c................................. Save peak frequency parameters in "fold.par" open(24,file='fold.par') nper=1 write(24,40) sname,nper,f0,tcen 40 format(a9,/,i2,/,f11.7,/,f11.4) close(24) c c................................. Create and save basic diagnostic plot if(iplot.ne.0) call SYSTEM('gnuplot cand_4_plot.sh') command=trim('cp cand_4_plot.png plots/cand_4_'//sname//'.png') if(ispl.eq.1) call SYSTEM(command) c 14 continue c c................................. Compute number of transits and the number of the observed c data points in them call events(n,t,f0,tcen,qtran,nev,ndit,itoc) c c................................. Print out result of the analysis of the TFA+FOUR+AR-filtered data write(*,3) sname,n,tot,avmag,sig_final,f0,depth,sde0,spd0, &dsp,qtran write(14,3) sname,n,tot,avmag,sig_final,f0,depth,sde0,spd0, &dsp,qtran,tcen,nev,ndit,sig_four,sig_tfa,snrt,fret,ring,itoc, &f03,snr03,f35,snr35,m_four 3 format(a9,2x,i5,1x,f5.0,1x,f8.5,1x,f9.6,1x,f12.8,1x,f9.6, &1x,f4.1,1x,f5.3,1x,f4.1,1x,f6.4,1x,f14.7,2(1x,i5),2(1x,f8.6), &1x,f6.2,1x,f7.2,1x,f7.4,1x,i5,2(1x,f10.6,1x,f5.1),5x,i3) c c................................. Print out test data if(itest.gt.0) write(12,20) sname,tfreq0,dep0,t14_0,pht0,frin0, &tfreq1,dep1,t14_1,pht1,frin1,amootv,phootv,frootv,iflare,igen, &std,1.0d0+rfac*fdep,iseed0 20 format(a9,2(2x,f12.8,1x,f9.6,1x,f5.3,1x,f5.3,1x,f5.3), &1x,f9.6,1x,f5.3,1x,f12.8,2(1x,i2),1x,f9.6,1x,f5.3,1x,i6) c if(iwhite.eq.0) go to 5 c c################################################################# c SUCCESSIVE PREWHITENING BY BLS SIGNALS c call bls_white(n,t,x,nf1,fmin1,df1,nb,qmi,qma, &nsing,p0,sname,nb_sp,ndsp,nbb,ntw,fw,dw,snrw) c c................................. Reconstruction of the significant transit components call multi_rec(ksm,n,t,x_int,x,nbb, &qmi,qma,ntw,fw,dw,snrw,snr_min,asig2, &ntc,xtr,xft,ft,xt,we,fr,td,qt,ri,tc,sig,nclip) c c................................. Save reconstructed LCs and transit parameters call save_m_rec(sname,ntc,n,t,xtr,xft,ft,xt, &fr,td,qt,ri,tc) c c################################################################# c c................................. Create and save plots for the significant transit components if(iplot.ne.0) call SYSTEM('./mult_plot.sh') command=trim('cp mult_plot.png plots/mult_'//sname//'.png') if(ispl.eq.1) call SYSTEM(command) c 5 continue c c................................. Print out execution time call cpu_time(t_finish) write(*,4) t_finish-t_start 4 format(/,'EXECUTION TIME [s] = ',f7.2,/) c 1 continue c close(14) if(itest.gt.0) close(12) c stop end c c subroutine sigma_clipping(ipr,asig,n,x,nclip) c-------------------------------------------------------------------------- c - Performs iterative sigma-clipping of {n,x} at asig*sigma level c - Upon return {n,x} is sigma-clipped and zero-averaged c - NOTE: Upon return, the number of data points is unchanged, because c outlying items are substituted by the average of the time c series. c-------------------------------------------------------------------------- implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333) c c....................................... Iterative sigma-clipping nclip=0 1 continue call astat(x,n,ave,sig) si1=asig*sig k=0 dd=-1.0d30 do 2 i=1,n d=dabs(x(i)-ave) if(d.lt.dd) go to 2 dd=d k=i 2 continue if(dd.lt.si1) go to 3 nclip=nclip+1 x(k)=ave go to 1 3 continue c c....................................... Compute zero-average time series call astat(x,n,ave,sig) do 5 i=1,n x(i)=x(i)-ave 5 continue c if(ipr.lt.0) return if(ipr.eq.0) write(*,10) nclip if(ipr.eq.1) write(*,11) nclip if(ipr.eq.2) write(*,12) nclip 10 format('>> nclip [after *PLACE UNKNOWN*] = ',i3) 11 format('>> nclip [after *datain_tfa_four*] = ',i3) 12 format('>> nclip [after *one_rec*] = ',i3) c c return end c c subroutine sigma_clipping_r(ipr,asig,n,x,xf,nclip) c------------------------------------------------------------------------ c - Performs iterative sigma-clipping of {n,x} in *respect of {xf}* c - Level of clipping: asig*sigma c - Upon return {n,x} is sigma-clipped c - N is the SAME as that of the input data, because the outlying c data points are replaced by the corresponding value of {xf} c------------------------------------------------------------------------ implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333),xf(30333),y(30333) c c............................... Iterative sigma-clipping based on {x-xf} nclip=0 do 4 i=1,n y(i)=x(i)-xf(i) 4 continue c 1 continue call astat(y,n,ave,sig) si1=asig*sig k=0 dd=-1.0d30 do 2 i=1,n d=dabs(y(i)-ave) if(d.lt.dd) go to 2 dd=d k=i 2 continue if(dd.lt.si1) go to 3 nclip=nclip+1 x(k)=xf(k) y(k)=0.0d0 go to 1 3 continue c if(ipr.lt.0) return if(ipr.eq.0) write(*,10) nclip if(ipr.eq.1) write(*,11) nclip if(ipr.eq.2) write(*,12) nclip 10 format(/,'>> nclip [after *PLACE UNKNOWN*] = ',i3) 11 format(/,'>> nclip [after *datain_tfa_four*] = ',i3) 12 format(/,'>> nclip [after *one_rec*] = ',i3) c c return end c c subroutine ts_ar_dat(npr,n,t,x_int,xf,xfa,xw) c--------------------------------------------------------------------- c This one prepares the data for 'ar_test_plot.sh' to test AR c--------------------------------------------------------------------- c c INPUT: {t} = Timebase of {x_int} c {x_int} = Original time series [interpolated to {t}] c {xf} = FOUR part of the FOUR+TFA fit to {x_int} c {xfa} = Final fit for the smooth stellar variability, c employing AR modeling at the edges and Fourier c elsewhere (AR modeling is a part of the c FOUR+TFA modeling) c {xw} = Residual of the complete model: c {xw}={x_int}-{xfa}-{tfats} c c OUTPUT: 'ts_ar.dat' c c NOTES: - If isel=1, then, depending on the decision made by c routine "ar_vs_four", the {xfa} array may be mixed c (i.e., FOUR on one side and AR on the other side) c c===================================================================== c c NOTES: c c *Because the ZERO point of the input time series {x} is fitted c by the TFA vector, the FOUR/AR fits could be shifted by a c constant. For this reason, we optimally shift {xf} and {xfa} c ONLY in this routine. c c *X_fit=1 indicates where the Gibbs oscillations have been c attempted to be remedied. c c *We assume that the moments of time are monotonic c (i.e., t(i) > t(i-1)) c c===================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x_int(30333),xf(30333),xfa(30333),xw(30333) c tmin=t(1) a_x=0.0d0 a_xf=0.0d0 a_xfa=0.0d0 do 1 i=1,n a_x=a_x+x_int(i) a_xf=a_xf+xf(i) a_xfa=a_xfa+xfa(i) 1 continue a_x=a_x/dfloat(n) a_xf=a_xf/dfloat(n)-a_x a_xfa=a_xfa/dfloat(n)-a_x c open(1,file='ts_ar.dat') write(1,44) 44 format('#',/, &'# TIME X_input X_fit X_pred X_Four', &' X_W',/, &'#-----------------------------------------------------', &'------------') n1=npr+1 n2=n-npr t1=t(1) do 43 i=1,n x_fit=1.0d0 if(i.lt.n1) go to 60 if(i.gt.n2) go to 60 x_fit=xfa(i)-a_xfa 60 continue write(1,45) t(i)-tmin,x_int(i),x_fit,xfa(i)-a_xfa, &xf(i)-a_xf,xw(i) 45 format(6(1x,f10.6)) 43 continue close(1) c return end c c subroutine end_pol(npol,npr,n,t,y,xfa) c--------------------------------------------------------------------- c Robust fit of low-order polynomials to the AR extrapolations c of the end sections c===================================================================== c NOTE: For continuity/stability [avoiding edge effect in the fit], c we fit a larger chunk than the one given by "npr". However, c we substitute only "npr" values with the data free from c the polynomial trend. c--------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),y(30333),xfa(30333) dimension u(30333),v(30333),xpi(30333) c if(npol.eq.0) return c nn=idint(1.5d0*dfloat(npr)) c c............................................ RIGHT end n1=n-nn+1 n2=n k=0 do 80 i=n1,n2 k=k+1 u(k)=t(i) v(k)=y(i)-xfa(i) 80 continue call robust_pol(nn,npol,u,v,xpi,sig,stdv) k=0 n3=n-npr+1 do 81 i=n1,n2 k=k+1 if(i.lt.n3) go to 81 xfa(i)=xfa(i)+xpi(k) 81 continue c c............................................ LEFT end n1=1 n2=nn k=0 do 1 i=n1,n2 k=k+1 u(k)=t(i) v(k)=y(i)-xfa(i) 1 continue call robust_pol(nn,npol,u,v,xpi,sig,stdv) k=0 n3=npr do 2 i=n1,n2 k=k+1 if(i.gt.n3) go to 2 xfa(i)=xfa(i)+xpi(k) 2 continue c return end c c subroutine robust_pol(n,np,t,x,xi,sig,stdv) c-------------------------------------------------------------- c This is a robust POLYNOMIAL fit with Cauchy weights c============================================================== c c DATE: 2020.06.03 c c NOTE: The constant in the weight factor is given by: c (s3*RMS)**2, where RMS is the RMS of the past fit c-------------------------------------------------------------- c c INPUT: n = Number of data points c np = Polynomial order c {t} = Time values c {x} = Time series values c c OUTPUT: {xi} = {best fitting polynomial} c sig = Standard deviation of {x}-{pf} c stdv = Non-weighted and unbiased standard deviation c of {x}-{pf} c c============================================================== implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333),xw(30333),we(30333) dimension ti(30333),xi(30333),c(995) c nd = np itp = 1 isp = 1 nn = n c s3=1.0d0 nit=20 sig=1.0d30 sig0=1.0d0 eps=0.001d0 call astat(x,n,aver,sig0) c c D = SUM (x-f)^2/(cc+(x-f)^2) c c................................ Initialize residuals do 2 i=1,n xw(i)=0.0d0 2 continue c c................................ START ITERATION CYCLE do 1 it=1,nit c if(it.le.2) go to 4 rsig=dabs(1.0d0-sig/sig0) if(rsig.lt.eps) go to 1 sig0=sig 4 continue c c................................ Update weights cc=s3*sig0 c2=cc*cc s=0.0d0 do 5 i=1,n dd=xw(i) d2=dd*dd d2=1.0d0/(c2+d2) s=s+d2 we(i)=d2 5 continue s=1.0d0/s do 6 i=1,n we(i)=s*we(i) 6 continue c call polfit_w(itp,isp,n,t,x,we,nd,nn,ti,xi,c) s=0.0d0 a=0.0d0 do 8 i=1,n d=x(i)-xi(i) xw(i)=d d2=d*d s=s+we(i)*d2 a=a+d2 8 continue sig=dsqrt(s) stdv=dsqrt(a/dfloat(n-np)) c 1 continue c return end c c subroutine robust_pol2(np,n,t,x,c,sig) c----------------------------------------------------------------- c This is a robust POLYNOMIAL fit with Cauchy kernel c================================================================= c c INPUT: np = Polynomial order c n = Number of data points c {t} = Time values c {x} = Time series values c c OUTPUT: {c} = Coefficients of the best-fitting polynomial c sig = Square root of the sum of the weighted c squared differences between {x} and the c polynomial fit. The sum of the weights is c equal to 1. c c NOTE: The constant in the weight factor is given by: c (s3*RMS)**2, where RMS is the RMS of the past fit c c================================================================= implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333) dimension xw(30333),we(30333) dimension ti(30333),xi(30333),c(995) c r=2.0d0/(t(n)-t(1)) tmid=(t(1)+t(n))/2.0d0 c nd = np itp = 1 isp = 1 nn = n c s3=1.0d0 nit=20 sig=1.0d30 sig0=1.0d0 eps=0.001d0 call astat(x,n,aver,sig0) c c D = SUM (x-f)^2/(cc+(x-f)^2) c c................................ Initialize residuals do 2 i=1,n xw(i)=0.0d0 2 continue c c................................ START ITERATION CYCLE do 1 it=1,nit c if(it.le.2) go to 4 rsig=dabs(1.0d0-sig/sig0) if(rsig.lt.eps) go to 1 sig0=sig 4 continue c c................................ Update weights cc=s3*sig0 c2=cc*cc s=0.0d0 do 5 i=1,n dd=xw(i) d2=dd*dd d2=1.0d0/(c2+d2) s=s+d2 we(i)=d2 5 continue s=1.0d0/s do 6 i=1,n we(i)=s*we(i) 6 continue c call polfit_w(itp,isp,n,t,x,we,nd,nn,ti,xi,c) s=0.0d0 do 8 i=1,n d=x(i)-xi(i) xw(i)=d s=s+we(i)*d*d 8 continue sig=dsqrt(s) c 1 continue c return end c c subroutine polfit_w(itp,isp,n,t,x,we,nd,nn,ti,xi,c) c c This is a polynomial fitting routine with *weights* c c Input time series to be fitted: c c (t(i),x(i)), i=1,2,...,n c c c(i), i=1,2,...,nd+1 regression coefficients c c nd = degree of the polynomial to be fitted ( must be lower than 20 ) c c Output time series: c (ti(i),xi(i)), i=1,2,...,nn c c ti(1)=t(1) c ti(nn)=t(n) c c Input parameters/arrays: n,t,x,nd,nn c ~~~~~~~~~~~~~~~~~~~~~~~~ c c Output parameters/arrays: tt,xx,c c ~~~~~~~~~~~~~~~~~~~~~~~~~ c c Note: We assume that the moments of time are monotonic, i.e., c ~~~~~ t(i) > t(i-1) c c------------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) dimension t(30333),g(995,996) dimension x(30333),c(995),td(21,30333) dimension ti(30333),xi(30333),u(30333) dimension we(30333) c if(n.gt.30333) write(*,*) 'Too many data points!' if(n.gt.30333) return if(nd.gt.20) write(*,*) 'Too high degree polynomial !' if(nd.gt.20) return c np=nd+1 c c Return, if the number of data points is too low c if(n.le.np) write(*,*) 'Too few data points !' if(n.le.np) return c it = itp is = isp c c it = 0 --- use array t(i) as the independent variable c = 1 --- transform t(i) to the interval [-1,1] c c is = 0 --- compute equidistant interpolated time series c = 1 --- use the same values of t(i) for interpolation c (ti(i)=t(i), i=1,2,...,n) c if(is.ne.1) go to 1 if(n.ne.nn) write(*,100) 100 format('In the polinomial fit: nn must be equal to n if is=1!') nn=n 1 continue c c transform t c tmin=t(1) tmax=t(n) r=2.0d0/(tmax-tmin) tmid=(tmin+tmax)/2.0d0 if(it.gt.0) go to 20 do 23 i=1,n u(i)=t(i) 23 continue go to 21 20 continue do 22 i=1,n u(i)=r*(t(i)-tmid) 22 continue 21 continue c c compute the various power of {t} c np1=np+1 do 500 i=1,n td(1,i)=1.0d0 do 501 j=2,np k=j-1 td(j,i)=u(i)*td(k,i) 501 continue 500 continue c c************************ c compute the fit * c************************ c do 1001 j=1,np c(j)=0.0d0 1001 continue c c**** computation of the normal matrix c do 11 i=1,np do 12 j=i,np s=0.0d0 do 13 k=1,n s=s+we(k)*td(i,k)*td(j,k) 13 continue g(i,j)=s 12 continue 11 continue do 53 i=1,np do 54 j=i,np g(j,i)=g(i,j) 54 continue 53 continue c do 55 i=1,np s=0.0d0 do 14 k=1,n s=s+we(k)*td(i,k)*x(k) 14 continue g(i,np1)=s 55 continue c c**** compute the solution c call gelim1(g,c,np,IFLAG) c c compute the fitted/interpolated time series c if(is.eq.1) go to 24 dt=(tmax-tmin)/dfloat(nn-1) do 26 i=1,nn ti(i)=tmin+dt*dfloat(i-1) 26 continue go to 27 24 continue do 25 i=1,n ti(i)=t(i) 25 continue 27 continue c if(it.eq.0) go to 28 do 29 i=1,nn u(i)=r*(ti(i)-tmid) 29 continue go to 30 28 continue do 31 i=1,nn u(i)=ti(i) 31 continue 30 continue c do 520 i=1,nn time=u(i) s=c(nd+1) do 521 j=1,nd j1=nd+1-j s=s*time+c(j1) 521 continue xi(i)=s 520 continue c return end c c subroutine cut_ts(npr,nt,t,x,n,u,v) c---------------------------------------------------------------- c This routine cuts *npr* data points on both sides of {t,xt} c---------------------------------------------------------------- c c INPUT: - npr :: Number of data points to be cut c - nt :: Original number of data points c - {t,x} :: Original time series c c OUTPUT: - n :: New number of data points c - {u,v} :: Truncated time series c================================================================ c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333) dimension u(30333),v(30333) c n1=npr+1 n2=nt-npr k=0 do 4 i=1,nt if(i.lt.n1) go to 4 if(i.gt.n2) go to 4 k=k+1 u(k)=t(i) v(k)=x(i) 4 continue n=k c return end c c subroutine AR_LR(m,n,x,c) c------------------------------------------------------------------ c This routine computes TWO-SIDE AUTOREGRESSIVE fit c------------------------------------------------------------------ c c DATE: - 2020.02.17 c c INPUT: - m :: Order of the AR model c - {x(i); i=1,2,...,n} :: Input time series c c OUTPUT: - {c(j); j=1,2,...,m} c c NOTE: - This model is two-sided, i.e.: c x(i) = 0.5*(c1*x(i+1)+c2*x(i+2)+ ... c c1*x(i-1)+c2*x(i-2)+ ... c================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333) dimension c(995) dimension h(995,30333) c c------------------------------------------------------------------ c c............................... Set LEFT only fit functions {h} n1=1 n2=m do 1 k=n1,n2 do 2 i=1,m i2=k+i h(i,k)=x(i2) 2 continue 1 continue c c............................... Set RIGHT only fit functions {h} n1=n-m+1 n2=n do 3 k=n1,n2 do 4 i=1,m i1=k-i h(i,k)=x(i1) 4 continue 3 continue c c............................... Set TWO-SIDED fit functions {h} n1=m+1 n2=n-m do 5 k=n1,n2 do 6 i=1,m i1=k-i i2=k+i h(i,k)=0.5d0*(x(i1)+x(i2)) 6 continue 5 continue c c............................... Solve the LS problem n1=1 n2=n call solve_norm(m,n1,n2,h,x,c) c return end c c subroutine LR_pred(m,npr,n,t,x,xf1,xf2,xp1,xp2, &tp1,tp2,xw,sig) c--------------------------------------------------------------------- c This routine extends the time series {n,t,x} in BOTH directions c--------------------------------------------------------------------- c c INPUT: - m :: AR order c - npr :: Number of data points to be predicted c - n :: Number of data points of the input time series c - {t} :: Moments of time of the input time series c - {x} :: Values of the input time series c c OUTPUT:- {xf1} :: Backward prediction in [1,n-m] c - {xf2} :: Forward prediction in [m+1,n] c - {xp1} :: Backward prediction: npr values, BEFORE t(1) c - {tp1} :: Time values for {xp1} c - {xp2} :: Forward prediction: npr values, AFTER t(n) c - {tp2} :: Time values for {xp2} c - {xw} :: Residuals for {x(i); i=1,2,...,n} c - sig :: Standard deviation of the FORWARD fit c c c NOTES: - ORIGINAL plan: c The extension is made ITERATIVELY within the c framework of Fahlman & Ulrich (1982, MN, 199, 53): c this means prediction from the AR model and LS c computation of the predicted values by using the c AR coefficients computed from the observed data. c c - ACTUAL implementation: c A SIMPLE AR prediction. [For one-sided prediction the c method of Fahlman & Ulrich may be inefficient] c c - The input data are interpolated to an equidistant grid. c The prediction is performed on this grid. The predicted c values are given on this EQUIDISTANT timebase. c c - We assume that the moments of time are monotonic, i.e., c t(i) > t(i-1) c c===================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension xd(30333),yd(30333) dimension xp1(30333),xp2(30333) dimension tp1(30333),tp2(30333) dimension xf1(30333),xf2(30333) dimension t(30333),x(30333) dimension u(30333),v(30333),xw(30333) dimension c(995) c write(*,8) 8 format('... AR prediction') c c.................................... Compute average sampling rate tmin=t(1) tmax=t(n) dt=(tmax-tmin)/dfloat(n-1) c c.................................... Interpolate data on that grid IQ=1 N1=n N2=n do 5 i=1,n u(i)=t(i) v(i)=x(i) xd(i)=tmin+dt*dfloat(i-1) 5 continue CALL INTER2(IQ,u,v,N1,xd,yd,N2) c c.................................... Compute AR coefficients call AR_LR(m,n,yd,c) c c.................................... Backward (L) prediction from the above model ip=-1 call ar_pred(ip,m,n,yd,c,npr,xp1) do 6 i=1,npr tp1(i)=tmin-dt*dfloat(i) 6 continue c c.................................... Forward (R) prediction from the above model ip=1 call ar_pred(ip,m,n,yd,c,npr,xp2) do 7 i=1,npr tp2(i)=tmax+dt*dfloat(i) 7 continue c c.................................... Backward (L) data prediction n1=1 n2=n-m do 1 i=n1,n2 s=0.0d0 do 2 j=1,m s=s+c(j)*yd(j+i) 2 continue xf1(i)=s xw(i)=s-yd(i) 1 continue c c.................................... Forward (R) data prediction n1=m+1 n2=n sum=0.0d0 do 3 i=n1,n2 s=0.0d0 do 4 j=1,m s=s+c(j)*yd(i-j) 4 continue xf2(i)=s d=s-yd(i) xw(i)=d sum=sum+d*d 3 continue sig=dsqrt(sum/dfloat(n2-n1+1-m)) c return end c c subroutine ar_pred(ip,m,n,x,c,npr,xp) c------------------------------------------------------------------ c This is an AR prediction of "npr" values preceeding (ip=-1) c or following (ip=+1) the dataset in [1,n] c------------------------------------------------------------------ c c NOTE: - The index of {xp} grows as we depart from the c 1st/last data point of the original time series c c================================================================== implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333),xp(30333) dimension c(995),y(995),z(995) c c............................... Set prediction basis {y} if(ip.eq.-1) go to 6 do 3 j=1,m k=n-j+1 y(j)=x(k) 3 continue go to 7 6 continue do 8 j=1,m y(j)=x(j) 8 continue 7 continue c c............................... Start predicting "npr" values do 1 i=1,npr c s=0.0d0 do 2 j=1,m s=s+c(j)*y(j) 2 continue xp(i)=s c c............................... Copy shifted {y} to {z} do 5 j=2,m z(j)=y(j-1) 5 continue z(1)=s c............................... Update {y} do 4 j=1,m y(j)=z(j) 4 continue c 1 continue c return end c c subroutine solve_norm(m,n1,n2,h,x,c) c------------------------------------------------------------------ c This routine solves the Least Squares problem derived from c the design matrix {h(i,j)} and data array {x(k)}: c c x_est (k) = SUM_j c(j)*h(j,k) c------------------------------------------------------------------ c c DATE: - 2020.01.31 c c INPUT: - m :: Number of parameters to be fitted c - n1 :: Index of the first array element c to be fitted c - n2 :: Index of the last array element c to be fitted c - {h(i,j)} :: m x (n2-n1+1) matrix c - {x} :: Data to be fitted c c OUTPUT: - {c} :: Regression coefficients c c NOTE: - The design matrix {h} is defined in [n1,n2], where c n2 > n1 and 1 < n1,n2 < n c c================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333),c(995) dimension h(995,30333) dimension g(995,996) c c------------------------------------------------------------------ c m1=m+1 c c............................... Compute normal matrix do 3 i=1,m do 4 j=i,m s=0.0d0 do 5 k=n1,n2 s=s+h(i,k)*h(j,k) 5 continue g(i,j)=s g(j,i)=s 4 continue 3 continue c c............................... Compute RHS do 7 i=1,m s=0.0d0 do 15 k=n1,n2 s=s+h(i,k)*x(k) 15 continue g(i,m1)=s 7 continue c c............................... Compute solution call gelim1(g,c,m,IFLAG) c c return end c c subroutine ar_four_ar(npr,nt,t,xf,tp1,xp1,tp2,xp2,xfa) c---------------------------------------------------------------------- c This routine combines the FOURIER and the AR time series c---------------------------------------------------------------------- c c INPUT: - npr :: Number of AR-predicted data points c - nt :: Original number of data points c - {t} :: Moments of time of the original time series c - {xf} :: Values of the FOURIER fit on {t} c - {tp1} :: Time values for {xp1} c - {xp1} :: Backward prediction: BEFORE t(npr+1) c - {tp2} :: Time values for {xp2} c - {xp2} :: Forward prediction: AFTER t(nt-npr) c c OUTPUT: - {xfa} :: {xp1}+{xf}+{xp2} combined time series on {t} c c NOTES: - {xf} is kept ONLY in the "inner part" of the time c series (i.e., between i=npr+1 and nt-npr c - The predictions are given on EQUIDISTANT timebase. c At the end, these values are INTERPOLATED back to the c INPUT timebase given by {t}. c c====================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),xf(30333),xfa(30333) dimension xp1(30333),xp2(30333) dimension tp1(30333),tp2(30333) dimension u(30333),v(30333) dimension xd(30333),yd(30333) c c....................................... INTERPOLATE prediction c back to timebase {t} IQ=1 N1=npr N2=npr do 1 i=1,npr j=npr-i+1 u(i)=tp1(j) v(i)=xp1(j) xd(i)=t(i) 1 continue CALL INTER2(IQ,u,v,N1,xd,yd,N2) do 2 i=1,npr xfa(i)=yd(i) 2 continue do 3 i=1,npr u(i)=tp2(i) v(i)=xp2(i) xd(i)=t(i+nt-npr) 3 continue CALL INTER2(IQ,u,v,N1,xd,yd,N2) do 4 i=1,npr xfa(i+nt-npr)=yd(i) 4 continue c c............................... Fill in the non-extrapolated part c by the FOURIER fit n1=npr+1 n2=nt-npr do 5 i=n1,n2 xfa(i)=xf(i) 5 continue c return end c c subroutine save_rec(n,t,x_rec,xft,xsyn,trsyn, &tfa_r,four_r,qtran) c---------------------------------------------------------- c Save reconstructed/filtered time series {x_rec} with c the best trapezoidal fit {xft} and the synthetic c signal {xsyn}, if test data are used [if not, then c this array is set equal to zero] c ... Also saved are: {tfa_r} and {four_r}, TFA and c FOUR vectors from the full/reconstructed model. c---------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x_rec(30333) dimension xft(30333),xsyn(30333),trsyn(30333) dimension tfa_r(30333),four_r(30333) c open(18,file='rec_tfa.dat') write(18,56) qtran 56 format('#',/,'# QTRAN = ',f7.5,/, &'#',91('='),/, &'# TIME',11x,'X_REC',9x,'XFT',10x,'SYNT',7x,'SYN_TRAN', &' FOUR_REC TFA_REC',/, &'#',91('-')) do 1 i=1,n write(18,2) t(i),x_rec(i),xft(i),xsyn(i),trsyn(i), &four_r(i),tfa_r(i) 1 continue 2 format(f14.7,6(2x,f11.7)) close(18) c return end c c subroutine save_sp(nb_sp,nf1,fmin1,df1,p) c---------------------------------------------------------- c - This routine saves the "nb_sp-binned-maximum-value" c spectrum as "sp.dat" c---------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension p(155555) c nst = nf1/nb_sp nf2 = nf1/nst df2 = nst*df1 c open(25,file='sp.dat') do 1 i=1,nf1,nst i1=i f1=fmin1+df1*dfloat(i1-1) i2=i+nst f2=fmin1+df1*dfloat(i2-1) freq=(f1+f2)*0.5d0 s=-1.0d30 i0=i1-1 do 2 j=1,nst k=i0+j s=dmax1(s,p(k)) 2 continue write(25,3) freq,s 3 format(f10.6,1x,f12.8) 1 continue close(25) c return end c c subroutine save_spec(sname,nb_sp,nf1,fmin1,df1,p) c---------------------------------------------------------- c - This routine saves the "nb_sp-binned-maximum-value" c spectrum as "spec/sname.sp" c---------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*9 sname character*17 spname c dimension p(155555) c spname= trim('spec/'//sname//'.sp') c nst = nf1/nb_sp nf2 = nf1/nst df2 = nst*df1 c open(19,file=spname) do 1 i=1,nf1,nst i1=i f1=fmin1+df1*dfloat(i1-1) i2=i+nst f2=fmin1+df1*dfloat(i2-1) freq=(f1+f2)*0.5d0 s=-1.0d30 i0=i1-1 do 2 j=1,nst k=i0+j s=dmax1(s,p(k)) 2 continue write(19,3) freq,s 3 format(f10.6,1x,f12.8) 1 continue close(19) c return end c c subroutine save_sp_iw(nb_sp,nf1,fmin1,df1,p,spn) c c---------------------------------------------------------- c This routine saves the "nbsp-binned-maximum-value" c frequency spectrum in the working directory under c the name "spn" c--------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*8 spn c dimension p(155555) c nst = nf1/nb_sp nf2 = nf1/nst df2 = nst*df1 c open(19,file=spn) do 1 i=1,nf1,nst i1=i f1=fmin1+df1*dfloat(i1-1) i2=i+nst f2=fmin1+df1*dfloat(i2-1) freq=(f1+f2)*0.5d0 s=-1.0d30 i0=i1-1 do 2 j=1,nst k=i0+j s=dmax1(s,p(k)) 2 continue write(19,3) freq,s 3 format(f10.6,1x,f12.8) 1 continue close(19) c return end c c subroutine save_lc_iw(n,t,y,xbt,lcn) c c---------------------------------------------------------- c This routine saves the time series {n,t,y} in the c working directory under the name "lcn" c--------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*8 lcn c dimension t(30333),y(30333),xbt(30333) c open(19,file=lcn) do 1 i=1,n write(19,3) t(i),y(i),xbt(i) 3 format(f13.7,2(1x,f10.7)) 1 continue close(19) c return end c c subroutine save_pa_iw(sname,p0,epc,pan) c------------------------------------------------------------------- c This routine saves folding parameters c------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*8 pan character*9 sname c open(24,file=pan) nper=1 f0=1.0d0/p0 write(24,40) sname,nper,f0,epc 40 format(a9,/,i2,/,f11.7,/,f13.6) close(24) c return end c c subroutine parin(isurvey,jsm,itemp,nstar,fmin1,fmax1,nf1, &iwhite,nb,qmi,qma,itest,tfreq0,tfreq1,dep0,dep1,t14_0, &t14_1,pht0,pht1,frin0,frin1,amootv,phootv,frootv,iflare, &igen,std,fdep,iseed,issp,isflc,ispl,iplot,asig,ic_flare, &ibg,ixy,m_four,m_opt) c------------------------------------------------------------------- c This routine reads in parameters more frequently changed while c running this code c=================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*9 name character*1 eq c call SYSTEM('grep -v "#" tran_k2_v0.par > temp.dat') open(1,file='temp.dat') c read(1,*) name,eq,isurvey read(1,*) name,eq,jsm read(1,*) name,eq,itemp read(1,*) name,eq,nstar read(1,*) name,eq,fmin1 read(1,*) name,eq,fmax1 read(1,*) name,eq,nf1 read(1,*) name,eq,iwhite read(1,*) name,eq,nb read(1,*) name,eq,qmi read(1,*) name,eq,qma c================================= read(1,*) name,eq,itest read(1,*) name,eq,tfreq0 read(1,*) name,eq,dep0 read(1,*) name,eq,t14_0 read(1,*) name,eq,pht0 read(1,*) name,eq,frin0 c================================= read(1,*) name,eq,tfreq1 read(1,*) name,eq,dep1 read(1,*) name,eq,t14_1 read(1,*) name,eq,pht1 read(1,*) name,eq,frin1 c================================= read(1,*) name,eq,frootv read(1,*) name,eq,amootv read(1,*) name,eq,phootv read(1,*) name,eq,iflare read(1,*) name,eq,igen read(1,*) name,eq,std read(1,*) name,eq,fdep read(1,*) name,eq,iseed c================================= read(1,*) name,eq,issp read(1,*) name,eq,isflc read(1,*) name,eq,ispl read(1,*) name,eq,iplot read(1,*) name,eq,asig read(1,*) name,eq,ic_flare read(1,*) name,eq,ibg read(1,*) name,eq,ixy read(1,*) name,eq,m_four read(1,*) name,eq,m_opt c================================= close(1) c return end c c subroutine minmax(n,x,xmin,xmax) c c This routine computes (min,max) of {x(i); i=1,...,n} c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333) c xmin = 1.0d30 xmax =-1.0d30 do 1 i=1,n xx=x(i) xmin = dmin1(xmin,xx) xmax = dmax1(xmax,xx) 1 continue c return end c c FUNCTION ARAND(IX) implicit real*8 (a-h,o-z) INTEGER A,P,IX,B15,B16,XHI,XALO,LEFTLO,FHI,K DATA A/16807/,B15/32768/,B16/65536/,P/2147483647/ XHI = IX/B16 XALO = (IX - XHI * B16) * A LEFTLO = XALO / B16 FHI = XHI * A + LEFTLO K = FHI / B15 IX = ((( XALO - LEFTLO * B16 ) - P ) + (FHI - K * B15 ) * B16) + K IF (IX.LT.0) IX = IX + P ARAND = DFLOAT(IX) * 4.65661287D-10 RETURN END C C FUNCTION GAUSS(ISEED) C C this routine generates Gaussian random variables of unit variance C implicit real*8 (a-h,o-z) implicit integer*4 (i-n) C C******** TRANSFORMING TO GAUSSIAN VARIABLE ******* C******** ******* C SOURCE: NUMERICAL RECIPES, P. 203 C 1 CONTINUE P1=2.0D0*ARAND(ISEED)-1.0D0 P2=2.0D0*ARAND(ISEED)-1.0D0 RR=P1*P1+P2*P2 IF(RR.GE.1.0D0) GO TO 1 FAC=DSQRT(-2.0D0*DLOG(RR)/RR) GAUSS=P1*FAC C RETURN END c c subroutine astat(y,nn,aver,disper) c c This routine computes the average and unbiased standard deviation of {y} c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) dimension y(30333) c s=0.0d0 do 1 i=1,nn s=s+y(i) 1 continue aver=s/dfloat(nn) di=0.0d0 do 2 i=1,nn d=y(i)-aver di=di+d*d 2 continue disper=0.0d0 if(nn.gt.1) disper=dsqrt(di/dfloat(nn-1)) c return end C C subroutine mfourdec(epoch,n,t,x,m,w,xw,sig,a00,aa,pp) C C---------------------------------------------------------------- C THIS CODE FITS A FOURIER SUM TO A TIME-SERIES C---------------------------------------------------------------- C C EPOCH = THE FIT IS COMPUTED ON A TIMEBASE OF {T(I)-EPOCH} C N = NUMBER OF DATA POINTS C T(I) = TIME C X(I) = TIME-SERIES C M = ORDER OF THE FOURIER FIT c W(J) = J-TH FREQUENCY (J=1,2,...,M) C XW(I) = PREWHITENED TIME-SERIES C SIG = STANDARD DEVIATION OF THE FIT [UNBAISED ESTIMATE] C A00 = ZERO FREQUENCY COMPONENT C AA(J) = AMPLITUDE OF THE J-TH COMPONENT C PP(J) = PHASE OF THE J-TH COMPONENT (IN [RAD]) C C FOURIER DECOMPOSITION HAS THE FOLLOWING FORM: C C A0 + A1*SIN(2*PI*F1*(T-EPOCH)+FI1) + ... C C================================================================ C IMPLICIT REAL*8 (A-H,O-Z) IMPLICIT INTEGER*4 (I-N) C DIMENSION X(30333),T(30333),G(995,996),W(995) DIMENSION U(30333),XW(30333) DIMENSION A(995),AA(995),PP(995) C P7 = 8.0D0*DATAN(1.0D0) c DO 30 I=1,N U(I)=T(I)-EPOCH 30 CONTINUE C M2=2*M M3=M2+1 M4=M3+1 CALL NORM(N,M,U,X,W,G) CALL gelim1(G,A,M3,IFLAG) A0=A(1) C C Compute amplitudes and phases C K=0 DO 37 J=2,M3,2 K=K+1 J1=J+1 AMP=DSQRT(A(J)*A(J)+A(J1)*A(J1)) PH=A(J1)/AMP PH=DACOS(PH) IF(A(J).LT.0.0D+00) PH=P7-PH A(K)=AMP AA(K)=AMP PP(K)=PH 37 CONTINUE A00=A0 C C Compute residuals and standard deviation C DISP=0.0d0 DO 2000 I=1,N S=A00 TIME=U(I) DO 2001 K=1,M PHASE=W(K)*TIME S=S+AA(K)*DSIN(P7*PHASE+PP(K)) 2001 CONTINUE DEVI=X(I)-S XW(I)=DEVI DISP=DISP+DEVI*DEVI 2000 CONTINUE SIG=DSQRT(DISP/DFLOAT(N-2*M-1)) C RETURN END C C SUBROUTINE NORM(N,M,T,X,F,G) C CALCULATION OF THE ELEMENTS OF THE NORMAL MATRIX AND R.H.S. implicit real*8 (a-h,o-z) implicit integer*4 (i-n) DIMENSION G(995,996),T(30333),X(30333),F(995) P7=8.0D0*DATAN(1.0D0) M3=2*M+1 M4=M3+1 L=0 G(1,1)=DFLOAT(N) DO 1 J=2,M3,2 S=0.0D+00 C=0.0D+00 L=L+1 J1=J+1 FL=F(L) DO 2 I=1,N PH=T(I)*FL PH=P7*PH S=S+DSIN(PH) 2 C=C+DCOS(PH) G(1,J)=C 1 G(1,J1)=S L1=0 DO 3 J=2,M3,2 L1=L1+1 J1=J+1 L2=L1-1 FL1=F(L1) DO 4 K=J,M3,2 K1=K+1 L2=L2+1 FL2=F(L2) S1=0.0D+00 S2=0.0D+00 C1=0.0D+00 C2=0.0D+00 DO 5 I=1,N PH1=T(I)*FL1 PH1=P7*PH1 PH2=T(I)*FL2 PH2=P7*PH2 SPH1=DSIN(PH1) CPH1=DCOS(PH1) SPH2=DSIN(PH2) CPH2=DCOS(PH2) C1=C1+CPH1*CPH2 S1=S1+CPH2*SPH1 C2=C2+SPH2*CPH1 5 S2=S2+SPH1*SPH2 G(J,K)=C1 G(J,K1)=C2 G(J1,K)=S1 4 G(J1,K1)=S2 3 CONTINUE S=0.0D+00 DO 6 I=1,N 6 S=S+X(I) G(1,M4)=S L=0 DO 7 J=2,M3,2 S=0.0D+00 C=0.0D+00 L=L+1 J1=J+1 FL=F(L) DO 8 I=1,N PH=T(I)*FL PH=P7*PH S=S+X(I)*DSIN(PH) 8 C=C+X(I)*DCOS(PH) G(J,M4)=C 7 G(J1,M4)=S DO 9 J=1,M3 DO 10 K=J,M3 10 G(K,J)=G(J,K) 9 CONTINUE RETURN END C c subroutine fdft(n,t,y,nf,fmin,df,p,pfr,pam) c c------------------------------------------------------------- c This is a FAST single frequency DFT routine based on c the idea of Kurtz (1985, MN, 213, 773) c [and mine ... a bit earlier ... unpublished ...] c------------------------------------------------------------- c c {t(i),y(i)}, i=1,2, ... , n --- time-series c {p(j)}, j=1,2, ... , nf --- ampl. spectr. c fmin --- min. test frequency c df --- frequency step c pfr --- peak frequency c pam --- peak amplitude c c WARNING: - Maximum number of data points MUST be c ~~~~~~~~ lower than *30333* c c - Maximum number of spectrum points MUST be c lower than *155555* c............................................................. c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c real*4 u(30333),v(30333),pi2,fp,sum real*4 ui,vi,a,da,omin,dom,s1i,c1i,s0i,c0i,s2i,sj,cj real*4 s(155555),c(155555) real*4 s1(30333),c1(30333),s0(30333),c0(30333) c dimension t(*),y(*),p(*) c fp = 2.0/float(n) pi2 = 8.0*atan(1.0) pam = -1.0 c if(n.gt.30333) write(*,*) ' n MUST be lower that 30333!' if(n.gt.30333) return if(nf.gt.155555) write(*,*) ' nf MUST be lower that 155555!' if(nf.gt.155555) return c c================================= c Set temporal time series c================================= c sum=0.0 t1=t(1) do 103 i=1,n u(i)=sngl(t(i)-t1) sum=sngl(sum+y(i)) 103 continue sum=sngl(sum/dfloat(n)) do 109 i=1,n v(i)=sngl(y(i)-sum) 109 continue c omin = sngl(pi2*fmin) dom = sngl(pi2*df) do 3 i=1,nf s(i) = 0.0 c(i) = 0.0 3 continue c do 5 i=1,n ui = u(i) vi = v(i) a = omin*ui da = dom*ui s1(i) = vi*sin(a) c1(i) = vi*cos(a) s0(i) = sin(da) c0(i) = cos(da) 5 continue c c... Compute amplitude spectrum c do 1 i=1,n s1i = s1(i) c1i = c1(i) s0i = s0(i) c0i = c0(i) do 2 j=1,nf s(j) = s(j) + s1i c(j) = c(j) + c1i s2i = s1i s1i = s1i*c0i+c1i*s0i c1i = c1i*c0i-s2i*s0i 2 continue 1 continue c do 4 j=1,nf sj = s(j) cj = c(j) a = fp*sqrt(sj*sj+cj*cj) p(j) = a if(a.lt.pam) go to 4 pam=a pfr=fmin+df*float(j-1) 4 continue c return end c c subroutine eebls(n,t,x,u,v,nf,fmin,df,nb,qmi,qma, &p,bper,bpow,depth,qtran,in1,in2) c c------------------------------------------------------------------------ c >>>>>>>>>>>> This routine computes BLS spectrum <<<<<<<<<<<<<< c c [ see Kovacs, Zucker & Mazeh 2002, A&A, Vol. 391, 369 ] c c This is the slightly modified version of the original BLS routine c by considering Edge Effect (EE) as suggested by c Peter R. McCullough [ pmcc@stsci.edu ]. c c This modification was motivated by considering the cases when c the low state (the transit event) happened to be devided between c the first and last bins. In these rare cases the original BLS c yields lower detection efficiency because of the lower number of c data points in the bin(s) covering the low state (the transit event). c c For further comments/tests see www.konkoly.hu/staff/kovacs.html c c The following additional modifications were made on June 7, 2004: c c - Constraint concerning the minimum number of bins in the transit c has been abandoned. c - Total time span of the time series is computed without the c assumption of sorted time values. c - Relative transit length is computed by using the bin indices of c the ingress and egress and not as the ratio of the number of c data points in the two states. c------------------------------------------------------------------------ c c Input parameters: c ~~~~~~~~~~~~~~~~~ c c n = number of data points c t = array {t(i)}, containing the time values of the time series c x = array {x(i)}, containing the data values of the time series c u = temporal/work/dummy array, must be dimensioned in the c calling program in the same way as {t(i)} c v = the same as {u(i)} c nf = number of frequency points in which the spectrum is computed c fmin = minimum frequency (MUST be > 0) c df = frequency step c nb = number of bins in the folded time series at any test period c qmi = minimum fractional transit length to be tested c qma = maximum fractional transit length to be tested c c Output parameters: c ~~~~~~~~~~~~~~~~~~ c c p = array {p(i)}, containing the values of the BLS spectrum c at the i-th frequency value -- the frequency values are c computed as f = fmin + (i-1)*df c bper = period at the highest peak in the frequency spectrum c bpow = value of {p(i)} at the highest peak c depth= depth of the transit at *bper* c qtran= fractional transit length [ T_transit/bper ] c in1 = bin index at the start of the transit [ 0 < in1 < nb+1 ] c in2 = bin index at the end of the transit [ 0 < in2 < nb+1 ] c c Remarks: c ~~~~~~~~ c c -- *fmin* MUST be greater than *1/total time span* c -- *nb* MUST be lower than *nbmax*. c -- Dimensions of arrays {y(i)} and {ibi(i)} MUST be greater than c or equal to *nbmax*. c -- The lowest number of points allowed in the transit is equal c to MAX(minbin,qmi*N), where *qmi* is the minimum transit c length/trial period, *N* is the total number of data points, c *minbin* is the preset minimum number of the data points in c the transit. c c======================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(*),x(*),u(*),v(*),p(*) dimension y(30333),ibi(30333) c minbin = 10 nbmax = 30333 jn1 = 1 jn2 = 1 rn3 = 0.0 s3 = 0.0 nb0 = nb c if(nb.gt.nbmax) write(*,*) ' NB > NBMAX !!' if(nb.gt.nbmax) nb=nbmax c c... Compute total time span c tmin = 1.0d30 tmax = -1.0d30 do 10 i=1,n tmin=dmin1(t(i),tmin) tmax=dmax1(t(i),tmax) 10 continue tot=tmax-tmin c fminold=fmin if(fmin.lt.1.0d0/tot) write(*,*) ' fmin < 1/T !!' if(fmin.lt.1.0d0/tot) fmin=1.1/tot c------------------------------------------------------------------------ c rn=dfloat(n) rnb=dfloat(nb) kma=idint(rnb*qma)+1 kkmi=idint(rn*qmi) if(kkmi.lt.minbin) kkmi=minbin bpow=0.0d0 c c The following variables are defined for the extension c of arrays ibi() and y() [ see below ] c nb1 = nb+1 nbkma = nb+kma c c================================= c Set temporal time series c================================= c s=0.0d0 do 103 i=1,n u(i)=t(i)-tmin s=s+x(i) 103 continue s=s/rn do 109 i=1,n v(i)=x(i)-s 109 continue c c****************************** c Start period search * c****************************** c do 100 jf=1,nf f0=fmin+df*dfloat(jf-1) p0=1.0d0/f0 c c====================================================== c Compute folded time series with *p0* period c====================================================== c do 101 j=1,nb y(j) = 0.0d0 ibi(j) = 0 101 continue c do 102 i=1,n ph = u(i)*f0 ph = ph-idint(ph) j = 1 + idint(nb*ph) ibi(j) = ibi(j) + 1 y(j) = y(j) + v(i) 102 continue c c----------------------------------------------- c Extend the arrays ibi() and y() beyond c nb by wrapping c do 104 j=nb1,nbkma jnb = j-nb ibi(j) = ibi(jnb) y(j) = y(jnb) 104 continue c----------------------------------------------- c c=============================================== c Compute BLS statistics for this period c=============================================== c power=0.0d0 c do 1 i=1,nb s = 0.0d0 kk = 0 nb2 = i+kma do 2 j=i,nb2 kk = kk+ibi(j) s = s+y(j) if(kk.lt.kkmi) go to 2 rn1 = dfloat(kk) pow = s*s/(rn1*(rn-rn1)) if(pow.lt.power) go to 2 power = pow jn1 = i jn2 = j rn3 = rn1 s3 = s 2 continue 1 continue c power = dsqrt(power) p(jf) = power c if(power.lt.bpow) go to 100 bpow = power in1 = jn1 in2 = jn2 qtran = dfloat(in2-in1+1)/rnb depth = -s3*rn/(rn3*(rn-rn3)) bper = p0 c 100 continue c c Edge correction of transit end index c if(in2.gt.nb) in2 = in2-nb c c Restore original value of 'fmin' and 'nb' c fmin=fminold nb=nb0 c return end c c subroutine weebls_new(n,t,x,w,u,v,nf,fmin,df,nb,qmi,qma, &p,bper,bpow,depth,qtran,in1,in2) c c------------------------------------------------------------------------ c This routine computes BLS spectrum [ see A&A, Vol. 391, 369 ] c c Version: November 06, 2018 c ~~~~~~~~ c c Description, modifications: c ~~~~~~~~~~~~~~~~~~~~~~~~~~~ c c - Fits boxes with the aid of Least Squares principle to the folded c time series for *nf* trial periods. c c - Considers Edge Effect: c c This was suggested by Peter R. McCullough [ pmcc@stsci.edu ]. c The modification was motivated by considering the case when c the low state (the transit event) is accidently devided between c the first and last bins. In these rare cases the original BLS c yields lower detection efficiency because of the lower number c of data points in the bin(s) covering the low state (the transit c event). For further comments/tests see c www.konkoly.hu/staff/kovacs.html c c - Uses weights in order to consider observational errors: c c Implementation was suggested by Kailash Sahu [ ksahu@stsci.edu ]. c Weighting enters in the general formulation of BLS but it was c considered originally as time consuming and usually not necessary. c c - For more technical details and modifications relative to the c very first version of 2002, see *Remarks* below. c------------------------------------------------------------------------ c c Input parameters: c ~~~~~~~~~~~~~~~~~ c c n = number of data points c t = array {t(i)}, contains the time values of the time series c x = array {x(i)}, contains the data values of the time series c w = array {w(i)}, contains the weights for each item of data c u = temporal/work/dummy array, must be dimensioned in the c calling program in the same way as {t(i)} c v = the same as {u(i)} c nf = number of frequency points in which the spectrum is computed c fmin = minimum frequency (MUST be > 0) c df = frequency step c nb = number of bins in the folded time series at any test period c qmi = minimum fractional transit length to be tested c qma = maximum fractional transit length to be tested c c Output parameters: c ~~~~~~~~~~~~~~~~~~ c c p = array {p(i)}, contains the values of the BLS spectrum. c The frequency corresponding to p(i) is equal to c f = fmin + (i-1)*df c bper = period at the highest peak in the frequency spectrum c bpow = value of {p(i)} at the highest peak c depth= depth of the transit at *bper* c qtran= fractional transit length [ T_transit/bper ] c in1 = bin index at the start of the transit [ 0 < in1 < nb+1 ] c in2 = bin index at the end of the transit [ 0 < in2 < nb+1 ] c c c Remarks: c ~~~~~~~~ c c -- *fmin* MUST be greater than *1/total time span* c -- *nb* MUST be lower than *nbmax* c -- Dimensions of arrays {y(i)} and {wib(i)} MUST be set equal c to 2*nbmax for complete security against array index overflow c -- The sum of the weights in the transit cannot be smaller c than *qmi*. This helps avoiding statistically unstable c situations. c -- In the original BLS code a check was made for the number of c bins covering the transit. This is considered to be unimportant, c and therefore omitted, because the function of this check is c already fulfilled by a similar check of the sum of the weights c in the transit. c -- The fractional transit length *qtran* is computed from the c bin indices of the ingress and egress and not from the number c of data points in the transit, as in the earlier versions of c BLS. c -- The time values {t(i)} need not to be sorted. c -- A check is made on the weights {w(i)} in order to verify if c their sum is within 1+/-eps. c -- The average of {v(i)=w(i)x(i)} is subtracted from {v(i)}, i.e., c the input time series {x(i)} need not to be zero-averaged. c -- The sign definition of the output parameter *depth* follows c astronomical magnitude definition (i.e., if depth<0, then c it means dimming, lower brightness). c -- Condition "if(j.eq.i) go to 2" is added to the inner loop c on November, 6, 2018. c======================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(*),x(*),w(*),u(*),v(*),p(*) dimension y(30333),wib(30333) dimension ibi(30333) c c------------------------------------------------------------------------ c... Parameter and {w(i)} normalization checks c eps = 1.0d-6 nbmax = 10000 nb0 = nb c c................... These are just dummy data initializations to stop c the compiler whining on "uninitialized" variables jn1 = 1 jn2 = 1 wkb = 0.0d0 sb = 0.0d0 c................... c if(nb.gt.nbmax) write(*,*) ' NB > NBMAX !!' if(nb.gt.nbmax) nb=nbmax s=0.0d0 do 200 i=1,n s=s+w(i) 200 continue d=dabs(s-1.0d0) if(d.lt.eps) go to 10 write(*,*) ' |SUM w(i)-1.0| > eps !!' s=1.0d0/s do 11 i=1,n w(i)=s*w(i) 11 continue 10 continue c c... Compute total time span, check *fmin* c tmin = 1.0d30 tmax = -1.0d30 do 201 i=1,n tmin=dmin1(tmin,t(i)) tmax=dmax1(tmax,t(i)) 201 continue ftot=1.0d0/(tmax-tmin) fmin5=fmin if(fmin5.gt.ftot) go to 12 write(*,*) ' fmin < 1/T !!' fmin5=ftot 12 continue c------------------------------------------------------------------------ c c... Set additional parameters c minbin = 5 rn = dfloat(n) rnb = dfloat(nb) kma = idint(qma*rnb)+1 kkmi= idint(rn*qmi) if(kkmi.lt.minbin) kkmi=minbin bpow= 0.0d0 c c The following variables are defined for the extension c of arrays {wib(i)} and {y(i)} [ see below ] c nb1 = nb+1 nbkma = nb+kma c c------------------------------------------------------------------------ c... Set temporal time series {u(i),v(i)} c c - t(i) --> u(i) > 0; c - x(i), w(i) --> v(i) c swx=0.0d0 do 202 i=1,n u(i)=t(i)-tmin v(i)=w(i)*x(i) swx=swx+v(i) 202 continue swx=swx/rn do 203 i=1,n v(i)=v(i)-swx 203 continue c c****************************** c Start period search * c****************************** c do 100 jf=1,nf f0=fmin5+df*dfloat(jf-1) p0=1.0d0/f0 c c====================================================== c Compute folded time series with period *p0* c====================================================== c do 101 j=1,nb y(j) = 0.0d0 wib(j) = 0.0d0 ibi(j) = 0 101 continue c do 102 i=1,n ph = u(i)*f0 ph = ph-idint(ph) j = 1 + idint(nb*ph) wib(j) = wib(j) + w(i) ibi(j) = ibi(j) + 1 y(j) = y(j) + v(i) 102 continue c c----------------------------------------------- c... Extend the arrays {wib(i)} and {y(i)} c beyond *nb* by wrapping c do 104 j=nb1,nbkma jnb = j-nb wib(j) = wib(jnb) ibi(j) = ibi(jnb) y(j) = y(jnb) 104 continue c----------------------------------------------- c c=============================================== c Compute BLS statistics for this period c=============================================== c power=0.0d0 c do 1 i=1,nb s = 0.0d0 wk = 0.0d0 kk = 0 nb2 = i+kma do 2 j= i,nb2 wk = wk+wib(j) kk = kk+ibi(j) s = s+y(j) c if(kk.lt.kkmi) go to 2 if(j.eq.i) go to 2 c pow = s*s/((1.0d0-wk)*wk) if(pow.lt.power) go to 2 c power = pow jn1 = i jn2 = j wkb = wk sb = s c 2 continue 1 continue c power = dsqrt(power) p(jf) = power c if(power.lt.bpow) go to 100 bpow = power in1 = jn1 in2 = jn2 qtran = dfloat(in2-in1+1)/rnb depth = -sb/((1.0d0-wkb)*wkb) bper = p0 c 100 continue c c... Edge correction of transit end index c if(in2.gt.nb) in2 = in2-nb c nb=nb0 c return end c c subroutine polfit(itp,isp,n,t,x,nd,nn,ti,xi,c) c c This is a polynomial fitting routine c c Input time series to be fitted: c c (t(i),x(i)), i=1,2,...,n c c c(i), i=1,2,...,nd+1 regression coefficients c c nd = degree of the polynomial to be fitted c c Output time series: c (ti(i),xi(i)), i=1,2,...,nn c c ti(1)=t(1) c ti(nn)=t(n) c c Input parameters/arrays: itp,isp,n,t,x,nd,nn c ~~~~~~~~~~~~~~~~~~~~~~~~ c c Output parameters/arrays: ti,xi,c c ~~~~~~~~~~~~~~~~~~~~~~~~~ c c Note: We assume that the moments of time are monotonic, i.e., c ~~~~~ t(i) > t(i-1) c c------------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) dimension t(30333),g(995,996) dimension x(30333),c(995),td(21,30333) dimension ti(30333),xi(30333),u(30333) c if(n.gt.30333) write(*,*) 'Too many data points!' if(n.gt.30333) return if(nd.gt.20) write(*,*) 'Too high degree polynomial !' if(nd.gt.20) nd=20 c np=nd+1 c it = itp is = isp c c it = 0 --- use array t(i) as the independent variable c = 1 --- transform t(i) to the interval [-1,1] c c is = 0 --- compute equidistant interpolated time series c = 1 --- use the same values of t(i) for interpolation c (t(i)=ti(i), i=1,2,...,n=nn) c if(is.ne.1) go to 1 if(n.ne.nn) write(*,100) 100 format(' In the polinomial fit: nn must be equal to n if is=1!') nn=n 1 continue c c transform t c tmin=t(1) tmax=t(n) r=2.0d0/(tmax-tmin) tmid=(tmin+tmax)/2.0d0 do 511 i=1,n u(i)=t(i) if(it.gt.0) u(i)=r*(t(i)-tmid) 511 continue c c change nd if the number of data is too low c if(n.lt.np) np=n np1=np+1 if(np.lt.2) write(*,*) 'Too few data !' if(np.lt.2) return c c compute the various power of t(i), i=1,2,...,n c do 500 i=1,n td(1,i)=1.0d0 do 501 j=2,np k=j-1 td(j,i)=u(i)*td(k,i) 501 continue 500 continue c c************************ c compute the fit * c************************ c do 1001 j=1,np c(j)=0.0d0 1001 continue c c**** computation of the normal matrix c do 11 i=1,np do 12 j=i,np s=0.0d0 do 13 k=1,n s=s+td(i,k)*td(j,k) 13 continue g(i,j)=s 12 continue 11 continue do 53 i=1,np do 54 j=i,np g(j,i)=g(i,j) 54 continue 53 continue c do 55 i=1,np s=0.0d0 do 14 k=1,n s=s+td(i,k)*x(k) 14 continue g(i,np1)=s 55 continue c c**** compute the solution c call gelim1(g,c,np,IFLAG) c c compute the interpolated time series c dt=(tmax-tmin)/dfloat(nn-1) c do 520 i=1,nn ti(i)=tmin+dt*dfloat(i-1) if(is.eq.1) ti(i)=t(i) time=ti(i) if(it.gt.0) time=r*(ti(i)-tmid) s=c(nd+1) do 521 j=1,nd j1=nd+1-j s=s*time+c(j1) 521 continue xi(i)=s 520 continue return end c c subroutine datin8f(fname,n,t,x,bgf) c c----------------------------------------------------------------------- c c WORKS WITH F L U X E S !! c c----------------------------------------------------------------------- c This routine reads in K2 time series as given by the TERRA pipeline c by Petigure - LCs have been downloaded from the IPAC ExoFop site c----------------------------------------------------------------------- c c Input: - fname = file name c ~~~~~~ c c Output: - {t(i),x(i),bgf(i); i=1,2,...,n} c ~~~~~~~ c c Comments: - Method of deriving the NORMALIZED time series: c ~~~~~~~~~ o Employ sigma-clipping [with *avsig* clip parameter] c to calculate the average flux c o Divide the data by the so-obtained average flux c - IMPORTANT: This version assumes that the comment lines c are confined to the top *nh* lines, and ALL c the other lines contain *NUMBERS* only, with c proper data separators (spaces or commas) c c----------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fname character*1 hash c dimension t(30333),x(30333),bgf(30333) c c---------------------------------------------------------------------- c nh = 40 avsig = 3.0d0 bigjd = 2400000.0d0 c c............................................. Read in header open(3,file=fname) do 10 i=1,nh read(3,*) hash 10 continue c c............................................. Read in data n=0 11 continue read(3,*,end=12) TIME_BJD,cad,xn,bgfn n = n+1 t(n) = TIME_BJD-bigjd x(n) = xn bgf(n) = bgfn go to 11 12 continue close(3) c c............................................. Compute outlier-free average call omit1(avsig,x,n,av0,si0,nclip) c c............................................. Normalize time series with the average fac=1.0d0/av0 do 22 i=1,n x(i)=x(i)*fac 22 continue c return end c c subroutine datin8fp(fname,n,t,x,x_p,y_p,bgf) c c----------------------------------------------------------------------- c c WORKS WITH F L U X E S !! c c----------------------------------------------------------------------- c This routine reads in K2 time series as converted by "pdc2pet.f" c from the *.tbl file resulting from the IPAC pipeline c----------------------------------------------------------------------- c c Input: - fname = file name c ~~~~~~ c c Output: - {t(i),x(i),x_p(i),y_p(i),bgf(i); i=1,2,...,n} c ~~~~~~~ c c Comments: - (X,Y) pixel positions are also stored in these files c ~~~~~~~~~ - Method of deriving the NORMALIZED time series: c o Employ sigma-clipping [with *avsig* clip parameter] c to calculate the average flux c o Divide the data by the so-obtained average flux c - IMPORTANT: This version assumes that the comment lines c are confined to the top *nh* lines, and ALL c the other lines contain *NUMBERS* only, with c proper data separators (spaces or commas) c c----------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fname character*1 hash c dimension t(30333),x(30333) dimension x_p(30333),y_p(30333),bgf(30333) c c---------------------------------------------------------------------- c avsig = 3.0d0 bigjd = 2400000.0d0 nh = 5 c c............................................. Read in header open(3,file=fname) do 10 i=1,nh read(3,*) hash 10 continue c c............................................. Read in data n=0 11 continue read(3,*,end=12) BJD,FLUX_SAP,e_FLUX_SAP,FLUX_PDC,e_FLUX_PDC, &X_pos,Y_pos,BG n = n+1 t(n) = BJD-bigjd x(n) = FLUX_SAP x_p(n) = X_pos y_p(n) = Y_pos bgf(n) = BG go to 11 12 continue close(3) c c............................................. Compute outlier-free average call omit1(avsig,x,n,av0,si0,nclip) c c............................................. Normalize time series with the average fac=1.0d0/av0 do 22 i=1,n x(i)=x(i)*fac 22 continue c c return end c c subroutine read_target(itest,iseed,fname,n,t,x,iflare, &frin0,frin1,tfreq0,dep0,tfreq1,dep1,tc0, &x_p,y_p,ji,bgft,qtr0,qtr1,pht0,pht1,igen,std, &xsyn,trsyn,avmag,frootv,amootv,phootv) c c---------------------------------------------------------------------- c Reads in TARGET time series and generates test data if itest > 0. c---------------------------------------------------------------------- c c Input: - itest = 0 ... Use data stored in the file c 1 ... 2 transits + SINUSOIDAL OOTV c 2 ... 2 transits + RR Lyrae OOTV c 3 ... 2 transits + SINUSOIDAL OOTV + edge transits c 4 ... 2 transits + RR Lyrae OOTV + edge transits c 31 ... RR Lyr + 1 transit c 32 ... RR Lyr + 1 sine c - iseed = Big integer number to initialize random number c generator c - testp = Test period (in [days]) c - fname = file name c - qtr = Fractional transit length TrTime/per, c where 'TrTime' is the length of transit c - dep = Depth [mag] of the transit c - pht = Initial phase [0,1] of the transit c - igen = 1 -- Add Gaussian noise to the noiseless c synthetic signal c = 0 -- Add observed time series to the c noiseless synthetic signal c - std = Standard deviation [mag] of the added c Gaussian noise c c frin = Duration of the ingress/egress in the units c of the total duration of the transit (from c the first contact to the last one) c amootv = Amplitude of the sinusoidal OOTV c phootv = Phase of the OOTV in the units of 2*pi radian c frootv = Frequency of the OOTV in the units of the c orbital period c avmag = Pre-signed average magnitude of the time series c c tc0 = Generated transit is assumed to have a transit c center at: tc0 + pht/tfreq, where,(pht,tfreq) are c are any of these: (pht0,tfreq0), (pht1,tfreq1) c c c Output: - {t(i),x(i); i=1,2,...,n} = time series c ~~~~~~~ - {tsysn(i),xsyn(i); i=1,2,...,n} = synthetic c (noiseless) time c series c - ji = 1 - if we have temporal coordinates for the target c 0 - otherwise c c - {x_p(i), y_p(i); i=1,2,...,n} = temporal (X,Y) c positions for the c target c c---------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fname c dimension t(30333),x(30333) dimension xsyn(30333),trsyn(30333) dimension x_p(30333),y_p(30333),bgft(30333) c c---------------------------------------------------------------------- c pi2 = 8.0d0*datan(1.0d0) testp = 1.0d0/tfreq0 c c... Check if the target file is a derivative of an IPAC *.tbl file c ji = 0 - it is not c > 0 - it is c ji=0 ji1=index(fname,'pdc') if(ji1.gt.0) ji=ji1 c c... Read in data from file 'fname' c if(ji.eq.0) call datin8f(fname,n,t,x,bgft) if(ji.gt.0) call datin8fp(fname,n,t,x,x_p,y_p,bgft) do 20 i=1,n xsyn(i)=0.0d0 trsyn(i)=0.0d0 20 continue if(itest.eq.0) return c c c*************************** c Generate test data * c*************************** c c........................................ The RR Lyr + 1 sine case if(itest.eq.32) call test_32(iseed,n,t,x,iflare, &tfreq0,dep0,tfreq1,dep1,pht2,igen,std,xsyn,avmag) if(itest.eq.32) return c c........................................ The RR Lyr + 1 transit case if(itest.eq.31) call test_31(iseed,n,t,x,iflare, &tfreq0,tfreq1,dep1,qtr,pht,frin,igen,std, &xsyn,trsyn,avmag) if(itest.eq.31) return c c........................................ Transits with OOTV call test_tr_oot(itest,iseed,n,t,x,iflare, &tfreq0,tfreq1,dep0,dep1,qtr0,qtr1,pht0,pht1,tc0, &frin0,frin1,frootv,amootv,phootv,igen,std,xsyn,trsyn) c return end c c subroutine test_tr_oot(itest,iseed,n,t,x,iflare, &tfreq0,tfreq1,dep0,dep1,qtr0,qtr1,pht0,pht1,tc0, &frin0,frin1,frootv,amootv,phootv,igen,std,xsyn,trsyn) c c---------------------------------------------------------------------- c This is TEST_TR_OOT (transits + OOTV + edge transits) c---------------------------------------------------------------------- c c NOTE: NONE c c====================================================================== implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333),trap0(30333),trap1(30333) dimension tru(30333),xsyn(30333),trsyn(30333) dimension x_noise(30333) c c---------------------------------------------------------------------- c pi2 = 8.0d0*datan(1.0d0) p0 = 1.0d0/tfreq0 p1 = 1.0d0/tfreq1 tim_in = 1.5d0 c................................... Parameter *tim_in* controls the epochs of c the transits at the edges. t14=qtr0/tfreq0 t1_1=t(1)+tim_in t4_1=t1_1+t14 t4_2=t(n)-tim_in t1_2=t4_2-t14 c irr=0 if(itest.eq.2) irr=1 if(itest.eq.4) irr=1 c c................................... Setting noise vector [with zero-average] if(igen.eq.1) go to 3 call astat(x,n,aver,disper) do 4 i=1,n x_noise(i)=x(i)-aver 4 continue go to 5 3 continue do 6 i=1,n x_noise(i)=std*gauss(iseed) xsyn(i)=0.0d0 6 continue 5 continue c c............................................ 1st transit e0 = tc0 + p0*pht0 call trapu(n,t,p0,e0,qtr0,frin0,tru) do 10 i=1,n trap0(i)=dep0*tru(i) xsyn(i)=xsyn(i)+trap0(i) 10 continue c c............................................ 2nd transit e1 = tc0 + p1*pht1 call trapu(n,t,p1,e1,qtr1,frin1,tru) do 11 i=1,n trap1(i)=dep1*tru(i) xsyn(i)=xsyn(i)+trap1(i) trsyn(i)=xsyn(i) 11 continue c c............................................ OOTV do 12 i=1,n c e_oot = tc0 + p0*phootv tt = pi2*(t(i)-e_oot)*frootv xx = amootv*dsin(tt) if(irr.eq.0) go to 99 c............................................ RR Cet (a typical RR Lyrae) xx = 0.3193d0*dsin( tt+1.6025d0)+ &0.1558d0*dsin( 2.0d0*tt + 5.5675d0) xx = xx + 0.1112d0*dsin(3.0d0*tt+3.6169d0)+ &0.0708d0*dsin( 4.0d0*tt + 1.6885d0) xx = xx + 0.0408d0*dsin(5.0d0*tt+6.1338d0)+ &0.0201d0*dsin( 6.0d0*tt + 4.1837d0) xx = xx + 0.0109d0*dsin(7.0d0*tt+2.1168d0)+ &0.0062d0*dsin( 8.0d0*tt + 0.5691d0) xx = xx + 0.0045d0*dsin(9.0d0*tt+5.6257d0)+ &0.0038d0*dsin(10.0d0*tt + 4.4387d0) xx=amootv*xx c 99 continue c c............................................ Add transit #1 to the edges yy=0.0d0 if(itest.eq.1) go to 15 if(itest.eq.2) go to 15 yy=0.0d0 if(t(i).lt.t1_1) go to 13 if(t(i).gt.t4_1) go to 14 yy=-dep0 go to 13 14 continue if(t(i).gt.t4_2) go to 13 if(t(i).lt.t1_2) go to 13 yy=-dep0 13 continue c 15 continue xsyn(i)=xsyn(i)+xx+yy c 12 continue c c............................................ Add noise to the synthetic signal do 1 i=1,n x(i)= xsyn(i)+x_noise(i) 1 continue c if(iflare.gt.0) call add_flare(n,x) c write(*,103) tfreq0,dep0,tfreq1,dep1 103 format(/,'This is TEST_1 (2 transits):',/, &'f0 =',f12.7,' d0 =',f12.7,' f1 =',f12.7,' d1 =',f12.7) c return end c c subroutine test_31(iseed,n,t,x,iflare, &tfreq0,tfreq1,dep1,qtr,pht,frin,igen,std, &xsyn,trsyn,avmag) c---------------------------------------------------------------------- c This is TEST_23 (RR Lyr + 1 transit) c---------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333) dimension xsyn(30333),trsyn(30333) dimension x_noise(30333) c c---------------------------------------------------------------------- c call minmax(n,t,tmin,tmax) c pi2 = 8.0d0*datan(1.0d0) t0 = tmin amp1 = dep1/2.0d0 ph1 = pht ph2 = pht+qtr didi = frin*qtr phin = ph1 + didi phout = ph2 - didi c................................... Setting noise vector [with zero-average] if(igen.eq.1) go to 3 call astat(x,n,aver,disper) do 4 i=1,n x_noise(i)=x(i)-aver 4 continue go to 5 3 continue do 6 i=1,n x_noise(i)=std*gauss(iseed) 6 continue 5 continue c do 1 i=1,n time = t(i)-t0 c c............................................ RR Cet t2 = pi2*time tt = t2*tfreq0 xx = 0.3193d0*dsin( tt+1.6025d0)+ &0.1558d0*dsin( 2.0d0*tt + 5.5675d0) xx = xx + 0.1112d0*dsin(3.0d0*tt+3.6169d0)+ &0.0708d0*dsin( 4.0d0*tt + 1.6885d0) xx = xx + 0.0408d0*dsin(5.0d0*tt+6.1338d0)+ &0.0201d0*dsin( 6.0d0*tt + 4.1837d0) xx = xx + 0.0109d0*dsin(7.0d0*tt+2.1168d0)+ &0.0062d0*dsin( 8.0d0*tt + 0.5691d0) xx = xx + 0.0045d0*dsin(9.0d0*tt+5.6257d0)+ &0.0038d0*dsin(10.0d0*tt + 4.4387d0) c c............................................ Transit ph=time*tfreq1 ph=ph-idint(ph) yy=0.0d0 rat1 = 0.0d0 rat2 = 0.0d0 if(didi.lt.1.0d-20) go to 8 rat1 = dep1/(phin-ph1) rat2 = -dep1/(ph2-phout) 8 continue if(ph.lt.ph1) go to 7 if(ph.gt.ph2) go to 7 yy = dep1 if(ph.lt.phin) yy = rat1*(ph-ph1) if(ph.gt.phout) yy = dep1+rat2*(ph-phout) 7 continue c c............................................ zz = avmag + xx + yy xsyn(i) = zz trsyn(i)= yy x(i) = zz+x_noise(i) c 1 continue c if(iflare.gt.0) call add_flare(n,x) c write(*,103) tfreq0,tfreq1,dep1 103 format(/,'This is TEST_31 (RR Lyrae + 1 transit):',/, &' ... Fourier decomposition of RR Cet is used ...',/, &'tfreq0 = ',f12.7,/, &'tfreq1 = ',f12.7,/, &'dep1 = ',f12.7) c return end c c subroutine test_32(iseed,n,t,x,iflare, &tfreq0,dep0,tfreq1,dep1,pht2,igen,std,xsyn,avmag) c---------------------------------------------------------------------- c This is TEST_23 (RR Lyr + 1 sine) c---------------------------------------------------------------------- c c NOTES: - "dep0" is the factor by which the total amplitude of the c of the prime signal is multiplied c ... So, it is NOT the amplitude of the prime component!! c c - We use the light curve of RR Cet (an RR Lyrae star) with c fundamental frequency given by the parameter "tfreq0" c amplitude scaled by dep0.[The true period of RR_Cet is c 0.553028770 days] c c - The side lobe parameters are given by (tfreq1,dep1,pht2) c c---------------------------------------------------------------------- implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333) dimension xsyn(30333) dimension x_noise(30333) c c---------------------------------------------------------------------- c call minmax(n,t,tmin,tmax) c pi2 = 8.0d0*datan(1.0d0) t0 = 0.5d0*(tmin+tmax) amp0= dep0 amp1= dep1 ph1 = pht2 c c................................... Setting noise vector [with zero-average] if(igen.eq.1) go to 3 call astat(x,n,aver,disper) do 4 i=1,n x_noise(i)=x(i)-aver 4 continue go to 5 3 continue do 6 i=1,n x_noise(i)=std*gauss(iseed) 6 continue 5 continue c c................................... Generate NOISELESS SYNTHETIC MONOPERIODIC time series freq=pi2*tfreq0 do 1 i=1,n time = t(i)-t0 tt = time*freq xx = 0.3193d0*dsin( tt+1.6025d0)+ &0.1558d0*dsin( 2.0d0*tt + 5.5675d0) xx = xx + 0.1112d0*dsin(3.0d0*tt+3.6169d0)+ &0.0708d0*dsin( 4.0d0*tt + 1.6885d0) xx = xx + 0.0408d0*dsin(5.0d0*tt+6.1338d0)+ &0.0201d0*dsin( 6.0d0*tt + 4.1837d0) xx = xx + 0.0109d0*dsin(7.0d0*tt+2.1168d0)+ &0.0062d0*dsin( 8.0d0*tt + 0.5691d0) xx = xx + 0.0045d0*dsin(9.0d0*tt+5.6257d0)+ &0.0038d0*dsin(10.0d0*tt + 4.4387d0) xsyn(i) = xx 1 continue c c................................... Find (max,min) for {xsyn} xmin= 1.0d30 xmax=-1.0d30 pmax=0.0d0 pmin=0.0d0 do 2 i=1,n xx=xsyn(i) if(xx.lt.xmax) go to 7 xmax=xx pmax=t(i) 7 continue if(xx.gt.xmin) go to 2 xmin=xx pmin=t(i) 2 continue a0=xmax-xmin t0_mod=pmax-t0+ph1/tfreq0 c c................................... Rescale amplitude, add side lobe and noise afac=amp0/a0 freq=pi2*tfreq1 do 8 i=1,n time = t(i)-t0_mod tt = time*freq c yy = amp1*dsin(tt) c xsyn(i)=afac*xsyn(i)*(1.0d0+yy) x(i)=xsyn(i)+x_noise(i)+avmag 8 continue c if(iflare.gt.0) call add_flare(n,x) c write(*,103) tfreq0,tfreq1,amp1 103 format(/,'This is TEST_32 (RR Lyrae + 1 sine):',/, &' ... Fourier decomposition of RR Cet is used ...',/, &'tfreq0 = ',f12.7,/, &'tfreq1 = ',f12.7,/, &'amp1 = ',f12.7) c return end c c subroutine read_ref(n_ref,ref_id,ref_ra,ref_de,ref_mag) c--------------------------------------------------------------------- c This routine reads in reference file c--------------------------------------------------------------------- c c INPUT - ref.pos = Reference/position/magnitude file c c OUTPUT: - n_ref = Number of reference stars c - {ref_id} = IDs of the reference stars c - {ref_ra} = RAs of the reference stars c - {ref_de} = DEs of the reference stars c - {ref_mag} = Average magnitudes of the reference stars c c NOTES: - NONE c c--------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*9 ref_id(30333) c dimension ref_mag(30333),ref_ra(30333),ref_de(30333) c c-------------------------------------------------------------------- c c... Get the number of the available targets c call SYSTEM('wc -l ref.pos > nr.dat') open(1,file='nr.dat') read(1,*) n_ref close(1) c c... Read in reference magnitudes c open(1,file='ref.pos') do 5 i=1,n_ref read(1,*) ref_id(i),ref_ra(i),ref_de(i),ref_mag(i) 5 continue close(1) c return end c c subroutine file_in(fname,sname,nfile,camp) c--------------------------------------------------------------------------- c This routine reads in the next star name, path and K2 campaing number c--------------------------------------------------------------------------- c c INPUT: - filename.lis c ** Per line format: 211681517 LC/211681517-c05.txt c c OUTPUT: - fname : Full path to the time series c - sname : Star name (9 characters) c - nfile : Number of the files remained in 'filename.lis' c - camp : Campampaing ID (e.g., c03) c c NOTES: - After each call of this routine the first row in 'filename.lis' c will be deleted ... c - The file format is *strict* as given above, i.e.: c * star name is a 9 character string c * campaign number must be in the 14-16 character range of c the full path c * the full path cannot be greater than 25 c c--------------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*1 blank character*3 camp character*9 sname,sn(30333) character*25 fname,path(30333) c c--------------------------------------------------------------------------- blank =' ' c open(13,file='filename.lis') c read(13,7,end=15) sname,fname 7 format(a9,9x,a25) 15 continue if(sname.eq.blank) nfile=-1 if(nfile.eq.-1) write(*,100) 100 format('No more file names in *filename.lis* !') if(sname.eq.blank) return camp = fname(14:16) c c... Save the remaining content of the file c n = 0 8 continue n = n+1 read(13,7,end=3) sn(n),path(n) go to 8 3 continue n = n-1 close(13) c open(13,file='filename.lis') if(n.eq.0) go to 103 do 10 i=1,n write(13,7) sn(i),path(i) 10 continue go to 102 103 continue write(13,7) blank 102 continue close(13) nfile=n c return end c c subroutine sppolfit_w(ndsp,nf,p) c c------------------------------------------------------------------- c This routine fits a polynomial to the frequency spectrum c and returns the residual spectrum. Weighted LS is employed c------------------------------------------------------------------- c c INPUT: - ndsp = degree of the polynomial to be fitted to the c spectrum c - nf = number of the spectrum points c - p(i) = spectrum array c c OUTPUT: p(i) = spectrum array [trend-free] c c NOTES: - It is assumed that p(i) is equidistant with c respect of the frequency. c - For saving time, an evenly sampled subset of the c full spectrum is fitted. c c------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension t(30333),x(30333),p(155555) dimension c(995) c if(ndsp.lt.1) return c nd = ndsp nsm = 30333 c c nsm = Upper limit of the number of spectrum points used in c the polynomial fit. These points are distributed c uniformly in the frequency band. c------------------------------------------------------------------- c n2 = 1+idint(dfloat(nf)/dfloat(nsm)) n = 0 do 4 i=1,nf,n2 n = n+1 t(n) = dfloat(i) x(n) = p(i) 4 continue c r=2.0d0/(t(n)-t(1)) tmid=(t(1)+t(n))/2.0d0 c call robust_pol2(nd,n,t,x,c,sig) c c Subtract fitted polynomial c pmin = 1.0d30 do 1 i=1,nf s=c(nd+1) time=dfloat(i) time=r*(time-tmid) do 2 j=1,nd j1=nd+1-j s=s*time+c(j1) 2 continue p(i) = p(i)-s pmin = dmin1(pmin,p(i)) c 1 continue c do 3 i=1,nf p(i) = p(i)-pmin 3 continue c return end c c subroutine read_tfa_lis(ji,nte,fid,idt0) c----------------------------------------------------------- c This routine reads in pre-defined list of templates c----------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fname,fid(995) character*20 fn character*9 sname,idt0(995) character*2 lc character*1 slash c c... Check data type on *template.lis* c k=0 open(30,file='template.lis') read(30,2,end=3) sname,lc,slash,fn k=k+1 2 format(a9,9x,a2,a1,a20) fname=trim(lc//slash//fn) 3 continue close(30) if(k.eq.0) write(*,4) if(k.eq.0) stop 4 format('>>>>>> template.lis is EMPTY !') c c... Check if TFA template star names are consistent with the c data type of the target c k=index(fname,'pdc') if(k.ne.ji) write(*,1) if(k.ne.ji) stop 1 format(56('#'),/, &' TFA template data type contradicts to TARGET data type',/, &56('#')) c if(k.gt.0) go to 5 c c....................................... Read in TERRA-type list nte=0 open(30,file='template.lis') 600 continue nte=nte+1 read(30,603,end=601) idt0(nte),fid(nte),ih 603 format(a9,9x,a20,1x,i4) go to 600 601 continue nte=nte-1 close(30) go to 800 c c....................................... Read in PDC-type list 5 continue nte=0 open(30,file='template.lis') 700 continue nte=nte+1 read(30,703,end=701) idt0(nte),fid(nte),ih 703 format(a9,9x,a23,1x,i5) go to 700 701 continue nte=nte-1 close(30) c 800 continue c return end c c subroutine tfa_ts(itemp,ji,nte,fid,mte0,idt0) c--------------------------------------------------------------------- c This routine reads in TFA, BG and XY time series c--------------------------------------------------------------------- c c INPUT: - itemp : 0 - if no TFA template is to be used c >0 - if the TFA templates given in the c template file list is to be used c - ji : 0 - ExoFOP download (Petigura's TERRA) c : 1 - IPAC download (Kepler PDC pipeline) c - nte : Number of templates c (i.e., number of array elements of {fid}) c - {fid} : Input file IDs c - {idt0} : Input object names c c OUTPUT: - {xte0} : Template time series c -->common /aa0/ c - {tte0} : Moments of time of {xte0} c --> common /aa0/ c - {nte0} : Number of data points of {xte0} c --> common /aa0/ c - {idt0} : Output object names [see NOTES] c - mte0 : nte+1 c c NOTE: - After completion: nte --> nte+1 c [the constant template is added] c - After completion: The object name array {idt0} is c shifted by one to allow constant c template ID. c c===================================================================== c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fid(995) character*25 fname character*9 idt(995),idt0(995) c dimension t(30333),x(30333),bgf(30333),x_p(30333),y_p(30333) c common /aa0/ tte0(995,30333),xte0(995,30333),nte0(995) common /tt/ ttemp(30333),ntemp c c................................... Set CONSTANT template and the current c template timebase k=1 do 599 i=1,ntemp xte0(k,i) = 1.0d0 599 continue nte0(k)=ntemp idt(k)='111111111' if(itemp.eq.0) go to 200 c do 4 kk=1,nte c c................................... Read in template fname = fid(kk) if(ji.eq.0) call datin8f(fname,n,t,x,bgf) if(ji.gt.0) call datin8fp(fname,n,t,x,x_p,y_p,bgf) k=k+1 idt(k)=idt0(kk) nte0(k)=n c c................................... Store the corresponding template array do 44 i=1,n tte0(k,i)=t(i) xte0(k,i)=x(i) 44 continue c 4 continue c 200 continue c................................... Store template names do 201 j=1,k idt0(j)=idt(j) 201 continue mte0=k c return end c c subroutine temp_new(nseq,ji,idvar,itemp,x_p,y_p,bgft,ibg,ixy, &nt,ta,xa,intemp,x_int) c c************************************************************************** c >>> Read in / reload TFA template, BG and (X,Y) time series c >>> Interpolate the above time series to the timebase of the target c************************************************************************** c c INPUT: nseq - 1 for the FIRST call of this routine c >1 for ALL SUBSEQUENT calls c ji - 0 - ExoFOP download is used (based on TERRA) c 1 - IPAC download is used (based on the Kepler pipe) c idvar - Character ID of the variable to be analyzed c itemp - 1 use TFA templates for co-trending c 0 do NOT use TFA templates c {x_p} - Temporal X coordinate of the target c {y_p} - Temporal Y coordinate of the target c {bgft} - Bacground flux of the target c ibg - Key for target background flux for LC correction c ixy - Key for target position for LC correction c nt - Number of data points of the target time series c ta(i) - Time values of the target time series c xa(i) - Values of the target time series c c c OUTPUT: x_int(i) - Input time series (i.e., {x_int}={xa}) c xte(j,i) - Current template set ( common /aa/ ) c xte0(j,i)- Template set of the 1st call ( common /aa0/ ) c tte0(j,i)- Original timebase of {xte0} ( common /aa0/ ) c mte0 - TFA template number (without BG and XY vectors) c set in the 1st call c m3_tfa - TFA+(BG, XY vector) [because of the avaibility c of the BG, XY vectors, m3_tfa is specific to c each call] c c c NOTES: - NO CHANGE in the input time series {ta,xa} c - TFA template timebase = Target timebase c - The array {x_int} comes from an earlier version of this c routine (when we used a common timebase not equivalent with c that of the target - so, we had to interpolate the target c time series to the template timebase - this is why we have c "int" in x_int). This has been left in, to avoid changes in c other places. c c-------------------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*25 fid(995) character*9 idvar character*9 idt(995),idt0(995) c dimension t(30333),x(30333),y(30333),ta(30333),xa(30333) dimension x_int(30333) dimension x_p(30333),y_p(30333),bgft(30333) c common /aa/ xte(995,30333) common /aa0/ tte0(995,30333),xte0(995,30333),nte0(995) common /cc/ m3_tfa,mte0 common /cc1/ idt common /tt/ ttemp(30333),ntemp c c-------------------------------------------------------------------------- c c SET BASIC PARAMETERS c nbg = 3 nxy = 3 intemp= 0 c c nbg = Degree of the polynomial of the background detrending c nxy = Degree of the polynomial of the position detrending c c............................................ Set the template timebase c equal to the target timebase c and {x_int}={xa} c ntemp=nt do 4 i=1,ntemp ttemp(i)=ta(i) x_int(i)=xa(i) xte(1,i)=1.0d0 4 continue c c.... Read in pre-defined list of templates c if(itemp.eq.1.and.nseq.eq.1) call read_tfa_lis(ji,nte,fid,idt0) c c.... Read in template time series c if(nseq.eq.1) call tfa_ts(itemp,ji,nte,fid,mte0,idt0) mte1=mte0 if(itemp.eq.0) go to 6 c c... We have the basic template set in the initialization part above: c c {nte0(j),tte0(j,i),xte0(j,i); j=1,2,...,mte0; i=1,2,...,nte0(j)} c c Here we: 1. INTERPOLATE TFA template set to the timebase of the c target c 2. CHECK if the target is a member of the template set c 3. ADD other systematics correction vectors c c.................................... Interpolate TFA template to {ttemp} c do 11 j=2,mte0 n=nte0(j) do 13 i=1,n t(i)=tte0(j,i) x(i)=xte0(j,i) 13 continue call tbase_inter(n,t,x,y) do 12 i=1,ntemp xte(j,i)= y(i) 12 continue 11 continue c.................................... Check if the target is a member c of the template set. If yes, then c exclude the corresponding template it=0 do 5 i=2,mte0 if(idt0(i).eq.idvar) it=i 5 continue if(it.eq.0) go to 6 c c.................................... Exclude the template with the same c identity as the target intemp=1 mte1=mte0-1 if(it.eq.mte0) go to 6 do 7 j=it,mte1 j1=j+1 do 8 i=1,ntemp xte(j,i)=xte(j1,i) 8 continue 7 continue c 6 continue c c............................................. Add BACGROUND vector to the templates c [a nbg-order polynomial] kin=mte1 k=kin if(ibg.gt.0) call add_bgf(kin,nbg,bgft,kout) if(ibg.gt.0) k=kout c c............................................. Add POSITION vectors to the templates c [a nxy-order polynomial] c kin=k if(ixy.gt.0) call add_xy(kin,nxy,x_p,y_p,kout) if(ixy.gt.0) k=kout c nte=k write(*,144) nte 144 format('Total number of TFA/EPD correcting vectors =',i4) c m3_tfa=nte c return end c c subroutine add_bgf(kin,nbg,bgft,kout) c----------------------------------------------------------- c Add BACGROUND vector to the templates c [powers of the background flux] c----------------------------------------------------------- c c INPUT: c kin = Number of TFA templates (including constant) c at the moment of the call of this routine c nbg = Degree of the polynomial of the background c detrending vector (each polynomial order c increases the number of correcting vectors c associated with the background) c {bgft}= Background flux time series c c OUTPUT: c kout = Total number of co-trending vectors at the c moment of exiting from this routine c {xte}= Updated TFA vectors with the background c vectors c c=========================================================== implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension bgft(30333),b(30333),t(30333),y(30333) c common /aa/ xte(995,30333) common /tt/ ttemp(30333),ntemp c c----------------------------------------------------------- c k=kin eps=1.0d-10 np=20 asig3=3.0d0 c c eps = if the RMS of the background flux is greater than c eps, then NO background flux is added to the TFA set c np = order of the polynomial to be fitted to the c background flux c asig3= sigma clipping parameter for the background flux c c... Compute relative background flux vector c call astat(bgft,ntemp,av_bg,sigma) if(sigma.gt.eps) go to 4 nbg=0 go to 6 4 continue do 10 i=1,ntemp t(i) = ttemp(i) b(i) = bgft(i) 10 continue c c... Treating outliers based on a *np*-order polynomial fit c ipr=-1 n=ntemp call robust_pol(n,np,t,b,y,sig,stdv) call sigma_clipping_r(ipr,asig3,n,b,y,nclip) c c... Compute powers of {b} c do 1 m=1,nbg k=k+1 do 3 i=1,ntemp xx=b(i) sx=1.0d0 do 5 j=1,m sx=sx*xx 5 continue y(i)=sx 3 continue call astat(y,ntemp,aver,sigma) fac=1.0d0/aver do 2 i=1,ntemp xte(k,i)=fac*y(i)-1.0d0 2 continue 1 continue c 6 continue kout=k write(*,558) kin,nbg,kout 558 format(/,'>>> Adding BACKGROUND vectors:',/, & 'Starting number of templates+1 =',i4,/, & 'Number of background vectors =',i4,/, & 'Final number of templates =',i4,/) c return end c c subroutine add_xy(kin,nxy,x_p,y_p,kout) c----------------------------------------------------------- c Add POSITION vectors to the templates c [powers of the (X,Y) positions] c----------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c dimension x(30333),y(30333),z(30333) dimension x_p(30333),y_p(30333) c common /aa/ xte(995,30333) common /tt/ ttemp(30333),ntemp c c----------------------------------------------------------- c k=kin kout=kin if(nxy.eq.0) return c c... Compute relative (X,Y) vectors c call astat(x_p,ntemp,avx_p,sigma) call astat(y_p,ntemp,avy_p,sigma) avx_p=1.0d0/avx_p avy_p=1.0d0/avy_p do 260 i=1,ntemp x(i) = x_p(i)*avx_p-1.0d0 y(i) = y_p(i)*avy_p-1.0d0 260 continue c c... Compute powers of {x},{y} c kxy=0 do 1 m=1,nxy m1=m+1 do 2 j=1,m1 kxy=kxy+1 k=k+1 jx=j-1 jy=m1-j do 3 i=1,ntemp xx=x(i) yy=y(i) c.................................. X**jx sx=1.0d0 if(jx.eq.0) go to 6 do 5 jp=1,jx sx=sx*xx 5 continue 6 continue c.................................. Y**jy sy=1.0d0 if(jy.eq.0) go to 8 do 7 jp=1,jy sy=sy*yy 7 continue 8 continue z(i)=sx*sy 3 continue call astat(z,ntemp,aver,sigma) fac=1.0d0/aver do 9 i=1,ntemp xte(k,i)=fac*z(i)-1.0d0 9 continue 2 continue 1 continue c kout=k write(*,558) kin,kxy,kout 558 format('>>> Adding POSITION vectors:',/, & 'Starting number of templates+1 =',i4,/, & 'Number of position vectors =',i4,/, & 'Final number of templates =',i4,/) c return end c c subroutine tbase_inter(n,t,x,y) c----------------------------------------------------------------- c This routine linearly interpolates time series c {x(i); i=1,2,...,n} to the timebase given by c {ttemp(i); i=1,2,...,ntemp} c Output time series: {y(i); i=1,2,...,n} c c NOTES: - Returns array {y(i); i=1,2,...,ntemp} that is: c * zero-averaged c * defined on the template timebase c {ttemp(i); i=1,2,...,ntemp} c c - It is assumed that both time bases - {t} and {temp} c are monotinic c----------------------------------------------------------------- c implicit real*8 (a-h,o-z) implicit integer*4 (i-n) c character*9 idt(995) c common /cc/ m3_tfa,mte0 common /cc1/ idt common /tt/ ttemp(30333),ntemp c dimension t(30333),x(30333),y(30333) c c.............................. FIRST-order interpolation c NOTE: If extrapolation is needed, we set c the values equal to the first/last c value of the time series. c c... Compute average of {x} c s=0.0d0 do 6 i=1,n s=s+x(i) 6 continue aver=s/dfloat(n) c s=0.0d0 xn=x(n) do 1 i=1,ntemp tt = ttemp(i) k = 0 y(i) = aver do 2 j=1,n if(k.gt.0) go to 2 if(t(j).lt.tt) go to 2 k=j 2 continue if(k.eq.0) go to 3 if(k.eq.1) k=2 k2=k k1=k2-1 x1=t(k1) x2=t(k2) y1=x(k1) y2=x(k2) yy = y1 + (tt-x1)*(y2-y1)/(x2-x1) go to 5 3 continue yy=xn 5 continue s=s+yy y(i)=yy 1 continue s=s/dfloat(ntemp) c do 4 i=1,ntemp y(i)=y(i)-s 4 continue c return end c c subroutine spsnr1(nf,p,fmin,df,frmax,pmax,snr,spd) c--------------------------------------------------------------------- c This routine computes the Signal-to-Noise Ratio and the Spectral c Peak Density of the frequency spectrum c--------------------------------------------------------------------- c c INPUT: nf = number of points in the frequency spectrum c p(i) = frequency spectrum , i=1,2,...,nf c fmin = frequency at p(1) c df = frequency step c c OUTPUT: frmax = Frequency at the maximum peak c pmax = Spectrum value at the maximum peak c snr = Signal-to-Noise Ratio c spd = Spectral Peak Density c c NOTE: - Signal-to-Noise Ratio is defined as follows: c [ p(highest peak) -
] / sigma(p) c c - If asig6 > 666.0, then sigma clipping is skipped for c the calculation of
and sigma(p)
c
c - SPD was introduced on 2020.04.25 to make distinction
c between SPARSE and DENSE spectra. This parameter is
c equal to the ratio of the frequency regime occupied
c by spectrum values above *r_peak*, the fraction of
c the amplitude of the highest peak, above which the
c occupation rate is calculated.
c
c---------------------------------------------------------------------
c
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
c
dimension p(155555),y(155555)
dimension in(155555)
c
asig6 = 777.0d0
r_peak= 0.30d0
eps0 = 1.0d-15
nn = nf
c
c................................. Find maximum of the spectrum
pmax=-1.0d30
k=0
do 10 i=1,nn
if(p(i).lt.pmax) go to 10
pmax=p(i)
k=i
10 continue
frmax=fmin+df*dfloat(k-1)
c
if(asig6.lt.666.0d0) go to 13
c
c................................. Compute RMS of {p} without sigma clipping
s=0.0d0
do 14 i=1,nn
s=s+p(i)
14 continue
s=s/dfloat(nn)
d=0.0d0
do 15 i=1,nn
dd=p(i)-s
d=d+dd*dd
15 continue
xave=s
xsig=dsqrt(d/dfloat(nn-1))
go to 40
c
13 continue
c
c................................. Store working array
do 12 i=1,nn
y(i)=p(i)
12 continue
20 continue
c
c................................. Compute average, sigma
s=0.0d0
do 30 i=1,nn
s=s+y(i)
30 continue
xave=s/dfloat(nn)
dev=0.0d0
do 31 i=1,nn
d=y(i)-xave
dev=dev+d*d
31 continue
xsig=dsqrt(dev/dfloat(nn-1))
if(xsig.le.eps0) go to 40
c
c................................. Find outliers
devm=asig6*xsig
k=0
do 11 i=1,nn
dev=dabs(y(i)-xave)
j=1
if(dev.gt.devm) j=0
k=k+j
in(i)=j
11 continue
c................................. If no outlier found, compute SDE
if(k.eq.nn) go to 22
c
c................................. Omit outliers
k=0
do 1 i=1,nn
if(in(i).eq.0) go to 1
k=k+1
y(k)=y(i)
1 continue
nn=k
go to 20
c
22 continue
c
c................................. Sigma clipping completed,
c update average and sigma
s=0.0d0
do 32 i=1,nn
s=s+y(i)
32 continue
xave=s/dfloat(nn)
dev=0.0d0
do 33 i=1,nn
d=y(i)-xave
dev=dev+d*d
33 continue
xsig=dsqrt(dev/dfloat(nn-1))
c
40 continue
c
snr = (pmax-xave)/xsig
if(snr.gt.99.9d0) snr=99.9d0
c
c................................. Compute SPD
c
np=0
rpmax=1.0d0/pmax
do 50 i=1,nf
r=p(i)*rpmax
if(r.gt.r_peak) np=np+1
50 continue
spd=dfloat(np)/dfloat(nf)
c
return
end
c
c
subroutine omit1(asig,x,n,xave,xsig,nclip)
c
c---------------------------------------------------------------------
c
c This routine computes average and standard deviation
c of { x(i) ; i = 1,2, ..., n } by iteratively omitting
c data which deviate more that asig*sigma
c
c Input parameters/arrays: asig, t(i), x(i), n
c ~~~~~~~~~~~~~~~~~~~~~~~~
c
c Output parameters: xave = average
c ~~~~~~~~~~~~~~~~~~ xsig = standard deviation
c nclip = number of data points clipped
c
c---------------------------------------------------------------------
c
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
c
dimension x(30333),y(30333)
dimension in(30333)
c
eps0 = 1.0d-15
nn = n
c
do 21 i=1,nn
y(i) = x(i)
21 continue
c
20 continue
c
call astat(y,nn,aver,disper)
c
c... Finding out if there is an outlier
c
if(disper.le.eps0) return
c
iout=0
devm=asig*disper
do 10 i=1,nn
dev=dabs(y(i)-aver)
in(i)=1
if(dev.gt.devm) in(i)=0
10 continue
c
c... Omit outlier
c
k=0
do 1 i=1,nn
if(in(i).eq.0) go to 1
k=k+1
y(k)=y(i)
1 continue
if(k.eq.nn) go to 22
nn=k
go to 20
22 continue
nclip=n-nn
call astat(y,nn,xave,xsig)
c
return
end
c
c
subroutine tru_fit(n,trap,x,c0,c1,sdev)
c
c-------------------------------------------------------------------
c This routine performs the following LS fit:
c
c SUM [x3(i) - c0 - c1*trap(i)]**2 = minimum
c
c Input: n = Number of data points
c ~~~~~~ trap(i) = Function to be fitted to the target function
c x(i) = Target funcion
c
c Output: c0, c1 = Regression coefficients
c ~~~~~~~ sdev = Standard deviation of the residuals of
c the fit [biased estimate]
c
c Notes: - The two time series MUST be defined on the SAME
c time base, no empty values in both of them
c
c-------------------------------------------------------------------
c
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
c
dimension trap(30333),x(30333)
c
n0=n
maxdim = 30333
if(n.gt.maxdim) write(*,*) ' n > maxdim in *tru_fit* !'
if(n.gt.maxdim) n=maxdim
c
c... Compute normal matrix
c
a11=0.0d0
a12=0.0d0
a22=0.0d0
a13=0.0d0
a23=0.0d0
do 1 i=1,n
a=trap(i)
b=x(i)
a12=a12+a
a22=a22+a*a
a13=a13+b
a23=a23+b*a
1 continue
a11=dfloat(n)
a21=a12
d=1.0d0/(a11*a22-a12*a21)
c0 =(a13*a22-a23*a12)*d
c1 =(a23*a11-a13*a21)*d
c
s=0.0d0
do 2 i=1,n
d=x(i)-c0-c1*trap(i)
s=s+d*d
2 continue
sdev = dsqrt(s/dfloat(n))
n=n0
c
return
end
c
c
subroutine tru_fit_w(n,we,trap,x,c0,c1,sdev)
c
c-------------------------------------------------------------------
c This routine performs the following WEIGHTED LS fit:
c
c SUM we(i)*[x3(i) - c0 - c1*trap(i)]**2 = minimum
c
c Input: n = Number of data points
c ~~~~~~ we(i) = Point-by-point weights
c trap(i) = Function to be fitted to the target function
c x(i) = Target funcion
c
c Output: c0, c1 = Regression coefficients
c ~~~~~~~ sdev = Standard deviation of the residuals of
c the fit [biased estimate]
c
c Notes: - The two time series MUST be defined on the SAME
c time base, no empty values in both of them
c
c-------------------------------------------------------------------
c
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
c
dimension trap(30333),x(30333),we(30333)
c
n0=n
maxdim = 30333
if(n.gt.maxdim) write(*,*) ' n > maxdim in *tru_fit* !'
if(n.gt.maxdim) n=maxdim
c
c... Compute normal matrix
c
a11=0.0d0
a12=0.0d0
a22=0.0d0
a13=0.0d0
a23=0.0d0
do 1 i=1,n
w=we(i)
a=trap(i)
b=x(i)
a11=a11+w
a12=a12+w*a
a22=a22+w*a*a
a13=a13+w*b
a23=a23+w*b*a
1 continue
a21=a12
d=1.0d0/(a11*a22-a12*a21)
c0 =(a13*a22-a23*a12)*d
c1 =(a23*a11-a13*a21)*d
c
s=0.0d0
do 2 i=1,n
d=x(i)-c0-c1*trap(i)
s=s+d*d
2 continue
sdev = dsqrt(s/dfloat(n))
n=n0
c
return
end
c
c
subroutine events(n,t,f0,epoch,qtran,nev,ndit,itoc)
c
c========================================================================
c This routine computes number of transits with observed
c data points in them
c
c Input: n -- Number of data points
c ~~~~~~ t(i) -- Time values
c f0 -- Folding frequency (1/period)
c epoch -- Epoch of the first ingress
c qtran -- Fractional transit length
c (in the units of the period)
c
c Output: nev -- Number of transits with observed data points
c ~~~~~~~ in them
c ndit -- Total number of data points in the transit
c itoc -- Transit Occupation Code
c An *ntc* digit integer, giving the relative
c number of data points in the top occupied
c transit events. The occupation ratio decreases
c from left to right. The highest occupation
c rate, corresponding to 10, is not shown. The
c leftmost digit shows the ratio of the number
c of data points of the next most occupated
c transit event to the most occupated one.
c That is, if this digit is, e.g., 7, this means
c that the second most occupied event contains
c 0.7*N_max data points, where N_max is the
c number of data points sitting in the event
c containing the highest number of data points.
c========================================================================
c
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
c
dimension t(30333),tr(30333)
c
c========================================================================
c
nevm = 30333
ntc = 5
p0 = 1.0d0/f0
dt = p0*qtran
call minmax(n,t,tmin,tmax)
c
c... Find the first moment of transit preceeding *tmin*
c
t0=epoch
11 continue
if(t0.lt.tmin) go to 12
t0=t0-p0
go to 11
12 continue
c
c... Find number of transits with observed data points
c
ndit=0
nev=0
ne=-1
trm=-1.0d30
k=0
1 continue
ne=ne+1
t1=t0+p0*dfloat(ne)
if(t1.gt.tmax) go to 10
t2=t1+dt
k=0
do 2 i=1,n
time=t(i)
if(time.lt.t1) go to 2
if(time.gt.t2) go to 2
k=k+1
2 continue
if(k.lt.1) go to 1
if(nev.gt.nevm) go to 10
nev=nev+1
ndit=ndit+k
tr(nev)=dfloat(k)
trm=dmax1(trm,tr(nev))
go to 1
10 continue
c
c... Compute Transit Occupation Code [TOC]
c
ntc1=ntc+1
toc=0.0
do 3 j=1,ntc1
tcm=-1.0d30
do 4 i=1,nev
if(tr(i).lt.tcm) go to 4
k=i
tcm=tr(i)
4 continue
tr(k)=-1.0
if(j.eq.1) go to 3
if(tcm.lt.0.0) tcm=0.0
tcm=idint(10.0*tcm/trm)
tcm=dmin1(9.0d0,tcm)
toc=toc+tcm*(10.0**(ntc1-j))
3 continue
itoc=idint(toc)
c
return
end
c
C
SUBROUTINE INTER2(IQ,X,Y,N1,XD,YD,N2)
C
C-----------------------------------------------------------------
C THIS ROUTINE INTERPOLATES THE SEQUENCE
C X(I),Y(I), I=1,2,...,N1
C
C IN X(I), INTO THE OTHER GRID GIVEN BY {XD(I), I=1,2,...,N2}
C
C OUTPUT IS GIVEN IN:
C XD(I),YD(I), I=1,2,...,N2
C
C VERSION: 2010.04.01 (cleaned up from INTERX)
C-----------------------------------------------------------------
C
implicit real*8 (a-h,o-z)
implicit integer*4 (i-n)
C
DIMENSION Y(30333),X(30333)
DIMENSION XD(30333),YD(30333)
C
C IQ = 2 ... QUADRATIC INTERPOLATION
C = 1 ... LINEAR INTERPOLATION
C
DO 10 I=1,N2
C
C FIND THE PROPER ARRAY INDICES FOR THE INTERPOLATION REGIME
C
RX=XD(I)
DEV=1.0D30
JJ=1
DO 1 J=1,N1
DD=DABS(RX-X(J))
IF(DD.GT.DEV) GO TO 1
DEV=DD
JJ=J
1 CONTINUE
IF(JJ.EQ.1) GO TO 2
IF(JJ.EQ.N1) GO TO 3
J1=JJ-1
J2=JJ
J3=JJ+1
GO TO 4
2 CONTINUE
J1=1
J2=2
J3=3
GO TO 4
3 CONTINUE
J1=N1-2
J2=N1-1
J3=N1
4 CONTINUE
C
IF(IQ.EQ.1) GO TO 5
C
C QUADRATIC INTERPOLATION
C
AA=(RX-X(J2))*(RX-X(J3))/((X(J1)-X(J2))*(X(J1)-X(J3)))
BB=(RX-X(J1))*(RX-X(J3))/((X(J2)-X(J1))*(X(J2)-X(J3)))
CC=(RX-X(J1))*(RX-X(J2))/((X(J3)-X(J1))*(X(J3)-X(J2)))
YD(I)=Y(J1)*AA+Y(J2)*BB+Y(J3)*CC
GO TO 11
C
5 CONTINUE
C
C LINEAR INTERPOLATION
C
J1=JJ
IF(J1.EQ.1) GO TO 6
IF(J1.EQ.N1) GO TO 7
J2=J1+1
IF(DABS(RX-X(J1-1)).LT.DABS(RX-X(J1+1))) J2=J1-1
GO TO 8
6 CONTINUE
J2=2
GO TO 8
7 CONTINUE
J2=N1-1
8 CONTINUE
C
YD(I)=Y(J1)+(RX-X(J1))*(Y(J2)-Y(J1))/(X(J2)-X(J1))
C
11 CONTINUE
C
10 continue
C
RETURN
END
C
c
subroutine dsp_ok(n,t,x,p0,tcen,qtran,dsp,snrt,fret)
c
c===============================================================================
c This routine computes DSP and OOTV parameters SNRT and FRET
c===============================================================================
c
c INPUT:
c
c n = Number of data points
c {t} = Time values of the time series
c {x} = Values of the time series
c p0 = Period
c tcen = Moment of time of the CENTER of the TRANSIT
c qtran = Relative length of the transit (t14/P)
c
c
c OUTPUT:
c
c dsp = depth / SQRT( var_depth + var_oot + dif_oot)
c
c snrt = SNR of the highest peak in the Fourier frequency spectrum
c of the OOTV
c
c fret = Frequency [in the units of p0] at the highest peak in the
c Fourier frequency spectrum of the OOTV
c
c NOTES: - The goal in the computation of the new version of DSP is to
c consider ALL parts of the folded time series EQUALLY in
c the statistical sense. Therefore, we aim at the SAME BINNING
c as given by the TRANSIT LENGTH. The time series is phased
c in [-0.5,0.5] with the transit center at phase zero.
c End phases may not be properly covered, because of the general
c non-commensurate nature of the transit length and the full
c phase.
c
c - The scatter of the folded LC is characterized by two parameters:
c var_depth = VARIANCE of the AVERAGE of the INTRANSIT datapoints
c var_oot = VARIANCE of the OOT bin AVERAGES
c diff_oot = AVERAGE of the SQUARED DIFFERENCES of the bin
c averages in the OOT regime
c
c - The intransit variance is computed with the assumption of
c flat-bottomed shape. Therefore, deviation from this shape
c (i.e., long ingress/egress phases) lowers the final value
c of DSP [this is helpful in putting less weights on likely
c EB false positives or on other variables with V-shaped dips].
c
c - Explanation of the usage of var_oot and diff_oot:
c var_oot : whenever there is an OOTV, this parameter is in
c effect (independently of the smoothness of the
c OOT function)
c diff_oot: for ragged/discontinuous OOT, the differences
c are more relevant, than the variance. Admittedly,
c this parameter will also decrease DSP for multis,
c not only for singles with untreated outliers.
c
c - The bin variance is computed with the following formula:
c
c SI = (1/(n-1))*SUM(x_i**2) - (n /(n-1)*(