-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathR8CLKDIB.F
449 lines (400 loc) · 13.4 KB
/
R8CLKDIB.F
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
C YW 2016/03/08 Modofied the DIB calculation for TOPHT less than 17.3 to avoid Nan vaue error.
SUBROUTINE R8PREPCOEF(VOLEQ, COEFFS, ERRFLAG)
USE CLKCOEF_MOD
implicit none
TYPE(CLKCOEF):: COEFFS
CHARACTER*10 VOLEQ
INTEGER ERRFLAG
C *** LOCAL VARIABLES ***
INTEGER SPEC,GEOA,EQN,SPECPR,GEOAPR,EQNPR,SPGRP
INTEGER PTR,FIRST,LAST,HALF,GSPEC,CHECK,DONEFLAG,LASTFLAG
INTEGER DIBCNT,TOTCNT,FOURCNT,NINECNT
INCLUDE 'r8clkcoef.inc' !'R8CLKCOEF.INC'
INCLUDE 'r8dib.inc' !'R8DIB.INC'
READ(VOLEQ(8:10),'(I3)')SPEC
READ(VOLEQ(2:2),'(I1)')GEOA
IF (GEOA.LT.1 .OR. GEOA.GT.9 .OR. GEOA.EQ.8)THEN
ERRFLAG = 1
GO TO 999
ENDIF
READ(VOLEQ(3:3),'(I1)')EQN
IF(EQN. NE. 0 .AND. EQN .NE. 4 .AND. EQN .NE. 7 .AND. EQN .NE. 9
> .AND. EQN .NE. 8) THEN
ERRFLAG = 1
GO TO 999
ENDIF
IF (SPEC.EQ.123 .OR. SPEC.EQ.197) THEN
SPEC = 100
ELSEIF (SPEC.EQ.268) THEN
SPEC = 261
ELSEIF (SPEC.EQ.313 .OR. SPEC.EQ.314 .OR. SPEC.EQ.317 .OR.
> SPEC.EQ.650 .OR. SPEC.EQ.651. OR.
> SPEC.EQ.691 .OR. SPEC.EQ.711 .OR. SPEC.EQ.742 .OR.
> SPEC.EQ.762 .OR. SPEC.EQ.920 .OR. SPEC.EQ.930 .OR.
> SPEC.EQ.545 .OR. SPEC.EQ.546) THEN
SPEC = 300
ELSEIF (SPEC.EQ.521 .OR. SPEC.EQ.550 .OR. SPEC.EQ.580 .OR.
> SPEC.EQ.601 .OR. SPEC.EQ.602 .OR. SPEC.EQ.318) THEN
SPEC = 500
ELSEIF (SPEC.EQ.804 .OR. SPEC.EQ.817 .OR. SPEC.EQ.820 .OR.
> SPEC.EQ.823 .OR. SPEC.EQ.825. OR.
> SPEC.EQ.826 .OR. SPEC.EQ.830 .OR. SPEC.EQ.834) THEN
SPEC = 800
ENDIF
IF (SPEC.NE.SPECPR .OR. EQN.NE.EQNPR .OR. GEOA.NE.GEOAPR) THEN
SPECPR = SPEC
EQNPR = EQN
GEOAPR = GEOA
GSPEC = GEOA*1000 + SPEC
C BINARY SEARCH FOR CORRECT COEFFICIENTS
DONEFLAG = 0
LASTFLAG = 0
FIRST = 1
LAST = 182
DO 5, WHILE (DONEFLAG.EQ.0)
IF(FIRST.EQ.LAST) LASTFLAG = 1
!DETERMINE WHERE TO CHECK
HALF=((LAST-FIRST+1)/2) + FIRST
CHECK=R8CF(HALF,1)*1000+R8CF(HALF,2)
!FOUND THE COEFFECIENTS
IF(GSPEC.EQ.CHECK)THEN
PTR = HALF
DONEFLAG=1
!MOVE DOWN THE LIST
ELSEIF(GSPEC.GT.CHECK)THEN
FIRST = HALF
!MOVE UP THE LIST
ELSEIF(GSPEC.LT.CHECK)THEN
LAST = HALF - 1
ENDIF
!DID NOT FIND A MATCH
IF(LASTFLAG.EQ.1 .AND. DONEFLAG.EQ.0)THEN
!END ROUTINE, NO MATCH
IF(GEOA.EQ.9) THEN
SPECPR = 0
EQNPR = 0
GEOAPR = 0
ERRFLAG = 6
GO TO 999
!SET GEOAPR TO 9 AND RETRY THE SEARCH
ELSE
GEOA = 9
GSPEC = GEOA*1000 + SPEC
FIRST = 1
LAST = 182
LASTFLAG = 0
ENDIF
ENDIF
5 CONTINUE
C END BINARY SEARCH
SPGRP = R8CF(PTR,3) + .5
IF (SPGRP.NE.100 .AND. SPGRP.NE.300 .AND. SPGRP.NE.500)THEN
ERRFLAG = 6
GO TO 999
ENDIF
COEFFS%A4 = R8CF(PTR,4)
COEFFS%B4 = R8CF(PTR,5)
DONEFLAG = 0
LASTFLAG = 0
FIRST = 1
LAST = 49
DO 10, WHILE (DONEFLAG.EQ.0)
IF(FIRST.EQ.LAST) LASTFLAG = 1
!DETERMINE WHERE TO CHECK
HALF=((LAST-FIRST+1)/2) + FIRST
!FOUND THE COEFFECIENTS
IF((INT(DIBMEN(HALF,1)+.5)).EQ.SPEC) THEN
DIBCNT = HALF
DONEFLAG=1
!MOVE DOWN THE LIST
ELSEIF((INT(DIBMEN(HALF,1)+.5)).GT.SPEC)THEN
LAST = HALF -1
!MOVE UP THE LIST
ELSEIF((INT(DIBMEN(HALF,1)+.5)).LT.SPEC)THEN
FIRST = HALF
ENDIF
!DID NOT FIND A MATCH
IF(LASTFLAG.EQ.1 .AND. DONEFLAG.EQ.0) THEN
ERRFLAG = 6
GO TO 999
ENDIF
10 CONTINUE
C END BINARY SEARCH
IF (EQN.EQ.0.OR.EQN.EQ.4 .OR. EQN.EQ.8) THEN
COEFFS%FIXDI = DIBMEN(DIBCNT,2)
C FIXDI = DIBMEN(DIBCNT,2)
ELSE
COEFFS%FIXDI = DIBMEN(DIBCNT,3)
C FIXDI = DIBMEN(DIBCNT,3)
ENDIF
C***********************************************************************
IF (EQN.EQ.0 .OR. EQN.EQ.8) THEN
DONEFLAG = 0
LASTFLAG = 0
FIRST = 1
LAST = 49
DO 20, WHILE (DONEFLAG.EQ.0)
IF(FIRST.EQ.LAST) LASTFLAG = 1
!DETERMINE WHERE TO CHECK
HALF=((LAST-FIRST+1)/2) + FIRST
!FOUND THE COEFFECIENTS
IF((INT(TOTAL(HALF,1)+.5)) .EQ. SPEC)THEN
TOTCNT = HALF
DONEFLAG=1
!MOVE DOWN THE LIST
ELSEIF((INT(TOTAL(HALF,1)+.5)).GT.SPEC)THEN
LAST = HALF -1
!MOVE UP THE LIST
ELSEIF((INT(TOTAL(HALF,1)+.5)).LT.SPEC)THEN
FIRST = HALF
ENDIF
!DID NOT FIND A MATCH
IF(LASTFLAG.EQ.1 .AND. DONEFLAG.EQ.0)THEN
ERRFLAG = 6
GO TO 999
ENDIF
20 CONTINUE
COEFFS%R = TOTAL (TOTCNT,2)
COEFFS%C = TOTAL (TOTCNT,3)
COEFFS%E = TOTAL (TOTCNT,4)
COEFFS%P = TOTAL (TOTCNT,5)
COEFFS%B = TOTAL (TOTCNT,6)
COEFFS%A = TOTAL (TOTCNT,7)
COEFFS%A17 = R8CF(PTR,14)
COEFFS%B17 = R8CF(PTR,15)
ELSEIF (EQN.EQ.4) THEN
DONEFLAG = 0
LASTFLAG = 0
FIRST = 1
LAST = 49
DO 40, WHILE (DONEFLAG.EQ.0)
IF(FIRST.EQ.LAST) LASTFLAG = 1
!DETERMINE WHERE TO CHECK
HALF=((LAST-FIRST+1)/2) + FIRST
!FOUND THE COEFFECIENTS
IF((INT(FOUR(HALF,1)+.5)) .EQ. SPEC)THEN
FOURCNT = HALF
DONEFLAG=1
!MOVE DOWN THE LIST
ELSEIF((INT(FOUR(HALF,1)+.5)).GT.SPEC)THEN
LAST = HALF -1
!MOVE UP THE LIST
ELSEIF((INT(FOUR(HALF,1)+.5)).LT.SPEC)THEN
FIRST = HALF
ENDIF
!DID NOT FIND A MATCH
IF(LASTFLAG.EQ.1 .AND. DONEFLAG.EQ.0) THEN
ERRFLAG = 6
GO TO 999
ENDIF
40 CONTINUE
COEFFS%R = FOUR (FOURCNT,2)
COEFFS%C = FOUR (FOURCNT,3)
COEFFS%E = FOUR (FOURCNT,4)
COEFFS%P = FOUR (FOURCNT,5)
COEFFS%Q = FOUR (FOURCNT,6)
COEFFS%A17 = R8CF(PTR,8)
COEFFS%B17 = R8CF(PTR,9)
ELSEIF (EQN.EQ.7) THEN
I = 1
60 IF (INT(SEVEN(I,1)+.5).LT.SPEC .AND. I.LE.15) THEN
I = I + 1
GO TO 60
ELSEIF (INT(SEVEN(I,1)+.5).GT.SPEC .OR. I.GT.15) THEN
ERRFLAG = 6
GO TO 999
ENDIF
COEFFS%R = SEVEN (I,2)
COEFFS%C = SEVEN (I,3)
COEFFS%E = SEVEN (I,4)
COEFFS%P = SEVEN (I,5)
COEFFS%Q = SEVEN (I,6)
COEFFS%A17 = R8CF(PTR,10)
COEFFS%B17 = R8CF(PTR,11)
ELSEIF (EQN.EQ.9) THEN
DONEFLAG = 0
LASTFLAG = 0
FIRST = 1
LAST = 34
DO 80, WHILE (DONEFLAG.EQ.0)
IF(FIRST.EQ.LAST) LASTFLAG = 1
!DETERMINE WHERE TO CHECK
HALF=((LAST-FIRST+1)/2) + FIRST
!FOUND THE COEFFECIENTS
IF((INT(NINE(HALF,1)+.5)) .EQ. SPEC)THEN
NINECNT = HALF
DONEFLAG=1
!MOVE DOWN THE LIST
ELSEIF((INT(NINE(HALF,1)+.5)).GT.SPEC)THEN
LAST = HALF -1
!MOVE UP THE LIST
ELSEIF((INT(NINE(HALF,1)+.5)).LT.SPEC)THEN
FIRST = HALF
ENDIF
!DID NOT FIND A MATCH
IF(LASTFLAG.EQ.1 .AND. DONEFLAG.EQ.0) THEN
ERRFLAG = 6
GO TO 999
ENDIF
80 CONTINUE
COEFFS%R = NINE(NINECNT,2)
COEFFS%C = NINE(NINECNT,3)
COEFFS%E = NINE(NINECNT,4)
COEFFS%P = NINE(NINECNT,5)
COEFFS%Q = NINE(NINECNT,6)
COEFFS%A17 = R8CF(PTR,12)
COEFFS%B17 = R8CF(PTR,13)
ENDIF
ENDIF
999 CONTINUE
RETURN
END
C-------------------------------------------------------------------------------------
SUBROUTINE R8CLKDIB(VOLEQ, FORST, DBHOB, HTTOT, UPSHT1,HTUP,DIB,
& ERRFLAG)
USE CLKCOEF_MOD
implicit none
TYPE(CLKCOEF):: COEFFS
CHARACTER*10 VOLEQ, VOLEQ0
CHARACTER*2 FORST
CHARACTER*1 CTYPE
INTEGER ERRFLAG, EQN, IS, IB, IT, IM
REAL DBHOB, HTTOT, HTUP, DIB, DIB17, DBHIB, TOPHT, HIGHHT,UPSHT1
REAL R,C,E,P,B,A,Q, A17, B17, DX, AD, BD, VOLTMP(15)
INTEGER SI, BA
REAL HTONE, HTTWO, MTOPP
REAL DXcalc,FAC, topDib, totHt
c INCLUDE 'R8CLKCOEF.INC'
c INCLUDE 'R8DIB.INC'
C DW 08/22 Add initialization of COEFFS variables to prevent underflow is left otherwise uninitialized
COEFFS%DBHIB = 0.0
COEFFS%DIB17 = 0.0
COEFFS%R = 0.0
COEFFS%C = 0.0
COEFFS%E = 0.0
COEFFS%P = 0.0
COEFFS%B = 0.0
COEFFS%A = 0.0
COEFFS%A4 = 0.0
COEFFS%B4 = 0.0
COEFFS%A17 = 0.0
COEFFS%B17 = 0.0
COEFFS%TOTHT = 0.0
COEFFS%Q = 0.0
COEFFS%FIXDI = 0.0
READ(VOLEQ(3:3),'(I1)')EQN
C GET COEF FOR THE EQUATION
CALL R8PREPCOEF(VOLEQ, COEFFS, ERRFLAG)
R=COEFFS%R
C=COEFFS%C
E=COEFFS%E
P=COEFFS%P
B=COEFFS%B
A=COEFFS%A
Q=COEFFS%Q
AD=COEFFS%A4
BD=COEFFS%B4
B17=COEFFS%B17
A17=COEFFS%A17
DX=COEFFS%FIXDI
DBHIB=AD+BD*DBHOB
C-- Set height indicator variables
IS=0
IB=0
IT=0
IM=0
IF(HTUP.LT.4.5) IS=1
IF(HTUP.GE.4.5.AND.HTUP.LE.17.3) IB=1
IF(HTUP.GT.17.3) IT=1
IF(HTUP.LT.(17.3+A*(HTTOT-17.3))) IM=1
HIGHHT = HTUP
C-----Get DIB at specified height---------------------------------------
C-- Total height provided (eqn 1)
IF (EQN.EQ.0 .OR. EQN.EQ.8) THEN
TOPHT = HTTOT
DIB17=DBHOB*(A17+B17*(17.3/TOPHT)**2)
dib=(is*(dbhib**2*(1+(c+e/dbhib**3)*((1-highht/topht)**r
& -(1-4.5/topht)**r)/(1-(1-4.5/topht)**r)))
& +ib*(dbhib**2-(dbhib**2-dib17**2)*((1-4.5/topht)**p
& -(1-highht/topht)**p)/((1-4.5/topht)**p-(1-17.3/topht)**p))
& +it*(dib17**2*(b*(((highht-17.3)/(topht-17.3))-1)**2
& +im*((1-b)/a**2)*(a-(highht-17.3)/(topht-17.3))**2)))**0.5
ELSE
C-- HEIGHT TO 4, 7 OR 9 TOP DOB
IF(UPSHT1.LT.0.1)THEN
c SI = 0
c BA = 0
c CALL R8_MHTS(FORST,VOLEQ,DBHOB,HTTOT,SI,BA,UPSHT1,ERRFLAG)
C CALL R8VOL2 TO GET THE UPSHT1. THIS WILL BE SAME AS FVS
HTONE = 0
HTTWO = 0
CTYPE = 'F'
CALL R8VOL2(VOLEQ,VOLTMP,DBHOB,HTONE,HTTWO,MTOPP,HTTOT,CTYPE,
> ERRFLAG)
UPSHT1 = HTTWO
ENDIF
TOPHT = UPSHT1
DIB17=DBHOB*(A17+B17*(17.3/TOPHT)**2)
c dib=(is*(dbhib**2*(1+(c+e/dbhib**3)*((1-highht/topht)**r
c & -(1-4.5/topht)**r)/(1-(1-4.5/topht)**r)))
c & +ib*(dbhib**2-(dbhib**2-dib17**2)*((1-4.5/topht)**p
c & -(1-highht/topht)**p)/((1-4.5/topht)**p-(1-17.3/topht)**p))
c & +IT*(DIB17**2-(DIB17**2-DX**2)*(1-((TOPHT-HTUP)/
c & (TOPHT-17.3))**Q)))**0.5
C The codes below replaces the above comment out codes for NaN problem
C when TOPHT < 17.3 (YW 2016/03/08)
IF(IS.GT.0)THEN
dib=(is*(dbhib**2*(1+(c+e/dbhib**3)*((1-highht/topht)**r
& -(1-4.5/topht)**r)/(1-(1-4.5/topht)**r))))**0.5
ELSEIF(IB.GT.0)THEN
IF(TOPHT.GT.17.3)THEN
dib = (ib*(dbhib**2-(dbhib**2-dib17**2)*((1-4.5/topht)**p
& -(1-highht/topht)**p)/((1-4.5/topht)**p
& -(1-17.3/topht)**p)))**0.5
ELSE
IF(highht.le.topht)THEN
dib = (dbhib**2-(highht-4.5)/(topht-4.5)
& *(dbhib**2-DX**2))**0.5
ELSEIF(HTTOT.GT.0)THEN
dib = (DX**2-(highht-topht)/(httot-topht)*DX**2)**0.5
ENDIF
ENDIF
ELSEIF(IT.GT.0)THEN
IF(TOPHT.GT.17.3)THEN
IF(HTUP.LE.TOPHT)THEN
dib = (IT*(DIB17**2-(DIB17**2-DX**2)*(1-((TOPHT-HTUP)/
& (TOPHT-17.3))**Q)))**0.5
ELSE
C For the part above UPSHT1, need to use total height equation
C If HTTOT is not provided, using R9 logic to calculate the total height.
C The dib is calculated with total equation
VOLEQ0 = VOLEQ
VOLEQ0(3:3) = '0'
CALL R8PREPCOEF(VOLEQ0, COEFFS, ERRFLAG)
COEFFS%DBHIB = DBHIB
COEFFS%DIB17 = DIB17
IF(HTTOT.LE.0.0)THEN
topDib = DX
a = COEFFS%A
b = COEFFS%B
CALL r9totHt(totHt,htTot,dbhIb,dib17,topHt,topDib,a,b,
& errFlag)
HTTOT = totHt
ENDIF
COEFFS%TOTHT = HTTOT
CALL R9DIB(DXcalc, UPSHT1, COEFFS)
FAC = DX/DXcalc
IF(HTUP.LT.HTTOT) THEN
CALL R9DIB(dib, HTUP, COEFFS)
ELSE
dib = 0.0
ENDIF
dib = dib*FAC
ENDIF
ELSE
dib = (DX**2-(highht-topht)/(httot-topht)*DX**2)**0.5
ENDIF
ENDIF
ENDIF
RETURN
END