OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
fit3t.f
Go to the documentation of this file.
1  subroutine fit3t(meas, nmeas, nquant, flag, nper, time, timdif,
2  1 measout, flgout )
3 c
4 c fit3t(meas, nmeas, nquant, flag, nper, time, timdif, measout, flgout )
5 c
6 c Purpose: fit data taken at certain times to a regular time interval
7 c using a least squares fit to a cubic polynomial
8 c
9 c Calling Arguments:
10 c
11 c Name Type I/O Description
12 c -------- ---- --- -----------
13 c meas R*4 I size nquant by nmeas array of measured
14 c quantitys
15 c nmeas I*4 I number of measurements in array
16 c nquant I*4 I number of quantities in the array
17 c flag I*4 I flag array for meas: 0- good, 1- bad
18 c nper I*4 I expansion factor from meas to measout
19 c time R*8 I times to fit to, also the first,
20 c nper+1th... are the tag times for
21 c the input data
22 c timdif R*4 I offset from the tag time that the
23 c measurement was made of the quantities
24 c measout R*4 O size nquant by nmeas * nper array of
25 c fitted measurements
26 c flgout I*4 O size nmeas array of output flags
27 c
28 c By: W. Robinson, GSC, 1 Apr 93
29 c
30 c Notes:
31 c
32 c Modification History:
33 c
34 c Eliminate unused variable flgout. F.S. Patt, GSC, August 16, 1996.
35 c
36 c Added code to set flgout output variable. F.S. Patt, GSC, Oct. 3, 1996.
37 c
38 c Modified to compute outputs only within range of valid input points.
39 c F. S. Patt, SAIC GSC, Feb. 4, 1999.
40 c
41 c Modified to include a check for poorly conditioned polynomial by means of
42 c a check on the inverted matrix. F. S. Patt, SAIC GSC, Mar. 10, 1999.
43 
44  implicit none
45 #include "nav_cnst.fin"
46 c
47  integer*4 nmeas, nquant, nper
48  real*4 meas(nquant,nmeas), measout(nquant,nmeas*nper),
49  * timdif(nmeas)
50  integer*4 iret, flag(nmeas),
51  * flgout(nmeas*nper)
52  real*8 time(nmeas*nper)
53 c
54 c note that summ is sum of measurements, sumt is sum of times...
55  real*8 sumt, sumt2, sumt3, sumt4, sumt5, sumt6, summ(20),
56  1 summt(20), summt2(20), summt3(20), x(4), m(4,4), a(4)
57  real*8 newtim(maxlin), t, tp, tdiff, t1
58  real*4 tolmat, tm3
59  integer*4 imeas, iquant, nsum
60  data tolmat/100./
61 
62 c
63 c initialize output flags
64 c
65  do imeas = 1, nmeas*nper
66  flgout(imeas) = 1
67  end do
68 c
69 c
70 c start, create the actual measurement time from the
71 c time and the time difference
72 c
73  do imeas = 1, nmeas
74  newtim(imeas) = time( (imeas - 1) * nper + 1 ) +
75  1 timdif(imeas) - time(1)
76  end do
77 c
78 c create components to go into the 4 simultaneous equations
79 c which make up the solution to the least squares fit
80 c
81  sumt = 0
82  sumt2 = 0
83  sumt3 = 0
84  sumt4 = 0
85  sumt5 = 0
86  sumt6 = 0
87  nsum = 0
88  tdiff = 999.
89  do iquant = 1,nquant
90  summ(iquant) = 0
91  summt(iquant) = 0
92  summt2(iquant) = 0
93  summt3(iquant) = 0
94  end do
95 c
96  do imeas = 1, nmeas
97  if (nsum.gt.0) tdiff = newtim(imeas) - tp
98  if( (flag(imeas) .eq. 0) .and. (tdiff .gt. 0.1) ) then
99  sumt = sumt + newtim(imeas)
100  sumt2 = sumt2 + newtim(imeas) * newtim(imeas)
101  sumt3 = sumt3 + newtim(imeas) **3
102  sumt4 = sumt4 + newtim(imeas) **4
103  sumt5 = sumt5 + newtim(imeas) **5
104  sumt6 = sumt6 + newtim(imeas) **6
105 c
106 c there are nquant solutions that will be done here
107 c
108  do iquant = 1, nquant
109  summ(iquant) = summ(iquant) + meas(iquant,imeas)
110  summt(iquant) = summt(iquant) + meas(iquant,imeas) *
111  1 newtim(imeas)
112  summt2(iquant) = summt2(iquant) + meas(iquant,imeas) *
113  1 newtim(imeas) * newtim(imeas)
114  summt3(iquant) = summt3(iquant) + meas(iquant,imeas) *
115  1 newtim(imeas) **3
116  end do
117  nsum = nsum + 1
118  if (nsum .eq. 1) t1 = newtim(imeas)
119  tp = newtim(imeas)
120  end if
121  end do
122 c
123 c check for minimum number of samples
124 c
125  if (nsum.gt.3) then
126 c
127 c set up the matrix for the computations
128 c
129  m(1,1) = nsum
130  m(2,1) = sumt
131  m(3,1) = sumt2
132  m(4,1) = sumt3
133  m(1,2) = m(2,1)
134  m(2,2) = sumt2
135  m(3,2) = sumt3
136  m(4,2) = sumt4
137  m(1,3) = m(3,1)
138  m(2,3) = m(3,2)
139  m(3,3) = sumt4
140  m(4,3) = sumt5
141  m(1,4) = m(4,1)
142  m(2,4) = m(4,2)
143  m(3,4) = m(4,3)
144  m(4,4) = sumt6
145 c
146 c loop and solve for each quantity
147 c
148  do iquant = 1,nquant
149 c
150 c create the right side of the simultaneous equation
151 c
152  x(1) = summ(iquant)
153  x(2) = summt(iquant)
154  x(3) = summt2(iquant)
155  x(4) = summt3(iquant)
156 c
157 c invert and solve for coefficients of cubic polynomial
158 c x = m a find a - the coefficients
159 c As m is the inverse, only create the inverse once,
160 c after that, multiply the inverted matrix by the right
161 c hand sides
162 c
163  if( iquant .eq. 1 ) then
164  call invert(m, x, 4, 4, a, iret )
165 
166 c check for poorly conditioned solution
167  tm3 = sqrt(m(4,4))*(tp - t1)**3
168  if (tm3 .gt. tolmat) iret = -1
169 
170  else
171  call matvec2( m, 4, x, a )
172  end if
173 c
174  if( iret .eq. 0 ) then
175 c
176 c fit the output measurements
177 c
178  do imeas = 1, nmeas * nper
179  t = time(imeas) - time(1)
180  if ((t .ge. t1) .and. (t .le. tp)) then
181  measout(iquant,imeas) = a(1) + a(2) * t +
182  * a(3) * t * t + a(4) * t * t * t
183  flgout(imeas) = 0
184  end if
185  end do
186  else
187  print *,' An error occured in the inversion process'
188  end if
189  end do
190 
191  else
192  print *,' Insufficient samples for smoothing'
193 
194  end if
195 c
196 c and end
197 c
198  990 continue
199  return
200  end
subroutine invert(a, b, n, l, c, ier)
Definition: invert.f:3
#define real
Definition: DbAlgOcean.cpp:26
subroutine matvec2(xm, nxm, v1, v2)
Definition: matvec2.f:2
subroutine fit3t(meas, nmeas, nquant, flag, nper, time, timdif, measout, flgout)
Definition: fit3t.f:3