OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
runfit3t.f
Go to the documentation of this file.
1  subroutine runfit3t(nfpts, nskip, measlcl, meas, nmeas, nquant,
2  1 flag, nper, time, timdif, measout, flgout, iret )
3 c $Header$
4 c $Log$
5 c
6 c
7 c
8 c runfit3t(nfpts, nskip, measlcl, meas, nmeas, nquant,
9 c flag, nper, time, timdif, measout, flgout, iret )
10 c
11 c Purpose: fit a set of data points taken at a set of times by fitting
12 c a cubic polynomial to overlaping sections of the data points
13 c
14 c Calling Arguments:
15 c
16 c Name Type I/O Description
17 c -------- ---- --- -----------
18 c nfpts I*4 I number of points to use in the polynomial
19 c fit, must be >= 4
20 c nskip I*4 I skip or number of points of input data
21 c to move to do the next fit. nskip must
22 c be <= nfpts
23 c measlcl R*4 I work array of size nquant, nfpts
24 c meas R*4 I size nquant by nmeas array of measured
25 c quantitys
26 c nmeas I*4 I number of measurements in array
27 c nquant I*4 I number of quantities in the array
28 c flag I*4 I flag array for meas: 0- good, 1- bad
29 c nper I*4 I expansion factor from meas to measout
30 c time R*8 I times to fit to, also the first,
31 c nper+1th... are the tag times for
32 c the input data
33 c timdif R*4 I offset from the tag time that the
34 c measurement was made of the quantities
35 c measout R*4 O size nquant by nmeas * nper array of
36 c fitted measurements
37 c flgout I*4 O flag array for measout: 0- good, 1- bad
38 c iret I*4 O return code: 0 if all good, -1 if bad
39 c inputs
40 c
41 c By: W. Robinson, GSC, 3 Apr 93
42 c
43 c Notes: The routine uses routine fit3t to do the polynomial fit.
44 c this routine will just feed it the sub-sections of the
45 c input data and smoothly merge the fits in the overlap
46 c area. the overlap is nfpts - nskip. Also note that if
47 c any sub-range contains fewer than 4 good points on which
48 c to do the fit, the output flags for that range will be
49 c set to bad = 1
50 c
51 c Modification History:
52 c
53 c Disabled check for nskip gt nfpts for processing short scenes
54 c F. S. Patt, GSC, March 3, 1994
55 c
56 c Minor "clean-up" of code. F. S. Patt, January 12, 1995.
57 c
58 c Eliminated unused variable flglcl in call to fit3t and corrected logic
59 c in calculation of ngood1 and ngood2. F.S. Patt, GSC, August 15, 1996.
60 c
61 c Added flglcl back to fit3t arguments and included logic to check flags
62 c in returned fitted data. F.S. Patt, GSC, October 6, 1997.
63 c
64 c Added a check in averaging logic for a current value in the output array.
65 c F.S. Patt, December 24, 1997.
66 
67  implicit none
68 #include "nav_cnst.fin"
69 c
70  integer*4 nquant, nmeas, nper
71  real*4 meas(nquant,nmeas), measout(nquant,nmeas*nper),
72  1 timdif(nmeas), measlcl(nquant,nmeas*nper)
73  integer*4 nfpts, nskip, flag(nmeas),
74  1 flgout(nmeas*nper), flglcl(maxlin), iret
75  real*8 time(nmeas*nper)
76 c
77  integer*4 ilin, iquant, line, ovrlap, ifits, nfits,
78  1 istfit, isttim, istfill, ngood,
79  1 ngood1, ngood2, nfp, nskp
80  real*4 fracinc
81  logical lastdone, do_end, badrange
82 c
83 c
84 c start, make sure that the input controls are valid
85 c
86  nfp = nfpts
87  nskp = nskip
88 
89  if (nfp.gt.nmeas) nfp = nmeas
90  iret = 0
91  if( nfp .lt. 4 ) then
92  print *,' Error in setup of runfit3t: # fit points must be > 3'
93  iret = -1
94  go to 990
95  end if
96 c
97 c initialize the output flags as bad
98 c
99  do ilin = 1, nmeas * nper
100  flgout(ilin) = 1
101  end do
102 c
103 c find the number of fits that can be fully
104 c done over the range nmeas
105 c
106  nfits = ( nmeas - nfp ) / nskp + 1
107 c
108 c most likely, there are some points left over at the end
109 c that will have to be fit. The fit range can not be
110 c moved by nskip in this case, so it must be flagged
111 c and handled differently
112 c
113  if( ( nfits * nskp + nfp - nskp ) .lt. nmeas ) then
114  do_end = .true.
115  nfits = nfits + 1
116  else
117  do_end = .false.
118  end if
119 c
120 c compute the overlap and indicate that the previous fit
121 c could not be done (as the initialization)
122 c
123  ovrlap = nfp - nskp
124  lastdone = .false.
125 c
126 c loop over the fit ranges
127 c
128  do ifits = 1, nfits
129 c
130 c find the start point in the input data to fit for and
131 c the corrosponding start point in the output ( and in the
132 c time array). if the end condition is set, compute start of
133 c fit range so the range ends at last measurement.
134 c also, re-set the overlap if the end is reached
135 c
136  if( ifits .eq. nfits .and. do_end ) then
137  istfit = nmeas - nfp + 1
138  ovrlap = ( (nfits-1) * nskp + ( nfp - nskp ) ) - istfit
139  else
140  istfit = ( ifits - 1 ) * nskp + 1
141  end if
142  isttim = ( istfit - 1 ) * nper + 1
143 c
144 c check for at least 4 unflagged points in the fit range
145 c and 2 good points in each half of the range. If this
146 c is not satisfied, do not do that range
147 c also, add check for unflagged endpoints
148 c
149  ngood = 0
150  badrange = .false.
151  ngood1 = 0
152  ngood2 = 0
153  do ilin = istfit, istfit + nfp - 1
154 c
155  if( flag(ilin) .eq. 0 ) ngood = ngood + 1
156 c
157  if( ilin .le. ( istfit + (nfp - 1) / 2 ) ) then
158  if( flag(ilin) .eq. 0 ) ngood1 = ngood1 + 1
159  else
160  if( flag(ilin) .eq. 0 ) ngood2 = ngood2 + 1
161  end if
162 c
163  end do
164 c
165  if( ( ngood .lt. 4 ) .or. ( ngood1 .lt. 2 ) .or.
166 c 1 ( flag(istfit) .ne. 0 ) .or.
167 c 1 ( flag( istfit + nfp - 1 ) .ne. 0 ) .or.
168  1 ( ngood2 .lt. 2 ) ) badrange = .true.
169 c
170  if( badrange ) then
171  lastdone = .false.
172  else
173 c
174 c code for good fit if 4 or more points exist in the range.
175 c fit over the current range
176 c
177  call fit3t(meas(1,istfit), nfp, nquant, flag(istfit), nper,
178  1 time(isttim), timdif(istfit), measlcl, flglcl)
179 c
180 c merge the overlap region if any
181 c
182  if( lastdone .and. ovrlap .gt. 0 ) then
183 c
184 c set the weighting as the fraction of the overlap
185 c
186  fracinc = 1 / float( ovrlap * nper )
187 c
188 c re-set the output measurements in the overlap by
189 c weighting with the current measurements
190 c
191  do ilin = 1, ovrlap * nper
192  line = ilin - 1 + isttim
193  if (flglcl(ilin).eq.0) then
194 c
195 c check to be sure there is already a value to average
196 c
197  if (flgout(line).eq.0) then
198  do iquant = 1, nquant
199  measout(iquant,line) = measout(iquant,line) *
200  1 ( 1 - fracinc * ilin ) +
201  1 measlcl(iquant,ilin) * fracinc * ilin
202  end do
203  else
204  do iquant = 1, nquant
205  measout(iquant,line) = measlcl(iquant,ilin)
206  end do
207  end if
208  flgout(line) = 0
209  end if
210  end do
211 c
212 c set the start index for transfer of the rest of the
213 c local measurements (measlcl) to the output (measout)
214 c
215  istfill = ovrlap * nper + 1
216  else
217 c
218 c set the start transfer index at 1
219 c
220  istfill = 1
221  end if
222 c
223 c note that the fit was done and transfer local measurements
224 c to the output array
225 c
226  lastdone = .true.
227 c
228  do ilin = istfill, nper * nfp
229  if (flglcl(ilin).eq.0) then
230  line = ilin - 1 + isttim
231  do iquant = 1, nquant
232  measout(iquant,line) = measlcl(iquant,ilin)
233  end do
234  flgout(line) = 0
235  end if
236  end do
237  end if
238  end do
239 c
240 c and end
241 c
242  990 continue
243  return
244  end
#define real
Definition: DbAlgOcean.cpp:26
subroutine runfit3t(nfpts, nskip, measlcl, meas, nmeas, nquant, flag, nper, time, timdif, measout, flgout, iret)
Definition: runfit3t.f:3
subroutine fit3t(meas, nmeas, nquant, flag, nper, time, timdif, measout, flgout)
Definition: fit3t.f:3