SURFEX v8.1
General documentation of Surfex
confi.F
Go to the documentation of this file.
1  SUBROUTINE confi (PFVAL,KEXP,KMANT,PNFVAL)
2  USE parkind1, ONLY : jprb
3  USE yomhook , ONLY : lhook, dr_hook
4  USE lfi_precision
5 !
6 !
7 !
8 !
9 !
10 !********************************************************************
11 !*
12 !* NAME : CONFI
13 !*
14 !* FUNCTION : CONVERT FLOATING POINT NUMBER FROM MACHINE
15 !* REPRESENTATION TO GRIB REPRESENTATION,
16 !* THE RESULT NUMBER NOT EXCEEDING THE INPUT NUMBER.
17 !* INPUT : PFVAL - FLOATING POINT NUMBER TO BE CONVERTED.
18 !*
19 !* OUTPUT : KEXP - 8 BIT SIGNED EXPONENT
20 !* KMANT - 24 BIT MANTISSA
21 !* PFVAL - UNCHANGED
22 !* PNFVAL - "EXACT" VALUE REPRESENTED BY (KEXP,KMANT).
23 !*
24 !* Jean CLOCHARD , French DMN, January 1990.
25 !* Nearly rewritten from CONFP subroutine from John HENNESSY, ECMWF.
26 !*
27 !* Overflowing values are truncated, with a message on the listing.
28 !*
29 !********************************************************************
30 !
31  IMPLICIT NONE
32 !
33  INTEGER (KIND=JPLIKM) :: kexp
34  INTEGER (KIND=JPLIKM) :: KMANT
35 !
36  REAL (KIND=JPDBLD) :: PFVAL
37  REAL (KIND=JPDBLD) :: PNFVAL
38 !
39  INTEGER (KIND=JPLIKM) :: IAUXIL, IEXP
40 !
41  REAL (KIND=JPDBLD) :: ZEPS, ZREF, ZC16, ZMANT
42 !
43  LOGICAL :: LLPOSI
44 !
45 !RJ INTRINSIC LOG, ABS, MIN, MAX, INT
46 !
47  SAVE zeps, zc16
48  DATA zeps / 1.e-12_jpdbld /
49  DATA zc16 / 16.0_jpdbld /
50 !
51 !
52 !
53 ! Elimination of sign.
54 !
55  REAL(KIND=JPRB) :: ZHOOK_HANDLE
56  IF (lhook) CALL dr_hook('CONFI',0,zhook_handle)
57  llposi=pfval.GE.0.0_jpdbld
58  zref = abs(pfval)
59 !
60 !
61 !
62 !
63 ! EXPONENT
64 !
65  IF (zref.EQ.0.0_jpdbld) THEN
66  kexp = 0
67  ELSE
68  iexp = int((log(zref)/log(16.0_jpdbld) + 65.0_jpdbld) +zeps)
69  kexp= max(0, min(127,iexp))
70  ENDIF
71 !
72 !
73 !
74 !
75 !
76 !
77 ! MANTISSA
78 !
79  201 CONTINUE
80 !
81  zmant = zref/(zc16**(kexp-70))
82 !
83  IF (llposi) THEN
84  kmant=int(zmant)
85  ELSE
86 !
87 ! Special case for negative values... because the "INT" function
88 ! is not equivalent to the "integer part" mathematical function
89 ! for this range of values.
90 !
91  iauxil=2+int(zmant)
92  kmant=iauxil - int( -zmant + REAL (IAUXIL,JPDBLD) )
93  ENDIF
94 !
95  IF (kmant.GE.2**24) THEN
96 !
97  IF (kexp.LT.127) THEN
98 !
99 ! Some rounding error ocurred in the computation of KEXP, and could
100 ! not be compensated by ZEPS. Incrementation of KEXP, and new value
101 ! of KMANT computed.
102 !
103  kexp=kexp+1
104  GOTO 201
105  ELSE
106  print *, &
107  &'*/*/* OVERFLOW OF GRIB FLOATING-POINT REPRESENTATION WITH ',pfval
108  kmant=2**24-1
109  ENDIF
110 !
111  ENDIF
112 !
113 ! COMPUTE "EXACT" VALUE REPRESENTED, AND ADD SIGN BIT TO EXPONENT.
114 !
115  IF (llposi) THEN
116  pnfval = kmant*(zc16**(kexp-70))
117  ELSE
118  pnfval = -kmant*(zc16**(kexp-70))
119  kexp=kexp+128
120  ENDIF
121 !
122 !
123  IF (lhook) CALL dr_hook('CONFI',1,zhook_handle)
124  ENDSUBROUTINE confi
subroutine confi(PFVAL, KEXP, KMANT, PNFVAL)
Definition: confi.F:2
integer, parameter jprb
Definition: parkind1.F90:32
logical lhook
Definition: yomhook.F90:15