1
C @(#)tsasin.for 19.1 (ESO-DMD) 02/25/03 13:33:26
2
C===========================================================================
3
C Copyright (C) 1995 European Southern Observatory (ESO)
5
C This program is free software; you can redistribute it and/or
6
C modify it under the terms of the GNU General Public License as
7
C published by the Free Software Foundation; either version 2 of
8
C the License, or (at your option) any later version.
10
C This program is distributed in the hope that it will be useful,
11
C but WITHOUT ANY WARRANTY; without even the implied warranty of
12
C MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
C GNU General Public License for more details.
15
C You should have received a copy of the GNU General Public
16
C License along with this program; if not, write to the Free
17
C Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
20
C Correspondence concerning ESO-MIDAS should be addressed as follows:
21
C Internet e-mail: midas@eso.org
22
C Postal address: European Southern Observatory
23
C Data Management Division
24
C Karl-Schwarzschild-Strasse 2
25
C D 85748 Garching bei Muenchen
27
C===========================================================================
28
C+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
C.COPYRIGHT (c) 1992 European Southern Observatory & Copernicus Astron. Center
31
C.AUTHOR Alex Schwarzenberg-Czerny, Copernicus Astron. Center, Warsaw
32
C.KEYWORD MIDAS, time series, SINEFIT/TSA
34
C.PURPOSE Fit sine (Fourier) series
35
C.VERSION 0.0 June 1992
41
C-----------------------------------------------------------------------------
44
INCLUDE 'MID_REL_INCL:TSA_DEF.INC'
45
INCLUDE 'MID_INCLUDE:ST_DEF.INC'
48
PARAMETER (MXORDER=20) ! DEPENDS ON LENGTH OF OUTD
49
REAL*8 FREQU ! INITIAL PERIOD VALUE
50
INTEGER NORDER ! NUMBER OF HARMONICS
51
INTEGER ITER ! NUMBER OF ITERATIONS
52
CHARACTER*60 INAME ! NAME OF INPUT TABLE
53
CHARACTER*60 ONAME ! NAME OF OUTPUT TABLE
54
REAL*8 COEF(MXORDER+3) ! NORDER COEFFICIENTS
56
INTEGER MODE,IACTS,KUN,KNUL
57
INTEGER TID,OTID,ITIME,IVAL
58
INTEGER NCOL,ICOL,NROW,IROW,ISOR
59
INTEGER LFIELD,TTYP,VTYP
60
INTEGER*8 PTIME,PVAL,POTIME,POVAL
65
DOUBLE PRECISION E(MXORDER+3) ! WORK TABLES
66
DOUBLE PRECISION B(MXORDER+3,MXORDER+3) ! WORK TABLES
68
INCLUDE 'MID_REL_INCL:TSA_DAT.INC'
69
INCLUDE 'MID_INCLUDE:ST_DAT.INC'
75
CALL STSPRO ('tsasin')
76
CALL STKRDC ('IN_A', 1,1,60,IACTS,INAME, KUN,KNUL,ISTAT)
77
CALL STKRDC ('OUT_A',1,1,60,IACTS,ONAME, KUN,KNUL,ISTAT)
78
CALL STKRDD ('STARTTSA', 1, 1,IACTS,FREQU, KUN,KNUL,ISTAT)
79
CALL STKRDI ('ORDERTSA', 1, 1,IACTS,NORDER,KUN,KNUL,ISTAT)
81
IF (NORDER.GT.MXORDER+1) THEN
82
NORDER=(MXORDER-1)/2*2+1
84
$ 'Will use max. permitted ORDER of',
86
CALL STTPUT(TEXT,ISTAT)
88
CALL STKRDI ('ITER', 1, 1, IACTS,ITER,KUN,KNUL,ISTAT)
90
CALL STKRDD ('OUTPUTD',1,NORDER-1,IACTS,COEF(1),
94
C Map input/output data
96
CALL TBTOPN (INAME,F_IO_MODE,TID,ISTAT)
98
CALL TBIGET (TID,NCOL,NROW,ISOR,ICOL,IROW,ISTAT)
99
CALL TBLSER (TID,'TIME',ITIME,ISTAT)
101
CALL STETER(3,'Column :TIME not found')
103
CALL TBLSER (TID,'VALUE',IVAL,ISTAT)
105
CALL STETER(4,'Column :VALUE not found')
107
CALL TBFGET (TID,ITIME,FORM,LFIELD,TTYP,ISTAT)
108
CALL TBFGET (TID,IVAL, FORM,LFIELD,VTYP,ISTAT)
109
CALL TBDGET (TID,ISTORE,ISTAT)
110
IF (ISTORE.NE.F_TRANS) THEN
111
TEXT='Input table '//INAME//' stored not transposed'
114
IF (TTYP.NE.D_R8_FORMAT.OR.VTYP.NE.D_R8_FORMAT) THEN
116
$ 'Data column(s) must be of DOUBLE PRECISION type')
118
CALL TBCMAP (TID,ITIME,PTIME,ISTAT)
120
CALL TBCMAP (TID,IVAL, PVAL, ISTAT)
126
C A. Schwarzenberg-Czerny 07-05-93
128
C Perform single nonlinear fit iteration, store coeficients and residuals
131
CALL TIMFIT(MADRID(PTIME),MADRID(PVAL),MADRID(POVAL),NROW,
132
$ MODE,NORDER,ITER,MXORDER,COEF,E,B)
134
CALL STKWRD ('STARTTSA', FREQU, 1, 1,KUN,ISTAT)
135
CALL STKWRD ('OUTPUTD',COEF(1),1,NORDER-1,KUN,ISTAT)
139
CALL DSCUPT(OTID,OTID,' ',ISTAT)
151
C Purpose: Given a trial period, fit a Fourier series by non-linear
152
C least squares method.
154
C Data: an ASCII file containing 3 columns (number of line(integer),
155
C time, value (both real*4)) and less than maxm-2 rows.
156
C A trial value of the period, an order of the series (<maxo/2-1)
157
C and a name of the data file are prompted for.
159
C Results: Correlation coefficients of the fitted parameters, their
160
C values and errors are displayed on the screen after each
161
C iteration. Finish iterations by typing 0 when changes in
162
C the parameters and std.dev. are negligible. After the final
163
C iteration values of the series are subtracted from the data and
164
C output as fitsin.res file.
166
C Method: The data are fitted by the following series:
167
C f(1)+f(2)*sin(w*t)+f(3)*cos(w*t)+...+f(2*ord+1)*cos(2*ord*w*t)
168
C by least squares. After the initial solution
169
C the value of the frequency w is varied as well as the coefficients so
170
C that due to nonlinearity of the problem several iterations are
171
C required. To continue iterations type 1.
173
C A. Schwarzenberg-Czerny June, 1987
175
SUBROUTINE TIMFIT(T,X,OX,NX,MODE,NORD,NITER,MXORD,F,E,B)
177
INCLUDE 'MID_REL_INCL:TSA_DEF.INC'
180
INTEGER NX ! NUMBER OF DATA
181
DOUBLE PRECISION CRACOW ! EXTERNAL FUNCTION
182
DOUBLE PRECISION T(NX) ! TIME VECTOR
183
DOUBLE PRECISION X(NX) ! INPUT DATA VECTOR
184
DOUBLE PRECISION OX(NX) ! OUTPUT DATA VECTOR
185
INTEGER MODE ! MODE OF WORK
186
INTEGER NORD ! ORDER OF THE SERIES
187
INTEGER MXORD ! TABLE SIZE-3
188
INTEGER NITER ! NUMBER OF ITERATIONS
189
DOUBLE PRECISION F(MXORD+3) ! FIT COEFFICIENTS (INCL. FREQ)
190
DOUBLE PRECISION E(MXORD+3),B(MXORD+3,MXORD+3) ! WORK TABLES
192
DOUBLE PRECISION FREQ
195
DOUBLE PRECISION TM,XM,TMX,TMI,DFREQ,PI2,CORRMX
196
DOUBLE PRECISION PHMT,DPHMT,EPMAX, FREQSTEP
197
INTEGER N,NP,I,N1,J,K,ITER
199
INCLUDE 'MID_REL_INCL:TSA_CONST.INC'
203
PI2=TWO*TWO*TWO*ATAN(ONE)
219
C input parameters and output results of previous iteration
222
IF (NORD.LT.1) CALL STETER(5,'Wrong parameter')
226
WRITE(MSG,'(a,1pg20.6,a,i4)')
227
$ ' START: frequency=',F(NORD)/PI2,' order=',(NORD-2)/2
228
CALL STTPUT(MSG,ISTAT)
235
CALL STETER(2,' Wrong mode')
242
C iterations for non-linear fit
246
C freq - current frequency
248
C tm - mean time of obserwation
249
C phmt - phase of base sinusoid at mean time
251
C epmax - epoch of maximum of the base sinusoid
252
C The harmonics are ignored by phase calculation
255
C set condition and normal equations
266
CALL FITSIN(E,N1,F,NP)
268
C *** prevent overwriting data before end of iterations, A.S-C, 07-05-93
269
IF (ITER.GT.NITER) THEN
274
3 B(K,J)=B(K,J)+E(J)*E(K)
276
C solve normal equations and evaluate results
278
IF (ITER.LE.NITER) THEN
279
IF (CRACOW(B,N1,MXORD+3,NX).NE.ZERO) THEN
281
C Update and output coefficients
287
7 CORRMX=MAX(CORRMX,ABS(B(I,J)/SQRT(B(J,J)*B(I,I))))
290
$ ' iter. freq. std.dev., max. corr.',
293
FREQSTEP=B(NORD,N1)/PI2
294
IF (FREQSTEP*(TMX-TMI).GT.TWO) THEN
296
$ '*** Large frequency step, divergence?',ISTAT)
299
WRITE(MSG,'(1x,i7,3(1pe15.7),'' '')')
300
$ ITER,F(NORD)/PI2,SQRT(MAX(B(N1,N1),ZERO)),CORRMX
301
CALL STTPUT(MSG,ISTAT)
302
IF (ITER.EQ.NITER) THEN
303
CALL STTPUT(' coefficients errors',ISTAT)
306
WRITE(MSG,'(1x,2(1pe15.7),'' '')')
307
$ F(J),SQRT(MAX(B(J,J),ZERO))
308
6 CALL STTPUT(MSG,ISTAT)
311
C Output ephemeris for base sinusoid
313
IF (NORD.GT.ZERO.AND.MODE.EQ.1) THEN
315
IF(.NOT.START) DFREQ=SQRT(MAX(B(N,N),ZERO))/PI2
316
PHMT=ATAN2(F(3),F(2))/PI2
317
DPHMT=SQRT(F(2)*F(2)*B(3,3)+F(3)*F(3)*B(2,2))/
318
$ (F(2)*F(2)+F(3)*F(3))
319
EPMAX=(HALF*HALF-PHMT)/FREQ+TM
320
IF (PHMT.LT.ZERO) PHMT=PHMT+ONE
321
MSG=' frequency error '//
322
$ 'phase at m.t. error '
323
CALL STTPUT(MSG,ISTAT)
324
WRITE(MSG,'(1x,5(1pe15.7))') FREQ,DFREQ,PHMT,DPHMT
325
CALL STTPUT(MSG,ISTAT)
327
$ ' epoch of max. mean time ',ISTAT)
328
WRITE(MSG,'(1x,5(1pe15.7))') EPMAX,TM
329
CALL STTPUT(MSG,ISTAT)
333
C next iterations with period correction
339
$ ' Singular equations, or wrong dimensions ')
350
SUBROUTINE FITSIN(E,N1,P,NP)
352
C condition equations for fit of Fourier series,
353
C last term contains residuals
355
INCLUDE 'MID_REL_INCL:TSA_DEF.INC'
358
DOUBLE PRECISION E(N1),P(N1-1+NP)
361
REAL SJ,CJ,T,X,FR,S,C,RHS,SAVE
363
INCLUDE 'MID_REL_INCL:TSA_CONST.INC'
380
E(NFR)=E(NFR)+J*T*(P(L)*CJ-P(L+1)*SJ)
381
RHS=RHS-P(L)*SJ-P(L+1)*CJ