FV3 Bundle
gsw_mod_error_functions.f90
Go to the documentation of this file.
1 !==========================================================================
3 !==========================================================================
4 
6 
7 implicit none
8 
9 logical, public :: gsw_error_check = .true.
10 logical, public :: gsw_abort_on_error = .true.
11 
12 real (r8), parameter, public :: gsw_error_limit = 1e10_r8
13 
14 integer, private :: nfuncs = 41
15 
16 integer, parameter, private :: maxlen = 40
17 integer, parameter, private :: maxfuncs = 50
18 character (len=maxlen), dimension(maxfuncs), private :: func_list
19 
20 data func_list / &
21  "gsw_ct_from_enthalpy_exact", &
22  "gsw_ct_from_enthalpy", &
23  "gsw_ct_from_rho", &
24  "gsw_deltasa_atlas", &
25  "gsw_deltasa_from_sp", &
26  "gsw_fdelta", &
27  "gsw_frazil_properties", &
28  "gsw_frazil_properties_potential", &
29  "gsw_frazil_properties_potential_poly", &
30  "gsw_geo_strf_dyn_height", &
31  "gsw_geo_strf_dyn_height_pc", &
32  "gsw_gibbs", &
33  "gsw_gibbs_ice", &
34  "gsw_ice_fraction_to_freeze_seawater", &
35  "gsw_ipv_vs_fnsquared_ratio", &
36  "gsw_melting_ice_into_seawater", &
37  "gsw_melting_ice_sa_ct_ratio", &
38  "gsw_melting_ice_sa_ct_ratio_poly", &
39  "gsw_melting_seaice_into_seawater", &
40  "gsw_melting_seaice_sa_ct_ratio", &
41  "gsw_melting_seaice_sa_ct_ratio_poly", &
42  "gsw_mlp", &
43  "gsw_nsquared", &
44  "gsw_nsquared_min", &
45  "gsw_pressure_freezing_ct", &
46  "gsw_rr68_interp_sa_ct", &
47  "gsw_saar", &
48  "gsw_sa_freezing_from_ct", &
49  "gsw_sa_freezing_from_ct_poly", &
50  "gsw_sa_freezing_from_t", &
51  "gsw_sa_freezing_from_t_poly", &
52  "gsw_sa_from_rho", &
53  "gsw_sa_from_sp", &
54  "gsw_sa_from_sstar", &
55  "gsw_seaice_fraction_to_freeze_seawater", &
56  "gsw_sp_from_c", &
57  "gsw_sp_from_sa", &
58  "gsw_sp_from_sstar", &
59  "gsw_sstar_from_sa", &
60  "gsw_sstar_from_sp", &
61  "gsw_turner_rsubrho", &
62  "", &
63  "", &
64  "", &
65  "", &
66  "", &
67  "", &
68  "", &
69  "", &
70  "" /
71 
72 public :: gsw_error_code
73 public :: gsw_error_handler
74 public :: gsw_error_addname
75 
76 private :: gsw_error_fnum
77 
78 contains
79 
80  elemental function gsw_error_code (err_num, func_name, error_code)
81 
82  ! Constructs an error code of the form 9.nabcxyz000000d15
83  !
84  ! where n = current error level (1-4)
85  ! abc = error code for level #1
86  ! xyz = error code for level #2
87  ! ...
88  ! and level error codes comprise ...
89  ! a = error number for level #1 (0-9)
90  ! bc = function number for level #1
91 
92  implicit none
93 
94  integer, intent(in) :: err_num
95  character (*), intent(in) :: func_name
96  real (r8), intent(in), optional :: error_code
97 
98  integer :: ival, k
99  real (r8) :: gsw_error_code, base_code, mult
100 
101  if (present(error_code)) then
102  k = int(error_code/1.0e14_r8) - 90
103  base_code = error_code + 1.0e14_r8
104  mult = 10.0_r8**(11-k*3)
105  else
106  base_code = 9.1e15_r8
107  mult = 1.0e11_r8
108  end if
109 
110  ival = err_num*100 + gsw_error_fnum(func_name)
111  gsw_error_code = base_code + ival*mult
112 
113  end function gsw_error_code
114 
115  !==========================================================================
116 
117  elemental function gsw_error_fnum (func_name)
119  implicit none
120 
121  character (*), intent(in) :: func_name
122 
123  integer :: gsw_error_fnum
124 
125  integer :: i
126  character (len=maxlen) :: fname
127 
128  fname = func_name
129  do i = 1, nfuncs
130  if (fname == func_list(i)) goto 100
131  end do
132  gsw_error_fnum = 99
133  return
134 
135 100 gsw_error_fnum = i
136  return
137 
138  end function gsw_error_fnum
139 
140  !==========================================================================
141 
142  subroutine gsw_error_handler (error_code)
144  implicit none
145 
146  real (r8), intent(in) :: error_code
147 
148  integer, parameter :: i8 = selected_int_kind(14)
149 
150  integer (i8) :: base_code
151 
152  integer :: func_num, ival, i, k
153 
154  character (len=maxlen) :: func_name
155 
156  print '(/"Trace for error code: ", es20.13/)', error_code
157 
158  base_code = int(error_code - 9.0e15_r8, i8)
159  k = int(base_code/1.0e14_r8)
160  base_code = base_code/(10**(14-k*3))
161 
162  do i = 1, k
163  ival = int(mod(base_code,1000))
164  func_num = mod(ival,100)
165  if (func_num .le. nfuncs) then
166  func_name = func_list(func_num)
167  else
168  func_name = "unknown"
169  end if
170  print '(" Code: ",i1," in function: ",a)', ival/100, func_name
171  base_code = base_code/1000
172  end do
173 
174  if (gsw_abort_on_error) stop
175 
176  end subroutine gsw_error_handler
177 
178  !==========================================================================
179 
180  subroutine gsw_error_addname (func_name)
182  implicit none
183 
184  character (*), intent(in) :: func_name
185 
186  if (nfuncs.ge.maxfuncs) return
187 
188  nfuncs = nfuncs + 1
189  func_list(nfuncs) = func_name
190  return
191 
192  end subroutine gsw_error_addname
193 
194 end module gsw_mod_error_functions
integer, parameter, private maxfuncs
elemental integer function, private gsw_error_fnum(func_name)
integer, parameter, private maxlen
real(r8), parameter, public gsw_error_limit
subroutine, public gsw_error_addname(func_name)
character(len=maxlen), dimension(maxfuncs), private func_list
subroutine, public gsw_error_handler(error_code)
void mult(long double m[], long double v[], long double out_v[])
Definition: mosaic_util.c:768
elemental real(r8) function, public gsw_error_code(err_num, func_name, error_code)